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

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