authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-13 11:59:34-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-13 11:59:34-07:00
log9ec892539ee19d39ed040ead628f04da2d11577e
treee9b10f562f9c2279f467a1000b83842371743650
parentbd77bc749a1b38ef754fdd5b7e14a8228cb6f72c

prefer checking a type's id over comparing it to a builtin_types entry


2 files changed, 36 insertions(+), 32 deletions(-)

src/analyze.cpp+24-20
......@@ -173,7 +173,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) {
173173 resolve_type(g, node->data.type.child_type);
174174 TypeTableEntry *child_type = node->data.type.child_type->codegen_node->data.type_node.entry;
175175 assert(child_type);
176 if (child_type == g->builtin_types.entry_unreachable) {
176 if (child_type->id == TypeTableEntryIdUnreachable) {
177177 add_node_error(g, node,
178178 buf_create_from_str("pointer to unreachable not allowed"));
179179 } else if (child_type->id == TypeTableEntryIdInvalid) {
......@@ -186,7 +186,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) {
186186 {
187187 resolve_type(g, node->data.type.child_type);
188188 TypeTableEntry *child_type = node->data.type.child_type->codegen_node->data.type_node.entry;
189 if (child_type == g->builtin_types.entry_unreachable) {
189 if (child_type->id == TypeTableEntryIdUnreachable) {
190190 add_node_error(g, node,
191191 buf_create_from_str("array of unreachable not allowed"));
192192 }
......@@ -240,10 +240,10 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
240240 AstNode *child = node->data.fn_proto.params.at(i);
241241 assert(child->type == NodeTypeParamDecl);
242242 TypeTableEntry *type_entry = resolve_type(g, child->data.param_decl.type);
243 if (type_entry == g->builtin_types.entry_unreachable) {
243 if (type_entry->id == TypeTableEntryIdUnreachable) {
244244 add_node_error(g, child->data.param_decl.type,
245245 buf_sprintf("parameter of type 'unreachable' not allowed"));
246 } else if (type_entry == g->builtin_types.entry_void) {
246 } else if (type_entry->id == TypeTableEntryIdVoid) {
247247 if (node->data.fn_proto.visib_mod == FnProtoVisibModExport) {
248248 add_node_error(g, child->data.param_decl.type,
249249 buf_sprintf("parameter of type 'void' not allowed on exported functions"));
......@@ -570,9 +570,9 @@ static void check_type_compatibility(CodeGen *g, AstNode *node,
570570 return; // anything will do
571571 if (expected_type == actual_type)
572572 return; // match
573 if (expected_type == g->builtin_types.entry_invalid || actual_type == g->builtin_types.entry_invalid)
573 if (expected_type->id == TypeTableEntryIdInvalid || actual_type->id == TypeTableEntryIdInvalid)
574574 return; // already complained
575 if (actual_type == g->builtin_types.entry_unreachable)
575 if (actual_type->id == TypeTableEntryIdUnreachable)
576576 return; // sorry toots; gotta run. good luck with that expected type.
577577
578578 add_node_error(g, node,
......@@ -815,11 +815,11 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
815815 if (child->type == NodeTypeLabel) {
816816 LabelTableEntry *label_entry = child->codegen_node->data.label_entry;
817817 assert(label_entry);
818 label_entry->entered_from_fallthrough = (return_type != g->builtin_types.entry_unreachable);
818 label_entry->entered_from_fallthrough = (return_type->id != TypeTableEntryIdUnreachable);
819819 return_type = g->builtin_types.entry_void;
820820 continue;
821821 }
822 if (return_type == g->builtin_types.entry_unreachable) {
822 if (return_type->id == TypeTableEntryIdUnreachable) {
823823 if (child->type == NodeTypeVoid) {
824824 // {unreachable;void;void} is allowed.
825825 // ignore void statements once we enter unreachable land.
......@@ -843,7 +843,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
843843 actual_return_type = g->builtin_types.entry_void;
844844 }
845845
846 if (actual_return_type == g->builtin_types.entry_unreachable) {
846 if (actual_return_type->id == TypeTableEntryIdUnreachable) {
847847 // "return exit(0)" should just be "exit(0)".
848848 add_node_error(g, node, buf_sprintf("returning is unreachable"));
849849 actual_return_type = g->builtin_types.entry_invalid;
......@@ -857,18 +857,22 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
857857 {
858858 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;;
859859
860 TypeTableEntry *explicit_type = variable_declaration->type != nullptr ?
861 resolve_type(g, variable_declaration->type) : nullptr;
862 if (explicit_type == g->builtin_types.entry_unreachable) {
863 add_node_error(g, variable_declaration->type,
864 buf_sprintf("variable of type 'unreachable' not allowed"));
860 TypeTableEntry *explicit_type = nullptr;
861 if (variable_declaration->type != nullptr) {
862 explicit_type = resolve_type(g, variable_declaration->type);
863 if (explicit_type->id == TypeTableEntryIdUnreachable) {
864 add_node_error(g, variable_declaration->type,
865 buf_sprintf("variable of type 'unreachable' not allowed"));
866 }
865867 }
866868
867 TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ?
868 analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr;
869 if (implicit_type == g->builtin_types.entry_unreachable) {
870 add_node_error(g, node,
871 buf_sprintf("variable initialization is unreachable"));
869 TypeTableEntry *implicit_type = nullptr;
870 if (variable_declaration->expr != nullptr) {
871 implicit_type = analyze_expression(g, import, context, explicit_type, variable_declaration->expr);
872 if (implicit_type->id == TypeTableEntryIdUnreachable) {
873 add_node_error(g, node,
874 buf_sprintf("variable initialization is unreachable"));
875 }
872876 }
873877
874878 if (implicit_type == nullptr && variable_declaration->is_const) {
......@@ -1185,7 +1189,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
11851189
11861190 TypeTableEntry *primary_type;
11871191 TypeTableEntry *other_type;
1188 if (then_type == g->builtin_types.entry_unreachable) {
1192 if (then_type->id == TypeTableEntryIdUnreachable) {
11891193 primary_type = else_type;
11901194 other_type = then_type;
11911195 } else {
src/codegen.cpp+12-12
......@@ -85,12 +85,12 @@ static LLVMZigDIType *to_llvm_debug_type(CodeGen *g, AstNode *type_node) {
8585
8686
8787static bool type_is_unreachable(CodeGen *g, AstNode *type_node) {
88 return get_type_for_type_node(g, type_node) == g->builtin_types.entry_unreachable;
88 return get_type_for_type_node(g, type_node)->id == TypeTableEntryIdUnreachable;
8989}
9090
9191static bool is_param_decl_type_void(CodeGen *g, AstNode *param_decl_node) {
9292 assert(param_decl_node->type == NodeTypeParamDecl);
93 return get_type_for_type_node(g, param_decl_node->data.param_decl.type) == g->builtin_types.entry_void;
93 return get_type_for_type_node(g, param_decl_node->data.param_decl.type)->id == TypeTableEntryIdVoid;
9494}
9595
9696static int count_non_void_params(CodeGen *g, ZigList<AstNode *> *params) {
......@@ -656,8 +656,8 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {
656656 LLVMValueRef cond_value = gen_expr(g, node->data.if_expr.condition);
657657
658658 TypeTableEntry *then_type = get_expr_type(node->data.if_expr.then_block);
659 bool use_expr_value = (then_type != g->builtin_types.entry_unreachable &&
660 then_type != g->builtin_types.entry_void);
659 bool use_expr_value = (then_type->id != TypeTableEntryIdUnreachable &&
660 then_type->id != TypeTableEntryIdVoid);
661661
662662 if (node->data.if_expr.else_node) {
663663 LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then");
......@@ -668,12 +668,12 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {
668668
669669 LLVMPositionBuilderAtEnd(g->builder, then_block);
670670 LLVMValueRef then_expr_result = gen_expr(g, node->data.if_expr.then_block);
671 if (get_expr_type(node->data.if_expr.then_block) != g->builtin_types.entry_unreachable)
671 if (get_expr_type(node->data.if_expr.then_block)->id != TypeTableEntryIdUnreachable)
672672 LLVMBuildBr(g->builder, endif_block);
673673
674674 LLVMPositionBuilderAtEnd(g->builder, else_block);
675675 LLVMValueRef else_expr_result = gen_expr(g, node->data.if_expr.else_node);
676 if (get_expr_type(node->data.if_expr.else_node) != g->builtin_types.entry_unreachable)
676 if (get_expr_type(node->data.if_expr.else_node)->id != TypeTableEntryIdUnreachable)
677677 LLVMBuildBr(g->builder, endif_block);
678678
679679 LLVMPositionBuilderAtEnd(g->builder, endif_block);
......@@ -698,7 +698,7 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {
698698
699699 LLVMPositionBuilderAtEnd(g->builder, then_block);
700700 gen_expr(g, node->data.if_expr.then_block);
701 if (get_expr_type(node->data.if_expr.then_block) != g->builtin_types.entry_unreachable)
701 if (get_expr_type(node->data.if_expr.then_block)->id != TypeTableEntryIdUnreachable)
702702 LLVMBuildBr(g->builder, endif_block);
703703
704704 LLVMPositionBuilderAtEnd(g->builder, endif_block);
......@@ -719,9 +719,9 @@ static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *i
719719
720720 if (implicit_return_type) {
721721 add_debug_source_node(g, block_node);
722 if (implicit_return_type == g->builtin_types.entry_void) {
722 if (implicit_return_type->id == TypeTableEntryIdVoid) {
723723 LLVMBuildRetVoid(g->builder);
724 } else if (implicit_return_type != g->builtin_types.entry_unreachable) {
724 } else if (implicit_return_type->id != TypeTableEntryIdUnreachable) {
725725 LLVMBuildRet(g->builder, return_value);
726726 }
727727 }
......@@ -862,7 +862,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
862862 } else {
863863 value = LLVMConstNull(variable->type->type_ref);
864864 }
865 if (variable->type == g->builtin_types.entry_void) {
865 if (variable->type->id == TypeTableEntryIdVoid) {
866866 return nullptr;
867867 } else {
868868 add_debug_source_node(g, node);
......@@ -924,7 +924,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
924924 node->codegen_node->expr_node.block_context,
925925 &node->data.symbol);
926926 assert(variable);
927 if (variable->type == g->builtin_types.entry_void) {
927 if (variable->type->id == TypeTableEntryIdVoid) {
928928 return nullptr;
929929 } else if (variable->is_ptr) {
930930 if (variable->type->id == TypeTableEntryIdArray) {
......@@ -1133,7 +1133,7 @@ static void do_code_gen(CodeGen *g) {
11331133 break;
11341134
11351135 LocalVariableTableEntry *var = entry->value;
1136 if (var->type == g->builtin_types.entry_void)
1136 if (var->type->id == TypeTableEntryIdVoid)
11371137 continue;
11381138
11391139 unsigned tag;