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