diff options
author | John Hodge <tpg@mutabah.net> | 2016-05-01 10:02:40 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-05-01 10:02:40 +0800 |
commit | 19a8b93c162c5e93ea65e5b828652f0c1887cdce (patch) | |
tree | 81542b8a097608dc370c8b896bfd43a40bc942a8 /src/resolve/absolute.cpp | |
parent | d296771ea7229a62ee26e818aea483456bc64187 (diff) | |
download | mrust-19a8b93c162c5e93ea65e5b828652f0c1887cdce.tar.gz |
Resolve - Absolute pass structure coming along
Diffstat (limited to 'src/resolve/absolute.cpp')
-rw-r--r-- | src/resolve/absolute.cpp | 194 |
1 files changed, 179 insertions, 15 deletions
diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index c965d2ee..fac61978 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -44,16 +44,129 @@ struct Context }) ); + const ::AST::Crate& m_crate; const ::AST::Module& m_mod; ::std::vector<Ent> m_name_context; - Context(const::AST::Module& mod): + Context(const ::AST::Crate& crate, const::AST::Module& mod): + m_crate(crate), m_mod(mod) {} + + AST::Path lookup_type(const Span& sp, const ::std::string& name) const { + return this->lookup(sp, name, true); + } + AST::Path lookup_value(const Span& sp, const ::std::string& name) const { + return this->lookup(sp, name, false); + } + AST::Path lookup(const Span& sp, const ::std::string& name, bool is_type) const { + for(auto it = m_name_context.rbegin(); it != m_name_context.rend(); ++ it) + { + TU_MATCH(Ent, (*it), (e), + (Module, + // TODO: m_type_items/m_value_items should store the path + if( is_type ) { + auto v = e.mod->m_type_items.find(name); + if( v != e.mod->m_type_items.end() ) { + return e.mod->path() + name; + } + } + else { + auto v = e.mod->m_value_items.find(name); + if( v != e.mod->m_value_items.end() ) { + return e.mod->path() + name; + } + } + ), + (VarBlock, + if( is_type ) { + // ignore + } + else { + TODO(sp, "resolve/absolute.cpp - Context::lookup - Handle VarBlock"); + } + ), + (Generic, + if( is_type ) { + TODO(sp, "resolve/absolute.cpp - Context::lookup - Handle Generic"); + } + else { + // ignore + } + ) + ) + } + + // Top-level module + if( is_type ) { + auto v = m_mod.m_type_items.find(name); + if( v != m_mod.m_type_items.end() ) { + return m_mod.path() + name; + } + } + else { + auto v = m_mod.m_value_items.find(name); + if( v != m_mod.m_value_items.end() ) { + return m_mod.path() + name; + } + } + + ERROR(sp, E0000, "Couldn't find name '" << name << "'"); + } }; -void Resolve_Absolute_Type(const ::AST::Crate& crate, const Context& context, TypeRef& type) + + +void Resolve_Absolute_Path(const Context& context, const Span& sp, bool is_type, ::AST::Path& path); +void Resolve_Absolute_Type(const Context& context, TypeRef& type); +void Resolve_Absolute_Expr(const Context& context, ::AST::Expr& expr); +void Resolve_Absolute_Expr(const Context& context, ::AST::ExprNode& node); +void Resolve_Absolute_Mod(const ::AST::Crate& crate, ::AST::Module& mod); + + +void Resolve_Absolute_Path(const Context& context, const Span& sp, bool is_type, ::AST::Path& path) +{ + TU_MATCH(::AST::Path::Class, (path.m_class), (e), + (Invalid, + BUG(sp, "Encountered invalid path"); + ), + (Local, + // Nothing to do (TODO: Check that it's valid?) + ), + (Relative, + if(e.nodes.size() == 0) + BUG(sp, "Resolve_Absolute_Path - Relative path with no nodes"); + if(e.nodes.size() > 1 || is_type) { + // Look up type + auto p = context.lookup_type(sp, e.nodes[0].name()); + DEBUG("Found - " << p); + path = mv$(p); + } + else { + // Look up value + auto p = context.lookup_value(sp, e.nodes[0].name()); + DEBUG("Found val - " << p); + path = mv$(p); + } + ), + (Self, + TODO(sp, "Resolve_Absolute_Path - Self-relative paths - " << path); + ), + (Super, + TODO(sp, "Resolve_Absolute_Path - Super-relative paths - " << path); + ), + (Absolute, + // Nothing to do (TODO: Bind?) + ), + (UFCS, + TODO(sp, "Resolve_Absolute_Path - UFCS"); + ) + ) +} + +void Resolve_Absolute_Type(const Context& context, TypeRef& type) { + const auto& sp = type.span(); TU_MATCH(TypeData, (type.m_data), (e), (None, // ! type @@ -64,48 +177,99 @@ void Resolve_Absolute_Type(const ::AST::Crate& crate, const Context& context, T (Unit, ), (Macro, - BUG(Span(), "Resolve_Absolute_Type - Encountered an unexpanded macro"); + BUG(sp, "Resolve_Absolute_Type - Encountered an unexpanded macro"); ), (Primitive, ), (Function, - TODO(Span(), "Resolve_Absolute_Type - Function - " << type); + TODO(sp, "Resolve_Absolute_Type - Function - " << type); ), (Tuple, for(auto& t : e.inner_types) - Resolve_Absolute_Type(crate, context, t); + Resolve_Absolute_Type(context, t); ), (Borrow, - Resolve_Absolute_Type(crate, context, *e.inner); + Resolve_Absolute_Type(context, *e.inner); ), (Pointer, - Resolve_Absolute_Type(crate, context, *e.inner); + Resolve_Absolute_Type(context, *e.inner); ), (Array, - Resolve_Absolute_Type(crate, context, *e.inner); + Resolve_Absolute_Type(context, *e.inner); + Resolve_Absolute_Expr(context, *e.size); ), (Generic, - TODO(Span(), "Resolve_Absolute_Type - Encountered generic"); + TODO(sp, "Resolve_Absolute_Type - Encountered generic"); ), (Path, - TODO(Span(), "Resolve_Absolute_Type - Path"); + TODO(sp, "Resolve_Absolute_Type - Path"); ), (TraitObject, - TODO(Span(), "Resolve_Absolute_Type - TraitObject"); + TODO(sp, "Resolve_Absolute_Type - TraitObject"); ) ) } -void Resolve_Absolute_Expr(const ::AST::Crate& crate, const Context& context, ::AST::Expr& expr) +void Resolve_Absolute_Expr(const Context& context, ::AST::Expr& expr) { if( expr.is_valid() ) { - TODO(Span(), "Resolve_Absolute_Expr"); + Resolve_Absolute_Expr(context, expr.node()); } } +void Resolve_Absolute_Expr(const Context& context, ::AST::ExprNode& node) +{ + TRACE_FUNCTION_F(""); + struct NV: + public AST::NodeVisitorDef + { + const Context& context; + + NV(const Context& context): + context(context) + { + } + + void visit(AST::ExprNode_Block& node) override { + if( node.m_local_mod ) { + Resolve_Absolute_Mod(this->context.m_crate, *node.m_local_mod); + + //this->context.push( *node.m_local_mod ); + } + AST::NodeVisitorDef::visit(node); + if( node.m_local_mod ) { + //this->context.pop(); + } + } + + void visit(AST::ExprNode_Match& node) override { + TODO(node.get_pos(), "Resolve_Absolute_Expr - ExprNode_Match"); + } + + void visit(AST::ExprNode_LetBinding& node) override { + TODO(Span(), "Resolve_Absolute_Expr - ExprNode_LetBinding"); + } + void visit(AST::ExprNode_StructLiteral& node) override { + TODO(Span(), "Resolve_Absolute_Expr - ExprNode_StructLiteral"); + } + void visit(AST::ExprNode_CallPath& node) override { + TODO(Span(), "Resolve_Absolute_Expr - ExprNode_CallPath"); + } + void visit(AST::ExprNode_NamedValue& node) override { + Resolve_Absolute_Path(this->context, Span(node.get_pos()), false, node.m_path); + } + void visit(AST::ExprNode_Cast& node) override { + Resolve_Absolute_Type(this->context, node.m_type); + AST::NodeVisitorDef::visit(node); + } + } expr_iter(context); + + node.visit( expr_iter ); +} void Resolve_Absolute_Mod(const ::AST::Crate& crate, ::AST::Module& mod) { + TRACE_FUNCTION_F("(mod="<<mod.path()<<")"); for( auto& i : mod.items() ) { TU_MATCH(AST::Item, (i.data), (e), @@ -133,8 +297,8 @@ void Resolve_Absolute_Mod(const ::AST::Crate& crate, ::AST::Module& mod) TODO(Span(), "Resolve_Absolute_Mod - Function"); ), (Static, - Resolve_Absolute_Type( crate, Context(mod), e.type() ); - Resolve_Absolute_Expr( crate, Context(mod), e.value() ); + Resolve_Absolute_Type( Context(crate, mod), e.type() ); + Resolve_Absolute_Expr( Context(crate, mod), e.value() ); ) ) } |