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