1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Type-checking for the rust-intrinsic and platform-intrinsic
12 //! intrinsics that the compiler exposes.
17 use middle::ty::FnSig;
18 use middle::ty::{self, Ty};
19 use middle::ty::fold::TypeFolder;
20 use {CrateCtxt, require_same_types};
22 use std::collections::{HashMap};
25 use syntax::attr::AttrMetaMethods;
26 use syntax::codemap::Span;
27 use syntax::parse::token;
31 fn equate_intrinsic_type<'a, 'tcx>(tcx: &ty::ctxt<'tcx>, it: &hir::ForeignItem,
34 inputs: Vec<ty::Ty<'tcx>>,
35 output: ty::FnOutput<'tcx>) {
36 let fty = tcx.mk_fn(None, tcx.mk_bare_fn(ty::BareFnTy {
37 unsafety: hir::Unsafety::Unsafe,
39 sig: ty::Binder(FnSig {
45 let i_ty = tcx.lookup_item_type(tcx.map.local_def_id(it.id));
46 let i_n_tps = i_ty.generics.types.len(subst::FnSpace);
48 span_err!(tcx.sess, it.span, E0094,
49 "intrinsic has wrong number of type \
50 parameters: found {}, expected {}",
53 require_same_types(tcx,
60 format!("intrinsic has wrong type: expected `{}`",
66 /// Remember to add all intrinsics here, in librustc_trans/trans/intrinsic.rs,
67 /// and in libcore/intrinsics.rs
68 pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &hir::ForeignItem) {
69 fn param<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, n: u32) -> Ty<'tcx> {
70 let name = token::intern(&format!("P{}", n));
71 ccx.tcx.mk_param(subst::FnSpace, n, name)
75 let name = it.name.as_str();
76 let (n_tps, inputs, output) = if name.starts_with("atomic_") {
77 let split : Vec<&str> = name.split('_').collect();
78 assert!(split.len() >= 2, "Atomic intrinsic not correct format");
80 //We only care about the operation here
81 let (n_tps, inputs, output) = match split[1] {
82 "cxchg" => (1, vec!(tcx.mk_mut_ptr(param(ccx, 0)),
86 "load" => (1, vec!(tcx.mk_imm_ptr(param(ccx, 0))),
88 "store" => (1, vec!(tcx.mk_mut_ptr(param(ccx, 0)), param(ccx, 0)),
91 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" |
92 "min" | "umax" | "umin" => {
93 (1, vec!(tcx.mk_mut_ptr(param(ccx, 0)), param(ccx, 0)),
96 "fence" | "singlethreadfence" => {
97 (0, Vec::new(), tcx.mk_nil())
100 span_err!(tcx.sess, it.span, E0092,
101 "unrecognized atomic operation function: `{}`", op);
105 (n_tps, inputs, ty::FnConverging(output))
106 } else if &name[..] == "abort" || &name[..] == "unreachable" {
107 (0, Vec::new(), ty::FnDiverging)
109 let (n_tps, inputs, output) = match &name[..] {
110 "breakpoint" => (0, Vec::new(), tcx.mk_nil()),
112 "pref_align_of" | "min_align_of" => (1, Vec::new(), ccx.tcx.types.usize),
113 "size_of_val" | "min_align_of_val" => {
115 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1),
118 ], ccx.tcx.types.usize)
120 "init" | "init_dropped" => (1, Vec::new(), param(ccx, 0)),
121 "uninit" => (1, Vec::new(), param(ccx, 0)),
122 "forget" => (1, vec!( param(ccx, 0) ), tcx.mk_nil()),
123 "transmute" => (2, vec!( param(ccx, 0) ), param(ccx, 1)),
127 tcx.mk_mut_ptr(param(ccx, 0)),
133 (1, vec![tcx.mk_mut_ptr(param(ccx, 0))], tcx.mk_nil())
135 "needs_drop" => (1, Vec::new(), ccx.tcx.types.bool),
137 "type_name" => (1, Vec::new(), tcx.mk_static_str()),
138 "type_id" => (1, Vec::new(), ccx.tcx.types.u64),
139 "offset" | "arith_offset" => {
142 tcx.mk_ptr(ty::TypeAndMut {
144 mutbl: hir::MutImmutable
148 tcx.mk_ptr(ty::TypeAndMut {
150 mutbl: hir::MutImmutable
153 "copy" | "copy_nonoverlapping" => {
156 tcx.mk_ptr(ty::TypeAndMut {
158 mutbl: hir::MutImmutable
160 tcx.mk_ptr(ty::TypeAndMut {
162 mutbl: hir::MutMutable
168 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
171 tcx.mk_ptr(ty::TypeAndMut {
173 mutbl: hir::MutMutable
175 tcx.mk_ptr(ty::TypeAndMut {
177 mutbl: hir::MutImmutable
183 "write_bytes" | "volatile_set_memory" => {
186 tcx.mk_ptr(ty::TypeAndMut {
188 mutbl: hir::MutMutable
195 "sqrtf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
196 "sqrtf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
199 vec!( tcx.types.f32, tcx.types.i32 ),
204 vec!( tcx.types.f64, tcx.types.i32 ),
207 "sinf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
208 "sinf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
209 "cosf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
210 "cosf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
213 vec!( tcx.types.f32, tcx.types.f32 ),
218 vec!( tcx.types.f64, tcx.types.f64 ),
221 "expf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
222 "expf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
223 "exp2f32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
224 "exp2f64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
225 "logf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
226 "logf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
227 "log10f32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
228 "log10f64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
229 "log2f32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
230 "log2f64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
233 vec!( tcx.types.f32, tcx.types.f32, tcx.types.f32 ),
238 vec!( tcx.types.f64, tcx.types.f64, tcx.types.f64 ),
241 "fabsf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
242 "fabsf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
243 "copysignf32" => (0, vec!( tcx.types.f32, tcx.types.f32 ), tcx.types.f32),
244 "copysignf64" => (0, vec!( tcx.types.f64, tcx.types.f64 ), tcx.types.f64),
245 "floorf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
246 "floorf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
247 "ceilf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
248 "ceilf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
249 "truncf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
250 "truncf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
251 "rintf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
252 "rintf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
253 "nearbyintf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
254 "nearbyintf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
255 "roundf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
256 "roundf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
257 "ctpop8" => (0, vec!( tcx.types.u8 ), tcx.types.u8),
258 "ctpop16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
259 "ctpop32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
260 "ctpop64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
261 "ctlz8" => (0, vec!( tcx.types.u8 ), tcx.types.u8),
262 "ctlz16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
263 "ctlz32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
264 "ctlz64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
265 "cttz8" => (0, vec!( tcx.types.u8 ), tcx.types.u8),
266 "cttz16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
267 "cttz32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
268 "cttz64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
269 "bswap16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
270 "bswap32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
271 "bswap64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
274 (1, vec!( tcx.mk_imm_ptr(param(ccx, 0)) ), param(ccx, 0)),
276 (1, vec!( tcx.mk_mut_ptr(param(ccx, 0)), param(ccx, 0) ), tcx.mk_nil()),
278 "i8_add_with_overflow" | "i8_sub_with_overflow" | "i8_mul_with_overflow" =>
279 (0, vec!(tcx.types.i8, tcx.types.i8),
280 tcx.mk_tup(vec!(tcx.types.i8, tcx.types.bool))),
282 "i16_add_with_overflow" | "i16_sub_with_overflow" | "i16_mul_with_overflow" =>
283 (0, vec!(tcx.types.i16, tcx.types.i16),
284 tcx.mk_tup(vec!(tcx.types.i16, tcx.types.bool))),
286 "i32_add_with_overflow" | "i32_sub_with_overflow" | "i32_mul_with_overflow" =>
287 (0, vec!(tcx.types.i32, tcx.types.i32),
288 tcx.mk_tup(vec!(tcx.types.i32, tcx.types.bool))),
290 "i64_add_with_overflow" | "i64_sub_with_overflow" | "i64_mul_with_overflow" =>
291 (0, vec!(tcx.types.i64, tcx.types.i64),
292 tcx.mk_tup(vec!(tcx.types.i64, tcx.types.bool))),
294 "u8_add_with_overflow" | "u8_sub_with_overflow" | "u8_mul_with_overflow" =>
295 (0, vec!(tcx.types.u8, tcx.types.u8),
296 tcx.mk_tup(vec!(tcx.types.u8, tcx.types.bool))),
298 "u16_add_with_overflow" | "u16_sub_with_overflow" | "u16_mul_with_overflow" =>
299 (0, vec!(tcx.types.u16, tcx.types.u16),
300 tcx.mk_tup(vec!(tcx.types.u16, tcx.types.bool))),
302 "u32_add_with_overflow" | "u32_sub_with_overflow" | "u32_mul_with_overflow"=>
303 (0, vec!(tcx.types.u32, tcx.types.u32),
304 tcx.mk_tup(vec!(tcx.types.u32, tcx.types.bool))),
306 "u64_add_with_overflow" | "u64_sub_with_overflow" | "u64_mul_with_overflow" =>
307 (0, vec!(tcx.types.u64, tcx.types.u64),
308 tcx.mk_tup(vec!(tcx.types.u64, tcx.types.bool))),
310 "unchecked_udiv" | "unchecked_sdiv" | "unchecked_urem" | "unchecked_srem" =>
311 (1, vec![param(ccx, 0), param(ccx, 0)], param(ccx, 0)),
313 "overflowing_add" | "overflowing_sub" | "overflowing_mul" =>
314 (1, vec![param(ccx, 0), param(ccx, 0)], param(ccx, 0)),
316 "return_address" => (0, vec![], tcx.mk_imm_ptr(tcx.types.u8)),
318 "assume" => (0, vec![tcx.types.bool], tcx.mk_nil()),
320 "discriminant_value" => (1, vec![
321 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1),
323 param(ccx, 0))], tcx.types.u64),
326 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
327 let fn_ty = ty::BareFnTy {
328 unsafety: hir::Unsafety::Normal,
330 sig: ty::Binder(FnSig {
331 inputs: vec![mut_u8],
332 output: ty::FnOutput::FnConverging(tcx.mk_nil()),
336 let fn_ty = tcx.mk_bare_fn(fn_ty);
337 (0, vec![tcx.mk_fn(None, fn_ty), mut_u8], mut_u8)
341 span_err!(tcx.sess, it.span, E0093,
342 "unrecognized intrinsic function: `{}`", *other);
346 (n_tps, inputs, ty::FnConverging(output))
348 equate_intrinsic_type(
358 /// Type-check `extern "platform-intrinsic" { ... }` functions.
359 pub fn check_platform_intrinsic_type(ccx: &CrateCtxt,
360 it: &hir::ForeignItem) {
362 let name = token::intern(&format!("P{}", n));
363 ccx.tcx.mk_param(subst::FnSpace, n, name)
367 let i_ty = tcx.lookup_item_type(tcx.map.local_def_id(it.id));
368 let i_n_tps = i_ty.generics.types.len(subst::FnSpace);
369 let name = it.name.as_str();
371 let (n_tps, inputs, output) = match &*name {
372 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
373 (2, vec![param(0), param(0)], param(1))
375 "simd_add" | "simd_sub" | "simd_mul" |
376 "simd_div" | "simd_shl" | "simd_shr" |
377 "simd_and" | "simd_or" | "simd_xor" => {
378 (1, vec![param(0), param(0)], param(0))
380 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
381 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
382 "simd_cast" => (2, vec![param(0)], param(1)),
383 name if name.starts_with("simd_shuffle") => {
384 match name["simd_shuffle".len()..].parse() {
386 let params = vec![param(0), param(0),
387 tcx.mk_ty(ty::TyArray(tcx.types.u32, n))];
388 (2, params, param(1))
391 span_err!(tcx.sess, it.span, E0439,
392 "invalid `simd_shuffle`, needs length: `{}`", name);
398 match intrinsics::Intrinsic::find(tcx, &name) {
400 // this function is a platform specific intrinsic
402 span_err!(tcx.sess, it.span, E0440,
403 "platform-specific intrinsic has wrong number of type \
404 parameters: found {}, expected 0",
409 let mut structural_to_nomimal = HashMap::new();
411 let sig = tcx.no_late_bound_regions(i_ty.ty.fn_sig()).unwrap();
412 if intr.inputs.len() != sig.inputs.len() {
413 span_err!(tcx.sess, it.span, E0444,
414 "platform-specific intrinsic has invalid number of \
415 arguments: found {}, expected {}",
416 intr.inputs.len(), sig.inputs.len());
419 let input_pairs = intr.inputs.iter().zip(&sig.inputs);
420 for (i, (expected_arg, arg)) in input_pairs.enumerate() {
421 match_intrinsic_type_to_type(tcx, &format!("argument {}", i + 1), it.span,
422 &mut structural_to_nomimal, expected_arg, arg);
424 match_intrinsic_type_to_type(tcx, "return value", it.span,
425 &mut structural_to_nomimal,
426 &intr.output, sig.output.unwrap());
430 span_err!(tcx.sess, it.span, E0441,
431 "unrecognized platform-specific intrinsic function: `{}`", name);
438 equate_intrinsic_type(
442 abi::PlatformIntrinsic,
444 ty::FnConverging(output)
448 // walk the expected type and the actual type in lock step, checking they're
449 // the same, in a kinda-structural way, i.e. `Vector`s have to be simd structs with
450 // exactly the right element type
451 fn match_intrinsic_type_to_type<'tcx, 'a>(
452 tcx: &ty::ctxt<'tcx>,
455 structural_to_nominal: &mut HashMap<&'a intrinsics::Type, ty::Ty<'tcx>>,
456 expected: &'a intrinsics::Type, t: ty::Ty<'tcx>)
458 use intrinsics::Type::*;
460 let simple_error = |real: &str, expected: &str| {
461 span_err!(tcx.sess, span, E0442,
462 "intrinsic {} has wrong type: found {}, expected {}",
463 position, real, expected)
467 Void => match t.sty {
468 ty::TyTuple(ref v) if v.is_empty() => {},
469 _ => simple_error(&format!("`{}`", t), "()"),
471 // (The width we pass to LLVM doesn't concern the type checker.)
472 Integer(signed, bits, _llvm_width) => match (signed, bits, &t.sty) {
473 (true, 8, &ty::TyInt(ast::IntTy::TyI8)) |
474 (false, 8, &ty::TyUint(ast::UintTy::TyU8)) |
475 (true, 16, &ty::TyInt(ast::IntTy::TyI16)) |
476 (false, 16, &ty::TyUint(ast::UintTy::TyU16)) |
477 (true, 32, &ty::TyInt(ast::IntTy::TyI32)) |
478 (false, 32, &ty::TyUint(ast::UintTy::TyU32)) |
479 (true, 64, &ty::TyInt(ast::IntTy::TyI64)) |
480 (false, 64, &ty::TyUint(ast::UintTy::TyU64)) => {},
481 _ => simple_error(&format!("`{}`", t),
483 if signed {"i"} else {"u"},
486 Float(bits) => match (bits, &t.sty) {
487 (32, &ty::TyFloat(ast::FloatTy::TyF32)) |
488 (64, &ty::TyFloat(ast::FloatTy::TyF64)) => {},
489 _ => simple_error(&format!("`{}`", t),
490 &format!("`f{n}`", n = bits)),
492 Pointer(ref inner_expected, ref _llvm_type, const_) => {
494 ty::TyRawPtr(ty::TypeAndMut { ty, mutbl }) => {
495 if (mutbl == hir::MutImmutable) != const_ {
496 simple_error(&format!("`{}`", t),
497 if const_ {"const pointer"} else {"mut pointer"})
499 match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
502 _ => simple_error(&format!("`{}`", t),
503 &format!("raw pointer")),
506 Vector(ref inner_expected, ref _llvm_type, len) => {
508 simple_error(&format!("non-simd type `{}`", t),
512 let t_len = t.simd_size(tcx);
513 if len as usize != t_len {
514 simple_error(&format!("vector with length {}", t_len),
515 &format!("length {}", len));
518 let t_ty = t.simd_type(tcx);
520 // check that a given structural type always has the same an intrinsic definition
521 let previous = structural_to_nominal.entry(expected).or_insert(t);
523 // this gets its own error code because it is non-trivial
524 span_err!(tcx.sess, span, E0443,
525 "intrinsic {} has wrong type: found `{}`, expected `{}` which \
526 was used for this vector type previously in this signature",
533 match_intrinsic_type_to_type(tcx,
536 structural_to_nominal,
540 Aggregate(_flatten, ref expected_contents) => {
542 ty::TyTuple(ref contents) => {
543 if contents.len() != expected_contents.len() {
544 simple_error(&format!("tuple with length {}", contents.len()),
545 &format!("tuple with length {}", expected_contents.len()));
548 for (e, c) in expected_contents.iter().zip(contents) {
549 match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
553 _ => simple_error(&format!("`{}`", t),