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

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

64ビット版でもインターフェイスのベース実装周りをコンパイルできるようにした(インターフェイス機構自体はまだ未完成)

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