Parse C++ references.
[cparser] / type_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 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 typedef enum type_modifier_t {
56         TYPE_MODIFIER_NONE              = 0,
57         TYPE_MODIFIER_TRANSPARENT_UNION = 1 << 0,
58 } type_modifier_t;
59 typedef unsigned short type_modifiers_t;
60
61 struct type_base_t {
62         type_kind_t       kind;
63         il_size_t         size;           /**< The size of this type. */
64         type_qualifiers_t qualifiers;
65         type_modifiers_t  modifiers;
66         il_alignment_t    alignment;      /**< The extra alignment of the type, 0 for default. */
67
68         ir_type          *firm_type;
69 };
70
71 struct atomic_type_t {
72         type_base_t         base;
73         atomic_type_kind_t  akind;
74 };
75
76 struct complex_type_t {
77         type_base_t         base;
78         atomic_type_kind_t  akind;
79 };
80
81 struct imaginary_type_t {
82         type_base_t         base;
83         atomic_type_kind_t  akind;
84 };
85
86 struct builtin_type_t {
87         type_base_t  base;
88         symbol_t    *symbol;
89         type_t      *real_type;
90 };
91
92 struct pointer_type_t {
93         type_base_t  base;
94         type_t      *points_to;
95         variable_t  *base_variable;  /**< Microsoft __based() extension */
96 };
97
98 struct reference_type_t {
99         type_base_t  base;
100         type_t      *refers_to;
101 };
102
103 struct array_type_t {
104         type_base_t   base;
105         type_t       *element_type;
106         expression_t *size_expression;
107         size_t        size;
108
109         ir_node      *size_node; /**< used by ast2firm phase */
110
111         bool          is_static         : 1; /**< a [static] type */
112         bool          is_variable       : 1; /**< a [*] type */
113         bool          has_implicit_size : 1;
114         bool          size_constant     : 1; /**< size expression is constant */
115         bool          is_vla            : 1; /**< it's a variable length array */
116 };
117
118 /**
119  * An entry in the parameter list of a function type.
120  */
121 struct function_parameter_t {
122         type_t               *type;  /**< The parameter type. */
123         function_parameter_t *next;  /**< Points to the next type in the parameter list.*/
124 };
125
126 /** Linkage specifications. */
127 typedef enum linkage_kind_t {
128         LINKAGE_INVALID,
129         LINKAGE_C,       /**< C linkage. */
130         LINKAGE_CXX      /**< C++ linkage. */
131 } linkage_kind_t;
132
133 /** Calling conventions. */
134 typedef enum cc_kind_t {
135         CC_DEFAULT,      /**< default calling convention. */
136         CC_CDECL,        /**< cdecl calling convention. */
137         CC_STDCALL,      /**< stdcall calling convention. */
138         CC_FASTCALL,     /**< fastcall calling convention. */
139         CC_THISCALL      /**< thiscall calling convention. */
140 } cc_kind_t;
141
142 /**
143  * A function type.
144  */
145 struct function_type_t {
146         type_base_t           base;
147         type_t               *return_type;        /**< The return type. */
148         function_parameter_t *parameters;         /**< A list of the parameter types. */
149         linkage_kind_t        linkage;
150         cc_kind_t             calling_convention; /**< The specified calling convention. */
151         bool                  variadic : 1;
152         bool                  unspecified_parameters : 1;
153         bool                  kr_style_parameters : 1;
154 };
155
156 struct compound_type_t {
157         type_base_t     base;
158         bool            packed : 1; /**< Set if packed was specified. */
159         /** the declaration of the compound type, the scope of the declaration
160          *  contains the compound entries. */
161         compound_t     *compound;
162 };
163
164 struct enum_type_t {
165         type_base_t     base;
166         /** the enum entity. You can find the enum entries by walking the
167          *  enum->base.next list until you don't find ENTITY_ENUM_VALUE entities
168          *  anymore */
169         enum_t         *enume;
170 };
171
172 struct typedef_type_t {
173         type_base_t  base;
174         typedef_t   *typedefe;
175         type_t      *resolved_type;
176 };
177
178 struct typeof_type_t {
179         type_base_t   base;
180         expression_t *expression;
181         type_t       *typeof_type;
182         type_t       *resolved_type;
183 };
184
185 struct bitfield_type_t {
186         type_base_t   base;
187         type_t       *base_type;
188         expression_t *size_expression; /**< The expression for the bit size. */
189         il_size_t     bit_size;        /**< Size of this bitfield in bits. */
190 };
191
192 union type_t {
193         type_kind_t      kind;
194         type_base_t      base;
195         atomic_type_t    atomic;
196         complex_type_t   complex;
197         imaginary_type_t imaginary;
198         builtin_type_t   builtin;
199         pointer_type_t   pointer;
200         reference_type_t reference;
201         array_type_t     array;
202         function_type_t  function;
203         compound_type_t  compound;
204         enum_type_t      enumt;
205         typedef_type_t   typedeft;
206         bitfield_type_t  bitfield;
207         typeof_type_t    typeoft;
208 };
209
210 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
211 type_t *make_complex_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
212 type_t *make_imaginary_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
213 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
214 type_t *make_reference_type(type_t *refers_to);
215 type_t *make_based_pointer_type(type_t *points_to,
216                                                                 type_qualifiers_t qualifiers, variable_t *variable);
217 type_t *make_array_type(type_t *element_type, size_t size,
218                         type_qualifiers_t qualifiers);
219
220 type_t *duplicate_type(const type_t *type);
221
222 static inline bool is_typeref(const type_t *type)
223 {
224         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
225 }
226
227 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
228 {
229         assert(!is_typeref(type));
230
231         if(type->kind != TYPE_ATOMIC)
232                 return false;
233         const atomic_type_t *atomic_type = &type->atomic;
234
235         return atomic_type->akind == atype;
236 }
237
238 static inline bool is_type_pointer(const type_t *type)
239 {
240         assert(!is_typeref(type));
241         return type->kind == TYPE_POINTER;
242 }
243
244 static inline bool is_type_reference(const type_t *type)
245 {
246         assert(!is_typeref(type));
247         return type->kind == TYPE_REFERENCE;
248 }
249
250 static inline bool is_type_array(const type_t *type)
251 {
252         assert(!is_typeref(type));
253         return type->kind == TYPE_ARRAY;
254 }
255
256 static inline bool is_type_function(const type_t *type)
257 {
258         assert(!is_typeref(type));
259         return type->kind == TYPE_FUNCTION;
260 }
261
262 static inline bool is_type_union(const type_t *type)
263 {
264         assert(!is_typeref(type));
265         return type->kind == TYPE_COMPOUND_UNION;
266 }
267
268 static inline bool is_type_struct(const type_t *type)
269 {
270         assert(!is_typeref(type));
271         return type->kind == TYPE_COMPOUND_STRUCT;
272 }
273
274 static inline bool is_type_builtin(const type_t *type)
275 {
276         assert(!is_typeref(type));
277         return type->kind == TYPE_BUILTIN;
278 }
279
280 static inline bool is_type_compound(const type_t *type)
281 {
282         assert(!is_typeref(type));
283         return type->kind == TYPE_COMPOUND_STRUCT
284                 || type->kind == TYPE_COMPOUND_UNION;
285 }
286
287 static inline bool is_type_valid(const type_t *type)
288 {
289         assert(!is_typeref(type));
290         return type->kind != TYPE_ERROR;
291 }
292
293 #endif