1 /*****************************************************************************/
5 /* Type string handling for the cc65 C compiler */
9 /* (C) 1998-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
60 /* Basic data types */
65 T_TYPE_NONE = 0x000000,
66 T_TYPE_CHAR = 0x000001,
67 T_TYPE_SHORT = 0x000002,
68 T_TYPE_INT = 0x000003,
69 T_TYPE_LONG = 0x000004,
70 T_TYPE_LONGLONG = 0x000005,
71 T_TYPE_ENUM = 0x000006,
72 T_TYPE_FLOAT = 0x000007,
73 T_TYPE_DOUBLE = 0x000008,
74 T_TYPE_VOID = 0x000009,
75 T_TYPE_STRUCT = 0x00000A,
76 T_TYPE_UNION = 0x00000B,
77 T_TYPE_ARRAY = 0x00000C,
78 T_TYPE_PTR = 0x00000D,
79 T_TYPE_FUNC = 0x00000E,
80 T_MASK_TYPE = 0x00000F,
83 T_CLASS_NONE = 0x000000,
84 T_CLASS_INT = 0x000010,
85 T_CLASS_FLOAT = 0x000020,
86 T_CLASS_PTR = 0x000030,
87 T_CLASS_STRUCT = 0x000040,
88 T_CLASS_FUNC = 0x000050,
89 T_MASK_CLASS = 0x000070,
92 T_SIGN_NONE = 0x000000,
93 T_SIGN_UNSIGNED = 0x000080,
94 T_SIGN_SIGNED = 0x000100,
95 T_MASK_SIGN = 0x000180,
97 /* Type size modifiers */
98 T_SIZE_NONE = 0x000000,
99 T_SIZE_SHORT = 0x000200,
100 T_SIZE_LONG = 0x000400,
101 T_SIZE_LONGLONG = 0x000600,
102 T_MASK_SIZE = 0x000600,
104 /* Type qualifiers */
105 T_QUAL_NONE = 0x000000,
106 T_QUAL_CONST = 0x000800,
107 T_QUAL_VOLATILE = 0x001000,
108 T_QUAL_RESTRICT = 0x002000,
109 T_QUAL_NEAR = 0x004000,
110 T_QUAL_FAR = 0x008000,
111 T_QUAL_ADDRSIZE = T_QUAL_NEAR | T_QUAL_FAR,
112 T_QUAL_FASTCALL = 0x010000,
113 T_MASK_QUAL = 0x01F800,
116 T_CHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
117 T_SCHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
118 T_UCHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
119 T_SHORT = T_TYPE_SHORT | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_SHORT,
120 T_USHORT = T_TYPE_SHORT | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_SHORT,
121 T_INT = T_TYPE_INT | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
122 T_UINT = T_TYPE_INT | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
123 T_LONG = T_TYPE_LONG | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_LONG,
124 T_ULONG = T_TYPE_LONG | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_LONG,
125 T_LONGLONG = T_TYPE_LONGLONG | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_LONGLONG,
126 T_ULONGLONG = T_TYPE_LONGLONG | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_LONGLONG,
127 T_ENUM = T_TYPE_ENUM | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
128 T_FLOAT = T_TYPE_FLOAT | T_CLASS_FLOAT | T_SIGN_NONE | T_SIZE_NONE,
129 T_DOUBLE = T_TYPE_DOUBLE | T_CLASS_FLOAT | T_SIGN_NONE | T_SIZE_NONE,
130 T_VOID = T_TYPE_VOID | T_CLASS_NONE | T_SIGN_NONE | T_SIZE_NONE,
131 T_STRUCT = T_TYPE_STRUCT | T_CLASS_STRUCT | T_SIGN_NONE | T_SIZE_NONE,
132 T_UNION = T_TYPE_UNION | T_CLASS_STRUCT | T_SIGN_NONE | T_SIZE_NONE,
133 T_ARRAY = T_TYPE_ARRAY | T_CLASS_PTR | T_SIGN_NONE | T_SIZE_NONE,
134 T_PTR = T_TYPE_PTR | T_CLASS_PTR | T_SIGN_NONE | T_SIZE_NONE,
135 T_FUNC = T_TYPE_FUNC | T_CLASS_FUNC | T_SIGN_NONE | T_SIZE_NONE,
143 /* Type code entry */
144 typedef unsigned long TypeCode;
147 typedef struct Type Type;
149 TypeCode C; /* Code for this entry */
151 void* P; /* Arbitrary attribute pointer */
152 long L; /* Numeric attribute value */
153 unsigned long U; /* Dito, unsigned */
154 } A; /* Type attribute if necessary */
157 /* A macro that expands to a full initializer for struct Type */
158 #define TYPE(T) { (T), { 0 } }
160 /* Maximum length of a type string */
161 #define MAXTYPELEN 30
163 /* Special encodings for element counts of an array */
164 #define UNSPECIFIED -1L /* Element count was not specified */
165 #define FLEXIBLE 0L /* Flexible array struct member */
167 /* Sizes. Floating point sizes come from fp.h */
168 #define SIZEOF_CHAR 1
169 #define SIZEOF_SHORT 2
171 #define SIZEOF_LONG 4
172 #define SIZEOF_LONGLONG 8
173 #define SIZEOF_FLOAT (FP_F_Size())
174 #define SIZEOF_DOUBLE (FP_D_Size())
177 /* Predefined type strings */
178 extern Type type_schar[];
179 extern Type type_uchar[];
180 extern Type type_int[];
181 extern Type type_uint[];
182 extern Type type_long[];
183 extern Type type_ulong[];
184 extern Type type_void[];
185 extern Type type_size_t[];
186 extern Type type_float[];
187 extern Type type_double[];
189 /* Forward for the SymEntry struct */
194 /*****************************************************************************/
196 /*****************************************************************************/
200 unsigned TypeLen (const Type* T);
201 /* Return the length of the type string */
203 Type* TypeCopy (Type* Dest, const Type* Src);
204 /* Copy a type string */
206 Type* TypeDup (const Type* T);
207 /* Create a copy of the given type on the heap */
209 Type* TypeAlloc (unsigned Len);
210 /* Allocate memory for a type string of length Len. Len *must* include the
214 void TypeFree (Type* T);
215 /* Free a type string */
217 int SignExtendChar (int C);
218 /* Do correct sign extension of a character */
220 TypeCode GetDefaultChar (void);
221 /* Return the default char type (signed/unsigned) depending on the settings */
223 Type* GetCharArrayType (unsigned Len);
224 /* Return the type for a char array of the given length */
226 Type* GetImplicitFuncType (void);
227 /* Return a type string for an inplicitly declared function */
229 Type* PointerTo (const Type* T);
230 /* Return a type string that is "pointer to T". The type string is allocated
231 * on the heap and may be freed after use.
234 void PrintType (FILE* F, const Type* T);
235 /* Output translation of type array. */
237 void PrintRawType (FILE* F, const Type* T);
238 /* Print a type string in raw format (for debugging) */
240 void PrintFuncSig (FILE* F, const char* Name, Type* T);
241 /* Print a function signature. */
243 int TypeHasAttr (const Type* T);
244 /* Return true if the given type has attribute data */
246 #if defined(HAVE_INLINE)
247 INLINE void CopyTypeAttr (const Type* Src, Type* Dest)
248 /* Copy attribute data from Src to Dest */
253 # define CopyTypeAttr(Src, Dest) ((Dest)->A = (Src)->A)
256 #if defined(HAVE_INLINE)
257 INLINE TypeCode UnqualifiedType (TypeCode T)
258 /* Return the unqalified type code */
260 return (T & ~T_MASK_QUAL);
263 # define UnqualifiedType(T) ((T) & ~T_MASK_QUAL)
266 unsigned SizeOf (const Type* T);
267 /* Compute size of object represented by type array. */
269 unsigned PSizeOf (const Type* T);
270 /* Compute size of pointer object. */
272 unsigned CheckedSizeOf (const Type* T);
273 /* Return the size of a data type. If the size is zero, emit an error and
274 * return some valid size instead (so the rest of the compiler doesn't have
275 * to work with invalid sizes).
277 unsigned CheckedPSizeOf (const Type* T);
278 /* Return the size of a data type that is pointed to by a pointer. If the
279 * size is zero, emit an error and return some valid size instead (so the
280 * rest of the compiler doesn't have to work with invalid sizes).
283 unsigned TypeOf (const Type* T);
284 /* Get the code generator base type of the object */
286 Type* Indirect (Type* T);
287 /* Do one indirection for the given type, that is, return the type where the
288 * given type points to.
291 Type* ArrayToPtr (const Type* T);
292 /* Convert an array to a pointer to it's first element */
294 #if defined(HAVE_INLINE)
295 INLINE TypeCode GetType (const Type* T)
296 /* Get the raw type */
298 return (T->C & T_MASK_TYPE);
301 # define GetType(T) ((T)->C & T_MASK_TYPE)
304 #if defined(HAVE_INLINE)
305 INLINE int IsTypeChar (const Type* T)
306 /* Return true if this is a character type */
308 return (GetType (T) == T_TYPE_CHAR);
311 # define IsTypeChar(T) (GetType (T) == T_TYPE_CHAR)
314 #if defined(HAVE_INLINE)
315 INLINE int IsTypeInt (const Type* T)
316 /* Return true if this is an int type (signed or unsigned) */
318 return (GetType (T) == T_TYPE_INT);
321 # define IsTypeInt(T) (GetType (T) == T_TYPE_INT)
324 #if defined(HAVE_INLINE)
325 INLINE int IsTypeLong (const Type* T)
326 /* Return true if this is a long type (signed or unsigned) */
328 return (GetType (T) == T_TYPE_LONG);
331 # define IsTypeLong(T) (GetType (T) == T_TYPE_LONG)
334 #if defined(HAVE_INLINE)
335 INLINE int IsTypeFloat (const Type* T)
336 /* Return true if this is a float type */
338 return (GetType (T) == T_TYPE_FLOAT);
341 # define IsTypeFloat(T) (GetType (T) == T_TYPE_FLOAT)
344 #if defined(HAVE_INLINE)
345 INLINE int IsTypeDouble (const Type* T)
346 /* Return true if this is a double type */
348 return (GetType (T) == T_TYPE_DOUBLE);
351 # define IsTypeDouble(T) (GetType (T) == T_TYPE_DOUBLE)
354 #if defined(HAVE_INLINE)
355 INLINE int IsTypePtr (const Type* T)
356 /* Return true if this is a pointer type */
358 return (GetType (T) == T_TYPE_PTR);
361 # define IsTypePtr(T) (GetType (T) == T_TYPE_PTR)
364 #if defined(HAVE_INLINE)
365 INLINE int IsTypeStruct (const Type* T)
366 /* Return true if this is a struct type */
368 return (GetType (T) == T_TYPE_STRUCT);
371 # define IsTypeStruct(T) (GetType (T) == T_TYPE_STRUCT)
374 #if defined(HAVE_INLINE)
375 INLINE int IsTypeUnion (const Type* T)
376 /* Return true if this is a union type */
378 return (GetType (T) == T_TYPE_UNION);
381 # define IsTypeUnion(T) (GetType (T) == T_TYPE_UNION)
384 #if defined(HAVE_INLINE)
385 INLINE int IsTypeArray (const Type* T)
386 /* Return true if this is an array type */
388 return (GetType (T) == T_TYPE_ARRAY);
391 # define IsTypeArray(T) (GetType (T) == T_TYPE_ARRAY)
394 #if defined(HAVE_INLINE)
395 INLINE int IsTypeVoid (const Type* T)
396 /* Return true if this is a void type */
398 return (GetType (T) == T_TYPE_VOID);
401 # define IsTypeVoid(T) (GetType (T) == T_TYPE_VOID)
404 #if defined(HAVE_INLINE)
405 INLINE int IsTypeFunc (const Type* T)
406 /* Return true if this is a function class */
408 return (GetType (T) == T_TYPE_FUNC);
411 # define IsTypeFunc(T) (GetType (T) == T_TYPE_FUNC)
414 #if defined(HAVE_INLINE)
415 INLINE int IsTypeFuncPtr (const Type* T)
416 /* Return true if this is a function pointer */
418 return (IsTypePtr (T) && IsTypeFunc (T+1));
421 # define IsTypeFuncPtr(T) (IsTypePtr (T) && IsTypeFunc (T+1))
424 #if defined(HAVE_INLINE)
425 INLINE TypeCode GetClass (const Type* T)
426 /* Get the class of a type string */
428 return (T->C & T_MASK_CLASS);
431 # define GetClass(T) ((T)->C & T_MASK_CLASS)
434 #if defined(HAVE_INLINE)
435 INLINE int IsClassInt (const Type* T)
436 /* Return true if this is an integer type */
438 return (GetClass (T) == T_CLASS_INT);
441 # define IsClassInt(T) (GetClass (T) == T_CLASS_INT)
444 #if defined(HAVE_INLINE)
445 INLINE int IsClassFloat (const Type* T)
446 /* Return true if this is a float type */
448 return (GetClass (T) == T_CLASS_FLOAT);
451 # define IsClassFloat(T) (GetClass (T) == T_CLASS_FLOAT)
454 #if defined(HAVE_INLINE)
455 INLINE int IsClassPtr (const Type* T)
456 /* Return true if this is a pointer type */
458 return (GetClass (T) == T_CLASS_PTR);
461 # define IsClassPtr(T) (GetClass (T) == T_CLASS_PTR)
464 #if defined(HAVE_INLINE)
465 INLINE int IsClassStruct (const Type* T)
466 /* Return true if this is a struct type */
468 return (GetClass (T) == T_CLASS_STRUCT);
471 # define IsClassStruct(T) (GetClass (T) == T_CLASS_STRUCT)
474 #if defined(HAVE_INLINE)
475 INLINE int IsClassFunc (const Type* T)
476 /* Return true if this is a function type */
478 return (GetClass (T) == T_CLASS_FUNC);
481 # define IsClassFunc(T) (GetClass (T) == T_CLASS_FUNC)
484 #if defined(HAVE_INLINE)
485 INLINE TypeCode GetSignedness (const Type* T)
486 /* Get the sign of a type */
488 return (T->C & T_MASK_SIGN);
491 # define GetSignedness(T) ((T)->C & T_MASK_SIGN)
494 #if defined(HAVE_INLINE)
495 INLINE int IsSignUnsigned (const Type* T)
496 /* Return true if this is an unsigned type */
498 return (GetSignedness (T) == T_SIGN_UNSIGNED);
501 # define IsSignUnsigned(T) (GetSignedness (T) == T_SIGN_UNSIGNED)
504 #if defined(HAVE_INLINE)
505 INLINE int IsSignSigned (const Type* T)
506 /* Return true if this is a signed type */
508 return (GetSignedness (T) == T_SIGN_SIGNED);
511 # define IsSignSigned(T) (GetSignedness (T) == T_SIGN_SIGNED)
514 #if defined(HAVE_INLINE)
515 INLINE TypeCode GetQualifier (const Type* T)
516 /* Get the qualifier from the given type string */
518 return (T->C & T_MASK_QUAL);
521 # define GetQualifier(T) ((T)->C & T_MASK_QUAL)
524 #if defined(HAVE_INLINE)
525 INLINE int IsQualConst (const Type* T)
526 /* Return true if the given type has a const memory image */
528 return (T->C & T_QUAL_CONST) != 0;
531 # define IsQualConst(T) ((T->C & T_QUAL_CONST) != 0)
534 #if defined(HAVE_INLINE)
535 INLINE int IsQualVolatile (const Type* T)
536 /* Return true if the given type has a volatile type qualifier */
538 return (T->C & T_QUAL_VOLATILE) != 0;
541 # define IsQualVolatile(T) (T->C & T_QUAL_VOLATILE) != 0)
544 #if defined(HAVE_INLINE)
545 INLINE int IsQualRestrict (const Type* T)
546 /* Return true if the given type has a restrict qualifier */
548 return (T->C & T_QUAL_RESTRICT) != 0;
551 # define IsQualRestrict(T) (T->C & T_QUAL_RESTRICT) != 0)
554 #if defined(HAVE_INLINE)
555 INLINE int IsQualNear (const Type* T)
556 /* Return true if the given type has a near qualifier */
558 return (T->C & T_QUAL_NEAR) != 0;
561 # define IsQualNear(T) (T->C & T_QUAL_NEAR) != 0)
564 #if defined(HAVE_INLINE)
565 INLINE int IsQualFar (const Type* T)
566 /* Return true if the given type has a far qualifier */
568 return (T->C & T_QUAL_FAR) != 0;
571 # define IsQualFar(T) (T->C & T_QUAL_FAR) != 0)
574 #if defined(HAVE_INLINE)
575 INLINE int IsQualFastcall (const Type* T)
576 /* Return true if the given type has a fastcall qualifier */
578 return (T->C & T_QUAL_FASTCALL) != 0;
581 # define IsQualFastcall(T) (T->C & T_QUAL_FASTCALL) != 0)
584 int IsVariadicFunc (const Type* T) attribute ((const));
585 /* Return true if this is a function type or pointer to function type with
586 * variable parameter list
589 #if defined(HAVE_INLINE)
590 INLINE TypeCode GetSizeModifier (const Type* T)
591 /* Get the size modifier of a type */
593 return (T->C & T_MASK_SIZE);
596 # define GetSizeModifier(T) ((T)->C & T_MASK_SIZE)
599 FuncDesc* GetFuncDesc (const Type* T) attribute ((const));
600 /* Get the FuncDesc pointer from a function or pointer-to-function type */
602 void SetFuncDesc (Type* T, FuncDesc* F);
603 /* Set the FuncDesc pointer in a function or pointer-to-function type */
605 Type* GetFuncReturn (Type* T) attribute ((const));
606 /* Return a pointer to the return type of a function or pointer-to-function type */
608 long GetElementCount (const Type* T);
609 /* Get the element count of the array specified in T (which must be of
613 void SetElementCount (Type* T, long Count);
614 /* Set the element count of the array specified in T (which must be of
618 Type* GetElementType (Type* T);
619 /* Return the element type of the given array type. */
621 struct SymEntry* GetSymEntry (const Type* T) attribute ((const));
622 /* Return a SymEntry pointer from a type */
624 void SetSymEntry (Type* T, struct SymEntry* S);
625 /* Set the SymEntry pointer for a type */
627 Type* IntPromotion (Type* T);
628 /* Apply the integer promotions to T and return the result. The returned type
629 * string may be T if there is no need to change it.
632 Type* PtrConversion (Type* T);
633 /* If the type is a function, convert it to pointer to function. If the
634 * expression is an array, convert it to pointer to first element. Otherwise
638 TypeCode AddrSizeQualifier (unsigned AddrSize);
639 /* Return T_QUAL_NEAR or T_QUAL_FAR depending on the address size */
641 #if defined(HAVE_INLINE)
642 INLINE TypeCode CodeAddrSizeQualifier (void)
643 /* Return T_QUAL_NEAR or T_QUAL_FAR depending on the code address size */
645 return AddrSizeQualifier (CodeAddrSize);
648 # define CodeAddrSizeQualifier() (AddrSizeQualifier (CodeAddrSize))
651 #if defined(HAVE_INLINE)
652 INLINE TypeCode DataAddrSizeQualifier (void)
653 /* Return T_QUAL_NEAR or T_QUAL_FAR depending on the data address size */
655 return AddrSizeQualifier (DataAddrSize);
658 # define DataAddrSizeQualifier() (AddrSizeQualifier (DataAddrSize))
663 /* End of datatype.h */