Skip to content

Commit 9a8dfd7

Browse files
committed
Generalized base::unsized_info
1 parent ff46476 commit 9a8dfd7

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;
@@ -188,16 +187,16 @@ pub fn compare_simd_types<'a, 'll:'a, 'tcx:'ll, Builder : BuilderMethods<'a, 'll
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, &'ll Value>,
190+
pub fn unsized_info<'a, 'll: 'a, 'tcx: 'll, Cx: 'a + CodegenMethods<'ll, 'tcx>>(
191+
cx: &'a 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 where &'a Cx: LayoutOf<Ty = Ty<'tcx>, TyLayout = TyLayout<'tcx>> + HasTyCtxt<'tcx> {
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: 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
@@ -321,7 +321,7 @@ impl<'ll, 'tcx : 'll> ConstMethods for CodegenCx<'ll, 'tcx, &'ll Value> {
321321
fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
322322
let len = s.len();
323323
let cs = consts::ptrcast(&self.const_cstr(s, false),
324-
&self.type_ptr_to(&self.layout_of(&self.tcx.mk_str()).llvm_type(&self)));
324+
&self.type_ptr_to(&self.layout_of(&self.tcx.mk_str()).llvm_type(self)));
325325
&self.const_fat_ptr(cs, &self.const_usize(len as u64))
326326
}
327327

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

@@ -1744,58 +1745,61 @@ pub fn extend_scope_to_file(
17441745
}
17451746
}
17461747

1747-
/// Creates debug information for the given vtable, which is for the
1748-
/// given type.
1749-
///
1750-
/// Adds the created metadata nodes directly to the crate's IR.
1751-
pub fn create_vtable_metadata(
1752-
cx: &CodegenCx<'ll, 'tcx, &'ll Value>,
1753-
ty: ty::Ty<'tcx>,
1754-
vtable: &'ll Value,
1755-
) {
1756-
if cx.dbg_cx.is_none() {
1757-
return;
1758-
}
1759-
1760-
let type_metadata = type_metadata(cx, ty, syntax_pos::DUMMY_SP);
1761-
1762-
unsafe {
1763-
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
1764-
// pointer will lead to hard to trace and debug LLVM assertions
1765-
// later on in llvm/lib/IR/Value.cpp.
1766-
let empty_array = create_DIArray(DIB(cx), &[]);
1748+
impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {
17671749

1768-
let name = const_cstr!("vtable");
1750+
/// Creates debug information for the given vtable, which is for the
1751+
/// given type.
1752+
///
1753+
/// Adds the created metadata nodes directly to the crate's IR.
1754+
fn create_vtable_metadata(
1755+
&self,
1756+
ty: ty::Ty<'tcx>,
1757+
vtable: &'ll Value,
1758+
) {
1759+
if self.dbg_cx.is_none() {
1760+
return;
1761+
}
17691762

1770-
// Create a new one each time. We don't want metadata caching
1771-
// here, because each vtable will refer to a unique containing
1772-
// type.
1773-
let vtable_type = llvm::LLVMRustDIBuilderCreateStructType(
1774-
DIB(cx),
1775-
NO_SCOPE_METADATA,
1776-
name.as_ptr(),
1777-
unknown_file_metadata(cx),
1778-
UNKNOWN_LINE_NUMBER,
1779-
Size::ZERO.bits(),
1780-
cx.tcx.data_layout.pointer_align.abi_bits() as u32,
1781-
DIFlags::FlagArtificial,
1782-
None,
1783-
empty_array,
1784-
0,
1785-
Some(type_metadata),
1786-
name.as_ptr()
1787-
);
1763+
let type_metadata = type_metadata(&self, ty, syntax_pos::DUMMY_SP);
17881764

1789-
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),
1790-
NO_SCOPE_METADATA,
1791-
name.as_ptr(),
1792-
ptr::null(),
1793-
unknown_file_metadata(cx),
1794-
UNKNOWN_LINE_NUMBER,
1795-
vtable_type,
1796-
true,
1797-
vtable,
1798-
None,
1799-
0);
1765+
unsafe {
1766+
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
1767+
// pointer will lead to hard to trace and debug LLVM assertions
1768+
// later on in llvm/lib/IR/Value.cpp.
1769+
let empty_array = create_DIArray(DIB(&self), &[]);
1770+
1771+
let name = const_cstr!("vtable");
1772+
1773+
// Create a new one each time. We don't want metadata caching
1774+
// here, because each vtable will refer to a unique containing
1775+
// type.
1776+
let vtable_type = llvm::LLVMRustDIBuilderCreateStructType(
1777+
DIB(&self),
1778+
NO_SCOPE_METADATA,
1779+
name.as_ptr(),
1780+
unknown_file_metadata(&self),
1781+
UNKNOWN_LINE_NUMBER,
1782+
Size::ZERO.bits(),
1783+
self.tcx.data_layout.pointer_align.abi_bits() as u32,
1784+
DIFlags::FlagArtificial,
1785+
None,
1786+
empty_array,
1787+
0,
1788+
Some(type_metadata),
1789+
name.as_ptr()
1790+
);
1791+
1792+
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(&self),
1793+
NO_SCOPE_METADATA,
1794+
name.as_ptr(),
1795+
ptr::null(),
1796+
unknown_file_metadata(&self),
1797+
UNKNOWN_LINE_NUMBER,
1798+
vtable_type,
1799+
true,
1800+
vtable,
1801+
None,
1802+
0);
1803+
}
18001804
}
18011805
}

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)