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

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

BasicSourceのシリアライズがうまくいっていない

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