1 |
|
|
#include "gwion_util.h" |
2 |
|
|
#include "gwion_ast.h" |
3 |
|
|
#include "gwion_env.h" |
4 |
|
|
#include "vm.h" |
5 |
|
|
#include "gwion.h" |
6 |
|
|
#define __CLEAN_IMPL |
7 |
|
|
#include "clean.h" |
8 |
|
|
|
9 |
|
139 |
ANN static void clean_array_sub(Clean *a, Array_Sub b) { |
10 |
✓✓ |
139 |
if(b->exp) |
11 |
|
107 |
clean_exp(a, b->exp); |
12 |
|
139 |
} |
13 |
|
|
|
14 |
|
1787 |
ANN static void clean_id_list(Clean *a, ID_List b) { |
15 |
✓✓ |
1787 |
if(b->next) |
16 |
|
49 |
clean_id_list(a, b->next); |
17 |
|
1787 |
} |
18 |
|
|
|
19 |
|
302 |
ANN static void clean_type_list(Clean *a, Type_List b) { |
20 |
|
302 |
clean_type_decl(a, b->td); |
21 |
✓✓ |
302 |
if(b->next) |
22 |
|
31 |
clean_type_list(a, b->next); |
23 |
|
302 |
} |
24 |
|
|
|
25 |
|
1766 |
ANN static void clean_tmpl(Clean *a, Tmpl *b) { |
26 |
✓✓ |
1766 |
if(b->list) |
27 |
|
1729 |
clean_id_list(a, b->list); |
28 |
✓✓ |
1766 |
if(b->call) |
29 |
|
191 |
clean_type_list(a, b->call); |
30 |
|
1766 |
} |
31 |
|
|
|
32 |
|
12 |
ANN static void clean_range(Clean *a, Range *b) { |
33 |
✓✓ |
12 |
if(b->start) |
34 |
|
11 |
clean_exp(a, b->start); |
35 |
✓✓ |
12 |
if(b->end) |
36 |
|
8 |
clean_exp(a, b->end); |
37 |
|
12 |
} |
38 |
|
|
|
39 |
|
4982 |
ANN static void clean_type_decl(Clean *a, Type_Decl *b) { |
40 |
✓✓ |
4982 |
if(b->exp) |
41 |
|
12 |
clean_exp(a, b->exp); |
42 |
✓✓ |
4982 |
if(b->array) |
43 |
|
28 |
clean_array_sub(a, b->array); |
44 |
✓✓ |
4982 |
if(b->types) |
45 |
|
80 |
clean_type_list(a, b->types); |
46 |
✓✓ |
4982 |
if(b->next) |
47 |
|
19 |
clean_type_decl(a, b->next); |
48 |
|
4982 |
} |
49 |
|
|
|
50 |
|
4755 |
ANN static void clean_prim(Clean *a, Exp_Primary *b) { |
51 |
✓✓✓✓
|
8396 |
if(b->prim_type == ae_prim_hack || |
52 |
✓✓ |
7243 |
b->prim_type == ae_prim_typeof || |
53 |
|
3602 |
b->prim_type == ae_prim_interp) |
54 |
|
1158 |
clean_exp(a, b->d.exp); |
55 |
✓✓ |
3597 |
else if(b->prim_type == ae_prim_array) |
56 |
|
26 |
clean_array_sub(a, b->d.array); |
57 |
✓✓ |
3571 |
else if(b->prim_type == ae_prim_range) |
58 |
|
3 |
clean_range(a, b->d.range); |
59 |
|
4755 |
} |
60 |
|
|
|
61 |
|
2638 |
ANN static void clean_var_decl(Clean *a, Var_Decl b) { |
62 |
✓✓ |
2638 |
if(b->array) |
63 |
|
53 |
clean_array_sub(a, b->array); |
64 |
✓✓✓✓
|
2638 |
if(a->scope && b->value) |
65 |
|
200 |
REM_REF(b->value, a->gwion) |
66 |
|
2638 |
} |
67 |
|
|
|
68 |
|
2316 |
ANN static void clean_var_decl_list(Clean *a, Var_Decl_List b) { |
69 |
|
2316 |
clean_var_decl(a, b->self); |
70 |
✓✓ |
2316 |
if(b->next) |
71 |
|
10 |
clean_var_decl_list(a, b->next); |
72 |
|
2316 |
} |
73 |
|
|
|
74 |
|
2306 |
ANN static void clean_exp_decl(Clean *a, Exp_Decl *b) { |
75 |
✓✗ |
2306 |
if(b->td) |
76 |
|
2306 |
clean_type_decl(a, b->td); |
77 |
|
2306 |
clean_var_decl_list(a, b->list); |
78 |
|
2306 |
} |
79 |
|
|
|
80 |
|
593 |
ANN static void clean_exp_binary(Clean *a, Exp_Binary *b) { |
81 |
|
593 |
clean_exp(a, b->lhs); |
82 |
|
593 |
clean_exp(a, b->rhs); |
83 |
|
593 |
} |
84 |
|
|
|
85 |
|
151 |
ANN static void clean_exp_unary(Clean *a, Exp_Unary *b) { |
86 |
✓✓ |
151 |
if(b->exp) |
87 |
|
96 |
clean_exp(a, b->exp); |
88 |
✓✓ |
151 |
if(b->td) |
89 |
|
27 |
clean_type_decl(a, b->td); |
90 |
✓✓ |
151 |
if(b->code) |
91 |
|
28 |
clean_stmt(a, b->code); |
92 |
|
151 |
} |
93 |
|
|
|
94 |
|
47 |
ANN static void clean_exp_cast(Clean *a, Exp_Cast *b) { |
95 |
|
47 |
clean_type_decl(a, b->td); |
96 |
|
47 |
clean_exp(a, b->exp); |
97 |
|
47 |
} |
98 |
|
|
|
99 |
|
40 |
ANN static void clean_exp_post(Clean *a, Exp_Postfix *b) { |
100 |
|
40 |
clean_exp(a, b->exp); |
101 |
|
40 |
} |
102 |
|
|
|
103 |
|
634 |
ANN static void clean_exp_call(Clean *a, Exp_Call *b) { |
104 |
|
634 |
clean_exp(a, b->func); |
105 |
✓✓ |
634 |
if(b->args) |
106 |
|
372 |
clean_exp(a, b->args); |
107 |
✓✓ |
634 |
if(b->tmpl) |
108 |
|
105 |
clean_tmpl(a, b->tmpl); |
109 |
|
634 |
} |
110 |
|
|
|
111 |
|
32 |
ANN static void clean_exp_array(Clean *a, Exp_Array *b) { |
112 |
|
32 |
clean_exp(a, b->base); |
113 |
|
32 |
clean_array_sub(a, b->array); |
114 |
|
32 |
} |
115 |
|
|
|
116 |
|
9 |
ANN static void clean_exp_slice(Clean *a, Exp_Slice *b) { |
117 |
|
9 |
clean_exp(a, b->base); |
118 |
|
9 |
clean_range(a, b->range); |
119 |
|
9 |
} |
120 |
|
|
|
121 |
|
13 |
ANN static void clean_exp_if(Clean *a, Exp_If *b) { |
122 |
|
13 |
clean_exp(a, b->cond); |
123 |
✓✓ |
13 |
if(b->if_exp) |
124 |
|
12 |
clean_exp(a, b->if_exp); |
125 |
|
13 |
clean_exp(a, b->else_exp); |
126 |
|
13 |
} |
127 |
|
|
|
128 |
|
570 |
ANN static void clean_exp_dot(Clean *a, Exp_Dot *b) { |
129 |
|
570 |
clean_exp(a, b->base); |
130 |
|
570 |
} |
131 |
|
|
|
132 |
|
19 |
ANN static void clean_exp_lambda(Clean *a, Exp_Lambda *b) { |
133 |
|
19 |
clean_func_def(a, b->def); |
134 |
|
19 |
} |
135 |
|
|
|
136 |
|
|
DECL_EXP_FUNC(clean, void, Clean*) |
137 |
|
9169 |
ANN static void clean_exp(Clean *a, Exp b) { |
138 |
|
9169 |
clean_exp_func[b->exp_type](a, &b->d); |
139 |
✓✓ |
9169 |
if(b->next) |
140 |
|
555 |
clean_exp(a, b->next); |
141 |
|
9169 |
} |
142 |
|
|
|
143 |
|
4065 |
ANN static void clean_stmt_exp(Clean *a, Stmt_Exp b) { |
144 |
✓✓ |
4065 |
if(b->val) |
145 |
|
4033 |
clean_exp(a, b->val); |
146 |
|
4065 |
} |
147 |
|
|
|
148 |
|
28 |
ANN static void clean_stmt_flow(Clean *a, Stmt_Flow b) { |
149 |
|
28 |
++a->scope; |
150 |
|
28 |
clean_exp(a, b->cond); |
151 |
|
28 |
clean_stmt(a, b->body); |
152 |
|
28 |
--a->scope; |
153 |
|
28 |
} |
154 |
|
|
|
155 |
|
|
#define clean_stmt_while clean_stmt_flow |
156 |
|
|
#define clean_stmt_until clean_stmt_flow |
157 |
|
|
|
158 |
|
13 |
ANN static void clean_stmt_for(Clean *a, Stmt_For b) { |
159 |
|
13 |
++a->scope; |
160 |
|
13 |
clean_stmt(a, b->c1); |
161 |
✓✗ |
13 |
if(b->c2) |
162 |
|
13 |
clean_stmt(a, b->c2); |
163 |
✓✓ |
13 |
if(b->c3) |
164 |
|
11 |
clean_exp(a, b->c3); |
165 |
|
13 |
clean_stmt(a, b->body); |
166 |
|
13 |
--a->scope; |
167 |
|
13 |
} |
168 |
|
|
|
169 |
|
13 |
ANN static void clean_stmt_auto(Clean *a, Stmt_Auto b) { |
170 |
|
13 |
++a->scope; |
171 |
|
13 |
clean_exp(a, b->exp); |
172 |
|
13 |
clean_stmt(a, b->body); |
173 |
✓✓ |
13 |
if(b->v) |
174 |
|
10 |
REM_REF(b->v, a->gwion) |
175 |
|
13 |
--a->scope; |
176 |
|
13 |
} |
177 |
|
|
|
178 |
|
10 |
ANN static void clean_stmt_loop(Clean *a, Stmt_Loop b) { |
179 |
|
10 |
++a->scope; |
180 |
|
10 |
clean_exp(a, b->cond); |
181 |
|
10 |
clean_stmt(a, b->body); |
182 |
|
10 |
--a->scope; |
183 |
|
10 |
} |
184 |
|
|
|
185 |
|
49 |
ANN static void clean_stmt_if(Clean *a, Stmt_If b) { |
186 |
|
49 |
++a->scope; |
187 |
|
49 |
clean_exp(a, b->cond); |
188 |
|
49 |
clean_stmt(a, b->if_body); |
189 |
✓✓ |
49 |
if(b->else_body) |
190 |
|
14 |
clean_stmt(a, b->else_body); |
191 |
|
49 |
--a->scope; |
192 |
|
49 |
} |
193 |
|
|
|
194 |
|
536 |
ANN static void clean_stmt_code(Clean *a, Stmt_Code b) { |
195 |
|
536 |
++a->scope; |
196 |
✓✓ |
536 |
if(b->stmt_list) |
197 |
|
451 |
clean_stmt_list(a, b->stmt_list); |
198 |
|
536 |
--a->scope; |
199 |
|
536 |
} |
200 |
|
|
|
201 |
|
11 |
ANN static void clean_stmt_varloop(Clean *a, Stmt_VarLoop b) { |
202 |
|
11 |
++a->scope; |
203 |
|
11 |
clean_exp(a, b->exp); |
204 |
|
11 |
clean_stmt(a, b->body); |
205 |
|
11 |
--a->scope; |
206 |
|
11 |
} |
207 |
|
|
|
208 |
|
74 |
ANN static void clean_stmt_return(Clean *a, Stmt_Exp b) { |
209 |
✓✓ |
74 |
if(b->val) |
210 |
|
70 |
clean_exp(a, b->val); |
211 |
|
74 |
} |
212 |
|
|
|
213 |
|
15 |
ANN static void clean_case_list(Clean *a, Stmt_List b) { |
214 |
|
15 |
clean_stmt_case(a, &b->stmt->d.stmt_match); |
215 |
✓✓ |
15 |
if(b->next) |
216 |
|
6 |
clean_case_list(a, b->next); |
217 |
|
15 |
} |
218 |
|
|
|
219 |
|
9 |
ANN static void clean_stmt_match(Clean *a, Stmt_Match b) { |
220 |
|
9 |
++a->scope; |
221 |
|
9 |
clean_exp(a, b->cond); |
222 |
|
9 |
clean_case_list(a, b->list); |
223 |
✓✓ |
9 |
if(b->where) |
224 |
|
4 |
clean_stmt(a, b->where); |
225 |
|
9 |
--a->scope; |
226 |
|
9 |
} |
227 |
|
|
|
228 |
|
15 |
ANN static void clean_stmt_case(Clean *a, Stmt_Match b) { |
229 |
|
15 |
++a->scope; |
230 |
|
15 |
clean_exp(a, b->cond); |
231 |
|
15 |
clean_stmt_list(a, b->list); |
232 |
✓✓ |
15 |
if(b->when) |
233 |
|
8 |
clean_exp(a, b->when); |
234 |
|
15 |
--a->scope; |
235 |
|
15 |
} |
236 |
|
|
|
237 |
|
|
|
238 |
|
41 |
ANN static void clean_dummy(Clean *a NUSED, void *b NUSED) {} |
239 |
|
|
#define clean_stmt_jump clean_dummy |
240 |
|
|
#define clean_stmt_pp clean_dummy |
241 |
|
|
#define clean_stmt_break clean_dummy |
242 |
|
|
#define clean_stmt_continue clean_dummy |
243 |
|
|
|
244 |
|
|
DECL_STMT_FUNC(clean, void, Clean*) |
245 |
|
4849 |
ANN static void clean_stmt(Clean *a, Stmt b) { |
246 |
|
4849 |
clean_stmt_func[b->stmt_type](a, &b->d); |
247 |
|
4849 |
} |
248 |
|
|
|
249 |
|
322 |
ANN static void clean_arg_list(Clean *a, Arg_List b) { |
250 |
✓✓ |
322 |
if(b->td) |
251 |
|
306 |
clean_type_decl(a, b->td); |
252 |
|
322 |
clean_var_decl(a, b->var_decl); |
253 |
✓✓ |
322 |
if(b->next) |
254 |
|
77 |
clean_arg_list(a, b->next); |
255 |
|
322 |
} |
256 |
|
|
|
257 |
|
37 |
ANN static void clean_decl_list(Clean *a, Decl_List b) { |
258 |
|
37 |
clean_exp(a, b->self); |
259 |
✓✓ |
37 |
if(b->next) |
260 |
|
11 |
clean_decl_list(a, b->next); |
261 |
|
37 |
} |
262 |
|
|
|
263 |
|
4281 |
ANN static void clean_stmt_list(Clean *a, Stmt_List b) { |
264 |
|
4281 |
clean_stmt(a, b->stmt); |
265 |
✓✓ |
4281 |
if(b->next) |
266 |
|
1616 |
clean_stmt_list(a, b->next); |
267 |
|
4281 |
} |
268 |
|
|
|
269 |
|
454 |
ANN static void clean_func_base(Clean *a, Func_Base *b) { |
270 |
✓✓ |
454 |
if(b->td) |
271 |
|
440 |
clean_type_decl(a, b->td); |
272 |
✓✓ |
454 |
if(b->args) |
273 |
|
245 |
clean_arg_list(a, b->args); |
274 |
✓✓ |
454 |
if(b->tmpl) |
275 |
|
126 |
clean_tmpl(a, b->tmpl); |
276 |
|
454 |
} |
277 |
|
|
|
278 |
|
381 |
ANN static void clean_func_def(Clean *a, Func_Def b) { |
279 |
|
381 |
clean_func_base(a, b->base); |
280 |
|
381 |
++a->scope; |
281 |
✓✗✓✓
|
381 |
if(b->d.code && !GET_FLAG(b, builtin)) |
282 |
|
372 |
clean_stmt(a, b->d.code); |
283 |
|
381 |
--a->scope; |
284 |
|
381 |
} |
285 |
|
|
|
286 |
|
44 |
ANN void func_def_cleaner(const Gwion gwion, Func_Def b) { |
287 |
|
44 |
Clean a = { .gwion=gwion }; |
288 |
|
44 |
clean_func_def(&a, b); |
289 |
✓✓ |
44 |
if(GET_FLAG(b, builtin)) |
290 |
|
4 |
b->d.code = NULL; |
291 |
|
44 |
free_func_def(gwion->mp, b); |
292 |
|
44 |
} |
293 |
|
|
|
294 |
|
1768 |
ANN static void clean_class_def(Clean *a, Class_Def b) { |
295 |
|
1768 |
clean_type_def(a, &b->base); |
296 |
✓✓ |
1768 |
if(b->body) |
297 |
|
1686 |
clean_ast(a, b->body); |
298 |
|
1768 |
} |
299 |
|
|
|
300 |
|
1500 |
ANN void class_def_cleaner(const Gwion gwion, Class_Def b) { |
301 |
|
1500 |
Clean a = { .gwion=gwion }; |
302 |
|
1500 |
clean_class_def(&a, b); |
303 |
|
1500 |
free_class_def(gwion->mp, b); |
304 |
|
1500 |
} |
305 |
|
|
|
306 |
|
9 |
ANN static void clean_enum_def(Clean *a, Enum_Def b) { |
307 |
|
9 |
clean_id_list(a, b->list); |
308 |
|
9 |
} |
309 |
|
|
|
310 |
|
26 |
ANN static void clean_union_def(Clean *a, Union_Def b) { |
311 |
|
26 |
clean_decl_list(a, b->l); |
312 |
✓✓ |
26 |
if(b->tmpl) |
313 |
|
5 |
clean_tmpl(a, b->tmpl); |
314 |
|
26 |
} |
315 |
|
|
|
316 |
|
73 |
ANN static void clean_fptr_def(Clean *a, Fptr_Def b) { |
317 |
|
73 |
clean_func_base(a, b->base); |
318 |
✓✓ |
73 |
if(b->type) |
319 |
|
69 |
REM_REF(b->type, a->gwion) |
320 |
|
73 |
} |
321 |
|
|
|
322 |
|
1790 |
ANN static void clean_type_def(Clean *a, Type_Def b) { |
323 |
✓✓ |
1790 |
if(b->ext) |
324 |
|
1535 |
clean_type_decl(a, b->ext); |
325 |
✓✓ |
1790 |
if(b->tmpl) |
326 |
|
1530 |
clean_tmpl(a, b->tmpl); |
327 |
|
1790 |
} |
328 |
|
|
|
329 |
|
|
DECL_SECTION_FUNC(clean, void, Clean*) |
330 |
|
|
|
331 |
|
2915 |
ANN static inline void clean_section(Clean *a, Section *b) { |
332 |
|
2915 |
clean_section_func[b->section_type](a, *(void**)&b->d); |
333 |
|
2915 |
} |
334 |
|
|
|
335 |
|
2915 |
ANN static void clean_ast(Clean *a, Ast b) { |
336 |
|
2915 |
clean_section(a, b->section); |
337 |
✓✓ |
2915 |
if(b->next) |
338 |
|
611 |
clean_ast(a, b->next); |
339 |
|
2915 |
} |
340 |
|
|
|
341 |
|
618 |
ANN void ast_cleaner(const Gwion gwion, Ast b) { |
342 |
|
618 |
Clean a = { .gwion=gwion }; |
343 |
|
618 |
clean_ast(&a, b); |
344 |
|
618 |
free_ast(gwion->mp, b); |
345 |
|
618 |
} |