Skip to content

Commit 56f3990

Browse files
committed
Generalized base::unsize_thin_ptr
1 parent 9a8dfd7 commit 56f3990

File tree

7 files changed

+65
-43
lines changed

7 files changed

+65
-43
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn unsized_info<'a, 'll: 'a, 'tcx: 'll, Cx: 'a + CodegenMethods<'ll, 'tcx>>(
208208
let vtable_ptr = cx.layout_of(cx.tcx().mk_mut_ptr(target))
209209
.field(cx, abi::FAT_PTR_EXTRA);
210210
cx.static_ptrcast(meth::get_vtable(cx, source, data.principal()),
211-
cx.backend_type(vtable_ptr))
211+
cx.backend_type(&vtable_ptr))
212212
}
213213
_ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
214214
source,
@@ -217,12 +217,14 @@ pub fn unsized_info<'a, 'll: 'a, 'tcx: 'll, Cx: 'a + CodegenMethods<'ll, 'tcx>>(
217217
}
218218

219219
/// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
220-
pub fn unsize_thin_ptr(
221-
bx: &Builder<'a, 'll, 'tcx, &'ll Value>,
222-
src: &'ll Value,
220+
pub fn unsize_thin_ptr<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>(
221+
bx: &Bx,
222+
src: <Bx::CodegenCx as Backend>::Value,
223223
src_ty: Ty<'tcx>,
224224
dst_ty: Ty<'tcx>
225-
) -> (&'ll Value, &'ll Value) {
225+
) -> (<Bx::CodegenCx as Backend>::Value, <Bx::CodegenCx as Backend>::Value) where
226+
&'a Bx::CodegenCx: LayoutOf<Ty = Ty<'tcx>, TyLayout = TyLayout<'tcx>> + HasTyCtxt<'tcx>
227+
{
226228
debug!("unsize_thin_ptr: {:?} => {:?}", src_ty, dst_ty);
227229
match (&src_ty.sty, &dst_ty.sty) {
228230
(&ty::Ref(_, a, _),
@@ -232,13 +234,13 @@ pub fn unsize_thin_ptr(
232234
(&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
233235
&ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
234236
assert!(bx.cx().type_is_sized(a));
235-
let ptr_ty = bx.cx().type_ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
237+
let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(&bx.cx().layout_of(b)));
236238
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
237239
}
238240
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
239241
let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
240242
assert!(bx.cx().type_is_sized(a));
241-
let ptr_ty = bx.cx().type_ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
243+
let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(&bx.cx().layout_of(b)));
242244
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
243245
}
244246
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
@@ -263,8 +265,8 @@ pub fn unsize_thin_ptr(
263265
}
264266
let (lldata, llextra) = result.unwrap();
265267
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
266-
(bx.bitcast(lldata, dst_layout.scalar_pair_element_llvm_type(bx.cx(), 0, true)),
267-
bx.bitcast(llextra, dst_layout.scalar_pair_element_llvm_type(bx.cx(), 1, true)))
268+
(bx.bitcast(lldata, bx.cx().scalar_pair_element_backend_type(&dst_layout, 0, true)),
269+
bx.bitcast(llextra, bx.cx().scalar_pair_element_backend_type(&dst_layout, 1, true)))
268270
}
269271
_ => bug!("unsize_thin_ptr: called on bad types"),
270272
}

src/librustc_codegen_llvm/context.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use attributes;
12-
use common;
1312
use llvm;
1413
use rustc::dep_graph::DepGraphSafe;
1514
use rustc::hir;
@@ -743,31 +742,6 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx, &'b Value> {
743742
llfn
744743
}
745744

746-
pub fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
747-
common::type_needs_drop(self.tcx, ty)
748-
}
749-
750-
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
751-
common::type_is_sized(self.tcx, ty)
752-
}
753-
754-
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
755-
common::type_is_freeze(self.tcx, ty)
756-
}
757-
758-
pub fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
759-
use syntax_pos::DUMMY_SP;
760-
if ty.is_sized(self.tcx.at(DUMMY_SP), ty::ParamEnv::reveal_all()) {
761-
return false;
762-
}
763-
764-
let tail = self.tcx.struct_tail(ty);
765-
match tail.sty {
766-
ty::Foreign(..) => false,
767-
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
768-
_ => bug!("unexpected unsized tail: {:?}", tail.sty),
769-
}
770-
}
771745
}
772746

773747
impl ty::layout::HasDataLayout for &'a CodegenCx<'ll, 'tcx, &'ll Value> {

src/librustc_codegen_llvm/glue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use meth;
2020
use rustc::ty::layout::LayoutOf;
2121
use rustc::ty::{self, Ty};
2222
use value::Value;
23-
use interfaces::{BuilderMethods, ConstMethods};
23+
use interfaces::*;
2424

2525
pub fn size_and_align_of_dst(
2626
bx: &Builder<'_, 'll, 'tcx, &'ll Value>,

src/librustc_codegen_llvm/interfaces/type_.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub trait BaseTypeMethods<'a, 'tcx: 'a> : Backend {
5151
fn tcx(&self) -> &TyCtxt<'a, 'tcx, 'tcx>;
5252
}
5353

54-
pub trait DerivedTypeMethods : Backend {
54+
pub trait DerivedTypeMethods<'tcx> : Backend {
5555
fn type_bool(&self) -> Self::Type;
5656
fn type_char(&self) -> Self::Type;
5757
fn type_i8p(&self) -> Self::Type;
@@ -76,10 +76,21 @@ pub trait DerivedTypeMethods : Backend {
7676
size: Size,
7777
align: Align
7878
) -> Self::Type;
79+
80+
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool;
81+
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool;
82+
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool;
83+
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool;
7984
}
8085

8186
pub trait LayoutTypeMethods<'tcx> : Backend {
82-
fn backend_type(&self, ty: TyLayout<'tcx>) -> Self::Type;
87+
fn backend_type(&self, ty: &TyLayout<'tcx>) -> Self::Type;
88+
fn scalar_pair_element_backend_type<'a>(
89+
&self,
90+
ty: &TyLayout<'tcx>,
91+
index: usize,
92+
immediate: bool
93+
) -> Self::Type;
8394
}
8495

85-
pub trait TypeMethods<'a, 'tcx: 'a> : BaseTypeMethods<'a, 'tcx> + DerivedTypeMethods + LayoutTypeMethods<'tcx> {}
96+
pub trait TypeMethods<'a, 'tcx: 'a> : BaseTypeMethods<'a, 'tcx> + DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx> {}

src/librustc_codegen_llvm/mir/analyze.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc::ty::layout::LayoutOf;
2222
use type_of::LayoutLlvmExt;
2323
use super::FunctionCx;
2424
use value::Value;
25+
use interfaces::*;
2526

2627
pub fn non_ssa_locals(fx: &FunctionCx<'a, 'll, 'tcx, &'ll Value>) -> BitSet<mir::Local> {
2728
let mir = fx.mir;

src/librustc_codegen_llvm/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use type_::Type;
2525
use type_of::LayoutLlvmExt;
2626
use value::Value;
2727

28-
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, IntrinsicDeclarationMethods};
28+
use interfaces::*;
2929

3030
use super::{FunctionCx, LocalRef};
3131
use super::operand::{OperandRef, OperandValue};

src/librustc_codegen_llvm/type_.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use interfaces::*;
2222
use syntax::ast;
2323
use rustc::ty::layout::{self, Align, Size};
2424
use rustc::util::nodemap::FxHashMap;
25-
use rustc::ty::{Ty, TyCtxt};
25+
use rustc::ty::{self, Ty, TyCtxt};
2626
use rustc::ty::layout::TyLayout;
2727
use rustc_data_structures::small_c_str::SmallCStr;
2828
use common::{self, TypeKind};
@@ -276,7 +276,7 @@ impl Type {
276276
}
277277
}
278278

279-
impl DerivedTypeMethods for CodegenCx<'ll, 'tcx, &'ll Value> {
279+
impl DerivedTypeMethods<'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {
280280

281281
fn type_bool(&self) -> &'ll Type {
282282
&self.type_i8()
@@ -373,12 +373,46 @@ impl DerivedTypeMethods for CodegenCx<'ll, 'tcx, &'ll Value> {
373373
assert_eq!(size % unit_size, 0);
374374
&self.type_array(&self.type_from_integer(unit), size / unit_size)
375375
}
376+
377+
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
378+
common::type_needs_drop(*self.tcx(), ty)
379+
}
380+
381+
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
382+
common::type_is_sized(*self.tcx(), ty)
383+
}
384+
385+
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
386+
common::type_is_freeze(*self.tcx(), ty)
387+
}
388+
389+
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
390+
use syntax_pos::DUMMY_SP;
391+
if ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all()) {
392+
return false;
393+
}
394+
395+
let tail = self.tcx().struct_tail(ty);
396+
match tail.sty {
397+
ty::Foreign(..) => false,
398+
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
399+
_ => bug!("unexpected unsized tail: {:?}", tail.sty),
400+
}
401+
}
376402
}
377403

378404
impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {
379-
fn backend_type(&self, ty: TyLayout<'tcx>) -> &'ll Type {
405+
fn backend_type(&self, ty: &TyLayout<'tcx>) -> &'ll Type {
380406
ty.llvm_type(&self)
381407
}
408+
fn scalar_pair_element_backend_type<'a>(
409+
&self,
410+
ty: &TyLayout<'tcx>,
411+
index: usize,
412+
immediate: bool
413+
) -> &'ll Type {
414+
ty.scalar_pair_element_llvm_type(&self, index, immediate)
415+
}
382416
}
383417

384418
impl TypeMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {}

0 commit comments

Comments
 (0)