From 9b0dfe8ba3eee1fbe513e77f1d0bf049fc4dc851 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 4 Mar 2022 17:08:47 +0100 Subject: [PATCH] Cleanup: remove CT_ELIF ast node. --- src/compiler/ast.c | 5 +++ src/compiler/compiler_internal.h | 16 +++++---- src/compiler/copying.c | 25 +++++++------- src/compiler/enums.h | 1 - src/compiler/llvm_codegen_expr.c | 4 +-- src/compiler/llvm_codegen_stmt.c | 1 - src/compiler/parse_stmt.c | 56 ++++++++++---------------------- src/compiler/sema_expr.c | 4 +-- src/compiler/sema_stmts.c | 15 ++++----- 9 files changed, 56 insertions(+), 71 deletions(-) diff --git a/src/compiler/ast.c b/src/compiler/ast.c index 6987a262c..8dd170846 100644 --- a/src/compiler/ast.c +++ b/src/compiler/ast.c @@ -217,6 +217,11 @@ Decl *decl_new_generated_var(Type *type, VarDeclKind kind, SourceSpan span) return decl; } +INLINE bool exprid_is_pure(ExprId expr_id) +{ + return expr_is_pure(exprptr(expr_id)); +} + bool expr_is_pure(Expr *expr) { if (!expr) return true; diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 70eab4def..b6e610058 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -48,6 +48,7 @@ typedef struct Type_ Type; typedef Type CanonicalType; typedef unsigned AstId; +typedef unsigned ExprId; typedef struct Int128_ { @@ -717,8 +718,8 @@ typedef struct typedef struct { - Expr *left; - Expr *right; + ExprId left; + ExprId right; } ExprSliceAssign; @@ -1002,8 +1003,8 @@ typedef struct typedef struct { FlowCommon flow; - Expr *cond; - Ast *body; + ExprId cond; + AstId body; void *break_block; void *continue_block; } AstWhileStmt; @@ -1023,7 +1024,7 @@ typedef struct struct { Expr *expr; - Ast *body; + AstId body; }; }; } AstDoStmt; @@ -1258,7 +1259,6 @@ typedef struct Ast_ AstForStmt for_stmt; // 32 AstForeachStmt foreach_stmt; AstCtIfStmt ct_if_stmt; // 24 - AstCtIfStmt ct_elif_stmt; // 24 AstId ct_else_stmt; // 8 AstCtForeachStmt ct_foreach_stmt; // 64 AstScopedStmt scoped_stmt; // 16 @@ -2467,11 +2467,13 @@ static inline bool type_is_promotable_float(Type *type) #define MACRO_COPY_DECL(x) x = copy_decl(x) #define MACRO_COPY_DECL_LIST(x) x = copy_decl_list(x) #define MACRO_COPY_EXPR(x) x = copy_expr(x) +#define MACRO_COPY_EXPRID(x) x = exprid_copy_deep(x) #define MACRO_COPY_TYPE(x) x = copy_type_info(x) #define MACRO_COPY_TYPE_LIST(x) x = type_info_copy_list_from_macro(x) #define MACRO_COPY_EXPR_LIST(x) x = copy_expr_list(x) #define MACRO_COPY_AST_LIST(x) x = copy_ast_list(x) #define MACRO_COPY_AST(x) x = ast_copy_deep(x) +#define MACRO_COPY_ASTID(x) x = astid_copy_deep(x) Expr **copy_expr_list(Expr **expr_list); Expr *copy_expr(Expr *source_expr); @@ -2500,7 +2502,9 @@ void platform_compiler(const char **files, unsigned file_count, const char* flag #define CAT2(a,b) a##b // actually concatenate #define TEMP(X) CAT(X, __LINE__) #define ASSIGN_AST_OR_RET(_assign, _ast_stmt, _res) Ast* TEMP(_ast) = (_ast_stmt); if (!ast_ok(TEMP(_ast))) return _res; _assign = TEMP(_ast) +#define ASSIGN_ASTID_OR_RET(_assign, _ast_stmt, _res) Ast* TEMP(_ast) = (_ast_stmt); if (!ast_ok(TEMP(_ast))) return _res; _assign = astid(TEMP(_ast)) #define ASSIGN_EXPR_OR_RET(_assign, _expr_stmt, _res) Expr* TEMP(_expr) = (_expr_stmt); if (!expr_ok(TEMP(_expr))) return _res; _assign = TEMP(_expr) +#define ASSIGN_EXPRID_OR_RET(_assign, _expr_stmt, _res) Expr* TEMP(_expr) = (_expr_stmt); if (!expr_ok(TEMP(_expr))) return _res; _assign = exprid(TEMP(_expr)) #define ASSIGN_TYPE_OR_RET(_assign, _type_stmt, _res) TypeInfo* TEMP(_type) = (_type_stmt); if (!type_info_ok(TEMP(_type))) return _res; _assign = TEMP(_type) #define ASSIGN_DECL_OR_RET(_assign, _decl_stmt, _res) Decl* TEMP(_decl) = (_decl_stmt); if (!decl_ok(TEMP(_decl))) return _res; _assign = TEMP(_decl) diff --git a/src/compiler/copying.c b/src/compiler/copying.c index 178259319..64264d302 100644 --- a/src/compiler/copying.c +++ b/src/compiler/copying.c @@ -40,6 +40,12 @@ static AstId astid_copy_deep(AstId source) return astid(ast_copy_deep(astptr(source))); } +static ExprId exprid_copy_deep(ExprId source) +{ + if (!source) return 0; + return exprid(copy_expr(exprptr(source))); +} + static DesignatorElement **macro_copy_designator_list(DesignatorElement **list) { @@ -117,8 +123,8 @@ Expr *copy_expr(Expr *source_expr) MACRO_COPY_TYPE(expr->type_expr); return expr; case EXPR_SLICE_ASSIGN: - MACRO_COPY_EXPR(expr->slice_assign_expr.left); - MACRO_COPY_EXPR(expr->slice_assign_expr.right); + MACRO_COPY_EXPRID(expr->slice_assign_expr.left); + MACRO_COPY_EXPRID(expr->slice_assign_expr.right); return expr; case EXPR_SLICE: MACRO_COPY_EXPR(expr->slice_expr.expr); @@ -280,15 +286,10 @@ Ast *ast_copy_deep(Ast *source) case AST_CT_IF_STMT: MACRO_COPY_EXPR(ast->ct_if_stmt.expr); MACRO_COPY_AST(ast->ct_if_stmt.elif); - ast->ct_if_stmt.then = astid_copy_deep(ast->ct_if_stmt.then); - return ast; - case AST_CT_ELIF_STMT: - MACRO_COPY_EXPR(ast->ct_elif_stmt.expr); - ast->ct_elif_stmt.then = astid_copy_deep(ast->ct_elif_stmt.then); - MACRO_COPY_AST(ast->ct_elif_stmt.elif); + MACRO_COPY_ASTID(ast->ct_if_stmt.then); return ast; case AST_CT_ELSE_STMT: - ast->ct_else_stmt = astid_copy_deep(ast->ct_else_stmt); + MACRO_COPY_ASTID(ast->ct_else_stmt); return ast; case AST_CT_FOREACH_STMT: ast->ct_foreach_stmt.body = astid_copy_deep(ast->ct_foreach_stmt.body); @@ -310,7 +311,7 @@ Ast *ast_copy_deep(Ast *source) return ast; case AST_DO_STMT: copy_flow(ast); - MACRO_COPY_AST(ast->do_stmt.body); + MACRO_COPY_ASTID(ast->do_stmt.body); MACRO_COPY_EXPR(ast->do_stmt.expr); return ast; case AST_EXPR_STMT: @@ -356,8 +357,8 @@ Ast *ast_copy_deep(Ast *source) return ast; case AST_WHILE_STMT: copy_flow(ast); - MACRO_COPY_EXPR(ast->while_stmt.cond); - MACRO_COPY_AST(ast->while_stmt.body); + MACRO_COPY_EXPRID(ast->while_stmt.cond); + MACRO_COPY_ASTID(ast->while_stmt.body); return ast; } UNREACHABLE; diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 2b253de1b..1a2619688 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -52,7 +52,6 @@ typedef enum AST_CONTINUE_STMT, AST_CT_ASSERT, AST_CT_IF_STMT, - AST_CT_ELIF_STMT, AST_CT_ELSE_STMT, AST_CT_FOR_STMT, AST_CT_FOREACH_STMT, diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 6faf92424..9711b4630 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -2436,14 +2436,14 @@ static void llvm_emit_slice_assign(GenContext *c, BEValue *be_value, Expr *expr) // while (slice_current <= end) pointer[slice_current++] = value; // First, find the value assigned. - Expr *assigned_value = expr->slice_assign_expr.right; + Expr *assigned_value = exprptr(expr->slice_assign_expr.right); llvm_emit_expr(c, be_value, assigned_value); BEValue parent; BEValue start; BEValue end; // Use general function to get all the values we need (a lot!) - llvm_emit_slice_values(c, expr->slice_assign_expr.left, &parent, &start, &end); + llvm_emit_slice_values(c, exprptr(expr->slice_assign_expr.left), &parent, &start, &end); llvm_value_rvalue(c, &start); llvm_value_rvalue(c, &end); diff --git a/src/compiler/llvm_codegen_stmt.c b/src/compiler/llvm_codegen_stmt.c index c0cd11fc3..4ff48ef50 100644 --- a/src/compiler/llvm_codegen_stmt.c +++ b/src/compiler/llvm_codegen_stmt.c @@ -1276,7 +1276,6 @@ void llvm_emit_stmt(GenContext *c, Ast *ast) break; case AST_CT_ASSERT: case AST_CT_IF_STMT: - case AST_CT_ELIF_STMT: case AST_CT_ELSE_STMT: case AST_CT_FOR_STMT: case AST_CT_SWITCH_STMT: diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index 8afcab972..c449c38e9 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -76,7 +76,7 @@ static inline Ast* parse_do_stmt(ParseContext *c) advance_and_verify(c, TOKEN_DO); ASSIGN_DECL_OR_RET(do_ast->do_stmt.flow.label, parse_optional_label(c, do_ast), poisoned_ast); - ASSIGN_AST_OR_RET(do_ast->do_stmt.body, parse_stmt(c), poisoned_ast); + ASSIGN_ASTID_OR_RET(do_ast->do_stmt.body, parse_stmt(c), poisoned_ast); if (try_consume(c, TOKEN_WHILE)) { @@ -133,9 +133,9 @@ static inline Ast* parse_while_stmt(ParseContext *c) ASSIGN_DECL_OR_RET(while_ast->while_stmt.flow.label, parse_optional_label(c, while_ast), poisoned_ast); CONSUME_OR_RET(TOKEN_LPAREN, poisoned_ast); - ASSIGN_EXPR_OR_RET(while_ast->while_stmt.cond, parse_cond(c), poisoned_ast); + ASSIGN_EXPRID_OR_RET(while_ast->while_stmt.cond, parse_cond(c), poisoned_ast); CONSUME_OR_RET(TOKEN_RPAREN, poisoned_ast); - ASSIGN_AST_OR_RET(while_ast->while_stmt.body, parse_stmt(c), poisoned_ast); + ASSIGN_ASTID_OR_RET(while_ast->while_stmt.body, parse_stmt(c), poisoned_ast); return while_ast; } @@ -559,62 +559,40 @@ static inline Ast* parse_ct_else_stmt(ParseContext *c) return ast; } -/** - * ct_elif_stmt - * : CT_ELIF '(' expression ')' ':' ct_compound_stmt (ct_elif_stmt | ct_else_stmt)? - */ -static inline Ast *parse_ct_elif_stmt(ParseContext *c) -{ - Ast *ast = ast_new_curr(c, AST_CT_ELIF_STMT); - advance_and_verify(c, TOKEN_CT_ELIF); - ASSIGN_EXPR_OR_RET(ast->ct_elif_stmt.expr, parse_const_paren_expr(c), poisoned_ast); - TRY_CONSUME(TOKEN_COLON, "$elif needs a ':' after the expression, did you forget it?"); - if (!parse_ct_compound_stmt(c, &ast->ct_elif_stmt.then)) return poisoned_ast; - if (tok_is(c, TOKEN_CT_ELIF)) - { - ASSIGN_AST_OR_RET(ast->ct_elif_stmt.elif, parse_ct_elif_stmt(c), poisoned_ast); - } - else if (tok_is(c, TOKEN_CT_ELSE)) - { - ASSIGN_AST_OR_RET(ast->ct_elif_stmt.elif, parse_ct_else_stmt(c), poisoned_ast); - } - return ast; -} /** * ct_if_stmt * : CT_IF '(' expression ')' ':' ct_compound_stmt (ct_elif_stmt | ct_else_stmt) CT_ENDIF EOS * ; */ -static inline Ast* parse_ct_if_stmt(ParseContext *c) +static inline Ast* parse_ct_if_stmt(ParseContext *c, bool is_elif) { Ast *ast = ast_new_curr(c, AST_CT_IF_STMT); - advance_and_verify(c, TOKEN_CT_IF); + advance_and_verify(c, is_elif ? TOKEN_CT_ELIF : TOKEN_CT_IF); ASSIGN_EXPR_OR_RET(ast->ct_if_stmt.expr, parse_const_paren_expr(c), poisoned_ast); - TRY_CONSUME(TOKEN_COLON, "$if needs a ':' after the expression, did you forget it?"); + if (is_elif) + { + TRY_CONSUME(TOKEN_COLON, "$elif needs a ':' after the expression, did you forget it?"); + } + else + { + TRY_CONSUME(TOKEN_COLON, "$if needs a ':' after the expression, did you forget it?"); + } if (!parse_ct_compound_stmt(c, &ast->ct_if_stmt.then)) return poisoned_ast; if (tok_is(c, TOKEN_CT_ELIF)) { - ASSIGN_AST_OR_RET(ast->ct_if_stmt.elif, parse_ct_elif_stmt(c), poisoned_ast); + ASSIGN_AST_OR_RET(ast->ct_if_stmt.elif, parse_ct_if_stmt(c, true), poisoned_ast); } else if (tok_is(c, TOKEN_CT_ELSE)) { ASSIGN_AST_OR_RET(ast->ct_if_stmt.elif, parse_ct_else_stmt(c), poisoned_ast); } + if (is_elif) return ast; advance_and_verify(c, TOKEN_CT_ENDIF); RANGE_EXTEND_PREV(ast); - do - { - if (!tok_is(c, TOKEN_EOS)) - { - sema_error_at_after(c->prev_span, "Expected ';'"); - return poisoned_ast; - } - advance(c); - } - while (0); + CONSUME_EOS_OR_RET(poisoned_ast); return ast; } @@ -907,7 +885,7 @@ Ast *parse_stmt(ParseContext *c) case TOKEN_CT_ASSERT: return parse_ct_assert_stmt(c); case TOKEN_CT_IF: - return parse_ct_if_stmt(c); + return parse_ct_if_stmt(c, false); case TOKEN_CT_SWITCH: return parse_ct_switch_stmt(c); case TOKEN_CT_FOREACH: diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 5f03a66c6..ed6949882 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -4310,8 +4310,8 @@ static inline bool sema_expr_analyse_slice_assign(SemaContext *context, Expr *ex Expr *left = expr->binary_expr.left; expr->type = right->type; expr->expr_kind = EXPR_SLICE_ASSIGN; - expr->slice_assign_expr.left = left; - expr->slice_assign_expr.right = right; + expr->slice_assign_expr.left = exprid(left); + expr->slice_assign_expr.right = exprid(right); return true; } diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 4bc012ee9..5a5436117 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -701,8 +701,8 @@ static inline bool sema_analyse_stmt_placement(Expr *cond, Ast *stmt) */ static inline bool sema_analyse_while_stmt(SemaContext *context, Ast *statement) { - Expr *cond = statement->while_stmt.cond; - Ast *body = statement->while_stmt.body; + Expr *cond = exprptr(statement->while_stmt.cond); + Ast *body = astptr(statement->while_stmt.body); bool success; @@ -755,7 +755,7 @@ static inline bool sema_analyse_while_stmt(SemaContext *context, Ast *statement) static inline bool sema_analyse_do_stmt(SemaContext *context, Ast *statement) { Expr *expr = statement->do_stmt.expr; - Ast *body = statement->do_stmt.body; + Ast *body = astptr(statement->do_stmt.body); bool success; // 1. Begin pushing the scope and break / continue. @@ -1697,15 +1697,15 @@ static bool sema_analyse_ct_if_stmt(SemaContext *context, Ast *statement) { return sema_analyse_then_overwrite(context, statement, elif->ct_else_stmt); } - assert(elif->ast_kind == AST_CT_ELIF_STMT); + assert(elif->ast_kind == AST_CT_IF_STMT); - res = sema_check_comp_time_bool(context, elif->ct_elif_stmt.expr); + res = sema_check_comp_time_bool(context, elif->ct_if_stmt.expr); if (res == -1) return false; if (res) { - return sema_analyse_then_overwrite(context, statement, elif->ct_elif_stmt.then); + return sema_analyse_then_overwrite(context, statement, elif->ct_if_stmt.then); } - elif = elif->ct_elif_stmt.elif; + elif = elif->ct_if_stmt.elif; } } @@ -2402,7 +2402,6 @@ static inline bool sema_analyse_statement_inner(SemaContext *context, Ast *state return sema_analyse_while_stmt(context, statement); case AST_CT_SWITCH_STMT: return sema_analyse_ct_switch_stmt(context, statement); - case AST_CT_ELIF_STMT: case AST_CT_ELSE_STMT: UNREACHABLE case AST_CT_FOREACH_STMT: