diff options
Diffstat (limited to 'src/resolve')
-rw-r--r-- | src/resolve/absolute.cpp | 228 | ||||
-rw-r--r-- | src/resolve/index.cpp | 58 | ||||
-rw-r--r-- | src/resolve/use.cpp | 50 |
3 files changed, 168 insertions, 168 deletions
diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index 0b9c2476..db85ed3f 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -20,7 +20,7 @@ struct GenericSlot Method, } level; unsigned short index; - + unsigned int to_binding() const { if(level == Level::Method && index != 0xFFFF) { return (unsigned int)index + 256; @@ -63,7 +63,7 @@ struct Context unsigned int m_var_count; unsigned int m_block_level; bool m_frozen_bind_set; - + Context(const ::AST::Crate& crate, const ::AST::Module& mod): m_crate(crate), m_mod(mod), @@ -71,11 +71,11 @@ struct Context m_block_level(0), m_frozen_bind_set( false ) {} - + void push(const ::AST::GenericParams& params, GenericSlot::Level level, bool has_self=false) { auto e = Ent::make_Generic({}); auto& data = e.as_Generic(); - + if( has_self ) { //assert( level == GenericSlot::Level::Top ); data.types.push_back( Named<GenericSlot> { "Self", GenericSlot { level, 0xFFFF } } ); @@ -90,7 +90,7 @@ struct Context if( params.lft_params().size() > 0 ) { //TODO(Span(), "resolve/absolute.cpp - Context::push(GenericParams) - Lifetime params - " << params); } - + m_name_context.push_back(mv$(e)); } void pop(const ::AST::GenericParams& , bool has_self=false) { @@ -111,7 +111,7 @@ struct Context BUG(Span(), "resolve/absolute.cpp - Context::pop(GenericParams) - Mismatched pop"); m_name_context.pop_back(); } - + class RootBlockScope { friend struct Context; Context& ctxt; @@ -133,7 +133,7 @@ struct Context RootBlockScope clear_rootblock() { return RootBlockScope(*this, ~0u); } - + void push_self(const TypeRef& tr) { m_name_context.push_back( Ent::make_ConcreteSelf(&tr) ); } @@ -224,7 +224,7 @@ struct Context } m_block_level -= 1; } - + /// Indicate that a multiple-pattern binding is started void start_patbind() { assert( m_block_level > 0 ); @@ -239,8 +239,8 @@ struct Context void end_patbind() { m_frozen_bind_set = false; } - - + + enum class LookupMode { Namespace, Type, @@ -292,7 +292,7 @@ struct Context } } break; - + case LookupMode::Type: //if( name == "IntoIterator" ) { // DEBUG("lookup_in_mod(mod="<<mod.path()<<")"); @@ -406,14 +406,14 @@ struct Context ) ) } - + // Top-level module DEBUG("- Top module (" << m_mod.path() << ")"); ::AST::Path rv; if( this->lookup_in_mod(m_mod, name, mode, rv) ) { return rv; } - + DEBUG("- Primitives"); switch(mode) { @@ -429,7 +429,7 @@ struct Context default: break; } - + return AST::Path(); } @@ -468,10 +468,10 @@ struct Context ) ) } - + ERROR(sp, E0000, "Unable to find local " << (mode == LookupMode::Variable ? "variable" : "type") << " '" << name << "'"); } - + /// Clones the context, including only the module-level items (i.e. just the Module entries) Context clone_mod() const { auto rv = Context(this->m_crate, this->m_mod); @@ -543,18 +543,18 @@ void Resolve_Absolute_Path_BindUFCS(Context& context, const Span& sp, Context::L nodes.erase( nodes.begin() ); path = ::AST::Path( ::AST::Path::TagUfcs(), TypeRef(span, mv$(inner_path)), ::AST::Path(), mv$(nodes) ); } - + const auto& ufcs = path.m_class.as_UFCS(); if( ufcs.nodes.size() == 0 ) { - + if( mode == Context::LookupMode::Type && ufcs.trait && *ufcs.trait == ::AST::Path() ) { return ; } - + BUG(sp, "UFCS with no nodes encountered - " << path); } const auto& node = ufcs.nodes.at(0); - + if( ufcs.trait && ufcs.trait->is_valid() ) { // Trait is specified, definitely a trait item @@ -567,7 +567,7 @@ void Resolve_Absolute_Path_BindUFCS(Context& context, const Span& sp, Context::L return ; assert( pb.as_Trait().trait_ ); const auto& tr = *pb.as_Trait().trait_; - + switch(mode) { case Context::LookupMode::Pattern: @@ -637,18 +637,18 @@ namespace { const auto& path_abs = path.m_class.as_Absolute(); auto type_path = ::AST::Path( path ); type_path.m_class.as_Absolute().nodes.resize( i+1 ); - + auto new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(sp, mv$(type_path)), ::AST::Path()); for( unsigned int j = i+1; j < path_abs.nodes.size(); j ++ ) new_path.nodes().push_back( mv$(path_abs.nodes[j]) ); - + return new_path; } AST::Path split_replace_into_ufcs_path(const Span& sp, AST::Path path, unsigned int i, const AST::Path& ty_path_tpl) { auto& path_abs = path.m_class.as_Absolute(); auto& n = path_abs.nodes[i]; - + auto type_path = ::AST::Path(ty_path_tpl); if( ! n.args().is_empty() ) { type_path.nodes().back().args() = mv$(n.args()); @@ -656,10 +656,10 @@ namespace { auto new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(sp, mv$(type_path)), ::AST::Path()); for( unsigned int j = i+1; j < path_abs.nodes.size(); j ++ ) new_path.nodes().push_back( mv$(path_abs.nodes[j]) ); - + return new_path; } - + void Resolve_Absolute_Path_BindAbsolute__hir_from_import(Context& context, const Span& sp, bool is_value, AST::Path& path, const ::HIR::SimplePath& p) { TRACE_FUNCTION_FR("path="<<path<<", p="<<p, path); @@ -671,7 +671,7 @@ namespace { auto it = hmod->m_mod_items.find(name); if( it == hmod->m_mod_items.end() ) ERROR(sp, E0000, "Couldn't find path component '" << name << "' of " << p); - + TU_MATCH_DEF(::HIR::TypeItem, (it->second->ent), (e), ( TODO(sp, "Unknown item type in path - " << i << " " << p << " - " << it->second->ent.tag_str()); @@ -685,7 +685,7 @@ namespace { ASSERT_BUG(sp, it != e.m_variants.end(), "Extern crate import path points to non-present variant - " << p); unsigned int var_idx = it - e.m_variants.begin(); auto pb = ::AST::PathBinding::make_EnumVar({nullptr, var_idx, &e}); - + // Construct output path (with same set of parameters) AST::Path rv( p.m_crate_name, {} ); rv.nodes().reserve( p.m_components.size() ); @@ -694,7 +694,7 @@ namespace { rv.nodes().back().args() = mv$( path.nodes().back().args() ); rv.bind( mv$(pb) ); path = mv$(rv); - + return ; ), (Module, @@ -702,9 +702,9 @@ namespace { ) ) } - + ::AST::PathBinding pb; - + const auto& name = p.m_components.back(); if( is_value ) { @@ -763,7 +763,7 @@ namespace { ) ) } - + // Construct output path (with same set of parameters) AST::Path rv( p.m_crate_name, {} ); rv.nodes().reserve( p.m_components.size() ); @@ -773,12 +773,12 @@ namespace { rv.bind( mv$(pb) ); path = mv$(rv); } - + void Resolve_Absolute_Path_BindAbsolute__hir_from(Context& context, const Span& sp, Context::LookupMode& mode, ::AST::Path& path, const AST::ExternCrate& crate, unsigned int start) { TRACE_FUNCTION_FR(path << " start=" << start, path); auto& path_abs = path.m_class.as_Absolute(); - + if( path_abs.nodes.empty() ) { switch(mode) { @@ -789,7 +789,7 @@ namespace { TODO(sp, ""); } } - + const ::HIR::Module* hmod = &crate.m_hir->m_root_module; for(unsigned int i = start; i < path_abs.nodes.size() - 1; i ++ ) { @@ -798,7 +798,7 @@ namespace { auto it = hmod->m_mod_items.find(n.name()); if( it == hmod->m_mod_items.end() ) ERROR(sp, E0000, "Couldn't find path component '" << n.name() << "' of " << path); - + TU_MATCH(::HIR::TypeItem, (it->second->ent), (e), (Import, // - Update path then restart @@ -830,7 +830,7 @@ namespace { } } trait_path.bind( ::AST::PathBinding::make_Trait({nullptr, &e}) ); - + ::AST::Path new_path; const auto& next_node = path_abs.nodes[i+1]; // If the named item can't be found in the trait, fall back to it being a type binding @@ -847,7 +847,7 @@ namespace { found = (e.m_values.find( next_node.name() ) != e.m_values.end()); break; } - + if( !found ) { new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(sp, mv$(trait_path))); } @@ -856,7 +856,7 @@ namespace { } for( unsigned int j = i+1; j < path_abs.nodes.size(); j ++ ) new_path.nodes().push_back( mv$(path_abs.nodes[j]) ); - + path = mv$(new_path); return Resolve_Absolute_Path_BindUFCS(context, sp, mode, path); ), @@ -881,7 +881,7 @@ namespace { for( const auto& var : e.m_variants ) { if( var.first == last_node.name() ) { - + if( i != path_abs.nodes.size() - 2 ) { ERROR(sp, E0000, "Unexpected enum in path " << path); } @@ -889,7 +889,7 @@ namespace { if( ! n.args().is_empty() ) { ERROR(sp, E0000, "Type parameters were not expected here (enum params go on the variant)"); } - + path.bind( ::AST::PathBinding::make_EnumVar({nullptr, static_cast<unsigned int>(&var - &*e.m_variants.begin()), &e}) ); path = split_into_crate(sp, mv$(path), start, crate.m_name); return; @@ -901,7 +901,7 @@ namespace { ) ) } - + const auto& name = path_abs.nodes.back().name(); switch(mode) { @@ -942,7 +942,7 @@ namespace { } } break; - + case Context::LookupMode::Pattern: { auto v = hmod->m_mod_items.find(name); @@ -1028,14 +1028,14 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex { TRACE_FUNCTION_FR("path = " << path, path); auto& path_abs = path.m_class.as_Absolute(); - + if( path_abs.crate != "" ) { // TODO: Handle items from other crates (back-converting HIR paths) Resolve_Absolute_Path_BindAbsolute__hir_from(context, sp, mode, path, context.m_crate.m_extern_crates.at(path_abs.crate), 0); return ; } - - + + const ::AST::Module* mod = &context.m_crate.m_root_module; for(unsigned int i = 0; i < path_abs.nodes.size() - 1; i ++ ) { @@ -1045,11 +1045,11 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex if( ! n.args().is_empty() ) { ERROR(sp, E0000, "Type parameters were not expected here"); } - + if( n.name() == "#" ) { TODO(sp, "magic module"); } - + char c; unsigned int idx; ::std::stringstream ss( n.name() ); @@ -1067,7 +1067,7 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex } const auto& name_ref = it->second; DEBUG("#" << i << " \"" << n.name() << "\" = " << name_ref.path << (name_ref.is_import ? " (import)" : "") ); - + TU_MATCH_DEF(::AST::PathBinding, (name_ref.path.binding()), (e), ( ERROR(sp, E0000, "Encountered non-namespace item '" << n.name() << "' ("<<name_ref.path<<") in path " << path); @@ -1143,7 +1143,7 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex } for( unsigned int j = i+1; j < path_abs.nodes.size(); j ++ ) new_path.nodes().push_back( mv$(path_abs.nodes[j]) ); - + path = mv$(new_path); return Resolve_Absolute_Path_BindUFCS(context, sp, mode, path); ), @@ -1164,7 +1164,7 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex const auto& last_node = path_abs.nodes.back(); for( const auto& var : e.enum_->variants() ) { if( var.m_name == last_node.name() ) { - + if( i != path_abs.nodes.size() - 2 ) { ERROR(sp, E0000, "Unexpected enum in path " << path); } @@ -1172,12 +1172,12 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex if( ! n.args().is_empty() ) { ERROR(sp, E0000, "Type parameters were not expected here (enum params go on the variant)"); } - + path.bind_enum_var(*e.enum_, var.m_name); return; } } - + path = split_replace_into_ufcs_path(sp, mv$(path), i, name_ref.path); return Resolve_Absolute_Path_BindUFCS(context, sp, mode, path); } @@ -1210,14 +1210,14 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex ) } } - + // Set binding to binding of node in last module ::AST::Path tmp; if( ! Context::lookup_in_mod(*mod, path_abs.nodes.back().name(), mode, tmp) ) { ERROR(sp, E0000, "Couldn't find " << Context::lookup_mode_msg(mode) << " '" << path_abs.nodes.back().name() << "' of " << path); } assert( ! tmp.binding().is_Unbound() ); - + // Replaces the path with the one returned by `lookup_in_mod`, ensuring that `use` aliases are eliminated DEBUG("Replace " << path << " with " << tmp); auto args = mv$(path.nodes().back().args()); @@ -1235,7 +1235,7 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context::LookupMode mode, ::AST::Path& path) { TRACE_FUNCTION_FR("mode = " << mode << ", path = " << path, path); - + TU_MATCH(::AST::Path::Class, (path.m_class), (e), (Invalid, BUG(sp, "Attempted resolution of invalid path"); @@ -1270,7 +1270,7 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: if( !pe.module_ ) { assert( pe.hir ); const auto& mod = *pe.hir; - + switch( e.nodes.size() == 2 ? mode : Context::LookupMode::Namespace ) { case Context::LookupMode::Namespace: @@ -1321,11 +1321,11 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: auto ct = coretype_fromstring(e.nodes[0].name()); p = ::AST::Path( ::AST::Path::TagUfcs(), TypeRef(Span("-",0,0,0,0), ct), ::AST::Path(), ::std::vector< ::AST::PathNode>() ); } - + DEBUG("Primitive module hack yeilded " << p); ) } - + if( e.nodes.size() > 1 ) { // Only primitive types turn `Local` paths @@ -1355,7 +1355,7 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: } path = mv$(p); } - + if( !path.is_trivial() ) Resolve_Absolute_PathNodes(context, sp, path.nodes()); ), @@ -1372,7 +1372,7 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: unsigned int start_len = e.count > mp_nodes.size() ? 0 : mp_nodes.size() - e.count; while( start_len > 0 && mp_nodes[start_len-1].name()[0] == '#' ) start_len --; - + // - Create a new path ::AST::Path np("", {}); auto& np_nodes = np.nodes(); @@ -1381,10 +1381,10 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: np_nodes.push_back( mp_nodes[i] ); for(auto& en : e.nodes) np_nodes.push_back( mv$(en) ); - + if( !path.is_trivial() ) Resolve_Absolute_PathNodes(context, sp, np_nodes); - + path = mv$(np); ), (Absolute, @@ -1398,11 +1398,11 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: if( e.trait && *e.trait != ::AST::Path() ) { Resolve_Absolute_Path(context, sp, Context::LookupMode::Type, *e.trait); } - + Resolve_Absolute_PathNodes(context, sp, e.nodes); ) ) - + DEBUG("path = " << path); // TODO: Should this be deferred until the HIR? // - Doing it here so the HIR lowering has a bit more information @@ -1424,7 +1424,7 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: Resolve_Absolute_Path_BindUFCS(context, sp, mode, path); ) ) - + // TODO: Expand default type parameters? // - Helps with cases like PartialOrd<Self>, but hinders when the default is a hint (in expressions) } @@ -1495,7 +1495,7 @@ void Resolve_Absolute_Type(Context& context, TypeRef& type) } assert( ufcs.nodes.size() == 1); ) - + TU_IFLET(::AST::PathBinding, e.path.binding(), Trait, be, auto ty = ::TypeRef( type.span(), {}, ::make_vec1(mv$(e.path)) ); type = mv$(ty); @@ -1534,12 +1534,12 @@ void Resolve_Absolute_ExprNode(Context& context, ::AST::ExprNode& node) public AST::NodeVisitorDef { Context& context; - + NV(Context& context): context(context) { } - + void visit(AST::ExprNode_Block& node) override { DEBUG("ExprNode_Block"); if( node.m_local_mod ) { @@ -1556,7 +1556,7 @@ void Resolve_Absolute_ExprNode(Context& context, ::AST::ExprNode& node) this->context.pop( *node.m_local_mod ); } } - + void visit(AST::ExprNode_Match& node) override { DEBUG("ExprNode_Match"); node.m_val->visit( *this ); @@ -1579,12 +1579,12 @@ void Resolve_Absolute_ExprNode(Context& context, ::AST::ExprNode& node) else { Resolve_Absolute_Pattern(this->context, true, arm.m_patterns[0]); } - + if(arm.m_cond) arm.m_cond->visit( *this ); assert( arm.m_code ); arm.m_code->visit( *this ); - + this->context.pop_block(); } } @@ -1606,7 +1606,7 @@ void Resolve_Absolute_ExprNode(Context& context, ::AST::ExprNode& node) node.m_code->visit( *this ); this->context.pop_block(); } - + void visit(AST::ExprNode_LetBinding& node) override { DEBUG("ExprNode_LetBinding"); Resolve_Absolute_Type(this->context, node.m_type); @@ -1616,14 +1616,14 @@ void Resolve_Absolute_ExprNode(Context& context, ::AST::ExprNode& node) void visit(AST::ExprNode_IfLet& node) override { DEBUG("ExprNode_IfLet"); node.m_value->visit( *this ); - + this->context.push_block(); Resolve_Absolute_Pattern(this->context, true, node.m_pattern); - + assert( node.m_true ); node.m_true->visit( *this ); this->context.pop_block(); - + if(node.m_false) node.m_false->visit(*this); } @@ -1653,17 +1653,17 @@ void Resolve_Absolute_ExprNode(Context& context, ::AST::ExprNode& node) } void visit(AST::ExprNode_Closure& node) override { DEBUG("ExprNode_Closure"); - + Resolve_Absolute_Type(this->context, node.m_return); - + this->context.push_block(); for( auto& arg : node.m_args ) { Resolve_Absolute_Type(this->context, arg.second); Resolve_Absolute_Pattern(this->context, false, arg.first); } - + node.m_code->visit(*this); - + this->context.pop_block(); } } expr_iter(context); @@ -1744,7 +1744,7 @@ void Resolve_Absolute_Pattern(Context& context, bool allow_refutable, ::AST::Pa } else { auto name = mv$( e.name ); - + pat = ::AST::Pattern(::AST::Pattern::TagBind(), mv$(name)); pat.binding().m_slot = context.push_var( pat.span(), pat.binding().m_name ); } @@ -1828,32 +1828,32 @@ void Resolve_Absolute_ImplItems(Context& item_context, ::AST::NamedList< ::AST: assert( e.params().lft_params().size() == 0 ); item_context.push( e.params(), GenericSlot::Level::Method, true ); Resolve_Absolute_Generic(item_context, e.params()); - + Resolve_Absolute_Type( item_context, e.type() ); - + item_context.pop( e.params(), true ); ), (Function, DEBUG("Function - " << i.name); item_context.push( e.params(), GenericSlot::Level::Method ); Resolve_Absolute_Generic(item_context, e.params()); - + Resolve_Absolute_Type( item_context, e.rettype() ); for(auto& arg : e.args()) Resolve_Absolute_Type( item_context, arg.second ); - + { auto _h = item_context.enter_rootblock(); item_context.push_block(); for(auto& arg : e.args()) { Resolve_Absolute_Pattern( item_context, false, arg.first ); } - + Resolve_Absolute_Expr( item_context, e.code() ); - + item_context.pop_block(); } - + item_context.pop( e.params() ); ), (Static, @@ -1891,9 +1891,9 @@ void Resolve_Absolute_ImplItems(Context& item_context, ::std::vector< ::AST::Im assert( e.params().lft_params().size() == 0 ); item_context.push( e.params(), GenericSlot::Level::Method, true ); Resolve_Absolute_Generic(item_context, e.params()); - + Resolve_Absolute_Type( item_context, e.type() ); - + item_context.pop( e.params(), true ); ), (Function, @@ -1925,9 +1925,9 @@ void Resolve_Absolute_Function(Context& item_context, ::AST::Function& fcn) for(auto& arg : fcn.args()) { Resolve_Absolute_Pattern( item_context, false, arg.first ); } - + Resolve_Absolute_Expr( item_context, fcn.code() ); - + item_context.pop_block(); } @@ -1944,7 +1944,7 @@ void Resolve_Absolute_Struct(Context& item_context, ::AST::Struct& e) { item_context.push( e.params(), GenericSlot::Level::Top ); Resolve_Absolute_Generic(item_context, e.params()); - + TU_MATCH(::AST::StructData, (e.m_data), (s), (Tuple, for(auto& field : s.ents) { @@ -1957,25 +1957,25 @@ void Resolve_Absolute_Struct(Context& item_context, ::AST::Struct& e) } ) ) - + item_context.pop( e.params() ); } void Resolve_Absolute_Union(Context& item_context, ::AST::Union& e) { item_context.push( e.m_params, GenericSlot::Level::Top ); Resolve_Absolute_Generic(item_context, e.m_params); - + for(auto& field : e.m_variants) { Resolve_Absolute_Type(item_context, field.m_type); } - + item_context.pop( e.m_params ); } void Resolve_Absolute_Trait(Context& item_context, ::AST::Trait& e) { item_context.push( e.params(), GenericSlot::Level::Top, true ); Resolve_Absolute_Generic(item_context, e.params()); - + for(auto& st : e.supertraits()) { if( !st.ent.is_valid() ) { DEBUG("- ST 'static"); @@ -1985,16 +1985,16 @@ void Resolve_Absolute_Trait(Context& item_context, ::AST::Trait& e) Resolve_Absolute_Path(item_context, st.sp, Context::LookupMode::Type, st.ent); } } - + Resolve_Absolute_ImplItems(item_context, e.items()); - + item_context.pop( e.params(), true ); } void Resolve_Absolute_Enum(Context& item_context, ::AST::Enum& e) { item_context.push( e.params(), GenericSlot::Level::Top ); Resolve_Absolute_Generic(item_context, e.params()); - + for(auto& variant : e.variants()) { TU_MATCH(::AST::EnumVariantData, (variant.m_data), (s), @@ -2014,7 +2014,7 @@ void Resolve_Absolute_Enum(Context& item_context, ::AST::Enum& e) ) ) } - + item_context.pop( e.params() ); } @@ -2024,7 +2024,7 @@ void Resolve_Absolute_Mod(const ::AST::Crate& crate, ::AST::Module& mod) { void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) { TRACE_FUNCTION_F("(mod="<<mod.path()<<")"); - + for( auto& i : mod.items() ) { TU_MATCH(AST::Item, (i.data), (e), @@ -2062,13 +2062,13 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) Resolve_Absolute_Generic(item_context, def.params()); assert( def.trait().ent.is_valid() ); Resolve_Absolute_Path(item_context, def.trait().sp, Context::LookupMode::Type, def.trait().ent); - + if( e.items().size() != 0 ) { ERROR(def.span(), E0000, "impl Trait for .. with methods"); } - + item_context.pop(def.params()); - + const_cast< ::AST::Trait*>(def.trait().ent.binding().as_Trait().trait_)->set_is_marker(); } else @@ -2076,14 +2076,14 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) item_context.push_self( def.type() ); item_context.push(def.params(), GenericSlot::Level::Top); Resolve_Absolute_Generic(item_context, def.params()); - + Resolve_Absolute_Type(item_context, def.type()); if( def.trait().ent.is_valid() ) { Resolve_Absolute_Path(item_context, def.trait().sp, Context::LookupMode::Type, def.trait().ent); } - + Resolve_Absolute_ImplItems(item_context, e.items()); - + item_context.pop(def.params()); item_context.pop_self( def.type() ); } @@ -2093,14 +2093,14 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) item_context.push_self( impl_def.type() ); item_context.push(impl_def.params(), GenericSlot::Level::Top); Resolve_Absolute_Generic(item_context, impl_def.params()); - + Resolve_Absolute_Type(item_context, impl_def.type()); if( !impl_def.trait().ent.is_valid() ) BUG(impl_def.span(), "Encountered negative impl with no trait"); Resolve_Absolute_Path(item_context, impl_def.trait().sp, Context::LookupMode::Type, impl_def.trait().ent); - + // No items - + item_context.pop(impl_def.params()); item_context.pop_self( impl_def.type() ); ), @@ -2123,9 +2123,9 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) DEBUG("Type - " << i.name); item_context.push( e.params(), GenericSlot::Level::Top, true ); Resolve_Absolute_Generic(item_context, e.params()); - + Resolve_Absolute_Type( item_context, e.type() ); - + item_context.pop( e.params(), true ); ), (Struct, @@ -2146,7 +2146,7 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) ) ) } - + // - Run through the indexed items and fix up those paths static Span sp; DEBUG("mod = " << mod.path()); diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp index c0276695..b65bea6f 100644 --- a/src/resolve/index.cpp +++ b/src/resolve/index.cpp @@ -53,11 +53,11 @@ namespace { void _add_item(const Span& sp, AST::Module& mod, IndexName location, const ::std::string& name, bool is_pub, ::AST::Path ir, bool error_on_collision=true) { auto& list = get_mod_index(mod, location); - + bool was_import = (ir != mod.path() + name); if( list.count(name) > 0 ) { - if( error_on_collision ) + if( error_on_collision ) { ERROR(sp, E0000, "Duplicate definition of name '" << name << "' in " << location << " scope (" << mod.path() << ")"); } @@ -99,7 +99,7 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) { ::AST::Path p = mod.path() + i.name; //DEBUG("- p = " << p << " : " << ::AST::Item::tag_to_str(i.data.tag())); - + TU_MATCH(AST::Item, (i.data), (e), (None, ), @@ -112,7 +112,7 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) ), (NegImpl, ), - + (Use, // Skip for now ), @@ -162,7 +162,7 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) ) ) } - + bool has_pub_wildcard = false; // Named imports for( const auto& i : mod.items() ) @@ -173,7 +173,7 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) if( i.name != "" ) { // 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) @@ -194,14 +194,14 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) (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 @@ -258,9 +258,9 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod) } } } - + mod.m_index_populated = (has_pub_wildcard ? 1 : 2); - + // Handle child modules for( auto& i : mod.items() ) { @@ -326,7 +326,7 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C if( ve.ent.is_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"); const auto* hmod = &crate.m_extern_crates.at(spath.m_crate_name).m_hir->m_root_module; for(unsigned int i = 0; i < spath.m_components.size()-1; i ++) { @@ -390,7 +390,7 @@ void Resolve_Index_Module_Wildcard(AST::Crate& crate, AST::Module& mod, bool han if( ! i.data.is_Use() ) continue ; const auto& i_data = i.data.as_Use(); - + if( i.name == "" && i.is_pub == handle_pub ) { const auto& sp = i_data.sp; @@ -414,7 +414,7 @@ void Resolve_Index_Module_Wildcard(AST::Crate& crate, AST::Module& mod, bool han (StructMethod, BUG(sp, "Import was bound to struct method"); ), - + (Crate, DEBUG("Glob crate " << i_data.path); const auto& hmod = e.crate_->m_hir->m_root_module; @@ -476,7 +476,7 @@ void Resolve_Index_Module_Wildcard(AST::Crate& crate, AST::Module& mod, bool han else { _add_item_value( sp, mod, ev.m_name, i.is_pub, mv$(p), false ); } - + idx += 1; } } @@ -495,7 +495,7 @@ void Resolve_Index_Module_Wildcard(AST::Crate& crate, AST::Module& mod, bool han else { _add_item_value( sp, mod, ev.first, i.is_pub, mv$(p), false ); } - + idx += 1; } } @@ -503,10 +503,10 @@ void Resolve_Index_Module_Wildcard(AST::Crate& crate, AST::Module& mod, bool han ) } } - + // handle_pub == true first, leading to full resoltion no matter what mod.m_index_populated = 2; - + // Handle child modules for( auto& i : mod.items() ) { @@ -538,7 +538,7 @@ void Resolve_Index_Module_Normalise_Path_ext(const ::AST::Crate& crate, const Sp // path.nodes().erase( path.nodes().begin() + i ); //} while( --i > 0 ); - + for(unsigned int i = start; i < info.nodes.size() - 1; i ++) { auto it = hmod->m_mod_items.find( info.nodes[i].name() ); @@ -575,7 +575,7 @@ void Resolve_Index_Module_Normalise_Path_ext(const ::AST::Crate& crate, const Sp ) } const auto& lastnode = info.nodes.back(); - + switch(loc) { case IndexName::Type: @@ -606,7 +606,7 @@ void Resolve_Index_Module_Normalise_Path_ext(const ::AST::Crate& crate, const Sp } } break; } - + ERROR(sp, E0000, "Couldn't find final node of path " << path); } @@ -619,17 +619,17 @@ bool Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span& Resolve_Index_Module_Normalise_Path_ext(crate, sp, path, loc, crate.m_extern_crates.at(info.crate), 0); return false; } - + const ::AST::Module* mod = &crate.m_root_module; for( unsigned int i = 0; i < info.nodes.size() - 1; i ++ ) { const auto& node = info.nodes[i]; - + auto it = mod->m_namespace_items.find( node.name() ); if( it == mod->m_namespace_items.end() ) ERROR(sp, E0000, "Couldn't find node " << i << " of path " << path); const auto& ie = it->second; - + if( ie.is_import ) { // Need to replace all nodes up to and including the current with the import path auto new_path = ie.path; @@ -658,10 +658,10 @@ bool Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span& ) } } - + const auto& node = info.nodes.back(); - - + + // TODO: Use get_mod_index instead. const ::AST::Module::IndexEnt* ie_p = nullptr; switch(loc) @@ -685,7 +685,7 @@ bool Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span& if( !ie_p ) ERROR(sp, E0000, "Couldn't find final node of path " << path); const auto& ie = *ie_p; - + if( ie.is_import ) { // TODO: Prevent infinite recursion if the user does something dumb path = ::AST::Path(ie.path); @@ -706,7 +706,7 @@ void Resolve_Index_Module_Normalise(const ::AST::Crate& crate, const Span& mod_s Resolve_Index_Module_Normalise(crate, item.data.span, e); ) } - + DEBUG("Index for " << mod.path()); for( auto& ent : mod.m_namespace_items ) { Resolve_Index_Module_Normalise_Path(crate, mod_span, ent.second.path, IndexName::Namespace); @@ -730,7 +730,7 @@ void Resolve_Index(AST::Crate& crate) Resolve_Index_Module_Wildcard(crate, crate.m_root_module, true); // - Add all private glob imported items Resolve_Index_Module_Wildcard(crate, crate.m_root_module, false); - + // - Normalise the index (ensuring all paths point directly to the item) Resolve_Index_Module_Normalise(crate, Span(), crate.m_root_module); } diff --git a/src/resolve/use.cpp b/src/resolve/use.cpp index 0196108f..55c949b8 100644 --- a/src/resolve/use.cpp +++ b/src/resolve/use.cpp @@ -89,22 +89,22 @@ void Resolve_Use(::AST::Crate& crate) void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path path, slice< const ::AST::Module* > parent_modules) { TRACE_FUNCTION_F("path = " << path); - + for(auto& use_stmt : mod.items()) { if( ! use_stmt.data.is_Use() ) continue ; auto& use_stmt_data = use_stmt.data.as_Use(); - + const Span& span = use_stmt_data.sp; use_stmt_data.path = Resolve_Use_AbsolutisePath(span, path, mv$(use_stmt_data.path)); if( !use_stmt_data.path.m_class.is_Absolute() ) BUG(span, "Use path is not absolute after absolutisation"); - + // TODO: Have Resolve_Use_GetBinding return the actual path use_stmt_data.path.bind( Resolve_Use_GetBinding(span, crate, use_stmt_data.path, parent_modules) ); DEBUG("'" << use_stmt.name << "' = " << use_stmt_data.path); - + // - If doing a glob, ensure the item type is valid if( use_stmt.name == "" ) { @@ -158,18 +158,18 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path { const AST::Crate& crate; ::std::vector< const AST::Module* > parent_modules; - + NV(const AST::Crate& crate, const AST::Module& cur_module): crate(crate), parent_modules() { parent_modules.push_back( &cur_module ); } - + void visit(AST::ExprNode_Block& node) override { if( node.m_local_mod ) { Resolve_Use_Mod(this->crate, *node.m_local_mod, node.m_local_mod->path(), this->parent_modules); - + parent_modules.push_back(&*node.m_local_mod); } AST::NodeVisitorDef::visit(node); @@ -178,7 +178,7 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path } } } expr_iter(crate, mod); - + // TODO: Check that all code blocks are covered by these // - NOTE: Handle anon modules by iterating code (allowing correct item mappings) for(auto& i : mod.items()) @@ -264,13 +264,13 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path assert( mod.anon_mods()[idx] ); return ::AST::PathBinding::make_Module({&*mod.anon_mods()[idx]}); } - + // Seach for the name defined in the module. for( const auto& item : mod.items() ) { if( item.data.is_None() ) continue ; - + if( item.name == des_item_name ) { //if( allow != Lookup::Any ) // DEBUG(mod.path() << " " << des_item_name << " " << item.data.tag_str()); @@ -305,7 +305,7 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path if( allow != Lookup::Value ) return ::AST::PathBinding::make_Trait({&e}); ), - + (Function, if( allow != Lookup::Type ) return ::AST::PathBinding::make_Function({&e}); @@ -335,7 +335,7 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path ) } } - + // Imports for( const auto& imp : mod.items() ) { @@ -406,7 +406,7 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path else { //out_path = imp_data.path; } - + TU_MATCH_DEF(::AST::PathBinding, (*binding), (e), ( BUG(sp2, "Wildcard import expanded to an invalid item class - " << binding->tag_str()); @@ -463,7 +463,7 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path namespace { const ::HIR::Module* get_hir_mod_by_path(const Span& sp, const ::AST::Crate& crate, const ::HIR::SimplePath& path); - + const void* get_hir_modenum_by_path(const Span& sp, const ::AST::Crate& crate, const ::HIR::SimplePath& path, bool& is_enum) { const auto* hmod = &crate.m_extern_crates.at( path.m_crate_name ).m_hir->m_root_module; @@ -533,7 +533,7 @@ namespace { ERROR(span, E0000, "Encountered enum at unexpected location in import"); } const auto& name = nodes[i].name(); - + auto it2 = ::std::find_if( enm.m_variants.begin(), enm.m_variants.end(), [&](const auto& x){ return x.first == name; } ); if( it2 == enm.m_variants.end() ) { ERROR(span, E0000, "Unable to find variant " << path); @@ -553,7 +553,7 @@ namespace { ERROR(span, E0000, "Encountered enum at unexpected location in import"); } const auto& name = nodes[i].name(); - + auto it2 = ::std::find_if( e.m_variants.begin(), e.m_variants.end(), [&](const auto& x){ return x.first == name; } ); if( it2 == e.m_variants.end() ) { ERROR(span, E0000, "Unable to find variant " << path); @@ -648,7 +648,7 @@ namespace { ) } } - + return ::AST::PathBinding::make_Unbound({}); } ::AST::PathBinding Resolve_Use_GetBinding__ext(const Span& span, const ::AST::Crate& crate, const ::AST::Path& path, const AST::ExternCrate& ec, unsigned int start, Lookup allow) @@ -660,20 +660,20 @@ namespace { { TRACE_FUNCTION_F(path); //::AST::Path rv; - + // If the path is directly referring to an external crate - call __ext if( path.m_class.is_Absolute() && path.m_class.as_Absolute().crate != "" ) { const auto& path_abs = path.m_class.as_Absolute(); - + return Resolve_Use_GetBinding__ext(span, crate, path, crate.m_extern_crates.at( path_abs.crate ), 0, allow); } - + const AST::Module* mod = &crate.m_root_module; const auto& nodes = path.nodes(); for( unsigned int i = 0; i < nodes.size()-1; i ++ ) { // TODO: If this came from an import, return the real path? - + //rv = Resolve_Use_CanoniseAndBind_Mod(span, crate, *mod, mv$(rv), nodes[i].name(), parent_modules, Lookup::Type); //const auto& b = rv.binding(); auto b = Resolve_Use_GetBinding_Mod(span, crate, *mod, nodes[i].name(), parent_modules, Lookup::Type); @@ -691,7 +691,7 @@ namespace { if( i != nodes.size() - 1 ) { ERROR(span, E0000, "Encountered enum at unexpected location in import"); } - + const auto& node2 = nodes[i]; int variant_index = -1; for( unsigned int j = 0; j < enum_.variants().size(); j ++ ) @@ -704,12 +704,12 @@ namespace { if( variant_index < 0 ) { ERROR(span, E0000, "Unknown enum variant '" << node2.name() << "'"); } - + return ::AST::PathBinding::make_EnumVar({&enum_, static_cast<unsigned int>(variant_index)}); ), (Module, ASSERT_BUG(span, e.module_ || e.hir, "nullptr module pointer in node " << i << " of " << path); - if( !e.module_ ) + if( !e.module_ ) { assert(e.hir); // TODO: Mangle the original path (or return a new path somehow) @@ -719,7 +719,7 @@ namespace { ) ) } - + assert(mod); return Resolve_Use_GetBinding_Mod(span, crate, *mod, nodes.back().name(), parent_modules, allow); } |