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

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

インターフェイス実装周りの仕様整備

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