From 277ae4e2b026b2b45d2ec5b6d59097f42722846d Mon Sep 17 00:00:00 2001 From: matias Date: Wed, 10 May 2023 02:58:17 -0400 Subject: Patch "no_copy" typo in parser.cpp --- src/parser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index f33a44f31..698ba99ab 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -2462,7 +2462,7 @@ gb_internal Ast *parse_operand(AstFile *f, bool lhs) { } is_raw_union = true; } else if (tag.string == "no_copy") { - if (is_packed) { + if (no_copy) { syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string)); } no_copy = true; -- cgit v1.2.3 From 8bf32ac697ea21ff3b37e5b31fe0fc10e700c9a4 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 22 May 2023 12:53:29 +0100 Subject: Minor change to handling of propagation of errors with `---` as a value --- src/check_decl.cpp | 9 ++++----- src/check_expr.cpp | 45 +++++++++++++++++++++----------------------- src/check_type.cpp | 6 +++--- src/checker.cpp | 4 ++-- src/llvm_backend.cpp | 10 ++-------- src/llvm_backend_const.cpp | 2 +- src/llvm_backend_debug.cpp | 2 +- src/llvm_backend_expr.cpp | 16 ++++++++-------- src/llvm_backend_general.cpp | 4 ++-- src/llvm_backend_proc.cpp | 8 ++++---- src/parser.cpp | 14 +++++++------- src/parser.hpp | 2 +- src/parser_pos.cpp | 4 ++-- src/tokenizer.cpp | 6 +++--- src/types.cpp | 19 ++++++++----------- 15 files changed, 69 insertions(+), 82 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index b16215571..84b0d39d8 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -70,13 +70,12 @@ gb_internal Type *check_init_variable(CheckerContext *ctx, Entity *e, Operand *o // NOTE(bill): Use the type of the operand Type *t = operand->type; if (is_type_untyped(t)) { - if (t == t_invalid || is_type_untyped_nil(t)) { - error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name)); + if (is_type_untyped_uninit(t)) { + error(e->token, "Invalid use of --- in %.*s", LIT(context_name)); e->type = t_invalid; return nullptr; - } - if (t == t_invalid || is_type_untyped_undef(t)) { - error(e->token, "Invalid use of --- in %.*s", LIT(context_name)); + } else if (t == t_invalid || is_type_untyped_nil(t)) { + error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name)); e->type = t_invalid; return nullptr; } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 20a690e1e..830b5315d 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -646,11 +646,8 @@ gb_internal i64 check_distance_between_types(CheckerContext *c, Operand *operand Type *src = base_type(s); Type *dst = base_type(type); - if (is_type_untyped_undef(src)) { - if (type_has_undef(dst)) { - return 1; - } - return -1; + if (is_type_untyped_uninit(src)) { + return 1; } if (is_type_untyped_nil(src)) { @@ -993,13 +990,13 @@ gb_internal void check_assignment(CheckerContext *c, Operand *operand, Type *typ if (is_type_untyped(operand->type)) { Type *target_type = type; if (type == nullptr || is_type_any(type)) { - if (type == nullptr && is_type_untyped_nil(operand->type)) { - error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name)); + if (type == nullptr && is_type_untyped_uninit(operand->type)) { + error(operand->expr, "Use of --- in %.*s", LIT(context_name)); operand->mode = Addressing_Invalid; return; } - if (type == nullptr && is_type_untyped_undef(operand->type)) { - error(operand->expr, "Use of --- in %.*s", LIT(context_name)); + if (type == nullptr && is_type_untyped_nil(operand->type)) { + error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name)); operand->mode = Addressing_Invalid; return; } @@ -3969,7 +3966,7 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar case Type_Union: - if (!is_operand_nil(*operand) && !is_operand_undef(*operand)) { + if (!is_operand_nil(*operand) && !is_operand_uninit(*operand)) { TEMPORARY_ALLOCATOR_GUARD(); isize count = t->Union.variants.count; @@ -4036,8 +4033,8 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar error_line("\n\n"); return; - } else if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) { - target_type = t_untyped_undef; + } else if (is_type_untyped_uninit(operand->type)) { + target_type = t_untyped_uninit; } else if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) { begin_error_block(); defer (end_error_block()); @@ -4070,8 +4067,8 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar default: - if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) { - target_type = t_untyped_undef; + if (is_type_untyped_uninit(operand->type)) { + target_type = t_untyped_uninit; } else if (is_type_untyped_nil(operand->type) && type_has_nil(target_type)) { target_type = t_untyped_nil; } else { @@ -4083,8 +4080,8 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar } if (is_type_any(target_type) && is_type_untyped(operand->type)) { - if (is_type_untyped_nil(operand->type) && is_type_untyped_undef(operand->type)) { - + if (is_type_untyped_nil(operand->type) && is_type_untyped_uninit(operand->type)) { + } else { target_type = default_type(operand->type); } @@ -5197,9 +5194,9 @@ gb_internal bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize } Ast *rhs_expr = unparen_expr(rhs[i]); - if (allow_undef && rhs_expr != nullptr && rhs_expr->kind == Ast_Undef) { + if (allow_undef && rhs_expr != nullptr && rhs_expr->kind == Ast_Uninit) { // NOTE(bill): Just handle this very specific logic here - o.type = t_untyped_undef; + o.type = t_untyped_uninit; o.mode = Addressing_Value; o.expr = rhs[i]; add_type_and_value(c, rhs[i], o.mode, o.type, o.value); @@ -7167,11 +7164,11 @@ gb_internal bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 } gb_internal bool ternary_compare_types(Type *x, Type *y) { - if (is_type_untyped_undef(x) && type_has_undef(y)) { + if (is_type_untyped_uninit(x)) { return true; } else if (is_type_untyped_nil(x) && type_has_nil(y)) { return true; - } else if (is_type_untyped_undef(y) && type_has_undef(x)) { + } else if (is_type_untyped_uninit(y)) { return true; } else if (is_type_untyped_nil(y) && type_has_nil(x)) { return true; @@ -7708,7 +7705,7 @@ gb_internal ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *n } o->type = x.type; - if (is_type_untyped_nil(o->type) || is_type_untyped_undef(o->type)) { + if (is_type_untyped_nil(o->type) || is_type_untyped_uninit(o->type)) { o->type = y.type; } @@ -9601,9 +9598,9 @@ gb_internal ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast check_ident(c, o, node, nullptr, type_hint, false); case_end; - case_ast_node(u, Undef, node); + case_ast_node(u, Uninit, node); o->mode = Addressing_Value; - o->type = t_untyped_undef; + o->type = t_untyped_uninit; error(node, "Use of --- outside of variable declaration"); case_end; @@ -10167,7 +10164,7 @@ gb_internal gbString write_expr_to_string(gbString str, Ast *node, bool shorthan str = string_append_string(str, bd->name.string); case_end; - case_ast_node(ud, Undef, node); + case_ast_node(ud, Uninit, node); str = gb_string_appendc(str, "---"); case_end; diff --git a/src/check_type.cpp b/src/check_type.cpp index dfe774f6b..bbfc25a12 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -135,7 +135,7 @@ gb_internal void check_struct_fields(CheckerContext *ctx, Ast *node, Slicemodule, var.var); - if (is_type_untyped_undef(init.type)) { - // LLVMSetInitializer(var.var.value, LLVMGetUndef(global_type)); - LLVMSetInitializer(var.var.value, LLVMConstNull(global_type)); - var.is_initialized = true; - continue; - } else if (is_type_untyped_nil(init.type)) { + if (is_type_untyped_nil(init.type)) { LLVMSetInitializer(var.var.value, LLVMConstNull(global_type)); var.is_initialized = true; continue; @@ -2363,8 +2358,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { } } } - if (!var.is_initialized && - (is_type_untyped_nil(tav.type) || is_type_untyped_undef(tav.type))) { + if (!var.is_initialized && is_type_untyped_nil(tav.type)) { var.is_initialized = true; } } diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index 8db6e2a1f..ea25a4594 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -1,6 +1,6 @@ gb_internal bool lb_is_const(lbValue value) { LLVMValueRef v = value.value; - if (is_type_untyped_nil(value.type) || is_type_untyped_undef(value.type)) { + if (is_type_untyped_nil(value.type)) { // TODO(bill): Is this correct behaviour? return true; } diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index fd26c41a0..bec719a4a 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -282,7 +282,7 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { case Basic_UntypedString: GB_PANIC("Basic_UntypedString"); break; case Basic_UntypedRune: GB_PANIC("Basic_UntypedRune"); break; case Basic_UntypedNil: GB_PANIC("Basic_UntypedNil"); break; - case Basic_UntypedUndef: GB_PANIC("Basic_UntypedUndef"); break; + case Basic_UntypedUninit: GB_PANIC("Basic_UntypedUninit"); break; default: GB_PANIC("Basic Unhandled"); break; } diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 69443c9a3..143e38a8d 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1486,12 +1486,12 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { GB_ASSERT(src != nullptr); GB_ASSERT(dst != nullptr); + if (is_type_untyped_uninit(src)) { + return lb_const_undef(m, t); + } if (is_type_untyped_nil(src)) { return lb_const_nil(m, t); } - if (is_type_untyped_undef(src)) { - return lb_const_undef(m, t); - } if (LLVMIsConstant(value.value)) { if (is_type_any(dst)) { @@ -2132,12 +2132,12 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { if (is_type_any(dst)) { + if (is_type_untyped_uninit(src)) { + return lb_const_undef(p->module, t); + } if (is_type_untyped_nil(src)) { return lb_const_nil(p->module, t); } - if (is_type_untyped_undef(src)) { - return lb_const_undef(p->module, t); - } lbAddr result = lb_add_local_generated(p, t, true); @@ -3136,11 +3136,11 @@ gb_internal lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) { return lb_addr_load(p, lb_build_addr(p, expr)); case_end; - case_ast_node(u, Undef, expr) + case_ast_node(u, Uninit, expr) lbValue res = {}; if (is_type_untyped(type)) { res.value = nullptr; - res.type = t_untyped_undef; + res.type = t_untyped_uninit; } else { res.value = LLVMGetUndef(lb_type(m, type)); res.type = type; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 7d2f574fe..628c1e5fa 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -677,7 +677,7 @@ gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { return; } GB_ASSERT(value.type != nullptr); - if (is_type_untyped_undef(value.type)) { + if (is_type_untyped_uninit(value.type)) { Type *t = lb_addr_type(addr); value.type = t; value.value = LLVMGetUndef(lb_type(p->module, t)); @@ -1828,7 +1828,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { case Basic_UntypedString: GB_PANIC("Basic_UntypedString"); break; case Basic_UntypedRune: GB_PANIC("Basic_UntypedRune"); break; case Basic_UntypedNil: GB_PANIC("Basic_UntypedNil"); break; - case Basic_UntypedUndef: GB_PANIC("Basic_UntypedUndef"); break; + case Basic_UntypedUninit: GB_PANIC("Basic_UntypedUninit"); break; } break; case Type_Named: diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index ddf058668..b8353a466 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -3218,10 +3218,10 @@ gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { Entity *e = params->variables[i]; if (args[i].type == nullptr) { continue; + } else if (is_type_untyped_uninit(args[i].type)) { + args[i] = lb_const_undef(m, e->type); } else if (is_type_untyped_nil(args[i].type)) { args[i] = lb_const_nil(m, e->type); - } else if (is_type_untyped_undef(args[i].type)) { - args[i] = lb_const_undef(m, e->type); } } @@ -3409,10 +3409,10 @@ gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { Entity *e = param_tuple->variables[i]; if (args[i].type == nullptr) { continue; + } else if (is_type_untyped_uninit(args[i].type)) { + args[i] = lb_const_undef(m, e->type); } else if (is_type_untyped_nil(args[i].type)) { args[i] = lb_const_nil(m, e->type); - } else if (is_type_untyped_undef(args[i].type)) { - args[i] = lb_const_undef(m, e->type); } } } diff --git a/src/parser.cpp b/src/parser.cpp index 698ba99ab..bb492fca9 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -115,7 +115,7 @@ gb_internal Ast *clone_ast(Ast *node, AstFile *f) { n->Ident.entity = nullptr; break; case Ast_Implicit: break; - case Ast_Undef: break; + case Ast_Uninit: break; case Ast_BasicLit: break; case Ast_BasicDirective: break; @@ -646,9 +646,9 @@ gb_internal Ast *ast_implicit(AstFile *f, Token token) { result->Implicit = token; return result; } -gb_internal Ast *ast_undef(AstFile *f, Token token) { - Ast *result = alloc_ast_node(f, Ast_Undef); - result->Undef = token; +gb_internal Ast *ast_uninit(AstFile *f, Token token) { + Ast *result = alloc_ast_node(f, Ast_Uninit); + result->Uninit = token; return result; } @@ -2092,8 +2092,8 @@ gb_internal Ast *parse_operand(AstFile *f, bool lhs) { case Token_Ident: return parse_ident(f); - case Token_Undef: - return ast_undef(f, expect_token(f, Token_Undef)); + case Token_Uninit: + return ast_uninit(f, expect_token(f, Token_Uninit)); case Token_context: return ast_implicit(f, expect_token(f, Token_context)); @@ -2292,7 +2292,7 @@ gb_internal Ast *parse_operand(AstFile *f, bool lhs) { skip_possible_newline_for_literal(f); - if (allow_token(f, Token_Undef)) { + if (allow_token(f, Token_Uninit)) { if (where_token.kind != Token_Invalid) { syntax_error(where_token, "'where' clauses are not allowed on procedure literals without a defined body (replaced with ---)"); } diff --git a/src/parser.hpp b/src/parser.hpp index aea3bbf21..d4883f287 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -373,7 +373,7 @@ gb_global char const *union_type_kind_strings[UnionType_COUNT] = { Entity *entity; \ }) \ AST_KIND(Implicit, "implicit", Token) \ - AST_KIND(Undef, "undef", Token) \ + AST_KIND(Uninit, "uninitialized value", Token) \ AST_KIND(BasicLit, "basic literal", struct { \ Token token; \ }) \ diff --git a/src/parser_pos.cpp b/src/parser_pos.cpp index 1274f05a0..52d49e897 100644 --- a/src/parser_pos.cpp +++ b/src/parser_pos.cpp @@ -2,7 +2,7 @@ gb_internal Token ast_token(Ast *node) { switch (node->kind) { case Ast_Ident: return node->Ident.token; case Ast_Implicit: return node->Implicit; - case Ast_Undef: return node->Undef; + case Ast_Uninit: return node->Uninit; case Ast_BasicLit: return node->BasicLit.token; case Ast_BasicDirective: return node->BasicDirective.token; case Ast_ProcGroup: return node->ProcGroup.token; @@ -137,7 +137,7 @@ Token ast_end_token(Ast *node) { return empty_token; case Ast_Ident: return node->Ident.token; case Ast_Implicit: return node->Implicit; - case Ast_Undef: return node->Undef; + case Ast_Uninit: return node->Uninit; case Ast_BasicLit: return node->BasicLit.token; case Ast_BasicDirective: return node->BasicDirective.token; case Ast_ProcGroup: return node->ProcGroup.close; diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 547a864fb..17a396b9f 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -54,7 +54,7 @@ TOKEN_KIND(Token__AssignOpEnd, ""), \ TOKEN_KIND(Token_Increment, "++"), \ TOKEN_KIND(Token_Decrement, "--"), \ TOKEN_KIND(Token_ArrowRight,"->"), \ - TOKEN_KIND(Token_Undef, "---"), \ + TOKEN_KIND(Token_Uninit, "---"), \ \ TOKEN_KIND(Token__ComparisonBegin, ""), \ TOKEN_KIND(Token_CmpEq, "=="), \ @@ -917,7 +917,7 @@ gb_internal void tokenizer_get_token(Tokenizer *t, Token *token, int repeat=0) { token->kind = Token_Decrement; if (t->curr_rune == '-') { advance_to_next_rune(t); - token->kind = Token_Undef; + token->kind = Token_Uninit; } break; case '>': @@ -1078,7 +1078,7 @@ semicolon_check:; case Token_Imag: case Token_Rune: case Token_String: - case Token_Undef: + case Token_Uninit: /*fallthrough*/ case Token_Question: case Token_Pointer: diff --git a/src/types.cpp b/src/types.cpp index 70c4bcdbe..35fbb719b 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -83,7 +83,7 @@ enum BasicKind { Basic_UntypedString, Basic_UntypedRune, Basic_UntypedNil, - Basic_UntypedUndef, + Basic_UntypedUninit, Basic_COUNT, @@ -515,7 +515,7 @@ gb_global Type basic_types[] = { {Type_Basic, {Basic_UntypedString, BasicFlag_String | BasicFlag_Untyped, 0, STR_LIT("untyped string")}}, {Type_Basic, {Basic_UntypedRune, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped rune")}}, {Type_Basic, {Basic_UntypedNil, BasicFlag_Untyped, 0, STR_LIT("untyped nil")}}, - {Type_Basic, {Basic_UntypedUndef, BasicFlag_Untyped, 0, STR_LIT("untyped undefined")}}, + {Type_Basic, {Basic_UntypedUninit, BasicFlag_Untyped, 0, STR_LIT("untyped uninitialized")}}, }; // gb_global Type basic_type_aliases[] = { @@ -589,7 +589,7 @@ gb_global Type *t_untyped_quaternion = &basic_types[Basic_UntypedQuaternion]; gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString]; gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune]; gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil]; -gb_global Type *t_untyped_undef = &basic_types[Basic_UntypedUndef]; +gb_global Type *t_untyped_uninit = &basic_types[Basic_UntypedUninit]; @@ -1866,14 +1866,15 @@ gb_internal bool is_type_typeid(Type *t) { } gb_internal bool is_type_untyped_nil(Type *t) { t = base_type(t); - return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedNil); + // NOTE(bill): checking for `nil` or `---` at once is just to improve the error handling + return (t->kind == Type_Basic && (t->Basic.kind == Basic_UntypedNil || t->Basic.kind == Basic_UntypedUninit)); } -gb_internal bool is_type_untyped_undef(Type *t) { +gb_internal bool is_type_untyped_uninit(Type *t) { t = base_type(t); - return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedUndef); + // NOTE(bill): checking for `nil` or `---` at once is just to improve the error handling + return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedUninit); } - gb_internal bool is_type_empty_union(Type *t) { t = base_type(t); return t->kind == Type_Union && t->Union.variants.count == 0; @@ -2206,10 +2207,6 @@ gb_internal bool is_type_polymorphic(Type *t, bool or_specialized=false) { } -gb_internal gb_inline bool type_has_undef(Type *t) { - return true; -} - gb_internal bool type_has_nil(Type *t) { t = base_type(t); switch (t->kind) { -- cgit v1.2.3 From d2f62730bc26fd18355de3c86d2d825a221aa288 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 29 May 2023 14:55:27 +0100 Subject: Fix #2560 --- src/parser.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index bb492fca9..1b48dce87 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -3744,8 +3744,18 @@ gb_internal bool allow_field_separator(AstFile *f) { if (allow_token(f, Token_Comma)) { return true; } - if (ALLOW_NEWLINE && token.kind == Token_Semicolon) { - if (!token_is_newline(token)) { + if (token.kind == Token_Semicolon) { + bool ok = false; + if (ALLOW_NEWLINE && token_is_newline(token)) { + TokenKind next = peek_token(f).kind; + switch (next) { + case Token_CloseBrace: + case Token_CloseParen: + ok = true; + break; + } + } + if (!ok) { String p = token_to_string(token); syntax_error(token_end_of_line(f, f->prev_token), "Expected a comma, got a %.*s", LIT(p)); } -- cgit v1.2.3 From 97490c6445cb3ba85f470e64e6ed6d24394c421a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 29 May 2023 23:17:06 +0100 Subject: Basic support for `#reverse for in` on normal arrays --- src/check_stmt.cpp | 21 ++++++++++++++++ src/llvm_backend_stmt.cpp | 64 ++++++++++++++++++++++++++++++++++------------- src/parser.cpp | 11 ++++++++ src/parser.hpp | 1 + 4 files changed, 79 insertions(+), 18 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 6c69ad59f..c64de40c7 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1461,6 +1461,7 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) bool is_map = false; bool use_by_reference_for_value = false; bool is_soa = false; + bool is_reverse = rs->reverse; Ast *expr = unparen_expr(rs->expr); @@ -1476,6 +1477,10 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) } array_add(&vals, x.type); array_add(&vals, t_int); + + if (is_reverse) { + error(node, "#reverse for is not yet supported with ranges"); + } } else { Operand operand = {Addressing_Invalid}; check_expr_base(ctx, &operand, expr, nullptr); @@ -1488,6 +1493,9 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) gb_string_free(t); goto skip_expr_range_stmt; } else { + if (is_reverse) { + error(node, "#reverse for is not supported for enum types"); + } array_add(&vals, operand.type); array_add(&vals, t_int); add_type_info_type(ctx, operand.type); @@ -1503,6 +1511,9 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) array_add(&vals, t_int); add_package_dependency(ctx, "runtime", "string_decode_rune"); } + if (is_reverse) { + error(node, "#reverse for is not supported for string types"); + } break; case Type_EnumeratedArray: @@ -1534,6 +1545,9 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) is_map = true; array_add(&vals, t->Map.key); array_add(&vals, t->Map.value); + if (is_reverse) { + error(node, "#reverse for is not supported for map types"); + } break; case Type_Tuple: @@ -1570,6 +1584,9 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) break; } + if (is_reverse) { + error(node, "#reverse for is not supported for multiple return valued parameters"); + } } break; @@ -1579,6 +1596,10 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) if (is_ptr) use_by_reference_for_value = true; array_add(&vals, t->Struct.soa_elem); array_add(&vals, t_int); + + if (is_reverse) { + error(node, "#reverse for is not yet supported for #soa types"); + } } break; } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 125913ac5..60468c7f0 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -249,7 +249,8 @@ gb_internal void lb_build_when_stmt(lbProcedure *p, AstWhenStmt *ws) { gb_internal void lb_build_range_indexed(lbProcedure *p, lbValue expr, Type *val_type, lbValue count_ptr, - lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_) { + lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_, + bool is_reverse) { lbModule *m = p->module; lbValue count = {}; @@ -267,23 +268,50 @@ gb_internal void lb_build_range_indexed(lbProcedure *p, lbValue expr, Type *val_ lbBlock *body = nullptr; - lbAddr index = lb_add_local_generated(p, t_int, false); - lb_addr_store(p, index, lb_const_int(m, t_int, cast(u64)-1)); + lbAddr index = {}; + lbValue incr = {}; + lbValue cond = {}; - loop = lb_create_block(p, "for.index.loop"); - lb_emit_jump(p, loop); - lb_start_block(p, loop); + index = lb_add_local_generated(p, t_int, false); - lbValue incr = lb_emit_arith(p, Token_Add, lb_addr_load(p, index), lb_const_int(m, t_int, 1), t_int); - lb_addr_store(p, index, incr); + if (!is_reverse) { + lb_addr_store(p, index, lb_const_int(m, t_int, cast(u64)-1)); - body = lb_create_block(p, "for.index.body"); - done = lb_create_block(p, "for.index.done"); - if (count.value == nullptr) { - GB_ASSERT(count_ptr.value != nullptr); - count = lb_emit_load(p, count_ptr); + loop = lb_create_block(p, "for.index.loop"); + lb_emit_jump(p, loop); + lb_start_block(p, loop); + + incr = lb_emit_arith(p, Token_Add, lb_addr_load(p, index), lb_const_int(m, t_int, 1), t_int); + lb_addr_store(p, index, incr); + + body = lb_create_block(p, "for.index.body"); + done = lb_create_block(p, "for.index.done"); + if (count.value == nullptr) { + GB_ASSERT(count_ptr.value != nullptr); + count = lb_emit_load(p, count_ptr); + } + cond = lb_emit_comp(p, Token_Lt, incr, count); + } else { + // NOTE(bill): REVERSED LOGIC + if (count.value == nullptr) { + GB_ASSERT(count_ptr.value != nullptr); + count = lb_emit_load(p, count_ptr); + } + count = lb_emit_conv(p, count, t_int); + lb_addr_store(p, index, count); + + loop = lb_create_block(p, "for.index.loop"); + lb_emit_jump(p, loop); + lb_start_block(p, loop); + + incr = lb_emit_arith(p, Token_Sub, lb_addr_load(p, index), lb_const_int(m, t_int, 1), t_int); + lb_addr_store(p, index, incr); + + body = lb_create_block(p, "for.index.body"); + done = lb_create_block(p, "for.index.done"); + cond = lb_emit_comp(p, Token_GtEq, incr, lb_const_int(m, t_int, 0)); } - lbValue cond = lb_emit_comp(p, Token_Lt, incr, count); + lb_emit_if(p, cond, body, done); lb_start_block(p, body); @@ -820,7 +848,7 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc } lbAddr count_ptr = lb_add_local_generated(p, t_int, false); lb_addr_store(p, count_ptr, lb_const_int(p->module, t_int, et->Array.count)); - lb_build_range_indexed(p, array, val0_type, count_ptr.addr, &val, &key, &loop, &done); + lb_build_range_indexed(p, array, val0_type, count_ptr.addr, &val, &key, &loop, &done, rs->reverse); break; } case Type_EnumeratedArray: { @@ -830,7 +858,7 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc } lbAddr count_ptr = lb_add_local_generated(p, t_int, false); lb_addr_store(p, count_ptr, lb_const_int(p->module, t_int, et->EnumeratedArray.count)); - lb_build_range_indexed(p, array, val0_type, count_ptr.addr, &val, &key, &loop, &done); + lb_build_range_indexed(p, array, val0_type, count_ptr.addr, &val, &key, &loop, &done, rs->reverse); break; } case Type_DynamicArray: { @@ -840,7 +868,7 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc array = lb_emit_load(p, array); } count_ptr = lb_emit_struct_ep(p, array, 1); - lb_build_range_indexed(p, array, val0_type, count_ptr, &val, &key, &loop, &done); + lb_build_range_indexed(p, array, val0_type, count_ptr, &val, &key, &loop, &done, rs->reverse); break; } case Type_Slice: { @@ -853,7 +881,7 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc count_ptr = lb_add_local_generated(p, t_int, false).addr; lb_emit_store(p, count_ptr, lb_slice_len(p, slice)); } - lb_build_range_indexed(p, slice, val0_type, count_ptr, &val, &key, &loop, &done); + lb_build_range_indexed(p, slice, val0_type, count_ptr, &val, &key, &loop, &done, rs->reverse); break; } case Type_Basic: { diff --git a/src/parser.cpp b/src/parser.cpp index 1b48dce87..afdb1078d 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4759,6 +4759,17 @@ gb_internal Ast *parse_stmt(AstFile *f) { return stmt; } else if (tag == "unroll") { return parse_unrolled_for_loop(f, name); + } else if (tag == "reverse") { + Ast *for_stmt = parse_for_stmt(f); + if (for_stmt->kind == Ast_RangeStmt) { + if (for_stmt->RangeStmt.reverse) { + syntax_error(token, "#reverse already applied to a 'for in' statement"); + } + for_stmt->RangeStmt.reverse = true; + } else { + syntax_error(token, "#reverse can only be applied to a 'for in' statement"); + } + return for_stmt; } else if (tag == "include") { syntax_error(token, "#include is not a valid import declaration kind. Did you mean 'import'?"); s = ast_bad_stmt(f, token, f->curr_token); diff --git a/src/parser.hpp b/src/parser.hpp index d4883f287..6ba4ef6d6 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -529,6 +529,7 @@ AST_KIND(_ComplexStmtBegin, "", bool) \ Token in_token; \ Ast *expr; \ Ast *body; \ + bool reverse; \ }) \ AST_KIND(UnrollRangeStmt, "#unroll range statement", struct { \ Scope *scope; \ -- cgit v1.2.3