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