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

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