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;
25 pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>)
28 inject_crates_ref(krate, alt_std_name)
34 pub fn maybe_inject_prelude(krate: ast::Crate) -> ast::Crate {
42 fn use_std(krate: &ast::Crate) -> bool {
43 !attr::contains_name(&krate.attrs[], "no_std")
46 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
47 attr::contains_name(attrs, "no_implicit_prelude")
50 struct StandardLibraryInjector<'a> {
51 alt_std_name: Option<String>,
54 impl<'a> fold::Folder for StandardLibraryInjector<'a> {
55 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
57 // The name to use in `extern crate "name" as std;`
58 let actual_crate_name = match self.alt_std_name {
59 Some(ref s) => token::intern_and_get_ident(&s[]),
60 None => token::intern_and_get_ident("std"),
63 let mut vis = vec!(ast::ViewItem {
64 node: ast::ViewItemExternCrate(token::str_to_ident("std"),
65 Some((actual_crate_name, ast::CookedStr)),
68 attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(
69 InternedString::new("macro_use")))),
74 // `extern crate` must be precede `use` items
75 mem::swap(&mut vis, &mut krate.module.view_items);
76 krate.module.view_items.extend(vis.into_iter());
78 // don't add #![no_std] here, that will block the prelude injection later.
79 // Add it during the prelude injection instead.
85 fn inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate {
86 let mut fold = StandardLibraryInjector {
87 alt_std_name: alt_std_name,
89 fold.fold_crate(krate)
92 struct PreludeInjector<'a>;
95 impl<'a> fold::Folder for PreludeInjector<'a> {
96 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
97 // Add #![no_std] here, so we don't re-inject when compiling pretty-printed source.
98 // This must happen here and not in StandardLibraryInjector because this
99 // fold happens second.
101 let no_std_attr = attr::mk_attr_inner(attr::mk_attr_id(),
102 attr::mk_word_item(InternedString::new("no_std")));
103 // std_inject runs after feature checking so manually mark this attr
104 attr::mark_used(&no_std_attr);
105 krate.attrs.push(no_std_attr);
107 if !no_prelude(&krate.attrs[]) {
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 let globs_attr = attr::mk_attr_inner(attr::mk_attr_id(),
113 InternedString::new("feature"),
115 attr::mk_word_item(InternedString::new("globs")),
117 // std_inject runs after feature checking so manually mark this attr
118 attr::mark_used(&globs_attr);
119 krate.attrs.push(globs_attr);
121 krate.module = self.fold_mod(krate.module);
126 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
127 if !no_prelude(&item.attrs[]) {
128 // only recur if there wasn't `#![no_implicit_prelude]`
129 // on this item, i.e. this means that the prelude is not
130 // implicitly imported though the whole subtree
131 fold::noop_fold_item(item, self)
133 SmallVector::one(item)
137 fn fold_mod(&mut self, ast::Mod {inner, view_items, items}: ast::Mod) -> ast::Mod {
138 let prelude_path = ast::Path {
143 identifier: token::str_to_ident("std"),
144 parameters: ast::PathParameters::none(),
147 identifier: token::str_to_ident("prelude"),
148 parameters: ast::PathParameters::none(),
151 identifier: token::str_to_ident("v1"),
152 parameters: ast::PathParameters::none(),
157 let (crates, uses): (Vec<_>, _) = view_items.iter().cloned().partition(|x| {
159 ast::ViewItemExternCrate(..) => true,
164 // add prelude after any `extern crate` but before any `use`
165 let mut view_items = crates;
166 let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path, ast::DUMMY_NODE_ID)));
167 view_items.push(ast::ViewItem {
168 node: ast::ViewItemUse(vp),
169 attrs: vec![ast::Attribute {
171 node: ast::Attribute_ {
172 id: attr::mk_attr_id(),
173 style: ast::AttrOuter,
174 value: P(ast::MetaItem {
176 node: ast::MetaWord(token::get_name(
177 special_idents::prelude_import.name)),
179 is_sugared_doc: false,
185 view_items.extend(uses.into_iter());
187 fold::noop_fold_mod(ast::Mod {
189 view_items: view_items,
195 fn inject_prelude(krate: ast::Crate) -> ast::Crate {
196 let mut fold = PreludeInjector;
197 fold.fold_crate(krate)