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

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