source: dev/BasicCompiler_Common/Type.cpp@ 117

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

String/ObjectをSystem名前空間に依存しない特殊型として扱うようにした

File size: 8.5 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 return PTR_SIZE;
246 }
247
248 SetError();
249 return 0;
250}
251
252bool Type::IsNull() const{
253 if( basicType == DEF_NON ){
254 return true;
255 }
256 return false;
257}
258
259bool Type::IsByte() const{
260 if( basicType == DEF_BYTE ){
261 return true;
262 }
263 return false;
264}
265bool Type::IsSByte() const{
266 if( basicType == DEF_SBYTE ){
267 return true;
268 }
269 return false;
270}
271bool Type::IsWord() const{
272 if( basicType == DEF_WORD ){
273 return true;
274 }
275 return false;
276}
277bool Type::IsInteger() const{
278 if( basicType == DEF_INTEGER ){
279 return true;
280 }
281 return false;
282}
283bool Type::IsDWord() const{
284 if( basicType == DEF_DWORD ){
285 return true;
286 }
287 return false;
288}
289bool Type::IsLong() const{
290 if( basicType == DEF_LONG ){
291 return true;
292 }
293 return false;
294}
295bool Type::IsQWord() const{
296 if( basicType == DEF_QWORD ){
297 return true;
298 }
299 return false;
300}
301bool Type::IsInt64() const{
302 if( basicType == DEF_INT64 ){
303 return true;
304 }
305 return false;
306}
307bool Type::IsSingle() const{
308 if( basicType == DEF_SINGLE ){
309 return true;
310 }
311 return false;
312}
313bool Type::IsDouble() const{
314 if( basicType == DEF_DOUBLE ){
315 return true;
316 }
317 return false;
318}
319bool Type::IsBoolean() const{
320 if( basicType == DEF_BOOLEAN ){
321 return true;
322 }
323 return false;
324}
325
326bool Type::IsPointer() const
327{
328 if(PTR_LEVEL( basicType )|| basicType == DEF_PTR_VOID || basicType == DEF_PTR_PROC
329 || ( basicType & FLAG_PTR ) ){
330 return true;
331 }
332
333 return false;
334}
335bool Type::IsSigned() const
336{
337 switch( basicType ){
338 case DEF_SBYTE:
339 case DEF_INTEGER:
340 case DEF_LONG:
341 case DEF_INT64:
342 case DEF_SINGLE:
343 case DEF_DOUBLE:
344 return true;
345 default:
346 break;
347 }
348 return false;
349}
350bool Type::IsNaturalWhole() const
351{
352 switch( basicType ){
353 case DEF_SBYTE:
354 case DEF_BYTE:
355 case DEF_INTEGER:
356 case DEF_WORD:
357 case DEF_LONG:
358 case DEF_DWORD:
359 case DEF_INT64:
360 case DEF_QWORD:
361 return true;
362 default:
363 break;
364 }
365 return false;
366}
367bool Type::IsWhole() const
368{
369 return (
370 IsNaturalWhole()
371 || IsPtrType( basicType )
372 || basicType == DEF_BOOLEAN
373 );
374}
375bool Type::IsReal() const
376{
377 switch( basicType ){
378 case DEF_SINGLE:
379 case DEF_DOUBLE:
380 return true;
381 default:
382 break;
383 }
384 return false;
385}
386bool Type::Is64() const
387{
388 switch( basicType ){
389 case DEF_QWORD:
390 case DEF_INT64:
391 return true;
392 default:
393 break;
394 }
395 return false;
396}
397bool Type::IsProcPtr() const
398{
399 if( basicType == DEF_PTR_PROC ){
400 return true;
401 }
402 return false;
403}
404bool Type::IsStruct() const
405{
406 if( basicType == DEF_STRUCT ){
407 return true;
408 }
409 return false;
410}
411bool Type::IsStructPtr() const
412{
413 if( basicType == DEF_PTR_STRUCT ){
414 return true;
415 }
416 return false;
417}
418bool Type::IsObject() const
419{
420 if( basicType == DEF_OBJECT ){
421 return true;
422 }
423 return false;
424}
425bool Type::IsObjectPtr() const
426{
427 if( basicType == DEF_PTR_OBJECT ){
428 return true;
429 }
430 return false;
431}
432bool Type::IsObjectClass() const
433{
434 if( basicType == DEF_OBJECT ){
435 if( lstrcmp( pClass->name,"Object")==0){
436 return true;
437 }
438 }
439 return false;
440}
441bool Type::IsStringClass() const
442{
443 if( basicType == DEF_OBJECT ){
444 if( lstrcmp( pClass->name,"String")==0){
445 return true;
446 }
447 }
448 return false;
449}
450bool Type::IsVoidPtr() const
451{
452 if( basicType == DEF_PTR_VOID ){
453 return true;
454 }
455 return false;
456}
457
458bool Type::IsAny() const
459{
460 if( basicType == DEF_ANY ){
461 return true;
462 }
463 return false;
464}
465
466bool Type::HasMember() const
467{
468 if( NATURAL_TYPE( basicType ) == DEF_OBJECT
469 || NATURAL_TYPE( basicType ) == DEF_STRUCT ){
470 return true;
471 }
472 return false;
473}
474
475const string Type::ToString() const
476{
477 if( PTR_LEVEL( basicType ) ){
478 //ポインタレベルが1以上の場合
479 Type type( *this );
480 type.PtrLevelDown();
481
482 return (string)"*" + type.ToString();
483 }
484 else if( IsObject() || IsStruct() ){
485 //オブジェクトまたは構造体
486
487 if( !( index == 0 || index == -1 ) ){
488 return pClass->name;
489 }
490 }
491 else if( IsProcPtr() ){
492 if( index == 0 || index == -1 ){
493 return "VoidPtr";
494 }
495 else{
496 if( Smoothie::Meta::procPointers[index]->ReturnType().IsNull() ){
497 return "*Sub";
498 }
499 return "*Function";
500 }
501 }
502 else{
503 // 基本型
504
505 for( int i=0; ; i++ ){
506 if( basicTypeList[i] == DEF_NON ){
507 break;
508 }
509 if( basicTypeList[i] == basicType ){
510 return basicTypeNameList[i];
511 }
512 }
513 }
514
515 extern int cp;
516 SetError(1,NULL,cp);
517
518 return (string)"(null)";
519}
520
521Type Type::String(){
522 return Type( DEF_OBJECT, *pobj_DBClass->GetStringClassPtr() );
523}
Note: See TracBrowser for help on using the repository browser.