0c64846143fe46fbfd42593c29fad9092dc8ae97
[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 method_parameter_t {
169         method_parameter_t *next;
170         symbol_t           *symbol;
171         type_t             *type;
172         int                 num;
173 };
174
175 struct declaration_t {
176         storage_class_t     storage_class;
177         type_t             *type;
178         symbol_t           *symbol;
179         method_parameter_t *parameters;
180         statement_t        *statement;
181         source_position_t   source_position;
182         context_t           context;
183
184         declaration_t      *next;
185 };
186
187 typedef enum {
188         STATEMENT_INVALID,
189         STATEMENT_COMPOUND,
190         STATEMENT_RETURN,
191         STATEMENT_DECLARATION,
192         STATEMENT_IF,
193         STATEMENT_EXPRESSION,
194         STATEMENT_GOTO,
195         STATEMENT_LABEL
196 } statement_type_t;
197
198 struct statement_t {
199         statement_type_t   type;
200         statement_t       *next;
201         source_position_t  source_position;
202 };
203
204 struct return_statement_t {
205         statement_t   statement;
206         expression_t *return_value;
207 };
208
209 struct compound_statement_t {
210         statement_t  statement;
211         statement_t *first_statement;
212         context_t    context;
213 };
214
215 struct declaration_statement_t {
216         statement_t    statement;
217         declaration_t  declaration;
218
219         int            value_number; /**< filled in by semantic phase */
220         int            refs;
221 };
222
223 struct if_statement_t {
224         statement_t   statement;
225         expression_t *condition;
226         statement_t  *true_statement;
227         statement_t  *false_statement;
228 };
229
230 struct goto_statement_t {
231         statement_t        statement;
232         symbol_t          *label_symbol;
233         label_statement_t *label;
234 };
235
236 struct label_statement_t {
237         statement_t        statement;
238         symbol_t          *symbol;
239 };
240
241 struct expression_statement_t {
242         statement_t   statement;
243         expression_t *expression;
244 };
245
246 struct translation_unit_t {
247         context_t context;
248 };
249
250 static inline
251 void *_allocate_ast(size_t size)
252 {
253         return obstack_alloc(&ast_obstack, size);
254 }
255
256 #define allocate_ast(size)                 _allocate_ast(size)
257
258 #endif