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) {...@@ -173,7 +173,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) {
173 resolve_type(g, node->data.type.child_type);173 resolve_type(g, node->data.type.child_type);
174 TypeTableEntry *child_type = node->data.type.child_type->codegen_node->data.type_node.entry;174 TypeTableEntry *child_type = node->data.type.child_type->codegen_node->data.type_node.entry;
175 assert(child_type);175 assert(child_type);
176 if (child_type == g->builtin_types.entry_unreachable) {176 if (child_type->id == TypeTableEntryIdUnreachable) {
177 add_node_error(g, node,177 add_node_error(g, node,
178 buf_create_from_str("pointer to unreachable not allowed"));178 buf_create_from_str("pointer to unreachable not allowed"));
179 } else if (child_type->id == TypeTableEntryIdInvalid) {179 } else if (child_type->id == TypeTableEntryIdInvalid) {
...@@ -186,7 +186,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) {...@@ -186,7 +186,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) {
186 {186 {
187 resolve_type(g, node->data.type.child_type);187 resolve_type(g, node->data.type.child_type);
188 TypeTableEntry *child_type = node->data.type.child_type->codegen_node->data.type_node.entry;188 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) {
190 add_node_error(g, node,190 add_node_error(g, node,
191 buf_create_from_str("array of unreachable not allowed"));191 buf_create_from_str("array of unreachable not allowed"));
192 }192 }
...@@ -240,10 +240,10 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t...@@ -240,10 +240,10 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
240 AstNode *child = node->data.fn_proto.params.at(i);240 AstNode *child = node->data.fn_proto.params.at(i);
241 assert(child->type == NodeTypeParamDecl);241 assert(child->type == NodeTypeParamDecl);
242 TypeTableEntry *type_entry = resolve_type(g, child->data.param_decl.type);242 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) {
244 add_node_error(g, child->data.param_decl.type,244 add_node_error(g, child->data.param_decl.type,
245 buf_sprintf("parameter of type 'unreachable' not allowed"));245 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) {
247 if (node->data.fn_proto.visib_mod == FnProtoVisibModExport) {247 if (node->data.fn_proto.visib_mod == FnProtoVisibModExport) {
248 add_node_error(g, child->data.param_decl.type,248 add_node_error(g, child->data.param_decl.type,
249 buf_sprintf("parameter of type 'void' not allowed on exported functions"));249 buf_sprintf("parameter of type 'void' not allowed on exported functions"));
...@@ -570,9 +570,9 @@ static void check_type_compatibility(CodeGen *g, AstNode *node,...@@ -570,9 +570,9 @@ static void check_type_compatibility(CodeGen *g, AstNode *node,
570 return; // anything will do570 return; // anything will do
571 if (expected_type == actual_type)571 if (expected_type == actual_type)
572 return; // match572 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)
574 return; // already complained574 return; // already complained
575 if (actual_type == g->builtin_types.entry_unreachable)575 if (actual_type->id == TypeTableEntryIdUnreachable)
576 return; // sorry toots; gotta run. good luck with that expected type.576 return; // sorry toots; gotta run. good luck with that expected type.
577577
578 add_node_error(g, node,578 add_node_error(g, node,
...@@ -815,11 +815,11 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -815,11 +815,11 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
815 if (child->type == NodeTypeLabel) {815 if (child->type == NodeTypeLabel) {
816 LabelTableEntry *label_entry = child->codegen_node->data.label_entry;816 LabelTableEntry *label_entry = child->codegen_node->data.label_entry;
817 assert(label_entry);817 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);
819 return_type = g->builtin_types.entry_void;819 return_type = g->builtin_types.entry_void;
820 continue;820 continue;
821 }821 }
822 if (return_type == g->builtin_types.entry_unreachable) {822 if (return_type->id == TypeTableEntryIdUnreachable) {
823 if (child->type == NodeTypeVoid) {823 if (child->type == NodeTypeVoid) {
824 // {unreachable;void;void} is allowed.824 // {unreachable;void;void} is allowed.
825 // ignore void statements once we enter unreachable land.825 // ignore void statements once we enter unreachable land.
...@@ -843,7 +843,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -843,7 +843,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
843 actual_return_type = g->builtin_types.entry_void;843 actual_return_type = g->builtin_types.entry_void;
844 }844 }
845845
846 if (actual_return_type == g->builtin_types.entry_unreachable) {846 if (actual_return_type->id == TypeTableEntryIdUnreachable) {
847 // "return exit(0)" should just be "exit(0)".847 // "return exit(0)" should just be "exit(0)".
848 add_node_error(g, node, buf_sprintf("returning is unreachable"));848 add_node_error(g, node, buf_sprintf("returning is unreachable"));
849 actual_return_type = g->builtin_types.entry_invalid;849 actual_return_type = g->builtin_types.entry_invalid;
...@@ -857,18 +857,22 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -857,18 +857,22 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
857 {857 {
858 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;;858 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;;
859859
860 TypeTableEntry *explicit_type = variable_declaration->type != nullptr ?860 TypeTableEntry *explicit_type = nullptr;
861 resolve_type(g, variable_declaration->type) : nullptr;861 if (variable_declaration->type != nullptr) {
862 if (explicit_type == g->builtin_types.entry_unreachable) {862 explicit_type = resolve_type(g, variable_declaration->type);
863 add_node_error(g, variable_declaration->type,863 if (explicit_type->id == TypeTableEntryIdUnreachable) {
864 buf_sprintf("variable of type 'unreachable' not allowed"));864 add_node_error(g, variable_declaration->type,
865 buf_sprintf("variable of type 'unreachable' not allowed"));
866 }
865 }867 }
866868
867 TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ?869 TypeTableEntry *implicit_type = nullptr;
868 analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr;870 if (variable_declaration->expr != nullptr) {
869 if (implicit_type == g->builtin_types.entry_unreachable) {871 implicit_type = analyze_expression(g, import, context, explicit_type, variable_declaration->expr);
870 add_node_error(g, node,872 if (implicit_type->id == TypeTableEntryIdUnreachable) {
871 buf_sprintf("variable initialization is unreachable"));873 add_node_error(g, node,
874 buf_sprintf("variable initialization is unreachable"));
875 }
872 }876 }
873877
874 if (implicit_type == nullptr && variable_declaration->is_const) {878 if (implicit_type == nullptr && variable_declaration->is_const) {
...@@ -1185,7 +1189,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -1185,7 +1189,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
11851189
1186 TypeTableEntry *primary_type;1190 TypeTableEntry *primary_type;
1187 TypeTableEntry *other_type;1191 TypeTableEntry *other_type;
1188 if (then_type == g->builtin_types.entry_unreachable) {1192 if (then_type->id == TypeTableEntryIdUnreachable) {
1189 primary_type = else_type;1193 primary_type = else_type;
1190 other_type = then_type;1194 other_type = then_type;
1191 } else {1195 } else {
src/codegen.cpp+12-12
...@@ -85,12 +85,12 @@ static LLVMZigDIType *to_llvm_debug_type(CodeGen *g, AstNode *type_node) {...@@ -85,12 +85,12 @@ static LLVMZigDIType *to_llvm_debug_type(CodeGen *g, AstNode *type_node) {
8585
8686
87static bool type_is_unreachable(CodeGen *g, AstNode *type_node) {87static 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;
89}89}
9090
91static bool is_param_decl_type_void(CodeGen *g, AstNode *param_decl_node) {91static bool is_param_decl_type_void(CodeGen *g, AstNode *param_decl_node) {
92 assert(param_decl_node->type == NodeTypeParamDecl);92 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;
94}94}
9595
96static int count_non_void_params(CodeGen *g, ZigList<AstNode *> *params) {96static int count_non_void_params(CodeGen *g, ZigList<AstNode *> *params) {
...@@ -656,8 +656,8 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {...@@ -656,8 +656,8 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {
656 LLVMValueRef cond_value = gen_expr(g, node->data.if_expr.condition);656 LLVMValueRef cond_value = gen_expr(g, node->data.if_expr.condition);
657657
658 TypeTableEntry *then_type = get_expr_type(node->data.if_expr.then_block);658 TypeTableEntry *then_type = get_expr_type(node->data.if_expr.then_block);
659 bool use_expr_value = (then_type != g->builtin_types.entry_unreachable &&659 bool use_expr_value = (then_type->id != TypeTableEntryIdUnreachable &&
660 then_type != g->builtin_types.entry_void);660 then_type->id != TypeTableEntryIdVoid);
661661
662 if (node->data.if_expr.else_node) {662 if (node->data.if_expr.else_node) {
663 LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then");663 LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then");
...@@ -668,12 +668,12 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {...@@ -668,12 +668,12 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {
668668
669 LLVMPositionBuilderAtEnd(g->builder, then_block);669 LLVMPositionBuilderAtEnd(g->builder, then_block);
670 LLVMValueRef then_expr_result = gen_expr(g, node->data.if_expr.then_block);670 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)
672 LLVMBuildBr(g->builder, endif_block);672 LLVMBuildBr(g->builder, endif_block);
673673
674 LLVMPositionBuilderAtEnd(g->builder, else_block);674 LLVMPositionBuilderAtEnd(g->builder, else_block);
675 LLVMValueRef else_expr_result = gen_expr(g, node->data.if_expr.else_node);675 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)
677 LLVMBuildBr(g->builder, endif_block);677 LLVMBuildBr(g->builder, endif_block);
678678
679 LLVMPositionBuilderAtEnd(g->builder, endif_block);679 LLVMPositionBuilderAtEnd(g->builder, endif_block);
...@@ -698,7 +698,7 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {...@@ -698,7 +698,7 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {
698698
699 LLVMPositionBuilderAtEnd(g->builder, then_block);699 LLVMPositionBuilderAtEnd(g->builder, then_block);
700 gen_expr(g, node->data.if_expr.then_block);700 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)
702 LLVMBuildBr(g->builder, endif_block);702 LLVMBuildBr(g->builder, endif_block);
703703
704 LLVMPositionBuilderAtEnd(g->builder, endif_block);704 LLVMPositionBuilderAtEnd(g->builder, endif_block);
...@@ -719,9 +719,9 @@ static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *i...@@ -719,9 +719,9 @@ static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *i
719719
720 if (implicit_return_type) {720 if (implicit_return_type) {
721 add_debug_source_node(g, block_node);721 add_debug_source_node(g, block_node);
722 if (implicit_return_type == g->builtin_types.entry_void) {722 if (implicit_return_type->id == TypeTableEntryIdVoid) {
723 LLVMBuildRetVoid(g->builder);723 LLVMBuildRetVoid(g->builder);
724 } else if (implicit_return_type != g->builtin_types.entry_unreachable) {724 } else if (implicit_return_type->id != TypeTableEntryIdUnreachable) {
725 LLVMBuildRet(g->builder, return_value);725 LLVMBuildRet(g->builder, return_value);
726 }726 }
727 }727 }
...@@ -862,7 +862,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {...@@ -862,7 +862,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
862 } else {862 } else {
863 value = LLVMConstNull(variable->type->type_ref);863 value = LLVMConstNull(variable->type->type_ref);
864 }864 }
865 if (variable->type == g->builtin_types.entry_void) {865 if (variable->type->id == TypeTableEntryIdVoid) {
866 return nullptr;866 return nullptr;
867 } else {867 } else {
868 add_debug_source_node(g, node);868 add_debug_source_node(g, node);
...@@ -924,7 +924,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {...@@ -924,7 +924,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
924 node->codegen_node->expr_node.block_context,924 node->codegen_node->expr_node.block_context,
925 &node->data.symbol);925 &node->data.symbol);
926 assert(variable);926 assert(variable);
927 if (variable->type == g->builtin_types.entry_void) {927 if (variable->type->id == TypeTableEntryIdVoid) {
928 return nullptr;928 return nullptr;
929 } else if (variable->is_ptr) {929 } else if (variable->is_ptr) {
930 if (variable->type->id == TypeTableEntryIdArray) {930 if (variable->type->id == TypeTableEntryIdArray) {
...@@ -1133,7 +1133,7 @@ static void do_code_gen(CodeGen *g) {...@@ -1133,7 +1133,7 @@ static void do_code_gen(CodeGen *g) {
1133 break;1133 break;
11341134
1135 LocalVariableTableEntry *var = entry->value;1135 LocalVariableTableEntry *var = entry->value;
1136 if (var->type == g->builtin_types.entry_void)1136 if (var->type->id == TypeTableEntryIdVoid)
1137 continue;1137 continue;
11381138
1139 unsigned tag;1139 unsigned tag;