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