- reworked handling environments and struct, union, enum namespace
[cparser] / ast_t.h
1 #ifndef AST_T_H
2 #define AST_T_H
3
4 #include "ast.h"
5 #include "symbol.h"
6 #include "token_t.h"
7 #include "type.h"
8 #include "adt/obst.h"
9
10 extern struct obstack ast_obstack;
11
12 typedef enum {
13         EXPR_INVALID = 0,
14         EXPR_REFERENCE,
15         EXPR_CONST,
16         EXPR_STRING_LITERAL,
17         EXPR_CALL,
18         EXPR_UNARY,
19         EXPR_BINARY,
20         EXPR_CONDITIONAL,
21         EXPR_SELECT,
22         EXPR_ARRAY_ACCESS,
23         EXPR_SIZEOF,
24
25         EXPR_FUNCTION,
26         EXPR_PRETTY_FUNCTION,
27         EXPR_BUILTIN_SYMBOL,
28         EXPR_OFFSETOF,
29         EXPR_VA_ARG,
30         EXPR_STATEMENT
31 } expresion_type_t;
32
33 struct context_t {
34         declaration_t   *declarations;
35 };
36
37 struct expression_t {
38         expresion_type_t   type;
39         type_t            *datatype;
40         source_position_t  source_position;
41 };
42
43 struct const_t {
44         expression_t  expression;
45         union {
46                 int         int_value;
47                 long double float_value;
48         } v;
49 };
50
51 struct string_literal_t {
52         expression_t  expression;
53         const char   *value;
54 };
55
56 struct builtin_symbol_expression_t {
57         expression_t  expression;
58         symbol_t     *symbol;
59 };
60
61 struct reference_expression_t {
62         expression_t   expression;
63         symbol_t      *symbol;
64         declaration_t *declaration;
65 };
66
67 struct call_argument_t {
68         expression_t    *expression;
69         call_argument_t *next;
70 };
71
72 struct call_expression_t {
73         expression_t     expression;
74         expression_t    *method;
75         call_argument_t *arguments;
76 };
77
78 typedef enum {
79         UNEXPR_INVALID = 0,
80         UNEXPR_NEGATE,
81         UNEXPR_PLUS,
82         UNEXPR_BITWISE_NEGATE,
83         UNEXPR_NOT,
84         UNEXPR_DEREFERENCE,
85         UNEXPR_TAKE_ADDRESS,
86         UNEXPR_POSTFIX_INCREMENT,
87         UNEXPR_POSTFIX_DECREMENT,
88         UNEXPR_PREFIX_INCREMENT,
89         UNEXPR_PREFIX_DECREMENT,
90         UNEXPR_CAST
91 } unary_expression_type_t;
92
93 struct unary_expression_t {
94         expression_t             expression;
95         unary_expression_type_t  type;
96         expression_t            *value;
97 };
98
99 typedef enum {
100         BINEXPR_INVALID = 0,
101         BINEXPR_ADD,
102         BINEXPR_SUB,
103         BINEXPR_MUL,
104         BINEXPR_DIV,
105         BINEXPR_MOD,
106         BINEXPR_EQUAL,
107         BINEXPR_NOTEQUAL,
108         BINEXPR_LESS,
109         BINEXPR_LESSEQUAL,
110         BINEXPR_GREATER,
111         BINEXPR_GREATEREQUAL,
112         BINEXPR_BITWISE_AND,
113         BINEXPR_BITWISE_OR,
114         BINEXPR_BITWISE_XOR,
115         BINEXPR_LOGICAL_AND,
116         BINEXPR_LOGICAL_OR,
117         BINEXPR_SHIFTLEFT,
118         BINEXPR_SHIFTRIGHT,
119         BINEXPR_ASSIGN,
120         BINEXPR_MUL_ASSIGN,
121         BINEXPR_DIV_ASSIGN,
122         BINEXPR_MOD_ASSIGN,
123         BINEXPR_ADD_ASSIGN,
124         BINEXPR_SUB_ASSIGN,
125         BINEXPR_SHIFTLEFT_ASSIGN,
126         BINEXPR_SHIFTRIGHT_ASSIGN,
127         BINEXPR_BITWISE_AND_ASSIGN,
128         BINEXPR_BITWISE_XOR_ASSIGN,
129         BINEXPR_BITWISE_OR_ASSIGN,
130         BINEXPR_COMMA
131 } binary_expression_type_t;
132
133 struct binary_expression_t {
134         expression_t              expression;
135         binary_expression_type_t  type;
136         expression_t             *left;
137         expression_t             *right;
138 };
139
140 struct select_expression_t {
141         expression_t   expression;
142         expression_t  *compound;
143         symbol_t      *symbol;
144
145         declaration_t *compound_entry;
146 };
147
148 struct array_access_expression_t {
149         expression_t  expression;
150         expression_t *array_ref;
151         expression_t *index;
152 };
153
154 struct sizeof_expression_t {
155         expression_t  expression;
156         type_t       *type;
157         expression_t *size_expression;
158 };
159
160 struct designator_t {
161         symbol_t     *symbol;
162         expression_t *array_access;
163         designator_t *next;
164 };
165
166 struct offsetof_expression_t {
167         expression_t  expression;
168         type_t       *type;
169         designator_t *designator;
170 };
171
172 struct va_arg_expression_t {
173         expression_t  expression;
174         expression_t *arg;
175         type_t       *type;
176 };
177
178 struct conditional_expression_t {
179         expression_t  expression;
180         expression_t *condition;
181         expression_t *true_expression;
182         expression_t *false_expression;
183 };
184
185 struct statement_expression_t {
186         expression_t  expression;
187         statement_t  *statement;
188 };
189
190 typedef enum {
191         STORAGE_CLASS_NONE,
192         STORAGE_CLASS_TYPEDEF,
193         STORAGE_CLASS_EXTERN,
194         STORAGE_CLASS_STATIC,
195         STORAGE_CLASS_AUTO,
196         STORAGE_CLASS_REGISTER,
197         STORAGE_CLASS_ENUM_ENTRY
198 } storage_class_t;
199
200 typedef enum {
201         NAMESPACE_NORMAL,
202         NAMESPACE_STRUCT,
203         NAMESPACE_UNION,
204         NAMESPACE_ENUM
205 } namespace_t;
206
207 typedef enum {
208         INITIALIZER_VALUE,
209         INITIALIZER_LIST,
210 } initializer_type_t;
211
212 struct initializer_t {
213         initializer_type_t type;
214         designator_t      *designator;
215         union {
216                 initializer_t *list;
217                 expression_t  *value;
218         } v;
219         initializer_t *next;
220 };
221
222 struct declaration_t {
223         unsigned short      namespace;
224         unsigned short      storage_class;
225         type_t             *type;
226         symbol_t           *symbol;
227         source_position_t   source_position;
228         union {
229                 bool            is_defined;
230                 statement_t    *statement;
231                 initializer_t  *initializer;
232         } init;
233         context_t           context;
234         context_t          *parent_context;
235
236         /** next declaration in a context */
237         declaration_t      *context_next;
238         /** next declaration with same symbol */
239         declaration_t      *next;
240 };
241
242 typedef enum {
243         STATEMENT_INVALID,
244         STATEMENT_COMPOUND,
245         STATEMENT_RETURN,
246         STATEMENT_DECLARATION,
247         STATEMENT_IF,
248         STATEMENT_SWITCH,
249         STATEMENT_EXPRESSION,
250         STATEMENT_CONTINUE,
251         STATEMENT_BREAK,
252         STATEMENT_GOTO,
253         STATEMENT_LABEL,
254         STATEMENT_CASE_LABEL,
255         STATEMENT_WHILE,
256         STATEMENT_DO_WHILE,
257         STATEMENT_FOR
258 } statement_type_t;
259
260 struct statement_t {
261         statement_type_t   type;
262         statement_t       *next;
263         source_position_t  source_position;
264 };
265
266 struct return_statement_t {
267         statement_t   statement;
268         expression_t *return_value;
269 };
270
271 struct compound_statement_t {
272         statement_t  statement;
273         statement_t *statements;
274         context_t    context;
275 };
276
277 struct declaration_statement_t {
278         statement_t    statement;
279         declaration_t *declarations_begin;
280         declaration_t *declarations_end;
281
282         int            value_number; /**< filled in by semantic phase */
283         int            refs;
284 };
285
286 struct if_statement_t {
287         statement_t   statement;
288         expression_t *condition;
289         statement_t  *true_statement;
290         statement_t  *false_statement;
291 };
292
293 struct switch_statement_t {
294         statement_t   statement;
295         expression_t *expression;
296         statement_t  *body;
297 };
298
299 struct goto_statement_t {
300         statement_t        statement;
301         symbol_t          *label_symbol;
302         label_statement_t *label;
303 };
304
305 struct case_label_statement_t {
306         statement_t   statement;
307         expression_t *expression;
308 };
309
310 struct label_statement_t {
311         statement_t        statement;
312         symbol_t          *symbol;
313 };
314
315 struct expression_statement_t {
316         statement_t   statement;
317         expression_t *expression;
318 };
319
320 struct while_statement_t {
321         statement_t   statement;
322         expression_t *condition;
323         statement_t  *body;
324 };
325
326 struct do_while_statement_t {
327         statement_t   statement;
328         expression_t *condition;
329         statement_t  *body;
330 };
331
332 struct for_statement_t {
333         statement_t   statement;
334         expression_t  *initialisation;
335         expression_t  *condition;
336         expression_t  *step;
337         statement_t   *body;
338         context_t      context;
339 };
340
341 struct translation_unit_t {
342         context_t context;
343 };
344
345 static inline
346 void *_allocate_ast(size_t size)
347 {
348         return obstack_alloc(&ast_obstack, size);
349 }
350
351 #define allocate_ast(size)                 _allocate_ast(size)
352
353 #endif