]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
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. | |
4 | // | |
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. | |
10 | ||
11 | //! Type-checking for the rust-intrinsic and platform-intrinsic | |
12 | //! intrinsics that the compiler exposes. | |
13 | ||
e9174d1e | 14 | use intrinsics; |
476ff2be | 15 | use rustc::traits::{ObligationCause, ObligationCauseCode}; |
8bb4bdeb | 16 | use rustc::ty::{self, TyCtxt, Ty}; |
476ff2be | 17 | use rustc::util::nodemap::FxHashMap; |
8bb4bdeb | 18 | use require_same_types; |
e9174d1e | 19 | |
83c7162d | 20 | use rustc_target::spec::abi::Abi; |
b039eaaf | 21 | use syntax::ast; |
476ff2be | 22 | use syntax::symbol::Symbol; |
3157f602 | 23 | use syntax_pos::Span; |
e9174d1e | 24 | |
54a0048b | 25 | use rustc::hir; |
e9174d1e | 26 | |
476ff2be SL |
27 | use std::iter; |
28 | ||
8bb4bdeb | 29 | fn equate_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, |
a7813a04 | 30 | it: &hir::ForeignItem, |
e9174d1e | 31 | n_tps: usize, |
7453a54e | 32 | abi: Abi, |
5bcae85e SL |
33 | inputs: Vec<Ty<'tcx>>, |
34 | output: Ty<'tcx>) { | |
32a655c1 | 35 | let def_id = tcx.hir.local_def_id(it.id); |
54a0048b | 36 | |
041b39d2 XL |
37 | match it.node { |
38 | hir::ForeignItemFn(..) => {} | |
39 | _ => { | |
40 | struct_span_err!(tcx.sess, it.span, E0622, | |
41 | "intrinsic must be a function") | |
42 | .span_label(it.span, "expected a function") | |
43 | .emit(); | |
44 | return; | |
45 | } | |
46 | } | |
54a0048b | 47 | |
7cac9316 | 48 | let i_n_tps = tcx.generics_of(def_id).types.len(); |
e9174d1e | 49 | if i_n_tps != n_tps { |
9e0c209e | 50 | let span = match it.node { |
32a655c1 | 51 | hir::ForeignItemFn(_, _, ref generics) => generics.span, |
041b39d2 | 52 | _ => bug!() |
9e0c209e SL |
53 | }; |
54 | ||
55 | struct_span_err!(tcx.sess, span, E0094, | |
56 | "intrinsic has wrong number of type \ | |
57 | parameters: found {}, expected {}", | |
58 | i_n_tps, n_tps) | |
7cac9316 | 59 | .span_label(span, format!("expected {} type parameter", n_tps)) |
9e0c209e | 60 | .emit(); |
041b39d2 | 61 | return; |
e9174d1e | 62 | } |
041b39d2 | 63 | |
83c7162d | 64 | let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig( |
041b39d2 XL |
65 | inputs.into_iter(), |
66 | output, | |
67 | false, | |
68 | hir::Unsafety::Unsafe, | |
69 | abi | |
70 | ))); | |
71 | let cause = ObligationCause::new(it.span, it.id, ObligationCauseCode::IntrinsicType); | |
72 | require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty); | |
e9174d1e SL |
73 | } |
74 | ||
75 | /// Remember to add all intrinsics here, in librustc_trans/trans/intrinsic.rs, | |
76 | /// and in libcore/intrinsics.rs | |
8bb4bdeb XL |
77 | pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, |
78 | it: &hir::ForeignItem) { | |
83c7162d | 79 | let param = |n| tcx.mk_param(n, Symbol::intern(&format!("P{}", n)).as_interned_str()); |
b039eaaf | 80 | let name = it.name.as_str(); |
e9174d1e SL |
81 | let (n_tps, inputs, output) = if name.starts_with("atomic_") { |
82 | let split : Vec<&str> = name.split('_').collect(); | |
83 | assert!(split.len() >= 2, "Atomic intrinsic not correct format"); | |
84 | ||
85 | //We only care about the operation here | |
86 | let (n_tps, inputs, output) = match split[1] { | |
8bb4bdeb XL |
87 | "cxchg" | "cxchgweak" => (1, vec![tcx.mk_mut_ptr(param(0)), |
88 | param(0), | |
89 | param(0)], | |
0531ce1d | 90 | tcx.intern_tup(&[param(0), tcx.types.bool])), |
8bb4bdeb XL |
91 | "load" => (1, vec![tcx.mk_imm_ptr(param(0))], |
92 | param(0)), | |
93 | "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], | |
e9174d1e SL |
94 | tcx.mk_nil()), |
95 | ||
96 | "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" | | |
97 | "min" | "umax" | "umin" => { | |
8bb4bdeb XL |
98 | (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], |
99 | param(0)) | |
e9174d1e SL |
100 | } |
101 | "fence" | "singlethreadfence" => { | |
102 | (0, Vec::new(), tcx.mk_nil()) | |
103 | } | |
104 | op => { | |
5bcae85e SL |
105 | struct_span_err!(tcx.sess, it.span, E0092, |
106 | "unrecognized atomic operation function: `{}`", op) | |
7cac9316 | 107 | .span_label(it.span, "unrecognized atomic operation") |
5bcae85e | 108 | .emit(); |
e9174d1e SL |
109 | return; |
110 | } | |
111 | }; | |
5bcae85e | 112 | (n_tps, inputs, output) |
e9174d1e | 113 | } else if &name[..] == "abort" || &name[..] == "unreachable" { |
5bcae85e | 114 | (0, Vec::new(), tcx.types.never) |
e9174d1e SL |
115 | } else { |
116 | let (n_tps, inputs, output) = match &name[..] { | |
117 | "breakpoint" => (0, Vec::new(), tcx.mk_nil()), | |
118 | "size_of" | | |
8bb4bdeb | 119 | "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize), |
e9174d1e SL |
120 | "size_of_val" | "min_align_of_val" => { |
121 | (1, vec![ | |
122 | tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1), | |
123 | ty::BrAnon(0))), | |
8bb4bdeb XL |
124 | param(0)) |
125 | ], tcx.types.usize) | |
e9174d1e | 126 | } |
8bb4bdeb XL |
127 | "rustc_peek" => (1, vec![param(0)], param(0)), |
128 | "init" => (1, Vec::new(), param(0)), | |
129 | "uninit" => (1, Vec::new(), param(0)), | |
8bb4bdeb | 130 | "transmute" => (2, vec![ param(0) ], param(1)), |
e9174d1e SL |
131 | "move_val_init" => { |
132 | (1, | |
c30ab7b3 | 133 | vec![ |
8bb4bdeb XL |
134 | tcx.mk_mut_ptr(param(0)), |
135 | param(0) | |
c30ab7b3 | 136 | ], |
e9174d1e SL |
137 | tcx.mk_nil()) |
138 | } | |
7cac9316 XL |
139 | "prefetch_read_data" | "prefetch_write_data" | |
140 | "prefetch_read_instruction" | "prefetch_write_instruction" => { | |
141 | (1, vec![tcx.mk_ptr(ty::TypeAndMut { | |
142 | ty: param(0), | |
143 | mutbl: hir::MutImmutable | |
144 | }), tcx.types.i32], | |
145 | tcx.mk_nil()) | |
146 | } | |
e9174d1e | 147 | "drop_in_place" => { |
8bb4bdeb | 148 | (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_nil()) |
e9174d1e | 149 | } |
8bb4bdeb | 150 | "needs_drop" => (1, Vec::new(), tcx.types.bool), |
e9174d1e SL |
151 | |
152 | "type_name" => (1, Vec::new(), tcx.mk_static_str()), | |
8bb4bdeb | 153 | "type_id" => (1, Vec::new(), tcx.types.u64), |
e9174d1e SL |
154 | "offset" | "arith_offset" => { |
155 | (1, | |
c30ab7b3 | 156 | vec![ |
e9174d1e | 157 | tcx.mk_ptr(ty::TypeAndMut { |
8bb4bdeb | 158 | ty: param(0), |
e9174d1e SL |
159 | mutbl: hir::MutImmutable |
160 | }), | |
8bb4bdeb | 161 | tcx.types.isize |
c30ab7b3 | 162 | ], |
e9174d1e | 163 | tcx.mk_ptr(ty::TypeAndMut { |
8bb4bdeb | 164 | ty: param(0), |
e9174d1e SL |
165 | mutbl: hir::MutImmutable |
166 | })) | |
167 | } | |
168 | "copy" | "copy_nonoverlapping" => { | |
169 | (1, | |
c30ab7b3 | 170 | vec![ |
e9174d1e | 171 | tcx.mk_ptr(ty::TypeAndMut { |
8bb4bdeb | 172 | ty: param(0), |
e9174d1e SL |
173 | mutbl: hir::MutImmutable |
174 | }), | |
175 | tcx.mk_ptr(ty::TypeAndMut { | |
8bb4bdeb | 176 | ty: param(0), |
e9174d1e SL |
177 | mutbl: hir::MutMutable |
178 | }), | |
179 | tcx.types.usize, | |
c30ab7b3 | 180 | ], |
e9174d1e SL |
181 | tcx.mk_nil()) |
182 | } | |
183 | "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => { | |
184 | (1, | |
c30ab7b3 | 185 | vec![ |
e9174d1e | 186 | tcx.mk_ptr(ty::TypeAndMut { |
8bb4bdeb | 187 | ty: param(0), |
e9174d1e SL |
188 | mutbl: hir::MutMutable |
189 | }), | |
190 | tcx.mk_ptr(ty::TypeAndMut { | |
8bb4bdeb | 191 | ty: param(0), |
e9174d1e SL |
192 | mutbl: hir::MutImmutable |
193 | }), | |
194 | tcx.types.usize, | |
c30ab7b3 | 195 | ], |
e9174d1e SL |
196 | tcx.mk_nil()) |
197 | } | |
198 | "write_bytes" | "volatile_set_memory" => { | |
199 | (1, | |
c30ab7b3 | 200 | vec![ |
e9174d1e | 201 | tcx.mk_ptr(ty::TypeAndMut { |
8bb4bdeb | 202 | ty: param(0), |
e9174d1e SL |
203 | mutbl: hir::MutMutable |
204 | }), | |
205 | tcx.types.u8, | |
206 | tcx.types.usize, | |
c30ab7b3 | 207 | ], |
e9174d1e SL |
208 | tcx.mk_nil()) |
209 | } | |
c30ab7b3 SL |
210 | "sqrtf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), |
211 | "sqrtf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
e9174d1e SL |
212 | "powif32" => { |
213 | (0, | |
c30ab7b3 | 214 | vec![ tcx.types.f32, tcx.types.i32 ], |
e9174d1e SL |
215 | tcx.types.f32) |
216 | } | |
217 | "powif64" => { | |
218 | (0, | |
c30ab7b3 | 219 | vec![ tcx.types.f64, tcx.types.i32 ], |
e9174d1e SL |
220 | tcx.types.f64) |
221 | } | |
c30ab7b3 SL |
222 | "sinf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), |
223 | "sinf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
224 | "cosf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
225 | "cosf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
e9174d1e SL |
226 | "powf32" => { |
227 | (0, | |
c30ab7b3 | 228 | vec![ tcx.types.f32, tcx.types.f32 ], |
e9174d1e SL |
229 | tcx.types.f32) |
230 | } | |
231 | "powf64" => { | |
232 | (0, | |
c30ab7b3 | 233 | vec![ tcx.types.f64, tcx.types.f64 ], |
e9174d1e SL |
234 | tcx.types.f64) |
235 | } | |
c30ab7b3 SL |
236 | "expf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), |
237 | "expf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
238 | "exp2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
239 | "exp2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
240 | "logf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
241 | "logf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
242 | "log10f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
243 | "log10f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
244 | "log2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
245 | "log2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
e9174d1e SL |
246 | "fmaf32" => { |
247 | (0, | |
c30ab7b3 | 248 | vec![ tcx.types.f32, tcx.types.f32, tcx.types.f32 ], |
e9174d1e SL |
249 | tcx.types.f32) |
250 | } | |
251 | "fmaf64" => { | |
252 | (0, | |
c30ab7b3 | 253 | vec![ tcx.types.f64, tcx.types.f64, tcx.types.f64 ], |
e9174d1e SL |
254 | tcx.types.f64) |
255 | } | |
c30ab7b3 SL |
256 | "fabsf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), |
257 | "fabsf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
258 | "copysignf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32), | |
259 | "copysignf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64), | |
260 | "floorf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
261 | "floorf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
262 | "ceilf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
263 | "ceilf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
264 | "truncf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
265 | "truncf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
266 | "rintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
267 | "rintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
268 | "nearbyintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
269 | "nearbyintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
270 | "roundf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32), | |
271 | "roundf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64), | |
e9174d1e SL |
272 | |
273 | "volatile_load" => | |
8bb4bdeb | 274 | (1, vec![ tcx.mk_imm_ptr(param(0)) ], param(0)), |
e9174d1e | 275 | "volatile_store" => |
8bb4bdeb | 276 | (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_nil()), |
e9174d1e | 277 | |
0531ce1d XL |
278 | "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | |
279 | "bswap" | "bitreverse" => | |
041b39d2 | 280 | (1, vec![param(0)], param(0)), |
e9174d1e | 281 | |
92a42be0 | 282 | "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => |
8bb4bdeb | 283 | (1, vec![param(0), param(0)], |
0531ce1d | 284 | tcx.intern_tup(&[param(0), tcx.types.bool])), |
e9174d1e | 285 | |
0531ce1d | 286 | "unchecked_div" | "unchecked_rem" | "exact_div" => |
8bb4bdeb | 287 | (1, vec![param(0), param(0)], param(0)), |
cc61c64b XL |
288 | "unchecked_shl" | "unchecked_shr" => |
289 | (1, vec![param(0), param(0)], param(0)), | |
e9174d1e SL |
290 | |
291 | "overflowing_add" | "overflowing_sub" | "overflowing_mul" => | |
8bb4bdeb | 292 | (1, vec![param(0), param(0)], param(0)), |
54a0048b | 293 | "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => |
8bb4bdeb | 294 | (1, vec![param(0), param(0)], param(0)), |
e9174d1e | 295 | |
e9174d1e | 296 | "assume" => (0, vec![tcx.types.bool], tcx.mk_nil()), |
9e0c209e SL |
297 | "likely" => (0, vec![tcx.types.bool], tcx.types.bool), |
298 | "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool), | |
e9174d1e SL |
299 | |
300 | "discriminant_value" => (1, vec![ | |
301 | tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1), | |
302 | ty::BrAnon(0))), | |
8bb4bdeb | 303 | param(0))], tcx.types.u64), |
e9174d1e SL |
304 | |
305 | "try" => { | |
306 | let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8); | |
83c7162d | 307 | let fn_ty = ty::Binder::bind(tcx.mk_fn_sig( |
8bb4bdeb XL |
308 | iter::once(mut_u8), |
309 | tcx.mk_nil(), | |
310 | false, | |
311 | hir::Unsafety::Normal, | |
312 | Abi::Rust, | |
313 | )); | |
54a0048b | 314 | (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32) |
e9174d1e SL |
315 | } |
316 | ||
ea8adc8c XL |
317 | "align_offset" => { |
318 | let ptr_ty = tcx.mk_imm_ptr(tcx.mk_nil()); | |
319 | (0, vec![ptr_ty, tcx.types.usize], tcx.types.usize) | |
320 | }, | |
321 | ||
ff7c6d11 XL |
322 | "nontemporal_store" => { |
323 | (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_nil()) | |
324 | } | |
325 | ||
e9174d1e | 326 | ref other => { |
9e0c209e SL |
327 | struct_span_err!(tcx.sess, it.span, E0093, |
328 | "unrecognized intrinsic function: `{}`", | |
329 | *other) | |
7cac9316 | 330 | .span_label(it.span, "unrecognized intrinsic") |
9e0c209e | 331 | .emit(); |
e9174d1e SL |
332 | return; |
333 | } | |
334 | }; | |
5bcae85e | 335 | (n_tps, inputs, output) |
e9174d1e | 336 | }; |
8bb4bdeb | 337 | equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, inputs, output) |
e9174d1e SL |
338 | } |
339 | ||
340 | /// Type-check `extern "platform-intrinsic" { ... }` functions. | |
8bb4bdeb XL |
341 | pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, |
342 | it: &hir::ForeignItem) { | |
e9174d1e | 343 | let param = |n| { |
83c7162d | 344 | let name = Symbol::intern(&format!("P{}", n)).as_interned_str(); |
8bb4bdeb | 345 | tcx.mk_param(n, name) |
e9174d1e SL |
346 | }; |
347 | ||
32a655c1 | 348 | let def_id = tcx.hir.local_def_id(it.id); |
7cac9316 | 349 | let i_n_tps = tcx.generics_of(def_id).types.len(); |
b039eaaf | 350 | let name = it.name.as_str(); |
e9174d1e SL |
351 | |
352 | let (n_tps, inputs, output) = match &*name { | |
353 | "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => { | |
354 | (2, vec![param(0), param(0)], param(1)) | |
355 | } | |
abe05a73 | 356 | "simd_add" | "simd_sub" | "simd_mul" | "simd_rem" | |
e9174d1e | 357 | "simd_div" | "simd_shl" | "simd_shr" | |
0531ce1d XL |
358 | "simd_and" | "simd_or" | "simd_xor" | |
359 | "simd_fmin" | "simd_fmax" => { | |
e9174d1e SL |
360 | (1, vec![param(0), param(0)], param(0)) |
361 | } | |
362 | "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)), | |
363 | "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)), | |
364 | "simd_cast" => (2, vec![param(0)], param(1)), | |
0531ce1d XL |
365 | "simd_select" => (2, vec![param(0), param(1), param(1)], param(1)), |
366 | "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool), | |
367 | "simd_reduce_add_ordered" | "simd_reduce_mul_ordered" | |
368 | => (2, vec![param(0), param(1)], param(1)), | |
369 | "simd_reduce_add_unordered" | "simd_reduce_mul_unordered" | | |
370 | "simd_reduce_and" | "simd_reduce_or" | "simd_reduce_xor" | | |
371 | "simd_reduce_min" | "simd_reduce_max" | | |
372 | "simd_reduce_min_nanless" | "simd_reduce_max_nanless" | |
373 | => (2, vec![param(0)], param(1)), | |
e9174d1e SL |
374 | name if name.starts_with("simd_shuffle") => { |
375 | match name["simd_shuffle".len()..].parse() { | |
376 | Ok(n) => { | |
377 | let params = vec![param(0), param(0), | |
ea8adc8c | 378 | tcx.mk_array(tcx.types.u32, n)]; |
e9174d1e SL |
379 | (2, params, param(1)) |
380 | } | |
381 | Err(_) => { | |
382 | span_err!(tcx.sess, it.span, E0439, | |
383 | "invalid `simd_shuffle`, needs length: `{}`", name); | |
384 | return | |
385 | } | |
386 | } | |
387 | } | |
388 | _ => { | |
54a0048b | 389 | match intrinsics::Intrinsic::find(&name) { |
e9174d1e SL |
390 | Some(intr) => { |
391 | // this function is a platform specific intrinsic | |
392 | if i_n_tps != 0 { | |
393 | span_err!(tcx.sess, it.span, E0440, | |
394 | "platform-specific intrinsic has wrong number of type \ | |
395 | parameters: found {}, expected 0", | |
396 | i_n_tps); | |
397 | return | |
398 | } | |
399 | ||
476ff2be | 400 | let mut structural_to_nomimal = FxHashMap(); |
e9174d1e | 401 | |
041b39d2 | 402 | let sig = tcx.fn_sig(def_id); |
ff7c6d11 | 403 | let sig = sig.no_late_bound_regions().unwrap(); |
476ff2be | 404 | if intr.inputs.len() != sig.inputs().len() { |
e9174d1e SL |
405 | span_err!(tcx.sess, it.span, E0444, |
406 | "platform-specific intrinsic has invalid number of \ | |
407 | arguments: found {}, expected {}", | |
476ff2be | 408 | sig.inputs().len(), intr.inputs.len()); |
e9174d1e SL |
409 | return |
410 | } | |
476ff2be | 411 | let input_pairs = intr.inputs.iter().zip(sig.inputs()); |
e9174d1e | 412 | for (i, (expected_arg, arg)) in input_pairs.enumerate() { |
8bb4bdeb | 413 | match_intrinsic_type_to_type(tcx, &format!("argument {}", i + 1), it.span, |
e9174d1e SL |
414 | &mut structural_to_nomimal, expected_arg, arg); |
415 | } | |
8bb4bdeb | 416 | match_intrinsic_type_to_type(tcx, "return value", it.span, |
e9174d1e | 417 | &mut structural_to_nomimal, |
476ff2be | 418 | &intr.output, sig.output()); |
e9174d1e SL |
419 | return |
420 | } | |
421 | None => { | |
422 | span_err!(tcx.sess, it.span, E0441, | |
423 | "unrecognized platform-specific intrinsic function: `{}`", name); | |
424 | return; | |
425 | } | |
426 | } | |
427 | } | |
428 | }; | |
429 | ||
8bb4bdeb | 430 | equate_intrinsic_type(tcx, it, n_tps, Abi::PlatformIntrinsic, |
5bcae85e | 431 | inputs, output) |
e9174d1e SL |
432 | } |
433 | ||
434 | // walk the expected type and the actual type in lock step, checking they're | |
435 | // the same, in a kinda-structural way, i.e. `Vector`s have to be simd structs with | |
436 | // exactly the right element type | |
8bb4bdeb XL |
437 | fn match_intrinsic_type_to_type<'a, 'tcx>( |
438 | tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
e9174d1e SL |
439 | position: &str, |
440 | span: Span, | |
ea8adc8c XL |
441 | structural_to_nominal: &mut FxHashMap<&'a intrinsics::Type, Ty<'tcx>>, |
442 | expected: &'a intrinsics::Type, t: Ty<'tcx>) | |
e9174d1e SL |
443 | { |
444 | use intrinsics::Type::*; | |
445 | ||
446 | let simple_error = |real: &str, expected: &str| { | |
8bb4bdeb | 447 | span_err!(tcx.sess, span, E0442, |
e9174d1e SL |
448 | "intrinsic {} has wrong type: found {}, expected {}", |
449 | position, real, expected) | |
450 | }; | |
451 | ||
452 | match *expected { | |
453 | Void => match t.sty { | |
0531ce1d | 454 | ty::TyTuple(ref v) if v.is_empty() => {}, |
e9174d1e SL |
455 | _ => simple_error(&format!("`{}`", t), "()"), |
456 | }, | |
457 | // (The width we pass to LLVM doesn't concern the type checker.) | |
458 | Integer(signed, bits, _llvm_width) => match (signed, bits, &t.sty) { | |
7453a54e SL |
459 | (true, 8, &ty::TyInt(ast::IntTy::I8)) | |
460 | (false, 8, &ty::TyUint(ast::UintTy::U8)) | | |
461 | (true, 16, &ty::TyInt(ast::IntTy::I16)) | | |
462 | (false, 16, &ty::TyUint(ast::UintTy::U16)) | | |
463 | (true, 32, &ty::TyInt(ast::IntTy::I32)) | | |
464 | (false, 32, &ty::TyUint(ast::UintTy::U32)) | | |
465 | (true, 64, &ty::TyInt(ast::IntTy::I64)) | | |
32a655c1 SL |
466 | (false, 64, &ty::TyUint(ast::UintTy::U64)) | |
467 | (true, 128, &ty::TyInt(ast::IntTy::I128)) | | |
468 | (false, 128, &ty::TyUint(ast::UintTy::U128)) => {}, | |
e9174d1e SL |
469 | _ => simple_error(&format!("`{}`", t), |
470 | &format!("`{}{n}`", | |
471 | if signed {"i"} else {"u"}, | |
472 | n = bits)), | |
473 | }, | |
474 | Float(bits) => match (bits, &t.sty) { | |
7453a54e SL |
475 | (32, &ty::TyFloat(ast::FloatTy::F32)) | |
476 | (64, &ty::TyFloat(ast::FloatTy::F64)) => {}, | |
e9174d1e SL |
477 | _ => simple_error(&format!("`{}`", t), |
478 | &format!("`f{n}`", n = bits)), | |
479 | }, | |
480 | Pointer(ref inner_expected, ref _llvm_type, const_) => { | |
481 | match t.sty { | |
482 | ty::TyRawPtr(ty::TypeAndMut { ty, mutbl }) => { | |
483 | if (mutbl == hir::MutImmutable) != const_ { | |
484 | simple_error(&format!("`{}`", t), | |
485 | if const_ {"const pointer"} else {"mut pointer"}) | |
486 | } | |
8bb4bdeb | 487 | match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal, |
e9174d1e SL |
488 | inner_expected, ty) |
489 | } | |
92a42be0 | 490 | _ => simple_error(&format!("`{}`", t), "raw pointer"), |
e9174d1e SL |
491 | } |
492 | } | |
493 | Vector(ref inner_expected, ref _llvm_type, len) => { | |
494 | if !t.is_simd() { | |
92a42be0 | 495 | simple_error(&format!("non-simd type `{}`", t), "simd type"); |
e9174d1e SL |
496 | return; |
497 | } | |
8bb4bdeb | 498 | let t_len = t.simd_size(tcx); |
e9174d1e SL |
499 | if len as usize != t_len { |
500 | simple_error(&format!("vector with length {}", t_len), | |
501 | &format!("length {}", len)); | |
502 | return; | |
503 | } | |
8bb4bdeb | 504 | let t_ty = t.simd_type(tcx); |
e9174d1e SL |
505 | { |
506 | // check that a given structural type always has the same an intrinsic definition | |
507 | let previous = structural_to_nominal.entry(expected).or_insert(t); | |
508 | if *previous != t { | |
509 | // this gets its own error code because it is non-trivial | |
8bb4bdeb | 510 | span_err!(tcx.sess, span, E0443, |
e9174d1e SL |
511 | "intrinsic {} has wrong type: found `{}`, expected `{}` which \ |
512 | was used for this vector type previously in this signature", | |
513 | position, | |
514 | t, | |
515 | *previous); | |
516 | return; | |
517 | } | |
518 | } | |
8bb4bdeb | 519 | match_intrinsic_type_to_type(tcx, |
e9174d1e SL |
520 | position, |
521 | span, | |
522 | structural_to_nominal, | |
523 | inner_expected, | |
524 | t_ty) | |
525 | } | |
526 | Aggregate(_flatten, ref expected_contents) => { | |
527 | match t.sty { | |
0531ce1d | 528 | ty::TyTuple(contents) => { |
e9174d1e SL |
529 | if contents.len() != expected_contents.len() { |
530 | simple_error(&format!("tuple with length {}", contents.len()), | |
531 | &format!("tuple with length {}", expected_contents.len())); | |
532 | return | |
533 | } | |
534 | for (e, c) in expected_contents.iter().zip(contents) { | |
8bb4bdeb | 535 | match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal, |
e9174d1e SL |
536 | e, c) |
537 | } | |
538 | } | |
539 | _ => simple_error(&format!("`{}`", t), | |
7cac9316 | 540 | "tuple"), |
e9174d1e SL |
541 | } |
542 | } | |
543 | } | |
544 | } |