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

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

UserProc/DllProc/ProcPointerクラスをSymbolクラスからの派生にした

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