diff options
| author | 0xrsp <95966883+0xrsp@users.noreply.github.com> | 2025-05-15 01:00:40 +0930 |
|---|---|---|
| committer | 0xrsp <95966883+0xrsp@users.noreply.github.com> | 2025-05-15 01:00:40 +0930 |
| commit | ea806f1d5e1d450e9c0b98a7a9d1f92afb7957ef (patch) | |
| tree | 75368bca8b6f5a17fe2b25b768af4efed797a860 /src | |
| parent | 25c935c305f373a1c539a235734d25759143a1c2 (diff) | |
new compiler intrinsics type_integer_to_unsigned,type_integer_to_signed
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_builtin.cpp | 84 | ||||
| -rw-r--r-- | src/checker_builtin_procs.hpp | 6 |
2 files changed, 90 insertions, 0 deletions
diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 8efaf6b26..147d4ba3a 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -5877,6 +5877,90 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } operand->mode = Addressing_Type; break; + case BuiltinProc_type_integer_to_unsigned: + if (operand->mode != Addressing_Type) { + error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name)); + return false; + } + + if (is_type_polymorphic(operand->type)) { + gbString t = type_to_string(operand->type); + error(operand->expr, "Expected a non-polymorphic type for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + { + Type *bt = base_type(operand->type); + + if (bt->kind != Type_Basic || + (bt->Basic.flags & BasicFlag_Unsigned) != 0 || + (bt->Basic.flags & BasicFlag_Integer) == 0) { + gbString t = type_to_string(operand->type); + error(operand->expr, "Expected a signed integer type for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + if ((bt->Basic.flags & BasicFlag_Untyped) != 0) { + gbString t = type_to_string(operand->type); + // NOTE: What should this error message be? + error(operand->expr, "Expected a typed integer for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + Type *u_type = &basic_types[bt->Basic.kind + 1]; + + operand->type = u_type; + } + break; + case BuiltinProc_type_integer_to_signed: + if (operand->mode != Addressing_Type) { + error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name)); + return false; + } + + if (is_type_polymorphic(operand->type)) { + gbString t = type_to_string(operand->type); + error(operand->expr, "Expected a non-polymorphic type for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + { + Type *bt = base_type(operand->type); + + if (bt->kind != Type_Basic || + (bt->Basic.flags & BasicFlag_Unsigned) == 0 || + (bt->Basic.flags & BasicFlag_Integer) == 0) { + gbString t = type_to_string(operand->type); + error(operand->expr, "Expected an unsigned integer type for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + if ((bt->Basic.flags & BasicFlag_Untyped) != 0) { + gbString t = type_to_string(operand->type); + // NOTE: What should this error message be? + error(operand->expr, "Expected a typed integer for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + if (bt->Basic.kind == Basic_uintptr) { + gbString t = type_to_string(operand->type); + // TODO: Robust error message for uintptr does not have a signed integer representation + error(operand->expr, "Expected a signed integer type for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + Type *u_type = &basic_types[bt->Basic.kind - 1]; + + operand->type = u_type; + } + break; case BuiltinProc_type_merge: { operand->mode = Addressing_Type; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index c4e487560..f3b55daa4 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -235,6 +235,9 @@ BuiltinProc__type_begin, BuiltinProc_type_convert_variants_to_pointers, BuiltinProc_type_merge, + BuiltinProc_type_integer_to_unsigned, + BuiltinProc_type_integer_to_signed, + BuiltinProc__type_simple_boolean_begin, BuiltinProc_type_is_boolean, BuiltinProc_type_is_integer, @@ -585,6 +588,9 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("type_convert_variants_to_pointers"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_merge"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_integer_to_unsigned"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_integer_to_signed"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT(""), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_boolean"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_integer"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, |