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

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