rustc_codegen_ssa/traits/
type_.rs1use rustc_abi::{AddressSpace, Float, Integer, Primitive, Reg, Scalar};
2use rustc_middle::bug;
3use rustc_middle::ty::Ty;
4use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, TyAndLayout};
5use rustc_target::callconv::{ArgAbi, CastTarget, FnAbi};
6
7use super::BackendTypes;
8use super::misc::MiscCodegenMethods;
9use crate::common::TypeKind;
10use crate::mir::place::PlaceRef;
11
12pub trait BaseTypeCodegenMethods: BackendTypes {
13 fn type_i8(&self) -> Self::Type;
14 fn type_i16(&self) -> Self::Type;
15 fn type_i32(&self) -> Self::Type;
16 fn type_i64(&self) -> Self::Type;
17 fn type_i128(&self) -> Self::Type;
18 fn type_isize(&self) -> Self::Type;
19
20 fn type_f16(&self) -> Self::Type;
21 fn type_f32(&self) -> Self::Type;
22 fn type_f64(&self) -> Self::Type;
23 fn type_f128(&self) -> Self::Type;
24
25 fn type_array(&self, ty: Self::Type, len: u64) -> Self::Type;
26 fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type;
27 fn type_kind(&self, ty: Self::Type) -> TypeKind;
28 fn type_ptr(&self) -> Self::Type;
29 fn type_ptr_ext(&self, address_space: AddressSpace) -> Self::Type;
30 fn element_type(&self, ty: Self::Type) -> Self::Type;
31
32 fn vector_length(&self, ty: Self::Type) -> usize;
34
35 fn float_width(&self, ty: Self::Type) -> usize;
36
37 fn int_width(&self, ty: Self::Type) -> u64;
39
40 fn val_ty(&self, v: Self::Value) -> Self::Type;
41}
42
43pub trait DerivedTypeCodegenMethods<'tcx>:
44 BaseTypeCodegenMethods + MiscCodegenMethods<'tcx> + HasTyCtxt<'tcx> + HasTypingEnv<'tcx>
45{
46 fn type_int(&self) -> Self::Type {
47 match &self.sess().target.c_int_width {
48 16 => self.type_i16(),
49 32 => self.type_i32(),
50 64 => self.type_i64(),
51 width => bug!("Unsupported c_int_width: {}", width),
52 }
53 }
54
55 fn type_from_integer(&self, i: Integer) -> Self::Type {
56 use Integer::*;
57 match i {
58 I8 => self.type_i8(),
59 I16 => self.type_i16(),
60 I32 => self.type_i32(),
61 I64 => self.type_i64(),
62 I128 => self.type_i128(),
63 }
64 }
65
66 fn type_from_float(&self, f: Float) -> Self::Type {
67 use Float::*;
68 match f {
69 F16 => self.type_f16(),
70 F32 => self.type_f32(),
71 F64 => self.type_f64(),
72 F128 => self.type_f128(),
73 }
74 }
75
76 fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
77 ty.needs_drop(self.tcx(), self.typing_env())
78 }
79
80 fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
81 ty.is_sized(self.tcx(), self.typing_env())
82 }
83
84 fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
85 ty.is_freeze(self.tcx(), self.typing_env())
86 }
87
88 fn type_from_primitive(&self, p: Primitive) -> Self::Type {
89 use Primitive::*;
90 match p {
91 Int(i, _) => self.type_from_integer(i),
92 Float(f) => self.type_from_float(f),
93 Pointer(address_space) => self.type_ptr_ext(address_space),
94 }
95 }
96
97 fn type_from_scalar(&self, s: Scalar) -> Self::Type {
98 self.type_from_primitive(s.primitive())
104 }
105}
106
107impl<'tcx, T> DerivedTypeCodegenMethods<'tcx> for T where
108 Self: BaseTypeCodegenMethods + MiscCodegenMethods<'tcx> + HasTyCtxt<'tcx> + HasTypingEnv<'tcx>
109{
110}
111
112pub trait LayoutTypeCodegenMethods<'tcx>: BackendTypes {
113 fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type;
116 fn cast_backend_type(&self, ty: &CastTarget) -> Self::Type;
117 fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type;
118 fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type;
119 fn reg_backend_type(&self, ty: &Reg) -> Self::Type;
120 fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type;
130 fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool;
131 fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool;
132 fn scalar_pair_element_backend_type(
133 &self,
134 layout: TyAndLayout<'tcx>,
135 index: usize,
136 immediate: bool,
137 ) -> Self::Type;
138
139 fn is_backend_ref(&self, layout: TyAndLayout<'tcx>) -> bool {
148 !(layout.is_zst()
149 || self.is_backend_immediate(layout)
150 || self.is_backend_scalar_pair(layout))
151 }
152}
153
154pub trait TypeMembershipCodegenMethods<'tcx>: BackendTypes {
157 fn add_type_metadata(&self, _function: Self::Function, _typeid: String) {}
158 fn set_type_metadata(&self, _function: Self::Function, _typeid: String) {}
159 fn typeid_metadata(&self, _typeid: String) -> Option<Self::Metadata> {
160 None
161 }
162 fn add_kcfi_type_metadata(&self, _function: Self::Function, _typeid: u32) {}
163 fn set_kcfi_type_metadata(&self, _function: Self::Function, _typeid: u32) {}
164}
165
166pub trait ArgAbiBuilderMethods<'tcx>: BackendTypes {
167 fn store_fn_arg(
168 &mut self,
169 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
170 idx: &mut usize,
171 dst: PlaceRef<'tcx, Self::Value>,
172 );
173 fn store_arg(
174 &mut self,
175 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
176 val: Self::Value,
177 dst: PlaceRef<'tcx, Self::Value>,
178 );
179}
180
181pub trait TypeCodegenMethods<'tcx> = DerivedTypeCodegenMethods<'tcx>
182 + LayoutTypeCodegenMethods<'tcx>
183 + TypeMembershipCodegenMethods<'tcx>;