source: dev/trunk/ab5.0/abdev/ab_common/include/Lexical/Type.h@ 637

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

リンカの依存関係解決モジュールを製作中

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