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