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<'a> {
49 alt_std_name: Option<String>
52 impl<'a> fold::Folder for StandardLibraryInjector<'a> {
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<'a>;
85 impl<'a> fold::Folder for PreludeInjector<'a> {
86 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
87 // only add `use std::prelude::*;` if there wasn't a
88 // `#![no_implicit_prelude]` at the crate level.
89 // fold_mod() will insert glob path.
90 if !no_prelude(&krate.attrs[]) {
91 krate.module = self.fold_mod(krate.module);
96 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
97 if !no_prelude(&item.attrs[]) {
98 // only recur if there wasn't `#![no_implicit_prelude]`
99 // on this item, i.e. this means that the prelude is not
100 // implicitly imported though the whole subtree
101 fold::noop_fold_item(item, self)
103 SmallVector::one(item)
107 fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
108 let prelude_path = ast::Path {
113 identifier: token::str_to_ident("std"),
114 parameters: ast::PathParameters::none(),
117 identifier: token::str_to_ident("prelude"),
118 parameters: ast::PathParameters::none(),
121 identifier: token::str_to_ident("v1"),
122 parameters: ast::PathParameters::none(),
127 let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
128 mod_.items.insert(0, P(ast::Item {
129 id: ast::DUMMY_NODE_ID,
130 ident: special_idents::invalid,
131 node: ast::ItemUse(vp),
132 attrs: vec![ast::Attribute {
134 node: ast::Attribute_ {
135 id: attr::mk_attr_id(),
136 style: ast::AttrOuter,
137 value: P(ast::MetaItem {
139 node: ast::MetaWord(token::get_name(
140 special_idents::prelude_import.name)),
142 is_sugared_doc: false,
149 fold::noop_fold_mod(mod_, self)
153 fn inject_prelude(krate: ast::Crate) -> ast::Crate {
154 let mut fold = PreludeInjector;
155 fold.fold_crate(krate)