/src/librustc/middle/weak_lang_items.rs

https://gitlab.com/alx741/rust · Rust · 126 lines · 92 code · 17 blank · 17 comment · 13 complexity · a557939d882d91dbd7b1a13b2fad50e9 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 middle::lang_items;
  14. use syntax::ast;
  15. use syntax::codemap::Span;
  16. use syntax::parse::token::InternedString;
  17. use hir::intravisit::Visitor;
  18. use hir::intravisit;
  19. use hir;
  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: &hir::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.eh_personality().is_none() {
  36. items.missing.push(lang_items::EhPersonalityLangItem);
  37. }
  38. if sess.target.target.options.custom_unwind_resume &
  39. items.eh_unwind_resume().is_none() {
  40. items.missing.push(lang_items::EhUnwindResumeLangItem);
  41. }
  42. {
  43. let mut cx = Context { sess: sess, items: items };
  44. krate.visit_all_items(&mut cx);
  45. }
  46. verify(sess, items);
  47. }
  48. pub fn link_name(attrs: &[ast::Attribute]) -> Option<InternedString> {
  49. lang_items::extract(attrs).and_then(|name| {
  50. $(if &name[..] == stringify!($name) {
  51. Some(InternedString::new(stringify!($sym)))
  52. } else)* {
  53. None
  54. }
  55. })
  56. }
  57. fn verify(sess: &Session, items: &lang_items::LanguageItems) {
  58. // We only need to check for the presence of weak lang items if we're
  59. // emitting something that's not an rlib.
  60. let needs_check = sess.crate_types.borrow().iter().any(|kind| {
  61. match *kind {
  62. config::CrateTypeDylib |
  63. config::CrateTypeExecutable |
  64. config::CrateTypeStaticlib => true,
  65. config::CrateTypeRlib => false,
  66. }
  67. });
  68. if !needs_check { return }
  69. let mut missing = HashSet::new();
  70. for cnum in sess.cstore.crates() {
  71. for item in sess.cstore.missing_lang_items(cnum) {
  72. missing.insert(item);
  73. }
  74. }
  75. $(
  76. if missing.contains(&lang_items::$item) && items.$name().is_none() {
  77. sess.err(&format!("language item required, but not found: `{}`",
  78. stringify!($name)));
  79. }
  80. )*
  81. }
  82. impl<'a> Context<'a> {
  83. fn register(&mut self, name: &str, span: Span) {
  84. $(if name == stringify!($name) {
  85. if self.items.$name().is_none() {
  86. self.items.missing.push(lang_items::$item);
  87. }
  88. } else)* {
  89. span_err!(self.sess, span, E0264,
  90. "unknown external lang item: `{}`",
  91. name);
  92. }
  93. }
  94. }
  95. impl<'a, 'v> Visitor<'v> for Context<'a> {
  96. fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
  97. match lang_items::extract(&i.attrs) {
  98. None => {}
  99. Some(lang_item) => self.register(&lang_item, i.span),
  100. }
  101. intravisit::walk_foreign_item(self, i)
  102. }
  103. }
  104. ) }
  105. weak_lang_items! {
  106. panic_fmt, PanicFmtLangItem, rust_begin_unwind;
  107. eh_personality, EhPersonalityLangItem, rust_eh_personality;
  108. eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume;
  109. }