1 /*****************************************************************************/
5 /* Type string handling for the cc65 C compiler */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
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,
137 /* Forward for a symbol entry */
141 typedef unsigned short type;
143 /* Maximum length of a type string */
144 #define MAXTYPELEN 30
146 /* Type elements needed for Encode/Decode */
147 #define DECODE_SIZE 5
149 /* Special encodings for element counts of an array */
150 #define UNSPECIFIED -1L /* Element count was not specified */
151 #define FLEXIBLE 0L /* Flexible array struct member */
154 #define SIZEOF_CHAR 1
155 #define SIZEOF_SHORT 2
157 #define SIZEOF_LONG 4
158 #define SIZEOF_LONGLONG 8
159 #define SIZEOF_FLOAT 4
160 #define SIZEOF_DOUBLE 4
163 /* Predefined type strings */
164 extern type type_uchar [];
165 extern type type_int [];
166 extern type type_uint [];
167 extern type type_long [];
168 extern type type_ulong [];
169 extern type type_void [];
170 extern type type_size_t [];
174 /*****************************************************************************/
176 /*****************************************************************************/
180 unsigned TypeLen (const type* Type);
181 /* Return the length of the type string */
183 type* TypeCpy (type* Dest, const type* Src);
184 /* Copy a type string */
186 type* TypeCat (type* Dest, const type* Src);
189 type* TypeDup (const type* Type);
190 /* Create a copy of the given type on the heap */
192 type* TypeAlloc (unsigned Len);
193 /* Allocate memory for a type string of length Len. Len *must* include the
197 void TypeFree (type* Type);
198 /* Free a type string */
200 int SignExtendChar (int C);
201 /* Do correct sign extension of a character */
203 type GetDefaultChar (void);
204 /* Return the default char type (signed/unsigned) depending on the settings */
206 type* GetCharArrayType (unsigned Len);
207 /* Return the type for a char array of the given length */
209 type* GetImplicitFuncType (void);
210 /* Return a type string for an inplicitly declared function */
212 type* PointerTo (const type* T);
213 /* Return a type string that is "pointer to T". The type string is allocated
214 * on the heap and may be freed after use.
217 void PrintType (FILE* F, const type* Type);
218 /* Output translation of type array. */
220 void PrintRawType (FILE* F, const type* Type);
221 /* Print a type string in raw format (for debugging) */
223 void PrintFuncSig (FILE* F, const char* Name, type* Type);
224 /* Print a function signature. */
226 void Encode (type* Type, unsigned long Val);
227 /* Encode Val into the given type string */
229 void EncodePtr (type* Type, void* P);
230 /* Encode a pointer into a type array */
232 unsigned long Decode (const type* Type);
233 /* Decode an unsigned long from a type array */
235 void* DecodePtr (const type* Type);
236 /* Decode a pointer from a type array */
238 int HasEncode (const type* Type);
239 /* Return true if the given type has encoded data */
241 void CopyEncode (const type* Source, type* Target);
242 /* Copy encoded data from Source to Target */
244 #if defined(HAVE_INLINE)
245 INLINE type UnqualifiedType (type T)
246 /* Return the unqalified type */
248 return (T & ~T_MASK_QUAL);
251 # define UnqualifiedType(T) ((T) & ~T_MASK_QUAL)
254 unsigned SizeOf (const type* Type);
255 /* Compute size of object represented by type array. */
257 unsigned PSizeOf (const type* Type);
258 /* Compute size of pointer object. */
260 unsigned CheckedSizeOf (const type* T);
261 /* Return the size of a data type. If the size is zero, emit an error and
262 * return some valid size instead (so the rest of the compiler doesn't have
263 * to work with invalid sizes).
265 unsigned CheckedPSizeOf (const type* T);
266 /* Return the size of a data type that is pointed to by a pointer. If the
267 * size is zero, emit an error and return some valid size instead (so the
268 * rest of the compiler doesn't have to work with invalid sizes).
271 unsigned TypeOf (const type* Type);
272 /* Get the code generator base type of the object */
274 type* Indirect (type* Type);
275 /* Do one indirection for the given type, that is, return the type where the
276 * given type points to.
279 type* ArrayToPtr (const type* Type);
280 /* Convert an array to a pointer to it's first element */
282 #if defined(HAVE_INLINE)
283 INLINE int IsTypeChar (const type* T)
284 /* Return true if this is a character type */
286 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
289 # define IsTypeChar(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_CHAR)
292 #if defined(HAVE_INLINE)
293 INLINE int IsTypeInt (const type* T)
294 /* Return true if this is an int type (signed or unsigned) */
296 return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
299 # define IsTypeInt(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_INT)
302 #if defined(HAVE_INLINE)
303 INLINE int IsTypeLong (const type* T)
304 /* Return true if this is a long type (signed or unsigned) */
306 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
309 # define IsTypeLong(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_LONG)
312 #if defined(HAVE_INLINE)
313 INLINE int IsTypeFloat (const type* T)
314 /* Return true if this is a float type */
316 return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
319 # define IsTypeFloat(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_FLOAT)
322 #if defined(HAVE_INLINE)
323 INLINE int IsTypeDouble (const type* T)
324 /* Return true if this is a double type */
326 return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
329 # define IsTypeDouble(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_DOUBLE)
332 #if defined(HAVE_INLINE)
333 INLINE int IsTypePtr (const type* T)
334 /* Return true if this is a pointer type */
336 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
339 # define IsTypePtr(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_PTR)
342 #if defined(HAVE_INLINE)
343 INLINE int IsTypeStruct (const type* T)
344 /* Return true if this is a struct type */
346 return ((T[0] & T_MASK_TYPE) == T_TYPE_STRUCT);
349 # define IsTypeStruct(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_STRUCT)
352 #if defined(HAVE_INLINE)
353 INLINE int IsTypeUnion (const type* T)
354 /* Return true if this is a union type */
356 return ((T[0] & T_MASK_TYPE) == T_TYPE_UNION);
359 # define IsTypeUnion(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_UNION)
362 #if defined(HAVE_INLINE)
363 INLINE int IsTypeArray (const type* T)
364 /* Return true if this is an array type */
366 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
369 # define IsTypeArray(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_ARRAY)
372 #if defined(HAVE_INLINE)
373 INLINE int IsTypeVoid (const type* T)
374 /* Return true if this is a void type */
376 return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
379 # define IsTypeVoid(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_VOID)
382 #if defined(HAVE_INLINE)
383 INLINE int IsTypeFunc (const type* T)
384 /* Return true if this is a function class */
386 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
389 # define IsTypeFunc(T) (((T)[0] & T_MASK_TYPE) == T_TYPE_FUNC)
392 #if defined(HAVE_INLINE)
393 INLINE int IsTypeFuncPtr (const type* T)
394 /* Return true if this is a function pointer */
396 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
399 # define IsTypeFuncPtr(T) \
400 ((((T)[0] & T_MASK_TYPE) == T_TYPE_PTR) && (((T)[1] & T_MASK_TYPE) == T_TYPE_FUNC))
403 int IsClassInt (const type* Type) attribute ((const));
404 /* Return true if this is an integer type */
406 int IsClassFloat (const type* Type) attribute ((const));
407 /* Return true if this is a float type */
409 int IsClassPtr (const type* Type) attribute ((const));
410 /* Return true if this is a pointer type */
412 int IsClassStruct (const type* Type) attribute ((const));
413 /* Return true if this is a struct type */
415 int IsSignUnsigned (const type* Type) attribute ((const));
416 /* Return true if this is an unsigned type */
418 int IsQualConst (const type* T) attribute ((const));
419 /* Return true if the given type has a const memory image */
421 int IsQualVolatile (const type* T) attribute ((const));
422 /* Return true if the given type has a volatile type qualifier */
424 int IsFastCallFunc (const type* T) attribute ((const));
425 /* Return true if this is a function type or pointer to function with
426 * __fastcall__ calling conventions
429 int IsVariadicFunc (const type* T) attribute ((const));
430 /* Return true if this is a function type or pointer to function type with
431 * variable parameter list
434 #if defined(HAVE_INLINE)
435 INLINE type GetType (const type* T)
436 /* Get the raw type */
438 return (T[0] & T_MASK_TYPE);
441 # define GetType(T) ((T)[0] & T_MASK_TYPE)
444 #if defined(HAVE_INLINE)
445 INLINE type GetClass (const type* T)
446 /* Get the class of a type string */
448 return (T[0] & T_MASK_CLASS);
451 # define GetClass(T) ((T)[0] & T_MASK_CLASS)
454 #if defined(HAVE_INLINE)
455 INLINE type GetSignedness (const type* T)
456 /* Get the sign of a type */
458 return (T[0] & T_MASK_SIGN);
461 # define GetSignedness(T) ((T)[0] & T_MASK_SIGN)
464 #if defined(HAVE_INLINE)
465 INLINE type GetSizeModifier (const type* T)
466 /* Get the size modifier of a type */
468 return (T[0] & T_MASK_SIZE);
471 # define GetSizeModifier(T) ((T)[0] & T_MASK_SIZE)
474 type GetQualifier (const type* T) attribute ((const));
475 /* Get the qualifier from the given type string */
477 FuncDesc* GetFuncDesc (const type* T) attribute ((const));
478 /* Get the FuncDesc pointer from a function or pointer-to-function type */
480 type* GetFuncReturn (type* T) attribute ((const));
481 /* Return a pointer to the return type of a function or pointer-to-function type */
483 long GetElementCount (const type* T);
484 /* Get the element count of the array specified in T (which must be of
488 type* GetElementType (type* T);
489 /* Return the element type of the given array type. */
493 /* End of datatype.h */