From dbce72146f26a6bd76954e5a18f9e074d6c8571c Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 7 May 2020 11:46:16 +0200 Subject: [PATCH] More AST dumps fixed. Throw simplified by removing "DECL_THROW", which also unearthed a bug. --- resources/testfragments/super_simple.c3 | 8 - src/compiler/ast.c | 293 +++++++++++------------- src/compiler/compiler_internal.h | 21 +- src/compiler/context.c | 1 - src/compiler/enums.h | 1 - src/compiler/llvm_codegen.c | 1 - src/compiler/llvm_codegen_debug_info.c | 1 - src/compiler/llvm_codegen_expr.c | 2 +- src/compiler/llvm_codegen_function.c | 1 - src/compiler/llvm_codegen_type.c | 2 - src/compiler/parser.c | 10 +- src/compiler/sema_decls.c | 15 +- src/compiler/sema_expr.c | 11 +- src/compiler/sema_stmts.c | 26 +-- src/compiler/sema_types.c | 8 +- src/compiler/types.c | 1 - 16 files changed, 163 insertions(+), 239 deletions(-) diff --git a/resources/testfragments/super_simple.c3 b/resources/testfragments/super_simple.c3 index fb377f1c3..8972cd4cd 100644 --- a/resources/testfragments/super_simple.c3 +++ b/resources/testfragments/super_simple.c3 @@ -759,10 +759,6 @@ func void testErrors() { printf("A2\n"); } - catch (error e) - { - printf("Wut\n"); - } try throwAOrB(2); catch (Err e) { @@ -772,10 +768,6 @@ func void testErrors() { printf("B2\n"); } - catch (error e) - { - printf("Wut\n"); - } printf("Throws: %d\n", throwsDone.times); printf("End of errors\n"); } diff --git a/src/compiler/ast.c b/src/compiler/ast.c index 5d50d7df8..036037913 100644 --- a/src/compiler/ast.c +++ b/src/compiler/ast.c @@ -16,9 +16,11 @@ static void fprint_ast_recursive(FILE *file, Ast *ast, int indent); #define DUMPEND() fprint_endparen(file, indent); return #define DUMPEXPR(_expr) fprint_expr_recursive(file, _expr, indent + 1) #define DUMPAST(_ast) fprint_ast_recursive(file, _ast, indent + 1) +#define DUMPASTS(_asts) fprint_asts_recursive(file, _asts, indent + 1) #define DUMPTI(_type_info) fprint_type_info_recursive(file, _type_info, indent + 1) #define DUMPTYPE(_type) fprint_type_recursive(file, _type, indent + 1) #define DUMPDECLS(_decls) fprint_decl_list(file, _decls, indent + 1) +#define DUMPDECL(_decl) fprint_decl_recursive(file, _decl, indent + 1) Decl *decl_new(DeclKind decl_kind, Token name, Visibility visibility) { @@ -75,7 +77,6 @@ Decl *decl_new_with_type(Token name, DeclKind decl_type, Visibility visibility) case DECL_TYPEDEF: kind = TYPE_TYPEDEF; break; - case DECL_THROWS: case DECL_POISONED: case DECL_VAR: case DECL_ENUM_CONSTANT: @@ -347,56 +348,52 @@ void fprint_type_recursive(FILE *file, Type *type, int indent) { if (!type) { - fprintf_indented(file, indent, "(none)\n"); + DUMP("(none)"); return; } switch (type->type_kind) { case TYPE_POISONED: - fprintf_indented(file, indent, "(type poison)\n"); + DUMP("(type poison)"); return; case TYPE_TYPEID: DUMP("(typeid)"); return; case TYPE_FUNC: - fprintf_indented(file, indent, "(type-func %s)\n", type->func.signature->mangled_signature); + DUMPF("(func-type %s)", type->name); return; case TYPE_STRUCT: - fprintf_indented(file, indent, "(struct %s::%s)\n", type->decl->module->name, type->decl->name); + DUMPF("(struct %s)", type->name); return; case TYPE_UNION: - fprintf_indented(file, indent, "(union %s::%s)\n", type->decl->module->name, type->decl->name); + DUMPF("(union %s)", type->name); return; case TYPE_ENUM: - fprintf_indented(file, indent, "(enum %s::%s)\n", type->decl->module->name, type->decl->name); + DUMPF("(enum %s)", type->name); return; case TYPE_ERROR: - fprintf_indented(file, indent, "(error %s::%s)\n", type->decl->module->name, type->decl->name); + DUMPF("(error %s)", type->name); return; case TYPE_TYPEDEF: - DUMPF("(user-defined %s", type->name); + DUMPF("(typedef %s", type->name); DUMPTYPE(type->canonical); DUMPEND(); case TYPE_POINTER: - fprintf_indented(file, indent, "(pointer\n"); - fprint_type_recursive(file, type->pointer, indent + 1); - fprint_endparen(file, indent); - break; + DUMP("(pointer"); + DUMPTYPE(type->pointer); + DUMPEND(); case TYPE_SUBARRAY: - fprintf_indented(file, indent, "(subarray\n"); - fprint_type_recursive(file, type->array.base, indent + 1); - fprint_endparen(file, indent); - break; + DUMP("(subarray"); + DUMPTYPE(type->array.base); + DUMPEND(); case TYPE_VARARRAY: - fprintf_indented(file, indent, "(vararray\n"); - fprint_type_recursive(file, type->array.base, indent + 1); - fprint_endparen(file, indent); - break; + DUMP("(vararray"); + DUMPTYPE(type->array.base); + DUMPEND(); case TYPE_ARRAY: - fprintf_indented(file, indent, "(array [%zu]\n", type->array.len); - fprint_type_recursive(file, type->array.base, indent + 1); - fprint_endparen(file, indent); - break; + DUMPF("(array [%zu]", type->array.len); + DUMPTYPE(type->array.base); + DUMPEND(); case TYPE_VOID: case TYPE_BOOL: case TYPE_I8: @@ -409,14 +406,14 @@ void fprint_type_recursive(FILE *file, Type *type, int indent) case TYPE_U64: case TYPE_F32: case TYPE_F64: - fprintf_indented(file, indent, "(type %s)\n", type->name); - break; + DUMPF("(%s)", type->name); + return; case TYPE_IXX: - fprintf_indented(file, indent, "(comp time int)\n"); - break; + DUMP("(ct int)"); + return; case TYPE_FXX: - fprintf_indented(file, indent, "(comp time float)\n"); - break; + DUMP("(ct float)"); + return; case TYPE_STRING: DUMP("(string)"); return; @@ -501,14 +498,15 @@ void fprint_expr_common(FILE *file, Expr *expr, int indent) { case RESOLVE_NOT_DONE: DUMP("(unresolved)"); - break; + return; case RESOLVE_RUNNING: DUMP("(resolving)"); - break; + return; case RESOLVE_DONE: - fprint_type_recursive(file, expr->type, indent); - break; + DUMPTYPE(expr->type); + return; } + UNREACHABLE } #define DUMPEXPC(_expr) fprint_expr_common(file, _expr, indent + 1) @@ -540,8 +538,8 @@ void fprint_expr_recursive(FILE *file, Expr *expr, int indent) case EXPR_BINARY: DUMPF("(binary %s", token_type_to_string(binaryop_to_token(expr->binary_expr.operator))); DUMPEXPC(expr); - fprint_expr_recursive(file, expr->binary_expr.left, indent + 1); - fprint_expr_recursive(file, expr->binary_expr.right, indent + 1); + DUMPEXPR(expr->binary_expr.left); + DUMPEXPR(expr->binary_expr.right); DUMPEND(); case EXPR_UNARY: DUMPF("(unary %s", token_type_to_string(unaryop_to_token(expr->unary_expr.operator))); @@ -574,12 +572,16 @@ void fprint_expr_recursive(FILE *file, Expr *expr, int indent) DUMPEND(); case EXPR_CALL: DUMP("(call"); - fprint_expr_common(file, expr, indent + 1); - fprint_expr_recursive(file, expr->call_expr.function, indent + 1); + DUMPEXPC(expr); + DUMPEXPR(expr->call_expr.function); + indent++; + DUMP("(args"); VECEACH(expr->call_expr.arguments, i) { - fprint_expr_recursive(file, expr->call_expr.arguments[i], indent + 1); + DUMPEXPR(expr->call_expr.arguments[i]); } + DUMPE(); + indent--; DUMPEND(); case EXPR_TERNARY: if (!expr->ternary_expr.then_expr) @@ -602,7 +604,6 @@ void fprint_expr_recursive(FILE *file, Expr *expr, int indent) switch (expr->expr_initializer.init_type) { case INITIALIZER_UNKNOWN: - fprintf(file, "not-analyzed\n"); break; case INITIALIZER_ZERO: @@ -616,11 +617,9 @@ void fprint_expr_recursive(FILE *file, Expr *expr, int indent) break; } DUMPEXPC(expr); + VECEACH(expr->expr_initializer.initializer_expr, i) { - VECEACH(expr->expr_initializer.initializer_expr, i) - { - fprint_expr_recursive(file, expr->expr_initializer.initializer_expr[i], indent + 1); - } + DUMPEXPR(expr->expr_initializer.initializer_expr[i]); } DUMPEND(); case EXPR_SUBSCRIPT: @@ -707,26 +706,34 @@ void fprint_expr_recursive(FILE *file, Expr *expr, int indent) void fprint_func_signature(FILE *file, FunctionSignature *signature, int indent) { - fprint_type_info_recursive(file, signature->rtype, indent); - if (!vec_size(signature->params)) + DUMP("(func-sig"); + DUMPTI(signature->rtype); + do { - fprintf_indented(file, indent, "(params none)\n"); - return; - } - fprintf_indented(file, indent, "(params\n"); - fprint_decl_list(file, signature->params, indent + 1); - fprint_endparen(file, indent); - if (signature->throw_any) - { - fprintf_indented(file, indent, "(throws any)\n"); - } - else - { - fprintf_indented(file, indent, "(throws\n"); - fprint_decl_list(file, signature->throws, indent + 1); - fprint_endparen(file, indent); - } + if (!vec_size(signature->params)) + { + DUMPI("(params none)"); + break; + } + indent++; + DUMP("(params"); + DUMPDECLS(signature->params); + DUMPE(); + if (signature->throw_any) + { + DUMP("(throws any)"); + } + else + { + DUMP("(throws"); + VECEACH(signature->throws, i) DUMPTI(signature->throws[i]); + DUMPE(); + } + indent--; + } while (false); + DUMPEND(); } + void fprint_decl_recursive(FILE *file, Decl *decl, int indent) { switch (decl->decl_kind) @@ -737,14 +744,8 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent) switch (decl->var.kind) { case VARDECL_CONST: - DUMPEXPR(decl->var.init_expr); - break; case VARDECL_GLOBAL: - DUMPEXPR(decl->var.init_expr); - break; case VARDECL_LOCAL: - DUMPEXPR(decl->var.init_expr); - break; case VARDECL_PARAM: DUMPEXPR(decl->var.init_expr); break; @@ -763,19 +764,18 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent) fprint_endparen(file, indent); break; case DECL_FUNC: - fprintf_indented(file, indent, "(func %s\n", decl->name); + DUMPF("(func %s", decl->name); if (decl->func.type_parent) { - fprint_indent(file, indent + 1); - fprintf(file, "(parent_type\n"); - fprint_type_info_recursive(file, decl->func.type_parent, indent + 2); - fprint_indent(file, indent + 1); - fprintf(file, ")\n"); + indent++; + DUMP("(parent_type"); + DUMPTI(decl->func.type_parent); + DUMPE(); + indent--; } fprint_func_signature(file, &decl->func.function_signature, indent + 1); - if (decl->func.body) fprint_ast_recursive(file, decl->func.body, indent + 1); - fprint_endparen(file, indent); - break; + if (decl->func.body) DUMPAST(decl->func.body); + DUMPEND(); case DECL_STRUCT: DUMPF("(struct %s", decl->name); DUMPDECLS(decl->strukt.members); @@ -785,91 +785,75 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent) DUMPDECLS(decl->strukt.members); DUMPEND(); case DECL_ENUM: - fprintf_indented(file, indent, "(enum %s\n", decl->name); - fprint_type_info_recursive(file, decl->enums.type_info, indent + 1); - fprint_decl_list(file, decl->enums.values, indent + 1); - fprint_endparen(file, indent); - break; + DUMPF("(enum %s", decl->name); + DUMPTI(decl->enums.type_info); + DUMPDECLS(decl->enums.values); + DUMPEND(); case DECL_ERROR: - fprintf_indented(file, indent, "(error %s\n", decl->name); - fprint_decl_list(file, decl->error.error_constants, indent + 1); - fprint_endparen(file, indent); - break; + DUMPF("(error %s", decl->name); + DUMPDECLS(decl->error.error_constants); + DUMPEND(); case DECL_ENUM_CONSTANT: if (!decl->enum_constant.expr) { - fprintf_indented(file, indent, "(enum-constant %s)\n", decl->name); + DUMPF("(enum-constant %s)", decl->name); return; } - fprintf_indented(file, indent, "(enum-constant %s\n", decl->name); - fprint_expr_recursive(file, decl->enum_constant.expr, indent + 1); - fprint_endparen(file, indent); - break; + DUMPF("(enum-constant %s", decl->name); + DUMPEXPR(decl->enum_constant.expr); + DUMPEND(); case DECL_ERROR_CONSTANT: - fprintf_indented(file, indent, "(error-constant %s)\n", decl->name); - break; + DUMPF("(error-constant %s)", decl->name); + return; case DECL_GENERIC: - fprintf_indented(file, indent, "(generic %s\n", decl->name); - fprint_indent(file, indent + 1); - fprintf(file, "(params\n"); + DUMPF("(generic %s\n", decl->name); + indent++; + DUMP("(params"); + VECEACH(decl->generic_decl.parameters, i) { - VECEACH(decl->generic_decl.parameters, i) - { - fprint_indent(file, indent + 2); - fprintf(file, "%s\n", decl->generic_decl.parameters[i].string); - } + DUMPFI("%s", decl->generic_decl.parameters[i].string); } - fprint_endparen(file, indent + 1); - fprint_indent(file, indent + 1); - fprintf(file, "(cases\n"); - { - VECEACH(decl->generic_decl.cases, i) - { - fprint_ast_recursive(file, decl->generic_decl.cases[i], indent + 2); - } - } - fprint_endparen(file, indent + 2); - fprint_endparen(file, indent); - break; + DUMPE(); + DUMP("(cases"); + DUMPASTS(decl->generic_decl.cases); + DUMPE(); + indent--; + DUMPEND(); case DECL_TYPEDEF: - fprintf_indented(file, indent, "(typedef %s\n", decl->name); + DUMPF("(typedef %s", decl->name); if (decl->typedef_decl.is_func) { fprint_func_signature(file, &decl->typedef_decl.function_signature, indent + 1); } else { - fprint_type_info_recursive(file, decl->typedef_decl.type_info, indent + 1); + DUMPTI(decl->typedef_decl.type_info); } - fprint_endparen(file, indent); - break; + DUMPEND(); case DECL_CT_IF: - fprintf_indented(file, indent, "(ct-if\n"); - fprint_expr_recursive(file, decl->ct_if_decl.expr, indent + 1); - fprint_decl_list(file, decl->ct_if_decl.then, indent + 1); + DUMP("(ct-if"); + DUMPEXPR(decl->ct_if_decl.expr); + DUMPDECLS(decl->ct_if_decl.then); if (decl->ct_if_decl.elif) { - fprint_decl_recursive(file, decl->ct_if_decl.elif, indent + 1); + DUMPDECL(decl->ct_if_decl.elif); } - fprint_endparen(file, indent); - break; + DUMPEND(); case DECL_CT_ELIF: - fprintf_indented(file, indent, "(ct-elif\n"); - fprint_expr_recursive(file, decl->ct_elif_decl.expr, indent + 1); - fprint_decl_list(file, decl->ct_elif_decl.then, indent + 1); + DUMP("(ct-elif"); + DUMPEXPR(decl->ct_elif_decl.expr); + DUMPDECLS(decl->ct_elif_decl.then); if (decl->ct_elif_decl.elif) { - fprint_decl_recursive(file, decl->ct_elif_decl.elif, indent + 1); + DUMPDECL(decl->ct_elif_decl.elif); } - fprint_endparen(file, indent); - break; + DUMPEND(); case DECL_CT_ELSE: - fprintf_indented(file, indent, "(ct-else\n"); - fprint_decl_list(file, decl->ct_else_decl, indent + 1); - fprint_endparen(file, indent); - break; + DUMP("(ct-else"); + DUMPDECLS(decl->ct_else_decl); + DUMPEND(); case DECL_POISONED: - fprintf_indented(file, indent, "(poisoned-decl)\n"); + DUMP("(poisoned-decl)"); return; case DECL_ARRAY_VALUE: fprintf_indented(file, indent, "(array value"); @@ -877,10 +861,10 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent) fprint_endparen(file, indent); return; case DECL_IMPORT: - fprintf_indented(file, indent, "(import %s", decl->name); + DUMPF("(import %s", decl->name); - fprint_endparen(file, indent); - break; + // TODO + DUMPEND(); case DECL_ATTRIBUTE: fprintf_indented(file, indent, "(attribute %s", decl->name); if (decl->attr.domains & ATTR_FUNC) @@ -918,11 +902,6 @@ void fprint_decl_recursive(FILE *file, Decl *decl, int indent) // TODO attribute fprint_endparen(file, indent); break; - case DECL_THROWS: - fprintf_indented(file, indent, "(throws"); - fprint_type_info_recursive(file, decl->throws, indent + 1); - fprint_endparen(file, indent); - break;; } } @@ -958,17 +937,16 @@ static void fprint_ast_recursive(FILE *file, Ast *ast, int indent) DUMPEND(); case AST_DECL_EXPR_LIST: DUMP("(declexprlist"); - fprint_asts_recursive(file, ast->decl_expr_stmt, indent + 1); + DUMPASTS(ast->decl_expr_stmt); DUMPEND(); case AST_DECLARE_STMT: DUMP("(declare"); - fprint_decl_recursive(file, ast->declare_stmt, indent + 1); + DUMPDECL(ast->declare_stmt); DUMPEND(); case AST_EXPR_STMT: DUMP("expr"); DUMPEXPR(ast->expr_stmt); DUMPEND(); - return; case AST_WHILE_STMT: DUMP("(while"); DUMPAST(ast->while_stmt.cond); @@ -1038,8 +1016,8 @@ static void fprint_ast_recursive(FILE *file, Ast *ast, int indent) DUMPEND(); case AST_SWITCH_STMT: DUMP("(switch"); - fprint_ast_recursive(file, ast->switch_stmt.cond, indent + 1); - fprint_asts_recursive(file, ast->switch_stmt.cases, indent + 1); + DUMPAST(ast->switch_stmt.cond); + DUMPASTS(ast->switch_stmt.cases); DUMPEND(); case AST_CASE_STMT: DUMP("(case"); @@ -1050,18 +1028,17 @@ static void fprint_ast_recursive(FILE *file, Ast *ast, int indent) DUMPAST(ast->defer_stmt.body); DUMPEND(); case AST_GENERIC_CASE_STMT: - fprintf(file, "(generic-case\n"); - fprint_indent(file, indent + 1); - fprintf(file, "(match\n"); + DUMP("(generic-case"); + indent++; + DUMP("(match"); + VECEACH(ast->generic_case_stmt.types, i) { - VECEACH(ast->generic_case_stmt.types, i) - { - fprint_type_info_recursive(file, ast->generic_case_stmt.types[i], indent + 2); - } + DUMPTI(ast->generic_case_stmt.types[i]); } - fprint_endparen(file, indent + 1); - fprint_ast_recursive(file, ast->generic_case_stmt.body, indent + 1); - break; + DUMPE(); + indent--; + DUMPAST(ast->generic_case_stmt.body); + DUMPEND(); case AST_GENERIC_DEFAULT_STMT: DUMP("(generic-default"); DUMPAST(ast->generic_default_stmt); diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 05a457b33..3711a086a 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -311,7 +311,7 @@ typedef struct _FunctionSignature ErrorReturn error_return : 4; TypeInfo *rtype; Decl** params; - Decl** throws; + TypeInfo** throws; const char *mangled_signature; } FunctionSignature; @@ -915,12 +915,7 @@ typedef struct SourceRange span; ThrowType kind : 4; ThrowInfo *throw_info; - // The error type of the throw. - union - { - Type *throw; - Decl **throws; - }; + TypeInfo **throws; } Throw; typedef struct _Context @@ -1245,18 +1240,14 @@ void *target_data_layout(); void *target_machine(); void *target_target(); -bool throw_completely_caught(Decl *throw, CatchInfo *catches); -static inline Throw throw_new_single(SourceRange range, ThrowType type, ThrowInfo *info, Type *throw) -{ - return (Throw) { .kind = type, .span = range, .throw_info = info, .throw = throw }; -} +bool throw_completely_caught(TypeInfo *throw, CatchInfo *catches); static inline Throw throw_new_union(SourceRange range, ThrowType type, ThrowInfo *info) { - return (Throw) { .kind = type, .span = range, .throw_info = info, .throw = type_error_union }; + return (Throw) { .kind = THROW_TYPE_CALL_ANY, .span = range, .throw_info = info }; } -static inline Throw throw_new_multiple(SourceRange range, ThrowInfo *info, Decl **throws) +static inline Throw throw_new(SourceRange range, ThrowType type, ThrowInfo *info, TypeInfo **throws) { - return (Throw) { .kind = THROW_TYPE_CALL_THROW_MANY, .span = range, .throw_info = info, .throws = throws }; + return (Throw) { .kind = type, .span = range, .throw_info = info, .throws = throws }; } #define TOKEN_MAX_LENGTH 0xFFFF diff --git a/src/compiler/context.c b/src/compiler/context.c index ea6026c73..2fbbaca87 100644 --- a/src/compiler/context.c +++ b/src/compiler/context.c @@ -131,7 +131,6 @@ void context_register_global_decl(Context *context, Decl *decl) case DECL_CT_ELSE: case DECL_CT_ELIF: case DECL_ATTRIBUTE: - case DECL_THROWS: UNREACHABLE break; case DECL_CT_IF: diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 580ee298e..f0055e3aa 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -195,7 +195,6 @@ typedef enum DECL_CT_ELSE, DECL_CT_ELIF, DECL_ATTRIBUTE, - DECL_THROWS, } DeclKind; // Ordering here is in priority if two branches should have the same exit. diff --git a/src/compiler/llvm_codegen.c b/src/compiler/llvm_codegen.c index 4a3e87cdf..8a3197cc7 100644 --- a/src/compiler/llvm_codegen.c +++ b/src/compiler/llvm_codegen.c @@ -339,7 +339,6 @@ static void gencontext_emit_decl(GenContext *context, Decl *decl) case DECL_CT_ELSE: case DECL_CT_ELIF: case DECL_ATTRIBUTE: - case DECL_THROWS: UNREACHABLE } } diff --git a/src/compiler/llvm_codegen_debug_info.c b/src/compiler/llvm_codegen_debug_info.c index 9cabb7d4d..542d4d176 100644 --- a/src/compiler/llvm_codegen_debug_info.c +++ b/src/compiler/llvm_codegen_debug_info.c @@ -9,7 +9,6 @@ static inline LLVMMetadataRef gencontext_create_debug_type_from_decl(GenContext static LLVMMetadataRef debug_params[512]; switch (decl->decl_kind) { - case DECL_THROWS: case DECL_ATTRIBUTE: case DECL_ENUM_CONSTANT: case DECL_POISONED: diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 479dc16f2..a28b06760 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -1133,7 +1133,7 @@ static inline bool gencontext_emit_throw_branch_for_single_throw(GenContext *con } -static inline void gencontext_emit_throw_branch(GenContext *context, LLVMValueRef value, Decl** errors, ThrowInfo *throw_info, ErrorReturn error_return) +static inline void gencontext_emit_throw_branch(GenContext *context, LLVMValueRef value, TypeInfo** errors, ThrowInfo *throw_info, ErrorReturn error_return) { Type *call_error_type; switch (error_return) diff --git a/src/compiler/llvm_codegen_function.c b/src/compiler/llvm_codegen_function.c index 561d3aee5..220ece4e5 100644 --- a/src/compiler/llvm_codegen_function.c +++ b/src/compiler/llvm_codegen_function.c @@ -272,7 +272,6 @@ void gencontext_emit_extern_decl(GenContext *context, Decl *decl) case DECL_CT_ELSE: case DECL_CT_ELIF: case DECL_ATTRIBUTE: - case DECL_THROWS: UNREACHABLE } } diff --git a/src/compiler/llvm_codegen_type.c b/src/compiler/llvm_codegen_type.c index e359e8014..79b711302 100644 --- a/src/compiler/llvm_codegen_type.c +++ b/src/compiler/llvm_codegen_type.c @@ -91,8 +91,6 @@ static inline LLVMTypeRef llvm_type_from_decl(LLVMContextRef context, Decl *decl context->error_type = error_type; } return context->error_type;*/ - case DECL_THROWS: - UNREACHABLE } UNREACHABLE } diff --git a/src/compiler/parser.c b/src/compiler/parser.c index 0d235ad54..ae0e68122 100644 --- a/src/compiler/parser.c +++ b/src/compiler/parser.c @@ -834,14 +834,12 @@ static inline bool parse_opt_throw_declaration(Context *context, Visibility visi signature->throw_any = true; return true; } - Decl **throws = NULL; + TypeInfo **throws = NULL; while (1) { - TypeInfo *type_info = parse_base_type(context); - if (!type_info_ok(type_info)) return false; - Decl *throw = decl_new(DECL_THROWS, context->tok, visibility); - throw->throws = type_info; - VECADD(throws, throw); + TypeInfo *throw = parse_base_type(context); + if (!type_info_ok(throw)) return false; + vec_add(throws, throw); if (!try_consume(context, TOKEN_COMMA)) break; } switch (context->tok.type) diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index 8138a51ed..37847605f 100644 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -261,10 +261,10 @@ static inline Type *sema_analyse_function_signature(Context *context, FunctionSi buffer[buffer_write_offset++] = '!'; VECEACH(signature->throws, i) { - Decl *err_decl = signature->throws[i]; - if (!sema_analyse_decl(context, err_decl)) + TypeInfo *err_decl = signature->throws[i]; + if (!sema_resolve_type_info(context, err_decl)) { - continue; + return false; } if (i > 0 && all_ok) { @@ -408,12 +408,6 @@ static inline bool sema_analyse_enum(Context *context, Decl *decl) return success; } -static inline bool sema_analyse_throws(Context *context, Decl *decl) -{ - if (!sema_resolve_type_info(context, decl->throws)) return false; - decl->type = decl->throws->type; - return true; -} static inline bool sema_analyse_method_function(Context *context, Decl *decl) @@ -534,9 +528,6 @@ bool sema_analyse_decl(Context *context, Decl *decl) decl->module = context->module; switch (decl->decl_kind) { - case DECL_THROWS: - if (!sema_analyse_throws(context, decl)) return decl_poison(decl); - break; case DECL_STRUCT: if (!sema_analyse_struct_union(context, decl)) return decl_poison(decl); sema_set_struct_size(decl); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 1b7122285..fc529cf41 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -276,14 +276,9 @@ static inline bool sema_expr_analyse_func_call(Context *context, Type *to, Expr } else { - if (vec_size(signature->throws) == 1) - { - vec_add(context->error_calls, throw_new_single(expr->span, THROW_TYPE_CALL_THROW_ONE, expr->call_expr.throw_info, signature->throws[0]->type)); - } - else - { - vec_add(context->error_calls, throw_new_multiple(expr->span, expr->call_expr.throw_info, signature->throws)); - } + vec_add(context->error_calls, throw_new(expr->span, + vec_size(signature->throws) == 1 ? THROW_TYPE_CALL_THROW_ONE : THROW_TYPE_CALL_THROW_MANY, + expr->call_expr.throw_info, signature->throws)); } } unsigned func_param_count = vec_size(func_params); diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 3cd4b6fff..3073edce7 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -444,13 +444,13 @@ static inline bool throw_completely_handled_call_throw_many(Throw *throw) { assert(throw->kind == THROW_TYPE_CALL_THROW_MANY && "Only for throw many"); assert(!throw->throw_info->is_completely_handled && "Expected unhandled"); - Decl **throws = throw->throws; + TypeInfo **throws = throw->throws; CatchInfo *catched = throw->throw_info->catches; unsigned catches = 0; unsigned throw_count = vec_size(throws); for (unsigned i = 0; i < throw_count; i++) { - Decl *throw_decl = throws[i]; + TypeInfo *throw_decl = throws[i]; if (throw_completely_caught(throw_decl, catched)) { catches++; @@ -530,7 +530,7 @@ static bool sema_analyse_catch_stmt(Context *context, Ast *statement) break; case THROW_TYPE_CALL_THROW_ONE: // If there is no match, ignore. - if (throw->throw != error_type) continue; + if (throw->throws[0]->type != error_type) continue; // Otherwise add and set to completely handled. vec_add(throw->throw_info->catches, catch); throw->throw_info->is_completely_handled = true; @@ -995,30 +995,20 @@ static inline void defer_list_walk_to_common_depth(Ast **defer_stmt, int this_de } } -static inline bool throw_add_error_return_catch(Throw *throw, Decl **func_throws) +static inline bool throw_add_error_return_catch(Throw *throw, TypeInfo **func_throws) { assert(throw->kind != THROW_TYPE_CALL_ANY); - Decl **throws; - unsigned throw_count; - if (throw->kind == THROW_TYPE_CALL_THROW_MANY) - { - throws = throw->throws; - throw_count = vec_size(throws); - } - else - { - throws = &throw->throw->decl; - throw_count = 1; - } + TypeInfo **throws = throw->throws; + unsigned throw_count = vec_size(throws); unsigned func_throw_count = vec_size(func_throws); assert(func_throw_count); bool catch_added = false; for (unsigned i = 0; i < func_throw_count; i++) { - Decl *func_throw = func_throws[i]; + TypeInfo *func_throw = func_throws[i]; for (unsigned j = 0; j < throw_count; j++) { - if (throws[j] == func_throw->type->decl) + if (throws[j]->type == func_throw->type) { // If the throw was already caught, ignore it. if (throw_completely_caught(throws[j], throw->throw_info->catches)) continue; diff --git a/src/compiler/sema_types.c b/src/compiler/sema_types.c index 5522aec91..e47a50b47 100644 --- a/src/compiler/sema_types.c +++ b/src/compiler/sema_types.c @@ -18,7 +18,7 @@ static inline bool sema_resolve_ptr_type(Context *context, TypeInfo *type_info) return true; } -bool throw_completely_caught(Decl *throw, CatchInfo *catches) +bool throw_completely_caught(TypeInfo *throw, CatchInfo *catches) { VECEACH(catches, i) { @@ -26,14 +26,14 @@ bool throw_completely_caught(Decl *throw, CatchInfo *catches) switch (catch_info->kind) { case CATCH_REGULAR: - if (throw == catch_info->catch->catch_stmt.error_param->type->decl) return true; + if (throw->type == catch_info->catch->catch_stmt.error_param->type) return true; break; case CATCH_TRY_ELSE: case CATCH_RETURN_ANY: return true; case CATCH_RETURN_MANY: case CATCH_RETURN_ONE: - if (throw == catch_info->error) return true; + if (throw->type == catch_info->error->type) return true; break; } } @@ -97,8 +97,6 @@ static bool sema_resolve_type_identifier(Context *context, TypeInfo *type_info) } switch (decl->decl_kind) { - case DECL_THROWS: - TODO case DECL_STRUCT: case DECL_UNION: case DECL_ERROR: diff --git a/src/compiler/types.c b/src/compiler/types.c index ee9fff297..5d0592db2 100644 --- a/src/compiler/types.c +++ b/src/compiler/types.c @@ -135,7 +135,6 @@ static void type_append_signature_name_user_defined(Decl *decl, char *dst, size_ *offset += len; return; } - case DECL_THROWS: case DECL_POISONED: case DECL_VAR: case DECL_ENUM_CONSTANT: