/src/libsyntax/std_inject.rs

https://gitlab.com/pranith/rust · Rust · 157 lines · 122 code · 19 blank · 16 comment · 10 complexity · 473081fb7108470d862ab5f097cf7f2a MD5 · raw file

  1. // Copyright 2012 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. use ast;
  11. use attr;
  12. use codemap::DUMMY_SP;
  13. use codemap;
  14. use fold::Folder;
  15. use fold;
  16. use parse::token::InternedString;
  17. use parse::token::special_idents;
  18. use parse::token;
  19. use ptr::P;
  20. use util::small_vector::SmallVector;
  21. pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>)
  22. -> ast::Crate {
  23. if use_std(&krate) {
  24. inject_crates_ref(krate, alt_std_name)
  25. } else {
  26. krate
  27. }
  28. }
  29. pub fn maybe_inject_prelude(krate: ast::Crate) -> ast::Crate {
  30. if use_std(&krate) {
  31. inject_prelude(krate)
  32. } else {
  33. krate
  34. }
  35. }
  36. pub fn use_std(krate: &ast::Crate) -> bool {
  37. !attr::contains_name(&krate.attrs, "no_std")
  38. }
  39. fn no_prelude(attrs: &[ast::Attribute]) -> bool {
  40. attr::contains_name(attrs, "no_implicit_prelude")
  41. }
  42. struct StandardLibraryInjector {
  43. alt_std_name: Option<String>,
  44. }
  45. impl fold::Folder for StandardLibraryInjector {
  46. fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
  47. // The name to use in `extern crate name as std;`
  48. let actual_crate_name = match self.alt_std_name {
  49. Some(ref s) => token::intern(&s),
  50. None => token::intern("std"),
  51. };
  52. krate.module.items.insert(0, P(ast::Item {
  53. id: ast::DUMMY_NODE_ID,
  54. ident: token::str_to_ident("std"),
  55. attrs: vec!(
  56. attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(
  57. InternedString::new("macro_use")))),
  58. node: ast::ItemExternCrate(Some(actual_crate_name)),
  59. vis: ast::Inherited,
  60. span: DUMMY_SP
  61. }));
  62. krate
  63. }
  64. }
  65. fn inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate {
  66. let mut fold = StandardLibraryInjector {
  67. alt_std_name: alt_std_name
  68. };
  69. fold.fold_crate(krate)
  70. }
  71. struct PreludeInjector;
  72. impl fold::Folder for PreludeInjector {
  73. fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
  74. // only add `use std::prelude::*;` if there wasn't a
  75. // `#![no_implicit_prelude]` at the crate level.
  76. // fold_mod() will insert glob path.
  77. if !no_prelude(&krate.attrs) {
  78. krate.module = self.fold_mod(krate.module);
  79. }
  80. krate
  81. }
  82. fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
  83. if !no_prelude(&item.attrs) {
  84. // only recur if there wasn't `#![no_implicit_prelude]`
  85. // on this item, i.e. this means that the prelude is not
  86. // implicitly imported though the whole subtree
  87. fold::noop_fold_item(item, self)
  88. } else {
  89. SmallVector::one(item)
  90. }
  91. }
  92. fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
  93. let prelude_path = ast::Path {
  94. span: DUMMY_SP,
  95. global: false,
  96. segments: vec![
  97. ast::PathSegment {
  98. identifier: token::str_to_ident("std"),
  99. parameters: ast::PathParameters::none(),
  100. },
  101. ast::PathSegment {
  102. identifier: token::str_to_ident("prelude"),
  103. parameters: ast::PathParameters::none(),
  104. },
  105. ast::PathSegment {
  106. identifier: token::str_to_ident("v1"),
  107. parameters: ast::PathParameters::none(),
  108. },
  109. ],
  110. };
  111. let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
  112. mod_.items.insert(0, P(ast::Item {
  113. id: ast::DUMMY_NODE_ID,
  114. ident: special_idents::invalid,
  115. node: ast::ItemUse(vp),
  116. attrs: vec![ast::Attribute {
  117. span: DUMMY_SP,
  118. node: ast::Attribute_ {
  119. id: attr::mk_attr_id(),
  120. style: ast::AttrOuter,
  121. value: P(ast::MetaItem {
  122. span: DUMMY_SP,
  123. node: ast::MetaWord(token::get_name(
  124. special_idents::prelude_import.name)),
  125. }),
  126. is_sugared_doc: false,
  127. },
  128. }],
  129. vis: ast::Inherited,
  130. span: DUMMY_SP,
  131. }));
  132. fold::noop_fold_mod(mod_, self)
  133. }
  134. }
  135. fn inject_prelude(krate: ast::Crate) -> ast::Crate {
  136. let mut fold = PreludeInjector;
  137. fold.fold_crate(krate)
  138. }