| ... | @@ -10305,49 +10305,75 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s | ... | @@ -10305,49 +10305,75 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s |
| 10305 | return result; | 10305 | return result; |
| 10306 | } | 10306 | } |
| 10307 | | 10307 | |
| 10308 | static IrInstruction *ir_analyze_enum_to_int(IrAnalyze *ira, IrInstruction *source_instr, | 10308 | static ZigType *ir_resolve_union_tag_type(IrAnalyze *ira, IrInstruction *source_instr, ZigType *union_type) { |
| 10309 | IrInstruction *target, ZigType *wanted_type) | 10309 | assert(union_type->id == ZigTypeIdUnion); |
| 10310 | { | 10310 | |
| 10311 | Error err; | 10311 | Error err; |
| 10312 | assert(wanted_type->id == ZigTypeIdInt || wanted_type->id == ZigTypeIdComptimeInt); | 10312 | if ((err = type_resolve(ira->codegen, union_type, ResolveStatusSizeKnown))) |
| | 10313 | return ira->codegen->builtin_types.entry_invalid; |
| 10313 | | 10314 | |
| 10314 | ZigType *actual_type = target->value.type; | 10315 | AstNode *decl_node = union_type->data.unionation.decl_node; |
| 10315 | if ((err = ensure_complete_type(ira->codegen, actual_type))) | 10316 | if (decl_node->data.container_decl.auto_enum || decl_node->data.container_decl.init_arg_expr != nullptr) { |
| 10316 | return ira->codegen->invalid_instruction; | 10317 | assert(union_type->data.unionation.tag_type != nullptr); |
| | 10318 | return union_type->data.unionation.tag_type; |
| | 10319 | } else { |
| | 10320 | ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("union '%s' has no tag", |
| | 10321 | buf_ptr(&union_type->name))); |
| | 10322 | add_error_note(ira->codegen, msg, decl_node, buf_sprintf("consider 'union(enum)' here")); |
| | 10323 | return ira->codegen->builtin_types.entry_invalid; |
| | 10324 | } |
| | 10325 | } |
| 10317 | | 10326 | |
| 10318 | if (wanted_type != actual_type->data.enumeration.tag_int_type) { | 10327 | static IrInstruction *ir_analyze_enum_to_int(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target) { |
| 10319 | ir_add_error(ira, source_instr, | 10328 | Error err; |
| 10320 | buf_sprintf("enum to integer cast to '%s' instead of its tag type, '%s'", | 10329 | |
| 10321 | buf_ptr(&wanted_type->name), | 10330 | IrInstruction *enum_target; |
| 10322 | buf_ptr(&actual_type->data.enumeration.tag_int_type->name))); | 10331 | ZigType *enum_type; |
| | 10332 | if (target->value.type->id == ZigTypeIdUnion) { |
| | 10333 | enum_type = ir_resolve_union_tag_type(ira, target, target->value.type); |
| | 10334 | if (type_is_invalid(enum_type)) |
| | 10335 | return ira->codegen->invalid_instruction; |
| | 10336 | enum_target = ir_implicit_cast(ira, target, enum_type); |
| | 10337 | if (type_is_invalid(enum_target->value.type)) |
| | 10338 | return ira->codegen->invalid_instruction; |
| | 10339 | } else if (target->value.type->id == ZigTypeIdEnum) { |
| | 10340 | enum_target = target; |
| | 10341 | enum_type = target->value.type; |
| | 10342 | } else { |
| | 10343 | ir_add_error(ira, target, |
| | 10344 | buf_sprintf("expected enum, found type '%s'", buf_ptr(&target->value.type->name))); |
| 10323 | return ira->codegen->invalid_instruction; | 10345 | return ira->codegen->invalid_instruction; |
| 10324 | } | 10346 | } |
| 10325 | | 10347 | |
| 10326 | assert(actual_type->id == ZigTypeIdEnum); | 10348 | if ((err = type_resolve(ira->codegen, enum_type, ResolveStatusSizeKnown))) |
| | 10349 | return ira->codegen->invalid_instruction; |
| 10327 | | 10350 | |
| 10328 | if (instr_is_comptime(target)) { | 10351 | ZigType *tag_type = enum_type->data.enumeration.tag_int_type; |
| 10329 | ConstExprValue *val = ir_resolve_const(ira, target, UndefBad); | 10352 | assert(tag_type->id == ZigTypeIdInt || tag_type->id == ZigTypeIdComptimeInt); |
| | 10353 | |
| | 10354 | if (instr_is_comptime(enum_target)) { |
| | 10355 | ConstExprValue *val = ir_resolve_const(ira, enum_target, UndefBad); |
| 10330 | if (!val) | 10356 | if (!val) |
| 10331 | return ira->codegen->invalid_instruction; | 10357 | return ira->codegen->invalid_instruction; |
| 10332 | IrInstruction *result = ir_const(ira, source_instr, wanted_type); | 10358 | IrInstruction *result = ir_const(ira, source_instr, tag_type); |
| 10333 | init_const_bigint(&result->value, wanted_type, &val->data.x_enum_tag); | 10359 | init_const_bigint(&result->value, tag_type, &val->data.x_enum_tag); |
| 10334 | return result; | 10360 | return result; |
| 10335 | } | 10361 | } |
| 10336 | | 10362 | |
| 10337 | // If there is only one possible tag, then we know at comptime what it is. | 10363 | // If there is only one possible tag, then we know at comptime what it is. |
| 10338 | if (actual_type->data.enumeration.layout == ContainerLayoutAuto && | 10364 | if (enum_type->data.enumeration.layout == ContainerLayoutAuto && |
| 10339 | actual_type->data.enumeration.src_field_count == 1) | 10365 | enum_type->data.enumeration.src_field_count == 1) |
| 10340 | { | 10366 | { |
| 10341 | assert(wanted_type== ira->codegen->builtin_types.entry_num_lit_int); | 10367 | assert(tag_type == ira->codegen->builtin_types.entry_num_lit_int); |
| 10342 | IrInstruction *result = ir_const(ira, source_instr, wanted_type); | 10368 | IrInstruction *result = ir_const(ira, source_instr, tag_type); |
| 10343 | init_const_bigint(&result->value, wanted_type, | 10369 | init_const_bigint(&result->value, tag_type, |
| 10344 | &actual_type->data.enumeration.fields[0].value); | 10370 | &enum_type->data.enumeration.fields[0].value); |
| 10345 | return result; | 10371 | return result; |
| 10346 | } | 10372 | } |
| 10347 | | 10373 | |
| 10348 | IrInstruction *result = ir_build_widen_or_shorten(&ira->new_irb, source_instr->scope, | 10374 | IrInstruction *result = ir_build_widen_or_shorten(&ira->new_irb, source_instr->scope, |
| 10349 | source_instr->source_node, target); | 10375 | source_instr->source_node, enum_target); |
| 10350 | result->value.type = wanted_type; | 10376 | result->value.type = tag_type; |
| 10351 | return result; | 10377 | return result; |
| 10352 | } | 10378 | } |
| 10353 | | 10379 | |
| ... | @@ -14358,12 +14384,12 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source | ... | @@ -14358,12 +14384,12 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source |
| 14358 | if (dst_size == 0) | 14384 | if (dst_size == 0) |
| 14359 | return ErrorNone; | 14385 | return ErrorNone; |
| 14360 | opt_ir_add_error_node(ira, codegen, source_node, | 14386 | opt_ir_add_error_node(ira, codegen, source_node, |
| 14361 | buf_sprintf("attempt to read %zu bytes from null pointer", | 14387 | buf_sprintf("attempt to read %" ZIG_PRI_usize " bytes from null pointer", |
| 14362 | dst_size)); | 14388 | dst_size)); |
| 14363 | return ErrorSemanticAnalyzeFail; | 14389 | return ErrorSemanticAnalyzeFail; |
| 14364 | case ConstPtrSpecialRef: { | 14390 | case ConstPtrSpecialRef: { |
| 14365 | opt_ir_add_error_node(ira, codegen, source_node, | 14391 | opt_ir_add_error_node(ira, codegen, source_node, |
| 14366 | buf_sprintf("attempt to read %zu bytes from pointer to %s which is %zu bytes", | 14392 | buf_sprintf("attempt to read %" ZIG_PRI_usize " bytes from pointer to %s which is %" ZIG_PRI_usize " bytes", |
| 14367 | dst_size, buf_ptr(&pointee->type->name), src_size)); | 14393 | dst_size, buf_ptr(&pointee->type->name), src_size)); |
| 14368 | return ErrorSemanticAnalyzeFail; | 14394 | return ErrorSemanticAnalyzeFail; |
| 14369 | } | 14395 | } |
| ... | @@ -14377,7 +14403,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source | ... | @@ -14377,7 +14403,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source |
| 14377 | src_size = elem_size * (array_val->type->data.array.len - elem_index); | 14403 | src_size = elem_size * (array_val->type->data.array.len - elem_index); |
| 14378 | if (dst_size > src_size) { | 14404 | if (dst_size > src_size) { |
| 14379 | opt_ir_add_error_node(ira, codegen, source_node, | 14405 | opt_ir_add_error_node(ira, codegen, source_node, |
| 14380 | buf_sprintf("attempt to read %zu bytes from %s at index %" ZIG_PRI_usize " which is %zu bytes", | 14406 | buf_sprintf("attempt to read %" ZIG_PRI_usize " bytes from %s at index %" ZIG_PRI_usize " which is %" ZIG_PRI_usize " bytes", |
| 14381 | dst_size, buf_ptr(&array_val->type->name), elem_index, src_size)); | 14407 | dst_size, buf_ptr(&array_val->type->name), elem_index, src_size)); |
| 14382 | return ErrorSemanticAnalyzeFail; | 14408 | return ErrorSemanticAnalyzeFail; |
| 14383 | } | 14409 | } |
| ... | @@ -21364,20 +21390,10 @@ static IrInstruction *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruct | ... | @@ -21364,20 +21390,10 @@ static IrInstruction *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruct |
| 21364 | | 21390 | |
| 21365 | return ir_const_type(ira, &instruction->base, enum_type->data.enumeration.tag_int_type); | 21391 | return ir_const_type(ira, &instruction->base, enum_type->data.enumeration.tag_int_type); |
| 21366 | } else if (enum_type->id == ZigTypeIdUnion) { | 21392 | } else if (enum_type->id == ZigTypeIdUnion) { |
| 21367 | if ((err = ensure_complete_type(ira->codegen, enum_type))) | 21393 | ZigType *tag_type = ir_resolve_union_tag_type(ira, instruction->target, enum_type); |
| 21368 | return ira->codegen->invalid_instruction; | 21394 | if (type_is_invalid(tag_type)) |
| 21369 | | | |
| 21370 | AstNode *decl_node = enum_type->data.unionation.decl_node; | | |
| 21371 | if (decl_node->data.container_decl.auto_enum || decl_node->data.container_decl.init_arg_expr != nullptr) { | | |
| 21372 | assert(enum_type->data.unionation.tag_type != nullptr); | | |
| 21373 | | | |
| 21374 | return ir_const_type(ira, &instruction->base, enum_type->data.unionation.tag_type); | | |
| 21375 | } else { | | |
| 21376 | ErrorMsg *msg = ir_add_error(ira, target_inst, buf_sprintf("union '%s' has no tag", | | |
| 21377 | buf_ptr(&enum_type->name))); | | |
| 21378 | add_error_note(ira->codegen, msg, decl_node, buf_sprintf("consider 'union(enum)' here")); | | |
| 21379 | return ira->codegen->invalid_instruction; | 21395 | return ira->codegen->invalid_instruction; |
| 21380 | } | 21396 | return ir_const_type(ira, &instruction->base, tag_type); |
| 21381 | } else { | 21397 | } else { |
| 21382 | ir_add_error(ira, target_inst, buf_sprintf("expected enum or union, found '%s'", | 21398 | ir_add_error(ira, target_inst, buf_sprintf("expected enum or union, found '%s'", |
| 21383 | buf_ptr(&enum_type->name))); | 21399 | buf_ptr(&enum_type->name))); |
| ... | @@ -21958,23 +21974,11 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr | ... | @@ -21958,23 +21974,11 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr |
| 21958 | | 21974 | |
| 21959 | | 21975 | |
| 21960 | static IrInstruction *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstructionEnumToInt *instruction) { | 21976 | static IrInstruction *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstructionEnumToInt *instruction) { |
| 21961 | Error err; | | |
| 21962 | IrInstruction *target = instruction->target->child; | 21977 | IrInstruction *target = instruction->target->child; |
| 21963 | if (type_is_invalid(target->value.type)) | 21978 | if (type_is_invalid(target->value.type)) |
| 21964 | return ira->codegen->invalid_instruction; | 21979 | return ira->codegen->invalid_instruction; |
| 21965 | | 21980 | |
| 21966 | if (target->value.type->id != ZigTypeIdEnum) { | 21981 | return ir_analyze_enum_to_int(ira, &instruction->base, target); |
| 21967 | ir_add_error(ira, instruction->target, | | |
| 21968 | buf_sprintf("expected enum, found type '%s'", buf_ptr(&target->value.type->name))); | | |
| 21969 | return ira->codegen->invalid_instruction; | | |
| 21970 | } | | |
| 21971 | | | |
| 21972 | if ((err = type_resolve(ira->codegen, target->value.type, ResolveStatusZeroBitsKnown))) | | |
| 21973 | return ira->codegen->invalid_instruction; | | |
| 21974 | | | |
| 21975 | ZigType *tag_type = target->value.type->data.enumeration.tag_int_type; | | |
| 21976 | | | |
| 21977 | return ir_analyze_enum_to_int(ira, &instruction->base, target, tag_type); | | |
| 21978 | } | 21982 | } |
| 21979 | | 21983 | |
| 21980 | static IrInstruction *ir_analyze_instruction_int_to_enum(IrAnalyze *ira, IrInstructionIntToEnum *instruction) { | 21984 | static IrInstruction *ir_analyze_instruction_int_to_enum(IrAnalyze *ira, IrInstructionIntToEnum *instruction) { |