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

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