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

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

vtbl構築をコード生成後(最終リンクの前)に行うようにした

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