aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-12-03 20:43:56 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-12-03 20:43:56 -0500
commit0ad1239522c70418990dc7b9da4e128da7cdd1d5 (patch)
tree3a434788633db0a3d6e30f779fc1239a7513205a /src/ir.cpp
parent137c8f5e8a6023db24f90555e968b592a4b843e4 (diff)
downloadzig-0ad1239522c70418990dc7b9da4e128da7cdd1d5.tar.gz
zig-0ad1239522c70418990dc7b9da4e128da7cdd1d5.zip
rework enums and unions and their relationship to each other
* @enumTagName renamed to @tagName and it works on enums and union-enums * Remove the EnumTag type. Now there is only enum and union, and the tag type of a union is always an enum. * unions support specifying the tag enum type, and they support inferring an enum tag type. * Enums no longer support field types but they do support setting the tag values. Likewise union-enums when inferring an enum tag type support setting the tag values. * It is now an error for enums and unions to have 0 fields. * switch statements support union-enums closes #618
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp488
1 files changed, 206 insertions, 282 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index e51f52adae..5da59fedb7 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -223,10 +223,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionStructFieldPtr *
return IrInstructionIdStructFieldPtr;
}
-static constexpr IrInstructionId ir_instruction_id(IrInstructionEnumFieldPtr *) {
- return IrInstructionIdEnumFieldPtr;
-}
-
static constexpr IrInstructionId ir_instruction_id(IrInstructionUnionFieldPtr *) {
return IrInstructionIdUnionFieldPtr;
}
@@ -319,8 +315,8 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCtz *) {
return IrInstructionIdCtz;
}
-static constexpr IrInstructionId ir_instruction_id(IrInstructionEnumTag *) {
- return IrInstructionIdEnumTag;
+static constexpr IrInstructionId ir_instruction_id(IrInstructionUnionTag *) {
+ return IrInstructionIdUnionTag;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionImport *) {
@@ -479,10 +475,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTestComptime *)
return IrInstructionIdTestComptime;
}
-static constexpr IrInstructionId ir_instruction_id(IrInstructionInitEnum *) {
- return IrInstructionIdInitEnum;
-}
-
static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCast *) {
return IrInstructionIdPtrCast;
}
@@ -913,27 +905,6 @@ static IrInstruction *ir_build_struct_field_ptr_from(IrBuilder *irb, IrInstructi
return new_instruction;
}
-static IrInstruction *ir_build_enum_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
- IrInstruction *enum_ptr, TypeEnumField *field)
-{
- IrInstructionEnumFieldPtr *instruction = ir_build_instruction<IrInstructionEnumFieldPtr>(irb, scope, source_node);
- instruction->enum_ptr = enum_ptr;
- instruction->field = field;
-
- ir_ref_instruction(enum_ptr, irb->current_basic_block);
-
- return &instruction->base;
-}
-
-static IrInstruction *ir_build_enum_field_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
- IrInstruction *enum_ptr, TypeEnumField *type_enum_field)
-{
- IrInstruction *new_instruction = ir_build_enum_field_ptr(irb, old_instruction->scope,
- old_instruction->source_node, enum_ptr, type_enum_field);
- ir_link_new_instruction(new_instruction, old_instruction);
- return new_instruction;
-}
-
static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
IrInstruction *union_ptr, TypeUnionField *field)
{
@@ -1528,8 +1499,8 @@ static IrInstruction *ir_build_switch_var(IrBuilder *irb, Scope *scope, AstNode
return &instruction->base;
}
-static IrInstruction *ir_build_enum_tag(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
- IrInstructionEnumTag *instruction = ir_build_instruction<IrInstructionEnumTag>(irb, scope, source_node);
+static IrInstruction *ir_build_union_tag(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
+ IrInstructionUnionTag *instruction = ir_build_instruction<IrInstructionUnionTag>(irb, scope, source_node);
instruction->value = value;
ir_ref_instruction(value, irb->current_basic_block);
@@ -1537,13 +1508,6 @@ static IrInstruction *ir_build_enum_tag(IrBuilder *irb, Scope *scope, AstNode *s
return &instruction->base;
}
-static IrInstruction *ir_build_enum_tag_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value) {
- IrInstruction *new_instruction = ir_build_enum_tag(irb, old_instruction->scope,
- old_instruction->source_node, value);
- ir_link_new_instruction(new_instruction, old_instruction);
- return new_instruction;
-}
-
static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *name) {
IrInstructionImport *instruction = ir_build_instruction<IrInstructionImport>(irb, scope, source_node);
instruction->name = name;
@@ -2033,28 +1997,6 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo
return &instruction->base;
}
-static IrInstruction *ir_build_init_enum(IrBuilder *irb, Scope *scope, AstNode *source_node,
- TypeTableEntry *enum_type, TypeEnumField *field, IrInstruction *init_value)
-{
- IrInstructionInitEnum *instruction = ir_build_instruction<IrInstructionInitEnum>(irb, scope, source_node);
- instruction->enum_type = enum_type;
- instruction->field = field;
- instruction->init_value = init_value;
-
- ir_ref_instruction(init_value, irb->current_basic_block);
-
- return &instruction->base;
-}
-
-static IrInstruction *ir_build_init_enum_from(IrBuilder *irb, IrInstruction *old_instruction,
- TypeTableEntry *enum_type, TypeEnumField *field, IrInstruction *init_value)
-{
- IrInstruction *new_instruction = ir_build_init_enum(irb, old_instruction->scope, old_instruction->source_node,
- enum_type, field, init_value);
- ir_link_new_instruction(new_instruction, old_instruction);
- return new_instruction;
-}
-
static IrInstruction *ir_build_ptr_cast(IrBuilder *irb, Scope *scope, AstNode *source_node,
IrInstruction *dest_type, IrInstruction *ptr)
{
@@ -2481,13 +2423,6 @@ static IrInstruction *ir_instruction_structfieldptr_get_dep(IrInstructionStructF
}
}
-static IrInstruction *ir_instruction_enumfieldptr_get_dep(IrInstructionEnumFieldPtr *instruction, size_t index) {
- switch (index) {
- case 0: return instruction->enum_ptr;
- default: return nullptr;
- }
-}
-
static IrInstruction *ir_instruction_unionfieldptr_get_dep(IrInstructionUnionFieldPtr *instruction, size_t index) {
switch (index) {
case 0: return instruction->union_ptr;
@@ -2657,7 +2592,7 @@ static IrInstruction *ir_instruction_maybewrap_get_dep(IrInstructionMaybeWrap *i
}
}
-static IrInstruction *ir_instruction_enumtag_get_dep(IrInstructionEnumTag *instruction, size_t index) {
+static IrInstruction *ir_instruction_uniontag_get_dep(IrInstructionUnionTag *instruction, size_t index) {
switch (index) {
case 0: return instruction->value;
default: return nullptr;
@@ -2943,13 +2878,6 @@ static IrInstruction *ir_instruction_testcomptime_get_dep(IrInstructionTestCompt
}
}
-static IrInstruction *ir_instruction_initenum_get_dep(IrInstructionInitEnum *instruction, size_t index) {
- switch (index) {
- case 0: return instruction->init_value;
- default: return nullptr;
- }
-}
-
static IrInstruction *ir_instruction_ptrcast_get_dep(IrInstructionPtrCast *instruction,
size_t index)
{
@@ -3184,8 +3112,6 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
return ir_instruction_fieldptr_get_dep((IrInstructionFieldPtr *) instruction, index);
case IrInstructionIdStructFieldPtr:
return ir_instruction_structfieldptr_get_dep((IrInstructionStructFieldPtr *) instruction, index);
- case IrInstructionIdEnumFieldPtr:
- return ir_instruction_enumfieldptr_get_dep((IrInstructionEnumFieldPtr *) instruction, index);
case IrInstructionIdUnionFieldPtr:
return ir_instruction_unionfieldptr_get_dep((IrInstructionUnionFieldPtr *) instruction, index);
case IrInstructionIdElemPtr:
@@ -3234,8 +3160,8 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
return ir_instruction_unwrapmaybe_get_dep((IrInstructionUnwrapMaybe *) instruction, index);
case IrInstructionIdMaybeWrap:
return ir_instruction_maybewrap_get_dep((IrInstructionMaybeWrap *) instruction, index);
- case IrInstructionIdEnumTag:
- return ir_instruction_enumtag_get_dep((IrInstructionEnumTag *) instruction, index);
+ case IrInstructionIdUnionTag:
+ return ir_instruction_uniontag_get_dep((IrInstructionUnionTag *) instruction, index);
case IrInstructionIdClz:
return ir_instruction_clz_get_dep((IrInstructionClz *) instruction, index);
case IrInstructionIdCtz:
@@ -3312,8 +3238,6 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
return ir_instruction_fnproto_get_dep((IrInstructionFnProto *) instruction, index);
case IrInstructionIdTestComptime:
return ir_instruction_testcomptime_get_dep((IrInstructionTestComptime *) instruction, index);
- case IrInstructionIdInitEnum:
- return ir_instruction_initenum_get_dep((IrInstructionInitEnum *) instruction, index);
case IrInstructionIdPtrCast:
return ir_instruction_ptrcast_get_dep((IrInstructionPtrCast *) instruction, index);
case IrInstructionIdBitCast:
@@ -4695,14 +4619,14 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
return ir_build_ptr_to_int(irb, scope, node, arg0_value);
}
- case BuiltinFnIdEnumTagName:
+ case BuiltinFnIdTagName:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
- IrInstruction *actual_tag = ir_build_enum_tag(irb, scope, node, arg0_value);
+ IrInstruction *actual_tag = ir_build_union_tag(irb, scope, node, arg0_value);
return ir_build_enum_tag_name(irb, scope, node, actual_tag);
}
case BuiltinFnIdEnumTagType:
@@ -8381,13 +8305,28 @@ static IrInstruction *ir_analyze_enum_to_int(IrAnalyze *ira, IrInstruction *sour
{
assert(wanted_type->id == TypeTableEntryIdInt);
+ TypeTableEntry *actual_type = target->value.type;
+ ensure_complete_type(ira->codegen, actual_type);
+ if (type_is_invalid(actual_type))
+ return ira->codegen->invalid_instruction;
+
+ if (wanted_type != actual_type->data.enumeration.tag_int_type) {
+ ir_add_error(ira, source_instr,
+ buf_sprintf("enum to integer cast to '%s' instead of its tag type, '%s'",
+ buf_ptr(&wanted_type->name),
+ buf_ptr(&actual_type->data.enumeration.tag_int_type->name)));
+ return ira->codegen->invalid_instruction;
+ }
+
+ assert(actual_type->id == TypeTableEntryIdEnum);
+
if (instr_is_comptime(target)) {
ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
if (!val)
return ira->codegen->invalid_instruction;
IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
source_instr->source_node, wanted_type);
- init_const_bigint(&result->value, wanted_type, &val->data.x_enum.tag);
+ init_const_bigint(&result->value, wanted_type, &val->data.x_enum_tag);
return result;
}
@@ -8397,6 +8336,31 @@ static IrInstruction *ir_analyze_enum_to_int(IrAnalyze *ira, IrInstruction *sour
return result;
}
+static IrInstruction *ir_analyze_union_to_tag(IrAnalyze *ira, IrInstruction *source_instr,
+ IrInstruction *target, TypeTableEntry *wanted_type)
+{
+ assert(target->value.type->id == TypeTableEntryIdUnion);
+ assert(wanted_type->id == TypeTableEntryIdEnum);
+ assert(wanted_type == target->value.type->data.unionation.tag_type);
+
+ if (instr_is_comptime(target)) {
+ ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
+ if (!val)
+ return ira->codegen->invalid_instruction;
+ IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
+ source_instr->source_node, wanted_type);
+ result->value.special = ConstValSpecialStatic;
+ result->value.type = wanted_type;
+ bigint_init_bigint(&result->value.data.x_enum_tag, &val->data.x_union.tag);
+ return result;
+ }
+
+ IrInstruction *result = ir_build_union_tag(&ira->new_irb, source_instr->scope,
+ source_instr->source_node, target);
+ result->value.type = wanted_type;
+ return result;
+}
+
static IrInstruction *ir_analyze_undefined_to_anything(IrAnalyze *ira, IrInstruction *source_instr,
IrInstruction *target, TypeTableEntry *wanted_type)
{
@@ -8452,6 +8416,22 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour
{
assert(wanted_type->id == TypeTableEntryIdEnum);
+ TypeTableEntry *actual_type = target->value.type;
+
+ ensure_complete_type(ira->codegen, wanted_type);
+ if (type_is_invalid(wanted_type))
+ return ira->codegen->invalid_instruction;
+
+ if (actual_type != wanted_type->data.enumeration.tag_int_type) {
+ ir_add_error(ira, source_instr,
+ buf_sprintf("integer to enum cast from '%s' instead of its tag type, '%s'",
+ buf_ptr(&actual_type->name),
+ buf_ptr(&wanted_type->data.enumeration.tag_int_type->name)));
+ return ira->codegen->invalid_instruction;
+ }
+
+ assert(actual_type->id == TypeTableEntryIdInt);
+
if (instr_is_comptime(target)) {
ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
if (!val)
@@ -8469,7 +8449,7 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour
IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
source_instr->source_node, wanted_type);
- bigint_init_bigint(&result->value.data.x_enum.tag, &val->data.x_bigint);
+ bigint_init_bigint(&result->value.data.x_enum_tag, &val->data.x_bigint);
return result;
}
@@ -8907,39 +8887,24 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
}
// explicit cast from integer to enum type with no payload
- if (actual_type->id == TypeTableEntryIdInt &&
- wanted_type->id == TypeTableEntryIdEnum &&
- wanted_type->data.enumeration.gen_field_count == 0)
- {
- ensure_complete_type(ira->codegen, wanted_type);
- if (type_is_invalid(wanted_type))
- return ira->codegen->invalid_instruction;
- if (actual_type == wanted_type->data.enumeration.tag_type->data.enum_tag.int_type) {
- return ir_analyze_int_to_enum(ira, source_instr, value, wanted_type);
- }
- ir_add_error(ira, source_instr,
- buf_sprintf("integer to enum cast from '%s' instead of its tag type, '%s'",
- buf_ptr(&actual_type->name),
- buf_ptr(&wanted_type->data.enumeration.tag_type->data.enum_tag.int_type->name)));
- return ira->codegen->invalid_instruction;
+ if (actual_type->id == TypeTableEntryIdInt && wanted_type->id == TypeTableEntryIdEnum) {
+ return ir_analyze_int_to_enum(ira, source_instr, value, wanted_type);
}
// explicit cast from enum type with no payload to integer
- if (wanted_type->id == TypeTableEntryIdInt &&
- actual_type->id == TypeTableEntryIdEnum &&
- actual_type->data.enumeration.gen_field_count == 0)
- {
- ensure_complete_type(ira->codegen, actual_type);
+ if (wanted_type->id == TypeTableEntryIdInt && actual_type->id == TypeTableEntryIdEnum) {
+ return ir_analyze_enum_to_int(ira, source_instr, value, wanted_type);
+ }
+
+ // explicit cast from union to the enum type of the union
+ if (actual_type->id == TypeTableEntryIdUnion && wanted_type->id == TypeTableEntryIdEnum) {
+ type_ensure_zero_bits_known(ira->codegen, actual_type);
if (type_is_invalid(actual_type))
return ira->codegen->invalid_instruction;
- if (wanted_type == actual_type->data.enumeration.tag_type->data.enum_tag.int_type) {
- return ir_analyze_enum_to_int(ira, source_instr, value, wanted_type);
+
+ if (actual_type->data.unionation.tag_type == wanted_type) {
+ return ir_analyze_union_to_tag(ira, source_instr, value, wanted_type);
}
- ir_add_error(ira, source_instr,
- buf_sprintf("enum to integer cast to '%s' instead of its tag type, '%s'",
- buf_ptr(&wanted_type->name),
- buf_ptr(&actual_type->data.enumeration.tag_type->data.enum_tag.int_type->name)));
- return ira->codegen->invalid_instruction;
}
// explicit cast from undefined to anything
@@ -9148,7 +9113,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic
if (!const_val)
return false;
- *out = (AtomicOrder)bigint_as_unsigned(&const_val->data.x_enum.tag);
+ *out = (AtomicOrder)bigint_as_unsigned(&const_val->data.x_enum_tag);
return true;
}
@@ -9168,7 +9133,7 @@ static bool ir_resolve_global_linkage(IrAnalyze *ira, IrInstruction *value, Glob
if (!const_val)
return false;
- *out = (GlobalLinkageId)bigint_as_unsigned(&const_val->data.x_enum.tag);
+ *out = (GlobalLinkageId)bigint_as_unsigned(&const_val->data.x_enum_tag);
return true;
}
@@ -9188,7 +9153,7 @@ static bool ir_resolve_float_mode(IrAnalyze *ira, IrInstruction *value, FloatMod
if (!const_val)
return false;
- *out = (FloatMode)bigint_as_unsigned(&const_val->data.x_enum.tag);
+ *out = (FloatMode)bigint_as_unsigned(&const_val->data.x_enum_tag);
return true;
}
@@ -9400,7 +9365,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
break;
case TypeTableEntryIdEnum:
- if (!is_equality_cmp || resolved_type->data.enumeration.gen_field_count != 0) {
+ if (!is_equality_cmp) {
ir_add_error_node(ira, source_node,
buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name)));
return ira->codegen->builtin_types.entry_invalid;
@@ -9419,9 +9384,6 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name)));
return ira->codegen->builtin_types.entry_invalid;
- case TypeTableEntryIdEnumTag:
- zig_panic("TODO implement comparison for enum tag type");
-
case TypeTableEntryIdVar:
zig_unreachable();
}
@@ -10170,7 +10132,6 @@ static VarClassRequired get_var_class_required(TypeTableEntry *type_entry) {
case TypeTableEntryIdVoid:
case TypeTableEntryIdPureError:
case TypeTableEntryIdFn:
- case TypeTableEntryIdEnumTag:
return VarClassRequiredAny;
case TypeTableEntryIdNumLitFloat:
case TypeTableEntryIdNumLitInt:
@@ -10913,7 +10874,6 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
case TypeTableEntryIdUnion:
case TypeTableEntryIdFn:
case TypeTableEntryIdBoundFn:
- case TypeTableEntryIdEnumTag:
{
ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base);
TypeTableEntry *result_type = get_error_type(ira->codegen, meta_type);
@@ -11001,7 +10961,6 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op
case TypeTableEntryIdNamespace:
case TypeTableEntryIdBlock:
case TypeTableEntryIdBoundFn:
- case TypeTableEntryIdEnumTag:
case TypeTableEntryIdArgTuple:
{
ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base);
@@ -11662,15 +11621,8 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
field_ptr_instruction, container_ptr, container_type);
}
} else if (bare_type->id == TypeTableEntryIdEnum) {
- TypeEnumField *field = find_enum_type_field(bare_type, field_name);
- if (field) {
- ir_build_enum_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field);
- return get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,
- get_abi_alignment(ira->codegen, field->type_entry), 0, 0);
- } else {
- return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
- field_ptr_instruction, container_ptr, container_type);
- }
+ return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
+ field_ptr_instruction, container_ptr, container_type);
} else if (bare_type->id == TypeTableEntryIdUnion) {
TypeUnionField *field = find_union_type_field(bare_type, field_name);
if (field) {
@@ -11841,20 +11793,27 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
TypeEnumField *field = find_enum_type_field(child_type, field_name);
if (field) {
- if (field->type_entry->id == TypeTableEntryIdVoid) {
- bool ptr_is_const = true;
- bool ptr_is_volatile = false;
- return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
- create_const_enum_tag(child_type, &field->value), child_type,
- ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
- } else {
- bool ptr_is_const = true;
- bool ptr_is_volatile = false;
- return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
- create_const_bigint(child_type->data.enumeration.tag_type, &field->value),
- child_type->data.enumeration.tag_type,
+ bool ptr_is_const = true;
+ bool ptr_is_volatile = false;
+ return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
+ create_const_enum(child_type, &field->value), child_type,
+ ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
+ }
+ } else if (child_type->id == TypeTableEntryIdUnion &&
+ (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||
+ child_type->data.unionation.decl_node->data.container_decl.auto_enum))
+ {
+ ensure_complete_type(ira->codegen, child_type);
+ if (type_is_invalid(child_type))
+ return ira->codegen->builtin_types.entry_invalid;
+ TypeUnionField *field = find_union_type_field(child_type, field_name);
+ if (field) {
+ TypeTableEntry *enum_type = child_type->data.unionation.tag_type;
+ bool ptr_is_const = true;
+ bool ptr_is_volatile = false;
+ return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
+ create_const_enum(enum_type, &field->enum_field->value), enum_type,
ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
- }
}
}
ScopeDecls *container_scope = get_container_scope(child_type);
@@ -12163,7 +12122,6 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi
case TypeTableEntryIdEnum:
case TypeTableEntryIdUnion:
case TypeTableEntryIdFn:
- case TypeTableEntryIdEnumTag:
case TypeTableEntryIdArgTuple:
case TypeTableEntryIdOpaque:
{
@@ -12511,7 +12469,6 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
case TypeTableEntryIdFn:
case TypeTableEntryIdNamespace:
case TypeTableEntryIdBoundFn:
- case TypeTableEntryIdEnumTag:
{
type_ensure_zero_bits_known(ira->codegen, child_type);
TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
@@ -12620,7 +12577,6 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
case TypeTableEntryIdFn:
case TypeTableEntryIdNamespace:
case TypeTableEntryIdBoundFn:
- case TypeTableEntryIdEnumTag:
{
TypeTableEntry *result_type = get_array_type(ira->codegen, child_type, size);
ConstExprValue *out_val = ir_build_const_from(ira, &array_type_instruction->base);
@@ -12671,7 +12627,6 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
case TypeTableEntryIdPureError:
case TypeTableEntryIdEnum:
case TypeTableEntryIdUnion:
- case TypeTableEntryIdEnumTag:
case TypeTableEntryIdFn:
{
uint64_t size_in_bytes = type_size(ira->codegen, type_entry);
@@ -12824,17 +12779,22 @@ static TypeTableEntry *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionC
}
}
-static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value) {
+static IrInstruction *ir_analyze_union_tag(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value) {
if (type_is_invalid(value->value.type))
return ira->codegen->invalid_instruction;
- if (value->value.type->id != TypeTableEntryIdEnum) {
+ if (value->value.type->id == TypeTableEntryIdEnum) {
+ return value;
+ }
+
+ if (value->value.type->id != TypeTableEntryIdUnion) {
ir_add_error(ira, source_instr,
- buf_sprintf("expected enum type, found '%s'", buf_ptr(&value->value.type->name)));
+ buf_sprintf("expected enum or union type, found '%s'", buf_ptr(&value->value.type->name)));
return ira->codegen->invalid_instruction;
}
- TypeTableEntry *tag_type = value->value.type->data.enumeration.tag_type;
+ TypeTableEntry *tag_type = value->value.type->data.unionation.tag_type;
+ assert(tag_type->id == TypeTableEntryIdEnum);
if (instr_is_comptime(value)) {
ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
@@ -12845,11 +12805,11 @@ static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_
source_instr->scope, source_instr->source_node);
const_instruction->base.value.type = tag_type;
const_instruction->base.value.special = ConstValSpecialStatic;
- bigint_init_bigint(&const_instruction->base.value.data.x_bigint, &val->data.x_enum.tag);
+ bigint_init_bigint(&const_instruction->base.value.data.x_enum_tag, &val->data.x_union.tag);
return &const_instruction->base;
}
- IrInstruction *result = ir_build_enum_tag(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
+ IrInstruction *result = ir_build_union_tag(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
result->value.type = tag_type;
return result;
}
@@ -12880,7 +12840,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
return ir_unreach_error(ira);
if (case_value->value.type->id == TypeTableEntryIdEnum) {
- case_value = ir_analyze_enum_tag(ira, &switch_br_instruction->base, case_value);
+ case_value = ir_analyze_union_tag(ira, &switch_br_instruction->base, case_value);
if (type_is_invalid(case_value->value.type))
return ir_unreach_error(ira);
}
@@ -12927,7 +12887,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
continue;
if (new_value->value.type->id == TypeTableEntryIdEnum) {
- new_value = ir_analyze_enum_tag(ira, &switch_br_instruction->base, new_value);
+ new_value = ir_analyze_union_tag(ira, &switch_br_instruction->base, new_value);
if (type_is_invalid(new_value->value.type))
continue;
}
@@ -13009,34 +12969,54 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
ir_build_load_ptr_from(&ira->new_irb, &switch_target_instruction->base, target_value_ptr);
return target_type;
- case TypeTableEntryIdEnum:
- {
- TypeTableEntry *tag_type = target_type->data.enumeration.tag_type;
- assert(tag_type != nullptr);
- if (pointee_val) {
- ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base);
- bigint_init_bigint(&out_val->data.x_bigint, &pointee_val->data.x_enum.tag);
- return tag_type;
- }
-
- IrInstruction *enum_value = ir_build_load_ptr(&ira->new_irb, switch_target_instruction->base.scope,
- switch_target_instruction->base.source_node, target_value_ptr);
- enum_value->value.type = target_type;
- ir_build_enum_tag_from(&ira->new_irb, &switch_target_instruction->base, enum_value);
+ case TypeTableEntryIdUnion: {
+ if (target_type->data.unionation.gen_tag_index == SIZE_MAX) {
+ ErrorMsg *msg = ir_add_error(ira, target_value_ptr,
+ buf_sprintf("switch on union which has no attached enum"));
+ add_error_note(ira->codegen, msg, target_type->data.unionation.decl_node,
+ buf_sprintf("union declared here"));
+ return ira->codegen->builtin_types.entry_invalid;
+ }
+ TypeTableEntry *tag_type = target_type->data.unionation.tag_type;
+ assert(tag_type != nullptr);
+ if (pointee_val) {
+ ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base);
+ bigint_init_bigint(&out_val->data.x_enum_tag, &pointee_val->data.x_union.tag);
return tag_type;
}
+
+ IrInstruction *union_value = ir_build_load_ptr(&ira->new_irb, switch_target_instruction->base.scope,
+ switch_target_instruction->base.source_node, target_value_ptr);
+ union_value->value.type = target_type;
+
+ IrInstruction *union_tag_inst = ir_build_union_tag(&ira->new_irb, switch_target_instruction->base.scope,
+ switch_target_instruction->base.source_node, union_value);
+ union_tag_inst->value.type = tag_type;
+ ir_link_new_instruction(union_tag_inst, &switch_target_instruction->base);
+ return tag_type;
+ }
+ case TypeTableEntryIdEnum: {
+ if (pointee_val) {
+ ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base);
+ bigint_init_bigint(&out_val->data.x_enum_tag, &pointee_val->data.x_enum_tag);
+ return target_type;
+ }
+
+ IrInstruction *enum_value = ir_build_load_ptr(&ira->new_irb, switch_target_instruction->base.scope,
+ switch_target_instruction->base.source_node, target_value_ptr);
+ enum_value->value.type = target_type;
+ ir_link_new_instruction(enum_value, &switch_target_instruction->base);
+ return target_type;
+ }
case TypeTableEntryIdErrorUnion:
- // see https://github.com/andrewrk/zig/issues/83
+ // see https://github.com/andrewrk/zig/issues/632
zig_panic("TODO switch on error union");
- case TypeTableEntryIdEnumTag:
- zig_panic("TODO switch on enum tag type");
case TypeTableEntryIdUnreachable:
case TypeTableEntryIdArray:
case TypeTableEntryIdStruct:
case TypeTableEntryIdUndefLit:
case TypeTableEntryIdNullLit:
case TypeTableEntryIdMaybe:
- case TypeTableEntryIdUnion:
case TypeTableEntryIdBlock:
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdArgTuple:
@@ -13059,19 +13039,13 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
assert(target_value_ptr->value.type->id == TypeTableEntryIdPointer);
TypeTableEntry *target_type = target_value_ptr->value.type->data.pointer.child_type;
- if (target_type->id == TypeTableEntryIdEnum) {
+ if (target_type->id == TypeTableEntryIdUnion) {
ConstExprValue *prong_val = ir_resolve_const(ira, prong_value, UndefBad);
if (!prong_val)
return ira->codegen->builtin_types.entry_invalid;
- TypeEnumField *field;
- if (prong_value->value.type->id == TypeTableEntryIdEnumTag) {
- field = find_enum_field_by_tag(target_type, &prong_val->data.x_bigint);
- } else if (prong_value->value.type->id == TypeTableEntryIdEnum) {
- field = find_enum_field_by_tag(target_type, &prong_val->data.x_enum.tag);
- } else {
- zig_unreachable();
- }
+ assert(prong_value->value.type->id == TypeTableEntryIdEnum);
+ TypeUnionField *field = find_union_field_by_tag(target_type, &prong_val->data.x_enum_tag);
if (instr_is_comptime(target_value_ptr)) {
ConstExprValue *target_val_ptr = ir_resolve_const(ira, target_value_ptr, UndefBad);
@@ -13082,11 +13056,11 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
out_val->data.x_ptr.special = ConstPtrSpecialRef;
out_val->data.x_ptr.mut = target_val_ptr->data.x_ptr.mut;
- out_val->data.x_ptr.data.ref.pointee = pointee_val->data.x_enum.payload;
+ out_val->data.x_ptr.data.ref.pointee = pointee_val->data.x_union.payload;
return get_pointer_to_type(ira->codegen, field->type_entry, target_val_ptr->type->data.pointer.is_const);
}
- ir_build_enum_field_ptr_from(&ira->new_irb, &instruction->base, target_value_ptr, field);
+ ir_build_union_field_ptr_from(&ira->new_irb, &instruction->base, target_value_ptr, field);
return get_pointer_to_type(ira->codegen, field->type_entry,
target_value_ptr->value.type->data.pointer.is_const);
} else {
@@ -13096,10 +13070,10 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
}
}
-static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira, IrInstructionEnumTag *enum_tag_instruction) {
- IrInstruction *value = enum_tag_instruction->value->other;
- IrInstruction *new_instruction = ir_analyze_enum_tag(ira, &enum_tag_instruction->base, value);
- ir_link_new_instruction(new_instruction, &enum_tag_instruction->base);
+static TypeTableEntry *ir_analyze_instruction_union_tag(IrAnalyze *ira, IrInstructionUnionTag *instruction) {
+ IrInstruction *value = instruction->value->other;
+ IrInstruction *new_instruction = ir_analyze_union_tag(ira, &instruction->base, value);
+ ir_link_new_instruction(new_instruction, &instruction->base);
return new_instruction->value.type;
}
@@ -13255,7 +13229,7 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir
ConstExprValue *out_val = ir_build_const_from(ira, instruction);
out_val->data.x_union.payload = field_val;
- out_val->data.x_union.tag = type_field->value;
+ out_val->data.x_union.tag = type_field->enum_field->value;
ConstParent *parent = get_const_val_parent(ira->codegen, field_val);
if (parent != nullptr) {
@@ -13502,46 +13476,9 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
buf_ptr(&container_type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
- } else if (container_type_value->value.type->id == TypeTableEntryIdEnumTag) {
- if (elem_count != 1) {
- ir_add_error(ira, &instruction->base, buf_sprintf("enum initialization requires exactly one element"));
- return ira->codegen->builtin_types.entry_invalid;
- }
- ConstExprValue *tag_value = ir_resolve_const(ira, container_type_value, UndefBad);
- if (!tag_value)
- return ira->codegen->builtin_types.entry_invalid;
-
- TypeTableEntry *enum_type = container_type_value->value.type->data.enum_tag.enum_type;
-
- TypeEnumField *field = find_enum_field_by_tag(enum_type, &tag_value->data.x_bigint);
- assert(field != nullptr);
- TypeTableEntry *this_field_type = field->type_entry;
-
- IrInstruction *init_value = instruction->items[0]->other;
- if (type_is_invalid(init_value->value.type))
- return ira->codegen->builtin_types.entry_invalid;
-
- IrInstruction *casted_init_value = ir_implicit_cast(ira, init_value, this_field_type);
- if (casted_init_value == ira->codegen->invalid_instruction)
- return ira->codegen->builtin_types.entry_invalid;
-
- if (instr_is_comptime(casted_init_value)) {
- ConstExprValue *init_val = ir_resolve_const(ira, casted_init_value, UndefOk);
- if (!init_val)
- return ira->codegen->builtin_types.entry_invalid;
- ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
- bigint_init_bigint(&out_val->data.x_enum.tag, &tag_value->data.x_bigint);
- out_val->data.x_enum.payload = init_val;
- return enum_type;
- }
-
- IrInstruction *new_instruction = ir_build_init_enum_from(&ira->new_irb, &instruction->base,
- enum_type, field, casted_init_value);
- ir_add_alloca(ira, new_instruction, enum_type);
- return enum_type;
} else {
ir_add_error(ira, container_type_value,
- buf_sprintf("expected type, found '%s'", buf_ptr(&container_type_value->value.type->name)));
+ buf_sprintf("expected type, found '%s' value", buf_ptr(&container_type_value->value.type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
}
@@ -13584,8 +13521,8 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_
eval_min_max_value(ira->codegen, target_type, out_val, is_max);
return target_type;
}
- case TypeTableEntryIdEnumTag:
- zig_panic("TODO min/max value for enum tag type");
+ case TypeTableEntryIdEnum:
+ zig_panic("TODO min/max value for enum type");
case TypeTableEntryIdVar:
case TypeTableEntryIdMetaType:
case TypeTableEntryIdUnreachable:
@@ -13599,7 +13536,6 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_
case TypeTableEntryIdMaybe:
case TypeTableEntryIdErrorUnion:
case TypeTableEntryIdPureError:
- case TypeTableEntryIdEnum:
case TypeTableEntryIdUnion:
case TypeTableEntryIdFn:
case TypeTableEntryIdNamespace:
@@ -13707,20 +13643,18 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIn
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
- assert(target->value.type->id == TypeTableEntryIdEnumTag);
+ assert(target->value.type->id == TypeTableEntryIdEnum);
if (instr_is_comptime(target)) {
- TypeTableEntry *enum_type = target->value.type->data.enum_tag.enum_type;
- uint64_t tag_value = bigint_as_unsigned(&target->value.data.x_bigint);
- TypeEnumField *field = &enum_type->data.enumeration.fields[tag_value];
+ TypeEnumField *field = find_enum_field_by_tag(target->value.type, &target->value.data.x_bigint);
ConstExprValue *array_val = create_const_str_lit(ira->codegen, field->name);
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
init_const_slice(ira->codegen, out_val, array_val, 0, buf_len(field->name), true);
return out_val->type;
}
- if (!target->value.type->data.enum_tag.generate_name_table) {
- target->value.type->data.enum_tag.generate_name_table = true;
+ if (!target->value.type->data.enumeration.generate_name_table) {
+ target->value.type->data.enumeration.generate_name_table = true;
ira->codegen->name_table_enums.append(target->value.type);
}
@@ -13869,7 +13803,7 @@ static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
TypeTableEntry *result_type = var_value->data.x_type;
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
- bigint_init_unsigned(&out_val->data.x_enum.tag, type_id_index(type_entry->id));
+ bigint_init_unsigned(&out_val->data.x_enum_tag, type_id_index(type_entry->id));
return result_type;
}
@@ -14698,14 +14632,14 @@ static TypeTableEntry *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInst
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
out_val->data.x_type = field->type_entry;
return ira->codegen->builtin_types.entry_type;
- } else if (container_type->id == TypeTableEntryIdEnum) {
- if (member_index >= container_type->data.enumeration.src_field_count) {
+ } else if (container_type->id == TypeTableEntryIdUnion) {
+ if (member_index >= container_type->data.unionation.src_field_count) {
ir_add_error(ira, index_value,
buf_sprintf("member index %" ZIG_PRI_u64 " out of bounds; '%s' has %" PRIu32 " members",
- member_index, buf_ptr(&container_type->name), container_type->data.enumeration.src_field_count));
+ member_index, buf_ptr(&container_type->name), container_type->data.unionation.src_field_count));
return ira->codegen->builtin_types.entry_invalid;
}
- TypeEnumField *field = &container_type->data.enumeration.fields[member_index];
+ TypeUnionField *field = &container_type->data.unionation.fields[member_index];
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
out_val->data.x_type = field->type_entry;
@@ -14752,6 +14686,18 @@ static TypeTableEntry *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInst
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
init_const_str_lit(ira->codegen, out_val, field->name);
return out_val->type;
+ } else if (container_type->id == TypeTableEntryIdUnion) {
+ if (member_index >= container_type->data.unionation.src_field_count) {
+ ir_add_error(ira, index_value,
+ buf_sprintf("member index %" ZIG_PRI_u64 " out of bounds; '%s' has %" PRIu32 " members",
+ member_index, buf_ptr(&container_type->name), container_type->data.unionation.src_field_count));
+ return ira->codegen->builtin_types.entry_invalid;
+ }
+ TypeUnionField *field = &container_type->data.unionation.fields[member_index];
+
+ ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
+ init_const_str_lit(ira->codegen, out_val, field->name);
+ return out_val->type;
} else {
ir_add_error(ira, container_type_value,
buf_sprintf("type '%s' does not support @memberName", buf_ptr(&container_type->name)));
@@ -14819,7 +14765,6 @@ static TypeTableEntry *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruc
case TypeTableEntryIdErrorUnion:
case TypeTableEntryIdPureError:
case TypeTableEntryIdEnum:
- case TypeTableEntryIdEnumTag:
case TypeTableEntryIdUnion:
case TypeTableEntryIdFn:
{
@@ -15125,10 +15070,9 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
if (type_is_invalid(switch_type))
return ira->codegen->builtin_types.entry_invalid;
- if (switch_type->id == TypeTableEntryIdEnumTag) {
- TypeTableEntry *enum_type = switch_type->data.enum_tag.enum_type;
+ if (switch_type->id == TypeTableEntryIdEnum) {
HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> field_prev_uses = {};
- field_prev_uses.init(enum_type->data.enumeration.src_field_count);
+ field_prev_uses.init(switch_type->data.enumeration.src_field_count);
for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i];
@@ -15141,22 +15085,13 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
if (type_is_invalid(end_value->value.type))
return ira->codegen->builtin_types.entry_invalid;
+ assert(start_value->value.type->id == TypeTableEntryIdEnum);
BigInt start_index;
+ bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);
+
+ assert(end_value->value.type->id == TypeTableEntryIdEnum);
BigInt end_index;
- if (start_value->value.type->id == TypeTableEntryIdEnumTag) {
- bigint_init_bigint(&start_index, &start_value->value.data.x_bigint);
- } else if (start_value->value.type->id == TypeTableEntryIdEnum) {
- bigint_init_bigint(&start_index, &start_value->value.data.x_enum.tag);
- } else {
- zig_unreachable();
- }
- if (end_value->value.type->id == TypeTableEntryIdEnumTag) {
- bigint_init_bigint(&end_index, &end_value->value.data.x_bigint);
- } else if (end_value->value.type->id == TypeTableEntryIdEnum) {
- bigint_init_bigint(&end_index, &end_value->value.data.x_enum.tag);
- } else {
- zig_unreachable();
- }
+ bigint_init_bigint(&end_index, &end_value->value.data.x_enum_tag);
BigInt field_index;
bigint_init_bigint(&field_index, &start_index);
@@ -15168,10 +15103,10 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
auto entry = field_prev_uses.put_unique(field_index, start_value->source_node);
if (entry) {
AstNode *prev_node = entry->value;
- TypeEnumField *enum_field = find_enum_field_by_tag(enum_type, &field_index);
+ TypeEnumField *enum_field = find_enum_field_by_tag(switch_type, &field_index);
assert(enum_field != nullptr);
ErrorMsg *msg = ir_add_error(ira, start_value,
- buf_sprintf("duplicate switch value: '%s.%s'", buf_ptr(&enum_type->name),
+ buf_sprintf("duplicate switch value: '%s.%s'", buf_ptr(&switch_type->name),
buf_ptr(enum_field->name)));
add_error_note(ira->codegen, msg, prev_node, buf_sprintf("other value is here"));
}
@@ -15179,13 +15114,13 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
}
}
if (!instruction->have_else_prong) {
- for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
- TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];
+ for (uint32_t i = 0; i < switch_type->data.enumeration.src_field_count; i += 1) {
+ TypeEnumField *enum_field = &switch_type->data.enumeration.fields[i];
auto entry = field_prev_uses.maybe_get(enum_field->value);
if (!entry) {
ir_add_error(ira, &instruction->base,
- buf_sprintf("enumeration value '%s.%s' not handled in switch", buf_ptr(&enum_type->name),
+ buf_sprintf("enumeration value '%s.%s' not handled in switch", buf_ptr(&switch_type->name),
buf_ptr(enum_field->name)));
}
}
@@ -15481,8 +15416,6 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
zig_panic("TODO buf_write_value_bytes pure error type");
case TypeTableEntryIdEnum:
zig_panic("TODO buf_write_value_bytes enum type");
- case TypeTableEntryIdEnumTag:
- zig_panic("TODO buf_write_value_bytes enum tag type");
case TypeTableEntryIdFn:
zig_panic("TODO buf_write_value_bytes fn type");
case TypeTableEntryIdUnion:
@@ -15541,8 +15474,6 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
zig_panic("TODO buf_read_value_bytes pure error type");
case TypeTableEntryIdEnum:
zig_panic("TODO buf_read_value_bytes enum type");
- case TypeTableEntryIdEnumTag:
- zig_panic("TODO buf_read_value_bytes enum tag type");
case TypeTableEntryIdFn:
zig_panic("TODO buf_read_value_bytes fn type");
case TypeTableEntryIdUnion:
@@ -15920,11 +15851,8 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag_type(IrAnalyze *ira, IrIn
if (type_is_invalid(enum_type))
return ira->codegen->builtin_types.entry_invalid;
- TypeTableEntry *non_int_tag_type = enum_type->data.enumeration.tag_type;
- assert(non_int_tag_type->id == TypeTableEntryIdEnumTag);
-
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
- out_val->data.x_type = non_int_tag_type->data.enum_tag.int_type;
+ out_val->data.x_type = enum_type->data.enumeration.tag_int_type;
return ira->codegen->builtin_types.entry_type;
}
@@ -15938,9 +15866,7 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
case IrInstructionIdStructInit:
case IrInstructionIdUnionInit:
case IrInstructionIdStructFieldPtr:
- case IrInstructionIdEnumFieldPtr:
case IrInstructionIdUnionFieldPtr:
- case IrInstructionIdInitEnum:
case IrInstructionIdMaybeWrap:
case IrInstructionIdErrWrapCode:
case IrInstructionIdErrWrapPayload:
@@ -16012,8 +15938,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_switch_target(ira, (IrInstructionSwitchTarget *)instruction);
case IrInstructionIdSwitchVar:
return ir_analyze_instruction_switch_var(ira, (IrInstructionSwitchVar *)instruction);
- case IrInstructionIdEnumTag:
- return ir_analyze_instruction_enum_tag(ira, (IrInstructionEnumTag *)instruction);
+ case IrInstructionIdUnionTag:
+ return ir_analyze_instruction_union_tag(ira, (IrInstructionUnionTag *)instruction);
case IrInstructionIdImport:
return ir_analyze_instruction_import(ira, (IrInstructionImport *)instruction);
case IrInstructionIdArrayLen:
@@ -16260,7 +16186,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdPtrTypeChild:
case IrInstructionIdArrayLen:
case IrInstructionIdStructFieldPtr:
- case IrInstructionIdEnumFieldPtr:
case IrInstructionIdUnionFieldPtr:
case IrInstructionIdArrayType:
case IrInstructionIdSliceType:
@@ -16271,7 +16196,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdCtz:
case IrInstructionIdSwitchVar:
case IrInstructionIdSwitchTarget:
- case IrInstructionIdEnumTag:
+ case IrInstructionIdUnionTag:
case IrInstructionIdRef:
case IrInstructionIdMinValue:
case IrInstructionIdMaxValue:
@@ -16293,7 +16218,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdErrWrapPayload:
case IrInstructionIdFnProto:
case IrInstructionIdTestComptime:
- case IrInstructionIdInitEnum:
case IrInstructionIdPtrCast:
case IrInstructionIdBitCast:
case IrInstructionIdWidenOrShorten: