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

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

Interfaceクラスを独自ファイルにした。

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