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