source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/Class.cpp@ 557

Last change on this file since 557 was 557, checked in by dai_9181, 16 years ago

エラーコード138をLexicalAnalyzer_Class.cppで表示するようにした。

File size: 31.4 KB
Line 
1#include "stdafx.h"
2
3#include <Source.h>
4#include <Class.h>
5#include <Compiler.h>
6
7#include "../common.h"
8#ifdef _AMD64_
9#include "../../compiler_x64/opcode.h"
10#else
11#include "../../compiler_x86/opcode.h"
12#endif
13
14using namespace ActiveBasic::Compiler;
15
16void CClass::Using() const
17{
18 if( this->IsUsing() )
19 {
20 // 既に使用することになっている
21 return;
22 }
23
24 Prototype::Using();
25
26 // 仮想関数になるメソッドに使用チェックをつける
27 const CClass &objThis = *this;
28 BOOST_FOREACH( const CMethod *pMethod, objThis.GetDynamicMethods() )
29 {
30 if( pMethod->IsVirtual() )
31 {
32 pMethod->GetUserProc().Using();
33 }
34 }
35}
36
37bool CClass::IsClass() const
38{
39 return classType == CClass::Class;
40}
41bool CClass::IsInterface() const
42{
43 return classType == CClass::Interface;
44}
45bool CClass::IsComInterface() const
46{
47 return classType == CClass::ComInterface;
48}
49bool CClass::IsEnum() const
50{
51 return classType == CClass::Enum;
52}
53bool CClass::IsDelegate() const
54{
55 return classType == CClass::Delegate;
56}
57bool CClass::IsStructure() const
58{
59 return classType == CClass::Structure;
60}
61
62
63// コンストラクタのコンパイルを開始
64void CClass::NotifyStartConstructorCompile() const
65{
66 isCompilingConstructor = true;
67}
68
69//コンストラクタのコンパイルを終了
70void CClass::NotifyFinishConstructorCompile() const
71{
72 isCompilingConstructor = false;
73}
74
75//コンストラクタをコンパイル中かどうかを判別
76bool CClass::IsCompilingConstructor() const
77{
78 return isCompilingConstructor;
79}
80
81//デストラクタのコンパイルを開始
82void CClass::NotifyStartDestructorCompile() const{
83 isCompilingDestructor = true;
84}
85
86//デストラクタのコンパイルを終了
87void CClass::NotifyFinishDestructorCompile() const{
88 isCompilingDestructor = false;
89}
90
91//デストラクタをコンパイル中かどうかを判別
92bool CClass::IsCompilingDestructor() const
93{
94 return isCompilingDestructor;
95}
96
97//自身の派生クラスかどうかを確認
98bool CClass::IsSubClass( const CClass *pSubClass ) const
99{
100 if( !pSubClass->HasSuperClass() )
101 {
102 return false;
103 }
104
105 const CClass *pTempClass = &pSubClass->GetSuperClass();
106 while( pTempClass ){
107 if( this == pTempClass ) return true;
108 pTempClass = &pTempClass->GetSuperClass();
109 }
110 return false;
111}
112
113//自身と等しいまたは派生クラスかどうかを確認
114bool CClass::IsEqualsOrSubClass( const CClass *pSubClass ) const
115{
116 if( IsEquals( pSubClass ) ) return true;
117 return IsSubClass( pSubClass );
118}
119
120// 自身と等しいまたは派生クラス、基底クラスかどうかを確認
121bool CClass::IsEqualsOrSubClassOrSuperClass( const CClass &objClass ) const
122{
123 if( IsEquals( &objClass ) ) return true;
124 if( IsSubClass( &objClass ) ) return true;
125 if( objClass.IsSubClass( this ) ) return true;
126 return false;
127}
128
129bool CClass::IsInheritsInterface( const CClass *pInterfaceClass ) const
130{
131 BOOST_FOREACH( const ::Interface *pInterface, interfaces ){
132 if( pInterfaceClass == &pInterface->GetClass() ){
133 return true;
134 }
135 }
136 return false;
137}
138
139bool CClass::InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine )
140{
141 //メソッドをコピー
142 BOOST_FOREACH( const CMethod *pBaseMethod, inheritsClass.GetDynamicMethods() ){
143 CMethod *pMethod = new DynamicMethod( *pBaseMethod );
144
145 // アクセシビリティ
146 if(pBaseMethod->GetAccessibility() == Prototype::Private){
147 pMethod->SetAccessibility( Prototype::None );
148 }
149 else{
150 pMethod->SetAccessibility( pBaseMethod->GetAccessibility() );
151 }
152
153 //pobj_Inherits
154 // ※継承元のClassIndexをセット(入れ子継承を考慮する)
155 if(pBaseMethod->GetInheritsClassPtr()==0){
156 pMethod->SetInheritsClassPtr( &inheritsClass );
157 }
158 else{
159 pMethod->SetInheritsClassPtr( pBaseMethod->GetInheritsClassPtr() );
160 }
161
162 GetDynamicMethods().push_back( pMethod );
163 }
164
165 //仮想関数の数
166 AddVtblNum( inheritsClass.GetVtblNum() );
167
168 //継承先のクラスをメンバとして保持する
169 SetSuperClass( &inheritsClass );
170 SetSuperClassActualTypeParameters( actualTypeParameters );
171
172 // インターフェイスを引き継ぐ
173 BOOST_FOREACH( ::Interface *pInterface, inheritsClass.GetInterfaces() )
174 {
175 interfaces.push_back( new ::Interface( *pInterface ) );
176 }
177
178 if( this->IsInterface() && inheritsClass.IsComInterface() )
179 {
180 // COMインターフェイスを継承した場合はCOMインターフェイスにする
181 this->SetClassType( CClass::ComInterface );
182 }
183
184 return true;
185}
186
187void CClass::Implements( const CClass &interfaceClass, const Types &actualTypeParameters, int nowLine )
188{
189 ::Interface *pDestInterface = new ::Interface( &interfaceClass, actualTypeParameters );
190
191 interfaces.push_back( pDestInterface );
192
193
194 /////////////////////////////////////////////////////////////////
195 // 基底クラスのメソッドからインターフェイスメソッドを再実装する
196 /////////////////////////////////////////////////////////////////
197 BOOST_FOREACH( CMethod *pMethod, GetDynamicMethods() )
198 {
199 CMethod *pMethodForOverride = pDestInterface->GetDynamicMethods().FindForOverride( pDestInterface->GetActualTypeParameters(), &pMethod->GetUserProc() );
200 if( pMethodForOverride )
201 {
202 pMethodForOverride->Override( &pMethod->GetUserProc(), pMethod->GetAccessibility(), false );
203
204 // 実装元になるメソッドは呼び出し不可にしておく(オーバーロードの解決から除外する)
205 pMethod->SetNotUseMark( true );
206 }
207 }
208
209
210 /////////////////////////////////////////////////////////////////
211 // キャストメソッドを追加(内部コードは自動生成すること)
212 /////////////////////////////////////////////////////////////////
213 if( interfaceClass.IsInterface() )
214 {
215 // Function Operator() As ITest
216
217 char methodName[255] = { 1, ESC_OPERATOR, CALC_AS, '\0' };
218
219 //関数ハッシュへ登録
220 UserProc *pUserProc = new UserProc(
221 NamespaceScopes(),
222 NamespaceScopesCollection(),
223 methodName,
224 Procedure::Function,
225 false,
226 false,
227 false );
228 pUserProc->SetParentClass( this );
229
230 Parameters params;
231 params.push_back( new Parameter( "_System_LocalThis", Type( DEF_PTR_VOID ) ) );
232 pUserProc->SetRealParams( params );
233
234 pUserProc->SetReturnType( Type( DEF_OBJECT, interfaceClass ) );
235 pUserProc->_paramStr = "";
236 pUserProc->Using();
237 compiler.GetObjectModule().meta.GetUserProcs().Insert( pUserProc, -1 );
238
239 LexicalAnalyzer::AddMethod(this,
240 pUserProc,
241 Prototype::Public,
242 0,
243 false, // isConst
244 false, // isAbstract
245 false, // isVirtual
246 false, // isOverride
247 "",
248 true, // isAutoGeneration
249 -1
250 );
251 }
252}
253
254CMember *CClass::CreateMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine )
255{
256 extern int cp;
257
258 //構文を解析
259 char VarName[VN_SIZE];
260 char initBuffer[VN_SIZE];
261 char lpszConstructParameter[VN_SIZE];
262 Subscripts subscripts;
263 Type type;
264 GetDimentionFormat(buffer,VarName,subscripts,type,initBuffer,lpszConstructParameter);
265
266 //重複チェック
267 if(this->DupliCheckAll(VarName)){
268 compiler.errorMessenger.Output(15,VarName,cp);
269 }
270
271 CMember *pMember = new CMember( accessibility, VarName, type, isConst, subscripts, initBuffer, lpszConstructParameter );
272 pMember->source_code_address = nowLine;
273 return pMember;
274}
275void CClass::AddMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine ){
276 dynamicMembers.push_back(
277 CreateMember( accessibility, isConst, isRef, buffer, nowLine )
278 );
279}
280void CClass::AddStaticMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine ){
281 staticMembers.push_back(
282 CreateMember( accessibility, isConst, isRef, buffer, nowLine )
283 );
284}
285
286bool CClass::DupliCheckAll(const char *name) const
287{
288 //重複チェック
289
290 //メンバ
291 if(DupliCheckMember(name)) return 1;
292
293 //メソッド
294 BOOST_FOREACH( const CMethod *pMethod, GetDynamicMethods() ){
295 if( lstrcmp( name, pMethod->GetUserProc().GetName().c_str() ) == 0 ){
296 return 1;
297 }
298 }
299
300 return 0;
301}
302bool CClass::DupliCheckMember(const char *name) const
303{
304 //重複チェック
305
306 if( this->HasSuperClass() )
307 {
308 if( this->GetSuperClass().DupliCheckMember( name ) )
309 {
310 // 基底クラスで重複が発見された
311 return true;
312 }
313 }
314
315 // 動的メンバ
316 BOOST_FOREACH( CMember *pMember, dynamicMembers )
317 {
318 if( GetName() == pMember->GetName() )
319 {
320 return true;
321 }
322 }
323
324 // 静的メンバ
325 BOOST_FOREACH( CMember *pMember, staticMembers ){
326 if( GetName() == pMember->GetName() ){
327 return true;
328 }
329 }
330
331 return false;
332}
333
334const CMember *CClass::FindDynamicMember( const char *memberName ) const
335{
336 if( this->HasSuperClass() )
337 {
338 // 基底クラスで検索
339 const CMember *result = this->GetSuperClass().FindDynamicMember( memberName );
340 if( result )
341 {
342 return result;
343 }
344 }
345
346 BOOST_FOREACH( CMember *pMember, GetDynamicMembers() )
347 {
348 if( pMember->GetName() == memberName )
349 {
350 return pMember;
351 }
352 }
353 return NULL;
354}
355
356void CClass::EnumDynamicMethodsOrInterfaceMethods( const char *methodName, std::vector<const UserProc *> &subs ) const
357{
358 // 動的メソッド
359 GetDynamicMethods().Enum( methodName, subs );
360
361 // インターフェイス メソッド
362 BOOST_FOREACH( ::Interface *pInterface, GetInterfaces() )
363 {
364 pInterface->GetDynamicMethods().Enum( methodName, subs );
365 }
366}
367const CMethod *CClass::GetDynamicMethodOrInterfaceMethod( const UserProc *pUserProc ) const
368{
369 // 動的メソッド
370 const CMethod *result = GetDynamicMethods().GetMethodPtr( pUserProc );
371
372 if( !result )
373 {
374 // インターフェイス メソッド
375 BOOST_FOREACH( ::Interface *pInterface, GetInterfaces() )
376 {
377 result = pInterface->GetDynamicMethods().GetMethodPtr( pUserProc );
378 if( result )
379 {
380 return result;
381 }
382 }
383 }
384
385 return result;
386}
387
388const ::Delegate &CClass::GetDelegate() const
389{
390 const ::Delegate *dg = compiler.GetObjectModule().meta.GetDelegates().GetHashArrayElement( GetName().c_str() );
391 while( dg )
392 {
393 if( dg->IsEqualSymbol( GetNamespaceScopes(), GetName() ) ){
394 //名前空間とクラス名が一致した
395 return *dg;
396 }
397 dg = dg->GetChainNext();
398 }
399
400 Jenga::Throw( "CClass::GetDelegateメソッドに失敗" );
401 static ::Delegate dummy;
402 return dummy;
403}
404
405//サイズを取得
406int CClass::GetSize() const
407{
408 int resultSize = 0;
409
410 int alignment = 1;
411 if( this->IsStructure() )
412 {
413 // 構造体のとき
414
415 if( this->GetFixedAlignment() )
416 {
417 // アラインメントの固定値が指定されていた場合はそれを取得
418 alignment = this->GetFixedAlignment();
419 }
420 }
421 else
422 {
423 // それ以外
424
425 if( this->HasSuperClass() )
426 {
427 // 基底クラスのサイズを追加
428 resultSize += this->GetSuperClass().GetSize();
429
430 // 基底クラスのアラインメントを取得
431 alignment = this->GetSuperClass().GetAlignment();
432 }
433 else
434 {
435 // 基底クラスが存在しないとき
436
437 // 仮想関数が存在する場合はvtbl及びvtblMasterListへのポインタのサイズを追加
438 resultSize += IsExistVirtualFunctions() ? PTR_SIZE*2 : 0;
439 }
440 }
441
442 BOOST_FOREACH( CMember *pMember, dynamicMembers )
443 {
444 // メンバサイズ
445 int tempMemberSize = pMember->GetType().GetSize();
446
447 // 一時アラインメントを算出
448 int tempAlignment = tempMemberSize;
449 if( pMember->GetType().IsStruct() )
450 {
451 // メンバが構造体の場合は、メンバのアラインメントを取得
452 tempAlignment = pMember->GetType().GetClass().GetAlignment();
453 }
454
455 // アラインメントを考慮してパディングを追加
456 if( GetFixedAlignment() && alignment < tempAlignment )
457 {
458 if( resultSize % alignment )
459 {
460 resultSize += alignment - ( resultSize % alignment );
461 }
462 }
463 else
464 {
465 if( alignment < tempAlignment )
466 {
467 // 最大アラインメントを更新
468 alignment = tempAlignment;
469 }
470
471 if( tempMemberSize == 0 )
472 {
473 if( !pMember->GetType().IsStruct() )
474 {
475 compiler.errorMessenger.OutputFatalError();
476 }
477
478 //メンバを持たない構造体
479 //※何もしない(オフセットの計算をしない)
480 }
481 else{
482 if( resultSize % tempAlignment )
483 {
484 resultSize += tempAlignment - ( resultSize % tempAlignment );
485 }
486 }
487 }
488
489 // メンバサイズを加算(配列を考慮)
490 resultSize += tempMemberSize * Variable::GetSubScriptCounts( pMember->GetSubscripts() );
491 }
492
493 if( alignment )
494 {
495 // 末尾アラインメントを考慮してパディングを追加
496 if( resultSize % alignment )
497 {
498 resultSize += alignment - ( resultSize % alignment );
499 }
500 }
501
502 return resultSize;
503}
504
505//メンバのオフセットを取得
506int CClass::GetMemberOffset( const char *memberName ) const
507{
508 int resultSize = 0;
509
510 int alignment = 1;
511 if( this->IsStructure() )
512 {
513 // 構造体のとき
514
515 if( this->GetFixedAlignment() )
516 {
517 // アラインメントの固定値が指定されていた場合はそれを取得
518 alignment = this->GetFixedAlignment();
519 }
520 }
521 else
522 {
523 // それ以外
524
525 if( this->HasSuperClass() )
526 {
527 if( this->GetSuperClass().HasDynamicMember( memberName ) )
528 {
529 // 基底クラスのメンバを取得
530 return this->GetSuperClass().GetMemberOffset( memberName );
531 }
532
533 // 基底クラスのサイズを追加
534 resultSize += this->GetSuperClass().GetSize();
535
536 // 基底クラスのアラインメントを取得
537 alignment = this->GetSuperClass().GetAlignment();
538 }
539 else
540 {
541 // 基底クラスが存在しないとき
542
543 // 仮想関数が存在する場合はvtbl及びvtblMasterListへのポインタのサイズを追加
544 resultSize += IsExistVirtualFunctions() ? PTR_SIZE*2 : 0;
545 }
546 }
547
548 BOOST_FOREACH( CMember *pMember, dynamicMembers )
549 {
550 // メンバサイズ
551 int tempMemberSize = pMember->GetType().GetSize();
552
553 // 一時アラインメントを算出
554 int tempAlignment = tempMemberSize;
555 if( pMember->GetType().IsStruct() )
556 {
557 // メンバが構造体の場合は、メンバのアラインメントを取得
558 tempAlignment = pMember->GetType().GetClass().GetAlignment();
559 }
560
561 // アラインメントを考慮してパディングを追加
562 if( GetFixedAlignment() && alignment < tempAlignment )
563 {
564 if( resultSize % alignment )
565 {
566 resultSize += alignment - ( resultSize % alignment );
567 }
568 }
569 else
570 {
571 if( alignment < tempAlignment )
572 {
573 // 最大アラインメントを更新
574 alignment = tempAlignment;
575 }
576
577 if( tempMemberSize == 0 )
578 {
579 if( !pMember->GetType().IsStruct() )
580 {
581 compiler.errorMessenger.OutputFatalError();
582 }
583
584 //メンバを持たない構造体
585 //※何もしない(オフセットの計算をしない)
586 }
587 else{
588 if( resultSize % tempAlignment )
589 {
590 resultSize += tempAlignment - ( resultSize % tempAlignment );
591 }
592 }
593 }
594
595 if(memberName){
596 //メンバ指定がある場合は、オフセットを返す
597 if( pMember->GetName() == memberName )
598 {
599 return resultSize;
600 }
601 }
602
603 // メンバサイズを加算(配列を考慮)
604 resultSize += tempMemberSize * Variable::GetSubScriptCounts( pMember->GetSubscripts() );
605 }
606
607 if( alignment )
608 {
609 // 末尾アラインメントを考慮してパディングを追加
610 if( resultSize % alignment )
611 {
612 resultSize += alignment - ( resultSize % alignment );
613 }
614 }
615
616 return resultSize;
617}
618int CClass::GetAlignment() const
619{
620 int alignment = 1;
621 if( this->IsStructure() )
622 {
623 // 構造体のとき
624
625 if( this->GetFixedAlignment() )
626 {
627 // アラインメントの固定値が指定されていた場合はそれを取得
628 return this->GetFixedAlignment();
629 }
630 }
631 else
632 {
633 // それ以外
634
635 if( this->HasSuperClass() )
636 {
637 // 基底クラスのアラインメントを取得
638 alignment = this->GetSuperClass().GetAlignment();
639 }
640 else
641 {
642 // 基底クラスが存在しないとき
643
644 // 仮想関数が存在する場合はvtbl及びvtblMasterListへのポインタのサイズを追加
645 alignment = PTR_SIZE;
646 }
647 }
648
649 BOOST_FOREACH( CMember *pMember, dynamicMembers )
650 {
651 int tempAlignment = pMember->GetType().GetSize();
652 if( pMember->GetType().IsStruct() )
653 {
654 // メンバが構造体の場合は、メンバのアラインメントを取得
655 tempAlignment = pMember->GetType().GetClass().GetAlignment();
656 }
657
658 if( alignment < tempAlignment )
659 {
660 // 最大アラインメントを更新
661 alignment = tempAlignment;
662 }
663 }
664
665 return alignment;
666}
667
668void CClass::GetVtblMasterListIndexAndVtblIndex( const UserProc *pUserProc, int &vtblMasterListIndex, int &vtblIndex ) const
669{
670 vtblMasterListIndex = 0;
671
672 vtblIndex = 0;
673 BOOST_FOREACH( const CMethod *pMethod, GetDynamicMethods() ){
674 if( &pMethod->GetUserProc() == pUserProc )
675 {
676 return;
677 }
678
679 if( pMethod->IsVirtual() )
680 {
681 vtblIndex++;
682 }
683 }
684
685 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
686 {
687 vtblMasterListIndex++;
688
689 vtblIndex = 0;
690 BOOST_FOREACH( const CMethod *pMethod, pInterface->GetDynamicMethods() ){
691 if( &pMethod->GetUserProc() == pUserProc )
692 {
693 return;
694 }
695
696 if( pMethod->IsVirtual() )
697 {
698 vtblIndex++;
699 }
700 }
701 }
702
703 compiler.errorMessenger.OutputFatalError();
704 return;
705}
706int CClass::GetVtblMasterListIndex( const CClass *pClass ) const
707{
708 int result = 0;
709
710 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
711 {
712 result++;
713
714 if( &pInterface->GetClass() == pClass )
715 {
716 return result;
717 }
718 }
719
720 compiler.errorMessenger.OutputFatalError();
721 return 0;
722}
723long CClass::GetComVtblOffset() const
724{
725 return comVtblOffset;
726}
727long CClass::GetVtblMasterListOffset() const
728{
729 //既に存在する場合はそれを返す
730 if( vtblMasterListOffset == -1 )
731 {
732 compiler.errorMessenger.OutputFatalError();
733 }
734
735 return vtblMasterListOffset;
736}
737void CClass::GenerateVTableMasterList( const std::vector<long> &vtableMasterList, long &offset )
738{
739 offset = compiler.GetObjectModule().dataTable.AddBinary(
740 (void *)&vtableMasterList[0],
741 static_cast<int>(vtableMasterList.size()*sizeof(LONG_PTR))
742 );
743}
744void CClass::GenerateFullVTables()
745{
746 if( IsAbstract() )
747 {
748 // 抽象クラスは無視
749 return;
750 }
751 if( !IsUsing() )
752 {
753 // 使われていないクラスは無視
754 return;
755 }
756
757 // vtblマスターリストの元データに不要なデータが含まれていたらエラー
758 if( vtblMasterList.size() )
759 {
760 compiler.errorMessenger.OutputFatalError();
761 }
762
763 // 自身のクラスのvtblを生成
764 GetDynamicMethods().GenerateVTablePart( this->vtbl_offset );
765 vtblMasterList.push_back( this->vtbl_offset );
766
767 // インターフェイスのvtblを生成
768 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
769 {
770 long tempVtblOffset;
771 pInterface->GetDynamicMethods().GenerateVTablePart( tempVtblOffset );
772 vtblMasterList.push_back( tempVtblOffset );
773
774 pInterface->SetVtblOffset( tempVtblOffset );
775
776 if( pInterface->GetClass().IsComInterface() )
777 {
778 if( this->comVtblOffset )
779 {
780 compiler.errorMessenger.OutputFatalError();
781 }
782 this->comVtblOffset = tempVtblOffset;
783 }
784 }
785
786 // vtblマスターリストを生成
787 GenerateVTableMasterList( vtblMasterList, this->vtblMasterListOffset );
788}
789void CClass::ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
790{
791 if( IsAbstract() )
792 {
793 // 抽象クラスは無視
794 return;
795 }
796 if( !IsUsing() )
797 {
798 // 使われていないクラスは無視
799 return;
800 }
801 if(vtbl_offset==-1) return;
802
803 // 自身のクラスのvtbl
804 {
805 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + vtbl_offset);
806
807 for( int i=0; i<GetVtblNum(); i++ ){
808 const UserProc *pUserProc = (UserProc *)pVtbl[i];
809 if(!pUserProc) continue;
810
811 if( pUserProc->GetBeginOpAddress() == 0
812 && pUserProc->GetEndOpAddress() == 0 )
813 {
814 Jenga::Throw( "未解決の仮想関数が存在する" );
815 }
816
817 pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
818 }
819 }
820
821 // インターフェイスのvtbl
822 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
823 {
824 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + pInterface->GetVtblOffset());
825
826 for( int i=0; i<pInterface->GetClass().GetVtblNum(); i++ ){
827 const UserProc *pUserProc = (UserProc *)pVtbl[i];
828 if(!pUserProc) continue;
829
830 if( pUserProc->GetBeginOpAddress() == 0
831 && pUserProc->GetEndOpAddress() == 0 )
832 {
833 Jenga::Throw( "未解決の仮想関数が存在する" );
834 }
835
836 pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
837 }
838 }
839
840 // vtblマスターリスト
841 LONG_PTR *pVtblMasterList = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + vtblMasterListOffset );
842 for( int i=0; i<static_cast<int>(vtblMasterList.size()); i++ )
843 {
844 pVtblMasterList[i] = vtblMasterList[i] + ImageBase + MemPos_DataSection;
845 }
846}
847bool CClass::IsAbstract() const
848{
849 // 未実装(abstract)の仮想関数を持つ場合はtrueを返す
850
851 BOOST_FOREACH( const CMethod *pMethod, GetDynamicMethods() ){
852 if(pMethod->IsVirtual()){
853 if(pMethod->IsAbstract()){
854 return true;
855 }
856 }
857 }
858
859 // インターフェイスのvtbl
860 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
861 {
862 BOOST_FOREACH( const CMethod *pMethod, pInterface->GetDynamicMethods() ){
863 if(pMethod->IsVirtual()){
864 if(pMethod->IsAbstract()){
865 return true;
866 }
867 }
868 }
869 }
870
871 return false;
872}
873
874CClass *Classes::Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name){
875 return new CClass(namespaceScopes, importedNamespaces, name);
876}
877bool Classes::Insert( CClass *pClass, int nowLine )
878{
879 /////////////////////////////////
880 // ハッシュデータに追加
881 /////////////////////////////////
882
883 if( !Put( pClass ) )
884 {
885 compiler.errorMessenger.Output(15,pClass->GetName(), nowLine);
886 return false;
887 }
888 return true;
889}
890CClass *Classes::Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine){
891 //////////////////////////////////////////////////////////////////////////
892 // クラスを追加
893 // ※名前のみを登録。その他の情報はSetClassメソッドで!
894 //////////////////////////////////////////////////////////////////////////
895
896 CClass *pClass = Create(namespaceScopes, importedNamespaces, name);
897
898 if( !Insert( pClass, nowLine ) )
899 {
900 return NULL;
901 }
902
903 return pClass;
904}
905
906void Classes::GenerateVTables()
907{
908 Iterator_Reset();
909 while( Iterator_HasNext() )
910 {
911 CClass *pClass = Iterator_GetNext();
912 pClass->GenerateFullVTables();
913 }
914}
915
916void Classes::ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection ){
917 Iterator_Reset();
918 while( Iterator_HasNext() )
919 {
920 CClass *pClass = Iterator_GetNext();
921 pClass->ActionVtblSchedule( ImageBase, MemPos_CodeSection, MemPos_DataSection);
922 }
923}
924
925
926void Classes::InitStaticMember(){
927 //静的メンバをグローバル領域に作成
928
929 //イテレータをリセット
930
931 extern int cp;
932 int back_cp=cp;
933
934 this->Iterator_Reset();
935 while(this->Iterator_HasNext()){
936 CClass &objClass = *this->Iterator_GetNext();
937 if( objClass.isTargetObjectModule == false )
938 {
939 // 静的リンクライブラリの場合は飛ばす(既にインスタンスが定義済みであるため)
940 continue;
941 }
942
943 // 名前空間をセット
944 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes() = objClass.GetNamespaceScopes();
945
946 DWORD dwFlags = 0;
947 if( objClass.GetName() == "_System_TypeBase" )
948 {
949 // _System_TypeBaseクラスはグローバル、スタティック領域を初期化するためのクラスなのでここでの初期化は除外する
950 dwFlags |= DIMFLAG_NONCALL_CONSTRACTOR;
951 }
952
953 // コンパイル中クラスとしてセット
954 compiler.SetCompilingClass( &objClass );
955
956 const EnumInfo *pEnumInfo = NULL;
957 if( objClass.IsEnum() )
958 {
959 pEnumInfo = compiler.enumInfoCollection.Find( objClass );
960 }
961
962 int i=0;
963 BOOST_FOREACH( CMember *member, objClass.GetStaticMembers() )
964 {
965 if( pEnumInfo )
966 {
967 cp = pEnumInfo->GetEnumMember( member->GetName() ).GetSourceIndex();
968 }
969
970 char temporary[VN_SIZE];
971 sprintf(temporary,"%s.%s",objClass.GetName().c_str(),member->GetName().c_str());
972 dim(
973 temporary,
974 member->GetSubscripts(),
975 member->GetType(),
976 member->GetInitializeExpression().c_str(),
977 member->GetConstructParameter().c_str(),
978 dwFlags);
979
980 i++;
981 }
982
983 compiler.SetCompilingClass( NULL );
984 }
985
986 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().clear();
987
988 cp=back_cp;
989}
990
991void Classes::Compile_System_InitializeUserTypes(){
992 char temporary[VN_SIZE];
993
994 ////////////////////////////////////////////////////////////////////
995 // クラス登録
996 ////////////////////////////////////////////////////////////////////
997
998 // イテレータをリセット
999 Iterator_Reset();
1000
1001 while( Iterator_HasNext() ){
1002 const CClass &objClass = *Iterator_GetNext();
1003
1004 if( !objClass.IsUsing() ){
1005 // 未使用のクラスは無視する
1006 continue;
1007 }
1008
1009 std::string referenceOffsetsBuffer;
1010 int numOfReference = 0;
1011 objClass.GetReferenceOffsetsInitializeBuffer( referenceOffsetsBuffer, numOfReference );
1012
1013 sprintf( temporary
1014 , "Add(%c%c_System_TypeForClass[strNamespace=\"%s\",name=\"%s\",fullName=\"%s\",referenceOffsets=[%s],numOfReference=%d])"
1015 , 1
1016 , ESC_SYSTEM_STATIC_NEW
1017 , objClass.GetNamespaceScopes().ToString().c_str() // 名前空間
1018 , objClass.GetName().c_str() // クラス名
1019 , objClass.GetFullName().c_str() // フルネーム
1020 , referenceOffsetsBuffer.c_str() // 参照メンバオフセット配列
1021 , numOfReference // 参照メンバの個数
1022 );
1023
1024 // コンパイル
1025 ChangeOpcode( temporary );
1026
1027 objClass.SetTypeInfoDataTableOffset(
1028 compiler.GetObjectModule().dataTable.GetLastMadeConstObjectDataTableOffset()
1029 );
1030 }
1031}
1032void Classes::Compile_System_InitializeUserTypesForBaseType()
1033{
1034 extern int cp;
1035 cp = -1;
1036 ////////////////////////////////////////////////////////////////////
1037 // 基底クラスを登録
1038 ////////////////////////////////////////////////////////////////////
1039
1040 char temporary[8192];
1041 sprintf(temporary, "%c%ctempType=Nothing%c%c_System_TypeForClass"
1042 , HIBYTE( COM_DIM )
1043 , LOBYTE( COM_DIM )
1044 , 1
1045 , ESC_AS
1046 );
1047 ChangeOpcode( temporary );
1048
1049 // イテレータをリセット
1050 Iterator_Reset();
1051
1052 while( Iterator_HasNext() ){
1053 const CClass &objClass = *Iterator_GetNext();
1054
1055 if( !objClass.IsUsing() ){
1056 // 未使用のクラスは無視する
1057 continue;
1058 }
1059
1060 if( objClass.HasSuperClass() || objClass.GetDynamicMembers().size() ){
1061 sprintf( temporary
1062 , "tempType=Search(\"%s\") As ActiveBasic.Core._System_TypeForClass"
1063 , objClass.GetFullName().c_str()
1064 );
1065
1066 // コンパイル
1067 MakeMiddleCode( temporary );
1068 ChangeOpcode( temporary );
1069
1070 sprintf( temporary
1071 , "tempType.SetClassInfo(%d,_System_GetComVtbl(%s),_System_GetVtblList(%s),_System_GetDefaultConstructor(%s),_System_GetDestructor(%s))"
1072 , objClass.GetSize()
1073 , objClass.GetFullName().c_str()
1074 , objClass.GetFullName().c_str()
1075 , objClass.GetFullName().c_str()
1076 , objClass.GetFullName().c_str()
1077 , objClass.GetName().c_str()
1078 );
1079
1080 // コンパイル
1081 ChangeOpcode( temporary );
1082
1083 if( objClass.HasSuperClass() )
1084 {
1085 sprintf( temporary
1086 , "tempType.SetBaseType(Search(\"%s\"))"
1087 , objClass.GetSuperClass().GetFullName().c_str()
1088 );
1089
1090 // コンパイル
1091 ChangeOpcode( temporary );
1092 }
1093
1094 if( objClass.GetDynamicMembers().size() )
1095 {
1096 // メンバの型を示すTypeInfoオブジェクトへのDataOffset配列の静的データ定義文字列を取得
1097 sprintf(
1098 temporary,
1099 "tempType.SetMembers([%s],[%s],[%s],%d)",
1100 objClass.GetStaticDefiningStringAsMemberNames().c_str(),
1101 objClass.GetStaticDefiningStringAsMemberTypeInfoNames().c_str(),
1102 objClass.GetStaticDefiningStringAsMemberOffsets().c_str(),
1103 objClass.GetDynamicMembers().size()
1104 );
1105 ChangeOpcode( temporary );
1106 }
1107 }
1108 }
1109}
1110
1111const CClass *Classes::Find( const NamespaceScopes &namespaceScopes, const std::string &name ) const
1112{
1113 if( namespaceScopes.size() == 0 && name == "Object" ){
1114 return GetObjectClassPtr();
1115 }
1116 else if( namespaceScopes.size() == 0 && name == "String" ){
1117 return GetStringClassPtr();
1118 }
1119
1120 std::vector<const CClass *> classes;
1121 const CClass *pClass = GetHashArrayElement( name.c_str() );
1122 while( pClass )
1123 {
1124 if( pClass->IsEqualSymbol( namespaceScopes, name ) ){
1125 //名前空間とクラス名が一致した
1126 classes.push_back( pClass );
1127 }
1128 pClass = pClass->GetChainNext();
1129 }
1130 if( classes.size() > 0 )
1131 {
1132 // 複数の名前空間の中に同一のクラス名が存在する場合があるので、アクセス可能で尚且つ階層が一番深いものをチョイスする
1133 pClass = classes.front();
1134
1135 BOOST_FOREACH( const CClass *pTempClass, classes )
1136 {
1137 if( pClass->GetNamespaceScopes().size() < pTempClass->GetNamespaceScopes().size() )
1138 {
1139 pClass = pTempClass;
1140 }
1141 }
1142
1143 return pClass;
1144 }
1145
1146 // TypeDefも見る
1147 int index = compiler.GetObjectModule().meta.GetTypeDefs().GetIndex( namespaceScopes, name );
1148 if( index != -1 ){
1149 Type type = compiler.GetObjectModule().meta.GetTypeDefs()[index].GetBaseType();
1150 if( type.IsObject() ){
1151 return &type.GetClass();
1152 }
1153 }
1154
1155 return NULL;
1156}
1157const CClass *Classes::Find( const std::string &fullName ) const
1158{
1159 char AreaName[VN_SIZE] = ""; //オブジェクト変数
1160 char NestName[VN_SIZE] = ""; //入れ子メンバ
1161 bool isNest = SplitMemberName( fullName.c_str(), AreaName, NestName );
1162
1163 return Find( NamespaceScopes( AreaName ), NestName );
1164}
1165
1166const CClass *Classes::GetStringClassPtr() const
1167{
1168 if( !pStringClass ){
1169 // キャッシュしておく
1170 pStringClass = this->Find( NamespaceScopes( "System" ), "String" );
1171
1172 if( !pStringClass )
1173 {
1174 compiler.errorMessenger.Output(400, "System.String", cp);
1175 static CClass dummy;
1176 return &dummy;
1177 }
1178 return pStringClass;
1179 }
1180 return pStringClass;
1181}
1182const CClass *Classes::GetObjectClassPtr() const
1183{
1184 if( !pObjectClass ){
1185 // キャッシュしておく
1186 pObjectClass = this->Find( NamespaceScopes( "System" ), "Object" );
1187
1188 if( !pObjectClass )
1189 {
1190 compiler.errorMessenger.Output(400, "System.Object", cp);
1191 static CClass dummy;
1192 return &dummy;
1193 }
1194 return pObjectClass;
1195 }
1196 return pObjectClass;
1197}
1198const CClass *Classes::GetInterfaceInfoClassPtr() const
1199{
1200 if( !pInterfaceInfo ){
1201 // キャッシュしておく
1202 pInterfaceInfo = this->Find( "ActiveBasic.Core.InterfaceInfo" );
1203
1204 if( !pInterfaceInfo )
1205 {
1206 compiler.errorMessenger.Output(400, "ActiveBasic.Core.InterfaceInfo", cp);
1207 static CClass dummy;
1208 return &dummy;
1209 }
1210 return pInterfaceInfo;
1211 }
1212 return pInterfaceInfo;
1213}
1214
1215std::string CClass::GetStaticDefiningStringAsMemberNames() const
1216{
1217 std::string result;
1218
1219 BOOST_FOREACH( const CMember *pMember, dynamicMembers )
1220 {
1221 if( result.size() )
1222 {
1223 result += ",";
1224 }
1225
1226 result += "\"" + pMember->GetName() + "\"";
1227 }
1228
1229 return result;
1230}
1231std::string CClass::GetStaticDefiningStringAsMemberTypeInfoNames() const
1232{
1233 std::string result;
1234
1235 BOOST_FOREACH( const CMember *pMember, dynamicMembers )
1236 {
1237 if( result.size() )
1238 {
1239 result += ",";
1240 }
1241
1242 result += "\"" + compiler.TypeToString( pMember->GetType() ) + "\"";
1243 }
1244
1245 return result;
1246}
1247std::string CClass::GetStaticDefiningStringAsMemberOffsets() const
1248{
1249 std::string result;
1250
1251 BOOST_FOREACH( const CMember *pMember, dynamicMembers )
1252 {
1253 if( result.size() )
1254 {
1255 result += ",";
1256 }
1257
1258 int offset = this->GetMemberOffset( pMember->GetName().c_str() );
1259
1260 char temporary[255];
1261 itoa( offset, temporary, 16 );
1262
1263 result += (std::string)"&H" + temporary;
1264 }
1265
1266 return result;
1267}
1268
1269void CClass::GetReferenceOffsetsInitializeBuffer( std::string &referenceOffsetsBuffer, int &numOfReference, int baseOffset ) const
1270{
1271 const CClass &thisClass = *this;
1272 BOOST_FOREACH( const CMember *pMember, thisClass.GetDynamicMembers() )
1273 {
1274 if( pMember->GetType().IsObject() || pMember->GetType().IsPointer() )
1275 {
1276 if( referenceOffsetsBuffer.size() )
1277 {
1278 referenceOffsetsBuffer += ",";
1279 }
1280
1281 char temp[255];
1282 sprintf( temp, "%d", baseOffset + thisClass.GetMemberOffset( pMember->GetName().c_str() ) );
1283 referenceOffsetsBuffer += temp;
1284
1285 numOfReference++;
1286 }
1287 if( pMember->GetType().IsStruct() && !pMember->GetType().IsPointer() )
1288 {
1289 // 構造体の実体をメンバに持つとき
1290 int baseOffset = thisClass.GetMemberOffset( pMember->GetName().c_str() );
1291
1292 // 構造体メンバでGCによるチェックが必要な参照位置を追加
1293 pMember->GetType().GetClass().GetReferenceOffsetsInitializeBuffer( referenceOffsetsBuffer, numOfReference, baseOffset );
1294 }
1295 }
1296}
Note: See TracBrowser for help on using the repository browser.