- add option --no-implicit-cast to supres printing of implicit casts
[cparser] / type_t.h
1 #ifndef TYPE_T_H
2 #define TYPE_T_H
3
4 #include <stdbool.h>
5 #include <assert.h>
6
7 #include <libfirm/firm_types.h>
8
9 #include "type.h"
10 #include "symbol.h"
11 #include "token_t.h"
12 #include "ast_t.h"
13 #include "adt/obst.h"
14
15 extern struct obstack *type_obst;
16
17 typedef enum {
18         TYPE_INVALID,
19         TYPE_ERROR,
20         TYPE_ATOMIC,
21         TYPE_COMPOUND_STRUCT,
22         TYPE_COMPOUND_UNION,
23         TYPE_ENUM,
24         TYPE_FUNCTION,
25         TYPE_POINTER,
26         TYPE_ARRAY,
27         TYPE_BITFIELD,
28         TYPE_BUILTIN,
29         TYPE_TYPEDEF,
30         TYPE_TYPEOF,
31 } type_kind_t;
32
33 /* note that the constant values represent the rank of the types as defined
34  * in ยง 6.3.1 */
35 typedef enum {
36         ATOMIC_TYPE_INVALID = 0,
37         ATOMIC_TYPE_VOID,
38         ATOMIC_TYPE_CHAR,
39         ATOMIC_TYPE_SCHAR,
40         ATOMIC_TYPE_UCHAR,
41         ATOMIC_TYPE_SHORT,
42         ATOMIC_TYPE_USHORT,
43         ATOMIC_TYPE_INT,
44         ATOMIC_TYPE_UINT,
45         ATOMIC_TYPE_LONG,
46         ATOMIC_TYPE_ULONG,
47         ATOMIC_TYPE_LONGLONG,
48         ATOMIC_TYPE_ULONGLONG,
49         ATOMIC_TYPE_FLOAT,
50         ATOMIC_TYPE_DOUBLE,
51         ATOMIC_TYPE_LONG_DOUBLE,
52         ATOMIC_TYPE_BOOL,
53 #ifdef PROVIDE_COMPLEX
54         ATOMIC_TYPE_FLOAT_COMPLEX,
55         ATOMIC_TYPE_DOUBLE_COMPLEX,
56         ATOMIC_TYPE_LONG_DOUBLE_COMPLEX,
57         ATOMIC_TYPE_FLOAT_IMAGINARY,
58         ATOMIC_TYPE_DOUBLE_IMAGINARY,
59         ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY,
60 #endif
61         ATOMIC_TYPE_LAST
62 } atomic_type_kind_t;
63
64 typedef enum {
65         TYPE_QUALIFIER_NONE     = 0,
66         TYPE_QUALIFIER_CONST    = 1 << 0,
67         TYPE_QUALIFIER_RESTRICT = 1 << 1,
68         TYPE_QUALIFIER_VOLATILE = 1 << 2,
69 } type_qualifier_t;
70
71 typedef unsigned int type_qualifiers_t;
72
73 struct type_base_t {
74         type_kind_t       kind;
75         type_qualifiers_t qualifiers;
76
77         ir_type          *firm_type;
78 };
79
80 struct atomic_type_t {
81         type_base_t         type;
82         atomic_type_kind_t  akind;
83 };
84
85 struct builtin_type_t {
86         type_base_t  type;
87         symbol_t    *symbol;
88         type_t      *real_type;
89 };
90
91 struct pointer_type_t {
92         type_base_t  type;
93         type_t      *points_to;
94 };
95
96 struct array_type_t {
97         type_base_t   type;
98         type_t       *element_type;
99         expression_t *size;
100         unsigned      is_static         : 1;
101         unsigned      is_variable       : 1;
102         unsigned      has_implicit_size : 1;
103 };
104
105 struct function_parameter_t {
106         type_t               *type;
107         function_parameter_t *next;
108 };
109
110 struct function_type_t {
111         type_base_t           type;
112         type_t               *return_type;
113         function_parameter_t *parameters;
114         unsigned              variadic : 1;
115         unsigned              unspecified_parameters : 1;
116         unsigned              kr_style_parameters : 1;
117 };
118
119 struct compound_type_t {
120         type_base_t    type;
121         /** the declaration of the compound type, the scope of the declaration
122          *  contains the compound entries. */
123         declaration_t *declaration;
124 };
125
126 struct enum_type_t {
127         type_base_t    type;
128         /** the declaration of the enum type. You can find the enum entries by
129          *  walking the declaration->next list until you don't find
130          *  STORAGE_CLASS_ENUM_ENTRY declarations anymore */
131         declaration_t *declaration;
132 };
133
134 struct typedef_type_t {
135         type_base_t    type;
136         declaration_t *declaration;
137         type_t        *resolved_type;
138 };
139
140 struct typeof_type_t {
141         type_base_t   type;
142         expression_t *expression;
143         type_t       *typeof_type;
144         type_t       *resolved_type;
145 };
146
147 struct bitfield_type_t {
148         type_base_t   type;
149         type_t       *base;
150         expression_t *size;
151 };
152
153 union type_t {
154         type_kind_t      kind;
155         type_base_t      base;
156         atomic_type_t    atomic;
157         builtin_type_t   builtin;
158         pointer_type_t   pointer;
159         array_type_t     array;
160         function_type_t  function;
161         compound_type_t  compound;
162         enum_type_t      enumt;
163         typedef_type_t   typedeft;
164         bitfield_type_t  bitfield;
165         typeof_type_t    typeoft;
166 };
167
168 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
169 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
170
171 type_t *duplicate_type(const type_t *type);
172
173 static inline bool is_typeref(const type_t *type)
174 {
175         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
176 }
177
178 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
179 {
180         assert(!is_typeref(type));
181
182         if(type->kind != TYPE_ATOMIC)
183                 return false;
184         const atomic_type_t *atomic_type = &type->atomic;
185
186         return atomic_type->akind == atype;
187 }
188
189 static inline bool is_type_pointer(const type_t *type)
190 {
191         assert(!is_typeref(type));
192         return type->kind == TYPE_POINTER;
193 }
194
195 static inline bool is_type_array(const type_t *type)
196 {
197         assert(!is_typeref(type));
198         return type->kind == TYPE_ARRAY;
199 }
200
201 static inline bool is_type_function(const type_t *type)
202 {
203         assert(!is_typeref(type));
204         return type->kind == TYPE_FUNCTION;
205 }
206
207 static inline bool is_type_compound(const type_t *type)
208 {
209         assert(!is_typeref(type));
210         return type->kind == TYPE_COMPOUND_STRUCT
211                 || type->kind == TYPE_COMPOUND_UNION;
212 }
213
214 static inline bool is_type_valid(const type_t *type)
215 {
216         return type->kind != TYPE_ERROR;
217 }
218
219 #endif