GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/clean.c Lines: 276 276 100.0 %
Date: 2020-08-07 19:15:19 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
140
ANN static void clean_array_sub(Clean *a, Array_Sub b) {
10
140
  if(b->exp)
11
107
    clean_exp(a, b->exp);
12
140
}
13
14
1074
ANN static void clean_id_list(Clean *a, ID_List b) {
15
1074
  if(b->next)
16
49
    clean_id_list(a, b->next);
17
1074
}
18
19
289
ANN static void clean_type_list(Clean *a, Type_List b) {
20
289
  clean_type_decl(a, b->td);
21
289
  if(b->next)
22
35
    clean_type_list(a, b->next);
23
289
}
24
25
1053
ANN static void clean_tmpl(Clean *a, Tmpl *b) {
26
1053
  if(b->list)
27
1016
    clean_id_list(a, b->list);
28
1053
  if(b->call)
29
170
    clean_type_list(a, b->call);
30
1053
}
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
3709
ANN static void clean_type_decl(Clean *a, Type_Decl *b) {
40
3709
  if(b->exp)
41
12
    clean_exp(a, b->exp);
42
3709
  if(b->array)
43
28
    clean_array_sub(a, b->array);
44
3709
  if(b->types)
45
84
    clean_type_list(a, b->types);
46
3709
  if(b->next)
47
18
    clean_type_decl(a, b->next);
48
3709
}
49
50
5154
ANN static void clean_prim(Clean *a, Exp_Primary *b) {
51

9071
  if(b->prim_type == ae_prim_hack   ||
52
7796
     b->prim_type == ae_prim_typeof ||
53
3879
     b->prim_type == ae_prim_interp)
54
1280
    clean_exp(a, b->d.exp);
55
3874
  else if(b->prim_type == ae_prim_array)
56
26
    clean_array_sub(a, b->d.array);
57
3848
  else if(b->prim_type == ae_prim_range)
58
3
    clean_range(a, b->d.range);
59
5154
}
60
61
2084
ANN static void clean_var_decl(Clean *a, Var_Decl b) {
62
2084
  if(b->array)
63
54
    clean_array_sub(a, b->array);
64

2084
  if(a->scope && b->value)
65
337
    REM_REF(b->value, a->gwion)
66
2084
}
67
68
1787
ANN static void clean_var_decl_list(Clean *a, Var_Decl_List b) {
69
1787
  clean_var_decl(a, b->self);
70
1787
  if(b->next)
71
12
    clean_var_decl_list(a, b->next);
72
1787
}
73
74
1775
ANN static void clean_exp_decl(Clean *a, Exp_Decl *b) {
75
1775
  if(b->td)
76
1775
    clean_type_decl(a, b->td);
77
1775
  clean_var_decl_list(a, b->list);
78
1775
}
79
80
755
ANN static void clean_exp_binary(Clean *a, Exp_Binary *b) {
81
755
  clean_exp(a, b->lhs);
82
755
  clean_exp(a, b->rhs);
83
755
}
84
85
159
ANN static void clean_exp_unary(Clean *a, Exp_Unary *b) {
86
159
  if(b->exp)
87
105
    clean_exp(a, b->exp);
88
159
  if(b->td)
89
26
    clean_type_decl(a, b->td);
90
159
  if(b->code)
91
28
    clean_stmt(a, b->code);
92
159
}
93
94
46
ANN static void clean_exp_cast(Clean *a, Exp_Cast *b) {
95
46
  clean_type_decl(a, b->td);
96
46
  clean_exp(a, b->exp);
97
46
}
98
99
42
ANN static void clean_exp_post(Clean *a, Exp_Postfix *b) {
100
42
  clean_exp(a, b->exp);
101
42
}
102
103
629
ANN static void clean_exp_call(Clean *a, Exp_Call *b) {
104
629
  clean_exp(a, b->func);
105
629
  if(b->args)
106
371
    clean_exp(a, b->args);
107
629
  if(b->tmpl)
108
91
    clean_tmpl(a, b->tmpl);
109
629
}
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
595
ANN static void clean_exp_dot(Clean *a, Exp_Dot *b) {
129
595
  clean_exp(a, b->base);
130
595
}
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
9228
ANN static void clean_exp(Clean *a, Exp b) {
138
9228
  clean_exp_func[b->exp_type](a, &b->d);
139
9228
  if(b->next)
140
537
    clean_exp(a, b->next);
141
9228
}
142
143
3693
ANN static void clean_stmt_exp(Clean *a, Stmt_Exp b) {
144
3693
  if(b->val)
145
3663
    clean_exp(a, b->val);
146
3693
}
147
148
29
ANN static void clean_stmt_flow(Clean *a, Stmt_Flow b) {
149
29
  ++a->scope;
150
29
  clean_exp(a, b->cond);
151
29
  clean_stmt(a, b->body);
152
29
  --a->scope;
153
29
}
154
155
#define clean_stmt_while clean_stmt_flow
156
#define clean_stmt_until clean_stmt_flow
157
158
15
ANN static void clean_stmt_for(Clean *a, Stmt_For b) {
159
15
  ++a->scope;
160
15
  clean_stmt(a, b->c1);
161
15
  if(b->c2)
162
15
    clean_stmt(a, b->c2);
163
15
  if(b->c3)
164
13
    clean_exp(a, b->c3);
165
15
  clean_stmt(a, b->body);
166
15
  --a->scope;
167
15
}
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
41
ANN static void clean_stmt_if(Clean *a, Stmt_If b) {
186
41
  ++a->scope;
187
41
  clean_exp(a, b->cond);
188
41
  clean_stmt(a, b->if_body);
189
41
  if(b->else_body)
190
14
    clean_stmt(a, b->else_body);
191
41
  --a->scope;
192
41
}
193
194
592
ANN static void clean_stmt_code(Clean *a, Stmt_Code b) {
195
592
  ++a->scope;
196
592
  if(b->stmt_list)
197
509
    clean_stmt_list(a, b->stmt_list);
198
592
  --a->scope;
199
592
}
200
201
7
ANN static void clean_stmt_varloop(Clean *a, Stmt_VarLoop b) {
202
7
  ++a->scope;
203
7
  clean_exp(a, b->exp);
204
7
  clean_stmt(a, b->body);
205
7
  --a->scope;
206
7
}
207
208
55
ANN static void clean_stmt_return(Clean *a, Stmt_Exp b) {
209
55
  if(b->val)
210
51
    clean_exp(a, b->val);
211
55
}
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
4505
ANN static void clean_stmt(Clean *a, Stmt b) {
246
4505
  clean_stmt_func[b->stmt_type](a, &b->d);
247
4505
}
248
249
297
ANN static void clean_arg_list(Clean *a, Arg_List b) {
250
297
  if(b->td)
251
281
    clean_type_decl(a, b->td);
252
297
  clean_var_decl(a, b->var_decl);
253
297
  if(b->next)
254
61
    clean_arg_list(a, b->next);
255
297
}
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
3955
ANN static void clean_stmt_list(Clean *a, Stmt_List b) {
264
3955
  clean_stmt(a, b->stmt);
265
3955
  if(b->next)
266
1908
    clean_stmt_list(a, b->next);
267
3955
}
268
269
439
ANN static void clean_func_base(Clean *a, Func_Base *b) {
270
439
  if(b->td)
271
425
    clean_type_decl(a, b->td);
272
439
  if(b->args)
273
236
    clean_arg_list(a, b->args);
274
439
  if(b->tmpl)
275
118
    clean_tmpl(a, b->tmpl);
276
439
}
277
278
368
ANN static void clean_func_def(Clean *a, Func_Def b) {
279
368
  clean_func_base(a, b->base);
280
368
  ++a->scope;
281

368
  if(b->d.code && !GET_FLAG(b, builtin))
282
359
    clean_stmt(a, b->d.code);
283
368
  --a->scope;
284
368
}
285
286
38
ANN void func_def_cleaner(const Gwion gwion, Func_Def b) {
287
38
  Clean a = { .gwion=gwion };
288
38
  clean_func_def(&a, b);
289
38
  if(GET_FLAG(b, builtin))
290
4
    b->d.code = NULL;
291
38
  free_func_def(gwion->mp, b);
292
38
}
293
294
1079
ANN static void clean_class_def(Clean *a, Class_Def b) {
295
1079
  clean_type_def(a, &b->base);
296
1079
  if(b->body)
297
997
    clean_ast(a, b->body);
298
1079
}
299
300
808
ANN void class_def_cleaner(const Gwion gwion, Class_Def b) {
301
808
  Clean a = { .gwion=gwion };
302
808
  clean_class_def(&a, b);
303
808
  free_class_def(gwion->mp, b);
304
808
}
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
71
ANN static void clean_fptr_def(Clean *a, Fptr_Def b) {
317
71
  clean_func_base(a, b->base);
318
71
  if(b->type)
319
67
    REM_REF(b->type, a->gwion)
320
71
}
321
322
1103
ANN static void clean_type_def(Clean *a, Type_Def b) {
323
1103
  if(b->ext)
324
849
    clean_type_decl(a, b->ext);
325
1103
  if(b->tmpl)
326
839
    clean_tmpl(a, b->tmpl);
327
1103
}
328
329
DECL_SECTION_FUNC(clean, void, Clean*)
330
331
2235
ANN static inline void clean_section(Clean *a, Section *b) {
332
2235
  clean_section_func[b->section_type](a, *(void**)&b->d);
333
2235
}
334
335
2235
ANN static void clean_ast(Clean *a, Ast b) {
336
2235
  clean_section(a, b->section);
337
2235
  if(b->next)
338
605
    clean_ast(a, b->next);
339
2235
}
340
341
633
ANN void ast_cleaner(const Gwion gwion, Ast b) {
342
633
  Clean a = { .gwion=gwion };
343
633
  clean_ast(&a, b);
344
633
  free_ast(gwion->mp, b);
345
633
}