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

Last change on this file since 276 was 276, checked in by dai_9181, 17 years ago
File size: 39.6 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::ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection){
723 if(vtbl_offset==-1) return;
724
725 LONG_PTR *pVtbl;
726 pVtbl=(LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr()+vtbl_offset);
727
728 int i;
729 for(i=0;i<GetVtblNum();i++){
730 UserProc *pUserProc;
731 pUserProc=(UserProc *)pVtbl[i];
732 if(!pUserProc) continue;
733
734 pVtbl[i]=pUserProc->GetBeginOpAddress()+ImageBase+MemPos_CodeSection;
735 }
736}
737bool CClass::IsAbstract() const
738{
739 // 未実装(abstract)の仮想関数を持つ場合はtrueを返す
740
741 BOOST_FOREACH( const CMethod *pMethod, methods ){
742 if(pMethod->IsVirtual()){
743 if(pMethod->IsAbstract()){
744 return true;
745 }
746 }
747 }
748
749 return false;
750}
751
752CClass *Classes::Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name){
753 return new CClass(namespaceScopes, importedNamespaces, name);
754}
755bool Classes::Insert( CClass *pClass )
756{
757 /////////////////////////////////
758 // ハッシュデータに追加
759 /////////////////////////////////
760
761 if( !Put( pClass ) )
762 {
763 SetError(15,pClass->GetName(), cp);
764 return false;
765 }
766 return true;
767}
768CClass *Classes::Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine){
769 //////////////////////////////////////////////////////////////////////////
770 // クラスを追加
771 // ※名前のみを登録。その他の情報はSetClassメソッドで!
772 //////////////////////////////////////////////////////////////////////////
773
774 CClass *pClass = Create(namespaceScopes, importedNamespaces, name);
775
776 if( !Insert( pClass ) )
777 {
778 return NULL;
779 }
780
781 return pClass;
782}
783
784void Classes::CollectClassesForNameOnly( const BasicSource &source )
785{
786 int i, i2;
787 char temporary[VN_SIZE];
788
789 // 名前空間管理
790 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
791 namespaceScopes.clear();
792
793 // Importsされた名前空間の管理
794 NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
795 importedNamespaces.clear();
796
797 for(i=0;;i++){
798 if(source[i]=='\0') break;
799
800 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
801 for(i+=2,i2=0;;i2++,i++){
802 if( IsCommandDelimitation( source[i] ) ){
803 temporary[i2]=0;
804 break;
805 }
806 temporary[i2]=source[i];
807 }
808 namespaceScopes.push_back( temporary );
809
810 continue;
811 }
812 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
813 if( namespaceScopes.size() <= 0 ){
814 SmoothieException::Throw(12, "End Namespace", i );
815 }
816 else{
817 namespaceScopes.pop_back();
818 }
819
820 i += 2;
821 continue;
822 }
823 else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
824 for(i+=2,i2=0;;i2++,i++){
825 if( IsCommandDelimitation( source[i] ) ){
826 temporary[i2]=0;
827 break;
828 }
829 temporary[i2]=source[i];
830 }
831 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
832 {
833 SmoothieException::Throw(64,temporary,i );
834 }
835
836 continue;
837 }
838 else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
839 importedNamespaces.clear();
840 continue;
841 }
842
843 if(source[i]==1&&(
844 source[i+1]==ESC_CLASS||
845 source[i+1]==ESC_TYPE||
846 source[i+1]==ESC_INTERFACE
847 )){
848 int nowLine;
849 nowLine=i;
850
851 i+=2;
852 Type blittableType;
853 if(memicmp(source.GetBuffer()+i,"Align(",6)==0){
854 //アラインメント修飾子
855 i+=6;
856 i=JumpStringInPare(source.GetBuffer(),i)+1;
857 }
858 else if( memicmp( source.GetBuffer() + i, "Blittable(", 10 ) == 0 ){
859 // Blittable修飾子
860 i+=10;
861 i+=GetStringInPare_RemovePare(temporary,source.GetBuffer()+i)+1;
862 compiler.StringToType( temporary, blittableType );
863 }
864
865 bool isEnum = false;
866 if( source[i] == 1 && source[i+1] == ESC_ENUM ){
867 // 列挙型の場合
868 isEnum = true;
869
870 i+=2;
871 }
872
873 int i2;
874 char temporary[VN_SIZE];
875 for(i2=0;;i++,i2++){
876 if(!IsVariableChar(source[i])){
877 temporary[i2]=0;
878 break;
879 }
880 temporary[i2]=source[i];
881 }
882
883 //クラスを追加
884 CClass *pClass = this->Add(namespaceScopes, importedNamespaces, temporary,nowLine);
885 if( pClass ){
886 if( source[nowLine+1] == ESC_CLASS ){
887 if( isEnum ){
888 pClass->SetClassType( CClass::Enum );
889 }
890 else{
891 pClass->SetClassType( CClass::Class );
892 }
893 }
894 else if( source[nowLine+1] == ESC_INTERFACE ){
895 pClass->SetClassType( CClass::Interface );
896 }
897 else{
898 pClass->SetClassType( CClass::Structure );
899 }
900 }
901
902 // Blittable型の場合
903 if( !blittableType.IsNull() ){
904 pClass->SetBlittableType( blittableType );
905
906 // Blittable型として登録
907 compiler.GetObjectModule().meta.GetBlittableTypes().push_back( BlittableType( blittableType, pClass ) );
908 }
909 }
910 }
911}
912
913void Classes::ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection){
914 Iterator_Reset();
915 while( Iterator_HasNext() )
916 {
917 CClass *pClass = Iterator_GetNext();
918 pClass->ActionVtblSchedule(ImageBase,MemPos_CodeSection);
919 }
920}
921
922
923void Classes::InitStaticMember(){
924 //静的メンバをグローバル領域に作成
925
926 //イテレータをリセット
927
928 extern int cp;
929 int back_cp=cp;
930
931 this->Iterator_Reset();
932 while(this->Iterator_HasNext()){
933 CClass &objClass = *this->Iterator_GetNext();
934 if( objClass.isTargetObjectModule == false )
935 {
936 // 静的リンクライブラリの場合は飛ばす(既にインスタンスが定義済みであるため)
937 continue;
938 }
939
940 // 名前空間をセット
941 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes() = objClass.GetNamespaceScopes();
942
943 int i=0;
944 BOOST_FOREACH( CMember *member, objClass.GetStaticMembers() ){
945 char temporary[VN_SIZE];
946 sprintf(temporary,"%s.%s",objClass.GetName().c_str(),member->GetName().c_str());
947 dim(
948 temporary,
949 member->GetSubscripts(),
950 member->GetType(),
951 member->GetInitializeExpression().c_str(),
952 member->GetConstructParameter().c_str(),
953 0);
954
955 i++;
956 }
957 }
958
959 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().clear();
960
961 cp=back_cp;
962}
963bool Classes::MemberVar_LoopRefCheck(const CClass &objClass){
964 bool result = true;
965 BOOST_FOREACH( CMember *pMember, objClass.GetDynamicMembers() ){
966 if(pMember->GetType().IsStruct()){
967 //循環参照でないかをチェック
968 if(pobj_LoopRefCheck->check(pMember->GetType().GetClass())){
969 extern int cp;
970 SetError(124,pMember->GetType().GetClass().GetName(),cp);
971 return false;
972 }
973
974 pobj_LoopRefCheck->add(objClass.GetName().c_str());
975
976 bool tempResult = MemberVar_LoopRefCheck(pMember->GetType().GetClass());
977 if( result )
978 {
979 result = tempResult;
980 }
981
982 pobj_LoopRefCheck->del(objClass.GetName().c_str());
983 }
984 }
985
986 return result;
987}
988void Classes::GetClass_recur(const char *lpszInheritsClass){
989 extern char *basbuf;
990 int i,i2,i3,sub_address,top_pos;
991 char temporary[8192];
992
993 // 名前空間管理
994 NamespaceScopes backupNamespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
995 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
996 namespaceScopes.clear();
997
998 for(i=0;;i++){
999 if(basbuf[i]=='\0') break;
1000
1001
1002 // 名前空間
1003 if( basbuf[i] == 1 && basbuf[i+1] == ESC_NAMESPACE ){
1004 for(i+=2,i2=0;;i2++,i++){
1005 if( IsCommandDelimitation( basbuf[i] ) ){
1006 temporary[i2]=0;
1007 break;
1008 }
1009 temporary[i2]=basbuf[i];
1010 }
1011 namespaceScopes.push_back( temporary );
1012
1013 continue;
1014 }
1015 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENDNAMESPACE ){
1016 if( namespaceScopes.size() <= 0 ){
1017 SetError(12, "End Namespace", i );
1018 }
1019 else{
1020 namespaceScopes.pop_back();
1021 }
1022
1023 i += 2;
1024 continue;
1025 }
1026
1027
1028
1029 if(basbuf[i]==1&&basbuf[i+1]==ESC_INTERFACE){
1030 //////////////////////////
1031 // インターフェイス
1032 //////////////////////////
1033
1034 top_pos=i;
1035
1036 i+=2;
1037
1038 //インターフェイス名を取得
1039 GetIdentifierToken( temporary, basbuf, i );
1040
1041 CClass *pobj_c = const_cast<CClass *>( this->Find(namespaceScopes, temporary) );
1042 if(!pobj_c) continue;
1043
1044 if(lpszInheritsClass){
1045 if(lstrcmp(lpszInheritsClass,pobj_c->GetName().c_str())!=0){
1046 //継承先先読み用
1047 continue;
1048 }
1049 }
1050
1051 if(pobj_c->IsReady()){
1052 //既に先読みされているとき
1053 continue;
1054 }
1055
1056 pobj_c->Readed();
1057
1058 pobj_c->SetConstructorMemberSubIndex( -1 );
1059 pobj_c->SetDestructorMemberSubIndex( -1 );
1060
1061 if(basbuf[i+1]==1&&basbuf[i+2]==ESC_INHERITS){
1062 //継承を行う場合
1063 for(i+=3,i2=0;;i++,i2++){
1064 if(IsCommandDelimitation(basbuf[i])){
1065 temporary[i2]=0;
1066 break;
1067 }
1068 temporary[i2]=basbuf[i];
1069 }
1070
1071 if(lstrcmpi(temporary,pobj_c->GetName().c_str())==0){
1072 SetError(105,temporary,i);
1073 goto Interface_InheritsError;
1074 }
1075
1076 //継承元クラスを取得
1077 const Classes &classes = *this;
1078 const CClass *pInheritsClass = classes.Find(temporary);
1079 if( !pInheritsClass ){
1080 SetError(106,temporary,i);
1081 goto Interface_InheritsError;
1082 }
1083
1084 //継承させる
1085 if( !pobj_c->InheritsClass( *pInheritsClass, i ) ){
1086 goto Interface_InheritsError;
1087 }
1088 }
1089 else{
1090 //継承無し
1091 if( &pobj_c->GetSuperClass() || pobj_c->GetVtblNum() )
1092 {
1093 // TODO: ここに来ないことが実証できたらこの分岐は消す
1094 Jenga::Throw( "GetClass_recur内の例外" );
1095 }
1096 }
1097Interface_InheritsError:
1098
1099 //メンバ変数、関数を取得
1100 while(1){
1101 i++;
1102
1103 //エラー
1104 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE||basbuf[i+1]==ESC_INTERFACE)){
1105 SetError(22,"Interface",i);
1106 i--;
1107 break;
1108 }
1109
1110 if(basbuf[i]==1&&basbuf[i+1]==ESC_INHERITS){
1111 SetError(111,NULL,i);
1112 break;
1113 }
1114
1115 sub_address=i;
1116
1117 for(i2=0;;i++,i2++){
1118 if(IsCommandDelimitation(basbuf[i])){
1119 temporary[i2]=0;
1120 break;
1121 }
1122 temporary[i2]=basbuf[i];
1123 }
1124 if(temporary[0]=='\0'){
1125 if(basbuf[i]=='\0'){
1126 i--;
1127 SetError(22,"Interface",top_pos);
1128 break;
1129 }
1130 continue;
1131 }
1132
1133 //End Interface記述の場合
1134 if(temporary[0]==1&&temporary[1]==ESC_ENDINTERFACE) break;
1135
1136 if(!(temporary[0]==1&&(
1137 temporary[1]==ESC_SUB||temporary[1]==ESC_FUNCTION
1138 ))){
1139 SetError(1,NULL,i);
1140 break;
1141 }
1142
1143 //メンバ関数を追加
1144 pobj_c->AddMethod(pobj_c,
1145 Prototype::Public, //Publicアクセス権
1146 0, //Static指定なし
1147 false, //Constではない
1148 1, //Abstract
1149 1, //Virtual
1150 0,
1151 temporary,
1152 sub_address
1153 );
1154 }
1155 }
1156
1157 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE)){
1158 //////////////////////////
1159 // クラス
1160 //////////////////////////
1161
1162 top_pos=i;
1163
1164 const DWORD dwClassType=basbuf[i+1];
1165
1166 i+=2;
1167
1168 int iAlign=0;
1169 if(memicmp(basbuf+i,"Align(",6)==0){
1170 //アラインメント修飾子
1171 i+=6;
1172 i+=GetStringInPare_RemovePare(temporary,basbuf+i)+1;
1173 iAlign=atoi(temporary);
1174
1175 if(!(iAlign==1||iAlign==2||iAlign==4||iAlign==8||iAlign==16))
1176 SetError(51,NULL,i);
1177 }
1178 else if( memicmp( basbuf + i, "Blittable(", 10 ) == 0 ){
1179 // Blittable修飾子
1180 i+=10;
1181 i=JumpStringInPare(basbuf,i)+1;
1182 }
1183
1184 if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENUM ){
1185 // 列挙型の場合
1186 i+=2;
1187 }
1188
1189 //クラス名を取得
1190 GetIdentifierToken( temporary, basbuf, i );
1191
1192 CClass *pobj_c = const_cast<CClass *>( this->Find(namespaceScopes, temporary) );
1193 if(!pobj_c) continue;
1194
1195 if(lpszInheritsClass){
1196 if( pobj_c->GetName() != lpszInheritsClass ){
1197 //継承先先読み用
1198 continue;
1199 }
1200 }
1201
1202 if(pobj_c->IsReady()){
1203 //既に先読みされているとき
1204 continue;
1205 }
1206
1207 pobj_c->SetFixedAlignment( iAlign );
1208
1209 pobj_c->Readed();
1210
1211 pobj_c->SetConstructorMemberSubIndex( -1 );
1212 pobj_c->SetDestructorMemberSubIndex( -1 );
1213
1214 //アクセス制限の初期値をセット
1215 Prototype::Accessibility accessibility;
1216 if(dwClassType==ESC_CLASS){
1217 accessibility = Prototype::Private;
1218 }
1219 else{
1220 accessibility = Prototype::Public;
1221 }
1222
1223 if( pobj_c->GetName() == "Object" || dwClassType == ESC_TYPE ){
1224 if( &pobj_c->GetSuperClass() || pobj_c->GetVtblNum() )
1225 {
1226 // TODO: ここに来ないことが実証できたらこの分岐は消す
1227 Jenga::Throw( "GetClass_recur内の例外" );
1228 }
1229 }
1230 else{
1231 bool isInherits = false;
1232 if(basbuf[i+1]==1&&basbuf[i+2]==ESC_INHERITS){
1233 //継承を行う場合
1234 isInherits = true;
1235
1236 for(i+=3,i2=0;;i++,i2++){
1237 if(IsCommandDelimitation(basbuf[i])){
1238 temporary[i2]=0;
1239 break;
1240 }
1241 temporary[i2]=basbuf[i];
1242 }
1243
1244 if(lstrcmpi(temporary,pobj_c->GetName().c_str())==0){
1245 SetError(105,temporary,i);
1246 goto InheritsError;
1247 }
1248 }
1249
1250 if( !isInherits ){
1251 //Objectを継承する
1252 lstrcpy( temporary, "Object" );
1253 }
1254
1255 pobj_c->Inherits( temporary, i );
1256 }
1257InheritsError:
1258
1259 //メンバとメソッドを取得
1260 while(1){
1261 i++;
1262
1263 //エラー
1264 if(basbuf[i]==1&&(basbuf[i+1]==ESC_CLASS||basbuf[i+1]==ESC_TYPE)){
1265 SetError(22,"Class",i);
1266 i--;
1267 break;
1268 }
1269
1270 if(basbuf[i]==1&&basbuf[i+1]==ESC_INHERITS){
1271 SetError(111,NULL,i);
1272 break;
1273 }
1274
1275 //Static修飾子
1276 BOOL bStatic;
1277 if(basbuf[i]==1&&basbuf[i+1]==ESC_STATIC){
1278 bStatic=1;
1279 i+=2;
1280 }
1281 else bStatic=0;
1282
1283 //Const修飾子
1284 bool isConst = false;
1285 if( basbuf[i] == 1 && basbuf[i + 1] == ESC_CONST ){
1286 isConst = true;
1287 i += 2;
1288 }
1289
1290 if(basbuf[i]==1&&(
1291 basbuf[i+1]==ESC_ABSTRACT||basbuf[i+1]==ESC_VIRTUAL||basbuf[i+1]==ESC_OVERRIDE||
1292 basbuf[i+1]==ESC_SUB||basbuf[i+1]==ESC_FUNCTION
1293 )){
1294 i3=basbuf[i+1];
1295 sub_address=i;
1296 }
1297 else i3=0;
1298
1299 bool isVirtual = false, isAbstract = false, isOverride = false;
1300 if(i3==ESC_ABSTRACT){
1301 isAbstract=1;
1302 isVirtual=1;
1303 i+=2;
1304
1305 i3=basbuf[i+1];
1306 }
1307 else if(i3==ESC_VIRTUAL){
1308 isAbstract=0;
1309 isVirtual=1;
1310 i+=2;
1311
1312 i3=basbuf[i+1];
1313 }
1314 else if(i3==ESC_OVERRIDE){
1315 isOverride=1;
1316 isVirtual=1;
1317
1318 i+=2;
1319
1320 i3=basbuf[i+1];
1321 }
1322
1323 for(i2=0;;i++,i2++){
1324 if(IsCommandDelimitation(basbuf[i])){
1325 temporary[i2]=0;
1326 break;
1327 }
1328 temporary[i2]=basbuf[i];
1329 }
1330 if(temporary[0]=='\0'){
1331 if(basbuf[i]=='\0'){
1332
1333 if(dwClassType==ESC_CLASS)
1334 SetError(22,"Class",top_pos);
1335 else
1336 SetError(22,"Type",top_pos);
1337
1338 i--;
1339 break;
1340 }
1341 continue;
1342 }
1343
1344 //End Class記述の場合
1345 if(temporary[0]==1&&temporary[1]==ESC_ENDCLASS&&dwClassType==ESC_CLASS) break;
1346 if(temporary[0]==1&&temporary[1]==ESC_ENDTYPE&&dwClassType==ESC_TYPE) break;
1347
1348 //アクセスを変更
1349 if(lstrcmpi(temporary,"Private")==0){
1350 accessibility = Prototype::Private;
1351 continue;
1352 }
1353 if(lstrcmpi(temporary,"Public")==0){
1354 accessibility = Prototype::Public;
1355 continue;
1356 }
1357 if(lstrcmpi(temporary,"Protected")==0){
1358 accessibility = Prototype::Protected;
1359 continue;
1360 }
1361
1362 extern int cp;
1363 if(i3==0){
1364 if(bStatic){
1365 //静的メンバを追加
1366 cp=i; //エラー用
1367 pobj_c->AddStaticMember( accessibility, isConst, false, temporary, i);
1368 }
1369 else{
1370 //メンバを追加
1371 cp=i; //エラー用
1372 pobj_c->AddMember( accessibility, isConst, false, temporary, i );
1373
1374
1375 if(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().IsStruct()){
1376 if( !pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().GetClass().IsReady() ){
1377 //参照先が読み取られていないとき
1378 GetClass_recur(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().GetClass().GetName().c_str());
1379 }
1380 }
1381
1382
1383 if(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().IsStruct()){
1384 //循環参照のチェック
1385 pobj_LoopRefCheck->add(pobj_c->GetName().c_str());
1386 if(!MemberVar_LoopRefCheck(pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().GetClass())){
1387 //エラー回避
1388 pobj_c->GetDynamicMembers()[pobj_c->GetDynamicMembers().size()-1]->GetType().SetBasicType( DEF_PTR_VOID );
1389 }
1390 pobj_LoopRefCheck->del(pobj_c->GetName().c_str());
1391 }
1392 }
1393 }
1394 else{
1395 //メソッドを追加
1396 cp=i; //エラー用
1397 pobj_c->AddMethod(pobj_c,
1398 accessibility,
1399 bStatic,
1400 isConst,
1401 isAbstract,
1402 isVirtual,
1403 isOverride,
1404 temporary,
1405 sub_address);
1406
1407 if( isAbstract ) continue;
1408
1409 for(;;i++){
1410 if(basbuf[i]=='\0'){
1411 i--;
1412 break;
1413 }
1414 if(basbuf[i-1]!='*'&&
1415 basbuf[i]==1&&(
1416 basbuf[i+1]==ESC_SUB||
1417 basbuf[i+1]==ESC_FUNCTION||
1418 basbuf[i+1]==ESC_MACRO||
1419 basbuf[i+1]==ESC_TYPE||
1420 basbuf[i+1]==ESC_CLASS||
1421 basbuf[i+1]==ESC_INTERFACE||
1422 basbuf[i+1]==ESC_ENUM)){
1423 GetDefaultNameFromES(i3,temporary);
1424 SetError(22,temporary,i);
1425 }
1426 if(basbuf[i]==1&&basbuf[i+1]==GetEndXXXCommand((char)i3)){
1427 i+=2;
1428 break;
1429 }
1430 }
1431 }
1432 }
1433 }
1434 }
1435
1436
1437 // 名前空間を元に戻す
1438 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes() = backupNamespaceScopes;
1439}
1440void Classes::GetAllClassInfo(void){
1441 //ループ継承チェック用のクラス
1442 pobj_LoopRefCheck=new CLoopRefCheck();
1443
1444 //クラスを取得
1445 GetClass_recur(0);
1446
1447 delete pobj_LoopRefCheck;
1448 pobj_LoopRefCheck=0;
1449
1450 // イテレータの準備
1451 this->Iterator_Init();
1452}
1453void Classes::Compile_System_InitializeUserTypes(){
1454 char temporary[VN_SIZE];
1455
1456 ////////////////////////////////////////////////////////////////////
1457 // クラス登録
1458 ////////////////////////////////////////////////////////////////////
1459
1460 // イテレータをリセット
1461 Iterator_Reset();
1462
1463 while( Iterator_HasNext() ){
1464 const CClass &objClass = *Iterator_GetNext();
1465
1466 if( !objClass.IsUsing() ){
1467 // 未使用のクラスは無視する
1468 continue;
1469 }
1470
1471 char referenceOffsetsBuffer[1024] = "";
1472 int numOfReference = 0;
1473 BOOST_FOREACH( CMember *pMember, objClass.GetDynamicMembers() ){
1474 if( pMember->GetType().IsObject() || pMember->GetType().IsPointer() ){
1475 if( referenceOffsetsBuffer[0] ){
1476 lstrcat( referenceOffsetsBuffer, "," );
1477 }
1478
1479 sprintf( referenceOffsetsBuffer + lstrlen( referenceOffsetsBuffer ),
1480 "%d",
1481 objClass.GetMemberOffset( pMember->GetName().c_str() ) );
1482
1483 numOfReference++;
1484 }
1485 }
1486
1487 sprintf( temporary
1488 , "Add(%c%c_System_TypeForClass(\"%s\",\"%s\",[%s],%d))"
1489 , 1
1490 , ESC_NEW
1491 , "" // 名前空間 (TODO: 実装)
1492 , objClass.GetName().c_str() // クラス名
1493 , referenceOffsetsBuffer // 参照メンバオフセット配列
1494 , numOfReference // 参照メンバの個数
1495 );
1496
1497 // コンパイル
1498 ChangeOpcode( temporary );
1499 }
1500
1501
1502 ////////////////////////////////////////////////////////////////////
1503 // 基底クラスを登録
1504 ////////////////////////////////////////////////////////////////////
1505
1506 sprintf(temporary, "%c%ctempType=Nothing%c%cTypeBaseImpl"
1507 , HIBYTE( COM_DIM )
1508 , LOBYTE( COM_DIM )
1509 , 1
1510 , ESC_AS
1511 );
1512 ChangeOpcode( temporary );
1513
1514 // イテレータをリセット
1515 Iterator_Reset();
1516
1517 while( Iterator_HasNext() ){
1518 const CClass &objClass = *Iterator_GetNext();
1519
1520 if( !objClass.IsUsing() ){
1521 // 未使用のクラスは無視する
1522 continue;
1523 }
1524
1525 if( objClass.HasSuperClass() ){
1526 sprintf( temporary
1527 , "tempType=Search(\"%s\",\"%s\")"
1528 , "" // 名前空間 (TODO: 実装)
1529 , objClass.GetName().c_str() // クラス名
1530 );
1531
1532 // コンパイル
1533 ChangeOpcode( temporary );
1534
1535 sprintf( temporary
1536 , "tempType.SetBaseType(Search(\"%s\",\"%s\"))"
1537 , "" // 名前空間 (TODO: 実装)
1538 , objClass.GetSuperClass().GetName().c_str() // 基底クラス名
1539 );
1540
1541 // コンパイル
1542 ChangeOpcode( temporary );
1543 }
1544 }
1545
1546
1547
1548 ////////////////////////////////////////////////////////////////////
1549 // 継承関係登録
1550 ////////////////////////////////////////////////////////////////////
1551 // TODO: 未完成
1552 /*
1553
1554 // イテレータをリセット
1555 Iterator_Reset();
1556
1557 while( Iterator_HasNext() ){
1558 CClass *pClass = Iterator_GetNext();
1559
1560 sprintf( genBuffer + length
1561 , "obj.Search( \"%s\" ).SetBaseType( Search( \"%s\" ) ):"
1562 , "" // クラス名
1563 , pClass->name // クラス名
1564 );
1565 length += lstrlen( genBuffer + length );
1566
1567 while( length + 8192 > max ){
1568 max += 8192;
1569 genBuffer = (char *)realloc( genBuffer, max );
1570 }
1571 }*/
1572}
1573
1574const CClass *Classes::Find( const NamespaceScopes &namespaceScopes, const string &name ) const
1575{
1576 if( namespaceScopes.size() == 0 && name == "Object" ){
1577 return GetObjectClassPtr();
1578 }
1579 else if( namespaceScopes.size() == 0 && name == "String" ){
1580 return GetStringClassPtr();
1581 }
1582
1583 const CClass *pClass = GetHashArrayElement( name.c_str() );
1584 while( pClass )
1585 {
1586 if( pClass->IsEqualSymbol( namespaceScopes, name ) ){
1587 //名前空間とクラス名が一致した
1588 return pClass;
1589 }
1590 pClass = pClass->GetChainNext();
1591 }
1592
1593 // TypeDefも見る
1594 int index = compiler.GetObjectModule().meta.GetTypeDefs().GetIndex( namespaceScopes, name );
1595 if( index != -1 ){
1596 Type type = compiler.GetObjectModule().meta.GetTypeDefs()[index].GetBaseType();
1597 if( type.IsObject() ){
1598 return &type.GetClass();
1599 }
1600 }
1601
1602 return NULL;
1603}
1604const CClass *Classes::Find( const string &fullName ) const
1605{
1606 char AreaName[VN_SIZE] = ""; //オブジェクト変数
1607 char NestName[VN_SIZE] = ""; //入れ子メンバ
1608 bool isNest = SplitMemberName( fullName.c_str(), AreaName, NestName );
1609
1610 return Find( NamespaceScopes( AreaName ), NestName );
1611}
1612void Classes::StartCompile( const UserProc *pUserProc ){
1613 const CClass *pParentClass = pUserProc->GetParentClassPtr();
1614 if( pParentClass ){
1615 pParentClass->Using();
1616
1617 pCompilingMethod = pParentClass->GetMethods().GetMethodPtr( pUserProc );
1618 if( !pCompilingMethod ){
1619 pCompilingMethod = pParentClass->GetStaticMethods().GetMethodPtr( pUserProc );
1620 if( !pCompilingMethod ){
1621 SmoothieException::Throw(300);
1622 }
1623 }
1624 }
1625 else{
1626 pCompilingMethod = NULL;
1627 }
1628}
1629
1630const CClass *Classes::GetStringClassPtr() const
1631{
1632 if( !pStringClass ){
1633 // キャッシュしておく
1634 pStringClass = this->Find( NamespaceScopes( "System" ), "String" );
1635
1636 if( !pStringClass )
1637 {
1638 SmoothieException::Throw();
1639 }
1640 return pStringClass;
1641 }
1642 return pStringClass;
1643}
1644const CClass *Classes::GetObjectClassPtr() const
1645{
1646 if( !pObjectClass ){
1647 // キャッシュしておく
1648 pObjectClass = this->Find( NamespaceScopes( "System" ), "Object" );
1649
1650 if( !pObjectClass )
1651 {
1652 SmoothieException::Throw();
1653 }
1654 return pObjectClass;
1655 }
1656 return pObjectClass;
1657}
Note: See TracBrowser for help on using the repository browser.