source: dev/trunk/jenga/src/smoothie/Type.cpp@ 181

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