]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/intrinsic.rs
Auto merge of #63810 - oli-obk:const_offset_from, r=RalfJung,nikic
[rust.git] / src / librustc_typeck / check / intrinsic.rs
1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
3
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;
9
10 use rustc_target::spec::abi::Abi;
11 use syntax::symbol::Symbol;
12
13 use rustc::hir;
14
15 use std::iter;
16
17 fn equate_intrinsic_type<'tcx>(
18     tcx: TyCtxt<'tcx>,
19     it: &hir::ForeignItem,
20     n_tps: usize,
21     abi: Abi,
22     safety: hir::Unsafety,
23     inputs: Vec<Ty<'tcx>>,
24     output: Ty<'tcx>,
25 ) {
26     let def_id = tcx.hir().local_def_id(it.hir_id);
27
28     match it.kind {
29         hir::ForeignItemKind::Fn(..) => {}
30         _ => {
31             struct_span_err!(tcx.sess, it.span, E0622,
32                              "intrinsic must be a function")
33                 .span_label(it.span, "expected a function")
34                 .emit();
35             return;
36         }
37     }
38
39     let i_n_tps = tcx.generics_of(def_id).own_counts().types;
40     if i_n_tps != n_tps {
41         let span = match it.kind {
42             hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
43             _ => bug!()
44         };
45
46         struct_span_err!(tcx.sess, span, E0094,
47                         "intrinsic has wrong number of type \
48                          parameters: found {}, expected {}",
49                         i_n_tps, n_tps)
50             .span_label(span, format!("expected {} type parameter", n_tps))
51             .emit();
52         return;
53     }
54
55     let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
56         inputs.into_iter(),
57         output,
58         false,
59         safety,
60         abi
61     )));
62     let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType);
63     require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
64 }
65
66 /// Returns `true` if the given intrinsic is unsafe to call or not.
67 pub fn intrinsic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
68     match intrinsic {
69         "size_of" | "min_align_of" | "needs_drop" | "caller_location" |
70         "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" |
71         "wrapping_add" | "wrapping_sub" | "wrapping_mul" |
72         "saturating_add" | "saturating_sub" |
73         "rotate_left" | "rotate_right" |
74         "ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse" |
75         "minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64" | "type_name"
76         => hir::Unsafety::Normal,
77         _ => hir::Unsafety::Unsafe,
78     }
79 }
80
81 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
82 /// and in libcore/intrinsics.rs
83 pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
84     let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)));
85     let name = it.ident.as_str();
86
87     let mk_va_list_ty = |mutbl| {
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_ref(tcx.mk_region(env_region), ty::TypeAndMut {
93                 ty: va_list_ty,
94                 mutbl
95             }), va_list_ty)
96         })
97     };
98
99     let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
100         let split : Vec<&str> = name.split('_').collect();
101         assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
102
103         //We only care about the operation here
104         let (n_tps, inputs, output) = match split[1] {
105             "cxchg" | "cxchgweak" => (1, vec![tcx.mk_mut_ptr(param(0)),
106                                               param(0),
107                                               param(0)],
108                                       tcx.intern_tup(&[param(0), tcx.types.bool])),
109             "load" => (1, vec![tcx.mk_imm_ptr(param(0))],
110                        param(0)),
111             "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
112                         tcx.mk_unit()),
113
114             "xchg" | "xadd" | "xsub" | "and"  | "nand" | "or" | "xor" | "max" |
115             "min"  | "umax" | "umin" => {
116                 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
117                  param(0))
118             }
119             "fence" | "singlethreadfence" => {
120                 (0, Vec::new(), tcx.mk_unit())
121             }
122             op => {
123                 struct_span_err!(tcx.sess, it.span, E0092,
124                       "unrecognized atomic operation function: `{}`", op)
125                   .span_label(it.span, "unrecognized atomic operation")
126                   .emit();
127                 return;
128             }
129         };
130         (n_tps, inputs, output, hir::Unsafety::Unsafe)
131     } else if &name[..] == "abort" || &name[..] == "unreachable" {
132         (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
133     } else {
134         let unsafety = intrinsic_operation_unsafety(&name[..]);
135         let (n_tps, inputs, output) = match &name[..] {
136             "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
137             "size_of" |
138             "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
139             "size_of_val" |  "min_align_of_val" => {
140                 (1, vec![
141                     tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
142                                                                  ty::BrAnon(0))),
143                                    param(0))
144                  ], tcx.types.usize)
145             }
146             "rustc_peek" => (1, vec![param(0)], param(0)),
147             "caller_location" => (
148                 0,
149                 vec![],
150                 tcx.mk_imm_ref(
151                     tcx.lifetimes.re_static,
152                     tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None))
153                         .subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())),
154                 ),
155             ),
156             "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
157             "init" => (1, Vec::new(), param(0)),
158             "uninit" => (1, Vec::new(), param(0)),
159             "forget" => (1, vec![param(0)], tcx.mk_unit()),
160             "transmute" => (2, vec![ param(0) ], param(1)),
161             "move_val_init" => {
162                 (1,
163                  vec![
164                     tcx.mk_mut_ptr(param(0)),
165                     param(0)
166                   ],
167                tcx.mk_unit())
168             }
169             "prefetch_read_data" | "prefetch_write_data" |
170             "prefetch_read_instruction" | "prefetch_write_instruction" => {
171                 (1, vec![tcx.mk_ptr(ty::TypeAndMut {
172                           ty: param(0),
173                           mutbl: hir::MutImmutable
174                          }), tcx.types.i32],
175                     tcx.mk_unit())
176             }
177             "drop_in_place" => {
178                 (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit())
179             }
180             "needs_drop" => (1, Vec::new(), tcx.types.bool),
181
182             "type_name" => (1, Vec::new(), tcx.mk_static_str()),
183             "type_id" => (1, Vec::new(), tcx.types.u64),
184             "offset" | "arith_offset" => {
185               (1,
186                vec![
187                   tcx.mk_ptr(ty::TypeAndMut {
188                       ty: param(0),
189                       mutbl: hir::MutImmutable
190                   }),
191                   tcx.types.isize
192                ],
193                tcx.mk_ptr(ty::TypeAndMut {
194                    ty: param(0),
195                    mutbl: hir::MutImmutable
196                }))
197             }
198             "copy" | "copy_nonoverlapping" => {
199               (1,
200                vec![
201                   tcx.mk_ptr(ty::TypeAndMut {
202                       ty: param(0),
203                       mutbl: hir::MutImmutable
204                   }),
205                   tcx.mk_ptr(ty::TypeAndMut {
206                       ty: param(0),
207                       mutbl: hir::MutMutable
208                   }),
209                   tcx.types.usize,
210                ],
211                tcx.mk_unit())
212             }
213             "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
214               (1,
215                vec![
216                   tcx.mk_ptr(ty::TypeAndMut {
217                       ty: param(0),
218                       mutbl: hir::MutMutable
219                   }),
220                   tcx.mk_ptr(ty::TypeAndMut {
221                       ty: param(0),
222                       mutbl: hir::MutImmutable
223                   }),
224                   tcx.types.usize,
225                ],
226                tcx.mk_unit())
227             }
228             "write_bytes" | "volatile_set_memory" => {
229               (1,
230                vec![
231                   tcx.mk_ptr(ty::TypeAndMut {
232                       ty: param(0),
233                       mutbl: hir::MutMutable
234                   }),
235                   tcx.types.u8,
236                   tcx.types.usize,
237                ],
238                tcx.mk_unit())
239             }
240             "sqrtf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
241             "sqrtf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
242             "powif32" => {
243                (0,
244                 vec![ tcx.types.f32, tcx.types.i32 ],
245                 tcx.types.f32)
246             }
247             "powif64" => {
248                (0,
249                 vec![ tcx.types.f64, tcx.types.i32 ],
250                 tcx.types.f64)
251             }
252             "sinf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
253             "sinf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
254             "cosf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
255             "cosf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
256             "powf32" => {
257                (0,
258                 vec![ tcx.types.f32, tcx.types.f32 ],
259                 tcx.types.f32)
260             }
261             "powf64" => {
262                (0,
263                 vec![ tcx.types.f64, tcx.types.f64 ],
264                 tcx.types.f64)
265             }
266             "expf32"   => (0, vec![ tcx.types.f32 ], tcx.types.f32),
267             "expf64"   => (0, vec![ tcx.types.f64 ], tcx.types.f64),
268             "exp2f32"  => (0, vec![ tcx.types.f32 ], tcx.types.f32),
269             "exp2f64"  => (0, vec![ tcx.types.f64 ], tcx.types.f64),
270             "logf32"   => (0, vec![ tcx.types.f32 ], tcx.types.f32),
271             "logf64"   => (0, vec![ tcx.types.f64 ], tcx.types.f64),
272             "log10f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
273             "log10f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
274             "log2f32"  => (0, vec![ tcx.types.f32 ], tcx.types.f32),
275             "log2f64"  => (0, vec![ tcx.types.f64 ], tcx.types.f64),
276             "fmaf32" => {
277                 (0,
278                  vec![ tcx.types.f32, tcx.types.f32, tcx.types.f32 ],
279                  tcx.types.f32)
280             }
281             "fmaf64" => {
282                 (0,
283                  vec![ tcx.types.f64, tcx.types.f64, tcx.types.f64 ],
284                  tcx.types.f64)
285             }
286             "fabsf32"      => (0, vec![ tcx.types.f32 ], tcx.types.f32),
287             "fabsf64"      => (0, vec![ tcx.types.f64 ], tcx.types.f64),
288             "minnumf32"    => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
289             "minnumf64"    => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
290             "maxnumf32"    => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
291             "maxnumf64"    => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
292             "copysignf32"  => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
293             "copysignf64"  => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
294             "floorf32"     => (0, vec![ tcx.types.f32 ], tcx.types.f32),
295             "floorf64"     => (0, vec![ tcx.types.f64 ], tcx.types.f64),
296             "ceilf32"      => (0, vec![ tcx.types.f32 ], tcx.types.f32),
297             "ceilf64"      => (0, vec![ tcx.types.f64 ], tcx.types.f64),
298             "truncf32"     => (0, vec![ tcx.types.f32 ], tcx.types.f32),
299             "truncf64"     => (0, vec![ tcx.types.f64 ], tcx.types.f64),
300             "rintf32"      => (0, vec![ tcx.types.f32 ], tcx.types.f32),
301             "rintf64"      => (0, vec![ tcx.types.f64 ], tcx.types.f64),
302             "nearbyintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
303             "nearbyintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
304             "roundf32"     => (0, vec![ tcx.types.f32 ], tcx.types.f32),
305             "roundf64"     => (0, vec![ tcx.types.f64 ], tcx.types.f64),
306
307             "volatile_load" | "unaligned_volatile_load" =>
308                 (1, vec![ tcx.mk_imm_ptr(param(0)) ], param(0)),
309             "volatile_store" | "unaligned_volatile_store" =>
310                 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit()),
311
312             "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" |
313             "bswap" | "bitreverse" =>
314                 (1, vec![param(0)], param(0)),
315
316             "add_with_overflow" | "sub_with_overflow"  | "mul_with_overflow" =>
317                 (1, vec![param(0), param(0)],
318                 tcx.intern_tup(&[param(0), tcx.types.bool])),
319
320             "ptr_offset_from" =>
321                 (1, vec![ tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0)) ], tcx.types.isize),
322             "unchecked_div" | "unchecked_rem" | "exact_div" =>
323                 (1, vec![param(0), param(0)], param(0)),
324             "unchecked_shl" | "unchecked_shr" |
325             "rotate_left" | "rotate_right" =>
326                 (1, vec![param(0), param(0)], param(0)),
327             "unchecked_add" | "unchecked_sub" | "unchecked_mul" =>
328                 (1, vec![param(0), param(0)], param(0)),
329             "wrapping_add" | "wrapping_sub" | "wrapping_mul" =>
330                 (1, vec![param(0), param(0)], param(0)),
331             "saturating_add" | "saturating_sub" =>
332                 (1, vec![param(0), param(0)], param(0)),
333             "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" =>
334                 (1, vec![param(0), param(0)], param(0)),
335
336             "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
337             "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
338             "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
339
340             "discriminant_value" => (1, vec![
341                     tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
342                                                                  ty::BrAnon(0))),
343                                    param(0))], tcx.types.u64),
344
345             "try" => {
346                 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
347                 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
348                     iter::once(mut_u8),
349                     tcx.mk_unit(),
350                     false,
351                     hir::Unsafety::Normal,
352                     Abi::Rust,
353                 ));
354                 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
355             }
356
357             "va_start" | "va_end" => {
358                 match mk_va_list_ty(hir::MutMutable) {
359                     Some((va_list_ref_ty, _)) => (0, vec![va_list_ref_ty], tcx.mk_unit()),
360                     None => bug!("`va_list` language item needed for C-variadic intrinsics")
361                 }
362             }
363
364             "va_copy" => {
365                 match mk_va_list_ty(hir::MutImmutable) {
366                     Some((va_list_ref_ty, va_list_ty)) => {
367                         let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
368                         (0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
369                     }
370                     None => bug!("`va_list` language item needed for C-variadic intrinsics")
371                 }
372             }
373
374             "va_arg" => {
375                 match mk_va_list_ty(hir::MutMutable) {
376                     Some((va_list_ref_ty, _)) => (1, vec![va_list_ref_ty], param(0)),
377                     None => bug!("`va_list` language item needed for C-variadic intrinsics")
378                 }
379             }
380
381             "nontemporal_store" => {
382                 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit())
383             }
384
385             ref other => {
386                 struct_span_err!(tcx.sess, it.span, E0093,
387                                  "unrecognized intrinsic function: `{}`",
388                                  *other)
389                                  .span_label(it.span, "unrecognized intrinsic")
390                                  .emit();
391                 return;
392             }
393         };
394         (n_tps, inputs, output, unsafety)
395     };
396     equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
397 }
398
399 /// Type-check `extern "platform-intrinsic" { ... }` functions.
400 pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
401     let param = |n| {
402         let name = Symbol::intern(&format!("P{}", n));
403         tcx.mk_ty_param(n, name)
404     };
405
406     let name = it.ident.as_str();
407
408     let (n_tps, inputs, output) = match &*name {
409         "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
410             (2, vec![param(0), param(0)], param(1))
411         }
412         "simd_add" | "simd_sub" | "simd_mul" | "simd_rem" |
413         "simd_div" | "simd_shl" | "simd_shr" |
414         "simd_and" | "simd_or" | "simd_xor" |
415         "simd_fmin" | "simd_fmax" | "simd_fpow" |
416         "simd_saturating_add" | "simd_saturating_sub" => {
417             (1, vec![param(0), param(0)], param(0))
418         }
419         "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" |
420         "simd_flog2" | "simd_flog10" | "simd_flog" |
421         "simd_fabs" | "simd_floor" | "simd_ceil" => {
422             (1, vec![param(0)], param(0))
423         }
424         "simd_fpowi" => {
425             (1, vec![param(0), tcx.types.i32], param(0))
426         }
427         "simd_fma" => {
428             (1, vec![param(0), param(0), param(0)], param(0))
429         }
430         "simd_gather" => {
431             (3, vec![param(0), param(1), param(2)], param(0))
432         }
433         "simd_scatter" => {
434             (3, vec![param(0), param(1), param(2)], tcx.mk_unit())
435         }
436         "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
437         "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
438         "simd_cast" => (2, vec![param(0)], param(1)),
439         "simd_bitmask" => (2, vec![param(0)], param(1)),
440         "simd_select" |
441         "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
442         "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
443         "simd_reduce_add_ordered" | "simd_reduce_mul_ordered"
444             => (2, vec![param(0), param(1)], param(1)),
445         "simd_reduce_add_unordered" | "simd_reduce_mul_unordered" |
446         "simd_reduce_and" | "simd_reduce_or"  | "simd_reduce_xor" |
447         "simd_reduce_min" | "simd_reduce_max" |
448         "simd_reduce_min_nanless" | "simd_reduce_max_nanless"
449             => (2, vec![param(0)], param(1)),
450         name if name.starts_with("simd_shuffle") => {
451             match name["simd_shuffle".len()..].parse() {
452                 Ok(n) => {
453                     let params = vec![param(0), param(0),
454                                       tcx.mk_array(tcx.types.u32, n)];
455                     (2, params, param(1))
456                 }
457                 Err(_) => {
458                     span_err!(tcx.sess, it.span, E0439,
459                               "invalid `simd_shuffle`, needs length: `{}`", name);
460                     return
461                 }
462             }
463         }
464         _ => {
465             let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
466             tcx.sess.span_err(it.span, &msg);
467             return;
468         }
469     };
470
471     equate_intrinsic_type(tcx, it, n_tps, Abi::PlatformIntrinsic, hir::Unsafety::Unsafe,
472                           inputs, output)
473 }