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.
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.
11 //! Validity checking for weak lang items
15 use metadata::csearch;
16 use middle::lang_items;
18 use syntax::codemap::Span;
19 use syntax::parse::token::InternedString;
20 use rustc_front::visit::Visitor;
21 use rustc_front::visit;
24 use std::collections::HashSet;
26 macro_rules! weak_lang_items {
27 ($($name:ident, $item:ident, $sym:ident;)*) => (
31 items: &'a mut lang_items::LanguageItems,
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: &hir::Crate,
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
42 if items.eh_personality().is_none() {
43 items.missing.push(lang_items::EhPersonalityLangItem);
45 if sess.target.target.options.custom_unwind_resume &
46 items.eh_unwind_resume().is_none() {
47 items.missing.push(lang_items::EhUnwindResumeLangItem);
51 let mut cx = Context { sess: sess, items: items };
52 visit::walk_crate(&mut cx, krate);
57 pub fn link_name(attrs: &[hir::Attribute]) -> Option<InternedString> {
58 lang_items::extract(attrs).and_then(|name| {
59 $(if &name[..] == stringify!($name) {
60 Some(InternedString::new(stringify!($sym)))
67 fn verify(sess: &Session, items: &lang_items::LanguageItems) {
68 // We only need to check for the presence of weak lang items if we're
69 // emitting something that's not an rlib.
70 let needs_check = sess.crate_types.borrow().iter().any(|kind| {
72 config::CrateTypeDylib |
73 config::CrateTypeExecutable |
74 config::CrateTypeStaticlib => true,
75 config::CrateTypeRlib => false,
78 if !needs_check { return }
80 let mut missing = HashSet::new();
81 sess.cstore.iter_crate_data(|cnum, _| {
82 for item in &csearch::get_missing_lang_items(&sess.cstore, cnum) {
83 missing.insert(*item);
88 if missing.contains(&lang_items::$item) && items.$name().is_none() {
89 sess.err(&format!("language item required, but not found: `{}`",
96 impl<'a> Context<'a> {
97 fn register(&mut self, name: &str, span: Span) {
98 $(if name == stringify!($name) {
99 if self.items.$name().is_none() {
100 self.items.missing.push(lang_items::$item);
103 span_err!(self.sess, span, E0264,
104 "unknown external lang item: `{}`",
110 impl<'a, 'v> Visitor<'v> for Context<'a> {
111 fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
112 match lang_items::extract(&i.attrs) {
114 Some(lang_item) => self.register(&lang_item, i.span),
116 visit::walk_foreign_item(self, i)
123 panic_fmt, PanicFmtLangItem, rust_begin_unwind;
124 eh_personality, EhPersonalityLangItem, rust_eh_personality;
125 eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume;