source: dev/trunk/ab5.0/abdev/ab_common/src/Lexical/Type.cpp@ 603

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

ObjectModuleに関連するクラス一式をab_commonプロジェクトに移動した。

File size: 10.6 KB
RevLine 
[206]1#include "stdafx.h"
2
3const int Type::basicTypeList[] = {
4 DEF_BYTE,
5 DEF_SBYTE,
6 DEF_WORD,
7 DEF_INTEGER,
8 DEF_DWORD,
9 DEF_LONG,
10 DEF_QWORD,
11 DEF_INT64,
12
13 DEF_SINGLE,
14 DEF_DOUBLE,
15
16 DEF_BOOLEAN,
17
18 DEF_PTR_VOID,
19
20 DEF_ANY,
21
22 DEF_NON
23};
24
[523]25const std::string Type::basicTypeNameList[] = {
[206]26 "Byte",
27 "SByte",
28 "Word",
29 "Integer",
30 "DWord",
31 "Long",
32 "QWord",
33 "Int64",
34
35 "Single",
36 "Double",
37
38 "Boolean",
39
40 "VoidPtr",
41
42 "Any",
43
44 ""
45};
46
[290]47Type::~Type()
48{
49}
50
[523]51bool Type::StringToBasicType( const std::string &typeName, int &basicType ){
[206]52 for( int i=0; ; i++ ){
53 if( basicTypeList[i] == DEF_NON ){
54 break;
55 }
56 if( basicTypeNameList[i] == typeName ){
57 basicType = basicTypeList[i];
58 return true;
59 }
60 }
61 return false;
62}
63const char *Type::BasicTypeToCharPtr( const Type &type )
64{
65 for( int i=0; ; i++ ){
66 if( basicTypeList[i] == DEF_NON ){
67 break;
68 }
69 if( basicTypeList[i] == type.GetBasicType() ){
70 return basicTypeNameList[i].c_str();
71 }
72 }
73 return NULL;
74}
75
76int Type::GetBasicSize( int basicType )
77{
78
79 // 基本型
80 switch( basicType ){
81 case DEF_SBYTE:
82 case DEF_BYTE:
83 case DEF_BOOLEAN:
84 return sizeof(BYTE);
85
86 case DEF_INTEGER:
87 case DEF_WORD:
88 return sizeof(WORD);
89
90 case DEF_LONG:
91 case DEF_DWORD:
92 return sizeof(DWORD);
93
94 case DEF_INT64:
95 case DEF_QWORD:
96 return sizeof(_int64);
97
98 case DEF_DOUBLE:
99 return sizeof(double);
100 case DEF_SINGLE:
101 return sizeof(float);
102 }
103
104 // ポインタ
105 if(IsPointer( basicType )){
106 return PTR_SIZE;
107 }
108
109 // オブジェクト
110 if(basicType==DEF_OBJECT){
111 return PTR_SIZE;
112 }
113
[556]114 throw;
[206]115
116 return 0;
117}
118
[290]119const CClass &Type::GetClass() const
120{
121 if( !HasMember() )
122 {
123 Jenga::Throw( "クラスまたは構造体でない型に対してGetClassを呼び出した" );
124 }
[206]125
[290]126 return *pClass;
127}
128
[206]129bool Type::Equals( const Type &type ) const
130{
131 if( basicType == type.basicType ){
132 if( NATURAL_TYPE( basicType ) == DEF_OBJECT
133 || NATURAL_TYPE( basicType ) == DEF_STRUCT ){
134
135 if( index == type.index ){
136 return true;
137 }
138
139 }
140 else{
141 return true;
142 }
143 }
144 return false;
145}
146
[447]147bool Type::IsCovariant( const Type &type ) const
148{
149 if( !this->IsObject() || !type.IsObject() )
150 {
151 // 共変性の判別はクラス型のみ
152 return false;
153 }
154
155 return this->GetClass().IsSubClass( &type.GetClass() );
156}
[448]157bool Type::IsContravariant( const Type &type ) const
158{
159 if( !this->IsObject() || !type.IsObject() )
160 {
161 // 反変性の判別はクラス型のみ
162 return false;
163 }
[447]164
[448]165 return type.GetClass().IsSubClass( &this->GetClass() );
166}
167
[206]168int Type::GetBasicSize() const
169{
170 return GetBasicSize( basicType );
171}
172int Type::GetSize() const
173{
174
175 // 基本型
176 switch( basicType ){
177 case DEF_LONG:
178 if(index==LITERAL_NULL||index==LITERAL_M128_0||index==LITERAL_0_255){
179 return sizeof(BYTE);
180 }
181 else if(index==LITERAL_M32768_0||index==LITERAL_0_65535){
182 return sizeof(WORD);
183 }
184 return sizeof(DWORD);
185
186 case DEF_SBYTE:
187 case DEF_BYTE:
188 case DEF_BOOLEAN:
189 return sizeof(BYTE);
190
191 case DEF_INTEGER:
192 case DEF_WORD:
193 return sizeof(WORD);
194
195 case DEF_DWORD:
196 return sizeof(DWORD);
197
198 case DEF_INT64:
199 case DEF_QWORD:
200 return sizeof(_int64);
201
202 case DEF_DOUBLE:
203 return sizeof(double);
204 case DEF_SINGLE:
205 return sizeof(float);
206 }
207
208 // ポインタ
209 if(IsPointer()){
210 return PTR_SIZE;
211 }
212
213 // 構造体
[290]214 if( IsStruct() )
215 {
[206]216 if( !pClass ){
[556]217 throw;
[206]218 }
219
220 return pClass->GetSize();
221 }
222
223 // オブジェクト
[290]224 if( IsObject() )
225 {
[206]226 return PTR_SIZE;
227 }
228
[556]229 throw;
[206]230}
231
232bool Type::IsNull() const{
233 if( basicType == DEF_NON ){
234 return true;
235 }
236 return false;
237}
238
239bool Type::IsByte() const{
240 if( basicType == DEF_BYTE ){
241 return true;
242 }
243 return false;
244}
245bool Type::IsSByte() const{
246 if( basicType == DEF_SBYTE ){
247 return true;
248 }
249 return false;
250}
251bool Type::IsWord() const{
252 if( basicType == DEF_WORD ){
253 return true;
254 }
255 return false;
256}
257bool Type::IsInteger() const{
258 if( basicType == DEF_INTEGER ){
259 return true;
260 }
261 return false;
262}
263bool Type::IsDWord() const{
264 if( basicType == DEF_DWORD ){
265 return true;
266 }
267 return false;
268}
269bool Type::IsLong() const{
270 if( basicType == DEF_LONG ){
271 return true;
272 }
273 return false;
274}
275bool Type::IsQWord() const{
276 if( basicType == DEF_QWORD ){
277 return true;
278 }
279 return false;
280}
281bool Type::IsInt64() const{
282 if( basicType == DEF_INT64 ){
283 return true;
284 }
285 return false;
286}
287bool Type::IsSingle() const{
288 if( basicType == DEF_SINGLE ){
289 return true;
290 }
291 return false;
292}
293bool Type::IsDouble() const{
294 if( basicType == DEF_DOUBLE ){
295 return true;
296 }
297 return false;
298}
299bool Type::IsBoolean() const{
300 if( basicType == DEF_BOOLEAN ){
301 return true;
302 }
303 return false;
304}
305
306bool Type::IsPointer( int basicType )
307{
[350]308 if( basicType == DEF_NON )
309 {
310 return false;
311 }
312
[206]313 if(PTR_LEVEL( basicType )|| basicType == DEF_PTR_VOID || basicType == DEF_PTR_PROC
314 || ( basicType & FLAG_PTR ) ){
315 return true;
316 }
317
318 return false;
319}
320bool Type::IsPointer() const
321{
322 return IsPointer( basicType );
323}
324bool Type::IsSigned() const
325{
326 switch( basicType ){
327 case DEF_SBYTE:
328 case DEF_INTEGER:
329 case DEF_LONG:
330 case DEF_INT64:
331 case DEF_SINGLE:
332 case DEF_DOUBLE:
333 return true;
334 default:
335 break;
336 }
337 return false;
338}
339bool Type::IsNaturalWhole() const
340{
341 switch( basicType ){
342 case DEF_SBYTE:
343 case DEF_BYTE:
344 case DEF_INTEGER:
345 case DEF_WORD:
346 case DEF_LONG:
347 case DEF_DWORD:
348 case DEF_INT64:
349 case DEF_QWORD:
350 return true;
351 default:
352 break;
353 }
354 return false;
355}
356bool Type::IsWhole() const
357{
358 return (
359 IsNaturalWhole()
360 || IsPointer( basicType )
361 || basicType == DEF_BOOLEAN
362 );
363}
364bool Type::IsReal() const
365{
366 switch( basicType ){
367 case DEF_SINGLE:
368 case DEF_DOUBLE:
369 return true;
370 default:
371 break;
372 }
373 return false;
374}
375bool Type::Is64() const
376{
377 switch( basicType ){
378 case DEF_QWORD:
379 case DEF_INT64:
380 return true;
381 default:
382 break;
383 }
384 return false;
385}
386bool Type::IsProcPtr() const
387{
388 if( basicType == DEF_PTR_PROC ){
389 return true;
390 }
391 return false;
392}
393bool Type::IsStruct() const
394{
395 if( basicType == DEF_STRUCT ){
396 return true;
397 }
398 return false;
399}
400bool Type::IsStructPtr() const
401{
402 if( basicType == DEF_PTR_STRUCT ){
403 return true;
404 }
405 return false;
406}
407bool Type::IsObject() const
408{
[290]409 return (
410 basicType == DEF_OBJECT
[316]411 || basicType == DEF_TYPE_PARAMETER
[290]412 );
[206]413}
414bool Type::IsObjectPtr() const
415{
416 if( basicType == DEF_PTR_OBJECT ){
417 return true;
418 }
419 return false;
420}
[290]421bool Type::IsTypeParameter() const
422{
[292]423 return ( NATURAL_TYPE(basicType) == DEF_TYPE_PARAMETER );
[290]424}
[206]425bool Type::IsObjectClass() const
426{
427 if( basicType == DEF_OBJECT ){
428 if( pClass->GetName() == "Object" ){
429 return true;
430 }
431 }
432 return false;
433}
434bool Type::IsStringClass() const
435{
436 if( basicType == DEF_OBJECT ){
437 if( pClass->GetName() == "String" ){
438 return true;
439 }
440 }
441 return false;
442}
443bool Type::IsVoidPtr() const
444{
445 if( basicType == DEF_PTR_VOID ){
446 return true;
447 }
448 return false;
449}
450
451bool Type::IsAny() const
452{
453 if( basicType == DEF_ANY ){
454 return true;
455 }
456 return false;
457}
458
[332]459bool Type::IsDelegate() const
460{
461 return ( IsObject() && GetClass().IsDelegate() );
462}
[350]463bool Type::IsInterface() const
464{
465 return ( IsObject() && GetClass().IsInterface() );
466}
[370]467bool Type::IsComInterface() const
468{
469 return ( IsObject() && GetClass().IsComInterface() );
470}
[332]471
[350]472
[206]473bool Type::HasMember() const
474{
475 if( NATURAL_TYPE( basicType ) == DEF_OBJECT
[290]476 || NATURAL_TYPE( basicType ) == DEF_STRUCT
477 || NATURAL_TYPE( basicType ) == DEF_TYPE_PARAMETER
478 ){
479 return true;
[206]480 }
481 return false;
482}
483
[301]484const Type &Type::GetActualGenericType( int index ) const
[290]485{
[301]486 return actualGenericTypes[index].GetType();
[290]487}
488bool Type::HasActualGenericType() const
489{
490 return ( actualGenericTypes.size() > 0 );
491}
492
[378]493std::string Type::ToString() const
494{
495 const char *basicTypeName = BasicTypeToCharPtr( *this );
496 if( basicTypeName )
497 {
498 return basicTypeName;
499 }
500
501 if( IsTypeParameter() )
502 {
503 return GetFormalTypeName();
504 }
505
506 std::string typeName = GetClass().GetFullName();
507 if( HasActualGenericType() )
508 {
509 std::string actualGenericTypesName;
510 BOOST_FOREACH( const GenericType &actualGenericType, actualGenericTypes )
511 {
512 if( actualGenericTypesName.size() )
513 {
514 actualGenericTypesName += ",";
515 }
516 actualGenericTypesName += actualGenericType.GetName();
517 }
518
519 typeName += "<" + actualGenericTypesName + ">";
520 }
521 return typeName;
522}
523
[206]524int Type::GetBasicTypeFromSimpleName( const char *variable ){
525 extern char DefIntVari[26],DefSngVari[26],DefStrVari[26],divNum,dsvNum,dStrvNum;
526 int i;
527 char name[VN_SIZE];
528
529 //構造体メンバの場合を考慮
530 for(i=lstrlen(variable);i>0;i--){
531 if(variable[i]=='.'){
532 i++;
533 break;
534 }
535 }
536
537 for(;;i++){
538 if(variable[i]=='('||variable[i]=='\0'){
539 name[i]=0;
540 break;
541 }
542 name[i]=variable[i];
543 }
544 //変数名から選択
545 i--;
546 if(name[i]=='#') return DEF_DOUBLE;
547 if(name[i]=='!') return DEF_SINGLE;
548 if(name[i]=='%') return DEF_INTEGER;
549 return DEF_DOUBLE;
550}
551
552
[299]553void ResolveFormalGenericTypeParameter( Type &typeParameter, const Type &classType, const UserProc *pUserProc )
554{
[424]555 if( !typeParameter.IsTypeParameter() )
556 {
557 // ジェネリックな型ではなかったとき
558 return;
559 }
560
[299]561 /////////////////////////////////////////////////////////
562 // ☆★☆ ジェネリクスサポート ☆★☆
563
[424]564 // ポインタレベルを抽出
565 int ptrLevel = PTR_LEVEL( typeParameter.GetBasicType() );
566
567 if( pUserProc )
[299]568 {
[424]569 if( classType.IsObject() )
[299]570 {
[424]571 // 基底クラスでの自己解決
572 const CClass *pClass = &classType.GetClass();
573 while( pClass->HasSuperClass() )
[299]574 {
[424]575 if( pUserProc->GetParentClassPtr() == &pClass->GetSuperClass() )
[299]576 {
[424]577 if( pClass->GetSuperClassActualTypeParameters().size() )
[299]578 {
[424]579 // TODO: 適切な形に実装し直す(暫定的にトップの型を持ってきている)
580 typeParameter = pClass->GetSuperClassActualTypeParameters()[0];
[299]581 }
582 }
[424]583 pClass = &pClass->GetSuperClass();
[299]584 }
585 }
[424]586 }
[299]587
[424]588 if( typeParameter.IsTypeParameter() )
589 {
590 if( classType.HasActualGenericType() )
[299]591 {
[424]592 typeParameter = classType.GetActualGenericType( typeParameter.GetFormalTypeIndex() );
[299]593 }
[424]594 else
[299]595 {
[424]596 // 制約クラス(指定されていないときはObjectクラス)にセットする
597 typeParameter.SetBasicType( DEF_OBJECT );
[299]598 }
599 }
600
[424]601 for( int i=0; i<ptrLevel; i++ )
602 {
603 typeParameter.PtrLevelUp();
604 }
605
[299]606 //
607 /////////////////////////////////////////////////////////
608}
609
610
[523]611const std::string BlittableType::GetCreateStaticMethodFullName() const
[206]612{
613 return pClass->GetNamespaceScopes().ToString() + "." + pClass->GetName() + "._Create";
614}
Note: See TracBrowser for help on using the repository browser.