]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/weak_lang_items.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / librustc / middle / weak_lang_items.rs
1 // Copyright 2014 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 //! Validity checking for weak lang items
12
13 use session::config;
14 use session::Session;
15 use metadata::csearch;
16 use middle::lang_items;
17
18 use syntax::ast;
19 use syntax::codemap::Span;
20 use syntax::parse::token::InternedString;
21 use syntax::visit::Visitor;
22 use syntax::visit;
23
24 use std::collections::HashSet;
25
26 macro_rules! weak_lang_items {
27     ($($name:ident, $item:ident, $sym:ident;)*) => (
28
29 struct Context<'a> {
30     sess: &'a Session,
31     items: &'a mut lang_items::LanguageItems,
32 }
33
34 /// Checks the crate for usage of weak lang items, returning a vector of all the
35 /// language items required by this crate, but not defined yet.
36 pub fn check_crate(krate: &ast::Crate,
37                    sess: &Session,
38                    items: &mut lang_items::LanguageItems) {
39     // These are never called by user code, they're generated by the compiler.
40     // They will never implicitly be added to the `missing` array unless we do
41     // so here.
42     if items.stack_exhausted().is_none() {
43         items.missing.push(lang_items::StackExhaustedLangItem);
44     }
45     if items.eh_personality().is_none() {
46         items.missing.push(lang_items::EhPersonalityLangItem);
47     }
48
49     {
50         let mut cx = Context { sess: sess, items: items };
51         visit::walk_crate(&mut cx, krate);
52     }
53     verify(sess, items);
54 }
55
56 pub fn link_name(attrs: &[ast::Attribute]) -> Option<InternedString> {
57     lang_items::extract(attrs).and_then(|name| {
58         $(if &name[..] == stringify!($name) {
59             Some(InternedString::new(stringify!($sym)))
60         } else)* {
61             None
62         }
63     })
64 }
65
66 fn verify(sess: &Session, items: &lang_items::LanguageItems) {
67     // We only need to check for the presence of weak lang items if we're
68     // emitting something that's not an rlib.
69     let needs_check = sess.crate_types.borrow().iter().any(|kind| {
70         match *kind {
71             config::CrateTypeDylib |
72             config::CrateTypeExecutable |
73             config::CrateTypeStaticlib => true,
74             config::CrateTypeRlib => false,
75         }
76     });
77     if !needs_check { return }
78
79     let mut missing = HashSet::new();
80     sess.cstore.iter_crate_data(|cnum, _| {
81         for item in &csearch::get_missing_lang_items(&sess.cstore, cnum) {
82             missing.insert(*item);
83         }
84     });
85
86     $(
87         if missing.contains(&lang_items::$item) && items.$name().is_none() {
88             sess.err(&format!("language item required, but not found: `{}`",
89                               stringify!($name)));
90
91         }
92     )*
93 }
94
95 impl<'a> Context<'a> {
96     fn register(&mut self, name: &str, span: Span) {
97         $(if name == stringify!($name) {
98             if self.items.$name().is_none() {
99                 self.items.missing.push(lang_items::$item);
100             }
101         } else)* {
102             span_err!(self.sess, span, E0264,
103                                "unknown external lang item: `{}`",
104                                        name);
105         }
106     }
107 }
108
109 impl<'a, 'v> Visitor<'v> for Context<'a> {
110     fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
111         match lang_items::extract(&i.attrs) {
112             None => {}
113             Some(lang_item) => self.register(&lang_item, i.span),
114         }
115         visit::walk_foreign_item(self, i)
116     }
117 }
118
119 ) }
120
121 weak_lang_items! {
122     panic_fmt,          PanicFmtLangItem,            rust_begin_unwind;
123     stack_exhausted,    StackExhaustedLangItem,     rust_stack_exhausted;
124     eh_personality,     EhPersonalityLangItem,      rust_eh_personality;
125 }