source: dev/trunk/abdev/BasicCompiler_Common/include/Class.h@ 355

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

静的領域に初期オブジェクトを配置可能にした

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