diff options
author | John Hodge <tpg@ucc.asn.au> | 2018-06-04 10:49:58 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2018-06-04 10:49:58 +0800 |
commit | 776fd7c37e679762cfa2870c1c1e1e9e76b8ecb6 (patch) | |
tree | dbb3cbb4da3985a6b56f6f68dc13285c240b989a | |
parent | bf8f8b4b4a9fe273451be59f68acafbe61968b83 (diff) | |
download | mrust-776fd7c37e679762cfa2870c1c1e1e9e76b8ecb6.tar.gz |
MIR Helpers - Handle ItemAddr to enum/struct constructors
-rw-r--r-- | src/hir/hir.cpp | 5 | ||||
-rw-r--r-- | src/hir_typeck/static.cpp | 27 | ||||
-rw-r--r-- | src/hir_typeck/static.hpp | 6 | ||||
-rw-r--r-- | src/mir/check.cpp | 1 | ||||
-rw-r--r-- | src/mir/helpers.cpp | 47 |
5 files changed, 81 insertions, 5 deletions
diff --git a/src/hir/hir.cpp b/src/hir/hir.cpp index 361a40c8..ee66ed5b 100644 --- a/src/hir/hir.cpp +++ b/src/hir/hir.cpp @@ -1129,7 +1129,10 @@ const ::HIR::Module& ::HIR::Crate::get_mod_by_path(const Span& sp, const ::HIR:: return e; ) else { - BUG(sp, "Module path " << path << " didn't point to a module"); + if( ignore_last_node ) + BUG(sp, "Parent path of " << path << " didn't point to a module"); + else + BUG(sp, "Module path " << path << " didn't point to a module"); } } } diff --git a/src/hir_typeck/static.cpp b/src/hir_typeck/static.cpp index 3629c6e4..8000f5af 100644 --- a/src/hir_typeck/static.cpp +++ b/src/hir_typeck/static.cpp @@ -1894,25 +1894,46 @@ StaticTraitResolve::ValuePtr StaticTraitResolve::get_value(const Span& sp, const out_params = MonomorphState {}; TU_MATCHA( (p.m_data), (pe), (Generic, - out_params.pp_method = &pe.m_params; - const ::HIR::Module& mod = m_crate.get_mod_by_path(sp, pe.m_path, true); + if( pe.m_path.m_components.size() > 1 ) + { + const auto& ti = m_crate.get_typeitem_by_path(sp, pe.m_path, /*ignore_crate_name=*/false, /*ignore_last_node=*/true); + if( const auto* e = ti.opt_Enum() ) + { + out_params.pp_impl = &pe.m_params; + auto idx = e->find_variant(pe.m_path.m_components.back()); + if( e->m_data.is_Data() ) + { + if( e->m_data.as_Data()[idx].type != ::HIR::TypeRef::new_unit() ) + { + return ValuePtr::Data_EnumConstructor { e, idx }; + } + } + return ValuePtr::Data_EnumValue { e, idx }; + } + } + const ::HIR::Module& mod = m_crate.get_mod_by_path(sp, pe.m_path, /*ignore_last_node=*/true); const auto& v = mod.m_value_items.at(pe.m_path.m_components.back()); TU_MATCHA( (v->ent), (ve), (Import, BUG(sp, "Module Import");), (Constant, + out_params.pp_method = &pe.m_params; return &ve; ), (Static, + out_params.pp_method = &pe.m_params; return &ve; ), (Function, + out_params.pp_method = &pe.m_params; return &ve; ), (StructConstant, + out_params.pp_impl = &pe.m_params; TODO(sp, "StructConstant - " << p); ), (StructConstructor, - TODO(sp, "StructConstructor - " << p); + out_params.pp_impl = &pe.m_params; + return ValuePtr::Data_StructConstructor { &ve.ty, &m_crate.get_struct_by_path(sp, ve.ty) }; ) ) throw ""; diff --git a/src/hir_typeck/static.hpp b/src/hir_typeck/static.hpp index 82ede197..8c9cb9a6 100644 --- a/src/hir_typeck/static.hpp +++ b/src/hir_typeck/static.hpp @@ -192,7 +192,11 @@ public: (NotFound, struct{}), (Constant, const ::HIR::Constant*), (Static, const ::HIR::Static*), - (Function, const ::HIR::Function*) + (Function, const ::HIR::Function*), + (EnumConstructor, struct { const ::HIR::Enum* e; size_t v; }), + (EnumValue, struct { const ::HIR::Enum* e; size_t v; }), + (StructConstructor, struct { const ::HIR::SimplePath* p; const ::HIR::Struct* s; }), + (StructConstant, struct { const ::HIR::SimplePath* p; const ::HIR::Struct* s; }) ); /// `signature_only` - Returns a pointer to an item with the correct signature, not the actual implementation (faster) diff --git a/src/mir/check.cpp b/src/mir/check.cpp index 17c77cd5..cfcc52d4 100644 --- a/src/mir/check.cpp +++ b/src/mir/check.cpp @@ -345,6 +345,7 @@ void MIR_Validate_ValState(::MIR::TypeResolve& state, const ::MIR::Function& fcn val_state.move_val(state, se); ), (Constant, + //(void)state.get_const_type(se); ), (SizedArray, val_state.move_val(state, se.val); diff --git a/src/mir/helpers.cpp b/src/mir/helpers.cpp index 2cb55e4a..38380fed 100644 --- a/src/mir/helpers.cpp +++ b/src/mir/helpers.cpp @@ -309,6 +309,53 @@ const ::HIR::TypeRef& MIR::TypeResolve::get_param_type(::HIR::TypeRef& tmp, cons auto rv = ::HIR::TypeRef( mv$(ft) ); m_resolve.expand_associated_types(this->sp, rv); return rv; + ), + (EnumValue, + MIR_BUG(*this, "get_const_type - ItemAddr points to an enum value - " << c); + ), + (EnumConstructor, + const auto& data_variant = ve.e->m_data.as_Data()[ve.v]; + MIR_ASSERT(*this, data_variant.type.m_data.is_Path(), c << " enum variant type must be Path - " << data_variant.type); + const auto& dvt_path = data_variant.type.m_data.as_Path(); + MIR_ASSERT(*this, dvt_path.binding.is_Struct(), c << " enum variant type path binding must be Struct - " << data_variant.type); + const auto& str = *dvt_path.binding.as_Struct(); + MIR_ASSERT(*this, str.m_data.is_Tuple(), c << " must point to a tuple-like variant"); + const auto& str_data = str.m_data.as_Tuple(); + + ::HIR::FunctionType ft; + ft.is_unsafe = false; + ft.m_abi = ABI_RUST; + auto enum_path = e.clone(); + enum_path.m_data.as_Generic().m_path.m_components.pop_back(); + ft.m_rettype = box$( ::HIR::TypeRef::new_path(mv$(enum_path), ve.e) ); + ft.m_arg_types.reserve(str_data.size()); + for(const auto& fld : str_data) + ft.m_arg_types.push_back( p.monomorph(this->sp, fld.ent) ); + + auto rv = ::HIR::TypeRef( mv$(ft) ); + m_resolve.expand_associated_types(this->sp, rv); + return rv; + ), + (StructConstant, + MIR_BUG(*this, c << " pointing to a struct constant"); + ), + (StructConstructor, + // TODO: Move this to a method on the struct? + const auto& str = *ve.s; + MIR_ASSERT(*this, str.m_data.is_Tuple(), c << " must point to a tuple-like struct"); + const auto& str_data = str.m_data.as_Tuple(); + + ::HIR::FunctionType ft; + ft.is_unsafe = false; + ft.m_abi = ABI_RUST; + ft.m_rettype = box$( ::HIR::TypeRef::new_path( ::HIR::GenericPath(*ve.p, e.m_data.as_Generic().m_params.clone()), &str) ); + ft.m_arg_types.reserve(str_data.size()); + for(const auto& fld : str_data) + ft.m_arg_types.push_back( p.monomorph(this->sp, fld.ent) ); + + auto rv = ::HIR::TypeRef( mv$(ft) ); + m_resolve.expand_associated_types(this->sp, rv); + return rv; ) ) ) |