implement K&R style function definitions, code cleanup here and there
[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_type_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_type_t;
60
61 typedef enum {
62         TYPE_QUALIFIER_NONE     = 0,
63         TYPE_QUALIFIER_CONST    = 1 << 0,
64         TYPE_QUALIFIER_RESTRICT = 1 << 1,
65         TYPE_QUALIFIER_VOLATILE = 1 << 2,
66 } type_qualifier_t;
67
68 typedef unsigned int type_qualifiers_t;
69
70 struct type_base_t {
71         type_type_t       type;
72         type_qualifiers_t qualifiers;
73
74         ir_type          *firm_type;
75 };
76
77 struct atomic_type_t {
78         type_base_t         type;
79         atomic_type_type_t  atype;
80 };
81
82 struct builtin_type_t {
83         type_base_t  type;
84         symbol_t    *symbol;
85         type_t      *real_type;
86 };
87
88 struct pointer_type_t {
89         type_base_t  type;
90         type_t      *points_to;
91 };
92
93 struct array_type_t {
94         type_base_t   type;
95         type_t       *element_type;
96         expression_t *size;
97         bool          is_static;
98         bool          is_variable;
99 };
100
101 struct function_parameter_t {
102         type_t               *type;
103         function_parameter_t *next;
104 };
105
106 struct function_type_t {
107         type_base_t           type;
108         type_t               *result_type;
109         function_parameter_t *parameters;
110         unsigned              variadic : 1;
111         unsigned              unspecified_parameters : 1;
112         unsigned              kr_style_parameters : 1;
113 };
114
115 struct compound_type_t {
116         type_base_t    type;
117         /** the declaration of the compound type, its context field
118          *  contains the compound entries. */
119         declaration_t *declaration;
120 };
121
122 struct enum_type_t {
123         type_base_t    type;
124         /** the declaration of the enum type. You can find the enum entries by
125          *  walking the declaration->next list until you don't find
126          *  STORAGE_CLASS_ENUM_ENTRY declarations anymore */
127         declaration_t *declaration;
128 };
129
130 struct typedef_type_t {
131         type_base_t    type;
132         declaration_t *declaration;
133         type_t        *resolved_type;
134 };
135
136 struct typeof_type_t {
137         type_base_t   type;
138         expression_t *expression;
139         type_t       *typeof_type;
140         type_t       *resolved_type;
141 };
142
143 union type_t {
144         type_type_t      type;
145         type_base_t      base;
146         atomic_type_t    atomic;
147         builtin_type_t   builtin;
148         pointer_type_t   pointer;
149         array_type_t     array;
150         function_type_t  function;
151         compound_type_t  compound;
152         enum_type_t      enumt;
153         typedef_type_t   typedeft;
154         typeof_type_t    typeoft;
155 };
156
157 type_t *make_atomic_type(atomic_type_type_t type, type_qualifiers_t qualifiers);
158 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
159
160 type_t *duplicate_type(type_t *type);
161
162 static inline bool is_typeref(const type_t *type)
163 {
164         return type->type == TYPE_TYPEDEF || type->type == TYPE_TYPEOF;
165 }
166
167 static inline bool is_type_atomic(const type_t *type, atomic_type_type_t atype)
168 {
169         assert(!is_typeref(type));
170
171         if(type->type != TYPE_ATOMIC)
172                 return false;
173         const atomic_type_t *atomic_type = &type->atomic;
174
175         return atomic_type->atype == atype;
176 }
177
178 static inline bool is_type_pointer(const type_t *type)
179 {
180         assert(!is_typeref(type));
181         return type->type == TYPE_POINTER;
182 }
183
184 static inline bool is_type_compound(const type_t *type)
185 {
186         assert(!is_typeref(type));
187         return type->type == TYPE_COMPOUND_STRUCT
188                 || type->type == TYPE_COMPOUND_UNION;
189 }
190
191 #endif