]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/lang_items.rs
New upstream version 1.23.0+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;
ea8adc8c 25use ty::{self, TyCtxt};
1a4d82fc 26use middle::weak_lang_items;
476ff2be 27use util::nodemap::FxHashMap;
223e47cc 28
b039eaaf 29use syntax::ast;
476ff2be
SL
30use syntax::symbol::Symbol;
31use hir::itemlikevisit::ItemLikeVisitor;
54a0048b 32use hir;
1a4d82fc 33
1a4d82fc
JJ
34// The actual lang items defined come at the end of this file in one handy table.
35// So you probably just want to nip down to the end.
3157f602 36macro_rules! language_item_table {
1a4d82fc
JJ
37 (
38 $( $variant:ident, $name:expr, $method:ident; )*
39 ) => {
40
9346a6ac
AL
41
42enum_from_u32! {
9e0c209e 43 #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
9346a6ac
AL
44 pub enum LangItem {
45 $($variant,)*
46 }
223e47cc
LB
47}
48
ea8adc8c
XL
49impl LangItem {
50 fn name(self) -> &'static str {
51 match self {
52 $( $variant => $name, )*
53 }
54 }
55}
56
223e47cc 57pub struct LanguageItems {
e9174d1e 58 pub items: Vec<Option<DefId>>,
1a4d82fc 59 pub missing: Vec<LangItem>,
223e47cc
LB
60}
61
970d7e83 62impl LanguageItems {
223e47cc 63 pub fn new() -> LanguageItems {
e9174d1e 64 fn foo(_: LangItem) -> Option<DefId> { None }
1a4d82fc 65
223e47cc 66 LanguageItems {
c30ab7b3 67 items: vec![$(foo($variant)),*],
1a4d82fc 68 missing: Vec::new(),
223e47cc
LB
69 }
70 }
71
7453a54e
SL
72 pub fn items(&self) -> &[Option<DefId>] {
73 &*self.items
223e47cc
LB
74 }
75
e9174d1e 76 pub fn require(&self, it: LangItem) -> Result<DefId, String> {
ea8adc8c 77 self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
c1a9b12d
SL
78 }
79
e9174d1e 80 pub fn fn_trait_kind(&self, id: DefId) -> Option<ty::ClosureKind> {
ea8adc8c
XL
81 match Some(id) {
82 x if x == self.fn_trait() => Some(ty::ClosureKind::Fn),
83 x if x == self.fn_mut_trait() => Some(ty::ClosureKind::FnMut),
84 x if x == self.fn_once_trait() => Some(ty::ClosureKind::FnOnce),
85 _ => None
1a4d82fc 86 }
223e47cc 87 }
1a4d82fc
JJ
88
89 $(
90 #[allow(dead_code)]
e9174d1e 91 pub fn $method(&self) -> Option<DefId> {
c34b1796 92 self.items[$variant as usize]
1a4d82fc
JJ
93 }
94 )*
223e47cc
LB
95}
96
b039eaaf 97struct LanguageItemCollector<'a, 'tcx: 'a> {
970d7e83 98 items: LanguageItems,
223e47cc 99
ea8adc8c 100 tcx: TyCtxt<'a, 'tcx, 'tcx>,
223e47cc 101
476ff2be 102 item_refs: FxHashMap<&'static str, usize>,
223e47cc
LB
103}
104
476ff2be 105impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'a, 'tcx> {
e9174d1e 106 fn visit_item(&mut self, item: &hir::Item) {
85aaf69f 107 if let Some(value) = extract(&item.attrs) {
476ff2be 108 let item_index = self.item_refs.get(&*value.as_str()).cloned();
85aaf69f
SL
109
110 if let Some(item_index) = item_index {
ea8adc8c
XL
111 let def_id = self.tcx.hir.local_def_id(item.id);
112 self.collect_item(item_index, def_id);
54a0048b 113 } else {
ea8adc8c
XL
114 let span = self.tcx.hir.span(item.id);
115 span_err!(self.tcx.sess, span, E0522,
54a0048b 116 "definition of an unknown language item: `{}`.",
476ff2be 117 value);
1a4d82fc 118 }
1a4d82fc 119 }
1a4d82fc 120 }
476ff2be 121
32a655c1
SL
122 fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
123 // at present, lang items are always items, not trait items
124 }
125
476ff2be
SL
126 fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
127 // at present, lang items are always items, not impl items
128 }
1a4d82fc
JJ
129}
130
b039eaaf 131impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
ea8adc8c 132 fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItemCollector<'a, 'tcx> {
476ff2be 133 let mut item_refs = FxHashMap();
1a4d82fc 134
c34b1796 135 $( item_refs.insert($name, $variant as usize); )*
970d7e83
LB
136
137 LanguageItemCollector {
ea8adc8c 138 tcx,
970d7e83 139 items: LanguageItems::new(),
041b39d2 140 item_refs,
970d7e83
LB
141 }
142 }
143
ea8adc8c 144 fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
223e47cc
LB
145 // Check for duplicates.
146 match self.items.items[item_index] {
147 Some(original_def_id) if original_def_id != item_def_id => {
ea8adc8c
XL
148 let name = LangItem::from_u32(item_index as u32).unwrap().name();
149 let mut err = match self.tcx.hir.span_if_local(item_def_id) {
a7813a04 150 Some(span) => struct_span_err!(
ea8adc8c 151 self.tcx.sess,
a7813a04
XL
152 span,
153 E0152,
154 "duplicate lang item found: `{}`.",
155 name),
ea8adc8c 156 None => self.tcx.sess.struct_err(&format!(
a7813a04 157 "duplicate lang item in crate `{}`: `{}`.",
ea8adc8c 158 self.tcx.crate_name(item_def_id.krate),
a7813a04
XL
159 name)),
160 };
ea8adc8c 161 if let Some(span) = self.tcx.hir.span_if_local(original_def_id) {
54a0048b
SL
162 span_note!(&mut err, span,
163 "first defined here.");
164 } else {
165 err.note(&format!("first defined in crate `{}`.",
ea8adc8c 166 self.tcx.crate_name(original_def_id.krate)));
54a0048b
SL
167 }
168 err.emit();
223e47cc 169 }
7453a54e 170 _ => {
223e47cc
LB
171 // OK.
172 }
173 }
174
175 // Matched.
176 self.items.items[item_index] = Some(item_def_id);
177 }
1a4d82fc 178}
223e47cc 179
476ff2be 180pub fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
85aaf69f 181 for attribute in attrs {
cc61c64b
XL
182 if attribute.check_name("lang") {
183 if let Some(value) = attribute.value_str() {
184 return Some(value)
185 }
223e47cc
LB
186 }
187 }
188
1a4d82fc 189 return None;
223e47cc
LB
190}
191
ea8adc8c
XL
192pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {
193 let mut collector = LanguageItemCollector::new(tcx);
194 for &cnum in tcx.crates().iter() {
195 for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() {
196 collector.collect_item(item_index, def_id);
197 }
198 }
199 tcx.hir.krate().visit_all_item_likes(&mut collector);
1a4d82fc 200 let LanguageItemCollector { mut items, .. } = collector;
ea8adc8c 201 weak_lang_items::check_crate(tcx, &mut items);
970d7e83 202 items
223e47cc 203}
1a4d82fc
JJ
204
205// End of the macro
206 }
207}
208
3157f602 209language_item_table! {
1a4d82fc 210// Variant name, Name, Method name;
c34b1796
AL
211 CharImplItem, "char", char_impl;
212 StrImplItem, "str", str_impl;
213 SliceImplItem, "slice", slice_impl;
abe05a73 214 SliceU8ImplItem, "slice_u8", slice_u8_impl;
c34b1796
AL
215 ConstPtrImplItem, "const_ptr", const_ptr_impl;
216 MutPtrImplItem, "mut_ptr", mut_ptr_impl;
217 I8ImplItem, "i8", i8_impl;
218 I16ImplItem, "i16", i16_impl;
219 I32ImplItem, "i32", i32_impl;
220 I64ImplItem, "i64", i64_impl;
32a655c1 221 I128ImplItem, "i128", i128_impl;
c34b1796
AL
222 IsizeImplItem, "isize", isize_impl;
223 U8ImplItem, "u8", u8_impl;
224 U16ImplItem, "u16", u16_impl;
225 U32ImplItem, "u32", u32_impl;
226 U64ImplItem, "u64", u64_impl;
32a655c1 227 U128ImplItem, "u128", u128_impl;
c34b1796
AL
228 UsizeImplItem, "usize", usize_impl;
229 F32ImplItem, "f32", f32_impl;
230 F64ImplItem, "f64", f64_impl;
231
1a4d82fc 232 SizedTraitLangItem, "sized", sized_trait;
d9579d0f 233 UnsizeTraitLangItem, "unsize", unsize_trait;
1a4d82fc 234 CopyTraitLangItem, "copy", copy_trait;
3b2f2976 235 CloneTraitLangItem, "clone", clone_trait;
1a4d82fc 236 SyncTraitLangItem, "sync", sync_trait;
cc61c64b 237 FreezeTraitLangItem, "freeze", freeze_trait;
1a4d82fc
JJ
238
239 DropTraitLangItem, "drop", drop_trait;
240
d9579d0f
AL
241 CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait;
242
1a4d82fc
JJ
243 AddTraitLangItem, "add", add_trait;
244 SubTraitLangItem, "sub", sub_trait;
245 MulTraitLangItem, "mul", mul_trait;
246 DivTraitLangItem, "div", div_trait;
247 RemTraitLangItem, "rem", rem_trait;
248 NegTraitLangItem, "neg", neg_trait;
249 NotTraitLangItem, "not", not_trait;
250 BitXorTraitLangItem, "bitxor", bitxor_trait;
251 BitAndTraitLangItem, "bitand", bitand_trait;
252 BitOrTraitLangItem, "bitor", bitor_trait;
253 ShlTraitLangItem, "shl", shl_trait;
254 ShrTraitLangItem, "shr", shr_trait;
b039eaaf
SL
255 AddAssignTraitLangItem, "add_assign", add_assign_trait;
256 SubAssignTraitLangItem, "sub_assign", sub_assign_trait;
257 MulAssignTraitLangItem, "mul_assign", mul_assign_trait;
258 DivAssignTraitLangItem, "div_assign", div_assign_trait;
259 RemAssignTraitLangItem, "rem_assign", rem_assign_trait;
260 BitXorAssignTraitLangItem, "bitxor_assign", bitxor_assign_trait;
261 BitAndAssignTraitLangItem, "bitand_assign", bitand_assign_trait;
262 BitOrAssignTraitLangItem, "bitor_assign", bitor_assign_trait;
263 ShlAssignTraitLangItem, "shl_assign", shl_assign_trait;
264 ShrAssignTraitLangItem, "shr_assign", shr_assign_trait;
1a4d82fc
JJ
265 IndexTraitLangItem, "index", index_trait;
266 IndexMutTraitLangItem, "index_mut", index_mut_trait;
1a4d82fc 267
85aaf69f 268 UnsafeCellTypeLangItem, "unsafe_cell", unsafe_cell_type;
1a4d82fc
JJ
269
270 DerefTraitLangItem, "deref", deref_trait;
271 DerefMutTraitLangItem, "deref_mut", deref_mut_trait;
272
273 FnTraitLangItem, "fn", fn_trait;
274 FnMutTraitLangItem, "fn_mut", fn_mut_trait;
275 FnOnceTraitLangItem, "fn_once", fn_once_trait;
276
ea8adc8c
XL
277 GeneratorStateLangItem, "generator_state", gen_state;
278 GeneratorTraitLangItem, "generator", gen_trait;
279
1a4d82fc
JJ
280 EqTraitLangItem, "eq", eq_trait;
281 OrdTraitLangItem, "ord", ord_trait;
282
1a4d82fc
JJ
283 // A number of panic-related lang items. The `panic` item corresponds to
284 // divide-by-zero and various panic cases with `match`. The
285 // `panic_bounds_check` item is for indexing arrays.
286 //
287 // The `begin_unwind` lang item has a predefined symbol name and is sort of
288 // a "weak lang item" in the sense that a crate is not required to have it
289 // defined to use it, but a final product is required to define it
290 // somewhere. Additionally, there are restrictions on crates that use a weak
291 // lang item, but do not have it defined.
292 PanicFnLangItem, "panic", panic_fn;
293 PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn;
294 PanicFmtLangItem, "panic_fmt", panic_fmt;
295
296 ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
7453a54e 297 BoxFreeFnLangItem, "box_free", box_free_fn;
cc61c64b 298 DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn;
1a4d82fc
JJ
299
300 StartFnLangItem, "start", start_fn;
301
1a4d82fc 302 EhPersonalityLangItem, "eh_personality", eh_personality;
c1a9b12d
SL
303 EhUnwindResumeLangItem, "eh_unwind_resume", eh_unwind_resume;
304 MSVCTryFilterLangItem, "msvc_try_filter", msvc_try_filter;
1a4d82fc 305
1a4d82fc
JJ
306 OwnedBoxLangItem, "owned_box", owned_box;
307
85aaf69f
SL
308 PhantomDataItem, "phantom_data", phantom_data;
309
1a4d82fc
JJ
310 NonZeroItem, "non_zero", non_zero;
311
85aaf69f 312 DebugTraitLangItem, "debug_trait", debug_trait;
1a4d82fc 313}
476ff2be 314
ea8adc8c 315impl<'a, 'tcx, 'gcx> TyCtxt<'a, 'tcx, 'gcx> {
476ff2be 316 pub fn require_lang_item(&self, lang_item: LangItem) -> DefId {
ea8adc8c 317 self.lang_items().require(lang_item).unwrap_or_else(|msg| {
476ff2be
SL
318 self.sess.fatal(&msg)
319 })
320 }
321}