]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/weak_lang_items.rs
Auto merge of #81635 - michaelwoerister:structured_def_path_hash, r=pnkfelix
[rust.git] / compiler / rustc_hir / src / weak_lang_items.rs
1 //! Validity checking for weak lang items
2
3 use crate::def_id::DefId;
4 use crate::{lang_items, LangItem, LanguageItems};
5
6 use rustc_ast as ast;
7 use rustc_data_structures::stable_map::StableMap;
8 use rustc_span::symbol::{sym, Symbol};
9
10 use std::lazy::SyncLazy;
11
12 macro_rules! weak_lang_items {
13     ($($name:ident, $item:ident, $sym:ident;)*) => (
14
15 pub static WEAK_ITEMS_REFS: SyncLazy<StableMap<Symbol, LangItem>> = SyncLazy::new(|| {
16     let mut map = StableMap::default();
17     $(map.insert(sym::$name, LangItem::$item);)*
18     map
19 });
20
21 /// The `check_name` argument avoids the need for `librustc_hir` to depend on
22 /// `librustc_session`.
23 pub fn link_name<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option<Symbol>
24 where
25     F: Fn(&'a ast::Attribute, Symbol) -> bool
26 {
27     lang_items::extract(check_name, attrs).and_then(|(name, _)| {
28         $(if name == sym::$name {
29             Some(sym::$sym)
30         } else)* {
31             None
32         }
33     })
34 }
35
36 impl LanguageItems {
37     pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool {
38         let did = Some(item_def_id);
39
40         $(self.$name() == did)||*
41     }
42 }
43
44 ) }
45
46 weak_lang_items! {
47     panic_impl,         PanicImpl,          rust_begin_unwind;
48     eh_personality,     EhPersonality,      rust_eh_personality;
49     eh_catch_typeinfo,  EhCatchTypeinfo,    rust_eh_catch_typeinfo;
50     oom,                Oom,                rust_oom;
51 }