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

Last change on this file since 207 was 206, checked in by dai_9181, 18 years ago

コード全体のリファクタリングを実施

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