1 // Some basic operations defined on AST nodes, mainly converting to
2 // strings for debugging.
7 #include "stdlib/datatypes.h"
8 #include "stdlib/optionals.h"
9 #include "stdlib/tables.h"
10 #include "stdlib/text.h"
12 const int op_tightness[NUM_AST_TAGS] = {
23 [UnsignedLeftShift] = 5,
24 [UnsignedRightShift] = 5,
30 [LessThanOrEquals] = 2,
32 [GreaterThanOrEquals] = 2,
39 const binop_info_t binop_info[NUM_AST_TAGS] = {
40 [Power] = {"power", "^"},
41 [PowerUpdate] = {"power", "^="},
42 [Multiply] = {"times", "*"},
43 [MultiplyUpdate] = {"times", "*="},
44 [Divide] = {"divided_by", "/"},
45 [DivideUpdate] = {"divided_by", "/="},
46 [Mod] = {"modulo", "mod"},
47 [ModUpdate] = {"modulo", "mod="},
48 [Mod1] = {"modulo1", "mod1"},
49 [Mod1Update] = {"modulo1", "mod1="},
50 [Plus] = {"plus", "+"},
51 [PlusUpdate] = {"plus", "+="},
52 [Minus] = {"minus", "-"},
53 [MinusUpdate] = {"minus", "-="},
54 [Concat] = {"concatenated_with", "++"},
55 [ConcatUpdate] = {"concatenated_with", "++="},
56 [LeftShift] = {"left_shifted", "<<"},
57 [LeftShiftUpdate] = {"left_shifted", "<<="},
58 [RightShift] = {"right_shifted", ">>"},
59 [RightShiftUpdate] = {"right_shifted", ">>="},
60 [UnsignedLeftShift] = {"unsigned_left_shifted", NULL},
61 [UnsignedLeftShiftUpdate] = {"unsigned_left_shifted", NULL},
62 [UnsignedRightShift] = {"unsigned_right_shifted", NULL},
63 [UnsignedRightShiftUpdate] = {"unsigned_right_shifted", NULL},
64 [And] = {"bit_and", "and"},
65 [AndUpdate] = {"bit_and", "and="},
66 [Or] = {"bit_or", "or"},
67 [OrUpdate] = {"bit_or", "or="},
68 [Xor] = {"bit_xor", "xor"},
69 [XorUpdate] = {"bit_xor", "xor="},
70 [Equals] = {NULL, "=="},
71 [NotEquals] = {NULL, "!="},
72 [LessThan] = {NULL, "<"},
73 [LessThanOrEquals] = {NULL, "<="},
74 [GreaterThan] = {NULL, ">"},
75 [GreaterThanOrEquals] = {NULL, ">="},
76 [Min] = {NULL, "_min_"},
77 [Max] = {NULL, "_max_"},
80 static Text_t ast_list_to_sexp(ast_list_t *asts);
81 static Text_t arg_list_to_sexp(arg_ast_t *args);
82 static Text_t arg_defs_to_sexp(arg_ast_t *args);
83 static Text_t when_clauses_to_sexp(when_clause_t *clauses);
84 static Text_t tags_to_sexp(tag_ast_t *tags);
85 static Text_t optional_sexp(const char *tag, ast_t *ast);
86 static Text_t optional_type_sexp(const char *tag, type_ast_t *ast);
88 static Text_t quoted_text(const char *text) {
89 return Text$quoted(Text$from_str(text), false, Text("\""));
92 Text_t ast_list_to_sexp(ast_list_t *asts) {
93 Text_t c = EMPTY_TEXT;
94 for (; asts; asts = asts->next) {
95 c = Texts(c, " ", ast_to_sexp(asts->ast));
100 Text_t arg_defs_to_sexp(arg_ast_t *args) {
101 Text_t c = Text("(args");
102 for (arg_ast_t *arg = args; arg; arg = arg->next) {
103 c = Texts(c, " (arg ", arg->name ? quoted_text(arg->name) : Text("nil"), " ", type_ast_to_sexp(arg->type), " ",
104 ast_to_sexp(arg->value), ")");
106 return Texts(c, ")");
109 Text_t arg_list_to_sexp(arg_ast_t *args) {
110 Text_t c = EMPTY_TEXT;
111 for (arg_ast_t *arg = args; arg; arg = arg->next) {
112 assert(arg->value && !arg->type);
113 if (arg->name) c = Texts(c, " :", arg->name);
114 c = Texts(c, " ", ast_to_sexp(arg->value));
119 Text_t when_clauses_to_sexp(when_clause_t *clauses) {
120 Text_t c = EMPTY_TEXT;
121 for (; clauses; clauses = clauses->next) {
122 c = Texts(c, " (case ", ast_to_sexp(clauses->pattern), " ", ast_to_sexp(clauses->body), ")");
127 Text_t tags_to_sexp(tag_ast_t *tags) {
128 Text_t c = EMPTY_TEXT;
129 for (; tags; tags = tags->next) {
130 c = Texts(c, "(tag \"", tags->name, "\" ", arg_defs_to_sexp(tags->fields), ")");
135 Text_t type_ast_to_sexp(type_ast_t *t) {
136 if (!t) return Text("nil");
139 #define T(type, ...) \
141 __typeof(t->__data.type) data = t->__data.type; \
143 return Texts(__VA_ARGS__); \
145 T(UnknownTypeAST, "(UnknownType)");
146 T(VarTypeAST, "(VarType \"", data.name, "\")");
147 T(PointerTypeAST, "(PointerType \"", data.is_stack ? "stack" : "heap", "\" ", type_ast_to_sexp(data.pointed),
149 T(ListTypeAST, "(ListType ", type_ast_to_sexp(data.item), ")");
150 T(TableTypeAST, "(TableType ", type_ast_to_sexp(data.key), " ", type_ast_to_sexp(data.value), ")");
151 T(FunctionTypeAST, "(FunctionType ", arg_defs_to_sexp(data.args), " ", type_ast_to_sexp(data.ret), ")");
152 T(OptionalTypeAST, "(OptionalType ", type_ast_to_sexp(data.type), ")");
153 T(EnumTypeAST, "(EnumType ", data.name, " ", tags_to_sexp(data.tags), ")");
155 default: return EMPTY_TEXT;
159 Text_t optional_sexp(const char *name, ast_t *ast) {
160 return ast ? Texts(" :", name, " ", ast_to_sexp(ast)) : EMPTY_TEXT;
163 Text_t optional_type_sexp(const char *name, type_ast_t *ast) {
164 return ast ? Texts(" :", name, " ", type_ast_to_sexp(ast)) : EMPTY_TEXT;
167 Text_t ast_to_sexp(ast_t *ast) {
168 if (!ast) return Text("nil");
171 #define T(type, ...) \
173 __typeof(ast->__data.type) data = ast->__data.type; \
175 return Texts(__VA_ARGS__); \
177 T(Unknown, "(Unknown)");
179 T(Bool, "(Bool ", data.b ? "yes" : "no", ")");
180 T(Var, "(Var ", quoted_text(data.name), ")");
181 T(Int, "(Int ", Text$quoted(ast_source(ast), false, Text("\"")), ")");
182 T(Num, "(Num ", Text$quoted(ast_source(ast), false, Text("\"")), ")");
183 T(TextLiteral, Text$quoted(data.text, false, Text("\"")));
184 T(TextJoin, "(Text", data.lang ? Texts(" :lang ", quoted_text(data.lang)) : EMPTY_TEXT,
185 ast_list_to_sexp(data.children), ")");
186 T(Path, "(Path ", quoted_text(data.path), ")");
187 T(Declare, "(Declare ", ast_to_sexp(data.var), " ", type_ast_to_sexp(data.type), " ", ast_to_sexp(data.value),
189 T(Assign, "(Assign (targets ", ast_list_to_sexp(data.targets), ") (values ", ast_list_to_sexp(data.values),
191 #define BINOP(name) T(name, "(" #name " ", ast_to_sexp(data.lhs), " ", ast_to_sexp(data.rhs), ")")
195 BINOP(MultiplyUpdate);
209 BINOP(LeftShiftUpdate);
211 BINOP(RightShiftUpdate);
212 BINOP(UnsignedLeftShift);
213 BINOP(UnsignedLeftShiftUpdate);
214 BINOP(UnsignedRightShift);
215 BINOP(UnsignedRightShiftUpdate);
226 BINOP(LessThanOrEquals);
228 BINOP(GreaterThanOrEquals);
230 T(Negative, "(Negative ", ast_to_sexp(data.value), ")");
231 T(Not, "(Not ", ast_to_sexp(data.value), ")");
232 T(HeapAllocate, "(HeapAllocate ", ast_to_sexp(data.value), ")");
233 T(StackReference, "(StackReference ", ast_to_sexp(data.value), ")");
234 T(Min, "(Min ", ast_to_sexp(data.lhs), " ", ast_to_sexp(data.rhs), optional_sexp("key", data.key), ")");
235 T(Max, "(Max ", ast_to_sexp(data.lhs), " ", ast_to_sexp(data.rhs), optional_sexp("key", data.key), ")");
236 T(List, "(List", ast_list_to_sexp(data.items), ")");
237 T(Table, "(Table", optional_sexp("default", data.default_value), optional_sexp("fallback", data.fallback),
238 ast_list_to_sexp(data.entries), ")");
239 T(TableEntry, "(TableEntry ", ast_to_sexp(data.key), " ", ast_to_sexp(data.value), ")");
240 T(Comprehension, "(Comprehension ", ast_to_sexp(data.expr), " (vars", ast_list_to_sexp(data.vars), ") ",
241 ast_to_sexp(data.iter), " ", optional_sexp("filter", data.filter), ")");
242 T(FunctionDef, "(FunctionDef ", ast_to_sexp(data.name), " ", arg_defs_to_sexp(data.args),
243 optional_type_sexp("return", data.ret_type), " ", ast_to_sexp(data.body), ")");
244 T(ConvertDef, "(ConvertDef ", arg_defs_to_sexp(data.args), " ", type_ast_to_sexp(data.ret_type), " ",
245 ast_to_sexp(data.body), ")");
246 T(Lambda, "(Lambda ", arg_defs_to_sexp(data.args), optional_type_sexp("return", data.ret_type), " ",
247 ast_to_sexp(data.body), ")");
248 T(FunctionCall, "(FunctionCall ", ast_to_sexp(data.fn), arg_list_to_sexp(data.args), ")");
249 T(MethodCall, "(MethodCall ", ast_to_sexp(data.self), " ", quoted_text(data.name), arg_list_to_sexp(data.args),
251 T(Block, "(Block", ast_list_to_sexp(data.statements), ")");
252 T(For, "(For (vars", ast_list_to_sexp(data.vars), ") ", ast_to_sexp(data.iter), " ", ast_to_sexp(data.body),
253 " ", ast_to_sexp(data.empty), ")");
254 T(While, "(While ", ast_to_sexp(data.condition), " ", ast_to_sexp(data.body), ")");
255 T(Repeat, "(Repeat ", ast_to_sexp(data.body), ")");
256 T(If, "(If ", ast_to_sexp(data.condition), " ", ast_to_sexp(data.body), optional_sexp("else", data.else_body),
258 T(When, "(When ", ast_to_sexp(data.subject), when_clauses_to_sexp(data.clauses),
259 optional_sexp("else", data.else_body), ")");
260 T(Reduction, "(Reduction ", quoted_text(binop_info[data.op].operator), " ", ast_to_sexp(data.key), " ",
261 ast_to_sexp(data.iter), ")");
262 T(Skip, "(Skip ", quoted_text(data.target), ")");
263 T(Stop, "(Stop ", quoted_text(data.target), ")");
265 T(Defer, "(Defer ", ast_to_sexp(data.body), ")");
266 T(Return, "(Return ", ast_to_sexp(data.value), ")");
267 T(StructDef, "(StructDef \"", data.name, "\" ", arg_defs_to_sexp(data.fields), " ", ast_to_sexp(data.namespace),
269 T(EnumDef, "(EnumDef \"", data.name, "\" (tags ", tags_to_sexp(data.tags), ") ", ast_to_sexp(data.namespace),
271 T(LangDef, "(LangDef \"", data.name, "\" ", ast_to_sexp(data.namespace), ")");
272 T(Index, "(Index ", ast_to_sexp(data.indexed), " ", ast_to_sexp(data.index), ")");
273 T(FieldAccess, "(FieldAccess ", ast_to_sexp(data.fielded), " \"", data.field, "\")");
274 T(NonOptional, "(NonOptional ", ast_to_sexp(data.value), ")");
275 T(DebugLog, "(DebugLog ", ast_list_to_sexp(data.values), ")");
276 T(Assert, "(Assert ", ast_to_sexp(data.expr), " ", optional_sexp("message", data.message), ")");
277 T(Use, "(Use ", optional_sexp("var", data.var), " ", quoted_text(data.path), ")");
278 T(InlineCCode, "(InlineCCode ", ast_list_to_sexp(data.chunks), optional_type_sexp("type", data.type_ast), ")");
279 T(Metadata, "((Metadata ", Text$quoted(data.key, false, Text("\"")), " ",
280 Text$quoted(data.value, false, Text("\"")), ")");
281 default: errx(1, "S-expressions are not implemented for this AST");
286 const char *ast_to_sexp_str(ast_t *ast) {
287 return Text$as_c_string(ast_to_sexp(ast));
290 OptionalText_t ast_source(ast_t *ast) {
291 if (ast == NULL || ast->start == NULL || ast->end == NULL) return NONE_TEXT;
292 return Text$from_strn(ast->start, (size_t)(ast->end - ast->start));
295 PUREFUNC bool is_idempotent(ast_t *ast) {
302 case TextLiteral: return true;
304 DeclareMatch(index, ast, Index);
305 return is_idempotent(index->indexed) && index->index != NULL && is_idempotent(index->index);
308 DeclareMatch(access, ast, FieldAccess);
309 return is_idempotent(access->fielded);
311 default: return false;
315 void _visit_topologically(ast_t *ast, Table_t definitions, Table_t *visited, Closure_t fn) {
316 void (*visit)(void *, ast_t *) = (void *)fn.fn;
317 if (ast->tag == StructDef) {
318 DeclareMatch(def, ast, StructDef);
319 if (Table$str_get(*visited, def->name)) return;
321 Table$str_set(visited, def->name, (void *)_visit_topologically);
322 for (arg_ast_t *field = def->fields; field; field = field->next) {
323 if (field->type && field->type->tag == VarTypeAST) {
324 const char *field_type_name = Match(field->type, VarTypeAST)->name;
325 ast_t *dependency = Table$str_get(definitions, field_type_name);
327 _visit_topologically(dependency, definitions, visited, fn);
331 visit(fn.userdata, ast);
332 } else if (ast->tag == EnumDef) {
333 DeclareMatch(def, ast, EnumDef);
334 if (Table$str_get(*visited, def->name)) return;
336 Table$str_set(visited, def->name, (void *)_visit_topologically);
337 for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
338 for (arg_ast_t *field = tag->fields; field; field = field->next) {
339 if (field->type && field->type->tag == VarTypeAST) {
340 const char *field_type_name = Match(field->type, VarTypeAST)->name;
341 ast_t *dependency = Table$str_get(definitions, field_type_name);
343 _visit_topologically(dependency, definitions, visited, fn);
348 visit(fn.userdata, ast);
349 } else if (ast->tag == LangDef) {
350 DeclareMatch(def, ast, LangDef);
351 if (Table$str_get(*visited, def->name)) return;
352 visit(fn.userdata, ast);
354 visit(fn.userdata, ast);
358 void visit_topologically(ast_list_t *asts, Closure_t fn) {
359 // Visit each top-level statement in topological order:
360 // - 'use' statements first
362 // - visiting typedefs' dependencies first
363 // - then function/variable declarations
365 Table_t definitions = EMPTY_TABLE;
366 for (ast_list_t *stmt = asts; stmt; stmt = stmt->next) {
367 if (stmt->ast->tag == StructDef) {
368 DeclareMatch(def, stmt->ast, StructDef);
369 Table$str_set(&definitions, def->name, stmt->ast);
370 } else if (stmt->ast->tag == EnumDef) {
371 DeclareMatch(def, stmt->ast, EnumDef);
372 Table$str_set(&definitions, def->name, stmt->ast);
373 } else if (stmt->ast->tag == LangDef) {
374 DeclareMatch(def, stmt->ast, LangDef);
375 Table$str_set(&definitions, def->name, stmt->ast);
379 void (*visit)(void *, ast_t *) = (void *)fn.fn;
380 Table_t visited = EMPTY_TABLE;
381 // First: 'use' statements in order:
382 for (ast_list_t *stmt = asts; stmt; stmt = stmt->next) {
383 if (stmt->ast->tag == Use) visit(fn.userdata, stmt->ast);
385 // Then typedefs in topological order:
386 for (ast_list_t *stmt = asts; stmt; stmt = stmt->next) {
387 if (stmt->ast->tag == StructDef || stmt->ast->tag == EnumDef || stmt->ast->tag == LangDef)
388 _visit_topologically(stmt->ast, definitions, &visited, fn);
390 // Then everything else in order:
391 for (ast_list_t *stmt = asts; stmt; stmt = stmt->next) {
392 if (!(stmt->ast->tag == StructDef || stmt->ast->tag == EnumDef || stmt->ast->tag == LangDef
393 || stmt->ast->tag == Use)) {
394 visit(fn.userdata, stmt->ast);
399 CONSTFUNC bool is_binary_operation(ast_t *ast) {
403 case BINOP_CASES: return true;
404 default: return false;
408 CONSTFUNC bool is_update_assignment(ast_t *ast) {
418 case LeftShiftUpdate:
419 case UnsignedLeftShiftUpdate:
420 case RightShiftUpdate:
421 case UnsignedRightShiftUpdate:
424 case XorUpdate: return true;
425 default: return false;
429 CONSTFUNC ast_e binop_tag(ast_e tag) {
431 case PowerUpdate: return Power;
432 case MultiplyUpdate: return Multiply;
433 case DivideUpdate: return Divide;
434 case ModUpdate: return Mod;
435 case Mod1Update: return Mod1;
436 case PlusUpdate: return Plus;
437 case MinusUpdate: return Minus;
438 case ConcatUpdate: return Concat;
439 case LeftShiftUpdate: return LeftShift;
440 case UnsignedLeftShiftUpdate: return UnsignedLeftShift;
441 case RightShiftUpdate: return RightShift;
442 case UnsignedRightShiftUpdate: return UnsignedRightShift;
443 case AndUpdate: return And;
444 case OrUpdate: return Or;
445 case XorUpdate: return Xor;
446 default: return Unknown;
450 static void ast_visit_list(ast_list_t *ast_list, visit_behavior_t (*visitor)(ast_t *, void *), void *userdata) {
451 for (ast_list_t *ast = ast_list; ast; ast = ast->next)
452 ast_visit(ast->ast, visitor, userdata);
455 static void ast_visit_args(arg_ast_t *args, visit_behavior_t (*visitor)(ast_t *, void *), void *userdata) {
456 for (arg_ast_t *arg = args; arg; arg = arg->next)
457 ast_visit(arg->value, visitor, userdata);
460 void ast_visit(ast_t *ast, visit_behavior_t (*visitor)(ast_t *, void *), void *userdata) {
462 if (visitor(ast, userdata) == VISIT_STOP) return;
473 case Metadata: return;
474 case TextJoin: ast_visit_list(Match(ast, TextJoin)->children, visitor, userdata); return;
476 DeclareMatch(decl, ast, Declare);
477 ast_visit(decl->var, visitor, userdata);
478 ast_visit(decl->value, visitor, userdata);
482 DeclareMatch(assign, ast, Assign);
483 ast_visit_list(assign->targets, visitor, userdata);
484 ast_visit_list(assign->values, visitor, userdata);
488 binary_operands_t op = BINARY_OPERANDS(ast);
489 ast_visit(op.lhs, visitor, userdata);
490 ast_visit(op.rhs, visitor, userdata);
494 ast_visit(Match(ast, Negative)->value, visitor, userdata);
498 ast_visit(Match(ast, Not)->value, visitor, userdata);
502 ast_visit(Match(ast, HeapAllocate)->value, visitor, userdata);
505 case StackReference: {
506 ast_visit(Match(ast, StackReference)->value, visitor, userdata);
510 DeclareMatch(min, ast, Min);
511 ast_visit(min->lhs, visitor, userdata);
512 ast_visit(min->key, visitor, userdata);
513 ast_visit(min->rhs, visitor, userdata);
517 DeclareMatch(max, ast, Max);
518 ast_visit(max->lhs, visitor, userdata);
519 ast_visit(max->key, visitor, userdata);
520 ast_visit(max->rhs, visitor, userdata);
524 ast_visit_list(Match(ast, List)->items, visitor, userdata);
528 DeclareMatch(table, ast, Table);
529 ast_visit_list(table->entries, visitor, userdata);
530 ast_visit(table->default_value, visitor, userdata);
531 ast_visit(table->fallback, visitor, userdata);
535 DeclareMatch(entry, ast, TableEntry);
536 ast_visit(entry->key, visitor, userdata);
537 ast_visit(entry->value, visitor, userdata);
540 case Comprehension: {
541 DeclareMatch(comp, ast, Comprehension);
542 ast_visit(comp->expr, visitor, userdata);
543 ast_visit_list(comp->vars, visitor, userdata);
544 ast_visit(comp->iter, visitor, userdata);
545 ast_visit(comp->filter, visitor, userdata);
549 DeclareMatch(def, ast, FunctionDef);
550 ast_visit(def->name, visitor, userdata);
551 ast_visit_args(def->args, visitor, userdata);
552 ast_visit(def->body, visitor, userdata);
556 DeclareMatch(def, ast, ConvertDef);
557 ast_visit_args(def->args, visitor, userdata);
558 ast_visit(def->body, visitor, userdata);
562 DeclareMatch(lambda, ast, Lambda);
563 ast_visit_args(lambda->args, visitor, userdata);
564 ast_visit(lambda->body, visitor, userdata);
568 DeclareMatch(call, ast, FunctionCall);
569 ast_visit(call->fn, visitor, userdata);
570 ast_visit_args(call->args, visitor, userdata);
574 DeclareMatch(call, ast, MethodCall);
575 ast_visit(call->self, visitor, userdata);
576 ast_visit_args(call->args, visitor, userdata);
580 ast_visit_list(Match(ast, Block)->statements, visitor, userdata);
584 DeclareMatch(for_, ast, For);
585 ast_visit_list(for_->vars, visitor, userdata);
586 ast_visit(for_->iter, visitor, userdata);
587 ast_visit(for_->body, visitor, userdata);
588 ast_visit(for_->empty, visitor, userdata);
592 DeclareMatch(while_, ast, While);
593 ast_visit(while_->condition, visitor, userdata);
594 ast_visit(while_->body, visitor, userdata);
598 ast_visit(Match(ast, Repeat)->body, visitor, userdata);
602 DeclareMatch(if_, ast, If);
603 ast_visit(if_->condition, visitor, userdata);
604 ast_visit(if_->body, visitor, userdata);
605 ast_visit(if_->else_body, visitor, userdata);
609 DeclareMatch(when, ast, When);
610 ast_visit(when->subject, visitor, userdata);
611 for (when_clause_t *clause = when->clauses; clause; clause = clause->next) {
612 ast_visit(clause->pattern, visitor, userdata);
613 ast_visit(clause->body, visitor, userdata);
615 ast_visit(when->else_body, visitor, userdata);
619 DeclareMatch(reduction, ast, Reduction);
620 ast_visit(reduction->key, visitor, userdata);
621 ast_visit(reduction->iter, visitor, userdata);
628 ast_visit(Match(ast, Defer)->body, visitor, userdata);
632 ast_visit(Match(ast, Return)->value, visitor, userdata);
636 DeclareMatch(def, ast, StructDef);
637 ast_visit_args(def->fields, visitor, userdata);
638 ast_visit(def->namespace, visitor, userdata);
642 DeclareMatch(def, ast, EnumDef);
643 for (tag_ast_t *tag = def->tags; tag; tag = tag->next)
644 ast_visit_args(tag->fields, visitor, userdata);
645 ast_visit(def->namespace, visitor, userdata);
649 ast_visit(Match(ast, LangDef)->namespace, visitor, userdata);
653 DeclareMatch(index, ast, Index);
654 ast_visit(index->indexed, visitor, userdata);
655 ast_visit(index->index, visitor, userdata);
659 ast_visit(Match(ast, FieldAccess)->fielded, visitor, userdata);
663 ast_visit(Match(ast, NonOptional)->value, visitor, userdata);
667 DeclareMatch(show, ast, DebugLog);
668 ast_visit_list(show->values, visitor, userdata);
672 DeclareMatch(assert, ast, Assert);
673 ast_visit(assert->expr, visitor, userdata);
674 ast_visit(assert->message, visitor, userdata);
678 ast_visit(Match(ast, Use)->var, visitor, userdata);
682 ast_visit_list(Match(ast, InlineCCode)->chunks, visitor, userdata);
685 default: errx(1, "Visiting is not supported for this AST: %s", Text$as_c_string(ast_to_sexp(ast)));
690 static void _recursive_type_ast_visit(type_ast_t *ast, void *userdata) {
691 if (ast == NULL) return;
693 visit_behavior_t (*visit)(type_ast_t *, void *) = ((Closure_t *)userdata)->fn;
694 void *visitor_userdata = ((Closure_t *)userdata)->userdata;
695 if (visit(ast, visitor_userdata) == VISIT_STOP) return;
699 case VarTypeAST: break;
700 case PointerTypeAST: {
701 _recursive_type_ast_visit(Match(ast, PointerTypeAST)->pointed, userdata);
705 _recursive_type_ast_visit(Match(ast, ListTypeAST)->item, userdata);
709 DeclareMatch(table, ast, TableTypeAST);
710 _recursive_type_ast_visit(table->key, userdata);
711 _recursive_type_ast_visit(table->value, userdata);
714 case FunctionTypeAST: {
715 DeclareMatch(fn, ast, FunctionTypeAST);
716 for (arg_ast_t *arg = fn->args; arg; arg = arg->next)
717 _recursive_type_ast_visit(arg->type, userdata);
718 _recursive_type_ast_visit(fn->ret, userdata);
721 case OptionalTypeAST: {
722 _recursive_type_ast_visit(Match(ast, OptionalTypeAST)->type, userdata);
726 for (tag_ast_t *tag = Match(ast, EnumTypeAST)->tags; tag; tag = tag->next) {
727 for (arg_ast_t *field = tag->fields; field; field = field->next) {
728 _recursive_type_ast_visit(field->type, userdata);
733 default: errx(1, "Invalid type AST");
737 static visit_behavior_t _type_ast_visit(ast_t *ast, void *userdata) {
740 _recursive_type_ast_visit(Match(ast, Declare)->type, userdata);
744 for (arg_ast_t *arg = Match(ast, FunctionDef)->args; arg; arg = arg->next)
745 _recursive_type_ast_visit(arg->type, userdata);
746 _recursive_type_ast_visit(Match(ast, FunctionDef)->ret_type, userdata);
750 for (arg_ast_t *arg = Match(ast, Lambda)->args; arg; arg = arg->next)
751 _recursive_type_ast_visit(arg->type, userdata);
752 _recursive_type_ast_visit(Match(ast, Lambda)->ret_type, userdata);
756 for (arg_ast_t *arg = Match(ast, ConvertDef)->args; arg; arg = arg->next)
757 _recursive_type_ast_visit(arg->type, userdata);
758 _recursive_type_ast_visit(Match(ast, ConvertDef)->ret_type, userdata);
762 for (arg_ast_t *field = Match(ast, StructDef)->fields; field; field = field->next)
763 _recursive_type_ast_visit(field->type, userdata);
767 for (tag_ast_t *tag = Match(ast, EnumDef)->tags; tag; tag = tag->next) {
768 for (arg_ast_t *field = tag->fields; field; field = field->next) {
769 _recursive_type_ast_visit(field->type, userdata);
775 _recursive_type_ast_visit(Match(ast, InlineCCode)->type_ast, userdata);
780 return VISIT_PROCEED;
783 void type_ast_visit(ast_t *ast, visit_behavior_t (*visitor)(type_ast_t *, void *), void *userdata) {
784 Closure_t fn = {.fn = visitor, .userdata = userdata};
785 ast_visit(ast, _type_ast_visit, &fn);
788 OptionalText_t ast_metadata(ast_t *ast, const char *key) {
789 if (ast->tag != Block) return NONE_TEXT;
790 Text_t key_text = Text$from_str(key);
791 for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) {
792 if (stmt->ast->tag == Metadata) {
793 DeclareMatch(m, stmt->ast, Metadata);
794 if (Text$equal_values(m->key, key_text)) return m->value;