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

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

共変戻り値のオーバーロードをサポートした。

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