From 957ce320ae603418f6d8434afca3496c718e1f4b Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 18 Aug 2023 01:57:56 +0200 Subject: [PATCH] Cleanup and size reduction of Ast/Expr. --- src/compiler/abi/c_abi_x86.c | 4 +- src/compiler/ast.c | 1 - src/compiler/codegen_internal.h | 2 +- src/compiler/compiler_internal.h | 57 ++++++++++++----------- src/compiler/copying.c | 23 +++++---- src/compiler/enums.h | 1 + src/compiler/expr.c | 3 ++ src/compiler/libraries.c | 8 +--- src/compiler/linker.c | 2 +- src/compiler/llvm_codegen.c | 5 -- src/compiler/llvm_codegen_expr.c | 1 + src/compiler/llvm_codegen_internal.h | 13 ++---- src/compiler/llvm_codegen_internal_impl.h | 14 +----- src/compiler/llvm_codegen_module.c | 3 +- src/compiler/llvm_codegen_stmt.c | 2 +- src/compiler/llvm_codegen_type.c | 2 - src/compiler/number.c | 2 +- src/compiler/parse_expr.c | 13 +++++- src/compiler/parse_global.c | 26 +++++------ src/compiler/parse_stmt.c | 19 ++++---- src/compiler/sema_casts.c | 2 +- src/compiler/sema_decls.c | 22 ++++----- src/compiler/sema_expr.c | 21 +++++---- src/compiler/sema_internal.h | 2 +- src/compiler/sema_liveness.c | 3 +- src/compiler/sema_name_resolution.c | 2 +- src/compiler/sema_stmts.c | 37 +++++++-------- src/compiler/sema_types.c | 1 - src/compiler/semantic_analyser.c | 7 +-- src/compiler/types.c | 2 +- src/utils/common.h | 1 - src/utils/file_utils.c | 4 +- src/utils/stringutils.c | 1 - src/utils/taskqueue.c | 1 - src/utils/whereami.c | 2 - wrapper/src/wrapper.cpp | 1 - 36 files changed, 148 insertions(+), 162 deletions(-) diff --git a/src/compiler/abi/c_abi_x86.c b/src/compiler/abi/c_abi_x86.c index 2717e2512..1631d2ab9 100644 --- a/src/compiler/abi/c_abi_x86.c +++ b/src/compiler/abi/c_abi_x86.c @@ -318,7 +318,7 @@ static bool x86_try_put_primitive_in_reg(CallABI call, Regs *regs, Type *type) /** * Handle the vector/regcalls with HVAs. */ -static inline ABIArgInfo *x86_classify_homogenous_aggregate(Regs *regs, Type *type, unsigned elements, bool is_vec_call) +UNUSED static inline ABIArgInfo *x86_classify_homogenous_aggregate(Regs *regs, Type *type, unsigned elements, bool is_vec_call) { // We now know it's a float/double or a vector, // since only those are valid for x86 @@ -353,8 +353,6 @@ static inline ABIArgInfo *x86_classify_homogenous_aggregate(Regs *regs, Type *ty static inline ABIArgInfo *x86_classify_vector(Regs *regs, Type *type) { - ByteSize size = type_size(type); - // MMX passed as i64 if (x86_is_mmxtype(type)) { diff --git a/src/compiler/ast.c b/src/compiler/ast.c index 55f2ff6fb..603d51a48 100644 --- a/src/compiler/ast.c +++ b/src/compiler/ast.c @@ -54,7 +54,6 @@ Decl *decl_new_with_type(const char *name, SourceSpan loc, DeclKind decl_type) case DECL_FNTYPE: case DECL_FUNC: UNREACHABLE - break; case DECL_UNION: kind = TYPE_UNION; break; diff --git a/src/compiler/codegen_internal.h b/src/compiler/codegen_internal.h index 7153c0afc..1e06f0681 100644 --- a/src/compiler/codegen_internal.h +++ b/src/compiler/codegen_internal.h @@ -96,7 +96,7 @@ static inline bool abi_type_is_valid(AbiType type) } -static inline bool abi_type_is_promotable_integer_or_bool(AbiType type) +UNUSED static inline bool abi_type_is_promotable_integer_or_bool(AbiType type) { if (abi_type_is_type(type)) { diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index fef8084ac..9024e511a 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -769,6 +769,12 @@ typedef struct typedef struct { + union + { + ExprId function; + DeclId func_ref; + }; + ExprId macro_body; bool is_type_method : 1; bool is_pointer_call : 1; bool splat_vararg : 1; @@ -781,21 +787,20 @@ typedef struct bool is_dynamic_dispatch : 1; bool has_optional_arg : 1; bool must_use : 1; - AstId body; - union - { - ExprId function; - DeclId func_ref; - }; Expr **arguments; union { Expr **varargs; Expr *splat; }; - Decl **body_arguments; } ExprCall; +typedef struct +{ + Ast *body; + Decl **body_arguments; +} ExprMacroBody; + typedef struct { ExprId expr; @@ -1137,28 +1142,28 @@ struct Expr_ ResolveStatus resolve_status : 4; union { Range vasplat_expr; - ExprTypeidInfo typeid_info_expr; - ExprAnySwitch any_switch; // 32 + ExprTypeidInfo typeid_info_expr; // 8 + ExprAnySwitch any_switch; // 32 ExprCast cast_expr; // 12 - ExprAny any_expr; + ExprAny any_expr; // 8 ExprPointerOffset pointer_offset_expr; - ExprAsmArg expr_asm_arg; - OperatorOverload overload_expr; + ExprAsmArg expr_asm_arg; // 24 + OperatorOverload overload_expr; // 4 TypeInfo *type_expr; // 8 ExprConst const_expr; // 32 ExprGuard rethrow_expr; // 16 Decl *decl_expr; // 8 - Decl *lambda_expr; + Decl *lambda_expr; // 8 ExprSliceAssign slice_assign_expr; // 8 ExprBinary binary_expr; // 12 ExprTernary ternary_expr; // 16 ExprUnary unary_expr; // 16 Expr** try_unwrap_chain_expr; // 8 ExprTryUnwrap try_unwrap_expr; // 24 - ExprCall call_expr; // 32 + ExprCall call_expr; // 40 Expr *inner_expr; // 8 - ExprEmbedExpr embed_expr; - ExprBuiltinAccess builtin_access_expr; + ExprEmbedExpr embed_expr; // 16 + ExprBuiltinAccess builtin_access_expr; // 8 ExprGenericIdent generic_ident_expr; ExprCatchUnwrap catch_unwrap_expr; // 24 ExprSubscript subscript_expr; // 12 @@ -1183,11 +1188,12 @@ struct Expr_ Expr** cond_expr; // 8 ExprBuiltin builtin_expr; // 16 BuiltinDefine test_hook_expr; + ExprMacroBody macro_body_expr; // 16; }; }; //static_assert(sizeof(ExprConst) == 32, "Not expected size"); -//static_assert(sizeof(Expr) == 56, "Expr not expected size"); +static_assert(sizeof(Expr) == 56, "Expr not expected size"); typedef struct { @@ -1205,11 +1211,11 @@ typedef struct typedef struct { + DeclId label; bool has_break : 1; bool no_exit : 1; bool skip_first : 1; bool if_chain : 1; - Decl *label; } FlowCommon; @@ -1328,10 +1334,8 @@ typedef struct typedef struct { - const char *index_name; - const char *value_name; - SourceSpan index_span; - SourceSpan value_span; + DeclId index; + DeclId value; AstId body; ExprId expr; } AstCtForeachStmt; @@ -1423,7 +1427,6 @@ typedef struct typedef struct AstDocDirective_ { - SourceSpan span; ContractKind kind : 4; union { @@ -1478,13 +1481,14 @@ typedef struct Ast_ AstId ct_else_stmt; // 4 AstCtForeachStmt ct_foreach_stmt; // 40 AstAssertStmt assert_stmt; // 16 - AstContractStmt contract; - AstDocFault contract_fault; + AstContractStmt contract_stmt; // 32 + AstDocFault contract_fault; // 24 }; } Ast; + //static_assert(sizeof(AstContinueBreakStmt) == 24, "Ooops"); -//static_assert(sizeof(Ast) == 56, "Not expected size on 64 bit"); +static_assert(sizeof(Ast) == 48, "Not expected size on 64 bit"); typedef struct Module_ { @@ -3132,6 +3136,7 @@ const char *arch_to_linker_arch(ArchType arch); #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_TYPEID_OR_RET(_assign, _type_stmt, _res) TypeInfo* TEMP(_type) = (_type_stmt); if (!type_info_ok(TEMP(_type))) return _res; _assign = type_infoid(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) +#define ASSIGN_DECLID_OR_RET(_assign, _decl_stmt, _res) Decl* TEMP(_decl) = (_decl_stmt); if (!decl_ok(TEMP(_decl))) return _res; _assign = TEMP(_decl) ? declid(TEMP(_decl)) : 0 diff --git a/src/compiler/copying.c b/src/compiler/copying.c index 78ab87b8e..2ee13118d 100644 --- a/src/compiler/copying.c +++ b/src/compiler/copying.c @@ -79,18 +79,18 @@ Expr **copy_expr_list(CopyStruct *c, Expr **expr_list) return result; } -static inline Decl *decl_copy_label_from_macro(CopyStruct *c, Decl *to_copy, Ast *ast) +static inline DeclId decl_copy_label_from_macro(CopyStruct *c, DeclId to_copy, Ast *ast) { - if (!to_copy) return NULL; - to_copy = copy_decl(c, to_copy); - to_copy->label.parent = astid(ast); - return to_copy; + if (!to_copy) return (DeclId)0; + Decl *copy = copy_decl(c, declptr(to_copy)); + copy->label.parent = astid(ast); + return declid(copy); } static inline void copy_flow(CopyStruct *c, Ast *ast) { - ast->flow.label = decl_copy_label_from_macro(c, ast->flow.label, ast); + ast->flow.label = decl_copy_label_from_macro(c,ast->flow.label, ast); } static TypeInfo** type_info_copy_list_from_macro(CopyStruct *c, TypeInfo **to_copy) @@ -309,6 +309,10 @@ Expr *copy_expr(CopyStruct *c, Expr *source_expr) MACRO_COPY_DECL_LIST(expr->body_expansion_expr.declarations); MACRO_COPY_ASTID(expr->body_expansion_expr.first_stmt); return expr; + case EXPR_MACRO_BODY: + MACRO_COPY_AST(expr->macro_body_expr.body); + MACRO_COPY_DECL_LIST(expr->macro_body_expr.body_arguments); + return expr; case EXPR_LAMBDA: if (copy_struct.is_template) { @@ -481,8 +485,7 @@ Expr *copy_expr(CopyStruct *c, Expr *source_expr) MACRO_COPY_EXPRID(expr->call_expr.function); } - MACRO_COPY_ASTID(expr->call_expr.body); - MACRO_COPY_DECL_LIST(expr->call_expr.body_arguments); + MACRO_COPY_EXPRID(expr->call_expr.macro_body); MACRO_COPY_EXPR_LIST(expr->call_expr.arguments); if (expr->call_expr.varargs) { @@ -574,7 +577,7 @@ RETRY: } break; case AST_CONTRACT: - doc_ast_copy(c, &source->contract); + doc_ast_copy(c, &source->contract_stmt); break; case AST_ASM_BLOCK_STMT: if (ast->asm_block_stmt.is_string) @@ -623,6 +626,8 @@ RETRY: MACRO_COPY_ASTID(ast->ct_else_stmt); break; case AST_CT_FOREACH_STMT: + MACRO_COPY_DECLID(ast->ct_foreach_stmt.index); + MACRO_COPY_DECLID(ast->ct_foreach_stmt.value); MACRO_COPY_ASTID(ast->ct_foreach_stmt.body); MACRO_COPY_EXPRID(ast->ct_foreach_stmt.expr); break; diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 5a2969cdb..c79c6459f 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -267,6 +267,7 @@ typedef enum EXPR_ANY, EXPR_ANYSWITCH, EXPR_VASPLAT, + EXPR_MACRO_BODY, } ExprKind; typedef enum diff --git a/src/compiler/expr.c b/src/compiler/expr.c index b27ce8fe6..a038054f2 100644 --- a/src/compiler/expr.c +++ b/src/compiler/expr.c @@ -124,6 +124,7 @@ bool expr_may_addr(Expr *expr) case EXPR_LAMBDA: case EXPR_GENERIC_IDENT: case EXPR_EMBED: + case EXPR_MACRO_BODY: return false; } UNREACHABLE @@ -319,6 +320,7 @@ bool expr_is_constant_eval(Expr *expr, ConstantEvalKind eval_kind) case EXPR_ASM: case EXPR_SUBSCRIPT_ASSIGN: case EXPR_GENERIC_IDENT: + case EXPR_MACRO_BODY: UNREACHABLE case EXPR_NOP: return true; @@ -658,6 +660,7 @@ bool expr_is_pure(Expr *expr) case EXPR_CT_DEFINED: case EXPR_LAMBDA: case EXPR_EMBED: + case EXPR_MACRO_BODY: return true; case EXPR_VASPLAT: return true; diff --git a/src/compiler/libraries.c b/src/compiler/libraries.c index 5f9615c0a..9fe479501 100644 --- a/src/compiler/libraries.c +++ b/src/compiler/libraries.c @@ -109,11 +109,7 @@ static Library *find_library(Library **libs, size_t lib_count, const char *name) { for (size_t i = 0; i < lib_count; i++) { - if (strcmp(libs[i]->provides, name) == 0) - { - return libs[i]; - break; - } + if (str_eq(libs[i]->provides, name)) return libs[i]; } error_exit("Required library '%s' could not be found.\n", name); } @@ -170,7 +166,6 @@ static inline JSONObject *resolve_zip_library(const char *lib, const char **resu FILE *f = fopen(lib, "rb"); if (!f) error_exit("Failed to open library '%s' for reading.", lib); ZipDirIterator iterator; - const char *error; // Find the manifest. ZipFile file; @@ -221,7 +216,6 @@ void resolve_libraries(void) { file_add_wildcard_files(&c3_libs, active_target.libdirs[i], false, &c3lib_suffix, 1); } - JsonParser parser; Library *libraries[MAX_LIB_DIRS * 2]; size_t lib_count = 0; VECEACH(c3_libs, i) diff --git a/src/compiler/linker.c b/src/compiler/linker.c index ed299d7cb..4626ec586 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -20,7 +20,7 @@ extern bool llvm_link_elf(const char **args, int arg_count, const char **error_s extern bool llvm_link_macho(const char **args, int arg_count, const char **error_string); extern bool llvm_link_coff(const char **args, int arg_count, const char **error_string); extern bool llvm_link_wasm(const char **args, int arg_count, const char **error_string); -extern bool llvm_link_mingw(const char **args, int arg_count, const char **error_string); +extern UNUSED bool llvm_link_mingw(const char **args, int arg_count, const char **error_string); extern bool llvm_ar(const char *out_name, const char **args, size_t count, int ArFormat); diff --git a/src/compiler/llvm_codegen.c b/src/compiler/llvm_codegen.c index eb03d31a2..0fe9324c5 100644 --- a/src/compiler/llvm_codegen.c +++ b/src/compiler/llvm_codegen.c @@ -1471,9 +1471,6 @@ AlignSize llvm_abi_alignment(GenContext *c, LLVMTypeRef type) return (AlignSize)LLVMABIAlignmentOfType(c->target_data, type); } - - - LLVMValueRef llvm_emit_memcpy(GenContext *c, LLVMValueRef dest, unsigned dest_align, LLVMValueRef source, unsigned src_align, uint64_t len) { assert(dest_align && src_align); @@ -1491,8 +1488,6 @@ void llvm_emit_memcpy_to_decl(GenContext *c, Decl *decl, LLVMValueRef source, un llvm_emit_memcpy(c, decl->backend_ref, decl->alignment, source, source_alignment, type_size(decl->type)); } - - TypeSize llvm_store_size(GenContext *c, LLVMTypeRef type) { return (TypeSize)LLVMStoreSizeOfType(c->target_data, type); diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 847e64fb9..f17254fef 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -6631,6 +6631,7 @@ void llvm_emit_expr(GenContext *c, BEValue *value, Expr *expr) case EXPR_VASPLAT: case EXPR_GENERIC_IDENT: case EXPR_EMBED: + case EXPR_MACRO_BODY: UNREACHABLE case EXPR_LAMBDA: llvm_emit_lambda(c, value, expr); diff --git a/src/compiler/llvm_codegen_internal.h b/src/compiler/llvm_codegen_internal.h index 6b08e9b69..c6753a16b 100644 --- a/src/compiler/llvm_codegen_internal.h +++ b/src/compiler/llvm_codegen_internal.h @@ -123,7 +123,6 @@ typedef struct GenContext_ LLVMTypeRef size_type; LLVMTypeRef typeid_type; LLVMTypeRef dtable_type; - LLVMTypeRef char_ptr_type; LLVMTypeRef ptr_type; LLVMTypeRef chars_type; Decl *panic_var; @@ -137,16 +136,12 @@ typedef struct GenContext_ int block_global_unique_count; int ast_alloca_addr_space; BreakContinue return_block; - int simple_return_expressions; - unsigned pointer_alignment; - int return_expressions; DebugContext debug; Module *code_module; LLVMValueRef return_out; LLVMValueRef optional_out; BEValue retval; bool current_block_is_target : 1; - LLVMTypeRef type_data_definitions[TYPE_KINDS]; SourceSpan last_emitted_loc; Decl **dynamic_functions; LLVMValueRef dyn_find_function; @@ -363,7 +358,7 @@ INLINE void llvm_set_alignment(LLVMValueRef alloca, AlignSize alignment); INLINE AlignSize llvm_type_or_alloca_align(LLVMValueRef dest, Type *type); INLINE LLVMValueRef llvm_zext_trunc(GenContext *c, LLVMValueRef data, LLVMTypeRef type); -INLINE LLVMValueRef llvm_sext_trunc(GenContext *c, LLVMValueRef data, LLVMTypeRef type); +INLINE UNUSED LLVMValueRef llvm_sext_trunc(GenContext *c, LLVMValueRef data, LLVMTypeRef type); INLINE void llvm_value_ext_trunc(GenContext *c, BEValue *value, Type *type); // -- Constants -- @@ -372,7 +367,7 @@ LLVMValueRef llvm_emit_const_initializer(GenContext *c, ConstInitializer *const_ LLVMValueRef llvm_emit_const_padding(GenContext *c, AlignSize size); LLVMValueRef llvm_emit_string_const(GenContext *c, const char *str, const char *extname); LLVMValueRef llvm_emit_empty_string_const(GenContext *c); -LLVMValueRef llvm_emit_zstring(GenContext *c, const char *str); +UNUSED LLVMValueRef llvm_emit_zstring(GenContext *c, const char *str); LLVMValueRef llvm_emit_zstring_named(GenContext *c, const char *str, const char *extname); INLINE LLVMValueRef llvm_const_int(GenContext *c, Type *type, uint64_t val); INLINE LLVMValueRef llvm_get_zero(GenContext *c, Type *type); @@ -455,7 +450,7 @@ LLVMValueRef llvm_emit_ashr_fixed(GenContext *c, LLVMValueRef data, int shift); INLINE LLVMValueRef llvm_emit_ashr(GenContext *c, LLVMValueRef value, LLVMValueRef shift); INLINE LLVMValueRef llvm_emit_shl(GenContext *c, LLVMValueRef value, LLVMValueRef shift); INLINE LLVMValueRef llvm_emit_lshr(GenContext *c, LLVMValueRef value, LLVMValueRef shift); -INLINE LLVMValueRef llvm_emit_trunc(GenContext *c, LLVMValueRef value, Type *type); +INLINE UNUSED LLVMValueRef llvm_emit_trunc(GenContext *c, LLVMValueRef value, Type *type); INLINE LLVMValueRef llvm_emit_trunc_bool(GenContext *c, LLVMValueRef value); INLINE LLVMValueRef llvm_emit_and(GenContext *c, BEValue *lhs, BEValue *rhs); INLINE LLVMValueRef llvm_emit_and_raw(GenContext *c, LLVMValueRef lhs, LLVMValueRef rhs); @@ -472,7 +467,7 @@ LLVMValueRef llvm_get_next_param(GenContext *context, unsigned *index); void llvm_emit_convert_value_from_coerced(GenContext *c, BEValue *result, LLVMTypeRef coerced, LLVMValueRef value, Type *original_type); void llvm_emit_coerce_store(GenContext *c, LLVMValueRef addr, AlignSize alignment, LLVMTypeRef coerced, LLVMValueRef value, LLVMTypeRef target_type); LLVMValueRef llvm_emit_coerce(GenContext *c, LLVMTypeRef coerced, BEValue *value, Type *original_type); -INLINE bool call_supports_variadic(CallABI abi); + static inline LLVMCallConv llvm_call_convention_from_call(CallABI abi); void llvm_emit_raw_call(GenContext *c, BEValue *result_value, FunctionPrototype *prototype, LLVMTypeRef func_type, LLVMValueRef func, LLVMValueRef *args, unsigned arg_count, int inline_flag, LLVMValueRef error_var, bool sret_return, BEValue *synthetic_return_param); void llvm_emit_parameter(GenContext *c, LLVMValueRef *args, unsigned *arg_count_ref, ABIArgInfo *info, BEValue *be_value, Type *type); diff --git a/src/compiler/llvm_codegen_internal_impl.h b/src/compiler/llvm_codegen_internal_impl.h index d42612151..7ae5c416c 100644 --- a/src/compiler/llvm_codegen_internal_impl.h +++ b/src/compiler/llvm_codegen_internal_impl.h @@ -120,7 +120,7 @@ INLINE LLVMValueRef llvm_store_to_ptr_raw(GenContext *c, LLVMValueRef pointer, L return llvm_store_to_ptr_raw_aligned(c, pointer, value, llvm_type_or_alloca_align(pointer, type)); } -INLINE void llvm_value_bitcast(GenContext *c, BEValue *value, Type *type) +INLINE void llvm_value_bitcast(GenContext *c UNUSED, BEValue *value, Type *type) { assert(llvm_value_is_addr(value)); type = type_lowering(type); @@ -186,18 +186,6 @@ static inline LLVMBasicBlockRef llvm_get_current_block_if_in_use(GenContext *con return block; } -INLINE bool call_supports_variadic(CallABI abi) -{ - switch (abi) - { - case CALL_X64_VECTOR: - return false; - default: - return true; - - } -} - static inline LLVMCallConv llvm_call_convention_from_call(CallABI abi) { switch (abi) diff --git a/src/compiler/llvm_codegen_module.c b/src/compiler/llvm_codegen_module.c index a5cfb2bea..bb88bac56 100644 --- a/src/compiler/llvm_codegen_module.c +++ b/src/compiler/llvm_codegen_module.c @@ -72,7 +72,6 @@ void gencontext_begin_module(GenContext *c) static const char *pic_level = "PIC Level"; static const char *pie_level = "PIE Level"; - LLVMMetadataRef setting; switch (active_target.reloc_model) { case RELOC_BIG_PIE: @@ -222,7 +221,7 @@ void gencontext_init_file_emit(GenContext *c, CompilationUnit *unit) } -void gencontext_end_file_emit(GenContext *c, CompilationUnit *ast) +void gencontext_end_file_emit(GenContext *c UNUSED, CompilationUnit *ast UNUSED) { } diff --git a/src/compiler/llvm_codegen_stmt.c b/src/compiler/llvm_codegen_stmt.c index d05eeeb57..f891e137b 100644 --- a/src/compiler/llvm_codegen_stmt.c +++ b/src/compiler/llvm_codegen_stmt.c @@ -313,7 +313,7 @@ static void llvm_emit_if_stmt(GenContext *c, Ast *ast) // Output boolean value and switch. - Decl *label = ast->if_stmt.flow.label; + Decl *label = declptrzero(ast->if_stmt.flow.label); if (label) { label->label.break_target = exit_block; diff --git a/src/compiler/llvm_codegen_type.c b/src/compiler/llvm_codegen_type.c index f9c92868c..d39a28c57 100644 --- a/src/compiler/llvm_codegen_type.c +++ b/src/compiler/llvm_codegen_type.c @@ -506,7 +506,6 @@ static LLVMValueRef llvm_get_introspection_for_enum(GenContext *c, Type *type) bool obfuscate = decl->obfuscate; for (unsigned i = 0; i < elements; i++) { - BEValue value; const char *name = enum_vals[i]->name; scratch_buffer_clear(); scratch_buffer_append(".enum."); @@ -587,7 +586,6 @@ static LLVMValueRef llvm_get_introspection_for_fault(GenContext *c, Type *type) Decl **fault_vals = decl->enums.values; unsigned elements = vec_size(fault_vals); LLVMValueRef ref = llvm_generate_temp_introspection_global(c, type); - AlignSize store_align; for (unsigned i = 0; i < elements; i++) { scratch_buffer_clear(); diff --git a/src/compiler/number.c b/src/compiler/number.c index b1726984b..44f301bbb 100644 --- a/src/compiler/number.c +++ b/src/compiler/number.c @@ -92,7 +92,7 @@ bool expr_const_compare(const ExprConst *left, const ExprConst *right, BinaryOp // The error case assert(right->const_kind == left->const_kind); Decl *right_decl = right->enum_err_val; - // Non matching cannot be compared. + // Non-matching cannot be compared. if (right_decl->type != left_decl->type) return false; int64_t right_ordinal = right->enum_err_val->enum_constant.ordinal; switch (op) diff --git a/src/compiler/parse_expr.c b/src/compiler/parse_expr.c index 5d17b45ff..a6071c618 100644 --- a/src/compiler/parse_expr.c +++ b/src/compiler/parse_expr.c @@ -880,7 +880,6 @@ static Expr *parse_call_expr(ParseContext *c, Expr *left) call->call_expr.function = exprid(left); call->call_expr.arguments = params; call->call_expr.splat_vararg = splat; - call->call_expr.body_arguments = body_args; RANGE_EXTEND_PREV(call); if (body_args && !tok_is(c, TOKEN_LBRACE)) { @@ -930,10 +929,20 @@ static Expr *parse_call_expr(ParseContext *c, Expr *left) call->call_expr.attr_force_inline = force_inline == 1; call->call_expr.attr_force_noinline = force_inline == 0; } + Ast *body = NULL; if (tok_is(c, TOKEN_LBRACE)) { - ASSIGN_ASTID_OR_RET(call->call_expr.body, parse_compound_stmt(c), poisoned_expr); + ASSIGN_AST_OR_RET(body, parse_compound_stmt(c), poisoned_expr); } + + if (body || body_args) + { + Expr *macro_body = expr_new(EXPR_MACRO_BODY, call->span); + macro_body->macro_body_expr.body = body; + macro_body->macro_body_expr.body_arguments = body_args; + call->call_expr.macro_body = exprid(macro_body); + } + return call; } diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 6ca31883d..0162f79f8 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -246,7 +246,7 @@ bool parse_module(ParseContext *c, AstId contracts) Ast *current = astptr(contracts); contracts = current->next; assert(current->ast_kind == AST_CONTRACT); - switch (current->contract.kind) + switch (current->contract_stmt.kind) { case CONTRACT_UNKNOWN: case CONTRACT_PURE: @@ -2385,10 +2385,10 @@ INLINE void append_docs(AstId **next, AstId *first, Ast *new_doc) static inline bool parse_doc_contract(ParseContext *c, AstId *docs, AstId **docs_next, ContractKind kind) { Ast *ast = ast_new_curr(c, AST_CONTRACT); - ast->contract.kind = kind; + ast->contract_stmt.kind = kind; const char *start = c->lexer.data.lex_start; advance(c); - ASSIGN_EXPR_OR_RET(ast->contract.contract.decl_exprs, parse_expression_list(c, kind == CONTRACT_CHECKED), false); + ASSIGN_EXPR_OR_RET(ast->contract_stmt.contract.decl_exprs, parse_expression_list(c, kind == CONTRACT_CHECKED), false); const char *end = start + 1; while (end[0] != '\n' && end[0] != '\0') end++; if (end > c->data.lex_start) end = c->data.lex_start; @@ -2421,13 +2421,13 @@ static inline bool parse_doc_contract(ParseContext *c, AstId *docs, AstId **docs scratch_buffer_append(": '"); scratch_buffer_append(symstr(c)); scratch_buffer_append("'."); - ast->contract.contract.comment = scratch_buffer_copy(); + ast->contract_stmt.contract.comment = scratch_buffer_copy(); advance(c); } else { scratch_buffer_append("."); - ast->contract.contract.expr_string = scratch_buffer_copy(); + ast->contract_stmt.contract.expr_string = scratch_buffer_copy(); } append_docs(docs_next, docs, ast); return true; @@ -2440,7 +2440,7 @@ static inline bool parse_doc_contract(ParseContext *c, AstId *docs, AstId **docs static inline bool parse_contract_param(ParseContext *c, AstId *docs, AstId **docs_next) { Ast *ast = ast_new_curr(c, AST_CONTRACT); - ast->contract.kind = CONTRACT_PARAM; + ast->contract_stmt.kind = CONTRACT_PARAM; advance(c); // [inout] [in] [out] @@ -2486,10 +2486,10 @@ static inline bool parse_contract_param(ParseContext *c, AstId *docs, AstId **do SEMA_ERROR_HERE("Expected a parameter name here."); return false; } - ast->contract.param.name = symstr(c); - ast->contract.param.span = c->span; - ast->contract.param.modifier = mod; - ast->contract.param.by_ref = is_ref; + ast->contract_stmt.param.name = symstr(c); + ast->contract_stmt.param.span = c->span; + ast->contract_stmt.param.modifier = mod; + ast->contract_stmt.param.by_ref = is_ref; advance(c); if (try_consume(c, TOKEN_COLON)) { @@ -2509,7 +2509,7 @@ static inline bool parse_doc_optreturn(ParseContext *c, AstId *docs, AstId **doc Ast *ast = ast_new_curr(c, AST_CONTRACT); ast->span = c->prev_span; advance_and_verify(c, TOKEN_BANG); - ast->contract.kind = CONTRACT_OPTIONALS; + ast->contract_stmt.kind = CONTRACT_OPTIONALS; while (1) { Ast *ret = ast_new_curr(c, AST_CONTRACT_FAULT); @@ -2531,7 +2531,7 @@ static inline bool parse_doc_optreturn(ParseContext *c, AstId *docs, AstId **doc RANGE_EXTEND_PREV(ast); // Just ignore our potential string: (void)try_consume(c, TOKEN_STRING); - ast->contract.faults = returns; + ast->contract_stmt.faults = returns; append_docs(docs_next, docs, ast); return true; } @@ -2594,7 +2594,7 @@ static bool parse_contracts(ParseContext *c, AstId *contracts_ref) else if (name == kw_at_pure) { Ast *ast = ast_new_curr(c, AST_CONTRACT); - ast->contract.kind = CONTRACT_PURE; + ast->contract_stmt.kind = CONTRACT_PURE; append_docs(next, contracts_ref, ast); advance(c); break; diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index df071ebae..20e307146 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -425,7 +425,7 @@ static inline Ast* parse_do_stmt(ParseContext *c) advance_and_verify(c, TOKEN_DO); do_ast->flow.skip_first = true; - ASSIGN_DECL_OR_RET(do_ast->for_stmt.flow.label, parse_optional_label(c, do_ast), poisoned_ast); + ASSIGN_DECLID_OR_RET(do_ast->for_stmt.flow.label, parse_optional_label(c, do_ast), poisoned_ast); ASSIGN_ASTID_OR_RET(do_ast->for_stmt.body, parse_stmt(c), poisoned_ast); if (try_consume(c, TOKEN_EOS)) @@ -492,7 +492,7 @@ static inline Ast* parse_while_stmt(ParseContext *c) Ast *while_ast = new_ast(AST_FOR_STMT, c->span); advance_and_verify(c, TOKEN_WHILE); - ASSIGN_DECL_OR_RET(while_ast->for_stmt.flow.label, parse_optional_label(c, while_ast), poisoned_ast); + ASSIGN_DECLID_OR_RET(while_ast->for_stmt.flow.label, parse_optional_label(c, while_ast), poisoned_ast); CONSUME_OR_RET(TOKEN_LPAREN, poisoned_ast); ASSIGN_EXPRID_OR_RET(while_ast->for_stmt.cond, parse_cond(c), poisoned_ast); CONSUME_OR_RET(TOKEN_RPAREN, poisoned_ast); @@ -518,7 +518,7 @@ static inline Ast* parse_if_stmt(ParseContext *c) { Ast *if_ast = new_ast(AST_IF_STMT, c->span); advance_and_verify(c, TOKEN_IF); - ASSIGN_DECL_OR_RET(if_ast->if_stmt.flow.label, parse_optional_label(c, if_ast), poisoned_ast); + ASSIGN_DECLID_OR_RET(if_ast->if_stmt.flow.label, parse_optional_label(c, if_ast), poisoned_ast); CONSUME_OR_RET(TOKEN_LPAREN, poisoned_ast); ASSIGN_EXPRID_OR_RET(if_ast->if_stmt.cond, parse_cond(c), poisoned_ast); unsigned row = c->span.row; @@ -640,7 +640,7 @@ static inline Ast* parse_switch_stmt(ParseContext *c) { Ast *switch_ast = new_ast(AST_SWITCH_STMT, c->span); advance_and_verify(c, TOKEN_SWITCH); - ASSIGN_DECL_OR_RET(switch_ast->switch_stmt.flow.label, parse_optional_label(c, switch_ast), poisoned_ast); + ASSIGN_DECLID_OR_RET(switch_ast->switch_stmt.flow.label, parse_optional_label(c, switch_ast), poisoned_ast); if (!try_consume(c, TOKEN_LPAREN)) { switch_ast->switch_stmt.cond = 0; @@ -665,7 +665,7 @@ static inline Ast* parse_for_stmt(ParseContext *c) advance_and_verify(c, TOKEN_FOR); // Label - ASSIGN_DECL_OR_RET(ast->for_stmt.flow.label, parse_optional_label(c, ast), poisoned_ast); + ASSIGN_DECLID_OR_RET(ast->for_stmt.flow.label, parse_optional_label(c, ast), poisoned_ast); CONSUME_OR_RET(TOKEN_LPAREN, poisoned_ast); if (try_consume(c, TOKEN_EOS)) @@ -747,7 +747,7 @@ static inline Ast* parse_foreach_stmt(ParseContext *c) advance_and_verify(c, TOKEN_FOREACH); } - ASSIGN_DECL_OR_RET(ast->foreach_stmt.flow.label, parse_optional_label(c, ast), poisoned_ast); + ASSIGN_DECLID_OR_RET(ast->foreach_stmt.flow.label, parse_optional_label(c, ast), poisoned_ast); CONSUME_OR_RET(TOKEN_LPAREN, poisoned_ast); // Parse the first variable. @@ -963,13 +963,12 @@ static inline Ast* parse_ct_foreach_stmt(ParseContext *c) CONSUME_OR_RET(TOKEN_LPAREN, poisoned_ast); if (peek(c) == TOKEN_COMMA) { - ast->ct_foreach_stmt.index_name = symstr(c); - ast->ct_foreach_stmt.index_span = c->span; + Decl *index = decl_new_var(symstr(c), c->span, NULL, VARDECL_LOCAL_CT); + ast->ct_foreach_stmt.index = declid(index); TRY_CONSUME_OR_RET(TOKEN_CT_IDENT, "Expected a compile time index variable", poisoned_ast); advance_and_verify(c, TOKEN_COMMA); } - ast->ct_foreach_stmt.value_name = symstr(c); - ast->ct_foreach_stmt.value_span = c->span; + ast->ct_foreach_stmt.value = declid(decl_new_var(symstr(c), c->span, NULL, VARDECL_LOCAL_CT)); TRY_CONSUME_OR_RET(TOKEN_CT_IDENT, "Expected a compile time variable", poisoned_ast); TRY_CONSUME_OR_RET(TOKEN_COLON, "Expected ':'.", poisoned_ast); ASSIGN_EXPRID_OR_RET(ast->ct_foreach_stmt.expr, parse_expr(c), poisoned_ast); diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index c3d712e97..1b30ac055 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -1177,7 +1177,7 @@ static bool cast_from_array(SemaContext *context, Expr *expr, Type *from, Type * { case TYPE_INFERRED_ARRAY: case TYPE_INFERRED_VECTOR: - // We infer the target type (note that to and to_type are now of different kinds. + // We infer the target type (note that to and to_type are now of different kinds). to_type = type_infer_len_from_actual_type(to_type, from); break; case TYPE_VECTOR: diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index 2ff9b441e..bc82ce510 100644 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -2088,7 +2088,7 @@ static inline bool sema_analyse_doc_header(AstId doc, Decl **params, Decl **extr { Ast *directive = astptr(doc); doc = directive->next; - ContractKind directive_kind = directive->contract.kind; + ContractKind directive_kind = directive->contract_stmt.kind; if (directive_kind == CONTRACT_PURE) { if (*pure_ref) @@ -2100,7 +2100,7 @@ static inline bool sema_analyse_doc_header(AstId doc, Decl **params, Decl **extr continue; } if (directive_kind != CONTRACT_PARAM) continue; - const char *param_name = directive->contract.param.name; + const char *param_name = directive->contract_stmt.param.name; Decl *extra_param = NULL; Decl *param = NULL; VECEACH(params, j) @@ -2113,13 +2113,13 @@ static inline bool sema_analyse_doc_header(AstId doc, Decl **params, Decl **extr param = extra_params[j]; if (param->name == param_name) goto NEXT; } - SEMA_ERROR(&directive->contract.param, "There is no parameter '%s', did you misspell it?", param_name); + SEMA_ERROR(&directive->contract_stmt.param, "There is no parameter '%s', did you misspell it?", param_name); return false; NEXT:; Type *type = param->type; if (type) type = type_flatten(type); bool may_be_pointer = !type || type_is_pointer(type); - if (directive->contract.param.by_ref) + if (directive->contract_stmt.param.by_ref) { if (!may_be_pointer) { @@ -2128,7 +2128,7 @@ static inline bool sema_analyse_doc_header(AstId doc, Decl **params, Decl **extr } param->var.not_null = true; } - switch (directive->contract.param.modifier) + switch (directive->contract_stmt.param.modifier) { case PARAM_ANY: goto ADDED; @@ -3219,27 +3219,27 @@ static bool sema_analyse_generic_module_contracts(SemaContext *c, Module *module assert(ast->ast_kind == AST_CONTRACT); SemaContext temp_context; - assert(ast->contract.kind == CONTRACT_CHECKED || ast->contract.kind == CONTRACT_REQUIRE); + assert(ast->contract_stmt.kind == CONTRACT_CHECKED || ast->contract_stmt.kind == CONTRACT_REQUIRE); SemaContext *new_context = context_transform_for_eval(c, &temp_context, module->units[0]); - if (ast->contract.kind == CONTRACT_CHECKED) + if (ast->contract_stmt.kind == CONTRACT_CHECKED) { if (!sema_analyse_checked(new_context, ast, error_span)) return false; } else { - FOREACH_BEGIN(Expr *expr, ast->contract.contract.decl_exprs->expression_list) + FOREACH_BEGIN(Expr *expr, ast->contract_stmt.contract.decl_exprs->expression_list) int res = sema_check_comp_time_bool(new_context, expr); if (res == -1) return false; if (res) continue; - if (ast->contract.contract.comment) + if (ast->contract_stmt.contract.comment) { sema_error_at(error_span, "Parameter(s) would violate constraint: %s.", - ast->contract.contract.comment); + ast->contract_stmt.contract.comment); } else { - sema_error_at(error_span, "Parameter(s) failed validation: %s", ast->contract.contract.expr_string); + sema_error_at(error_span, "Parameter(s) failed validation: %s", ast->contract_stmt.contract.expr_string); } return false; FOREACH_END(); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 43ad781fc..0176bd619 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -515,6 +515,7 @@ static bool sema_binary_is_expr_lvalue(Expr *top_expr, Expr *expr) case EXPR_VASPLAT: case EXPR_TEST_HOOK: case EXPR_GENERIC_IDENT: + case EXPR_MACRO_BODY: goto ERR; } UNREACHABLE @@ -627,6 +628,7 @@ static bool expr_may_ref(Expr *expr) case EXPR_VASPLAT: case EXPR_TEST_HOOK: case EXPR_GENERIC_IDENT: + case EXPR_MACRO_BODY: return false; } UNREACHABLE @@ -1125,7 +1127,8 @@ static inline int sema_call_find_index_of_named_parameter(SemaContext *context, static inline bool sema_call_check_invalid_body_arguments(SemaContext *context, Expr *call, CalledDecl *callee) { - Decl **body_arguments = call->call_expr.body_arguments; + Expr *macro_body = exprptrzero(call->call_expr.macro_body); + Decl **body_arguments = macro_body ? macro_body->macro_body_expr.body_arguments : NULL; bool has_body_arguments = vec_size(body_arguments) > 0; // 1. Check if there are body arguments but no actual block. @@ -1143,7 +1146,7 @@ static inline bool sema_call_check_invalid_body_arguments(SemaContext *context, } // 2. If there is a body then... - if (call->call_expr.body) + if (macro_body && macro_body->macro_body_expr.body) { // 2a. If not a macro then this is an error. if (!callee->macro) @@ -1170,7 +1173,7 @@ static inline bool sema_call_check_invalid_body_arguments(SemaContext *context, return false; } - // 4. No body and no block parameter, this is fine. + // 4. No "body" and no block parameter, this is fine. return true; } @@ -1841,7 +1844,8 @@ bool sema_expr_analyse_macro_call(SemaContext *context, Expr *call_expr, Expr *s has_optional_arg = has_optional_arg || IS_OPTIONAL(args[i]); } - Decl **body_params = call_expr->call_expr.body_arguments; + Expr *macro_body = exprptrzero(call_expr->call_expr.macro_body); + Decl **body_params = macro_body ? macro_body->macro_body_expr.body_arguments : NULL; unsigned body_params_count = vec_size(body_params); Decl **macro_body_params = decl->func_decl.body_param ? declptr(decl->func_decl.body_param)->body_params : NULL; unsigned expected_body_params = vec_size(macro_body_params); @@ -1859,7 +1863,7 @@ bool sema_expr_analyse_macro_call(SemaContext *context, Expr *call_expr, Expr *s { Decl *body_param = macro_body_params[i]; assert(body_param->resolve_status == RESOLVE_DONE); - Decl *body_arg = call_expr->call_expr.body_arguments[i]; + Decl *body_arg = body_params[i]; if (!body_arg->var.type_info) { SEMA_ERROR(body_arg, "Expected a type parameter before this variable name."); @@ -1907,8 +1911,7 @@ bool sema_expr_analyse_macro_call(SemaContext *context, Expr *call_expr, Expr *s macro_context.inlining_span = context->current_macro ? context->inlining_span : call_expr->span; macro_context.current_macro = decl; - AstId body_id = call_expr->call_expr.body; - macro_context.yield_body = body_id ? astptr(body_id) : NULL; + macro_context.yield_body = macro_body ? macro_body->macro_body_expr.body : NULL; macro_context.yield_params = body_params; macro_context.yield_context = context; macro_context.macro_varargs = call_expr->call_expr.varargs; @@ -2073,7 +2076,8 @@ static bool sema_call_analyse_body_expansion(SemaContext *macro_context, Expr *c assert(body_param); ExprCall *call_expr = &call->call_expr; - if (vec_size(call_expr->body_arguments)) + Expr *macro_body = exprptrzero(call_expr->macro_body); + if (macro_body && vec_size(macro_body->macro_body_expr.body_arguments)) { SEMA_ERROR(call, "Nested expansion is not possible."); return false; @@ -7696,6 +7700,7 @@ static inline bool sema_analyse_expr_dispatch(SemaContext *context, Expr *expr) case EXPR_OPERATOR_CHARS: case EXPR_TEST_HOOK: case EXPR_SWIZZLE: + case EXPR_MACRO_BODY: UNREACHABLE case EXPR_EMBED: return sema_expr_analyse_embed(context, expr, false); diff --git a/src/compiler/sema_internal.h b/src/compiler/sema_internal.h index 98d98bc72..74143e064 100644 --- a/src/compiler/sema_internal.h +++ b/src/compiler/sema_internal.h @@ -34,7 +34,7 @@ void generic_context_release_locals_list(Decl **); AstId context_get_defers(SemaContext *context, AstId defer_top, AstId defer_bottom, bool is_success); void context_pop_defers(SemaContext *context, AstId *next); void context_pop_defers_and_replace_ast(SemaContext *context, Ast *ast); -void context_change_scope_for_label(SemaContext *context, Decl *label); +void context_change_scope_for_label(SemaContext *context, DeclId label); void context_change_scope_with_flags(SemaContext *context, ScopeFlags flags); SemaContext *context_transform_for_eval(SemaContext *context, SemaContext *temp_context, CompilationUnit *eval_unit); diff --git a/src/compiler/sema_liveness.c b/src/compiler/sema_liveness.c index 14a7ebed7..c17612b5a 100644 --- a/src/compiler/sema_liveness.c +++ b/src/compiler/sema_liveness.c @@ -196,7 +196,6 @@ static void sema_trace_const_initializer_liveness(ConstInitializer *const_init) goto RETRY; case CONST_INIT_ARRAY_FULL: { - bool was_modified = false; Type *array_type = const_init->type; ConstInitializer **elements = const_init->init_array_full; ArraySize size = array_type->array.len; @@ -259,6 +258,7 @@ RETRY: case EXPR_ANYSWITCH: case EXPR_GENERIC_IDENT: case EXPR_EMBED: + case EXPR_MACRO_BODY: UNREACHABLE case EXPR_DESIGNATOR: sema_trace_expr_liveness(expr->designator_expr.value); @@ -569,7 +569,6 @@ RETRY: case DECL_BODYPARAM: case DECL_GLOBALS: UNREACHABLE - return; case DECL_FNTYPE:; sema_trace_func_liveness(&decl->fntype_decl); return; diff --git a/src/compiler/sema_name_resolution.c b/src/compiler/sema_name_resolution.c index 6888e8aff..797858994 100644 --- a/src/compiler/sema_name_resolution.c +++ b/src/compiler/sema_name_resolution.c @@ -153,7 +153,7 @@ static Decl *sema_find_decl_in_imports(Decl **imports, NameResolve *name_resolve { if (!path) { - // Prefer already found builtin over new found no builtin + // Prefer already found builtin over newly found non-builtin if (decl->is_autoimport && !found->is_autoimport) continue; // Prefer new builtin over non-builtin if (found->is_autoimport && !decl->is_autoimport) diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index f04122b3a..113d7e9cc 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -272,7 +272,7 @@ static void sema_unwrappable_from_catch_in_else(SemaContext *c, Expr *cond) static inline bool assert_create_from_contract(SemaContext *context, Ast *directive, AstId **asserts, SourceSpan evaluation_location) { - Expr *declexpr = directive->contract.contract.decl_exprs; + Expr *declexpr = directive->contract_stmt.contract.decl_exprs; assert(declexpr->expr_kind == EXPR_EXPRESSION_LIST); Expr **exprs = declexpr->expression_list; @@ -286,8 +286,8 @@ static inline bool assert_create_from_contract(SemaContext *context, Ast *direct } if (!sema_analyse_cond_expr(context, expr)) return false; - const char *comment = directive->contract.contract.comment; - if (!comment) comment = directive->contract.contract.expr_string; + const char *comment = directive->contract_stmt.contract.comment; + if (!comment) comment = directive->contract_stmt.contract.expr_string; if (expr_is_const(expr)) { assert(expr->const_expr.const_kind == CONST_BOOL); @@ -500,7 +500,7 @@ static inline bool sema_analyse_return_stmt(SemaContext *context, Ast *statement while (doc_directive) { Ast *directive = astptr(doc_directive); - if (directive->contract.kind == CONTRACT_ENSURE) + if (directive->contract_stmt.kind == CONTRACT_ENSURE) { if (!assert_create_from_contract(context, directive, &append_id, statement->span)) return false; } @@ -1641,11 +1641,12 @@ static inline bool sema_analyse_if_stmt(SemaContext *context, Ast *statement) if (then->ast_kind == AST_IF_CATCH_SWITCH_STMT) { - Decl *label = statement->if_stmt.flow.label; - then->switch_stmt.flow.label = label; - statement->if_stmt.flow.label = NULL; + DeclId label_id = statement->if_stmt.flow.label; + then->switch_stmt.flow.label = label_id; + statement->if_stmt.flow.label = 0; + Decl *label = declptrzero(label_id); if (label) label->label.parent = astid(then); - SCOPE_START_WITH_LABEL(statement->if_stmt.flow.label); + SCOPE_START_WITH_LABEL(label_id); success = success && sema_analyse_switch_stmt(context, then); then_jump = context->active_scope.jump_end; SCOPE_END; @@ -2460,17 +2461,15 @@ static inline bool sema_analyse_ct_foreach_stmt(SemaContext *context, Ast *state expressions = collection->const_expr.untyped_list; count = vec_size(expressions); } - Decl *index = NULL; - const char *index_name = statement->ct_foreach_stmt.index_name; + Decl *index = declptrzero(statement->ct_foreach_stmt.index); AstId start = 0; - if (index_name) + if (index) { - index = decl_new_var(index_name, statement->ct_foreach_stmt.index_span, NULL, VARDECL_LOCAL_CT); index->type = type_int; if (!sema_add_local(context, index)) goto FAILED; } - Decl *value = decl_new_var(statement->ct_foreach_stmt.value_name, statement->ct_foreach_stmt.value_span, NULL, VARDECL_LOCAL_CT); + Decl *value = declptr(statement->ct_foreach_stmt.value); if (!sema_add_local(context, value)) goto FAILED; // Get the body Ast *body = astptr(statement->ct_foreach_stmt.body); @@ -2849,7 +2848,7 @@ static bool sema_analyse_require(SemaContext *context, Ast *directive, AstId **a static bool sema_analyse_ensure(SemaContext *context, Ast *directive) { - Expr *declexpr = directive->contract.contract.decl_exprs; + Expr *declexpr = directive->contract_stmt.contract.decl_exprs; assert(declexpr->expr_kind == EXPR_EXPRESSION_LIST); VECEACH(declexpr->expression_list, j) @@ -2868,7 +2867,7 @@ static bool sema_analyse_optional_returns(SemaContext *context, Ast *directive) { Ast **returns = NULL; context->call_env.opt_returns = NULL; - FOREACH_BEGIN(Ast *ret, directive->contract.faults) + FOREACH_BEGIN(Ast *ret, directive->contract_stmt.faults) if (ret->contract_fault.resolved) continue; TypeInfo *type_info = ret->contract_fault.type; const char *ident = ret->contract_fault.ident; @@ -2903,7 +2902,7 @@ NEXT:; bool sema_analyse_checked(SemaContext *context, Ast *directive, SourceSpan span) { - Expr *declexpr = directive->contract.contract.decl_exprs; + Expr *declexpr = directive->contract_stmt.contract.decl_exprs; bool success = true; bool suppress_error = global_context.suppress_errors; global_context.suppress_errors = true; @@ -2914,10 +2913,10 @@ bool sema_analyse_checked(SemaContext *context, Ast *directive, SourceSpan span) { Expr *expr = declexpr->cond_expr[j]; if (sema_analyse_expr(context, expr)) continue; - const char *comment = directive->contract.contract.comment; + const char *comment = directive->contract_stmt.contract.comment; global_context.suppress_errors = suppress_error; sema_error_at(span.row == 0 ? expr->span : span, "Contraint failed: %s", - comment ? comment : directive->contract.contract.expr_string); + comment ? comment : directive->contract_stmt.contract.expr_string); success = false; goto END; } @@ -2942,7 +2941,7 @@ bool sema_analyse_contracts(SemaContext *context, AstId doc, AstId **asserts, So while (doc) { Ast *directive = astptr(doc); - switch (directive->contract.kind) + switch (directive->contract_stmt.kind) { case CONTRACT_UNKNOWN: case CONTRACT_PURE: diff --git a/src/compiler/sema_types.c b/src/compiler/sema_types.c index 6dc6367af..ad5077752 100644 --- a/src/compiler/sema_types.c +++ b/src/compiler/sema_types.c @@ -295,7 +295,6 @@ static bool sema_resolve_type_identifier(SemaContext *context, TypeInfo *type_in INLINE bool sema_resolve_evaltype(SemaContext *context, TypeInfo *type_info, bool is_pointee) { Expr *expr = type_info->unresolved_type_expr; - TokenType type; Expr *inner = sema_ct_eval_expr(context, "$evaltype", expr, true); if (!inner) return type_info_poison(type_info); if (inner->expr_kind != EXPR_TYPEINFO) diff --git a/src/compiler/semantic_analyser.c b/src/compiler/semantic_analyser.c index db236c00c..2f614cdfe 100644 --- a/src/compiler/semantic_analyser.c +++ b/src/compiler/semantic_analyser.c @@ -48,12 +48,13 @@ void context_change_scope_with_flags(SemaContext *context, ScopeFlags flags) } } -void context_change_scope_for_label(SemaContext *context, Decl *label) +void context_change_scope_for_label(SemaContext *context, DeclId label_id) { context_change_scope_with_flags(context, SCOPE_NONE); - if (label) + if (label_id) { + Decl *label = declptr(label_id); label->label.defer = context->active_scope.defer_last; sema_add_local(context, label); label->label.scope_defer = astid(context->active_scope.in_defer); @@ -314,7 +315,7 @@ void sema_analysis_run(void) // Set a maximum of symbols in the std_module and test module htable_init(&global_context.std_module.symbols, 0x1000); - // Setup the func prototype hash map + // Set up the func prototype hash map type_func_prototype_init(0x10000); // Do we have zero modules? diff --git a/src/compiler/types.c b/src/compiler/types.c index 83ead44a5..f6d237cdb 100644 --- a/src/compiler/types.c +++ b/src/compiler/types.c @@ -1917,7 +1917,7 @@ static inline Type *type_find_max_ptr_type(Type *type, Type *other) return type_get_ptr(max_type); } - // Neither subarray, vararray or pointer? Then no max + // Neither subarray, vararray nor pointer? Then no max if (other->type_kind != TYPE_POINTER) return NULL; Type* other_pointer_type = other->pointer; diff --git a/src/utils/common.h b/src/utils/common.h index 92c5d477c..56fb9cc9d 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -17,7 +17,6 @@ #define NO_ARENA 0 #define MAX_VECTOR_WIDTH 65536 #define MAX_ARRAY_SIZE INT64_MAX -#define MAX_IDENTIFIER_LENGTH 31 #define MAX_SOURCE_LOCATION_LEN 255 #define PROJECT_JSON "project.json" diff --git a/src/utils/file_utils.c b/src/utils/file_utils.c index 1d7d53cbf..1abc6b2b6 100644 --- a/src/utils/file_utils.c +++ b/src/utils/file_utils.c @@ -36,7 +36,7 @@ #endif -uint16_t *win_utf8to16(const char *value) +uint16_t *win_utf8to16(const char *value UNUSED) { #if (_MSC_VER) size_t len = strlen(value); @@ -57,7 +57,7 @@ uint16_t *win_utf8to16(const char *value) } #include -char *win_utf16to8(const uint16_t *wname) +char *win_utf16to8(const uint16_t *wname UNUSED) { #if (_MSC_VER) size_t len = wcslen(wname); diff --git a/src/utils/stringutils.c b/src/utils/stringutils.c index 95fb5004e..eafb922e8 100644 --- a/src/utils/stringutils.c +++ b/src/utils/stringutils.c @@ -329,7 +329,6 @@ void scratch_buffer_append_double(double d) scratch_buffer.len += len_needed; //removing unused zeroes and dot - size_t i = scratch_buffer.len; while (scratch_buffer.len > 0) { if (scratch_buffer.str[scratch_buffer.len - 1] != '0' && scratch_buffer.str[scratch_buffer.len - 1] != '.') diff --git a/src/utils/taskqueue.c b/src/utils/taskqueue.c index 45bcd24d2..fe8148b62 100644 --- a/src/utils/taskqueue.c +++ b/src/utils/taskqueue.c @@ -16,7 +16,6 @@ typedef struct TaskQueue_ static void *taskqueue_thread(void *data) { TaskQueue *task_queue = data; - bool is_active = false; while (1) { pthread_mutex_lock(&task_queue->lock); diff --git a/src/utils/whereami.c b/src/utils/whereami.c index e4fe9480e..ada531243 100644 --- a/src/utils/whereami.c +++ b/src/utils/whereami.c @@ -211,9 +211,7 @@ static int get_executable_path_raw(char *out, int capacity, int *dirname_length) #include #include -#include #include -#include #include "lib.h" diff --git a/wrapper/src/wrapper.cpp b/wrapper/src/wrapper.cpp index 5c4c50312..0cfb7cda1 100644 --- a/wrapper/src/wrapper.cpp +++ b/wrapper/src/wrapper.cpp @@ -109,7 +109,6 @@ static bool llvm_link(ObjFormat format, const char **args, int arg_count, const case MINGW: printf("Mingw not enabled"); exit(1); - break; default: printf("Unsupported linker"); exit(-1);