rustc_codegen_ssa/traits/
type_.rs

1use 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    /// Returns the number of elements in `self` if it is an LLVM vector type.
33    fn vector_length(&self, ty: Self::Type) -> usize;
34
35    fn float_width(&self, ty: Self::Type) -> usize;
36
37    /// Retrieves the bit width of the integer type `self`.
38    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        // `MaybeUninit` being `repr(transparent)` somewhat implies that the type
99        // of a scalar has to be the type of its primitive (which is true in LLVM,
100        // where noundef is a parameter attribute or metadata) but if we ever get
101        // a backend where that's no longer true, every use of this will need to
102        // to carefully scrutinized and re-evaluated.
103        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    /// The backend type used for a rust type when it's in memory,
114    /// such as when it's stack-allocated or when it's being loaded or stored.
115    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    /// The backend type used for a rust type when it's in an SSA register.
121    ///
122    /// For nearly all types this is the same as the [`Self::backend_type`], however
123    /// `bool` (and other `0`-or-`1` values) are kept as `i1` in registers but as
124    /// [`BaseTypeCodegenMethods::type_i8`] in memory.
125    ///
126    /// Converting values between the two different backend types is done using
127    /// [`from_immediate`](super::BuilderMethods::from_immediate) and
128    /// [`to_immediate_scalar`](super::BuilderMethods::to_immediate_scalar).
129    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    /// A type that produces an [`OperandValue::Ref`] when loaded.
140    ///
141    /// AKA one that's not a ZST, not `is_backend_immediate`, and
142    /// not `is_backend_scalar_pair`. For such a type, a
143    /// [`load_operand`] doesn't actually `load` anything.
144    ///
145    /// [`OperandValue::Ref`]: crate::mir::operand::OperandValue::Ref
146    /// [`load_operand`]: super::BuilderMethods::load_operand
147    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
154// For backends that support CFI using type membership (i.e., testing whether a given pointer is
155// associated with a type identifier).
156pub 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>;