- create an error type
[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         bool          is_static;
101         bool          is_variable;
102 };
103
104 struct function_parameter_t {
105         type_t               *type;
106         function_parameter_t *next;
107 };
108
109 struct function_type_t {
110         type_base_t           type;
111         type_t               *return_type;
112         function_parameter_t *parameters;
113         unsigned              variadic : 1;
114         unsigned              unspecified_parameters : 1;
115         unsigned              kr_style_parameters : 1;
116 };
117
118 struct compound_type_t {
119         type_base_t    type;
120         /** the declaration of the compound type, its context field
121          *  contains the compound entries. */
122         declaration_t *declaration;
123 };
124
125 struct enum_type_t {
126         type_base_t    type;
127         /** the declaration of the enum type. You can find the enum entries by
128          *  walking the declaration->next list until you don't find
129          *  STORAGE_CLASS_ENUM_ENTRY declarations anymore */
130         declaration_t *declaration;
131 };
132
133 struct typedef_type_t {
134         type_base_t    type;
135         declaration_t *declaration;
136         type_t        *resolved_type;
137 };
138
139 struct typeof_type_t {
140         type_base_t   type;
141         expression_t *expression;
142         type_t       *typeof_type;
143         type_t       *resolved_type;
144 };
145
146 struct bitfield_type_t {
147         type_base_t   type;
148         type_t       *base;
149         expression_t *size;
150 };
151
152 union type_t {
153         type_kind_t      kind;
154         type_base_t      base;
155         atomic_type_t    atomic;
156         builtin_type_t   builtin;
157         pointer_type_t   pointer;
158         array_type_t     array;
159         function_type_t  function;
160         compound_type_t  compound;
161         enum_type_t      enumt;
162         typedef_type_t   typedeft;
163         bitfield_type_t  bitfield;
164         typeof_type_t    typeoft;
165 };
166
167 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
168 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
169
170 type_t *duplicate_type(type_t *type);
171
172 static inline bool is_typeref(const type_t *type)
173 {
174         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
175 }
176
177 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
178 {
179         assert(!is_typeref(type));
180
181         if(type->kind != TYPE_ATOMIC)
182                 return false;
183         const atomic_type_t *atomic_type = &type->atomic;
184
185         return atomic_type->akind == atype;
186 }
187
188 static inline bool is_type_pointer(const type_t *type)
189 {
190         assert(!is_typeref(type));
191         return type->kind == TYPE_POINTER;
192 }
193
194 static inline bool is_type_array(const type_t *type)
195 {
196         assert(!is_typeref(type));
197         return type->kind == TYPE_ARRAY;
198 }
199
200 static inline bool is_type_function(const type_t *type)
201 {
202         assert(!is_typeref(type));
203         return type->kind == TYPE_FUNCTION;
204 }
205
206 static inline bool is_type_compound(const type_t *type)
207 {
208         assert(!is_typeref(type));
209         return type->kind == TYPE_COMPOUND_STRUCT
210                 || type->kind == TYPE_COMPOUND_UNION;
211 }
212
213 #endif