From 883cf34da3e273cd3878026d855b9579055c09c0 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 22 Dec 2018 18:31:15 +0800 Subject: Constant Evaluation - Evaluate missing associated constants with trait impl versions --- src/hir/expr_ptr.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/hir/expr_ptr.cpp') diff --git a/src/hir/expr_ptr.cpp b/src/hir/expr_ptr.cpp index fcef34bd..7b3a6811 100644 --- a/src/hir/expr_ptr.cpp +++ b/src/hir/expr_ptr.cpp @@ -45,6 +45,13 @@ } +const Span& HIR::ExprPtr::span() const +{ + static Span static_sp; + if( *this ) + return (*this)->span(); + return static_sp; +} const ::MIR::Function* HIR::ExprPtr::get_mir_opt() const { if(!this->m_mir) -- cgit v1.2.3 From 7a1a685ef5e0d831f6762f1bc9171de66d933e32 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 25 Apr 2019 12:38:17 +0800 Subject: HIR - Add more complete privacy handling (allowing for autoderef to skip private fields) --- src/hir/deserialise.cpp | 10 +++-- src/hir/dump.cpp | 4 +- src/hir/expr_ptr.cpp | 6 +++ src/hir/expr_state.hpp | 3 +- src/hir/from_ast.cpp | 78 +++++++++++++++++++++++------------- src/hir/hir.hpp | 64 ++++++++++++++++++++++++++++- src/hir/hir_ops.cpp | 1 + src/hir/serialise.cpp | 6 +-- src/hir_conv/bind.cpp | 28 +++++++++---- src/hir_conv/constant_evaluation.cpp | 3 +- src/hir_expand/closures.cpp | 8 ++-- src/hir_expand/vtable.cpp | 10 ++--- src/hir_typeck/expr_cs.cpp | 7 ++-- src/hir_typeck/expr_visit.cpp | 8 ++-- src/hir_typeck/expr_visit.hpp | 6 ++- src/hir_typeck/helpers.cpp | 10 ++--- src/hir_typeck/helpers.hpp | 4 +- src/resolve/index.cpp | 4 +- src/trans/enumerate.cpp | 4 +- 19 files changed, 189 insertions(+), 75 deletions(-) (limited to 'src/hir/expr_ptr.cpp') diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index cc376474..9933255f 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -101,10 +101,14 @@ rv.push_back( cb() ); return rv; } + ::HIR::Publicity deserialise_pub() + { + return (m_in.read_bool() ? ::HIR::Publicity::new_global() : ::HIR::Publicity::new_none()); + } template ::HIR::VisEnt deserialise_visent() { - return ::HIR::VisEnt { m_in.read_bool(), D::des(*this) }; + return ::HIR::VisEnt { deserialise_pub(), D::des(*this) }; } template @@ -153,7 +157,7 @@ { auto name = m_in.read_string(); rv.m_methods.insert( ::std::make_pair( mv$(name), ::HIR::TypeImpl::VisImplEnt< ::HIR::Function> { - m_in.read_bool(), m_in.read_bool(), deserialise_function() + deserialise_pub(), m_in.read_bool(), deserialise_function() } ) ); } size_t const_count = m_in.read_count(); @@ -161,7 +165,7 @@ { auto name = m_in.read_string(); rv.m_constants.insert( ::std::make_pair( mv$(name), ::HIR::TypeImpl::VisImplEnt< ::HIR::Constant> { - m_in.read_bool(), m_in.read_bool(), deserialise_constant() + deserialise_pub(), m_in.read_bool(), deserialise_constant() } ) ); } // m_src_module doesn't matter after typeck diff --git a/src/hir/dump.cpp b/src/hir/dump.cpp index a60ec643..3b981e26 100644 --- a/src/hir/dump.cpp +++ b/src/hir/dump.cpp @@ -152,7 +152,7 @@ namespace { m_os << "("; for(const auto& fld : flds) { - m_os << (fld.is_public ? "pub " : "") << fld.ent << ", "; + m_os << fld.publicity << " " << fld.ent << ", "; } if( item.m_params.m_bounds.empty() ) { @@ -175,7 +175,7 @@ namespace { inc_indent(); for(const auto& fld : flds) { - m_os << indent() << (fld.second.is_public ? "pub " : "") << fld.first << ": " << fld.second.ent << ",\n"; + m_os << indent() << fld.second.publicity << " " << fld.first << ": " << fld.second.ent << ",\n"; } dec_indent(); m_os << indent() << "}\n"; diff --git a/src/hir/expr_ptr.cpp b/src/hir/expr_ptr.cpp index 7b3a6811..0c219e0f 100644 --- a/src/hir/expr_ptr.cpp +++ b/src/hir/expr_ptr.cpp @@ -90,6 +90,12 @@ void HIR::ExprPtr::set_mir(::MIR::FunctionPointer mir) { assert( !this->m_mir ); m_mir = ::std::move(mir); + // Reset the HIR tree to be a placeholder node (thus freeing the backing memory) + //if( node ) + //{ + // auto sp = node->span(); + // node = ExprPtrInner(::std::unique_ptr(new ::HIR::ExprNode_Tuple(sp, {}))); + //} } diff --git a/src/hir/expr_state.hpp b/src/hir/expr_state.hpp index 5491b4d3..11fffe67 100644 --- a/src/hir/expr_state.hpp +++ b/src/hir/expr_state.hpp @@ -10,8 +10,9 @@ namespace HIR { -struct ExprState +class ExprState { +public: ::HIR::SimplePath m_mod_path; const ::HIR::Module& m_module; diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 014b744c..af6bc9cb 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -868,9 +868,20 @@ const ::AST::Crate* g_ast_crate_ptr; namespace { template - ::HIR::VisEnt new_visent(bool pub, T v) { + ::HIR::VisEnt new_visent(HIR::Publicity pub, T v) { return ::HIR::VisEnt { pub, mv$(v) }; } + + ::HIR::SimplePath get_parent_module(const ::HIR::ItemPath& p) { + const ::HIR::ItemPath* parent_ip = p.parent; + assert(parent_ip); + while(parent_ip->name && parent_ip->name[0] == '#') + { + parent_ip = parent_ip->parent; + assert(parent_ip); + } + return parent_ip->get_simple_path(); + } } ::HIR::Struct LowerHIR_Struct(::HIR::ItemPath path, const ::AST::Struct& ent, const ::AST::AttributeList& attrs) @@ -878,6 +889,9 @@ namespace { TRACE_FUNCTION_F(path); ::HIR::Struct::Data data; + auto priv_path = ::HIR::Publicity::new_priv( get_parent_module(path) ); + auto get_pub = [&](bool is_pub){ return is_pub ? ::HIR::Publicity::new_global() : priv_path; }; + TU_MATCH(::AST::StructData, (ent.m_data), (e), (Unit, data = ::HIR::Struct::Data::make_Unit({}); @@ -886,14 +900,14 @@ namespace { ::HIR::Struct::Data::Data_Tuple fields; for(const auto& field : e.ents) - fields.push_back( { field.m_is_public, LowerHIR_Type(field.m_type) } ); + fields.push_back( { get_pub(field.m_is_public), LowerHIR_Type(field.m_type) } ); data = ::HIR::Struct::Data::make_Tuple( mv$(fields) ); ), (Struct, ::HIR::Struct::Data::Data_Named fields; for(const auto& field : e.ents) - fields.push_back( ::std::make_pair( field.m_name, new_visent(field.m_is_public, LowerHIR_Type(field.m_type)) ) ); + fields.push_back( ::std::make_pair( field.m_name, new_visent( get_pub(field.m_is_public), LowerHIR_Type(field.m_type)) ) ); data = ::HIR::Struct::Data::make_Named( mv$(fields) ); ) ) @@ -956,7 +970,6 @@ namespace { ::HIR::Enum LowerHIR_Enum(::HIR::ItemPath path, const ::AST::Enum& ent, const ::AST::AttributeList& attrs, ::std::function push_struct) { - // 1. Figure out what sort of enum this is (value or data) bool has_value = false; bool has_data = false; @@ -1053,14 +1066,14 @@ namespace { { ::HIR::Struct::Data::Data_Tuple fields; for(const auto& field : ve->m_sub_types) - fields.push_back( new_visent(true, LowerHIR_Type(field)) ); + fields.push_back( new_visent(::HIR::Publicity::new_global(), LowerHIR_Type(field)) ); data = ::HIR::Struct::Data::make_Tuple( mv$(fields) ); } else if( const auto* ve = var.m_data.opt_Struct() ) { ::HIR::Struct::Data::Data_Named fields; for(const auto& field : ve->m_fields) - fields.push_back( ::std::make_pair( field.m_name, new_visent(true, LowerHIR_Type(field.m_type)) ) ); + fields.push_back( ::std::make_pair( field.m_name, new_visent(::HIR::Publicity::new_global(), LowerHIR_Type(field.m_type)) ) ); data = ::HIR::Struct::Data::make_Named( mv$(fields) ); } else @@ -1106,6 +1119,9 @@ namespace { } ::HIR::Union LowerHIR_Union(::HIR::ItemPath path, const ::AST::Union& f, const ::AST::AttributeList& attrs) { + auto priv_path = ::HIR::Publicity::new_priv( get_parent_module(path) ); + auto get_pub = [&](bool is_pub){ return is_pub ? ::HIR::Publicity::new_global() : priv_path; }; + auto repr = ::HIR::Union::Repr::Rust; if( const auto* attr_repr = attrs.get("repr") ) @@ -1124,7 +1140,7 @@ namespace { ::HIR::Struct::Data::Data_Named variants; for(const auto& field : f.m_variants) - variants.push_back( ::std::make_pair( field.m_name, new_visent(field.m_is_public, LowerHIR_Type(field.m_type)) ) ); + variants.push_back( ::std::make_pair( field.m_name, new_visent(get_pub(field.m_is_public), LowerHIR_Type(field.m_type)) ) ); return ::HIR::Union { LowerHIR_GenericParams(f.m_params, nullptr), @@ -1369,10 +1385,10 @@ namespace { }; } -void _add_mod_ns_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::HIR::TypeItem ti) { +void _add_mod_ns_item(::HIR::Module& mod, ::std::string name, ::HIR::Publicity is_pub, ::HIR::TypeItem ti) { mod.m_mod_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::TypeItem> { is_pub, mv$(ti) }) ) ); } -void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::HIR::ValueItem ti) { +void _add_mod_val_item(::HIR::Module& mod, ::std::string name, ::HIR::Publicity is_pub, ::HIR::ValueItem ti) { mod.m_value_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::ValueItem> { is_pub, mv$(ti) }) ) ); } @@ -1383,6 +1399,9 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H mod.m_traits = mv$(traits); + auto priv_path = ::HIR::Publicity::new_priv( path.get_simple_path() ); + auto get_pub = [&](bool is_pub)->::HIR::Publicity{ return (is_pub ? ::HIR::Publicity::new_global() : priv_path); }; + // Populate trait list for(const auto& item : ast_mod.m_type_items) { @@ -1402,7 +1421,7 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H ::std::string name = FMT("#" << i); auto item_path = ::HIR::ItemPath(path, name.c_str()); auto ti = ::HIR::TypeItem::make_Module( LowerHIR_Module(submod, item_path, mod.m_traits) ); - _add_mod_ns_item( mod, mv$(name), false, mv$(ti) ); + _add_mod_ns_item( mod, mv$(name), get_pub(false), mv$(ti) ); } } @@ -1456,12 +1475,12 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H // Ignore - The index is used to add `Import`s ), (Module, - _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Module(e, mv$(item_path)) ); + _add_mod_ns_item( mod, item.name, get_pub(item.is_pub), LowerHIR_Module(e, mv$(item_path)) ); ), (Crate, // All 'extern crate' items should be normalised into a list in the crate root // - If public, add a namespace import here referring to the root of the imported crate - _add_mod_ns_item( mod, item.name, item.is_pub, ::HIR::TypeItem::make_Import({ ::HIR::SimplePath(e.name, {}), false, 0} ) ); + _add_mod_ns_item( mod, item.name, get_pub(item.is_pub), ::HIR::TypeItem::make_Import({ ::HIR::SimplePath(e.name, {}), false, 0} ) ); ), (Type, if( e.type().m_data.is_Any() ) @@ -1470,39 +1489,39 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H { ERROR(item.data.span, E0000, "Generics on extern type"); } - _add_mod_ns_item(mod, item.name, item.is_pub, ::HIR::ExternType {}); + _add_mod_ns_item(mod, item.name, get_pub(item.is_pub), ::HIR::ExternType {}); break; } - _add_mod_ns_item( mod, item.name, item.is_pub, ::HIR::TypeItem::make_TypeAlias( LowerHIR_TypeAlias(e) ) ); + _add_mod_ns_item( mod, item.name, get_pub(item.is_pub), ::HIR::TypeItem::make_TypeAlias( LowerHIR_TypeAlias(e) ) ); ), (Struct, /// Add value reference if( e.m_data.is_Unit() ) { - _add_mod_val_item( mod, item.name, item.is_pub, ::HIR::ValueItem::make_StructConstant({item_path.get_simple_path()}) ); + _add_mod_val_item( mod, item.name, get_pub(item.is_pub), ::HIR::ValueItem::make_StructConstant({item_path.get_simple_path()}) ); } else if( e.m_data.is_Tuple() ) { - _add_mod_val_item( mod, item.name, item.is_pub, ::HIR::ValueItem::make_StructConstructor({item_path.get_simple_path()}) ); + _add_mod_val_item( mod, item.name, get_pub(item.is_pub), ::HIR::ValueItem::make_StructConstructor({item_path.get_simple_path()}) ); } else { } - _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Struct(item_path, e, item.data.attrs) ); + _add_mod_ns_item( mod, item.name, get_pub(item.is_pub), LowerHIR_Struct(item_path, e, item.data.attrs) ); ), (Enum, - auto enm = LowerHIR_Enum(item_path, e, item.data.attrs, [&](auto name, auto str){ _add_mod_ns_item(mod, name, item.is_pub, mv$(str)); }); - _add_mod_ns_item( mod, item.name, item.is_pub, mv$(enm) ); + auto enm = LowerHIR_Enum(item_path, e, item.data.attrs, [&](auto name, auto str){ _add_mod_ns_item(mod, name, get_pub(item.is_pub), mv$(str)); }); + _add_mod_ns_item( mod, item.name, get_pub(item.is_pub), mv$(enm) ); ), (Union, - _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Union(item_path, e, item.data.attrs) ); + _add_mod_ns_item( mod, item.name, get_pub(item.is_pub), LowerHIR_Union(item_path, e, item.data.attrs) ); ), (Trait, - _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Trait(item_path.get_simple_path(), e) ); + _add_mod_ns_item( mod, item.name, get_pub(item.is_pub), LowerHIR_Trait(item_path.get_simple_path(), e) ); ), (Function, - _add_mod_val_item(mod, item.name, item.is_pub, LowerHIR_Function(item_path, item.data.attrs, e, ::HIR::TypeRef{})); + _add_mod_val_item(mod, item.name, get_pub(item.is_pub), LowerHIR_Function(item_path, item.data.attrs, e, ::HIR::TypeRef{})); ), (Static, if( e.s_class() == ::AST::Static::CONST ) - _add_mod_val_item(mod, item.name, item.is_pub, ::HIR::ValueItem::make_Constant(::HIR::Constant { + _add_mod_val_item(mod, item.name, get_pub(item.is_pub), ::HIR::ValueItem::make_Constant(::HIR::Constant { ::HIR::GenericParams {}, LowerHIR_Type( e.type() ), LowerHIR_Expr( e.value() ) @@ -1516,7 +1535,7 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H linkage.name = item.name; } - _add_mod_val_item(mod, item.name, item.is_pub, ::HIR::ValueItem::make_Static(::HIR::Static { + _add_mod_val_item(mod, item.name, get_pub(item.is_pub), ::HIR::ValueItem::make_Static(::HIR::Static { mv$(linkage), (e.s_class() == ::AST::Static::MUT), LowerHIR_Type( e.type() ), @@ -1542,7 +1561,7 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H DEBUG("Import NS " << ie.first << " = " << hir_path); ti = ::HIR::TypeItem::make_Import({ mv$(hir_path), false, 0 }); } - _add_mod_ns_item(mod, ie.first, ie.second.is_pub, mv$(ti)); + _add_mod_ns_item(mod, ie.first, get_pub(ie.second.is_pub), mv$(ti)); } } for( const auto& ie : ast_mod.m_value_items ) @@ -1561,7 +1580,7 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H vi = ::HIR::ValueItem::make_Import({ mv$(hir_path), true, pb.idx }); } } - _add_mod_val_item(mod, ie.first, ie.second.is_pub, mv$(vi)); + _add_mod_val_item(mod, ie.first, get_pub(ie.second.is_pub), mv$(vi)); } } @@ -1687,6 +1706,9 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat auto type = LowerHIR_Type(impl.def().type()); ::HIR::ItemPath path(type); + auto priv_path = ::HIR::Publicity::new_priv( LowerHIR_SimplePath(Span(), ast_mod.path()) ); // TODO: Does this need to consume anon modules? + auto get_pub = [&](bool is_pub){ return is_pub ? ::HIR::Publicity::new_global() : priv_path; }; + ::std::map< ::std::string, ::HIR::TypeImpl::VisImplEnt< ::HIR::Function> > methods; ::std::map< ::std::string, ::HIR::TypeImpl::VisImplEnt< ::HIR::Constant> > constants; @@ -1703,7 +1725,7 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat ), (Static, if( e.s_class() == ::AST::Static::CONST ) { - constants.insert( ::std::make_pair(item.name, ::HIR::TypeImpl::VisImplEnt< ::HIR::Constant> { item.is_pub, item.is_specialisable, ::HIR::Constant { + constants.insert( ::std::make_pair(item.name, ::HIR::TypeImpl::VisImplEnt< ::HIR::Constant> { get_pub(item.is_pub), item.is_specialisable, ::HIR::Constant { ::HIR::GenericParams {}, LowerHIR_Type( e.type() ), LowerHIR_Expr( e.value() ) @@ -1715,7 +1737,7 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat ), (Function, methods.insert( ::std::make_pair(item.name, ::HIR::TypeImpl::VisImplEnt< ::HIR::Function> { - item.is_pub, item.is_specialisable, LowerHIR_Function(item_path, item.data->attrs, e, type) + get_pub(item.is_pub), item.is_specialisable, LowerHIR_Function(item_path, item.data->attrs, e, type) } ) ); ) ) diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index 48787583..f0844580 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -41,10 +41,70 @@ class TypeItem; class ItemPath; +class Publicity +{ + ::std::shared_ptr<::HIR::SimplePath> vis_path; + + Publicity(::std::shared_ptr<::HIR::SimplePath> p) + :vis_path(p) + { + } +public: + + static Publicity new_global() { + return Publicity({}); + } + static Publicity new_none() { + static ::std::shared_ptr<::HIR::SimplePath> none_path = ::std::make_shared(); + return Publicity(none_path); + } + static Publicity new_priv(::HIR::SimplePath p) { + return Publicity(::std::make_shared(::std::move(p))); + } + + bool is_global() const { + return !vis_path; + } + bool is_visible(const ::HIR::SimplePath& p) const { + // No path = global public + if( !vis_path ) + return true; + // Empty simple path = full private + if( *vis_path == ::HIR::SimplePath() ) { + return false; + } + // Crate names must match + if(p.m_crate_name != vis_path->m_crate_name) + return false; + // `p` must be a child of vis_path + if(p.m_components.size() < vis_path->m_components.size()) + return false; + for(size_t i = 0; i < vis_path->m_components.size(); i ++) + { + if(p.m_components[i] != vis_path->m_components[i]) + return false; + } + return true; + } + + friend ::std::ostream& operator<<(::std::ostream& os, const Publicity& x) { + if( !x.vis_path ) { + os << "pub"; + } + else if( *x.vis_path == ::HIR::SimplePath() ) { + os << "priv"; + } + else { + os << "pub(" << *x.vis_path << ")"; + } + return os; + } +}; + template struct VisEnt { - bool is_public; + Publicity publicity; Ent ent; }; @@ -396,7 +456,7 @@ class TypeImpl public: template struct VisImplEnt { - bool is_pub; + Publicity publicity; bool is_specialisable; T data; }; diff --git a/src/hir/hir_ops.cpp b/src/hir/hir_ops.cpp index b09dbdf7..89e48c80 100644 --- a/src/hir/hir_ops.cpp +++ b/src/hir/hir_ops.cpp @@ -1036,6 +1036,7 @@ const ::MIR::Function* HIR::Crate::get_or_gen_mir(const ::HIR::ItemPath& ip, con ms.m_impl_generics = ep.m_state->m_impl_generics; ms.m_item_generics = ep.m_state->m_item_generics; ms.m_traits = ep.m_state->m_traits; + ms.m_mod_paths.push_back(ep.m_state->m_mod_path); Typecheck_Code(ms, const_cast<::HIR::Function::args_t&>(args), ret_ty, ep_mut); //Debug_SetStagePre("Expand HIR Annotate"); HIR_Expand_AnnotateUsage_Expr(*this, ep_mut); diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp index 0d01ed25..dd81596f 100644 --- a/src/hir/serialise.cpp +++ b/src/hir/serialise.cpp @@ -60,7 +60,7 @@ template void serialise(const ::HIR::VisEnt& e) { - m_out.write_bool(e.is_public); + m_out.write_bool(e.publicity.is_global()); // At this stage, we only care if the item is visible outside the crate or not serialise(e.ent); } template @@ -322,14 +322,14 @@ m_out.write_count(impl.m_methods.size()); for(const auto& v : impl.m_methods) { m_out.write_string(v.first); - m_out.write_bool(v.second.is_pub); + m_out.write_bool(v.second.publicity.is_global()); m_out.write_bool(v.second.is_specialisable); serialise(v.second.data); } m_out.write_count(impl.m_constants.size()); for(const auto& v : impl.m_constants) { m_out.write_string(v.first); - m_out.write_bool(v.second.is_pub); + m_out.write_bool(v.second.publicity.is_global()); m_out.write_bool(v.second.is_specialisable); serialise(v.second.data); } diff --git a/src/hir_conv/bind.cpp b/src/hir_conv/bind.cpp index c26ae03f..a3b0041b 100644 --- a/src/hir_conv/bind.cpp +++ b/src/hir_conv/bind.cpp @@ -120,7 +120,7 @@ namespace { m_cur_module.ptr = &mod; m_cur_module.path = &p; - m_ms.push_traits(mod); + m_ms.push_traits(p, mod); ::HIR::Visitor::visit_module(p, mod); m_ms.pop_traits(mod); @@ -512,12 +512,16 @@ namespace { void visit_type_impl(::HIR::TypeImpl& impl) override { - TRACE_FUNCTION_F("impl " << impl.m_type); + TRACE_FUNCTION_F("impl " << impl.m_type << " - from " << impl.m_src_module); auto _ = this->m_ms.set_impl_generics(impl.m_params); + auto mod_ip = ::HIR::ItemPath(impl.m_src_module); const auto* mod = (impl.m_src_module != ::HIR::SimplePath() ? &this->m_ms.m_crate.get_mod_by_path(Span(), impl.m_src_module) : nullptr); - if(mod) - m_ms.push_traits(*mod); + if(mod) { + m_ms.push_traits(impl.m_src_module, *mod); + m_cur_module.ptr = mod; + m_cur_module.path = &mod_ip; + } ::HIR::Visitor::visit_type_impl(impl); if(mod) m_ms.pop_traits(*mod); @@ -527,9 +531,13 @@ namespace { TRACE_FUNCTION_F("impl " << trait_path << " for " << impl.m_type); auto _ = this->m_ms.set_impl_generics(impl.m_params); + auto mod_ip = ::HIR::ItemPath(impl.m_src_module); const auto* mod = (impl.m_src_module != ::HIR::SimplePath() ? &this->m_ms.m_crate.get_mod_by_path(Span(), impl.m_src_module) : nullptr); - if(mod) - m_ms.push_traits(*mod); + if(mod) { + m_ms.push_traits(impl.m_src_module, *mod); + m_cur_module.ptr = mod; + m_cur_module.path = &mod_ip; + } m_ms.m_traits.push_back( ::std::make_pair( &trait_path, &this->m_ms.m_crate.get_trait_by_path(Span(), trait_path) ) ); ::HIR::Visitor::visit_trait_impl(trait_path, impl); m_ms.m_traits.pop_back( ); @@ -541,9 +549,13 @@ namespace { TRACE_FUNCTION_F("impl " << trait_path << " for " << impl.m_type << " { }"); auto _ = this->m_ms.set_impl_generics(impl.m_params); + auto mod_ip = ::HIR::ItemPath(impl.m_src_module); const auto* mod = (impl.m_src_module != ::HIR::SimplePath() ? &this->m_ms.m_crate.get_mod_by_path(Span(), impl.m_src_module) : nullptr); - if(mod) - m_ms.push_traits(*mod); + if(mod) { + m_ms.push_traits(impl.m_src_module, *mod); + m_cur_module.ptr = mod; + m_cur_module.path = &mod_ip; + } ::HIR::Visitor::visit_marker_impl(trait_path, impl); if(mod) m_ms.pop_traits(*mod); diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp index 3d1bf4a5..6049056c 100644 --- a/src/hir_conv/constant_evaluation.cpp +++ b/src/hir_conv/constant_evaluation.cpp @@ -1035,6 +1035,7 @@ namespace { tp.pp_impl = ms.pp_impl->clone(); ep.m_mir = Trans_Monomorphise(resolve, mv$(tp), template_const.m_value.m_mir); ep.m_state = ::HIR::ExprStatePtr( ::HIR::ExprState(*m_mod, m_mod_path->get_simple_path()) ); + DEBUG("TMP TMP " << trait_path << " - " << ep.m_state->m_mod_path); ep.m_state->stage = ::HIR::ExprState::Stage::Mir; impl.m_constants.insert(::std::make_pair( vi.first, @@ -1211,7 +1212,7 @@ namespace { { // ::std::unique_ptr> ::std::unique_ptr<::HIR::VisEnt<::HIR::ValueItem>> iv; - iv.reset( new ::HIR::VisEnt<::HIR::ValueItem> { false, ::HIR::ValueItem::make_Static(mv$(v.second)) } ); + iv.reset( new ::HIR::VisEnt<::HIR::ValueItem> { ::HIR::Publicity::new_none(), ::HIR::ValueItem::make_Static(mv$(v.second)) } ); mod.m_value_items.insert(::std::make_pair( v.first, mv$(iv) )); } mod.m_inline_statics.clear(); diff --git a/src/hir_expand/closures.cpp b/src/hir_expand/closures.cpp index ee0cff08..d7d476e5 100644 --- a/src/hir_expand/closures.cpp +++ b/src/hir_expand/closures.cpp @@ -720,7 +720,7 @@ namespace { // - Fix type to replace closure types with known paths ExprVisitor_Fixup fixup { m_resolve.m_crate, ¶ms, monomorph_cb }; fixup.visit_type(ty_mono); - capture_types.push_back( ::HIR::VisEnt< ::HIR::TypeRef> { false, mv$(ty_mono) } ); + capture_types.push_back( ::HIR::VisEnt< ::HIR::TypeRef> { ::HIR::Publicity::new_none(), mv$(ty_mono) } ); } auto closure_struct_path = m_new_type( ::HIR::Struct { @@ -1182,7 +1182,7 @@ namespace { m_new_type = [&](auto s)->auto { auto name = FMT("closure#I_" << closure_count); closure_count += 1; - auto boxed = box$(( ::HIR::VisEnt< ::HIR::TypeItem> { false, ::HIR::TypeItem( mv$(s) ) } )); + auto boxed = box$(( ::HIR::VisEnt< ::HIR::TypeItem> { ::HIR::Publicity::new_none(), ::HIR::TypeItem( mv$(s) ) } )); crate.m_root_module.m_mod_items.insert( ::std::make_pair(name, mv$(boxed)) ); return ::HIR::SimplePath(crate.m_crate_name, {}) + name; }; @@ -1209,7 +1209,7 @@ namespace { crate.m_type_impls.push_back( ::HIR::TypeImpl { mv$(impl.second.m_params), mv$(impl.second.m_type), - make_map1(impl.second.m_methods.begin()->first, ::HIR::TypeImpl::VisImplEnt< ::HIR::Function> { true, false, mv$(impl.second.m_methods.begin()->second.data) }), + make_map1(impl.second.m_methods.begin()->first, ::HIR::TypeImpl::VisImplEnt< ::HIR::Function> { ::HIR::Publicity::new_global(), false, mv$(impl.second.m_methods.begin()->second.data) }), {}, mv$(impl.second.m_src_module) } ); @@ -1233,7 +1233,7 @@ namespace { m_new_type = [&](auto s)->auto { auto name = FMT("closure#" << closure_count); closure_count += 1; - auto boxed = box$( (::HIR::VisEnt< ::HIR::TypeItem> { false, ::HIR::TypeItem( mv$(s) ) }) ); + auto boxed = box$( (::HIR::VisEnt< ::HIR::TypeItem> { ::HIR::Publicity::new_none(), ::HIR::TypeItem( mv$(s) ) }) ); mod.m_mod_items.insert( ::std::make_pair(name, mv$(boxed)) ); return (p + name).get_simple_path(); }; diff --git a/src/hir_expand/vtable.cpp b/src/hir_expand/vtable.cpp index 79e163ac..2b3dcfb4 100644 --- a/src/hir_expand/vtable.cpp +++ b/src/hir_expand/vtable.cpp @@ -32,7 +32,7 @@ namespace { ::std::vector< decltype(mod.m_mod_items)::value_type> new_types; m_new_type = [&](bool pub, auto name, auto s)->auto { - auto boxed = box$( (::HIR::VisEnt< ::HIR::TypeItem> { pub, ::HIR::TypeItem( mv$(s) ) }) ); + auto boxed = box$( (::HIR::VisEnt< ::HIR::TypeItem> { (pub ? ::HIR::Publicity::new_global() : ::HIR::Publicity::new_none()), ::HIR::TypeItem( mv$(s) ) }) ); auto ret = (p + name).get_simple_path(); new_types.push_back( ::std::make_pair( mv$(name), mv$(boxed)) ); return ret; @@ -173,7 +173,7 @@ namespace { DEBUG("- '" << vi.first << "' is @" << fields.size()); fields.push_back( ::std::make_pair( vi.first, - ::HIR::VisEnt< ::HIR::TypeRef> { true, mv$(fcn_type) } + ::HIR::VisEnt< ::HIR::TypeRef> { ::HIR::Publicity::new_global(), mv$(fcn_type) } ) ); ), (Static, @@ -204,11 +204,11 @@ namespace { ft.m_abi = ABI_RUST; ft.m_rettype.reset( new ::HIR::TypeRef(::HIR::TypeRef::new_unit()) ); ft.m_arg_types.push_back( ::HIR::TypeRef::new_pointer(::HIR::BorrowType::Owned, ::HIR::TypeRef::new_unit()) ); - vtc.fields.push_back(::std::make_pair( "#drop_glue", ::HIR::VisEnt<::HIR::TypeRef> { false, ::HIR::TypeRef(mv$(ft)) } )); + vtc.fields.push_back(::std::make_pair( "#drop_glue", ::HIR::VisEnt<::HIR::TypeRef> { ::HIR::Publicity::new_none(), ::HIR::TypeRef(mv$(ft)) } )); // - Size of data - vtc.fields.push_back(::std::make_pair( "#size", ::HIR::VisEnt<::HIR::TypeRef> { false, ::HIR::CoreType::Usize } )); + vtc.fields.push_back(::std::make_pair( "#size", ::HIR::VisEnt<::HIR::TypeRef> { ::HIR::Publicity::new_none(), ::HIR::CoreType::Usize } )); // - Alignment of data - vtc.fields.push_back(::std::make_pair( "#align", ::HIR::VisEnt<::HIR::TypeRef> { false, ::HIR::CoreType::Usize } )); + vtc.fields.push_back(::std::make_pair( "#align", ::HIR::VisEnt<::HIR::TypeRef> { ::HIR::Publicity::new_none(), ::HIR::CoreType::Usize } )); // - Add methods if( ! vtc.add_ents_from_trait(tr, trait_path) ) { diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp index 762adf5a..1fd44698 100644 --- a/src/hir_typeck/expr_cs.cpp +++ b/src/hir_typeck/expr_cs.cpp @@ -156,9 +156,9 @@ struct Context const ::HIR::SimplePath m_lang_Box; - Context(const ::HIR::Crate& crate, const ::HIR::GenericParams* impl_params, const ::HIR::GenericParams* item_params): + Context(const ::HIR::Crate& crate, const ::HIR::GenericParams* impl_params, const ::HIR::GenericParams* item_params, const ::HIR::SimplePath& mod_path): m_crate(crate), - m_resolve(m_ivars, crate, impl_params, item_params) + m_resolve(m_ivars, crate, impl_params, item_params, mod_path) ,next_rule_idx( 0 ) ,m_lang_Box( crate.get_lang_item_path_opt("owned_box") ) { @@ -8062,7 +8062,8 @@ void Typecheck_Code_CS(const typeck::ModuleState& ms, t_args& args, const ::HIR: TRACE_FUNCTION; auto root_ptr = expr.into_unique(); - Context context { ms.m_crate, ms.m_impl_generics, ms.m_item_generics }; + assert(!ms.m_mod_paths.empty()); + Context context { ms.m_crate, ms.m_impl_generics, ms.m_item_generics, ms.m_mod_paths.back() }; for( auto& arg : args ) { context.handle_pattern( Span(), arg.first, arg.second ); diff --git a/src/hir_typeck/expr_visit.cpp b/src/hir_typeck/expr_visit.cpp index 1a153aaf..d713c135 100644 --- a/src/hir_typeck/expr_visit.cpp +++ b/src/hir_typeck/expr_visit.cpp @@ -36,7 +36,7 @@ namespace { public: void visit_module(::HIR::ItemPath p, ::HIR::Module& mod) override { - m_ms.push_traits(mod); + m_ms.push_traits(p, mod); ::HIR::Visitor::visit_module(p, mod); m_ms.pop_traits(mod); } @@ -58,7 +58,7 @@ namespace { auto _ = this->m_ms.set_impl_generics(impl.m_params); const auto& mod = this->m_ms.m_crate.get_mod_by_path(Span(), impl.m_src_module); - m_ms.push_traits(mod); + m_ms.push_traits(impl.m_src_module, mod); ::HIR::Visitor::visit_type_impl(impl); m_ms.pop_traits(mod); } @@ -68,7 +68,7 @@ namespace { auto _ = this->m_ms.set_impl_generics(impl.m_params); const auto& mod = this->m_ms.m_crate.get_mod_by_path(Span(), impl.m_src_module); - m_ms.push_traits(mod); + m_ms.push_traits(impl.m_src_module, mod); m_ms.m_traits.push_back( ::std::make_pair( &trait_path, &this->m_ms.m_crate.get_trait_by_path(Span(), trait_path) ) ); ::HIR::Visitor::visit_trait_impl(trait_path, impl); m_ms.m_traits.pop_back( ); @@ -80,7 +80,7 @@ namespace { auto _ = this->m_ms.set_impl_generics(impl.m_params); const auto& mod = this->m_ms.m_crate.get_mod_by_path(Span(), impl.m_src_module); - m_ms.push_traits(mod); + m_ms.push_traits(impl.m_src_module, mod); ::HIR::Visitor::visit_marker_impl(trait_path, impl); m_ms.pop_traits(mod); } diff --git a/src/hir_typeck/expr_visit.hpp b/src/hir_typeck/expr_visit.hpp index 21a775dc..9388b1dd 100644 --- a/src/hir_typeck/expr_visit.hpp +++ b/src/hir_typeck/expr_visit.hpp @@ -5,6 +5,7 @@ * hir_typeck/expr_visit.hpp * - Helpers for the HIR typecheck expression visiting */ +#include namespace typeck { struct ModuleState @@ -15,6 +16,7 @@ namespace typeck { ::HIR::GenericParams* m_item_generics; ::std::vector< ::std::pair< const ::HIR::SimplePath*, const ::HIR::Trait* > > m_traits; + ::std::vector m_mod_paths; ModuleState(const ::HIR::Crate& crate): m_crate(crate), @@ -44,8 +46,9 @@ namespace typeck { return NullOnDrop< ::HIR::GenericParams>(m_item_generics); } - void push_traits(const ::HIR::Module& mod) { + void push_traits(::HIR::ItemPath p, const ::HIR::Module& mod) { auto sp = Span(); + m_mod_paths.push_back( p.get_simple_path() ); DEBUG("Module has " << mod.m_traits.size() << " in-scope traits"); // - Push a NULL entry to prevent parent module import lists being searched m_traits.push_back( ::std::make_pair(nullptr, nullptr) ); @@ -59,6 +62,7 @@ namespace typeck { for(unsigned int i = 0; i < mod.m_traits.size(); i ++ ) m_traits.pop_back(); m_traits.pop_back(); + m_mod_paths.pop_back(); } }; } diff --git a/src/hir_typeck/helpers.cpp b/src/hir_typeck/helpers.cpp index f94cdf26..0ebb9c07 100644 --- a/src/hir_typeck/helpers.cpp +++ b/src/hir_typeck/helpers.cpp @@ -4334,8 +4334,8 @@ bool TraitResolution::find_field(const Span& sp, const ::HIR::TypeRef& ty, const (Tuple, for( unsigned int i = 0; i < se.size(); i ++ ) { - // TODO: Privacy - if( FMT(i) == name ) { + DEBUG(i << ": " << se[i].publicity); + if( se[i].publicity.is_visible(this->m_vis_path) && FMT(i) == name ) { field_ty = monomorphise_type_with(sp, se[i].ent, monomorph); return true; } @@ -4344,8 +4344,8 @@ bool TraitResolution::find_field(const Span& sp, const ::HIR::TypeRef& ty, const (Named, for( const auto& fld : se ) { - // TODO: Privacy - if( fld.first == name ) { + DEBUG(fld.first << ": " << fld.second.publicity << ", " << this->m_vis_path); + if( fld.second.publicity.is_visible(this->m_vis_path) && fld.first == name ) { field_ty = monomorphise_type_with(sp, fld.second.ent, monomorph); return true; } @@ -4379,7 +4379,7 @@ bool TraitResolution::find_field(const Span& sp, const ::HIR::TypeRef& ty, const for( const auto& fld : unm.m_variants ) { // TODO: Privacy - if( fld.first == name ) { + if( fld.second.publicity.is_visible(this->m_vis_path) && fld.first == name ) { field_ty = monomorphise_type_with(sp, fld.second.ent, monomorph); return true; } diff --git a/src/hir_typeck/helpers.hpp b/src/hir_typeck/helpers.hpp index 651a36c8..dd9a1581 100644 --- a/src/hir_typeck/helpers.hpp +++ b/src/hir_typeck/helpers.hpp @@ -150,17 +150,19 @@ class TraitResolution const ::HIR::Crate& m_crate; const ::HIR::GenericParams* m_impl_params; const ::HIR::GenericParams* m_item_params; + const ::HIR::SimplePath& m_vis_path; ::std::map< ::HIR::TypeRef, ::HIR::TypeRef> m_type_equalities; ::HIR::SimplePath m_lang_Box; mutable ::std::vector< ::HIR::TypeRef> m_eat_active_stack; public: - TraitResolution(const HMTypeInferrence& ivars, const ::HIR::Crate& crate, const ::HIR::GenericParams* impl_params, const ::HIR::GenericParams* item_params): + TraitResolution(const HMTypeInferrence& ivars, const ::HIR::Crate& crate, const ::HIR::GenericParams* impl_params, const ::HIR::GenericParams* item_params, const ::HIR::SimplePath& vis_path): m_ivars(ivars), m_crate(crate), m_impl_params( impl_params ), m_item_params( item_params ) + ,m_vis_path(vis_path) { prep_indexes(); m_lang_Box = crate.get_lang_item_path_opt("owned_box"); diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp index f010b420..f38219ef 100644 --- a/src/resolve/index.cpp +++ b/src/resolve/index.cpp @@ -286,7 +286,7 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C { for(const auto& it : hmod.m_mod_items) { const auto& ve = *it.second; - if( ve.is_public ) { + if( ve.publicity.is_global() ) { const auto* vep = &ve.ent; AST::Path p; if( vep->is_Import() ) { @@ -346,7 +346,7 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C } for(const auto& it : hmod.m_value_items) { const auto& ve = *it.second; - if( ve.is_public ) { + if( ve.publicity.is_global() ) { AST::Path p; const auto* vep = &ve.ent; if( ve.ent.is_Import() ) { diff --git a/src/trans/enumerate.cpp b/src/trans/enumerate.cpp index e1e2bbd5..afdbd626 100644 --- a/src/trans/enumerate.cpp +++ b/src/trans/enumerate.cpp @@ -152,14 +152,14 @@ namespace { const bool EMIT_ALL = true; for(auto& vi : mod.m_value_items) { - Trans_Enumerate_ValItem(state, vi.second->ent, EMIT_ALL || (is_visible && vi.second->is_public), [&](){ return mod_path + vi.first; }); + Trans_Enumerate_ValItem(state, vi.second->ent, EMIT_ALL || (is_visible && vi.second->publicity.is_global()), [&](){ return mod_path + vi.first; }); } for(auto& ti : mod.m_mod_items) { if(auto* e = ti.second->ent.opt_Module() ) { - Trans_Enumerate_Public_Mod(state, *e, mod_path + ti.first, ti.second->is_public); + Trans_Enumerate_Public_Mod(state, *e, mod_path + ti.first, ti.second->publicity.is_global()); } } } -- cgit v1.2.3 From 4c4a7b88914861b644ec56738ced8cfc179f93f9 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 5 May 2019 12:42:57 +0800 Subject: MIR - Wrap HIR::Path-s in MIR::Constant in unique_ptr --- src/hir/deserialise.cpp | 4 +-- src/hir/expr_ptr.cpp | 13 ++++--- src/hir/serialise.cpp | 4 +-- src/hir_conv/bind.cpp | 47 ++++++++++--------------- src/hir_conv/constant_evaluation.cpp | 6 ++-- src/mir/cleanup.cpp | 18 +++++----- src/mir/dump.cpp | 4 +-- src/mir/from_hir.cpp | 20 ++++++----- src/mir/helpers.cpp | 10 +++--- src/mir/mir.cpp | 12 +++---- src/mir/mir.hpp | 67 ++++++++++++++++++++++++++++++------ src/mir/optimise.cpp | 4 +-- src/trans/codegen_c.cpp | 8 ++--- src/trans/codegen_mmir.cpp | 2 +- src/trans/enumerate.cpp | 4 +-- src/trans/monomorphise.cpp | 6 ++-- 16 files changed, 135 insertions(+), 94 deletions(-) (limited to 'src/hir/expr_ptr.cpp') diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index cd25a3cb..c5350b55 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -532,8 +532,8 @@ return ::MIR::Constant::make_Bytes( mv$(bytes) ); } _(StaticString, m_in.read_string() ) - _(Const, { deserialise_path() } ) - _(ItemAddr, deserialise_path() ) + _(Const, { box$(deserialise_path()) } ) + _(ItemAddr, box$(deserialise_path()) ) #undef _ default: BUG(Span(), "Bad tag for MIR::Const - " << tag); diff --git a/src/hir/expr_ptr.cpp b/src/hir/expr_ptr.cpp index 0c219e0f..7f493a0d 100644 --- a/src/hir/expr_ptr.cpp +++ b/src/hir/expr_ptr.cpp @@ -91,11 +91,14 @@ void HIR::ExprPtr::set_mir(::MIR::FunctionPointer mir) assert( !this->m_mir ); m_mir = ::std::move(mir); // Reset the HIR tree to be a placeholder node (thus freeing the backing memory) - //if( node ) - //{ - // auto sp = node->span(); - // node = ExprPtrInner(::std::unique_ptr(new ::HIR::ExprNode_Tuple(sp, {}))); - //} + if( false && node ) + { + auto sp = node->span(); + node = ExprPtrInner(::std::unique_ptr(new ::HIR::ExprNode_Loop( + sp, "", + ::std::unique_ptr(new ::HIR::ExprNode_Tuple(sp, {})) + ))); + } } diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp index e90cf995..2c72c801 100644 --- a/src/hir/serialise.cpp +++ b/src/hir/serialise.cpp @@ -795,10 +795,10 @@ m_out.write_string(e); ), (Const, - serialise_path(e.p); + serialise_path(*e.p); ), (ItemAddr, - serialise_path(e); + serialise_path(*e); ) ) } diff --git a/src/hir_conv/bind.cpp b/src/hir_conv/bind.cpp index 940b2cab..84854315 100644 --- a/src/hir_conv/bind.cpp +++ b/src/hir_conv/bind.cpp @@ -729,25 +729,29 @@ namespace { ) ) } + static void visit_constant(Visitor& upper_visitor, ::MIR::Constant& e) + { + TU_MATCHA( (e), (ce), + (Int, ), + (Uint,), + (Float, ), + (Bool, ), + (Bytes, ), + (StaticString, ), // String + (Const, + upper_visitor.visit_path(*ce.p, ::HIR::Visitor::PathContext::VALUE); + ), + (ItemAddr, + upper_visitor.visit_path(*ce, ::HIR::Visitor::PathContext::VALUE); + ) + ) + } static void visit_param(Visitor& upper_visitor, ::MIR::Param& p) { TU_MATCHA( (p), (e), (LValue, H::visit_lvalue(upper_visitor, e);), (Constant, - TU_MATCHA( (e), (ce), - (Int, ), - (Uint,), - (Float, ), - (Bool, ), - (Bytes, ), - (StaticString, ), // String - (Const, - upper_visitor.visit_path(ce.p, ::HIR::Visitor::PathContext::VALUE); - ), - (ItemAddr, - upper_visitor.visit_path(ce, ::HIR::Visitor::PathContext::VALUE); - ) - ) + H::visit_constant(upper_visitor, e); ) ) } @@ -765,20 +769,7 @@ namespace { H::visit_lvalue(*this, e); ), (Constant, - TU_MATCHA( (e), (ce), - (Int, ), - (Uint,), - (Float, ), - (Bool, ), - (Bytes, ), - (StaticString, ), // String - (Const, - this->visit_path(ce.p, ::HIR::Visitor::PathContext::VALUE); - ), - (ItemAddr, - this->visit_path(ce, ::HIR::Visitor::PathContext::VALUE); - ) - ) + H::visit_constant(*this, e); ), (SizedArray, H::visit_param(*this, e.val); diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp index 6067b32f..0ccb6e6f 100644 --- a/src/hir_conv/constant_evaluation.cpp +++ b/src/hir_conv/constant_evaluation.cpp @@ -390,11 +390,11 @@ namespace HIR { TU_ARM(c, StaticString, e2) return ::HIR::Literal(e2); TU_ARM(c, Const, e2) { - auto p = ms.monomorph(state.sp, e2.p); + auto p = ms.monomorph(state.sp, *e2.p); // If there's any mention of generics in this path, then return Literal::Defer if( visit_path_tys_with(p, [&](const auto& ty)->bool { return ty.m_data.is_Generic(); }) ) { - DEBUG("Return Literal::Defer for constant " << e2.p << " which references a generic parameter"); + DEBUG("Return Literal::Defer for constant " << *e2.p << " which references a generic parameter"); return ::HIR::Literal::make_Defer({}); } MonomorphState const_ms; @@ -417,7 +417,7 @@ namespace HIR { return clone_literal( c.m_value_res ); } TU_ARM(c, ItemAddr, e2) - return ::HIR::Literal::make_BorrowPath( ms.monomorph(state.sp, e2) ); + return ::HIR::Literal::make_BorrowPath( ms.monomorph(state.sp, *e2) ); } throw ""; }; diff --git a/src/mir/cleanup.cpp b/src/mir/cleanup.cpp index fdc0f3d4..dd5332b6 100644 --- a/src/mir/cleanup.cpp +++ b/src/mir/cleanup.cpp @@ -216,7 +216,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const MIR::TypeResolve& state, con if( path == ::HIR::GenericPath() ) MIR_TODO(state, "Literal of type " << ty << " - " << lit); DEBUG("Unknown type " << ty << ", but a path was provided - Return ItemAddr " << path); - return ::MIR::Constant( mv$(path) ); + return ::MIR::Constant::make_ItemAddr( box$(path) ); ), (Tuple, MIR_ASSERT(state, lit.is_List(), "Non-list literal for Tuple - " << lit); @@ -378,7 +378,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const MIR::TypeResolve& state, con if( const auto* pp = lit.opt_BorrowPath() ) { const auto& path = *pp; - auto ptr_val = ::MIR::Constant::make_ItemAddr(path.clone()); + auto ptr_val = ::MIR::Constant::make_ItemAddr(box$(path.clone())); // TODO: Get the metadata type (for !Sized wrapper types) if( te.inner->m_data.is_Slice() ) { @@ -397,7 +397,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const MIR::TypeResolve& state, con auto vtable_path = ::HIR::Path(&ty == &tmp ? mv$(tmp) : ty.clone(), tep->m_trait.m_path.clone(), "vtable#"); - auto vtable_val = ::MIR::Param( ::MIR::Constant::make_ItemAddr(mv$(vtable_path)) ); + auto vtable_val = ::MIR::Param( ::MIR::Constant::make_ItemAddr(box$(vtable_path)) ); return ::MIR::RValue::make_MakeDst({ ::MIR::Param(mv$(ptr_val)), mv$(vtable_val) }); } @@ -458,7 +458,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const MIR::TypeResolve& state, con (Function, //MIR_TODO(state, "Const function pointer " << lit << " w/ type " << ty); MIR_ASSERT(state, lit.is_BorrowPath(), ""); - return ::MIR::Constant::make_ItemAddr( lit.as_BorrowPath().clone() ); + return ::MIR::Constant::make_ItemAddr( box$( lit.as_BorrowPath().clone() ) ); ) ) } @@ -703,7 +703,7 @@ bool MIR_Cleanup_Unsize_GetMetadata(const ::MIR::TypeResolve& state, MirMutator& MIR_ASSERT(state, state.m_resolve.type_is_sized(state.sp, src_ty), "Attempting to get vtable for unsized type - " << src_ty); ::HIR::Path vtable { src_ty.clone(), trait_path.m_path.clone(), "vtable#" }; - out_meta_val = ::MIR::Constant::make_ItemAddr(mv$(vtable)); + out_meta_val = ::MIR::Constant::make_ItemAddr(box$(vtable)); } } return true; @@ -1092,18 +1092,18 @@ void MIR_Cleanup(const StaticTraitResolve& resolve, const ::HIR::ItemPath& path, TU_IFLET( ::MIR::Constant, e, Const, ce, // 1. Find the constant ::HIR::TypeRef ty; - const auto* lit_ptr = MIR_Cleanup_GetConstant(state, ce.p, ty); + const auto* lit_ptr = MIR_Cleanup_GetConstant(state, *ce.p, ty); if( lit_ptr && !lit_ptr->is_Defer() ) { - DEBUG("Replace constant " << ce.p << " with " << *lit_ptr); - se.src = MIR_Cleanup_LiteralToRValue(state, mutator, *lit_ptr, mv$(ty), mv$(ce.p)); + DEBUG("Replace constant " << *ce.p << " with " << *lit_ptr); + se.src = MIR_Cleanup_LiteralToRValue(state, mutator, *lit_ptr, mv$(ty), mv$(*ce.p)); if( auto* p = se.src.opt_Constant() ) { MIR_Cleanup_Constant(state, mutator, *p); } } else { - DEBUG("No replacement for constant " << ce.p); + DEBUG("No replacement for constant " << *ce.p); } ) ) diff --git a/src/mir/dump.cpp b/src/mir/dump.cpp index ffce8abb..b02c1e5b 100644 --- a/src/mir/dump.cpp +++ b/src/mir/dump.cpp @@ -244,10 +244,10 @@ namespace { os << "\"" << ce << "\""; ), (Const, - os << ce.p; + os << *ce.p; ), (ItemAddr, - os << "addr " << ce; + os << "addr " << *ce; ) ) } diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp index 8d51bf55..b8549f8a 100644 --- a/src/mir/from_hir.cpp +++ b/src/mir/from_hir.cpp @@ -162,6 +162,7 @@ namespace { void destructure_from_ex(const Span& sp, const ::HIR::Pattern& pat, ::MIR::LValue lval, int allow_refutable=0) // 1 : yes, 2 : disallow binding { + TRACE_FUNCTION_F(pat << ", allow_refutable=" << allow_refutable); if( allow_refutable != 3 && pat.m_binding.is_valid() ) { if( allow_refutable == 2 ) { BUG(sp, "Binding when not expected"); @@ -425,9 +426,10 @@ namespace { // 2. Obtain pointer to element ::HIR::BorrowType bt = H::get_borrow_type(sp, e.extra_bind); ::MIR::LValue ptr_val = m_builder.lvalue_or_temp(sp, - ::HIR::TypeRef::new_pointer( bt, inner_type.clone() ), + ::HIR::TypeRef::new_borrow( bt, inner_type.clone() ), ::MIR::RValue::make_Borrow({ 0, bt, ::MIR::LValue::make_Field({ box$(lval.clone()), static_cast(e.leading.size()) }) }) ); + // TODO: Cast to raw pointer? Or keep as a borrow? // Construct fat pointer m_builder.push_stmt_assign( sp, m_builder.get_variable(sp, e.extra_bind.m_slot), ::MIR::RValue::make_MakeDst({ mv$(ptr_val), mv$(len_val) }) ); @@ -2185,7 +2187,7 @@ namespace { // TODO: Ideally, the creation of the wrapper function would happen somewhere before trans? auto tmp = m_builder.new_temporary( node.m_res_type ); - m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_ItemAddr(node.m_path.clone()) ); + m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_ItemAddr(box$(node.m_path.clone())) ); m_builder.set_result( sp, mv$(tmp) ); return ; } @@ -2196,7 +2198,7 @@ namespace { ), (Constant, auto tmp = m_builder.new_temporary( e.m_type ); - m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_Const({node.m_path.clone()}) ); + m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_Const({box$(node.m_path.clone())}) ); m_builder.set_result( node.span(), mv$(tmp) ); ), (Static, @@ -2245,13 +2247,13 @@ namespace { fcn_ty_data.m_arg_types.push_back( monomorphise_type_with(sp, arg.second, monomorph_cb) ); } auto tmp = m_builder.new_temporary( ::HIR::TypeRef( mv$(fcn_ty_data) ) ); - m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_ItemAddr(node.m_path.clone()) ); + m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_ItemAddr(box$(node.m_path.clone())) ); m_builder.set_result( sp, mv$(tmp) ); ), (StructConstructor, // TODO: Ideally, the creation of the wrapper function would happen somewhere before this? auto tmp = m_builder.new_temporary( node.m_res_type ); - m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_ItemAddr(node.m_path.clone()) ); + m_builder.push_stmt_assign( sp, tmp.clone(), ::MIR::Constant::make_ItemAddr(box$(node.m_path.clone())) ); m_builder.set_result( sp, mv$(tmp) ); ) ) @@ -2263,13 +2265,13 @@ namespace { ASSERT_BUG(sp, it != tr.m_values.end(), "Cannot find trait item for " << node.m_path); TU_MATCHA( (it->second), (e), (Constant, - m_builder.set_result( sp, ::MIR::Constant::make_Const({node.m_path.clone()}) ); + m_builder.set_result( sp, ::MIR::Constant::make_Const({box$(node.m_path.clone())}) ); ), (Static, TODO(sp, "Associated statics (non-rustc) - " << node.m_path); ), (Function, - m_builder.set_result( sp, ::MIR::Constant::make_ItemAddr(node.m_path.clone()) ); + m_builder.set_result( sp, ::MIR::Constant::make_ItemAddr(box$(node.m_path.clone())) ); ) ) ), @@ -2285,7 +2287,7 @@ namespace { { auto it = impl.m_methods.find(pe.item); if( it != impl.m_methods.end() ) { - m_builder.set_result( sp, ::MIR::Constant::make_ItemAddr(node.m_path.clone()) ); + m_builder.set_result( sp, ::MIR::Constant::make_ItemAddr(box$(node.m_path.clone())) ); return true; } } @@ -2293,7 +2295,7 @@ namespace { { auto it = impl.m_constants.find(pe.item); if( it != impl.m_constants.end() ) { - m_builder.set_result( sp, ::MIR::Constant::make_Const({node.m_path.clone()}) ); + m_builder.set_result( sp, ::MIR::Constant::make_Const({box$(node.m_path.clone())}) ); return true; } } diff --git a/src/mir/helpers.cpp b/src/mir/helpers.cpp index 38388aed..1453acb3 100644 --- a/src/mir/helpers.cpp +++ b/src/mir/helpers.cpp @@ -270,7 +270,7 @@ const ::HIR::TypeRef& MIR::TypeResolve::get_param_type(::HIR::TypeRef& tmp, cons ), (Const, MonomorphState p; - auto v = m_resolve.get_value(this->sp, e.p, p, /*signature_only=*/true); + auto v = m_resolve.get_value(this->sp, *e.p, p, /*signature_only=*/true); if( const auto* ve = v.opt_Constant() ) { const auto& ty = (*ve)->m_type; if( monomorphise_type_needed(ty) ) { @@ -282,12 +282,12 @@ const ::HIR::TypeRef& MIR::TypeResolve::get_param_type(::HIR::TypeRef& tmp, cons return ty.clone(); } else { - MIR_BUG(*this, "get_const_type - Not a constant " << e.p); + MIR_BUG(*this, "get_const_type - Not a constant " << *e.p); } ), (ItemAddr, MonomorphState p; - auto v = m_resolve.get_value(this->sp, e, p, /*signature_only=*/true); + auto v = m_resolve.get_value(this->sp, *e, p, /*signature_only=*/true); TU_MATCHA( (v), (ve), (NotFound, MIR_BUG(*this, "get_const_type - ItemAddr points to unknown value - " << c); @@ -325,7 +325,7 @@ const ::HIR::TypeRef& MIR::TypeResolve::get_param_type(::HIR::TypeRef& tmp, cons ::HIR::FunctionType ft; ft.is_unsafe = false; ft.m_abi = ABI_RUST; - auto enum_path = e.clone(); + auto enum_path = e->clone(); enum_path.m_data.as_Generic().m_path.m_components.pop_back(); ft.m_rettype = box$( ::HIR::TypeRef::new_path(mv$(enum_path), ve.e) ); ft.m_arg_types.reserve(str_data.size()); @@ -348,7 +348,7 @@ const ::HIR::TypeRef& MIR::TypeResolve::get_param_type(::HIR::TypeRef& tmp, cons ::HIR::FunctionType ft; ft.is_unsafe = false; ft.m_abi = ABI_RUST; - ft.m_rettype = box$( ::HIR::TypeRef::new_path( ::HIR::GenericPath(*ve.p, e.m_data.as_Generic().m_params.clone()), &str) ); + ft.m_rettype = box$( ::HIR::TypeRef::new_path( ::HIR::GenericPath(*ve.p, e->m_data.as_Generic().m_params.clone()), &str) ); ft.m_arg_types.reserve(str_data.size()); for(const auto& fld : str_data) ft.m_arg_types.push_back( p.monomorph(this->sp, fld.ent) ); diff --git a/src/mir/mir.cpp b/src/mir/mir.cpp index 657e695d..638b8e46 100644 --- a/src/mir/mir.cpp +++ b/src/mir/mir.cpp @@ -44,10 +44,10 @@ namespace MIR { os << "\"" << FmtEscaped(e) << "\""; ), (Const, - os << e.p; + os << *e.p; ), (ItemAddr, - os << "&" << e; + os << "&" << *e; ) ) return os; @@ -82,10 +82,10 @@ namespace MIR { return ::ord(ae, be); ), (Const, - return ::ord(ae.p, be.p); + return ::ord(*ae.p, *be.p); ), (ItemAddr, - return ::ord(ae, be); + return ::ord(*ae, *be); ) ) throw ""; @@ -571,8 +571,8 @@ namespace MIR { (Bool, return ::MIR::Constant(e2); ), (Bytes, return ::MIR::Constant(e2); ), (StaticString, return ::MIR::Constant(e2); ), - (Const, return ::MIR::Constant::make_Const({e2.p.clone()}); ), - (ItemAddr, return ::MIR::Constant(e2.clone()); ) + (Const, return ::MIR::Constant::make_Const({box$(e2.p->clone())}); ), + (ItemAddr, return ::MIR::Constant(box$(e2->clone())); ) ) throw ""; } diff --git a/src/mir/mir.hpp b/src/mir/mir.hpp index 7100d4aa..c24aac51 100644 --- a/src/mir/mir.hpp +++ b/src/mir/mir.hpp @@ -26,7 +26,21 @@ struct LValue class Storage { uintptr_t val; + static uintptr_t MAX_ARG = (1 << 30) - 1; // max value of 30 bits public: + Storage(const Storage&) = delete; + Storage& operator=(const Storage&) = delete; + Storage(Storage&& x) + :val(x.val) + { + x.val = 0; + } + Storage& operator=(Storage&& x) + { + this->~Storage(); + this->val = x.val; + x.val = 0; + } ~Storage() { if( is_Static() ) { @@ -34,32 +48,64 @@ struct LValue val = 0; } } + + static Storage new_Return() { return Storage { MAX_ARG << 2 }; } + static Storage new_Argument(unsigned idx) { assert(idx < MAX_ARG); return Storage { idx << 2 }; ) + static Storage new_Local(unsigned idx) { assert(idx <= MAX_ARG); return Storage { (idx << 2) | 1 } }; + static Storage new_Static(::HIR::Path p) { + ::HIR::Path* ptr = new ::HIR::Path(::std::move(p)); + return Storage { static_cast(ptr) | 2; } + } + + bool is_Return() const { return val == (MAX_ARG << 2) /*&& (val & 3) == 0*/; } bool is_Argument() const { return val != (MAX_ARG << 2) && (val & 3) == 0; } - bool is_Return() const { return val == (MAX_ARG << 2); } - bool is_Local() const { return (val & 3) == 1; } - bool is_Static() const { return (val & 3) == 2; } + bool is_Local() const { return (val & 3) == 1; } + bool is_Static() const { return (val & 3) == 2; } + // No as_Return + unsigned as_Argument() const { assert(is_Argument()); return val >> 2; } + unsigned as_Local() const { assert(is_Local()); return val >> 2; } const ::HIR::Path& as_Static() const { assert(is_Static()); return *reinterpret_cast(val & ~3u); } }; class Wrapper { uintptr_t val; public: - bool is_Deref() const { return (val & 3) == 0; } + static Wrapper new_Deref() { return Wrapper { 0 }; } + static Wrapepr new_Field (unsigned idx) { return Wrapper { (idx << 2) | 1 }; } + static Wrapepr new_Downcast(unsigned idx) { return Wrapper { (idx << 2) | 2 }; } + static Wrapepr new_Index (unsigned idx) { return Wrapper { (idx << 2) | 3 }; } + + bool is_Deref () const { return (val & 3) == 0; } // Stores the field index - bool is_Field() const { return (val & 3) == 1; } + bool is_Field () const { return (val & 3) == 1; } // Stores the variant index bool is_Downcast() const { return (val & 3) == 2; } // Stores a Local index - bool is_Index() const { return (val & 3) == 3; } + bool is_Index () const { return (val & 3) == 3; } + // no as_Deref() const unsigned as_Field() const { assert(is_Field()); return (val >> 2); } + const unsigned as_Downcast() const { assert(is_Downcast()); return (val >> 2); } + // TODO: Should this return a LValue? const unsigned as_Index() const { assert(is_Index()); return (val >> 2); } }; Storage m_root; ::std::vector m_wrappers; + static LValue new_Return() { return LValue { Storage::new_Return(), {} }; } + static LValue new_Argument(unsigned idx) { return LValue { Storage::new_Argument(idx), {} }; } + static LValue new_Local(unsigned idx) { return LValue { Storage::new_Local(idx), {} }; } + static LValue new_Static(::HIR::Path p) { return LValue { Storage::new_Static(::std::move(p)), {} }; } + + static LValue new_Deref(LValue lv) { lv.m_wrappers.push_back(Wrapper::new_Deref()); } + static LValue new_Field(LValue lv, unsigned idx) { lv.m_wrappers.push_back(Wrapper::new_Field(idx)); } + static LValue new_Downcast(LValue lv, unsigned idx) { lv.m_wrappers.push_back(Wrapper::new_Downcast(idx)); } + static LValue new_Index(LValue lv, unsigned local_idx) { lv.m_wrappers.push_back(Wrapper::new_Index(local_idx)); } + + LValue monomorphise(const MonomorphState& ms, unsigned local_offset=0); + //LValue monomorphise(const TransParams& ms, unsigned local_offset=0); LValue clone() const; class Ref @@ -159,11 +205,10 @@ TAGGED_UNION_EX(Constant, (), Int, ( }), (Bytes, ::std::vector< ::std::uint8_t>), // Byte string (StaticString, ::std::string), // String - // TODO: Should these ::HIR::Path structures be behind pointers? - // - HIR::Path is ~11 words long, without it MIR::Constant is 4 instead of 12 - // - MIR::Param is quite common, potential large space saving. - (Const, struct { ::HIR::Path p; }), // `const` - (ItemAddr, ::HIR::Path) // address of a value + // NOTE: These are behind pointers to save inline space (HIR::Path is ~11 + // words, compared to 4 for MIR::Constant without it) + (Const, struct { ::std::unique_ptr<::HIR::Path> p; }), // `const` + (ItemAddr, ::std::unique_ptr<::HIR::Path>) // address of a value ), (), (), ( friend ::std::ostream& operator<<(::std::ostream& os, const Constant& v); ::Ordering ord(const Constant& b) const; diff --git a/src/mir/optimise.cpp b/src/mir/optimise.cpp index 7c284546..a45b1c05 100644 --- a/src/mir/optimise.cpp +++ b/src/mir/optimise.cpp @@ -1213,10 +1213,10 @@ bool MIR_Optimise_Inlining(::MIR::TypeResolve& state, ::MIR::Function& fcn, bool (Bytes, return ::MIR::Constant(ce);), (StaticString, return ::MIR::Constant(ce);), (Const, - return ::MIR::Constant::make_Const({ this->monomorph(ce.p) }); + return ::MIR::Constant::make_Const({ box$(this->monomorph(*ce.p)) }); ), (ItemAddr, - return ::MIR::Constant::make_ItemAddr(this->monomorph(ce)); + return ::MIR::Constant::make_ItemAddr(box$(this->monomorph(*ce))); ) ) throw ""; diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index 08b4f2de..d238a523 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -5743,7 +5743,7 @@ namespace { (Const, // TODO: This should have been eliminated? ("MIR Cleanup" should have removed all inline Const references) ::HIR::TypeRef ty; - const auto& lit = get_literal_for_const(c.p, ty); + const auto& lit = get_literal_for_const(*c.p, ty); if(lit.is_Integer() || lit.is_Float()) { emit_literal(ty, lit, {}); @@ -5764,7 +5764,7 @@ namespace { } ), (ItemAddr, - TU_MATCHA( (c.m_data), (pe), + TU_MATCHA( (c->m_data), (pe), (Generic, if( pe.m_path.m_components.size() > 1 && m_crate.get_typeitem_by_path(sp, pe.m_path, false, true).is_Enum() ) ; @@ -5781,7 +5781,7 @@ namespace { } ), (UfcsUnknown, - MIR_BUG(*m_mir_res, "UfcsUnknown in trans " << c); + MIR_BUG(*m_mir_res, "UfcsUnknown in trans " << *c); ), (UfcsInherent, // TODO: If the target is a function, don't emit the & @@ -5792,7 +5792,7 @@ namespace { m_of << "&"; ) ) - m_of << Trans_Mangle(c); + m_of << Trans_Mangle(*c); ) ) } diff --git a/src/trans/codegen_mmir.cpp b/src/trans/codegen_mmir.cpp index 4867cf6f..51b8aac3 100644 --- a/src/trans/codegen_mmir.cpp +++ b/src/trans/codegen_mmir.cpp @@ -104,7 +104,7 @@ namespace os << " " << v.t; } break; TU_ARM(e, ItemAddr, v) { - os << "ADDROF " << v; + os << "ADDROF " << *v; } break; default: os << e; diff --git a/src/trans/enumerate.cpp b/src/trans/enumerate.cpp index 00b938ff..934f40a2 100644 --- a/src/trans/enumerate.cpp +++ b/src/trans/enumerate.cpp @@ -1541,10 +1541,10 @@ void Trans_Enumerate_FillFrom_MIR_Constant(EnumState& state, const ::MIR::Consta (StaticString, ), // String (Const, // - Check if this constant has a value of Defer - Trans_Enumerate_FillFrom_Path(state, ce.p, pp); + Trans_Enumerate_FillFrom_Path(state, *ce.p, pp); ), (ItemAddr, - Trans_Enumerate_FillFrom_Path(state, ce, pp); + Trans_Enumerate_FillFrom_Path(state, *ce, pp); ) ) } diff --git a/src/trans/monomorphise.cpp b/src/trans/monomorphise.cpp index cf101443..50924e2f 100644 --- a/src/trans/monomorphise.cpp +++ b/src/trans/monomorphise.cpp @@ -71,15 +71,15 @@ namespace { ), (Const, return ::MIR::Constant::make_Const({ - params.monomorph(resolve, ce.p) + box$(params.monomorph(resolve, *ce.p)) }); ), (ItemAddr, - auto p = params.monomorph(resolve, ce); + auto p = params.monomorph(resolve, *ce); // TODO: If this is a pointer to a function on a trait object, replace with the address loaded from the vtable. // - Requires creating a new temporary for the vtable pointer. // - Also requires knowing what the receiver is. - return ::MIR::Constant( mv$(p) ); + return ::MIR::Constant( box$(p) ); ) ) throw ""; -- cgit v1.2.3