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