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