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

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