Skip to content

Commit 78dd95f

Browse files
denismerigouxeddyb
authored andcommitted
Generalized base::unsize_thin_ptr
1 parent 034f697 commit 78dd95f

File tree

7 files changed

+61
-41
lines changed

7 files changed

+61
-41
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn unsized_info<'tcx, Cx: CodegenMethods<'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,12 @@ pub fn unsized_info<'tcx, Cx: CodegenMethods<'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>,
222-
src: &'ll Value,
220+
pub fn unsize_thin_ptr<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
221+
bx: &Bx,
222+
src: Bx::Value,
223223
src_ty: Ty<'tcx>,
224224
dst_ty: Ty<'tcx>
225-
) -> (&'ll Value, &'ll Value) {
225+
) -> (Bx::Value, Bx::Value) {
226226
debug!("unsize_thin_ptr: {:?} => {:?}", src_ty, dst_ty);
227227
match (&src_ty.sty, &dst_ty.sty) {
228228
(&ty::Ref(_, a, _),
@@ -232,13 +232,13 @@ pub fn unsize_thin_ptr(
232232
(&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
233233
&ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
234234
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()));
235+
let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(&bx.cx().layout_of(b)));
236236
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
237237
}
238238
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
239239
let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
240240
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()));
241+
let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(&bx.cx().layout_of(b)));
242242
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
243243
}
244244
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
@@ -263,8 +263,8 @@ pub fn unsize_thin_ptr(
263263
}
264264
let (lldata, llextra) = result.unwrap();
265265
// 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)))
266+
(bx.bitcast(lldata, bx.cx().scalar_pair_element_backend_type(&dst_layout, 0, true)),
267+
bx.bitcast(llextra, bx.cx().scalar_pair_element_backend_type(&dst_layout, 1, true)))
268268
}
269269
_ => bug!("unsize_thin_ptr: called on bad types"),
270270
}

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;
@@ -746,31 +745,6 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
746745
llfn
747746
}
748747

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

776750
impl ty::layout::HasDataLayout for CodegenCx<'ll, 'tcx> {

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, HasTyCtxt};
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>,

src/librustc_codegen_llvm/interfaces/type_.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,21 @@ pub trait DerivedTypeMethods<'tcx>: Backend<'tcx> {
6161
fn type_from_integer(&self, i: layout::Integer) -> Self::Type;
6262
fn type_pointee_for_abi_align(&self, align: Align) -> Self::Type;
6363
fn type_padding_filler(&self, size: Size, align: Align) -> Self::Type;
64+
65+
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool;
66+
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool;
67+
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool;
68+
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool;
6469
}
6570

6671
pub trait LayoutTypeMethods<'tcx>: Backend<'tcx> {
67-
fn backend_type(&self, ty: TyLayout<'tcx>) -> Self::Type;
72+
fn backend_type(&self, ty: &TyLayout<'tcx>) -> Self::Type;
73+
fn scalar_pair_element_backend_type<'a>(
74+
&self,
75+
ty: &TyLayout<'tcx>,
76+
index: usize,
77+
immediate: bool
78+
) -> Self::Type;
6879
}
6980

7081
pub trait TypeMethods<'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
@@ -20,9 +20,9 @@ use value::Value;
2020

2121

2222
use syntax::ast;
23-
use rustc::ty::layout::{self, Align, Size};
23+
use rustc::ty::layout::{self, Align, Size, HasTyCtxt};
2424
use rustc::util::nodemap::FxHashMap;
25-
use rustc::ty::Ty;
25+
use rustc::ty::{self, Ty};
2626
use rustc::ty::layout::TyLayout;
2727
use rustc_data_structures::small_c_str::SmallCStr;
2828
use common::{self, TypeKind};
@@ -365,10 +365,44 @@ impl DerivedTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
365365
assert_eq!(size % unit_size, 0);
366366
self.type_array(self.type_from_integer(unit), size / unit_size)
367367
}
368+
369+
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
370+
common::type_needs_drop(self.tcx(), ty)
371+
}
372+
373+
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
374+
common::type_is_sized(self.tcx(), ty)
375+
}
376+
377+
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
378+
common::type_is_freeze(self.tcx(), ty)
379+
}
380+
381+
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
382+
use syntax_pos::DUMMY_SP;
383+
if ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all()) {
384+
return false;
385+
}
386+
387+
let tail = self.tcx().struct_tail(ty);
388+
match tail.sty {
389+
ty::Foreign(..) => false,
390+
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
391+
_ => bug!("unexpected unsized tail: {:?}", tail.sty),
392+
}
393+
}
368394
}
369395

370396
impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
371-
fn backend_type(&self, ty: TyLayout<'tcx>) -> &'ll Type {
397+
fn backend_type(&self, ty: &TyLayout<'tcx>) -> &'ll Type {
372398
ty.llvm_type(&self)
373399
}
400+
fn scalar_pair_element_backend_type<'a>(
401+
&self,
402+
ty: &TyLayout<'tcx>,
403+
index: usize,
404+
immediate: bool
405+
) -> &'ll Type {
406+
ty.scalar_pair_element_llvm_type(&self, index, immediate)
407+
}
374408
}

0 commit comments

Comments
 (0)