Skip to content

Commit 847e0bc

Browse files
committed
Traitification of type_ methods
The methods are now attached to CodegenCx instead of Type
1 parent 9f7adef commit 847e0bc

File tree

20 files changed

+408
-408
lines changed

20 files changed

+408
-408
lines changed

src/librustc_codegen_llvm/abi.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use type_::Type;
1919
use type_of::{LayoutLlvmExt, PointerKind};
2020
use value::Value;
2121

22-
use interfaces::{BuilderMethods, CommonMethods};
22+
use interfaces::{BuilderMethods, CommonMethods, TypeMethods};
2323

2424
use rustc_target::abi::{LayoutOf, Size, TyLayout};
2525
use rustc::ty::{self, Ty};
@@ -112,16 +112,16 @@ pub trait LlvmType {
112112
impl LlvmType for Reg {
113113
fn llvm_type(&self, cx: &CodegenCx<'ll, '_, &'ll Value>) -> &'ll Type {
114114
match self.kind {
115-
RegKind::Integer => Type::ix(cx, self.size.bits()),
115+
RegKind::Integer => cx.ix(self.size.bits()),
116116
RegKind::Float => {
117117
match self.size.bits() {
118-
32 => Type::f32(cx),
119-
64 => Type::f64(cx),
118+
32 => cx.f32(),
119+
64 => cx.f64(),
120120
_ => bug!("unsupported float: {:?}", self)
121121
}
122122
}
123123
RegKind::Vector => {
124-
Type::vector(Type::i8(cx), self.size.bytes())
124+
cx.vector(cx.i8(), self.size.bytes())
125125
}
126126
}
127127
}
@@ -145,7 +145,7 @@ impl LlvmType for CastTarget {
145145

146146
// Simplify to array when all chunks are the same size and type
147147
if rem_bytes == 0 {
148-
return Type::array(rest_ll_unit, rest_count);
148+
return cx.array(rest_ll_unit, rest_count);
149149
}
150150
}
151151

@@ -160,10 +160,10 @@ impl LlvmType for CastTarget {
160160
if rem_bytes != 0 {
161161
// Only integers can be really split further.
162162
assert_eq!(self.rest.unit.kind, RegKind::Integer);
163-
args.push(Type::ix(cx, rem_bytes * 8));
163+
args.push(cx.ix(rem_bytes * 8));
164164
}
165165

166-
Type::struct_(cx, &args, false)
166+
cx.struct_(&args, false)
167167
}
168168
}
169169

@@ -212,7 +212,7 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
212212
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
213213
let can_store_through_cast_ptr = false;
214214
if can_store_through_cast_ptr {
215-
let cast_dst = bx.pointercast(dst.llval, cast.llvm_type(cx).ptr_to());
215+
let cast_dst = bx.pointercast(dst.llval, cx.ptr_to(cast.llvm_type(cx)));
216216
bx.store(val, cast_dst, self.layout.align);
217217
} else {
218218
// The actual return type is a struct, but the ABI
@@ -240,8 +240,8 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
240240

241241
// ...and then memcpy it to the intended destination.
242242
base::call_memcpy(bx,
243-
bx.pointercast(dst.llval, Type::i8p(cx)),
244-
bx.pointercast(llscratch, Type::i8p(cx)),
243+
bx.pointercast(dst.llval, cx.i8p()),
244+
bx.pointercast(llscratch, cx.i8p()),
245245
cx.c_usize(self.layout.size.bytes()),
246246
self.layout.align.min(scratch_align),
247247
MemFlags::empty());
@@ -605,14 +605,14 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
605605
);
606606

607607
let llreturn_ty = match self.ret.mode {
608-
PassMode::Ignore => Type::void(cx),
608+
PassMode::Ignore => cx.void(),
609609
PassMode::Direct(_) | PassMode::Pair(..) => {
610610
self.ret.layout.immediate_llvm_type(cx)
611611
}
612612
PassMode::Cast(cast) => cast.llvm_type(cx),
613613
PassMode::Indirect(..) => {
614-
llargument_tys.push(self.ret.memory_ty(cx).ptr_to());
615-
Type::void(cx)
614+
llargument_tys.push(cx.ptr_to(self.ret.memory_ty(cx)));
615+
cx.void()
616616
}
617617
};
618618

@@ -638,15 +638,15 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
638638
continue;
639639
}
640640
PassMode::Cast(cast) => cast.llvm_type(cx),
641-
PassMode::Indirect(_, None) => arg.memory_ty(cx).ptr_to(),
641+
PassMode::Indirect(_, None) => cx.ptr_to(arg.memory_ty(cx)),
642642
};
643643
llargument_tys.push(llarg_ty);
644644
}
645645

646646
if self.variadic {
647-
Type::variadic_func(&llargument_tys, llreturn_ty)
647+
cx.variadic_func(&llargument_tys, llreturn_ty)
648648
} else {
649-
Type::func(&llargument_tys, llreturn_ty)
649+
cx.func(&llargument_tys, llreturn_ty)
650650
}
651651
}
652652

src/librustc_codegen_llvm/asm.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010

1111
use llvm;
1212
use context::CodegenCx;
13-
use type_::Type;
1413
use type_of::LayoutLlvmExt;
1514
use builder::Builder;
1615
use value::Value;
1716

1817
use rustc::hir;
19-
use interfaces::{BuilderMethods, CommonMethods};
18+
use interfaces::{BuilderMethods, CommonMethods, TypeMethods};
2019

2120
use mir::place::PlaceRef;
2221
use mir::operand::OperandValue;
@@ -76,9 +75,9 @@ pub fn codegen_inline_asm(
7675
// Depending on how many outputs we have, the return type is different
7776
let num_outputs = output_types.len();
7877
let output_type = match num_outputs {
79-
0 => Type::void(bx.cx()),
78+
0 => bx.cx().void(),
8079
1 => output_types[0],
81-
_ => Type::struct_(bx.cx(), &output_types, false)
80+
_ => bx.cx().struct_(&output_types, false)
8281
};
8382

8483
let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();

src/librustc_codegen_llvm/back/write.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@ impl CommonWriteMethods for CodegenContext<'ll> {
447447
}
448448
}
449449

450+
impl CodegenContext<'ll> {
451+
pub fn ptr_to(&self, ty: &'ll Type) -> &'ll Type {
452+
unsafe {
453+
llvm::LLVMPointerType(ty, 0)
454+
}
455+
}
456+
}
457+
450458

451459
pub struct DiagnosticHandlers<'a> {
452460
data: *mut (&'a CodegenContext<'a>, &'a Handler),
@@ -2572,7 +2580,7 @@ fn create_msvc_imps(cgcx: &CodegenContext, llcx: &llvm::Context, llmod: &llvm::M
25722580
"\x01__imp_"
25732581
};
25742582
unsafe {
2575-
let i8p_ty = Type::i8p_llcx(llcx);
2583+
let i8p_ty = Type::i8p_llcx(cgcx, llcx);
25762584
let globals = base::iter_globals(llmod)
25772585
.filter(|&val| {
25782586
llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage &&

src/librustc_codegen_llvm/base.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use CrateInfo;
7373
use rustc_data_structures::small_c_str::SmallCStr;
7474
use rustc_data_structures::sync::Lrc;
7575

76-
use interfaces::{BuilderMethods, CommonMethods, CommonWriteMethods};
76+
use interfaces::{BuilderMethods, CommonMethods, CommonWriteMethods, TypeMethods};
7777

7878
use std::any::Any;
7979
use std::ffi::CString;
@@ -234,13 +234,13 @@ pub fn unsize_thin_ptr(
234234
(&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
235235
&ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
236236
assert!(bx.cx().type_is_sized(a));
237-
let ptr_ty = bx.cx().layout_of(b).llvm_type(bx.cx()).ptr_to();
237+
let ptr_ty = bx.cx().ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
238238
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
239239
}
240240
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
241241
let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
242242
assert!(bx.cx().type_is_sized(a));
243-
let ptr_ty = bx.cx().layout_of(b).llvm_type(bx.cx()).ptr_to();
243+
let ptr_ty = bx.cx().ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
244244
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
245245
}
246246
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
@@ -353,14 +353,14 @@ fn cast_shift_rhs<'ll, F, G>(bx: &Builder<'_, 'll, '_, &'ll Value>,
353353
if op.is_shift() {
354354
let mut rhs_llty = bx.cx().val_ty(rhs);
355355
let mut lhs_llty = bx.cx().val_ty(lhs);
356-
if rhs_llty.kind() == TypeKind::Vector {
357-
rhs_llty = rhs_llty.element_type()
356+
if bx.cx().kind(rhs_llty) == TypeKind::Vector {
357+
rhs_llty = bx.cx().element_type(rhs_llty)
358358
}
359-
if lhs_llty.kind() == TypeKind::Vector {
360-
lhs_llty = lhs_llty.element_type()
359+
if bx.cx().kind(lhs_llty) == TypeKind::Vector {
360+
lhs_llty = bx.cx().element_type(lhs_llty)
361361
}
362-
let rhs_sz = rhs_llty.int_width();
363-
let lhs_sz = lhs_llty.int_width();
362+
let rhs_sz = bx.cx().int_width(rhs_llty);
363+
let lhs_sz = bx.cx().int_width(lhs_llty);
364364
if lhs_sz < rhs_sz {
365365
trunc(rhs, lhs_llty)
366366
} else if lhs_sz > rhs_sz {
@@ -393,8 +393,8 @@ pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll>(
393393
bx: &Builder<'_ ,'ll, '_, &'ll Value>,
394394
val: &'ll Value
395395
) -> &'ll Value {
396-
if bx.cx().val_ty(val) == Type::i1(bx.cx()) {
397-
bx.zext(val, Type::i8(bx.cx()))
396+
if bx.cx().val_ty(val) == bx.cx().i1() {
397+
bx.zext(val, bx.cx().i8())
398398
} else {
399399
val
400400
}
@@ -417,7 +417,7 @@ pub fn to_immediate_scalar(
417417
scalar: &layout::Scalar,
418418
) -> &'ll Value {
419419
if scalar.is_bool() {
420-
return bx.trunc(val, Type::i1(bx.cx()));
420+
return bx.trunc(val, bx.cx().i1());
421421
}
422422
val
423423
}
@@ -433,16 +433,16 @@ pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll>(
433433
if flags.contains(MemFlags::NONTEMPORAL) {
434434
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
435435
let val = bx.load(src, align);
436-
let ptr = bx.pointercast(dst, bx.cx().val_ty(val).ptr_to());
436+
let ptr = bx.pointercast(dst, bx.cx().ptr_to(bx.cx().val_ty(val)));
437437
bx.store_with_flags(val, ptr, align, flags);
438438
return;
439439
}
440440
let cx = bx.cx();
441441
let ptr_width = &cx.sess().target.target.target_pointer_width;
442442
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
443443
let memcpy = cx.get_intrinsic(&key);
444-
let src_ptr = bx.pointercast(src, Type::i8p(cx));
445-
let dst_ptr = bx.pointercast(dst, Type::i8p(cx));
444+
let src_ptr = bx.pointercast(src, cx.i8p());
445+
let dst_ptr = bx.pointercast(dst, cx.i8p());
446446
let size = bx.intcast(n_bytes, cx.isize_ty, false);
447447
let align = cx.c_i32(align.abi() as i32);
448448
let volatile = cx.c_bool(flags.contains(MemFlags::VOLATILE));
@@ -556,7 +556,7 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx<'ll, '_, &'ll Value>) {
556556
use_start_lang_item: bool,
557557
) {
558558
let llfty =
559-
Type::func(&[Type::c_int(cx), Type::i8p(cx).ptr_to()], Type::c_int(cx));
559+
cx.func(&[cx.t_int(), cx.ptr_to(cx.i8p())], cx.t_int());
560560

561561
let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
562562
// Given that `main()` has no arguments,
@@ -599,15 +599,15 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx<'ll, '_, &'ll Value>) {
599599
start_def_id,
600600
cx.tcx.intern_substs(&[main_ret_ty.into()]),
601601
);
602-
(start_fn, vec![bx.pointercast(rust_main, Type::i8p(cx).ptr_to()),
602+
(start_fn, vec![bx.pointercast(rust_main, cx.ptr_to(cx.i8p())),
603603
arg_argc, arg_argv])
604604
} else {
605605
debug!("using user-defined start fn");
606606
(rust_main, vec![arg_argc, arg_argv])
607607
};
608608

609609
let result = bx.call(start_fn, &args, None);
610-
bx.ret(bx.intcast(result, Type::c_int(cx), true));
610+
bx.ret(bx.intcast(result, cx.t_int(), true));
611611
}
612612
}
613613

@@ -1258,7 +1258,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12581258
if !cx.used_statics.borrow().is_empty() {
12591259
let name = const_cstr!("llvm.used");
12601260
let section = const_cstr!("llvm.metadata");
1261-
let array = cx.c_array(Type::i8(&cx).ptr_to(), &*cx.used_statics.borrow());
1261+
let array = cx.c_array(&cx.ptr_to(cx.i8()), &*cx.used_statics.borrow());
12621262

12631263
unsafe {
12641264
let g = llvm::LLVMAddGlobal(cx.llmod,

src/librustc_codegen_llvm/builder.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::ty::TyCtxt;
1919
use rustc::ty::layout::{Align, Size};
2020
use rustc::session::{config, Session};
2121
use rustc_data_structures::small_c_str::SmallCStr;
22-
use interfaces::{BuilderMethods, Backend, CommonMethods, CommonWriteMethods};
22+
use interfaces::{BuilderMethods, Backend, CommonMethods, CommonWriteMethods, TypeMethods};
2323
use syntax;
2424

2525
use std::borrow::Cow;
@@ -778,7 +778,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
778778
}).collect::<Vec<_>>();
779779

780780
debug!("Asm Output Type: {:?}", output);
781-
let fty = type_::Type::func(&argtys[..], output);
781+
let fty = &self.cx().func(&argtys[..], output);
782782
unsafe {
783783
let v = llvm::LLVMRustInlineAsm(
784784
fty, asm, cons, volatile, alignstack, AsmDialect::from_generic(dia));
@@ -848,10 +848,10 @@ impl BuilderMethods<'a, 'll, 'tcx>
848848
fn vector_splat(&self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
849849
unsafe {
850850
let elt_ty = self.cx.val_ty(elt);
851-
let undef = llvm::LLVMGetUndef(type_::Type::vector(elt_ty, num_elts as u64));
851+
let undef = llvm::LLVMGetUndef(&self.cx().vector(elt_ty, num_elts as u64));
852852
let vec = self.insert_element(undef, elt, self.cx.c_i32(0));
853-
let vec_i32_ty = type_::Type::vector(type_::Type::i32(self.cx), num_elts as u64);
854-
self.shuffle_vector(vec, undef, self.cx.c_null(vec_i32_ty))
853+
let vec_i32_ty = &self.cx().vector(&self.cx().i32(), num_elts as u64);
854+
self.shuffle_vector(vec, undef, self.cx().c_null(vec_i32_ty))
855855
}
856856
}
857857

@@ -1160,9 +1160,9 @@ impl BuilderMethods<'a, 'll, 'tcx>
11601160
ptr: &'ll Value) -> &'ll Value {
11611161
let dest_ptr_ty = self.cx.val_ty(ptr);
11621162
let stored_ty = self.cx.val_ty(val);
1163-
let stored_ptr_ty = stored_ty.ptr_to();
1163+
let stored_ptr_ty = self.cx.ptr_to(stored_ty);
11641164

1165-
assert_eq!(dest_ptr_ty.kind(), llvm::TypeKind::Pointer);
1165+
assert_eq!(self.cx.kind(dest_ptr_ty), llvm::TypeKind::Pointer);
11661166

11671167
if dest_ptr_ty == stored_ptr_ty {
11681168
ptr
@@ -1181,14 +1181,14 @@ impl BuilderMethods<'a, 'll, 'tcx>
11811181
args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
11821182
let mut fn_ty = self.cx.val_ty(llfn);
11831183
// Strip off pointers
1184-
while fn_ty.kind() == llvm::TypeKind::Pointer {
1185-
fn_ty = fn_ty.element_type();
1184+
while self.cx.kind(fn_ty) == llvm::TypeKind::Pointer {
1185+
fn_ty = self.cx.element_type(fn_ty);
11861186
}
11871187

1188-
assert!(fn_ty.kind() == llvm::TypeKind::Function,
1188+
assert!(self.cx.kind(fn_ty) == llvm::TypeKind::Function,
11891189
"builder::{} not passed a function, but {:?}", typ, fn_ty);
11901190

1191-
let param_tys = fn_ty.func_params();
1191+
let param_tys = self.cx.func_params(fn_ty);
11921192

11931193
let all_args_match = param_tys.iter()
11941194
.zip(args.iter().map(|&v| self.cx().val_ty(v)))
@@ -1245,7 +1245,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
12451245

12461246
let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
12471247

1248-
let ptr = self.pointercast(ptr, type_::Type::i8p(self.cx));
1248+
let ptr = self.pointercast(ptr, self.cx.i8p());
12491249
self.call(lifetime_intrinsic, &[self.cx.c_u64(size), ptr], None);
12501250
}
12511251

0 commit comments

Comments
 (0)