source: dev/BasicCompiler_Common/Type.cpp@ 75

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

TYPEINFO→Typeへのリファクタリングを実施。64bitはほぼ完了。32bitが全般的に未完成。

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