Skip to content

Commit 034f697

Browse files
denismerigouxeddyb
authored andcommitted
Generalized base::unsized_info
1 parent 484e07c commit 034f697

26 files changed

+341
-290
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use builder::{Builder, MemFlags};
5555
use callee;
5656
use rustc_mir::monomorphize::item::DefPathBasedNames;
5757
use common::{self, IntPredicate, RealPredicate, TypeKind};
58-
use consts;
5958
use context::CodegenCx;
6059
use debuginfo;
6160
use declare;
@@ -188,16 +187,16 @@ pub fn compare_simd_types<'a, 'tcx: 'a, Builder: BuilderMethods<'a, 'tcx>>(
188187
/// The `old_info` argument is a bit funny. It is intended for use
189188
/// in an upcast, where the new vtable for an object will be derived
190189
/// from the old one.
191-
pub fn unsized_info(
192-
cx: &CodegenCx<'ll, 'tcx>,
190+
pub fn unsized_info<'tcx, Cx: CodegenMethods<'tcx>>(
191+
cx: &Cx,
193192
source: Ty<'tcx>,
194193
target: Ty<'tcx>,
195-
old_info: Option<&'ll Value>,
196-
) -> &'ll Value {
197-
let (source, target) = cx.tcx.struct_lockstep_tails(source, target);
194+
old_info: Option<Cx::Value>,
195+
) -> Cx::Value {
196+
let (source, target) = cx.tcx().struct_lockstep_tails(source, target);
198197
match (&source.sty, &target.sty) {
199198
(&ty::Array(_, len), &ty::Slice(_)) => {
200-
cx.const_usize(len.unwrap_usize(cx.tcx))
199+
cx.const_usize(len.unwrap_usize(cx.tcx()))
201200
}
202201
(&ty::Dynamic(..), &ty::Dynamic(..)) => {
203202
// For now, upcasts are limited to changes in marker
@@ -206,10 +205,10 @@ pub fn unsized_info(
206205
old_info.expect("unsized_info: missing old info for trait upcast")
207206
}
208207
(_, &ty::Dynamic(ref data, ..)) => {
209-
let vtable_ptr = cx.layout_of(cx.tcx.mk_mut_ptr(target))
208+
let vtable_ptr = cx.layout_of(cx.tcx().mk_mut_ptr(target))
210209
.field(cx, abi::FAT_PTR_EXTRA);
211-
consts::ptrcast(meth::get_vtable(cx, source, data.principal()),
212-
vtable_ptr.llvm_type(cx))
210+
cx.static_ptrcast(meth::get_vtable(cx, source, data.principal()),
211+
cx.backend_type(vtable_ptr))
213212
}
214213
_ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
215214
source,

src/librustc_codegen_llvm/builder.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use context::CodegenCx;
1515
use type_::Type;
1616
use value::Value;
1717
use libc::{c_uint, c_char};
18-
use rustc::ty::TyCtxt;
19-
use rustc::ty::layout::{Align, Size};
18+
use rustc::ty::{self, Ty, TyCtxt};
19+
use rustc::ty::layout::{Align, Size, TyLayout};
2020
use rustc::session::{config, Session};
2121
use rustc_data_structures::small_c_str::SmallCStr;
2222
use interfaces::*;
@@ -56,7 +56,36 @@ bitflags! {
5656
}
5757
}
5858

59-
impl HasCodegen for Builder<'a, 'll, 'tcx> {
59+
impl BackendTypes for Builder<'_, 'll, '_> {
60+
type Value = &'ll Value;
61+
type BasicBlock = &'ll BasicBlock;
62+
type Type = &'ll Type;
63+
type Context = &'ll llvm::Context;
64+
}
65+
66+
impl ty::layout::HasDataLayout for Builder<'_, '_, '_> {
67+
fn data_layout(&self) -> &ty::layout::TargetDataLayout {
68+
self.cx.data_layout()
69+
}
70+
}
71+
72+
impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
73+
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
74+
self.cx.tcx
75+
}
76+
}
77+
78+
impl ty::layout::LayoutOf for Builder<'_, '_, 'tcx> {
79+
type Ty = Ty<'tcx>;
80+
type TyLayout = TyLayout<'tcx>;
81+
82+
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
83+
self.cx.layout_of(ty)
84+
}
85+
}
86+
87+
88+
impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
6089
type CodegenCx = CodegenCx<'ll, 'tcx>;
6190
}
6291

@@ -98,10 +127,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
98127
self.cx.sess()
99128
}
100129

101-
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
102-
self.cx.tcx
103-
}
104-
105130
fn llfn(&self) -> &'ll Value {
106131
unsafe {
107132
llvm::LLVMGetBasicBlockParent(self.llbb())

src/librustc_codegen_llvm/callee.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use llvm;
2222
use monomorphize::Instance;
2323
use type_of::LayoutLlvmExt;
2424
use value::Value;
25-
use interfaces::BaseTypeMethods;
25+
use interfaces::*;
2626

2727
use rustc::hir::def_id::DefId;
2828
use rustc::ty::{self, TypeFoldable};
@@ -206,31 +206,33 @@ pub fn get_fn(
206206
llfn
207207
}
208208

209-
pub fn resolve_and_get_fn(
210-
cx: &CodegenCx<'ll, 'tcx>,
209+
pub fn resolve_and_get_fn<'tcx,
210+
Cx: Backend<'tcx> + MiscMethods<'tcx> + TypeMethods<'tcx>
211+
>(
212+
cx: &Cx,
211213
def_id: DefId,
212214
substs: &'tcx Substs<'tcx>,
213-
) -> &'ll Value {
214-
get_fn(
215-
cx,
215+
) -> Cx::Value {
216+
cx.get_fn(
216217
ty::Instance::resolve(
217-
cx.tcx,
218+
cx.tcx(),
218219
ty::ParamEnv::reveal_all(),
219220
def_id,
220221
substs
221222
).unwrap()
222223
)
223224
}
224225

225-
pub fn resolve_and_get_fn_for_vtable(
226-
cx: &CodegenCx<'ll, 'tcx>,
226+
pub fn resolve_and_get_fn_for_vtable<'tcx,
227+
Cx: Backend<'tcx> + MiscMethods<'tcx> + TypeMethods<'tcx>
228+
>(
229+
cx: &Cx,
227230
def_id: DefId,
228231
substs: &'tcx Substs<'tcx>,
229-
) -> &'ll Value {
230-
get_fn(
231-
cx,
232+
) -> Cx::Value {
233+
cx.get_fn(
232234
ty::Instance::resolve_for_vtable(
233-
cx.tcx,
235+
cx.tcx(),
234236
ty::ParamEnv::reveal_all(),
235237
def_id,
236238
substs

src/librustc_codegen_llvm/common.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ use declare;
2323
use type_::Type;
2424
use type_of::LayoutLlvmExt;
2525
use value::Value;
26-
use interfaces::{Backend, ConstMethods, BaseTypeMethods};
26+
use interfaces::{BackendTypes, BuilderMethods, ConstMethods, BaseTypeMethods};
2727

2828
use rustc::ty::{self, Ty, TyCtxt};
2929
use rustc::ty::layout::{HasDataLayout, LayoutOf};
3030
use rustc::hir;
31-
use interfaces::BuilderMethods;
3231

3332
use libc::{c_uint, c_char};
3433

@@ -213,15 +212,14 @@ impl Funclet<'ll> {
213212
}
214213
}
215214

216-
impl Backend for CodegenCx<'ll, 'tcx> {
215+
impl BackendTypes for CodegenCx<'ll, 'tcx> {
217216
type Value = &'ll Value;
218217
type BasicBlock = &'ll BasicBlock;
219218
type Type = &'ll Type;
220219
type Context = &'ll llvm::Context;
221220
}
222221

223-
impl<'ll, 'tcx: 'll> ConstMethods for CodegenCx<'ll, 'tcx> {
224-
222+
impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
225223
// LLVM constant constructors.
226224
fn const_null(&self, t: &'ll Type) -> &'ll Value {
227225
unsafe {
@@ -319,7 +317,7 @@ impl<'ll, 'tcx: 'll> ConstMethods for CodegenCx<'ll, 'tcx> {
319317
fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
320318
let len = s.len();
321319
let cs = consts::ptrcast(self.const_cstr(s, false),
322-
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(&self)));
320+
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
323321
self.const_fat_ptr(cs, self.const_usize(len as u64))
324322
}
325323

src/librustc_codegen_llvm/consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl StaticMethods<'tcx> for CodegenCx<'ll, 'tcx> {
201201

202202
let g = if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
203203

204-
let llty = self.layout_of(ty).llvm_type(&self);
204+
let llty = self.layout_of(ty).llvm_type(self);
205205
let (g, attrs) = match self.tcx.hir.get(id) {
206206
Node::Item(&hir::Item {
207207
ref attrs, span, node: hir::ItemKind::Static(..), ..
@@ -329,7 +329,7 @@ impl StaticMethods<'tcx> for CodegenCx<'ll, 'tcx> {
329329

330330
let instance = Instance::mono(self.tcx, def_id);
331331
let ty = instance.ty(self.tcx);
332-
let llty = self.layout_of(ty).llvm_type(&self);
332+
let llty = self.layout_of(ty).llvm_type(self);
333333
let g = if val_llty == llty {
334334
g
335335
} else {

src/librustc_codegen_llvm/context.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use value::Value;
2323
use monomorphize::partitioning::CodegenUnit;
2424
use type_::Type;
2525
use type_of::PointeeInfo;
26-
use interfaces::{BaseTypeMethods, DerivedTypeMethods, IntrinsicDeclarationMethods};
26+
use interfaces::*;
2727

2828
use rustc_data_structures::base_n;
2929
use rustc_data_structures::small_c_str::SmallCStr;
@@ -322,7 +322,18 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
322322
}
323323
}
324324

325-
impl IntrinsicDeclarationMethods for CodegenCx<'b, 'tcx> {
325+
impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
326+
fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>,
327+
ty::PolyExistentialTraitRef<'tcx>), &'ll Value>>
328+
{
329+
&self.vtables
330+
}
331+
fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
332+
callee::get_fn(&&self,instance)
333+
}
334+
}
335+
336+
impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {
326337
fn get_intrinsic(&self, key: &str) -> &'b Value {
327338
if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
328339
return v;

src/librustc_codegen_llvm/debuginfo/metadata.rs

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use super::utils::{debug_context, DIB, span_start,
1717
use super::namespace::mangled_name_of_instance;
1818
use super::type_names::compute_debuginfo_type_name;
1919
use super::{CrateDebugContext};
20+
use interfaces::*;
2021
use abi;
2122
use interfaces::ConstMethods;
2223
use value::Value;
@@ -1983,58 +1984,60 @@ pub fn extend_scope_to_file(
19831984
}
19841985
}
19851986

1986-
/// Creates debug information for the given vtable, which is for the
1987-
/// given type.
1988-
///
1989-
/// Adds the created metadata nodes directly to the crate's IR.
1990-
pub fn create_vtable_metadata(
1991-
cx: &CodegenCx<'ll, 'tcx>,
1992-
ty: ty::Ty<'tcx>,
1993-
vtable: &'ll Value,
1994-
) {
1995-
if cx.dbg_cx.is_none() {
1996-
return;
1997-
}
1998-
1999-
let type_metadata = type_metadata(cx, ty, syntax_pos::DUMMY_SP);
2000-
2001-
unsafe {
2002-
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
2003-
// pointer will lead to hard to trace and debug LLVM assertions
2004-
// later on in llvm/lib/IR/Value.cpp.
2005-
let empty_array = create_DIArray(DIB(cx), &[]);
2006-
2007-
let name = const_cstr!("vtable");
1987+
impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
1988+
/// Creates debug information for the given vtable, which is for the
1989+
/// given type.
1990+
///
1991+
/// Adds the created metadata nodes directly to the crate's IR.
1992+
fn create_vtable_metadata(
1993+
&self,
1994+
ty: ty::Ty<'tcx>,
1995+
vtable: &'ll Value,
1996+
) {
1997+
if self.dbg_cx.is_none() {
1998+
return;
1999+
}
20082000

2009-
// Create a new one each time. We don't want metadata caching
2010-
// here, because each vtable will refer to a unique containing
2011-
// type.
2012-
let vtable_type = llvm::LLVMRustDIBuilderCreateStructType(
2013-
DIB(cx),
2014-
NO_SCOPE_METADATA,
2015-
name.as_ptr(),
2016-
unknown_file_metadata(cx),
2017-
UNKNOWN_LINE_NUMBER,
2018-
Size::ZERO.bits(),
2019-
cx.tcx.data_layout.pointer_align.abi_bits() as u32,
2020-
DIFlags::FlagArtificial,
2021-
None,
2022-
empty_array,
2023-
0,
2024-
Some(type_metadata),
2025-
name.as_ptr()
2026-
);
2001+
let type_metadata = type_metadata(&self, ty, syntax_pos::DUMMY_SP);
20272002

2028-
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),
2029-
NO_SCOPE_METADATA,
2030-
name.as_ptr(),
2031-
ptr::null(),
2032-
unknown_file_metadata(cx),
2033-
UNKNOWN_LINE_NUMBER,
2034-
vtable_type,
2035-
true,
2036-
vtable,
2037-
None,
2038-
0);
2003+
unsafe {
2004+
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
2005+
// pointer will lead to hard to trace and debug LLVM assertions
2006+
// later on in llvm/lib/IR/Value.cpp.
2007+
let empty_array = create_DIArray(DIB(&self), &[]);
2008+
2009+
let name = const_cstr!("vtable");
2010+
2011+
// Create a new one each time. We don't want metadata caching
2012+
// here, because each vtable will refer to a unique containing
2013+
// type.
2014+
let vtable_type = llvm::LLVMRustDIBuilderCreateStructType(
2015+
DIB(&self),
2016+
NO_SCOPE_METADATA,
2017+
name.as_ptr(),
2018+
unknown_file_metadata(&self),
2019+
UNKNOWN_LINE_NUMBER,
2020+
Size::ZERO.bits(),
2021+
self.tcx.data_layout.pointer_align.abi_bits() as u32,
2022+
DIFlags::FlagArtificial,
2023+
None,
2024+
empty_array,
2025+
0,
2026+
Some(type_metadata),
2027+
name.as_ptr()
2028+
);
2029+
2030+
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(&self),
2031+
NO_SCOPE_METADATA,
2032+
name.as_ptr(),
2033+
ptr::null(),
2034+
unknown_file_metadata(&self),
2035+
UNKNOWN_LINE_NUMBER,
2036+
vtable_type,
2037+
true,
2038+
vtable,
2039+
None,
2040+
0);
2041+
}
20392042
}
20402043
}

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ mod source_loc;
5858
pub use self::create_scope_map::{create_mir_scopes, MirDebugScope};
5959
pub use self::source_loc::start_emitting_source_locations;
6060
pub use self::metadata::create_global_var_metadata;
61-
pub use self::metadata::create_vtable_metadata;
6261
pub use self::metadata::extend_scope_to_file;
6362
pub use self::source_loc::set_source_location;
6463

src/librustc_codegen_llvm/glue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std;
1717
use builder::Builder;
1818
use common::*;
1919
use meth;
20-
use rustc::ty::layout::LayoutOf;
20+
use rustc::ty::layout::{LayoutOf, HasTyCtxt};
2121
use rustc::ty::{self, Ty};
2222
use value::Value;
2323
use interfaces::{BuilderMethods, ConstMethods};

0 commit comments

Comments
 (0)