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;
17 use parse::token::InternedString;
18 use parse::token::special_idents;
21 use util::small_vector::SmallVector;
23 pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>)
26 inject_crates_ref(krate, alt_std_name)
32 pub fn maybe_inject_prelude(krate: ast::Crate) -> ast::Crate {
40 pub fn use_std(krate: &ast::Crate) -> bool {
41 !attr::contains_name(&krate.attrs[], "no_std")
44 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
45 attr::contains_name(attrs, "no_implicit_prelude")
48 struct StandardLibraryInjector {
49 alt_std_name: Option<String>,
52 impl fold::Folder for StandardLibraryInjector {
53 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
55 // The name to use in `extern crate "name" as std;`
56 let actual_crate_name = match self.alt_std_name {
57 Some(ref s) => token::intern_and_get_ident(&s[..]),
58 None => token::intern_and_get_ident("std"),
61 krate.module.items.insert(0, P(ast::Item {
62 id: ast::DUMMY_NODE_ID,
63 ident: token::str_to_ident("std"),
65 attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(
66 InternedString::new("macro_use")))),
67 node: ast::ItemExternCrate(Some((actual_crate_name, ast::CookedStr))),
76 fn inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate {
77 let mut fold = StandardLibraryInjector {
78 alt_std_name: alt_std_name
80 fold.fold_crate(krate)
83 struct PreludeInjector;
86 impl fold::Folder for PreludeInjector {
87 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
88 // only add `use std::prelude::*;` if there wasn't a
89 // `#![no_implicit_prelude]` at the crate level.
90 // fold_mod() will insert glob path.
91 if !no_prelude(&krate.attrs[]) {
92 krate.module = self.fold_mod(krate.module);
97 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
98 if !no_prelude(&item.attrs[]) {
99 // only recur if there wasn't `#![no_implicit_prelude]`
100 // on this item, i.e. this means that the prelude is not
101 // implicitly imported though the whole subtree
102 fold::noop_fold_item(item, self)
104 SmallVector::one(item)
108 fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
109 let prelude_path = ast::Path {
114 identifier: token::str_to_ident("std"),
115 parameters: ast::PathParameters::none(),
118 identifier: token::str_to_ident("prelude"),
119 parameters: ast::PathParameters::none(),
122 identifier: token::str_to_ident("v1"),
123 parameters: ast::PathParameters::none(),
128 let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
129 mod_.items.insert(0, P(ast::Item {
130 id: ast::DUMMY_NODE_ID,
131 ident: special_idents::invalid,
132 node: ast::ItemUse(vp),
133 attrs: vec![ast::Attribute {
135 node: ast::Attribute_ {
136 id: attr::mk_attr_id(),
137 style: ast::AttrOuter,
138 value: P(ast::MetaItem {
140 node: ast::MetaWord(token::get_name(
141 special_idents::prelude_import.name)),
143 is_sugared_doc: false,
150 fold::noop_fold_mod(mod_, self)
154 fn inject_prelude(krate: ast::Crate) -> ast::Crate {
155 let mut fold = PreludeInjector;
156 fold.fold_crate(krate)