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