source: dev/BasicCompiler_Common/Type.cpp@ 77

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

デフォルトパラメータに対応。

File size: 8.0 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 return GetBasicSize( basicType );
176}
177int Type::GetSize() const
178{
179
180 // 基本型
181 switch( basicType ){
182 case DEF_LONG:
183 if(index==LITERAL_NULL||index==LITERAL_M128_0||index==LITERAL_0_255){
184 return sizeof(BYTE);
185 }
186 else if(index==LITERAL_M32768_0||index==LITERAL_0_65535){
187 return sizeof(WORD);
188 }
189 return sizeof(DWORD);
190
191 case DEF_SBYTE:
192 case DEF_BYTE:
193 case DEF_BOOLEAN:
194 return sizeof(BYTE);
195
196 case DEF_INTEGER:
197 case DEF_WORD:
198 return sizeof(WORD);
199
200 case DEF_DWORD:
201 return sizeof(DWORD);
202
203 case DEF_INT64:
204 case DEF_QWORD:
205 return sizeof(_int64);
206
207 case DEF_DOUBLE:
208 return sizeof(double);
209 case DEF_SINGLE:
210 return sizeof(float);
211 }
212
213 // ポインタ
214 if(IsPtrType(basicType)){
215 return PTR_SIZE;
216 }
217
218 // 構造体
219 if( basicType == DEF_STRUCT ){
220 if( !pClass ){
221 SetError();
222 return 0;
223 }
224
225 return pClass->GetSize();
226 }
227
228 // オブジェクト
229 if(basicType==DEF_OBJECT){
230 return PTR_SIZE;
231 }
232
233 SetError();
234 return 0;
235}
236
237bool Type::IsNull() const{
238 if( basicType == DEF_NON ){
239 return true;
240 }
241 return false;
242}
243
244bool Type::IsByte() const{
245 if( basicType == DEF_BYTE ){
246 return true;
247 }
248 return false;
249}
250bool Type::IsSByte() const{
251 if( basicType == DEF_SBYTE ){
252 return true;
253 }
254 return false;
255}
256bool Type::IsWord() const{
257 if( basicType == DEF_WORD ){
258 return true;
259 }
260 return false;
261}
262bool Type::IsInteger() const{
263 if( basicType == DEF_INTEGER ){
264 return true;
265 }
266 return false;
267}
268bool Type::IsDWord() const{
269 if( basicType == DEF_DWORD ){
270 return true;
271 }
272 return false;
273}
274bool Type::IsLong() const{
275 if( basicType == DEF_LONG ){
276 return true;
277 }
278 return false;
279}
280bool Type::IsQWord() const{
281 if( basicType == DEF_QWORD ){
282 return true;
283 }
284 return false;
285}
286bool Type::IsInt64() const{
287 if( basicType == DEF_INT64 ){
288 return true;
289 }
290 return false;
291}
292bool Type::IsSingle() const{
293 if( basicType == DEF_SINGLE ){
294 return true;
295 }
296 return false;
297}
298bool Type::IsDouble() const{
299 if( basicType == DEF_DOUBLE ){
300 return true;
301 }
302 return false;
303}
304bool Type::IsBoolean() const{
305 if( basicType == DEF_BOOLEAN ){
306 return true;
307 }
308 return false;
309}
310
311bool Type::IsPointer() const
312{
313 if(PTR_LEVEL( basicType )|| basicType == DEF_PTR_VOID || basicType == DEF_PTR_PROC
314 || ( basicType & FLAG_PTR ) ){
315 return true;
316 }
317
318 return false;
319}
320bool Type::IsSigned() const
321{
322 switch( basicType ){
323 case DEF_SBYTE:
324 case DEF_INTEGER:
325 case DEF_LONG:
326 case DEF_INT64:
327 case DEF_SINGLE:
328 case DEF_DOUBLE:
329 return true;
330 default:
331 break;
332 }
333 return false;
334}
335bool Type::IsNaturalWhole() const
336{
337 switch( basicType ){
338 case DEF_SBYTE:
339 case DEF_BYTE:
340 case DEF_INTEGER:
341 case DEF_WORD:
342 case DEF_LONG:
343 case DEF_DWORD:
344 case DEF_INT64:
345 case DEF_QWORD:
346 return true;
347 default:
348 break;
349 }
350 return false;
351}
352bool Type::IsWhole() const
353{
354 return (
355 IsNaturalWhole()
356 || IsPtrType( basicType )
357 || basicType == DEF_BOOLEAN
358 );
359}
360bool Type::IsReal() const
361{
362 switch( basicType ){
363 case DEF_SINGLE:
364 case DEF_DOUBLE:
365 return true;
366 default:
367 break;
368 }
369 return false;
370}
371bool Type::Is64() const
372{
373 switch( basicType ){
374 case DEF_QWORD:
375 case DEF_INT64:
376 return true;
377 default:
378 break;
379 }
380 return false;
381}
382bool Type::IsProcPtr() const
383{
384 if( basicType == DEF_PTR_PROC ){
385 return true;
386 }
387 return false;
388}
389bool Type::IsStruct() const
390{
391 if( basicType == DEF_STRUCT ){
392 return true;
393 }
394 return false;
395}
396bool Type::IsStructPtr() const
397{
398 if( basicType == DEF_PTR_STRUCT ){
399 return true;
400 }
401 return false;
402}
403bool Type::IsObject() const
404{
405 if( basicType == DEF_OBJECT ){
406 return true;
407 }
408 return false;
409}
410bool Type::IsObjectPtr() const
411{
412 if( basicType == DEF_PTR_OBJECT ){
413 return true;
414 }
415 return false;
416}
417bool Type::IsStringObject() const
418{
419 if( basicType == DEF_OBJECT ){
420 if( lstrcmp( pClass->name,"String")==0){
421 return true;
422 }
423 }
424 return false;
425}
426bool Type::IsVoidPtr() const
427{
428 if( basicType == DEF_PTR_VOID ){
429 return true;
430 }
431 return false;
432}
433
434bool Type::IsAny() const
435{
436 if( basicType == DEF_ANY ){
437 return true;
438 }
439 return false;
440}
441
442const string Type::ToString() const
443{
444 if( PTR_LEVEL( basicType ) ){
445 //ポインタレベルが1以上の場合
446 Type type( *this );
447 type.PtrLevelDown();
448
449 return (string)"*" + type.ToString();
450 }
451 else if( IsObject() || IsStruct() ){
452 //オブジェクトまたは構造体
453
454 if( !( index == 0 || index == -1 ) ){
455 return pClass->name;
456 }
457 }
458 else if( IsProcPtr() ){
459 if( index == 0 || index == -1 ){
460 return "VoidPtr";
461 }
462 else{
463 extern ProcPointer **ppProcPointer;
464 if( ppProcPointer[index]->ReturnType().IsNull() ){
465 return "*Sub";
466 }
467 return "*Function";
468 }
469 }
470 else{
471 // 基本型
472
473 for( int i=0; ; i++ ){
474 if( basicTypeList[i] == DEF_NON ){
475 break;
476 }
477 if( basicTypeList[i] == basicType ){
478 return basicTypeNameList[i];
479 }
480 }
481 }
482
483 extern int cp;
484 SetError(1,NULL,cp);
485
486 return (string)"(null)";
487}
488
489Type Type::String(){
490 extern CClass *pobj_StringClass;
491 if( pobj_StringClass == NULL ){
492 SetError();
493 }
494 return Type( DEF_OBJECT, *pobj_StringClass );
495}
Note: See TracBrowser for help on using the repository browser.