1 /*****************************************************************************/
5 /* Type string handling for the cc65 C compiler */
9 /* (C) 1998-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
59 /* Basic data types */
66 T_TYPE_SHORT = 0x0002,
69 T_TYPE_LONGLONG = 0x0005,
71 T_TYPE_FLOAT = 0x0007,
72 T_TYPE_DOUBLE = 0x0008,
74 T_TYPE_STRUCT = 0x000A,
75 T_TYPE_UNION = 0x000B,
76 T_TYPE_ARRAY = 0x000C,
82 T_CLASS_NONE = 0x0000,
84 T_CLASS_FLOAT = 0x0040,
86 T_CLASS_STRUCT = 0x0080,
87 T_CLASS_FUNC = 0x00A0,
88 T_MASK_CLASS = 0x00E0,
92 T_SIGN_UNSIGNED = 0x0100,
93 T_SIGN_SIGNED = 0x0200,
96 /* Type size modifiers */
98 T_SIZE_SHORT = 0x0400,
100 T_SIZE_LONGLONG = 0x0C00,
101 T_MASK_SIZE = 0x0C00,
103 /* Type qualifiers */
104 T_QUAL_NONE = 0x0000,
105 T_QUAL_CONST = 0x1000,
106 T_QUAL_VOLATILE = 0x2000,
107 T_MASK_QUAL = 0x3000,
110 T_CHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
111 T_SCHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
112 T_UCHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
113 T_SHORT = T_TYPE_SHORT | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_SHORT,
114 T_USHORT = T_TYPE_SHORT | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_SHORT,
115 T_INT = T_TYPE_INT | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
116 T_UINT = T_TYPE_INT | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
117 T_LONG = T_TYPE_LONG | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_LONG,
118 T_ULONG = T_TYPE_LONG | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_LONG,
119 T_LONGLONG = T_TYPE_LONGLONG | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_LONGLONG,
120 T_ULONGLONG = T_TYPE_LONGLONG | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_LONGLONG,
121 T_ENUM = T_TYPE_ENUM | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
122 T_FLOAT = T_TYPE_FLOAT | T_CLASS_FLOAT | T_SIGN_NONE | T_SIZE_NONE,
123 T_DOUBLE = T_TYPE_DOUBLE | T_CLASS_FLOAT | T_SIGN_NONE | T_SIZE_NONE,
124 T_VOID = T_TYPE_VOID | T_CLASS_NONE | T_SIGN_NONE | T_SIZE_NONE,
125 T_STRUCT = T_TYPE_STRUCT | T_CLASS_STRUCT | T_SIGN_NONE | T_SIZE_NONE,
126 T_UNION = T_TYPE_UNION | T_CLASS_STRUCT | T_SIGN_NONE | T_SIZE_NONE,
127 T_ARRAY = T_TYPE_ARRAY | T_CLASS_PTR | T_SIGN_NONE | T_SIZE_NONE,
128 T_PTR = T_TYPE_PTR | T_CLASS_PTR | T_SIGN_NONE | T_SIZE_NONE,
129 T_FUNC = T_TYPE_FUNC | T_CLASS_FUNC | T_SIGN_NONE | T_SIZE_NONE,
135 /* Forward for a symbol entry */
139 typedef unsigned short type;
141 /* Maximum length of a type string */
142 #define MAXTYPELEN 30
144 /* Type elements needed for Encode/Decode */
145 #define DECODE_SIZE 5
147 /* Special encodings for element counts of an array */
148 #define UNSPECIFIED -1L /* Element count was not specified */
149 #define FLEXIBLE 0L /* Flexible array struct member */
152 #define SIZEOF_CHAR 1
153 #define SIZEOF_SHORT 2
155 #define SIZEOF_LONG 4
156 #define SIZEOF_LONGLONG 8
157 #define SIZEOF_FLOAT 4
158 #define SIZEOF_DOUBLE 4
161 /* Predefined type strings */
162 extern type type_uchar [];
163 extern type type_int [];
164 extern type type_uint [];
165 extern type type_long [];
166 extern type type_ulong [];
167 extern type type_void [];
168 extern type type_size_t [];
172 /*****************************************************************************/
174 /*****************************************************************************/
178 unsigned TypeLen (const type* Type);
179 /* Return the length of the type string */
181 type* TypeCpy (type* Dest, const type* Src);
182 /* Copy a type string */
184 type* TypeCat (type* Dest, const type* Src);
187 type* TypeDup (const type* Type);
188 /* Create a copy of the given type on the heap */
190 type* TypeAlloc (unsigned Len);
191 /* Allocate memory for a type string of length Len. Len *must* include the
195 void TypeFree (type* Type);
196 /* Free a type string */
198 int SignExtendChar (int C);
199 /* Do correct sign extension of a character */
201 type GetDefaultChar (void);
202 /* Return the default char type (signed/unsigned) depending on the settings */
204 type* GetCharArrayType (unsigned Len);
205 /* Return the type for a char array of the given length */
207 type* GetImplicitFuncType (void);
208 /* Return a type string for an inplicitly declared function */
210 type* PointerTo (const type* T);
211 /* Return a type string that is "pointer to T". The type string is allocated
212 * on the heap and may be freed after use.
215 void PrintType (FILE* F, const type* Type);
216 /* Output translation of type array. */
218 void PrintRawType (FILE* F, const type* Type);
219 /* Print a type string in raw format (for debugging) */
221 void PrintFuncSig (FILE* F, const char* Name, type* Type);
222 /* Print a function signature. */
224 void Encode (type* Type, unsigned long Val);
225 /* Encode Val into the given type string */
227 void EncodePtr (type* Type, void* P);
228 /* Encode a pointer into a type array */
230 unsigned long Decode (const type* Type);
231 /* Decode an unsigned long from a type array */
233 void* DecodePtr (const type* Type);
234 /* Decode a pointer from a type array */
236 int HasEncode (const type* Type);
237 /* Return true if the given type has encoded data */
239 void CopyEncode (const type* Source, type* Target);
240 /* Copy encoded data from Source to Target */
242 #if defined(HAVE_INLINE)
243 INLINE type UnqualifiedType (type T)
244 /* Return the unqalified type */
246 return (T & ~T_MASK_QUAL);
249 # define UnqualifiedType(T) ((T) & ~T_MASK_QUAL)
252 unsigned SizeOf (const type* Type);
253 /* Compute size of object represented by type array. */
255 unsigned PSizeOf (const type* Type);
256 /* Compute size of pointer object. */
258 unsigned CheckedSizeOf (const type* T);
259 /* Return the size of a data type. If the size is zero, emit an error and
260 * return some valid size instead (so the rest of the compiler doesn't have
261 * to work with invalid sizes).
263 unsigned CheckedPSizeOf (const type* T);
264 /* Return the size of a data type that is pointed to by a pointer. If the
265 * size is zero, emit an error and return some valid size instead (so the
266 * rest of the compiler doesn't have to work with invalid sizes).
269 unsigned TypeOf (const type* Type);
270 /* Get the code generator base type of the object */
272 type* Indirect (type* Type);
273 /* Do one indirection for the given type, that is, return the type where the
274 * given type points to.
277 type* ArrayToPtr (const type* Type);
278 /* Convert an array to a pointer to it's first element */
280 #if defined(HAVE_INLINE)
281 INLINE int IsTypeChar (const type* T)
282 /* Return true if this is a character type */
284 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
287 # define IsTypeChar(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_CHAR)
290 #if defined(HAVE_INLINE)
291 INLINE int IsTypeInt (const type* T)
292 /* Return true if this is an int type (signed or unsigned) */
294 return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
297 # define IsTypeInt(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_INT)
300 #if defined(HAVE_INLINE)
301 INLINE int IsTypeLong (const type* T)
302 /* Return true if this is a long type (signed or unsigned) */
304 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
307 # define IsTypeLong(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_LONG)
310 #if defined(HAVE_INLINE)
311 INLINE int IsTypeFloat (const type* T)
312 /* Return true if this is a float type */
314 return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
317 # define IsTypeFloat(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_FLOAT)
320 #if defined(HAVE_INLINE)
321 INLINE int IsTypeDouble (const type* T)
322 /* Return true if this is a double type */
324 return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
327 # define IsTypeDouble(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_DOUBLE)
330 #if defined(HAVE_INLINE)
331 INLINE int IsTypePtr (const type* T)
332 /* Return true if this is a pointer type */
334 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
337 # define IsTypePtr(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_PTR)
340 #if defined(HAVE_INLINE)
341 INLINE int IsTypeStruct (const type* T)
342 /* Return true if this is a struct type */
344 return ((T[0] & T_MASK_TYPE) == T_TYPE_STRUCT);
347 # define IsTypeStruct(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_STRUCT)
350 #if defined(HAVE_INLINE)
351 INLINE int IsTypeUnion (const type* T)
352 /* Return true if this is a union type */
354 return ((T[0] & T_MASK_TYPE) == T_TYPE_UNION);
357 # define IsTypeUnion(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_UNION)
360 #if defined(HAVE_INLINE)
361 INLINE int IsTypeArray (const type* T)
362 /* Return true if this is an array type */
364 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
367 # define IsTypeArray(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_ARRAY)
370 #if defined(HAVE_INLINE)
371 INLINE int IsTypeVoid (const type* T)
372 /* Return true if this is a void type */
374 return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
377 # define IsTypeVoid(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_VOID)
380 #if defined(HAVE_INLINE)
381 INLINE int IsTypeFunc (const type* T)
382 /* Return true if this is a function class */
384 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
387 # define IsTypeFunc(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_FUNC)
390 #if defined(HAVE_INLINE)
391 INLINE int IsTypeFuncPtr (const type* T)
392 /* Return true if this is a function pointer */
394 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
397 # define IsTypeFuncPtr(T) \
398 ((((T)[0] & T_MASK_TYPE) == T_TYPE_PTR) && (((T)[1] & T_MASK_TYPE) == T_TYPE_FUNC))
401 int IsClassInt (const type* Type) attribute ((const));
402 /* Return true if this is an integer type */
404 int IsClassFloat (const type* Type) attribute ((const));
405 /* Return true if this is a float type */
407 int IsClassPtr (const type* Type) attribute ((const));
408 /* Return true if this is a pointer type */
410 int IsClassStruct (const type* Type) attribute ((const));
411 /* Return true if this is a struct type */
413 int IsSignUnsigned (const type* Type) attribute ((const));
414 /* Return true if this is an unsigned type */
416 int IsQualConst (const type* T) attribute ((const));
417 /* Return true if the given type has a const memory image */
419 int IsQualVolatile (const type* T) attribute ((const));
420 /* Return true if the given type has a volatile type qualifier */
422 int IsFastCallFunc (const type* T) attribute ((const));
423 /* Return true if this is a function type or pointer to function with
424 * __fastcall__ calling conventions
427 int IsVariadicFunc (const type* T) attribute ((const));
428 /* Return true if this is a function type or pointer to function type with
429 * variable parameter list
432 #if defined(HAVE_INLINE)
433 INLINE type GetType (const type* T)
434 /* Get the raw type */
436 return (T[0] & T_MASK_TYPE);
439 # define GetType(T) ((T)[0] & T_MASK_TYPE)
442 #if defined(HAVE_INLINE)
443 INLINE type GetClass (const type* T)
444 /* Get the class of a type string */
446 return (T[0] & T_MASK_CLASS);
449 # define GetClass(T) ((T)[0] & T_MASK_CLASS)
452 #if defined(HAVE_INLINE)
453 INLINE type GetSignedness (const type* T)
454 /* Get the sign of a type */
456 return (T[0] & T_MASK_SIGN);
459 # define GetSignedness(T) ((T)[0] & T_MASK_SIGN)
462 #if defined(HAVE_INLINE)
463 INLINE type GetSizeModifier (const type* T)
464 /* Get the size modifier of a type */
466 return (T[0] & T_MASK_SIZE);
469 # define GetSizeModifier(T) ((T)[0] & T_MASK_SIZE)
472 type GetQualifier (const type* T) attribute ((const));
473 /* Get the qualifier from the given type string */
475 FuncDesc* GetFuncDesc (const type* T) attribute ((const));
476 /* Get the FuncDesc pointer from a function or pointer-to-function type */
478 type* GetFuncReturn (type* T) attribute ((const));
479 /* Return a pointer to the return type of a function or pointer-to-function type */
481 long GetElementCount (const type* T);
482 /* Get the element count of the array specified in T (which must be of
486 type* GetElementType (type* T);
487 /* Return the element type of the given array type. */
491 /* End of datatype.h */