diff options
author | John Hodge <tpg@mutabah.net> | 2018-12-28 16:58:52 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2018-12-28 16:58:52 +0800 |
commit | e16796df604ba1017a19917ea94840ee8c4b0336 (patch) | |
tree | 5989361e02b98f036612c064229a77d040424942 /src/resolve/index.cpp | |
parent | 83f59c8887adcaa3486cb64c5a78a4660223e234 (diff) | |
download | mrust-e16796df604ba1017a19917ea94840ee8c4b0336.tar.gz |
Resolve - Clean up AST path bindings, one binding per namespace
- This is prep work for supporting `use` of macros
Diffstat (limited to 'src/resolve/index.cpp')
-rw-r--r-- | src/resolve/index.cpp | 197 |
1 files changed, 87 insertions, 110 deletions
diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp index 659bded4..148bf069 100644 --- a/src/resolve/index.cpp +++ b/src/resolve/index.cpp @@ -125,46 +125,47 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) ), // - Types/modules only (Module, - p.bind( ::AST::PathBinding::make_Module({&e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Module({&e}); _add_item(i.data.span, mod, IndexName::Namespace, i.name, i.is_pub, mv$(p)); ), (Crate, ASSERT_BUG(i.data.span, crate.m_extern_crates.count(e.name) > 0, "Referenced crate '" << e.name << "' isn't loaded for `extern crate`"); - p.bind( ::AST::PathBinding::make_Crate({ &crate.m_extern_crates.at(e.name) }) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Crate({ &crate.m_extern_crates.at(e.name) }); _add_item(i.data.span, mod, IndexName::Namespace, i.name, i.is_pub, mv$(p)); ), (Enum, - p.bind( ::AST::PathBinding::make_Enum({&e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Enum({&e}); _add_item_type(i.data.span, mod, i.name, i.is_pub, mv$(p)); ), (Union, - p.bind( ::AST::PathBinding::make_Union({&e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Union({&e}); _add_item_type(i.data.span, mod, i.name, i.is_pub, mv$(p)); ), (Trait, - p.bind( ::AST::PathBinding::make_Trait({&e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Trait({&e}); _add_item_type(i.data.span, mod, i.name, i.is_pub, mv$(p)); ), (Type, - p.bind( ::AST::PathBinding::make_TypeAlias({&e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_TypeAlias({&e}); _add_item_type(i.data.span, mod, i.name, i.is_pub, mv$(p)); ), // - Mixed (Struct, - p.bind( ::AST::PathBinding::make_Struct({&e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Struct({&e}); // - If the struct is a tuple-like struct (or unit-like), it presents in the value namespace if( ! e.m_data.is_Struct() ) { + p.m_bindings.value = ::AST::PathBinding_Value::make_Struct({&e}); _add_item_value(i.data.span, mod, i.name, i.is_pub, p); } _add_item_type(i.data.span, mod, i.name, i.is_pub, mv$(p)); ), // - Values only (Function, - p.bind( ::AST::PathBinding::make_Function({&e}) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_Function({&e}); _add_item_value(i.data.span, mod, i.name, i.is_pub, mv$(p)); ), (Static, - p.bind( ::AST::PathBinding::make_Static({&e}) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_Static({&e}); _add_item_value(i.data.span, mod, i.name, i.is_pub, mv$(p)); ) ) @@ -182,73 +183,48 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) // TODO: Ensure that the path is canonical? const auto& sp = i_data.sp; - struct H { - static void handle_pb(const Span& sp, AST::Module& mod, const AST::Named<AST::Item>& i, const AST::PathBinding& pb, bool allow_collide) - { - const auto& i_data = i.data.as_Use(); - TU_MATCH(::AST::PathBinding, (pb), (e), - (Unbound, - ), - (Variable, - BUG(sp, "Import was bound to variable"); - ), - (TypeParameter, - BUG(sp, "Import was bound to type parameter"); - ), - (TraitMethod, - BUG(sp, "Import was bound to trait method"); - ), - (StructMethod, - BUG(sp, "Import was bound to struct method"); - ), - - (Crate , _add_item(sp, mod, IndexName::Namespace, i.name, i.is_pub, i_data.path, !allow_collide); ), - (Module, _add_item(sp, mod, IndexName::Namespace, i.name, i.is_pub, i_data.path, !allow_collide); ), - (Enum, _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); ), - (Union, _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); ), - (Trait, _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); ), - (TypeAlias,_add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); ), - - (Struct, - _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); - // - If the struct is a tuple-like struct, it presents in the value namespace - assert(e.struct_ || e.hir); - if( !(e.struct_ ? e.struct_->m_data.is_Struct() : e.hir->m_data.is_Named()) ) - { - _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); - } - ), - (Static , _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); ), - (Function, _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); ), - (EnumVar , - bool is_struct = false; - if( e.enum_ ) { - ASSERT_BUG(sp, e.idx < e.enum_->variants().size(), "Variant out of range for " << i_data.path); - is_struct = e.enum_->variants().at(e.idx).m_data.is_Struct(); - } - else { - //ASSERT_BUG(sp, e.idx < e.hir->m_variants.size(), "Variant out of range for " << i_data.path); - is_struct = TU_TEST1(e.hir->m_data, Data, .at(e.idx).is_struct); - } - if( is_struct ) - _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); - else - _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); - ) - ) + ASSERT_BUG(sp, i_data.path.m_bindings.has_binding(), "`use " << i_data.path << "` left unbound in module " << mod.path()); + + bool allow_collide = false; + // - Types + {TU_MATCH_HDRA( (i_data.path.m_bindings.type), {) + TU_ARMA(Unbound, _e) { + DEBUG(i.name << " - Not a type/module"); } - }; - if( i_data.path.binding().is_Unbound() ) { - BUG(sp, "`use " << i_data.path << "` left unbound in module " << mod.path()); - } - else { - H::handle_pb(sp, mod, i, i_data.path.binding(), false); - } - if( i_data.alt_binding.is_Unbound() ) { - // Doesn't matter - } - else { - H::handle_pb(sp, mod, i, i_data.alt_binding, true); + TU_ARMA(TypeParameter, e) + BUG(sp, "Import was bound to type parameter"); + TU_ARMA(Crate , e) + _add_item(sp, mod, IndexName::Namespace, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(Module, e) + _add_item(sp, mod, IndexName::Namespace, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(Enum, e) + _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(Union, e) + _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(Trait, e) + _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(TypeAlias, e) + _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(Struct, e) + _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(EnumVar, e) + _add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + }} + // - Values + TU_MATCH_HDRA( (i_data.path.m_bindings.value), {) + TU_ARMA(Unbound, _e) { + DEBUG(i.name << " - Not a value"); + } + TU_ARMA(Variable, e) + BUG(sp, "Import was bound to a variable"); + TU_ARMA(Struct, e) + _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(EnumVar, e) + _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(Static , e) + _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); + TU_ARMA(Function, e) + _add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide); } } else @@ -298,22 +274,22 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C //throw ""; ), (Module, - p.bind( ::AST::PathBinding::make_Module({nullptr, &e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Module({nullptr, &e}); ), (Trait, - p.bind( ::AST::PathBinding::make_Trait({nullptr, &e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Trait({nullptr, &e}); ), (Struct, - p.bind( ::AST::PathBinding::make_Struct({nullptr, &e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Struct({nullptr, &e}); ), (Union, - p.bind( ::AST::PathBinding::make_Union({nullptr, &e}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Union({nullptr, &e}); ), (Enum, - p.bind( ::AST::PathBinding::make_Enum({nullptr}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_Enum({nullptr}); ), (TypeAlias, - p.bind( ::AST::PathBinding::make_TypeAlias({nullptr}) ); + p.m_bindings.type = ::AST::PathBinding_Type::make_TypeAlias({nullptr}); ) ) _add_item_type( sp, dst_mod, it.first, is_pub, mv$(p), false ); @@ -333,8 +309,8 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C for(unsigned int i = 0; i < spath.m_components.size()-1; i ++) { const auto& it = hmod->m_mod_items.at( spath.m_components[i] ); if(it->ent.is_Enum()) { - ASSERT_BUG(sp, i + 1 == spath.m_components.size() - 1, ""); - p.bind( ::AST::PathBinding::make_EnumVar({nullptr, 0}) ); + ASSERT_BUG(sp, i + 1 == spath.m_components.size() - 1, "Found enum not at penultimate component of HIR import path"); + p.m_bindings.value = ::AST::PathBinding_Value::make_EnumVar({nullptr, 0}); // TODO: What's the index? vep = nullptr; hmod = nullptr; break ; @@ -355,20 +331,20 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C throw ""; ), (Constant, - p.bind( ::AST::PathBinding::make_Static({nullptr}) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_Static({nullptr}); ), (Static, - p.bind( ::AST::PathBinding::make_Static({nullptr}) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_Static({nullptr}); ), // TODO: What if these refer to an enum variant? (StructConstant, - p.bind( ::AST::PathBinding::make_Struct({ nullptr, &crate.m_extern_crates.at(e.ty.m_crate_name).m_hir->get_typeitem_by_path(sp, e.ty, true).as_Struct() }) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_Struct({ nullptr, &crate.m_extern_crates.at(e.ty.m_crate_name).m_hir->get_typeitem_by_path(sp, e.ty, true).as_Struct() }); ), (StructConstructor, - p.bind( ::AST::PathBinding::make_Struct({ nullptr, &crate.m_extern_crates.at(e.ty.m_crate_name).m_hir->get_typeitem_by_path(sp, e.ty, true).as_Struct() }) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_Struct({ nullptr, &crate.m_extern_crates.at(e.ty.m_crate_name).m_hir->get_typeitem_by_path(sp, e.ty, true).as_Struct() }); ), (Function, - p.bind( ::AST::PathBinding::make_Function({nullptr}) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_Function({nullptr}); ) ) } @@ -418,14 +394,14 @@ void Resolve_Index_Module_Wildcard__submod(AST::Crate& crate, AST::Module& dst_m void Resolve_Index_Module_Wildcard__use_stmt(AST::Crate& crate, AST::Module& dst_mod, const AST::UseStmt& i_data, bool is_pub) { const auto& sp = i_data.sp; - const auto& b = i_data.path.binding(); + const auto& b = i_data.path.m_bindings.type; - TU_IFLET(::AST::PathBinding, b, Crate, e, + TU_IFLET(::AST::PathBinding_Type, b, Crate, e, DEBUG("Glob crate " << i_data.path); const auto& hmod = e.crate_->m_hir->m_root_module; Resolve_Index_Module_Wildcard__glob_in_hir_mod(sp, crate, dst_mod, hmod, i_data.path, is_pub); ) - else TU_IFLET(::AST::PathBinding, b, Module, e, + else TU_IFLET(::AST::PathBinding_Type, b, Module, e, DEBUG("Glob mod " << i_data.path); if( !e.module_ ) { @@ -438,7 +414,7 @@ void Resolve_Index_Module_Wildcard__use_stmt(AST::Crate& crate, AST::Module& dst Resolve_Index_Module_Wildcard__submod(crate, dst_mod, *e.module_, is_pub); } ) - else TU_IFLET(::AST::PathBinding, b, Enum, e, + else TU_IFLET(::AST::PathBinding_Type, b, Enum, e, ASSERT_BUG(sp, e.enum_ || e.hir, "Glob import but enum pointer not set - " << i_data.path); if( e.enum_ ) { @@ -446,11 +422,12 @@ void Resolve_Index_Module_Wildcard__use_stmt(AST::Crate& crate, AST::Module& dst unsigned int idx = 0; for( const auto& ev : e.enum_->variants() ) { ::AST::Path p = i_data.path + ev.m_name; - p.bind( ::AST::PathBinding::make_EnumVar({e.enum_, idx}) ); if( ev.m_data.is_Struct() ) { + p.m_bindings.type = ::AST::PathBinding_Type::make_EnumVar({e.enum_, idx}); _add_item_type ( sp, dst_mod, ev.m_name, is_pub, mv$(p), false ); } else { + p.m_bindings.value = ::AST::PathBinding_Value::make_EnumVar({e.enum_, idx}); _add_item_value( sp, dst_mod, ev.m_name, is_pub, mv$(p), false ); } @@ -467,7 +444,7 @@ void Resolve_Index_Module_Wildcard__use_stmt(AST::Crate& crate, AST::Module& dst for(const auto& ev : de->variants) { ::AST::Path p = i_data.path + ev.name; - p.bind( ::AST::PathBinding::make_EnumVar({nullptr, idx, e.hir}) ); + p.m_bindings.value = ::AST::PathBinding_Value::make_EnumVar({nullptr, idx, e.hir}); _add_item_value( sp, dst_mod, ev.name, is_pub, mv$(p), false ); @@ -480,12 +457,13 @@ void Resolve_Index_Module_Wildcard__use_stmt(AST::Crate& crate, AST::Module& dst for(const auto& ev : *de) { ::AST::Path p = i_data.path + ev.name; - p.bind( ::AST::PathBinding::make_EnumVar({nullptr, idx, e.hir}) ); if( ev.is_struct ) { + p.m_bindings.type = ::AST::PathBinding_Type::make_EnumVar({nullptr, idx, e.hir}); _add_item_type ( sp, dst_mod, ev.name, is_pub, mv$(p), false ); } else { + p.m_bindings.value = ::AST::PathBinding_Value::make_EnumVar({nullptr, idx, e.hir}); _add_item_value( sp, dst_mod, ev.name, is_pub, mv$(p), false ); } @@ -598,9 +576,9 @@ 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(); + auto bindings = path.m_bindings.clone(); path = hir_to_ast(e.path); - path.bind( mv$(binding) ); + path.m_bindings = mv$(bindings); ) return ; } @@ -611,9 +589,9 @@ 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(); + auto bindings = path.m_bindings.clone(); path = hir_to_ast(e.path); - path.bind( mv$(binding) ); + path.m_bindings = mv$(bindings); ) return ; } @@ -648,27 +626,26 @@ bool Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span& auto new_path = ie.path; for(unsigned int j = i+1; j < info.nodes.size(); j ++) new_path.nodes().push_back( mv$(info.nodes[j]) ); - new_path.bind( path.binding().clone() ); + new_path.m_bindings = path.m_bindings.clone(); path = mv$(new_path); return Resolve_Index_Module_Normalise_Path(crate, sp, path, loc); } else { - TU_MATCH_DEF(::AST::PathBinding, (ie.path.binding()), (e), - ( + TU_MATCH_HDR( (ie.path.m_bindings.type), {) + default: BUG(sp, "Path " << path << " pointed to non-module " << ie.path); - ), - (Module, + TU_ARM( ie.path.m_bindings.type, Module, e) { mod = e.module_; - ), - (Crate, + } + TU_ARM( ie.path.m_bindings.type, Crate, e) { Resolve_Index_Module_Normalise_Path_ext(crate, sp, path, loc, *e.crate_, i+1); return false; - ), - (Enum, + } + TU_ARM( ie.path.m_bindings.type, Enum, e) { // NOTE: Just assuming that if an Enum is hit, it's sane return false; - ) - ) + } + } } } |