source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/include/Type.h@ 519

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

jenga.hを追加。

File size: 8.9 KB
Line 
1#pragma once
2
3#include <string>
4#include <vector>
5
6#include <boost/foreach.hpp>
7
8#include <BoostSerializationSupport.h>
9
10#include <option.h>
11#include <Program.h>
12
13#include <windows.h>
14
15class CClass;
16
17class GenericType;
18typedef std::vector<GenericType> GenericTypes;
19
20class Type{
21 int basicType;
22 union{
23 LONG_PTR index;
24 const CClass *pClass;
25 };
26
27 // ジェネリクス クラス インスタンス型の場合に使う
28 GenericTypes actualGenericTypes;
29
30 // 型パラメータで使う
31 std::string formalTypeName; // 型パラメータの名前
32 int formalTypeIndex; // 型パラメータの引数番号
33
34 // XMLシリアライズ用
35private:
36 friend class boost::serialization::access;
37 template<class Archive> void serialize(Archive& ar, const unsigned int version)
38 {
39 trace_for_serialize( "serializing - Type" );
40
41 ar & BOOST_SERIALIZATION_NVP( basicType );
42 if( HasMember() )
43 {
44 ar & boost::serialization::make_nvp("pClass", const_cast<CClass *&>(pClass));
45 ar & BOOST_SERIALIZATION_NVP( actualGenericTypes );
46 }
47 else
48 {
49 ar & BOOST_SERIALIZATION_NVP( index );
50 }
51
52 if( IsTypeParameter() )
53 {
54 ar & BOOST_SERIALIZATION_NVP( formalTypeName );
55 ar & BOOST_SERIALIZATION_NVP( formalTypeIndex );
56 }
57 }
58
59public:
60 static int GetBasicSize( int basicType );
61
62 Type():
63 basicType( DEF_NON ),
64 index( -1 ){}
65 Type( int basicType ):
66 basicType( basicType ),
67 index( -1 ){}
68
69 Type( int basicType, LONG_PTR index )
70 : basicType( basicType )
71 , index( index )
72 {
73 }
74
75 Type( int basicType, const CClass &objClass ):
76 basicType( basicType ),
77 index( (LONG_PTR)&objClass ){}
78
79 Type( const Type &type )
80 : basicType( type.basicType )
81 , index( type.index )
82 , actualGenericTypes( type.actualGenericTypes )
83 , formalTypeName( type.formalTypeName )
84 , formalTypeIndex( type.formalTypeIndex )
85 {
86 }
87
88 ~Type();
89
90 void operator= ( const Type &type )
91 {
92 basicType = type.basicType;
93 index = type.index;
94 actualGenericTypes = type.actualGenericTypes;
95 formalTypeName = type.formalTypeName;
96 formalTypeIndex = type.formalTypeIndex;
97 }
98
99 __inline int GetBasicType() const
100 {
101 return basicType;
102 }
103 LONG_PTR GetIndex() const
104 {
105 return index;
106 }
107 const CClass &GetClass() const;
108
109 void SetBasicType( int basicType ){
110 this->basicType = basicType;
111 }
112 void SetIndex( LONG_PTR index ){
113 this->index = index;
114 }
115 void SetClassPtr( const CClass *pClass )
116 {
117 int naturalBasicType = NATURAL_TYPE( basicType );
118 if( !HasMember() )
119 {
120 Jenga::Throw( "クラスまたは構造体でない型に対してSetClassPtrを呼び出した" );
121 }
122 this->pClass = pClass;
123 }
124 void SetNull(){
125 SetBasicType( DEF_NON );
126 SetIndex( -1 );
127 }
128 void SetType( int basicType, LONG_PTR index ){
129 SetBasicType( basicType );
130 SetIndex( index );
131 }
132 void SetType( int basicType, const CClass *pClass ){
133 SetBasicType( basicType );
134 this->pClass = pClass;
135 }
136 void SetActualGenericTypes( const GenericTypes &genericTypes )
137 {
138 this->actualGenericTypes = genericTypes;
139 }
140
141 int PtrLevel() const
142 {
143 return PTR_LEVEL( basicType );
144 }
145 void PtrLevelUp(){
146 PTR_LEVEL_UP( basicType );
147 }
148 void PtrLevelDown(){
149 PTR_LEVEL_DOWN( basicType );
150 }
151
152 bool Equals( const Type &type ) const;
153 bool IsCovariant( const Type &type ) const;
154 bool IsContravariant( const Type &type ) const;
155
156 int GetBasicSize() const;
157 int GetSize() const;
158
159 bool IsNull() const;
160
161 bool IsByte() const;
162 bool IsSByte() const;
163 bool IsWord() const;
164 bool IsInteger() const;
165 bool IsDWord() const;
166 bool IsLong() const;
167 bool IsQWord() const;
168 bool IsInt64() const;
169 bool IsSingle() const;
170 bool IsDouble() const;
171 bool IsBoolean() const;
172
173 static bool IsPointer( int basicType );
174 bool IsPointer() const;
175 bool IsSigned() const;
176 bool IsNaturalWhole() const;
177 bool IsWhole() const;
178 bool IsReal() const;
179 bool Is64() const;
180 bool IsProcPtr() const;
181 bool IsStruct() const;
182 bool IsStructPtr() const;
183 bool IsObject() const;
184 bool IsObjectPtr() const;
185 bool IsTypeParameter() const;
186 bool IsObjectClass() const;
187 bool IsStringClass() const;
188 bool IsVoidPtr() const;
189 bool IsAny() const;
190 bool IsDelegate() const;
191 bool IsInterface() const;
192 bool IsComInterface() const;
193
194 // オブジェクトや構造体など、メンバを持つ型かどうかを判別する
195 bool HasMember() const;
196
197 // 型パラメータの名前を取得
198 const std::string &GetFormalTypeName() const
199 {
200 if( !IsTypeParameter() )
201 {
202 Jenga::Throw( "型パラメータでない型に対してGetFormalTypeNameメソッドが呼ばれた" );
203 }
204 return formalTypeName;
205 }
206 void SetFormalTypeName( const std::string &formalTypeName )
207 {
208 if( !IsTypeParameter() )
209 {
210 Jenga::Throw( "型パラメータでない型に対してSetFormalTypeNameメソッドが呼ばれた" );
211 }
212 this->formalTypeName = formalTypeName;
213 }
214 int GetFormalTypeIndex() const
215 {
216 if( !IsTypeParameter() )
217 {
218 Jenga::Throw( "型パラメータでない型に対してGetFormalTypeIndexメソッドが呼ばれた" );
219 }
220 return formalTypeIndex;
221 }
222 void SetFormalTypeIndex( int formalTypeIndex )
223 {
224 if( !IsTypeParameter() )
225 {
226 Jenga::Throw( "型パラメータでない型に対してSetFormalTypeIndexメソッドが呼ばれた" );
227 }
228 this->formalTypeIndex = formalTypeIndex;
229 }
230
231 // 未完成
232 const Type &GetActualGenericType( int index ) const;
233 bool HasActualGenericType() const;
234
235 //型名を取得
236 std::string ToString() const;
237
238
239private:
240 static const int basicTypeList[];
241 static const std::string basicTypeNameList[];
242public:
243 static bool StringToBasicType( const std::string &typeName, int &basicType );
244 static const char *Type::BasicTypeToCharPtr( const Type &type );
245 static int GetBasicTypeFromSimpleName( const char *variable );
246};
247typedef std::vector<Type> Types;
248
249/*!
250@brief ジェネリックな型を解決する
251@param typeParameter ジェネリック型を指定する。ここに解決後の型が入る。
252 classType インスタンス化されているオブジェクトの型
253 pUserProc 現在コンパイル中の関数(ただしクラスメソッドのみ)
254*/
255void ResolveFormalGenericTypeParameter( Type &typeParameter, const Type &classType, const UserProc *pUserProc = NULL );
256
257class GenericType
258{
259 std::string name;
260 Type type;
261
262 // XMLシリアライズ用
263private:
264 friend class boost::serialization::access;
265 template<class Archive> void serialize(Archive& ar, const unsigned int version)
266 {
267 trace_for_serialize( "serializing - GenericType" );
268
269 ar & BOOST_SERIALIZATION_NVP( name );
270 ar & BOOST_SERIALIZATION_NVP( type );
271 }
272
273public:
274 GenericType( const std::string &name, const Type &type )
275 : name( name )
276 , type( type )
277 {
278 }
279 GenericType()
280 {
281 }
282 ~GenericType()
283 {
284 }
285
286 const std::string &GetName() const
287 {
288 return name;
289 }
290 const Type &GetType() const
291 {
292 return type;
293 }
294};
295
296class BlittableType
297{
298 Type basicType;
299 CClass *pClass;
300
301 // XMLシリアライズ用
302private:
303 friend class boost::serialization::access;
304 template<class Archive> void serialize(Archive& ar, const unsigned int version)
305 {
306 trace_for_serialize( "serializing - BlittableType" );
307
308 ar & BOOST_SERIALIZATION_NVP( basicType );
309 ar & BOOST_SERIALIZATION_NVP( pClass );
310 }
311
312public:
313 bool isTargetObjectModule;
314 BlittableType( const Type &basicType, CClass *pClass )
315 : basicType( basicType )
316 , pClass( pClass )
317 , isTargetObjectModule( true )
318 {
319 }
320 BlittableType()
321 : isTargetObjectModule( true )
322 {
323 }
324 const Type &GetBasicType() const
325 {
326 return basicType;
327 }
328 const CClass *GetClassPtr() const
329 {
330 return pClass;
331 }
332 const std::string GetCreateStaticMethodFullName() const;
333};
334class BlittableTypes : public std::vector<BlittableType>
335{
336 // XMLシリアライズ用
337private:
338 friend class boost::serialization::access;
339 template<class Archive> void serialize(Archive& ar, const unsigned int version)
340 {
341 trace_for_serialize( "serializing - BlittableTypes" );
342
343 ar & boost::serialization::make_nvp("vector_BlittableType",
344 boost::serialization::base_object<std::vector<BlittableType>>(*this));
345 }
346
347public:
348 bool IsExist( Type type ) const
349 {
350 const BlittableTypes &blittableTypes = *this;
351 BOOST_FOREACH( const BlittableType &blittableType, blittableTypes ){
352 if( blittableType.GetBasicType().Equals( type ) ){
353 return true;
354 }
355 }
356 return false;
357 }
358 const BlittableType &Find( const Type &type ) const
359 {
360 const BlittableTypes &blittableTypes = *this;
361 BOOST_FOREACH( const BlittableType &blittableType, blittableTypes ){
362 if( blittableType.GetBasicType().Equals( type ) ){
363 return blittableType;
364 }
365 }
366 Jenga::Throw( "Blittable型ではない" );
367
368 static BlittableType dummy;
369 return dummy;
370 }
371 const CClass *GetClassPtr( const Type &type ) const
372 {
373 const BlittableTypes &blittableTypes = *this;
374 BOOST_FOREACH( const BlittableType &blittableType, blittableTypes ){
375 if( blittableType.GetBasicType().Equals( type ) ){
376 return blittableType.GetClassPtr();
377 }
378 }
379 return NULL;
380 }
381 const CClass &GetClass( const Type &type ) const
382 {
383 return *GetClassPtr( type );
384 }
385};
Note: See TracBrowser for help on using the repository browser.