]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
Get rid of ast_setfunc
[xonotic/gmqcc.git] / ast.c
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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gmqcc.h"
28 #include "ast.h"
29
30 #define ast_instantiate(T, ctx, destroyfn)                    \
31     T *self = (T*)mem_a(sizeof(T));                           \
32     ast_node_init((ast_node*)self, ctx);                      \
33     ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn;
34
35 /* It must not be possible to get here. */
36 static void _ast_node_destroy(ast_node *self)
37 {
38     fprintf(stderr, "ast node missing destroy()\n");
39     abort();
40 }
41
42 /* Initialize main ast node aprts */
43 static void ast_node_init(ast_node *self, lex_ctx ctx)
44 {
45     self->node.context = ctx;
46     self->node.destroy = &_ast_node_destroy;
47     self->node.keep    = false;
48 }
49
50 /* General expression initialization */
51 static void ast_expression_init(ast_expression *self,
52                                 ast_expression_codegen *codegen)
53 {
54     self->expression.codegen = codegen;
55 }
56
57 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t, bool keep)
58 {
59     ast_instantiate(ast_value, ctx, ast_value_delete);
60     ast_expression_init((ast_expression*)self,
61                         (ast_expression_codegen*)&ast_value_codegen);
62     self->expression.node.keep = keep;
63
64     self->name = name ? util_strdup(name) : NULL;
65     self->vtype = t;
66     self->next = NULL;
67     MEM_VECTOR_INIT(self, params);
68     self->isconst = false;
69     memset(&self->constval, 0, sizeof(self->constval));
70
71     self->ir_v = NULL;
72
73     return self;
74 }
75 MEM_VEC_FUNCTIONS(ast_value, ast_value*, params)
76
77 void ast_value_delete(ast_value* self)
78 {
79     size_t i;
80     if (self->name)
81         mem_d((void*)self->name);
82     for (i = 0; i < self->params_count; ++i)
83         ast_unref(self->params[i]);
84     MEM_VECTOR_CLEAR(self, params);
85     if (self->next) /* delete, not unref, types are always copied */
86         ast_delete(self->next);
87     if (self->isconst) {
88         switch (self->vtype)
89         {
90         case TYPE_STRING:
91             mem_d((void*)self->constval.vstring);
92             break;
93         /* NOTE: delete function? currently collected in
94          * the parser structure
95          */
96         default:
97             break;
98         }
99     }
100     mem_d(self);
101 }
102
103 bool ast_value_set_name(ast_value *self, const char *name)
104 {
105     if (self->name)
106         mem_d((void*)self->name);
107     self->name = util_strdup(name);
108     return !!self->name;
109 }
110
111 ast_binary* ast_binary_new(lex_ctx ctx, int op,
112                            ast_value* left, ast_value* right)
113 {
114     ast_instantiate(ast_binary, ctx, ast_binary_delete);
115     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
116
117     self->op = op;
118     self->left = left;
119     self->right = right;
120
121     return self;
122 }
123
124 void ast_binary_delete(ast_binary *self)
125 {
126     ast_unref(self->left);
127     ast_unref(self->right);
128     mem_d(self);
129 }
130
131 ast_store* ast_store_new(lex_ctx ctx, int op,
132                          ast_value *dest, ast_value *source)
133 {
134     ast_instantiate(ast_store, ctx, ast_store_delete);
135     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
136
137     self->op = op;
138     self->dest = dest;
139     self->source = source;
140
141     return self;
142 }
143
144 void ast_store_delete(ast_store *self)
145 {
146     ast_unref(self->dest);
147     ast_unref(self->source);
148     mem_d(self);
149 }
150
151 ast_block* ast_block_new(lex_ctx ctx)
152 {
153     ast_instantiate(ast_block, ctx, ast_block_delete);
154     ast_expression_init((ast_expression*)self,
155                         (ast_expression_codegen*)&ast_block_codegen);
156
157     MEM_VECTOR_INIT(self, locals);
158     MEM_VECTOR_INIT(self, exprs);
159
160     return self;
161 }
162 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
163 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
164
165 void ast_block_delete(ast_block *self)
166 {
167     size_t i;
168     for (i = 0; i < self->locals_count; ++i)
169         ast_delete(self->locals[i]);
170     MEM_VECTOR_CLEAR(self, locals);
171     for (i = 0; i < self->exprs_count; ++i)
172         ast_unref(self->exprs[i]);
173     MEM_VECTOR_CLEAR(self, exprs);
174     mem_d(self);
175 }
176
177 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
178 {
179     ast_instantiate(ast_function, ctx, ast_function_delete);
180
181     self->vtype = vtype;
182     self->name = name ? util_strdup(name) : NULL;
183     MEM_VECTOR_INIT(self, blocks);
184
185     return self;
186 }
187
188 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
189
190 void ast_function_delete(ast_function *self)
191 {
192     size_t i;
193     if (self->name)
194         mem_d((void*)self->name);
195     if (self->vtype)
196         ast_value_delete(self->vtype);
197     for (i = 0; i < self->blocks_count; ++i)
198         ast_delete(self->blocks[i]);
199     MEM_VECTOR_CLEAR(self, blocks);
200     mem_d(self);
201 }
202
203 /*********************************************************************/
204 /* AST codegen aprt
205  */
206
207 /* Some dummies so it compiles... */
208 bool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
209 {
210     return false;
211 }
212
213 bool ast_block_codegen(ast_block *self, ast_function *func, ir_value **out)
214 {
215     return false;
216 }
217
218 bool ast_store_codegen(ast_store *self, ast_function *func, ir_value **out)
219 {
220     return false;
221 }
222
223 bool ast_binary_codegen(ast_binary *self, ast_function *func, ir_value **out)
224 {
225     return false;
226 }