]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.h
if-then-else AST node - this one is not for ternary expressions
[xonotic/gmqcc.git] / ast.h
1 /*
2  * Copyright (C) 2012
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef GMQCC_AST_HDR
24 #define GMQCC_AST_HDR
25 #include "ir.h"
26
27 /* Note: I will not be using a _t suffix for the
28  * "main" ast node types for now.
29  */
30
31 typedef union ast_node_u ast_node;
32 typedef union ast_expression_u ast_expression;
33
34 typedef struct ast_value_s      ast_value;
35 typedef struct ast_function_s   ast_function;
36 typedef struct ast_block_s      ast_block;
37 typedef struct ast_binary_s     ast_binary;
38 typedef struct ast_store_s      ast_store;
39 typedef struct ast_entfield_s   ast_entfield;
40 typedef struct ast_ifthen_s     ast_ifthen;
41
42 /* Node interface with common components
43  */
44 typedef void ast_node_delete(ast_node*);
45 typedef struct
46 {
47     lex_ctx          context;
48     /* I don't feel comfortable using keywords like 'delete' as names... */
49     ast_node_delete *destroy;
50     /* keep: if a node contains this node, 'keep'
51      * prevents its dtor from destroying this node as well.
52      */
53     bool             keep;
54 } ast_node_common;
55
56 #define ast_delete(x) ( ( (ast_node*)(x) ) -> node.destroy )((ast_node*)(x))
57 #define ast_unref(x) do                     \
58 {                                           \
59     if (! (((ast_node*)(x))->node.keep) ) { \
60         ast_delete(x);                      \
61     }                                       \
62 } while(0)
63
64 /* Expression interface
65  *
66  * Any expression or block returns an ir_value, and needs
67  * to know the current function.
68  */
69 typedef bool ast_expression_codegen(ast_expression*,
70                                     ast_function*,
71                                     bool lvalue,
72                                     ir_value**);
73 typedef struct
74 {
75     ast_node_common         node;
76     ast_expression_codegen *codegen;
77 } ast_expression_common;
78
79 /* Value
80  *
81  * Types are also values, both have a type and a name.
82  * especially considering possible constructs like typedefs.
83  * typedef float foo;
84  * is like creating a 'float foo', foo serving as the type's name.
85  */
86 struct ast_value_s
87 {
88     ast_expression_common expression;
89
90     const char *name;
91
92     int         vtype;
93     ast_value  *next;
94
95     bool isconst;
96     union {
97         double        vfloat;
98         int           vint;
99         vector        vvec;
100         const char   *vstring;
101         int           ventity;
102         ast_function *vfunc;
103     } constval;
104
105     ir_value *ir_v;
106
107     /* if vtype is qc_function, params contain parameters, and
108      * 'next' the return type.
109      */
110     MEM_VECTOR_MAKE(ast_value*, params);
111 };
112 ast_value* ast_value_new(lex_ctx ctx, const char *name, int qctype);
113 /* This will NOT delete an underlying ast_function */
114 void ast_value_delete(ast_value*);
115
116 bool ast_value_set_name(ast_value*, const char *name);
117
118 bool ast_value_codegen(ast_value*, ast_function*, bool lvalue, ir_value**);
119
120 /* Binary
121  *
122  * A value-returning binary expression.
123  */
124 struct ast_binary_s
125 {
126     ast_expression_common expression;
127
128     int             op;
129     ast_expression *left;
130     ast_expression *right;
131 };
132 ast_binary* ast_binary_new(lex_ctx    ctx,
133                            int        op,
134                            ast_expression *left,
135                            ast_expression *right);
136 void ast_binary_delete(ast_binary*);
137
138 bool ast_binary_codegen(ast_binary*, ast_function*, bool lvalue, ir_value**);
139
140 /* Entity-field
141  *
142  * This must do 2 things:
143  * -) Provide a way to fetch an entity field value. (Rvalue)
144  * -) Provide a pointer to an entity field. (Lvalue)
145  * The problem:
146  * In original QC, there's only a STORE via pointer, but
147  * no LOAD via pointer.
148  * So we must know beforehand if we are going to read or assign
149  * the field.
150  * For this we will have to extend the codegen() functions with
151  * a flag saying whether or not we need an L or an R-value.
152  */
153 struct ast_entfield_s
154 {
155     ast_expression_common expression;
156     /* The entity can come from an expression of course. */
157     ast_expression *entity;
158     /* As can the field, it just must result in a value of TYPE_FIELD */
159     ast_expression *field;
160 };
161 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field);
162 void ast_entfield_delete(ast_entfield*);
163
164 bool ast_entfield_codegen(ast_entfield*, ast_function*, bool lvalue, ir_value**);
165
166 /* Store
167  *
168  * Stores left<-right and returns left.
169  * Specialized binary expression node
170  */
171 struct ast_store_s
172 {
173     ast_expression_common expression;
174     int             op;
175     ast_value      *dest; /* When we add pointers this might have to change to expression */
176     ast_expression *source;
177 };
178 ast_store* ast_store_new(lex_ctx ctx, int op,
179                          ast_value *d, ast_expression *s);
180 void ast_store_delete(ast_store*);
181
182 bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
183
184 /* If
185  *
186  * A general 'if then else' statement, either side can be NULL and will
187  * thus be omitted. It is an error for *both* cases to be NULL at once.
188  *
189  * During its 'codegen' it'll be changing the ast_function's block.
190  *
191  * An if is also an "expression". Its codegen will put NULL into the
192  * output field though. For ternary expressions an ast_ternary will be
193  * added.
194  */
195 struct ast_ifthen_s
196 {
197     ast_expression_common expression;
198     ast_expression *cond;
199     /* It's all just 'expressions', since an ast_block is one too. */
200     ast_expression *on_true;
201     ast_expression *on_false;
202 };
203 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
204 void ast_ifthen_delete(ast_ifthen*);
205
206 bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
207
208 /* Blocks
209  *
210  */
211 struct ast_block_s
212 {
213     ast_expression_common expression;
214
215     MEM_VECTOR_MAKE(ast_value*,      locals);
216     MEM_VECTOR_MAKE(ast_expression*, exprs);
217 };
218 ast_block* ast_block_new(lex_ctx ctx);
219 void ast_block_delete(ast_block*);
220
221 MEM_VECTOR_PROTO(ast_block, ast_value*, locals);
222 MEM_VECTOR_PROTO(ast_block, ast_expression*, exprs);
223
224 bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
225
226 /* Function
227  *
228  * Contains a list of blocks... at least in theory.
229  * Usually there's just the main block, other blocks are inside that.
230  *
231  * Technically, functions don't need to be an AST node, since we have
232  * neither functions inside functions, nor lambdas, and function
233  * pointers could just work with a name. However, this way could be
234  * more flexible, and adds no real complexity.
235  */
236 struct ast_function_s
237 {
238     ast_node_common node;
239
240     ast_value  *vtype;
241     const char *name;
242
243     ir_function *ir_func;
244
245     MEM_VECTOR_MAKE(ast_block*, blocks);
246 };
247 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype);
248 /* This will NOT delete the underlying ast_value */
249 void ast_function_delete(ast_function*);
250
251 MEM_VECTOR_PROTO(ast_function, ast_block*, blocks);
252
253 bool ast_function_codegen(ast_function *self, ir_builder *builder);
254
255 /* Expression union
256  */
257 union ast_expression_u
258 {
259     ast_expression_common expression;
260
261     ast_binary binary;
262     ast_block  block;
263 };
264
265 /* Node union
266  */
267 union ast_node_u
268 {
269     ast_node_common node;
270     ast_expression  expression;
271 };
272
273 #endif