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