From 4d5821408df2765e2d10e2d49856dbcab8837fd1 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 20 Sep 2022 22:18:00 +0200 Subject: [PATCH] Add @castable and @convertible builtin macros, removes the need $castable and $convertible --- lib/std/core/builtin.c3 | 9 +++++++ lib/std/core/string.c3 | 4 ++-- src/compiler/ast.c | 1 - src/compiler/copying.c | 4 ---- src/compiler/enums.h | 3 --- src/compiler/llvm_codegen_expr.c | 1 - src/compiler/parse_expr.c | 17 -------------- src/compiler/parse_stmt.c | 2 -- src/compiler/sema_casts.c | 2 -- src/compiler/sema_expr.c | 40 -------------------------------- src/compiler/tokens.c | 4 ---- src/version.h | 2 +- 12 files changed, 12 insertions(+), 77 deletions(-) diff --git a/lib/std/core/builtin.c3 b/lib/std/core/builtin.c3 index 826aff83a..7e443268d 100644 --- a/lib/std/core/builtin.c3 +++ b/lib/std/core/builtin.c3 @@ -138,3 +138,12 @@ macro prefetch(void* ptr, PrefetchLocality $locality = VERY_NEAR, bool $write = $$prefetch(ptr, $write ? 1 : 0, $locality.ordinal); } +macro bool @castable(#expr, $To) @builtin +{ + return $checks(($To)#expr); +} + +macro bool @convertible(#expr, $To) @builtin +{ + return $checks($To x = #expr); +} diff --git a/lib/std/core/string.c3 b/lib/std/core/string.c3 index f6c4295da..3b51cc6bc 100644 --- a/lib/std/core/string.c3 +++ b/lib/std/core/string.c3 @@ -267,9 +267,9 @@ macro void String.append(String* str, value) $case Char32: str.append_char32(value); $default: - $if ($convertible($Type, Char32)): + $if (@convertible($Type, Char32)): str.append_char32(value); - $elif ($convertible($Type, char[])): + $elif (@convertible($Type, char[])): str.append_chars(value); $else: $assert("Unsupported type for appending"); diff --git a/src/compiler/ast.c b/src/compiler/ast.c index 12820d699..cac025f5d 100644 --- a/src/compiler/ast.c +++ b/src/compiler/ast.c @@ -282,7 +282,6 @@ bool expr_is_pure(Expr *expr) case EXPR_NOP: case EXPR_STRINGIFY: case EXPR_RETVAL: - case EXPR_CT_CONV: case EXPR_TYPEINFO: case EXPR_CT_EVAL: case EXPR_CT_IDENT: diff --git a/src/compiler/copying.c b/src/compiler/copying.c index a52d4a520..32e09b5d2 100644 --- a/src/compiler/copying.c +++ b/src/compiler/copying.c @@ -208,10 +208,6 @@ Expr *copy_expr(CopyStruct *c, Expr *source_expr) case EXPR_BUILTIN_ACCESS: MACRO_COPY_EXPRID(expr->builtin_access_expr.inner); return expr; - case EXPR_CT_CONV: - MACRO_COPY_TYPEID(expr->ct_call_expr.type_from); - MACRO_COPY_TYPEID(expr->ct_call_expr.type_to); - return expr; case EXPR_DECL: MACRO_COPY_DECL(expr->decl_expr); return expr; diff --git a/src/compiler/enums.h b/src/compiler/enums.h index a1342ba7f..ab19d5069 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -213,7 +213,6 @@ typedef enum EXPR_CONST, EXPR_CT_CHECKS, EXPR_CT_CALL, - EXPR_CT_CONV, EXPR_CT_IDENT, EXPR_CT_EVAL, EXPR_CT_ARG, @@ -569,8 +568,6 @@ typedef enum TOKEN_CT_SWITCH, // $switch TOKEN_CT_TYPEFROM, // $typefrom TOKEN_CT_TYPEOF, // $typeof - TOKEN_CT_CONVERTIBLE, // $convertible - TOKEN_CT_CASTABLE, // $castable TOKEN_CT_VACOUNT, // $vacount TOKEN_CT_VATYPE, // $vatype TOKEN_CT_VACONST, // $vaconst, diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index c676ae1b3..578d4aa04 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -5541,7 +5541,6 @@ void llvm_emit_expr(GenContext *c, BEValue *value, Expr *expr) { case NON_RUNTIME_EXPR: case EXPR_COND: - case EXPR_CT_CONV: case EXPR_CT_ARG: case EXPR_ASM: case EXPR_VASPLAT: diff --git a/src/compiler/parse_expr.c b/src/compiler/parse_expr.c index e62a9ef37..8e4ff8d4e 100644 --- a/src/compiler/parse_expr.c +++ b/src/compiler/parse_expr.c @@ -959,21 +959,6 @@ static Expr *parse_ct_arg(ParseContext *c, Expr *left) return expr; } -static Expr *parse_ct_conv(ParseContext *c, Expr *left) -{ - assert(!left && "Unexpected left hand side"); - Expr *expr = EXPR_NEW_TOKEN(EXPR_CT_CONV); - expr->ct_call_expr.token_type = c->tok; - advance(c); - CONSUME_OR_RET(TOKEN_LPAREN, poisoned_expr); - ASSIGN_TYPEID_OR_RET(expr->ct_call_expr.type_from, parse_type(c), poisoned_expr); - TRY_CONSUME_AFTER(TOKEN_COMMA, "Expected ',' here.", poisoned_expr); - ASSIGN_TYPEID_OR_RET(expr->ct_call_expr.type_to, parse_type(c), poisoned_expr); - TRY_CONSUME_AFTER(TOKEN_RPAREN, "Expected ')' here.", poisoned_expr); - RANGE_EXTEND_PREV(expr); - return expr; -} - static Expr *parse_identifier(ParseContext *c, Expr *left) { assert(!left && "Unexpected left hand side"); @@ -1784,8 +1769,6 @@ ParseRule rules[TOKEN_EOF + 1] = { [TOKEN_CT_TYPEOF] = { parse_type_expr, NULL, PREC_NONE }, [TOKEN_CT_STRINGIFY] = { parse_ct_stringify, NULL, PREC_NONE }, [TOKEN_CT_EVALTYPE] = { parse_type_expr, NULL, PREC_NONE }, - [TOKEN_CT_CONVERTIBLE] = { parse_ct_conv, NULL, PREC_NONE }, - [TOKEN_CT_CASTABLE] = { parse_ct_conv, NULL, PREC_NONE }, [TOKEN_LBRACE] = { parse_initializer_list, NULL, PREC_NONE }, [TOKEN_CT_VACOUNT] = { parse_ct_arg, NULL, PREC_NONE }, [TOKEN_CT_VAARG] = { parse_ct_arg, NULL, PREC_NONE }, diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index 7cd36539b..bf95aa776 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -1289,8 +1289,6 @@ Ast *parse_stmt(ParseContext *c) case TOKEN_RVEC: case TOKEN_CT_ENDFOR: case TOKEN_CT_ENDFOREACH: - case TOKEN_CT_CASTABLE: - case TOKEN_CT_CONVERTIBLE: case TOKEN_CT_VASPLAT: case TOKEN_IMPLIES: SEMA_ERROR_HERE("Unexpected '%s' found when expecting a statement.", diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index 6b8b37c50..146d9d0d9 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -841,7 +841,6 @@ Expr *recursive_may_narrow_float(Expr *expr, Type *type) case EXPR_STRINGIFY: case EXPR_CT_EVAL: case EXPR_VARIANT: - case EXPR_CT_CONV: case EXPR_POINTER_OFFSET: case EXPR_CT_ARG: case EXPR_ASM: @@ -1014,7 +1013,6 @@ Expr *recursive_may_narrow_int(Expr *expr, Type *type) case EXPR_STRINGIFY: case EXPR_CT_EVAL: case EXPR_VARIANT: - case EXPR_CT_CONV: case EXPR_POINTER_OFFSET: case EXPR_CT_ARG: case EXPR_ASM: diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 98677a4dc..2e271dec5 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -367,8 +367,6 @@ bool expr_is_constant_eval(Expr *expr, ConstantEvalKind eval_kind) { case EXPR_POINTER_OFFSET: return exprid_is_constant_eval(expr->pointer_offset_expr.ptr, eval_kind) && exprid_is_constant_eval(expr->pointer_offset_expr.offset, eval_kind); - case EXPR_CT_CONV: - return true; case EXPR_RETVAL: return false; case EXPR_BUILTIN: @@ -691,7 +689,6 @@ static bool sema_check_expr_lvalue(Expr *top_expr, Expr *expr) case EXPR_COMPOUND_LITERAL: case EXPR_CONST: case EXPR_CT_CALL: - case EXPR_CT_CONV: case EXPR_CT_EVAL: case EXPR_COND: case EXPR_DECL: @@ -792,7 +789,6 @@ bool expr_may_addr(Expr *expr) case EXPR_COMPOUND_LITERAL: case EXPR_CONST: case EXPR_CT_CALL: - case EXPR_CT_CONV: case EXPR_CT_IDENT: case EXPR_CT_EVAL: case EXPR_COND: @@ -8361,40 +8357,6 @@ static inline bool sema_expr_analyse_ct_call(SemaContext *context, Expr *expr) } } -static inline bool sema_expr_analyse_ct_conv(SemaContext *c, Expr *expr) -{ - TypeInfo *from = type_infoptr(expr->ct_call_expr.type_from); - TypeInfo *to = type_infoptr(expr->ct_call_expr.type_to); - if (!sema_resolve_type_info(c, from)) return false; - if (!sema_resolve_type_info(c, to)) return false; - Type *from_type = from->type; - Type *to_type = to->type; - if (IS_OPTIONAL(from)) - { - SEMA_ERROR(from, "Only non-optional types can be checked."); - return false; - } - if (IS_OPTIONAL(to)) - { - SEMA_ERROR(to, "Only non-optional types can be checked."); - return false; - } - bool result; - switch (expr->ct_call_expr.token_type) - { - case TOKEN_CT_CONVERTIBLE: - result = cast_may_implicit(from_type, to_type, true, false); - break; - case TOKEN_CT_CASTABLE: - result = cast_may_explicit(from_type, to_type, true, false); - break; - default: - UNREACHABLE - } - expr_rewrite_const_bool(expr, type_bool, result); - return true; -} - static inline BuiltinFunction builtin_by_name(const char *name) { @@ -8482,8 +8444,6 @@ static inline bool sema_analyse_expr_dispatch(SemaContext *context, Expr *expr) return sema_expr_analyse_retval(context, expr); case EXPR_BUILTIN: return sema_expr_analyse_builtin(context, expr, true); - case EXPR_CT_CONV: - return sema_expr_analyse_ct_conv(context, expr); case EXPR_CT_CALL: return sema_expr_analyse_ct_call(context, expr); case EXPR_HASH_IDENT: diff --git a/src/compiler/tokens.c b/src/compiler/tokens.c index b52c73485..923ca7db9 100644 --- a/src/compiler/tokens.c +++ b/src/compiler/tokens.c @@ -388,10 +388,6 @@ const char *token_type_to_string(TokenType type) return "$typefrom"; case TOKEN_CT_TYPEOF: return "$typeof"; - case TOKEN_CT_CONVERTIBLE: - return "$convertible"; - case TOKEN_CT_CASTABLE: - return "$castable"; case TOKEN_CT_STRINGIFY: return "$stringify"; case TOKEN_EOF: diff --git a/src/version.h b/src/version.h index 72f04e659..18b9ede93 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.3.53" \ No newline at end of file +#define COMPILER_VERSION "0.3.54" \ No newline at end of file