GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/clean.c Lines: 276 276 100.0 %
Date: 2020-09-14 20:46:08 Branches: 105 108 97.2 %

Line Branch Exec Source
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
133
ANN static void clean_array_sub(Clean *a, Array_Sub b) {
10
133
  if(b->exp)
11
109
    clean_exp(a, b->exp);
12
133
}
13
14
1768
ANN static void clean_id_list(Clean *a, ID_List b) {
15
1768
  if(b->next)
16
42
    clean_id_list(a, b->next);
17
1768
}
18
19
287
ANN static void clean_type_list(Clean *a, Type_List b) {
20
287
  clean_type_decl(a, b->td);
21
287
  if(b->next)
22
29
    clean_type_list(a, b->next);
23
287
}
24
25
1752
ANN static void clean_tmpl(Clean *a, Tmpl *b) {
26
1752
  if(b->list)
27
1717
    clean_id_list(a, b->list);
28
1752
  if(b->call)
29
180
    clean_type_list(a, b->call);
30
1752
}
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
4939
ANN static void clean_type_decl(Clean *a, Type_Decl *b) {
40
4939
  if(b->exp)
41
12
    clean_exp(a, b->exp);
42
4939
  if(b->array)
43
21
    clean_array_sub(a, b->array);
44
4939
  if(b->types)
45
78
    clean_type_list(a, b->types);
46
4939
  if(b->next)
47
19
    clean_type_decl(a, b->next);
48
4939
}
49
50
4746
ANN static void clean_prim(Clean *a, Exp_Primary *b) {
51

8380
  if(b->prim_type == ae_prim_hack   ||
52
7229
     b->prim_type == ae_prim_typeof ||
53
3595
     b->prim_type == ae_prim_interp)
54
1154
    clean_exp(a, b->d.exp);
55
3592
  else if(b->prim_type == ae_prim_array)
56
26
    clean_array_sub(a, b->d.array);
57
3566
  else if(b->prim_type == ae_prim_range)
58
3
    clean_range(a, b->d.range);
59
4746
}
60
61
2624
ANN static void clean_var_decl(Clean *a, Var_Decl b) {
62
2624
  if(b->array)
63
54
    clean_array_sub(a, b->array);
64

2624
  if(a->scope && b->value)
65
198
    REM_REF(b->value, a->gwion)
66
2624
}
67
68
2306
ANN static void clean_var_decl_list(Clean *a, Var_Decl_List b) {
69
2306
  clean_var_decl(a, b->self);
70
2306
  if(b->next)
71
11
    clean_var_decl_list(a, b->next);
72
2306
}
73
74
2295
ANN static void clean_exp_decl(Clean *a, Exp_Decl *b) {
75
2295
  if(b->td)
76
2295
    clean_type_decl(a, b->td);
77
2295
  clean_var_decl_list(a, b->list);
78
2295
}
79
80
596
ANN static void clean_exp_binary(Clean *a, Exp_Binary *b) {
81
596
  clean_exp(a, b->lhs);
82
596
  clean_exp(a, b->rhs);
83
596
}
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
44
ANN static void clean_exp_cast(Clean *a, Exp_Cast *b) {
95
44
  clean_type_decl(a, b->td);
96
44
  clean_exp(a, b->exp);
97
44
}
98
99
41
ANN static void clean_exp_post(Clean *a, Exp_Postfix *b) {
100
41
  clean_exp(a, b->exp);
101
41
}
102
103
633
ANN static void clean_exp_call(Clean *a, Exp_Call *b) {
104
633
  clean_exp(a, b->func);
105
633
  if(b->args)
106
371
    clean_exp(a, b->args);
107
633
  if(b->tmpl)
108
103
    clean_tmpl(a, b->tmpl);
109
633
}
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
9149
ANN static void clean_exp(Clean *a, Exp b) {
138
9149
  clean_exp_func[b->exp_type](a, &b->d);
139
9149
  if(b->next)
140
546
    clean_exp(a, b->next);
141
9149
}
142
143
4054
ANN static void clean_stmt_exp(Clean *a, Stmt_Exp b) {
144
4054
  if(b->val)
145
4022
    clean_exp(a, b->val);
146
4054
}
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
14
ANN static void clean_stmt_auto(Clean *a, Stmt_Auto b) {
170
14
  ++a->scope;
171
14
  clean_exp(a, b->exp);
172
14
  clean_stmt(a, b->body);
173
14
  if(b->v)
174
11
    REM_REF(b->v, a->gwion)
175
14
  --a->scope;
176
14
}
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
47
ANN static void clean_stmt_if(Clean *a, Stmt_If b) {
186
47
  ++a->scope;
187
47
  clean_exp(a, b->cond);
188
47
  clean_stmt(a, b->if_body);
189
47
  if(b->else_body)
190
14
    clean_stmt(a, b->else_body);
191
47
  --a->scope;
192
47
}
193
194
535
ANN static void clean_stmt_code(Clean *a, Stmt_Code b) {
195
535
  ++a->scope;
196
535
  if(b->stmt_list)
197
450
    clean_stmt_list(a, b->stmt_list);
198
535
  --a->scope;
199
535
}
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
75
ANN static void clean_stmt_return(Clean *a, Stmt_Exp b) {
209
75
  if(b->val)
210
71
    clean_exp(a, b->val);
211
75
}
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
4837
ANN static void clean_stmt(Clean *a, Stmt b) {
246
4837
  clean_stmt_func[b->stmt_type](a, &b->d);
247
4837
}
248
249
318
ANN static void clean_arg_list(Clean *a, Arg_List b) {
250
318
  if(b->td)
251
302
    clean_type_decl(a, b->td);
252
318
  clean_var_decl(a, b->var_decl);
253
318
  if(b->next)
254
75
    clean_arg_list(a, b->next);
255
318
}
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
4271
ANN static void clean_stmt_list(Clean *a, Stmt_List b) {
264
4271
  clean_stmt(a, b->stmt);
265
4271
  if(b->next)
266
1613
    clean_stmt_list(a, b->next);
267
4271
}
268
269
448
ANN static void clean_func_base(Clean *a, Func_Base *b) {
270
448
  if(b->td)
271
434
    clean_type_decl(a, b->td);
272
448
  if(b->args)
273
243
    clean_arg_list(a, b->args);
274
448
  if(b->tmpl)
275
121
    clean_tmpl(a, b->tmpl);
276
448
}
277
278
375
ANN static void clean_func_def(Clean *a, Func_Def b) {
279
375
  clean_func_base(a, b->base);
280
375
  ++a->scope;
281

375
  if(b->d.code && !GET_FLAG(b, builtin))
282
371
    clean_stmt(a, b->d.code);
283
375
  --a->scope;
284
375
}
285
286
41
ANN void func_def_cleaner(const Gwion gwion, Func_Def b) {
287
41
  Clean a = { .gwion=gwion };
288
41
  clean_func_def(&a, b);
289
41
  if(GET_FLAG(b, builtin))
290
3
    b->d.code = NULL;
291
41
  free_func_def(gwion->mp, b);
292
41
}
293
294
1760
ANN static void clean_class_def(Clean *a, Class_Def b) {
295
1760
  clean_type_def(a, &b->base);
296
1760
  if(b->body)
297
1679
    clean_ast(a, b->body);
298
1760
}
299
300
1493
ANN void class_def_cleaner(const Gwion gwion, Class_Def b) {
301
1493
  Clean a = { .gwion=gwion };
302
1493
  clean_class_def(&a, b);
303
1493
  free_class_def(gwion->mp, b);
304
1493
}
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
1782
ANN static void clean_type_def(Clean *a, Type_Def b) {
323
1782
  if(b->ext)
324
1531
    clean_type_decl(a, b->ext);
325
1782
  if(b->tmpl)
326
1523
    clean_tmpl(a, b->tmpl);
327
1782
}
328
329
DECL_SECTION_FUNC(clean, void, Clean*)
330
331
2905
ANN static inline void clean_section(Clean *a, Section *b) {
332
2905
  clean_section_func[b->section_type](a, *(void**)&b->d);
333
2905
}
334
335
2905
ANN static void clean_ast(Clean *a, Ast b) {
336
2905
  clean_section(a, b->section);
337
2905
  if(b->next)
338
606
    clean_ast(a, b->next);
339
2905
}
340
341
620
ANN void ast_cleaner(const Gwion gwion, Ast b) {
342
620
  Clean a = { .gwion=gwion };
343
620
  clean_ast(&a, b);
344
620
  free_ast(gwion->mp, b);
345
620
}