source: dev/trunk/abdev/BasicCompiler_Common/include/Type.h@ 350

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

インターフェイスを実装

File size: 8.5 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
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
193 // オブジェクトや構造体など、メンバを持つ型かどうかを判別する
194 bool HasMember() const;
195
196 // 型パラメータの名前を取得
197 const std::string &GetFormalTypeName() const
198 {
199 if( !IsTypeParameter() )
200 {
201 Jenga::Throw( "型パラメータでない型に対してGetFormalTypeNameメソッドが呼ばれた" );
202 }
203 return formalTypeName;
204 }
205 void SetFormalTypeName( const std::string &formalTypeName )
206 {
207 if( !IsTypeParameter() )
208 {
209 Jenga::Throw( "型パラメータでない型に対してSetFormalTypeNameメソッドが呼ばれた" );
210 }
211 this->formalTypeName = formalTypeName;
212 }
213 int GetFormalTypeIndex() const
214 {
215 if( !IsTypeParameter() )
216 {
217 Jenga::Throw( "型パラメータでない型に対してGetFormalTypeIndexメソッドが呼ばれた" );
218 }
219 return formalTypeIndex;
220 }
221 void SetFormalTypeIndex( int formalTypeIndex )
222 {
223 if( !IsTypeParameter() )
224 {
225 Jenga::Throw( "型パラメータでない型に対してSetFormalTypeIndexメソッドが呼ばれた" );
226 }
227 this->formalTypeIndex = formalTypeIndex;
228 }
229
230 // 未完成
231 const Type &GetActualGenericType( int index ) const;
232 bool HasActualGenericType() const;
233
234
235private:
236 static const int basicTypeList[];
237 static const std::string basicTypeNameList[];
238public:
239 static bool StringToBasicType( const std::string &typeName, int &basicType );
240 static const char *Type::BasicTypeToCharPtr( const Type &type );
241 static int GetBasicTypeFromSimpleName( const char *variable );
242};
243typedef std::vector<Type> Types;
244
245void ResolveFormalGenericTypeParameter( Type &typeParameter, const Type &classType, const UserProc *pUserProc = NULL );
246
247class GenericType
248{
249 std::string name;
250 Type type;
251
252 // XMLシリアライズ用
253private:
254 friend class boost::serialization::access;
255 template<class Archive> void serialize(Archive& ar, const unsigned int version)
256 {
257 trace_for_serialize( "serializing - GenericType" );
258
259 ar & BOOST_SERIALIZATION_NVP( name );
260 ar & BOOST_SERIALIZATION_NVP( type );
261 }
262
263public:
264 GenericType( const std::string &name, const Type &type )
265 : name( name )
266 , type( type )
267 {
268 }
269 GenericType()
270 {
271 }
272 ~GenericType()
273 {
274 }
275
276 const std::string &GetName() const
277 {
278 return name;
279 }
280 const Type &GetType() const
281 {
282 return type;
283 }
284};
285
286class BlittableType
287{
288 Type basicType;
289 CClass *pClass;
290
291 // XMLシリアライズ用
292private:
293 friend class boost::serialization::access;
294 template<class Archive> void serialize(Archive& ar, const unsigned int version)
295 {
296 trace_for_serialize( "serializing - BlittableType" );
297
298 ar & BOOST_SERIALIZATION_NVP( basicType );
299 ar & BOOST_SERIALIZATION_NVP( pClass );
300 }
301
302public:
303 bool isTargetObjectModule;
304 BlittableType( const Type &basicType, CClass *pClass )
305 : basicType( basicType )
306 , pClass( pClass )
307 , isTargetObjectModule( true )
308 {
309 }
310 BlittableType()
311 : isTargetObjectModule( true )
312 {
313 }
314 const Type &GetBasicType() const
315 {
316 return basicType;
317 }
318 const CClass *GetClassPtr() const
319 {
320 return pClass;
321 }
322 const std::string GetCreateStaticMethodFullName() const;
323};
324class BlittableTypes : public std::vector<BlittableType>
325{
326 // XMLシリアライズ用
327private:
328 friend class boost::serialization::access;
329 template<class Archive> void serialize(Archive& ar, const unsigned int version)
330 {
331 trace_for_serialize( "serializing - BlittableTypes" );
332
333 ar & boost::serialization::make_nvp("vector_BlittableType",
334 boost::serialization::base_object<std::vector<BlittableType>>(*this));
335 }
336
337public:
338 bool IsExist( Type type ) const
339 {
340 const BlittableTypes &blittableTypes = *this;
341 BOOST_FOREACH( const BlittableType &blittableType, blittableTypes ){
342 if( blittableType.GetBasicType().Equals( type ) ){
343 return true;
344 }
345 }
346 return false;
347 }
348 const BlittableType &Find( const Type &type ) const
349 {
350 const BlittableTypes &blittableTypes = *this;
351 BOOST_FOREACH( const BlittableType &blittableType, blittableTypes ){
352 if( blittableType.GetBasicType().Equals( type ) ){
353 return blittableType;
354 }
355 }
356 Jenga::Throw( "Blittable型ではない" );
357
358 static BlittableType dummy;
359 return dummy;
360 }
361 const CClass *GetClassPtr( const Type &type ) const
362 {
363 const BlittableTypes &blittableTypes = *this;
364 BOOST_FOREACH( const BlittableType &blittableType, blittableTypes ){
365 if( blittableType.GetBasicType().Equals( type ) ){
366 return blittableType.GetClassPtr();
367 }
368 }
369 return NULL;
370 }
371 const CClass &GetClass( const Type &type ) const
372 {
373 return *GetClassPtr( type );
374 }
375};
Note: See TracBrowser for help on using the repository browser.