source: dev/trunk/abdev/BasicCompiler_Common/src/Class.cpp@ 322

Last change on this file since 322 was 322, checked in by dai_9181, 17 years ago

コンパイラ組み込みテンプレートエンジンを実装。
静的リンクライブラリ、デバッグ情報の内部形式をテキストからバイナリに変更した。

File size: 44.0 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/SmoothieException.h>
5#include <jenga/include/smoothie/LexicalAnalysis.h>
6
7#include <Source.h>
8#include <Class.h>
9#include <Compiler.h>
10#include <NamespaceSupporter.h>
11
12#include "../common.h"
13#ifdef _AMD64_
14#include "../../BasicCompiler64/opcode.h"
15#else
16#include "../../BasicCompiler32/opcode.h"
17#endif
18
19
20class CLoopRefCheck{
21 char **names;
22 int num;
23 void init(){
24 int i;
25 for(i=0;i<num;i++){
26 free(names[i]);
27 }
28 free(names);
29 }
30public:
31 CLoopRefCheck()
32 {
33 names=(char **)malloc(1);
34 num=0;
35 }
36 ~CLoopRefCheck()
37 {
38 init();
39 }
40 void add(const char *lpszInheritsClass)
41 {
42 names=(char **)realloc(names,(num+1)*sizeof(char *));
43 names[num]=(char *)malloc(lstrlen(lpszInheritsClass)+1);
44 lstrcpy(names[num],lpszInheritsClass);
45 num++;
46 }
47 void del(const char *lpszInheritsClass)
48 {
49 int i;
50 for(i=0;i<num;i++){
51 if(lstrcmp(names[i],lpszInheritsClass)==0){
52 free(names[i]);
53 break;
54 }
55 }
56 if(i!=num){
57 num--;
58 for(;i<num;i++){
59 names[i]=names[i+1];
60 }
61 }
62 }
63 BOOL check(const CClass &inheritsClass) const
64 {
65 //ループ継承チェック
66 int i;
67 for(i=0;i<num;i++){
68 if( inheritsClass.GetName() == names[i] ){
69 return 1;
70 }
71 }
72 return 0;
73 }
74};
75CLoopRefCheck *pobj_LoopRefCheck;
76
77
78bool CClass::IsClass() const
79{
80 return classType == CClass::Class;
81}
82bool CClass::IsInterface() const
83{
84 return classType == CClass::Interface;
85}
86bool CClass::IsEnum() const
87{
88 return classType == CClass::Enum;
89}
90bool CClass::IsDelegate() const
91{
92 return classType == CClass::Delegate;
93}
94bool CClass::IsStructure() const
95{
96 return classType == CClass::Structure;
97}
98
99
100// コンストラクタのコンパイルを開始
101void CClass::NotifyStartConstructorCompile() const
102{
103 isCompilingConstructor = true;
104}
105
106//コンストラクタのコンパイルを終了
107void CClass::NotifyFinishConstructorCompile() const
108{
109 isCompilingConstructor = false;
110}
111
112//コンストラクタをコンパイル中かどうかを判別
113bool CClass::IsCompilingConstructor() const
114{
115 return isCompilingConstructor;
116}
117
118//デストラクタのコンパイルを開始
119void CClass::NotifyStartDestructorCompile() const{
120 isCompilingDestructor = true;
121}
122
123//デストラクタのコンパイルを終了
124void CClass::NotifyFinishDestructorCompile() const{
125 isCompilingDestructor = false;
126}
127
128//デストラクタをコンパイル中かどうかを判別
129bool CClass::IsCompilingDestructor() const
130{
131 return isCompilingDestructor;
132}
133
134//自身の派生クラスかどうかを確認
135bool CClass::IsSubClass( const CClass *pClass ) const
136{
137 if( !pClass->HasSuperClass() )
138 {
139 return false;
140 }
141
142 const CClass *pTempClass = &pClass->GetSuperClass();
143 while( pTempClass ){
144 if( this == pTempClass ) return true;
145 pTempClass = &pTempClass->GetSuperClass();
146 }
147 return false;
148}
149
150//自身と等しいまたは派生クラスかどうかを確認
151bool CClass::IsEqualsOrSubClass( const CClass *pClass ) const
152{
153 if( IsEquals( pClass ) ) return true;
154 return IsSubClass( pClass );
155}
156
157// 自身と等しいまたは派生クラス、基底クラスかどうかを確認
158bool CClass::IsEqualsOrSubClassOrSuperClass( const CClass &objClass ) const
159{
160 if( IsEquals( &objClass ) ) return true;
161 if( IsSubClass( &objClass ) ) return true;
162 if( objClass.IsSubClass( this ) ) return true;
163 return false;
164}
165
166bool CClass::IsInheritsInterface( const CClass *pInterfaceClass ) const
167{
168 BOOST_FOREACH( const InheritedInterface &objInterface, interfaces ){
169 if( pInterfaceClass == &objInterface.GetInterfaceClass() ){
170 return true;
171 }
172 }
173 return false;
174}
175
176bool CClass::Inherits( const char *inheritNames, int nowLine ){
177 int i = 0;
178 bool isInheritsClass = false;
179 while( true ){
180
181 char temporary[VN_SIZE];
182 for( int i2=0;; i++, i2++ ){
183 if( inheritNames[i] == '\0' || inheritNames[i] == ',' ){
184 temporary[i2] = 0;
185 break;
186 }
187 temporary[i2] = inheritNames[i];
188 }
189
190 // ジェネリクス構文を分解
191 char className[VN_SIZE];
192 Jenga::Common::Strings typeParameterStrings;
193 SplitGenericClassInstance( temporary, className, typeParameterStrings );
194
195 // 型パラメータ文字列から型データを取得
196 std::vector<Type> actualTypeParameters;
197 BOOST_FOREACH( const std::string &typeParameterStr, typeParameterStrings )
198 {
199 Type type;
200 compiler.StringToType( typeParameterStr, type );
201 actualTypeParameters.push_back( type );
202 }
203
204 //継承元クラスを取得
205 const CClass *pInheritsClass = compiler.GetObjectModule().meta.GetClasses().Find(className);
206 if( !pInheritsClass ){
207 SmoothieException::Throw(106,className,nowLine);
208 return false;
209 }
210
211 if( pInheritsClass->IsInterface() ){
212 // インターフェイスはあとで継承する
213 }
214 else if( pInheritsClass->IsClass() ){
215 // クラスを継承する
216 isInheritsClass = true;
217
218 if( !InheritsClass( *pInheritsClass, actualTypeParameters, nowLine ) ){
219 return false;
220 }
221 }
222 else{
223 SmoothieException::Throw(135,NULL,nowLine);
224 return false;
225 }
226
227 if( inheritNames[i] == '\0' ){
228 break;
229 }
230 i++;
231 }
232
233 if( !isInheritsClass ){
234 // クラスを一つも継承していないとき
235 if( !InheritsClass( *compiler.GetObjectModule().meta.GetClasses().GetObjectClassPtr(), Types(), nowLine ) ){
236 return false;
237 }
238 }
239
240 i=0;
241 while( true ){
242
243 char temporary[VN_SIZE];
244 for( int i2=0;; i++, i2++ ){
245 if( inheritNames[i] == '\0' || inheritNames[i] == ',' ){
246 temporary[i2] = 0;
247 break;
248 }
249 temporary[i2] = inheritNames[i];
250 }
251
252 char className[VN_SIZE];
253 Jenga::Common::Strings typeParameters;
254 SplitGenericClassInstance( temporary, className, typeParameters );
255
256 //継承元クラスを取得
257 const CClass *pInheritsClass = compiler.GetObjectModule().meta.GetClasses().Find(className);
258 if( !pInheritsClass ){
259 SmoothieException::Throw(106,className,nowLine);
260 return false;
261 }
262
263 if( pInheritsClass->IsInterface() ){
264 // インターフェイスを継承する
265 if( !InheritsInterface( *pInheritsClass, nowLine ) ){
266 return false;
267 }
268 }
269 else if( pInheritsClass->IsClass() ){
270 // クラスはさっき継承した
271 }
272 else{
273 SmoothieException::Throw(135,NULL,nowLine);
274 return false;
275 }
276
277 if( inheritNames[i] == '\0' ){
278 break;
279 }
280 i++;
281 }
282
283 return true;
284}
285bool CClass::InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine ){
286
287 //ループ継承でないかをチェック
288 if(pobj_LoopRefCheck->check(inheritsClass)){
289 SmoothieException::Throw(123,inheritsClass.GetName(),nowLine);
290 return false;
291 }
292
293 if( !inheritsClass.IsReady() ){
294 //継承先が読み取られていないとき
295 pobj_LoopRefCheck->add(this->GetName().c_str());
296 compiler.GetObjectModule().meta.GetClasses().GetClass_recur(inheritsClass.GetName().c_str());
297 pobj_LoopRefCheck->del(this->GetName().c_str());
298 }
299
300 //メンバをコピー
301 BOOST_FOREACH( CMember *inheritsClassDynamicMember, inheritsClass.GetDynamicMembers() ){
302 CMember *pMember = new CMember( *inheritsClassDynamicMember );
303
304 // アクセシビリティ
305 if( inheritsClassDynamicMember->IsPrivate() ){
306 pMember->SetAccessibility( Prototype::None );
307 }
308 else{
309 pMember->SetAccessibility( inheritsClassDynamicMember->GetAccessibility() );
310 }
311
312 // メンバのみ、型パラメータを解決する(メソッドのほうは呼び出し時に解決する)
313 if( pMember->GetType().IsTypeParameter() )
314 {
315 pMember->ResetType( actualTypeParameters[pMember->GetType().GetFormalTypeIndex()] );
316 }
317
318 dynamicMembers.push_back( pMember );
319 }
320
321 //メソッドをコピー
322 BOOST_FOREACH( const CMethod *pBaseMethod, inheritsClass.GetMethods() ){
323 CMethod *pMethod = new DynamicMethod( *pBaseMethod );
324
325 // アクセシビリティ
326 if(pBaseMethod->GetAccessibility() == Prototype::Private){
327 pMethod->SetAccessibility( Prototype::None );
328 }
329 else{
330 pMethod->SetAccessibility( pBaseMethod->GetAccessibility() );
331 }
332
333 //pobj_Inherits
334 // ※継承元のClassIndexをセット(入れ子継承を考慮する)
335 if(pBaseMethod->GetInheritsClassPtr()==0){
336 pMethod->SetInheritsClassPtr( &inheritsClass );
337 }
338 else{
339 pMethod->SetInheritsClassPtr( pBaseMethod->GetInheritsClassPtr() );
340 }
341
342 methods.push_back( pMethod );
343 }
344
345 //仮想関数の数
346 AddVtblNum( inheritsClass.GetVtblNum() );
347
348 //継承先のクラスをメンバとして保持する
349 SetSuperClass( &inheritsClass );
350 SetSuperClassActualTypeParameters( actualTypeParameters );
351
352 return true;
353}
354bool CClass::InheritsInterface( const CClass &inheritsInterface, int nowLine ){
355
356 //ループ継承でないかをチェック
357 if(pobj_LoopRefCheck->check(inheritsInterface)){
358 SmoothieException::Throw(123,inheritsInterface.GetName(),nowLine);
359 return false;
360 }
361
362 if( !inheritsInterface.IsReady() ){
363 //継承先が読み取られていないとき
364 pobj_LoopRefCheck->add(this->GetName().c_str());
365 compiler.GetObjectModule().meta.GetClasses().GetClass_recur(inheritsInterface.GetName().c_str());
366 pobj_LoopRefCheck->del(this->GetName().c_str());
367 }
368
369 //メソッドをコピー
370 BOOST_FOREACH( const CMethod *pBaseMethod, inheritsInterface.GetMethods() ){
371 CMethod *pMethod = new DynamicMethod( *pBaseMethod );
372
373 // アクセシビリティ
374 if(pBaseMethod->GetAccessibility() == Prototype::Private){
375 pMethod->SetAccessibility( Prototype::None );
376 }
377 else{
378 pMethod->SetAccessibility( pBaseMethod->GetAccessibility() );
379 }
380
381 //pobj_Inherits
382 // ※継承元のClassIndexをセット(入れ子継承を考慮する)
383 if(pBaseMethod->GetInheritsClassPtr()==0){
384 pMethod->SetInheritsClassPtr( &inheritsInterface );
385 }
386 else{
387 pMethod->SetInheritsClassPtr( pBaseMethod->GetInheritsClassPtr() );
388 }
389
390 methods.push_back( pMethod );
391 }
392
393 interfaces.push_back( InheritedInterface( const_cast<CClass *>(&inheritsInterface), vtblNum ) );
394
395 //仮想関数の数
396 AddVtblNum( inheritsInterface.GetVtblNum() );
397
398 return true;
399}
400CMember *CClass::CreateMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine )
401{
402 extern int cp;
403
404 //構文を解析
405 char VarName[VN_SIZE];
406 char initBuffer[VN_SIZE];
407 char lpszConstructParameter[VN_SIZE];
408 Subscripts subscripts;
409 Type type;
410 GetDimentionFormat(buffer,VarName,subscripts,type,initBuffer,lpszConstructParameter);
411
412 //重複チェック
413 if(this->DupliCheckAll(VarName)){
414 SetError(15,VarName,cp);
415 }
416
417 CMember *pMember = new CMember( accessibility, VarName, type, isConst, subscripts, initBuffer, lpszConstructParameter );
418 pMember->source_code_address = nowLine;
419 return pMember;
420}
421void CClass::AddMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine ){
422 dynamicMembers.push_back(
423 CreateMember( accessibility, isConst, isRef, buffer, nowLine )
424 );
425}
426void CClass::AddStaticMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine ){
427 staticMembers.push_back(
428 CreateMember( accessibility, isConst, isRef, buffer, nowLine )
429 );
430}
431
432void CClass::AddMethod(CClass *pobj_c, Prototype::Accessibility accessibility, BOOL bStatic, bool isConst, bool isAbstract,
433 bool isVirtual, bool isOverride, char *buffer, int nowLine){
434 int i,i2;
435 char temporary[VN_SIZE];
436
437 i=2;
438 for(i2=0;;i++,i2++){
439 if(buffer[i]=='('||buffer[i]=='\0'){
440 temporary[i2]=0;
441 break;
442 }
443 temporary[i2]=buffer[i];
444 }
445
446
447 //関数ハッシュへ登録
448 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Add( NamespaceScopes(), NamespaceScopesCollection(), buffer,nowLine,isVirtual,pobj_c, (bStatic!=0) );
449 if(!pUserProc) return;
450
451
452 ////////////////////////////////////////////////////////////
453 // コンストラクタ、デストラクタの場合の処理
454 ////////////////////////////////////////////////////////////
455 BOOL fConstructor=0,bDestructor=0;
456
457 if(lstrcmp(temporary,pobj_c->GetName().c_str())==0){
458 //コンストラクタの場合
459
460 //標準コンストラクタ(引数なし)
461 if(pUserProc->Params().size()==0) fConstructor=1;
462
463 //強制的にConst修飾子をつける
464 isConst = true;
465 }
466 else if(temporary[0]=='~'){
467 //デストラクタの場合はその名前が正しいかチェックを行う
468 if(lstrcmp(temporary+1,pobj_c->GetName().c_str())!=0)
469 SetError(117,NULL,nowLine);
470 else
471 bDestructor=1;
472 }
473 if(fConstructor||bDestructor){
474 // コンストラクタ、デストラクタのアクセシビリティをチェック
475
476 //強制的にConst修飾子をつける
477 isConst = true;
478 }
479
480 if( fConstructor == 1 )
481 pobj_c->SetConstructorMemberSubIndex( (int)pobj_c->GetMethods().size() );
482 else if( bDestructor )
483 pobj_c->SetDestructorMemberSubIndex( (int)pobj_c->GetMethods().size() );
484
485
486
487 //////////////////
488 // 重複チェック
489 //////////////////
490
491 if(pobj_c->DupliCheckMember(temporary)){
492 SetError(15,temporary,nowLine);
493 return;
494 }
495
496 //メソッド
497 BOOST_FOREACH( const CMethod *pMethod, pobj_c->GetMethods() ){
498 //基底クラスと重複する場合はオーバーライドを行う
499 if( pMethod->GetInheritsClassPtr() ) continue;
500
501 if( pMethod->GetUserProc().GetName() == temporary ){
502 if( pMethod->GetUserProc().Params().Equals( pUserProc->Params() ) ){
503 //関数名、パラメータ属性が合致したとき
504 SetError(15,pUserProc->GetName().c_str(),nowLine);
505 return;
506 }
507 }
508 }
509
510 //仮想関数の場合
511 if( isAbstract ) pUserProc->CompleteCompile();
512
513 //メソッドのオーバーライド
514 BOOST_FOREACH( CMethod *pMethod, pobj_c->GetMethods() ){
515 if( pMethod->GetUserProc().GetName() == temporary ){
516 if( pMethod->GetUserProc().Params().Equals( pUserProc->Params() ) )
517 {
518 //メンバ関数を上書き
519 pMethod->SetUserProcPtr( pUserProc );
520 pMethod->Override();
521
522 if( !pMethod->IsVirtual() )
523 {
524 // オーバーライドミス
525 SetError(136,NULL,cp);
526 }
527 if( !isOverride )
528 {
529 SetError(127,NULL,nowLine);
530 }
531 if(pMethod->GetAccessibility() != accessibility )
532 {
533 SetError(128,NULL,nowLine);
534 }
535
536 pUserProc->SetMethod( pMethod );
537 return;
538 }
539 }
540 }
541
542 if( isVirtual ){
543 pobj_c->AddVtblNum( 1 );
544 }
545
546 if( isOverride ){
547 SetError(12,"Override",nowLine);
548 }
549
550 if(bStatic){
551 pobj_c->GetStaticMethods().AddStatic( pUserProc, accessibility );
552 }
553 else{
554 pobj_c->GetMethods().Add(pUserProc, accessibility, isConst, isAbstract, isVirtual);
555 }
556}
557
558bool CClass::DupliCheckAll(const char *name){
559 //重複チェック
560
561 //メンバ
562 if(DupliCheckMember(name)) return 1;
563
564 //メソッド
565 BOOST_FOREACH( const CMethod *pMethod, methods ){
566 if( lstrcmp( name, pMethod->GetUserProc().GetName().c_str() ) == 0 ){
567 return 1;
568 }
569 }
570
571 return 0;
572}
573bool CClass::DupliCheckMember(const char *name){
574 //重複チェック
575
576 // 動的メンバ
577 BOOST_FOREACH( CMember *pMember, dynamicMembers ){
578 if( GetName() == pMember->GetName() ){
579 return 1;
580 }
581 }
582
583 // 静的メンバ
584 BOOST_FOREACH( CMember *pMember, staticMembers ){
585 if( GetName() == pMember->GetName() ){
586 return 1;
587 }
588 }
589
590 return 0;
591}
592
593//サイズを取得
594int CClass::GetSize() const
595{
596 return GetMemberOffset( NULL, NULL );
597}
598
599//メンバのオフセットを取得
600int CClass::GetMemberOffset( const char *memberName, int *pMemberNum ) const
601{
602 int i2;
603
604 //仮想関数が存在する場合は関数リストへのポインタのサイズを追加
605 int offset = IsExistVirtualFunctions() ? PTR_SIZE : 0;
606
607 int alignment = 1;
608 if( GetFixedAlignment() )
609 {
610 alignment = GetFixedAlignment();
611 }
612
613 int iMaxAlign=0;
614 int i = -1;
615 BOOST_FOREACH( CMember *pMember, dynamicMembers ){
616 i++;
617
618 i2 = pMember->GetType().GetSize();
619
620 //アラインメントを算出
621 int member_size;
622 if( pMember->GetType().IsStruct() ){
623 //メンバクラスのアラインメントを取得
624 member_size=pMember->GetType().GetClass().GetAlignment();
625 }
626 else{
627 //メンバサイズを取得
628 member_size=i2;
629 }
630 if(iMaxAlign<member_size) iMaxAlign=member_size;
631
632 //アラインメントを考慮
633 if(GetFixedAlignment()&&GetFixedAlignment()<member_size){
634 if(offset%alignment) offset+=alignment-(offset%alignment);
635 }
636 else{
637 if(alignment<member_size) alignment=member_size;
638
639 if(member_size==0){
640 //メンバを持たないクラス
641 //※何もしない(オフセットの計算をしない)
642 }
643 else{
644 if(offset%member_size) offset+=member_size-(offset%member_size);
645 }
646 }
647
648 if(memberName){
649 //メンバ指定がある場合は、オフセットを返す
650 if( pMember->GetName() == memberName ){
651 if(pMemberNum) *pMemberNum=i;
652 return offset;
653 }
654 }
655
656 //配列を考慮したメンバサイズを取得
657 member_size = i2 * Variable::GetSubScriptCounts( pMember->GetSubscripts() );
658
659 //メンバサイズを加算
660 offset+= member_size;
661 }
662
663 if(iMaxAlign<alignment) alignment=iMaxAlign;
664
665 //アラインメントを考慮
666 if(alignment){
667 if(offset%alignment) offset+=alignment-(offset%alignment);
668 }
669
670 if(pMemberNum) *pMemberNum=i;
671 return offset;
672}
673int CClass::GetAlignment() const
674{
675 //仮想関数が存在する場合は関数リストへのポインタのサイズを追加
676 int alignment = IsExistVirtualFunctions() ? PTR_SIZE : 0;
677
678 BOOST_FOREACH( CMember *pMember, dynamicMembers ){
679 int member_size;
680 if(pMember->GetType().IsStruct()){
681 //メンバクラスのアラインメントを取得
682 member_size=pMember->GetType().GetClass().GetAlignment();
683 }
684 else{
685 //メンバサイズを取得
686 member_size = pMember->GetType().GetSize();
687 }
688
689 //アラインメントをセット
690 if(alignment<member_size) alignment=member_size;
691 }
692
693 if(alignment==0) return 0;
694
695 if(GetFixedAlignment()) alignment=GetFixedAlignment();
696
697 return alignment;
698}
699int CClass::GetFuncNumInVtbl( const UserProc *pUserProc ) const
700{
701 int n = 0;
702 BOOST_FOREACH( const CMethod *pMethod, methods ){
703 if( &pMethod->GetUserProc() == pUserProc ) break;
704 if( pMethod->IsVirtual() ) n++;
705 }
706 return n;
707}
708LONG_PTR CClass::GetVtblGlobalOffset(void) const
709{
710
711 //既に存在する場合はそれを返す
712 if(vtbl_offset!=-1) return vtbl_offset;
713
714
715
716 //////////////////////////////////////
717 // 存在しないときは新たに生成する
718 //////////////////////////////////////
719
720 const UserProc **ppsi;
721 ppsi=(const UserProc **)malloc(GetVtblNum()*sizeof(UserProc *));
722
723 //関数テーブルに値をセット
724 int i2 = 0;
725 BOOST_FOREACH( const CMethod *pMethod, methods ){
726 if(pMethod->IsVirtual()){
727 pMethod->GetUserProc().Using();
728
729 if(pMethod->IsAbstract()){
730 extern int cp;
731 SmoothieException::Throw(300,NULL,cp);
732
733 ppsi[i2]=0;
734 }
735 else{
736 ppsi[i2]=&pMethod->GetUserProc();
737 }
738 i2++;
739 }
740 }
741
742 vtbl_offset=compiler.GetObjectModule().dataTable.AddBinary((void *)ppsi,GetVtblNum()*sizeof(LONG_PTR));
743
744 for( int i=0; i < GetVtblNum(); i++ ){
745 pobj_Reloc->AddSchedule_DataSection(vtbl_offset+i*sizeof(LONG_PTR));
746 }
747
748 free(ppsi);
749
750 return vtbl_offset;
751}
752void CClass::GenerateVTables()
753{
754 if( IsAbstract() )
755 {
756 // 抽象クラスは無視
757 return;
758 }
759 if( !IsUsing() )
760 {
761 // 使われていないクラスは無視
762 return;
763 }
764
765 const UserProc **ppsi;
766 ppsi=(const UserProc **)malloc(GetVtblNum()*sizeof(UserProc *));
767
768 //関数テーブルに値をセット
769 int i2 = 0;
770 BOOST_FOREACH( const CMethod *pMethod, methods ){
771 if(pMethod->IsVirtual()){
772 if( !pMethod->GetUserProc().IsUsing() )
773 {
774 ts((char *)pMethod->GetUserProc().GetFullName().c_str());
775 }
776 pMethod->GetUserProc().Using();
777
778 if(pMethod->IsAbstract()){
779 extern int cp;
780 SmoothieException::Throw(300,NULL,cp);
781
782 ppsi[i2]=0;
783 }
784 else{
785 ppsi[i2]=&pMethod->GetUserProc();
786 }
787 i2++;
788 }
789 }
790
791 vtbl_offset=compiler.GetObjectModule().dataTable.AddBinary((void *)ppsi,GetVtblNum()*sizeof(LONG_PTR));
792
793 for( int i=0; i < GetVtblNum(); i++ ){
794 pobj_Reloc->AddSchedule_DataSection(vtbl_offset+i*sizeof(LONG_PTR));
795 }
796
797 free(ppsi);
798}
799void CClass::ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection){
800 if( IsAbstract() )
801 {
802 // 抽象クラスは無視
803 return;
804 }
805 if( !IsUsing() )
806 {
807 // 使われていないクラスは無視
808 return;
809 }
810 if(vtbl_offset==-1) return;
811
812 LONG_PTR *pVtbl;
813 pVtbl=(LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr()+vtbl_offset);
814
815 int i;
816 for(i=0;i<GetVtblNum();i++){
817 UserProc *pUserProc;
818 pUserProc=(UserProc *)pVtbl[i];
819 if(!pUserProc) continue;
820
821 if( pUserProc->GetBeginOpAddress() == 0
822 && pUserProc->GetEndOpAddress() == 0 )
823 {
824 Jenga::Throw( "未解決の仮想関数が存在する" );
825 }
826
827 pVtbl[i]=pUserProc->GetBeginOpAddress()+ImageBase+MemPos_CodeSection;
828 }
829}
830bool CClass::IsAbstract() const
831{
832 // 未実装(abstract)の仮想関数を持つ場合はtrueを返す
833
834 BOOST_FOREACH( const CMethod *pMethod, methods ){
835 if(pMethod->IsVirtual()){
836 if(pMethod->IsAbstract()){
837 return true;
838 }
839 }
840 }
841
842 return false;
843}
844
845CClass *Classes::Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name){
846 return new CClass(namespaceScopes, importedNamespaces, name);
847}
848bool Classes::Insert( CClass *pClass )
849{
850 /////////////////////////////////
851 // ハッシュデータに追加
852 /////////////////////////////////
853
854 if( !Put( pClass ) )
855 {
856 SetError(15,pClass->GetName(), cp);
857 return false;
858 }
859 return true;
860}
861CClass *Classes::Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine){
862 //////////////////////////////////////////////////////////////////////////
863 // クラスを追加
864 // ※名前のみを登録。その他の情報はSetClassメソッドで!
865 //////////////////////////////////////////////////////////////////////////
866
867 CClass *pClass = Create(namespaceScopes, importedNamespaces, name);
868
869 if( !Insert( pClass ) )
870 {
871 return NULL;
872 }
873
874 return pClass;
875}
876
877void Classes::CollectClassesForNameOnly( const BasicSource &source )
878{
879 int i, i2;
880 char temporary[VN_SIZE];
881
882 // 名前空間管理
883 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
884 namespaceScopes.clear();
885
886 // Importsされた名前空間の管理
887 NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
888 importedNamespaces.clear();
889
890 for(i=0;;i++){
891 if(source[i]=='\0') break;
892
893 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
894 for(i+=2,i2=0;;i2++,i++){
895 if( IsCommandDelimitation( source[i] ) ){
896 temporary[i2]=0;
897 break;
898 }
899 temporary[i2]=source[i];
900 }
901 namespaceScopes.push_back( temporary );
902
903 continue;
904 }
905 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
906 if( namespaceScopes.size() <= 0 ){
907 SmoothieException::Throw(12, "End Namespace", i );
908 }
909 else{
910 namespaceScopes.pop_back();
911 }
912
913 i += 2;
914 continue;
915 }
916 else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
917 for(i+=2,i2=0;;i2++,i++){
918 if( IsCommandDelimitation( source[i] ) ){
919 temporary[i2]=0;
920 break;
921 }
922 temporary[i2]=source[i];
923 }
924 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
925 {
926 SmoothieException::Throw(64,temporary,i );
927 }
928
929 continue;
930 }
931 else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
932 importedNamespaces.clear();
933 continue;
934 }
935
936 if(source[i]==1&&(
937 source[i+1]==ESC_CLASS||
938 source[i+1]==ESC_TYPE||
939 source[i+1]==ESC_INTERFACE
940 ))
941 {
942 int nowLine = i;
943 i += 2;
944
945 Type blittableType;
946 if(memicmp(source.GetBuffer()+i,"Align(",6)==0){
947 //アラインメント修飾子
948 i+=6;
949 i=JumpStringInPare(source.GetBuffer(),i)+1;
950 }
951 else if( memicmp( source.GetBuffer() + i, "Blittable(", 10 ) == 0 ){
952 // Blittable修飾子
953 i+=10;
954 i+=GetStringInPare_RemovePare(temporary,source.GetBuffer()+i)+1;
955 compiler.StringToType( temporary, blittableType );
956 }
957
958 bool isEnum = false;
959 bool isDelegate = false;
960 if( source[i] == 1 && source[i+1] == ESC_ENUM ){
961 // 列挙型の場合
962 isEnum = true;
963
964 i += 2;
965 }
966 else if( source[i] == 1 && source[i+1] == ESC_DELEGATE )
967 {
968 // デリゲートの場合
969 isDelegate = true;
970
971 i += 2;
972 }
973
974 for(i2=0;;i++,i2++){
975 if(!IsVariableChar(source[i])){
976 temporary[i2]=0;
977 break;
978 }
979 temporary[i2]=source[i];
980 }
981
982 //クラスを追加
983 CClass *pClass = this->Add(namespaceScopes, importedNamespaces, temporary,nowLine);
984 if( pClass ){
985 if( source[nowLine+1] == ESC_CLASS ){
986 if( isEnum )
987 {
988 pClass->SetClassType( CClass::Enum );
989 }
990 else if( isDelegate )
991 {
992 pClass->SetClassType( CClass::Delegate );
993 }
994 else{
995 pClass->SetClassType( CClass::Class );
996 }
997 }
998 else if( source[nowLine+1] == ESC_INTERFACE ){
999 pClass->SetClassType( CClass::Interface );
1000 }
1001 else{
1002 pClass->SetClassType( CClass::Structure );
1003 }
1004 }
1005
1006 // Blittable型の場合
1007 if( !blittableType.IsNull() ){
1008 pClass->SetBlittableType( blittableType );
1009
1010 // Blittable型として登録
1011 compiler.GetObjectModule().meta.GetBlittableTypes().push_back( BlittableType( blittableType, pClass ) );
1012 }
1013 }
1014 }
1015}
1016
1017void Classes::GenerateVTables()
1018{
1019 Iterator_Reset();
1020 while( Iterator_HasNext() )
1021 {
1022 CClass *pClass = Iterator_GetNext();
1023 pClass->GenerateVTables();
1024 }
1025}
1026
1027void Classes::ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection){
1028 Iterator_Reset();
1029 while( Iterator_HasNext() )
1030 {
1031 CClass *pClass = Iterator_GetNext();
1032 pClass->ActionVtblSchedule(ImageBase,MemPos_CodeSection);
1033 }
1034}
1035
1036
1037void Classes::InitStaticMember(){
1038 //静的メンバをグローバル領域に作成
1039
1040 //イテレータをリセット
1041
1042 extern int cp;
1043 int back_cp=cp;
1044
1045 this->Iterator_Reset();
1046 while(this->Iterator_HasNext()){
1047 CClass &objClass = *this->Iterator_GetNext();
1048 if( objClass.isTargetObjectModule == false )
1049 {
1050 // 静的リンクライブラリの場合は飛ばす(既にインスタンスが定義済みであるため)
1051 continue;
1052 }
1053
1054 // 名前空間をセット
1055 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes() = objClass.GetNamespaceScopes();
1056
1057 int i=0;
1058 BOOST_FOREACH( CMember *member, objClass.GetStaticMembers() ){
1059 char temporary[VN_SIZE];
1060 sprintf(temporary,"%s.%s",objClass.GetName().c_str(),member->GetName().c_str());
1061 dim(
1062 temporary,
1063 member->GetSubscripts(),
1064 member->GetType(),
1065 member->GetInitializeExpression().c_str(),
1066 member->GetConstructParameter().c_str(),
1067 0);
1068
1069 i++;
1070 }
1071 }
1072
1073 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().clear();
1074
1075 cp=back_cp;
1076}
1077bool Classes::MemberVar_LoopRefCheck(const CClass &objClass){
1078 bool result = true;
1079 BOOST_FOREACH( CMember *pMember, objClass.GetDynamicMembers() ){
1080 if(pMember->GetType().IsStruct()){
1081 //循環参照でないかをチェック
1082 if(pobj_LoopRefCheck->check(pMember->GetType().GetClass())){
1083 extern int cp;
1084 SetError(124,pMember->GetType().GetClass().GetName(),cp);
1085 return false;
1086 }
1087
1088 pobj_LoopRefCheck->add(objClass.GetName().c_str());
1089
1090 bool tempResult = MemberVar_LoopRefCheck(pMember->GetType().GetClass());
1091 if( result )
1092 {
1093 result = tempResult;
1094 }
1095
1096 pobj_LoopRefCheck->del(objClass.GetName().c_str());
1097 }
1098 }
1099
1100 return result;
1101}
1102void Classes::GetClass_recur(const char *lpszInheritsClass){
1103 extern char *basbuf;
1104 int i,i2,i3,sub_address,top_pos;
1105 char temporary[8192];
1106
1107 // 名前空間管理
1108 NamespaceScopes backupNamespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
1109 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
1110 namespaceScopes.clear();
1111
1112 // Importsされた名前空間の管理
1113 NamespaceScopesCollection backupImportedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
1114 compiler.GetNamespaceSupporter().GetImportedNamespaces().clear();
1115
1116 // 呼び出し元でコンパイル中のクラスポインタをバックアップ
1117 const CClass *pBackCompilingClass = compiler.pCompilingClass;
1118
1119 for(i=0;;i++){
1120 if(basbuf[i]=='\0') break;
1121
1122
1123 // 名前空間
1124 if( basbuf[i] == 1 && basbuf[i+1] == ESC_NAMESPACE ){
1125 for(i+=2,i2=0;;i2++,i++){
1126 if( IsCommandDelimitation( basbuf[i] ) ){
1127 temporary[i2]=0;
1128 break;
1129 }
1130 temporary[i2]=basbuf[i];
1131 }
1132 namespaceScopes.push_back( temporary );
1133
1134 continue;
1135 }
1136 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENDNAMESPACE ){
1137 if( namespaceScopes.size() <= 0 ){
1138 SetError(12, "End Namespace", i );
1139 }
1140 else{
1141 namespaceScopes.pop_back();
1142 }
1143
1144 i += 2;
1145 continue;
1146 }
1147
1148 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_IMPORTS ){
1149 for(i+=2,i2=0;;i2++,i++){
1150 if( IsCommandDelimitation( basbuf[i] ) ){
1151 temporary[i2]=0;
1152 break;
1153 }
1154 temporary[i2]=basbuf[i];
1155 }
1156 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
1157 {
1158 SmoothieException::Throw(64,temporary,i );
1159 }
1160
1161 continue;
1162 }
1163 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
1164 compiler.GetNamespaceSupporter().GetImportedNamespaces().clear();
1165 continue;
1166 }
1167
1168
1169
1170 if(basbuf[i]==1&&basbuf[i+1]==ESC_INTERFACE){
1171 //////////////////////////
1172 // インターフェイス
1173 //////////////////////////
1174
1175 top_pos=i;
1176
1177 i+=2;
1178
1179 //インターフェイス名を取得
1180 GetIdentifierToken( temporary, basbuf, i );
1181
1182 CClass *pobj_c = const_cast<CClass *>( this->Find(namespaceScopes, temporary) );
1183 if(!pobj_c) continue;
1184
1185 if(lpszInheritsClass){
1186 if(lstrcmp(lpszInheritsClass,pobj_c->GetName().c_str())!=0){
1187 //継承先先読み用
1188 continue;
1189 }
1190 }
1191
1192 if(pobj_c->IsReady()){
1193 //既に先読みされているとき
1194 continue;
1195 }
1196
1197 pobj_c->Readed();
1198
1199 pobj_c->SetConstructorMemberSubIndex( -1 );
1200 pobj_c->SetDestructorMemberSubIndex( -1 );
1201
1202 if(basbuf[i+1]==1&&basbuf[i+2]==ESC_INHERITS){
1203 //継承を行う場合
1204 for(i+=3,i2=0;;i++,i2++){
1205 if(IsCommandDelimitation(basbuf[i])){
1206 temporary[i2]=0;
1207 break;
1208 }
1209 temporary[i2]=basbuf[i];
1210 }
1211
1212 if(lstrcmpi(temporary,pobj_c->GetName().c_str())==0){
1213 SetError(105,temporary,i);
1214 goto Interface_InheritsError;
1215 }
1216
1217 //継承元クラスを取得
1218 const Classes &classes = *this;
1219 const CClass *pInheritsClass = classes.Find(temporary);
1220 if( !pInheritsClass ){
1221 SetError(106,temporary,i);
1222 goto Interface_InheritsError;
1223 }
1224
1225 //継承させる
1226 if( !pobj_c->InheritsClass( *pInheritsClass, Types(), i ) ){
1227 goto Interface_InheritsError;
1228 }
1229 }
1230 else{
1231 //継承無し
1232 if( &pobj_c->GetSuperClass() || pobj_c->GetVtblNum() )
1233 {
1234 // TODO: ここに来ないことが実証できたらこの分岐は消す
1235 Jenga::Throw( "GetClass_recur内の例外" );
1236 }
1237 }
1238Interface_InheritsError:
1239
1240 //メンバ変数、関数を取得
1241 while(1){
1242 i++;
1243
1244 //エラー
1245 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE||basbuf[i+1]==ESC_INTERFACE)){
1246 SetError(22,"Interface",i);
1247 i--;
1248 break;
1249 }
1250
1251 if(basbuf[i]==1&&basbuf[i+1]==ESC_INHERITS){
1252 SetError(111,NULL,i);
1253 break;
1254 }
1255
1256 sub_address=i;
1257
1258 for(i2=0;;i++,i2++){
1259 if(IsCommandDelimitation(basbuf[i])){
1260 temporary[i2]=0;
1261 break;
1262 }
1263 temporary[i2]=basbuf[i];
1264 }
1265 if(temporary[0]=='\0'){
1266 if(basbuf[i]=='\0'){
1267 i--;
1268 SetError(22,"Interface",top_pos);
1269 break;
1270 }
1271 continue;
1272 }
1273
1274 //End Interface記述の場合
1275 if(temporary[0]==1&&temporary[1]==ESC_ENDINTERFACE) break;
1276
1277 if(!(temporary[0]==1&&(
1278 temporary[1]==ESC_SUB||temporary[1]==ESC_FUNCTION
1279 ))){
1280 SetError(1,NULL,i);
1281 break;
1282 }
1283
1284 //メンバ関数を追加
1285 pobj_c->AddMethod(pobj_c,
1286 Prototype::Public, //Publicアクセス権
1287 0, //Static指定なし
1288 false, //Constではない
1289 1, //Abstract
1290 1, //Virtual
1291 0,
1292 temporary,
1293 sub_address
1294 );
1295 }
1296 }
1297
1298 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE)){
1299 //////////////////////////
1300 // クラス
1301 //////////////////////////
1302
1303 top_pos=i;
1304
1305 const DWORD dwClassType=basbuf[i+1];
1306
1307 i+=2;
1308
1309 int iAlign=0;
1310 if(memicmp(basbuf+i,"Align(",6)==0){
1311 //アラインメント修飾子
1312 i+=6;
1313 i+=GetStringInPare_RemovePare(temporary,basbuf+i)+1;
1314 iAlign=atoi(temporary);
1315
1316 if(!(iAlign==1||iAlign==2||iAlign==4||iAlign==8||iAlign==16))
1317 SetError(51,NULL,i);
1318 }
1319 else if( memicmp( basbuf + i, "Blittable(", 10 ) == 0 ){
1320 // Blittable修飾子
1321 i+=10;
1322 i=JumpStringInPare(basbuf,i)+1;
1323 }
1324
1325 if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENUM )
1326 {
1327 // 列挙型の場合
1328 i += 2;
1329 }
1330 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_DELEGATE )
1331 {
1332 // デリゲートの場合
1333 i += 2;
1334 }
1335
1336 //クラス名を取得
1337 GetCommandToken( temporary, basbuf, i );
1338
1339 char className[VN_SIZE];
1340 Jenga::Common::Strings typeParameters;
1341 SplitGenericClassInstance( temporary, className, typeParameters );
1342
1343 CClass *pobj_c = const_cast<CClass *>( this->Find(namespaceScopes, className) );
1344 if(!pobj_c) continue;
1345
1346 compiler.pCompilingClass = pobj_c;
1347
1348 if(lpszInheritsClass){
1349 if( pobj_c->GetName() != lpszInheritsClass ){
1350 //継承先先読み用
1351 continue;
1352 }
1353 }
1354
1355 if(pobj_c->IsReady()){
1356 //既に先読みされているとき
1357 continue;
1358 }
1359
1360
1361 /////////////////////////////////////////////////////////
1362 // ☆★☆ ジェネリクスサポート ☆★☆
1363 BOOST_FOREACH( const std::string &typeParameter, typeParameters )
1364 {
1365 pobj_c->AddFormalGenericType( GenericType( typeParameter, Type(DEF_OBJECT,*GetObjectClassPtr()) ) );
1366 }
1367 /////////////////////////////////////////////////////////
1368
1369
1370 pobj_c->SetFixedAlignment( iAlign );
1371
1372 pobj_c->Readed();
1373
1374 pobj_c->SetConstructorMemberSubIndex( -1 );
1375 pobj_c->SetDestructorMemberSubIndex( -1 );
1376
1377 //アクセス制限の初期値をセット
1378 Prototype::Accessibility accessibility;
1379 if(dwClassType==ESC_CLASS){
1380 accessibility = Prototype::Private;
1381 }
1382 else{
1383 accessibility = Prototype::Public;
1384 }
1385
1386 if( pobj_c->GetName() == "Object" || dwClassType == ESC_TYPE ){
1387 if( &pobj_c->GetSuperClass() || pobj_c->GetVtblNum() )
1388 {
1389 // TODO: ここに来ないことが実証できたらこの分岐は消す
1390 Jenga::Throw( "GetClass_recur内の例外" );
1391 }
1392 }
1393 else{
1394 bool isInherits = false;
1395 if(basbuf[i+1]==1&&basbuf[i+2]==ESC_INHERITS){
1396 //継承を行う場合
1397 isInherits = true;
1398
1399 i += 3;
1400 GetCommandToken( temporary, basbuf, i );
1401
1402 if(lstrcmpi(temporary,pobj_c->GetName().c_str())==0){
1403 SetError(105,temporary,i);
1404 goto InheritsError;
1405 }
1406 }
1407
1408 if( !isInherits ){
1409 //Objectを継承する
1410 lstrcpy( temporary, "Object" );
1411 }
1412
1413 pobj_c->Inherits( temporary, i );
1414 }
1415InheritsError:
1416
1417 //メンバとメソッドを取得
1418 while(1){
1419 i++;
1420
1421 //エラー
1422 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE)){
1423 SetError(22,"Class",i);
1424 i--;
1425 break;
1426 }
1427
1428 if(basbuf[i]==1&&basbuf[i+1]==ESC_INHERITS){
1429 SetError(111,NULL,i);
1430 break;
1431 }
1432
1433 //Static修飾子
1434 BOOL bStatic;
1435 if(basbuf[i]==1&&basbuf[i+1]==ESC_STATIC){
1436 bStatic=1;
1437 i+=2;
1438 }
1439 else bStatic=0;
1440
1441 //Const修飾子
1442 bool isConst = false;
1443 if( basbuf[i] == 1 && basbuf[i + 1] == ESC_CONST ){
1444 isConst = true;
1445 i += 2;
1446 }
1447
1448 if(basbuf[i]==1&&(
1449 basbuf[i+1]==ESC_ABSTRACT||basbuf[i+1]==ESC_VIRTUAL||basbuf[i+1]==ESC_OVERRIDE||
1450 basbuf[i+1]==ESC_SUB||basbuf[i+1]==ESC_FUNCTION
1451 )){
1452 i3=basbuf[i+1];
1453 sub_address=i;
1454 }
1455 else i3=0;
1456
1457 bool isVirtual = false, isAbstract = false, isOverride = false;
1458 if(i3==ESC_ABSTRACT){
1459 isAbstract=1;
1460 isVirtual=1;
1461 i+=2;
1462
1463 i3=basbuf[i+1];
1464 }
1465 else if(i3==ESC_VIRTUAL){
1466 isAbstract=0;
1467 isVirtual=1;
1468 i+=2;
1469
1470 i3=basbuf[i+1];
1471 }
1472 else if(i3==ESC_OVERRIDE){
1473 isOverride=1;
1474 isVirtual=1;
1475
1476 i+=2;
1477
1478 i3=basbuf[i+1];
1479 }
1480
1481 for(i2=0;;i++,i2++){
1482 if(IsCommandDelimitation(basbuf[i])){
1483 temporary[i2]=0;
1484 break;
1485 }
1486 temporary[i2]=basbuf[i];
1487 }
1488 if(temporary[0]=='\0'){
1489 if(basbuf[i]=='\0'){
1490
1491 if(dwClassType==ESC_CLASS)
1492 SetError(22,"Class",top_pos);
1493 else
1494 SetError(22,"Type",top_pos);
1495
1496 i--;
1497 break;
1498 }
1499 continue;
1500 }
1501
1502 //End Class記述の場合
1503 if(temporary[0]==1&&temporary[1]==ESC_ENDCLASS&&dwClassType==ESC_CLASS) break;
1504 if(temporary[0]==1&&temporary[1]==ESC_ENDTYPE&&dwClassType==ESC_TYPE) break;
1505
1506 //アクセスを変更
1507 if(lstrcmpi(temporary,"Private")==0){
1508 accessibility = Prototype::Private;
1509 continue;
1510 }
1511 if(lstrcmpi(temporary,"Public")==0){
1512 accessibility = Prototype::Public;
1513 continue;
1514 }
1515 if(lstrcmpi(temporary,"Protected")==0){
1516 accessibility = Prototype::Protected;
1517 continue;
1518 }
1519
1520 extern int cp;
1521 if(i3==0){
1522 if(bStatic){
1523 //静的メンバを追加
1524 cp=i; //エラー用
1525 pobj_c->AddStaticMember( accessibility, isConst, false, temporary, i);
1526 }
1527 else{
1528 //メンバを追加
1529 cp=i; //エラー用
1530 pobj_c->AddMember( accessibility, isConst, false, temporary, i );
1531
1532
1533 if(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().IsStruct()){
1534 if( !pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().GetClass().IsReady() ){
1535 //参照先が読み取られていないとき
1536 GetClass_recur(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().GetClass().GetName().c_str());
1537 }
1538 }
1539
1540
1541 if(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().IsStruct()){
1542 //循環参照のチェック
1543 pobj_LoopRefCheck->add(pobj_c->GetName().c_str());
1544 if(!MemberVar_LoopRefCheck(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().GetClass())){
1545 //エラー回避
1546 Type &type = const_cast<Type &>(pobj_c->GetDynamicMembers().back()->GetType());
1547 type.SetBasicType( DEF_PTR_VOID );
1548 }
1549 pobj_LoopRefCheck->del(pobj_c->GetName().c_str());
1550 }
1551 }
1552 }
1553 else{
1554 //メソッドを追加
1555 cp=i; //エラー用
1556 pobj_c->AddMethod(pobj_c,
1557 accessibility,
1558 bStatic,
1559 isConst,
1560 isAbstract,
1561 isVirtual,
1562 isOverride,
1563 temporary,
1564 sub_address);
1565
1566 if( isAbstract ) continue;
1567
1568 for(;;i++){
1569 if(basbuf[i]=='\0'){
1570 i--;
1571 break;
1572 }
1573 if(basbuf[i-1]!='*'&&
1574 basbuf[i]==1&&(
1575 basbuf[i+1]==ESC_SUB||
1576 basbuf[i+1]==ESC_FUNCTION||
1577 basbuf[i+1]==ESC_MACRO||
1578 basbuf[i+1]==ESC_TYPE||
1579 basbuf[i+1]==ESC_CLASS||
1580 basbuf[i+1]==ESC_INTERFACE||
1581 basbuf[i+1]==ESC_ENUM)){
1582 GetDefaultNameFromES(i3,temporary);
1583 SetError(22,temporary,i);
1584 }
1585 if(basbuf[i]==1&&basbuf[i+1]==GetEndXXXCommand((char)i3)){
1586 i+=2;
1587 break;
1588 }
1589 }
1590 }
1591 }
1592 }
1593 }
1594
1595 // 呼び出し元でコンパイル中のクラスポインタを元に戻す
1596 compiler.pCompilingClass = pBackCompilingClass;
1597
1598 // 名前空間を元に戻す
1599 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes() = backupNamespaceScopes;
1600
1601 // インポートされた名前空間を元に戻す
1602 compiler.GetNamespaceSupporter().GetImportedNamespaces() = backupImportedNamespaces;
1603}
1604void Classes::GetAllClassInfo(void){
1605 //ループ継承チェック用のクラス
1606 pobj_LoopRefCheck=new CLoopRefCheck();
1607
1608 //クラスを取得
1609 GetClass_recur(0);
1610
1611 delete pobj_LoopRefCheck;
1612 pobj_LoopRefCheck=0;
1613
1614 // イテレータの準備
1615 this->Iterator_Init();
1616}
1617void Classes::Compile_System_InitializeUserTypes(){
1618 char temporary[VN_SIZE];
1619
1620 ////////////////////////////////////////////////////////////////////
1621 // クラス登録
1622 ////////////////////////////////////////////////////////////////////
1623
1624 // イテレータをリセット
1625 Iterator_Reset();
1626
1627 while( Iterator_HasNext() ){
1628 const CClass &objClass = *Iterator_GetNext();
1629
1630 if( !objClass.IsUsing() ){
1631 // 未使用のクラスは無視する
1632 continue;
1633 }
1634
1635 char referenceOffsetsBuffer[1024] = "";
1636 int numOfReference = 0;
1637 BOOST_FOREACH( CMember *pMember, objClass.GetDynamicMembers() ){
1638 if( pMember->GetType().IsObject() || pMember->GetType().IsPointer() ){
1639 if( referenceOffsetsBuffer[0] ){
1640 lstrcat( referenceOffsetsBuffer, "," );
1641 }
1642
1643 sprintf( referenceOffsetsBuffer + lstrlen( referenceOffsetsBuffer ),
1644 "%d",
1645 objClass.GetMemberOffset( pMember->GetName().c_str() ) );
1646
1647 numOfReference++;
1648 }
1649 }
1650
1651 sprintf( temporary
1652 , "Add(%c%c_System_TypeForClass(\"%s\",\"%s\",[%s],%d))"
1653 , 1
1654 , ESC_NEW
1655 , "" // 名前空間 (TODO: 実装)
1656 , objClass.GetName().c_str() // クラス名
1657 , referenceOffsetsBuffer // 参照メンバオフセット配列
1658 , numOfReference // 参照メンバの個数
1659 );
1660
1661 // コンパイル
1662 ChangeOpcode( temporary );
1663 }
1664
1665
1666 ////////////////////////////////////////////////////////////////////
1667 // 基底クラスを登録
1668 ////////////////////////////////////////////////////////////////////
1669
1670 sprintf(temporary, "%c%ctempType=Nothing%c%cTypeBaseImpl"
1671 , HIBYTE( COM_DIM )
1672 , LOBYTE( COM_DIM )
1673 , 1
1674 , ESC_AS
1675 );
1676 ChangeOpcode( temporary );
1677
1678 // イテレータをリセット
1679 Iterator_Reset();
1680
1681 while( Iterator_HasNext() ){
1682 const CClass &objClass = *Iterator_GetNext();
1683
1684 if( !objClass.IsUsing() ){
1685 // 未使用のクラスは無視する
1686 continue;
1687 }
1688
1689 if( objClass.HasSuperClass() ){
1690 sprintf( temporary
1691 , "tempType=Search(\"%s\",\"%s\")"
1692 , "" // 名前空間 (TODO: 実装)
1693 , objClass.GetName().c_str() // クラス名
1694 );
1695
1696 // コンパイル
1697 ChangeOpcode( temporary );
1698
1699 sprintf( temporary
1700 , "tempType.SetBaseType(Search(\"%s\",\"%s\"))"
1701 , "" // 名前空間 (TODO: 実装)
1702 , objClass.GetSuperClass().GetName().c_str() // 基底クラス名
1703 );
1704
1705 // コンパイル
1706 ChangeOpcode( temporary );
1707 }
1708 }
1709
1710
1711
1712 ////////////////////////////////////////////////////////////////////
1713 // 継承関係登録
1714 ////////////////////////////////////////////////////////////////////
1715 // TODO: 未完成
1716 /*
1717
1718 // イテレータをリセット
1719 Iterator_Reset();
1720
1721 while( Iterator_HasNext() ){
1722 CClass *pClass = Iterator_GetNext();
1723
1724 sprintf( genBuffer + length
1725 , "obj.Search( \"%s\" ).SetBaseType( Search( \"%s\" ) ):"
1726 , "" // クラス名
1727 , pClass->name // クラス名
1728 );
1729 length += lstrlen( genBuffer + length );
1730
1731 while( length + 8192 > max ){
1732 max += 8192;
1733 genBuffer = (char *)realloc( genBuffer, max );
1734 }
1735 }*/
1736}
1737
1738const CClass *Classes::Find( const NamespaceScopes &namespaceScopes, const string &name ) const
1739{
1740 if( namespaceScopes.size() == 0 && name == "Object" ){
1741 return GetObjectClassPtr();
1742 }
1743 else if( namespaceScopes.size() == 0 && name == "String" ){
1744 return GetStringClassPtr();
1745 }
1746
1747 const CClass *pClass = GetHashArrayElement( name.c_str() );
1748 while( pClass )
1749 {
1750 if( pClass->IsEqualSymbol( namespaceScopes, name ) ){
1751 //名前空間とクラス名が一致した
1752 return pClass;
1753 }
1754 pClass = pClass->GetChainNext();
1755 }
1756
1757 // TypeDefも見る
1758 int index = compiler.GetObjectModule().meta.GetTypeDefs().GetIndex( namespaceScopes, name );
1759 if( index != -1 ){
1760 Type type = compiler.GetObjectModule().meta.GetTypeDefs()[index].GetBaseType();
1761 if( type.IsObject() ){
1762 return &type.GetClass();
1763 }
1764 }
1765
1766 return NULL;
1767}
1768const CClass *Classes::Find( const string &fullName ) const
1769{
1770 char AreaName[VN_SIZE] = ""; //オブジェクト変数
1771 char NestName[VN_SIZE] = ""; //入れ子メンバ
1772 bool isNest = SplitMemberName( fullName.c_str(), AreaName, NestName );
1773
1774 return Find( NamespaceScopes( AreaName ), NestName );
1775}
1776void Classes::StartCompile( const UserProc *pUserProc ){
1777 const CClass *pParentClass = pUserProc->GetParentClassPtr();
1778 if( pParentClass ){
1779 pParentClass->Using();
1780
1781 // 仮想関数になるメソッドに使用チェックをつける
1782 BOOST_FOREACH( const CMethod *pMethod, pParentClass->GetMethods() )
1783 {
1784 if( pMethod->IsVirtual() )
1785 {
1786 pMethod->GetUserProc().Using();
1787 }
1788 }
1789
1790 pCompilingMethod = pParentClass->GetMethods().GetMethodPtr( pUserProc );
1791 if( !pCompilingMethod ){
1792 pCompilingMethod = pParentClass->GetStaticMethods().GetMethodPtr( pUserProc );
1793 if( !pCompilingMethod ){
1794 SmoothieException::Throw(300);
1795 }
1796 }
1797 }
1798 else{
1799 pCompilingMethod = NULL;
1800 }
1801}
1802
1803const CClass *Classes::GetStringClassPtr() const
1804{
1805 if( !pStringClass ){
1806 // キャッシュしておく
1807 pStringClass = this->Find( NamespaceScopes( "System" ), "String" );
1808
1809 if( !pStringClass )
1810 {
1811 SmoothieException::Throw();
1812 }
1813 return pStringClass;
1814 }
1815 return pStringClass;
1816}
1817const CClass *Classes::GetObjectClassPtr() const
1818{
1819 if( !pObjectClass ){
1820 // キャッシュしておく
1821 pObjectClass = this->Find( NamespaceScopes( "System" ), "Object" );
1822
1823 if( !pObjectClass )
1824 {
1825 SmoothieException::Throw();
1826 }
1827 return pObjectClass;
1828 }
1829 return pObjectClass;
1830}
Note: See TracBrowser for help on using the repository browser.