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

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

・デリゲートの共変戻り値、反変引数に対応した。
・core.libで定義されたデリゲートがアプリケーションプロジェクトで利用できないバグを修正。

File size: 10.8 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}
164bool Type::IsContravariant( const Type &type ) const
165{
166 if( !this->IsObject() || !type.IsObject() )
167 {
168 // 反変性の判別はクラス型のみ
169 return false;
170 }
171
172 return type.GetClass().IsSubClass( &this->GetClass() );
173}
174
175int Type::GetBasicSize() const
176{
177 return GetBasicSize( basicType );
178}
179int Type::GetSize() const
180{
181
182 // 基本型
183 switch( basicType ){
184 case DEF_LONG:
185 if(index==LITERAL_NULL||index==LITERAL_M128_0||index==LITERAL_0_255){
186 return sizeof(BYTE);
187 }
188 else if(index==LITERAL_M32768_0||index==LITERAL_0_65535){
189 return sizeof(WORD);
190 }
191 return sizeof(DWORD);
192
193 case DEF_SBYTE:
194 case DEF_BYTE:
195 case DEF_BOOLEAN:
196 return sizeof(BYTE);
197
198 case DEF_INTEGER:
199 case DEF_WORD:
200 return sizeof(WORD);
201
202 case DEF_DWORD:
203 return sizeof(DWORD);
204
205 case DEF_INT64:
206 case DEF_QWORD:
207 return sizeof(_int64);
208
209 case DEF_DOUBLE:
210 return sizeof(double);
211 case DEF_SINGLE:
212 return sizeof(float);
213 }
214
215 // ポインタ
216 if(IsPointer()){
217 return PTR_SIZE;
218 }
219
220 // 構造体
221 if( IsStruct() )
222 {
223 if( !pClass ){
224 SmoothieException::Throw();
225 return 0;
226 }
227
228 return pClass->GetSize();
229 }
230
231 // オブジェクト
232 if( IsObject() )
233 {
234 return PTR_SIZE;
235 }
236
237 SmoothieException::Throw();
238 return 0;
239}
240
241bool Type::IsNull() const{
242 if( basicType == DEF_NON ){
243 return true;
244 }
245 return false;
246}
247
248bool Type::IsByte() const{
249 if( basicType == DEF_BYTE ){
250 return true;
251 }
252 return false;
253}
254bool Type::IsSByte() const{
255 if( basicType == DEF_SBYTE ){
256 return true;
257 }
258 return false;
259}
260bool Type::IsWord() const{
261 if( basicType == DEF_WORD ){
262 return true;
263 }
264 return false;
265}
266bool Type::IsInteger() const{
267 if( basicType == DEF_INTEGER ){
268 return true;
269 }
270 return false;
271}
272bool Type::IsDWord() const{
273 if( basicType == DEF_DWORD ){
274 return true;
275 }
276 return false;
277}
278bool Type::IsLong() const{
279 if( basicType == DEF_LONG ){
280 return true;
281 }
282 return false;
283}
284bool Type::IsQWord() const{
285 if( basicType == DEF_QWORD ){
286 return true;
287 }
288 return false;
289}
290bool Type::IsInt64() const{
291 if( basicType == DEF_INT64 ){
292 return true;
293 }
294 return false;
295}
296bool Type::IsSingle() const{
297 if( basicType == DEF_SINGLE ){
298 return true;
299 }
300 return false;
301}
302bool Type::IsDouble() const{
303 if( basicType == DEF_DOUBLE ){
304 return true;
305 }
306 return false;
307}
308bool Type::IsBoolean() const{
309 if( basicType == DEF_BOOLEAN ){
310 return true;
311 }
312 return false;
313}
314
315bool Type::IsPointer( int basicType )
316{
317 if( basicType == DEF_NON )
318 {
319 return false;
320 }
321
322 if(PTR_LEVEL( basicType )|| basicType == DEF_PTR_VOID || basicType == DEF_PTR_PROC
323 || ( basicType & FLAG_PTR ) ){
324 return true;
325 }
326
327 return false;
328}
329bool Type::IsPointer() const
330{
331 return IsPointer( basicType );
332}
333bool Type::IsSigned() const
334{
335 switch( basicType ){
336 case DEF_SBYTE:
337 case DEF_INTEGER:
338 case DEF_LONG:
339 case DEF_INT64:
340 case DEF_SINGLE:
341 case DEF_DOUBLE:
342 return true;
343 default:
344 break;
345 }
346 return false;
347}
348bool Type::IsNaturalWhole() const
349{
350 switch( basicType ){
351 case DEF_SBYTE:
352 case DEF_BYTE:
353 case DEF_INTEGER:
354 case DEF_WORD:
355 case DEF_LONG:
356 case DEF_DWORD:
357 case DEF_INT64:
358 case DEF_QWORD:
359 return true;
360 default:
361 break;
362 }
363 return false;
364}
365bool Type::IsWhole() const
366{
367 return (
368 IsNaturalWhole()
369 || IsPointer( basicType )
370 || basicType == DEF_BOOLEAN
371 );
372}
373bool Type::IsReal() const
374{
375 switch( basicType ){
376 case DEF_SINGLE:
377 case DEF_DOUBLE:
378 return true;
379 default:
380 break;
381 }
382 return false;
383}
384bool Type::Is64() const
385{
386 switch( basicType ){
387 case DEF_QWORD:
388 case DEF_INT64:
389 return true;
390 default:
391 break;
392 }
393 return false;
394}
395bool Type::IsProcPtr() const
396{
397 if( basicType == DEF_PTR_PROC ){
398 return true;
399 }
400 return false;
401}
402bool Type::IsStruct() const
403{
404 if( basicType == DEF_STRUCT ){
405 return true;
406 }
407 return false;
408}
409bool Type::IsStructPtr() const
410{
411 if( basicType == DEF_PTR_STRUCT ){
412 return true;
413 }
414 return false;
415}
416bool Type::IsObject() const
417{
418 return (
419 basicType == DEF_OBJECT
420 || basicType == DEF_TYPE_PARAMETER
421 );
422}
423bool Type::IsObjectPtr() const
424{
425 if( basicType == DEF_PTR_OBJECT ){
426 return true;
427 }
428 return false;
429}
430bool Type::IsTypeParameter() const
431{
432 return ( NATURAL_TYPE(basicType) == DEF_TYPE_PARAMETER );
433}
434bool Type::IsObjectClass() const
435{
436 if( basicType == DEF_OBJECT ){
437 if( pClass->GetName() == "Object" ){
438 return true;
439 }
440 }
441 return false;
442}
443bool Type::IsStringClass() const
444{
445 if( basicType == DEF_OBJECT ){
446 if( pClass->GetName() == "String" ){
447 return true;
448 }
449 }
450 return false;
451}
452bool Type::IsVoidPtr() const
453{
454 if( basicType == DEF_PTR_VOID ){
455 return true;
456 }
457 return false;
458}
459
460bool Type::IsAny() const
461{
462 if( basicType == DEF_ANY ){
463 return true;
464 }
465 return false;
466}
467
468bool Type::IsDelegate() const
469{
470 return ( IsObject() && GetClass().IsDelegate() );
471}
472bool Type::IsInterface() const
473{
474 return ( IsObject() && GetClass().IsInterface() );
475}
476bool Type::IsComInterface() const
477{
478 return ( IsObject() && GetClass().IsComInterface() );
479}
480
481
482bool Type::HasMember() const
483{
484 if( NATURAL_TYPE( basicType ) == DEF_OBJECT
485 || NATURAL_TYPE( basicType ) == DEF_STRUCT
486 || NATURAL_TYPE( basicType ) == DEF_TYPE_PARAMETER
487 ){
488 return true;
489 }
490 return false;
491}
492
493const Type &Type::GetActualGenericType( int index ) const
494{
495 return actualGenericTypes[index].GetType();
496}
497bool Type::HasActualGenericType() const
498{
499 return ( actualGenericTypes.size() > 0 );
500}
501
502std::string Type::ToString() const
503{
504 const char *basicTypeName = BasicTypeToCharPtr( *this );
505 if( basicTypeName )
506 {
507 return basicTypeName;
508 }
509
510 if( IsTypeParameter() )
511 {
512 return GetFormalTypeName();
513 }
514
515 std::string typeName = GetClass().GetFullName();
516 if( HasActualGenericType() )
517 {
518 std::string actualGenericTypesName;
519 BOOST_FOREACH( const GenericType &actualGenericType, actualGenericTypes )
520 {
521 if( actualGenericTypesName.size() )
522 {
523 actualGenericTypesName += ",";
524 }
525 actualGenericTypesName += actualGenericType.GetName();
526 }
527
528 typeName += "<" + actualGenericTypesName + ">";
529 }
530 return typeName;
531}
532
533int Type::GetBasicTypeFromSimpleName( const char *variable ){
534 extern char DefIntVari[26],DefSngVari[26],DefStrVari[26],divNum,dsvNum,dStrvNum;
535 int i;
536 char name[VN_SIZE];
537
538 //構造体メンバの場合を考慮
539 for(i=lstrlen(variable);i>0;i--){
540 if(variable[i]=='.'){
541 i++;
542 break;
543 }
544 }
545
546 for(;;i++){
547 if(variable[i]=='('||variable[i]=='\0'){
548 name[i]=0;
549 break;
550 }
551 name[i]=variable[i];
552 }
553 //変数名から選択
554 i--;
555 if(name[i]=='#') return DEF_DOUBLE;
556 if(name[i]=='!') return DEF_SINGLE;
557 if(name[i]=='%') return DEF_INTEGER;
558 return DEF_DOUBLE;
559}
560
561
562void ResolveFormalGenericTypeParameter( Type &typeParameter, const Type &classType, const UserProc *pUserProc )
563{
564 if( !typeParameter.IsTypeParameter() )
565 {
566 // ジェネリックな型ではなかったとき
567 return;
568 }
569
570 /////////////////////////////////////////////////////////
571 // ☆★☆ ジェネリクスサポート ☆★☆
572
573 // ポインタレベルを抽出
574 int ptrLevel = PTR_LEVEL( typeParameter.GetBasicType() );
575
576 if( pUserProc )
577 {
578 if( classType.IsObject() )
579 {
580 // 基底クラスでの自己解決
581 const CClass *pClass = &classType.GetClass();
582 while( pClass->HasSuperClass() )
583 {
584 if( pUserProc->GetParentClassPtr() == &pClass->GetSuperClass() )
585 {
586 if( pClass->GetSuperClassActualTypeParameters().size() )
587 {
588 // TODO: 適切な形に実装し直す(暫定的にトップの型を持ってきている)
589 typeParameter = pClass->GetSuperClassActualTypeParameters()[0];
590 }
591 }
592 pClass = &pClass->GetSuperClass();
593 }
594 }
595 }
596
597 if( typeParameter.IsTypeParameter() )
598 {
599 if( classType.HasActualGenericType() )
600 {
601 typeParameter = classType.GetActualGenericType( typeParameter.GetFormalTypeIndex() );
602 }
603 else
604 {
605 // 制約クラス(指定されていないときはObjectクラス)にセットする
606 typeParameter.SetBasicType( DEF_OBJECT );
607 }
608 }
609
610 for( int i=0; i<ptrLevel; i++ )
611 {
612 typeParameter.PtrLevelUp();
613 }
614
615 //
616 /////////////////////////////////////////////////////////
617}
618
619
620const string BlittableType::GetCreateStaticMethodFullName() const
621{
622 return pClass->GetNamespaceScopes().ToString() + "." + pClass->GetName() + "._Create";
623}
Note: See TracBrowser for help on using the repository browser.