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

Last change on this file since 346 was 346, checked in by dai_9181, 17 years ago
File size: 13.9 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;
[345]77 mutable LONG_PTR 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};
[346]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 // 動的メンバ
[346]217 BOOST_FOREACH( CMember *member, dynamicMembers )
218 {
[206]219 delete member;
220 }
[191]221
[206]222 // 静的メンバ
[346]223 BOOST_FOREACH( CMember *member, staticMembers )
224 {
[206]225 delete member;
226 }
[346]227
228 // インターフェイス
229 BOOST_FOREACH( ::Interface *pInterface, interfaces )
230 {
231 delete pInterface;
232 }
[206]233 }
[184]234
[270]235 virtual const std::string &GetKeyName() const
236 {
237 return GetName();
238 }
239 virtual bool IsDuplication( const CClass *pClass ) const
240 {
241 if( pClass->IsEqualSymbol( *this ) )
242 {
243 return true;
244 }
245 return false;
246 }
247
[206]248 void Readed(){
249 isReady = true;
250 }
251 bool IsReady() const{
252 return isReady;
253 }
[184]254
[206]255 const NamespaceScopesCollection &GetImportedNamespaces() const
256 {
257 return importedNamespaces;
258 }
259
[290]260 // 型パラメータ
261 void AddFormalGenericType( GenericType genericType )
262 {
263 this->formalGenericTypes.push_back( genericType );
264 }
[299]265 int GetFormalGenericTypeParameterIndex( const std::string &name ) const
266 {
267 int i = 0;
268 BOOST_FOREACH( const GenericType &genericType, formalGenericTypes )
269 {
270 if( genericType.GetName() == name )
271 {
272 return i;
273 }
274 i++;
275 }
276 return -1;
277 }
[290]278 bool IsExistFormalGenericTypeParameter( const std::string &name ) const
279 {
280 BOOST_FOREACH( const GenericType &genericType, formalGenericTypes )
281 {
282 if( genericType.GetName() == name )
283 {
284 return true;
285 }
286 }
287 return false;
288 }
289
[206]290 // 継承元クラス
291 bool HasSuperClass() const
292 {
293 return ( pSuperClass != NULL );
294 }
295 const CClass &GetSuperClass() const
296 {
297 return *pSuperClass;
298 }
299 void SetSuperClass( const CClass *pSuperClass )
300 {
301 this->pSuperClass = pSuperClass;
302 }
[299]303 const Types &GetSuperClassActualTypeParameters() const
304 {
305 return superClassActualTypeParameters;
306 }
307 void SetSuperClassActualTypeParameters( const Types &actualTypeParameters )
308 {
309 this->superClassActualTypeParameters = actualTypeParameters;
310 }
[206]311
312 // Blittable型
313 bool IsBlittableType() const
314 {
315 return !blittableType.IsNull();
316 }
317 const Type &GetBlittableType() const
318 {
319 return blittableType;
320 }
321 void SetBlittableType( const Type &type ){
322 blittableType = type;
323 }
324
325 bool IsClass() const;
326 bool IsInterface() const;
327 bool IsEnum() const;
328 bool IsDelegate() const;
329 bool IsStructure() const;
330 void SetClassType( ClassType classType )
331 {
332 this->classType = classType;
333 }
334
335
336 //コンストラクタをコンパイルしているかどうかのチェックフラグ
[184]337private:
[206]338 mutable bool isCompilingConstructor;
[184]339public:
[206]340 void NotifyStartConstructorCompile() const;
341 void NotifyFinishConstructorCompile() const;
342 bool IsCompilingConstructor() const;
[184]343
[206]344 //デストラクタをコンパイルしているかどうかのチェックフラグ
345private:
346 mutable bool isCompilingDestructor;
347public:
348 void NotifyStartDestructorCompile() const;
349 void NotifyFinishDestructorCompile() const;
350 bool IsCompilingDestructor() const;
[191]351
[193]352
[206]353 //自身の派生クラスかどうかを確認
354 bool IsSubClass( const CClass *pClass ) const;
355
356 //自身と等しいまたは派生クラスかどうかを確認
357 bool IsEqualsOrSubClass( const CClass *pClass ) const;
358
359 // 自身と等しいまたは派生クラス、基底クラスかどうかを確認
360 bool IsEqualsOrSubClassOrSuperClass( const CClass &objClass ) const;
361
362 // インターフェイス
363 bool HasInterfaces() const
364 {
365 return ( interfaces.size() != 0 );
366 }
[342]367 const Interfaces &GetInterfaces() const
368 {
369 return interfaces;
370 }
[206]371 bool IsInheritsInterface( const CClass *pInterfaceClass ) const;
372
[340]373 // クラス継承
[206]374 bool Inherits( const char *inheritNames, int nowLine );
[299]375 bool InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine );
[206]376 bool InheritsInterface( const CClass &inheritsClass, int nowLine );
377
[340]378 // インターフェイス実装
[342]379 bool Implements( const CClass &interfaceClass, int nowLine );
[340]380 bool Implements( const char *interfaceNames, int nowLine );
381
[206]382 //メンバ、メソッドの追加
383 CMember *CreateMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine );
384 void AddMember( Prototype::Accessibility accessibility, bool idConst, bool isRef, char *buffer, int nowLine );
385 void AddStaticMember( Prototype::Accessibility accessibility, bool isConst, bool isRef, char *buffer, int nowLine );
386
387 void AddMethod(CClass *pobj_c, Prototype::Accessibility accessibility, BOOL bStatic, bool isConst, bool isAbstract,
388 bool isVirtual, bool isOverride, char *buffer, int nowLine);
389
390 //重複チェック
391 bool DupliCheckAll(const char *name);
392 bool DupliCheckMember(const char *name);
393
394 const Members &GetDynamicMembers() const
395 {
396 return dynamicMembers;
397 }
398 const Members &GetStaticMembers() const
399 {
400 return staticMembers;
401 }
402 Members &GetDynamicMembers()
403 {
404 return dynamicMembers;
405 }
406 Members &GetStaticMembers()
407 {
408 return staticMembers;
409 }
410
411 const Methods &GetStaticMethods() const
412 {
413 return staticMethods;
414 }
415 Methods &GetStaticMethods()
416 {
417 return staticMethods;
418 }
419
420 //デフォルト コンストラクタ
421 const CMethod *GetConstructorMethod() const
422 {
423 if( ConstructorMemberSubIndex == -1 ) return NULL;
[342]424 return GetDynamicMethods()[ConstructorMemberSubIndex];
[206]425 }
426 void SetConstructorMemberSubIndex( int constructorMemberSubIndex )
427 {
428 this->ConstructorMemberSubIndex = constructorMemberSubIndex;
429 }
430
431 //デストラクタ メソッドを取得
432 const CMethod *GetDestructorMethod() const
433 {
434 if( DestructorMemberSubIndex == -1 ) return NULL;
[342]435 return GetDynamicMethods()[DestructorMemberSubIndex];
[206]436 }
437 void SetDestructorMemberSubIndex( int destructorMemberSubIndex )
438 {
439 this->DestructorMemberSubIndex = destructorMemberSubIndex;
440 }
441
[325]442 // デリゲート情報を取得
443 const ::Delegate &GetDelegate() const;
444
[232]445 // ユーザ指定のアラインメント固定値
446 int GetFixedAlignment() const
447 {
448 return fixedAlignment;
449 }
450 void SetFixedAlignment( int fixedAlignment )
451 {
452 this->fixedAlignment = fixedAlignment;
453 }
454
[206]455 // メンバの総合サイズを取得
456 int GetSize() const;
457
458 // メンバのオフセットを取得
459 int GetMemberOffset( const char *memberName, int *pMemberNum = NULL ) const;
460private:
461 // アラインメント値を取得
462 int GetAlignment() const;
463
[342]464
465 /////////////////////////////////////////////////////////////////
466 // vtbl
467 /////////////////////////////////////////////////////////////////
[206]468public:
[342]469 // vtblに存在する仮想関数の数
470 int GetVtblNum() const
471 {
472 return vtblNum;
473 }
474 void SetVtblNum( int vtblNum )
475 {
476 this->vtblNum = vtblNum;
477 }
478 void AddVtblNum( int vtblNum )
479 {
480 this->vtblNum += vtblNum;
481 }
482 bool IsExistVirtualFunctions() const
483 {
484 return ( vtblNum > 0 );
485 }
486
487private:
488 long vtbl_offset;
489 long vtblMasterListOffset;
[345]490 std::vector<long> vtblMasterList;
[342]491public:
492 int GetVtblMasterListIndex( const UserProc *pUserProc ) const;
[206]493 int GetFuncNumInVtbl( const UserProc *pUserProc ) const;
[345]494 long GetVtblMasterListOffset() const;
495 void GenerateVTablePart( long &vtableDataTableOffset ) const;
496 void GenerateVTableMasterList( const std::vector<long> &vtableMasterList, long &offset );
[342]497 void GenerateFullVTables();
498 void ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
[206]499 bool IsAbstract() const;
500
501
502 //線形リスト用
503 CClass *pobj_NextClass;
504};
505
[270]506class Classes : public Jenga::Common::Hashmap<CClass>
[206]507{
[191]508 // XMLシリアライズ用
[206]509public:
510 Classes()
511 : pCompilingMethod( NULL )
512 , pStringClass( NULL )
513 , pObjectClass( NULL )
514 {
515 }
516 ~Classes()
517 {
518 }
519
520 virtual CClass *Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name);
521 bool Insert( CClass *pClass );
522 CClass *Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine);
523 virtual void CollectClassesForNameOnly( const BasicSource &source );
524
[342]525 // vtblを一時的に生成
[282]526 void GenerateVTables();
[206]527
[342]528 // vtblのを正規のオフセットで再構築
529 void ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
530
[206]531 virtual void InitStaticMember();
532
533private:
534 bool MemberVar_LoopRefCheck(const CClass &objClass);
535public:
536 virtual void GetClass_recur(const char *lpszInheritsClass);
537 virtual void GetAllClassInfo();
538 virtual void Compile_System_InitializeUserTypes();
539
540 const CClass *Find( const NamespaceScopes &namespaceScopes, const string &name ) const;
541 const CClass *Find( const string &fullName ) const;
542
543
544 /////////////////////////////
545 // 現在コンパイル中の情報
546 /////////////////////////////
547private:
548 const CMethod *pCompilingMethod;
549public:
550 void StartCompile( const UserProc *pUserProc );
551
552 //現在コンパイル中のメソッド情報を取得
553 const CMethod *GetNowCompilingMethodInfo(){
554 return pCompilingMethod;
555 }
556
557
558 /////////////////////////////
559 // 特殊クラス
560 /////////////////////////////
[272]561 mutable const CClass *pStringClass;
562 mutable const CClass *pObjectClass;
563 const CClass *GetStringClassPtr() const;
564 const CClass *GetObjectClassPtr() const;
[184]565};
Note: See TracBrowser for help on using the repository browser.