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

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