diff options
-rw-r--r-- | src/mir/dump.cpp | 3 | ||||
-rw-r--r-- | src/mir/from_hir.cpp | 75 | ||||
-rw-r--r-- | src/mir/from_hir.hpp | 2 | ||||
-rw-r--r-- | src/mir/from_hir_match.cpp | 6 | ||||
-rw-r--r-- | src/mir/mir.cpp | 3 | ||||
-rw-r--r-- | src/mir/mir.hpp | 7 | ||||
-rw-r--r-- | src/mir/mir_builder.cpp | 27 |
7 files changed, 110 insertions, 13 deletions
diff --git a/src/mir/dump.cpp b/src/mir/dump.cpp index 3ab267dc..aa17a651 100644 --- a/src/mir/dump.cpp +++ b/src/mir/dump.cpp @@ -270,6 +270,9 @@ namespace { (StaticString, os << "\"" << ce << "\""; ), + (Const, + os << ce.p; + ), (ItemAddr, os << "addr " << ce; ) diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp index 4477d0e6..7c91b34f 100644 --- a/src/mir/from_hir.cpp +++ b/src/mir/from_hir.cpp @@ -123,14 +123,27 @@ namespace { } else if( allow_refutable == 0 ) { ASSERT_BUG(sp, pat.m_data.is_Any(), "Destructure patterns can't bind and match"); - - m_builder.push_stmt_assign( ::MIR::LValue::make_Variable(pat.m_binding.m_slot), mv$(lval) ); } else { // Refutable and binding allowed - m_builder.push_stmt_assign( ::MIR::LValue::make_Variable(pat.m_binding.m_slot), lval.clone() ); - - destructure_from_ex(sp, pat, mv$(lval), 3); + destructure_from_ex(sp, pat, lval.clone(), 3); + } + + switch( pat.m_binding.m_type ) + { + case ::HIR::PatternBinding::Type::Move: + m_builder.push_stmt_assign( ::MIR::LValue::make_Variable(pat.m_binding.m_slot), mv$(lval) ); + break; + case ::HIR::PatternBinding::Type::Ref: + m_builder.push_stmt_assign( ::MIR::LValue::make_Variable(pat.m_binding.m_slot), ::MIR::RValue::make_Borrow({ + 0, ::HIR::BorrowType::Shared, mv$(lval) + }) ); + break; + case ::HIR::PatternBinding::Type::MutRef: + m_builder.push_stmt_assign( ::MIR::LValue::make_Variable(pat.m_binding.m_slot), ::MIR::RValue::make_Borrow({ + 0, ::HIR::BorrowType::Unique, mv$(lval) + }) ); + break; } return; } @@ -1159,8 +1172,58 @@ namespace { } void visit(::HIR::ExprNode_PathValue& node) override { + const auto& sp = node.span(); TRACE_FUNCTION_F("_PathValue - " << node.m_path); - m_builder.set_result( node.span(), ::MIR::LValue::make_Static(node.m_path.clone()) ); + TU_MATCH( ::HIR::Path::Data, (node.m_path.m_data), (pe), + (Generic, + const auto& vi = m_builder.crate().get_valitem_by_path(node.span(), pe.m_path); + TU_MATCHA( (vi), (e), + (Import, + BUG(sp, "All references via imports should be replaced"); + ), + (Constant, + auto tmp = m_builder.new_temporary( e.m_type ); + m_builder.push_stmt_assign( tmp.clone(), ::MIR::Constant::make_Const({node.m_path.clone()}) ); + m_builder.set_result( node.span(), mv$(tmp) ); + ), + (Static, + m_builder.set_result( node.span(), ::MIR::LValue::make_Static(node.m_path.clone()) ); + ), + (StructConstant, + BUG(sp, "StructConstant as PathValue"); + ), + (Function, + // TODO: Obtain function type for this function (i.e. a type that is specifically for this function) + auto fcn_ty_data = ::HIR::FunctionType { + e.m_unsafe, + e.m_abi, + box$( monomorphise_type(sp, e.m_params, pe.m_params, e.m_return) ), + {} + }; + fcn_ty_data.m_arg_types.reserve( e.m_args.size() ); + for(const auto& arg : e.m_args) + { + fcn_ty_data.m_arg_types.push_back( monomorphise_type(sp, e.m_params, pe.m_params, arg.second) ); + } + auto tmp = m_builder.new_temporary( ::HIR::TypeRef( mv$(fcn_ty_data) ) ); + m_builder.push_stmt_assign( tmp.clone(), ::MIR::Constant::make_ItemAddr(node.m_path.clone()) ); + m_builder.set_result( node.span(), mv$(tmp) ); + ), + (StructConstructor, + BUG(sp, "StructConstructor as PathValue"); + ) + ) + ), + (UfcsKnown, + TODO(sp, "PathValue - UfcsKnown"); + ), + (UfcsUnknown, + BUG(sp, "PathValue - Encountered UfcsUnknown - " << node.m_path); + ), + (UfcsInherent, + TODO(sp, "PathValue - UfcsInherent"); + ) + ) } void visit(::HIR::ExprNode_Variable& node) override { diff --git a/src/mir/from_hir.hpp b/src/mir/from_hir.hpp index 9e1ea5c1..ae9c80c8 100644 --- a/src/mir/from_hir.hpp +++ b/src/mir/from_hir.hpp @@ -100,6 +100,8 @@ public: MirBuilder(const StaticTraitResolve& resolve, ::MIR::Function& output); ~MirBuilder(); + const ::HIR::Crate& crate() const { return m_resolve.m_crate; } + // - Values ::MIR::LValue new_temporary(const ::HIR::TypeRef& ty); ::MIR::LValue lvalue_or_temp(const ::HIR::TypeRef& ty, ::MIR::RValue val); diff --git a/src/mir/from_hir_match.cpp b/src/mir/from_hir_match.cpp index 78691dcc..3ad3a11b 100644 --- a/src/mir/from_hir_match.cpp +++ b/src/mir/from_hir_match.cpp @@ -1461,6 +1461,9 @@ void DecisionTreeNode::populate_tree_from_rule(const Span& sp, const PatternRule and_then(branch); } ), + (Const, + throw ""; + ), (ItemAddr, throw ""; ) @@ -1555,6 +1558,9 @@ void DecisionTreeNode::populate_tree_from_rule(const Span& sp, const PatternRule (StaticString, ERROR(sp, E0000, "Use of string in value range patter"); ), + (Const, + throw ""; + ), (ItemAddr, throw ""; ) diff --git a/src/mir/mir.cpp b/src/mir/mir.cpp index 7a201f46..fb69bf3e 100644 --- a/src/mir/mir.cpp +++ b/src/mir/mir.cpp @@ -29,6 +29,9 @@ namespace MIR { (StaticString, os << "\"" << e << "\""; ), + (Const, + os << e.p; + ), (ItemAddr, os << "&" << e; ) diff --git a/src/mir/mir.hpp b/src/mir/mir.hpp index 8989ac35..3c52ab10 100644 --- a/src/mir/mir.hpp +++ b/src/mir/mir.hpp @@ -86,9 +86,10 @@ TAGGED_UNION(Constant, Int, (Uint, ::std::uint64_t), (Float, double), (Bool, bool), - (Bytes, ::std::vector< ::std::uint8_t>), - (StaticString, ::std::string), - (ItemAddr, ::HIR::Path) + (Bytes, ::std::vector< ::std::uint8_t>), // Byte string + (StaticString, ::std::string), // String + (Const, struct { ::HIR::Path p; }), // `const` + (ItemAddr, ::HIR::Path) // address of a value ); extern ::std::ostream& operator<<(::std::ostream& os, const Constant& v); diff --git a/src/mir/mir_builder.cpp b/src/mir/mir_builder.cpp index 5ed178dc..0aca6b81 100644 --- a/src/mir/mir_builder.cpp +++ b/src/mir/mir_builder.cpp @@ -565,8 +565,22 @@ void MirBuilder::with_val_type(const ::MIR::LValue& val, ::std::function<void(co TODO(sp, "Argument"); ), (Static, - TODO(sp, "Static - " << e); - // TODO: Having a reference to the relevant static would be useful. + TU_MATCHA( (e.m_data), (pe), + (Generic, + ASSERT_BUG(sp, pe.m_params.m_types.empty(), "Path params on static"); + const auto& s = m_resolve.m_crate.get_static_by_path(sp, pe.m_path); + cb( s.m_type ); + ), + (UfcsKnown, + TODO(sp, "Static - UfcsKnown - " << e); + ), + (UfcsUnknown, + BUG(sp, "Encountered UfcsUnknown in Static - " << e); + ), + (UfcsInherent, + TODO(sp, "Static - UfcsInherent - " << e); + ) + ) ), (Return, TODO(sp, "Return"); @@ -673,7 +687,11 @@ void MirBuilder::with_val_type(const ::MIR::LValue& val, ::std::function<void(co ), (Struct, // HACK! Create tuple. - TODO(sp, "Downcast - Named"); + ::std::vector< ::HIR::TypeRef> tys; + for(const auto& fld : ve) + tys.push_back( monomorphise_type(sp, enm.m_params, te.path.m_data.as_Generic().m_params, fld.second.ent) ); + ::HIR::TypeRef tup( mv$(tys) ); + cb(tup); ) ) ) @@ -845,6 +863,7 @@ void MirBuilder::drop_scope_values(const ScopeDef& sd) void MirBuilder::moved_lvalue(const ::MIR::LValue& lv) { + TRACE_FUNCTION_F(lv); TU_MATCHA( (lv), (e), (Variable, if( !lvalue_is_copy(lv) ) { @@ -877,7 +896,7 @@ void MirBuilder::moved_lvalue(const ::MIR::LValue& lv) if( lvalue_is_copy(lv) ) { } else { - BUG(Span(), "Move out of index with non-Copy values - &move? - " << lv); + BUG(Span(), "Move out of deref with non-Copy values - &move? - " << lv); moved_lvalue(*e.val); } ), |