summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Hodge <tpg@ucc.asn.au>2017-09-10 21:02:07 +0800
committerJohn Hodge <tpg@ucc.asn.au>2017-09-10 21:02:07 +0800
commit9c31f71380cee5d7e6a67d2f7f805619366e2e64 (patch)
treece0a884071e5203010a9a4ddafdc7cd021ba4c0a /src
parent22ce602788b285b303854a095a0c340274a76b06 (diff)
downloadmrust-9c31f71380cee5d7e6a67d2f7f805619366e2e64.tar.gz
Consteval - Remove BorrowOf in favor of BorrowPath/BorrowData
Diffstat (limited to 'src')
-rw-r--r--src/hir/deserialise.cpp3
-rw-r--r--src/hir/hir.cpp10
-rw-r--r--src/hir/hir.hpp5
-rw-r--r--src/hir/serialise.cpp5
-rw-r--r--src/hir_conv/bind.cpp5
-rw-r--r--src/hir_conv/constant_evaluation.cpp32
-rw-r--r--src/hir_expand/const_eval_full.cpp29
-rw-r--r--src/mir/cleanup.cpp44
-rw-r--r--src/trans/codegen_c.cpp10
-rw-r--r--src/trans/enumerate.cpp5
10 files changed, 98 insertions, 50 deletions
diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp
index 5bc83395..1eccfcac 100644
--- a/src/hir/deserialise.cpp
+++ b/src/hir/deserialise.cpp
@@ -974,7 +974,8 @@ namespace {
})
_(Integer, m_in.read_u64() )
_(Float, m_in.read_double() )
- _(BorrowOf, deserialise_path() )
+ _(BorrowPath, deserialise_path() )
+ _(BorrowData, box$(deserialise_literal()) )
_(String, m_in.read_string() )
#undef _
default:
diff --git a/src/hir/hir.cpp b/src/hir/hir.cpp
index b913c4e3..ed6230d5 100644
--- a/src/hir/hir.cpp
+++ b/src/hir/hir.cpp
@@ -36,9 +36,12 @@ namespace HIR {
(Float,
os << e;
),
- (BorrowOf,
+ (BorrowPath,
os << "&" << e;
),
+ (BorrowData,
+ os << "&" << *e;
+ ),
(String,
os << "\"" << e << "\"";
)
@@ -75,9 +78,12 @@ namespace HIR {
(Float,
return le == re;
),
- (BorrowOf,
+ (BorrowPath,
return le == re;
),
+ (BorrowData,
+ return *le == *re;
+ ),
(String,
return le == re;
)
diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp
index a1cc7c16..fc928348 100644
--- a/src/hir/hir.hpp
+++ b/src/hir/hir.hpp
@@ -59,7 +59,10 @@ TAGGED_UNION(Literal, Invalid,
// Literal values
(Integer, uint64_t),
(Float, double),
- (BorrowOf, ::HIR::Path),
+ // Borrow of a path (existing item)
+ (BorrowPath, ::HIR::Path),
+ // Borrow of inline data
+ (BorrowData, ::std::unique_ptr<Literal>),
// String = &'static str or &[u8; N]
(String, ::std::string)
);
diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp
index f50aa5c1..f5c07c35 100644
--- a/src/hir/serialise.cpp
+++ b/src/hir/serialise.cpp
@@ -440,9 +440,12 @@ namespace {
(Float,
m_out.write_double(e);
),
- (BorrowOf,
+ (BorrowPath,
serialise_path(e);
),
+ (BorrowData,
+ serialise(*e);
+ ),
(String,
m_out.write_string(e);
)
diff --git a/src/hir_conv/bind.cpp b/src/hir_conv/bind.cpp
index 364b3a76..553d3b27 100644
--- a/src/hir_conv/bind.cpp
+++ b/src/hir_conv/bind.cpp
@@ -127,9 +127,12 @@ namespace {
),
(Float,
),
- (BorrowOf,
+ (BorrowPath,
visit_path(e, ::HIR::Visitor::PathContext::VALUE);
),
+ (BorrowData,
+ visit_literal(sp, *e);
+ ),
(String,
)
)
diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp
index 4686139d..0b54331c 100644
--- a/src/hir_conv/constant_evaluation.cpp
+++ b/src/hir_conv/constant_evaluation.cpp
@@ -75,9 +75,12 @@ namespace {
(Float,
return ::HIR::Literal(e);
),
- (BorrowOf,
+ (BorrowPath,
return ::HIR::Literal(e.clone());
),
+ (BorrowData,
+ return ::HIR::Literal(box$( clone_literal(*e) ));
+ ),
(String,
return ::HIR::Literal(e);
)
@@ -440,11 +443,6 @@ namespace {
}
void visit(::HIR::ExprNode_Borrow& node) override {
- //m_rv_type = ::HIR::TypeRef();
- //m_rv = ::HIR::Literal::make_BorrowOf( ::HIR::SimplePath() );
- //return ;
- //badnode(node);
-
TU_MATCH_DEF( ::HIR::TypeRef::Data, (m_exp_type.m_data), (te),
(
//ERROR(node.span(), E0000, "Invalid expected type for a &-ptr - " << m_exp_type);
@@ -468,12 +466,8 @@ namespace {
ERROR(node.span(), E0000, "Could not trivially infer type of referenced static - " << m_rv_type << ", lit = " << 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( mv$(path) );
+ m_rv = ::HIR::Literal::make_BorrowData( box$(val) );
}
void visit(::HIR::ExprNode_Cast& node) override {
TRACE_FUNCTION_F("_Cast");
@@ -1069,7 +1063,7 @@ namespace {
(Invalid,
BUG(sp, "Read of invalid lvalue - " << lv);
),
- (BorrowOf,
+ (BorrowPath,
return ::HIR::Literal(e.clone());
),
(Integer,
@@ -1106,7 +1100,7 @@ namespace {
return clone_literal( ent.as_Constant()->m_value_res );
),
(ItemAddr,
- return ::HIR::Literal::make_BorrowOf( e2.clone() );
+ return ::HIR::Literal::make_BorrowPath( e2.clone() );
)
)
throw "";
@@ -1165,14 +1159,8 @@ namespace {
auto inner_val = read_lval(e.val);
- ::HIR::TypeRef inner_ty;
- const auto& inner_ty_r = state.get_lvalue_type(inner_ty, e.val);
- if( &inner_ty_r != &inner_ty )
- inner_ty = inner_ty_r.clone();
-
// Create new static containing borrowed data
- auto item_path = newval_state.new_static( mv$(inner_ty), mv$(inner_val) );
- val = ::HIR::Literal::make_BorrowOf( mv$(item_path) );
+ val = ::HIR::Literal::make_BorrowData( box$(inner_val) );
),
(Cast,
auto inval = read_lval(e.val);
@@ -1237,9 +1225,9 @@ namespace {
TU_IFLET( ::HIR::Literal, inval, Integer, i,
val = ::HIR::Literal(i);
)
- else TU_IFLET( ::HIR::Literal, inval, BorrowOf, i,
+ else if( inval.is_BorrowPath() || inval.is_BorrowData() ) {
val = mv$(inval);
- )
+ }
else {
BUG(sp, "Invalid cast of " << inval.tag_str() << " to " << e.type);
}
diff --git a/src/hir_expand/const_eval_full.cpp b/src/hir_expand/const_eval_full.cpp
index 39bbce02..b3f5bf1e 100644
--- a/src/hir_expand/const_eval_full.cpp
+++ b/src/hir_expand/const_eval_full.cpp
@@ -78,9 +78,12 @@ namespace {
(Float,
return ::HIR::Literal(e);
),
- (BorrowOf,
+ (BorrowPath,
return ::HIR::Literal(e.clone());
),
+ (BorrowData,
+ return ::HIR::Literal(box$(clone_literal(*e)));
+ ),
(String,
return ::HIR::Literal(e);
)
@@ -107,11 +110,14 @@ namespace {
),
(Float,
),
- (BorrowOf,
+ (BorrowPath,
DEBUG(e);
e = ms.monomorph(sp, e);
// TODO: expand associated types
),
+ (BorrowData,
+ monomorph_literal_inplace(sp, *e, ms);
+ ),
(String,
)
)
@@ -366,7 +372,7 @@ namespace {
(Invalid,
BUG(sp, "Read of lvalue with Literal::Invalid - " << lv);
),
- (BorrowOf,
+ (BorrowPath,
return ::HIR::Literal(e.clone());
),
(Integer,
@@ -411,7 +417,7 @@ namespace {
return val;
),
(ItemAddr,
- return ::HIR::Literal::make_BorrowOf( ms.monomorph(sp, e2) );
+ return ::HIR::Literal::make_BorrowPath( ms.monomorph(sp, e2) );
)
)
throw "";
@@ -474,8 +480,8 @@ namespace {
val = read_lval(*p->val);
}
else if( const auto* p = e.val.opt_Static() ) {
- // Borrow of a static, emit BorrowOf with the same path
- val = ::HIR::Literal::make_BorrowOf( p->clone() );
+ // Borrow of a static, emit BorrowPath with the same path
+ val = ::HIR::Literal::make_BorrowPath( p->clone() );
}
else {
auto inner_val = read_lval(e.val);
@@ -486,8 +492,9 @@ namespace {
inner_ty = inner_ty_r.clone();
// Create new static containing borrowed data
+ // NOTE: Doesn't use BorrowData
auto item_path = newval_state.new_static( mv$(inner_ty), mv$(inner_val) );
- val = ::HIR::Literal::make_BorrowOf( mv$(item_path) );
+ val = ::HIR::Literal::make_BorrowPath( mv$(item_path) );
}
),
(Cast,
@@ -554,17 +561,17 @@ namespace {
TU_IFLET( ::HIR::Literal, inval, Integer, i,
val = ::HIR::Literal(i);
)
- else TU_IFLET( ::HIR::Literal, inval, BorrowOf, i,
+ else if( inval.is_BorrowData() || inval.is_BorrowPath() ) {
val = mv$(inval);
- )
+ }
else {
BUG(sp, "Invalid cast of " << inval.tag_str() << " to " << e.type);
}
),
(Borrow,
- TU_IFLET( ::HIR::Literal, inval, BorrowOf, i,
+ if( inval.is_BorrowData() || inval.is_BorrowPath() ) {
val = mv$(inval);
- )
+ }
else {
BUG(sp, "Invalid cast of " << inval.tag_str() << " to " << e.type);
}
diff --git a/src/mir/cleanup.cpp b/src/mir/cleanup.cpp
index 84052efc..9718f046 100644
--- a/src/mir/cleanup.cpp
+++ b/src/mir/cleanup.cpp
@@ -360,7 +360,7 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR
throw "";
),
(Pointer,
- if( lit.is_BorrowOf() ) {
+ if( lit.is_BorrowPath() || lit.is_BorrowData() ) {
// TODO:
MIR_TODO(state, "BorrowOf into pointer - " << lit << " into " << ty);
}
@@ -370,9 +370,10 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR
}
),
(Borrow,
- if( lit.is_BorrowOf() )
+ if( const auto* pp = lit.opt_BorrowPath() )
{
- const auto& path = lit.as_BorrowOf();
+ const auto& path = *pp;
+ auto ptr_val = ::MIR::Constant::make_ItemAddr(path.clone());
// TODO: Get the metadata type (for !Sized wrapper types)
if( te.inner->m_data.is_Slice() )
{
@@ -381,9 +382,8 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR
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_val = ::MIR::Param( ::MIR::Constant::make_ItemAddr(path.clone()) );
auto size_val = ::MIR::Param( ::MIR::Constant::make_Uint({ size, ::HIR::CoreType::Usize }) );
- return ::MIR::RValue::make_MakeDst({ mv$(ptr_val), mv$(size_val) });
+ return ::MIR::RValue::make_MakeDst({ ::MIR::Param(mv$(ptr_val)), mv$(size_val) });
}
else if( const auto* tep = te.inner->m_data.opt_TraitObject() )
{
@@ -392,14 +392,42 @@ const ::HIR::Literal* MIR_Cleanup_GetConstant(const Span& sp, const StaticTraitR
auto vtable_path = ::HIR::Path(&ty == &tmp ? mv$(tmp) : ty.clone(), tep->m_trait.m_path.clone(), "#vtable");
- auto ptr_val = ::MIR::Param( ::MIR::Constant::make_ItemAddr(path.clone()) );
auto vtable_val = ::MIR::Param( ::MIR::Constant::make_ItemAddr(mv$(vtable_path)) );
- return ::MIR::RValue::make_MakeDst({ mv$(ptr_val), mv$(vtable_val) });
+ return ::MIR::RValue::make_MakeDst({ ::MIR::Param(mv$(ptr_val)), mv$(vtable_val) });
}
else
{
- return ::MIR::Constant::make_ItemAddr( path.clone() );
+ return mv$(ptr_val);
+ }
+ }
+ else if( const auto* e = lit.opt_BorrowData() ) {
+ const auto& inner_lit = **e;
+ // 1. Make a new lvalue for the inner data
+ // 2. Borrow that slot
+ if( const auto* tie = te.inner->m_data.opt_Slice() )
+ {
+ MIR_ASSERT(state, inner_lit.is_List(), "BorrowData of non-list resulting in &[T]");
+ auto size = inner_lit.as_List().size();
+ auto inner_ty = ::HIR::TypeRef::new_array(tie->inner->clone(), size);
+ auto size_val = ::MIR::Param( ::MIR::Constant::make_Uint({ size, ::HIR::CoreType::Usize }) );
+ auto ptr_ty = ::HIR::TypeRef::new_borrow(te.type, inner_ty.clone());
+
+ auto rval = MIR_Cleanup_LiteralToRValue(state, mutator, inner_lit, inner_ty.clone(), ::HIR::GenericPath());
+
+ auto lval = mutator.in_temporary( mv$(inner_ty), mv$(rval) );
+ auto ptr_val = mutator.in_temporary( mv$(ptr_ty), ::MIR::RValue::make_Borrow({ 0, te.type, mv$(lval) }));
+ return ::MIR::RValue::make_MakeDst({ ::MIR::Param(mv$(ptr_val)), mv$(size_val) });
+ }
+ else if( te.inner->m_data.is_TraitObject() )
+ {
+ MIR_BUG(state, "BorrowData returning TraitObject shouldn't be allowed - " << ty << " from " << inner_lit);
+ }
+ else
+ {
+ auto rval = MIR_Cleanup_LiteralToRValue(state, mutator, inner_lit, te.inner->clone(), ::HIR::GenericPath());
+ auto lval = mutator.in_temporary( te.inner->clone(), mv$(rval) );
+ return ::MIR::RValue::make_Borrow({ 0, te.type, mv$(lval) });
}
}
else if( te.inner->m_data.is_Slice() && *te.inner->m_data.as_Slice().inner == ::HIR::CoreType::U8 ) {
diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp
index 416aa1f9..e8643591 100644
--- a/src/trans/codegen_c.cpp
+++ b/src/trans/codegen_c.cpp
@@ -1544,7 +1544,7 @@ namespace {
(Float,
this->emit_float(e);
),
- (BorrowOf,
+ (BorrowPath,
TU_MATCHA( (e.m_data), (pe),
(Generic,
const auto& vi = m_crate.get_valitem_by_path(sp, pe.m_path);
@@ -1595,6 +1595,9 @@ namespace {
)
m_of << Trans_Mangle( params.monomorph(m_resolve, e));
),
+ (BorrowData,
+ MIR_TODO(*m_mir_res, "Handle BorrowData - " << *e);
+ ),
(String,
m_of << "{ ";
m_of << "\"" << ::std::hex;
@@ -4057,7 +4060,7 @@ namespace {
emit_dst(); m_of << " = ";
emit_literal(ty, lit, {});
),
- (BorrowOf,
+ (BorrowPath,
if( ty.m_data.is_Function() )
{
emit_dst(); m_of << " = " << Trans_Mangle(e);
@@ -4092,6 +4095,9 @@ namespace {
emit_dst(); m_of << " = &" << Trans_Mangle(e);
}
),
+ (BorrowData,
+ MIR_TODO(*m_mir_res, "Handle BorrowData - " << *e);
+ ),
(String,
emit_dst(); m_of << ".PTR = ";
m_of << "\"" << ::std::oct;
diff --git a/src/trans/enumerate.cpp b/src/trans/enumerate.cpp
index b62b21e8..54bf4ed7 100644
--- a/src/trans/enumerate.cpp
+++ b/src/trans/enumerate.cpp
@@ -1559,9 +1559,12 @@ void Trans_Enumerate_FillFrom_Literal(EnumState& state, const ::HIR::Literal& li
),
(Float,
),
- (BorrowOf,
+ (BorrowPath,
Trans_Enumerate_FillFrom_Path(state, e, pp);
),
+ (BorrowData,
+ Trans_Enumerate_FillFrom_Literal(state, *e, pp);
+ ),
(String,
)
)