1use std::borrow::Borrow;
2use std::hash::{Hash, Hasher};
3use std::{fmt, ptr};
4
5use libc::c_uint;
6use rustc_abi::{AddressSpace, Align, Integer, Reg, Size};
7use rustc_codegen_ssa::common::TypeKind;
8use rustc_codegen_ssa::traits::*;
9use rustc_data_structures::small_c_str::SmallCStr;
10use rustc_middle::bug;
11use rustc_middle::ty::layout::TyAndLayout;
12use rustc_middle::ty::{self, Ty};
13use rustc_target::callconv::{CastTarget, FnAbi};
14
15use crate::abi::{FnAbiLlvmExt, LlvmType};
16use crate::context::{CodegenCx, GenericCx, SCx};
17pub(crate) use crate::llvm::Type;
18use crate::llvm::{FALSE, Metadata, TRUE, ToLlvmBool};
19use crate::type_of::LayoutLlvmExt;
20use crate::value::Value;
21use crate::{common, llvm};
22
23impl PartialEq for Type {
24 fn eq(&self, other: &Self) -> bool {
25 ptr::eq(self, other)
26 }
27}
28
29impl Eq for Type {}
30
31impl Hash for Type {
32 fn hash<H: Hasher>(&self, state: &mut H) {
33 ptr::hash(self, state);
34 }
35}
36
37impl fmt::Debug for Type {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str(
40 &llvm::build_string(|s| unsafe {
41 llvm::LLVMRustWriteTypeToString(self, s);
42 })
43 .expect("non-UTF8 type description from LLVM"),
44 )
45 }
46}
47
48impl<'ll> CodegenCx<'ll, '_> {}
49impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
50 pub(crate) fn type_named_struct(&self, name: &str) -> &'ll Type {
51 let name = SmallCStr::new(name);
52 unsafe { llvm::LLVMStructCreateNamed(self.llcx(), name.as_ptr()) }
53 }
54
55 pub(crate) fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
56 unsafe {
57 llvm::LLVMStructSetBody(ty, els.as_ptr(), els.len() as c_uint, packed.to_llvm_bool())
58 }
59 }
60 pub(crate) fn type_void(&self) -> &'ll Type {
61 unsafe { llvm::LLVMVoidTypeInContext(self.llcx()) }
62 }
63
64 pub(crate) fn type_ix(&self, num_bits: u64) -> &'ll Type {
66 unsafe { llvm::LLVMIntTypeInContext(self.llcx(), num_bits as c_uint) }
67 }
68
69 pub(crate) fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
70 unsafe { llvm::LLVMVectorType(ty, len as c_uint) }
71 }
72
73 pub(crate) fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
74 unsafe {
75 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
76 let mut args = Vec::with_capacity(n_args);
77 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
78 args.set_len(n_args);
79 args
80 }
81 }
82}
83impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
84 pub(crate) fn type_bool(&self) -> &'ll Type {
85 self.type_i8()
86 }
87
88 pub(crate) fn type_int_from_ty(&self, t: ty::IntTy) -> &'ll Type {
89 match t {
90 ty::IntTy::Isize => self.type_isize(),
91 ty::IntTy::I8 => self.type_i8(),
92 ty::IntTy::I16 => self.type_i16(),
93 ty::IntTy::I32 => self.type_i32(),
94 ty::IntTy::I64 => self.type_i64(),
95 ty::IntTy::I128 => self.type_i128(),
96 }
97 }
98
99 pub(crate) fn type_uint_from_ty(&self, t: ty::UintTy) -> &'ll Type {
100 match t {
101 ty::UintTy::Usize => self.type_isize(),
102 ty::UintTy::U8 => self.type_i8(),
103 ty::UintTy::U16 => self.type_i16(),
104 ty::UintTy::U32 => self.type_i32(),
105 ty::UintTy::U64 => self.type_i64(),
106 ty::UintTy::U128 => self.type_i128(),
107 }
108 }
109
110 pub(crate) fn type_float_from_ty(&self, t: ty::FloatTy) -> &'ll Type {
111 match t {
112 ty::FloatTy::F16 => self.type_f16(),
113 ty::FloatTy::F32 => self.type_f32(),
114 ty::FloatTy::F64 => self.type_f64(),
115 ty::FloatTy::F128 => self.type_f128(),
116 }
117 }
118
119 pub(crate) fn type_padding_filler(&self, size: Size, align: Align) -> &'ll Type {
122 let unit = Integer::approximate_align(self, align);
123 let size = size.bytes();
124 let unit_size = unit.size().bytes();
125 assert_eq!(size % unit_size, 0);
126 self.type_array(self.type_from_integer(unit), size / unit_size)
127 }
128}
129
130impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
131 pub(crate) fn llcx(&self) -> &'ll llvm::Context {
132 (**self).borrow().llcx
133 }
134
135 pub(crate) fn llmod(&self) -> &'ll llvm::Module {
136 (**self).borrow().llmod
137 }
138
139 pub(crate) fn isize_ty(&self) -> &'ll Type {
140 (**self).borrow().isize_ty
141 }
142
143 pub(crate) fn type_variadic_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
144 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, TRUE) }
145 }
146
147 pub(crate) fn type_i1(&self) -> &'ll Type {
148 unsafe { llvm::LLVMInt1TypeInContext(self.llcx()) }
149 }
150
151 pub(crate) fn type_struct(&self, els: &[&'ll Type], packed: bool) -> &'ll Type {
152 unsafe {
153 llvm::LLVMStructTypeInContext(
154 self.llcx(),
155 els.as_ptr(),
156 els.len() as c_uint,
157 packed.to_llvm_bool(),
158 )
159 }
160 }
161}
162
163impl<'ll, CX: Borrow<SCx<'ll>>> BaseTypeCodegenMethods for GenericCx<'ll, CX> {
164 fn type_i8(&self) -> &'ll Type {
165 unsafe { llvm::LLVMInt8TypeInContext(self.llcx()) }
166 }
167
168 fn type_i16(&self) -> &'ll Type {
169 unsafe { llvm::LLVMInt16TypeInContext(self.llcx()) }
170 }
171
172 fn type_i32(&self) -> &'ll Type {
173 unsafe { llvm::LLVMInt32TypeInContext(self.llcx()) }
174 }
175
176 fn type_i64(&self) -> &'ll Type {
177 unsafe { llvm::LLVMInt64TypeInContext(self.llcx()) }
178 }
179
180 fn type_i128(&self) -> &'ll Type {
181 unsafe { llvm::LLVMIntTypeInContext(self.llcx(), 128) }
182 }
183
184 fn type_isize(&self) -> &'ll Type {
185 self.isize_ty()
186 }
187
188 fn type_f16(&self) -> &'ll Type {
189 unsafe { llvm::LLVMHalfTypeInContext(self.llcx()) }
190 }
191
192 fn type_f32(&self) -> &'ll Type {
193 unsafe { llvm::LLVMFloatTypeInContext(self.llcx()) }
194 }
195
196 fn type_f64(&self) -> &'ll Type {
197 unsafe { llvm::LLVMDoubleTypeInContext(self.llcx()) }
198 }
199
200 fn type_f128(&self) -> &'ll Type {
201 unsafe { llvm::LLVMFP128TypeInContext(self.llcx()) }
202 }
203
204 fn type_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
205 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, FALSE) }
206 }
207
208 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
209 llvm::LLVMGetTypeKind(ty).to_rust().to_generic()
210 }
211
212 fn type_ptr(&self) -> &'ll Type {
213 self.type_ptr_ext(AddressSpace::ZERO)
214 }
215
216 fn type_ptr_ext(&self, address_space: AddressSpace) -> &'ll Type {
217 unsafe { llvm::LLVMPointerTypeInContext(self.llcx(), address_space.0) }
218 }
219
220 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
221 match self.type_kind(ty) {
222 TypeKind::Array | TypeKind::Vector => unsafe { llvm::LLVMGetElementType(ty) },
223 TypeKind::Pointer => bug!("element_type is not supported for opaque pointers"),
224 other => bug!("element_type called on unsupported type {other:?}"),
225 }
226 }
227
228 fn vector_length(&self, ty: &'ll Type) -> usize {
229 unsafe { llvm::LLVMGetVectorSize(ty) as usize }
230 }
231
232 fn float_width(&self, ty: &'ll Type) -> usize {
233 match self.type_kind(ty) {
234 TypeKind::Half => 16,
235 TypeKind::Float => 32,
236 TypeKind::Double => 64,
237 TypeKind::X86_FP80 => 80,
238 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
239 other => bug!("llvm_float_width called on a non-float type {other:?}"),
240 }
241 }
242
243 fn int_width(&self, ty: &'ll Type) -> u64 {
244 unsafe { llvm::LLVMGetIntTypeWidth(ty) as u64 }
245 }
246
247 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
248 common::val_ty(v)
249 }
250
251 fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
252 unsafe { llvm::LLVMArrayType2(ty, len) }
253 }
254}
255
256impl Type {
257 pub(crate) fn ix_llcx(llcx: &llvm::Context, num_bits: u64) -> &Type {
259 unsafe { llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint) }
260 }
261
262 pub(crate) fn ptr_llcx(llcx: &llvm::Context) -> &Type {
263 unsafe { llvm::LLVMPointerTypeInContext(llcx, AddressSpace::ZERO.0) }
264 }
265}
266
267impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
268 fn backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
269 layout.llvm_type(self)
270 }
271 fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
272 layout.immediate_llvm_type(self)
273 }
274 fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
275 layout.is_llvm_immediate()
276 }
277 fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
278 layout.is_llvm_scalar_pair()
279 }
280 fn scalar_pair_element_backend_type(
281 &self,
282 layout: TyAndLayout<'tcx>,
283 index: usize,
284 immediate: bool,
285 ) -> &'ll Type {
286 layout.scalar_pair_element_llvm_type(self, index, immediate)
287 }
288 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
289 ty.llvm_type(self)
290 }
291 fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
292 fn_abi.llvm_type(self)
293 }
294 fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
295 fn_abi.ptr_to_llvm_type(self)
296 }
297 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {
298 ty.llvm_type(self)
299 }
300}
301
302impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
303 fn add_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
304 let typeid_metadata = self.create_metadata(typeid);
305 unsafe {
306 let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
307 llvm::LLVMRustGlobalAddMetadata(
308 function,
309 llvm::MD_type as c_uint,
310 llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
311 )
312 }
313 }
314
315 fn set_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
316 let typeid_metadata = self.create_metadata(typeid);
317 unsafe {
318 let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
319 llvm::LLVMGlobalSetMetadata(
320 function,
321 llvm::MD_type as c_uint,
322 llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
323 )
324 }
325 }
326
327 fn typeid_metadata(&self, typeid: &[u8]) -> Option<&'ll Metadata> {
328 Some(self.create_metadata(typeid))
329 }
330
331 fn add_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
332 let kcfi_type_metadata = self.const_u32(kcfi_typeid);
333 unsafe {
334 llvm::LLVMRustGlobalAddMetadata(
335 function,
336 llvm::MD_kcfi_type as c_uint,
337 llvm::LLVMMDNodeInContext2(
338 self.llcx,
339 &llvm::LLVMValueAsMetadata(kcfi_type_metadata),
340 1,
341 ),
342 )
343 }
344 }
345
346 fn set_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
347 let kcfi_type_metadata = self.const_u32(kcfi_typeid);
348 unsafe {
349 llvm::LLVMGlobalSetMetadata(
350 function,
351 llvm::MD_kcfi_type as c_uint,
352 llvm::LLVMMDNodeInContext2(
353 self.llcx,
354 &llvm::LLVMValueAsMetadata(kcfi_type_metadata),
355 1,
356 ),
357 )
358 }
359 }
360}