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

Last change on this file since 389 was 387, checked in by dai_9181, 17 years ago

メンバ情報を保持するようにした

File size: 16.1 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 {
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 {
242 }
243 ~CClass()
244 {
245 // 動的メンバ
246 BOOST_FOREACH( CMember *member, dynamicMembers )
247 {
248 delete member;
249 }
250
251 // 静的メンバ
252 BOOST_FOREACH( CMember *member, staticMembers )
253 {
254 delete member;
255 }
256
257 // インターフェイス
258 BOOST_FOREACH( ::Interface *pInterface, interfaces )
259 {
260 delete pInterface;
261 }
262 }
263
264 virtual const std::string &GetKeyName() const
265 {
266 return GetName();
267 }
268 virtual bool IsDuplication( const CClass *pClass ) const
269 {
270 if( pClass->IsEqualSymbol( *this ) )
271 {
272 return true;
273 }
274 return false;
275 }
276
277 void Readed(){
278 isReady = true;
279 }
280 bool IsReady() const{
281 return isReady;
282 }
283
284 const NamespaceScopesCollection &GetImportedNamespaces() const
285 {
286 return importedNamespaces;
287 }
288
289 // 型パラメータ
290 void AddFormalGenericType( GenericType genericType )
291 {
292 this->formalGenericTypes.push_back( genericType );
293 }
294 int GetFormalGenericTypeParameterIndex( const std::string &name ) const
295 {
296 int i = 0;
297 BOOST_FOREACH( const GenericType &genericType, formalGenericTypes )
298 {
299 if( genericType.GetName() == name )
300 {
301 return i;
302 }
303 i++;
304 }
305 return -1;
306 }
307 bool IsExistFormalGenericTypeParameter( const std::string &name ) const
308 {
309 BOOST_FOREACH( const GenericType &genericType, formalGenericTypes )
310 {
311 if( genericType.GetName() == name )
312 {
313 return true;
314 }
315 }
316 return false;
317 }
318 bool IsGeneric() const
319 {
320 return ( this->formalGenericTypes.size() != 0 );
321 }
322
323 // 継承元クラス
324 bool HasSuperClass() const
325 {
326 return ( pSuperClass != NULL );
327 }
328 const CClass &GetSuperClass() const
329 {
330 return *pSuperClass;
331 }
332 void SetSuperClass( const CClass *pSuperClass )
333 {
334 this->pSuperClass = pSuperClass;
335 }
336 const Types &GetSuperClassActualTypeParameters() const
337 {
338 return superClassActualTypeParameters;
339 }
340 void SetSuperClassActualTypeParameters( const Types &actualTypeParameters )
341 {
342 this->superClassActualTypeParameters = actualTypeParameters;
343 }
344
345 // Blittable型
346 bool IsBlittableType() const
347 {
348 return !blittableType.IsNull();
349 }
350 const Type &GetBlittableType() const
351 {
352 return blittableType;
353 }
354 void SetBlittableType( const Type &type ){
355 blittableType = type;
356 }
357
358 bool IsClass() const;
359 bool IsInterface() const;
360 bool IsComInterface() const;
361 bool IsEnum() const;
362 bool IsDelegate() const;
363 bool IsStructure() const;
364 void SetClassType( ClassType classType )
365 {
366 this->classType = classType;
367 }
368
369
370 //コンストラクタをコンパイルしているかどうかのチェックフラグ
371private:
372 mutable bool isCompilingConstructor;
373public:
374 void NotifyStartConstructorCompile() const;
375 void NotifyFinishConstructorCompile() const;
376 bool IsCompilingConstructor() const;
377
378 //デストラクタをコンパイルしているかどうかのチェックフラグ
379private:
380 mutable bool isCompilingDestructor;
381public:
382 void NotifyStartDestructorCompile() const;
383 void NotifyFinishDestructorCompile() const;
384 bool IsCompilingDestructor() const;
385
386
387 //自身の派生クラスかどうかを確認
388 bool IsSubClass( const CClass *pClass ) const;
389
390 //自身と等しいまたは派生クラスかどうかを確認
391 bool IsEqualsOrSubClass( const CClass *pClass ) const;
392
393 // 自身と等しいまたは派生クラス、基底クラスかどうかを確認
394 bool IsEqualsOrSubClassOrSuperClass( const CClass &objClass ) const;
395
396 // インターフェイス
397 bool HasInterfaces() const
398 {
399 return ( interfaces.size() != 0 );
400 }
401 const Interfaces &GetInterfaces() const
402 {
403 return interfaces;
404 }
405 bool IsInheritsInterface( const CClass *pInterfaceClass ) const;
406
407 // クラス継承
408 bool Inherits( const char *inheritNames, int nowLine );
409 bool InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine );
410 bool InheritsInterface( const CClass &inheritsClass, int nowLine );
411
412 // インターフェイス実装
413 bool Implements( const CClass &interfaceClass, const Types &actualTypeParameters, int nowLine );
414 bool Implements( const char *interfaceNames, int nowLine );
415
416 //メンバ、メソッドの追加
417 CMember *CreateMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine );
418 void AddMember( Prototype::Accessibility accessibility, bool idConst, bool isRef, char *buffer, int nowLine );
419 void AddStaticMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine );
420
421 void AddMethod(CClass *pobj_c, Prototype::Accessibility accessibility, BOOL bStatic, bool isConst, bool isAbstract,
422 bool isVirtual, bool isOverride, bool isAutoGeneration, char *buffer, int nowLine);
423
424 //重複チェック
425 bool DupliCheckAll(const char *name);
426 bool DupliCheckMember(const char *name);
427
428 const Members &GetDynamicMembers() const
429 {
430 return dynamicMembers;
431 }
432 const Members &GetStaticMembers() const
433 {
434 return staticMembers;
435 }
436 Members &GetDynamicMembers()
437 {
438 return dynamicMembers;
439 }
440 Members &GetStaticMembers()
441 {
442 return staticMembers;
443 }
444
445 const CMember *FindDynamicMember( const char *memberName ) const
446 {
447 BOOST_FOREACH( CMember *pMember, GetDynamicMembers() )
448 {
449 if( pMember->GetName() == memberName )
450 {
451 return pMember;
452 }
453 }
454 return 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 // メンバの総合サイズを取得
505 int GetSize() const;
506
507 // メンバのオフセットを取得
508 int GetMemberOffset( const char *memberName, int *pMemberNum = NULL ) const;
509private:
510 // アラインメント値を取得
511 int GetAlignment() const;
512
513
514 /////////////////////////////////////////////////////////////////
515 // vtbl
516 /////////////////////////////////////////////////////////////////
517public:
518 // vtblに存在する仮想関数の数
519 int GetVtblNum() const
520 {
521 return vtblNum;
522 }
523 void SetVtblNum( int vtblNum )
524 {
525 this->vtblNum = vtblNum;
526 }
527 void AddVtblNum( int vtblNum )
528 {
529 this->vtblNum += vtblNum;
530 }
531 bool IsExistVirtualFunctions() const
532 {
533 return ( vtblNum > 0 );
534 }
535
536private:
537 long vtbl_offset;
538 long comVtblOffset;
539 long vtblMasterListOffset;
540 std::vector<long> vtblMasterList;
541public:
542 void GetVtblMasterListIndexAndVtblIndex( const UserProc *pUserProc, int &vtblMasterListIndex, int &vtblIndex ) const;
543 int GetVtblMasterListIndex( const CClass *pClass ) const;
544 long GetComVtblOffset() const;
545 long GetVtblMasterListOffset() const;
546 void GenerateVTableMasterList( const std::vector<long> &vtableMasterList, long &offset );
547 void GenerateFullVTables();
548 void ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
549 bool IsAbstract() const;
550
551
552 // TypeInfo用
553 mutable int typeInfoDataTableOffset;
554 void SetTypeInfoDataTableOffset( int typeInfoDataTableOffset ) const
555 {
556 this->typeInfoDataTableOffset = typeInfoDataTableOffset;
557 }
558 int GetTypeInfoDataTableOffset() const
559 {
560 return typeInfoDataTableOffset;
561 }
562
563 // 動的型データ用のメンバデータを取得
564 std::string GetStaticDefiningStringAsMemberNames() const;
565 std::string GetStaticDefiningStringAsMemberTypeInfoNames() const;
566
567
568 //線形リスト用
569 CClass *pobj_NextClass;
570};
571
572class Classes : public Jenga::Common::Hashmap<CClass>
573{
574 // XMLシリアライズ用
575public:
576 Classes()
577 : pCompilingMethod( NULL )
578 , pStringClass( NULL )
579 , pObjectClass( NULL )
580 , pInterfaceInfo( NULL )
581 {
582 }
583 ~Classes()
584 {
585 }
586
587 virtual CClass *Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name);
588 bool Insert( CClass *pClass, int nowLine );
589 CClass *Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine);
590 virtual void CollectClassesForNameOnly( const BasicSource &source );
591
592 // vtblを一時的に生成
593 void GenerateVTables();
594
595 // vtblのを正規のオフセットで再構築
596 void ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
597
598 virtual void InitStaticMember();
599
600private:
601 bool MemberVar_LoopRefCheck(const CClass &objClass);
602public:
603 virtual void GetClass_recur(const char *lpszInheritsClass);
604 void LookaheadClass( const char *className );
605 bool LoopRefCheck( const CClass &objClass );
606 virtual void GetAllClassInfo();
607 virtual void Compile_System_InitializeUserTypes();
608 virtual void Compile_System_InitializeUserTypesForBaseType();
609
610 const CClass *Find( const NamespaceScopes &namespaceScopes, const string &name ) const;
611 const CClass *Find( const string &fullName ) const;
612
613
614 /////////////////////////////
615 // 現在コンパイル中の情報
616 /////////////////////////////
617private:
618 const CMethod *pCompilingMethod;
619public:
620 void StartCompile( const UserProc *pUserProc );
621
622 //現在コンパイル中のメソッド情報を取得
623 const CMethod *GetNowCompilingMethodInfo(){
624 return pCompilingMethod;
625 }
626
627
628 /////////////////////////////
629 // 特殊クラス
630 /////////////////////////////
631 mutable const CClass *pStringClass;
632 mutable const CClass *pObjectClass;
633 mutable const CClass *pInterfaceInfo;
634 const CClass *GetStringClassPtr() const;
635 const CClass *GetObjectClassPtr() const;
636 const CClass *GetInterfaceInfoClassPtr() const;
637};
Note: See TracBrowser for help on using the repository browser.