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.
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.
13 use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
17 use parse::token::{intern, InternedString, special_idents};
18 use parse::{token, ParseSess};
20 use util::small_vector::SmallVector;
22 /// Craft a span that will be ignored by the stability lint's
23 /// call to codemap's is_internal check.
24 /// The expanded code uses the unstable `#[prelude_import]` attribute.
25 fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
29 format: MacroAttribute(intern("std_inject")),
31 allow_internal_unstable: true,
34 let expn_id = sess.codemap().record_expansion(info);
40 pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>)
45 let name = if no_std(&krate) {"core"} else {"std"};
46 let mut fold = CrateInjector {
47 item_name: token::str_to_ident(name),
48 crate_name: token::intern(&alt_std_name.unwrap_or(name.to_string())),
50 fold.fold_crate(krate)
54 pub fn maybe_inject_prelude(sess: &ParseSess, krate: ast::Crate) -> ast::Crate {
58 let name = if no_std(&krate) {"core"} else {"std"};
59 let mut fold = PreludeInjector {
60 span: ignored_span(sess, DUMMY_SP),
61 crate_identifier: token::str_to_ident(name),
63 fold.fold_crate(krate)
67 pub fn no_core(krate: &ast::Crate) -> bool {
68 attr::contains_name(&krate.attrs, "no_core")
71 pub fn no_std(krate: &ast::Crate) -> bool {
72 attr::contains_name(&krate.attrs, "no_std") || no_core(krate)
75 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
76 attr::contains_name(attrs, "no_implicit_prelude")
79 struct CrateInjector {
80 item_name: ast::Ident,
81 crate_name: ast::Name,
84 impl fold::Folder for CrateInjector {
85 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
86 krate.module.items.insert(0, P(ast::Item {
87 id: ast::DUMMY_NODE_ID,
88 ident: self.item_name,
90 attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(
91 InternedString::new("macro_use")))),
92 node: ast::ItemExternCrate(Some(self.crate_name)),
101 struct PreludeInjector {
103 crate_identifier: ast::Ident,
106 impl fold::Folder for PreludeInjector {
107 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
108 // only add `use std::prelude::*;` if there wasn't a
109 // `#![no_implicit_prelude]` at the crate level.
110 // fold_mod() will insert glob path.
111 if !no_prelude(&krate.attrs) {
112 krate.module = self.fold_mod(krate.module);
117 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
118 if !no_prelude(&item.attrs) {
119 // only recur if there wasn't `#![no_implicit_prelude]`
120 // on this item, i.e. this means that the prelude is not
121 // implicitly imported though the whole subtree
122 fold::noop_fold_item(item, self)
124 SmallVector::one(item)
128 fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
129 let prelude_path = ast::Path {
134 identifier: self.crate_identifier,
135 parameters: ast::PathParameters::none(),
138 identifier: token::str_to_ident("prelude"),
139 parameters: ast::PathParameters::none(),
142 identifier: token::str_to_ident("v1"),
143 parameters: ast::PathParameters::none(),
148 let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
149 mod_.items.insert(0, P(ast::Item {
150 id: ast::DUMMY_NODE_ID,
151 ident: special_idents::invalid,
152 node: ast::ItemUse(vp),
153 attrs: vec![ast::Attribute {
155 node: ast::Attribute_ {
156 id: attr::mk_attr_id(),
157 style: ast::AttrStyle::Outer,
158 value: P(ast::MetaItem {
160 node: ast::MetaWord(special_idents::prelude_import.name.as_str()),
162 is_sugared_doc: false,
169 fold::noop_fold_mod(mod_, self)