Skip to content

Commit c62b576

Browse files
committed
Generalized base::unsized_info
1 parent df3fd1c commit c62b576

File tree

17 files changed

+190
-102
lines changed

17 files changed

+190
-102
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc::middle::weak_lang_items;
3939
use rustc::mir::mono::{Linkage, Visibility, Stats, CodegenUnitNameBuilder};
4040
use rustc::middle::cstore::{EncodedMetadata};
4141
use rustc::ty::{self, Ty, TyCtxt};
42-
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf};
42+
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, HasTyCtxt};
4343
use rustc::ty::query::Providers;
4444
use rustc::middle::cstore::{self, LinkagePreference};
4545
use rustc::middle::exported_symbols;
@@ -56,7 +56,6 @@ use callee;
5656
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
5757
use rustc_mir::monomorphize::item::DefPathBasedNames;
5858
use common::{self, IntPredicate, RealPredicate, TypeKind};
59-
use consts;
6059
use context::CodegenCx;
6160
use debuginfo;
6261
use declare;
@@ -189,16 +188,16 @@ pub fn compare_simd_types<'a, 'll:'a, 'tcx:'ll, Builder : BuilderMethods<'a, 'll
189188
/// The `old_info` argument is a bit funny. It is intended for use
190189
/// in an upcast, where the new vtable for an object will be derived
191190
/// from the old one.
192-
pub fn unsized_info(
193-
cx: &CodegenCx<'ll, 'tcx, &'ll Value>,
191+
pub fn unsized_info<'a, 'll: 'a, 'tcx: 'll, Cx: 'a + CodegenMethods<'ll, 'tcx>>(
192+
cx: &'a Cx,
194193
source: Ty<'tcx>,
195194
target: Ty<'tcx>,
196-
old_info: Option<&'ll Value>,
197-
) -> &'ll Value {
198-
let (source, target) = cx.tcx.struct_lockstep_tails(source, target);
195+
old_info: Option<Cx::Value>,
196+
) -> Cx::Value where &'a Cx: LayoutOf<Ty = Ty<'tcx>, TyLayout = TyLayout<'tcx>> + HasTyCtxt<'tcx> {
197+
let (source, target) = cx.tcx().struct_lockstep_tails(source, target);
199198
match (&source.sty, &target.sty) {
200199
(&ty::Array(_, len), &ty::Slice(_)) => {
201-
cx.const_usize(len.unwrap_usize(cx.tcx))
200+
cx.const_usize(len.unwrap_usize(*cx.tcx()))
202201
}
203202
(&ty::Dynamic(..), &ty::Dynamic(..)) => {
204203
// For now, upcasts are limited to changes in marker
@@ -207,10 +206,10 @@ pub fn unsized_info(
207206
old_info.expect("unsized_info: missing old info for trait upcast")
208207
}
209208
(_, &ty::Dynamic(ref data, ..)) => {
210-
let vtable_ptr = cx.layout_of(cx.tcx.mk_mut_ptr(target))
209+
let vtable_ptr = cx.layout_of(cx.tcx().mk_mut_ptr(target))
211210
.field(cx, abi::FAT_PTR_EXTRA);
212-
consts::ptrcast(meth::get_vtable(cx, source, data.principal()),
213-
vtable_ptr.llvm_type(cx))
211+
cx.static_ptrcast(meth::get_vtable(cx, source, data.principal()),
212+
cx.backend_type(vtable_ptr))
214213
}
215214
_ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
216215
source,

src/librustc_codegen_llvm/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ bitflags! {
5656
}
5757
}
5858

59-
impl<'a, 'll: 'a, 'tcx: 'll> HasCodegen<'a> for Builder<'a, 'll, 'tcx, &'ll Value> {
59+
impl HasCodegen<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx, &'ll Value> {
6060
type CodegenCx = CodegenCx<'ll, 'tcx, &'ll Value>;
6161
}
6262

63-
impl<'a, 'll: 'a, 'tcx: 'll> BuilderMethods<'a, 'll, 'tcx>
63+
impl BuilderMethods<'a, 'll, 'tcx>
6464
for Builder<'a, 'll, 'tcx, &'ll Value> {
6565

6666
fn new_block<'b>(

src/librustc_codegen_llvm/callee.rs

Lines changed: 8 additions & 7 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,15 +206,16 @@ pub fn get_fn(
206206
llfn
207207
}
208208

209-
pub fn resolve_and_get_fn(
210-
cx: &CodegenCx<'ll, 'tcx, &'ll Value>,
209+
pub fn resolve_and_get_fn<'ll, 'tcx: 'll,
210+
Cx : Backend + MiscMethods<'tcx> + TypeMethods<'ll, '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

src/librustc_codegen_llvm/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'ll, 'tcx : 'll> ConstMethods for CodegenCx<'ll, 'tcx, &'ll Value> {
319319
fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
320320
let len = s.len();
321321
let cs = consts::ptrcast(&self.const_cstr(s, false),
322-
&self.type_ptr_to(&self.layout_of(&self.tcx.mk_str()).llvm_type(&self)));
322+
&self.type_ptr_to(&self.layout_of(&self.tcx.mk_str()).llvm_type(self)));
323323
&self.const_fat_ptr(cs, &self.const_usize(len as u64))
324324
}
325325

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, &'ll Value> {
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, &'ll Value> {
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: 14 additions & 1 deletion
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;
@@ -323,6 +323,19 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx, &'b Value> {
323323
}
324324
}
325325

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

src/librustc_codegen_llvm/debuginfo/metadata.rs

Lines changed: 54 additions & 50 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 value::Value;
2223

@@ -1982,58 +1983,61 @@ pub fn extend_scope_to_file(
19821983
}
19831984
}
19841985

1985-
/// Creates debug information for the given vtable, which is for the
1986-
/// given type.
1987-
///
1988-
/// Adds the created metadata nodes directly to the crate's IR.
1989-
pub fn create_vtable_metadata(
1990-
cx: &CodegenCx<'ll, 'tcx, &'ll Value>,
1991-
ty: ty::Ty<'tcx>,
1992-
vtable: &'ll Value,
1993-
) {
1994-
if cx.dbg_cx.is_none() {
1995-
return;
1996-
}
1997-
1998-
let type_metadata = type_metadata(cx, ty, syntax_pos::DUMMY_SP);
1986+
impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {
19991987

2000-
unsafe {
2001-
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
2002-
// pointer will lead to hard to trace and debug LLVM assertions
2003-
// later on in llvm/lib/IR/Value.cpp.
2004-
let empty_array = create_DIArray(DIB(cx), &[]);
2005-
2006-
let name = const_cstr!("vtable");
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+
}
20072000

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

2027-
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),
2028-
NO_SCOPE_METADATA,
2029-
name.as_ptr(),
2030-
ptr::null(),
2031-
unknown_file_metadata(cx),
2032-
UNKNOWN_LINE_NUMBER,
2033-
vtable_type,
2034-
true,
2035-
vtable,
2036-
None,
2037-
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+
}
20382042
}
20392043
}

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/interfaces/builder.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ use rustc::ty::layout::{Align, Size};
1515
use rustc::session::Session;
1616
use builder::MemFlags;
1717
use super::backend::Backend;
18-
use super::type_::TypeMethods;
19-
use super::consts::ConstMethods;
20-
use super::intrinsic::IntrinsicDeclarationMethods;
18+
use super::CodegenMethods;
2119

2220
use std::borrow::Cow;
2321
use std::ops::Range;
2422
use syntax::ast::AsmDialect;
2523

26-
pub trait HasCodegen<'a> {
27-
type CodegenCx : 'a + Backend + TypeMethods + ConstMethods + IntrinsicDeclarationMethods;
24+
pub trait HasCodegen<'a, 'll: 'a, 'tcx :'ll> {
25+
type CodegenCx : 'a + CodegenMethods<'ll, 'tcx>;
2826
}
2927

30-
pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll> : HasCodegen<'a> {
28+
pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll> : HasCodegen<'a, 'll, 'tcx> {
3129
fn new_block<'b>(
3230
cx: &'a Self::CodegenCx,
3331
llfn: <Self::CodegenCx as Backend>::Value,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use rustc::ty::Ty;
12+
use super::backend::Backend;
13+
14+
pub trait DebugInfoMethods<'tcx> : Backend {
15+
fn create_vtable_metadata(
16+
&self,
17+
ty: Ty<'tcx>,
18+
vtable: Self::Value,
19+
);
20+
}

0 commit comments

Comments
 (0)