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

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

vtblの構造を変更。vtblMasterListをはさんでvtblを表現した。
その他メンバ名変更。
ClassPrototypeクラスを追加。

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