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 1U
169 #define SIZEOF_SHORT 2U
170 #define SIZEOF_INT 2U
171 #define SIZEOF_LONG 4U
172 #define SIZEOF_LONGLONG 8U
173 #define SIZEOF_FLOAT (FP_F_Size())
174 #define SIZEOF_DOUBLE (FP_D_Size())
175 #define SIZEOF_PTR SIZEOF_INT
178 #define CHAR_BITS (8 * SIZEOF_CHAR)
179 #define SHORT_BITS (8 * SIZEOF_SHORT)
180 #define INT_BITS (8 * SIZEOF_INT)
181 #define LONG_BITS (8 * SIZEOF_LONG)
182 #define LONGLONG_BITS (8 * SIZEOF_LONGLONG)
183 #define FLOAT_BITS (8 * SIZEOF_FLOAT)
184 #define DOUBLE_BITS (8 * SIZEOF_DOUBLE)
185 #define PTR_BITS (8 * SIZEOF_PTR)
187 /* Predefined type strings */
188 extern Type type_schar[];
189 extern Type type_uchar[];
190 extern Type type_int[];
191 extern Type type_uint[];
192 extern Type type_long[];
193 extern Type type_ulong[];
194 extern Type type_void[];
195 extern Type type_size_t[];
196 extern Type type_float[];
197 extern Type type_double[];
199 /* Forward for the SymEntry struct */
204 /*****************************************************************************/
206 /*****************************************************************************/
210 unsigned TypeLen (const Type* T);
211 /* Return the length of the type string */
213 Type* TypeCopy (Type* Dest, const Type* Src);
214 /* Copy a type string */
216 Type* TypeDup (const Type* T);
217 /* Create a copy of the given type on the heap */
219 Type* TypeAlloc (unsigned Len);
220 /* Allocate memory for a type string of length Len. Len *must* include the
224 void TypeFree (Type* T);
225 /* Free a type string */
227 int SignExtendChar (int C);
228 /* Do correct sign extension of a character */
230 TypeCode GetDefaultChar (void);
231 /* Return the default char type (signed/unsigned) depending on the settings */
233 Type* GetCharArrayType (unsigned Len);
234 /* Return the type for a char array of the given length */
236 Type* GetImplicitFuncType (void);
237 /* Return a type string for an inplicitly declared function */
239 Type* PointerTo (const Type* T);
240 /* Return a type string that is "pointer to T". The type string is allocated
241 * on the heap and may be freed after use.
244 void PrintType (FILE* F, const Type* T);
245 /* Output translation of type array. */
247 void PrintRawType (FILE* F, const Type* T);
248 /* Print a type string in raw format (for debugging) */
250 void PrintFuncSig (FILE* F, const char* Name, Type* T);
251 /* Print a function signature. */
253 int TypeHasAttr (const Type* T);
254 /* Return true if the given type has attribute data */
256 #if defined(HAVE_INLINE)
257 INLINE void CopyTypeAttr (const Type* Src, Type* Dest)
258 /* Copy attribute data from Src to Dest */
263 # define CopyTypeAttr(Src, Dest) ((Dest)->A = (Src)->A)
266 #if defined(HAVE_INLINE)
267 INLINE TypeCode UnqualifiedType (TypeCode T)
268 /* Return the unqalified type code */
270 return (T & ~T_MASK_QUAL);
273 # define UnqualifiedType(T) ((T) & ~T_MASK_QUAL)
276 unsigned SizeOf (const Type* T);
277 /* Compute size of object represented by type array. */
279 unsigned PSizeOf (const Type* T);
280 /* Compute size of pointer object. */
282 unsigned CheckedSizeOf (const Type* T);
283 /* Return the size of a data type. If the size is zero, emit an error and
284 * return some valid size instead (so the rest of the compiler doesn't have
285 * to work with invalid sizes).
287 unsigned CheckedPSizeOf (const Type* T);
288 /* Return the size of a data type that is pointed to by a pointer. If the
289 * size is zero, emit an error and return some valid size instead (so the
290 * rest of the compiler doesn't have to work with invalid sizes).
293 unsigned TypeOf (const Type* T);
294 /* Get the code generator base type of the object */
296 Type* Indirect (Type* T);
297 /* Do one indirection for the given type, that is, return the type where the
298 * given type points to.
301 Type* ArrayToPtr (Type* T);
302 /* Convert an array to a pointer to it's first element */
304 #if defined(HAVE_INLINE)
305 INLINE TypeCode GetType (const Type* T)
306 /* Get the raw type */
308 return (T->C & T_MASK_TYPE);
311 # define GetType(T) ((T)->C & T_MASK_TYPE)
314 #if defined(HAVE_INLINE)
315 INLINE int IsTypeChar (const Type* T)
316 /* Return true if this is a character type */
318 return (GetType (T) == T_TYPE_CHAR);
321 # define IsTypeChar(T) (GetType (T) == T_TYPE_CHAR)
324 #if defined(HAVE_INLINE)
325 INLINE int IsTypeInt (const Type* T)
326 /* Return true if this is an int type (signed or unsigned) */
328 return (GetType (T) == T_TYPE_INT);
331 # define IsTypeInt(T) (GetType (T) == T_TYPE_INT)
334 #if defined(HAVE_INLINE)
335 INLINE int IsTypeLong (const Type* T)
336 /* Return true if this is a long type (signed or unsigned) */
338 return (GetType (T) == T_TYPE_LONG);
341 # define IsTypeLong(T) (GetType (T) == T_TYPE_LONG)
344 #if defined(HAVE_INLINE)
345 INLINE int IsTypeFloat (const Type* T)
346 /* Return true if this is a float type */
348 return (GetType (T) == T_TYPE_FLOAT);
351 # define IsTypeFloat(T) (GetType (T) == T_TYPE_FLOAT)
354 #if defined(HAVE_INLINE)
355 INLINE int IsTypeDouble (const Type* T)
356 /* Return true if this is a double type */
358 return (GetType (T) == T_TYPE_DOUBLE);
361 # define IsTypeDouble(T) (GetType (T) == T_TYPE_DOUBLE)
364 #if defined(HAVE_INLINE)
365 INLINE int IsTypePtr (const Type* T)
366 /* Return true if this is a pointer type */
368 return (GetType (T) == T_TYPE_PTR);
371 # define IsTypePtr(T) (GetType (T) == T_TYPE_PTR)
374 #if defined(HAVE_INLINE)
375 INLINE int IsTypeStruct (const Type* T)
376 /* Return true if this is a struct type */
378 return (GetType (T) == T_TYPE_STRUCT);
381 # define IsTypeStruct(T) (GetType (T) == T_TYPE_STRUCT)
384 #if defined(HAVE_INLINE)
385 INLINE int IsTypeUnion (const Type* T)
386 /* Return true if this is a union type */
388 return (GetType (T) == T_TYPE_UNION);
391 # define IsTypeUnion(T) (GetType (T) == T_TYPE_UNION)
394 #if defined(HAVE_INLINE)
395 INLINE int IsTypeArray (const Type* T)
396 /* Return true if this is an array type */
398 return (GetType (T) == T_TYPE_ARRAY);
401 # define IsTypeArray(T) (GetType (T) == T_TYPE_ARRAY)
404 #if defined(HAVE_INLINE)
405 INLINE int IsTypeVoid (const Type* T)
406 /* Return true if this is a void type */
408 return (GetType (T) == T_TYPE_VOID);
411 # define IsTypeVoid(T) (GetType (T) == T_TYPE_VOID)
414 #if defined(HAVE_INLINE)
415 INLINE int IsTypeFunc (const Type* T)
416 /* Return true if this is a function class */
418 return (GetType (T) == T_TYPE_FUNC);
421 # define IsTypeFunc(T) (GetType (T) == T_TYPE_FUNC)
424 #if defined(HAVE_INLINE)
425 INLINE int IsTypeFuncPtr (const Type* T)
426 /* Return true if this is a function pointer */
428 return (IsTypePtr (T) && IsTypeFunc (T+1));
431 # define IsTypeFuncPtr(T) (IsTypePtr (T) && IsTypeFunc (T+1))
434 #if defined(HAVE_INLINE)
435 INLINE TypeCode GetClass (const Type* T)
436 /* Get the class of a type string */
438 return (T->C & T_MASK_CLASS);
441 # define GetClass(T) ((T)->C & T_MASK_CLASS)
444 #if defined(HAVE_INLINE)
445 INLINE int IsClassInt (const Type* T)
446 /* Return true if this is an integer type */
448 return (GetClass (T) == T_CLASS_INT);
451 # define IsClassInt(T) (GetClass (T) == T_CLASS_INT)
454 #if defined(HAVE_INLINE)
455 INLINE int IsClassFloat (const Type* T)
456 /* Return true if this is a float type */
458 return (GetClass (T) == T_CLASS_FLOAT);
461 # define IsClassFloat(T) (GetClass (T) == T_CLASS_FLOAT)
464 #if defined(HAVE_INLINE)
465 INLINE int IsClassPtr (const Type* T)
466 /* Return true if this is a pointer type */
468 return (GetClass (T) == T_CLASS_PTR);
471 # define IsClassPtr(T) (GetClass (T) == T_CLASS_PTR)
474 #if defined(HAVE_INLINE)
475 INLINE int IsClassStruct (const Type* T)
476 /* Return true if this is a struct type */
478 return (GetClass (T) == T_CLASS_STRUCT);
481 # define IsClassStruct(T) (GetClass (T) == T_CLASS_STRUCT)
484 #if defined(HAVE_INLINE)
485 INLINE int IsClassFunc (const Type* T)
486 /* Return true if this is a function type */
488 return (GetClass (T) == T_CLASS_FUNC);
491 # define IsClassFunc(T) (GetClass (T) == T_CLASS_FUNC)
494 #if defined(HAVE_INLINE)
495 INLINE TypeCode GetSignedness (const Type* T)
496 /* Get the sign of a type */
498 return (T->C & T_MASK_SIGN);
501 # define GetSignedness(T) ((T)->C & T_MASK_SIGN)
504 #if defined(HAVE_INLINE)
505 INLINE int IsSignUnsigned (const Type* T)
506 /* Return true if this is an unsigned type */
508 return (GetSignedness (T) == T_SIGN_UNSIGNED);
511 # define IsSignUnsigned(T) (GetSignedness (T) == T_SIGN_UNSIGNED)
514 #if defined(HAVE_INLINE)
515 INLINE int IsSignSigned (const Type* T)
516 /* Return true if this is a signed type */
518 return (GetSignedness (T) == T_SIGN_SIGNED);
521 # define IsSignSigned(T) (GetSignedness (T) == T_SIGN_SIGNED)
524 #if defined(HAVE_INLINE)
525 INLINE TypeCode GetQualifier (const Type* T)
526 /* Get the qualifier from the given type string */
528 return (T->C & T_MASK_QUAL);
531 # define GetQualifier(T) ((T)->C & T_MASK_QUAL)
534 #if defined(HAVE_INLINE)
535 INLINE int IsQualConst (const Type* T)
536 /* Return true if the given type has a const memory image */
538 return (T->C & T_QUAL_CONST) != 0;
541 # define IsQualConst(T) (((T)->C & T_QUAL_CONST) != 0)
544 #if defined(HAVE_INLINE)
545 INLINE int IsQualVolatile (const Type* T)
546 /* Return true if the given type has a volatile type qualifier */
548 return (T->C & T_QUAL_VOLATILE) != 0;
551 # define IsQualVolatile(T) (((T)->C & T_QUAL_VOLATILE) != 0)
554 #if defined(HAVE_INLINE)
555 INLINE int IsQualRestrict (const Type* T)
556 /* Return true if the given type has a restrict qualifier */
558 return (T->C & T_QUAL_RESTRICT) != 0;
561 # define IsQualRestrict(T) (((T)->C & T_QUAL_RESTRICT) != 0)
564 #if defined(HAVE_INLINE)
565 INLINE int IsQualNear (const Type* T)
566 /* Return true if the given type has a near qualifier */
568 return (T->C & T_QUAL_NEAR) != 0;
571 # define IsQualNear(T) (((T)->C & T_QUAL_NEAR) != 0)
574 #if defined(HAVE_INLINE)
575 INLINE int IsQualFar (const Type* T)
576 /* Return true if the given type has a far qualifier */
578 return (T->C & T_QUAL_FAR) != 0;
581 # define IsQualFar(T) (((T)->C & T_QUAL_FAR) != 0)
584 #if defined(HAVE_INLINE)
585 INLINE int IsQualFastcall (const Type* T)
586 /* Return true if the given type has a fastcall qualifier */
588 return (T->C & T_QUAL_FASTCALL) != 0;
591 # define IsQualFastcall(T) (((T)->C & T_QUAL_FASTCALL) != 0)
594 int IsVariadicFunc (const Type* T) attribute ((const));
595 /* Return true if this is a function type or pointer to function type with
596 * variable parameter list
599 #if defined(HAVE_INLINE)
600 INLINE TypeCode GetSizeModifier (const Type* T)
601 /* Get the size modifier of a type */
603 return (T->C & T_MASK_SIZE);
606 # define GetSizeModifier(T) ((T)->C & T_MASK_SIZE)
609 FuncDesc* GetFuncDesc (const Type* T) attribute ((const));
610 /* Get the FuncDesc pointer from a function or pointer-to-function type */
612 void SetFuncDesc (Type* T, FuncDesc* F);
613 /* Set the FuncDesc pointer in a function or pointer-to-function type */
615 Type* GetFuncReturn (Type* T) attribute ((const));
616 /* Return a pointer to the return type of a function or pointer-to-function type */
618 long GetElementCount (const Type* T);
619 /* Get the element count of the array specified in T (which must be of
623 void SetElementCount (Type* T, long Count);
624 /* Set the element count of the array specified in T (which must be of
628 Type* GetElementType (Type* T);
629 /* Return the element type of the given array type. */
631 struct SymEntry* GetSymEntry (const Type* T) attribute ((const));
632 /* Return a SymEntry pointer from a type */
634 void SetSymEntry (Type* T, struct SymEntry* S);
635 /* Set the SymEntry pointer for a type */
637 Type* IntPromotion (Type* T);
638 /* Apply the integer promotions to T and return the result. The returned type
639 * string may be T if there is no need to change it.
642 Type* PtrConversion (Type* T);
643 /* If the type is a function, convert it to pointer to function. If the
644 * expression is an array, convert it to pointer to first element. Otherwise
648 TypeCode AddrSizeQualifier (unsigned AddrSize);
649 /* Return T_QUAL_NEAR or T_QUAL_FAR depending on the address size */
651 #if defined(HAVE_INLINE)
652 INLINE TypeCode CodeAddrSizeQualifier (void)
653 /* Return T_QUAL_NEAR or T_QUAL_FAR depending on the code address size */
655 return AddrSizeQualifier (CodeAddrSize);
658 # define CodeAddrSizeQualifier() (AddrSizeQualifier (CodeAddrSize))
661 #if defined(HAVE_INLINE)
662 INLINE TypeCode DataAddrSizeQualifier (void)
663 /* Return T_QUAL_NEAR or T_QUAL_FAR depending on the data address size */
665 return AddrSizeQualifier (DataAddrSize);
668 # define DataAddrSizeQualifier() (AddrSizeQualifier (DataAddrSize))
673 /* End of datatype.h */