]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/lang_items.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / librustc / middle / lang_items.rs
CommitLineData
223e47cc
LB
1// Copyright 2012 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
223e47cc
LB
11// Detecting language items.
12//
13// Language items are items that represent concepts intrinsic to the language
14// itself. Examples are:
15//
1a4d82fc 16// * Traits that specify "kinds"; e.g. "Sync", "Send".
223e47cc 17//
970d7e83 18// * Traits that represent operators; e.g. "Add", "Sub", "Index".
223e47cc
LB
19//
20// * Functions called by the compiler itself.
21
1a4d82fc 22pub use self::LangItem::*;
223e47cc 23
54a0048b 24use hir::def_id::DefId;
0bf4aa26 25use hir::check_attr::Target;
ea8adc8c 26use ty::{self, TyCtxt};
1a4d82fc 27use middle::weak_lang_items;
476ff2be 28use util::nodemap::FxHashMap;
223e47cc 29
b039eaaf 30use syntax::ast;
476ff2be 31use syntax::symbol::Symbol;
2c00a5a8 32use syntax_pos::Span;
476ff2be 33use hir::itemlikevisit::ItemLikeVisitor;
54a0048b 34use hir;
1a4d82fc 35
1a4d82fc
JJ
36// The actual lang items defined come at the end of this file in one handy table.
37// So you probably just want to nip down to the end.
3157f602 38macro_rules! language_item_table {
1a4d82fc 39 (
0bf4aa26 40 $( $variant:ident, $name:expr, $method:ident, $target:path; )*
1a4d82fc
JJ
41 ) => {
42
9346a6ac 43enum_from_u32! {
ff7c6d11 44 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
9346a6ac
AL
45 pub enum LangItem {
46 $($variant,)*
47 }
223e47cc
LB
48}
49
ea8adc8c
XL
50impl LangItem {
51 fn name(self) -> &'static str {
52 match self {
53 $( $variant => $name, )*
54 }
55 }
56}
57
223e47cc 58pub struct LanguageItems {
e9174d1e 59 pub items: Vec<Option<DefId>>,
1a4d82fc 60 pub missing: Vec<LangItem>,
223e47cc
LB
61}
62
970d7e83 63impl LanguageItems {
223e47cc 64 pub fn new() -> LanguageItems {
e9174d1e 65 fn foo(_: LangItem) -> Option<DefId> { None }
1a4d82fc 66
223e47cc 67 LanguageItems {
c30ab7b3 68 items: vec![$(foo($variant)),*],
1a4d82fc 69 missing: Vec::new(),
223e47cc
LB
70 }
71 }
72
7453a54e
SL
73 pub fn items(&self) -> &[Option<DefId>] {
74 &*self.items
223e47cc
LB
75 }
76
e9174d1e 77 pub fn require(&self, it: LangItem) -> Result<DefId, String> {
ea8adc8c 78 self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
c1a9b12d
SL
79 }
80
e9174d1e 81 pub fn fn_trait_kind(&self, id: DefId) -> Option<ty::ClosureKind> {
ea8adc8c
XL
82 match Some(id) {
83 x if x == self.fn_trait() => Some(ty::ClosureKind::Fn),
84 x if x == self.fn_mut_trait() => Some(ty::ClosureKind::FnMut),
85 x if x == self.fn_once_trait() => Some(ty::ClosureKind::FnOnce),
86 _ => None
1a4d82fc 87 }
223e47cc 88 }
1a4d82fc
JJ
89
90 $(
91 #[allow(dead_code)]
e9174d1e 92 pub fn $method(&self) -> Option<DefId> {
c34b1796 93 self.items[$variant as usize]
1a4d82fc
JJ
94 }
95 )*
223e47cc
LB
96}
97
b039eaaf 98struct LanguageItemCollector<'a, 'tcx: 'a> {
970d7e83 99 items: LanguageItems,
ea8adc8c 100 tcx: TyCtxt<'a, 'tcx, 'tcx>,
0bf4aa26 101 item_refs: FxHashMap<&'static str, (usize, Target)>,
223e47cc
LB
102}
103
476ff2be 104impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'a, 'tcx> {
e9174d1e 105 fn visit_item(&mut self, item: &hir::Item) {
2c00a5a8 106 if let Some((value, span)) = extract(&item.attrs) {
0bf4aa26
XL
107 let actual_target = Target::from_item(item);
108 match self.item_refs.get(&*value.as_str()).cloned() {
109 // Known lang item with attribute on correct target.
110 Some((item_index, expected_target)) if actual_target == expected_target => {
111 let def_id = self.tcx.hir.local_def_id(item.id);
112 self.collect_item(item_index, def_id);
113 },
114 // Known lang item with attribute on incorrect target.
115 Some((_, expected_target)) => {
116 let mut err = struct_span_err!(
117 self.tcx.sess, span, E0718,
118 "`{}` language item must be applied to a {}",
119 value, expected_target,
120 );
121 err.span_label(
122 span,
123 format!(
124 "attribute should be applied to a {}, not a {}",
125 expected_target, actual_target,
126 ),
127 );
128 err.emit();
129 },
130 // Unknown lang item.
131 _ => {
132 let mut err = struct_span_err!(
133 self.tcx.sess, span, E0522,
134 "definition of an unknown language item: `{}`",
135 value
136 );
137 err.span_label(
138 span,
139 format!("definition of unknown language item `{}`", value)
140 );
141 err.emit();
142 },
1a4d82fc 143 }
1a4d82fc 144 }
1a4d82fc 145 }
476ff2be 146
32a655c1
SL
147 fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
148 // at present, lang items are always items, not trait items
149 }
150
476ff2be
SL
151 fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
152 // at present, lang items are always items, not impl items
153 }
1a4d82fc
JJ
154}
155
b039eaaf 156impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
ea8adc8c 157 fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItemCollector<'a, 'tcx> {
0bf4aa26 158 let mut item_refs = FxHashMap::default();
1a4d82fc 159
0bf4aa26 160 $( item_refs.insert($name, ($variant as usize, $target)); )*
970d7e83
LB
161
162 LanguageItemCollector {
ea8adc8c 163 tcx,
970d7e83 164 items: LanguageItems::new(),
041b39d2 165 item_refs,
970d7e83
LB
166 }
167 }
168
ea8adc8c 169 fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
223e47cc 170 // Check for duplicates.
0bf4aa26
XL
171 if let Some(original_def_id) = self.items.items[item_index] {
172 if original_def_id != item_def_id {
ea8adc8c
XL
173 let name = LangItem::from_u32(item_index as u32).unwrap().name();
174 let mut err = match self.tcx.hir.span_if_local(item_def_id) {
a7813a04 175 Some(span) => struct_span_err!(
ea8adc8c 176 self.tcx.sess,
a7813a04
XL
177 span,
178 E0152,
179 "duplicate lang item found: `{}`.",
180 name),
ea8adc8c 181 None => self.tcx.sess.struct_err(&format!(
a7813a04 182 "duplicate lang item in crate `{}`: `{}`.",
ea8adc8c 183 self.tcx.crate_name(item_def_id.krate),
a7813a04
XL
184 name)),
185 };
ea8adc8c 186 if let Some(span) = self.tcx.hir.span_if_local(original_def_id) {
0bf4aa26 187 span_note!(&mut err, span, "first defined here.");
54a0048b
SL
188 } else {
189 err.note(&format!("first defined in crate `{}`.",
ea8adc8c 190 self.tcx.crate_name(original_def_id.krate)));
54a0048b
SL
191 }
192 err.emit();
223e47cc 193 }
223e47cc
LB
194 }
195
196 // Matched.
197 self.items.items[item_index] = Some(item_def_id);
198 }
1a4d82fc 199}
223e47cc 200
2c00a5a8 201pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
85aaf69f 202 for attribute in attrs {
cc61c64b
XL
203 if attribute.check_name("lang") {
204 if let Some(value) = attribute.value_str() {
2c00a5a8 205 return Some((value, attribute.span));
cc61c64b 206 }
b7449926
XL
207 } else if attribute.check_name("panic_implementation") ||
208 attribute.check_name("panic_handler")
209 {
94b46f34 210 return Some((Symbol::intern("panic_impl"), attribute.span))
8faf50e0
XL
211 } else if attribute.check_name("alloc_error_handler") {
212 return Some((Symbol::intern("oom"), attribute.span))
223e47cc
LB
213 }
214 }
215
0bf4aa26 216 None
223e47cc
LB
217}
218
ea8adc8c
XL
219pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {
220 let mut collector = LanguageItemCollector::new(tcx);
221 for &cnum in tcx.crates().iter() {
222 for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() {
223 collector.collect_item(item_index, def_id);
224 }
225 }
226 tcx.hir.krate().visit_all_item_likes(&mut collector);
1a4d82fc 227 let LanguageItemCollector { mut items, .. } = collector;
ea8adc8c 228 weak_lang_items::check_crate(tcx, &mut items);
970d7e83 229 items
223e47cc 230}
1a4d82fc
JJ
231
232// End of the macro
233 }
234}
235
3157f602 236language_item_table! {
0bf4aa26
XL
237// Variant name, Name, Method name, Target;
238 CharImplItem, "char", char_impl, Target::Impl;
239 StrImplItem, "str", str_impl, Target::Impl;
240 SliceImplItem, "slice", slice_impl, Target::Impl;
241 SliceU8ImplItem, "slice_u8", slice_u8_impl, Target::Impl;
242 StrAllocImplItem, "str_alloc", str_alloc_impl, Target::Impl;
243 SliceAllocImplItem, "slice_alloc", slice_alloc_impl, Target::Impl;
244 SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl, Target::Impl;
245 ConstPtrImplItem, "const_ptr", const_ptr_impl, Target::Impl;
246 MutPtrImplItem, "mut_ptr", mut_ptr_impl, Target::Impl;
247 I8ImplItem, "i8", i8_impl, Target::Impl;
248 I16ImplItem, "i16", i16_impl, Target::Impl;
249 I32ImplItem, "i32", i32_impl, Target::Impl;
250 I64ImplItem, "i64", i64_impl, Target::Impl;
251 I128ImplItem, "i128", i128_impl, Target::Impl;
252 IsizeImplItem, "isize", isize_impl, Target::Impl;
253 U8ImplItem, "u8", u8_impl, Target::Impl;
254 U16ImplItem, "u16", u16_impl, Target::Impl;
255 U32ImplItem, "u32", u32_impl, Target::Impl;
256 U64ImplItem, "u64", u64_impl, Target::Impl;
257 U128ImplItem, "u128", u128_impl, Target::Impl;
258 UsizeImplItem, "usize", usize_impl, Target::Impl;
259 F32ImplItem, "f32", f32_impl, Target::Impl;
260 F64ImplItem, "f64", f64_impl, Target::Impl;
261 F32RuntimeImplItem, "f32_runtime", f32_runtime_impl, Target::Impl;
262 F64RuntimeImplItem, "f64_runtime", f64_runtime_impl, Target::Impl;
263
264 SizedTraitLangItem, "sized", sized_trait, Target::Trait;
265 UnsizeTraitLangItem, "unsize", unsize_trait, Target::Trait;
266 CopyTraitLangItem, "copy", copy_trait, Target::Trait;
267 CloneTraitLangItem, "clone", clone_trait, Target::Trait;
268 SyncTraitLangItem, "sync", sync_trait, Target::Trait;
269 FreezeTraitLangItem, "freeze", freeze_trait, Target::Trait;
270
271 DropTraitLangItem, "drop", drop_trait, Target::Trait;
272
273 CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait, Target::Trait;
274
275 AddTraitLangItem, "add", add_trait, Target::Trait;
276 SubTraitLangItem, "sub", sub_trait, Target::Trait;
277 MulTraitLangItem, "mul", mul_trait, Target::Trait;
278 DivTraitLangItem, "div", div_trait, Target::Trait;
279 RemTraitLangItem, "rem", rem_trait, Target::Trait;
280 NegTraitLangItem, "neg", neg_trait, Target::Trait;
281 NotTraitLangItem, "not", not_trait, Target::Trait;
282 BitXorTraitLangItem, "bitxor", bitxor_trait, Target::Trait;
283 BitAndTraitLangItem, "bitand", bitand_trait, Target::Trait;
284 BitOrTraitLangItem, "bitor", bitor_trait, Target::Trait;
285 ShlTraitLangItem, "shl", shl_trait, Target::Trait;
286 ShrTraitLangItem, "shr", shr_trait, Target::Trait;
287 AddAssignTraitLangItem, "add_assign", add_assign_trait, Target::Trait;
288 SubAssignTraitLangItem, "sub_assign", sub_assign_trait, Target::Trait;
289 MulAssignTraitLangItem, "mul_assign", mul_assign_trait, Target::Trait;
290 DivAssignTraitLangItem, "div_assign", div_assign_trait, Target::Trait;
291 RemAssignTraitLangItem, "rem_assign", rem_assign_trait, Target::Trait;
292 BitXorAssignTraitLangItem, "bitxor_assign", bitxor_assign_trait, Target::Trait;
293 BitAndAssignTraitLangItem, "bitand_assign", bitand_assign_trait, Target::Trait;
294 BitOrAssignTraitLangItem, "bitor_assign", bitor_assign_trait, Target::Trait;
295 ShlAssignTraitLangItem, "shl_assign", shl_assign_trait, Target::Trait;
296 ShrAssignTraitLangItem, "shr_assign", shr_assign_trait, Target::Trait;
297 IndexTraitLangItem, "index", index_trait, Target::Trait;
298 IndexMutTraitLangItem, "index_mut", index_mut_trait, Target::Trait;
299
300 UnsafeCellTypeLangItem, "unsafe_cell", unsafe_cell_type, Target::Struct;
301
302 DerefTraitLangItem, "deref", deref_trait, Target::Trait;
303 DerefMutTraitLangItem, "deref_mut", deref_mut_trait, Target::Trait;
304
305 FnTraitLangItem, "fn", fn_trait, Target::Trait;
306 FnMutTraitLangItem, "fn_mut", fn_mut_trait, Target::Trait;
307 FnOnceTraitLangItem, "fn_once", fn_once_trait, Target::Trait;
308
309 GeneratorStateLangItem, "generator_state", gen_state, Target::Enum;
310 GeneratorTraitLangItem, "generator", gen_trait, Target::Trait;
311
312 EqTraitLangItem, "eq", eq_trait, Target::Trait;
313 PartialOrdTraitLangItem, "partial_ord", partial_ord_trait, Target::Trait;
314 OrdTraitLangItem, "ord", ord_trait, Target::Trait;
1a4d82fc 315
1a4d82fc
JJ
316 // A number of panic-related lang items. The `panic` item corresponds to
317 // divide-by-zero and various panic cases with `match`. The
318 // `panic_bounds_check` item is for indexing arrays.
319 //
320 // The `begin_unwind` lang item has a predefined symbol name and is sort of
321 // a "weak lang item" in the sense that a crate is not required to have it
322 // defined to use it, but a final product is required to define it
323 // somewhere. Additionally, there are restrictions on crates that use a weak
324 // lang item, but do not have it defined.
0bf4aa26
XL
325 PanicFnLangItem, "panic", panic_fn, Target::Fn;
326 PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn, Target::Fn;
327 PanicInfoLangItem, "panic_info", panic_info, Target::Struct;
328 PanicImplLangItem, "panic_impl", panic_impl, Target::Fn;
b7449926 329 // Libstd panic entry point. Necessary for const eval to be able to catch it
0bf4aa26 330 BeginPanicFnLangItem, "begin_panic", begin_panic_fn, Target::Fn;
1a4d82fc 331
0bf4aa26
XL
332 ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn, Target::Fn;
333 BoxFreeFnLangItem, "box_free", box_free_fn, Target::Fn;
334 DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn, Target::Fn;
335 OomLangItem, "oom", oom, Target::Fn;
336 AllocLayoutLangItem, "alloc_layout", alloc_layout, Target::Struct;
1a4d82fc 337
0bf4aa26 338 StartFnLangItem, "start", start_fn, Target::Fn;
1a4d82fc 339
0bf4aa26
XL
340 EhPersonalityLangItem, "eh_personality", eh_personality, Target::Fn;
341 EhUnwindResumeLangItem, "eh_unwind_resume", eh_unwind_resume, Target::Fn;
342 MSVCTryFilterLangItem, "msvc_try_filter", msvc_try_filter, Target::Static;
1a4d82fc 343
0bf4aa26 344 OwnedBoxLangItem, "owned_box", owned_box, Target::Struct;
1a4d82fc 345
0bf4aa26 346 PhantomDataItem, "phantom_data", phantom_data, Target::Struct;
85aaf69f 347
0bf4aa26 348 ManuallyDropItem, "manually_drop", manually_drop, Target::Struct;
8faf50e0 349
0bf4aa26 350 DebugTraitLangItem, "debug_trait", debug_trait, Target::Trait;
ff7c6d11
XL
351
352 // A lang item for each of the 128-bit operators we can optionally lower.
0bf4aa26
XL
353 I128AddFnLangItem, "i128_add", i128_add_fn, Target::Fn;
354 U128AddFnLangItem, "u128_add", u128_add_fn, Target::Fn;
355 I128SubFnLangItem, "i128_sub", i128_sub_fn, Target::Fn;
356 U128SubFnLangItem, "u128_sub", u128_sub_fn, Target::Fn;
357 I128MulFnLangItem, "i128_mul", i128_mul_fn, Target::Fn;
358 U128MulFnLangItem, "u128_mul", u128_mul_fn, Target::Fn;
359 I128DivFnLangItem, "i128_div", i128_div_fn, Target::Fn;
360 U128DivFnLangItem, "u128_div", u128_div_fn, Target::Fn;
361 I128RemFnLangItem, "i128_rem", i128_rem_fn, Target::Fn;
362 U128RemFnLangItem, "u128_rem", u128_rem_fn, Target::Fn;
363 I128ShlFnLangItem, "i128_shl", i128_shl_fn, Target::Fn;
364 U128ShlFnLangItem, "u128_shl", u128_shl_fn, Target::Fn;
365 I128ShrFnLangItem, "i128_shr", i128_shr_fn, Target::Fn;
366 U128ShrFnLangItem, "u128_shr", u128_shr_fn, Target::Fn;
ff7c6d11
XL
367 // And overflow versions for the operators that are checkable.
368 // While MIR calls these Checked*, they return (T,bool), not Option<T>.
0bf4aa26
XL
369 I128AddoFnLangItem, "i128_addo", i128_addo_fn, Target::Fn;
370 U128AddoFnLangItem, "u128_addo", u128_addo_fn, Target::Fn;
371 I128SuboFnLangItem, "i128_subo", i128_subo_fn, Target::Fn;
372 U128SuboFnLangItem, "u128_subo", u128_subo_fn, Target::Fn;
373 I128MuloFnLangItem, "i128_mulo", i128_mulo_fn, Target::Fn;
374 U128MuloFnLangItem, "u128_mulo", u128_mulo_fn, Target::Fn;
375 I128ShloFnLangItem, "i128_shlo", i128_shlo_fn, Target::Fn;
376 U128ShloFnLangItem, "u128_shlo", u128_shlo_fn, Target::Fn;
377 I128ShroFnLangItem, "i128_shro", i128_shro_fn, Target::Fn;
378 U128ShroFnLangItem, "u128_shro", u128_shro_fn, Target::Fn;
ff7c6d11 379
94b46f34 380 // Align offset for stride != 1, must not panic.
0bf4aa26
XL
381 AlignOffsetLangItem, "align_offset", align_offset_fn, Target::Fn;
382
383 TerminationTraitLangItem, "termination", termination, Target::Trait;
94b46f34 384
0bf4aa26
XL
385 Arc, "arc", arc, Target::Struct;
386 Rc, "rc", rc, Target::Struct;
1a4d82fc 387}
476ff2be 388
ea8adc8c 389impl<'a, 'tcx, 'gcx> TyCtxt<'a, 'tcx, 'gcx> {
476ff2be 390 pub fn require_lang_item(&self, lang_item: LangItem) -> DefId {
ea8adc8c 391 self.lang_items().require(lang_item).unwrap_or_else(|msg| {
476ff2be
SL
392 self.sess.fatal(&msg)
393 })
394 }
395}