From 5d0917df41ad7fedb3fda04d8cb18b495d8adf03 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 4 Nov 2015 11:52:06 +0800 Subject: Improve name resolution logic --- src/ast/ast.hpp | 2 ++ src/ast/path.hpp | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src/ast') diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index f4f2b474..f057d9a3 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -742,9 +742,11 @@ public: ItemRef(): m_type(None) {} Type tag() const { return m_type; } + bool is_None() const { return m_type == None; } const Type& as_None() const { return m_type; } // HACK: Returns &Type in place of &void #define _(ty,ident) \ ItemRef(const ty& ref): m_type(ident), m_ref(&ref) {} \ + bool is_##ident() const { return m_type == ident; } \ const ty& as_##ident() const { assert(m_type == ident); return *(const ty*)m_ref; } _(AST::Module, Module) _(::std::string, Crate) diff --git a/src/ast/path.hpp b/src/ast/path.hpp index 23b4dc11..a2178a8a 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -266,7 +266,19 @@ public: bool is_valid() const { return !m_class.is_Invalid(); } bool is_absolute() const { return m_class.is_Absolute(); } bool is_relative() const { return m_class.is_Relative() || m_class.is_Super() || m_class.is_Self(); } - size_t size() const { return nodes().size(); } + + size_t size() const { + TU_MATCH(Class, (m_class), (ent), + (Invalid, assert(!m_class.is_Invalid()); throw ::std::runtime_error("Path::nodes() on Invalid"); ), + (Local, return 1;), + (Relative, return ent.nodes.size();), + (Self, return ent.nodes.size();), + (Super, return ent.nodes.size();), + (Absolute, return ent.nodes.size();), + (UFCS, return ent.nodes.size();) + ) + throw ::std::runtime_error("Path::nodes() fell off"); + } const ::std::string& crate() const { return m_crate; } bool is_concrete() const; @@ -283,7 +295,7 @@ public: (Self, return ent.nodes;), (Super, return ent.nodes;), (Absolute, return ent.nodes;), - (UFCS, return ent.nodes;) + (UFCS, return ent.nodes;) ) throw ::std::runtime_error("Path::nodes() fell off"); } -- cgit v1.2.3