summaryrefslogtreecommitdiff
path: root/src/mir/cleanup.cpp
diff options
context:
space:
mode:
authorJohn Hodge <tpg@ucc.asn.au>2017-11-01 20:46:36 +0800
committerJohn Hodge <tpg@ucc.asn.au>2017-11-01 20:46:36 +0800
commitcb271f8ea98d1a5c65a5e636a0e73a85710027b4 (patch)
tree9694427c074c0e0d581cde6e01b6167016e0f844 /src/mir/cleanup.cpp
parent8306f43ccdf0414b48891aa5eb04d8901899c052 (diff)
downloadmrust-cb271f8ea98d1a5c65a5e636a0e73a85710027b4.tar.gz
HIR - Refactor enums to only embed a single field (and give variants types)
Diffstat (limited to 'src/mir/cleanup.cpp')
-rw-r--r--src/mir/cleanup.cpp46
1 files changed, 16 insertions, 30 deletions
diff --git a/src/mir/cleanup.cpp b/src/mir/cleanup.cpp
index 9718f046..455eef43 100644
--- a/src/mir/cleanup.cpp
+++ b/src/mir/cleanup.cpp
@@ -289,7 +289,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR
}
)
)
- return ::MIR::RValue::make_Struct({ te.path.m_data.as_Generic().clone(), ~0u, mv$(lvals) });
+ return ::MIR::RValue::make_Struct({ te.path.m_data.as_Generic().clone(), mv$(lvals) });
}
else if( te.binding.is_Enum() )
{
@@ -298,33 +298,19 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR
auto monomorph = [&](const auto& tpl) { return monomorphise_type(state.sp, enm.m_params, te.path.m_data.as_Generic().m_params, tpl); };
- ::std::vector< ::MIR::Param> lvals;
- MIR_ASSERT(state, lit_var.idx < enm.m_variants.size(), "Variant index out of range");
- TU_MATCHA( (enm.m_variants[lit_var.idx].second), (ve),
- (Unit,
- ),
- (Value,
- ),
- (Tuple,
- MIR_ASSERT(state, lit_var.vals.size() == ve.size(), "Value count mismatch in literal for " << ty << " #" << lit_var.idx << " - exp " << ve.size() << ", " << lit);
- for(unsigned int i = 0; i < ve.size(); i ++)
- {
- auto ent_ty = monomorph(ve[i].ent);
- auto rval = MIR_Cleanup_LiteralToRValue(state, mutator, lit_var.vals[i], ent_ty.clone(), ::HIR::GenericPath());
- lvals.push_back( mutator.in_temporary(mv$(ent_ty), mv$(rval)) );
- }
- ),
- (Struct,
- MIR_ASSERT(state, lit_var.vals.size() == ve.size(), "Value count mismatch in literal for " << ty << " #" << lit_var.idx << " - exp " << ve.size() << ", " << lit);
- for(unsigned int i = 0; i < ve.size(); i ++)
- {
- auto ent_ty = monomorph(ve[i].second.ent);
- auto rval = MIR_Cleanup_LiteralToRValue(state, mutator, lit_var.vals[i], ent_ty.clone(), ::HIR::GenericPath());
- lvals.push_back( mutator.in_temporary(mv$(ent_ty), mv$(rval)) );
- }
- )
- )
- return ::MIR::RValue::make_Struct({ te.path.m_data.as_Generic().clone(), lit_var.idx, mv$(lvals) });
+ MIR_ASSERT(state, lit_var.idx < enm.num_variants(), "Variant index out of range");
+ ::MIR::Param p;
+ if( const auto* e = enm.m_data.opt_Data() )
+ {
+ auto ty = monomorph( e->at(lit_var.idx).type );
+ auto rval = MIR_Cleanup_LiteralToRValue(state, mutator, *lit_var.val, ty.clone(), ::HIR::GenericPath());
+ p = mutator.in_temporary(mv$(ty), mv$(rval));
+ }
+ else
+ {
+ p = mutator.in_temporary(::HIR::TypeRef::new_unit(), ::MIR::RValue::make_Tuple({}));
+ }
+ return ::MIR::RValue::make_Variant({ te.path.m_data.as_Generic().clone(), lit_var.idx, mv$(p) });
}
else
{
@@ -540,7 +526,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR
)
auto new_path = ty_path.clone();
- return mutator.in_temporary( mv$(ty), ::MIR::RValue::make_Struct({ mv$(new_path), ~0u, mv$(vals) }) );
+ return mutator.in_temporary( mv$(ty), ::MIR::RValue::make_Struct({ mv$(new_path), mv$(vals) }) );
}
else if( ty.m_data.is_Pointer() )
{
@@ -820,7 +806,7 @@ bool MIR_Cleanup_Unsize_GetMetadata(const ::MIR::TypeResolve& state, MirMutator&
}
)
)
- return ::MIR::RValue::make_Struct({ dte.path.m_data.as_Generic().clone(), ~0u, mv$(ents) });
+ return ::MIR::RValue::make_Struct({ dte.path.m_data.as_Generic().clone(), mv$(ents) });
}
if( dst_ty.m_data.is_Borrow() )