1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
4 use crate::require_same_types;
6 use rustc::traits::{ObligationCause, ObligationCauseCode};
7 use rustc::ty::subst::Subst;
8 use rustc::ty::{self, Ty, TyCtxt};
9 use rustc_errors::struct_span_err;
11 use rustc_span::symbol::Symbol;
12 use rustc_target::spec::abi::Abi;
16 fn equate_intrinsic_type<'tcx>(
18 it: &hir::ForeignItem<'_>,
21 safety: hir::Unsafety,
22 inputs: Vec<Ty<'tcx>>,
25 let def_id = tcx.hir().local_def_id(it.hir_id);
28 hir::ForeignItemKind::Fn(..) => {}
30 struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
31 .span_label(it.span, "expected a function")
37 let i_n_tps = tcx.generics_of(def_id).own_counts().types;
39 let span = match it.kind {
40 hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
48 "intrinsic has wrong number of type \
49 parameters: found {}, expected {}",
53 .span_label(span, format!("expected {} type parameter", n_tps))
58 let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
65 let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType);
66 require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
69 /// Returns `true` if the given intrinsic is unsafe to call or not.
70 pub fn intrinsic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
72 "size_of" | "min_align_of" | "needs_drop" | "caller_location" | "size_of_val"
73 | "min_align_of_val" | "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow"
74 | "wrapping_add" | "wrapping_sub" | "wrapping_mul" | "saturating_add"
75 | "saturating_sub" | "rotate_left" | "rotate_right" | "ctpop" | "ctlz" | "cttz"
76 | "bswap" | "bitreverse" | "discriminant_value" | "type_id" | "likely" | "unlikely"
77 | "minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64" | "type_name" => {
80 _ => hir::Unsafety::Unsafe,
84 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
85 /// and in libcore/intrinsics.rs
86 pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
87 let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)));
88 let name = it.ident.as_str();
90 let mk_va_list_ty = |mutbl| {
91 tcx.lang_items().va_list().map(|did| {
92 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
93 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
94 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
96 tcx.mk_ref(tcx.mk_region(env_region), ty::TypeAndMut { ty: va_list_ty, mutbl }),
102 let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
103 let split: Vec<&str> = name.split('_').collect();
104 assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
106 //We only care about the operation here
107 let (n_tps, inputs, output) = match split[1] {
108 "cxchg" | "cxchgweak" => (
110 vec![tcx.mk_mut_ptr(param(0)), param(0), param(0)],
111 tcx.intern_tup(&[param(0), tcx.types.bool]),
113 "load" => (1, vec![tcx.mk_imm_ptr(param(0))], param(0)),
114 "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
116 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" | "min" | "umax"
117 | "umin" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], param(0)),
118 "fence" | "singlethreadfence" => (0, Vec::new(), tcx.mk_unit()),
124 "unrecognized atomic operation function: `{}`",
127 .span_label(it.span, "unrecognized atomic operation")
132 (n_tps, inputs, output, hir::Unsafety::Unsafe)
133 } else if &name[..] == "abort" || &name[..] == "unreachable" {
134 (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
136 let unsafety = intrinsic_operation_unsafety(&name[..]);
137 let (n_tps, inputs, output) = match &name[..] {
138 "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
139 "size_of" | "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
140 "size_of_val" | "min_align_of_val" => (
143 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
148 "rustc_peek" => (1, vec![param(0)], param(0)),
149 "caller_location" => (0, vec![], tcx.caller_location_ty()),
150 "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
151 "init" => (1, Vec::new(), param(0)),
152 "uninit" => (1, Vec::new(), param(0)),
153 "forget" => (1, vec![param(0)], tcx.mk_unit()),
154 "transmute" => (2, vec![param(0)], param(1)),
155 "move_val_init" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
157 | "prefetch_write_data"
158 | "prefetch_read_instruction"
159 | "prefetch_write_instruction" => (
162 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
167 "drop_in_place" => (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit()),
168 "needs_drop" => (1, Vec::new(), tcx.types.bool),
170 "type_name" => (1, Vec::new(), tcx.mk_static_str()),
171 "type_id" => (1, Vec::new(), tcx.types.u64),
172 "offset" | "arith_offset" => (
175 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
178 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
180 "copy" | "copy_nonoverlapping" => (
183 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
184 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
189 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => (
192 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
193 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
198 "write_bytes" | "volatile_set_memory" => (
201 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
207 "sqrtf32" => (0, vec![tcx.types.f32], tcx.types.f32),
208 "sqrtf64" => (0, vec![tcx.types.f64], tcx.types.f64),
209 "powif32" => (0, vec![tcx.types.f32, tcx.types.i32], tcx.types.f32),
210 "powif64" => (0, vec![tcx.types.f64, tcx.types.i32], tcx.types.f64),
211 "sinf32" => (0, vec![tcx.types.f32], tcx.types.f32),
212 "sinf64" => (0, vec![tcx.types.f64], tcx.types.f64),
213 "cosf32" => (0, vec![tcx.types.f32], tcx.types.f32),
214 "cosf64" => (0, vec![tcx.types.f64], tcx.types.f64),
215 "powf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
216 "powf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
217 "expf32" => (0, vec![tcx.types.f32], tcx.types.f32),
218 "expf64" => (0, vec![tcx.types.f64], tcx.types.f64),
219 "exp2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
220 "exp2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
221 "logf32" => (0, vec![tcx.types.f32], tcx.types.f32),
222 "logf64" => (0, vec![tcx.types.f64], tcx.types.f64),
223 "log10f32" => (0, vec![tcx.types.f32], tcx.types.f32),
224 "log10f64" => (0, vec![tcx.types.f64], tcx.types.f64),
225 "log2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
226 "log2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
227 "fmaf32" => (0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32),
228 "fmaf64" => (0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64),
229 "fabsf32" => (0, vec![tcx.types.f32], tcx.types.f32),
230 "fabsf64" => (0, vec![tcx.types.f64], tcx.types.f64),
231 "minnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
232 "minnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
233 "maxnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
234 "maxnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
235 "copysignf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
236 "copysignf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
237 "floorf32" => (0, vec![tcx.types.f32], tcx.types.f32),
238 "floorf64" => (0, vec![tcx.types.f64], tcx.types.f64),
239 "ceilf32" => (0, vec![tcx.types.f32], tcx.types.f32),
240 "ceilf64" => (0, vec![tcx.types.f64], tcx.types.f64),
241 "truncf32" => (0, vec![tcx.types.f32], tcx.types.f32),
242 "truncf64" => (0, vec![tcx.types.f64], tcx.types.f64),
243 "rintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
244 "rintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
245 "nearbyintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
246 "nearbyintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
247 "roundf32" => (0, vec![tcx.types.f32], tcx.types.f32),
248 "roundf64" => (0, vec![tcx.types.f64], tcx.types.f64),
250 "volatile_load" | "unaligned_volatile_load" => {
251 (1, vec![tcx.mk_imm_ptr(param(0))], param(0))
253 "volatile_store" | "unaligned_volatile_store" => {
254 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit())
257 "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | "bswap"
258 | "bitreverse" => (1, vec![param(0)], param(0)),
260 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
261 (1, vec![param(0), param(0)], tcx.intern_tup(&[param(0), tcx.types.bool]))
264 "ptr_offset_from" => {
265 (1, vec![tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0))], tcx.types.isize)
267 "unchecked_div" | "unchecked_rem" | "exact_div" => {
268 (1, vec![param(0), param(0)], param(0))
270 "unchecked_shl" | "unchecked_shr" | "rotate_left" | "rotate_right" => {
271 (1, vec![param(0), param(0)], param(0))
273 "unchecked_add" | "unchecked_sub" | "unchecked_mul" => {
274 (1, vec![param(0), param(0)], param(0))
276 "wrapping_add" | "wrapping_sub" | "wrapping_mul" => {
277 (1, vec![param(0), param(0)], param(0))
279 "saturating_add" | "saturating_sub" => (1, vec![param(0), param(0)], param(0)),
280 "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => {
281 (1, vec![param(0), param(0)], param(0))
283 "float_to_int_approx_unchecked" => (2, vec![param(0)], param(1)),
285 "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
286 "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
287 "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
289 "discriminant_value" => (
292 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
299 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
300 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
304 hir::Unsafety::Normal,
307 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
310 "va_start" | "va_end" => match mk_va_list_ty(hir::Mutability::Mut) {
311 Some((va_list_ref_ty, _)) => (0, vec![va_list_ref_ty], tcx.mk_unit()),
312 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
315 "va_copy" => match mk_va_list_ty(hir::Mutability::Not) {
316 Some((va_list_ref_ty, va_list_ty)) => {
317 let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
318 (0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
320 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
323 "va_arg" => match mk_va_list_ty(hir::Mutability::Mut) {
324 Some((va_list_ref_ty, _)) => (1, vec![va_list_ref_ty], param(0)),
325 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
328 "nontemporal_store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
330 "miri_start_panic" => {
331 // FIXME - the relevant types aren't lang items,
332 // so it's not trivial to check this
341 "unrecognized intrinsic function: `{}`",
344 .span_label(it.span, "unrecognized intrinsic")
349 (n_tps, inputs, output, unsafety)
351 equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
354 /// Type-check `extern "platform-intrinsic" { ... }` functions.
355 pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
357 let name = Symbol::intern(&format!("P{}", n));
358 tcx.mk_ty_param(n, name)
361 let name = it.ident.as_str();
363 let (n_tps, inputs, output) = match &*name {
364 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
365 (2, vec![param(0), param(0)], param(1))
380 | "simd_saturating_add"
381 | "simd_saturating_sub" => (1, vec![param(0), param(0)], param(0)),
382 "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" | "simd_flog2"
383 | "simd_flog10" | "simd_flog" | "simd_fabs" | "simd_floor" | "simd_ceil" => {
384 (1, vec![param(0)], param(0))
386 "simd_fpowi" => (1, vec![param(0), tcx.types.i32], param(0)),
387 "simd_fma" => (1, vec![param(0), param(0), param(0)], param(0)),
388 "simd_gather" => (3, vec![param(0), param(1), param(2)], param(0)),
389 "simd_scatter" => (3, vec![param(0), param(1), param(2)], tcx.mk_unit()),
390 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
391 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
392 "simd_cast" => (2, vec![param(0)], param(1)),
393 "simd_bitmask" => (2, vec![param(0)], param(1)),
394 "simd_select" | "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
395 "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
396 "simd_reduce_add_ordered" | "simd_reduce_mul_ordered" => {
397 (2, vec![param(0), param(1)], param(1))
399 "simd_reduce_add_unordered"
400 | "simd_reduce_mul_unordered"
406 | "simd_reduce_min_nanless"
407 | "simd_reduce_max_nanless" => (2, vec![param(0)], param(1)),
408 name if name.starts_with("simd_shuffle") => match name["simd_shuffle".len()..].parse() {
410 let params = vec![param(0), param(0), tcx.mk_array(tcx.types.u32, n)];
411 (2, params, param(1))
418 "invalid `simd_shuffle`, needs length: `{}`",
426 let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
427 tcx.sess.struct_span_err(it.span, &msg).emit();
432 equate_intrinsic_type(
436 Abi::PlatformIntrinsic,
437 hir::Unsafety::Unsafe,