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.
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.
11 use super::backend::Backend;
12 use super::HasCodegen;
13 use mir::place::PlaceRef;
14 use rustc::ty::layout::TyLayout;
15 use rustc::ty::layout::{self, Align, Size};
17 use rustc::util::nodemap::FxHashMap;
18 use rustc_codegen_utils::common::TypeKind;
19 use rustc_target::abi::call::{ArgType, CastTarget, FnType, Reg};
20 use std::cell::RefCell;
23 pub trait BaseTypeMethods<'tcx>: Backend<'tcx> {
24 fn type_void(&self) -> Self::Type;
25 fn type_metadata(&self) -> Self::Type;
26 fn type_i1(&self) -> Self::Type;
27 fn type_i8(&self) -> Self::Type;
28 fn type_i16(&self) -> Self::Type;
29 fn type_i32(&self) -> Self::Type;
30 fn type_i64(&self) -> Self::Type;
31 fn type_i128(&self) -> Self::Type;
33 // Creates an integer type with the given number of bits, e.g. i24
34 fn type_ix(&self, num_bits: u64) -> Self::Type;
36 fn type_f32(&self) -> Self::Type;
37 fn type_f64(&self) -> Self::Type;
38 fn type_x86_mmx(&self) -> Self::Type;
40 fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type;
41 fn type_variadic_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type;
42 fn type_struct(&self, els: &[Self::Type], packed: bool) -> Self::Type;
43 fn type_named_struct(&self, name: &str) -> Self::Type;
44 fn type_array(&self, ty: Self::Type, len: u64) -> Self::Type;
45 fn type_vector(&self, ty: Self::Type, len: u64) -> Self::Type;
46 fn type_kind(&self, ty: Self::Type) -> TypeKind;
47 fn set_struct_body(&self, ty: Self::Type, els: &[Self::Type], packed: bool);
48 fn type_ptr_to(&self, ty: Self::Type) -> Self::Type;
49 fn element_type(&self, ty: Self::Type) -> Self::Type;
51 /// Return the number of elements in `self` if it is a LLVM vector type.
52 fn vector_length(&self, ty: Self::Type) -> usize;
54 fn func_params_types(&self, ty: Self::Type) -> Vec<Self::Type>;
55 fn float_width(&self, ty: Self::Type) -> usize;
57 /// Retrieve the bit width of the integer type `self`.
58 fn int_width(&self, ty: Self::Type) -> u64;
60 fn val_ty(&self, v: Self::Value) -> Self::Type;
61 fn scalar_lltypes(&self) -> &RefCell<FxHashMap<Ty<'tcx>, Self::Type>>;
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;
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;
78 /// Return a LLVM type that has at most the required alignment,
79 /// and exactly the required size, as a best-effort padding array.
80 fn type_padding_filler(&self, size: Size, align: Align) -> Self::Type;
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;
88 pub trait LayoutTypeMethods<'tcx>: Backend<'tcx> {
89 fn backend_type(&self, layout: TyLayout<'tcx>) -> Self::Type;
90 fn cast_backend_type(&self, ty: &CastTarget) -> Self::Type;
91 fn fn_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> Self::Type;
92 fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> Self::Type;
93 fn reg_backend_type(&self, ty: &Reg) -> Self::Type;
94 fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> Self::Type;
95 fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool;
96 fn scalar_pair_element_backend_type<'a>(
98 layout: TyLayout<'tcx>,
104 pub trait ArgTypeMethods<'tcx>: HasCodegen<'tcx> {
107 ty: &ArgType<'tcx, Ty<'tcx>>,
109 dst: PlaceRef<'tcx, Self::Value>,
113 ty: &ArgType<'tcx, Ty<'tcx>>,
115 dst: PlaceRef<'tcx, Self::Value>,
117 fn memory_ty(&self, ty: &ArgType<'tcx, Ty<'tcx>>) -> Self::Type;
120 pub trait TypeMethods<'tcx>:
121 BaseTypeMethods<'tcx> + DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx>
125 impl<T> TypeMethods<'tcx> for T where
126 Self: BaseTypeMethods<'tcx> + DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx>