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