]> git.sur5r.net Git - cc65/blob - src/cc65/datatype.h
c5a93dc4d34ca616d1e886aa8425bc8c6c3d734d
[cc65] / src / cc65 / datatype.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                datatype.h                                 */
4 /*                                                                           */
5 /*               Type string handling for the cc65 C compiler                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #ifndef DATATYPE_H
37 #define DATATYPE_H
38
39
40
41 #include <stdio.h>
42
43 /* common */
44 #include "attrib.h"
45 #include "inline.h"
46
47 /* cc65 */
48 #include "funcdesc.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58
59 /* Basic data types */
60 enum {
61     T_END           = 0x0000,
62
63     /* Basic types */
64     T_TYPE_NONE     = 0x0000,
65     T_TYPE_CHAR     = 0x0001,
66     T_TYPE_SHORT    = 0x0002,
67     T_TYPE_INT      = 0x0003,
68     T_TYPE_LONG     = 0x0004,
69     T_TYPE_LONGLONG = 0x0005,
70     T_TYPE_ENUM     = 0x0006,
71     T_TYPE_FLOAT    = 0x0007,
72     T_TYPE_DOUBLE   = 0x0008,
73     T_TYPE_VOID     = 0x0009,
74     T_TYPE_STRUCT   = 0x000A,
75     T_TYPE_UNION    = 0x000B,
76     T_TYPE_ARRAY    = 0x000C,
77     T_TYPE_PTR      = 0x000D,
78     T_TYPE_FUNC     = 0x000E,
79     T_MASK_TYPE     = 0x001F,
80
81     /* Type classes */
82     T_CLASS_NONE    = 0x0000,
83     T_CLASS_INT     = 0x0020,
84     T_CLASS_FLOAT   = 0x0040,
85     T_CLASS_PTR     = 0x0060,
86     T_CLASS_STRUCT  = 0x0080,
87     T_CLASS_FUNC    = 0x00A0,
88     T_MASK_CLASS    = 0x00E0,
89
90     /* Type signedness */
91     T_SIGN_NONE     = 0x0000,
92     T_SIGN_UNSIGNED = 0x0100,
93     T_SIGN_SIGNED   = 0x0200,
94     T_MASK_SIGN     = 0x0300,
95
96     /* Type size modifiers */
97     T_SIZE_NONE     = 0x0000,
98     T_SIZE_SHORT    = 0x0400,
99     T_SIZE_LONG     = 0x0800,
100     T_SIZE_LONGLONG = 0x0C00,
101     T_MASK_SIZE     = 0x0C00,
102
103     /* Type qualifiers */
104     T_QUAL_NONE     = 0x0000,
105     T_QUAL_CONST    = 0x1000,
106     T_QUAL_VOLATILE = 0x2000,
107     T_MASK_QUAL     = 0x3000,
108
109     /* Types */
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,
130
131 };
132
133
134
135 /* Forward for a symbol entry */
136 struct SymEntry;
137
138 /* Type entry */
139 typedef unsigned short type;
140
141 /* Maximum length of a type string */
142 #define MAXTYPELEN      30
143
144 /* Type elements needed for Encode/Decode */
145 #define DECODE_SIZE     5
146
147 /* Sizes */
148 #define SIZEOF_CHAR     1
149 #define SIZEOF_SHORT    2
150 #define SIZEOF_INT      2
151 #define SIZEOF_LONG     4
152 #define SIZEOF_LONGLONG 8
153 #define SIZEOF_FLOAT    4
154 #define SIZEOF_DOUBLE   4
155 #define SIZEOF_PTR      2
156
157 /* Predefined type strings */
158 extern type type_uchar [];
159 extern type type_int [];
160 extern type type_uint [];
161 extern type type_long [];
162 extern type type_ulong [];
163 extern type type_void [];
164 extern type type_size_t [];
165
166
167
168 /*****************************************************************************/
169 /*                                   Code                                    */
170 /*****************************************************************************/
171
172
173
174 unsigned TypeLen (const type* Type);
175 /* Return the length of the type string */
176
177 type* TypeCpy (type* Dest, const type* Src);
178 /* Copy a type string */
179
180 type* TypeCat (type* Dest, const type* Src);
181 /* Append Src */
182
183 type* TypeDup (const type* Type);
184 /* Create a copy of the given type on the heap */
185
186 type* TypeAlloc (unsigned Len);
187 /* Allocate memory for a type string of length Len. Len *must* include the
188  * trailing T_END.
189  */
190
191 void TypeFree (type* Type);
192 /* Free a type string */
193
194 int SignExtendChar (int C);
195 /* Do correct sign extension of a character */
196
197 type GetDefaultChar (void);
198 /* Return the default char type (signed/unsigned) depending on the settings */
199
200 type* GetCharArrayType (unsigned Len);
201 /* Return the type for a char array of the given length */
202
203 type* GetImplicitFuncType (void);
204 /* Return a type string for an inplicitly declared function */
205
206 type* PointerTo (const type* T);
207 /* Return a type string that is "pointer to T". The type string is allocated
208  * on the heap and may be freed after use.
209  */
210
211 void PrintType (FILE* F, const type* Type);
212 /* Output translation of type array. */
213
214 void PrintRawType (FILE* F, const type* Type);
215 /* Print a type string in raw format (for debugging) */
216
217 void PrintFuncSig (FILE* F, const char* Name, type* Type);
218 /* Print a function signature. */
219
220 void Encode (type* Type, unsigned long Val);
221 /* Encode an unsigned long into a type array */
222
223 void EncodePtr (type* Type, void* P);
224 /* Encode a pointer into a type array */
225
226 unsigned long Decode (const type* Type);
227 /* Decode an unsigned long from a type array */
228
229 void* DecodePtr (const type* Type);
230 /* Decode a pointer from a type array */
231
232 int HasEncode (const type* Type);
233 /* Return true if the given type has encoded data */
234
235 void CopyEncode (const type* Source, type* Target);
236 /* Copy encoded data from Source to Target */
237
238 #if defined(HAVE_INLINE)
239 INLINE type UnqualifiedType (type T)
240 /* Return the unqalified type */
241 {
242     return (T & ~T_MASK_QUAL);
243 }
244 #else
245 #  define UnqualifiedType(T)    ((T) & ~T_MASK_QUAL)
246 #endif
247
248 unsigned SizeOf (const type* Type);
249 /* Compute size of object represented by type array. */
250
251 unsigned PSizeOf (const type* Type);
252 /* Compute size of pointer object. */
253
254 unsigned CheckedSizeOf (const type* T);
255 /* Return the size of a data type. If the size is zero, emit an error and
256  * return some valid size instead (so the rest of the compiler doesn't have
257  * to work with invalid sizes).
258  */
259 unsigned CheckedPSizeOf (const type* T);
260 /* Return the size of a data type that is pointed to by a pointer. If the
261  * size is zero, emit an error and return some valid size instead (so the
262  * rest of the compiler doesn't have to work with invalid sizes).
263  */
264
265 unsigned TypeOf (const type* Type);
266 /* Get the code generator base type of the object */
267
268 type* Indirect (type* Type);
269 /* Do one indirection for the given type, that is, return the type where the
270  * given type points to.
271  */
272
273 type* ArrayToPtr (const type* Type);
274 /* Convert an array to a pointer to it's first element */
275
276 #if defined(HAVE_INLINE)
277 INLINE int IsTypeChar (const type* T)
278 /* Return true if this is a character type */
279 {
280     return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
281 }
282 #else
283 #  define IsTypeChar(T)         (((T)[0] & T_MASK_TYPE) == T_TYPE_CHAR)
284 #endif
285
286 #if defined(HAVE_INLINE)
287 INLINE int IsTypeInt (const type* T)
288 /* Return true if this is an int type (signed or unsigned) */
289 {
290     return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
291 }
292 #else
293 #  define IsTypeInt(T)          (((T)[0] & T_MASK_TYPE) == T_TYPE_INT)
294 #endif
295
296 #if defined(HAVE_INLINE)
297 INLINE int IsTypeLong (const type* T)
298 /* Return true if this is a long type (signed or unsigned) */
299 {
300     return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
301 }
302 #else
303 #  define IsTypeLong(T)         (((T)[0] & T_MASK_TYPE) == T_TYPE_LONG)
304 #endif
305
306 #if defined(HAVE_INLINE)
307 INLINE int IsTypeFloat (const type* T)
308 /* Return true if this is a float type */
309 {
310     return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
311 }
312 #else
313 #  define IsTypeFloat(T)        (((T)[0] & T_MASK_TYPE) == T_TYPE_FLOAT)
314 #endif
315
316 #if defined(HAVE_INLINE)
317 INLINE int IsTypeDouble (const type* T)
318 /* Return true if this is a double type */
319 {
320     return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
321 }
322 #else
323 #  define IsTypeDouble(T)       (((T)[0] & T_MASK_TYPE) == T_TYPE_DOUBLE)
324 #endif
325
326 #if defined(HAVE_INLINE)
327 INLINE int IsTypePtr (const type* T)
328 /* Return true if this is a pointer type */
329 {
330     return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
331 }
332 #else
333 #  define IsTypePtr(T)          (((T)[0] & T_MASK_TYPE) == T_TYPE_PTR)
334 #endif
335
336 #if defined(HAVE_INLINE)
337 INLINE int IsTypeStruct (const type* T)
338 /* Return true if this is a struct type */
339 {
340     return ((T[0] & T_MASK_TYPE) == T_TYPE_STRUCT);
341 }
342 #else
343 #  define IsTypeStruct(T)       (((T)[0] & T_MASK_TYPE) == T_TYPE_STRUCT)
344 #endif
345
346 #if defined(HAVE_INLINE)
347 INLINE int IsTypeUnion (const type* T)
348 /* Return true if this is a union type */
349 {
350     return ((T[0] & T_MASK_TYPE) == T_TYPE_UNION);
351 }
352 #else
353 #  define IsTypeUnion(T)       (((T)[0] & T_MASK_TYPE) == T_TYPE_UNION)
354 #endif
355
356 #if defined(HAVE_INLINE)
357 INLINE int IsTypeArray (const type* T)
358 /* Return true if this is an array type */
359 {
360     return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
361 }
362 #else
363 #  define IsTypeArray(T)        (((T)[0] & T_MASK_TYPE) == T_TYPE_ARRAY)
364 #endif
365
366 #if defined(HAVE_INLINE)
367 INLINE int IsTypeVoid (const type* T)
368 /* Return true if this is a void type */
369 {
370     return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
371 }
372 #else
373 #  define IsTypeVoid(T)         (((T)[0] & T_MASK_TYPE) == T_TYPE_VOID)
374 #endif
375
376 #if defined(HAVE_INLINE)
377 INLINE int IsTypeFunc (const type* T)
378 /* Return true if this is a function class */
379 {
380     return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
381 }
382 #else
383 #  define IsTypeFunc(T)         (((T)[0] & T_MASK_TYPE) == T_TYPE_FUNC)
384 #endif
385
386 #if defined(HAVE_INLINE)
387 INLINE int IsTypeFuncPtr (const type* T)
388 /* Return true if this is a function pointer */
389 {
390     return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
391 }
392 #else
393 #  define IsTypeFuncPtr(T)      \
394         ((((T)[0] & T_MASK_TYPE) == T_TYPE_PTR) && (((T)[1] & T_MASK_TYPE) == T_TYPE_FUNC))
395 #endif
396
397 int IsClassInt (const type* Type) attribute ((const));
398 /* Return true if this is an integer type */
399
400 int IsClassFloat (const type* Type) attribute ((const));
401 /* Return true if this is a float type */
402
403 int IsClassPtr (const type* Type) attribute ((const));
404 /* Return true if this is a pointer type */
405
406 int IsClassStruct (const type* Type) attribute ((const));
407 /* Return true if this is a struct type */
408
409 int IsSignUnsigned (const type* Type) attribute ((const));
410 /* Return true if this is an unsigned type */
411
412 int IsQualConst (const type* T) attribute ((const));
413 /* Return true if the given type has a const memory image */
414
415 int IsQualVolatile (const type* T) attribute ((const));
416 /* Return true if the given type has a volatile type qualifier */
417
418 int IsFastCallFunc (const type* T) attribute ((const));
419 /* Return true if this is a function type or pointer to function with
420  * __fastcall__ calling conventions
421  */
422
423 int IsVariadicFunc (const type* T) attribute ((const));
424 /* Return true if this is a function type or pointer to function type with
425  * variable parameter list
426  */
427
428 #if defined(HAVE_INLINE)
429 INLINE type GetType (const type* T)
430 /* Get the raw type */
431 {
432     return (T[0] & T_MASK_TYPE);
433 }
434 #else
435 #  define GetType(T)    ((T)[0] & T_MASK_TYPE)
436 #endif
437
438 #if defined(HAVE_INLINE)
439 INLINE type GetClass (const type* T)
440 /* Get the class of a type string */
441 {
442     return (T[0] & T_MASK_CLASS);
443 }
444 #else
445 #  define GetClass(T)    ((T)[0] & T_MASK_CLASS)
446 #endif
447
448 #if defined(HAVE_INLINE)
449 INLINE type GetSignedness (const type* T)
450 /* Get the sign of a type */
451 {
452     return (T[0] & T_MASK_SIGN);
453 }
454 #else
455 #  define GetSignedness(T)      ((T)[0] & T_MASK_SIGN)
456 #endif
457
458 #if defined(HAVE_INLINE)
459 INLINE type GetSizeModifier (const type* T)
460 /* Get the size modifier of a type */
461 {
462     return (T[0] & T_MASK_SIZE);
463 }
464 #else
465 #  define GetSizeModifier(T)      ((T)[0] & T_MASK_SIZE)
466 #endif
467
468 type GetQualifier (const type* T) attribute ((const));
469 /* Get the qualifier from the given type string */
470
471 FuncDesc* GetFuncDesc (const type* T) attribute ((const));
472 /* Get the FuncDesc pointer from a function or pointer-to-function type */
473
474 type* GetFuncReturn (type* T) attribute ((const));
475 /* Return a pointer to the return type of a function or pointer-to-function type */
476
477
478
479 /* End of datatype.h */
480
481 #endif
482
483
484