Changeset 206 in dev for trunk/abdev/BasicCompiler32/Compile_Var.cpp
- Timestamp:
- Jul 12, 2007, 2:58:26 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/abdev/BasicCompiler32/Compile_Var.cpp
r193 r206 1 #include "stdafx.h" 2 1 3 #include <jenga/include/smoothie/Smoothie.h> 2 4 #include <jenga/include/smoothie/LexicalAnalysis.h> … … 5 7 #include <CodeGenerator.h> 6 8 #include <Compiler.h> 7 #include <Variable Impl.h>9 #include <Variable.h> 8 10 9 11 #include "../BasicCompiler_Common/common.h" … … 11 13 12 14 //変数 13 Variables globalVars; 15 // TODO: xml未完成 14 16 int AllGlobalVarSize; 15 17 int AllInitGlobalVarSize; … … 64 66 } 65 67 } 66 bool GetArrayOffset(const int *SubScripts,char *array, const Type &type){68 bool GetArrayOffset(const Subscripts &subscripts,char *array, const Type &type){ 67 69 extern HANDLE hHeap; 68 70 int i,i2,i3,i4; … … 83 85 } 84 86 if(array[i]==','||array[i]=='\0'){ 85 if(SubScripts[i3]==-1){ 87 if( i3 >= (int)subscripts.size() ) 88 { 86 89 for(i3--;i3>=0;i3--) HeapDefaultFree(pParm[i3]); 87 90 return 0; … … 96 99 97 100 if(array[i]=='\0'){ 98 if(SubScripts[i3]!=-1){ 101 if( i3 < (int)subscripts.size() ) 102 { 99 103 for(i3--;i3>=0;i3--) HeapDefaultFree(pParm[i3]); 100 104 return 0; … … 131 135 op_pop(REG_EAX); 132 136 133 for(i2=i+1,i4=1;i2<i3;i2++) i4*= SubScripts[i2]+1;137 for(i2=i+1,i4=1;i2<i3;i2++) i4*=subscripts[i2]+1; 134 138 135 139 //imul eax,i4 … … 175 179 char lpPtrOffset[VN_SIZE]; //第2次配列 176 180 char NestMember[VN_SIZE]; //入れ子メンバ 177 CClass::RefTyperefType;181 ReferenceKind refType; 178 182 lstrcpy(VarName,member); 179 183 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember,refType)) return false; … … 195 199 196 200 //アクセシビリティをチェック 197 if(&objClass== Smoothie::Temp::pCompilingClass){201 if(&objClass==compiler.pCompilingClass){ 198 202 //同一クラスオブジェクトの場合はプライベートアクセスを容認する 199 203 if(pMember->IsNoneAccess()){ … … 228 232 //ポインタ変数の場合 229 233 if( resultType.IsPointer() ){ 230 if( pMember->SubScripts[0]==-1){234 if( pMember->GetSubscripts().size() == 0 ){ 231 235 lstrcpy(lpPtrOffset,array); 232 236 array[0]=0; … … 250 254 if(array[0]){ 251 255 //配列オフセット 252 if(!GetArrayOffset(pMember-> SubScripts,array,pMember->GetType())){256 if(!GetArrayOffset(pMember->GetSubscripts(),array,pMember->GetType())){ 253 257 if(isErrorEnabled) SetError(14,member,cp); 254 258 } 255 259 } 256 else if( pMember->SubScripts[0]!=-1){260 else if( pMember->GetSubscripts().size() > 0 ){ 257 261 resultType.SetBasicType( resultType.GetBasicType() | FLAG_PTR ); 258 262 } … … 262 266 263 267 if( resultType.IsObject() || resultType.IsStruct() ){ 264 if( refType != CClass::Dot ){268 if( refType != RefDot ){ 265 269 if(isErrorEnabled) SetError(104,member,cp); 266 270 return false; … … 278 282 //pObj[n].member 279 283 if( ( resultType.IsObjectPtr() || resultType.IsStructPtr() ) 280 && refType != CClass::Dot ){284 && refType != RefDot ){ 281 285 if(isErrorEnabled) SetError(104,member,cp); 282 286 return false; … … 292 296 //pObj->member 293 297 if( ( resultType.IsObjectPtr() || resultType.IsStructPtr() ) 294 && refType != CClass::Pointer ){298 && refType != RefPointer ){ 295 299 if(isErrorEnabled) SetError(104,member,cp); 296 300 return false; … … 306 310 if(lpPtrOffset[0]){ 307 311 //ppObj[n]->member 308 if( refType != CClass::Pointer ){312 if( refType != RefPointer ){ 309 313 if(isErrorEnabled) SetError(104,member,cp); 310 314 return false; … … 357 361 } 358 362 359 bool GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,RELATIVE_VAR *pRelativeVar,Type &resultType, int *pss){363 bool GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,RELATIVE_VAR *pRelativeVar,Type &resultType, Subscripts *pResultSubscripts ){ 360 364 char variable[VN_SIZE]; 361 365 … … 371 375 372 376 // 先頭オブジェクトまたはクラス名と入れ子メンバに分割 373 CClass::RefTyperefType;377 ReferenceKind refType; 374 378 char member[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE]; 375 379 GetVarFormatString(simpleName,array,lpPtrOffset,member,refType); … … 384 388 } 385 389 386 const int *pSubScripts;390 const Subscripts *pSubscripts; 387 391 bool bConst = false; 388 392 … … 393 397 ////////////////// 394 398 395 const Variable *pVar = UserProc::CompilingUserProc(). localVars.BackSearch( Symbol( VarName ) );399 const Variable *pVar = UserProc::CompilingUserProc().GetLocalVars().BackSearch( Symbol( VarName ) ); 396 400 if( pVar ){ 397 401 //ポインタ変数の場合 398 if( pVar-> IsPointer() ){402 if( pVar->GetType().IsPointer() ){ 399 403 if( !pVar->IsArray() ){ 400 404 lstrcpy(lpPtrOffset,array); … … 410 414 } 411 415 412 pRelativeVar->offset=-pVar-> offset;416 pRelativeVar->offset=-pVar->GetOffsetAddress(); 413 417 pRelativeVar->bOffsetOffset=0; 414 418 if( pVar->IsRef() ){ … … 417 421 } 418 422 else pRelativeVar->dwKind=VAR_LOCAL; 419 resultType = *pVar;420 pSub Scripts=pVar->GetSubScriptsPtr();423 resultType = pVar->GetType(); 424 pSubscripts = &pVar->GetSubscripts(); 421 425 bConst = pVar->IsConst(); 422 426 … … 425 429 } 426 430 427 if( Smoothie::Temp::pCompilingClass){431 if(compiler.pCompilingClass){ 428 432 ////////////////////// 429 433 // クラスメンバの参照 … … 438 442 pRelativeVar->dwKind=VAR_DIRECTMEM; 439 443 440 resultType.SetType( DEF_OBJECT, Smoothie::Temp::pCompilingClass );444 resultType.SetType( DEF_OBJECT, compiler.pCompilingClass ); 441 445 return true; 442 446 } … … 451 455 452 456 bool isFound = false; 453 BOOST_FOREACH( CMember *pMember, Smoothie::Temp::pCompilingClass->GetDynamicMembers() ){457 BOOST_FOREACH( CMember *pMember, compiler.pCompilingClass->GetDynamicMembers() ){ 454 458 if( pMember->GetName() == VarName ){ 455 459 isFound = true; … … 465 469 if( isWriteAccess && 466 470 pMethod->IsConst() && 467 Smoothie::Temp::pCompilingClass->IsCompilingConstructor() == false &&468 Smoothie::Temp::pCompilingClass->IsCompilingDestructor() == false471 compiler.pCompilingClass->IsCompilingConstructor() == false && 472 compiler.pCompilingClass->IsCompilingDestructor() == false 469 473 ){ 470 474 SetError(131, NULL, cp ); … … 481 485 isErrorEnabled, 482 486 isWriteAccess, 483 * Smoothie::Temp::pCompilingClass,487 *compiler.pCompilingClass, 484 488 variable, 485 489 pRelativeVar, … … 502 506 GetNowStaticVarFullName(VarName,temporary); 503 507 504 pVar = globalVars.Find( Symbol( temporary ) );508 pVar = compiler.GetMeta().GetGlobalVars().Find( Symbol( temporary ) ); 505 509 if( pVar ){ 506 510 goto GlobalOk; … … 521 525 char tempArray[VN_SIZE]; 522 526 { 523 CClass::RefTyperefType;527 ReferenceKind refType; 524 528 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember, refType ); 525 529 } … … 533 537 char temp2[VN_SIZE]; 534 538 sprintf(temp2,"%s.%s",VarName,temporary); 535 pVar = globalVars.Find( Symbol( temp2 ) );539 pVar = compiler.GetMeta().GetGlobalVars().Find( Symbol( temp2 ) ); 536 540 if( pVar ){ 537 541 lstrcpy(member,tempMember); … … 541 545 } 542 546 543 if( Smoothie::Temp::pCompilingClass){547 if(compiler.pCompilingClass){ 544 548 //自身のクラスから静的メンバを参照する場合 545 549 char temp2[VN_SIZE]; 546 sprintf(temp2,"%s.%s", Smoothie::Temp::pCompilingClass->GetName().c_str(),VarName);547 pVar = globalVars.Find( Symbol( temp2 ) );550 sprintf(temp2,"%s.%s",compiler.pCompilingClass->GetName().c_str(),VarName); 551 pVar = compiler.GetMeta().GetGlobalVars().Find( Symbol( temp2 ) ); 548 552 if( pVar ){ 549 553 goto GlobalOk; … … 555 559 ///////////////////// 556 560 557 pVar = globalVars.BackSearch( Symbol( VarName ) );561 pVar = compiler.GetMeta().GetGlobalVars().BackSearch( Symbol( VarName ) ); 558 562 if( pVar ){ 559 563 goto GlobalOk; … … 568 572 GlobalOk: 569 573 //ポインタ変数の場合 570 if( pVar-> IsPointer() ){574 if( pVar->GetType().IsPointer() ){ 571 575 if( !pVar->IsArray() ){ 572 576 lstrcpy(lpPtrOffset,array); … … 582 586 } 583 587 584 pRelativeVar->offset=pVar-> offset;588 pRelativeVar->offset=pVar->GetOffsetAddress(); 585 589 pRelativeVar->bOffsetOffset=0; 586 590 if( pVar->IsRef() ){ … … 589 593 } 590 594 else pRelativeVar->dwKind=VAR_GLOBAL; 591 resultType = *pVar;592 pSub Scripts=pVar->GetSubScriptsPtr();595 resultType = pVar->GetType(); 596 pSubscripts=&pVar->GetSubscripts(); 593 597 bConst = pVar->IsConst(); 594 598 } … … 610 614 } 611 615 612 if( array[0]==0&&pSubScripts[0]!=-1){616 if( array[0] == 0 && pSubscripts->size() > 0 ){ 613 617 //配列の先頭ポインタを示す場合 614 618 resultType.SetBasicType( resultType.GetBasicType() | FLAG_PTR ); 615 if(pss) memcpy(pss,pSubScripts,MAX_ARRAYDIM); 619 620 if( pResultSubscripts ) 621 { 622 (*pResultSubscripts) = *pSubscripts; 623 } 616 624 return true; 617 625 } … … 625 633 } 626 634 if(array[0]){ 627 if(!GetArrayOffset( pSubScripts,array,resultType)){635 if(!GetArrayOffset(*pSubscripts,array,resultType)){ 628 636 SetError(14,variable,cp); 629 637 pRelativeVar->dwKind=NON_VAR; … … 634 642 if( resultType.IsObject() || resultType.IsStruct() ){ 635 643 //実態オブジェクトのメンバを参照(obj.member) 636 if( refType != CClass::Dot ){644 if( refType != RefDot ){ 637 645 SetError(104,VarName,cp); 638 646 pRelativeVar->dwKind=NON_VAR; … … 649 657 if(lpPtrOffset[0]){ 650 658 //pObj[n].member 651 if( refType != CClass::Dot ){659 if( refType != RefDot ){ 652 660 SetError(104,VarName,cp); 653 661 pRelativeVar->dwKind=NON_VAR; … … 659 667 else{ 660 668 //pObj->member 661 if( refType != CClass::Pointer ){669 if( refType != RefPointer ){ 662 670 SetError(104,VarName,cp); 663 671 pRelativeVar->dwKind=NON_VAR; … … 677 685 if(lpPtrOffset[0]){ 678 686 //ppObj[n]->member 679 if( refType != CClass::Pointer ){687 if( refType != RefPointer ){ 680 688 SetError(104,VarName,cp); 681 689 pRelativeVar->dwKind=NON_VAR; … … 722 730 } 723 731 724 bool SetInitGlobalData(int offset,const Type &type,const int *SubScripts,const char *lpszInitBuf){732 bool SetInitGlobalData(int offset,const Type &type,const Subscripts &subscripts,const char *lpszInitBuf){ 725 733 extern BYTE *initGlobalBuf; 726 734 int i2,i3; … … 735 743 int typeSize = type.GetSize(); 736 744 737 if(SubScripts[0]!=-1){ 738 typeSize*=JumpSubScripts(SubScripts+1); 739 int i=0; 740 i2=0; 741 while(1){ 742 if(SubScripts[0]<i2){ 743 SetError(41,0,cp); 744 return 0; 745 } 746 i=GetOneParameter(InitBuf,i,temporary); 747 if(!SetInitGlobalData( 748 offset+i2*typeSize, 749 type, 750 SubScripts+1, 751 temporary)) return false; 752 i2++; 753 if(InitBuf[i]=='\0') break; 745 if( subscripts.size() > 0 ){ 746 Subscripts nestSubscripts; 747 for( int i=1; i<(int)subscripts.size(); i++ ) 748 { 749 nestSubscripts.push_back( subscripts[i] ); 750 } 751 752 typeSize*=JumpSubScripts( nestSubscripts ); 753 { 754 int i=0; 755 i2=0; 756 while(1){ 757 if( subscripts[0] < i2 ){ 758 SetError(41,0,cp); 759 return 0; 760 } 761 i=GetOneParameter(InitBuf,i,temporary); 762 if(!SetInitGlobalData( 763 offset+i2*typeSize, 764 type, 765 nestSubscripts, 766 temporary)) return false; 767 i2++; 768 if(InitBuf[i]=='\0') break; 769 } 754 770 } 755 771 return true; … … 772 788 if(!SetInitGlobalData(offset+i3, 773 789 pMember->GetType(), 774 pMember-> SubScripts,790 pMember->GetSubscripts(), 775 791 temporary)) return false; 776 792 } … … 793 809 } 794 810 795 if( SubScripts[0]!=-1){811 if( subscripts.size() > 0 ){ 796 812 SetError(41,0,cp); 797 813 return false; … … 861 877 return true; 862 878 } 863 bool InitLocalVar(int offset,const Type &type,const int *SubScripts,const char *lpszInitBuf){864 int i ,i2,i3;879 bool InitLocalVar(int offset,const Type &type,const Subscripts &subscripts,const char *lpszInitBuf){ 880 int i2,i3; 865 881 char temporary[VN_SIZE]; 866 882 char InitBuf[VN_SIZE]; … … 873 889 int typeSize = type.GetSize(); 874 890 875 if(SubScripts[0]!=-1){ 876 typeSize*=JumpSubScripts(SubScripts+1); 877 i=0; 878 i2=0; 879 while(1){ 880 if(SubScripts[0]<i2){ 881 SetError(41,0,cp); 882 return false; 883 } 884 i=GetOneParameter(InitBuf,i,temporary); 885 if(!InitLocalVar( 886 offset+i2*typeSize, 887 type, 888 SubScripts+1, 889 temporary)) return false; 890 i2++; 891 if(InitBuf[i]=='\0') break; 891 if( subscripts.size() > 0 ){ 892 Subscripts nestSubscripts; 893 for( int i=1; i<(int)subscripts.size(); i++ ) 894 { 895 nestSubscripts.push_back( subscripts[i] ); 896 } 897 898 typeSize*=JumpSubScripts( nestSubscripts ); 899 { 900 int i=0; 901 i2=0; 902 while(1){ 903 if( subscripts[0] < i2 ){ 904 SetError(41,0,cp); 905 return 0; 906 } 907 i=GetOneParameter(InitBuf,i,temporary); 908 if(!InitLocalVar( 909 offset+i2*typeSize, 910 type, 911 nestSubscripts, 912 temporary)) return false; 913 i2++; 914 if(InitBuf[i]=='\0') break; 915 } 892 916 } 893 917 return true; … … 910 934 if(!InitLocalVar(offset+i3, 911 935 pMember->GetType(), 912 pMember-> SubScripts,936 pMember->GetSubscripts(), 913 937 temporary)) return false; 914 938 … … 927 951 /////////////////////////////////////// 928 952 929 if( SubScripts[0]!=-1){953 if( subscripts.size() > 0 ){ 930 954 SetError(41,0,cp); 931 955 return false; … … 1075 1099 } 1076 1100 1077 void dim( char *VarName, int *SubScripts,Type &type,const char *InitBuf,const char *ConstractParameter,DWORD dwFlags){1101 void dim( char *VarName, const Subscripts &subscripts, Type &type,const char *InitBuf,const char *ConstractParameter,DWORD dwFlags){ 1078 1102 if( UserProc::IsGlobalAreaCompiling() ){ 1079 1103 ///////////////////////// … … 1081 1105 ///////////////////////// 1082 1106 1083 AddGlobalVariable(VarName, SubScripts,type,InitBuf,ConstractParameter,dwFlags);1107 AddGlobalVariable(VarName,subscripts,type,InitBuf,ConstractParameter,dwFlags); 1084 1108 } 1085 1109 else{ … … 1088 1112 ///////////////// 1089 1113 1090 if( UserProc::CompilingUserProc(). localVars.DuplicateCheck( Symbol( VarName ) ) ){1114 if( UserProc::CompilingUserProc().GetLocalVars().DuplicateCheck( Symbol( VarName ) ) ){ 1091 1115 //2重定義のエラー 1092 1116 SetError(15,VarName,cp); … … 1096 1120 bool isConst = ( dwFlags & DIMFLAG_CONST ) ? true:false; 1097 1121 1098 Variable *pVar = new Variable Impl( VarName, type, isConst);1099 1100 if( SubScripts[0] != -1){1122 Variable *pVar = new Variable( VarName, type, isConst, false, ConstractParameter ); 1123 1124 if( subscripts.size() > 0 ){ 1101 1125 //配列あり 1102 pVar->SetArray( SubScripts ); 1103 } 1104 1105 //コンストラクタ用パラメータ 1106 pVar->paramStrForConstructor = ConstractParameter; 1126 pVar->SetArray( subscripts ); 1127 } 1107 1128 1108 1129 //レキシカルスコープ 1109 pVar->S copeLevel=GetLexicalScopes().GetNowLevel();1110 pVar->S copeStartAddress=GetLexicalScopes().GetStartAddress();1130 pVar->SetScopeLevel( GetLexicalScopes().GetNowLevel() ); 1131 pVar->SetScopeStartAddress( GetLexicalScopes().GetStartAddress() ); 1111 1132 pVar->bLiving=TRUE; 1112 1133 … … 1115 1136 1116 1137 // 変数を追加 1117 UserProc::CompilingUserProc(). localVars.push_back( pVar );1138 UserProc::CompilingUserProc().GetLocalVars().push_back( pVar ); 1118 1139 1119 1140 //アラインメントを考慮 1120 if( pVar-> IsStruct() ){1121 int alignment = pVar->Get Class().iAlign;1141 if( pVar->GetType().IsStruct() ){ 1142 int alignment = pVar->GetType().GetClass().iAlign; 1122 1143 1123 1144 if( alignment ){ … … 1139 1160 1140 1161 AllLocalVarSize += pVar->GetMemorySize(); 1141 pVar-> offset = AllLocalVarSize;1162 pVar->SetOffsetAddress( AllLocalVarSize ); 1142 1163 1143 1164 //レキシカルスコープ 1144 pVar->S copeLevel=GetLexicalScopes().GetNowLevel();1145 pVar->S copeStartAddress=GetLexicalScopes().GetStartAddress();1165 pVar->SetScopeLevel( GetLexicalScopes().GetNowLevel() ); 1166 pVar->SetScopeStartAddress( GetLexicalScopes().GetStartAddress() ); 1146 1167 pVar->bLiving=TRUE; 1147 1168 … … 1153 1174 1154 1175 int result = 0; 1155 if( !pVar-> IsObject() ){1156 result = InitLocalVar(-pVar-> offset,1157 *pVar,1158 pVar->GetSub ScriptsPtr(),1176 if( !pVar->GetType().IsObject() ){ 1177 result = InitLocalVar(-pVar->GetOffsetAddress(), 1178 pVar->GetType(), 1179 pVar->GetSubscripts(), 1159 1180 InitBuf); 1160 1181 } … … 1184 1205 //add eax,offset 1185 1206 OpBuffer[obp++]=(char)0x05; 1186 *((long *)(OpBuffer+obp))=-pVar-> offset;1207 *((long *)(OpBuffer+obp))=-pVar->GetOffsetAddress(); 1187 1208 AddLocalVarAddrSchedule(); 1188 1209 obp+=sizeof(long); … … 1204 1225 if( type.IsObject() &&(dwFlags&DIMFLAG_NONCALL_CONSTRACTOR)==0&&InitBuf[0]=='\0'){ 1205 1226 char objectSize[255]; 1206 if( SubScripts[0] == -1){1227 if( subscripts.size() == 0 ){ 1207 1228 objectSize[0] = 0; 1208 1229 } 1209 1230 else{ 1210 if( SubScripts[1] != -1 ){1231 if( subscripts.size() > 1 ){ 1211 1232 SetError(300,NULL,cp); 1212 1233 } 1213 sprintf( objectSize, "%d", SubScripts[0] );1234 sprintf( objectSize, "%d", subscripts[0] ); 1214 1235 } 1215 1236 Operator_New( type.GetClass(), objectSize, ConstractParameter, type ); … … 1323 1344 1324 1345 bool Compile_AddGlobalRootsForGc(){ 1325 UserProc *pUserProc_AddGlobalRootPtr = GetClassMethod( "_System_CGarbageCollection", "AddGlobalRootPtr" );1346 const UserProc *pUserProc_AddGlobalRootPtr = GetClassMethod( "_System_CGarbageCollection", "AddGlobalRootPtr" ); 1326 1347 if( !pUserProc_AddGlobalRootPtr ){ 1327 1348 SetError(3, "_System_CGarbageCollection.AddGlobalRootPtr", -1 ); … … 1329 1350 } 1330 1351 1331 BOOST_FOREACH( const Variable *pVar, globalVars){1332 if( pVar-> IsObject() || pVar->IsPointer() || pVar->IsStruct() ){1352 BOOST_FOREACH( const Variable *pVar, compiler.GetMeta().GetGlobalVars() ){ 1353 if( pVar->GetType().IsObject() || pVar->GetType().IsPointer() || pVar->GetType().IsStruct() ){ 1333 1354 // オブジェクトまたはポインタだったとき 1334 1355 // ※構造体も含む(暫定対応) … … 1342 1363 1343 1364 //mov eax,offset 1344 op_mov_RV(REG_EAX,(int)pVar-> offset);1365 op_mov_RV(REG_EAX,(int)pVar->GetOffsetAddress()); 1345 1366 obp-=sizeof(long); 1346 1367 pobj_GlobalVarSchedule->add();
Note:
See TracChangeset
for help on using the changeset viewer.