source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/Type.cpp@ 556

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

Typeクラスで発生するOutputFatalErrorをthrowに置換。

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