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

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

幾つかの構文解析系の処理をLexicalAnalyzerに実装し直した

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