source: dev/BasicCompiler_Common/Type.cpp@ 78

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

CTypeDef → TypeDef
Houseクラスを追加。
オーバーロードレベルの種類を追加(レベル1に挿入)

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