From 13075f30738556212ca0ff02d28dab75c268c1fd Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 31 Jan 2016 14:53:08 +0800 Subject: Name resolution in anon modules --- src/ast/path.cpp | 6 +++--- src/convert/ast_iterate.cpp | 8 ++++++++ src/convert/resolve.cpp | 29 ++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/ast/path.cpp b/src/ast/path.cpp index c2250999..1703da53 100644 --- a/src/ast/path.cpp +++ b/src/ast/path.cpp @@ -26,8 +26,8 @@ namespace AST { (StructMethod, os << "StructMethod"; ), (TraitMethod, os << "TraitMethod"; ), - (TypeParameter, os << "TypeParameter(" << i.level << " # " << i.idx << ")"; ), - (Variable, os << "Variable(" << i.slot << ")"; ) + (TypeParameter, os << "TyParam(" << i.level << " # " << i.idx << ")"; ), + (Variable, os << "Var(" << i.slot << ")"; ) ) return os; } @@ -60,7 +60,7 @@ void PathNode::print_pretty(::std::ostream& os, bool is_type_context) const os << m_name; if( m_params.size() ) { - if( is_type_context ) + if( ! is_type_context ) os << "::"; os << "<"; os << m_params; diff --git a/src/convert/ast_iterate.cpp b/src/convert/ast_iterate.cpp index fd4ea614..79d4caf8 100644 --- a/src/convert/ast_iterate.cpp +++ b/src/convert/ast_iterate.cpp @@ -299,6 +299,14 @@ void CASTIterator::handle_module(AST::Path path, AST::Module& mod) DEBUG("Handling submod '" << submod.first.name() << "'"); handle_module(path + submod.first.name(), submod.first); } + unsigned int anon_mod_idx = 0; + for( auto& anonmod : mod.anon_mods() ) + { + auto& submod = *anonmod; + DEBUG("Handling submod #" << anon_mod_idx); + handle_module(path + FMT("#" << anon_mod_idx), submod); + anon_mod_idx += 1; + } UNINDENT(); } void CASTIterator::handle_function(AST::Path path, AST::Function& fcn) diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index 6a544713..0e5b9598 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -21,6 +21,7 @@ class CPathResolver: const AST::Crate& m_crate; AST::Module* m_module; AST::Path m_module_path; + ::std::vector< ::std::pair > m_module_stack; struct LocalItem { @@ -358,7 +359,7 @@ void resolve_path(const Span& span, const AST::Crate& root_crate, AST::Path& pat } } if( path.binding().is_Enum() ) { - throw ParseError::Generic( FMT("Unable to find component '" << node.name() << "' of import " << path << " (enum)") ); + ERROR(span, E0000, "Unable to find component '" << node.name() << "' of import " << path << " (enum)" ); } break; } @@ -790,7 +791,7 @@ void CPathResolver::handle_path_abs(const Span& span, AST::Path& path, CASTItera continue ; } - throw ParseError::Generic("Unable to find component '" + node.name() + "'"); + ERROR(span, E0000, "Unable to find component '" << node.name() << "' of path " << path); ), // Sub-module (Module, @@ -1361,13 +1362,25 @@ bool CPathResolver::find_local_item(const Span& span, AST::Path& path, const ::s return false; } bool CPathResolver::find_mod_item(const Span& span, AST::Path& path, const ::std::string& name) { + TRACE_FUNCTION_F("path="<name() = '" << mod->name() << "'"); + if( lookup_path_in_module(span, m_crate, *mod, *modpath, path, name, path.size()==1) ) return true; - if( mod->name() == "" ) - throw ParseError::Todo("Handle anon modules when resoling unqualified relative paths"); - } while( mod->name() == "" ); + DEBUG("mod->name() = '" << mod->name() << "', idx = " << idx); + if( mod->name() == "" && idx > 0 ) { + idx --; + mod = m_module_stack[idx].first; + modpath = &m_module_stack[idx].second; + } + else { + break ; + } + } return false; } bool CPathResolver::find_self_mod_item(const Span& span, AST::Path& path, const ::std::string& name) { @@ -1549,7 +1562,9 @@ void CPathResolver::handle_module(AST::Path path, AST::Module& mod) // NOTE: Assigning here is safe, as the CASTIterator handle_module iterates submodules as the last action m_module = &mod; m_module_path = AST::Path(path); + m_module_stack.push_back( ::std::make_pair(&mod, m_module_path) ); CASTIterator::handle_module(mv$(path), mod); + m_module_stack.pop_back(); m_scope_stack = mv$(saved); } void CPathResolver::handle_trait(AST::Path path, AST::Trait& trait) -- cgit v1.2.3