1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
4 use rustc::traits::{ObligationCause, ObligationCauseCode};
5 use rustc::ty::{self, TyCtxt, Ty};
6 use rustc::ty::subst::Subst;
7 use crate::require_same_types;
9 use rustc_target::spec::abi::Abi;
10 use syntax::symbol::InternedString;
16 fn equate_intrinsic_type<'a, 'tcx>(
17 tcx: TyCtxt<'a, 'tcx, 'tcx>,
18 it: &hir::ForeignItem,
21 safety: hir::Unsafety,
22 inputs: Vec<Ty<'tcx>>,
25 let def_id = tcx.hir().local_def_id_from_hir_id(it.hir_id);
28 hir::ForeignItemKind::Fn(..) => {}
30 struct_span_err!(tcx.sess, it.span, E0622,
31 "intrinsic must be a function")
32 .span_label(it.span, "expected a function")
38 let i_n_tps = tcx.generics_of(def_id).own_counts().types;
40 let span = match it.node {
41 hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
45 struct_span_err!(tcx.sess, span, E0094,
46 "intrinsic has wrong number of type \
47 parameters: found {}, expected {}",
49 .span_label(span, format!("expected {} type parameter", n_tps))
54 let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
61 let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType);
62 require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
65 /// Returns `true` if the given intrinsic is unsafe to call or not.
66 pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
68 "size_of" | "min_align_of" | "needs_drop" |
69 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" |
70 "overflowing_add" | "overflowing_sub" | "overflowing_mul" |
71 "saturating_add" | "saturating_sub" |
72 "rotate_left" | "rotate_right" |
73 "ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse" |
74 "minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64"
75 => hir::Unsafety::Normal,
76 _ => hir::Unsafety::Unsafe,
80 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
81 /// and in libcore/intrinsics.rs
82 pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
83 it: &hir::ForeignItem) {
84 let param = |n| tcx.mk_ty_param(n, InternedString::intern(&format!("P{}", n)));
85 let name = it.ident.as_str();
87 let mk_va_list_ty = || {
88 tcx.lang_items().va_list().map(|did| {
89 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
90 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
91 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
92 tcx.mk_mut_ref(tcx.mk_region(env_region), va_list_ty)
96 let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
97 let split : Vec<&str> = name.split('_').collect();
98 assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
100 //We only care about the operation here
101 let (n_tps, inputs, output) = match split[1] {
102 "cxchg" | "cxchgweak" => (1, vec![tcx.mk_mut_ptr(param(0)),
105 tcx.intern_tup(&[param(0), tcx.types.bool])),
106 "load" => (1, vec![tcx.mk_imm_ptr(param(0))],
108 "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
111 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" |
112 "min" | "umax" | "umin" => {
113 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
116 "fence" | "singlethreadfence" => {
117 (0, Vec::new(), tcx.mk_unit())
120 struct_span_err!(tcx.sess, it.span, E0092,
121 "unrecognized atomic operation function: `{}`", op)
122 .span_label(it.span, "unrecognized atomic operation")
127 (n_tps, inputs, output, hir::Unsafety::Unsafe)
128 } else if &name[..] == "abort" || &name[..] == "unreachable" {
129 (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
131 let unsafety = intrisic_operation_unsafety(&name[..]);
132 let (n_tps, inputs, output) = match &name[..] {
133 "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
135 "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
136 "size_of_val" | "min_align_of_val" => {
138 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
143 "rustc_peek" => (1, vec![param(0)], param(0)),
144 "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
145 "init" => (1, Vec::new(), param(0)),
146 "uninit" => (1, Vec::new(), param(0)),
147 "forget" => (1, vec![param(0)], tcx.mk_unit()),
148 "transmute" => (2, vec![ param(0) ], param(1)),
152 tcx.mk_mut_ptr(param(0)),
157 "prefetch_read_data" | "prefetch_write_data" |
158 "prefetch_read_instruction" | "prefetch_write_instruction" => {
159 (1, vec![tcx.mk_ptr(ty::TypeAndMut {
161 mutbl: hir::MutImmutable
166 (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 {
177 mutbl: hir::MutImmutable
181 tcx.mk_ptr(ty::TypeAndMut {
183 mutbl: hir::MutImmutable
186 "copy" | "copy_nonoverlapping" => {
189 tcx.mk_ptr(ty::TypeAndMut {
191 mutbl: hir::MutImmutable
193 tcx.mk_ptr(ty::TypeAndMut {
195 mutbl: hir::MutMutable
201 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
204 tcx.mk_ptr(ty::TypeAndMut {
206 mutbl: hir::MutMutable
208 tcx.mk_ptr(ty::TypeAndMut {
210 mutbl: hir::MutImmutable
216 "write_bytes" | "volatile_set_memory" => {
219 tcx.mk_ptr(ty::TypeAndMut {
221 mutbl: hir::MutMutable
228 "sqrtf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
229 "sqrtf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
232 vec![ tcx.types.f32, tcx.types.i32 ],
237 vec![ tcx.types.f64, tcx.types.i32 ],
240 "sinf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
241 "sinf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
242 "cosf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
243 "cosf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
246 vec![ tcx.types.f32, tcx.types.f32 ],
251 vec![ tcx.types.f64, tcx.types.f64 ],
254 "expf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
255 "expf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
256 "exp2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
257 "exp2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
258 "logf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
259 "logf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
260 "log10f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
261 "log10f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
262 "log2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
263 "log2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
266 vec![ tcx.types.f32, tcx.types.f32, tcx.types.f32 ],
271 vec![ tcx.types.f64, tcx.types.f64, tcx.types.f64 ],
274 "fabsf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
275 "fabsf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
276 "minnumf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
277 "minnumf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
278 "maxnumf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
279 "maxnumf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
280 "copysignf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
281 "copysignf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
282 "floorf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
283 "floorf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
284 "ceilf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
285 "ceilf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
286 "truncf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
287 "truncf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
288 "rintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
289 "rintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
290 "nearbyintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
291 "nearbyintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
292 "roundf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
293 "roundf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
295 "volatile_load" | "unaligned_volatile_load" =>
296 (1, vec![ tcx.mk_imm_ptr(param(0)) ], param(0)),
297 "volatile_store" | "unaligned_volatile_store" =>
298 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit()),
300 "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" |
301 "bswap" | "bitreverse" =>
302 (1, vec![param(0)], param(0)),
304 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" =>
305 (1, vec![param(0), param(0)],
306 tcx.intern_tup(&[param(0), tcx.types.bool])),
308 "unchecked_div" | "unchecked_rem" | "exact_div" =>
309 (1, vec![param(0), param(0)], param(0)),
310 "unchecked_shl" | "unchecked_shr" |
311 "rotate_left" | "rotate_right" =>
312 (1, vec![param(0), param(0)], param(0)),
313 "unchecked_add" | "unchecked_sub" | "unchecked_mul" =>
314 (1, vec![param(0), param(0)], param(0)),
315 "overflowing_add" | "overflowing_sub" | "overflowing_mul" =>
316 (1, vec![param(0), param(0)], param(0)),
317 "saturating_add" | "saturating_sub" =>
318 (1, vec![param(0), param(0)], param(0)),
319 "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" =>
320 (1, vec![param(0), param(0)], param(0)),
322 "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
323 "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
324 "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
326 "discriminant_value" => (1, vec![
327 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
329 param(0))], tcx.types.u64),
332 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
333 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
337 hir::Unsafety::Normal,
340 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
343 "va_start" | "va_end" => {
344 match mk_va_list_ty() {
345 Some(va_list_ty) => (0, vec![va_list_ty], tcx.mk_unit()),
346 None => bug!("`va_list` language item needed for C-variadic intrinsics")
351 match tcx.lang_items().va_list() {
353 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
354 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
355 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
356 let ret_ty = match va_list_ty.sty {
357 ty::Adt(def, _) if def.is_struct() => {
358 let fields = &def.non_enum_variant().fields;
359 match tcx.type_of(fields[0].did).subst(tcx, &[region.into()]).sty {
360 ty::Ref(_, element_ty, _) => match element_ty.sty {
361 ty::Adt(..) => element_ty,
364 _ => bug!("va_list structure is invalid")
368 bug!("va_list structure is invalid")
371 (0, vec![tcx.mk_imm_ref(tcx.mk_region(env_region), va_list_ty)], ret_ty)
373 None => bug!("`va_list` language item needed for C-variadic intrinsics")
378 match mk_va_list_ty() {
379 Some(va_list_ty) => (1, vec![va_list_ty], param(0)),
380 None => bug!("`va_list` language item needed for C-variadic intrinsics")
384 "nontemporal_store" => {
385 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit())
389 struct_span_err!(tcx.sess, it.span, E0093,
390 "unrecognized intrinsic function: `{}`",
392 .span_label(it.span, "unrecognized intrinsic")
397 (n_tps, inputs, output, unsafety)
399 equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
402 /// Type-check `extern "platform-intrinsic" { ... }` functions.
403 pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
404 it: &hir::ForeignItem) {
406 let name = InternedString::intern(&format!("P{}", n));
407 tcx.mk_ty_param(n, name)
410 let name = it.ident.as_str();
412 let (n_tps, inputs, output) = match &*name {
413 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
414 (2, vec![param(0), param(0)], param(1))
416 "simd_add" | "simd_sub" | "simd_mul" | "simd_rem" |
417 "simd_div" | "simd_shl" | "simd_shr" |
418 "simd_and" | "simd_or" | "simd_xor" |
419 "simd_fmin" | "simd_fmax" | "simd_fpow" |
420 "simd_saturating_add" | "simd_saturating_sub" => {
421 (1, vec![param(0), param(0)], param(0))
423 "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" |
424 "simd_flog2" | "simd_flog10" | "simd_flog" |
425 "simd_fabs" | "simd_floor" | "simd_ceil" => {
426 (1, vec![param(0)], param(0))
429 (1, vec![param(0), tcx.types.i32], param(0))
432 (1, vec![param(0), param(0), param(0)], param(0))
435 (3, vec![param(0), param(1), param(2)], param(0))
438 (3, vec![param(0), param(1), param(2)], tcx.mk_unit())
440 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
441 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
442 "simd_cast" => (2, vec![param(0)], param(1)),
443 "simd_bitmask" => (2, vec![param(0)], param(1)),
445 "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
446 "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
447 "simd_reduce_add_ordered" | "simd_reduce_mul_ordered"
448 => (2, vec![param(0), param(1)], param(1)),
449 "simd_reduce_add_unordered" | "simd_reduce_mul_unordered" |
450 "simd_reduce_and" | "simd_reduce_or" | "simd_reduce_xor" |
451 "simd_reduce_min" | "simd_reduce_max" |
452 "simd_reduce_min_nanless" | "simd_reduce_max_nanless"
453 => (2, vec![param(0)], param(1)),
454 name if name.starts_with("simd_shuffle") => {
455 match name["simd_shuffle".len()..].parse() {
457 let params = vec![param(0), param(0),
458 tcx.mk_array(tcx.types.u32, n)];
459 (2, params, param(1))
462 span_err!(tcx.sess, it.span, E0439,
463 "invalid `simd_shuffle`, needs length: `{}`", name);
469 let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
470 tcx.sess.span_err(it.span, &msg);
475 equate_intrinsic_type(tcx, it, n_tps, Abi::PlatformIntrinsic, hir::Unsafety::Unsafe,