source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_Class.cpp@ 603

Last change on this file since 603 was 603, checked in by dai_9181, 16 years ago

ObjectModuleに関連するクラス一式をab_commonプロジェクトに移動した。

File size: 34.7 KB
Line 
1#include "stdafx.h"
2
3#include "../common.h"
4#ifdef _AMD64_
5#include "../../compiler_x64/opcode.h"
6#else
7#include "../../compiler_x86/opcode.h"
8#endif
9
10using namespace ActiveBasic::Compiler;
11
12
13void LexicalAnalyzer::CollectClassesForNameOnly( const char *source, Classes &classes )
14{
15 int i, i2;
16 char temporary[VN_SIZE];
17
18 // 名前空間管理
19 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
20 namespaceScopes.clear();
21
22 // Importsされた名前空間の管理
23 NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
24 importedNamespaces.clear();
25
26 for(i=0;;i++){
27 if(source[i]=='\0') break;
28
29 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
30 for(i+=2,i2=0;;i2++,i++){
31 if( IsCommandDelimitation( source[i] ) ){
32 temporary[i2]=0;
33 break;
34 }
35 temporary[i2]=source[i];
36 }
37 namespaceScopes.push_back( temporary );
38
39 continue;
40 }
41 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
42 if( namespaceScopes.size() <= 0 ){
43 compiler.errorMessenger.Output(12, "End Namespace", i );
44 }
45 else{
46 namespaceScopes.pop_back();
47 }
48
49 i += 2;
50 continue;
51 }
52 else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
53 for(i+=2,i2=0;;i2++,i++){
54 if( IsCommandDelimitation( source[i] ) ){
55 temporary[i2]=0;
56 break;
57 }
58 temporary[i2]=source[i];
59 }
60 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
61 {
62 compiler.errorMessenger.Output(64,temporary,i );
63 }
64
65 continue;
66 }
67 else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
68 importedNamespaces.clear();
69 continue;
70 }
71
72 if(source[i]==1&&(
73 source[i+1]==ESC_CLASS||
74 source[i+1]==ESC_TYPE||
75 source[i+1]==ESC_INTERFACE
76 ))
77 {
78 int nowLine = i;
79 i += 2;
80
81 Type blittableType;
82 if(memicmp(source+i,"Align(",6)==0){
83 //アラインメント修飾子
84 i+=6;
85 i=JumpStringInPare(source,i)+1;
86 }
87 else if( memicmp( source + i, "Blittable(", 10 ) == 0 ){
88 // Blittable修飾子
89 i+=10;
90 i+=GetStringInPare_RemovePare(temporary,source+i)+1;
91 compiler.StringToType( temporary, blittableType );
92 }
93
94 bool isEnum = false;
95 bool isDelegate = false;
96 if( source[i] == 1 && source[i+1] == ESC_ENUM ){
97 // 列挙型の場合
98 isEnum = true;
99
100 i += 2;
101 }
102 else if( source[i] == 1 && source[i+1] == ESC_DELEGATE )
103 {
104 // デリゲートの場合
105 isDelegate = true;
106
107 i += 2;
108 }
109
110 for(i2=0;;i++,i2++){
111 if(!IsVariableChar(source[i])){
112 temporary[i2]=0;
113 break;
114 }
115 temporary[i2]=source[i];
116 }
117
118 //クラスを追加
119 CClass *pClass = new CClass( namespaceScopes, importedNamespaces, temporary );
120 if( classes.IsExist( pClass ) )
121 {
122 // 既に存在している
123 compiler.errorMessenger.Output(15,pClass->GetName(), nowLine);
124
125 delete pClass;
126
127 continue;
128 }
129
130 classes.Put( pClass );
131
132 if( source[nowLine+1] == ESC_CLASS )
133 {
134 if( isEnum )
135 {
136 pClass->SetClassType( CClass::Enum );
137 }
138 else if( isDelegate )
139 {
140 pClass->SetClassType( CClass::Delegate );
141 }
142 else{
143 pClass->SetClassType( CClass::Class );
144 }
145 }
146 else if( source[nowLine+1] == ESC_INTERFACE )
147 {
148 pClass->SetClassType( CClass::Interface );
149 }
150 else
151 {
152 pClass->SetClassType( CClass::Structure );
153 }
154
155 // Blittable型の場合
156 if( !blittableType.IsNull() ){
157 pClass->SetBlittableType( blittableType );
158
159 // Blittable型として登録
160 compiler.GetObjectModule().meta.GetBlittableTypes().push_back( BlittableType( blittableType, pClass ) );
161 }
162 }
163 }
164}
165
166
167class CLoopRefCheck{
168 char **names;
169 int num;
170 void init(){
171 int i;
172 for(i=0;i<num;i++){
173 free(names[i]);
174 }
175 free(names);
176 }
177public:
178 CLoopRefCheck()
179 {
180 names=(char **)malloc(1);
181 num=0;
182 }
183 ~CLoopRefCheck()
184 {
185 init();
186 }
187 void add(const char *lpszInheritsClass)
188 {
189 names=(char **)realloc(names,(num+1)*sizeof(char *));
190 names[num]=(char *)malloc(lstrlen(lpszInheritsClass)+1);
191 lstrcpy(names[num],lpszInheritsClass);
192 num++;
193 }
194 void del(const char *lpszInheritsClass)
195 {
196 int i;
197 for(i=0;i<num;i++){
198 if(lstrcmp(names[i],lpszInheritsClass)==0){
199 free(names[i]);
200 break;
201 }
202 }
203 if(i!=num){
204 num--;
205 for(;i<num;i++){
206 names[i]=names[i+1];
207 }
208 }
209 }
210 BOOL check(const CClass &inheritsClass) const
211 {
212 //ループ継承チェック
213 int i;
214 for(i=0;i<num;i++){
215 if( inheritsClass.GetName() == names[i] ){
216 return 1;
217 }
218 }
219 return 0;
220 }
221};
222CLoopRefCheck *pobj_LoopRefCheck;
223
224bool MemberVar_LoopRefCheck(const CClass &objClass){
225 if( objClass.HasSuperClass() )
226 {
227 // 基底クラスをチェック
228 if( MemberVar_LoopRefCheck( objClass.GetSuperClass() ) == false )
229 {
230 return false;
231 }
232 }
233
234 bool result = true;
235 BOOST_FOREACH( Member *pMember, objClass.GetDynamicMembers() ){
236 if(pMember->GetType().IsStruct()){
237 //循環参照でないかをチェック
238 if(pobj_LoopRefCheck->check(pMember->GetType().GetClass())){
239 extern int cp;
240 compiler.errorMessenger.Output(124,pMember->GetType().GetClass().GetName(),cp);
241 return false;
242 }
243
244 pobj_LoopRefCheck->add(objClass.GetName().c_str());
245
246 bool tempResult = MemberVar_LoopRefCheck(pMember->GetType().GetClass());
247 if( result )
248 {
249 result = tempResult;
250 }
251
252 pobj_LoopRefCheck->del(objClass.GetName().c_str());
253 }
254 }
255
256 return result;
257}
258
259void OverrideErrorCheck( const DynamicMethod::OverrideResult &result )
260{
261 switch( result.enumType )
262 {
263 case DynamicMethod::OverrideResult::Successful:
264 break;
265 case DynamicMethod::OverrideResult::NotVirtual:
266 compiler.errorMessenger.Output(136, result.pMethod->GetUserProc().GetName(), cp);
267 break;
268 case DynamicMethod::OverrideResult::NotUseOverrideModifier:
269 compiler.errorMessenger.Output(127, result.pMethod->GetUserProc().GetName(), cp);
270 break;
271 case DynamicMethod::OverrideResult::DifferentAccesibility:
272 compiler.errorMessenger.Output(128, result.pMethod->GetUserProc().GetName(), cp);
273 break;
274 default:
275 throw;
276 }
277}
278
279Member *LexicalAnalyzer::CreateMember( const CClass &_class, Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine )
280{
281 extern int cp;
282
283 //構文を解析
284 char VarName[VN_SIZE];
285 char initBuffer[VN_SIZE];
286 char lpszConstructParameter[VN_SIZE];
287 Subscripts subscripts;
288 Type type;
289 GetDimentionFormat(buffer,VarName,subscripts,type,initBuffer,lpszConstructParameter);
290
291 //重複チェック
292 if( _class.DupliCheckAll( VarName ) ){
293 compiler.errorMessenger.Output(15,VarName,cp);
294 }
295
296 Member *pMember = new Member( accessibility, VarName, type, isConst, subscripts, initBuffer, lpszConstructParameter );
297 pMember->source_code_address = nowLine;
298 return pMember;
299}
300
301void LexicalAnalyzer::AddMethod(CClass *pobj_c, UserProc *pUserProc, Prototype::Accessibility accessibility, BOOL bStatic, bool isConst, bool isAbstract,
302 bool isVirtual, bool isOverride, const char *interfaceName, bool isAutoGeneration, int nowLine)
303{
304 if( isAutoGeneration )
305 {
306 // コード自動生成
307 pUserProc->ThisIsAutoGenerationProc();
308 }
309
310
311 ////////////////////////////////////////////////////////////
312 // コンストラクタ、デストラクタの場合の処理
313 ////////////////////////////////////////////////////////////
314 BOOL fConstructor=0,bDestructor=0;
315
316 if( pUserProc->GetName() == pobj_c->GetName() ){
317 //コンストラクタの場合
318
319 //標準コンストラクタ(引数なし)
320 if(pUserProc->Params().size()==0) fConstructor=1;
321
322 //強制的にConst修飾子をつける
323 isConst = true;
324 }
325 else if(pUserProc->GetName()[0]=='~'){
326 //デストラクタの場合はその名前が正しいかチェックを行う
327 if(lstrcmp(pUserProc->GetName().c_str()+1,pobj_c->GetName().c_str())!=0)
328 compiler.errorMessenger.Output(117,NULL,nowLine);
329 else
330 bDestructor=1;
331 }
332 if(fConstructor||bDestructor){
333 // コンストラクタ、デストラクタのアクセシビリティをチェック
334
335 //強制的にConst修飾子をつける
336 isConst = true;
337 }
338
339 if( fConstructor == 1 )
340 pobj_c->SetConstructorMemberSubIndex( (int)pobj_c->GetDynamicMethods().size() );
341 else if( bDestructor )
342 pobj_c->SetDestructorMemberSubIndex( (int)pobj_c->GetDynamicMethods().size() );
343
344
345
346 //////////////////
347 // 重複チェック
348 //////////////////
349
350 if(pobj_c->DupliCheckMember( pUserProc->GetName().c_str() )){
351 compiler.errorMessenger.Output(15,pUserProc->GetName(),nowLine);
352 return;
353 }
354
355 //メソッド
356 BOOST_FOREACH( const CMethod *pMethod, pobj_c->GetDynamicMethods() )
357 {
358 //基底クラスと重複する場合はオーバーライドを行う
359 if( pMethod->GetInheritsClassPtr() ) continue;
360
361 if( pMethod->GetUserProc().IsEqualForOverride( pobj_c->GetSuperClassActualTypeParameters(), pUserProc ) )
362 {
363 //関数名、パラメータ、戻り値が合致したとき
364 compiler.errorMessenger.Output(15,pUserProc->GetName().c_str(),nowLine);
365 return;
366 }
367 }
368
369 //仮想関数の場合
370 if( isAbstract ) pUserProc->CompleteCompile();
371
372 // メソッドのオーバーライド
373 DynamicMethod *pMethodForOverride = pobj_c->GetDynamicMethods().FindForOverride( pobj_c->GetSuperClassActualTypeParameters(), pUserProc );
374 if( pMethodForOverride )
375 {
376 DynamicMethod::OverrideResult result;
377 result.enumType = pMethodForOverride->Override( pUserProc, accessibility, isOverride );
378 result.pMethod = pMethodForOverride;
379 OverrideErrorCheck( result );
380
381 pUserProc->SetMethod( pMethodForOverride );
382 return;
383 }
384 else
385 {
386 // インターフェイス メソッドのオーバーライド
387 BOOST_FOREACH( ::Interface *pInterface, pobj_c->GetInterfaces() )
388 {
389 if( interfaceName[0] )
390 {
391 if( pInterface->GetClass().GetName() != interfaceName )
392 {
393 // 指定されたインターフェイス名と整合しないとき
394 continue;
395 }
396 }
397
398 if( !pInterface->GetClass().IsReady() ){
399 // インターフェイスが未解析のとき
400 LexicalAnalyzer::LookaheadClass(
401 pInterface->GetClass().GetName().c_str(),
402 compiler.GetObjectModule().meta.GetClasses()
403 );
404 }
405
406 DynamicMethod *pMethodForOverride = pInterface->GetDynamicMethods().FindForOverride( pInterface->GetActualTypeParameters(), pUserProc );
407 if( pMethodForOverride )
408 {
409 DynamicMethod::OverrideResult result;
410 result.enumType = pMethodForOverride->Override( pUserProc, accessibility, isOverride );
411 result.pMethod = pMethodForOverride;
412 OverrideErrorCheck( result );
413
414 pUserProc->SetMethod( pMethodForOverride );
415 return;
416 }
417 }
418 }
419
420 if( interfaceName[0] )
421 {
422 compiler.errorMessenger.Output(139,interfaceName,nowLine);
423 }
424
425 if( isVirtual ){
426 pobj_c->AddVtblNum( 1 );
427 }
428
429 if( isOverride ){
430 compiler.errorMessenger.Output(12,"Override",nowLine);
431 }
432
433 if(bStatic){
434 pobj_c->GetStaticMethods().AddStatic( pUserProc, accessibility );
435 }
436 else{
437 pobj_c->GetDynamicMethods().Add(pUserProc, accessibility, isConst, isAbstract, isVirtual);
438 }
439}
440
441bool LexicalAnalyzer::Inherits( CClass &_class, const char *inheritNames, int nowLine ){
442 int i = 0;
443 bool isInheritsClass = false;
444 while( true ){
445
446 char temporary[VN_SIZE];
447 for( int i2=0;; i++, i2++ ){
448 if( inheritNames[i] == '\0' || inheritNames[i] == ',' ){
449 temporary[i2] = 0;
450 break;
451 }
452 temporary[i2] = inheritNames[i];
453 }
454
455 // ジェネリクス構文を分解
456 char className[VN_SIZE];
457 Jenga::Common::Strings typeParameterStrings;
458 SplitGenericClassInstance( temporary, className, typeParameterStrings );
459
460 // 型パラメータ文字列から型データを取得
461 Types actualTypeParameters;
462 BOOST_FOREACH( const std::string &typeParameterStr, typeParameterStrings )
463 {
464 Type type;
465 compiler.StringToType( typeParameterStr, type );
466 actualTypeParameters.push_back( type );
467 }
468
469 //継承元クラスを取得
470 const CClass *pInheritsClass = compiler.GetObjectModule().meta.FindClassSupportedTypeDef(
471 LexicalAnalyzer::FullNameToSymbol( className )
472 );
473 if( !pInheritsClass ){
474 compiler.errorMessenger.Output(106,className,nowLine);
475 return false;
476 }
477
478 if( pInheritsClass->IsClass() ){
479 // クラスを継承する
480 isInheritsClass = true;
481
482 //ループ継承でないかをチェック
483 if( !LexicalAnalyzer::LoopRefCheck(*pInheritsClass) )
484 {
485 compiler.errorMessenger.Output(123,pInheritsClass->GetName(),nowLine);
486 return false;
487 }
488
489 if( !pInheritsClass->IsReady() ){
490 //継承先が読み取られていないとき
491 LexicalAnalyzer::LookaheadClass(
492 pInheritsClass->GetName().c_str(),
493 compiler.GetObjectModule().meta.GetClasses()
494 );
495 }
496
497 if( !_class.InheritsClass( *pInheritsClass, actualTypeParameters, nowLine ) ){
498 return false;
499 }
500 }
501 else{
502 compiler.errorMessenger.Output(135,pInheritsClass->GetFullName().c_str(),nowLine);
503 return false;
504 }
505
506 if( inheritNames[i] == '\0' ){
507 break;
508 }
509 i++;
510 }
511
512 if( !isInheritsClass ){
513 const CClass *pObjectClass = compiler.GetObjectModule().meta.GetClasses().GetObjectClassPtr();
514 //ループ継承でないかをチェック
515 if( !LexicalAnalyzer::LoopRefCheck( *pObjectClass ) )
516 {
517 compiler.errorMessenger.Output(123,pObjectClass->GetName(),nowLine);
518 return false;
519 }
520
521 if( !pObjectClass->IsReady() ){
522 //継承先が読み取られていないとき
523 LexicalAnalyzer::LookaheadClass(
524 pObjectClass->GetName().c_str(),
525 compiler.GetObjectModule().meta.GetClasses()
526 );
527 }
528
529 // クラスを一つも継承していないとき
530 if( !_class.InheritsClass( *pObjectClass, Types(), nowLine ) ){
531 return false;
532 }
533 }
534
535 return true;
536}
537
538void LexicalAnalyzer::Implements( CClass &_class, Interface *pInterface, std::vector<DynamicMethod::OverrideResult> &overrideResults )
539{
540 _class.AddInterface( pInterface );
541
542
543 /////////////////////////////////////////////////////////////////
544 // 基底クラスのメソッドからインターフェイスメソッドを再実装する
545 /////////////////////////////////////////////////////////////////
546 BOOST_FOREACH( CMethod *pMethod, _class.GetDynamicMethods() )
547 {
548 DynamicMethod *pMethodForOverride = pInterface->GetDynamicMethods().FindForOverride( pInterface->GetActualTypeParameters(), &pMethod->GetUserProc() );
549 if( pMethodForOverride )
550 {
551 DynamicMethod::OverrideResult result;
552 result.enumType = pMethodForOverride->Override( &pMethod->GetUserProc(), pMethod->GetAccessibility(), false );
553 result.pMethod = pMethod;
554 overrideResults.push_back( result );
555
556 // 実装元になるメソッドは呼び出し不可にしておく(オーバーロードの解決から除外する)
557 pMethod->SetNotUseMark( true );
558 }
559 }
560
561
562 /////////////////////////////////////////////////////////////////
563 // キャストメソッドを追加(内部コードは自動生成すること)
564 // ※COMインターフェイスは除外すること
565 /////////////////////////////////////////////////////////////////
566 if( pInterface->GetClass().IsInterface() )
567 {
568 // Function Operator() As ITest
569
570 char methodName[255] = { 1, ESC_OPERATOR, CALC_AS, '\0' };
571
572 //関数ハッシュへ登録
573 UserProc *pUserProc = new UserProc(
574 NamespaceScopes(),
575 NamespaceScopesCollection(),
576 methodName,
577 Procedure::Function,
578 false,
579 false,
580 false );
581 pUserProc->SetParentClass( &_class );
582
583 Parameters params;
584 params.push_back( new Parameter( "_System_LocalThis", Type( DEF_PTR_VOID ) ) );
585 pUserProc->SetRealParams( params );
586
587 pUserProc->SetReturnType( Type( DEF_OBJECT, pInterface->GetClass() ) );
588 pUserProc->_paramStr = "";
589 pUserProc->Using();
590
591 // 関数を追加
592 if( compiler.GetObjectModule().meta.GetUserProcs().IsExist( pUserProc ) )
593 {
594 // 既に存在している
595 compiler.errorMessenger.Output(15,pUserProc->GetName(),-1);
596
597 delete pUserProc;
598 }
599 else
600 {
601 compiler.GetObjectModule().meta.GetUserProcs().Put( pUserProc );
602 }
603
604 LexicalAnalyzer::AddMethod(
605 &_class,
606 pUserProc,
607 Prototype::Public,
608 0,
609 false, // isConst
610 false, // isAbstract
611 false, // isVirtual
612 false, // isOverride
613 "",
614 true, // isAutoGeneration
615 -1
616 );
617 }
618}
619
620bool LexicalAnalyzer::Implements( CClass &_class, const char *interfaceNames, int nowLine )
621{
622 Jenga::Common::Strings paramStrs;
623 SplitParameter( interfaceNames, paramStrs );
624
625 BOOST_FOREACH( const std::string &paramStr, paramStrs )
626 {
627 char className[VN_SIZE];
628 Jenga::Common::Strings typeParameterStrings;
629 SplitGenericClassInstance( paramStr.c_str(), className, typeParameterStrings );
630
631 Types actualTypeParameters;
632 BOOST_FOREACH( const std::string &typeParameterStr, typeParameterStrings )
633 {
634 Type type;
635 compiler.StringToType( typeParameterStr, type );
636 actualTypeParameters.push_back( type );
637 }
638
639 //継承元クラスを取得
640 const CClass *pInterfaceClass = compiler.GetObjectModule().meta.FindClassSupportedTypeDef(
641 LexicalAnalyzer::FullNameToSymbol( className )
642 );
643 if( !pInterfaceClass ){
644 compiler.errorMessenger.Output(106,paramStr.c_str(),nowLine);
645 continue;
646 }
647
648 if( !pInterfaceClass->IsReady() ){
649 // インターフェイスが未解析のとき
650 LexicalAnalyzer::LookaheadClass(
651 pInterfaceClass->GetName().c_str(),
652 compiler.GetObjectModule().meta.GetClasses()
653 );
654 }
655
656 if( pInterfaceClass->IsInterface() || pInterfaceClass->IsComInterface() )
657 {
658 // インターフェイスを継承する
659 std::vector<DynamicMethod::OverrideResult> overrideResults;
660 Implements(
661 _class,
662 new ::Interface( pInterfaceClass, actualTypeParameters ),
663 overrideResults
664 );
665
666 // エラーチェック
667 BOOST_FOREACH( const DynamicMethod::OverrideResult result, overrideResults )
668 {
669 OverrideErrorCheck( result );
670 }
671 }
672 else
673 {
674 // インターフェイスではないとき
675 compiler.errorMessenger.Output(138,pInterfaceClass->GetName().c_str(),nowLine );
676 }
677 }
678
679 return true;
680}
681
682void GetClass_recur( const char *lpszInheritsClass, Classes &classes )
683{
684 extern char *basbuf;
685 int i,i2,i3,sub_address,top_pos;
686 char temporary[8192];
687
688 // 名前空間管理
689 NamespaceScopes backupNamespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
690 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
691 namespaceScopes.clear();
692
693 // Importsされた名前空間の管理
694 NamespaceScopesCollection backupImportedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
695 compiler.GetNamespaceSupporter().GetImportedNamespaces().clear();
696
697 // 呼び出し元でコンパイル中のクラスポインタをバックアップ
698 const CClass *pBackCompilingClass = compiler.IsCompilingClass()
699 ? &compiler.GetCompilingClass()
700 : NULL;
701
702 for(i=0;;i++){
703 if(basbuf[i]=='\0') break;
704
705
706 // 名前空間
707 if( basbuf[i] == 1 && basbuf[i+1] == ESC_NAMESPACE ){
708 for(i+=2,i2=0;;i2++,i++){
709 if( IsCommandDelimitation( basbuf[i] ) ){
710 temporary[i2]=0;
711 break;
712 }
713 temporary[i2]=basbuf[i];
714 }
715 namespaceScopes.push_back( temporary );
716
717 continue;
718 }
719 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENDNAMESPACE ){
720 if( namespaceScopes.size() <= 0 ){
721 compiler.errorMessenger.Output(12, "End Namespace", i );
722 }
723 else{
724 namespaceScopes.pop_back();
725 }
726
727 i += 2;
728 continue;
729 }
730
731 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_IMPORTS ){
732 for(i+=2,i2=0;;i2++,i++){
733 if( IsCommandDelimitation( basbuf[i] ) ){
734 temporary[i2]=0;
735 break;
736 }
737 temporary[i2]=basbuf[i];
738 }
739 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
740 {
741 compiler.errorMessenger.Output(64,temporary,i );
742 }
743
744 continue;
745 }
746 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
747 compiler.GetNamespaceSupporter().GetImportedNamespaces().clear();
748 continue;
749 }
750
751
752
753 if(basbuf[i]==1&&basbuf[i+1]==ESC_INTERFACE){
754 //////////////////////////
755 // インターフェイス
756 //////////////////////////
757
758 top_pos=i;
759
760 i+=2;
761
762 //インターフェイス名を取得
763 GetCommandToken( temporary, basbuf, i );
764
765 char className[VN_SIZE];
766 Jenga::Common::Strings typeParameters;
767 Jenga::Common::Strings typeParameterBaseClassNames;
768 SplitGenericClassInstance( temporary, className, typeParameters, true, &typeParameterBaseClassNames );
769
770 CClass *pobj_c = const_cast<CClass *>( classes.FindEx( Symbol( namespaceScopes, className ) ) );
771 if(!pobj_c) continue;
772
773 compiler.SetCompilingClass( pobj_c );
774
775 if(lpszInheritsClass){
776 if(lstrcmp(lpszInheritsClass,pobj_c->GetName().c_str())!=0){
777 //継承先先読み用
778 continue;
779 }
780 }
781
782 if(pobj_c->IsReady()){
783 //既に先読みされているとき
784 continue;
785 }
786
787 /////////////////////////////////////////////////////////
788 // ☆★☆ ジェネリクスサポート ☆★☆
789 for( i2=0; i2<static_cast<int>(typeParameters.size()); i2++ )
790 {
791 Type baseType( DEF_OBJECT, *classes.GetObjectClassPtr() );
792 if( typeParameterBaseClassNames[i2].size() )
793 {
794 if( !compiler.StringToType( typeParameterBaseClassNames[i2], baseType ) )
795 {
796 compiler.errorMessenger.Output(106,typeParameterBaseClassNames[i2],i);
797 }
798 else if( !baseType.IsObject() )
799 {
800 compiler.errorMessenger.Output(106,typeParameterBaseClassNames[i2],i);
801 }
802 }
803
804 pobj_c->AddFormalGenericType( GenericType( typeParameters[i2], baseType ) );
805 }
806 /////////////////////////////////////////////////////////
807
808 pobj_c->Readed();
809
810 pobj_c->SetConstructorMemberSubIndex( -1 );
811 pobj_c->SetDestructorMemberSubIndex( -1 );
812
813 if( memcmp( basbuf+i+1, "__COM", 5 ) == 0 && IsCommandDelimitation( basbuf[i+1+5] ) )
814 {
815 // COMインターフェイス
816 pobj_c->SetClassType( CClass::ComInterface );
817
818 i += 6;
819 }
820
821 if(basbuf[i+1]==1&&basbuf[i+2]==ESC_INHERITS){
822 //継承を行う場合
823 for(i+=3,i2=0;;i++,i2++){
824 if(IsCommandDelimitation(basbuf[i])){
825 temporary[i2]=0;
826 break;
827 }
828 temporary[i2]=basbuf[i];
829 }
830
831 if(lstrcmpi(temporary,pobj_c->GetName().c_str())==0){
832 compiler.errorMessenger.Output(105,temporary,i);
833 goto Interface_InheritsError;
834 }
835
836 //継承元クラスを取得
837 const CClass *pInheritsClass = compiler.GetObjectModule().meta.FindClassSupportedTypeDef(
838 LexicalAnalyzer::FullNameToSymbol( temporary )
839 );
840 if( !pInheritsClass ){
841 compiler.errorMessenger.Output(106,temporary,i);
842 goto Interface_InheritsError;
843 }
844
845 //ループ継承でないかをチェック
846 if( !LexicalAnalyzer::LoopRefCheck( *pInheritsClass ) )
847 {
848 compiler.errorMessenger.Output(123,pInheritsClass->GetName(),i);
849 goto Interface_InheritsError;
850 }
851
852 //継承させる
853 if( !pobj_c->InheritsClass( *pInheritsClass, Types(), i ) ){
854 goto Interface_InheritsError;
855 }
856 }
857 else{
858 //継承無し
859 if( &pobj_c->GetSuperClass() || pobj_c->GetVtblNum() )
860 {
861 // TODO: ここに来ないことが実証できたらこの分岐は消す
862 Jenga::Throw( "GetClass_recur内の例外" );
863 }
864 }
865Interface_InheritsError:
866
867 //メンバ変数、関数を取得
868 while(1){
869 i++;
870
871 //エラー
872 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE||basbuf[i+1]==ESC_INTERFACE)){
873 compiler.errorMessenger.Output(22,"Interface",i);
874 i--;
875 break;
876 }
877
878 if(basbuf[i]==1&&basbuf[i+1]==ESC_INHERITS){
879 compiler.errorMessenger.Output(111,NULL,i);
880 break;
881 }
882 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_IMPLEMENTS )
883 {
884 compiler.errorMessenger.Output(137, NULL, i );
885 break;
886 }
887
888 sub_address=i;
889
890 for(i2=0;;i++,i2++){
891 if(IsCommandDelimitation(basbuf[i])){
892 temporary[i2]=0;
893 break;
894 }
895 temporary[i2]=basbuf[i];
896 }
897 if(temporary[0]=='\0'){
898 if(basbuf[i]=='\0'){
899 i--;
900 compiler.errorMessenger.Output(22,"Interface",top_pos);
901 break;
902 }
903 continue;
904 }
905
906 //End Interface記述の場合
907 if(temporary[0]==1&&temporary[1]==ESC_ENDINTERFACE) break;
908
909 if(!(temporary[0]==1&&(
910 temporary[1]==ESC_SUB||temporary[1]==ESC_FUNCTION
911 ))){
912 compiler.errorMessenger.Output(1,NULL,i);
913 break;
914 }
915
916 //関数ハッシュへ登録
917 char interfaceName[VN_SIZE] = "";
918 UserProc *pUserProc = LexicalAnalyzer::ParseUserProc( NamespaceScopes(), NamespaceScopesCollection(), temporary,sub_address,true,pobj_c, false, interfaceName );
919 if( pUserProc )
920 {
921 // 関数を追加
922 if( compiler.GetObjectModule().meta.GetUserProcs().IsExist( pUserProc ) )
923 {
924 // 既に存在している
925 compiler.errorMessenger.Output(15,pUserProc->GetName(),i);
926
927 delete pUserProc;
928 }
929 else
930 {
931 compiler.GetObjectModule().meta.GetUserProcs().Put( pUserProc );
932 }
933
934 //メンバ関数を追加
935 LexicalAnalyzer::AddMethod(pobj_c,
936 pUserProc,
937 Prototype::Public, //Publicアクセス権
938 0, // bStatic
939 false, // isConst
940 true, // isAbstract
941 true, // isVirtual
942 false, // isOverride
943 interfaceName,
944 false, // isAutoGeneration
945 sub_address
946 );
947 }
948 }
949 }
950
951 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE)){
952 //////////////////////////
953 // クラス
954 //////////////////////////
955
956 top_pos=i;
957
958 const DWORD dwClassType=basbuf[i+1];
959
960 i+=2;
961
962 int iAlign=0;
963 if(memicmp(basbuf+i,"Align(",6)==0){
964 //アラインメント修飾子
965 i+=6;
966 i+=GetStringInPare_RemovePare(temporary,basbuf+i)+1;
967 iAlign=atoi(temporary);
968
969 if( dwClassType != ESC_TYPE )
970 {
971 compiler.errorMessenger.Output(140,NULL,i);
972 }
973
974 if(!(iAlign==1||iAlign==2||iAlign==4||iAlign==8||iAlign==16))
975 compiler.errorMessenger.Output(51,NULL,i);
976 }
977 else if( memicmp( basbuf + i, "Blittable(", 10 ) == 0 ){
978 // Blittable修飾子
979 i+=10;
980 i=JumpStringInPare(basbuf,i)+1;
981
982 if( dwClassType != ESC_CLASS )
983 {
984 compiler.errorMessenger.Output(141,NULL,i);
985 }
986 }
987
988 if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENUM )
989 {
990 // 列挙型の場合
991 i += 2;
992 }
993 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_DELEGATE )
994 {
995 // デリゲートの場合
996 i += 2;
997 }
998
999 //クラス名を取得
1000 GetCommandToken( temporary, basbuf, i );
1001
1002 char className[VN_SIZE];
1003 Jenga::Common::Strings typeParameters;
1004 Jenga::Common::Strings typeParameterBaseClassNames;
1005 SplitGenericClassInstance( temporary, className, typeParameters, true, &typeParameterBaseClassNames );
1006
1007 CClass *pobj_c = const_cast<CClass *>( classes.FindEx( Symbol( namespaceScopes, className ) ) );
1008 if(!pobj_c) continue;
1009
1010 compiler.SetCompilingClass( pobj_c );
1011
1012 if(lpszInheritsClass){
1013 if( pobj_c->GetName() != lpszInheritsClass ){
1014 //継承先先読み用
1015 continue;
1016 }
1017 }
1018
1019 if( lstrcmp(className,"Control")==0)
1020 {
1021 int test=0;
1022 }
1023
1024 if(pobj_c->IsReady()){
1025 //既に先読みされているとき
1026 continue;
1027 }
1028
1029
1030 /////////////////////////////////////////////////////////
1031 // ☆★☆ ジェネリクスサポート ☆★☆
1032 for( i2=0; i2<static_cast<int>(typeParameters.size()); i2++ )
1033 {
1034 Type baseType( DEF_OBJECT, *classes.GetObjectClassPtr() );
1035 if( typeParameterBaseClassNames[i2].size() )
1036 {
1037 if( !compiler.StringToType( typeParameterBaseClassNames[i2], baseType ) )
1038 {
1039 compiler.errorMessenger.Output(106,typeParameterBaseClassNames[i2],i);
1040 }
1041 else if( !baseType.IsObject() )
1042 {
1043 compiler.errorMessenger.Output(106,typeParameterBaseClassNames[i2],i);
1044 }
1045 }
1046
1047 pobj_c->AddFormalGenericType( GenericType( typeParameters[i2], baseType ) );
1048 }
1049 /////////////////////////////////////////////////////////
1050
1051
1052 pobj_c->SetFixedAlignment( iAlign );
1053
1054 pobj_c->Readed();
1055
1056 pobj_c->SetConstructorMemberSubIndex( -1 );
1057 pobj_c->SetDestructorMemberSubIndex( -1 );
1058
1059 //アクセス制限の初期値をセット
1060 Prototype::Accessibility accessibility;
1061 if(dwClassType==ESC_CLASS){
1062 accessibility = Prototype::Private;
1063 }
1064 else{
1065 accessibility = Prototype::Public;
1066 }
1067
1068 if( pobj_c->GetName() == "Object"
1069 || dwClassType == ESC_TYPE )
1070 {
1071 // 何も継承しない
1072
1073 if( &pobj_c->GetSuperClass() || pobj_c->GetVtblNum() )
1074 {
1075 // TODO: ここに来ないことが実証できたらこの分岐は消す
1076 Jenga::Throw( "GetClass_recur内の例外" );
1077 }
1078 }
1079 else{
1080 if(basbuf[i+1]==1&&basbuf[i+2]==ESC_INHERITS)
1081 {
1082 // クラス継承先が指定されているとき
1083 i += 3;
1084 GetCommandToken( temporary, basbuf, i );
1085
1086 if(lstrcmpi(temporary,pobj_c->GetName().c_str())==0){
1087 compiler.errorMessenger.Output(105,temporary,i);
1088 goto InheritsError;
1089 }
1090 }
1091 else
1092 {
1093 // 何の指定もないときはObjectクラスを継承する
1094 lstrcpy( temporary, "Object" );
1095 }
1096 LexicalAnalyzer::Inherits( *pobj_c, temporary, i );
1097
1098 if( basbuf[i+1] == 1 && basbuf[i+2] == ESC_IMPLEMENTS )
1099 {
1100 // インターフェイス実装を行う場合
1101 i += 3;
1102 GetCommandToken( temporary, basbuf, i );
1103
1104 LexicalAnalyzer::Implements( *pobj_c, temporary, i );
1105 }
1106 }
1107InheritsError:
1108
1109 //メンバとメソッドを取得
1110 while(1){
1111 i++;
1112
1113 //エラー
1114 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE)){
1115 compiler.errorMessenger.Output(22,"Class",i);
1116 i--;
1117 break;
1118 }
1119
1120 if(basbuf[i]==1&&basbuf[i+1]==ESC_INHERITS){
1121 compiler.errorMessenger.Output(111,NULL,i);
1122 break;
1123 }
1124 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_IMPLEMENTS )
1125 {
1126 compiler.errorMessenger.Output(137, NULL, i );
1127 break;
1128 }
1129
1130 //Static修飾子
1131 BOOL bStatic;
1132 if(basbuf[i]==1&&basbuf[i+1]==ESC_STATIC){
1133 bStatic=1;
1134 i+=2;
1135 }
1136 else bStatic=0;
1137
1138 //Const修飾子
1139 bool isConst = false;
1140 if( basbuf[i] == 1 && basbuf[i + 1] == ESC_CONST ){
1141 isConst = true;
1142 i += 2;
1143 }
1144
1145 if(basbuf[i]==1&&(
1146 basbuf[i+1]==ESC_ABSTRACT||basbuf[i+1]==ESC_VIRTUAL||basbuf[i+1]==ESC_OVERRIDE||
1147 basbuf[i+1]==ESC_SUB||basbuf[i+1]==ESC_FUNCTION
1148 )){
1149 i3=basbuf[i+1];
1150 sub_address=i;
1151 }
1152 else i3=0;
1153
1154 bool isVirtual = false, isAbstract = false, isOverride = false;
1155 if(i3==ESC_ABSTRACT){
1156 isAbstract=1;
1157 isVirtual=1;
1158 i+=2;
1159
1160 i3=basbuf[i+1];
1161 }
1162 else if(i3==ESC_VIRTUAL){
1163 isAbstract=0;
1164 isVirtual=1;
1165 i+=2;
1166
1167 i3=basbuf[i+1];
1168 }
1169 else if(i3==ESC_OVERRIDE){
1170 isOverride=1;
1171 isVirtual=1;
1172
1173 i+=2;
1174
1175 i3=basbuf[i+1];
1176 }
1177
1178 for(i2=0;;i++,i2++){
1179 if(IsCommandDelimitation(basbuf[i])){
1180 temporary[i2]=0;
1181 break;
1182 }
1183 temporary[i2]=basbuf[i];
1184 }
1185 if(temporary[0]=='\0'){
1186 if(basbuf[i]=='\0'){
1187
1188 if(dwClassType==ESC_CLASS)
1189 compiler.errorMessenger.Output(22,"Class",top_pos);
1190 else
1191 compiler.errorMessenger.Output(22,"Type",top_pos);
1192
1193 i--;
1194 break;
1195 }
1196 continue;
1197 }
1198
1199 //End Class記述の場合
1200 if(temporary[0]==1&&temporary[1]==ESC_ENDCLASS&&dwClassType==ESC_CLASS) break;
1201 if(temporary[0]==1&&temporary[1]==ESC_ENDTYPE&&dwClassType==ESC_TYPE) break;
1202
1203 //アクセスを変更
1204 if(lstrcmpi(temporary,"Private")==0){
1205 accessibility = Prototype::Private;
1206 continue;
1207 }
1208 if(lstrcmpi(temporary,"Public")==0){
1209 accessibility = Prototype::Public;
1210 continue;
1211 }
1212 if(lstrcmpi(temporary,"Protected")==0){
1213 accessibility = Prototype::Protected;
1214 continue;
1215 }
1216
1217 extern int cp;
1218 if(i3==0){
1219 if(bStatic){
1220 //静的メンバを追加
1221 cp=i; //エラー用
1222 pobj_c->AddStaticMember(
1223 LexicalAnalyzer::CreateMember( *pobj_c, accessibility, isConst, false, temporary, i )
1224 );
1225 }
1226 else{
1227 //メンバを追加
1228 cp=i; //エラー用
1229 pobj_c->AddDynamicMember(
1230 LexicalAnalyzer::CreateMember( *pobj_c, accessibility, isConst, false, temporary, i )
1231 );
1232
1233
1234 if(pobj_c->GetDynamicMembers().back()->GetType().IsStruct()){
1235 if( !pobj_c->GetDynamicMembers().back()->GetType().GetClass().IsReady() ){
1236 //参照先が読み取られていないとき
1237 GetClass_recur( pobj_c->GetDynamicMembers().back()->GetType().GetClass().GetName().c_str(), classes );
1238 }
1239 }
1240
1241
1242 if(pobj_c->GetDynamicMembers().back()->GetType().IsStruct()){
1243 //循環参照のチェック
1244 pobj_LoopRefCheck->add(pobj_c->GetName().c_str());
1245 if(!MemberVar_LoopRefCheck(pobj_c->GetDynamicMembers().back()->GetType().GetClass())){
1246 //エラー回避
1247 Type &type = const_cast<Type &>(pobj_c->GetDynamicMembers().back()->GetType());
1248 type.SetBasicType( DEF_PTR_VOID );
1249 }
1250 pobj_LoopRefCheck->del(pobj_c->GetName().c_str());
1251 }
1252 }
1253 }
1254 else{
1255 //関数ハッシュへ登録
1256 char interfaceName[VN_SIZE] = "";
1257 UserProc *pUserProc = LexicalAnalyzer::ParseUserProc( NamespaceScopes(), NamespaceScopesCollection(), temporary,sub_address,isVirtual,pobj_c, (bStatic!=0), interfaceName );
1258 if( pUserProc )
1259 {
1260 // 関数を追加
1261 if( compiler.GetObjectModule().meta.GetUserProcs().IsExist( pUserProc ) )
1262 {
1263 // 既に存在している
1264 compiler.errorMessenger.Output(15,pUserProc->GetName(),i);
1265
1266 delete pUserProc;
1267 }
1268 else
1269 {
1270 compiler.GetObjectModule().meta.GetUserProcs().Put( pUserProc );
1271 }
1272
1273 //メソッドを追加
1274 cp=i; //エラー用
1275 LexicalAnalyzer::AddMethod(pobj_c,
1276 pUserProc,
1277 accessibility,
1278 bStatic,
1279 isConst,
1280 isAbstract,
1281 isVirtual,
1282 isOverride,
1283 interfaceName,
1284 false,
1285 sub_address);
1286 }
1287
1288 if( isAbstract ) continue;
1289
1290 for(;;i++){
1291 if(basbuf[i]=='\0'){
1292 i--;
1293 break;
1294 }
1295 if(basbuf[i-1]!='*'&&
1296 basbuf[i]==1&&(
1297 basbuf[i+1]==ESC_SUB||
1298 basbuf[i+1]==ESC_FUNCTION||
1299 basbuf[i+1]==ESC_MACRO||
1300 basbuf[i+1]==ESC_TYPE||
1301 basbuf[i+1]==ESC_CLASS||
1302 basbuf[i+1]==ESC_INTERFACE||
1303 basbuf[i+1]==ESC_ENUM)){
1304 GetDefaultNameFromES(i3,temporary);
1305 compiler.errorMessenger.Output(22,temporary,i);
1306 }
1307 if(basbuf[i]==1&&basbuf[i+1]==GetEndXXXCommand((char)i3)){
1308 i+=2;
1309 break;
1310 }
1311 }
1312 }
1313 }
1314 }
1315 }
1316
1317 // 呼び出し元でコンパイル中のクラスポインタを元に戻す
1318 compiler.SetCompilingClass( pBackCompilingClass );
1319
1320 // 名前空間を元に戻す
1321 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes() = backupNamespaceScopes;
1322
1323 // インポートされた名前空間を元に戻す
1324 compiler.GetNamespaceSupporter().GetImportedNamespaces() = backupImportedNamespaces;
1325}
1326
1327void LexicalAnalyzer::LookaheadClass( const char *className, Classes &classes )
1328{
1329 pobj_LoopRefCheck->add( className );
1330 GetClass_recur( className, classes );
1331 pobj_LoopRefCheck->del( className );
1332}
1333
1334bool LexicalAnalyzer::LoopRefCheck( const CClass &objClass )
1335{
1336 if( pobj_LoopRefCheck->check( objClass ) )
1337 {
1338 return false;
1339 }
1340
1341 return true;
1342}
1343
1344void LexicalAnalyzer::CollectClasses( const char *source, Classes &classes ){
1345 //ループ継承チェック用のクラス
1346 pobj_LoopRefCheck=new CLoopRefCheck();
1347
1348 //クラスを取得
1349 GetClass_recur( 0, classes );
1350
1351 delete pobj_LoopRefCheck;
1352 pobj_LoopRefCheck=0;
1353
1354 // イテレータの準備
1355 classes.Iterator_Init();
1356}
Note: See TracBrowser for help on using the repository browser.