From 1f614012969b0d9052ee1bb0526afddc7e6187b6 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 5 Jun 2016 00:30:12 +0800 Subject: Resolve - Replace `Self` when known --- src/ast/types.cpp | 16 +++------------- src/ast/types.hpp | 12 ++---------- src/hir_typeck/expr.cpp | 1 + src/resolve/absolute.cpp | 31 ++++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/ast/types.cpp b/src/ast/types.cpp index 7b1c4cdb..070f02ba 100644 --- a/src/ast/types.cpp +++ b/src/ast/types.cpp @@ -168,16 +168,6 @@ Ordering TypeRef::ord(const TypeRef& x) const return OrdEqual; ), (Generic, - if( ent.params != x_ent.params ) - { - DEBUG(*this << " == " << x); - if( ent.params ) DEBUG("- (L) " << *ent.params); - if( x_ent.params ) DEBUG("- (R) " << *x_ent.params); - throw ::std::runtime_error("Can't compare mismatched generic types"); - //BUG(m_span, "Can't compare mismatched generic types"); - } - else { - } return ::ord(ent.name, x_ent.name); ), (Path, @@ -245,7 +235,7 @@ Ordering TypeRef::ord(const TypeRef& x) const os << "]"; ) _(Generic, - os << "/* arg */ " << ent.name << "/*"<( m_data.as_Generic().params ); - } bool is_reference() const { return m_data.is_Borrow(); } bool is_pointer() const { return m_data.is_Pointer(); } diff --git a/src/hir_typeck/expr.cpp b/src/hir_typeck/expr.cpp index 7b22f9d3..f0a2cee1 100644 --- a/src/hir_typeck/expr.cpp +++ b/src/hir_typeck/expr.cpp @@ -888,6 +888,7 @@ namespace { // ------------------ if( node_ptr_ptr != nullptr ) { + // TODO: Allow cases where `right`: ::core::ops::Unsize<`left`> // - If left is a trait object, right can unsize // - If left is a slice, right can unsize/deref if( l_e.inner->m_data.is_Slice() && !r_e.inner->m_data.is_Slice() ) diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index 180cab0f..86d35ca7 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -68,6 +68,7 @@ struct Context if( has_self ) { assert( level == GenericSlot::Level::Top ); data.types.push_back( Named { "Self", GenericSlot { level, 0xFFFF } } ); + m_name_context.push_back( Ent::make_ConcreteSelf(nullptr) ); } if( params.ty_params().size() > 0 ) { const auto& typs = params.ty_params(); @@ -106,6 +107,25 @@ struct Context BUG(Span(), "resolve/absolute.cpp - Context::pop(TypeRef) - Mismatched pop"); } } + ::TypeRef get_self() const { + for(auto it = m_name_context.rbegin(); it != m_name_context.rend(); ++ it) + { + TU_MATCH_DEF(Ent, (*it), (e), + ( + ), + (ConcreteSelf, + if( e ) { + return e->clone(); + } + else { + return ::TypeRef("Self"); + } + ) + ) + } + + TODO(Span(), "Error when get_self called with no self"); + } void push_block() { m_block_level += 1; @@ -815,7 +835,16 @@ void Resolve_Absolute_Type(Context& context, TypeRef& type) } ), (Generic, - // TODO: Should this be bound to the relevant index, or just leave as-is? + if( e.name == "Self" ) + { + type = context.get_self(); + } + else + { + auto idx = context.lookup_local(type.span(), e.name, Context::LookupMode::Type); + // TODO: Should this be bound to the relevant index, or just leave as-is? + e.index = idx; + } ), (Path, Resolve_Absolute_Path(context, type.span(), Context::LookupMode::Type, e.path); -- cgit v1.2.3