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