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