diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hir_typeck/expr_cs.cpp | 47 | ||||
-rw-r--r-- | src/hir_typeck/helpers.cpp | 3 |
2 files changed, 46 insertions, 4 deletions
diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp index 777ae1cc..9b81021f 100644 --- a/src/hir_typeck/expr_cs.cpp +++ b/src/hir_typeck/expr_cs.cpp @@ -1795,7 +1795,50 @@ namespace { } this->context.equate_types(node.span(), node.m_res_type, node.m_cache.m_arg_types.back()); - // TODO: Apply derefs! + // Add derefs + if( deref_count > 0 ) + { + DEBUG("- Inserting " << deref_count << " dereferences"); + // Get dereferencing! + auto& node_ptr = node.m_value; + ::HIR::TypeRef tmp_ty; + const ::HIR::TypeRef* cur_ty = &node_ptr->m_res_type; + while( deref_count-- ) + { + auto span = node_ptr->span(); + node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_Deref( mv$(span), mv$(node_ptr) )); + cur_ty = this->context.m_resolve.autoderef(span, *cur_ty, tmp_ty); + assert(cur_ty); + auto ty = cur_ty->clone(); + DEBUG("- Deref " << &*node_ptr << " -> " << ty); + node_ptr->m_res_type = mv$(ty); + } + } + + // Autoref + { + // TODO: Get the unmangled receiver type + const auto& receiver_type = node.m_cache.m_arg_types.front(); + // This only happens when the method is being called on a value + // TODO: How to tell the receiver type correctly once Self is expanded? (not a problem for trait methods... but this is monomorphised) + TU_IFLET(::HIR::TypeRef::Data, (receiver_type.m_data), Borrow, (e), + auto& node_ptr = node.m_value; + // - Add correct borrow operation + auto span = node_ptr->span(); + auto ty = ::HIR::TypeRef::new_borrow(e.type, node_ptr->m_res_type.clone()); + auto op = ( + e.type == ::HIR::BorrowType::Shared ? ::HIR::ExprNode_UniOp::Op::Ref : + /*e.type == ::HIR::BorrowType::Unique ? */::HIR::ExprNode_UniOp::Op::RefMut/* : + 0*/ + ); + node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_UniOp( mv$(span), op, mv$(node_ptr) )); + DEBUG("- Ref " << &*node_ptr << " -> " << ty); + node_ptr->m_res_type = mv$(ty); + ) + else { + // Nothing needs adding + } + } this->m_completed = true; } @@ -3261,10 +3304,8 @@ namespace { ERROR(sp, E0000, "Type mismatch between " << ty_dst << " and " << ty_src << " - Mutability differs"); } context.equate_types(sp, *l_e.inner, *r_e.inner); - // Add downcast auto span = node_ptr->span(); - node_ptr->m_res_type = ty_src.clone(); node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_Cast( mv$(span), mv$(node_ptr), ty_dst.clone() )); node_ptr->m_res_type = ty_dst.clone(); diff --git a/src/hir_typeck/helpers.cpp b/src/hir_typeck/helpers.cpp index c4c112b4..ad12b2cb 100644 --- a/src/hir_typeck/helpers.cpp +++ b/src/hir_typeck/helpers.cpp @@ -2073,8 +2073,9 @@ bool TraitResolution::trait_contains_type(const Span& sp, const ::HIR::GenericPa // ------------------------------------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------------------------------------- -const ::HIR::TypeRef* TraitResolution::autoderef(const Span& sp, const ::HIR::TypeRef& ty, ::HIR::TypeRef& tmp_type) const +const ::HIR::TypeRef* TraitResolution::autoderef(const Span& sp, const ::HIR::TypeRef& ty_in, ::HIR::TypeRef& tmp_type) const { + const auto& ty = this->m_ivars.get_type(ty_in); TU_IFLET(::HIR::TypeRef::Data, ty.m_data, Borrow, e, DEBUG("Deref " << ty << " into " << *e.inner); return &*e.inner; |