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