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

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

Implements修飾子を作り始めた

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