Skip to content

Commit b25b804

Browse files
denismerigouxeddyb
authored andcommitted
Added default impl for DerivedTypeMethods + empty impl for Cranelift BaseTypeMethods
1 parent 54dd3a4 commit b25b804

File tree

3 files changed

+111
-151
lines changed

3 files changed

+111
-151
lines changed

src/librustc_codegen_llvm/type_.rs

Lines changed: 5 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ use context::CodegenCx;
1818
use rustc_codegen_ssa::interfaces::*;
1919
use value::Value;
2020

21-
22-
use syntax::ast;
23-
use rustc::ty::layout::{self, Align, Size, HasTyCtxt};
2421
use rustc::util::nodemap::FxHashMap;
25-
use rustc::ty::{self, Ty};
22+
use rustc::ty::Ty;
2623
use rustc::ty::layout::TyLayout;
2724
use rustc_target::abi::call::{CastTarget, FnType, Reg};
2825
use rustc_data_structures::small_c_str::SmallCStr;
2926
use common;
30-
use rustc_codegen_ssa;
3127
use rustc_codegen_ssa::common::TypeKind;
3228
use type_of::LayoutLlvmExt;
3329
use abi::{LlvmType, FnTypeExt};
@@ -108,6 +104,10 @@ impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
108104
}
109105
}
110106

107+
fn type_isize(&self) -> &'ll Type {
108+
self.isize_ty
109+
}
110+
111111
fn type_f32(&self) -> &'ll Type {
112112
unsafe {
113113
llvm::LLVMFloatTypeInContext(self.llcx)
@@ -274,121 +274,6 @@ impl Type {
274274
}
275275
}
276276

277-
impl DerivedTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
278-
fn type_bool(&self) -> &'ll Type {
279-
self.type_i8()
280-
}
281-
282-
fn type_i8p(&self) -> &'ll Type {
283-
self.type_ptr_to(self.type_i8())
284-
}
285-
286-
fn type_isize(&self) -> &'ll Type {
287-
self.isize_ty
288-
}
289-
290-
fn type_int(&self) -> &'ll Type {
291-
match &self.sess().target.target.target_c_int_width[..] {
292-
"16" => self.type_i16(),
293-
"32" => self.type_i32(),
294-
"64" => self.type_i64(),
295-
width => bug!("Unsupported target_c_int_width: {}", width),
296-
}
297-
}
298-
299-
fn type_int_from_ty(
300-
&self,
301-
t: ast::IntTy
302-
) -> &'ll Type {
303-
match t {
304-
ast::IntTy::Isize => self.isize_ty,
305-
ast::IntTy::I8 => self.type_i8(),
306-
ast::IntTy::I16 => self.type_i16(),
307-
ast::IntTy::I32 => self.type_i32(),
308-
ast::IntTy::I64 => self.type_i64(),
309-
ast::IntTy::I128 => self.type_i128(),
310-
}
311-
}
312-
313-
fn type_uint_from_ty(
314-
&self,
315-
t: ast::UintTy
316-
) -> &'ll Type {
317-
match t {
318-
ast::UintTy::Usize => self.isize_ty,
319-
ast::UintTy::U8 => self.type_i8(),
320-
ast::UintTy::U16 => self.type_i16(),
321-
ast::UintTy::U32 => self.type_i32(),
322-
ast::UintTy::U64 => self.type_i64(),
323-
ast::UintTy::U128 => self.type_i128(),
324-
}
325-
}
326-
327-
fn type_float_from_ty(
328-
&self,
329-
t: ast::FloatTy
330-
) -> &'ll Type {
331-
match t {
332-
ast::FloatTy::F32 => self.type_f32(),
333-
ast::FloatTy::F64 => self.type_f64(),
334-
}
335-
}
336-
337-
fn type_from_integer(&self, i: layout::Integer) -> &'ll Type {
338-
use rustc::ty::layout::Integer::*;
339-
match i {
340-
I8 => self.type_i8(),
341-
I16 => self.type_i16(),
342-
I32 => self.type_i32(),
343-
I64 => self.type_i64(),
344-
I128 => self.type_i128(),
345-
}
346-
}
347-
348-
fn type_pointee_for_abi_align(&self, align: Align) -> &'ll Type {
349-
// FIXME(eddyb) We could find a better approximation if ity.align < align.
350-
let ity = layout::Integer::approximate_abi_align(self, align);
351-
self.type_from_integer(ity)
352-
}
353-
354-
fn type_padding_filler(
355-
&self,
356-
size: Size,
357-
align: Align
358-
) -> &'ll Type {
359-
let unit = layout::Integer::approximate_abi_align(self, align);
360-
let size = size.bytes();
361-
let unit_size = unit.size().bytes();
362-
assert_eq!(size % unit_size, 0);
363-
self.type_array(self.type_from_integer(unit), size / unit_size)
364-
}
365-
366-
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
367-
rustc_codegen_ssa::common::type_needs_drop(self.tcx(), ty)
368-
}
369-
370-
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
371-
rustc_codegen_ssa::common::type_is_sized(self.tcx(), ty)
372-
}
373-
374-
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
375-
rustc_codegen_ssa::common::type_is_freeze(self.tcx(), ty)
376-
}
377-
378-
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
379-
use syntax_pos::DUMMY_SP;
380-
if ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all()) {
381-
return false;
382-
}
383-
384-
let tail = self.tcx().struct_tail(ty);
385-
match tail.sty {
386-
ty::Foreign(..) => false,
387-
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
388-
_ => bug!("unexpected unsized tail: {:?}", tail.sty),
389-
}
390-
}
391-
}
392277

393278
impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
394279
fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {

src/librustc_codegen_ssa/interfaces/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub trait BackendTypes {
3535
}
3636

3737
pub trait Backend<'tcx>:
38-
BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyLayout = TyLayout<'tcx>>
38+
Sized + BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyLayout = TyLayout<'tcx>>
3939
{
4040
}
4141

src/librustc_codegen_ssa/interfaces/type_.rs

Lines changed: 105 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use super::misc::MiscMethods;
1112
use super::Backend;
1213
use super::HasCodegen;
13-
use common::TypeKind;
14+
use common::{self, TypeKind};
1415
use mir::place::PlaceRef;
15-
use rustc::ty::layout::TyLayout;
16-
use rustc::ty::layout::{self, Align, Size};
17-
use rustc::ty::Ty;
16+
use rustc::ty::layout::{self, Align, Size, TyLayout};
17+
use rustc::ty::{self, Ty};
1818
use rustc::util::nodemap::FxHashMap;
1919
use rustc_target::abi::call::{ArgType, CastTarget, FnType, Reg};
2020
use std::cell::RefCell;
@@ -32,6 +32,7 @@ pub trait BaseTypeMethods<'tcx>: Backend<'tcx> {
3232

3333
// Creates an integer type with the given number of bits, e.g. i24
3434
fn type_ix(&self, num_bits: u64) -> Self::Type;
35+
fn type_isize(&self) -> Self::Type;
3536

3637
fn type_f32(&self) -> Self::Type;
3738
fn type_f64(&self) -> Self::Type;
@@ -61,30 +62,109 @@ pub trait BaseTypeMethods<'tcx>: Backend<'tcx> {
6162
fn scalar_lltypes(&self) -> &RefCell<FxHashMap<Ty<'tcx>, Self::Type>>;
6263
}
6364

64-
pub trait DerivedTypeMethods<'tcx>: Backend<'tcx> {
65-
fn type_bool(&self) -> Self::Type;
66-
fn type_i8p(&self) -> Self::Type;
67-
fn type_isize(&self) -> Self::Type;
68-
fn type_int(&self) -> Self::Type;
69-
fn type_int_from_ty(&self, t: ast::IntTy) -> Self::Type;
70-
fn type_uint_from_ty(&self, t: ast::UintTy) -> Self::Type;
71-
fn type_float_from_ty(&self, t: ast::FloatTy) -> Self::Type;
72-
fn type_from_integer(&self, i: layout::Integer) -> Self::Type;
73-
74-
/// Return a LLVM type that has at most the required alignment,
75-
/// as a conservative approximation for unknown pointee types.
76-
fn type_pointee_for_abi_align(&self, align: Align) -> Self::Type;
65+
pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {
66+
fn type_bool(&self) -> Self::Type {
67+
self.type_i8()
68+
}
69+
70+
fn type_i8p(&self) -> Self::Type {
71+
self.type_ptr_to(self.type_i8())
72+
}
73+
74+
fn type_int(&self) -> Self::Type {
75+
match &self.sess().target.target.target_c_int_width[..] {
76+
"16" => self.type_i16(),
77+
"32" => self.type_i32(),
78+
"64" => self.type_i64(),
79+
width => bug!("Unsupported target_c_int_width: {}", width),
80+
}
81+
}
82+
83+
fn type_int_from_ty(&self, t: ast::IntTy) -> Self::Type {
84+
match t {
85+
ast::IntTy::Isize => self.type_isize(),
86+
ast::IntTy::I8 => self.type_i8(),
87+
ast::IntTy::I16 => self.type_i16(),
88+
ast::IntTy::I32 => self.type_i32(),
89+
ast::IntTy::I64 => self.type_i64(),
90+
ast::IntTy::I128 => self.type_i128(),
91+
}
92+
}
93+
94+
fn type_uint_from_ty(&self, t: ast::UintTy) -> Self::Type {
95+
match t {
96+
ast::UintTy::Usize => self.type_isize(),
97+
ast::UintTy::U8 => self.type_i8(),
98+
ast::UintTy::U16 => self.type_i16(),
99+
ast::UintTy::U32 => self.type_i32(),
100+
ast::UintTy::U64 => self.type_i64(),
101+
ast::UintTy::U128 => self.type_i128(),
102+
}
103+
}
104+
105+
fn type_float_from_ty(&self, t: ast::FloatTy) -> Self::Type {
106+
match t {
107+
ast::FloatTy::F32 => self.type_f32(),
108+
ast::FloatTy::F64 => self.type_f64(),
109+
}
110+
}
111+
112+
fn type_from_integer(&self, i: layout::Integer) -> Self::Type {
113+
use rustc::ty::layout::Integer::*;
114+
match i {
115+
I8 => self.type_i8(),
116+
I16 => self.type_i16(),
117+
I32 => self.type_i32(),
118+
I64 => self.type_i64(),
119+
I128 => self.type_i128(),
120+
}
121+
}
122+
123+
fn type_pointee_for_abi_align(&self, align: Align) -> Self::Type {
124+
// FIXME(eddyb) We could find a better approximation if ity.align < align.
125+
let ity = layout::Integer::approximate_abi_align(self, align);
126+
self.type_from_integer(ity)
127+
}
77128

78129
/// Return a LLVM type that has at most the required alignment,
79130
/// and exactly the required size, as a best-effort padding array.
80-
fn type_padding_filler(&self, size: Size, align: Align) -> Self::Type;
81-
82-
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool;
83-
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool;
84-
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool;
85-
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool;
131+
fn type_padding_filler(&self, size: Size, align: Align) -> Self::Type {
132+
let unit = layout::Integer::approximate_abi_align(self, align);
133+
let size = size.bytes();
134+
let unit_size = unit.size().bytes();
135+
assert_eq!(size % unit_size, 0);
136+
self.type_array(self.type_from_integer(unit), size / unit_size)
137+
}
138+
139+
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
140+
common::type_needs_drop(self.tcx(), ty)
141+
}
142+
143+
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
144+
common::type_is_sized(self.tcx(), ty)
145+
}
146+
147+
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
148+
common::type_is_freeze(self.tcx(), ty)
149+
}
150+
151+
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
152+
use syntax_pos::DUMMY_SP;
153+
if ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all()) {
154+
return false;
155+
}
156+
157+
let tail = self.tcx().struct_tail(ty);
158+
match tail.sty {
159+
ty::Foreign(..) => false,
160+
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
161+
_ => bug!("unexpected unsized tail: {:?}", tail.sty),
162+
}
163+
}
86164
}
87165

166+
impl<T> DerivedTypeMethods<'tcx> for T where Self: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {}
167+
88168
pub trait LayoutTypeMethods<'tcx>: Backend<'tcx> {
89169
fn backend_type(&self, layout: TyLayout<'tcx>) -> Self::Type;
90170
fn cast_backend_type(&self, ty: &CastTarget) -> Self::Type;
@@ -119,11 +199,6 @@ pub trait ArgTypeMethods<'tcx>: HasCodegen<'tcx> {
119199
fn memory_ty(&self, ty: &ArgType<'tcx, Ty<'tcx>>) -> Self::Type;
120200
}
121201

122-
pub trait TypeMethods<'tcx>:
123-
BaseTypeMethods<'tcx> + DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx>
124-
{
125-
}
202+
pub trait TypeMethods<'tcx>: DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx> {}
126203

127-
impl<T> TypeMethods<'tcx> for T where
128-
Self: BaseTypeMethods<'tcx> + DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx>
129-
{}
204+
impl<T> TypeMethods<'tcx> for T where Self: DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx> {}

0 commit comments

Comments
 (0)