]> git.proxmox.com Git - rustc.git/blame - src/librustc_codegen_ssa/traits/type_.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_codegen_ssa / traits / type_.rs
CommitLineData
a1dfa0c6
XL
1use super::misc::MiscMethods;
2use super::Backend;
3use super::HasCodegen;
532ac7d7 4use crate::common::TypeKind;
9fa01778 5use crate::mir::place::PlaceRef;
ba9703b0
XL
6use rustc_middle::ty::layout::TyAndLayout;
7use rustc_middle::ty::{self, Ty};
dfeec247 8use rustc_span::DUMMY_SP;
60c5eb7d 9use rustc_target::abi::call::{ArgAbi, CastTarget, FnAbi, Reg};
ba9703b0 10use rustc_target::abi::Integer;
a1dfa0c6
XL
11
12// This depends on `Backend` and not `BackendTypes`, because consumers will probably want to use
13// `LayoutOf` or `HasTyCtxt`. This way, they don't have to add a constraint on it themselves.
14pub trait BaseTypeMethods<'tcx>: Backend<'tcx> {
a1dfa0c6
XL
15 fn type_i1(&self) -> Self::Type;
16 fn type_i8(&self) -> Self::Type;
17 fn type_i16(&self) -> Self::Type;
18 fn type_i32(&self) -> Self::Type;
19 fn type_i64(&self) -> Self::Type;
20 fn type_i128(&self) -> Self::Type;
a1dfa0c6
XL
21 fn type_isize(&self) -> Self::Type;
22
23 fn type_f32(&self) -> Self::Type;
24 fn type_f64(&self) -> Self::Type;
a1dfa0c6
XL
25
26 fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type;
a1dfa0c6 27 fn type_struct(&self, els: &[Self::Type], packed: bool) -> Self::Type;
a1dfa0c6
XL
28 fn type_kind(&self, ty: Self::Type) -> TypeKind;
29 fn type_ptr_to(&self, ty: Self::Type) -> Self::Type;
30 fn element_type(&self, ty: Self::Type) -> Self::Type;
31
9fa01778 32 /// Returns the number of elements in `self` if it is a LLVM vector type.
a1dfa0c6
XL
33 fn vector_length(&self, ty: Self::Type) -> usize;
34
a1dfa0c6
XL
35 fn float_width(&self, ty: Self::Type) -> usize;
36
9fa01778 37 /// Retrieves the bit width of the integer type `self`.
a1dfa0c6
XL
38 fn int_width(&self, ty: Self::Type) -> u64;
39
40 fn val_ty(&self, v: Self::Value) -> Self::Type;
a1dfa0c6
XL
41}
42
43pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {
a1dfa0c6
XL
44 fn type_i8p(&self) -> Self::Type {
45 self.type_ptr_to(self.type_i8())
46 }
47
48 fn type_int(&self) -> Self::Type {
49 match &self.sess().target.target.target_c_int_width[..] {
50 "16" => self.type_i16(),
51 "32" => self.type_i32(),
52 "64" => self.type_i64(),
53 width => bug!("Unsupported target_c_int_width: {}", width),
54 }
55 }
56
ba9703b0
XL
57 fn type_from_integer(&self, i: Integer) -> Self::Type {
58 use Integer::*;
a1dfa0c6
XL
59 match i {
60 I8 => self.type_i8(),
61 I16 => self.type_i16(),
62 I32 => self.type_i32(),
63 I64 => self.type_i64(),
64 I128 => self.type_i128(),
65 }
66 }
67
a1dfa0c6 68 fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
532ac7d7 69 ty.needs_drop(self.tcx(), ty::ParamEnv::reveal_all())
a1dfa0c6
XL
70 }
71
72 fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
532ac7d7 73 ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all())
a1dfa0c6
XL
74 }
75
76 fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
532ac7d7 77 ty.is_freeze(self.tcx(), ty::ParamEnv::reveal_all(), DUMMY_SP)
a1dfa0c6
XL
78 }
79
80 fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
416331ca
XL
81 let param_env = ty::ParamEnv::reveal_all();
82 if ty.is_sized(self.tcx().at(DUMMY_SP), param_env) {
a1dfa0c6
XL
83 return false;
84 }
85
416331ca 86 let tail = self.tcx().struct_tail_erasing_lifetimes(ty, param_env);
e74abb32 87 match tail.kind {
a1dfa0c6
XL
88 ty::Foreign(..) => false,
89 ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
532ac7d7 90 _ => bug!("unexpected unsized tail: {:?}", tail),
a1dfa0c6
XL
91 }
92 }
93}
94
95impl<T> DerivedTypeMethods<'tcx> for T where Self: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {}
96
97pub trait LayoutTypeMethods<'tcx>: Backend<'tcx> {
ba9703b0 98 fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type;
a1dfa0c6 99 fn cast_backend_type(&self, ty: &CastTarget) -> Self::Type;
60c5eb7d 100 fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type;
a1dfa0c6 101 fn reg_backend_type(&self, ty: &Reg) -> Self::Type;
ba9703b0
XL
102 fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type;
103 fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool;
104 fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool;
105 fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64;
dc9dc135 106 fn scalar_pair_element_backend_type(
a1dfa0c6 107 &self,
ba9703b0 108 layout: TyAndLayout<'tcx>,
a1dfa0c6
XL
109 index: usize,
110 immediate: bool,
111 ) -> Self::Type;
112}
113
60c5eb7d 114pub trait ArgAbiMethods<'tcx>: HasCodegen<'tcx> {
a1dfa0c6
XL
115 fn store_fn_arg(
116 &mut self,
60c5eb7d 117 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
a1dfa0c6
XL
118 idx: &mut usize,
119 dst: PlaceRef<'tcx, Self::Value>,
120 );
60c5eb7d 121 fn store_arg(
a1dfa0c6 122 &mut self,
60c5eb7d 123 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
a1dfa0c6
XL
124 val: Self::Value,
125 dst: PlaceRef<'tcx, Self::Value>,
126 );
60c5eb7d 127 fn arg_memory_ty(&self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>) -> Self::Type;
a1dfa0c6
XL
128}
129
130pub trait TypeMethods<'tcx>: DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx> {}
131
132impl<T> TypeMethods<'tcx> for T where Self: DerivedTypeMethods<'tcx> + LayoutTypeMethods<'tcx> {}