1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
Copyright (C) 2019 Leo Tenenbaum.
This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever.
You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>.
*/
/* these copy functions MUST be used before typing!!!! (except for copy_val) */
typedef struct {
Allocator *allocr;
Block *block;
} Copier;
static void copy_expr(Copier *c, Expression *out, Expression *in);
static void copy_decl(Copier *c, Declaration *out, Declaration *in);
static void copy_block(Copier *c, Block *out, Block *in);
static void copy_type(Copier *c, Type *out, Type *in);
static Copier copier_create(Allocator *a, Block *b) {
Copier c;
c.allocr = a;
c.block = b;
return c;
}
static void copy_val(Allocator *a, Value *out, Value *in, Type *t) {
assert(t->flags & TYPE_IS_RESOLVED);
switch (t->kind) {
case TYPE_BUILTIN:
case TYPE_FN:
case TYPE_PTR:
case TYPE_SLICE:
case TYPE_VOID:
case TYPE_UNKNOWN:
*out = *in;
break;
case TYPE_ARR: {
size_t bytes = t->arr.n * compiler_sizeof(t->arr.of);
out->arr = allocr_malloc(a, bytes);
memcpy(out->arr, in->arr, bytes);
} break;
case TYPE_TUPLE: {
size_t bytes = arr_len(t->tuple) * sizeof(*out->tuple);
out->tuple = allocr_malloc(a, bytes);
memcpy(out->tuple, in->tuple, bytes);
} break;
case TYPE_STRUCT: {
size_t bytes = compiler_sizeof(t);
out->struc = allocr_malloc(a, bytes);
memcpy(out->struc, in->struc, bytes);
} break;
case TYPE_TYPE:
/* copy_type(c, out->type = allocr_malloc(c->allocr, sizeof *out->type), in->type); */
/*
i don't think this can be a problem right now,
but might eventually be,
if a function returns a type and
the old type doesn't work when the stuff on the stack
is freed. if you switch back to using a copier, make sure you check where
copy_val(NULL, ...) is used!!!
*/
*out = *in;
break;
case TYPE_EXPR:
assert(0);
}
}
/* only works on unresolved types */
static void copy_type(Copier *c, Type *out, Type *in) {
assert(!(in->flags & TYPE_IS_RESOLVED));
*out = *in;
switch (in->kind) {
case TYPE_BUILTIN:
case TYPE_TYPE:
case TYPE_VOID:
case TYPE_UNKNOWN:
break;
case TYPE_EXPR:
copy_expr(c, out->expr = allocr_malloc(c->allocr, sizeof *out->expr), in->expr);
break;
case TYPE_FN: {
size_t ntypes = arr_len(in->fn.types);
out->fn.types = NULL;
arr_set_lena(&out->fn.types, ntypes, c->allocr);
for (size_t i = 0; i < ntypes; ++i) {
copy_type(c, &out->fn.types[i], &in->fn.types[i]);
}
} break;
case TYPE_TUPLE: {
size_t ntypes = arr_len(in->tuple);
out->tuple = NULL;
arr_set_lena(&out->tuple, ntypes, c->allocr);
for (size_t i = 0; i < ntypes; ++i) {
copy_type(c, &out->tuple[i], &in->tuple[i]);
}
} break;
case TYPE_ARR:
/* if (in->flags & TYPE_IS_RESOLVED) { */
/* out->arr.n = in->arr.n; */
/* } else { */
out->arr.n_expr = allocr_malloc(c->allocr, sizeof *out->arr.n_expr);
copy_expr(c, out->arr.n_expr, in->arr.n_expr);
/* } */
out->arr.of = allocr_malloc(c->allocr, sizeof *out->arr.of);
copy_type(c, out->arr.of, in->arr.of);
break;
case TYPE_PTR:
out->ptr = allocr_malloc(c->allocr, sizeof *out->ptr);
copy_type(c, out->ptr, in->ptr);
break;
case TYPE_SLICE:
out->ptr = allocr_malloc(c->allocr, sizeof *out->slice);
copy_type(c, out->slice, in->slice);
break;
case TYPE_STRUCT: {
out->struc = allocr_malloc(c->allocr, sizeof *out->struc);
*out->struc = *in->struc;
size_t nfields = arr_len(in->struc->fields);
out->struc->fields = NULL;
arr_set_lena(&out->struc->fields, nfields, c->allocr);
for (size_t i = 0; i < nfields; ++i) {
Field *fout = &out->struc->fields[i];
Field *fin = &in->struc->fields[i];
*fout = *fin;
copy_type(c, fout->type = allocr_malloc(c->allocr, sizeof *fout->type), fin->type);
}
} break;
}
}
static void copy_fn_expr(Copier *c, FnExpr *fout, FnExpr *fin, bool copy_body) {
*fout = *fin;
size_t i;
size_t nparam_decls = arr_len(fin->params);
fout->params = NULL;
arr_set_lena(&fout->params, nparam_decls, c->allocr);
for (i = 0; i < nparam_decls; ++i)
copy_decl(c, fout->params + i, fin->params + i);
size_t nret_decls = arr_len(fin->ret_decls);
if (fin->ret_decls) {
fout->ret_decls = NULL;
arr_set_lena(&fout->ret_decls, nret_decls, c->allocr);
for (i = 0; i < nret_decls; ++i)
copy_decl(c, fout->ret_decls + i, fin->ret_decls + i);
}
copy_type(c, &fout->ret_type, &fin->ret_type);
if (copy_body)
copy_block(c, &fout->body, &fin->body);
}
static void copy_expr(Copier *c, Expression *out, Expression *in) {
Allocator *a = c->allocr;
*out = *in;
assert(!(in->flags & EXPR_FOUND_TYPE));
switch (in->kind) {
case EXPR_LITERAL_FLOAT:
case EXPR_LITERAL_INT:
case EXPR_LITERAL_STR:
case EXPR_LITERAL_CHAR:
case EXPR_LITERAL_BOOL:
case EXPR_IDENT:
break;
case EXPR_UNARY_OP:
copy_expr(c, out->unary.of = allocr_malloc(a, sizeof *out->unary.of), in->unary.of);
break;
case EXPR_BINARY_OP:
copy_expr(c, out->binary.lhs = allocr_malloc(a, sizeof *out->binary.lhs), in->binary.lhs);
copy_expr(c, out->binary.rhs = allocr_malloc(a, sizeof *out->binary.rhs), in->binary.rhs);
break;
case EXPR_IF: {
IfExpr *iin = &in->if_;
IfExpr *iout = &out->if_;
if (iin->cond)
copy_expr(c, iout->cond = allocr_malloc(a, sizeof *iout->cond), iin->cond);
if (iin->next_elif)
copy_expr(c, iout->next_elif = allocr_malloc(a, sizeof *iout->next_elif), iin->next_elif);
copy_block(c, &iout->body, &iin->body);
} break;
case EXPR_WHILE: {
WhileExpr *win = &in->while_;
WhileExpr *wout = &out->while_;
if (win->cond)
copy_expr(c, wout->cond = allocr_malloc(a, sizeof *wout->cond), win->cond);
copy_block(c, &wout->body, &win->body);
} break;
case EXPR_EACH: {
EachExpr *ein = &in->each;
EachExpr *eout = &out->each;
if (ein->flags & EACH_ANNOTATED_TYPE)
copy_type(c, &eout->type, &ein->type);
if (ein->flags & EACH_IS_RANGE) {
copy_expr(c, eout->range.from = allocr_malloc(a, sizeof *eout->range.from), ein->range.from);
if (ein->range.to)
copy_expr(c, eout->range.to = allocr_malloc(a, sizeof *eout->range.to), ein->range.to);
if (ein->range.step)
copy_expr(c, eout->range.step = allocr_malloc(a, sizeof *eout->range.step), ein->range.step);
} else {
copy_expr(c, eout->of = allocr_malloc(a, sizeof *eout->of), ein->of);
}
copy_block(c, &eout->body, &ein->body);
} break;
case EXPR_FN:
copy_fn_expr(c, &out->fn, &in->fn, true);
break;
case EXPR_CAST: {
CastExpr *cin = &in->cast;
CastExpr *cout = &out->cast;
copy_type(c, &cout->type, &cin->type);
copy_expr(c, cout->expr = allocr_malloc(a, sizeof *cout->expr), cin->expr);
} break;
case EXPR_NEW: {
NewExpr *nin = &in->new;
NewExpr *nout = &out->new;
copy_type(c, &nout->type, &nin->type);
if (nin->n) copy_expr(c, nout->n = allocr_malloc(a, sizeof *nout->n), nin->n);
} break;
case EXPR_CALL: {
CallExpr *cin = &in->call;
CallExpr *cout = &out->call;
copy_expr(c, cout->fn = allocr_malloc(a, sizeof *cout->fn), cin->fn);
size_t nargs = arr_len(cin->args);
cout->arg_exprs = NULL;
arr_set_lena(&cout->args, nargs, a);
for (size_t i = 0; i < nargs; ++i) {
Argument *arg_in = &cin->args[i];
Argument *arg_out = &cout->args[i];
*arg_out = *arg_in;
copy_expr(c, &arg_out->val, &arg_in->val);
}
} break;
case EXPR_BLOCK:
copy_block(c, &out->block, &in->block);
break;
case EXPR_TUPLE: {
size_t nexprs = arr_len(in->tuple);
out->tuple = NULL;
arr_set_lena(&out->tuple, nexprs, a);
for (size_t i = 0; i < nexprs; ++i)
copy_expr(c, out->tuple + i, in->tuple + i);
} break;
case EXPR_C:
copy_expr(c, out->c.code = allocr_malloc(a, sizeof *out->c.code), in->c.code);
break;
case EXPR_DSIZEOF:
copy_expr(c, out->dsizeof.of = allocr_malloc(a, sizeof *out->dsizeof.of), in->dsizeof.of);
break;
case EXPR_DALIGNOF:
copy_expr(c, out->dalignof.of = allocr_malloc(a, sizeof *out->dalignof.of), in->dalignof.of);
break;
case EXPR_SLICE: {
SliceExpr *sin = &in->slice;
SliceExpr *sout = &out->slice;
copy_expr(c, sout->of = allocr_malloc(a, sizeof *sout->of), sin->of);
if (sin->from)
copy_expr(c, sout->from = allocr_malloc(a, sizeof *sout->from), sin->from);
if (sin->to)
copy_expr(c, sout->to = allocr_malloc(a, sizeof *sout->to), sin->to);
} break;
case EXPR_TYPE:
copy_type(c, &out->typeval, &in->typeval);
break;
case EXPR_VAL:
copy_val(a, &out->val, &in->val, &in->type);
break;
}
}
static void copy_decl(Copier *c, Declaration *out, Declaration *in) {
*out = *in;
assert(!(in->flags & DECL_FOUND_TYPE));
if (in->flags & DECL_HAS_EXPR)
copy_expr(c, &out->expr, &in->expr);
if (in->flags & DECL_FOUND_VAL) {
copy_val(c->allocr, &out->val, &in->val, &in->type);
}
if (in->flags & DECL_ANNOTATES_TYPE)
copy_type(c, &out->type, &in->type);
}
static void copy_stmt(Copier *c, Statement *out, Statement *in) {
*out = *in;
switch (in->kind) {
case STMT_RET:
if (in->ret.flags & RET_HAS_EXPR)
copy_expr(c, &out->ret.expr, &in->ret.expr);
break;
case STMT_EXPR:
copy_expr(c, &out->expr, &in->expr);
break;
case STMT_DECL:
copy_decl(c, &out->decl, &in->decl);
break;
}
}
static void copy_block(Copier *c, Block *out, Block *in) {
*out = *in;
size_t nstmts = arr_len(in->stmts);
out->stmts = NULL;
Block *prev = c->block;
c->block = out;
if (in->ret_expr)
copy_expr(c, out->ret_expr = allocr_malloc(c->allocr, sizeof *out->ret_expr), in->ret_expr);
arr_set_lena(&out->stmts, nstmts, c->allocr);
for (size_t i = 0; i < nstmts; ++i) {
copy_stmt(c, &out->stmts[i], &in->stmts[i]);
}
c->block = prev;
}
|