My Project
Loading...
Searching...
No Matches
ast_cpy.c
Go to the documentation of this file.
1#include "gwion_util.h"
2#include "gwion_ast.h"
3
4ANN Stmt* cpy_stmt(MemPool p, const Stmt* src);
5ANN static void cpy_stmt2(MemPool p, Stmt* a, const Stmt* src);
6ANN Exp* cpy_exp(MemPool p, const Exp* src);
7ANN TmplArgList *cpy_tmplarg_list(MemPool p, const TmplArgList *src);
8ANN ArgList *cpy_arg_list(MemPool p, const ArgList *src);
9ANN Class_Def cpy_class_def(MemPool p, const Class_Def src);
10ANN static StmtList *cpy_stmt_list(MemPool p, const StmtList *src);
11
12ANN static void cpy_var_decl(MemPool p NUSED, Var_Decl *a, const Var_Decl *src) {
13 a->tag = src->tag;
14}
15
16ANN static void cpy_exp_dot(MemPool p, Exp_Dot *a, const Exp_Dot *src) {
17 a->base = cpy_exp(p, src->base);
18 cpy_var_decl(p, &a->var, &src->var);
19}
20
21ANN static void cpy_exp_lambda(MemPool p, Exp_Lambda *a,
22 const Exp_Lambda *src) {
23 a->def = cpy_func_def(p, src->def);
24}
25
26ANN Array_Sub cpy_array_sub(MemPool p, const Array_Sub src) {
27 Array_Sub a = mp_calloc(p, Array_Sub);
28 if (src->exp) a->exp = cpy_exp(p, src->exp);
29 a->depth = src->depth;
30 return a;
31}
32
33ANN Range *cpy_range(MemPool p, const Range *src) {
34 Range *a = mp_calloc(p, Array_Sub);
35 if (src->start) a->start = cpy_exp(p, src->start);
36 if (src->end) a->end = cpy_exp(p, src->end);
37 return a;
38}
39
40ANN static void cpy_exp_array(MemPool p, Exp_Array *a, const Exp_Array *src) {
41 a->base = cpy_exp(p, src->base);
42 a->array = cpy_array_sub(p, src->array);
43}
44
45ANN static void cpy_exp_slice(MemPool p, Exp_Slice *a, const Exp_Slice *src) {
46 a->base = cpy_exp(p, src->base);
47 a->range = cpy_range(p, src->range);
48}
49
50ANN Type_Decl *cpy_type_decl(MemPool p, const Type_Decl *src) {
51 Type_Decl *a = mp_calloc(p, Type_Decl);
52 a->tag = src->tag;
53 if (src->array) a->array = cpy_array_sub(p, src->array); // 1
54 if (src->types) a->types = cpy_tmplarg_list(p, src->types); // 1
55 a->flag = src->flag; // 1
56 if (src->next) a->next = cpy_type_decl(p, src->next);
57 if (src->fptr) a->fptr = cpy_fptr_def(p, src->fptr);
58 a->option = src->option;
59 a->ref = src->ref;
60 return a;
61}
62
63ANN TagList *cpy_taglist(MemPool p, const TagList *src) {
64 TagList *a = new_taglist(p, src->len);
65 for(uint32_t i = 0; i < src->len; i++) {
66 const Tag tag = taglist_at(src, i);
67 taglist_set(a, i, tag);
68 }
69 return a;
70}
71
72ANN SpecializedList *cpy_specialized_list(MemPool p, const SpecializedList *src) {
73 SpecializedList *tgt = new_specializedlist(p, src->len);
74 for(uint32_t i = 0; i < src->len; i++) {
75 const Specialized _src = specializedlist_at(src, i);
76 Specialized *_tgt = specializedlist_ptr_at(tgt, i);
77 _tgt->tag = _src.tag;
78 if (_src.td) _tgt->td = cpy_type_decl(p, _src.td);
79 if (_src.traits) _tgt->traits = cpy_taglist(p, _src.traits);
80 }
81 return tgt;
82}
83
84ANN void cpy_tmplarg(MemPool p, const TmplArg *src, TmplArg *tgt) {
85 tgt->type = src->type;
86 if(src->type == tmplarg_td)
87 tgt->d.td = cpy_type_decl(p, src->d.td);
88 else tgt->d.exp = cpy_exp(p, src->d.exp);
89}
90
91ANN TmplArgList *cpy_tmplarg_list(MemPool p, const TmplArgList *src) {
92 TmplArgList *a = new_tmplarglist(p, src->len);
93 for(uint32_t i = 0; i < src->len; i++) {
94 const TmplArg _src = tmplarglist_at(src, i);
95 TmplArg *_tgt = tmplarglist_ptr_at(a, i);
96 cpy_tmplarg(p, &_src, _tgt);
97 }
98 return a;
99}
100
101ANN static void cpy_variable(MemPool p, Variable *a, const Variable *src) {
102 if (src->td) a->td = cpy_type_decl(p, src->td);
103 cpy_var_decl(p, &a->vd, &src->vd);
104}
105
106ANN ArgList *cpy_arg_list(MemPool p, const ArgList *src) {
107 ArgList *arg = new_arglist(p, src->len);
108 for(m_uint i = 0; i < src->len; i++) {
109 const Arg _src = arglist_at(src, i);
110 Arg *_arg = arglist_ptr_at(arg, i);
111 cpy_variable(p, &_arg->var, &_src.var);
112 if (_src.exp) _arg->exp = cpy_exp(p, _src.exp);
113 }
114 return arg;
115}
116
117ANN static void cpy_exp_decl(MemPool p, Exp_Decl *a, const Exp_Decl *src) {
118 cpy_variable(p, &a->var, &src->var);
119 if(src->args) a->args = cpy_exp(p, src->args);
120}
121
122ANN static void cpy_prim(MemPool p, Exp_Primary *a, const Exp_Primary *src) {
123 switch (src->prim_type) {
124 case ae_prim_id:
125 case ae_prim_locale:
126 case ae_prim_perform:
127 a->d.var = src->d.var;
128 break;
129 case ae_prim_num:
130 a->d.gwint.num = src->d.gwint.num;
131 break;
132 case ae_prim_float:
133 a->d.fnum = src->d.fnum;
134 break;
135 case ae_prim_char:
136 a->d.chr = src->d.chr;
137 break;
138 case ae_prim_str:
139 a->d.string.data = src->d.string.data;
140 a->d.string.delim = src->d.string.delim;
141 break;
142 case ae_prim_array:
143 a->d.array = cpy_array_sub(p, src->d.array);
144 break;
145 case ae_prim_range:
146 a->d.range = cpy_range(p, src->d.range);
147 break;
148 default:
149 if (src->d.exp) a->d.exp = cpy_exp(p, src->d.exp);
150 break;
151 }
152 a->prim_type = src->prim_type;
153}
154
155ANN Tmpl *cpy_tmpl(MemPool p, const Tmpl *src) {
156 Tmpl *a = mp_calloc(p, Tmpl);
157 if (!src->call)
158 a->list = cpy_specialized_list(p, src->list);
159 else {
160 a->list = src->list;
161 a->call = cpy_tmplarg_list(p, src->call);
162 }
163 return a;
164}
165
166ANN static void cpy_exp_call(MemPool p, Exp_Call *a, const Exp_Call *src) {
167 a->func = cpy_exp(p, src->func);
168 if (src->args) a->args = cpy_exp(p, src->args);
169 if (src->tmpl) a->tmpl = cpy_tmpl(p, src->tmpl);
170}
171
172ANN static void cpy_exp_cast(MemPool p, Exp_Cast *a, const Exp_Cast *src) {
173 a->td = cpy_type_decl(p, src->td);
174 a->exp = cpy_exp(p, src->exp);
175}
176
177ANN static void cpy_exp_binary(MemPool p, Exp_Binary *a,
178 const Exp_Binary *src) {
179 a->lhs = cpy_exp(p, src->lhs);
180 a->rhs = cpy_exp(p, src->rhs);
181 a->op = src->op;
182}
183
184ANN static void cpy_exp_postfix(MemPool p, Exp_Postfix *a,
185 const Exp_Postfix *src) {
186 a->op = src->op;
187 a->exp = cpy_exp(p, src->exp);
188}
189
190ANN static void cpy_exp_if(MemPool p, Exp_If *a, const Exp_If *src) {
191 a->cond = cpy_exp(p, src->cond);
192 if (src->if_exp) a->if_exp = cpy_exp(p, src->if_exp);
193 if (src->else_exp) a->else_exp = cpy_exp(p, src->else_exp);
194}
195
196ANN static CaptureList *cpy_captures(MemPool p, const CaptureList *src) {
197 CaptureList *a = new_capturelist(p, src->len);
198 for(uint32_t i = 0; i < src->len; i++) {
199 // NOTE: use list copy?
200 const Capture capture = capturelist_at(src, i);
201 capturelist_set(a, i, capture);
202 }
203 return a;
204}
205
206ANN static void cpy_exp_unary(MemPool p, Exp_Unary *a, const Exp_Unary *src) {
207 a->op = src->op;
208 switch ((a->unary_type = src->unary_type)) {
209 case unary_exp:
210 a->exp = cpy_exp(p, src->exp);
211 break;
212 case unary_td:
213 a->ctor.td = cpy_type_decl(p, src->ctor.td);
214 if(src->ctor.exp)
215 a->ctor.exp = cpy_exp(p, src->ctor.exp);
216 break;
217 case unary_code:
218 a->code = cpy_stmt_list(p, src->code);
219 break;
220 }
221 if(src->captures)
222 a->captures = cpy_captures(p, src->captures);
223}
224
225ANN Exp* cpy_exp(MemPool p, const Exp* src) {
226 Exp* a = mp_calloc2(p, sizeof(Exp));
227 if (src->next) a->next = cpy_exp(p, src->next);
228 switch (src->exp_type) {
229 case ae_exp_post: // !! naming
230 cpy_exp_postfix(p, &a->d.exp_post, &src->d.exp_post);
231 break;
232 case ae_exp_primary:
233 cpy_prim(p, &a->d.prim, &src->d.prim);
234 break;
235 case ae_exp_decl:
236 cpy_exp_decl(p, &a->d.exp_decl, &src->d.exp_decl);
237 break;
238 case ae_exp_unary:
239 cpy_exp_unary(p, &a->d.exp_unary, &src->d.exp_unary);
240 break;
241 case ae_exp_binary:
242 cpy_exp_binary(p, &a->d.exp_binary, &src->d.exp_binary);
243 break;
244 case ae_exp_cast:
245 cpy_exp_cast(p, &a->d.exp_cast, &src->d.exp_cast);
246 break;
247 case ae_exp_call:
248 cpy_exp_call(p, &a->d.exp_call, &src->d.exp_call);
249 break;
250 case ae_exp_if:
251 cpy_exp_if(p, &a->d.exp_if, &src->d.exp_if);
252 break;
253 case ae_exp_dot:
254 cpy_exp_dot(p, &a->d.exp_dot, &src->d.exp_dot);
255 break;
256 case ae_exp_array:
257 cpy_exp_array(p, &a->d.exp_array, &src->d.exp_array);
258 break;
259 case ae_exp_slice:
260 cpy_exp_slice(p, &a->d.exp_slice, &src->d.exp_slice);
261 break;
262 case ae_exp_lambda:
263 cpy_exp_lambda(p, &a->d.exp_lambda, &src->d.exp_lambda);
264 break;
265 case ae_exp_td:
266 a->d.exp_td = cpy_type_decl(p, src->d.exp_td);
267 break;
268 case ae_exp_named:
269 a->d.exp_named.exp = cpy_exp(p, src->d.exp_named.exp);
270 }
271 a->exp_type = src->exp_type;
272 a->emit_var = src->emit_var;
273 // a->meta = src->meta;// maybe meta shoyuld be set as in constructors
274 a->loc = src->loc;
275 return a;
276}
277
278ANN static void cpy_stmt_exp(MemPool p, const Stmt_Exp a, const struct Stmt_Exp_ *src) {
279 if (src->val) a->val = cpy_exp(p, src->val);
280}
281
282ANN static void cpy_stmt_flow(MemPool p, Stmt_Flow a, const struct Stmt_Flow_ *src) {
283 if (src->cond) a->cond = cpy_exp(p, src->cond);
284 if (src->body) a->body = cpy_stmt(p, src->body);
285 a->is_do = src->is_do;
286}
287
288ANN static void cpy_stmt_defer(MemPool p, Stmt_Defer a, const struct Stmt_Defer_ *src) {
289 a->stmt = cpy_stmt(p, src->stmt);
290}
291
292ANN static void cpy_stmt_code(MemPool p, Stmt_Code a, const struct Stmt_Code_ *src) {
293 if (src->stmt_list) a->stmt_list = cpy_stmt_list(p, src->stmt_list);
294}
295
296ANN static void cpy_stmt_for(MemPool p, Stmt_For a, const struct Stmt_For_ *src) {
297 if (src->c1) a->c1 = cpy_stmt(p, src->c1);
298 if (src->c2) a->c2 = cpy_stmt(p, src->c2);
299 if (src->c3) a->c3 = cpy_exp(p, src->c3);
300 if (src->body) a->body = cpy_stmt(p, src->body);
301}
302
303ANN static void cpy_stmt_each(MemPool p, Stmt_Each a, const struct Stmt_Each_ *src) {
304 a->var = src->var;
305 a->exp = cpy_exp(p, src->exp);
306 a->body = cpy_stmt(p, src->body);
307 a->is_ref = src->is_ref;
308 if (src->idx.tag.sym) a->idx = src->idx;
309}
310
311ANN static void cpy_stmt_loop(MemPool p, Stmt_Loop a, const struct Stmt_Loop_ *src) {
312 a->cond = cpy_exp(p, src->cond);
313 a->body = cpy_stmt(p, src->body);
314 if (src->idx.tag.sym) a->idx = src->idx;
315}
316
317ANN static void cpy_stmt_if(MemPool p, Stmt_If a, const struct Stmt_If_ *src) {
318 if (src->cond) a->cond = cpy_exp(p, src->cond);
319 if (src->if_body) a->if_body = cpy_stmt(p, src->if_body);
320 if (src->else_body) a->else_body = cpy_stmt(p, src->else_body);
321}
322
323ANN static void cpy_stmt_case(MemPool p, const Stmt_Match a, const struct Match *src) {
324 a->cond = cpy_exp(p, src->cond);
325 a->list = cpy_stmt_list(p, src->list);
326 if (src->when) a->when = cpy_exp(p, src->when);
327}
328
329ANN static void cpy_stmt_pp(MemPool p NUSED, Stmt_PP a, const struct Stmt_PP_ *src) {
330 if (src->data) a->data = strdup(src->data);
331 if (src->exp) a->exp = cpy_exp(p, src->exp);
332 a->xid = src->xid;
333}
334
335ANN static StmtList *cpy_stmt_cases(MemPool p, const StmtList *src) {
336 const m_uint sz = src->len;
337 StmtList *a = new_stmtlist(p, sz);
338 for(m_uint i = 0; i < sz; i++) {
339 Stmt* a_stmt = stmtlist_ptr_at(a, i);
340 Stmt src_stmt = stmtlist_at(src, i);
341 cpy_stmt_case(p, &a_stmt->d.stmt_match, &src_stmt.d.stmt_match);
342 }
343 return a;
344}
345
346ANN static HandlerList *cpy_handler_list(MemPool p, const HandlerList *src) {
347 HandlerList *tgt = new_handlerlist(p, src->len);
348 for(m_uint i = 0; i < src->len; i++) {
349 const Handler src_handler = handlerlist_at(src, i);
350 Handler *tgt_handler = handlerlist_ptr_at(tgt, i);
351 tgt_handler->stmt = cpy_stmt(p, src_handler.stmt);
352 tgt_handler->tag = src_handler.tag;
353 }
354 return tgt;
355}
356
357ANN static void cpy_stmt_try(MemPool p, Stmt_Try a, const struct Stmt_Try_ *src) {
358 a->stmt = cpy_stmt(p, src->stmt);
359 a->handler = cpy_handler_list(p, src->handler);
360}
361
362ANN static void cpy_stmt_match(MemPool p, Stmt_Match a, const struct Match *src) {
363 a->cond = cpy_exp(p, src->cond);
364 a->list = cpy_stmt_cases(p, src->list);
365 if (src->where) a->where = cpy_stmt(p, src->where);
366}
367
368
369ANN static EnumValueList *cpy_enum_list(MemPool p, const EnumValueList *src) {
370 EnumValueList *tgt = new_enumvaluelist(p, src->len);
371 memcpy(tgt->ptr, src->ptr, src->len * sizeof(EnumValue)); // TODO: xxxlist copy functions
372 return tgt;
373}
374
375ANN static Enum_Def cpy_enum_def(MemPool p, const Enum_Def src) {
376 Enum_Def a = mp_calloc(p, Enum_Def);
377 a->list = cpy_enum_list(p, src->list);
378 a->tag = src->tag;
379 a->flag = src->flag;
380 return a;
381}
382
383ANN Func_Base *cpy_func_base(MemPool p, const Func_Base *src) {
384 Func_Base *a = mp_calloc(p, Func_Base);
385 if (src->td) a->td = cpy_type_decl(p, src->td); // 1
386 a->tag = src->tag; // 1
387 if (src->args) a->args = cpy_arg_list(p, src->args); // 1
388 if (src->tmpl) a->tmpl = cpy_tmpl(p, src->tmpl); // 1
389 // if(src->effects.ptr)
390 // vector_copy2((Vector)&src->effects, &a->effects);
391 a->flag = src->flag;
392 a->fbflag = src->fbflag;
393 return a;
394}
395
396ANN /*static */ Fptr_Def cpy_fptr_def(MemPool p, const Fptr_Def src) {
397 Fptr_Def a = mp_calloc(p, Fptr_Def);
398 a->base = cpy_func_base(p, src->base);
399 return a;
400}
401
402ANN static void cpy_type_def2(MemPool p, Type_Def a, const Type_Def src) {
403 a->tag = src->tag;
404 if (src->ext) a->ext = cpy_type_decl(p, src->ext);
405 if (src->when) a->when = cpy_exp(p, src->when);
406 if (src->tmpl) a->tmpl = cpy_tmpl(p, src->tmpl);
407}
408
409ANN static Type_Def cpy_type_def(MemPool p, const Type_Def src) {
410 Type_Def a = mp_calloc(p, Type_Def);
411 cpy_type_def2(p, a, src);
412 return a;
413}
414
415ANN VariableList *cpy_variable_list(MemPool p, const VariableList *src) {
416 VariableList *a = new_variablelist(p, src->len);
417 for(uint32_t i = 0; i < src->len; i++) {
418 const Variable _src = variablelist_at(src, i);
419 Variable *_tgt = variablelist_ptr_at(a, i);
420 cpy_variable(p, _tgt, &_src);
421 }
422 return a;
423}
424
425ANN Union_Def cpy_union_def(MemPool p, const Union_Def src) {
426 Union_Def a = mp_calloc(p, Union_Def);
427 a->tag = src->tag;
428 a->l = cpy_variable_list(p, src->l);
429 if (src->tmpl) a->tmpl = cpy_tmpl(p, src->tmpl);
430 a->flag = src->flag;
431 return a;
432}
433
434ANN Stmt* cpy_stmt(MemPool p, const Stmt* src) {
435 Stmt* a = mp_calloc2(p, sizeof(Stmt));
436 cpy_stmt2(p, a, src);
437 return a;
438}
439
440ANN Stmt* cpy_stmt3(MemPool p, const Stmt* src) {
441 Stmt* a = mp_calloc2(p, sizeof(Stmt));
442 memcpy(a, src, sizeof(Stmt));
443 return a;
444}
445
446ANN static void cpy_stmt_spread(MemPool p, Spread_Def a, const struct Spread_Def_ *src) {
447 a->tag = src->tag;
448 a->list = cpy_taglist(p, src->list);
449 a->data = mstrdup(p, src->data);
450}
451
452ANN static void cpy_stmt_using(MemPool p, Stmt_Using a, const struct Stmt_Using_ *src) {
453 if(src->tag.sym) {
454 a->d.exp = cpy_exp(p, src->d.exp);
455 a->tag = src->tag;
456 } else
457 a->d.td = cpy_type_decl(p, src->d.td);
458}
459
460ANN static UsingStmtList *cpy_import_list(MemPool p, const UsingStmtList *src) {
461 UsingStmtList *tgt = new_usingstmtlist(p, src->len);
462 for(uint32_t i = 0; i < src->len; i++) {
463 const UsingStmt src_item = usingstmtlist_at(src, i);
464 Stmt_Using tgt_item = usingstmtlist_ptr_at(tgt, i);
465 tgt_item->tag = src_item.tag;
466 if(src_item.d.exp)
467 tgt_item->d.exp = cpy_exp(p, src_item.d.exp);
468 }
469 return tgt;
470}
471
472ANN static void cpy_stmt_import(MemPool p, Stmt_Import a, const struct Stmt_Import_ *src) {
473 a->tag = src->tag;
474 if(src->selection)
475 a->selection = cpy_import_list(p, src->selection);
476}
477
478
479ANN static void cpy_stmt2(MemPool p, Stmt* a, const Stmt* src) {
480 switch (src->stmt_type) {
481 case ae_stmt_exp:
482 case ae_stmt_return:
483 cpy_stmt_exp(p, &a->d.stmt_exp, &src->d.stmt_exp);
484 break;
485 case ae_stmt_code:
486 cpy_stmt_code(p, &a->d.stmt_code, &src->d.stmt_code);
487 break;
488 case ae_stmt_while:
489 case ae_stmt_until:
490 cpy_stmt_flow(p, &a->d.stmt_flow, &src->d.stmt_flow);
491 break;
492 case ae_stmt_loop:
493 cpy_stmt_loop(p, &a->d.stmt_loop, &src->d.stmt_loop);
494 break;
495 case ae_stmt_for:
496 cpy_stmt_for(p, &a->d.stmt_for, &src->d.stmt_for);
497 break;
498 case ae_stmt_each:
499 cpy_stmt_each(p, &a->d.stmt_each, &src->d.stmt_each);
500 break;
501 case ae_stmt_if:
502 cpy_stmt_if(p, &a->d.stmt_if, &src->d.stmt_if);
503 break;
504 case ae_stmt_try:
505 cpy_stmt_try(p, &a->d.stmt_try, &src->d.stmt_try);
506 break;
507 case ae_stmt_match:
508 cpy_stmt_match(p, &a->d.stmt_match, &src->d.stmt_match);
509 break;
510 case ae_stmt_pp:
511 cpy_stmt_pp(p, &a->d.stmt_pp, &src->d.stmt_pp);
512 break;
513 case ae_stmt_defer:
514 cpy_stmt_defer(p, &a->d.stmt_defer, &src->d.stmt_defer);
515 break;
516 case ae_stmt_spread:
518 break;
519 case ae_stmt_using:
520 cpy_stmt_using(p, &a->d.stmt_using, &src->d.stmt_using);
521 break;
522 case ae_stmt_import:
524 break;
525 case ae_stmt_break:
526 case ae_stmt_continue:
527 case ae_stmt_retry:
528 break;
529 }
530 a->stmt_type = src->stmt_type;
531 a->loc = src->loc;
532}
533
534ANN Func_Def cpy_func_def(MemPool p, const Func_Def src) {
535 Func_Def a = mp_calloc(p, Func_Def);
536 a->base = cpy_func_base(p, src->base);
537 if (src->d.code) {
538 if(!src->builtin && src->d.code) a->d.code = cpy_stmt_list(p, src->d.code);
539 else a->d.dl_func_ptr = src->d.dl_func_ptr;
540 }
541 if (src->captures) a->captures = cpy_captures(p, src->captures);
542 // a->trait = src->trait;
543 a->builtin = src->builtin;
544 return a;
545}
546
547ANN StmtList *cpy_stmt_list(MemPool p, const StmtList *src) {
548 const m_uint sz = src->len;
549 StmtList *a = new_stmtlist(p, sz);
550 for(m_uint i = 0; i < sz; i++) {
551 Stmt* astmt = stmtlist_ptr_at(a, i);
552 const Stmt sstmt = stmtlist_at(src, i);
553 cpy_stmt2(p, astmt, &sstmt);
554 }
555 return a;
556}
557
558ANN static Trait_Def cpy_trait_def(MemPool p, const Trait_Def src) {
559 Trait_Def a = mp_calloc(p, Trait_Def);
560 a->tag = src->tag;
561 if (src->body) a->body = cpy_ast(p, src->body);
562 if (src->traits) a->traits = cpy_taglist(p, src->traits);
563 a->flag = src->flag;
564 return a;
565}
566
567ANN static Prim_Def cpy_prim_def(MemPool p, const Prim_Def src) {
568 Prim_Def a = mp_calloc(p, Prim_Def);
569 a->tag = src->tag;
570 a->size = src->size;
571 a->flag = src->flag;
572 return a;
573}
574
575ANN static void cpy_section(MemPool p, Section *const a, const Section *src) {
576// Section *a = mp_calloc(p, Section);
577 switch (src->section_type) {
578 case ae_section_stmt:
579 //if(src->d.stmt_list)
580 a->d.stmt_list = cpy_stmt_list(p, src->d.stmt_list);
581 break;
582 case ae_section_class:
583 a->d.class_def = cpy_class_def(p, src->d.class_def);
584 break;
585 case ae_section_trait:
586 a->d.trait_def = cpy_trait_def(p, src->d.trait_def);
587 break;
589 a->d.extend_def = cpy_extend_def(p, src->d.extend_def);
590 break;
591 case ae_section_func:
592 a->d.func_def = cpy_func_def(p, src->d.func_def);
593 break;
594 case ae_section_enum:
595 a->d.enum_def = cpy_enum_def(p, src->d.enum_def);
596 break;
597 case ae_section_union:
598 a->d.union_def = cpy_union_def(p, src->d.union_def);
599 break;
600 case ae_section_fptr:
601 a->d.fptr_def = cpy_fptr_def(p, src->d.fptr_def);
602 break;
603 case ae_section_type:
604 a->d.type_def = cpy_type_def(p, src->d.type_def);
605 break;
607 a->d.prim_def = cpy_prim_def(p, src->d.prim_def);
608 break;
609 }
610 a->section_type = src->section_type;
611// return a;
612}
613
614ANN Extend_Def cpy_extend_def(MemPool p, const Extend_Def src) {
615 Extend_Def a = mp_calloc(p, Extend_Def);
616 a->traits = cpy_taglist(p, src->traits);
617 a->td = cpy_type_decl(p, src->td);
618 return a;
619}
620
621ANN Class_Def cpy_class_def(MemPool p, const Class_Def src) {
622 Class_Def a = mp_calloc(p, Class_Def);
623 cpy_type_def2(p, &a->base, &src->base);
624 if (src->body) a->body = cpy_ast(p, src->body);
625 if (src->traits) a->traits = cpy_taglist(p, src->traits);
626 a->flag = src->flag;
627 a->cflag = src->cflag;
628 return a;
629}
630
631ANN Ast cpy_ast(MemPool p, Ast src) {
632 Ast a = new_sectionlist(p, src->len);
633 for(m_uint i = 0; i < src->len; i++) {
634 Section * asec = sectionlist_ptr_at(a, i);
635 const Section ssec = sectionlist_at(src, i);
636 cpy_section(p, asec, &ssec);
637 }
638 return a;
639}
@ ae_stmt_exp
Definition absyn.h:159
@ ae_stmt_continue
Definition absyn.h:168
@ ae_stmt_spread
Definition absyn.h:175
@ ae_stmt_loop
Definition absyn.h:164
@ ae_stmt_return
Definition absyn.h:169
@ ae_stmt_while
Definition absyn.h:160
@ ae_stmt_pp
Definition absyn.h:173
@ ae_stmt_code
Definition absyn.h:166
@ ae_stmt_match
Definition absyn.h:172
@ ae_stmt_for
Definition absyn.h:162
@ ae_stmt_if
Definition absyn.h:165
@ ae_stmt_until
Definition absyn.h:161
@ ae_stmt_import
Definition absyn.h:177
@ ae_stmt_each
Definition absyn.h:163
@ ae_stmt_break
Definition absyn.h:167
@ ae_stmt_try
Definition absyn.h:170
@ ae_stmt_defer
Definition absyn.h:174
@ ae_stmt_retry
Definition absyn.h:171
@ ae_stmt_using
Definition absyn.h:176
@ ae_prim_locale
Definition absyn.h:435
@ ae_prim_array
Definition absyn.h:427
@ ae_prim_id
Definition absyn.h:423
@ ae_prim_float
Definition absyn.h:425
@ ae_prim_perform
Definition absyn.h:434
@ ae_prim_num
Definition absyn.h:424
@ ae_prim_char
Definition absyn.h:431
@ ae_prim_str
Definition absyn.h:426
@ ae_prim_range
Definition absyn.h:428
@ ae_exp_unary
Definition absyn.h:402
@ ae_exp_binary
Definition absyn.h:401
@ ae_exp_post
Definition absyn.h:405
@ ae_exp_dot
Definition absyn.h:410
@ ae_exp_call
Definition absyn.h:406
@ ae_exp_decl
Definition absyn.h:400
@ ae_exp_primary
Definition absyn.h:403
@ ae_exp_if
Definition absyn.h:409
@ ae_exp_lambda
Definition absyn.h:411
@ ae_exp_td
Definition absyn.h:412
@ ae_exp_cast
Definition absyn.h:404
@ ae_exp_slice
Definition absyn.h:408
@ ae_exp_array
Definition absyn.h:407
@ ae_exp_named
Definition absyn.h:413
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
@ tmplarg_td
Definition absyn.h:254
@ 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 Exp * cpy_exp(MemPool p, const Exp *src)
Definition ast_cpy.c:225
static ANN Trait_Def cpy_trait_def(MemPool p, const Trait_Def src)
Definition ast_cpy.c:558
static ANN void cpy_stmt_code(MemPool p, Stmt_Code a, const struct Stmt_Code_ *src)
Definition ast_cpy.c:292
static ANN void cpy_stmt_pp(MemPool p NUSED, Stmt_PP a, const struct Stmt_PP_ *src)
Definition ast_cpy.c:329
static ANN void cpy_stmt_match(MemPool p, Stmt_Match a, const struct Match *src)
Definition ast_cpy.c:362
static ANN void cpy_exp_slice(MemPool p, Exp_Slice *a, const Exp_Slice *src)
Definition ast_cpy.c:45
ANN TmplArgList * cpy_tmplarg_list(MemPool p, const TmplArgList *src)
Definition ast_cpy.c:91
ANN Union_Def cpy_union_def(MemPool p, const Union_Def src)
Definition ast_cpy.c:425
ANN Func_Base * cpy_func_base(MemPool p, const Func_Base *src)
Definition ast_cpy.c:383
ANN Array_Sub cpy_array_sub(MemPool p, const Array_Sub src)
Definition ast_cpy.c:26
ANN Type_Decl * cpy_type_decl(MemPool p, const Type_Decl *src)
Definition ast_cpy.c:50
static ANN void cpy_exp_if(MemPool p, Exp_If *a, const Exp_If *src)
Definition ast_cpy.c:190
static ANN void cpy_exp_call(MemPool p, Exp_Call *a, const Exp_Call *src)
Definition ast_cpy.c:166
static ANN CaptureList * cpy_captures(MemPool p, const CaptureList *src)
Definition ast_cpy.c:196
static ANN void cpy_stmt_if(MemPool p, Stmt_If a, const struct Stmt_If_ *src)
Definition ast_cpy.c:317
static ANN void cpy_stmt_using(MemPool p, Stmt_Using a, const struct Stmt_Using_ *src)
Definition ast_cpy.c:452
static ANN UsingStmtList * cpy_import_list(MemPool p, const UsingStmtList *src)
Definition ast_cpy.c:460
static ANN StmtList * cpy_stmt_cases(MemPool p, const StmtList *src)
Definition ast_cpy.c:335
ANN Tmpl * cpy_tmpl(MemPool p, const Tmpl *src)
Definition ast_cpy.c:155
static ANN void cpy_exp_dot(MemPool p, Exp_Dot *a, const Exp_Dot *src)
Definition ast_cpy.c:16
static ANN void cpy_var_decl(MemPool p NUSED, Var_Decl *a, const Var_Decl *src)
Definition ast_cpy.c:12
static ANN void cpy_stmt_spread(MemPool p, Spread_Def a, const struct Spread_Def_ *src)
Definition ast_cpy.c:446
ANN Ast cpy_ast(MemPool p, Ast src)
Definition ast_cpy.c:631
static ANN void cpy_stmt_exp(MemPool p, const Stmt_Exp a, const struct Stmt_Exp_ *src)
Definition ast_cpy.c:278
static ANN void cpy_exp_unary(MemPool p, Exp_Unary *a, const Exp_Unary *src)
Definition ast_cpy.c:206
static ANN void cpy_prim(MemPool p, Exp_Primary *a, const Exp_Primary *src)
Definition ast_cpy.c:122
static ANN void cpy_stmt_loop(MemPool p, Stmt_Loop a, const struct Stmt_Loop_ *src)
Definition ast_cpy.c:311
ANN Extend_Def cpy_extend_def(MemPool p, const Extend_Def src)
Definition ast_cpy.c:614
ANN Fptr_Def cpy_fptr_def(MemPool p, const Fptr_Def src)
Definition ast_cpy.c:396
static ANN void cpy_variable(MemPool p, Variable *a, const Variable *src)
Definition ast_cpy.c:101
static ANN void cpy_stmt_try(MemPool p, Stmt_Try a, const struct Stmt_Try_ *src)
Definition ast_cpy.c:357
static ANN Enum_Def cpy_enum_def(MemPool p, const Enum_Def src)
Definition ast_cpy.c:375
static ANN void cpy_exp_lambda(MemPool p, Exp_Lambda *a, const Exp_Lambda *src)
Definition ast_cpy.c:21
static ANN HandlerList * cpy_handler_list(MemPool p, const HandlerList *src)
Definition ast_cpy.c:346
ANN ArgList * cpy_arg_list(MemPool p, const ArgList *src)
Definition ast_cpy.c:106
ANN SpecializedList * cpy_specialized_list(MemPool p, const SpecializedList *src)
Definition ast_cpy.c:72
static ANN void cpy_stmt2(MemPool p, Stmt *a, const Stmt *src)
Definition ast_cpy.c:479
static ANN void cpy_type_def2(MemPool p, Type_Def a, const Type_Def src)
Definition ast_cpy.c:402
ANN Stmt * cpy_stmt3(MemPool p, const Stmt *src)
Definition ast_cpy.c:440
static ANN void cpy_stmt_import(MemPool p, Stmt_Import a, const struct Stmt_Import_ *src)
Definition ast_cpy.c:472
static ANN void cpy_exp_binary(MemPool p, Exp_Binary *a, const Exp_Binary *src)
Definition ast_cpy.c:177
ANN VariableList * cpy_variable_list(MemPool p, const VariableList *src)
Definition ast_cpy.c:415
static ANN void cpy_exp_decl(MemPool p, Exp_Decl *a, const Exp_Decl *src)
Definition ast_cpy.c:117
static ANN void cpy_stmt_case(MemPool p, const Stmt_Match a, const struct Match *src)
Definition ast_cpy.c:323
static ANN void cpy_section(MemPool p, Section *const a, const Section *src)
Definition ast_cpy.c:575
ANN void cpy_tmplarg(MemPool p, const TmplArg *src, TmplArg *tgt)
Definition ast_cpy.c:84
ANN TagList * cpy_taglist(MemPool p, const TagList *src)
Definition ast_cpy.c:63
static ANN void cpy_stmt_for(MemPool p, Stmt_For a, const struct Stmt_For_ *src)
Definition ast_cpy.c:296
ANN Range * cpy_range(MemPool p, const Range *src)
Definition ast_cpy.c:33
ANN Stmt * cpy_stmt(MemPool p, const Stmt *src)
Definition ast_cpy.c:434
static ANN void cpy_exp_postfix(MemPool p, Exp_Postfix *a, const Exp_Postfix *src)
Definition ast_cpy.c:184
static ANN Type_Def cpy_type_def(MemPool p, const Type_Def src)
Definition ast_cpy.c:409
static ANN void cpy_exp_cast(MemPool p, Exp_Cast *a, const Exp_Cast *src)
Definition ast_cpy.c:172
static ANN EnumValueList * cpy_enum_list(MemPool p, const EnumValueList *src)
Definition ast_cpy.c:369
static ANN StmtList * cpy_stmt_list(MemPool p, const StmtList *src)
Definition ast_cpy.c:547
static ANN void cpy_stmt_each(MemPool p, Stmt_Each a, const struct Stmt_Each_ *src)
Definition ast_cpy.c:303
static ANN Prim_Def cpy_prim_def(MemPool p, const Prim_Def src)
Definition ast_cpy.c:567
static ANN void cpy_exp_array(MemPool p, Exp_Array *a, const Exp_Array *src)
Definition ast_cpy.c:40
static ANN void cpy_stmt_flow(MemPool p, Stmt_Flow a, const struct Stmt_Flow_ *src)
Definition ast_cpy.c:282
ANN Func_Def cpy_func_def(MemPool p, const Func_Def src)
Definition ast_cpy.c:534
static ANN void cpy_stmt_defer(MemPool p, Stmt_Defer a, const struct Stmt_Defer_ *src)
Definition ast_cpy.c:288
ANN Class_Def cpy_class_def(MemPool p, const Class_Def src)
Definition ast_cpy.c:621
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
uint16_t depth
Definition absyn.h:355
Exp * exp
Definition absyn.h:353
uint16_t delim
Definition absyn.h:419
m_str data
Definition absyn.h:418
ae_flag flag
Definition absyn.h:884
Ast body
Definition absyn.h:881
enum cflag cflag
Definition absyn.h:883
TagList * traits
Definition absyn.h:882
struct Type_Def_ base
Definition absyn.h:880
ae_flag flag
Definition absyn.h:717
EnumValueList * list
Definition absyn.h:715
Tag tag
Definition absyn.h:714
array expression.
Definition absyn.h:362
Exp * base
Definition absyn.h:363
Array_Sub array
Definition absyn.h:364
Symbol op
Definition absyn.h:511
Exp * rhs
Definition absyn.h:510
Exp * lhs
Definition absyn.h:509
Tmpl * tmpl
Definition absyn.h:500
Exp * args
Definition absyn.h:499
Exp * func
Definition absyn.h:498
Exp * exp
Definition absyn.h:506
Type_Decl * td
Definition absyn.h:505
Exp * args
Definition absyn.h:441
Variable var
Definition absyn.h:439
a dot expression.
Definition absyn.h:329
Var_Decl var
Definition absyn.h:330
Exp * base
Definition absyn.h:331
Exp * cond
Definition absyn.h:518
Exp * if_exp
Definition absyn.h:519
Exp * else_exp
Definition absyn.h:520
a lambda expression.
Definition absyn.h:343
Func_Def def
Definition absyn.h:344
Exp * exp
Definition absyn.h:539
Exp * exp
Definition absyn.h:515
Symbol op
Definition absyn.h:514
union Exp_Primary::prim_data d
ae_prim_t prim_type
Definition absyn.h:475
slice.
Definition absyn.h:379
Range * range
Definition absyn.h:381
Exp * base
Definition absyn.h:380
enum unary_type unary_type
Definition absyn.h:535
Exp * exp
Definition absyn.h:530
StmtList * code
Definition absyn.h:531
struct UnaryNew ctor
Definition absyn.h:532
Symbol op
Definition absyn.h:528
CaptureList * captures
Definition absyn.h:534
Definition absyn.h:559
int16_t emit_var
Definition absyn.h:583
ae_exp_t exp_type
Definition absyn.h:582
union Exp::exp_data d
loc_t loc
Definition absyn.h:581
Exp * next
Definition absyn.h:578
Type_Decl * td
Definition absyn.h:865
TagList * traits
Definition absyn.h:866
Func_Base * base
Definition absyn.h:749
Tag tag
Definition absyn.h:731
Tmpl * tmpl
Definition absyn.h:735
ArgList * args
Definition absyn.h:732
Type_Decl * td
Definition absyn.h:730
ae_flag flag
Definition absyn.h:738
enum fbflag fbflag
Definition absyn.h:739
bool builtin
Definition absyn.h:787
union Func_Def_::func_def_data d
Func_Base * base
Definition absyn.h:779
CaptureList * captures
Definition absyn.h:784
Stmt * stmt
Definition absyn.h:95
Tag tag
Definition absyn.h:94
Definition absyn.h:53
Exp * when
Definition absyn.h:58
Exp * cond
Definition absyn.h:54
Stmt * where
Definition absyn.h:57
struct StmtList * list
Definition absyn.h:55
m_uint size
Definition absyn.h:806
Tag tag
Definition absyn.h:805
ae_flag flag
Definition absyn.h:807
range.
Definition absyn.h:371
Exp * end
end of range expression
Definition absyn.h:373
Exp * start
start of range expression
Definition absyn.h:372
ae_section_t section_type
Definition absyn.h:850
union Section_::section_data d
Tag tag
Definition absyn.h:387
Type_Decl * td
Definition absyn.h:388
TagList * traits
Definition absyn.h:389
Tag tag
Definition absyn.h:152
TagList * list
Definition absyn.h:153
m_str data
Definition absyn.h:154
struct StmtList * stmt_list
Definition absyn.h:63
Stmt * stmt
Definition absyn.h:131
Var_Decl idx
Definition absyn.h:77
Var_Decl var
Definition absyn.h:74
Exp * exp
Definition absyn.h:75
bool is_ref
Definition absyn.h:78
Stmt * body
Definition absyn.h:76
Exp * val
Definition absyn.h:40
bool is_do
Definition absyn.h:50
Stmt * body
Definition absyn.h:49
Exp * cond
Definition absyn.h:48
Stmt * body
Definition absyn.h:70
Stmt * c2
Definition absyn.h:68
Stmt * c1
Definition absyn.h:67
Exp * c3
Definition absyn.h:69
Stmt * else_body
Definition absyn.h:90
Stmt * if_body
Definition absyn.h:89
Exp * cond
Definition absyn.h:88
UsingStmtList * selection
Definition absyn.h:148
Exp * cond
Definition absyn.h:82
Var_Decl idx
Definition absyn.h:84
Stmt * body
Definition absyn.h:83
m_str data
Definition absyn.h:124
Exp * exp
Definition absyn.h:125
Symbol xid
Definition absyn.h:126
HandlerList * handler
Definition absyn.h:106
Stmt * stmt
Definition absyn.h:105
Exp * exp
Definition absyn.h:137
Type_Decl * td
Definition absyn.h:136
Tag tag
Definition absyn.h:139
union Stmt_Using_::@2 d
Definition absyn.h:187
loc_t loc
position
Definition absyn.h:205
ae_stmt_t stmt_type
Definition absyn.h:206
union Stmt::stmt_data d
Definition absyn.h:25
Symbol sym
Definition absyn.h:26
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
SpecializedList * list
Definition absyn.h:479
TmplArgList * call
Definition absyn.h:480
Ast body
Definition absyn.h:796
ae_flag flag
Definition absyn.h:798
Tag tag
Definition absyn.h:795
TagList * traits
Definition absyn.h:797
Tag tag
Definition absyn.h:279
Type_Decl * next
Definition absyn.h:282
ae_flag flag
Definition absyn.h:285
TmplArgList * types
Definition absyn.h:281
Array_Sub array
Definition absyn.h:280
bool ref
Definition absyn.h:286
uint8_t option
Definition absyn.h:284
Fptr_Def fptr
Definition absyn.h:283
Tag tag
Definition absyn.h:759
Type_Decl * ext
Definition absyn.h:757
Exp * when
Definition absyn.h:761
Tmpl * tmpl
Definition absyn.h:760
Type_Decl * td
Definition absyn.h:524
Exp * exp
Definition absyn.h:525
Tmpl * tmpl
Definition absyn.h:772
Tag tag
Definition absyn.h:770
ae_flag flag
Definition absyn.h:773
VariableList * l
Definition absyn.h:769
Tag tag
Definition absyn.h:34
variable declaration
Definition absyn.h:244
Var_Decl vd
Definition absyn.h:246
Type_Decl * td
Definition absyn.h:245
m_int num
Definition absyn.h:452
Exp_Lambda exp_lambda
Definition absyn.h:572
Exp_Named exp_named
Definition absyn.h:574
Exp_Decl exp_decl
Definition absyn.h:563
Exp_Array exp_array
Definition absyn.h:570
Exp_Unary exp_unary
Definition absyn.h:564
Exp_Cast exp_cast
Definition absyn.h:566
Exp_Postfix exp_post
Definition absyn.h:561
Exp_Primary prim
Definition absyn.h:562
Exp_Binary exp_binary
Definition absyn.h:565
Exp_Dot exp_dot
Definition absyn.h:569
Exp_Call exp_call
Definition absyn.h:567
Exp_Slice exp_slice
Definition absyn.h:571
Exp_If exp_if
Definition absyn.h:568
Type_Decl * exp_td
Definition absyn.h:573
struct gwint gwint
Definition absyn.h:467
struct Symbol_ * var
Definition absyn.h:466
struct AstString string
Definition absyn.h:470
StmtList * stmt_list
Definition absyn.h:838
Union_Def union_def
Definition absyn.h:844
Trait_Def trait_def
Definition absyn.h:839
Class_Def class_def
Definition absyn.h:840
Extend_Def extend_def
Definition absyn.h:841
struct Stmt_Loop_ stmt_loop
Definition absyn.h:192
struct Spread_Def_ stmt_spread
Definition absyn.h:201
struct Stmt_Defer_ stmt_defer
Definition absyn.h:200
struct Stmt_Import_ stmt_import
Definition absyn.h:203
struct Stmt_Try_ stmt_try
Definition absyn.h:196
struct Stmt_Each_ stmt_each
Definition absyn.h:194
struct Stmt_Code_ stmt_code
Definition absyn.h:190
struct Stmt_Using_ stmt_using
Definition absyn.h:202
struct Stmt_For_ stmt_for
Definition absyn.h:193
struct Match stmt_match
Definition absyn.h:197
struct Stmt_Flow_ stmt_flow
Definition absyn.h:191
struct Stmt_Exp_ stmt_exp
Definition absyn.h:189
struct Stmt_PP_ stmt_pp
Definition absyn.h:199
struct Stmt_If_ stmt_if
Definition absyn.h:195