source: dev/trunk/ab5.0/abdev/ab_common/src/Lexical/Type.cpp@ 750

Last change on this file since 750 was 750, checked in by イグトランス (egtra), 16 years ago

BOOST_FOREACHを可能なものはVC++ 2005 for eachへ置換(やや速くなる)。

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