From c2ddc804aef34ed7593d0123a3f2403a769e2ed0 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 24 Mar 2015 19:14:51 +0800 Subject: Path resolution coming along --- src/ast/ast.cpp | 11 +++++++---- src/convert/ast_iterate.cpp | 15 +++++++++++---- src/convert/resolve.cpp | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 2d5fdca2..b2575317 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -581,11 +581,11 @@ Module::ItemRef Module::find_item(const ::std::string& needle, bool ignore_priva { for( const auto& imp : this->imports() ) { - DEBUG("imp: '" << imp.name << "' = " << imp.data); + //DEBUG("imp: '" << imp.name << "' = " << imp.data); if( !imp.is_pub && ignore_private_wildcard ) { // not public, ignore - DEBUG("Private import, ignore"); + //DEBUG("Private import, '" << imp.name << "' = " << imp.data); } else if( imp.name == needle ) { @@ -613,8 +613,11 @@ Module::ItemRef Module::find_item(const ::std::string& needle, bool ignore_priva // - If it's a module, recurse case AST::Path::MODULE: { auto rv = imp.data.bound_module().find_item(needle); - if( rv.type() != Module::ItemRef::ITEM_none ) - return rv; + if( rv.type() != Module::ItemRef::ITEM_none ) { + // Don't return RV, return the import (so caller can rewrite path if need be) + return ItemRef(imp); + //return rv; + } break; } // - If it's an enum, search for this name and then pass to resolve case AST::Path::ENUM: { diff --git a/src/convert/ast_iterate.cpp b/src/convert/ast_iterate.cpp index 02914c49..153fc1c9 100644 --- a/src/convert/ast_iterate.cpp +++ b/src/convert/ast_iterate.cpp @@ -24,15 +24,20 @@ void CASTIterator::handle_expr(AST::ExprNode& node) } void CASTIterator::handle_params(AST::TypeParams& params) { + DEBUG("params"); for( auto& param : params.params() ) { if( param.is_type() ) local_type( param.name(), TypeRef(TypeRef::TagArg(), param.name()) ); } + DEBUG("Bounds"); for( auto& bound : params.bounds() ) { handle_type(bound.test()); - handle_path(bound.bound(), CASTIterator::MODE_TYPE); + if( !bound.is_trait() ) + DEBUG("namecheck lifetime bounds?"); + else + handle_path(bound.bound(), CASTIterator::MODE_TYPE); } } @@ -236,14 +241,16 @@ void CASTIterator::handle_module(AST::Path path, AST::Module& mod) } void CASTIterator::handle_function(AST::Path path, AST::Function& fcn) { - INDENT(); - DEBUG("(" << path << ", ..."); + TRACE_FUNCTION_F("path = " << path << ", fcn.params() = " << fcn.params()); start_scope(); + DEBUG("params"); handle_params(fcn.params()); + DEBUG("ret type"); handle_type(fcn.rettype()); + DEBUG("args"); for( auto& arg : fcn.args() ) { handle_type(arg.second); @@ -251,6 +258,7 @@ void CASTIterator::handle_function(AST::Path path, AST::Function& fcn) handle_pattern( arg.first, arg.second ); } + DEBUG("code"); if( fcn.code().is_valid() ) { INDENT(); @@ -259,7 +267,6 @@ void CASTIterator::handle_function(AST::Path path, AST::Function& fcn) } end_scope(); - UNINDENT(); } void CASTIterator::handle_impl(AST::Path modpath, AST::Impl& impl) { diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index fada5150..58c15468 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -33,6 +33,10 @@ class CPathResolver: name( ::std::move(name) ), path( ::std::move(path) ) {} + + friend ::std::ostream& operator<<(::std::ostream& os, const LocalItem& x) { + return os << "'" << x.name << "' = " << x.path; + } }; const AST::Crate& m_crate; AST::Module* m_module; @@ -140,6 +144,30 @@ public: } } + void visit(AST::ExprNode_Loop& node) + { + switch( node.m_type ) + { + case AST::ExprNode_Loop::FOR: + AST::NodeVisitor::visit(node.m_cond); + m_res.start_scope(); + m_res.handle_pattern(node.m_pattern, TypeRef()); + AST::NodeVisitor::visit(node.m_code); + m_res.end_scope(); + break; + case AST::ExprNode_Loop::WHILELET: + AST::NodeVisitor::visit(node.m_cond); + m_res.start_scope(); + m_res.handle_pattern(node.m_pattern, TypeRef()); + AST::NodeVisitor::visit(node.m_code); + m_res.end_scope(); + break; + default: + AST::NodeVisitorDef::visit(node); + break; + } + } + void visit(AST::ExprNode_LetBinding& node) { DEBUG("ExprNode_LetBinding"); @@ -175,6 +203,7 @@ void CPathResolver::end_scope() // Returns the bound path for the local item ::rust::option CPathResolver::lookup_local(LocalItem::Type type, const ::std::string& name) const { + DEBUG("m_locals = [" << m_locals << "]"); for( auto it = m_locals.end(); it -- != m_locals.begin(); ) { if( it->type == type && it->name == name ) { @@ -199,12 +228,14 @@ bool lookup_path_in_module(const AST::Crate& crate, const AST::Module& module, c const auto& imp = item.unwrap_Use(); if( imp.name == "" ) { + DEBUG("Wildcard import found, " << imp.data << " + " << path); // Wildcard path, prefix entirely with the path path = imp.data + path; return true; } else { + DEBUG("Named import found, " << imp.data << " + " << path << " [1..]"); path = AST::Path::add_tailing(imp.data, path); path.resolve( crate ); return true; @@ -257,7 +288,7 @@ bool lookup_path_in_module(const AST::Crate& crate, const AST::Module& module, c } void CPathResolver::handle_path(AST::Path& path, CASTIterator::PathMode mode) { - DEBUG("path = " << path); + TRACE_FUNCTION_F("path = " << path << ", m_module_path = " << m_module_path); // Handle generic components of the path for( auto& ent : path.nodes() ) @@ -282,6 +313,7 @@ void CPathResolver::handle_path(AST::Path& path, CASTIterator::PathMode mode) } else if( path.is_relative() ) { + assert(path.size() > 0); DEBUG("Relative, local"); // If there's a single node, and we're in expresion mode, look for a variable @@ -347,6 +379,7 @@ void CPathResolver::handle_path(AST::Path& path, CASTIterator::PathMode mode) } void CPathResolver::handle_type(TypeRef& type) { + TRACE_FUNCTION_F("type = " << type); if( type.is_path() && type.path().is_relative() && type.path().size() == 1 ) { const auto& name = type.path()[0].name(); @@ -376,7 +409,7 @@ void CPathResolver::handle_expr(AST::ExprNode& node) void CPathResolver::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint) { - DEBUG("pat = " << pat); + TRACE_FUNCTION_F("pat = " << pat); // Resolve "Maybe Bind" entries if( pat.data().tag() == AST::Pattern::Data::MaybeBind ) { -- cgit v1.2.3