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

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

CClass::GetStaticDefiningStringAsMemberOffsetsメソッドを追加。
エラーコード142を追加。

File size: 16.2 KB
Line 
1#pragma once
2
3#include <option.h>
4#include <Program.h>
5#include <Prototype.h>
6#include <Type.h>
7#include <Method.h>
8#include <Member.h>
9#include <Source.h>
10
11class UserProc;
12class CClass;
13class Delegate;
14
15class DynamicMethodsPrototype
16{
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
28public:
29 DynamicMethodsPrototype(){}
30 DynamicMethodsPrototype( const DynamicMethodsPrototype &dynamicMethodsPrototype )
31 : dynamicMethods( dynamicMethodsPrototype.dynamicMethods )
32 {
33 }
34 ~DynamicMethodsPrototype(){}
35
36 const Methods &GetDynamicMethods() const
37 {
38 return dynamicMethods;
39 }
40 Methods &GetDynamicMethods()
41 {
42 return dynamicMethods;
43 }
44
45 void AddDynamicMethods( CMethod *pMethod )
46 {
47 dynamicMethods.push_back( pMethod );
48 }
49};
50
51class ClassPrototype : public Prototype, public DynamicMethodsPrototype
52{
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{
77 const CClass *pInterfaceClass;
78 mutable LONG_PTR vtblOffset;
79
80 // 型パラメータ(実パラメータ)
81 Types actualTypeParameters;
82
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 );
93 ar & BOOST_SERIALIZATION_NVP( actualTypeParameters );
94 }
95
96public:
97 Interface( const CClass *pInterfaceClass, const Types &actualTypeParameters );
98 Interface( const Interface &objInterface )
99 : DynamicMethodsPrototype( objInterface )
100 , pInterfaceClass( objInterface.pInterfaceClass )
101 , vtblOffset( objInterface.vtblOffset )
102 {
103 }
104 Interface()
105 {
106 }
107
108 const CClass &GetClass() const{
109 return *pInterfaceClass;
110 }
111 LONG_PTR GetVtblOffset() const
112 {
113 return vtblOffset;
114 }
115 void SetVtblOffset( LONG_PTR vtblOffset ) const
116 {
117 this->vtblOffset = vtblOffset;
118 }
119
120 const Types &GetActualTypeParameters() const
121 {
122 return actualTypeParameters;
123 }
124
125 std::string GetFullNameWithActualGenericTypeParameters() const;
126};
127typedef std::vector<Interface *> Interfaces;
128
129class CClass: public ClassPrototype, public Jenga::Common::ObjectInHashmap<CClass>
130{
131public:
132 // 型の種類
133 enum ClassType{
134 Class,
135 Interface,
136 ComInterface,
137 Enum,
138 Delegate,
139 Structure,
140 };
141
142private:
143 ClassType classType;
144
145 // importされている名前空間
146 NamespaceScopesCollection importedNamespaces;
147
148 // 型パラメータ
149 GenericTypes formalGenericTypes;
150
151 // 基底クラス
152 const CClass *pSuperClass;
153
154 // 基底クラスの型パラメータ(実パラメータ)
155 Types superClassActualTypeParameters;
156
157 // Blittable型情報
158 Type blittableType;
159
160 // 実装するインターフェイス
161 Interfaces interfaces;
162
163 // 動的メンバ
164 Members dynamicMembers;
165
166 // 静的メンバ
167 Members staticMembers;
168
169 // 動的メソッド
170 int ConstructorMemberSubIndex;
171 int DestructorMemberSubIndex;
172 int vtblNum; // 仮想関数の数
173
174 // 静的メソッド
175 Methods staticMethods;
176
177 //アラインメント値
178 int fixedAlignment;
179
180 // XMLシリアライズ用
181private:
182 friend class boost::serialization::access;
183 template<class Archive> void serialize(Archive& ar, const unsigned int version)
184 {
185 trace_for_serialize( "serializing - CClass" );
186
187 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( ClassPrototype );
188 ar & BOOST_SERIALIZATION_NVP( classType );
189 ar & BOOST_SERIALIZATION_NVP( importedNamespaces );
190 ar & BOOST_SERIALIZATION_NVP( formalGenericTypes );
191 ar & boost::serialization::make_nvp( "pSuperClass", const_cast<CClass *&>(pSuperClass) );
192 ar & BOOST_SERIALIZATION_NVP( superClassActualTypeParameters );
193 ar & BOOST_SERIALIZATION_NVP( blittableType );
194 ar & BOOST_SERIALIZATION_NVP( interfaces );
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 );
201 ar & BOOST_SERIALIZATION_NVP( fixedAlignment );
202 }
203
204 bool isReady;
205public:
206
207 CClass( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const string &name )
208 : ClassPrototype( namespaceScopes, name )
209 , importedNamespaces( importedNamespaces )
210 , classType( Class )
211 , pSuperClass( NULL )
212 , blittableType( Type() )
213 , isReady( false )
214 , fixedAlignment( 0 )
215 , ConstructorMemberSubIndex( -1 )
216 , DestructorMemberSubIndex( -1 )
217 , vtblNum( 0 )
218 , vtbl_offset( -1 )
219 , comVtblOffset( 0 )
220 , isCompilingConstructor( false )
221 , isCompilingDestructor( false )
222 , pobj_NextClass( NULL )
223 , cacheSize( 0 )
224 {
225 }
226 CClass()
227 : ClassPrototype()
228 , importedNamespaces()
229 , classType()
230 , pSuperClass( NULL )
231 , blittableType( Type() )
232 , isReady( false )
233 , fixedAlignment( 0 )
234 , ConstructorMemberSubIndex( -1 )
235 , DestructorMemberSubIndex( -1 )
236 , vtblNum( 0 )
237 , vtbl_offset( -1 )
238 , comVtblOffset( 0 )
239 , isCompilingConstructor( false )
240 , isCompilingDestructor( false )
241 , pobj_NextClass( NULL )
242 , cacheSize( 0 )
243 {
244 }
245 ~CClass()
246 {
247 // 動的メンバ
248 BOOST_FOREACH( CMember *member, dynamicMembers )
249 {
250 delete member;
251 }
252
253 // 静的メンバ
254 BOOST_FOREACH( CMember *member, staticMembers )
255 {
256 delete member;
257 }
258
259 // インターフェイス
260 BOOST_FOREACH( ::Interface *pInterface, interfaces )
261 {
262 delete pInterface;
263 }
264 }
265
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
279 void Readed(){
280 isReady = true;
281 }
282 bool IsReady() const{
283 return isReady;
284 }
285
286 const NamespaceScopesCollection &GetImportedNamespaces() const
287 {
288 return importedNamespaces;
289 }
290
291 // 型パラメータ
292 void AddFormalGenericType( GenericType genericType )
293 {
294 this->formalGenericTypes.push_back( genericType );
295 }
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 }
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 }
320 bool IsGeneric() const
321 {
322 return ( this->formalGenericTypes.size() != 0 );
323 }
324
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 }
338 const Types &GetSuperClassActualTypeParameters() const
339 {
340 return superClassActualTypeParameters;
341 }
342 void SetSuperClassActualTypeParameters( const Types &actualTypeParameters )
343 {
344 this->superClassActualTypeParameters = actualTypeParameters;
345 }
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;
362 bool IsComInterface() const;
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 //コンストラクタをコンパイルしているかどうかのチェックフラグ
373private:
374 mutable bool isCompilingConstructor;
375public:
376 void NotifyStartConstructorCompile() const;
377 void NotifyFinishConstructorCompile() const;
378 bool IsCompilingConstructor() const;
379
380 //デストラクタをコンパイルしているかどうかのチェックフラグ
381private:
382 mutable bool isCompilingDestructor;
383public:
384 void NotifyStartDestructorCompile() const;
385 void NotifyFinishDestructorCompile() const;
386 bool IsCompilingDestructor() const;
387
388
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 }
403 const Interfaces &GetInterfaces() const
404 {
405 return interfaces;
406 }
407 bool IsInheritsInterface( const CClass *pInterfaceClass ) const;
408
409 // クラス継承
410 bool Inherits( const char *inheritNames, int nowLine );
411 bool InheritsClass( const CClass &inheritsClass, const Types &actualTypeParameters, int nowLine );
412 bool InheritsInterface( const CClass &inheritsClass, int nowLine );
413
414 // インターフェイス実装
415 bool Implements( const CClass &interfaceClass, const Types &actualTypeParameters, int nowLine );
416 bool Implements( const char *interfaceNames, int nowLine );
417
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,
424 bool isVirtual, bool isOverride, bool isAutoGeneration, char *buffer, int nowLine);
425
426 //重複チェック
427 bool DupliCheckAll(const char *name) const;
428 bool DupliCheckMember(const char *name) const;
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 }
446
447 const CMember *FindDynamicMember( const char *memberName ) const;
448 bool HasDynamicMember( const char *memberName ) const
449 {
450 return ( FindDynamicMember( memberName ) != NULL );
451 }
452
453 void EnumDynamicMethodsOrInterfaceMethods( const char *methodName, std::vector<const UserProc *> &subs ) const;
454 const CMethod *GetDynamicMethodOrInterfaceMethod( const UserProc *pUserProc ) const;
455
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;
469 return GetDynamicMethods()[ConstructorMemberSubIndex];
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;
480 return GetDynamicMethods()[DestructorMemberSubIndex];
481 }
482 void SetDestructorMemberSubIndex( int destructorMemberSubIndex )
483 {
484 this->DestructorMemberSubIndex = destructorMemberSubIndex;
485 }
486
487 // デリゲート情報を取得
488 const ::Delegate &GetDelegate() const;
489
490 // ユーザ指定のアラインメント固定値
491 int GetFixedAlignment() const
492 {
493 return fixedAlignment;
494 }
495 void SetFixedAlignment( int fixedAlignment )
496 {
497 this->fixedAlignment = fixedAlignment;
498 }
499
500 // メンバの総合サイズを取得
501private:
502 int cacheSize;
503public:
504 int GetSize() const;
505
506 // メンバのオフセットを取得
507 int GetMemberOffset( const char *memberName ) const;
508private:
509 // アラインメント値を取得
510 int GetAlignment() const;
511
512
513 /////////////////////////////////////////////////////////////////
514 // vtbl
515 /////////////////////////////////////////////////////////////////
516public:
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 // 構造体以外は仮想関数を持つ
533 return !IsStructure();
534 }
535
536private:
537 long vtbl_offset;
538 long comVtblOffset;
539 long vtblMasterListOffset;
540 std::vector<long> vtblMasterList;
541public:
542 void GetVtblMasterListIndexAndVtblIndex( const UserProc *pUserProc, int &vtblMasterListIndex, int &vtblIndex ) const;
543 int GetVtblMasterListIndex( const CClass *pClass ) const;
544 long GetComVtblOffset() const;
545 long GetVtblMasterListOffset() const;
546 void GenerateVTableMasterList( const std::vector<long> &vtableMasterList, long &offset );
547 void GenerateFullVTables();
548 void ActionVtblSchedule( LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
549 bool IsAbstract() const;
550
551
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
563 // 動的型データ用のメンバデータを取得
564 std::string GetStaticDefiningStringAsMemberNames() const;
565 std::string GetStaticDefiningStringAsMemberTypeInfoNames() const;
566 std::string GetStaticDefiningStringAsMemberOffsets() const;
567
568
569 //線形リスト用
570 CClass *pobj_NextClass;
571};
572
573class Classes : public Jenga::Common::Hashmap<CClass>
574{
575 // XMLシリアライズ用
576public:
577 Classes()
578 : pCompilingMethod( NULL )
579 , pStringClass( NULL )
580 , pObjectClass( NULL )
581 , pInterfaceInfo( NULL )
582 {
583 }
584 ~Classes()
585 {
586 }
587
588 virtual CClass *Create( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name);
589 bool Insert( CClass *pClass, int nowLine );
590 CClass *Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const char *name,int nowLine);
591 virtual void CollectClassesForNameOnly( const BasicSource &source );
592
593 // vtblを一時的に生成
594 void GenerateVTables();
595
596 // vtblのを正規のオフセットで再構築
597 void ActionVtblSchedule(LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection );
598
599 virtual void InitStaticMember();
600
601private:
602 bool MemberVar_LoopRefCheck(const CClass &objClass);
603public:
604 virtual void GetClass_recur(const char *lpszInheritsClass);
605 void LookaheadClass( const char *className );
606 bool LoopRefCheck( const CClass &objClass );
607 virtual void GetAllClassInfo();
608 virtual void Compile_System_InitializeUserTypes();
609 virtual void Compile_System_InitializeUserTypesForBaseType();
610
611 const CClass *Find( const NamespaceScopes &namespaceScopes, const string &name ) const;
612 const CClass *Find( const string &fullName ) const;
613
614
615 /////////////////////////////
616 // 現在コンパイル中の情報
617 /////////////////////////////
618private:
619 const CMethod *pCompilingMethod;
620public:
621 void StartCompile( const UserProc *pUserProc );
622
623 //現在コンパイル中のメソッド情報を取得
624 const CMethod *GetNowCompilingMethodInfo(){
625 return pCompilingMethod;
626 }
627
628
629 /////////////////////////////
630 // 特殊クラス
631 /////////////////////////////
632 mutable const CClass *pStringClass;
633 mutable const CClass *pObjectClass;
634 mutable const CClass *pInterfaceInfo;
635 const CClass *GetStringClassPtr() const;
636 const CClass *GetObjectClassPtr() const;
637 const CClass *GetInterfaceInfoClassPtr() const;
638};
Note: See TracBrowser for help on using the repository browser.