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

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

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

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