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

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

[416]のコミットによって発生した64bit版での不具合を修正。

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