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

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

非仮想関数のオーバーライドをエラー扱いにした

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