source: dev/trunk/abdev/BasicCompiler_Common/src/Type.cpp@ 424

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

・ジェネリックな型をパラメータに持つメソッドのオーバーロード解決に対応した。
・型パラメータの制約クラス指定に対応した。

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