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

Last change on this file since 350 was 350, checked in by dai_9181, 17 years ago

インターフェイスを実装

File size: 14.2 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,
[350]388 bool isVirtual, bool isOverride, bool isAutoGeneration, char *buffer, int nowLine);
[206]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
[350]411 void EnumDynamicMethodsOrInterfaceMethods( const char *methodName, std::vector<const UserProc *> &subs ) const;
412 const CMethod *GetDynamicMethodOrInterfaceMethod( const UserProc *pUserProc ) const;
[347]413
[206]414 const Methods &GetStaticMethods() const
415 {
416 return staticMethods;
417 }
418 Methods &GetStaticMethods()
419 {
420 return staticMethods;
421 }
422
423 //デフォルト コンストラクタ
424 const CMethod *GetConstructorMethod() const
425 {
426 if( ConstructorMemberSubIndex == -1 ) return NULL;
[342]427 return GetDynamicMethods()[ConstructorMemberSubIndex];
[206]428 }
429 void SetConstructorMemberSubIndex( int constructorMemberSubIndex )
430 {
431 this->ConstructorMemberSubIndex = constructorMemberSubIndex;
432 }
433
434 //デストラクタ メソッドを取得
435 const CMethod *GetDestructorMethod() const
436 {
437 if( DestructorMemberSubIndex == -1 ) return NULL;
[342]438 return GetDynamicMethods()[DestructorMemberSubIndex];
[206]439 }
440 void SetDestructorMemberSubIndex( int destructorMemberSubIndex )
441 {
442 this->DestructorMemberSubIndex = destructorMemberSubIndex;
443 }
444
[325]445 // デリゲート情報を取得
446 const ::Delegate &GetDelegate() const;
447
[232]448 // ユーザ指定のアラインメント固定値
449 int GetFixedAlignment() const
450 {
451 return fixedAlignment;
452 }
453 void SetFixedAlignment( int fixedAlignment )
454 {
455 this->fixedAlignment = fixedAlignment;
456 }
457
[206]458 // メンバの総合サイズを取得
459 int GetSize() const;
460
461 // メンバのオフセットを取得
462 int GetMemberOffset( const char *memberName, int *pMemberNum = NULL ) const;
463private:
464 // アラインメント値を取得
465 int GetAlignment() const;
466
[342]467
468 /////////////////////////////////////////////////////////////////
469 // vtbl
470 /////////////////////////////////////////////////////////////////
[206]471public:
[342]472 // vtblに存在する仮想関数の数
473 int GetVtblNum() const
474 {
475 return vtblNum;
476 }
477 void SetVtblNum( int vtblNum )
478 {
479 this->vtblNum = vtblNum;
480 }
481 void AddVtblNum( int vtblNum )
482 {
483 this->vtblNum += vtblNum;
484 }
485 bool IsExistVirtualFunctions() const
486 {
487 return ( vtblNum > 0 );
488 }
489
490private:
491 long vtbl_offset;
492 long vtblMasterListOffset;
[345]493 std::vector<long> vtblMasterList;
[342]494public:
[348]495 void GetVtblMasterListIndexAndVtblIndex( const UserProc *pUserProc, int &vtblMasterListIndex, int &vtblIndex ) const;
[350]496 int GetVtblMasterListIndex( const CClass *pClass ) const;
[345]497 long GetVtblMasterListOffset() const;
498 void GenerateVTableMasterList( const std::vector<long> &vtableMasterList, long &offset );
[342]499 void GenerateFullVTables();
500 void ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
[206]501 bool IsAbstract() const;
502
503
504 //線形リスト用
505 CClass *pobj_NextClass;
506};
507
[270]508class Classes : public Jenga::Common::Hashmap<CClass>
[206]509{
[191]510 // XMLシリアライズ用
[206]511public:
512 Classes()
513 : pCompilingMethod( NULL )
514 , pStringClass( NULL )
515 , pObjectClass( NULL )
[350]516 , pInterfaceInfo( NULL )
[206]517 {
518 }
519 ~Classes()
520 {
521 }
522
523 virtual CClass *Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name);
524 bool Insert( CClass *pClass );
525 CClass *Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine);
526 virtual void CollectClassesForNameOnly( const BasicSource &source );
527
[342]528 // vtblを一時的に生成
[282]529 void GenerateVTables();
[206]530
[342]531 // vtblのを正規のオフセットで再構築
532 void ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
533
[206]534 virtual void InitStaticMember();
535
536private:
537 bool MemberVar_LoopRefCheck(const CClass &objClass);
538public:
539 virtual void GetClass_recur(const char *lpszInheritsClass);
540 virtual void GetAllClassInfo();
541 virtual void Compile_System_InitializeUserTypes();
542
543 const CClass *Find( const NamespaceScopes &namespaceScopes, const string &name ) const;
544 const CClass *Find( const string &fullName ) const;
545
546
547 /////////////////////////////
548 // 現在コンパイル中の情報
549 /////////////////////////////
550private:
551 const CMethod *pCompilingMethod;
552public:
553 void StartCompile( const UserProc *pUserProc );
554
555 //現在コンパイル中のメソッド情報を取得
556 const CMethod *GetNowCompilingMethodInfo(){
557 return pCompilingMethod;
558 }
559
560
561 /////////////////////////////
562 // 特殊クラス
563 /////////////////////////////
[272]564 mutable const CClass *pStringClass;
565 mutable const CClass *pObjectClass;
[349]566 mutable const CClass *pInterfaceInfo;
[272]567 const CClass *GetStringClassPtr() const;
568 const CClass *GetObjectClassPtr() const;
[349]569 const CClass *GetInterfaceInfoClassPtr() const;
[184]570};
Note: See TracBrowser for help on using the repository browser.