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

Last change on this file since 379 was 379, checked in by dai_9181, 16 years ago

ジェネリクスインターフェイスをサポートした

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