type: Make an assert()ion independent of the last entry of an enum.
[cparser] / type_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #ifndef TYPE_T_H
6 #define TYPE_T_H
7
8 #include <stdbool.h>
9 #include <assert.h>
10
11 #include <libfirm/firm_types.h>
12
13 #include "type.h"
14 #include "symbol.h"
15 #include "token_t.h"
16 #include "ast_t.h"
17 #include "adt/obst.h"
18
19 typedef enum type_kind_t {
20         TYPE_ERROR = 1,
21         TYPE_ATOMIC,
22         TYPE_COMPLEX,
23         TYPE_IMAGINARY,
24         TYPE_COMPOUND_STRUCT,
25         TYPE_COMPOUND_UNION,
26         TYPE_ENUM,
27         TYPE_FUNCTION,
28         TYPE_POINTER,
29         TYPE_REFERENCE,
30         TYPE_ARRAY,
31         TYPE_TYPEDEF,
32         TYPE_TYPEOF,
33 } type_kind_t;
34
35 struct type_base_t {
36         type_kind_t       kind;
37         type_qualifiers_t qualifiers;
38
39         /* cached ast2firm infos */
40         ir_type          *firm_type;
41 };
42
43 /**
44  * used for atomic types, complex and imaginary and as base for enum
45  */
46 struct atomic_type_t {
47         type_base_t         base;
48         atomic_type_kind_t  akind;
49 };
50
51 struct pointer_type_t {
52         type_base_t  base;
53         type_t      *points_to;
54         variable_t  *base_variable;  /**< Microsoft __based() extension: base variable or NULL. */
55 };
56
57 struct reference_type_t {
58         type_base_t  base;
59         type_t      *refers_to;
60 };
61
62 struct array_type_t {
63         type_base_t   base;
64         type_t       *element_type;
65         expression_t *size_expression;
66         size_t        size;
67
68         ir_node      *size_node; /**< used by ast2firm phase */
69
70         bool          is_static         : 1; /**< a [static] type */
71         bool          is_variable       : 1; /**< a [*] type */
72         bool          has_implicit_size : 1;
73         bool          size_constant     : 1; /**< size expression is constant */
74         bool          is_vla            : 1; /**< it's a variable length array */
75 };
76
77 /**
78  * An entry in the parameter list of a function type.
79  */
80 struct function_parameter_t {
81         type_t               *type;  /**< The parameter type. */
82         function_parameter_t *next;  /**< Points to the next type in the parameter list.*/
83 };
84
85 /** Linkage specifications. */
86 typedef enum linkage_kind_t {
87         LINKAGE_C = 1,   /**< C linkage. */
88         LINKAGE_CXX      /**< C++ linkage. */
89 } linkage_kind_t;
90
91 /** Calling conventions. */
92 typedef enum cc_kind_t {
93         CC_DEFAULT,      /**< default calling convention. */
94         CC_CDECL,        /**< cdecl calling convention. */
95         CC_STDCALL,      /**< stdcall calling convention. */
96         CC_FASTCALL,     /**< fastcall calling convention. */
97         CC_THISCALL      /**< thiscall calling convention. */
98 } cc_kind_t;
99
100 /**
101  * A function type.
102  */
103 struct function_type_t {
104         type_base_t           base;
105         type_t               *return_type;        /**< The return type. */
106         function_parameter_t *parameters;         /**< A list of the parameter types. */
107         linkage_kind_t        linkage;
108         cc_kind_t             calling_convention; /**< The specified calling convention. */
109         decl_modifiers_t      modifiers;
110         bool                  variadic : 1;
111         bool                  unspecified_parameters : 1;
112         bool                  kr_style_parameters : 1;
113 };
114
115 struct compound_type_t {
116         type_base_t     base;
117         bool            packed : 1; /**< Set if packed was specified. */
118         /** the declaration of the compound type, the scope of the declaration
119          *  contains the compound entries. */
120         compound_t     *compound;
121 };
122
123 struct enum_type_t {
124         atomic_type_t       base;
125         /** the enum entity. You can find the enum entries by walking the
126          *  enum->base.next list until you don't find ENTITY_ENUM_VALUE entities
127          *  anymore */
128         enum_t             *enume;
129 };
130
131 struct typedef_type_t {
132         type_base_t  base;
133         typedef_t   *typedefe;
134         type_t      *resolved_type;
135 };
136
137 struct typeof_type_t {
138         type_base_t   base;
139         expression_t *expression;
140         type_t       *typeof_type;
141         type_t       *resolved_type;
142 };
143
144 union type_t {
145         type_kind_t      kind;
146         type_base_t      base;
147         atomic_type_t    atomic;
148         pointer_type_t   pointer;
149         reference_type_t reference;
150         array_type_t     array;
151         function_type_t  function;
152         compound_type_t  compound;
153         enum_type_t      enumt;
154         typedef_type_t   typedeft;
155         typeof_type_t    typeoft;
156 };
157
158 typedef struct atomic_type_properties_t atomic_type_properties_t;
159 struct atomic_type_properties_t {
160         unsigned   size;              /**< type size in bytes */
161         unsigned   alignment;         /**< type alignment in bytes */
162         /** some ABIs are broken and require an alignment different from the
163          * recommended/best alignment inside structs. Fixing ABIs is difficult
164          * so people rather stick with the wrong values for compatibility.
165          * (double type on x86 System V ABI)
166          */
167         unsigned   struct_alignment;
168         unsigned   flags;             /**< type flags from atomic_type_flag_t */
169         unsigned   rank;              /**< integer conversion rank */
170 };
171
172 extern atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1];
173 extern atomic_type_properties_t pointer_properties;
174
175 /** The default calling convention for functions. */
176 extern cc_kind_t default_calling_convention;
177
178 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
179 type_t *make_complex_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
180 type_t *make_imaginary_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
181 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
182 type_t *make_reference_type(type_t *refers_to);
183 type_t *make_based_pointer_type(type_t *points_to,
184                                                                 type_qualifiers_t qualifiers, variable_t *variable);
185 type_t *make_array_type(type_t *element_type, size_t size,
186                         type_qualifiers_t qualifiers);
187 function_parameter_t *allocate_parameter(type_t*);
188
189 /**
190  * Duplicates a type.
191  *
192  * @param type  The type to copy.
193  * @return A copy of the type.
194  *
195  * @note This does not produce a deep copy!
196  */
197 type_t *duplicate_type(const type_t *type);
198
199 type_t *identify_new_type(type_t *type);
200
201 static inline bool is_typeref(const type_t *type)
202 {
203         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
204 }
205
206 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
207 {
208         assert(!is_typeref(type));
209
210         if (type->kind != TYPE_ATOMIC)
211                 return false;
212         const atomic_type_t *atomic_type = &type->atomic;
213
214         return atomic_type->akind == atype;
215 }
216
217 static inline bool is_type_void(type_t const *const type)
218 {
219         return is_type_atomic(type, ATOMIC_TYPE_VOID);
220 }
221
222 static inline bool is_type_pointer(const type_t *type)
223 {
224         assert(!is_typeref(type));
225         return type->kind == TYPE_POINTER;
226 }
227
228 static inline bool is_type_reference(const type_t *type)
229 {
230         assert(!is_typeref(type));
231         return type->kind == TYPE_REFERENCE;
232 }
233
234 static inline bool is_type_array(const type_t *type)
235 {
236         assert(!is_typeref(type));
237         return type->kind == TYPE_ARRAY;
238 }
239
240 static inline bool is_type_function(const type_t *type)
241 {
242         assert(!is_typeref(type));
243         return type->kind == TYPE_FUNCTION;
244 }
245
246 static inline bool is_type_union(const type_t *type)
247 {
248         assert(!is_typeref(type));
249         return type->kind == TYPE_COMPOUND_UNION;
250 }
251
252 static inline bool is_type_struct(const type_t *type)
253 {
254         assert(!is_typeref(type));
255         return type->kind == TYPE_COMPOUND_STRUCT;
256 }
257
258 static inline bool is_type_compound(const type_t *type)
259 {
260         assert(!is_typeref(type));
261         return type->kind == TYPE_COMPOUND_STRUCT
262                 || type->kind == TYPE_COMPOUND_UNION;
263 }
264
265 static inline bool is_type_valid(const type_t *type)
266 {
267         assert(!is_typeref(type));
268         return type->kind != TYPE_ERROR;
269 }
270
271 /**
272  * return integer conversion rank of an atomic type kind
273  */
274 static inline unsigned get_akind_rank(atomic_type_kind_t akind)
275 {
276         return atomic_type_properties[akind].rank;
277 }
278
279 static inline bool is_akind_signed(atomic_type_kind_t akind)
280 {
281         return atomic_type_properties[akind].flags & ATOMIC_TYPE_FLAG_SIGNED;
282 }
283
284 static inline atomic_type_kind_t get_arithmetic_akind(const type_t *type)
285 {
286         assert(type->kind == TYPE_ATOMIC || type->kind == TYPE_COMPLEX
287                || type->kind == TYPE_IMAGINARY || type->kind == TYPE_ENUM);
288         /* note that atomic, complex and enum share atomic_type_t base */
289         return type->atomic.akind;
290 }
291
292 /**
293  * Allocate a type node of given kind and initialize all
294  * fields with zero.
295  *
296  * @param kind             type kind to allocate
297  */
298 type_t *allocate_type_zero(type_kind_t kind);
299
300 /**
301  * Creates a return_type (func)(void) function type if not
302  * already exists.
303  *
304  * @param return_type    the return type
305  */
306 type_t *make_function_0_type(type_t *return_type,
307                              decl_modifiers_t modifiers);
308
309 /**
310  * Creates a return_type (func)(argument_type) function type if not
311  * already exists.
312  *
313  * @param return_type    the return type
314  * @param argument_type  the argument type
315  */
316 type_t *make_function_1_type(type_t *return_type, type_t *argument_type1,
317                              decl_modifiers_t modifiers);
318
319
320 /**
321  * Creates a return_type (func)(argument_type1,argument_type2) function type
322  * if not already exists.
323  */
324 type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
325                              type_t *argument_type2,
326                              decl_modifiers_t modifiers);
327
328 /**
329  * Creates a return_type (func)(argument_type, ...) function type if not
330  * already exists.
331  *
332  * @param return_type    the return type
333  * @param argument_type  the argument type
334  */
335 type_t *make_function_1_type_variadic(type_t *return_type,
336                                       type_t *argument_type,
337                                       decl_modifiers_t modifiers);
338
339 /**
340  * Create a function type with n parameters
341  */
342 type_t *make_function_type(type_t *return_type, int n_types,
343                            type_t *const *argument_types,
344                                                    decl_modifiers_t modifiers);
345
346 #endif