From a4cc207022712c238e827bda9e871870970cca25 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 24 Aug 2024 12:59:17 +0100 Subject: Add a recursion depth limit for #3987 with a consideration to use a `switch` statement or refactor the code to not use a large if-else chain --- src/parser.hpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/parser.hpp') diff --git a/src/parser.hpp b/src/parser.hpp index 565a8e621..711e6f060 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -120,6 +120,8 @@ struct AstFile { bool allow_type; bool in_when_statement; + isize recursion_depth_else_if; + isize total_file_decl_count; isize delayed_decl_count; Slice decls; -- cgit v1.2.3 From b6d9a0c32e64fd4ce4d3350fca9a671d5daac126 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 24 Aug 2024 13:16:55 +0100 Subject: Manually implement tail-recursion for `parse_if_stmt` --- src/parser.cpp | 30 ++++++++++++++++++++---------- src/parser.hpp | 2 -- 2 files changed, 20 insertions(+), 12 deletions(-) (limited to 'src/parser.hpp') diff --git a/src/parser.cpp b/src/parser.cpp index 6461c9dd0..1a45c4ea5 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4532,12 +4532,10 @@ gb_internal Ast *parse_if_stmt(AstFile *f) { return ast_bad_stmt(f, f->curr_token, f->curr_token); } - if (f->recursion_depth_else_if > 256) { - syntax_error(f->curr_token, "if-else chain recursion depth limit hit. Consider using a 'switch' statement instead or refactor the code to not require a large if-else chain"); - f->recursion_depth_else_if = 0; - return ast_bad_stmt(f, f->curr_token, f->curr_token); - } + Ast *top_if_stmt = nullptr; + Ast *prev_if_stmt = nullptr; +if_else_chain:; Token token = expect_token(f, Token_if); Ast *init = nullptr; Ast *cond = nullptr; @@ -4579,14 +4577,24 @@ gb_internal Ast *parse_if_stmt(AstFile *f) { ignore_strict_style = true; } skip_possible_newline_for_literal(f, ignore_strict_style); + + Ast *curr_if_stmt = ast_if_stmt(f, token, init, cond, body, nullptr); + if (top_if_stmt == nullptr) { + top_if_stmt = curr_if_stmt; + } + if (prev_if_stmt != nullptr) { + prev_if_stmt->IfStmt.else_stmt = curr_if_stmt; + } + if (f->curr_token.kind == Token_else) { Token else_token = expect_token(f, Token_else); switch (f->curr_token.kind) { case Token_if: - f->recursion_depth_else_if += 1; - else_stmt = parse_if_stmt(f); - f->recursion_depth_else_if -= 1; - break; + // NOTE(bill): Instead of relying on recursive descent for an if-else chain + // we can just inline the tail-recursion manually with a simple loop like + // construct using a `goto` + prev_if_stmt = curr_if_stmt; + goto if_else_chain; case Token_OpenBrace: else_stmt = parse_block_stmt(f, false); break; @@ -4601,7 +4609,9 @@ gb_internal Ast *parse_if_stmt(AstFile *f) { } } - return ast_if_stmt(f, token, init, cond, body, else_stmt); + curr_if_stmt->IfStmt.else_stmt = else_stmt; + + return top_if_stmt; } gb_internal Ast *parse_when_stmt(AstFile *f) { diff --git a/src/parser.hpp b/src/parser.hpp index 711e6f060..565a8e621 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -120,8 +120,6 @@ struct AstFile { bool allow_type; bool in_when_statement; - isize recursion_depth_else_if; - isize total_file_decl_count; isize delayed_decl_count; Slice decls; -- cgit v1.2.3 From 8ba87e01bd9a26ffd391d2ad69ff0eb6a023d1cf Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 24 Aug 2024 13:56:30 +0100 Subject: Improve `parse_enforce_tabs` usage --- src/parser.cpp | 22 +++++++++++++++++----- src/parser.hpp | 5 ++++- 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'src/parser.hpp') diff --git a/src/parser.cpp b/src/parser.cpp index 1a45c4ea5..b250a3163 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1921,6 +1921,9 @@ gb_internal Array parse_enum_field_list(AstFile *f) { f->curr_token.kind != Token_EOF) { CommentGroup *docs = f->lead_comment; CommentGroup *comment = nullptr; + + parse_enforce_tabs(f); + Ast *name = parse_value(f); Ast *value = nullptr; if (f->curr_token.kind == Token_Eq) { @@ -2259,6 +2262,7 @@ gb_internal Array parse_union_variant_list(AstFile *f) { auto variants = array_make(ast_allocator(f)); while (f->curr_token.kind != Token_CloseBrace && f->curr_token.kind != Token_EOF) { + parse_enforce_tabs(f); Ast *type = parse_type(f); if (type->kind != Ast_BadExpr) { array_add(&variants, type); @@ -4274,6 +4278,7 @@ gb_internal Ast *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_fl while (f->curr_token.kind != follow && f->curr_token.kind != Token_Colon && f->curr_token.kind != Token_EOF) { + if (!is_signature) parse_enforce_tabs(f); u32 flags = parse_field_prefixes(f); Ast *param = parse_var_type(f, allow_ellipsis, allow_typeid_token); if (param->kind == Ast_Ellipsis) { @@ -4363,6 +4368,8 @@ gb_internal Ast *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_fl f->curr_token.kind != Token_EOF && f->curr_token.kind != Token_Semicolon) { CommentGroup *docs = f->lead_comment; + + if (!is_signature) parse_enforce_tabs(f); u32 set_flags = parse_field_prefixes(f); Token tag = {}; Array names = parse_ident_list(f, allow_poly_names); @@ -5375,6 +5382,11 @@ gb_internal u64 check_vet_flags(AstFile *file) { gb_internal void parse_enforce_tabs(AstFile *f) { + // Checks to see if tabs have been used for indentation + if ((check_vet_flags(f) & VetFlag_Tabs) == 0) { + return; + } + Token prev = f->prev_token; Token curr = f->curr_token; if (prev.pos.line < curr.pos.line) { @@ -5391,6 +5403,10 @@ gb_internal void parse_enforce_tabs(AstFile *f) { isize len = end-it; for (isize i = 0; i < len; i++) { + if (it[i] == '/') { + // ignore comments + break; + } if (it[i] == ' ') { syntax_error(curr, "With '-vet-tabs', tabs must be used for indentation"); break; @@ -5405,11 +5421,7 @@ gb_internal Array parse_stmt_list(AstFile *f) { while (f->curr_token.kind != Token_case && f->curr_token.kind != Token_CloseBrace && f->curr_token.kind != Token_EOF) { - - // Checks to see if tabs have been used for indentation - if (check_vet_flags(f) & VetFlag_Tabs) { - parse_enforce_tabs(f); - } + parse_enforce_tabs(f); Ast *stmt = parse_stmt(f); if (stmt && stmt->kind != Ast_EmptyStmt) { diff --git a/src/parser.hpp b/src/parser.hpp index 565a8e621..f1794f79a 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -885,4 +885,7 @@ gb_internal gb_inline gbAllocator ast_allocator(AstFile *f) { gb_internal Ast *alloc_ast_node(AstFile *f, AstKind kind); gb_internal gbString expr_to_string(Ast *expression); -gb_internal bool allow_field_separator(AstFile *f); \ No newline at end of file +gb_internal bool allow_field_separator(AstFile *f); + + +gb_internal void parse_enforce_tabs(AstFile *f); \ No newline at end of file -- cgit v1.2.3 From 43ec2b9253379d03b69f40b57ef2aafd2c968416 Mon Sep 17 00:00:00 2001 From: avanspector Date: Mon, 26 Aug 2024 20:59:16 +0200 Subject: checker: delay foreign block checking if file scope, otherwise as before --- src/checker.cpp | 28 ++++++++++++++++++++-------- src/parser.cpp | 2 +- src/parser.hpp | 1 + 3 files changed, 22 insertions(+), 9 deletions(-) (limited to 'src/parser.hpp') diff --git a/src/checker.cpp b/src/checker.cpp index 35b84c155..fdc1ce840 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -4474,8 +4474,6 @@ gb_internal void correct_type_aliases_in_scope(CheckerContext *c, Scope *s) { } } -bool is_collect_entities_post = false; - // NOTE(bill): If file_scopes == nullptr, this will act like a local scope gb_internal void check_collect_entities(CheckerContext *c, Slice const &nodes) { AstFile *curr_file = nullptr; @@ -4534,8 +4532,8 @@ gb_internal void check_collect_entities(CheckerContext *c, Slice const &n case_end; case_ast_node(fb, ForeignBlockDecl, decl); - if (is_collect_entities_post) { - check_add_foreign_block_decl(c, decl); + if (curr_file != nullptr) { + array_add(&curr_file->delayed_decls_queues[AstDelayQueue_ForeignBlock], decl); } case_end; @@ -4552,6 +4550,14 @@ gb_internal void check_collect_entities(CheckerContext *c, Slice const &n // NOTE(bill): 'when' stmts need to be handled after the other as the condition may refer to something // declared after this stmt in source if (curr_file == nullptr) { + // For 'foreign' block statements that are not in file scope. + for_array(decl_index, nodes) { + Ast *decl = nodes[decl_index]; + if (decl->kind == Ast_ForeignBlockDecl) { + check_add_foreign_block_decl(c, decl); + } + } + for_array(decl_index, nodes) { Ast *decl = nodes[decl_index]; if (decl->kind == Ast_WhenStmt) { @@ -5512,6 +5518,16 @@ gb_internal void check_import_entities(Checker *c) { correct_type_aliases_in_scope(&ctx, pkg->scope); } + for_array(i, pkg->files) { + AstFile *f = pkg->files[i]; + reset_checker_context(&ctx, f, &untyped); + + for (Ast *decl : f->delayed_decls_queues[AstDelayQueue_ForeignBlock]) { + check_add_foreign_block_decl(&ctx, decl); + } + array_clear(&f->delayed_decls_queues[AstDelayQueue_ForeignBlock]); + } + for_array(i, pkg->files) { AstFile *f = pkg->files[i]; reset_checker_context(&ctx, f, &untyped); @@ -6441,10 +6457,6 @@ gb_internal void check_parsed_files(Checker *c) { TIME_SECTION("export entities - post"); check_export_entities(c); - TIME_SECTION("collect entities - post"); - is_collect_entities_post = true; - check_collect_entities_all(c); - TIME_SECTION("add entities from packages"); check_merge_queues_into_arrays(c); diff --git a/src/parser.cpp b/src/parser.cpp index b250a3163..e3143dd33 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -6446,7 +6446,7 @@ gb_internal bool parse_file(Parser *p, AstFile *f) { } f->total_file_decl_count += calc_decl_count(stmt); - if (stmt->kind == Ast_WhenStmt || stmt->kind == Ast_ExprStmt || stmt->kind == Ast_ImportDecl) { + if (stmt->kind == Ast_WhenStmt || stmt->kind == Ast_ExprStmt || stmt->kind == Ast_ImportDecl || stmt->kind == Ast_ForeignBlockDecl) { f->delayed_decl_count += 1; } } diff --git a/src/parser.hpp b/src/parser.hpp index f1794f79a..d5117a31e 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -82,6 +82,7 @@ enum AstFileFlag : u32 { enum AstDelayQueueKind { AstDelayQueue_Import, AstDelayQueue_Expr, + AstDelayQueue_ForeignBlock, AstDelayQueue_COUNT, }; -- cgit v1.2.3