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

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

BasicSourceのシリアライズがうまくいっていない

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