- countless bugfixes
[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_SELECT,
21         EXPR_ARRAY_ACCESS,
22         EXPR_SIZEOF,
23
24         EXPR_FUNCTION,
25         EXPR_PRETTY_FUNCTION,
26         EXPR_STATEMENT
27 } expresion_type_t;
28
29 struct context_t {
30         declaration_t   *declarations;
31         compound_type_t *structs;
32         compound_type_t *unions;
33         enum_type_t     *enums;
34 };
35
36 struct expression_t {
37         expresion_type_t   type;
38         type_t            *datatype;
39         source_position_t  source_position;
40 };
41
42 struct const_t {
43         expression_t  expression;
44         int           value;
45 };
46
47 struct string_literal_t {
48         expression_t  expression;
49         const char   *value;
50 };
51
52 struct reference_expression_t {
53         expression_t   expression;
54         symbol_t      *symbol;
55         declaration_t *declaration;
56 };
57
58 struct call_argument_t {
59         expression_t    *expression;
60         call_argument_t *next;
61 };
62
63 struct call_expression_t {
64         expression_t     expression;
65         expression_t    *method;
66         call_argument_t *arguments;
67 };
68
69 typedef enum {
70         UNEXPR_INVALID = 0,
71         UNEXPR_NEGATE,
72         UNEXPR_PLUS,
73         UNEXPR_BITWISE_NEGATE,
74         UNEXPR_NOT,
75         UNEXPR_DEREFERENCE,
76         UNEXPR_TAKE_ADDRESS,
77         UNEXPR_POSTFIX_INCREMENT,
78         UNEXPR_POSTFIX_DECREMENT,
79         UNEXPR_PREFIX_INCREMENT,
80         UNEXPR_PREFIX_DECREMENT,
81         UNEXPR_CAST
82 } unary_expression_type_t;
83
84 struct unary_expression_t {
85         expression_t             expression;
86         unary_expression_type_t  type;
87         expression_t            *value;
88 };
89
90 typedef enum {
91         BINEXPR_INVALID = 0,
92         BINEXPR_ADD,
93         BINEXPR_SUB,
94         BINEXPR_MUL,
95         BINEXPR_DIV,
96         BINEXPR_MOD,
97         BINEXPR_EQUAL,
98         BINEXPR_NOTEQUAL,
99         BINEXPR_LESS,
100         BINEXPR_LESSEQUAL,
101         BINEXPR_GREATER,
102         BINEXPR_GREATEREQUAL,
103         BINEXPR_BITWISE_AND,
104         BINEXPR_BITWISE_OR,
105         BINEXPR_BITWISE_XOR,
106         BINEXPR_LOGICAL_AND,
107         BINEXPR_LOGICAL_OR,
108         BINEXPR_SHIFTLEFT,
109         BINEXPR_SHIFTRIGHT,
110         BINEXPR_ASSIGN,
111         BINEXPR_MUL_ASSIGN,
112         BINEXPR_DIV_ASSIGN,
113         BINEXPR_MOD_ASSIGN,
114         BINEXPR_ADD_ASSIGN,
115         BINEXPR_SUB_ASSIGN,
116         BINEXPR_SHIFTLEFT_ASSIGN,
117         BINEXPR_SHIFTRIGHT_ASSIGN,
118         BINEXPR_BITWISE_AND_ASSIGN,
119         BINEXPR_BITWISE_XOR_ASSIGN,
120         BINEXPR_BITWISE_OR_ASSIGN,
121         BINEXPR_COMMA
122 } binary_expression_type_t;
123
124 struct binary_expression_t {
125         expression_t              expression;
126         binary_expression_type_t  type;
127         expression_t             *left;
128         expression_t             *right;
129 };
130
131 struct select_expression_t {
132         expression_t   expression;
133         expression_t  *compound;
134         symbol_t      *symbol;
135
136         declaration_t *compound_entry;
137 };
138
139 struct array_access_expression_t {
140         expression_t  expression;
141         expression_t *array_ref;
142         expression_t *index;
143 };
144
145 struct sizeof_expression_t {
146         expression_t  expression;
147         type_t       *type;
148         expression_t *size_expression;
149 };
150
151 struct conditional_expression_t {
152         expression_t  expression;
153         expression_t *condition;
154         expression_t *true_expression;
155         expression_t *false_expression;
156 };
157
158 struct statement_expression_t {
159         expression_t  expression;
160         statement_t  *statement;
161 };
162
163 typedef enum {
164         STORAGE_CLASS_NONE,
165         STORAGE_CLASS_TYPEDEF,
166         STORAGE_CLASS_EXTERN,
167         STORAGE_CLASS_STATIC,
168         STORAGE_CLASS_AUTO,
169         STORAGE_CLASS_REGISTER,
170         STORAGE_CLASS_ENUM_ENTRY
171 } storage_class_t;
172
173 struct declaration_t {
174         storage_class_t     storage_class;
175         type_t             *type;
176         symbol_t           *symbol;
177         statement_t        *statement;
178         expression_t       *initializer;
179         source_position_t   source_position;
180         context_t           context;
181
182         declaration_t      *next;
183 };
184
185 typedef enum {
186         STATEMENT_INVALID,
187         STATEMENT_COMPOUND,
188         STATEMENT_RETURN,
189         STATEMENT_DECLARATION,
190         STATEMENT_IF,
191         STATEMENT_SWITCH,
192         STATEMENT_EXPRESSION,
193         STATEMENT_CONTINUE,
194         STATEMENT_BREAK,
195         STATEMENT_GOTO,
196         STATEMENT_LABEL,
197         STATEMENT_CASE_LABEL,
198         STATEMENT_WHILE,
199         STATEMENT_DO_WHILE,
200         STATEMENT_FOR
201 } statement_type_t;
202
203 struct statement_t {
204         statement_type_t   type;
205         statement_t       *next;
206         source_position_t  source_position;
207 };
208
209 struct return_statement_t {
210         statement_t   statement;
211         expression_t *return_value;
212 };
213
214 struct compound_statement_t {
215         statement_t  statement;
216         statement_t *statements;
217         context_t    context;
218 };
219
220 struct declaration_statement_t {
221         statement_t    statement;
222         declaration_t *declarations_begin;
223         declaration_t *declarations_end;
224
225         int            value_number; /**< filled in by semantic phase */
226         int            refs;
227 };
228
229 struct if_statement_t {
230         statement_t   statement;
231         expression_t *condition;
232         statement_t  *true_statement;
233         statement_t  *false_statement;
234 };
235
236 struct switch_statement_t {
237         statement_t   statement;
238         expression_t *expression;
239         statement_t  *body;
240 };
241
242 struct goto_statement_t {
243         statement_t        statement;
244         symbol_t          *label_symbol;
245         label_statement_t *label;
246 };
247
248 struct case_label_statement_t {
249         statement_t   statement;
250         expression_t *expression;
251 };
252
253 struct label_statement_t {
254         statement_t        statement;
255         symbol_t          *symbol;
256 };
257
258 struct expression_statement_t {
259         statement_t   statement;
260         expression_t *expression;
261 };
262
263 struct while_statement_t {
264         statement_t   statement;
265         expression_t *condition;
266         statement_t  *body;
267 };
268
269 struct do_while_statement_t {
270         statement_t   statement;
271         expression_t *condition;
272         statement_t  *body;
273 };
274
275 struct for_statement_t {
276         statement_t   statement;
277         expression_t  *initialisation;
278         expression_t  *condition;
279         expression_t  *step;
280         statement_t   *body;
281         context_t      context;
282 };
283
284 struct translation_unit_t {
285         context_t context;
286 };
287
288 static inline
289 void *_allocate_ast(size_t size)
290 {
291         return obstack_alloc(&ast_obstack, size);
292 }
293
294 #define allocate_ast(size)                 _allocate_ast(size)
295
296 #endif