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