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

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

CClass::GetDelegateメソッドを廃止し、代わりにMeta::ToDelegateメソッドを実装。

File size: 24.5 KB
RevLine 
[206]1#include "stdafx.h"
2
[266]3#include <Source.h>
[206]4#include <Class.h>
[184]5#include <Compiler.h>
6
7#include "../common.h"
8#ifdef _AMD64_
[485]9#include "../../compiler_x64/opcode.h"
[184]10#else
[484]11#include "../../compiler_x86/opcode.h"
[184]12#endif
13
[511]14using namespace ActiveBasic::Compiler;
[184]15
[540]16void CClass::Using() const
17{
18 if( this->IsUsing() )
19 {
20 // 既に使用することになっている
21 return;
22 }
[511]23
[540]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
[206]37bool CClass::IsClass() const
[193]38{
[206]39 return classType == CClass::Class;
40}
41bool CClass::IsInterface() const
42{
43 return classType == CClass::Interface;
44}
[370]45bool CClass::IsComInterface() const
46{
47 return classType == CClass::ComInterface;
48}
[206]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//自身の派生クラスかどうかを確認
[447]98bool CClass::IsSubClass( const CClass *pSubClass ) const
[206]99{
[447]100 if( !pSubClass->HasSuperClass() )
[206]101 {
[193]102 return false;
103 }
104
[447]105 const CClass *pTempClass = &pSubClass->GetSuperClass();
[206]106 while( pTempClass ){
107 if( this == pTempClass ) return true;
108 pTempClass = &pTempClass->GetSuperClass();
109 }
110 return false;
[193]111}
112
[206]113//自身と等しいまたは派生クラスかどうかを確認
[447]114bool CClass::IsEqualsOrSubClass( const CClass *pSubClass ) const
[206]115{
[447]116 if( IsEquals( pSubClass ) ) return true;
117 return IsSubClass( pSubClass );
[206]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{
[346]131 BOOST_FOREACH( const ::Interface *pInterface, interfaces ){
132 if( pInterfaceClass == &pInterface->GetClass() ){
[206]133 return true;
134 }
135 }
136 return false;
137}
138
[376]139bool CClass::InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine )
140{
[184]141 //メソッドをコピー
[342]142 BOOST_FOREACH( const CMethod *pBaseMethod, inheritsClass.GetDynamicMethods() ){
[184]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
[342]162 GetDynamicMethods().push_back( pMethod );
[184]163 }
164
165 //仮想関数の数
166 AddVtblNum( inheritsClass.GetVtblNum() );
167
168 //継承先のクラスをメンバとして保持する
[204]169 SetSuperClass( &inheritsClass );
[299]170 SetSuperClassActualTypeParameters( actualTypeParameters );
[184]171
[351]172 // インターフェイスを引き継ぐ
173 BOOST_FOREACH( ::Interface *pInterface, inheritsClass.GetInterfaces() )
174 {
175 interfaces.push_back( new ::Interface( *pInterface ) );
176 }
177
[370]178 if( this->IsInterface() && inheritsClass.IsComInterface() )
179 {
180 // COMインターフェイスを継承した場合はCOMインターフェイスにする
181 this->SetClassType( CClass::ComInterface );
[184]182 }
183
184 return true;
185}
[340]186
[561]187void CClass::AddDynamicMember( Member *pMember )
[184]188{
[561]189 dynamicMembers.push_back( pMember );
[184]190}
[561]191void CClass::AddStaticMember( Member *pMember )
192{
193 staticMembers.push_back( pMember );
[184]194}
195
[409]196bool CClass::DupliCheckAll(const char *name) const
197{
[206]198 //重複チェック
199
200 //メンバ
201 if(DupliCheckMember(name)) return 1;
202
203 //メソッド
[342]204 BOOST_FOREACH( const CMethod *pMethod, GetDynamicMethods() ){
[206]205 if( lstrcmp( name, pMethod->GetUserProc().GetName().c_str() ) == 0 ){
206 return 1;
207 }
208 }
209
210 return 0;
211}
[409]212bool CClass::DupliCheckMember(const char *name) const
213{
[206]214 //重複チェック
215
[409]216 if( this->HasSuperClass() )
217 {
218 if( this->GetSuperClass().DupliCheckMember( name ) )
219 {
220 // 基底クラスで重複が発見された
221 return true;
222 }
223 }
224
[206]225 // 動的メンバ
[561]226 BOOST_FOREACH( Member *pMember, dynamicMembers )
[409]227 {
228 if( GetName() == pMember->GetName() )
229 {
230 return true;
[206]231 }
232 }
233
234 // 静的メンバ
[561]235 BOOST_FOREACH( Member *pMember, staticMembers ){
[206]236 if( GetName() == pMember->GetName() ){
[409]237 return true;
[206]238 }
239 }
240
[409]241 return false;
[206]242}
243
[561]244const Member *CClass::FindDynamicMember( const char *memberName ) const
[409]245{
246 if( this->HasSuperClass() )
247 {
248 // 基底クラスで検索
[561]249 const Member *result = this->GetSuperClass().FindDynamicMember( memberName );
[409]250 if( result )
251 {
252 return result;
253 }
254 }
255
[561]256 BOOST_FOREACH( Member *pMember, GetDynamicMembers() )
[409]257 {
258 if( pMember->GetName() == memberName )
259 {
260 return pMember;
261 }
262 }
263 return NULL;
264}
265
[350]266void CClass::EnumDynamicMethodsOrInterfaceMethods( const char *methodName, std::vector<const UserProc *> &subs ) const
[347]267{
268 // 動的メソッド
269 GetDynamicMethods().Enum( methodName, subs );
270
271 // インターフェイス メソッド
272 BOOST_FOREACH( ::Interface *pInterface, GetInterfaces() )
273 {
274 pInterface->GetDynamicMethods().Enum( methodName, subs );
275 }
276}
[350]277const CMethod *CClass::GetDynamicMethodOrInterfaceMethod( const UserProc *pUserProc ) const
[347]278{
279 // 動的メソッド
280 const CMethod *result = GetDynamicMethods().GetMethodPtr( pUserProc );
281
282 if( !result )
283 {
284 // インターフェイス メソッド
285 BOOST_FOREACH( ::Interface *pInterface, GetInterfaces() )
286 {
287 result = pInterface->GetDynamicMethods().GetMethodPtr( pUserProc );
[350]288 if( result )
289 {
290 return result;
291 }
[347]292 }
293 }
294
295 return result;
296}
297
[206]298//サイズを取得
299int CClass::GetSize() const
[184]300{
[409]301 int resultSize = 0;
302
303 int alignment = 1;
304 if( this->IsStructure() )
305 {
306 // 構造体のとき
307
308 if( this->GetFixedAlignment() )
309 {
310 // アラインメントの固定値が指定されていた場合はそれを取得
311 alignment = this->GetFixedAlignment();
312 }
313 }
314 else
315 {
316 // それ以外
317
318 if( this->HasSuperClass() )
319 {
320 // 基底クラスのサイズを追加
321 resultSize += this->GetSuperClass().GetSize();
322
323 // 基底クラスのアラインメントを取得
324 alignment = this->GetSuperClass().GetAlignment();
325 }
326 else
327 {
328 // 基底クラスが存在しないとき
329
330 // 仮想関数が存在する場合はvtbl及びvtblMasterListへのポインタのサイズを追加
331 resultSize += IsExistVirtualFunctions() ? PTR_SIZE*2 : 0;
332 }
333 }
334
[561]335 BOOST_FOREACH( Member *pMember, dynamicMembers )
[409]336 {
337 // メンバサイズ
338 int tempMemberSize = pMember->GetType().GetSize();
339
340 // 一時アラインメントを算出
341 int tempAlignment = tempMemberSize;
342 if( pMember->GetType().IsStruct() )
343 {
344 // メンバが構造体の場合は、メンバのアラインメントを取得
345 tempAlignment = pMember->GetType().GetClass().GetAlignment();
346 }
347
348 // アラインメントを考慮してパディングを追加
349 if( GetFixedAlignment() && alignment < tempAlignment )
350 {
351 if( resultSize % alignment )
352 {
353 resultSize += alignment - ( resultSize % alignment );
354 }
355 }
356 else
357 {
358 if( alignment < tempAlignment )
359 {
360 // 最大アラインメントを更新
361 alignment = tempAlignment;
362 }
363
364 if( tempMemberSize == 0 )
365 {
366 if( !pMember->GetType().IsStruct() )
367 {
[465]368 compiler.errorMessenger.OutputFatalError();
[409]369 }
370
371 //メンバを持たない構造体
372 //※何もしない(オフセットの計算をしない)
373 }
374 else{
375 if( resultSize % tempAlignment )
376 {
377 resultSize += tempAlignment - ( resultSize % tempAlignment );
378 }
379 }
380 }
381
382 // メンバサイズを加算(配列を考慮)
383 resultSize += tempMemberSize * Variable::GetSubScriptCounts( pMember->GetSubscripts() );
384 }
385
386 if( alignment )
387 {
388 // 末尾アラインメントを考慮してパディングを追加
389 if( resultSize % alignment )
390 {
391 resultSize += alignment - ( resultSize % alignment );
392 }
393 }
394
395 return resultSize;
[206]396}
[184]397
[206]398//メンバのオフセットを取得
[409]399int CClass::GetMemberOffset( const char *memberName ) const
[206]400{
[409]401 int resultSize = 0;
[206]402
[232]403 int alignment = 1;
[409]404 if( this->IsStructure() )
[232]405 {
[409]406 // 構造体のとき
407
408 if( this->GetFixedAlignment() )
409 {
410 // アラインメントの固定値が指定されていた場合はそれを取得
411 alignment = this->GetFixedAlignment();
412 }
[232]413 }
[409]414 else
415 {
416 // それ以外
[206]417
[409]418 if( this->HasSuperClass() )
419 {
420 if( this->GetSuperClass().HasDynamicMember( memberName ) )
421 {
422 // 基底クラスのメンバを取得
423 return this->GetSuperClass().GetMemberOffset( memberName );
424 }
[206]425
[409]426 // 基底クラスのサイズを追加
427 resultSize += this->GetSuperClass().GetSize();
[206]428
[409]429 // 基底クラスのアラインメントを取得
430 alignment = this->GetSuperClass().GetAlignment();
[206]431 }
[409]432 else
433 {
434 // 基底クラスが存在しないとき
435
436 // 仮想関数が存在する場合はvtbl及びvtblMasterListへのポインタのサイズを追加
437 resultSize += IsExistVirtualFunctions() ? PTR_SIZE*2 : 0;
[206]438 }
[409]439 }
[206]440
[561]441 BOOST_FOREACH( Member *pMember, dynamicMembers )
[409]442 {
443 // メンバサイズ
444 int tempMemberSize = pMember->GetType().GetSize();
445
446 // 一時アラインメントを算出
447 int tempAlignment = tempMemberSize;
448 if( pMember->GetType().IsStruct() )
449 {
450 // メンバが構造体の場合は、メンバのアラインメントを取得
451 tempAlignment = pMember->GetType().GetClass().GetAlignment();
[206]452 }
453
[409]454 // アラインメントを考慮してパディングを追加
455 if( GetFixedAlignment() && alignment < tempAlignment )
456 {
457 if( resultSize % alignment )
458 {
459 resultSize += alignment - ( resultSize % alignment );
460 }
461 }
462 else
463 {
464 if( alignment < tempAlignment )
465 {
466 // 最大アラインメントを更新
467 alignment = tempAlignment;
468 }
469
470 if( tempMemberSize == 0 )
471 {
472 if( !pMember->GetType().IsStruct() )
473 {
[465]474 compiler.errorMessenger.OutputFatalError();
[409]475 }
476
477 //メンバを持たない構造体
[206]478 //※何もしない(オフセットの計算をしない)
479 }
480 else{
[409]481 if( resultSize % tempAlignment )
482 {
483 resultSize += tempAlignment - ( resultSize % tempAlignment );
484 }
[206]485 }
486 }
487
488 if(memberName){
489 //メンバ指定がある場合は、オフセットを返す
[409]490 if( pMember->GetName() == memberName )
491 {
492 return resultSize;
[206]493 }
494 }
495
[409]496 // メンバサイズを加算(配列を考慮)
497 resultSize += tempMemberSize * Variable::GetSubScriptCounts( pMember->GetSubscripts() );
[206]498 }
499
[409]500 if( alignment )
501 {
502 // 末尾アラインメントを考慮してパディングを追加
503 if( resultSize % alignment )
504 {
505 resultSize += alignment - ( resultSize % alignment );
506 }
[206]507 }
508
[409]509 return resultSize;
[206]510}
511int CClass::GetAlignment() const
512{
[409]513 int alignment = 1;
514 if( this->IsStructure() )
515 {
516 // 構造体のとき
[206]517
[409]518 if( this->GetFixedAlignment() )
519 {
520 // アラインメントの固定値が指定されていた場合はそれを取得
521 return this->GetFixedAlignment();
[206]522 }
[409]523 }
524 else
525 {
526 // それ以外
527
528 if( this->HasSuperClass() )
529 {
530 // 基底クラスのアラインメントを取得
531 alignment = this->GetSuperClass().GetAlignment();
[206]532 }
[409]533 else
534 {
535 // 基底クラスが存在しないとき
[206]536
[409]537 // 仮想関数が存在する場合はvtbl及びvtblMasterListへのポインタのサイズを追加
538 alignment = PTR_SIZE;
539 }
[206]540 }
541
[561]542 BOOST_FOREACH( Member *pMember, dynamicMembers )
[409]543 {
544 int tempAlignment = pMember->GetType().GetSize();
545 if( pMember->GetType().IsStruct() )
546 {
547 // メンバが構造体の場合は、メンバのアラインメントを取得
548 tempAlignment = pMember->GetType().GetClass().GetAlignment();
549 }
[206]550
[409]551 if( alignment < tempAlignment )
552 {
553 // 最大アラインメントを更新
554 alignment = tempAlignment;
555 }
556 }
[206]557
558 return alignment;
559}
[342]560
[348]561void CClass::GetVtblMasterListIndexAndVtblIndex( const UserProc *pUserProc, int &vtblMasterListIndex, int &vtblIndex ) const
[342]562{
[348]563 vtblMasterListIndex = 0;
564
565 vtblIndex = 0;
[342]566 BOOST_FOREACH( const CMethod *pMethod, GetDynamicMethods() ){
567 if( &pMethod->GetUserProc() == pUserProc )
568 {
[348]569 return;
[342]570 }
[348]571
572 if( pMethod->IsVirtual() )
573 {
574 vtblIndex++;
575 }
[342]576 }
577
[346]578 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
[342]579 {
[348]580 vtblMasterListIndex++;
[342]581
[348]582 vtblIndex = 0;
[347]583 BOOST_FOREACH( const CMethod *pMethod, pInterface->GetDynamicMethods() ){
[342]584 if( &pMethod->GetUserProc() == pUserProc )
585 {
[348]586 return;
[342]587 }
[348]588
589 if( pMethod->IsVirtual() )
590 {
591 vtblIndex++;
592 }
[342]593 }
594 }
595
[465]596 compiler.errorMessenger.OutputFatalError();
[348]597 return;
[342]598}
[350]599int CClass::GetVtblMasterListIndex( const CClass *pClass ) const
600{
601 int result = 0;
602
603 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
604 {
605 result++;
606
607 if( &pInterface->GetClass() == pClass )
608 {
609 return result;
610 }
611 }
612
[465]613 compiler.errorMessenger.OutputFatalError();
[350]614 return 0;
615}
[345]616long CClass::GetVtblMasterListOffset() const
[206]617{
[184]618 //既に存在する場合はそれを返す
[342]619 if( vtblMasterListOffset == -1 )
620 {
[465]621 compiler.errorMessenger.OutputFatalError();
[342]622 }
[184]623
[342]624 return vtblMasterListOffset;
625}
[206]626bool CClass::IsAbstract() const
627{
628 // 未実装(abstract)の仮想関数を持つ場合はtrueを返す
[184]629
[342]630 BOOST_FOREACH( const CMethod *pMethod, GetDynamicMethods() ){
[206]631 if(pMethod->IsVirtual()){
632 if(pMethod->IsAbstract()){
633 return true;
634 }
635 }
636 }
637
[351]638 // インターフェイスのvtbl
639 BOOST_FOREACH( const ::Interface *pInterface, interfaces )
640 {
641 BOOST_FOREACH( const CMethod *pMethod, pInterface->GetDynamicMethods() ){
642 if(pMethod->IsVirtual()){
643 if(pMethod->IsAbstract()){
644 return true;
645 }
646 }
647 }
648 }
649
[206]650 return false;
[184]651}
652
[206]653CClass *Classes::Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name){
654 return new CClass(namespaceScopes, importedNamespaces, name);
655}
[369]656bool Classes::Insert( CClass *pClass, int nowLine )
[206]657{
658 /////////////////////////////////
659 // ハッシュデータに追加
660 /////////////////////////////////
661
[270]662 if( !Put( pClass ) )
663 {
[465]664 compiler.errorMessenger.Output(15,pClass->GetName(), nowLine);
[270]665 return false;
[206]666 }
667 return true;
668}
669CClass *Classes::Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine){
670 //////////////////////////////////////////////////////////////////////////
671 // クラスを追加
672 // ※名前のみを登録。その他の情報はSetClassメソッドで!
673 //////////////////////////////////////////////////////////////////////////
674
675 CClass *pClass = Create(namespaceScopes, importedNamespaces, name);
676
[369]677 if( !Insert( pClass, nowLine ) )
[206]678 {
679 return NULL;
680 }
681
682 return pClass;
683}
684
[282]685
[206]686void Classes::InitStaticMember(){
[184]687 //静的メンバをグローバル領域に作成
688
689 //イテレータをリセット
690
691 extern int cp;
692 int back_cp=cp;
693
[270]694 this->Iterator_Reset();
[184]695 while(this->Iterator_HasNext()){
696 CClass &objClass = *this->Iterator_GetNext();
[272]697 if( objClass.isTargetObjectModule == false )
698 {
699 // 静的リンクライブラリの場合は飛ばす(既にインスタンスが定義済みであるため)
700 continue;
701 }
[184]702
703 // 名前空間をセット
[199]704 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes() = objClass.GetNamespaceScopes();
[184]705
[355]706 DWORD dwFlags = 0;
707 if( objClass.GetName() == "_System_TypeBase" )
708 {
709 // _System_TypeBaseクラスはグローバル、スタティック領域を初期化するためのクラスなのでここでの初期化は除外する
710 dwFlags |= DIMFLAG_NONCALL_CONSTRACTOR;
711 }
712
[406]713 // コンパイル中クラスとしてセット
[536]714 compiler.SetCompilingClass( &objClass );
[406]715
716 const EnumInfo *pEnumInfo = NULL;
717 if( objClass.IsEnum() )
718 {
719 pEnumInfo = compiler.enumInfoCollection.Find( objClass );
720 }
721
[184]722 int i=0;
[561]723 BOOST_FOREACH( Member *member, objClass.GetStaticMembers() )
[406]724 {
725 if( pEnumInfo )
726 {
727 cp = pEnumInfo->GetEnumMember( member->GetName() ).GetSourceIndex();
728 }
729
[184]730 char temporary[VN_SIZE];
731 sprintf(temporary,"%s.%s",objClass.GetName().c_str(),member->GetName().c_str());
732 dim(
733 temporary,
[206]734 member->GetSubscripts(),
[184]735 member->GetType(),
736 member->GetInitializeExpression().c_str(),
737 member->GetConstructParameter().c_str(),
[355]738 dwFlags);
[184]739
740 i++;
741 }
[406]742
[536]743 compiler.SetCompilingClass( NULL );
[184]744 }
745
[199]746 compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().clear();
[184]747
748 cp=back_cp;
749}
750
[206]751void Classes::Compile_System_InitializeUserTypes(){
[184]752 char temporary[VN_SIZE];
753
754 ////////////////////////////////////////////////////////////////////
755 // クラス登録
756 ////////////////////////////////////////////////////////////////////
757
758 // イテレータをリセット
759 Iterator_Reset();
760
761 while( Iterator_HasNext() ){
762 const CClass &objClass = *Iterator_GetNext();
763
764 if( !objClass.IsUsing() ){
765 // 未使用のクラスは無視する
766 continue;
[417]767 }
[184]768
[417]769 std::string referenceOffsetsBuffer;
[184]770 int numOfReference = 0;
[417]771 objClass.GetReferenceOffsetsInitializeBuffer( referenceOffsetsBuffer, numOfReference );
[184]772
773 sprintf( temporary
[355]774 , "Add(%c%c_System_TypeForClass[strNamespace=\"%s\",name=\"%s\",fullName=\"%s\",referenceOffsets=[%s],numOfReference=%d])"
[184]775 , 1
[355]776 , ESC_SYSTEM_STATIC_NEW
777 , objClass.GetNamespaceScopes().ToString().c_str() // 名前空間
778 , objClass.GetName().c_str() // クラス名
779 , objClass.GetFullName().c_str() // フルネーム
[417]780 , referenceOffsetsBuffer.c_str() // 参照メンバオフセット配列
[355]781 , numOfReference // 参照メンバの個数
[184]782 );
783
784 // コンパイル
785 ChangeOpcode( temporary );
[355]786
787 objClass.SetTypeInfoDataTableOffset(
788 compiler.GetObjectModule().dataTable.GetLastMadeConstObjectDataTableOffset()
789 );
[184]790 }
[355]791}
792void Classes::Compile_System_InitializeUserTypesForBaseType()
793{
794 extern int cp;
795 cp = -1;
[184]796 ////////////////////////////////////////////////////////////////////
797 // 基底クラスを登録
798 ////////////////////////////////////////////////////////////////////
799
[387]800 char temporary[8192];
[431]801 sprintf(temporary, "%c%ctempType=Nothing%c%c_System_TypeForClass"
[184]802 , HIBYTE( COM_DIM )
803 , LOBYTE( COM_DIM )
804 , 1
805 , ESC_AS
806 );
807 ChangeOpcode( temporary );
808
809 // イテレータをリセット
810 Iterator_Reset();
811
812 while( Iterator_HasNext() ){
813 const CClass &objClass = *Iterator_GetNext();
814
815 if( !objClass.IsUsing() ){
816 // 未使用のクラスは無視する
817 continue;
818 }
819
[409]820 if( objClass.HasSuperClass() || objClass.GetDynamicMembers().size() ){
[184]821 sprintf( temporary
[431]822 , "tempType=Search(\"%s\") As ActiveBasic.Core._System_TypeForClass"
[355]823 , objClass.GetFullName().c_str()
[387]824 );
[184]825
826 // コンパイル
[431]827 MakeMiddleCode( temporary );
[184]828 ChangeOpcode( temporary );
829
[431]830 sprintf( temporary
831 , "tempType.SetClassInfo(%d,_System_GetComVtbl(%s),_System_GetVtblList(%s),_System_GetDefaultConstructor(%s),_System_GetDestructor(%s))"
832 , objClass.GetSize()
833 , objClass.GetFullName().c_str()
834 , objClass.GetFullName().c_str()
835 , objClass.GetFullName().c_str()
836 , objClass.GetFullName().c_str()
837 , objClass.GetName().c_str()
838 );
839
840 // コンパイル
841 ChangeOpcode( temporary );
842
[409]843 if( objClass.HasSuperClass() )
844 {
845 sprintf( temporary
846 , "tempType.SetBaseType(Search(\"%s\"))"
847 , objClass.GetSuperClass().GetFullName().c_str()
848 );
[184]849
[409]850 // コンパイル
851 ChangeOpcode( temporary );
852 }
[184]853
[409]854 if( objClass.GetDynamicMembers().size() )
855 {
856 // メンバの型を示すTypeInfoオブジェクトへのDataOffset配列の静的データ定義文字列を取得
857 sprintf(
858 temporary,
[412]859 "tempType.SetMembers([%s],[%s],[%s],%d)",
[409]860 objClass.GetStaticDefiningStringAsMemberNames().c_str(),
861 objClass.GetStaticDefiningStringAsMemberTypeInfoNames().c_str(),
[412]862 objClass.GetStaticDefiningStringAsMemberOffsets().c_str(),
[409]863 objClass.GetDynamicMembers().size()
864 );
865 ChangeOpcode( temporary );
866 }
[184]867 }
[387]868 }
[184]869}
[193]870
[523]871const CClass *Classes::Find( const NamespaceScopes &namespaceScopes, const std::string &name ) const
[193]872{
873 if( namespaceScopes.size() == 0 && name == "Object" ){
874 return GetObjectClassPtr();
875 }
876 else if( namespaceScopes.size() == 0 && name == "String" ){
877 return GetStringClassPtr();
878 }
879
[380]880 std::vector<const CClass *> classes;
[270]881 const CClass *pClass = GetHashArrayElement( name.c_str() );
882 while( pClass )
883 {
884 if( pClass->IsEqualSymbol( namespaceScopes, name ) ){
885 //名前空間とクラス名が一致した
[380]886 classes.push_back( pClass );
[193]887 }
[270]888 pClass = pClass->GetChainNext();
[193]889 }
[380]890 if( classes.size() > 0 )
891 {
892 // 複数の名前空間の中に同一のクラス名が存在する場合があるので、アクセス可能で尚且つ階層が一番深いものをチョイスする
893 pClass = classes.front();
[193]894
[380]895 BOOST_FOREACH( const CClass *pTempClass, classes )
896 {
897 if( pClass->GetNamespaceScopes().size() < pTempClass->GetNamespaceScopes().size() )
898 {
899 pClass = pTempClass;
900 }
901 }
902
903 return pClass;
904 }
905
[193]906 // TypeDefも見る
[265]907 int index = compiler.GetObjectModule().meta.GetTypeDefs().GetIndex( namespaceScopes, name );
[193]908 if( index != -1 ){
[265]909 Type type = compiler.GetObjectModule().meta.GetTypeDefs()[index].GetBaseType();
[193]910 if( type.IsObject() ){
911 return &type.GetClass();
912 }
913 }
914
915 return NULL;
916}
[523]917const CClass *Classes::Find( const std::string &fullName ) const
[206]918{
919 char AreaName[VN_SIZE] = ""; //オブジェクト変数
920 char NestName[VN_SIZE] = ""; //入れ子メンバ
921 bool isNest = SplitMemberName( fullName.c_str(), AreaName, NestName );
922
923 return Find( NamespaceScopes( AreaName ), NestName );
924}
925
[272]926const CClass *Classes::GetStringClassPtr() const
[206]927{
928 if( !pStringClass ){
[272]929 // キャッシュしておく
930 pStringClass = this->Find( NamespaceScopes( "System" ), "String" );
931
932 if( !pStringClass )
933 {
[465]934 compiler.errorMessenger.Output(400, "System.String", cp);
[351]935 static CClass dummy;
936 return &dummy;
[272]937 }
938 return pStringClass;
[206]939 }
940 return pStringClass;
941}
[272]942const CClass *Classes::GetObjectClassPtr() const
[206]943{
944 if( !pObjectClass ){
[272]945 // キャッシュしておく
946 pObjectClass = this->Find( NamespaceScopes( "System" ), "Object" );
947
948 if( !pObjectClass )
949 {
[465]950 compiler.errorMessenger.Output(400, "System.Object", cp);
[351]951 static CClass dummy;
952 return &dummy;
[272]953 }
954 return pObjectClass;
[206]955 }
956 return pObjectClass;
957}
[349]958const CClass *Classes::GetInterfaceInfoClassPtr() const
959{
960 if( !pInterfaceInfo ){
961 // キャッシュしておく
962 pInterfaceInfo = this->Find( "ActiveBasic.Core.InterfaceInfo" );
963
964 if( !pInterfaceInfo )
965 {
[465]966 compiler.errorMessenger.Output(400, "ActiveBasic.Core.InterfaceInfo", cp);
[351]967 static CClass dummy;
968 return &dummy;
[349]969 }
970 return pInterfaceInfo;
971 }
972 return pInterfaceInfo;
973}
[387]974
975std::string CClass::GetStaticDefiningStringAsMemberNames() const
976{
977 std::string result;
978
[561]979 BOOST_FOREACH( const Member *pMember, dynamicMembers )
[387]980 {
981 if( result.size() )
982 {
983 result += ",";
984 }
985
986 result += "\"" + pMember->GetName() + "\"";
987 }
988
989 return result;
990}
991std::string CClass::GetStaticDefiningStringAsMemberTypeInfoNames() const
992{
993 std::string result;
994
[561]995 BOOST_FOREACH( const Member *pMember, dynamicMembers )
[387]996 {
997 if( result.size() )
998 {
999 result += ",";
1000 }
1001
1002 result += "\"" + compiler.TypeToString( pMember->GetType() ) + "\"";
1003 }
1004
1005 return result;
1006}
[412]1007std::string CClass::GetStaticDefiningStringAsMemberOffsets() const
1008{
1009 std::string result;
[387]1010
[561]1011 BOOST_FOREACH( const Member *pMember, dynamicMembers )
[412]1012 {
1013 if( result.size() )
1014 {
1015 result += ",";
1016 }
1017
1018 int offset = this->GetMemberOffset( pMember->GetName().c_str() );
1019
1020 char temporary[255];
1021 itoa( offset, temporary, 16 );
1022
1023 result += (std::string)"&H" + temporary;
1024 }
1025
1026 return result;
1027}
[417]1028
1029void CClass::GetReferenceOffsetsInitializeBuffer( std::string &referenceOffsetsBuffer, int &numOfReference, int baseOffset ) const
1030{
1031 const CClass &thisClass = *this;
[561]1032 BOOST_FOREACH( const Member *pMember, thisClass.GetDynamicMembers() )
[417]1033 {
1034 if( pMember->GetType().IsObject() || pMember->GetType().IsPointer() )
1035 {
1036 if( referenceOffsetsBuffer.size() )
1037 {
1038 referenceOffsetsBuffer += ",";
1039 }
1040
1041 char temp[255];
1042 sprintf( temp, "%d", baseOffset + thisClass.GetMemberOffset( pMember->GetName().c_str() ) );
1043 referenceOffsetsBuffer += temp;
1044
1045 numOfReference++;
1046 }
1047 if( pMember->GetType().IsStruct() && !pMember->GetType().IsPointer() )
1048 {
1049 // 構造体の実体をメンバに持つとき
1050 int baseOffset = thisClass.GetMemberOffset( pMember->GetName().c_str() );
1051
1052 // 構造体メンバでGCによるチェックが必要な参照位置を追加
1053 pMember->GetType().GetClass().GetReferenceOffsetsInitializeBuffer( referenceOffsetsBuffer, numOfReference, baseOffset );
1054 }
1055 }
1056}
Note: See TracBrowser for help on using the repository browser.