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
Line 
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
25const std::string Type::basicTypeNameList[] = {
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
47Type::~Type()
48{
49}
50
51bool Type::StringToBasicType( const std::string &typeName, int &basicType ){
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
114 throw;
115
116 return 0;
117}
118
119const CClass &Type::GetClass() const
120{
121 if( !HasMember() )
122 {
123 Jenga::Throw( "クラスまたは構造体でない型に対してGetClassを呼び出した" );
124 }
125
126 return *pClass;
127}
128
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
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}
157bool Type::IsContravariant( const Type &type ) const
158{
159 if( !this->IsObject() || !type.IsObject() )
160 {
161 // 反変性の判別はクラス型のみ
162 return false;
163 }
164
165 return type.GetClass().IsSubClass( &this->GetClass() );
166}
167
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 // 構造体
214 if( IsStruct() )
215 {
216 if( !pClass ){
217 throw;
218 }
219
220 return pClass->GetSize();
221 }
222
223 // オブジェクト
224 if( IsObject() )
225 {
226 return PTR_SIZE;
227 }
228
229 throw;
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{
308 if( basicType == DEF_NON )
309 {
310 return false;
311 }
312
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{
409 return (
410 basicType == DEF_OBJECT
411 || basicType == DEF_TYPE_PARAMETER
412 );
413}
414bool Type::IsObjectPtr() const
415{
416 if( basicType == DEF_PTR_OBJECT ){
417 return true;
418 }
419 return false;
420}
421bool Type::IsTypeParameter() const
422{
423 return ( NATURAL_TYPE(basicType) == DEF_TYPE_PARAMETER );
424}
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
459bool Type::IsDelegate() const
460{
461 return ( IsObject() && GetClass().IsDelegate() );
462}
463bool Type::IsInterface() const
464{
465 return ( IsObject() && GetClass().IsInterface() );
466}
467bool Type::IsComInterface() const
468{
469 return ( IsObject() && GetClass().IsComInterface() );
470}
471
472
473bool Type::HasMember() const
474{
475 if( NATURAL_TYPE( basicType ) == DEF_OBJECT
476 || NATURAL_TYPE( basicType ) == DEF_STRUCT
477 || NATURAL_TYPE( basicType ) == DEF_TYPE_PARAMETER
478 ){
479 return true;
480 }
481 return false;
482}
483
484const Type &Type::GetActualGenericType( int index ) const
485{
486 return actualGenericTypes[index].GetType();
487}
488bool Type::HasActualGenericType() const
489{
490 return ( actualGenericTypes.size() > 0 );
491}
492
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
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
553void ResolveFormalGenericTypeParameter( Type &typeParameter, const Type &classType, const UserProc *pUserProc )
554{
555 if( !typeParameter.IsTypeParameter() )
556 {
557 // ジェネリックな型ではなかったとき
558 return;
559 }
560
561 /////////////////////////////////////////////////////////
562 // ☆★☆ ジェネリクスサポート ☆★☆
563
564 // ポインタレベルを抽出
565 int ptrLevel = PTR_LEVEL( typeParameter.GetBasicType() );
566
567 if( pUserProc )
568 {
569 if( classType.IsObject() )
570 {
571 // 基底クラスでの自己解決
572 const CClass *pClass = &classType.GetClass();
573 while( pClass->HasSuperClass() )
574 {
575 if( pUserProc->GetParentClassPtr() == &pClass->GetSuperClass() )
576 {
577 if( pClass->GetSuperClassActualTypeParameters().size() )
578 {
579 // TODO: 適切な形に実装し直す(暫定的にトップの型を持ってきている)
580 typeParameter = pClass->GetSuperClassActualTypeParameters()[0];
581 }
582 }
583 pClass = &pClass->GetSuperClass();
584 }
585 }
586 }
587
588 if( typeParameter.IsTypeParameter() )
589 {
590 if( classType.HasActualGenericType() )
591 {
592 typeParameter = classType.GetActualGenericType( typeParameter.GetFormalTypeIndex() );
593 }
594 else
595 {
596 // 制約クラス(指定されていないときはObjectクラス)にセットする
597 typeParameter.SetBasicType( DEF_OBJECT );
598 }
599 }
600
601 for( int i=0; i<ptrLevel; i++ )
602 {
603 typeParameter.PtrLevelUp();
604 }
605
606 //
607 /////////////////////////////////////////////////////////
608}
609
610
611const std::string BlittableType::GetCreateStaticMethodFullName() const
612{
613 return pClass->GetNamespaceScopes().ToString() + "." + pClass->GetName() + "._Create";
614}
Note: See TracBrowser for help on using the repository browser.