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

Last change on this file since 180 was 180, checked in by dai_9181, 17 years ago

Meta::GetClassesメソッドを追加

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