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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
58 /* Basic data types */
65 T_TYPE_SHORT = 0x0002,
68 T_TYPE_LONGLONG = 0x0005,
70 T_TYPE_FLOAT = 0x0007,
71 T_TYPE_DOUBLE = 0x0008,
73 T_TYPE_STRUCT = 0x000A,
74 T_TYPE_UNION = 0x000B,
75 T_TYPE_ARRAY = 0x000C,
81 T_CLASS_NONE = 0x0000,
83 T_CLASS_FLOAT = 0x0040,
85 T_CLASS_STRUCT = 0x0080,
86 T_CLASS_FUNC = 0x00A0,
87 T_MASK_CLASS = 0x00E0,
91 T_SIGN_UNSIGNED = 0x0100,
92 T_SIGN_SIGNED = 0x0200,
95 /* Type size modifiers */
97 T_SIZE_SHORT = 0x0400,
99 T_SIZE_LONGLONG = 0x0C00,
100 T_MASK_SIZE = 0x0C00,
102 /* Type qualifiers */
103 T_QUAL_NONE = 0x0000,
104 T_QUAL_CONST = 0x1000,
105 T_QUAL_VOLATILE = 0x2000,
106 T_MASK_QUAL = 0x3000,
109 T_CHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
110 T_SCHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
111 T_UCHAR = T_TYPE_CHAR | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
112 T_SHORT = T_TYPE_SHORT | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_SHORT,
113 T_USHORT = T_TYPE_SHORT | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_SHORT,
114 T_INT = T_TYPE_INT | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
115 T_UINT = T_TYPE_INT | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_NONE,
116 T_LONG = T_TYPE_LONG | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_LONG,
117 T_ULONG = T_TYPE_LONG | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_LONG,
118 T_LONGLONG = T_TYPE_LONGLONG | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_LONGLONG,
119 T_ULONGLONG = T_TYPE_LONGLONG | T_CLASS_INT | T_SIGN_UNSIGNED | T_SIZE_LONGLONG,
120 T_ENUM = T_TYPE_ENUM | T_CLASS_INT | T_SIGN_SIGNED | T_SIZE_NONE,
121 T_FLOAT = T_TYPE_FLOAT | T_CLASS_FLOAT | T_SIGN_NONE | T_SIZE_NONE,
122 T_DOUBLE = T_TYPE_DOUBLE | T_CLASS_FLOAT | T_SIGN_NONE | T_SIZE_NONE,
123 T_VOID = T_TYPE_VOID | T_CLASS_NONE | T_SIGN_NONE | T_SIZE_NONE,
124 T_STRUCT = T_TYPE_STRUCT | T_CLASS_STRUCT | T_SIGN_NONE | T_SIZE_NONE,
125 T_UNION = T_TYPE_UNION | T_CLASS_STRUCT | T_SIGN_NONE | T_SIZE_NONE,
126 T_ARRAY = T_TYPE_ARRAY | T_CLASS_PTR | T_SIGN_NONE | T_SIZE_NONE,
127 T_PTR = T_TYPE_PTR | T_CLASS_PTR | T_SIGN_NONE | T_SIZE_NONE,
128 T_FUNC = T_TYPE_FUNC | T_CLASS_FUNC | T_SIGN_NONE | T_SIZE_NONE,
134 /* Forward for a symbol entry */
138 typedef unsigned short type;
140 /* Maximum length of a type string */
141 #define MAXTYPELEN 30
143 /* Type elements needed for Encode/Decode */
144 #define DECODE_SIZE 5
146 /* Predefined type strings */
147 extern type type_uchar [];
148 extern type type_int [];
149 extern type type_uint [];
150 extern type type_long [];
151 extern type type_ulong [];
152 extern type type_void [];
153 extern type type_size_t [];
157 /*****************************************************************************/
159 /*****************************************************************************/
163 unsigned TypeLen (const type* Type);
164 /* Return the length of the type string */
166 type* TypeCpy (type* Dest, const type* Src);
167 /* Copy a type string */
169 type* TypeCat (type* Dest, const type* Src);
172 type* TypeDup (const type* Type);
173 /* Create a copy of the given type on the heap */
175 type* TypeAlloc (unsigned Len);
176 /* Allocate memory for a type string of length Len. Len *must* include the
180 void TypeFree (type* Type);
181 /* Free a type string */
183 int SignExtendChar (int C);
184 /* Do correct sign extension of a character */
186 type GetDefaultChar (void);
187 /* Return the default char type (signed/unsigned) depending on the settings */
189 type* GetCharArrayType (unsigned Len);
190 /* Return the type for a char array of the given length */
192 type* GetImplicitFuncType (void);
193 /* Return a type string for an inplicitly declared function */
195 type* PointerTo (const type* T);
196 /* Return a type string that is "pointer to T". The type string is allocated
197 * on the heap and may be freed after use.
200 void PrintType (FILE* F, const type* Type);
201 /* Output translation of type array. */
203 void PrintRawType (FILE* F, const type* Type);
204 /* Print a type string in raw format (for debugging) */
206 void PrintFuncSig (FILE* F, const char* Name, type* Type);
207 /* Print a function signature. */
209 void Encode (type* Type, unsigned long Val);
210 /* Encode an unsigned long into a type array */
212 void EncodePtr (type* Type, void* P);
213 /* Encode a pointer into a type array */
215 unsigned long Decode (const type* Type);
216 /* Decode an unsigned long from a type array */
218 void* DecodePtr (const type* Type);
219 /* Decode a pointer from a type array */
221 int HasEncode (const type* Type);
222 /* Return true if the given type has encoded data */
224 void CopyEncode (const type* Source, type* Target);
225 /* Copy encoded data from Source to Target */
227 type UnqualifiedType (type T);
228 /* Return the unqalified type */
230 unsigned SizeOf (const type* Type);
231 /* Compute size of object represented by type array. */
233 unsigned PSizeOf (const type* Type);
234 /* Compute size of pointer object. */
236 unsigned CheckedSizeOf (const type* T);
237 /* Return the size of a data type. If the size is zero, emit an error and
238 * return some valid size instead (so the rest of the compiler doesn't have
239 * to work with invalid sizes).
241 unsigned CheckedPSizeOf (const type* T);
242 /* Return the size of a data type that is pointed to by a pointer. If the
243 * size is zero, emit an error and return some valid size instead (so the
244 * rest of the compiler doesn't have to work with invalid sizes).
247 unsigned TypeOf (const type* Type);
248 /* Get the code generator base type of the object */
250 type* Indirect (type* Type);
251 /* Do one indirection for the given type, that is, return the type where the
252 * given type points to.
255 int IsTypeChar (const type* T) attribute ((const));
256 /* Return true if this is a character type */
258 int IsTypeInt (const type* T) attribute ((const));
259 /* Return true if this is an int type (signed or unsigned) */
261 int IsTypeLong (const type* T) attribute ((const));
262 /* Return true if this is a long type (signed or unsigned) */
264 int IsTypeFloat (const type* T) attribute ((const));
265 /* Return true if this is a float type */
267 int IsTypeDouble (const type* T) attribute ((const));
268 /* Return true if this is a double type */
270 int IsTypePtr (const type* Type) attribute ((const));
271 /* Return true if this is a pointer type */
273 int IsTypeArray (const type* Type) attribute ((const));
274 /* Return true if this is an array type */
276 int IsTypeVoid (const type* Type) attribute ((const));
277 /* Return true if this is a void type */
279 int IsTypeFunc (const type* Type) attribute ((const));
280 /* Return true if this is a function class */
282 int IsClassInt (const type* Type) attribute ((const));
283 /* Return true if this is an integer type */
285 int IsClassFloat (const type* Type) attribute ((const));
286 /* Return true if this is a float type */
288 int IsClassPtr (const type* Type) attribute ((const));
289 /* Return true if this is a pointer type */
291 int IsClassStruct (const type* Type) attribute ((const));
292 /* Return true if this is a struct type */
294 int IsSignUnsigned (const type* Type) attribute ((const));
295 /* Return true if this is an unsigned type */
297 int IsQualConst (const type* T) attribute ((const));
298 /* Return true if the given type has a const memory image */
300 int IsQualVolatile (const type* T) attribute ((const));
301 /* Return true if the given type has a volatile type qualifier */
303 int IsFastCallFunc (const type* T) attribute ((const));
304 /* Return true if this is a function type or pointer to function with
305 * __fastcall__ calling conventions
308 int IsVariadicFunc (const type* T) attribute ((const));
309 /* Return true if this is a function type or pointer to function type with
310 * variable parameter list
313 int IsTypeFuncPtr (const type* T) attribute ((const));
314 /* Return true if this is a function pointer */
316 type GetType (const type* T) attribute ((const));
317 /* Get the raw type */
319 type GetClass (const type* T) attribute ((const));
320 /* Get the class of a type string */
322 type GetSignedness (const type* T) attribute ((const));
323 /* Get the sign of a type */
325 type GetSizeModifier (const type* T) attribute ((const));
326 /* Get the size modifier of a type */
328 type GetQualifier (const type* T) attribute ((const));
329 /* Get the qualifier from the given type string */
331 FuncDesc* GetFuncDesc (const type* T) attribute ((const));
332 /* Get the FuncDesc pointer from a function or pointer-to-function type */
334 type* GetFuncReturn (type* T) attribute ((const));
335 /* Return a pointer to the return type of a function or pointer-to-function type */
339 /* End of datatype.h */