1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
4 use rustc::middle::lang_items::PanicLocationLangItem;
5 use rustc::traits::{ObligationCause, ObligationCauseCode};
6 use rustc::ty::{self, TyCtxt, Ty};
7 use rustc::ty::subst::Subst;
8 use crate::require_same_types;
10 use rustc_target::spec::abi::Abi;
11 use syntax::symbol::Symbol;
15 use rustc_error_codes::*;
19 fn equate_intrinsic_type<'tcx>(
21 it: &hir::ForeignItem,
24 safety: hir::Unsafety,
25 inputs: Vec<Ty<'tcx>>,
28 let def_id = tcx.hir().local_def_id(it.hir_id);
31 hir::ForeignItemKind::Fn(..) => {}
33 struct_span_err!(tcx.sess, it.span, E0622,
34 "intrinsic must be a function")
35 .span_label(it.span, "expected a function")
41 let i_n_tps = tcx.generics_of(def_id).own_counts().types;
43 let span = match it.kind {
44 hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
48 struct_span_err!(tcx.sess, span, E0094,
49 "intrinsic has wrong number of type \
50 parameters: found {}, expected {}",
52 .span_label(span, format!("expected {} type parameter", n_tps))
57 let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
64 let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType);
65 require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
68 /// Returns `true` if the given intrinsic is unsafe to call or not.
69 pub fn intrinsic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
71 "size_of" | "min_align_of" | "needs_drop" | "caller_location" |
72 "size_of_val" | "min_align_of_val" |
73 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" |
74 "wrapping_add" | "wrapping_sub" | "wrapping_mul" |
75 "saturating_add" | "saturating_sub" |
76 "rotate_left" | "rotate_right" |
77 "ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse" |
78 "discriminant_value" | "type_id" | "likely" | "unlikely" |
79 "minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64" | "type_name"
80 => hir::Unsafety::Normal,
81 _ => hir::Unsafety::Unsafe,
85 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
86 /// and in libcore/intrinsics.rs
87 pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
88 let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)));
89 let name = it.ident.as_str();
91 let mk_va_list_ty = |mutbl| {
92 tcx.lang_items().va_list().map(|did| {
93 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
94 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
95 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
96 (tcx.mk_ref(tcx.mk_region(env_region), ty::TypeAndMut {
103 let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
104 let split : Vec<&str> = name.split('_').collect();
105 assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
107 //We only care about the operation here
108 let (n_tps, inputs, output) = match split[1] {
109 "cxchg" | "cxchgweak" => (1, vec![tcx.mk_mut_ptr(param(0)),
112 tcx.intern_tup(&[param(0), tcx.types.bool])),
113 "load" => (1, vec![tcx.mk_imm_ptr(param(0))],
115 "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
118 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" |
119 "min" | "umax" | "umin" => {
120 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
123 "fence" | "singlethreadfence" => {
124 (0, Vec::new(), tcx.mk_unit())
127 struct_span_err!(tcx.sess, it.span, E0092,
128 "unrecognized atomic operation function: `{}`", op)
129 .span_label(it.span, "unrecognized atomic operation")
134 (n_tps, inputs, output, hir::Unsafety::Unsafe)
135 } else if &name[..] == "abort" || &name[..] == "unreachable" {
136 (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
138 let unsafety = intrinsic_operation_unsafety(&name[..]);
139 let (n_tps, inputs, output) = match &name[..] {
140 "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
142 "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
143 "size_of_val" | "min_align_of_val" => {
145 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
150 "rustc_peek" => (1, vec![param(0)], param(0)),
151 "caller_location" => (
155 tcx.lifetimes.re_static,
156 tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None))
157 .subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())),
160 "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
161 "init" => (1, Vec::new(), param(0)),
162 "uninit" => (1, Vec::new(), param(0)),
163 "forget" => (1, vec![param(0)], tcx.mk_unit()),
164 "transmute" => (2, vec![ param(0) ], param(1)),
168 tcx.mk_mut_ptr(param(0)),
173 "prefetch_read_data" | "prefetch_write_data" |
174 "prefetch_read_instruction" | "prefetch_write_instruction" => {
175 (1, vec![tcx.mk_ptr(ty::TypeAndMut {
177 mutbl: hir::Mutability::Immutable
182 (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit())
184 "needs_drop" => (1, Vec::new(), tcx.types.bool),
186 "type_name" => (1, Vec::new(), tcx.mk_static_str()),
187 "type_id" => (1, Vec::new(), tcx.types.u64),
188 "offset" | "arith_offset" => {
191 tcx.mk_ptr(ty::TypeAndMut {
193 mutbl: hir::Mutability::Immutable
197 tcx.mk_ptr(ty::TypeAndMut {
199 mutbl: hir::Mutability::Immutable
202 "copy" | "copy_nonoverlapping" => {
205 tcx.mk_ptr(ty::TypeAndMut {
207 mutbl: hir::Mutability::Immutable
209 tcx.mk_ptr(ty::TypeAndMut {
211 mutbl: hir::Mutability::Mutable
217 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
220 tcx.mk_ptr(ty::TypeAndMut {
222 mutbl: hir::Mutability::Mutable
224 tcx.mk_ptr(ty::TypeAndMut {
226 mutbl: hir::Mutability::Immutable
232 "write_bytes" | "volatile_set_memory" => {
235 tcx.mk_ptr(ty::TypeAndMut {
237 mutbl: hir::Mutability::Mutable
244 "sqrtf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
245 "sqrtf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
248 vec![ tcx.types.f32, tcx.types.i32 ],
253 vec![ tcx.types.f64, tcx.types.i32 ],
256 "sinf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
257 "sinf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
258 "cosf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
259 "cosf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
262 vec![ tcx.types.f32, tcx.types.f32 ],
267 vec![ tcx.types.f64, tcx.types.f64 ],
270 "expf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
271 "expf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
272 "exp2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
273 "exp2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
274 "logf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
275 "logf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
276 "log10f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
277 "log10f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
278 "log2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
279 "log2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
282 vec![ tcx.types.f32, tcx.types.f32, tcx.types.f32 ],
287 vec![ tcx.types.f64, tcx.types.f64, tcx.types.f64 ],
290 "fabsf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
291 "fabsf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
292 "minnumf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
293 "minnumf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
294 "maxnumf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
295 "maxnumf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
296 "copysignf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
297 "copysignf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
298 "floorf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
299 "floorf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
300 "ceilf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
301 "ceilf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
302 "truncf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
303 "truncf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
304 "rintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
305 "rintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
306 "nearbyintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
307 "nearbyintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
308 "roundf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
309 "roundf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
311 "volatile_load" | "unaligned_volatile_load" =>
312 (1, vec![ tcx.mk_imm_ptr(param(0)) ], param(0)),
313 "volatile_store" | "unaligned_volatile_store" =>
314 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit()),
316 "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" |
317 "bswap" | "bitreverse" =>
318 (1, vec![param(0)], param(0)),
320 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" =>
321 (1, vec![param(0), param(0)],
322 tcx.intern_tup(&[param(0), tcx.types.bool])),
325 (1, vec![ tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0)) ], tcx.types.isize),
326 "unchecked_div" | "unchecked_rem" | "exact_div" =>
327 (1, vec![param(0), param(0)], param(0)),
328 "unchecked_shl" | "unchecked_shr" |
329 "rotate_left" | "rotate_right" =>
330 (1, vec![param(0), param(0)], param(0)),
331 "unchecked_add" | "unchecked_sub" | "unchecked_mul" =>
332 (1, vec![param(0), param(0)], param(0)),
333 "wrapping_add" | "wrapping_sub" | "wrapping_mul" =>
334 (1, vec![param(0), param(0)], param(0)),
335 "saturating_add" | "saturating_sub" =>
336 (1, vec![param(0), param(0)], param(0)),
337 "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" =>
338 (1, vec![param(0), param(0)], param(0)),
340 "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
341 "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
342 "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
344 "discriminant_value" => (1, vec![
345 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
347 param(0))], tcx.types.u64),
350 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
351 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
355 hir::Unsafety::Normal,
358 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
361 "va_start" | "va_end" => {
362 match mk_va_list_ty(hir::Mutability::Mutable) {
363 Some((va_list_ref_ty, _)) => (0, vec![va_list_ref_ty], tcx.mk_unit()),
364 None => bug!("`va_list` language item needed for C-variadic intrinsics")
369 match mk_va_list_ty(hir::Mutability::Immutable) {
370 Some((va_list_ref_ty, va_list_ty)) => {
371 let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
372 (0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
374 None => bug!("`va_list` language item needed for C-variadic intrinsics")
379 match mk_va_list_ty(hir::Mutability::Mutable) {
380 Some((va_list_ref_ty, _)) => (1, vec![va_list_ref_ty], param(0)),
381 None => bug!("`va_list` language item needed for C-variadic intrinsics")
385 "nontemporal_store" => {
386 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit())
389 "miri_start_panic" => {
390 // FIXME - the relevant types aren't lang items,
391 // so it's not trivial to check this
396 struct_span_err!(tcx.sess, it.span, E0093,
397 "unrecognized intrinsic function: `{}`",
399 .span_label(it.span, "unrecognized intrinsic")
404 (n_tps, inputs, output, unsafety)
406 equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
409 /// Type-check `extern "platform-intrinsic" { ... }` functions.
410 pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
412 let name = Symbol::intern(&format!("P{}", n));
413 tcx.mk_ty_param(n, name)
416 let name = it.ident.as_str();
418 let (n_tps, inputs, output) = match &*name {
419 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
420 (2, vec![param(0), param(0)], param(1))
422 "simd_add" | "simd_sub" | "simd_mul" | "simd_rem" |
423 "simd_div" | "simd_shl" | "simd_shr" |
424 "simd_and" | "simd_or" | "simd_xor" |
425 "simd_fmin" | "simd_fmax" | "simd_fpow" |
426 "simd_saturating_add" | "simd_saturating_sub" => {
427 (1, vec![param(0), param(0)], param(0))
429 "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" |
430 "simd_flog2" | "simd_flog10" | "simd_flog" |
431 "simd_fabs" | "simd_floor" | "simd_ceil" => {
432 (1, vec![param(0)], param(0))
435 (1, vec![param(0), tcx.types.i32], param(0))
438 (1, vec![param(0), param(0), param(0)], param(0))
441 (3, vec![param(0), param(1), param(2)], param(0))
444 (3, vec![param(0), param(1), param(2)], tcx.mk_unit())
446 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
447 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
448 "simd_cast" => (2, vec![param(0)], param(1)),
449 "simd_bitmask" => (2, vec![param(0)], param(1)),
451 "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
452 "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
453 "simd_reduce_add_ordered" | "simd_reduce_mul_ordered"
454 => (2, vec![param(0), param(1)], param(1)),
455 "simd_reduce_add_unordered" | "simd_reduce_mul_unordered" |
456 "simd_reduce_and" | "simd_reduce_or" | "simd_reduce_xor" |
457 "simd_reduce_min" | "simd_reduce_max" |
458 "simd_reduce_min_nanless" | "simd_reduce_max_nanless"
459 => (2, vec![param(0)], param(1)),
460 name if name.starts_with("simd_shuffle") => {
461 match name["simd_shuffle".len()..].parse() {
463 let params = vec![param(0), param(0),
464 tcx.mk_array(tcx.types.u32, n)];
465 (2, params, param(1))
468 span_err!(tcx.sess, it.span, E0439,
469 "invalid `simd_shuffle`, needs length: `{}`", name);
475 let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
476 tcx.sess.span_err(it.span, &msg);
481 equate_intrinsic_type(tcx, it, n_tps, Abi::PlatformIntrinsic, hir::Unsafety::Unsafe,