/src/librustc/middle/weak_lang_items.rs

https://gitlab.com/pranith/rust · Rust · 125 lines · 91 code · 17 blank · 17 comment · 12 complexity · ccaf2994f6ed65c129153ebcf916e9f5 MD5 · raw file

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