diff options
-rw-r--r-- | src/hir/deserialise.cpp | 10 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 25 | ||||
-rw-r--r-- | src/hir/hir.hpp | 22 | ||||
-rw-r--r-- | src/hir/serialise.cpp | 10 | ||||
-rw-r--r-- | src/hir_conv/constant_evaluation.cpp | 16 | ||||
-rw-r--r-- | src/hir_expand/closures.cpp | 3 | ||||
-rw-r--r-- | src/hir_expand/const_eval_full.cpp | 15 | ||||
-rw-r--r-- | src/hir_expand/vtable.cpp | 3 | ||||
-rw-r--r-- | src/mir/cleanup.cpp | 2 |
9 files changed, 78 insertions, 28 deletions
diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index d986e079..732e8156 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -480,12 +480,21 @@ namespace { } } + ::HIR::Linkage deserialise_linkage() + { + return ::HIR::Linkage { + ::HIR::Linkage::Type::Auto, + m_in.read_string(), + }; + } + // - Value items ::HIR::Function deserialise_function() { TRACE_FUNCTION; ::HIR::Function rv { + deserialise_linkage(), static_cast< ::HIR::Function::Receiver>( m_in.read_tag() ), m_in.read_string(), m_in.read_bool(), @@ -524,6 +533,7 @@ namespace { TRACE_FUNCTION; return ::HIR::Static { + deserialise_linkage(), m_in.read_bool(), deserialise_type(), ::HIR::ExprPtr {}, diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 50060ccd..729b0f45 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -17,7 +17,7 @@ #include <hir/item_path.hpp> ::HIR::Module LowerHIR_Module(const ::AST::Module& module, ::HIR::ItemPath path, ::std::vector< ::HIR::SimplePath> traits = {}); -::HIR::Function LowerHIR_Function(::HIR::ItemPath path, const ::AST::Function& f, const ::HIR::TypeRef& self_type); +::HIR::Function LowerHIR_Function(::HIR::ItemPath path, const ::AST::MetaItems& attrs, const ::AST::Function& f, const ::HIR::TypeRef& self_type); ::HIR::PathParams LowerHIR_PathParams(const Span& sp, const ::AST::PathParams& src_params, bool allow_assoc); ::HIR::TraitPath LowerHIR_TraitPath(const Span& sp, const ::AST::Path& path); @@ -982,7 +982,7 @@ namespace { ), (Function, ::HIR::TypeRef self_type {"Self", 0xFFFF}; - rv.m_values.insert( ::std::make_pair(item.name, ::HIR::TraitValueItem::make_Function( LowerHIR_Function(item_path, i, self_type) )) ); + rv.m_values.insert( ::std::make_pair(item.name, ::HIR::TraitValueItem::make_Function( LowerHIR_Function(item_path, item.data.attrs, i, self_type) )) ); ), (Static, if( i.s_class() == ::AST::Static::CONST ) @@ -992,7 +992,9 @@ namespace { LowerHIR_Expr( i.value() ) })) ); else { + ::HIR::Linkage linkage; rv.m_values.insert( ::std::make_pair(item.name, ::HIR::TraitValueItem::make_Static(::HIR::Static { + mv$(linkage), (i.s_class() == ::AST::Static::MUT), LowerHIR_Type( i.type() ), LowerHIR_Expr( i.value() ) @@ -1002,14 +1004,11 @@ namespace { ) } - // TODO: If this trait is object safe, build up the vtable (and vtable type) - // - Or do it in a pass? - rv.m_is_marker = f.is_marker(); return rv; } -::HIR::Function LowerHIR_Function(::HIR::ItemPath p, const ::AST::Function& f, const ::HIR::TypeRef& self_type) +::HIR::Function LowerHIR_Function(::HIR::ItemPath p, const ::AST::MetaItems& attrs, const ::AST::Function& f, const ::HIR::TypeRef& self_type) { static Span sp; @@ -1059,8 +1058,10 @@ namespace { } } - // TODO: ABI and unsafety/constness + ::HIR::Linkage linkage; + return ::HIR::Function { + mv$(linkage), receiver, f.abi(), f.is_unsafe(), f.is_const(), LowerHIR_GenericParams(f.params(), nullptr), // TODO: If this is a method, then it can add the Self: Sized bound @@ -1164,7 +1165,7 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H _add_mod_ns_item( mod, item.name, 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, e, ::HIR::TypeRef{})); + _add_mod_val_item(mod, item.name, item.is_pub, LowerHIR_Function(item_path, item.data.attrs, e, ::HIR::TypeRef{})); ), (Static, if( e.s_class() == ::AST::Static::CONST ) @@ -1174,7 +1175,9 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H LowerHIR_Expr( e.value() ) })); else { + ::HIR::Linkage linkage; _add_mod_val_item(mod, item.name, item.is_pub, ::HIR::ValueItem::make_Static(::HIR::Static { + mv$(linkage), (e.s_class() == ::AST::Static::MUT), LowerHIR_Type( e.type() ), LowerHIR_Expr( e.value() ) @@ -1306,7 +1309,7 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat ), (Function, DEBUG("- method " << item.name); - methods.insert( ::std::make_pair(item.name, ::HIR::TraitImpl::ImplEnt< ::HIR::Function> { item.is_specialisable, LowerHIR_Function(item_path, e, type) }) ); + methods.insert( ::std::make_pair(item.name, ::HIR::TraitImpl::ImplEnt< ::HIR::Function> { item.is_specialisable, LowerHIR_Function(item_path, item.data->attrs, e, type) }) ); ) ) } @@ -1374,7 +1377,9 @@ 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, e, type) } ) ); + 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) + } ) ); ) ) } diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index efd1f10f..49d03fbd 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -68,15 +68,33 @@ extern bool operator==(const Literal& l, const Literal& r); static inline bool operator!=(const Literal& l, const Literal& r) { return !(l == r); } // -------------------------------------------------------------------- -// Type structures +// Value structures // -------------------------------------------------------------------- +struct Linkage +{ + enum class Type + { + Auto, // Default + Weak, // Weak linkage (multiple definitions are allowed + External, // Force the symbol to be externally visible + }; + + // Linkage type + Type type = Type::Auto; + + // External symbol name + ::std::string name; +}; + class Static { public: + Linkage m_linkage; bool m_is_mut; TypeRef m_type; ExprPtr m_value; + Literal m_value_res; }; struct Constant @@ -104,6 +122,8 @@ public: typedef ::std::vector< ::std::pair< ::HIR::Pattern, ::HIR::TypeRef> > args_t; + Linkage m_linkage; + Receiver m_receiver; ::std::string m_abi; bool m_unsafe; diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp index fd2f2749..5f8749b5 100644 --- a/src/hir/serialise.cpp +++ b/src/hir/serialise.cpp @@ -729,11 +729,19 @@ namespace { void serialise(unsigned int v) { m_out.write_count(v); }; + void serialise(const ::HIR::Linkage& linkage) + { + //m_out.write_tag( static_cast<int>(linkage.type) ); + m_out.write_string( linkage.name ); + } + // - Value items void serialise(const ::HIR::Function& fcn) { TRACE_FUNCTION_F("_function:"); + serialise(fcn.m_linkage); + m_out.write_tag( static_cast<int>(fcn.m_receiver) ); m_out.write_string(fcn.m_abi); m_out.write_bool(fcn.m_unsafe); @@ -762,6 +770,8 @@ namespace { { TRACE_FUNCTION_F("_static:"); + serialise(item.m_linkage); + m_out.write_bool(item.m_is_mut); serialise(item.m_type); diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp index 64695f84..38444b82 100644 --- a/src/hir_conv/constant_evaluation.cpp +++ b/src/hir_conv/constant_evaluation.cpp @@ -37,6 +37,7 @@ namespace { next_item_idx ++; auto rv = (mod_path + name.c_str()).get_simple_path(); newval_output.push_back( ::std::make_pair( mv$(name), ::HIR::Static { + ::HIR::Linkage {}, false, mv$(type), ::HIR::ExprPtr(), @@ -477,23 +478,16 @@ namespace { ERROR(node.span(), E0000, "Only shared borrows are allowed in constants"); } - // Create new static containing borrowed data - auto name = FMT(m_newval_state.name_prefix << &node); - if( visit_ty_with(m_rv_type, [&](const auto& x){ return x.m_data.is_Infer(); }) ) { ERROR(node.span(), E0000, "Could not trivially infer type of referenced static - " << m_rv_type << ", lit = " << val); } - m_newval_state.newval_output.push_back(::std::make_pair( name, ::HIR::Static { - false, - //m_rv_type.clone(), - {}, - ::HIR::ExprNodeP(), - mv$(val) - } )); + // Create new static containing borrowed data + //auto path = m_newval_state.new_static( m_rv_type.clone(), mv$(val) ); + auto path = m_newval_state.new_static( ::HIR::TypeRef(), mv$(val) ); m_rv_type = ::HIR::TypeRef::new_borrow( node.m_type, mv$(m_rv_type) ); - m_rv = ::HIR::Literal::make_BorrowOf( (m_newval_state.mod_path + name).get_simple_path() ); + m_rv = ::HIR::Literal::make_BorrowOf( mv$(path) ); } void visit(::HIR::ExprNode_Cast& node) override { TRACE_FUNCTION_F("_Cast"); diff --git a/src/hir_expand/closures.cpp b/src/hir_expand/closures.cpp index 4ffc88ac..2f593a1d 100644 --- a/src/hir_expand/closures.cpp +++ b/src/hir_expand/closures.cpp @@ -294,6 +294,7 @@ namespace { mv$(params), mv$(trait_params), mv$(closure_type), make_map1( ::std::string("call_once"), ::HIR::TraitImpl::ImplEnt< ::HIR::Function> { false, ::HIR::Function { + ::HIR::Linkage {}, ::HIR::Function::Receiver::Value, ABI_RUST, false, false, {}, @@ -331,6 +332,7 @@ namespace { mv$(params), mv$(trait_params), mv$(closure_type), make_map1( ::std::string("call_mut"), ::HIR::TraitImpl::ImplEnt< ::HIR::Function> { false, ::HIR::Function { + ::HIR::Linkage {}, ::HIR::Function::Receiver::BorrowUnique, ABI_RUST, false, false, {}, @@ -366,6 +368,7 @@ namespace { mv$(params), mv$(trait_params), mv$(closure_type), make_map1( ::std::string("call"), ::HIR::TraitImpl::ImplEnt< ::HIR::Function> { false, ::HIR::Function { + ::HIR::Linkage {}, ::HIR::Function::Receiver::BorrowShared, ABI_RUST, false, false, {}, diff --git a/src/hir_expand/const_eval_full.cpp b/src/hir_expand/const_eval_full.cpp index 3246fc90..3f7e7a81 100644 --- a/src/hir_expand/const_eval_full.cpp +++ b/src/hir_expand/const_eval_full.cpp @@ -37,6 +37,7 @@ namespace { next_item_idx ++; auto rv = (mod_path + name.c_str()).get_simple_path(); newval_output.push_back( ::std::make_pair( mv$(name), ::HIR::Static { + ::HIR::Linkage(), false, mv$(type), ::HIR::ExprPtr(), @@ -819,10 +820,16 @@ namespace { for( auto& item : m_new_values ) { - mod.m_value_items.insert( ::std::make_pair( - mv$(item.first), - box$(::HIR::VisEnt<::HIR::ValueItem> { false, ::HIR::ValueItem(mv$(item.second)) }) - ) ); + auto boxed_ent = box$(::HIR::VisEnt<::HIR::ValueItem> { false, ::HIR::ValueItem(mv$(item.second)) }); + auto it = mod.m_value_items.find( item.first ); + if( it != mod.m_value_items.end() ) + { + it->second = mv$(boxed_ent); + } + else + { + mod.m_value_items.insert( ::std::make_pair( mv$(item.first), mv$(boxed_ent) ) ); + } } m_new_values = mv$(saved); m_mod_path = saved_mp; diff --git a/src/hir_expand/vtable.cpp b/src/hir_expand/vtable.cpp index baf5a016..c36704ed 100644 --- a/src/hir_expand/vtable.cpp +++ b/src/hir_expand/vtable.cpp @@ -220,7 +220,7 @@ namespace { tr.m_values.insert( ::std::make_pair( "#vtable", - ::HIR::TraitValueItem(::HIR::Static { false, ::HIR::TypeRef( mv$(path) ), {},{} }) + ::HIR::TraitValueItem(::HIR::Static { ::HIR::Linkage(), false, ::HIR::TypeRef( mv$(path) ), {},{} }) ) ); } @@ -259,6 +259,7 @@ namespace { const auto& vtable_ref = m_crate.get_struct_by_path(sp, vtable_sp); impl.m_statics.insert(::std::make_pair( "#vtable", ::HIR::TraitImpl::ImplEnt<::HIR::Static> { true, ::HIR::Static { + ::HIR::Linkage(), false, ::HIR::TypeRef::new_path(::HIR::GenericPath(mv$(vtable_sp), mv$(vtable_params)), &vtable_ref), {}, diff --git a/src/mir/cleanup.cpp b/src/mir/cleanup.cpp index f8f151ce..ddaaa8da 100644 --- a/src/mir/cleanup.cpp +++ b/src/mir/cleanup.cpp @@ -239,7 +239,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR { ::HIR::TypeRef tmp; const auto& ty = state.get_static_type(tmp, path); - MIR_ASSERT(state, ty.m_data.is_Array(), "BorrowOf returning slice not of an array"); + MIR_ASSERT(state, ty.m_data.is_Array(), "BorrowOf returning slice not of an array, instead " << ty); unsigned int size = ty.m_data.as_Array().size_val; auto ptr_type = ::HIR::TypeRef::new_borrow(::HIR::BorrowType::Shared, |