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

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

インターフェイスを実装

File size: 14.2 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
79public:
80    Interface( const CClass *pInterfaceClass );
81    Interface( const Interface &objInterface )
82        : DynamicMethodsPrototype( objInterface )
83        , pInterfaceClass( objInterface.pInterfaceClass )
84        , vtblOffset( objInterface.vtblOffset )
85    {
86    }
87
88    const CClass &GetClass() const{
89        return *pInterfaceClass;
90    }
91    LONG_PTR GetVtblOffset() const
92    {
93        return vtblOffset;
94    }
95    void SetVtblOffset( LONG_PTR vtblOffset ) const
96    {
97        this->vtblOffset = vtblOffset;
98    }
99};
100typedef std::vector<Interface *> Interfaces;
101
102class CClass: public ClassPrototype, public Jenga::Common::ObjectInHashmap<CClass>
103{
104public:
105    // 型の種類
106    enum ClassType{
107        Class,
108        Interface,
109        Enum,
110        Delegate,
111        Structure,
112    };
113
114private:
115    ClassType classType;
116
117    // importされている名前空間
118    NamespaceScopesCollection importedNamespaces;
119
120    // 型パラメータ
121    GenericTypes formalGenericTypes;
122
123    // 継承クラス
124    const CClass *pSuperClass;
125
126    // 継承クラスの型パラメータ(実パラメータ)
127    Types superClassActualTypeParameters;
128
129    // Blittable型情報
130    Type blittableType;
131
132    // 実装するインターフェイス
133    Interfaces interfaces;
134
135    // 動的メンバ
136    Members dynamicMembers;
137
138    // 静的メンバ
139    Members staticMembers;
140
141    // 動的メソッド
142    int ConstructorMemberSubIndex;
143    int DestructorMemberSubIndex;
144    int vtblNum;                    // 仮想関数の数
145
146    // 静的メソッド
147    Methods staticMethods;
148
149    //アラインメント値
150    int fixedAlignment;
151
152    // XMLシリアライズ用
153    // TODO: xml未完成
154private:
155    friend class boost::serialization::access;
156    template<class Archive> void serialize(Archive& ar, const unsigned int version)
157    {
158        trace_for_serialize( "serializing - CClass" );
159
160        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( ClassPrototype );
161        ar & BOOST_SERIALIZATION_NVP( classType );
162        ar & BOOST_SERIALIZATION_NVP( importedNamespaces );
163        ar & BOOST_SERIALIZATION_NVP( formalGenericTypes );
164        ar & boost::serialization::make_nvp( "pSuperClass", const_cast<CClass *&>(pSuperClass) );
165        ar & BOOST_SERIALIZATION_NVP( superClassActualTypeParameters );
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 );
174        ar & BOOST_SERIALIZATION_NVP( fixedAlignment );
175    }
176
177    bool isReady;
178public:
179
180    CClass( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const string &name )
181        : ClassPrototype( namespaceScopes, name )
182        , importedNamespaces( importedNamespaces )
183        , classType( Class )
184        , pSuperClass( NULL )
185        , blittableType( Type() )
186        , isReady( false )
187        , fixedAlignment( 0 )
188        , ConstructorMemberSubIndex( -1 )
189        , DestructorMemberSubIndex( -1 )
190        , vtblNum( 0 )
191        , vtbl_offset( -1 )
192        , isCompilingConstructor( false )
193        , isCompilingDestructor( false )
194        , pobj_NextClass( NULL )
195    {
196    }
197    CClass()
198        : ClassPrototype()
199        , importedNamespaces()
200        , classType()
201        , pSuperClass( NULL )
202        , blittableType( Type() )
203        , isReady( false )
204        , fixedAlignment( 0 )
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        {
219            delete member;
220        }
221
222        // 静的メンバ
223        BOOST_FOREACH( CMember *member, staticMembers )
224        {
225            delete member;
226        }
227
228        // インターフェイス
229        BOOST_FOREACH( ::Interface *pInterface, interfaces )
230        {
231            delete pInterface;
232        }
233    }
234
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
248    void Readed(){
249        isReady = true;
250    }
251    bool IsReady() const{
252        return isReady;
253    }
254
255    const NamespaceScopesCollection &GetImportedNamespaces() const
256    {
257        return importedNamespaces;
258    }
259
260    // 型パラメータ
261    void AddFormalGenericType( GenericType genericType )
262    {
263        this->formalGenericTypes.push_back( genericType );
264    }
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    }
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
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    }
303    const Types &GetSuperClassActualTypeParameters() const
304    {
305        return superClassActualTypeParameters;
306    }
307    void SetSuperClassActualTypeParameters( const Types &actualTypeParameters )
308    {
309        this->superClassActualTypeParameters = actualTypeParameters;
310    }
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    //コンストラクタをコンパイルしているかどうかのチェックフラグ
337private:
338    mutable bool isCompilingConstructor;
339public:
340    void NotifyStartConstructorCompile() const;
341    void NotifyFinishConstructorCompile() const;
342    bool IsCompilingConstructor() const;
343
344    //デストラクタをコンパイルしているかどうかのチェックフラグ
345private:
346    mutable bool isCompilingDestructor;
347public:
348    void NotifyStartDestructorCompile() const;
349    void NotifyFinishDestructorCompile() const;
350    bool IsCompilingDestructor() const;
351
352
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    }
367    const Interfaces &GetInterfaces() const
368    {
369        return interfaces;
370    }
371    bool IsInheritsInterface( const CClass *pInterfaceClass ) const;
372
373    // クラス継承
374    bool Inherits( const char *inheritNames, int nowLine );
375    bool InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine );
376    bool InheritsInterface( const CClass &inheritsClass, int nowLine );
377
378    // インターフェイス実装
379    bool Implements( const CClass &interfaceClass, int nowLine );
380    bool Implements( const char *interfaceNames, int nowLine );
381
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, bool isAutoGeneration, 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    void EnumDynamicMethodsOrInterfaceMethods( const char *methodName, std::vector<const UserProc *> &subs ) const;
412    const CMethod *GetDynamicMethodOrInterfaceMethod( const UserProc *pUserProc ) const;
413
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;
427        return GetDynamicMethods()[ConstructorMemberSubIndex];
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;
438        return GetDynamicMethods()[DestructorMemberSubIndex];
439    }
440    void SetDestructorMemberSubIndex( int destructorMemberSubIndex )
441    {
442        this->DestructorMemberSubIndex = destructorMemberSubIndex;
443    }
444
445    // デリゲート情報を取得
446    const ::Delegate &GetDelegate() const;
447
448    // ユーザ指定のアラインメント固定値
449    int GetFixedAlignment() const
450    {
451        return fixedAlignment;
452    }
453    void SetFixedAlignment( int fixedAlignment )
454    {
455        this->fixedAlignment = fixedAlignment;
456    }
457
458    // メンバの総合サイズを取得
459    int GetSize() const;
460
461    // メンバのオフセットを取得
462    int GetMemberOffset( const char *memberName, int *pMemberNum = NULL ) const;
463private:
464    // アラインメント値を取得
465    int GetAlignment() const;
466
467
468    /////////////////////////////////////////////////////////////////
469    // vtbl
470    /////////////////////////////////////////////////////////////////
471public:
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;
493    std::vector<long> vtblMasterList;
494public:
495    void GetVtblMasterListIndexAndVtblIndex( const UserProc *pUserProc, int &vtblMasterListIndex, int &vtblIndex ) const;
496    int GetVtblMasterListIndex( const CClass *pClass ) const;
497    long GetVtblMasterListOffset() const;
498    void GenerateVTableMasterList( const std::vector<long> &vtableMasterList, long &offset );
499    void GenerateFullVTables();
500    void ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
501    bool IsAbstract() const;
502
503
504    //線形リスト用
505    CClass *pobj_NextClass;
506};
507
508class Classes : public Jenga::Common::Hashmap<CClass>
509{
510    // XMLシリアライズ用
511public:
512    Classes()
513        : pCompilingMethod( NULL )
514        , pStringClass( NULL )
515        , pObjectClass( NULL )
516        , pInterfaceInfo( NULL )
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
528    // vtblを一時的に生成
529    void GenerateVTables();
530
531    // vtblのを正規のオフセットで再構築
532    void ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
533
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    /////////////////////////////
564    mutable const CClass *pStringClass;
565    mutable const CClass *pObjectClass;
566    mutable const CClass *pInterfaceInfo;
567    const CClass *GetStringClassPtr() const;
568    const CClass *GetObjectClassPtr() const;
569    const CClass *GetInterfaceInfoClassPtr() const;
570};
Note: See TracBrowser for help on using the repository browser.