2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
11 #include <libfirm/firm_types.h>
16 typedef enum type_kind_t {
34 type_qualifiers_t qualifiers;
36 /* cached ast2firm infos */
41 * used for atomic types, complex and imaginary and as base for enum
43 struct atomic_type_t {
45 atomic_type_kind_t akind;
48 struct pointer_type_t {
51 variable_t *base_variable; /**< Microsoft __based() extension: base variable or NULL. */
54 struct reference_type_t {
62 expression_t *size_expression;
65 ir_node *size_node; /**< used by ast2firm phase */
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 */
75 * An entry in the parameter list of a function type.
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.*/
82 /** Linkage specifications. */
83 typedef enum linkage_kind_t {
84 LINKAGE_C = 1, /**< C linkage. */
85 LINKAGE_CXX /**< C++ linkage. */
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. */
100 struct function_type_t {
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;
108 bool unspecified_parameters : 1;
109 bool kr_style_parameters : 1;
112 struct compound_type_t {
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;
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
128 struct typedef_type_t {
131 type_t *resolved_type;
134 struct typeof_type_t {
136 expression_t *expression;
138 type_t *resolved_type;
144 atomic_type_t atomic;
145 pointer_type_t pointer;
146 reference_type_t reference;
148 function_type_t function;
149 compound_type_t compound;
151 typedef_type_t typedeft;
152 typeof_type_t typeoft;
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)
164 unsigned struct_alignment;
165 unsigned flags; /**< type flags from atomic_type_flag_t */
166 unsigned rank; /**< integer conversion rank */
169 extern atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1];
170 extern atomic_type_properties_t pointer_properties;
172 /** The default calling convention for functions. */
173 extern cc_kind_t default_calling_convention;
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*);
189 * @param type The type to copy.
190 * @return A copy of the type.
192 * @note This does not produce a deep copy!
194 type_t *duplicate_type(const type_t *type);
196 type_t *identify_new_type(type_t *type);
198 static inline bool is_typeref(const type_t *type)
200 return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
203 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
205 assert(!is_typeref(type));
207 if (type->kind != TYPE_ATOMIC)
209 const atomic_type_t *atomic_type = &type->atomic;
211 return atomic_type->akind == atype;
214 static inline bool is_type_void(type_t const *const type)
216 return is_type_atomic(type, ATOMIC_TYPE_VOID);
219 static inline bool is_type_pointer(const type_t *type)
221 assert(!is_typeref(type));
222 return type->kind == TYPE_POINTER;
225 static inline bool is_type_reference(const type_t *type)
227 assert(!is_typeref(type));
228 return type->kind == TYPE_REFERENCE;
231 static inline bool is_type_array(const type_t *type)
233 assert(!is_typeref(type));
234 return type->kind == TYPE_ARRAY;
237 static inline bool is_type_function(const type_t *type)
239 assert(!is_typeref(type));
240 return type->kind == TYPE_FUNCTION;
243 static inline bool is_type_union(const type_t *type)
245 assert(!is_typeref(type));
246 return type->kind == TYPE_COMPOUND_UNION;
249 static inline bool is_type_struct(const type_t *type)
251 assert(!is_typeref(type));
252 return type->kind == TYPE_COMPOUND_STRUCT;
255 static inline bool is_type_compound(const type_t *type)
257 assert(!is_typeref(type));
258 return type->kind == TYPE_COMPOUND_STRUCT
259 || type->kind == TYPE_COMPOUND_UNION;
262 static inline bool is_type_valid(const type_t *type)
264 assert(!is_typeref(type));
265 return type->kind != TYPE_ERROR;
269 * return integer conversion rank of an atomic type kind
271 static inline unsigned get_akind_rank(atomic_type_kind_t akind)
273 return atomic_type_properties[akind].rank;
276 static inline bool is_akind_signed(atomic_type_kind_t akind)
278 return atomic_type_properties[akind].flags & ATOMIC_TYPE_FLAG_SIGNED;
281 static inline atomic_type_kind_t get_arithmetic_akind(const type_t *type)
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;
290 * Allocate a type node of given kind and initialize all
293 * @param kind type kind to allocate
295 type_t *allocate_type_zero(type_kind_t kind);
298 * Creates a return_type (func)(void) function type if not
301 * @param return_type the return type
303 type_t *make_function_0_type(type_t *return_type,
304 decl_modifiers_t modifiers);
307 * Creates a return_type (func)(argument_type) function type if not
310 * @param return_type the return type
311 * @param argument_type the argument type
313 type_t *make_function_1_type(type_t *return_type, type_t *argument_type1,
314 decl_modifiers_t modifiers);
318 * Creates a return_type (func)(argument_type1,argument_type2) function type
319 * if not already exists.
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);
326 * Creates a return_type (func)(argument_type, ...) function type if not
329 * @param return_type the return type
330 * @param argument_type the argument type
332 type_t *make_function_1_type_variadic(type_t *return_type,
333 type_t *argument_type,
334 decl_modifiers_t modifiers);
337 * Create a function type with n parameters
339 type_t *make_function_type(type_t *return_type, int n_types,
340 type_t *const *argument_types,
341 decl_modifiers_t modifiers);