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