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

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

・デリゲートの共変戻り値、反変引数に対応した。
・core.libで定義されたデリゲートがアプリケーションプロジェクトで利用できないバグを修正。

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