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

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