Changeset 75 in dev for BasicCompiler_Common/Variable.cpp
- Timestamp:
- Mar 20, 2007, 4:36:16 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
BasicCompiler_Common/Variable.cpp
r73 r75 1 #include "../BasicCompiler_Common/common.h"2 3 #ifdef _AMD64_4 #include "../BasicCompiler64/opcode.h"5 #else6 #include "../BasicCompiler32/opcode.h"7 #endif8 9 BOOL IsVariableTopChar(char c){10 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||c=='_') return 1;11 return 0;12 }13 BOOL IsVariableChar(char c){14 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||15 c=='%'||c=='!'||c=='#'||c=='$'||16 c=='_'||c=='.') return 1;17 return 0;18 }19 BOOL IsPtrType(int type){20 if(type==-1) return 0;21 22 if(PTR_LEVEL(type)||type==DEF_PTR_VOID||type==DEF_PTR_PROC||23 (type&FLAG_PTR) ) return 1;24 25 return 0;26 }27 BOOL IsSignedType(int type){28 switch(type){29 case DEF_SBYTE:30 case DEF_INTEGER:31 case DEF_LONG:32 case DEF_INT64:33 case DEF_SINGLE:34 case DEF_DOUBLE:35 case DEF_CHAR:36 return 1;37 default:38 break;39 }40 return 0;41 }42 BOOL IsNaturalWholeNumberType(int type){43 switch(type){44 case DEF_SBYTE:45 case DEF_BYTE:46 case DEF_INTEGER:47 case DEF_WORD:48 case DEF_LONG:49 case DEF_DWORD:50 case DEF_INT64:51 case DEF_QWORD:52 case DEF_CHAR:53 return 1;54 default:55 break;56 }57 return 0;58 }59 BOOL IsWholeNumberType(int type){60 return (61 IsNaturalWholeNumberType(type)62 || IsPtrType(type)63 || type == DEF_BOOLEAN64 );65 }66 BOOL IsRealNumberType(int type){67 switch(type){68 case DEF_DOUBLE:69 case DEF_SINGLE:70 return 1;71 default:72 break;73 }74 return 0;75 }76 BOOL Is64Type(int type){77 switch(type){78 case DEF_INT64:79 case DEF_QWORD:80 return 1;81 default:82 break;83 }84 #ifdef _AMD64_85 return IsPtrType(type);86 #else87 return 0;88 #endif89 }90 int GetSignedType(int type){91 switch(type){92 case DEF_BYTE:93 return DEF_SBYTE;94 case DEF_WORD:95 return DEF_INTEGER;96 case DEF_DWORD:97 return DEF_LONG;98 case DEF_QWORD:99 return DEF_INT64;100 default:101 break;102 }103 #ifdef _AMD64_104 if(IsPtrType(type)) return DEF_INT64;105 #else106 if(IsPtrType(type)) return DEF_LONG;107 #endif108 return type;109 }110 int GetUnsignedType(int type){111 switch(type){112 case DEF_SBYTE:113 return DEF_BYTE;114 case DEF_INTEGER:115 return DEF_WORD;116 case DEF_LONG:117 return DEF_DWORD;118 case DEF_INT64:119 return DEF_QWORD;120 case DEF_CHAR:121 if( isUnicode ) return DEF_WORD;122 return DEF_BYTE;123 }124 return type;125 }126 int GetTypeSize(int type,LONG_PTR lpIndex){127 if(type==DEF_LONG){128 if(lpIndex==LITERAL_NULL||lpIndex==LITERAL_M128_0||lpIndex==LITERAL_0_255)129 return sizeof(BYTE);130 else if(lpIndex==LITERAL_M32768_0||lpIndex==LITERAL_0_65535)131 return sizeof(WORD);132 133 return sizeof(DWORD);134 }135 136 //整数型137 if(type==DEF_INT64||type==DEF_QWORD)138 return sizeof(_int64);139 else if(type==DEF_LONG||type==DEF_DWORD)140 return sizeof(DWORD);141 else if(type==DEF_INTEGER||type==DEF_WORD)142 return sizeof(WORD);143 else if(type==DEF_SBYTE||type==DEF_BYTE || type == DEF_BOOLEAN)144 return sizeof(BYTE);145 146 //実数型147 else if(type==DEF_DOUBLE) return sizeof(double);148 else if(type==DEF_SINGLE) return sizeof(float);149 150 //文字型151 else if( type == DEF_CHAR ){152 if( isUnicode ) return sizeof( WORD );153 return sizeof( BYTE );154 }155 156 //ポインタ型157 else if(IsPtrType(type)) return PTR_SIZE;158 159 else if( type == DEF_STRUCT ){160 CClass *pobj_c;161 pobj_c=(CClass *)lpIndex;162 if(!pobj_c){163 SetError(300,NULL,cp);164 return 0;165 }166 167 return pobj_c->GetSize();168 }169 170 else if(type==DEF_OBJECT){171 return PTR_SIZE;172 }173 else{174 SetError(300,NULL,cp);175 }176 return 0;177 }178 int GetPtrType(int type,LONG_PTR lpIndex){179 return MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)+1);180 }181 int GetTypeFixed(const char *TypeName,LONG_PTR *lpNum){182 int type;183 184 if(lpNum) *lpNum=-1;185 186 if(TypeName[0]=='*'){187 if(TypeName[1]==1&&(TypeName[2]==ESC_FUNCTION||TypeName[2]==ESC_SUB)){188 //関数ポインタ(*Function)189 return DEF_PTR_PROC;190 }191 192 type=GetTypeFixed(TypeName+1,lpNum);193 if(type==-1) return -1;194 195 return GetPtrType(type,*lpNum);196 }197 198 //整数型199 if(lstrcmpi(TypeName,"SByte")==0) return DEF_SBYTE;200 else if(lstrcmpi(TypeName,"Byte")==0) return DEF_BYTE;201 else if(lstrcmpi(TypeName,"Integer")==0) return DEF_INTEGER;202 else if(lstrcmpi(TypeName,"WORD")==0) return DEF_WORD;203 else if(lstrcmpi(TypeName,"Long")==0) return DEF_LONG;204 else if(lstrcmpi(TypeName,"DWORD")==0) return DEF_DWORD;205 else if(lstrcmpi(TypeName,"Int64")==0) return DEF_INT64;206 else if(lstrcmpi(TypeName,"QWORD")==0) return DEF_QWORD;207 208 //実数型209 else if(lstrcmpi(TypeName,"Single")==0) return DEF_SINGLE;210 else if(lstrcmpi(TypeName,"Double")==0) return DEF_DOUBLE;211 212 //文字型213 //else if( lstrcmpi( TypeName, "Char" ) == 0 ) return DEF_CHAR;214 215 //bool型216 else if( lstrcmpi( TypeName, "Boolean" ) == 0 ) return DEF_BOOLEAN;217 218 //ポインタ型219 //※VoidPtr型以外のポインタ型はTypeDef宣言により定義される220 else if(lstrcmpi(TypeName,"VoidPtr")==0) return DEF_PTR_VOID;221 222 //その他223 else if(lstrcmpi(TypeName,"Any")==0) return DEF_ANY;224 225 226 ////////////////////227 // TypeDefされた型228 ////////////////////229 int i;230 i=pobj_DBTypeDef->check(TypeName);231 if(i!=-1){232 return GetTypeFixed(pobj_DBTypeDef->ppobj_TypeDef[i]->lpszBaseName,lpNum);233 }234 235 if(!lpNum) return -1;236 237 //クラス238 CClass *pobj_c;239 pobj_c=pobj_DBClass->check(TypeName);240 if(pobj_c){241 *lpNum=(LONG_PTR)pobj_c;242 243 if( pobj_c->IsStructure() ){244 return DEF_STRUCT;245 }246 else{247 return DEF_OBJECT;248 }249 }250 251 return -1;252 }253 void GetOriginalTypeName(char *buffer){254 // TypeDefされた型に対して、オリジナルの型の名前に変更する255 256 if(buffer[0]=='*'){257 if(buffer[1]==1&&(buffer[2]==ESC_FUNCTION||buffer[2]==ESC_SUB)) return;258 259 GetOriginalTypeName(buffer+1);260 return;261 }262 263 int i;264 i=pobj_DBTypeDef->check(buffer);265 if(i!=-1){266 lstrcpy(buffer,pobj_DBTypeDef->ppobj_TypeDef[i]->lpszBaseName);267 GetOriginalTypeName(buffer);268 }269 }270 BOOL GetTypeName(int type,LONG_PTR lpIndex,char *name){271 if(PTR_LEVEL(type)){272 //ポインタ型273 name[0]='*';274 return GetTypeName(MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)-1),lpIndex,name+1);275 }276 277 //整数型278 if(type==DEF_SBYTE) lstrcpy(name,"SByte");279 else if(type==DEF_BYTE) lstrcpy(name,"Byte");280 else if(type==DEF_INTEGER) lstrcpy(name,"Integer");281 else if(type==DEF_WORD) lstrcpy(name,"Word");282 else if(type==DEF_LONG) lstrcpy(name,"Long");283 else if(type==DEF_DWORD) lstrcpy(name,"DWord");284 else if(type==DEF_INT64) lstrcpy(name,"Int64");285 else if(type==DEF_QWORD) lstrcpy(name,"QWord");286 287 //実数型288 else if(type==DEF_SINGLE) lstrcpy(name,"Single");289 else if(type==DEF_DOUBLE) lstrcpy(name,"Double");290 291 //文字型292 //else if(type==DEF_CHAR) lstrcpy(name,"Char");293 294 //bool型295 else if(type==DEF_BOOLEAN) lstrcpy(name,"Boolean");296 297 //オブジェクト298 else if(type==DEF_OBJECT || type==DEF_STRUCT){299 if(lpIndex==0) lstrcpy(name,"non");300 else{301 lstrcpy(name,((CClass *)lpIndex)->name);302 }303 }304 305 //ポインタ型306 else if(type==DEF_PTR_VOID) lstrcpy(name,"VoidPtr");307 308 else if(type==DEF_PTR_PROC){309 if(lpIndex==-1) lstrcpy(name,"VoidPtr");310 else{311 extern PROCPTRINFO *pProcPtrInfo;312 if(pProcPtrInfo[lpIndex].ReturnType==DEF_NON)313 lstrcpy(name,"*Sub");314 else lstrcpy(name,"*Function");315 }316 }317 318 else{319 extern int cp;320 SetError(1,NULL,cp);321 return 0;322 }323 return 1;324 }325 326 TYPEINFO GetStringTypeInfo(){327 extern CClass *pobj_StringClass;328 TYPEINFO TypeInfo = { DEF_OBJECT, (LONG_PTR)pobj_StringClass };329 return TypeInfo;330 }331 332 bool FormatUseProcReturnObject( const char *term, char *procName, char *parameter, CClass::RefType &refType, char *member ){333 int p1 = 0, p2 = 0;334 335 for( int i=0; term[i]!='\0' ; i++ ){336 337 if( term[i] == '[' ){338 i = JumpStringInBracket( term, i + 1 );339 if( term[i] == '\0' ) break;340 continue;341 }342 if( term[i] == '(' ){343 int temp_p = i;344 i = JumpStringInPare( term, i + 1 ) + 1;345 if( term[i] == '\0' ) break;346 if( term[i] == '.'347 || term[i] == 1 && term[i] == ESC_PSMEM ){348 p1 = temp_p;349 p2 = i;350 }351 continue;352 }353 }354 if( !p1 ) return false;355 356 //メソッド名357 memcpy( procName, term, p1 );358 procName[p1] = 0;359 360 //パラメータ361 memcpy( parameter, term + p1 + 1, p2 - p1 - 2 );362 parameter[ p2 - p1 - 2 ] = 0;363 364 //参照タイプ365 if( term[p2] == '.' ){366 refType = CClass::Dot;367 }368 else{369 refType = CClass::Pointer;370 p2++;371 }372 373 //メンバ374 lstrcpy( member, term + p2 + 1 );375 376 return true;377 }378 379 BOOL GetVarFormatString(char *buffer,char *array,char *array2,char *NestMember,CClass::RefType &refType){380 extern int cp;381 int i,i2,i3;382 char cPare_Open,cPare_Close;383 384 array[0]=0;385 array2[0]=0;386 NestMember[0]=0;387 for(i=0;;i++){388 if(buffer[i]=='\"'){389 for(i++;;i++){390 if(IsDBCSLeadByte(buffer[i])){391 i++;392 continue;393 }394 if(buffer[i]=='\"') break;395 }396 }397 if(buffer[i]=='['||buffer[i]=='('){398 if(buffer[i]=='['){399 cPare_Open='[';400 cPare_Close=']';401 }402 else{403 cPare_Open='(';404 cPare_Close=')';405 }406 buffer[i]=0;407 for(i++,i2=0;;i++,i2++){408 if(buffer[i]==cPare_Open){409 if(cPare_Open=='[') i3=GetStringInBracket(array+i2,buffer+i);410 else i3=GetStringInPare(array+i2,buffer+i);411 i+=i3-1;412 i2+=i3-1;413 continue;414 }415 if(buffer[i]==cPare_Close){416 array[i2]=0;417 break;418 }419 array[i2]=buffer[i];420 }421 if(buffer[i+1]==cPare_Open){422 for(i+=2,i2=0;;i++,i2++){423 if(buffer[i]==cPare_Open){424 if(cPare_Open=='[') i3=GetStringInBracket(array2+i2,buffer+i);425 else i3=GetStringInPare(array2+i2,buffer+i);426 i+=i3-1;427 i2+=i3-1;428 continue;429 }430 if(buffer[i]==cPare_Close){431 array2[i2]=0;432 break;433 }434 array2[i2]=buffer[i];435 }436 if(buffer[i+1]==cPare_Open){437 SetError(14,buffer,cp);438 return 0;439 }440 }441 continue;442 }443 if(buffer[i]=='.'){444 lstrcpy(NestMember,buffer+i+1);445 refType = CClass::Dot;446 buffer[i]=0;447 break;448 }449 if(buffer[i]==1&&buffer[i+1]==ESC_PSMEM){450 lstrcpy(NestMember,buffer+i+2);451 refType = CClass::Pointer;452 buffer[i]=0;453 break;454 }455 if(buffer[i]=='\0') break;456 }457 return 1;458 }459 460 void GetArrayElement(char *buffer,char *variable,char *array_element){461 array_element[0]=0;462 463 if(buffer[lstrlen(buffer)-1]!=']'){464 lstrcpy(variable,buffer);465 return;466 }467 468 int i,i2;469 for(i=0;;i++){470 if(buffer[i]=='\0') break;471 if(buffer[i]=='['){472 i2=GetStringInBracket(array_element,buffer+i);473 i+=i2-1;474 continue;475 }476 }477 478 lstrcpy(variable,buffer);479 variable[lstrlen(variable)-lstrlen(array_element)]=0;480 481 RemoveStringBracket(array_element);482 }483 484 BOOL CheckVarNameError(char *name,int NowLine){485 int i2;486 487 if(!IsVariableTopChar(name[0])){488 SetError(1,NULL,NowLine);489 return 0;490 }491 for(i2=1;;i2++){492 if(name[i2]=='\0') break;493 if(!IsVariableChar(name[i2])){494 SetError(1,NULL,NowLine);495 return 0;496 }497 }498 return 1;499 }500 501 int JumpSubScripts(int *ss){502 //DIMで定義された並んだ配列の数だけアドレスを進める503 int i,i2;504 for(i=0,i2=1;i<255;i++){505 if(ss[i]==-1) break;506 i2*=ss[i]+1;507 }508 return i2;509 }510 void GetArrange(char *variable,char *variAnswer,int *SubScripts){511 extern int cp;512 int i,i2,i3,i4;513 double dbl;514 _int64 i64data;515 BOOL bBracket;516 char temporary[VN_SIZE];517 518 for(i=0;;i++){519 if(variable[i]=='('||variable[i]=='['){520 if(variable[i]=='[') bBracket=1;521 else bBracket=0;522 523 variAnswer[i]=0;524 for(i++,i2=0,i3=0;;i++,i2++){525 if(variable[i]==','){526 temporary[i2]=0;527 528 i2=StaticCalculation(true, temporary,0,&i64data,0);529 if(IsRealNumberType(i2)){530 memcpy(&dbl,&i64data,sizeof(double));531 i64data=(_int64)dbl;532 }533 534 if(i64data<0){535 //error536 SubScripts[i3]=0;537 }538 else SubScripts[i3]=(int)i64data;539 i3++;540 i2=-1;541 continue;542 }543 if(variable[i]=='('){544 i4=GetStringInPare(temporary+i2,variable+i);545 i2+=i4-1;546 i+=i4-1;547 continue;548 }549 if(variable[i]=='['){550 i4=GetStringInBracket(temporary+i2,variable+i);551 i2+=i4-1;552 i+=i4-1;553 continue;554 }555 if(variable[i]==')'&&bBracket==0||556 variable[i]==']'&&bBracket){557 temporary[i2]=0;558 if(i2==0){559 SubScripts[i3]=-2;560 break;561 }562 563 i2=StaticCalculation(true, temporary,0,&i64data,0);564 if(IsRealNumberType(i2)){565 memcpy(&dbl,&i64data,sizeof(double));566 i64data=(_int64)dbl;567 }568 569 if(i64data<0){570 //error571 SubScripts[i3]=0;572 }573 else SubScripts[i3]=(int)i64data;574 SubScripts[i3+1]=-1;575 break;576 }577 if(variable[i]=='\"'){578 SetError(1,NULL,cp);579 return;580 }581 temporary[i2]=variable[i];582 }583 break;584 }585 variAnswer[i]=variable[i];586 if(variable[i]=='\0'){587 SubScripts[0]=-1;588 break;589 }590 }591 }592 593 int GetTypeFromSimpleName(char *variable){594 extern char DefIntVari[26],DefSngVari[26],DefStrVari[26],divNum,dsvNum,dStrvNum;595 int i;596 char name[VN_SIZE];597 598 //構造体メンバの場合を考慮599 for(i=lstrlen(variable);i>0;i--){600 if(variable[i]=='.'){601 i++;602 break;603 }604 }605 606 for(;;i++){607 if(variable[i]=='('||variable[i]=='\0'){608 name[i]=0;609 break;610 }611 name[i]=variable[i];612 }613 //変数名から選択614 i--;615 if(name[i]=='#') return DEF_DOUBLE;616 if(name[i]=='!') return DEF_SINGLE;617 if(name[i]=='%') return DEF_INTEGER;618 return DEF_DOUBLE;619 }620 621 622 int GetMemberType(CClass *pobj_c,char *member,LONG_PTR *plpNestIndex,BOOL bPrivateAccess,BOOL bError){623 extern int cp;624 int i;625 626 //クラス、配列の構成要素を解析する627 char VarName[VN_SIZE]; //変数名628 char array[VN_SIZE]; //第1次配列629 char lpPtrOffset[VN_SIZE]; //第2次配列630 char NestMember[VN_SIZE]; //入れ子メンバ631 CClass::RefType refType = CClass::Non;632 lstrcpy(VarName,member);633 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember,refType)) return 0;634 635 for(i=0;i<pobj_c->iMemberNum;i++){636 if(lstrcmp(pobj_c->ppobj_Member[i]->name,VarName)==0) break;637 }638 if(i==pobj_c->iMemberNum){639 if(bError) SetError(103,VarName,cp);640 return -1;641 }642 643 //アクセシビリティをチェック644 if(pobj_c==pobj_CompilingClass){645 //同一クラスオブジェクトの場合はプライベートアクセスを容認する646 if(pobj_c->ppobj_Member[i]->dwAccess==ACCESS_NON){647 if(bError) SetError(107,VarName,cp);648 return -1;649 }650 }651 else{652 if((bPrivateAccess==0&&pobj_c->ppobj_Member[i]->dwAccess==ACCESS_PRIVATE)||653 pobj_c->ppobj_Member[i]->dwAccess==ACCESS_NON){654 if(bError) SetError(107,VarName,cp);655 return -1;656 }657 else if(bPrivateAccess==0&&pobj_c->ppobj_Member[i]->dwAccess==ACCESS_PROTECTED){658 if(bError) SetError(108,VarName,cp);659 return -1;660 }661 }662 663 int type;664 type=pobj_c->ppobj_Member[i]->TypeInfo.type;665 *plpNestIndex=pobj_c->ppobj_Member[i]->TypeInfo.u.lpIndex;666 667 //ポインタ変数の場合668 if(IsPtrType(type)){669 if(pobj_c->ppobj_Member[i]->SubScripts[0]==-1){670 lstrcpy(lpPtrOffset,array);671 array[0]=0;672 }673 }674 else{675 if(lpPtrOffset[0]){676 if(bError) SetError(16,member,cp);677 return 0;678 }679 }680 681 if( refType != CClass::Non ){682 //入れ子構造の場合683 684 return GetMemberType(pobj_c->ppobj_Member[i]->TypeInfo.u.pobj_Class,685 NestMember,686 plpNestIndex,687 0,688 bError);689 }690 691 if(array[0]==0&&pobj_c->ppobj_Member[i]->SubScripts[0]!=-1){692 return type|FLAG_PTR;693 }694 695 if(lpPtrOffset[0]){696 if(PTR_LEVEL(type)){697 type=MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)-1);698 }699 else{700 //エラー701 if(bError) SetError(1,NULL,cp);702 return -1;703 }704 }705 706 return type;707 }708 int GetVarType(const char *NameBuffer,LONG_PTR *plpIndex,BOOL bError){709 extern BOOL bCompilingGlobal;710 int i;711 LONG_PTR lpIndex;712 char variable[VN_SIZE],member[VN_SIZE],VarName[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE];713 714 if(NameBuffer[0]=='.'){715 GetWithName(variable);716 lstrcat(variable,NameBuffer);717 }718 else lstrcpy(variable,NameBuffer);719 720 721 CClass::RefType refType;722 if( FormatUseProcReturnObject( variable, VarName, array, refType, member ) ){723 // 戻り値オブジェクトのメンバを直接参照しているとき724 //例: func().member725 726 void *pInfo;727 int idProc=GetProc(VarName,&pInfo);728 729 if(idProc){730 731 int type=GetReturnTypeOfProc(idProc,pInfo,VarName,array,&lpIndex);732 733 return GetMemberType((CClass *)lpIndex,member,plpIndex,1,bError);734 }735 }736 737 738 739 lstrcpy(VarName,variable);740 GetVarFormatString(VarName,array,lpPtrOffset,member,refType);741 742 int *pSubScripts;743 int type;744 745 extern int MaxGlobalVarNum;746 extern VARIABLE *GlobalVar;747 extern int MaxLocalVarNum;748 extern VARIABLE *LocalVar;749 750 if(bCompilingGlobal==0){751 /////////////////752 // ローカル変数753 /////////////////754 755 for(i=MaxLocalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ756 if( LocalVar[i].bLiving //現在のスコープで有効なもの757 && LocalVar[i].ScopeLevel <= obj_LexScopes.GetNowLevel() //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮)758 ){759 if(lstrcmp(VarName,LocalVar[i].name)==0) break;760 }761 }762 if(i>=0){763 //ポインタ変数の場合764 if(IsPtrType(LocalVar[i].type)){765 if(LocalVar[i].SubScripts[0]==-1){766 lstrcpy(lpPtrOffset,array);767 array[0]=0;768 }769 }770 else{771 if(lpPtrOffset[0]){772 if(bError) SetError(16,variable,cp);773 return -1;774 }775 }776 777 type=LocalVar[i].type;778 lpIndex=LocalVar[i].u.index;779 pSubScripts=LocalVar[i].SubScripts;780 781 goto ok;782 }783 }784 785 if(pobj_CompilingClass){786 ///////////////////////787 // クラスメンバの参照788 ///////////////////////789 790 if(lstrcmpi(variable,"This")==0){791 //Thisオブジェクト792 *plpIndex=(LONG_PTR)pobj_CompilingClass;793 return DEF_OBJECT;794 }795 796 if(_memicmp(variable,"This.",5)==0){797 //Thisオブジェクトのメンバを参照するとき798 SlideString(variable+5,-5);799 lstrcpy(VarName,variable);800 }801 else{802 //クラス内メンバを参照するとき(通常)803 804 for(i=0;i<pobj_CompilingClass->iMemberNum;i++){805 if(lstrcmp(VarName,pobj_CompilingClass->ppobj_Member[i]->name)==0) break;806 }807 if(i==pobj_CompilingClass->iMemberNum) goto NonClassMember;808 }809 810 return GetMemberType(pobj_CompilingClass,variable,plpIndex,1,bError);811 }812 813 NonClassMember:814 815 //////////////////////////816 // 静的ローカル変数817 // ※"Static.Object.Method.Variable"818 //////////////////////////819 820 char temporary[VN_SIZE];821 extern SubInfo *pCompilingSubInfo;822 if(pCompilingSubInfo){823 GetNowStaticVarFullName(VarName,temporary);824 825 for(i=0;i<MaxGlobalVarNum;i++){826 if(lstrcmp(temporary,GlobalVar[i].name)==0) break;827 }828 if(i!=MaxGlobalVarNum){829 goto GlobalOk;830 }831 }832 833 834 //////////////////////////835 // クラスの静的メンバ836 //////////////////////////837 838 if(member[0]){839 lstrcpy(temporary,member);840 char tempMember[VN_SIZE];841 char tempArray[VN_SIZE];842 {843 CClass::RefType refType;844 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember,refType);845 }846 847 char temp2[VN_SIZE];848 sprintf(temp2,"%s.%s",VarName,temporary);849 for(i=0;i<MaxGlobalVarNum;i++){850 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;851 }852 853 if(i!=MaxGlobalVarNum){854 lstrcpy(member,tempMember);855 lstrcpy(array,tempArray);856 goto GlobalOk;857 }858 }859 860 if(pobj_CompilingClass){861 //自身のクラスから静的メンバを参照する場合862 char temp2[VN_SIZE];863 sprintf(temp2,"%s.%s",pobj_CompilingClass->name,VarName);864 for(i=0;i<MaxGlobalVarNum;i++){865 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;866 }867 868 if(i!=MaxGlobalVarNum){869 goto GlobalOk;870 }871 }872 873 874 ////////////////////875 // グローバル変数876 ////////////////////877 878 for(i=MaxGlobalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ879 if( GlobalVar[i].bLiving //現在のスコープで有効なもの880 && GlobalVar[i].ScopeLevel <= obj_LexScopes.GetNowLevel() //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮)881 ){882 if(lstrcmp(VarName,GlobalVar[i].name)==0) break;883 }884 }885 886 if(i>=0){887 goto GlobalOk;888 }889 890 //変数として見つからなかったとき891 if(bError) SetError(3,variable,cp);892 return -1;893 894 GlobalOk:895 896 //ポインタ変数の場合897 if(IsPtrType(GlobalVar[i].type)){898 if(GlobalVar[i].SubScripts[0]==-1){899 lstrcpy(lpPtrOffset,array);900 array[0]=0;901 }902 }903 else{904 if(lpPtrOffset[0]){905 if(bError) SetError(16,variable,cp);906 return -1;907 }908 }909 910 type=GlobalVar[i].type;911 lpIndex=GlobalVar[i].u.index;912 pSubScripts=GlobalVar[i].SubScripts;913 914 ok:915 916 if(member[0]){917 if( NATURAL_TYPE(type)==DEF_OBJECT918 || NATURAL_TYPE(type)==DEF_STRUCT){919 return GetMemberType((CClass *)lpIndex,member,plpIndex,0,bError);920 }921 }922 923 if(array[0]==0&&pSubScripts[0]!=-1){924 //配列の先頭ポインタを示す場合925 type|=FLAG_PTR;926 }927 928 if(lpPtrOffset[0]){929 if(PTR_LEVEL(type)){930 type=MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)-1);931 }932 else{933 //エラー934 if(bError) SetError(1,NULL,cp);935 return -1;936 }937 }938 939 *plpIndex=lpIndex;940 return type;941 }942 943 BOOL GetVarOffsetReadOnly(const char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss ){944 //読み取り専用で変数へアクセス945 return GetVarOffset(946 true, //エラー表示有効947 false, //書き込みアクセスは無し948 NameBuffer,949 pType,950 pRelativeVar,951 plpIndex,952 pss);953 }954 BOOL GetVarOffsetReadWrite(const char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss ){955 //読み書き両用で変数へアクセス956 return GetVarOffset(957 true, //エラー表示有効958 true, //書き込みアクセス959 NameBuffer,960 pType,961 pRelativeVar,962 plpIndex,963 pss);964 }965 966 967 968 BOOL GetDimentionFormat(const char *buffer, const bool isRef, char *VarName,int *SubScripts,TYPEINFO *pTypeInfo,char *InitBuf,char *ConstractParameter){969 int i,i2,i3,IsStr;970 char variable[VN_SIZE],temporary[8192];971 972 for(i=0;;i++){973 if((buffer[i]==1&&buffer[i+1]==ESC_AS)||974 buffer[i]=='='||975 buffer[i]=='\0'){976 variable[i]=0;977 break;978 }979 variable[i]=buffer[i];980 }981 982 if(buffer[i]=='='){983 ////////////////////////////////////984 // 初期化データが指定されいるとき985 ////////////////////////////////////986 987 for(i++,i2=0,IsStr=0;;i++,i2++){988 if(buffer[i]=='\"') IsStr^=1;989 if(buffer[i]=='('&&IsStr==0){990 i3=GetStringInPare(InitBuf+i2,buffer+i);991 i+=i3-1;992 i2+=i3-1;993 continue;994 }995 if(buffer[i]=='['&&IsStr==0){996 i3=GetStringInBracket(InitBuf+i2,buffer+i);997 i+=i3-1;998 i2+=i3-1;999 continue;1000 }1001 if((buffer[i]==1&&buffer[i+1]==ESC_AS)||1002 (buffer[i]==','&&IsStr==0)||1003 buffer[i]=='\0'){1004 InitBuf[i2]=0;1005 break;1006 }1007 InitBuf[i2]=buffer[i];1008 }1009 }1010 else{1011 //初期化データなし1012 InitBuf[0]=0;1013 }1014 1015 ConstractParameter[0]=0;1016 if(buffer[i]==1&&buffer[i+1]==ESC_AS){1017 /////////////////////////////1018 // "As ~" による型指定あり1019 /////////////////////////////1020 1021 for(i+=2,i2=0;;i++,i2++){1022 if(buffer[i]==','||buffer[i]=='('||buffer[i]=='\0'){1023 temporary[i2]=0;1024 break;1025 }1026 temporary[i2]=buffer[i];1027 }1028 pTypeInfo->type=GetTypeFixed(temporary,&pTypeInfo->u.lpIndex);1029 if(pTypeInfo->type==-1){1030 SetError(3,temporary,cp);1031 pTypeInfo->type=DEF_LONG;1032 }1033 1034 if(pTypeInfo->type==DEF_PTR_PROC){1035 if(temporary[0]=='*'&&1036 temporary[1]==1&&1037 (temporary[2]==ESC_FUNCTION||temporary[2]==ESC_SUB)){1038 if(buffer[i]!='('){1039 SetError(10,temporary,cp);1040 return 0;1041 }1042 i3=GetStringInPare(temporary+3,buffer+i);1043 i+=i3;1044 i2+=i3;1045 1046 if(temporary[2]==ESC_FUNCTION&&buffer[i]==1&&buffer[i+1]==ESC_AS){1047 temporary[i2++]=buffer[i++];1048 temporary[i2++]=buffer[i++];1049 for(;;i++,i2++){1050 if(!IsVariableChar(buffer[i])){1051 temporary[i2]=0;1052 break;1053 }1054 temporary[i2]=buffer[i];1055 }1056 }1057 }1058 else{1059 //TypeDefをする前のベース型を取得1060 GetOriginalTypeName(temporary);1061 }1062 1063 if(temporary[3]!='('){1064 SetError(10,temporary,cp);1065 return 0;1066 }1067 1068 //関数ポインタ1069 pTypeInfo->u.lpIndex=AddProcPtrInfo(temporary+3,temporary[2]);1070 }1071 1072 if(buffer[i]=='('){1073 //コンストラクタに渡すパラメータを取得1074 i2=GetStringInPare(ConstractParameter,buffer+i);1075 i+=i2;1076 RemoveStringPare(ConstractParameter);1077 1078 if(pTypeInfo->type!=DEF_OBJECT){1079 SetError(112,variable,cp);1080 return 0;1081 }1082 }1083 }1084 else{1085 /////////////////1086 // As指定なし1087 /////////////////1088 1089 if( InitBuf[0] == '\0' ){1090 //As指定も、初期値指定もない場合1091 pTypeInfo->type=GetTypeFromSimpleName(variable);1092 1093 i2=lstrlen(variable)-1;1094 if(i2>=0){1095 if(!(variable[i2]=='#'||variable[i2]=='!'||variable[i2]=='%'||variable[i2]=='$'))1096 SetError(-103,variable,cp);1097 }1098 }1099 else{1100 //初期値の型を判別して自動的に型情報を付加する1101 TYPEINFO BaseType = GetStringTypeInfo();1102 int result = NumOpe_GetType_Old( InitBuf, &BaseType, &pTypeInfo->u.lpIndex );1103 1104 //エラーの場合1105 if( result == -1 ) return 0;1106 1107 pTypeInfo->type = result;1108 }1109 1110 }1111 1112 if( InitBuf[0] != '\0' && ConstractParameter[0] != '\0' ){1113 //初期値とコンストラクタパラメータが同時に呼び出されているとき1114 SetError(132, NULL, cp);1115 }1116 1117 /*1118 TODO: 消す1119 if( pTypeInfo->type == DEF_OBJECT && InitBuf[0] != '\0' && InitBuf[0] != '[' && isRef == false ){1120 if( pTypeInfo->u.pobj_Class->GetCopyConstructorMethod() ){1121 //オブジェクトでコピーコンストラクタが存在するとき1122 lstrcpy( ConstractParameter, InitBuf );1123 InitBuf[0] = 0;1124 }1125 }*/1126 1127 GetArrange(variable,VarName,SubScripts);1128 return 1;1129 }1130 1131 BOOL GetNowStaticVarFullName(char *VarName,char *FullName){1132 extern SubInfo *pCompilingSubInfo;1133 if(!pCompilingSubInfo) return 0;1134 1135 //Static識別1136 lstrcpy(FullName,"Static%");1137 1138 //クラス名1139 if(pobj_CompilingClass){1140 lstrcat(FullName,pobj_CompilingClass->name);1141 lstrcat(FullName,"%");1142 }1143 1144 //関数(またはメソッド)名1145 lstrcat(FullName,pCompilingSubInfo->name);1146 lstrcat(FullName,"%");1147 1148 //ID1149 char temp[255];1150 sprintf(temp,"%x",pCompilingSubInfo->id);1151 lstrcat(FullName,temp);1152 lstrcat(FullName,"%");1153 1154 //変数名1155 lstrcat(FullName,VarName);1156 1157 return 1;1158 }1159 1160 1161 void AddGlobalVariable(bool isRef, char *name,int *SubScripts,TYPEINFO *pTypeInfo,int TypeSize,char *InitBuf,char *ConstractParameter,DWORD dwFlag){1162 /////////////////////////1163 // グローバル変数を追加1164 /////////////////////////1165 extern VARIABLE *GlobalVar;1166 extern int MaxGlobalVarNum;1167 extern int AllInitGlobalVarSize;1168 extern int AllGlobalVarSize;1169 1170 int i2,i3,VarSize;1171 1172 for(i2=0;i2<MaxGlobalVarNum;i2++){1173 if(GlobalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==GlobalVar[i2].ScopeLevel){1174 if(lstrcmp(GlobalVar[i2].name,name)==0){1175 //2重定義のエラー1176 SetError(15,name,cp);1177 return;1178 }1179 }1180 }1181 1182 GlobalVar=(VARIABLE *)HeapReAlloc(hHeap,0,GlobalVar,(MaxGlobalVarNum+1)*sizeof(VARIABLE));1183 VARIABLE *pVar = &GlobalVar[MaxGlobalVarNum];1184 MaxGlobalVarNum++;1185 1186 if( isRef ){1187 //参照型1188 pVar->fRef = REF_VARIABLE;1189 TypeSize = PTR_SIZE;1190 }1191 else pVar->fRef=0;1192 1193 for(i2=1,i3=0;i3<255;i3++){1194 //配列要素数1195 pVar->SubScripts[i3]=SubScripts[i3];1196 1197 if(SubScripts[i3]==-1) break;1198 i2*=SubScripts[i3]+1;1199 }1200 VarSize=TypeSize*i2;1201 if(VarSize%PTR_SIZE) VarSize+=PTR_SIZE-(VarSize%PTR_SIZE);1202 1203 lstrcpy(pVar->name,name);1204 if(dwFlag & DIMFLAG_CONST) pVar->bConst = true;1205 else pVar->bConst = false;1206 if(SubScripts[0]==-1) pVar->bArray=0;1207 else pVar->bArray=1;1208 pVar->type=pTypeInfo->type;1209 pVar->u.index=pTypeInfo->u.lpIndex;1210 1211 //コンストラクタ用パラメータ1212 pVar->ConstractParameter=(char *)HeapAlloc(hHeap,0,lstrlen(ConstractParameter)+1);1213 lstrcpy(pVar->ConstractParameter,ConstractParameter);1214 1215 //レキシカルスコープ1216 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();1217 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();1218 pVar->bLiving=TRUE;1219 1220 //エラー用1221 pVar->source_code_address=cp;1222 1223 //アラインメントを考慮1224 int alignment = 0;1225 if( pVar->type==DEF_STRUCT ){1226 alignment = pVar->u.pobj_c->iAlign;1227 }1228 1229 if(InitBuf[0]||dwFlag==DIMFLAG_INITDEBUGVAR){1230 //初期バッファがあるとき1231 1232 if( alignment ){1233 if( AllInitGlobalVarSize % alignment ){1234 AllInitGlobalVarSize += alignment - (AllInitGlobalVarSize % alignment);1235 }1236 }1237 1238 pVar->offset=AllInitGlobalVarSize;1239 AllInitGlobalVarSize+=VarSize;1240 }1241 else{1242 //初期バッファがないとき1243 1244 if( alignment ){1245 if( AllGlobalVarSize % alignment ){1246 AllGlobalVarSize += alignment - (AllGlobalVarSize % alignment);1247 }1248 }1249 1250 pVar->offset=AllGlobalVarSize | 0x80000000;1251 AllGlobalVarSize+=VarSize;1252 }1253 1254 if(InitBuf[0]){1255 int result = 0;1256 if( pVar->type != DEF_OBJECT ){1257 //初期バッファにデータをセット1258 extern BYTE *initGlobalBuf;1259 initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,1260 HEAP_ZERO_MEMORY,1261 initGlobalBuf,1262 AllInitGlobalVarSize);1263 1264 result = SetInitGlobalData(pVar->offset,1265 pVar->type,1266 pVar->u.index,1267 pVar->SubScripts,1268 InitBuf);1269 }1270 1271 if(!result){1272 //動的な式だった場合は代入演算を行う1273 1274 //初期代入時のみ、書き込みアクセスを許可する1275 bool bConstBack = pVar->bConst;1276 pVar->bConst = false;1277 1278 //代入1279 char temporary[8192];1280 sprintf(temporary,"%s=%s",name,InitBuf);1281 OpcodeCalc(temporary);1282 1283 //アクセス制限を元に戻す1284 pVar->bConst = bConstBack;1285 }1286 }1287 1288 1289 if( pTypeInfo->type==DEF_OBJECT ){1290 //デストラクタの利用フラグをオンにする1291 CMethod *method = pTypeInfo->u.pobj_Class->GetDestructorMethod();1292 if( method ){1293 method->psi->bUse = 1;1294 }1295 }1296 1297 /*1298 TODO: 消す1299 if(pTypeInfo->type==DEF_OBJECT){1300 //利用対象のクラスコンストラクタ、デストラクタに使用チェックをつける1301 for(i2=0;i2<pTypeInfo->u.pobj_Class->iMethodNum;i2++){1302 if(lstrcmp(pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->name,pTypeInfo->u.pobj_Class->name)==0){1303 //コンストラクタ1304 pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->bUse=1;1305 }1306 if(pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->name[0]=='~'){1307 //デストラクタ1308 pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->bUse=1;1309 }1310 }1311 }*/1312 }
Note:
See TracChangeset
for help on using the changeset viewer.