more on MS __declspec
[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         source_position_t source_position;
96
97         ir_type          *firm_type;
98 };
99
100 struct atomic_type_t {
101         type_base_t         type;
102         atomic_type_kind_t  akind;
103 };
104
105 struct builtin_type_t {
106         type_base_t  type;
107         symbol_t    *symbol;
108         type_t      *real_type;
109 };
110
111 struct pointer_type_t {
112         type_base_t  type;
113         type_t      *points_to;
114 };
115
116 struct array_type_t {
117         type_base_t   type;
118         type_t       *element_type;
119         expression_t *size_expression;
120         size_t        size;
121
122         ir_node      *size_node; /**< used by ast2firm phase */
123
124         unsigned      is_static         : 1; /**< a [static] type */
125         unsigned      is_variable       : 1; /**< a [*] type */
126         unsigned      has_implicit_size : 1;
127         unsigned      size_constant     : 1; /**< size expression is constant */
128         unsigned      is_vla            : 1; /**< it's a variable length array */
129 };
130
131 struct function_parameter_t {
132         type_t               *type;
133         function_parameter_t *next;
134 };
135
136 struct function_type_t {
137         type_base_t           type;
138         type_t               *return_type;
139         function_parameter_t *parameters;
140         unsigned              variadic : 1;
141         unsigned              unspecified_parameters : 1;
142         unsigned              kr_style_parameters : 1;
143 };
144
145 struct compound_type_t {
146         type_base_t    type;
147         /** the declaration of the compound type, the scope of the declaration
148          *  contains the compound entries. */
149         declaration_t *declaration;
150 };
151
152 struct enum_type_t {
153         type_base_t    type;
154         /** the declaration of the enum type. You can find the enum entries by
155          *  walking the declaration->next list until you don't find
156          *  STORAGE_CLASS_ENUM_ENTRY declarations anymore */
157         declaration_t *declaration;
158 };
159
160 struct typedef_type_t {
161         type_base_t    type;
162         declaration_t *declaration;
163         type_t        *resolved_type;
164 };
165
166 struct typeof_type_t {
167         type_base_t   type;
168         expression_t *expression;
169         type_t       *typeof_type;
170         type_t       *resolved_type;
171 };
172
173 struct bitfield_type_t {
174         type_base_t   type;
175         type_t       *base;
176         expression_t *size;
177 };
178
179 union type_t {
180         type_kind_t      kind;
181         type_base_t      base;
182         atomic_type_t    atomic;
183         builtin_type_t   builtin;
184         pointer_type_t   pointer;
185         array_type_t     array;
186         function_type_t  function;
187         compound_type_t  compound;
188         enum_type_t      enumt;
189         typedef_type_t   typedeft;
190         bitfield_type_t  bitfield;
191         typeof_type_t    typeoft;
192 };
193
194 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
195 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
196 type_t *make_array_type(type_t *element_type, size_t size,
197                         type_qualifiers_t qualifiers);
198
199 type_t *duplicate_type(const type_t *type);
200
201 static inline bool is_typeref(const type_t *type)
202 {
203         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
204 }
205
206 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
207 {
208         assert(!is_typeref(type));
209
210         if(type->kind != TYPE_ATOMIC)
211                 return false;
212         const atomic_type_t *atomic_type = &type->atomic;
213
214         return atomic_type->akind == atype;
215 }
216
217 static inline bool is_type_pointer(const type_t *type)
218 {
219         assert(!is_typeref(type));
220         return type->kind == TYPE_POINTER;
221 }
222
223 static inline bool is_type_array(const type_t *type)
224 {
225         assert(!is_typeref(type));
226         return type->kind == TYPE_ARRAY;
227 }
228
229 static inline bool is_type_function(const type_t *type)
230 {
231         assert(!is_typeref(type));
232         return type->kind == TYPE_FUNCTION;
233 }
234
235 static inline bool is_type_union(const type_t *type)
236 {
237         assert(!is_typeref(type));
238         return type->kind == TYPE_COMPOUND_UNION;
239 }
240
241 static inline bool is_type_struct(const type_t *type)
242 {
243         assert(!is_typeref(type));
244         return type->kind == TYPE_COMPOUND_STRUCT;
245 }
246
247 static inline bool is_type_builtin(const type_t *type)
248 {
249         assert(!is_typeref(type));
250         return type->kind == TYPE_BUILTIN;
251 }
252
253 static inline bool is_type_compound(const type_t *type)
254 {
255         assert(!is_typeref(type));
256         return type->kind == TYPE_COMPOUND_STRUCT
257                 || type->kind == TYPE_COMPOUND_UNION;
258 }
259
260 static inline bool is_type_valid(const type_t *type)
261 {
262         return type->kind != TYPE_ERROR;
263 }
264
265 #endif