source: dev/BasicCompiler_Common/Type.cpp@ 131

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

Prototypeクラスを用意した。

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