From 9ec892539ee19d39ed040ead628f04da2d11577e Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Sun, 13 Dec 2015 11:59:34 -0700 Subject: [PATCH] prefer checking a type's id over comparing it to a builtin_types entry --- src/analyze.cpp | 44 ++++++++++++++++++++++++-------------------- src/codegen.cpp | 24 ++++++++++++------------ 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 3d46dfcdf..7b7c09280 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -173,7 +173,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) { resolve_type(g, node->data.type.child_type); TypeTableEntry *child_type = node->data.type.child_type->codegen_node->data.type_node.entry; assert(child_type); - if (child_type == g->builtin_types.entry_unreachable) { + if (child_type->id == TypeTableEntryIdUnreachable) { add_node_error(g, node, buf_create_from_str("pointer to unreachable not allowed")); } else if (child_type->id == TypeTableEntryIdInvalid) { @@ -186,7 +186,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) { { resolve_type(g, node->data.type.child_type); TypeTableEntry *child_type = node->data.type.child_type->codegen_node->data.type_node.entry; - if (child_type == g->builtin_types.entry_unreachable) { + if (child_type->id == TypeTableEntryIdUnreachable) { add_node_error(g, node, buf_create_from_str("array of unreachable not allowed")); } @@ -240,10 +240,10 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t AstNode *child = node->data.fn_proto.params.at(i); assert(child->type == NodeTypeParamDecl); TypeTableEntry *type_entry = resolve_type(g, child->data.param_decl.type); - if (type_entry == g->builtin_types.entry_unreachable) { + if (type_entry->id == TypeTableEntryIdUnreachable) { add_node_error(g, child->data.param_decl.type, buf_sprintf("parameter of type 'unreachable' not allowed")); - } else if (type_entry == g->builtin_types.entry_void) { + } else if (type_entry->id == TypeTableEntryIdVoid) { if (node->data.fn_proto.visib_mod == FnProtoVisibModExport) { add_node_error(g, child->data.param_decl.type, buf_sprintf("parameter of type 'void' not allowed on exported functions")); @@ -570,9 +570,9 @@ static void check_type_compatibility(CodeGen *g, AstNode *node, return; // anything will do if (expected_type == actual_type) return; // match - if (expected_type == g->builtin_types.entry_invalid || actual_type == g->builtin_types.entry_invalid) + if (expected_type->id == TypeTableEntryIdInvalid || actual_type->id == TypeTableEntryIdInvalid) return; // already complained - if (actual_type == g->builtin_types.entry_unreachable) + if (actual_type->id == TypeTableEntryIdUnreachable) return; // sorry toots; gotta run. good luck with that expected type. add_node_error(g, node, @@ -815,11 +815,11 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, if (child->type == NodeTypeLabel) { LabelTableEntry *label_entry = child->codegen_node->data.label_entry; assert(label_entry); - label_entry->entered_from_fallthrough = (return_type != g->builtin_types.entry_unreachable); + label_entry->entered_from_fallthrough = (return_type->id != TypeTableEntryIdUnreachable); return_type = g->builtin_types.entry_void; continue; } - if (return_type == g->builtin_types.entry_unreachable) { + if (return_type->id == TypeTableEntryIdUnreachable) { if (child->type == NodeTypeVoid) { // {unreachable;void;void} is allowed. // ignore void statements once we enter unreachable land. @@ -843,7 +843,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, actual_return_type = g->builtin_types.entry_void; } - if (actual_return_type == g->builtin_types.entry_unreachable) { + if (actual_return_type->id == TypeTableEntryIdUnreachable) { // "return exit(0)" should just be "exit(0)". add_node_error(g, node, buf_sprintf("returning is unreachable")); actual_return_type = g->builtin_types.entry_invalid; @@ -857,18 +857,22 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, { AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;; - TypeTableEntry *explicit_type = variable_declaration->type != nullptr ? - resolve_type(g, variable_declaration->type) : nullptr; - if (explicit_type == g->builtin_types.entry_unreachable) { - add_node_error(g, variable_declaration->type, - buf_sprintf("variable of type 'unreachable' not allowed")); + TypeTableEntry *explicit_type = nullptr; + if (variable_declaration->type != nullptr) { + explicit_type = resolve_type(g, variable_declaration->type); + if (explicit_type->id == TypeTableEntryIdUnreachable) { + add_node_error(g, variable_declaration->type, + buf_sprintf("variable of type 'unreachable' not allowed")); + } } - TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ? - analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr; - if (implicit_type == g->builtin_types.entry_unreachable) { - add_node_error(g, node, - buf_sprintf("variable initialization is unreachable")); + TypeTableEntry *implicit_type = nullptr; + if (variable_declaration->expr != nullptr) { + implicit_type = analyze_expression(g, import, context, explicit_type, variable_declaration->expr); + if (implicit_type->id == TypeTableEntryIdUnreachable) { + add_node_error(g, node, + buf_sprintf("variable initialization is unreachable")); + } } if (implicit_type == nullptr && variable_declaration->is_const) { @@ -1185,7 +1189,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, TypeTableEntry *primary_type; TypeTableEntry *other_type; - if (then_type == g->builtin_types.entry_unreachable) { + if (then_type->id == TypeTableEntryIdUnreachable) { primary_type = else_type; other_type = then_type; } else { diff --git a/src/codegen.cpp b/src/codegen.cpp index c38b1bd76..fe20d3ace 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -85,12 +85,12 @@ static LLVMZigDIType *to_llvm_debug_type(CodeGen *g, AstNode *type_node) { static bool type_is_unreachable(CodeGen *g, AstNode *type_node) { - return get_type_for_type_node(g, type_node) == g->builtin_types.entry_unreachable; + return get_type_for_type_node(g, type_node)->id == TypeTableEntryIdUnreachable; } static bool is_param_decl_type_void(CodeGen *g, AstNode *param_decl_node) { assert(param_decl_node->type == NodeTypeParamDecl); - return get_type_for_type_node(g, param_decl_node->data.param_decl.type) == g->builtin_types.entry_void; + return get_type_for_type_node(g, param_decl_node->data.param_decl.type)->id == TypeTableEntryIdVoid; } static int count_non_void_params(CodeGen *g, ZigList *params) { @@ -656,8 +656,8 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) { LLVMValueRef cond_value = gen_expr(g, node->data.if_expr.condition); TypeTableEntry *then_type = get_expr_type(node->data.if_expr.then_block); - bool use_expr_value = (then_type != g->builtin_types.entry_unreachable && - then_type != g->builtin_types.entry_void); + bool use_expr_value = (then_type->id != TypeTableEntryIdUnreachable && + then_type->id != TypeTableEntryIdVoid); if (node->data.if_expr.else_node) { LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then"); @@ -668,12 +668,12 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) { LLVMPositionBuilderAtEnd(g->builder, then_block); LLVMValueRef then_expr_result = gen_expr(g, node->data.if_expr.then_block); - if (get_expr_type(node->data.if_expr.then_block) != g->builtin_types.entry_unreachable) + if (get_expr_type(node->data.if_expr.then_block)->id != TypeTableEntryIdUnreachable) LLVMBuildBr(g->builder, endif_block); LLVMPositionBuilderAtEnd(g->builder, else_block); LLVMValueRef else_expr_result = gen_expr(g, node->data.if_expr.else_node); - if (get_expr_type(node->data.if_expr.else_node) != g->builtin_types.entry_unreachable) + if (get_expr_type(node->data.if_expr.else_node)->id != TypeTableEntryIdUnreachable) LLVMBuildBr(g->builder, endif_block); LLVMPositionBuilderAtEnd(g->builder, endif_block); @@ -698,7 +698,7 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) { LLVMPositionBuilderAtEnd(g->builder, then_block); gen_expr(g, node->data.if_expr.then_block); - if (get_expr_type(node->data.if_expr.then_block) != g->builtin_types.entry_unreachable) + if (get_expr_type(node->data.if_expr.then_block)->id != TypeTableEntryIdUnreachable) LLVMBuildBr(g->builder, endif_block); LLVMPositionBuilderAtEnd(g->builder, endif_block); @@ -719,9 +719,9 @@ static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *i if (implicit_return_type) { add_debug_source_node(g, block_node); - if (implicit_return_type == g->builtin_types.entry_void) { + if (implicit_return_type->id == TypeTableEntryIdVoid) { LLVMBuildRetVoid(g->builder); - } else if (implicit_return_type != g->builtin_types.entry_unreachable) { + } else if (implicit_return_type->id != TypeTableEntryIdUnreachable) { LLVMBuildRet(g->builder, return_value); } } @@ -862,7 +862,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) { } else { value = LLVMConstNull(variable->type->type_ref); } - if (variable->type == g->builtin_types.entry_void) { + if (variable->type->id == TypeTableEntryIdVoid) { return nullptr; } else { add_debug_source_node(g, node); @@ -924,7 +924,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) { node->codegen_node->expr_node.block_context, &node->data.symbol); assert(variable); - if (variable->type == g->builtin_types.entry_void) { + if (variable->type->id == TypeTableEntryIdVoid) { return nullptr; } else if (variable->is_ptr) { if (variable->type->id == TypeTableEntryIdArray) { @@ -1133,7 +1133,7 @@ static void do_code_gen(CodeGen *g) { break; LocalVariableTableEntry *var = entry->value; - if (var->type == g->builtin_types.entry_void) + if (var->type->id == TypeTableEntryIdVoid) continue; unsigned tag;