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