diff options
author | John Hodge <tpg@mutabah.net> | 2016-10-30 18:34:38 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-10-30 18:34:38 +0800 |
commit | e87f3ee6f2c8bbbfd1e18285528dfa305479b2ee (patch) | |
tree | 186e4c51de05194188a62d75e0e44e1a6007a0e2 | |
parent | 649f9f98efe3f1b138c34f3762a5feba9dde457c (diff) | |
download | mrust-e87f3ee6f2c8bbbfd1e18285528dfa305479b2ee.tar.gz |
HIR+Resolve - Refactor handling of enum variant imports
-rw-r--r-- | src/hir/deserialise.cpp | 4 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 31 | ||||
-rw-r--r-- | src/hir/hir.hpp | 4 | ||||
-rw-r--r-- | src/hir/serialise.cpp | 8 | ||||
-rw-r--r-- | src/resolve/absolute.cpp | 38 | ||||
-rw-r--r-- | src/resolve/index.cpp | 14 | ||||
-rw-r--r-- | src/resolve/use.cpp | 20 |
7 files changed, 66 insertions, 53 deletions
diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index 4c8b1a43..3cbf442b 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -408,7 +408,7 @@ namespace { switch( m_in.read_tag() ) { case 0: - return ::HIR::TypeItem( deserialise_simplepath() ); + return ::HIR::TypeItem({ deserialise_simplepath(), m_in.read_bool(), static_cast<unsigned int>(m_in.read_count()) }); case 1: return ::HIR::TypeItem( deserialise_module() ); case 2: @@ -428,7 +428,7 @@ namespace { switch( m_in.read_tag() ) { case 0: - return ::HIR::ValueItem( deserialise_simplepath() ); + return ::HIR::ValueItem({ deserialise_simplepath(), m_in.read_bool(), static_cast<unsigned int>(m_in.read_count()) }); case 1: return ::HIR::ValueItem( deserialise_constant() ); case 2: diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index b0185596..a3957d2b 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -1096,7 +1096,7 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H (Crate, // All 'extern crate' items should be normalised into a list in the crate root // - If public, add a namespace import here referring to the root of the imported crate - _add_mod_ns_item( mod, item.name, item.is_pub, ::HIR::TypeItem::make_Import( ::HIR::SimplePath(e.name, {}) ) ); + _add_mod_ns_item( mod, item.name, item.is_pub, ::HIR::TypeItem::make_Import({ ::HIR::SimplePath(e.name, {}), false, 0} ) ); ), (Type, _add_mod_ns_item( mod, item.name, item.is_pub, ::HIR::TypeItem::make_TypeAlias( LowerHIR_TypeAlias(e) ) ); @@ -1141,17 +1141,34 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H for( const auto& ie : ast_mod.m_namespace_items ) { if( ie.second.is_import ) { - _add_mod_ns_item(mod, ie.first, ie.second.is_pub, ::HIR::TypeItem::make_Import({ - LowerHIR_SimplePath( Span(), ie.second.path ) - }) ); + auto hir_path = LowerHIR_SimplePath( Span(), ie.second.path ); + ::HIR::TypeItem ti; + TU_MATCH_DEF( ::AST::PathBinding, (ie.second.path.binding()), (pb), + ( + ti = ::HIR::TypeItem::make_Import({ mv$(hir_path), false, 0 }); + ), + (EnumVar, + ti = ::HIR::TypeItem::make_Import({ mv$(hir_path), true, pb.idx }); + ) + ) + _add_mod_ns_item(mod, ie.first, ie.second.is_pub, mv$(ti)); } } for( const auto& ie : ast_mod.m_value_items ) { if( ie.second.is_import ) { - _add_mod_val_item(mod, ie.first, ie.second.is_pub, ::HIR::ValueItem::make_Import({ - LowerHIR_SimplePath( Span(), ie.second.path ) - }) ); + auto hir_path = LowerHIR_SimplePath( Span(), ie.second.path ); + ::HIR::ValueItem vi; + + TU_MATCH_DEF( ::AST::PathBinding, (ie.second.path.binding()), (pb), + ( + vi = ::HIR::ValueItem::make_Import({ mv$(hir_path), false, 0 }); + ), + (EnumVar, + vi = ::HIR::ValueItem::make_Import({ mv$(hir_path), true, pb.idx }); + ) + ) + _add_mod_val_item(mod, ie.first, ie.second.is_pub, mv$(vi)); } } diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index 6c560510..fed2e577 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -257,7 +257,7 @@ public: // -------------------------------------------------------------------- TAGGED_UNION(TypeItem, Import, - (Import, ::HIR::SimplePath), // `pub use` statements (no globs) + (Import, struct { ::HIR::SimplePath path; bool is_variant; unsigned int idx; }), (Module, Module), (TypeAlias, TypeAlias), // NOTE: These don't introduce new values (Enum, Enum), @@ -265,7 +265,7 @@ TAGGED_UNION(TypeItem, Import, (Trait, Trait) ); TAGGED_UNION(ValueItem, Import, - (Import, ::HIR::SimplePath), + (Import, struct { ::HIR::SimplePath path; bool is_variant; unsigned int idx; }), (Constant, Constant), (Static, Static), (StructConstant, struct { ::HIR::SimplePath ty; }), diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp index c7224840..cb928832 100644 --- a/src/hir/serialise.cpp +++ b/src/hir/serialise.cpp @@ -610,7 +610,9 @@ namespace { TU_MATCHA( (item), (e), (Import, m_out.write_tag(0); - serialise_simplepath(e); + serialise_simplepath(e.path); + m_out.write_bool(e.is_variant); + m_out.write_bool(e.idx); ), (Module, m_out.write_tag(1); @@ -639,7 +641,9 @@ namespace { TU_MATCHA( (item), (e), (Import, m_out.write_tag(0); - serialise_simplepath(e); + serialise_simplepath(e.path); + m_out.write_bool(e.is_variant); + m_out.write_bool(e.idx); ), (Constant, m_out.write_tag(1); diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index f9d1b5ea..da3a5619 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -798,8 +798,8 @@ namespace { TU_MATCH(::HIR::TypeItem, (it->second->ent), (e), (Import, // - Update path then restart - auto newpath = AST::Path(e.m_crate_name, {}); - for(const auto& n : e.m_components) + auto newpath = AST::Path(e.path.m_crate_name, {}); + for(const auto& n : e.path.m_components) newpath.nodes().push_back( AST::PathNode(n) ); for(unsigned int j = i + 1; j < path.nodes().size(); j ++) newpath.nodes().push_back( mv$(path.nodes()[j]) ); @@ -904,15 +904,11 @@ namespace { { auto v = hmod->m_mod_items.find(name); if( v != hmod->m_mod_items.end() ) { - const auto& ti = v->second->ent; - if( ti.is_Import() ) { - DEBUG("= Import " << ti.as_Import()); - Resolve_Absolute_Path_BindAbsolute__hir_from_import(context, sp, false, path, ti.as_Import()); - return ; - } TU_MATCH(::HIR::TypeItem, (v->second->ent), (e), (Import, - throw ""; + DEBUG("= Import " << e.path); + Resolve_Absolute_Path_BindAbsolute__hir_from_import(context, sp, false, path, e.path); + return ; ), (Trait, path.bind( ::AST::PathBinding::make_Trait({nullptr, &e}) ); @@ -941,13 +937,13 @@ namespace { { auto v = hmod->m_mod_items.find(name); if( v != hmod->m_mod_items.end() ) { - if( v->second->ent.is_Import() ) { - Resolve_Absolute_Path_BindAbsolute__hir_from_import(context, sp, false, path, v->second->ent.as_Import()); - TODO(sp, "Imports in HIR mod items"); - } TU_MATCH_DEF(::HIR::TypeItem, (v->second->ent), (e), ( ), + (Import, + Resolve_Absolute_Path_BindAbsolute__hir_from_import(context, sp, false, path, e.path); + TODO(sp, "Imports in HIR mod items - " << e.path); + ), (Struct, // Bind and update path path.bind( ::AST::PathBinding::make_Struct({nullptr, &e}) ); @@ -960,12 +956,12 @@ namespace { { auto v = hmod->m_value_items.find(name); if( v != hmod->m_value_items.end() ) { - if( v->second->ent.is_Import() ) { - TODO(sp, "Imports in HIR val items - " << v->second->ent.as_Import()); - } TU_MATCH_DEF(::HIR::ValueItem, (v->second->ent), (e), ( ), + (Import, + TODO(sp, "Imports in HIR val items - " << e.path); + ), (Constant, // Bind and update path path.bind( ::AST::PathBinding::make_Static({nullptr, nullptr}) ); @@ -981,14 +977,10 @@ namespace { { auto v = hmod->m_value_items.find(name); if( v != hmod->m_value_items.end() ) { - if( v->second->ent.is_Import() ) { - Resolve_Absolute_Path_BindAbsolute__hir_from_import(context, sp, true, path, v->second->ent.as_Import()); + TU_MATCH(::HIR::ValueItem, (v->second->ent), (e), + (Import, + Resolve_Absolute_Path_BindAbsolute__hir_from_import(context, sp, true, path, e.path); return ; - } - TU_MATCH_DEF(::HIR::ValueItem, (v->second->ent), (e), - ( - // TODO: Handle other values types - TODO(sp, "Bind item type " << v->second->ent.tag_str()); ), (Function, path.bind( ::AST::PathBinding::make_Function({nullptr/*, &e*/}) ); diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp index 93e5c742..09d3909b 100644 --- a/src/resolve/index.cpp +++ b/src/resolve/index.cpp @@ -268,7 +268,7 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C if( ve.is_public ) { AST::Path p; if( ve.ent.is_Import() ) { - p = hir_to_ast( ve.ent.as_Import() ); + p = hir_to_ast( ve.ent.as_Import().path ); } else { p = path + it.first; @@ -302,7 +302,7 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C AST::Path p; const auto* vep = &ve.ent; if( ve.ent.is_Import() ) { - const auto& spath = ve.ent.as_Import(); + const auto& spath = ve.ent.as_Import().path; p = hir_to_ast( spath ); ASSERT_BUG(sp, crate.m_extern_crates.count(spath.m_crate_name) == 1, "Crate " << spath.m_crate_name << " is not loaded"); @@ -526,15 +526,15 @@ void Resolve_Index_Module_Normalise_Path_ext(const ::AST::Crate& crate, const Sp const auto* item_ptr = &it->second->ent; if( item_ptr->is_Import() ) { const auto& e = item_ptr->as_Import(); - const auto& ec = crate.m_extern_crates.at( e.m_crate_name ); - item_ptr = &ec.m_hir->get_typeitem_by_path(sp, e, true); // ignore_crate_name=true + const auto& ec = crate.m_extern_crates.at( e.path.m_crate_name ); + item_ptr = &ec.m_hir->get_typeitem_by_path(sp, e.path, true); // ignore_crate_name=true } TU_MATCH_DEF(::HIR::TypeItem, (*item_ptr), (e), ( BUG(sp, "Path " << path << " pointed to non-module in component " << i); ), (Import, - BUG(sp, "Recursive import in " << path << " - " << it->second->ent.as_Import() << " -> " << e); + BUG(sp, "Recursive import in " << path << " - " << it->second->ent.as_Import().path << " -> " << e.path); ), (Enum, if( i != info.nodes.size() - 2 ) { @@ -560,7 +560,7 @@ void Resolve_Index_Module_Normalise_Path_ext(const ::AST::Crate& crate, const Sp TU_IFLET( ::HIR::TypeItem, it_m->second->ent, Import, e, // Replace the path with this path (maintaining binding) auto binding = path.binding().clone(); - path = hir_to_ast(e); + path = hir_to_ast(e.path); path.bind( mv$(binding) ); ) return ; @@ -573,7 +573,7 @@ void Resolve_Index_Module_Normalise_Path_ext(const ::AST::Crate& crate, const Sp TU_IFLET( ::HIR::ValueItem, it_v->second->ent, Import, e, // Replace the path with this path (maintaining binding) auto binding = path.binding().clone(); - path = hir_to_ast(e); + path = hir_to_ast(e.path); path.bind( mv$(binding) ); ) return ; diff --git a/src/resolve/use.cpp b/src/resolve/use.cpp index 7063e7f0..b5afcbc3 100644 --- a/src/resolve/use.cpp +++ b/src/resolve/use.cpp @@ -471,10 +471,10 @@ namespace { TU_IFLET( ::HIR::TypeItem, (it->second->ent), Module, mod, hmod = &mod; ) - else TU_IFLET( ::HIR::TypeItem, (it->second->ent), Import, import_path, - hmod = get_hir_mod_by_path(sp, crate, import_path); + else TU_IFLET( ::HIR::TypeItem, (it->second->ent), Import, import, + hmod = get_hir_mod_by_path(sp, crate, import.path); if( !hmod ) - BUG(sp, ""); + BUG(sp, "Import in module position didn't resolve as a module - " << import.path); ) else TU_IFLET( ::HIR::TypeItem, (it->second->ent), Enum, enm, if( &node == &path.m_components.back() ) { @@ -519,7 +519,7 @@ namespace { ), (Import, bool is_enum = false; - auto ptr = get_hir_modenum_by_path(span, crate, e, is_enum); + auto ptr = get_hir_modenum_by_path(span, crate, e.path, is_enum); if( !ptr ) BUG(span, "Path component " << nodes[i].name() << " pointed to non-module (" << path << ")"); if( is_enum ) { @@ -566,12 +566,12 @@ namespace { if( item_ptr->is_Import() ) { const auto& e = item_ptr->as_Import(); // This doesn't need to recurse - it can just do a single layer (as no Import should refer to another) - const auto& ec = crate.m_extern_crates.at( e.m_crate_name ); - item_ptr = &ec.m_hir->get_typeitem_by_path(span, e, true); // ignore_crate_name=true + const auto& ec = crate.m_extern_crates.at( e.path.m_crate_name ); + item_ptr = &ec.m_hir->get_typeitem_by_path(span, e.path, true); // ignore_crate_name=true } TU_MATCHA( (*item_ptr), (e), (Import, - BUG(span, "Recursive import in " << path << " - " << it->second->ent.as_Import() << " -> " << e); + BUG(span, "Recursive import in " << path << " - " << it->second->ent.as_Import().path << " -> " << e.path); ), (Module, return ::AST::PathBinding::make_Module({nullptr, &e}); @@ -599,12 +599,12 @@ namespace { if( item_ptr->is_Import() ) { const auto& e = item_ptr->as_Import(); // This doesn't need to recurse - it can just do a single layer (as no Import should refer to another) - const auto& ec = crate.m_extern_crates.at( e.m_crate_name ); - item_ptr = &ec.m_hir->get_valitem_by_path(span, e, true); // ignore_crate_name=true + const auto& ec = crate.m_extern_crates.at( e.path.m_crate_name ); + item_ptr = &ec.m_hir->get_valitem_by_path(span, e.path, true); // ignore_crate_name=true } TU_MATCHA( (*item_ptr), (e), (Import, - BUG(span, "Recursive import in " << path << " - " << it2->second->ent.as_Import() << " -> " << e); + BUG(span, "Recursive import in " << path << " - " << it2->second->ent.as_Import().path << " -> " << e.path); ), (Constant, return ::AST::PathBinding::make_Static({ nullptr }); |