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

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

COM修飾子に対応。COMインターフェイスを呼び出せるようにした

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