diff options
Diffstat (limited to 'src/expand/mod.cpp')
-rw-r--r-- | src/expand/mod.cpp | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index dad84423..7f2dc06a 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -494,7 +494,7 @@ struct CExpandExpr: } void visit(::AST::ExprNode_UniOp& node) override { this->visit_nodelete(node, node.m_value); - // - Desugar question mark operator before resolve? + // - Desugar question mark operator before resolve so it can create names if( node.m_type == ::AST::ExprNode_UniOp::QMARK ) { auto core_crate = (crate.m_load_std == ::AST::Crate::LOAD_NONE ? "" : "core"); auto path_Ok = ::AST::Path(core_crate, {::AST::PathNode("result"), ::AST::PathNode("Result"), ::AST::PathNode("Ok")}); @@ -537,6 +537,73 @@ struct CExpandExpr: replacement.reset(new ::AST::ExprNode_Match( mv$(node.m_value), mv$(arms) )); } + else if( node.m_type == ::AST::ExprNode_UniOp::BOX ) + { + // TODO: Should this be a desugar, or a HIR/MIR operation? + // - The only avaliable language item is `exchange_malloc` + // TODO: These should be language items (but are not) + auto core_crate = (crate.m_load_std == ::AST::Crate::LOAD_NONE ? "" : "core"); + auto path_Place = ::AST::Path(core_crate, {::AST::PathNode("ops"), ::AST::PathNode("Place")}); + auto path_BoxPlace = ::AST::Path(core_crate, {::AST::PathNode("ops"), ::AST::PathNode("BoxPlace")}); + auto path_Boxed = ::AST::Path(core_crate, {::AST::PathNode("ops"), ::AST::PathNode("Boxed")}); + auto path_ptr_write= ::AST::Path(core_crate, {::AST::PathNode("intrinsics"), ::AST::PathNode("move_val_init")}); + ::std::vector< ::AST::ExprNodeP> nodes; + // `let mut place = BoxPlace::make_place();` + nodes.push_back(::AST::ExprNodeP(new ::AST::ExprNode_LetBinding( + ::AST::Pattern(AST::Pattern::TagBind(), "place#box", ::AST::PatternBinding::Type::MOVE, true), + ::TypeRef(), + ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( + ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), ::AST::Path(path_BoxPlace), {::AST::PathNode("make_place")}), + {} + )) + ))); + // `let raw_place = Place::pointer(&mut place);` + nodes.push_back(::AST::ExprNodeP(new ::AST::ExprNode_LetBinding( + ::AST::Pattern(AST::Pattern::TagBind(), "raw_place#box", ::AST::PatternBinding::Type::MOVE, false), + ::TypeRef(), + ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( + ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), ::AST::Path(path_Place), {::AST::PathNode("pointer")}), + ::make_vec1( + ::AST::ExprNodeP(new ::AST::ExprNode_UniOp(::AST::ExprNode_UniOp::REFMUT, + ::AST::ExprNodeP(new ::AST::ExprNode_NamedValue( ::AST::Path("place#box") )) + )) + ) + )) + ))); + // `let value = EXPR;` + nodes.push_back(::AST::ExprNodeP(new ::AST::ExprNode_LetBinding( + ::AST::Pattern(AST::Pattern::TagBind(), "value#box", ::AST::PatternBinding::Type::MOVE, false), + ::TypeRef(), + mv$( node.m_value ) + ))); + // `unsafe {` + { + ::std::vector< ::AST::ExprNodeP> nodes_unsafe; + // `::std::ptr::write(raw_place, value);` + nodes_unsafe.push_back(::AST::ExprNodeP(new ::AST::ExprNode_CallPath( + ::AST::Path(path_ptr_write), + ::make_vec2( + ::AST::ExprNodeP(new ::AST::ExprNode_NamedValue( ::AST::Path("raw_place#box") )), + ::AST::ExprNodeP(new ::AST::ExprNode_NamedValue( ::AST::Path("value#box") )) + ) + ))); + // `Boxed::finalize(place)` + nodes_unsafe.push_back(::AST::ExprNodeP(new ::AST::ExprNode_CallPath( + ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), ::AST::Path(path_Boxed), {::AST::PathNode("finalize")}), + ::make_vec1( + ::AST::ExprNodeP(new ::AST::ExprNode_NamedValue( ::AST::Path("place#box") )) + ) + ))); + // `}` + for(auto& n : nodes_unsafe) + n->set_pos( node.get_pos() ); + nodes.push_back(::AST::ExprNodeP(new ::AST::ExprNode_Block( mv$(nodes_unsafe), nullptr ))); + dynamic_cast< ::AST::ExprNode_Block&>(*nodes.back()).m_is_unsafe = true; + } + for(auto& n : nodes) + n->set_pos( node.get_pos() ); + replacement.reset(new ::AST::ExprNode_Block( mv$(nodes), nullptr )); + } } }; |