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