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

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