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

Last change on this file since 750 was 750, checked in by イグトランス (egtra), 16 years ago

BOOST_FOREACHを可能なものはVC++ 2005 for eachへ置換(やや速くなる)。

File size: 9.2 KB
Line 
1#pragma once
2
3class CClass;
4
5class GenericType;
6typedef std::vector<GenericType> GenericTypes;
7
8class Type
9{
10 int basicType;
11 union{
12 LONG_PTR index;
13 const CClass *pClass;
14 };
15
16 // ジェネリクス クラス インスタンス型の場合に使う
17 GenericTypes actualGenericTypes;
18
19 // 型パラメータで使う
20 std::string formalTypeName; // 型パラメータの名前
21 int formalTypeIndex; // 型パラメータの引数番号
22
23 // XMLシリアライズ用
24private:
25 friend class boost::serialization::access;
26 template<class Archive> void serialize(Archive& ar, const unsigned int version)
27 {
28 trace_for_serialize( "serializing - Type" );
29
30 ar & BOOST_SERIALIZATION_NVP( basicType );
31 if( HasMember() )
32 {
33 ar & boost::serialization::make_nvp("pClass", const_cast<CClass *&>(pClass));
34 ar & BOOST_SERIALIZATION_NVP( actualGenericTypes );
35 }
36 else
37 {
38 ar & BOOST_SERIALIZATION_NVP( index );
39 }
40
41 if( IsTypeParameter() )
42 {
43 ar & BOOST_SERIALIZATION_NVP( formalTypeName );
44 ar & BOOST_SERIALIZATION_NVP( formalTypeIndex );
45 }
46 }
47
48public:
49 static int GetBasicSize( int basicType );
50
51 Type():
52 basicType( DEF_NON ),
53 index( -1 ){}
54 Type( int basicType ):
55 basicType( basicType ),
56 index( -1 ){}
57
58 Type( int basicType, LONG_PTR index )
59 : basicType( basicType )
60 , index( index )
61 {
62 }
63
64 Type( int basicType, const CClass &objClass ):
65 basicType( basicType ),
66 index( (LONG_PTR)&objClass ){}
67
68 Type( const Type &type )
69 : basicType( type.basicType )
70 , index( type.index )
71 , actualGenericTypes( type.actualGenericTypes )
72 , formalTypeName( type.formalTypeName )
73 , formalTypeIndex( type.formalTypeIndex )
74 {
75 }
76
77 ~Type();
78
79 void operator= ( const Type &type )
80 {
81 basicType = type.basicType;
82 index = type.index;
83 actualGenericTypes = type.actualGenericTypes;
84 formalTypeName = type.formalTypeName;
85 formalTypeIndex = type.formalTypeIndex;
86 }
87
88 __inline int GetBasicType() const
89 {
90 return basicType;
91 }
92 LONG_PTR GetIndex() const
93 {
94 return index;
95 }
96 const CClass &GetClass() const;
97
98 void SetBasicType( int basicType ){
99 this->basicType = basicType;
100 }
101 void SetIndex( LONG_PTR index ){
102 this->index = index;
103 }
104 void SetClassPtr( const CClass *pClass )
105 {
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 virtual bool Resolve( const ObjectModule &resolver, ResolveErrors &resolveErrors );
232
233
234private:
235 static const int basicTypeList[];
236 static const std::string basicTypeNameList[];
237public:
238 static bool StringToBasicType( const std::string &typeName, int &basicType );
239 static const char *Type::BasicTypeToCharPtr( const Type &type );
240 static int GetBasicTypeFromSimpleName( const char *variable );
241};
242
243class Types
244 : public std::vector<Type>
245{
246 // XMLシリアライズ用
247private:
248 friend class boost::serialization::access;
249 template<class Archive> void serialize(Archive& ar, const unsigned int version)
250 {
251 ar & boost::serialization::make_nvp("vector_Type", boost::serialization::base_object<vector<Type>>(*this));
252 }
253
254public:
255 bool IsEquals( const Types &Types ) const;
256};
257
258/*!
259@brief ジェネリックな型を解決する
260@param typeParameter ジェネリック型を指定する。ここに解決後の型が入る。
261 classType インスタンス化されているオブジェクトの型
262 pUserProc 現在コンパイル中の関数(ただしクラスメソッドのみ)
263*/
264void ResolveFormalGenericTypeParameter( Type &typeParameter, const Type &classType, const UserProc *pUserProc = NULL );
265
266class GenericType
267{
268 std::string name;
269 Type type;
270
271 // XMLシリアライズ用
272private:
273 friend class boost::serialization::access;
274 template<class Archive> void serialize(Archive& ar, const unsigned int version)
275 {
276 trace_for_serialize( "serializing - GenericType" );
277
278 ar & BOOST_SERIALIZATION_NVP( name );
279 ar & BOOST_SERIALIZATION_NVP( type );
280 }
281
282public:
283 GenericType( const std::string &name, const Type &type )
284 : name( name )
285 , type( type )
286 {
287 }
288 GenericType()
289 {
290 }
291 ~GenericType()
292 {
293 }
294
295 const std::string &GetName() const
296 {
297 return name;
298 }
299 const Type &GetType() const
300 {
301 return type;
302 }
303 Type &GetType()
304 {
305 return type;
306 }
307};
308
309class BlittableType
310{
311 Type basicType;
312 const CClass *pClass;
313
314 // XMLシリアライズ用
315private:
316 friend class boost::serialization::access;
317 template<class Archive> void serialize(Archive& ar, const unsigned int version)
318 {
319 trace_for_serialize( "serializing - BlittableType" );
320
321 ar & BOOST_SERIALIZATION_NVP( basicType );
322 ar & boost::serialization::make_nvp("pClass", const_cast<CClass *&>(pClass) );
323 }
324
325public:
326 BlittableType( const Type &basicType, const CClass *pClass )
327 : basicType( basicType )
328 , pClass( pClass )
329 {
330 }
331 BlittableType()
332 {
333 }
334 const Type &GetBasicType() const
335 {
336 return basicType;
337 }
338 const CClass *GetClassPtr() const
339 {
340 return pClass;
341 }
342 const std::string GetCreateStaticMethodFullName() const;
343
344 virtual bool Resolve( const ObjectModule &resolver, ResolveErrors &resolveErrors );
345};
346class BlittableTypes : public std::vector<BlittableType>
347{
348 // XMLシリアライズ用
349private:
350 friend class boost::serialization::access;
351 template<class Archive> void serialize(Archive& ar, const unsigned int version)
352 {
353 trace_for_serialize( "serializing - BlittableTypes" );
354
355 ar & boost::serialization::make_nvp("vector_BlittableType",
356 boost::serialization::base_object<std::vector<BlittableType>>(*this));
357 }
358
359public:
360 bool IsExist( Type type ) const
361 {
362 const BlittableTypes &blittableTypes = *this;
363 foreach( const BlittableType &blittableType, blittableTypes ){
364 if( blittableType.GetBasicType().Equals( type ) ){
365 return true;
366 }
367 }
368 return false;
369 }
370 const BlittableType &Find( const Type &type ) const
371 {
372 const BlittableTypes &blittableTypes = *this;
373 foreach( const BlittableType &blittableType, blittableTypes ){
374 if( blittableType.GetBasicType().Equals( type ) ){
375 return blittableType;
376 }
377 }
378 Jenga::Throw( "Blittable型ではない" );
379
380 static BlittableType dummy;
381 return dummy;
382 }
383 const CClass *GetClassPtr( const Type &type ) const
384 {
385 const BlittableTypes &blittableTypes = *this;
386 foreach( const BlittableType &blittableType, blittableTypes ){
387 if( blittableType.GetBasicType().Equals( type ) ){
388 return blittableType.GetClassPtr();
389 }
390 }
391 return NULL;
392 }
393 const CClass &GetClass( const Type &type ) const
394 {
395 return *GetClassPtr( type );
396 }
397};
Note: See TracBrowser for help on using the repository browser.