source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/include/Class.h@ 498

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

・ジェネリックな型をパラメータに持つメソッドのオーバーロード解決に対応した。
・型パラメータの制約クラス指定に対応した。

File size: 16.4 KB
Line 
1#pragma once
2
3#include <option.h>
4#include <Program.h>
5#include <Prototype.h>
6#include <Type.h>
7#include <Method.h>
8#include <Member.h>
9#include <Source.h>
10
11class UserProc;
12class CClass;
13class Delegate;
14
15class DynamicMethodsPrototype
16{
17 // 動的メソッド
18 Methods dynamicMethods;
19
20 // XMLシリアライズ用
21private:
22 friend class boost::serialization::access;
23 template<class Archive> void serialize(Archive& ar, const unsigned int version)
24 {
25 ar & BOOST_SERIALIZATION_NVP( dynamicMethods );
26 }
27
28public:
29 DynamicMethodsPrototype(){}
30 DynamicMethodsPrototype( const DynamicMethodsPrototype &dynamicMethodsPrototype )
31 : dynamicMethods( dynamicMethodsPrototype.dynamicMethods )
32 {
33 }
34 ~DynamicMethodsPrototype(){}
35
36 const Methods &GetDynamicMethods() const
37 {
38 return dynamicMethods;
39 }
40 Methods &GetDynamicMethods()
41 {
42 return dynamicMethods;
43 }
44
45 void AddDynamicMethods( CMethod *pMethod )
46 {
47 dynamicMethods.push_back( pMethod );
48 }
49};
50
51class ClassPrototype : public Prototype, public DynamicMethodsPrototype
52{
53 // XMLシリアライズ用
54private:
55 friend class boost::serialization::access;
56 template<class Archive> void serialize(Archive& ar, const unsigned int version)
57 {
58 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Prototype );
59 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( DynamicMethodsPrototype );
60 }
61
62public:
63 ClassPrototype( const NamespaceScopes &namespaceScopes, const string &name )
64 : Prototype( namespaceScopes, name )
65 , DynamicMethodsPrototype()
66 {
67 }
68 ClassPrototype()
69 : Prototype()
70 , DynamicMethodsPrototype()
71 {
72 }
73};
74
75class Interface : public DynamicMethodsPrototype
76{
77 const CClass *pInterfaceClass;
78 mutable LONG_PTR vtblOffset;
79
80 // 型パラメータ(実パラメータ)
81 Types actualTypeParameters;
82
83 // XMLシリアライズ用
84private:
85 friend class boost::serialization::access;
86 template<class Archive> void serialize(Archive& ar, const unsigned int version)
87 {
88 trace_for_serialize( "serializing - Interface" );
89
90 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( DynamicMethodsPrototype );
91 ar & boost::serialization::make_nvp("pInterfaceClass", const_cast<CClass *&>(pInterfaceClass) );
92 ar & BOOST_SERIALIZATION_NVP( vtblOffset );
93 ar & BOOST_SERIALIZATION_NVP( actualTypeParameters );
94 }
95
96public:
97 Interface( const CClass *pInterfaceClass, const Types &actualTypeParameters );
98 Interface( const Interface &objInterface )
99 : DynamicMethodsPrototype( objInterface )
100 , pInterfaceClass( objInterface.pInterfaceClass )
101 , vtblOffset( objInterface.vtblOffset )
102 {
103 }
104 Interface()
105 {
106 }
107
108 const CClass &GetClass() const{
109 return *pInterfaceClass;
110 }
111 LONG_PTR GetVtblOffset() const
112 {
113 return vtblOffset;
114 }
115 void SetVtblOffset( LONG_PTR vtblOffset ) const
116 {
117 this->vtblOffset = vtblOffset;
118 }
119
120 const Types &GetActualTypeParameters() const
121 {
122 return actualTypeParameters;
123 }
124
125 std::string GetFullNameWithActualGenericTypeParameters() const;
126};
127typedef std::vector<Interface *> Interfaces;
128
129class CClass: public ClassPrototype, public Jenga::Common::ObjectInHashmap<CClass>
130{
131public:
132 // 型の種類
133 enum ClassType{
134 Class,
135 Interface,
136 ComInterface,
137 Enum,
138 Delegate,
139 Structure,
140 };
141
142private:
143 ClassType classType;
144
145 // importされている名前空間
146 NamespaceScopesCollection importedNamespaces;
147
148 // 型パラメータ
149 GenericTypes formalGenericTypes;
150
151 // 基底クラス
152 const CClass *pSuperClass;
153
154 // 基底クラスの型パラメータ(実パラメータ)
155 Types superClassActualTypeParameters;
156
157 // Blittable型情報
158 Type blittableType;
159
160 // 実装するインターフェイス
161 Interfaces interfaces;
162
163 // 動的メンバ
164 Members dynamicMembers;
165
166 // 静的メンバ
167 Members staticMembers;
168
169 // 動的メソッド
170 int ConstructorMemberSubIndex;
171 int DestructorMemberSubIndex;
172 int vtblNum; // 仮想関数の数
173
174 // 静的メソッド
175 Methods staticMethods;
176
177 //アラインメント値
178 int fixedAlignment;
179
180 // XMLシリアライズ用
181private:
182 friend class boost::serialization::access;
183 template<class Archive> void serialize(Archive& ar, const unsigned int version)
184 {
185 trace_for_serialize( "serializing - CClass" );
186
187 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( ClassPrototype );
188 ar & BOOST_SERIALIZATION_NVP( classType );
189 ar & BOOST_SERIALIZATION_NVP( importedNamespaces );
190 ar & BOOST_SERIALIZATION_NVP( formalGenericTypes );
191 ar & boost::serialization::make_nvp( "pSuperClass", const_cast<CClass *&>(pSuperClass) );
192 ar & BOOST_SERIALIZATION_NVP( superClassActualTypeParameters );
193 ar & BOOST_SERIALIZATION_NVP( blittableType );
194 ar & BOOST_SERIALIZATION_NVP( interfaces );
195 ar & BOOST_SERIALIZATION_NVP( dynamicMembers );
196 ar & BOOST_SERIALIZATION_NVP( staticMembers );
197 ar & BOOST_SERIALIZATION_NVP( ConstructorMemberSubIndex );
198 ar & BOOST_SERIALIZATION_NVP( DestructorMemberSubIndex );
199 ar & BOOST_SERIALIZATION_NVP( vtblNum );
200 ar & BOOST_SERIALIZATION_NVP( staticMethods );
201 ar & BOOST_SERIALIZATION_NVP( fixedAlignment );
202 }
203
204 bool isReady;
205public:
206
207 CClass( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const string &name )
208 : ClassPrototype( namespaceScopes, name )
209 , importedNamespaces( importedNamespaces )
210 , classType( Class )
211 , pSuperClass( NULL )
212 , blittableType( Type() )
213 , isReady( false )
214 , fixedAlignment( 0 )
215 , ConstructorMemberSubIndex( -1 )
216 , DestructorMemberSubIndex( -1 )
217 , vtblNum( 0 )
218 , vtbl_offset( -1 )
219 , comVtblOffset( 0 )
220 , isCompilingConstructor( false )
221 , isCompilingDestructor( false )
222 , pobj_NextClass( NULL )
223 , cacheSize( 0 )
224 {
225 }
226 CClass()
227 : ClassPrototype()
228 , importedNamespaces()
229 , classType()
230 , pSuperClass( NULL )
231 , blittableType( Type() )
232 , isReady( false )
233 , fixedAlignment( 0 )
234 , ConstructorMemberSubIndex( -1 )
235 , DestructorMemberSubIndex( -1 )
236 , vtblNum( 0 )
237 , vtbl_offset( -1 )
238 , comVtblOffset( 0 )
239 , isCompilingConstructor( false )
240 , isCompilingDestructor( false )
241 , pobj_NextClass( NULL )
242 , cacheSize( 0 )
243 {
244 }
245 ~CClass()
246 {
247 // 動的メンバ
248 BOOST_FOREACH( CMember *member, dynamicMembers )
249 {
250 delete member;
251 }
252
253 // 静的メンバ
254 BOOST_FOREACH( CMember *member, staticMembers )
255 {
256 delete member;
257 }
258
259 // インターフェイス
260 BOOST_FOREACH( ::Interface *pInterface, interfaces )
261 {
262 delete pInterface;
263 }
264 }
265
266 virtual const std::string &GetKeyName() const
267 {
268 return GetName();
269 }
270 virtual bool IsDuplication( const CClass *pClass ) const
271 {
272 if( pClass->IsEqualSymbol( *this ) )
273 {
274 return true;
275 }
276 return false;
277 }
278
279 void Readed(){
280 isReady = true;
281 }
282 bool IsReady() const{
283 return isReady;
284 }
285
286 const NamespaceScopesCollection &GetImportedNamespaces() const
287 {
288 return importedNamespaces;
289 }
290
291 // 型パラメータ
292 const GenericTypes &GetFormalGenericTypes() const
293 {
294 return formalGenericTypes;
295 }
296 void AddFormalGenericType( GenericType genericType )
297 {
298 this->formalGenericTypes.push_back( genericType );
299 }
300 int GetFormalGenericTypeParameterIndex( const std::string &name ) const
301 {
302 int i = 0;
303 BOOST_FOREACH( const GenericType &genericType, formalGenericTypes )
304 {
305 if( genericType.GetName() == name )
306 {
307 return i;
308 }
309 i++;
310 }
311 return -1;
312 }
313 bool IsExistFormalGenericTypeParameter( const std::string &name ) const
314 {
315 BOOST_FOREACH( const GenericType &genericType, formalGenericTypes )
316 {
317 if( genericType.GetName() == name )
318 {
319 return true;
320 }
321 }
322 return false;
323 }
324 bool IsGeneric() const
325 {
326 return ( this->formalGenericTypes.size() != 0 );
327 }
328
329 // 継承元クラス
330 bool HasSuperClass() const
331 {
332 return ( pSuperClass != NULL );
333 }
334 const CClass &GetSuperClass() const
335 {
336 return *pSuperClass;
337 }
338 void SetSuperClass( const CClass *pSuperClass )
339 {
340 this->pSuperClass = pSuperClass;
341 }
342 const Types &GetSuperClassActualTypeParameters() const
343 {
344 return superClassActualTypeParameters;
345 }
346 void SetSuperClassActualTypeParameters( const Types &actualTypeParameters )
347 {
348 this->superClassActualTypeParameters = actualTypeParameters;
349 }
350
351 // Blittable型
352 bool IsBlittableType() const
353 {
354 return !blittableType.IsNull();
355 }
356 const Type &GetBlittableType() const
357 {
358 return blittableType;
359 }
360 void SetBlittableType( const Type &type ){
361 blittableType = type;
362 }
363
364 bool IsClass() const;
365 bool IsInterface() const;
366 bool IsComInterface() const;
367 bool IsEnum() const;
368 bool IsDelegate() const;
369 bool IsStructure() const;
370 void SetClassType( ClassType classType )
371 {
372 this->classType = classType;
373 }
374
375
376 //コンストラクタをコンパイルしているかどうかのチェックフラグ
377private:
378 mutable bool isCompilingConstructor;
379public:
380 void NotifyStartConstructorCompile() const;
381 void NotifyFinishConstructorCompile() const;
382 bool IsCompilingConstructor() const;
383
384 //デストラクタをコンパイルしているかどうかのチェックフラグ
385private:
386 mutable bool isCompilingDestructor;
387public:
388 void NotifyStartDestructorCompile() const;
389 void NotifyFinishDestructorCompile() const;
390 bool IsCompilingDestructor() const;
391
392
393 //自身の派生クラスかどうかを確認
394 bool IsSubClass( const CClass *pClass ) const;
395
396 //自身と等しいまたは派生クラスかどうかを確認
397 bool IsEqualsOrSubClass( const CClass *pClass ) const;
398
399 // 自身と等しいまたは派生クラス、基底クラスかどうかを確認
400 bool IsEqualsOrSubClassOrSuperClass( const CClass &objClass ) const;
401
402 // インターフェイス
403 bool HasInterfaces() const
404 {
405 return ( interfaces.size() != 0 );
406 }
407 const Interfaces &GetInterfaces() const
408 {
409 return interfaces;
410 }
411 bool IsInheritsInterface( const CClass *pInterfaceClass ) const;
412
413 // クラス継承
414 bool Inherits( const char *inheritNames, int nowLine );
415 bool InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine );
416 bool InheritsInterface( const CClass &inheritsClass, int nowLine );
417
418 // インターフェイス実装
419 bool Implements( const CClass &interfaceClass, const Types &actualTypeParameters, int nowLine );
420 bool Implements( const char *interfaceNames, int nowLine );
421
422 //メンバ、メソッドの追加
423 CMember *CreateMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine );
424 void AddMember( Prototype::Accessibility accessibility, bool idConst, bool isRef, char *buffer, int nowLine );
425 void AddStaticMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine );
426
427 void AddMethod(CClass *pobj_c, Prototype::Accessibility accessibility, BOOL bStatic, bool isConst, bool isAbstract,
428 bool isVirtual, bool isOverride, bool isAutoGeneration, char *buffer, int nowLine);
429
430 //重複チェック
431 bool DupliCheckAll(const char *name) const;
432 bool DupliCheckMember(const char *name) const;
433
434 const Members &GetDynamicMembers() const
435 {
436 return dynamicMembers;
437 }
438 const Members &GetStaticMembers() const
439 {
440 return staticMembers;
441 }
442 Members &GetDynamicMembers()
443 {
444 return dynamicMembers;
445 }
446 Members &GetStaticMembers()
447 {
448 return staticMembers;
449 }
450
451 const CMember *FindDynamicMember( const char *memberName ) const;
452 bool HasDynamicMember( const char *memberName ) const
453 {
454 return ( FindDynamicMember( memberName ) != NULL );
455 }
456
457 void EnumDynamicMethodsOrInterfaceMethods( const char *methodName, std::vector<const UserProc *> &subs ) const;
458 const CMethod *GetDynamicMethodOrInterfaceMethod( const UserProc *pUserProc ) const;
459
460 const Methods &GetStaticMethods() const
461 {
462 return staticMethods;
463 }
464 Methods &GetStaticMethods()
465 {
466 return staticMethods;
467 }
468
469 //デフォルト コンストラクタ
470 const CMethod *GetConstructorMethod() const
471 {
472 if( ConstructorMemberSubIndex == -1 ) return NULL;
473 return GetDynamicMethods()[ConstructorMemberSubIndex];
474 }
475 void SetConstructorMemberSubIndex( int constructorMemberSubIndex )
476 {
477 this->ConstructorMemberSubIndex = constructorMemberSubIndex;
478 }
479
480 //デストラクタ メソッドを取得
481 const CMethod *GetDestructorMethod() const
482 {
483 if( DestructorMemberSubIndex == -1 ) return NULL;
484 return GetDynamicMethods()[DestructorMemberSubIndex];
485 }
486 void SetDestructorMemberSubIndex( int destructorMemberSubIndex )
487 {
488 this->DestructorMemberSubIndex = destructorMemberSubIndex;
489 }
490
491 // デリゲート情報を取得
492 const ::Delegate &GetDelegate() const;
493
494 // ユーザ指定のアラインメント固定値
495 int GetFixedAlignment() const
496 {
497 return fixedAlignment;
498 }
499 void SetFixedAlignment( int fixedAlignment )
500 {
501 this->fixedAlignment = fixedAlignment;
502 }
503
504 // メンバの総合サイズを取得
505private:
506 int cacheSize;
507public:
508 int GetSize() const;
509
510 // メンバのオフセットを取得
511 int GetMemberOffset( const char *memberName ) const;
512private:
513 // アラインメント値を取得
514 int GetAlignment() const;
515
516
517 /////////////////////////////////////////////////////////////////
518 // vtbl
519 /////////////////////////////////////////////////////////////////
520public:
521 // vtblに存在する仮想関数の数
522 int GetVtblNum() const
523 {
524 return vtblNum;
525 }
526 void SetVtblNum( int vtblNum )
527 {
528 this->vtblNum = vtblNum;
529 }
530 void AddVtblNum( int vtblNum )
531 {
532 this->vtblNum += vtblNum;
533 }
534 bool IsExistVirtualFunctions() const
535 {
536 // 構造体以外は仮想関数を持つ
537 return !IsStructure();
538 }
539
540private:
541 long vtbl_offset;
542 long comVtblOffset;
543 long vtblMasterListOffset;
544 std::vector<long> vtblMasterList;
545public:
546 void GetVtblMasterListIndexAndVtblIndex( const UserProc *pUserProc, int &vtblMasterListIndex, int &vtblIndex ) const;
547 int GetVtblMasterListIndex( const CClass *pClass ) const;
548 long GetComVtblOffset() const;
549 long GetVtblMasterListOffset() const;
550 void GenerateVTableMasterList( const std::vector<long> &vtableMasterList, long &offset );
551 void GenerateFullVTables();
552 void ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
553 bool IsAbstract() const;
554
555
556 // TypeInfo用
557 mutable int typeInfoDataTableOffset;
558 void SetTypeInfoDataTableOffset( int typeInfoDataTableOffset ) const
559 {
560 this->typeInfoDataTableOffset = typeInfoDataTableOffset;
561 }
562 int GetTypeInfoDataTableOffset() const
563 {
564 return typeInfoDataTableOffset;
565 }
566
567 // 動的型データ用のメンバデータを取得
568 std::string GetStaticDefiningStringAsMemberNames() const;
569 std::string GetStaticDefiningStringAsMemberTypeInfoNames() const;
570 std::string GetStaticDefiningStringAsMemberOffsets() const;
571 void GetReferenceOffsetsInitializeBuffer( std::string &referenceOffsetsBuffer, int &numOfReference, int baseOffset = 0 ) const;
572
573
574 //線形リスト用
575 CClass *pobj_NextClass;
576};
577
578class Classes : public Jenga::Common::Hashmap<CClass>
579{
580 // XMLシリアライズ用
581public:
582 Classes()
583 : pCompilingMethod( NULL )
584 , pStringClass( NULL )
585 , pObjectClass( NULL )
586 , pInterfaceInfo( NULL )
587 {
588 }
589 ~Classes()
590 {
591 }
592
593 virtual CClass *Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name);
594 bool Insert( CClass *pClass, int nowLine );
595 CClass *Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine);
596 virtual void CollectClassesForNameOnly( const BasicSource &source );
597
598 // vtblを一時的に生成
599 void GenerateVTables();
600
601 // vtblのを正規のオフセットで再構築
602 void ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
603
604 virtual void InitStaticMember();
605
606private:
607 bool MemberVar_LoopRefCheck(const CClass &objClass);
608public:
609 virtual void GetClass_recur(const char *lpszInheritsClass);
610 void LookaheadClass( const char *className );
611 bool LoopRefCheck( const CClass &objClass );
612 virtual void GetAllClassInfo();
613 virtual void Compile_System_InitializeUserTypes();
614 virtual void Compile_System_InitializeUserTypesForBaseType();
615
616 const CClass *Find( const NamespaceScopes &namespaceScopes, const string &name ) const;
617 const CClass *Find( const string &fullName ) const;
618
619
620 /////////////////////////////
621 // 現在コンパイル中の情報
622 /////////////////////////////
623private:
624 const CMethod *pCompilingMethod;
625public:
626 void StartCompile( const UserProc *pUserProc );
627
628 //現在コンパイル中のメソッド情報を取得
629 const CMethod *GetNowCompilingMethodInfo(){
630 return pCompilingMethod;
631 }
632
633
634 /////////////////////////////
635 // 特殊クラス
636 /////////////////////////////
637 mutable const CClass *pStringClass;
638 mutable const CClass *pObjectClass;
639 mutable const CClass *pInterfaceInfo;
640 const CClass *GetStringClassPtr() const;
641 const CClass *GetObjectClassPtr() const;
642 const CClass *GetInterfaceInfoClassPtr() const;
643};
Note: See TracBrowser for help on using the repository browser.