implement transparent_union
[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 {
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_ARRAY,
48         TYPE_BITFIELD,
49         TYPE_BUILTIN,
50         TYPE_TYPEDEF,
51         TYPE_TYPEOF,
52 } type_kind_t;
53
54 typedef enum {
55         TYPE_MODIFIER_NONE              = 0,
56         TYPE_MODIFIER_TRANSPARENT_UNION = 1 << 0,
57 } type_modifier_t;
58 typedef unsigned short type_modifiers_t;
59
60 struct type_base_t {
61         type_kind_t       kind;
62         type_qualifiers_t qualifiers;
63         type_modifiers_t  modifiers;
64         unsigned char     alignment;      /**< The extra alignment of the type, 0 for default. */
65         source_position_t source_position;
66
67         ir_type          *firm_type;
68 };
69
70 struct atomic_type_t {
71         type_base_t         base;
72         atomic_type_kind_t  akind;
73 };
74
75 struct complex_type_t {
76         type_base_t         base;
77         atomic_type_kind_t  akind;
78 };
79
80 struct imaginary_type_t {
81         type_base_t         base;
82         atomic_type_kind_t  akind;
83 };
84
85 struct builtin_type_t {
86         type_base_t  base;
87         symbol_t    *symbol;
88         type_t      *real_type;
89 };
90
91 struct pointer_type_t {
92         type_base_t  base;
93         type_t      *points_to;
94 };
95
96 struct array_type_t {
97         type_base_t   base;
98         type_t       *element_type;
99         expression_t *size_expression;
100         size_t        size;
101
102         ir_node      *size_node; /**< used by ast2firm phase */
103
104         unsigned      is_static         : 1; /**< a [static] type */
105         unsigned      is_variable       : 1; /**< a [*] type */
106         unsigned      has_implicit_size : 1;
107         unsigned      size_constant     : 1; /**< size expression is constant */
108         unsigned      is_vla            : 1; /**< it's a variable length array */
109 };
110
111 struct function_parameter_t {
112         type_t               *type;
113         function_parameter_t *next;
114 };
115
116 struct function_type_t {
117         type_base_t           base;
118         type_t               *return_type;
119         function_parameter_t *parameters;
120         unsigned              variadic : 1;
121         unsigned              unspecified_parameters : 1;
122         unsigned              kr_style_parameters : 1;
123 };
124
125 struct compound_type_t {
126         type_base_t    base;
127         /** the declaration of the compound type, the scope of the declaration
128          *  contains the compound entries. */
129         declaration_t *declaration;
130 };
131
132 struct enum_type_t {
133         type_base_t    base;
134         /** the declaration of the enum type. You can find the enum entries by
135          *  walking the declaration->next list until you don't find
136          *  STORAGE_CLASS_ENUM_ENTRY declarations anymore */
137         declaration_t *declaration;
138 };
139
140 struct typedef_type_t {
141         type_base_t    base;
142         declaration_t *declaration;
143         type_t        *resolved_type;
144 };
145
146 struct typeof_type_t {
147         type_base_t   base;
148         expression_t *expression;
149         type_t       *typeof_type;
150         type_t       *resolved_type;
151 };
152
153 struct bitfield_type_t {
154         type_base_t   base;
155         type_t       *base_type;
156         expression_t *size;
157 };
158
159 union type_t {
160         type_kind_t      kind;
161         type_base_t      base;
162         atomic_type_t    atomic;
163         complex_type_t   complex;
164         imaginary_type_t imaginary;
165         builtin_type_t   builtin;
166         pointer_type_t   pointer;
167         array_type_t     array;
168         function_type_t  function;
169         compound_type_t  compound;
170         enum_type_t      enumt;
171         typedef_type_t   typedeft;
172         bitfield_type_t  bitfield;
173         typeof_type_t    typeoft;
174 };
175
176 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
177 type_t *make_complex_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
178 type_t *make_imaginary_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
179 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
180 type_t *make_array_type(type_t *element_type, size_t size,
181                         type_qualifiers_t qualifiers);
182
183 type_t *duplicate_type(const type_t *type);
184
185 static inline bool is_typeref(const type_t *type)
186 {
187         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
188 }
189
190 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
191 {
192         assert(!is_typeref(type));
193
194         if(type->kind != TYPE_ATOMIC)
195                 return false;
196         const atomic_type_t *atomic_type = &type->atomic;
197
198         return atomic_type->akind == atype;
199 }
200
201 static inline bool is_type_pointer(const type_t *type)
202 {
203         assert(!is_typeref(type));
204         return type->kind == TYPE_POINTER;
205 }
206
207 static inline bool is_type_array(const type_t *type)
208 {
209         assert(!is_typeref(type));
210         return type->kind == TYPE_ARRAY;
211 }
212
213 static inline bool is_type_function(const type_t *type)
214 {
215         assert(!is_typeref(type));
216         return type->kind == TYPE_FUNCTION;
217 }
218
219 static inline bool is_type_union(const type_t *type)
220 {
221         assert(!is_typeref(type));
222         return type->kind == TYPE_COMPOUND_UNION;
223 }
224
225 static inline bool is_type_struct(const type_t *type)
226 {
227         assert(!is_typeref(type));
228         return type->kind == TYPE_COMPOUND_STRUCT;
229 }
230
231 static inline bool is_type_builtin(const type_t *type)
232 {
233         assert(!is_typeref(type));
234         return type->kind == TYPE_BUILTIN;
235 }
236
237 static inline bool is_type_compound(const type_t *type)
238 {
239         assert(!is_typeref(type));
240         return type->kind == TYPE_COMPOUND_STRUCT
241                 || type->kind == TYPE_COMPOUND_UNION;
242 }
243
244 static inline bool is_type_valid(const type_t *type)
245 {
246         return type->kind != TYPE_ERROR;
247 }
248
249 #endif