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