From cb271f8ea98d1a5c65a5e636a0e73a85710027b4 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 1 Nov 2017 20:46:36 +0800 Subject: HIR - Refactor enums to only embed a single field (and give variants types) --- src/mir/helpers.cpp | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) (limited to 'src/mir/helpers.cpp') diff --git a/src/mir/helpers.cpp b/src/mir/helpers.cpp index ddab9317..1e43e361 100644 --- a/src/mir/helpers.cpp +++ b/src/mir/helpers.cpp @@ -200,34 +200,20 @@ const ::HIR::TypeRef& ::MIR::TypeResolve::get_lvalue_type(::HIR::TypeRef& tmp, c if( te.binding.is_Enum() ) { const auto& enm = *te.binding.as_Enum(); - const auto& variants = enm.m_variants; - MIR_ASSERT(*this, e.variant_index < variants.size(), "Variant index out of range"); + MIR_ASSERT(*this, enm.m_data.is_Data(), "Downcast on non-data enum - " << ty); + const auto& variants = enm.m_data.as_Data(); + MIR_ASSERT(*this, e.variant_index < variants.size(), "Variant index out of range for " << ty); const auto& variant = variants[e.variant_index]; - // TODO: Make data variants refer to associated types (unify enum and struct handling) - TU_MATCHA( (variant.second), (ve), - (Value, - ), - (Unit, - ), - (Tuple, - // HACK! Create tuple. - ::std::vector< ::HIR::TypeRef> tys; - for(const auto& fld : ve) - tys.push_back( monomorphise_type(sp, enm.m_params, te.path.m_data.as_Generic().m_params, fld.ent) ); - tmp = ::HIR::TypeRef( mv$(tys) ); - m_resolve.expand_associated_types(sp, tmp); - return tmp; - ), - (Struct, - // HACK! Create tuple. - ::std::vector< ::HIR::TypeRef> tys; - for(const auto& fld : ve) - tys.push_back( monomorphise_type(sp, enm.m_params, te.path.m_data.as_Generic().m_params, fld.second.ent) ); - tmp = ::HIR::TypeRef( mv$(tys) ); + + const auto& var_ty = variant.type; + if( monomorphise_type_needed(var_ty) ) { + tmp = monomorphise_type(sp, enm.m_params, te.path.m_data.as_Generic().m_params, var_ty); m_resolve.expand_associated_types(sp, tmp); return tmp; - ) - ) + } + else { + return var_ty; + } } else { -- cgit v1.2.3