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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
27 /* Note: I will not be using a _t suffix for the
28 * "main" ast node types for now.
31 typedef union ast_node_u ast_node;
32 typedef union ast_expression_u ast_expression;
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;
42 /* Node interface with common components
44 typedef void ast_node_delete(ast_node*);
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.
56 #define ast_delete(x) ( ( (ast_node*)(x) ) -> node.destroy )((ast_node*)(x))
57 #define ast_unref(x) do \
59 if (! (((ast_node*)(x))->node.keep) ) { \
64 /* Expression interface
66 * Any expression or block returns an ir_value, and needs
67 * to know the current function.
69 typedef bool ast_expression_codegen(ast_expression*,
76 ast_expression_codegen *codegen;
77 } ast_expression_common;
81 * Types are also values, both have a type and a name.
82 * especially considering possible constructs like typedefs.
84 * is like creating a 'float foo', foo serving as the type's name.
88 ast_expression_common expression;
107 /* if vtype is qc_function, params contain parameters, and
108 * 'next' the return type.
110 MEM_VECTOR_MAKE(ast_value*, params);
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*);
116 bool ast_value_set_name(ast_value*, const char *name);
118 bool ast_value_codegen(ast_value*, ast_function*, bool lvalue, ir_value**);
122 * A value-returning binary expression.
126 ast_expression_common expression;
129 ast_expression *left;
130 ast_expression *right;
132 ast_binary* ast_binary_new(lex_ctx ctx,
134 ast_expression *left,
135 ast_expression *right);
136 void ast_binary_delete(ast_binary*);
138 bool ast_binary_codegen(ast_binary*, ast_function*, bool lvalue, ir_value**);
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)
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
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.
153 struct ast_entfield_s
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;
161 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field);
162 void ast_entfield_delete(ast_entfield*);
164 bool ast_entfield_codegen(ast_entfield*, ast_function*, bool lvalue, ir_value**);
168 * Stores left<-right and returns left.
169 * Specialized binary expression node
173 ast_expression_common expression;
175 ast_value *dest; /* When we add pointers this might have to change to expression */
176 ast_expression *source;
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*);
182 bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
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.
189 * During its 'codegen' it'll be changing the ast_function's block.
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
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;
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*);
206 bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
213 ast_expression_common expression;
215 MEM_VECTOR_MAKE(ast_value*, locals);
216 MEM_VECTOR_MAKE(ast_expression*, exprs);
218 ast_block* ast_block_new(lex_ctx ctx);
219 void ast_block_delete(ast_block*);
221 MEM_VECTOR_PROTO(ast_block, ast_value*, locals);
222 MEM_VECTOR_PROTO(ast_block, ast_expression*, exprs);
224 bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
228 * Contains a list of blocks... at least in theory.
229 * Usually there's just the main block, other blocks are inside that.
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.
236 struct ast_function_s
238 ast_node_common node;
243 ir_function *ir_func;
245 MEM_VECTOR_MAKE(ast_block*, blocks);
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*);
251 MEM_VECTOR_PROTO(ast_function, ast_block*, blocks);
253 bool ast_function_codegen(ast_function *self, ir_builder *builder);
257 union ast_expression_u
259 ast_expression_common expression;
269 ast_node_common node;
270 ast_expression expression;