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