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