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