From 94c1331c071d4816bec44345393dfd07f75c97dc Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 15 Feb 2023 11:31:51 +0000 Subject: Implement `@(fini)` (opposite of `@(init)`) --- src/check_decl.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/check_decl.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index f0059424e..63e6514e0 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -816,9 +816,14 @@ gb_internal void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) { if (ac.test) { e->flags |= EntityFlag_Test; } - if (ac.init) { + if (ac.init && ac.fini) { + error(e->token, "A procedure cannot be both declared as @(init) and @(fini)"); + } else if (ac.init) { e->flags |= EntityFlag_Init; + } else if (ac.fini) { + e->flags |= EntityFlag_Fini; } + if (ac.set_cold) { e->flags |= EntityFlag_Cold; } -- cgit v1.2.3 From 8a2a70a3c2a7ac14687d100325e12b8d3d02256e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 17 Feb 2023 13:00:37 +0000 Subject: Fix overriding procedure information for literals --- src/check_decl.cpp | 17 +++++++++++++++++ src/check_expr.cpp | 4 +++- src/check_type.cpp | 12 ++++++++---- src/checker.cpp | 4 ++-- src/checker.hpp | 4 ++-- src/llvm_backend_const.cpp | 1 - src/llvm_backend_expr.cpp | 2 +- 7 files changed, 33 insertions(+), 11 deletions(-) (limited to 'src/check_decl.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 63e6514e0..dc929cbca 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1480,6 +1480,23 @@ gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de ctx->curr_proc_sig = type; ctx->curr_proc_calling_convention = type->Proc.calling_convention; + if (ctx->proc_name == "sort") { + if (type->Proc.param_count > 0) { + TypeTuple *params = &type->Proc.params->Tuple; + for (Entity *e : params->variables) { + if (e->kind == Entity_Constant) { + Ast *ident = e->identifier.load(); + if (ident) { + add_entity(ctx, e->scope, ident, e); + ident->tav.mode = Addressing_Constant; + ident->tav.value = e->Constant.value; + ident->tav.type = e->type; + } + } + } + } + } + if (ctx->pkg->name != "runtime") { switch (type->Proc.calling_convention) { case ProcCC_None: diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 58372f7a3..93db559f8 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5418,7 +5418,9 @@ gb_internal CALL_ARGUMENT_CHECKER(check_call_arguments_internal) { data->score = score; data->result_type = final_proc_type->Proc.results; data->gen_entity = gen_entity; - add_type_and_value(c, ce->proc, Addressing_Value, final_proc_type, {}); + if (!are_types_identical(final_proc_type, ce->proc->tav.type)) { + add_type_and_value(c, ce->proc, Addressing_Value, final_proc_type, {}); + } } return err; diff --git a/src/check_type.cpp b/src/check_type.cpp index ec661134b..24ca8628e 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -1666,17 +1666,21 @@ gb_internal Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_para if (is_poly_name) { bool valid = false; if (is_type_proc(op.type)) { - Entity *proc_entity = entity_from_expr(op.expr); - valid = (proc_entity != nullptr) && (op.value.kind == ExactValue_Procedure); - if (valid) { + Ast *expr = unparen_expr(op.expr); + Entity *proc_entity = entity_from_expr(expr); + if (proc_entity) { poly_const = exact_value_procedure(proc_entity->identifier.load() ? proc_entity->identifier.load() : op.expr); + valid = true; + } else if (expr->kind == Ast_ProcLit) { + poly_const = exact_value_procedure(expr); + valid = true; } } if (!valid) { if (op.mode == Addressing_Constant) { poly_const = op.value; } else { - error(op.expr, "Expected a constant value for this polymorphic name parameter"); + error(op.expr, "Expected a constant value for this polymorphic name parameter, got %s", expr_to_string(op.expr)); success = false; } } diff --git a/src/checker.cpp b/src/checker.cpp index 5163fe675..94bbb32e6 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1416,7 +1416,7 @@ gb_internal isize type_info_index(CheckerInfo *info, Type *type, bool error_on_f } -gb_internal void add_untyped(CheckerContext *c, Ast *expr, AddressingMode mode, Type *type, ExactValue value) { +gb_internal void add_untyped(CheckerContext *c, Ast *expr, AddressingMode mode, Type *type, ExactValue const &value) { if (expr == nullptr) { return; } @@ -1433,7 +1433,7 @@ gb_internal void add_untyped(CheckerContext *c, Ast *expr, AddressingMode mode, check_set_expr_info(c, expr, mode, type, value); } -gb_internal void add_type_and_value(CheckerContext *ctx, Ast *expr, AddressingMode mode, Type *type, ExactValue value) { +gb_internal void add_type_and_value(CheckerContext *ctx, Ast *expr, AddressingMode mode, Type *type, ExactValue const &value) { if (expr == nullptr) { return; } diff --git a/src/checker.hpp b/src/checker.hpp index 4c3b8725a..b82612813 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -485,9 +485,9 @@ gb_internal void scope_lookup_parent (Scope *s, String const &name, Scope **s gb_internal Entity *scope_insert (Scope *s, Entity *entity); -gb_internal void add_type_and_value (CheckerContext *c, Ast *expression, AddressingMode mode, Type *type, ExactValue value); +gb_internal void add_type_and_value (CheckerContext *c, Ast *expression, AddressingMode mode, Type *type, ExactValue const &value); gb_internal ExprInfo *check_get_expr_info (CheckerContext *c, Ast *expr); -gb_internal void add_untyped (CheckerContext *c, Ast *expression, AddressingMode mode, Type *basic_type, ExactValue value); +gb_internal void add_untyped (CheckerContext *c, Ast *expression, AddressingMode mode, Type *basic_type, ExactValue const &value); gb_internal void add_entity_use (CheckerContext *c, Ast *identifier, Entity *entity); gb_internal void add_implicit_entity (CheckerContext *c, Ast *node, Entity *e); gb_internal void add_entity_and_decl_info(CheckerContext *c, Ast *identifier, Entity *e, DeclInfo *d, bool is_exported=true); diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index ee564bbf1..fc1598024 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -411,7 +411,6 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bo Ast *expr = unparen_expr(value.value_procedure); if (expr->kind == Ast_ProcLit) { res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr); - } else { Entity *e = entity_from_expr(expr); res = lb_find_procedure_value_from_entity(m, e); diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 7cf8d56db..480831a68 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -3138,7 +3138,7 @@ gb_internal lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) { Entity *e = entity_from_expr(expr); e = strip_entity_wrapping(e); - GB_ASSERT_MSG(e != nullptr, "%s", expr_to_string(expr)); + GB_ASSERT_MSG(e != nullptr, "%s in %.*s %p", expr_to_string(expr), LIT(p->name), expr); if (e->kind == Entity_Builtin) { Token token = ast_token(expr); GB_PANIC("TODO(bill): lb_build_expr Entity_Builtin '%.*s'\n" -- cgit v1.2.3 From c40b6c7c2fd8e193951087197c60a1359ad0002b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 17 Feb 2023 13:02:41 +0000 Subject: Add constant data to the identifier directly --- src/check_decl.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'src/check_decl.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index dc929cbca..1adb1e2ed 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1480,23 +1480,6 @@ gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de ctx->curr_proc_sig = type; ctx->curr_proc_calling_convention = type->Proc.calling_convention; - if (ctx->proc_name == "sort") { - if (type->Proc.param_count > 0) { - TypeTuple *params = &type->Proc.params->Tuple; - for (Entity *e : params->variables) { - if (e->kind == Entity_Constant) { - Ast *ident = e->identifier.load(); - if (ident) { - add_entity(ctx, e->scope, ident, e); - ident->tav.mode = Addressing_Constant; - ident->tav.value = e->Constant.value; - ident->tav.type = e->type; - } - } - } - } - } - if (ctx->pkg->name != "runtime") { switch (type->Proc.calling_convention) { case ProcCC_None: @@ -1549,6 +1532,19 @@ gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de break; } } + + // Add constant data to the identifier directly + for (Entity *e : params->variables) { + if (e->kind == Entity_Constant) { + Ast *ident = e->identifier.load(); + if (ident) { + add_entity(ctx, e->scope, ident, e); + ident->tav.mode = Addressing_Constant; + ident->tav.value = e->Constant.value; + ident->tav.type = e->type; + } + } + } } } -- cgit v1.2.3 From 20eacc4a8493f0d0088ceebbcc1490207c48b5ed Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 19 Feb 2023 12:10:28 +0000 Subject: Fix issue that conflicts with constant parapoly procedures and `deferred_*` procedures --- src/check_decl.cpp | 13 ------------- src/check_expr.cpp | 5 ++--- 2 files changed, 2 insertions(+), 16 deletions(-) (limited to 'src/check_decl.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 1adb1e2ed..63e6514e0 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1532,19 +1532,6 @@ gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de break; } } - - // Add constant data to the identifier directly - for (Entity *e : params->variables) { - if (e->kind == Entity_Constant) { - Ast *ident = e->identifier.load(); - if (ident) { - add_entity(ctx, e->scope, ident, e); - ident->tav.mode = Addressing_Constant; - ident->tav.value = e->Constant.value; - ident->tav.type = e->type; - } - } - } } } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 93db559f8..356f0aaa3 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5418,9 +5418,8 @@ gb_internal CALL_ARGUMENT_CHECKER(check_call_arguments_internal) { data->score = score; data->result_type = final_proc_type->Proc.results; data->gen_entity = gen_entity; - if (!are_types_identical(final_proc_type, ce->proc->tav.type)) { - add_type_and_value(c, ce->proc, Addressing_Value, final_proc_type, {}); - } + + add_type_and_value(c, ce->proc, Addressing_Value, final_proc_type, {}); } return err; -- cgit v1.2.3 From f5d507a9b9d20941bd86ba8559f710f21f0c8ccd Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 22 Feb 2023 11:30:08 +0000 Subject: Improve errors about conversions of constant integers --- src/check_decl.cpp | 2 +- src/check_expr.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/error.cpp | 33 ++++++++++++++++++++++------- 3 files changed, 82 insertions(+), 15 deletions(-) (limited to 'src/check_decl.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 63e6514e0..7978aa0ef 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -578,7 +578,7 @@ gb_internal void check_const_decl(CheckerContext *ctx, Entity *e, Ast *type_expr if (operand.mode == Addressing_Invalid || base_type(operand.type) == t_invalid) { gbString str = expr_to_string(init); - error(e->token, "Invalid declaration type '%s'", str); + error(init, "Invalid declaration value '%s'", str); gb_string_free(str); } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index c0920bde8..d7e7b29a4 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -2019,6 +2019,47 @@ gb_internal bool check_representable_as_constant(CheckerContext *c, ExactValue i } +gb_internal bool check_integer_exceed_suggestion(CheckerContext *c, Operand *o, Type *type) { + if (is_type_integer(type) && o->value.kind == ExactValue_Integer) { + gbString b = type_to_string(type); + + i64 sz = type_size_of(type); + BigInt *bi = &o->value.value_integer; + if (is_type_unsigned(type)) { + if (big_int_is_neg(bi)) { + error_line("\tA negative value cannot be represented by the unsigned integer type '%s'\n", b); + } else { + BigInt one = big_int_make_u64(1); + BigInt max_size = big_int_make_u64(1); + BigInt bits = big_int_make_i64(8*sz); + big_int_shl_eq(&max_size, &bits); + big_int_sub_eq(&max_size, &one); + String max_size_str = big_int_to_string(temporary_allocator(), &max_size); + error_line("\tThe maximum value that can be represented by '%s' is '%.*s'\n", b, LIT(max_size_str)); + } + } else { + BigInt zero = big_int_make_u64(0); + BigInt one = big_int_make_u64(1); + BigInt max_size = big_int_make_u64(1); + BigInt bits = big_int_make_i64(8*sz - 1); + big_int_shl_eq(&max_size, &bits); + if (big_int_is_neg(bi)) { + big_int_neg(&max_size, &max_size); + String max_size_str = big_int_to_string(temporary_allocator(), &max_size); + error_line("\tThe minimum value that can be represented by '%s' is '%.*s'\n", b, LIT(max_size_str)); + } else { + big_int_sub_eq(&max_size, &one); + String max_size_str = big_int_to_string(temporary_allocator(), &max_size); + error_line("\tThe maximum value that can be represented by '%s' is '%.*s'\n", b, LIT(max_size_str)); + } + } + + gb_string_free(b); + + return true; + } + return false; +} gb_internal void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type) { gbString a = expr_to_string(o->expr); gbString b = type_to_string(type); @@ -2050,6 +2091,8 @@ gb_internal void check_assignment_error_suggestion(CheckerContext *c, Operand *o error_line("\t whereas slices in general are assumed to be mutable.\n"); } else if (is_type_u8_slice(src) && are_types_identical(dst, t_string) && o->mode != Addressing_Constant) { error_line("\tSuggestion: the expression may be casted to %s\n", b); + } else if (check_integer_exceed_suggestion(c, o, type)) { + return; } } @@ -2089,8 +2132,8 @@ gb_internal void check_cast_error_suggestion(CheckerContext *c, Operand *o, Type } } else if (are_types_identical(src, t_string) && is_type_u8_slice(dst)) { error_line("\tSuggestion: a string may be transmuted to %s\n", b); - } else if (is_type_u8_slice(src) && are_types_identical(dst, t_string) && o->mode != Addressing_Constant) { - error_line("\tSuggestion: the expression may be casted to %s\n", b); + } else if (check_integer_exceed_suggestion(c, o, type)) { + return; } } @@ -2124,10 +2167,11 @@ gb_internal bool check_is_expressible(CheckerContext *ctx, Operand *o, Type *typ error(o->expr, "'%s' truncated to '%s', got %s", a, b, s); } else { if (are_types_identical(o->type, type)) { - error(o->expr, "Numeric value '%s' cannot be represented by '%s', got %s", a, c, s); + error(o->expr, "Numeric value '%s' from '%s' cannot be represented by '%s'", s, a, b); } else { - error(o->expr, "Cannot convert numeric value '%s' to '%s' from '%s', got %s", a, b, c, s); + error(o->expr, "Cannot convert numeric value '%s' from '%s' to '%s' from '%s'", s, a, b, c); } + check_assignment_error_suggestion(ctx, o, type); } } else { @@ -2934,15 +2978,21 @@ gb_internal void check_cast(CheckerContext *c, Operand *x, Type *type) { if (is_const_expr) { gbString val_str = exact_value_to_string(x->value); if (is_type_float(x->type) && is_type_integer(type)) { - error_line("\t%s cannot be expressed without truncation/rounding as the type '%s'\n", val_str, to_type); + error_line("\t%s cannot be represented without truncation/rounding as the type '%s'\n", val_str, to_type); // NOTE(bill): keep the mode and modify the type to minimize errors further on x->mode = Addressing_Constant; x->type = type; } else { - error_line("\t%s cannot be expressed as the type '%s'\n", val_str, to_type); + error_line("\t'%s' cannot be represented as the type '%s'\n", val_str, to_type); + if (is_type_numeric(type)) { + // NOTE(bill): keep the mode and modify the type to minimize errors further on + x->mode = Addressing_Constant; + x->type = type; + } } gb_string_free(val_str); + } check_cast_error_suggestion(c, x, type); diff --git a/src/error.cpp b/src/error.cpp index a0bb4ad5b..33157948f 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -97,15 +97,37 @@ gb_internal AstFile *thread_safe_get_ast_file_from_id(i32 index) { +// NOTE: defined in build_settings.cpp +gb_internal bool global_warnings_as_errors(void); +gb_internal bool global_ignore_warnings(void); +gb_internal bool show_error_line(void); +gb_internal gbString get_file_line_as_string(TokenPos const &pos, i32 *offset); + +gb_internal void warning(Token const &token, char const *fmt, ...); +gb_internal void error(Token const &token, char const *fmt, ...); +gb_internal void error(TokenPos pos, char const *fmt, ...); +gb_internal void error_line(char const *fmt, ...); +gb_internal void syntax_error(Token const &token, char const *fmt, ...); +gb_internal void syntax_error(TokenPos pos, char const *fmt, ...); +gb_internal void syntax_warning(Token const &token, char const *fmt, ...); +gb_internal void compiler_error(char const *fmt, ...); + gb_internal void begin_error_block(void) { mutex_lock(&global_error_collector.block_mutex); global_error_collector.in_block.store(true); } gb_internal void end_error_block(void) { - if (global_error_collector.error_buffer.count > 0) { - isize n = global_error_collector.error_buffer.count; - u8 *text = gb_alloc_array(permanent_allocator(), u8, n+1); + isize n = global_error_collector.error_buffer.count; + if (n > 0) { + u8 *text = global_error_collector.error_buffer.data; + if (show_error_line() && n >= 2 && !(text[n-2] == '\n' && text[n-1] == '\n')) { + // add an extra new line as padding when the error line is being shown + error_line("\n"); + } + + n = global_error_collector.error_buffer.count; + text = gb_alloc_array(permanent_allocator(), u8, n+1); gb_memmove(text, global_error_collector.error_buffer.data, n); text[n] = 0; String s = {text, n}; @@ -152,11 +174,6 @@ gb_internal ERROR_OUT_PROC(default_error_out_va) { gb_global ErrorOutProc *error_out_va = default_error_out_va; -// NOTE: defined in build_settings.cpp -gb_internal bool global_warnings_as_errors(void); -gb_internal bool global_ignore_warnings(void); -gb_internal bool show_error_line(void); -gb_internal gbString get_file_line_as_string(TokenPos const &pos, i32 *offset); gb_internal void error_out(char const *fmt, ...) { va_list va; -- cgit v1.2.3