From e2140817df9345bcc7dd421360fa814b5f88e7ed Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 2 Apr 2023 12:13:11 +0200 Subject: [PATCH] Using For from Array --- base.cpp | 2 -- core_codegen_c_language.cpp | 10 +++++----- core_compiler.cpp | 6 +++--- core_typechecking.cpp | 26 +++++++++++++------------- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/base.cpp b/base.cpp index 2353739..523db1a 100644 --- a/base.cpp +++ b/base.cpp @@ -440,5 +440,3 @@ operator!=(Intern_String a, Intern_String b) { #define For_Linked_List_Named(a, it) for (auto *it = (a); it; it = it->next) // @todo: reference? #define For_Linked_List(a) For_Linked_List_Named(a, it) -#define For_Named(a, it) for (auto &it : (a)) -#define For(a) For_Named((a), it) diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index a8bafba..f951287 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -755,7 +755,7 @@ gen_ast(Ast *ast) { global_indent++; For(node->cases) { - For_Named(it->labels, label) { + For2(it->labels, label) { gen_line(it); genln(""); gen("case "); @@ -951,7 +951,7 @@ compile_to_c_code() { } // Generate slice and tuple types - For_Named(pctx->all_types, type) { + For2(pctx->all_types, type) { Scoped_Arena scratch(pctx->scratch); if (type->kind == TYPE_SLICE) { @@ -1021,7 +1021,7 @@ compile_to_c_code() { genln("int64_t type_infos_len = %d;", length(&pctx->all_types)); genln("Type_Info *type_infos = (Type_Info[]){"); global_indent++; - For_Named(pctx->all_types, t) { + For2(pctx->all_types, t) { if (t->kind == TYPE_POLYMORPH) continue; genln("{/*%Q*/.kind = %d, .size = %d, .align = %d, .is_unsigned = %s, .type = %d, ", typestring(t), (S32)t->kind, (S32)t->size, (S32)t->align, t->is_unsigned ? "true" : "false", t->type_id); @@ -1038,7 +1038,7 @@ compile_to_c_code() { gen(".lambda_return = %d, ", t->func.ret->type_id); gen(".lambda_arguments.len = %d, ", t->func.args.len); gen(".lambda_arguments.data = (Type_Info[%d]){", t->func.args.len); - For_Named(t->func.args, arg) { + For2(t->func.args, arg) { gen("{.type = %d}, ", arg->type_id); } gen("}"); @@ -1047,7 +1047,7 @@ compile_to_c_code() { case TYPE_STRUCT: { gen(".struct_members.len = %d, ", t->agg.members.len); gen(".struct_members.data = (Type_Info_Struct_Member[]){"); - For_Named(t->agg.members, m) { + For2(t->agg.members, m) { gen("{.name = (%Q){(uint8_t *)\"%Q\", %d}, .type = %d, .offset = %d}, ", prefixed_string_type, m.name, m.name.len, m.type->type_id, m.offset); } gen("}"); diff --git a/core_compiler.cpp b/core_compiler.cpp index 4a300dd..8f05f53 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -191,7 +191,7 @@ CORE_Static void parse_all_modules() { pctx->time.parsing = os_time(); - For_Named(pctx->modules, module) { + For2(pctx->modules, module) { if (module->state != MODULE_REGISTERED) continue; @@ -287,8 +287,8 @@ resolve_everything_in_module(Ast_Module *module) { if (module->state == MODULE_RESOLVED) return; pctx->time.typechecking = os_time(); - For_Named(module->all_loaded_files, file) { - For_Named(file->decls, decl) { + For2(module->all_loaded_files, file) { + For2(file->decls, decl) { if (decl->flags & AST_POLYMORPH) continue; // @cleanup: Why I'm not calling resolve_decl here? diff --git a/core_typechecking.cpp b/core_typechecking.cpp index e2493ad..eb8a0b7 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -461,7 +461,7 @@ inside_scope_search(Scope_Search *search, Ast_Scope *scope, int level) { scope->visit_id = search->scope_visit_id; // Search for declarations in current scope - For_Named(scope->decls, decl) { + For2(scope->decls, decl) { if (decl->name == search->name) { search->results.add(decl); if (search->exit_on_find) { @@ -488,7 +488,7 @@ CORE_Static void scope_search(Scope_Search *search) { // Climb the scope tree and search each scope in module and also // search in implicitly imported scopes - For_Named(search->scopes, scope) { + For2(search->scopes, scope) { for (Ast_Scope *it = scope; it; it = it->parent_scope) { inside_scope_search(search, it, 0); @@ -844,7 +844,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret) { resolve_stmt(it->init, ret); resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL); try_propagating_resolved_type_to_untyped_literals(it->expr, pctx->type_bool); - For_Named(it->scope->stmts, jt) + For2(it->scope->stmts, jt) resolve_stmt(jt, ret); } BREAK(); @@ -858,18 +858,18 @@ resolve_stmt(Ast *ast, Ast_Type *ret) { try_converting_untyped_to_default_type(&base.value); try_propagating_resolved_type_to_untyped_literals(node->value, base.type); For(node->cases) { - For_Named(it->labels, label) { + For2(it->labels, label) { Operand op = resolve_expr(label, AST_CANT_BE_NULL, 0, 0); if (!op.is_const) compiler_error(label->pos, "Switch label required to be constant"); make_sure_value_is_compatible_with_type(label->pos, &op, base.type, TYPE_AND_EXPR_REQUIRED); try_propagating_resolved_type_to_untyped_literals(label, base.type); } - For_Named(it->scope->stmts, stmt) + For2(it->scope->stmts, stmt) resolve_stmt(stmt, ret); } if (node->default_scope) - For_Named(node->default_scope->stmts, stmt) + For2(node->default_scope->stmts, stmt) resolve_stmt(stmt, ret); BREAK(); @@ -1066,7 +1066,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type) { Ast_Type *item_type = 0; if (is_flag_set(it->call_flags, CALL_NAME)) { - For_Named(type->agg.members, m) { + For2(type->agg.members, m) { if (it->name->intern_val == m.name) { it->resolved_name = m.name; it->resolved_index = (S32)type->agg.members.get_index(m); @@ -1646,7 +1646,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str // every time. // Might be wise to revise it later but the approach of starting with // lambda_args seems correct. - For_Named(lambda->args, lambda_arg) { + For2(lambda->args, lambda_arg) { // assert(lambda_arg->type); // @todo: maybe add this check at the end Ast_Call_Item *item = 0; @@ -1682,9 +1682,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str } // Make sure we found every item. - For_Named(node->exprs, call_it) { + For2(node->exprs, call_it) { bool included = false; - For_Named(matches, match_it) { + For2(matches, match_it) { if (call_it == match_it.call_arg) { included = true; break; @@ -1712,8 +1712,8 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str // We now need to fix the matches list CAUSE ITS FUCKED // Thanks polymorphism - For_Named(matches, match_it) { - For_Named(lambda->args, lambda_it) { + For2(matches, match_it) { + For2(lambda->args, lambda_it) { if (match_it.lambda_arg->name == lambda_it->name) { match_it.lambda_arg = lambda_it; break; @@ -1859,7 +1859,7 @@ resolve_decl(Ast_Decl *ast) { node->type_val = type_enum(node, type_of_enum); S64 value = 1; - For_Named(node->scope->decls, decl) { + For2(node->scope->decls, decl) { Operand op = {}; if (decl->expr) { op = require_const_int(decl->expr, AST_CANT_BE_NULL);