My Project
Loading...
Searching...
No Matches
ast_free.c
Go to the documentation of this file.
1#include "gwion_util.h"
2#include "gwion_ast.h"
3
4AST_FREE(StmtList*, stmt_list);
5static AST_FREE(Stmt*, stmt2);
6
7AST_FREE(Array_Sub, array_sub) {
8 if (a->exp) free_exp(p, a->exp);
9 mp_free(p, Array_Sub, a);
10}
11
12AST_FREE(Range *, range) {
13 if (a->start) free_exp(p, a->start);
14 if (a->end) free_exp(p, a->end);
15 mp_free(p, Range, a);
16}
17
19
20ANN AST_FREE(Exp_Array *, exp_array) {
21 free_array_sub(p, a->array);
22 free_exp(p, a->base);
23}
24
25ANN AST_FREE(Exp_Slice *, exp_slice) {
26 free_range(p, a->range);
27 free_exp(p, a->base);
28}
29
30AST_FREE(SpecializedList*, specialized_list) {
31 for(uint32_t i = 0; i < a->len; i++) {
32 const Specialized spec = specializedlist_at(a, i);
33 if (spec.traits) free_taglist(p, spec.traits);
34 }
35 free_specializedlist(p, a);
36}
37
38AST_FREE(Type_Decl *, type_decl) {
39 if (a->types) free_tmplarg_list(p, a->types);
40 if (a->array) free_array_sub(p, a->array);
41 if (a->fptr) free_fptr_def(p, a->fptr);
42 if (a->next) free_type_decl(p, a->next);
43 mp_free(p, Type_Decl, a);
44}
45
46static AST_FREE(Variable*, variable) {
47 if (a->td) free_type_decl(p, a->td);
48}
49
50ANN AST_FREE(Exp_Decl *, exp_decl) {
51 free_variable(p, &a->var);
52 if(a->args) free_exp(p, a->args);
53}
54
55ANN static AST_FREE(Exp_Binary *, exp_binary) {
56 free_exp(p, a->lhs);
57 free_exp(p, a->rhs);
58}
59
60ANN static AST_FREE(Exp_Cast *, exp_cast) {
61 free_type_decl(p, a->td);
62 free_exp(p, a->exp);
63}
64
65ANN static inline AST_FREE(Exp_Postfix *, exp_post) { free_exp(p, a->exp); }
66
67ANN static AST_FREE(Exp_Unary *, exp_unary) {
68 switch (a->unary_type) {
69 case unary_exp:
70// if (a->exp)
71free_exp(p, a->exp);
72 break;
73 case unary_td:
74 free_type_decl(p, a->ctor.td);
75 if (a->ctor.exp) free_exp(p, a->ctor.exp);
76 break;
77 case unary_code:
78 if (a->code) free_stmt_list(p, a->code);
79 break;
80 }
81 if (a->captures) free_capturelist(p, a->captures);
82}
83
84ANN static AST_FREE(Exp_If *, exp_if) {
85 free_exp(p, a->cond);
86 if (a->if_exp) free_exp(p, a->if_exp);
87 free_exp(p, a->else_exp);
88}
89
90AST_FREE(Tmpl *, tmpl) {
91 if (!a->call) free_specialized_list(p, a->list);
92 else free_tmplarg_list(p, a->call);
93 mp_free(p, Tmpl, a);
94}
95
96ANN AST_FREE(Func_Base *, func_base) {
97 if (a->args) free_arg_list(p, a->args);
98 if (a->td) free_type_decl(p, a->td);
99 if (a->tmpl) free_tmpl(p, a->tmpl);
100 if (a->effects.ptr) vector_release(&a->effects);
101}
102
103AST_FREE(Func_Def, func_def) {
104 free_func_base(p, a->base);
105 if (!a->builtin && a->d.code) free_stmt_list(p, a->d.code);
106 if (a->captures) free_capturelist(p, a->captures);
107 mp_free(p, Func_Def, a);
108}
109
110ANN AST_FREE(Type_Def, type_def) {
111 free_type_decl(p, a->ext);
112 if (a->when) {
113 free_exp(p, a->when);
114 if (a->when_def) free_func_def(p, a->when_def);
115 }
116 mp_free(p, Type_Def, a);
117}
118
119ANN AST_FREE(Fptr_Def, fptr_def) {
120 free_func_base(p, a->base);
121 mp_free(p, Fptr_Def, a);
122}
123
124ANN static AST_FREE(Exp_Call *, exp_call) {
125 if (a->tmpl) free_tmpl(p, a->tmpl);
126 free_exp(p, a->func);
127 if (a->args) free_exp(p, a->args);
128}
129
130ANN static AST_FREE(Exp_Dot *, exp_dot) {
131 if (a->base) free_exp(p, a->base);
132}
133
134ANN static AST_FREE(Exp_Primary *, prim) {
135 const ae_prim_t t = a->prim_type;
136 if (t == ae_prim_hack || t == ae_prim_dict || t == ae_prim_interp)
137 free_exp(p, a->d.exp);
138 else if (t == ae_prim_array)
139 free_array_sub(p, a->d.array);
140 else if (t == ae_prim_range)
141 free_range(p, a->d.range);
142}
143
144ANN static AST_FREE(Exp_Named *, exp_named) {
145 free_exp(p, a->exp);
146}
147
148ANN static AST_FREE(Type_Decl **, exp_td) { free_type_decl(p, *a); }
149DECL_EXP_FUNC(free, void, MemPool,)
150
152 free_exp_func[a->exp_type](p, &a->d);
153 Exp* next = a->next;
154 mp_free2(p, sizeof(Exp), a);
155 if (next) free_exp(p, next);
156}
157
158AST_FREE(ArgList*, arg_list) {
159 for(uint32_t i = 0; i < a->len; i++) {
160 Arg *arg = arglist_ptr_at(a, i);
161 free_variable(p, &arg->var);
162 if (arg->exp) free_exp(p, arg->exp);
163 }
164 free_arglist(p, a);
165}
166
167ANN static AST_FREE(Stmt_Code, stmt_code) {
168 if (a->stmt_list) free_stmt_list(p, a->stmt_list);
169}
170
171ANN static inline AST_FREE(struct Stmt_Exp_ *, stmt_exp) {
172 if (a->val) free_exp(p, a->val);
173}
174
175ANN static AST_FREE(struct Stmt_Flow_ *, stmt_flow) {
176 free_exp(p, a->cond);
177 free_stmt(p, a->body);
178}
179
180ANN static AST_FREE(struct Match *, stmt_case) {
181 free_exp(p, a->cond);
182 free_stmt_list(p, a->list);
183 if (a->when) free_exp(p, a->when);
184}
185
186ANN static AST_FREE(HandlerList*, handler_list) {
187 for(uint32_t i = 0; i < a->len; i++) {
188 const Handler handler = handlerlist_at(a, i);
189 free_stmt(p, handler.stmt);
190 }
191 free_handlerlist(p, a);
192}
193
194ANN static AST_FREE(struct Stmt_Try_ *, stmt_try) {
195 free_stmt(p, a->stmt);
196 free_handler_list(p, a->handler);
197}
198
199ANN static AST_FREE(struct Match *, stmt_match) {
200 free_exp(p, a->cond);
201 for(m_uint i = 0; i < a->list->len; i++) {
202 Stmt* stmt = stmtlist_ptr_at(a->list, i);
203 free_stmt_case(p, &stmt->d.stmt_match);
204 }
205 free_stmtlist(p, a->list);
206 if (a->where) free_stmt(p, a->where);
207}
208
209ANN static AST_FREE(Stmt_For, stmt_for) {
210 free_stmt(p, a->c1);
211 free_stmt(p, a->c2);
212 if (a->c3) free_exp(p, a->c3);
213 free_stmt(p, a->body);
214}
215
216ANN static AST_FREE(Stmt_Each, stmt_each) {
217 free_exp(p, a->exp);
218 free_stmt(p, a->body);
219}
220
221ANN static AST_FREE(Stmt_Loop, stmt_loop) {
222 free_exp(p, a->cond);
223 free_stmt(p, a->body);
224}
225
226ANN static AST_FREE(Stmt_If, stmt_if) {
227 free_exp(p, a->cond);
228 free_stmt(p, a->if_body);
229 if (a->else_body) free_stmt(p, a->else_body);
230}
231
232ANN AST_FREE(Enum_Def, enum_def) {
233 free_enumvaluelist(p, a->list);
234 mp_free(p, Enum_Def, a);
235}
236
238 if (a->data) xfree(a->data);
239 if (a->exp) free_exp(p, a->exp);
240}
241
242ANN static AST_FREE(Stmt_Defer, stmt_defer) { free_stmt(p, a->stmt); }
243
244ANN AST_FREE(VariableList*, variable_list) {
245 for(uint32_t i = 0; i < a->len; i++) {
246 const Variable tgt = variablelist_at(a, i);
247 free_type_decl(p, tgt.td);
248 }
249 free_variablelist(p, a);
250}
251
252ANN AST_FREE(Union_Def, union_def) {
253 free_variable_list(p, a->l);
254 mp_free(p, Union_Def, a);
255}
256
257#define free_stmt_retry (void *)dummy_func
258#define free_stmt_break (void *)dummy_func
259#define free_stmt_continue (void *)dummy_func
260#define free_stmt_return free_stmt_exp
261#define free_stmt_while free_stmt_flow
262#define free_stmt_until free_stmt_flow
263
264AST_FREE(Spread_Def, stmt_spread) {
265 free_taglist(p, a->list);
266 free_mstr(p, a->data);
267}
268
269static AST_FREE(Stmt_Using, stmt_using) {
270 if(a->tag.sym)
271 free_exp(p, a->d.exp);
272 else
273 free_type_decl(p, a->d.td);
274}
275
276static AST_FREE(Stmt_Import, stmt_import) {
277 if(a->selection) {
278 for(uint32_t i = 0; i < a->selection->len; i++) {
279 Stmt_Using item = usingstmtlist_ptr_at(a->selection, i);
280 if(item->d.exp)
281 free_exp(p, item->d.exp);
282 }
283 free_usingstmtlist(p, a->selection);
284 }
285}
286
287
288DECL_STMT_FUNC(free, void, MemPool,);
289static AST_FREE(Stmt*, stmt2) {
290 free_stmt_func[a->stmt_type](p, &a->d);
291}
292
293AST_FREE(Stmt*, stmt) {
294 free_stmt_func[a->stmt_type](p, &a->d);
295 mp_free2(p, sizeof(Stmt), a);
296}
297
298AST_FREE(StmtList*, stmt_list) {
299 for(m_uint i = 0; i < a->len; i++) {
300 Stmt stmt = stmtlist_at(a, i);
301 free_stmt2(p, &stmt);
302 }
303 free_stmtlist(p, a);
304}
305
306AST_FREE(Extend_Def, extend_def) {
307 free_type_decl(p, a->td);
308 mp_free(p, Extend_Def, a);
309}
310
311AST_FREE(Class_Def, class_def) {
312 if (a->base.ext) free_type_decl(p, a->base.ext);
313 if (a->base.tmpl) free_tmpl(p, a->base.tmpl);
314 if (a->traits) free_taglist(p, a->traits);
315 if (a->body) free_ast(p, a->body);
316 mp_free(p, Class_Def, a);
317}
318
319AST_FREE(Trait_Def, trait_def) {
320 if (a->traits) free_taglist(p, a->traits);
321 if (a->body) free_ast(p, a->body);
322 mp_free(p, Trait_Def, a);
323}
324
325AST_FREE(Prim_Def, prim_def) {
326 mp_free(p, Prim_Def, a);
327}
328
329ANN static AST_FREE(const Section *, section) {
330 const ae_section_t t = a->section_type;
331 if (t == ae_section_class)
332 free_class_def(p, a->d.class_def);
333 else if (t == ae_section_trait)
334 free_trait_def(p, a->d.trait_def);
335 else if (t == ae_section_extend)
336 free_extend_def(p, a->d.extend_def);
337 else if (t == ae_section_stmt)
338 free_stmt_list(p, a->d.stmt_list);
339 else if (t == ae_section_func)
340 free_func_def(p, a->d.func_def);
341 else if (t == ae_section_enum)
342 free_enum_def(p, a->d.enum_def);
343 else if (t == ae_section_union)
344 free_union_def(p, a->d.union_def);
345 else if (t == ae_section_fptr)
346 free_fptr_def(p, a->d.fptr_def);
347 else if (t == ae_section_type)
348 free_type_def(p, a->d.type_def);
349 else if (t == ae_section_primitive)
350 free_prim_def(p, a->d.prim_def);
351}
352
353AST_FREE(TmplArgList*, tmplarg_list) {
354 for(uint32_t i = 0; i < a->len; i++) {
355 const TmplArg arg = tmplarglist_at(a, i);
356 if(arg.type == tmplarg_td) free_type_decl(p, arg.d.td);
357 else free_exp(p, arg.d.exp);
358 }
359 free_tmplarglist(p, a);
360}
361
363 for(m_uint i = 0; i < a->len; i++) {
364 Section section = sectionlist_at(a, i);
365 free_section(p, &section);
366 }
367 free_sectionlist(p, a);
368}
ANN void free_variablelist(MemPool p, VariableList *)
#define AST_FREE(type, name)
Definition absyn.h:11
ANN void free_tmpl(MemPool p, Tmpl *)
ANN void free_stmt_list(MemPool, StmtList *)
ANN void free_ast(MemPool p, Ast)
ae_prim_t
Definition absyn.h:422
@ ae_prim_array
Definition absyn.h:427
@ ae_prim_hack
Definition absyn.h:430
@ ae_prim_interp
Definition absyn.h:433
@ ae_prim_range
Definition absyn.h:428
@ ae_prim_dict
Definition absyn.h:429
ANN void free_trait_def(MemPool p, Trait_Def)
ANEW stmt_pp
Definition absyn.h:235
ANN void free_stmt(MemPool p, Stmt *)
ANN void free_class_def(MemPool p, Class_Def)
ANN void free_union_def(MemPool p, Union_Def)
ANN void free_type_decl(MemPool p, Type_Decl *)
ANN void free_specialized_list(MemPool p, SpecializedList *)
exp_lambda
Definition absyn.h:347
struct SectionList * Ast
Definition absyn.h:23
@ unary_code
Definition absyn.h:522
@ unary_exp
Definition absyn.h:522
@ unary_td
Definition absyn.h:522
ANN void free_fptr_def(MemPool p, Fptr_Def)
ANN void free_type_def(MemPool p, Type_Def)
@ tmplarg_td
Definition absyn.h:254
ANN void free_func_def(MemPool p, Func_Def)
ANN void free_tmplarg_list(MemPool p, TmplArgList *)
ae_section_t
Definition absyn.h:824
@ ae_section_primitive
Definition absyn.h:834
@ ae_section_class
Definition absyn.h:827
@ ae_section_enum
Definition absyn.h:830
@ ae_section_union
Definition absyn.h:831
@ ae_section_fptr
Definition absyn.h:832
@ ae_section_func
Definition absyn.h:826
@ ae_section_type
Definition absyn.h:833
@ ae_section_stmt
Definition absyn.h:825
@ ae_section_extend
Definition absyn.h:829
@ ae_section_trait
Definition absyn.h:828
ANN void free_enum_def(MemPool p, Enum_Def)
Exp *ANN void free_array_sub(MemPool p, Array_Sub)
ANN void free_exp(MemPool p, Exp *)
ANN void free_arg_list(MemPool p, ArgList *)
ANN void free_func_base(MemPool p, Func_Base *)
ANEW const Type_Decl Exp * exp
Definition absyn.h:680
ANN void free_extend_def(MemPool p, Extend_Def)
ANN void free_prim_def(MemPool p, Prim_Def)
#define DECL_STMT_FUNC(prefix, type, arg, mod)
Definition ast_pass.h:47
#define DECL_EXP_FUNC(prefix, type, arg, mod)
Definition ast_pass.h:28
include this file to use gwion-ast library
Definition absyn.h:310
Exp * exp
Definition absyn.h:312
Variable var
Definition absyn.h:311
array_subscript.
Definition absyn.h:352
array expression.
Definition absyn.h:362
a dot expression.
Definition absyn.h:329
a lambda expression.
Definition absyn.h:343
slice.
Definition absyn.h:379
Definition absyn.h:559
Exp * next
Definition absyn.h:578
Stmt * stmt
Definition absyn.h:95
Definition absyn.h:53
range.
Definition absyn.h:371
TagList * traits
Definition absyn.h:389
Exp * exp
Definition absyn.h:137
union Stmt_Using_::@2 d
Definition absyn.h:187
union Stmt::stmt_data d
Exp * exp
Definition absyn.h:261
enum tmplarg_t type
Definition absyn.h:263
union TmplArg::@3 d
Type_Decl * td
Definition absyn.h:260
Definition absyn.h:478
variable declaration
Definition absyn.h:244
Type_Decl * td
Definition absyn.h:245
struct Match stmt_match
Definition absyn.h:197