2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
11 #include <libfirm/firm_types.h>
19 typedef enum type_kind_t {
37 type_qualifiers_t qualifiers;
39 /* cached ast2firm infos */
44 * used for atomic types, complex and imaginary and as base for enum
46 struct atomic_type_t {
48 atomic_type_kind_t akind;
51 struct pointer_type_t {
54 variable_t *base_variable; /**< Microsoft __based() extension: base variable or NULL. */
57 struct reference_type_t {
65 expression_t *size_expression;
68 ir_node *size_node; /**< used by ast2firm phase */
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 */
78 * An entry in the parameter list of a function type.
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.*/
85 /** Linkage specifications. */
86 typedef enum linkage_kind_t {
87 LINKAGE_C = 1, /**< C linkage. */
88 LINKAGE_CXX /**< C++ linkage. */
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. */
103 struct function_type_t {
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;
111 bool unspecified_parameters : 1;
112 bool kr_style_parameters : 1;
115 struct compound_type_t {
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;
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
131 struct typedef_type_t {
134 type_t *resolved_type;
137 struct typeof_type_t {
139 expression_t *expression;
141 type_t *resolved_type;
147 atomic_type_t atomic;
148 pointer_type_t pointer;
149 reference_type_t reference;
151 function_type_t function;
152 compound_type_t compound;
154 typedef_type_t typedeft;
155 typeof_type_t typeoft;
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)
167 unsigned struct_alignment;
168 unsigned flags; /**< type flags from atomic_type_flag_t */
169 unsigned rank; /**< integer conversion rank */
172 extern atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1];
173 extern atomic_type_properties_t pointer_properties;
175 /** The default calling convention for functions. */
176 extern cc_kind_t default_calling_convention;
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*);
192 * @param type The type to copy.
193 * @return A copy of the type.
195 * @note This does not produce a deep copy!
197 type_t *duplicate_type(const type_t *type);
199 type_t *identify_new_type(type_t *type);
201 static inline bool is_typeref(const type_t *type)
203 return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
206 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
208 assert(!is_typeref(type));
210 if (type->kind != TYPE_ATOMIC)
212 const atomic_type_t *atomic_type = &type->atomic;
214 return atomic_type->akind == atype;
217 static inline bool is_type_void(type_t const *const type)
219 return is_type_atomic(type, ATOMIC_TYPE_VOID);
222 static inline bool is_type_pointer(const type_t *type)
224 assert(!is_typeref(type));
225 return type->kind == TYPE_POINTER;
228 static inline bool is_type_reference(const type_t *type)
230 assert(!is_typeref(type));
231 return type->kind == TYPE_REFERENCE;
234 static inline bool is_type_array(const type_t *type)
236 assert(!is_typeref(type));
237 return type->kind == TYPE_ARRAY;
240 static inline bool is_type_function(const type_t *type)
242 assert(!is_typeref(type));
243 return type->kind == TYPE_FUNCTION;
246 static inline bool is_type_union(const type_t *type)
248 assert(!is_typeref(type));
249 return type->kind == TYPE_COMPOUND_UNION;
252 static inline bool is_type_struct(const type_t *type)
254 assert(!is_typeref(type));
255 return type->kind == TYPE_COMPOUND_STRUCT;
258 static inline bool is_type_compound(const type_t *type)
260 assert(!is_typeref(type));
261 return type->kind == TYPE_COMPOUND_STRUCT
262 || type->kind == TYPE_COMPOUND_UNION;
265 static inline bool is_type_valid(const type_t *type)
267 assert(!is_typeref(type));
268 return type->kind != TYPE_ERROR;
272 * return integer conversion rank of an atomic type kind
274 static inline unsigned get_akind_rank(atomic_type_kind_t akind)
276 return atomic_type_properties[akind].rank;
279 static inline bool is_akind_signed(atomic_type_kind_t akind)
281 return atomic_type_properties[akind].flags & ATOMIC_TYPE_FLAG_SIGNED;
284 static inline atomic_type_kind_t get_arithmetic_akind(const type_t *type)
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;
293 * Allocate a type node of given kind and initialize all
296 * @param kind type kind to allocate
298 type_t *allocate_type_zero(type_kind_t kind);
301 * Creates a return_type (func)(void) function type if not
304 * @param return_type the return type
306 type_t *make_function_0_type(type_t *return_type,
307 decl_modifiers_t modifiers);
310 * Creates a return_type (func)(argument_type) function type if not
313 * @param return_type the return type
314 * @param argument_type the argument type
316 type_t *make_function_1_type(type_t *return_type, type_t *argument_type1,
317 decl_modifiers_t modifiers);
321 * Creates a return_type (func)(argument_type1,argument_type2) function type
322 * if not already exists.
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);
329 * Creates a return_type (func)(argument_type, ...) function type if not
332 * @param return_type the return type
333 * @param argument_type the argument type
335 type_t *make_function_1_type_variadic(type_t *return_type,
336 type_t *argument_type,
337 decl_modifiers_t modifiers);
340 * Create a function type with n parameters
342 type_t *make_function_type(type_t *return_type, int n_types,
343 type_t *const *argument_types,
344 decl_modifiers_t modifiers);