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.
12 use driver::session::Session;
16 use syntax::codemap::DUMMY_SP;
18 use syntax::fold::Folder;
20 use syntax::owned_slice::OwnedSlice;
21 use syntax::parse::token::InternedString;
22 use syntax::parse::token;
23 use syntax::util::small_vector::SmallVector;
26 use std::gc::{Gc, GC};
28 pub fn maybe_inject_crates_ref(sess: &Session, krate: ast::Crate)
31 inject_crates_ref(sess, krate)
37 pub fn maybe_inject_prelude(sess: &Session, krate: ast::Crate) -> ast::Crate {
39 inject_prelude(sess, krate)
45 fn use_std(krate: &ast::Crate) -> bool {
46 !attr::contains_name(krate.attrs.as_slice(), "no_std")
49 fn use_start(krate: &ast::Crate) -> bool {
50 !attr::contains_name(krate.attrs.as_slice(), "no_start")
53 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
54 attr::contains_name(attrs, "no_implicit_prelude")
57 struct StandardLibraryInjector<'a> {
61 impl<'a> fold::Folder for StandardLibraryInjector<'a> {
62 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
64 // The name to use in `extern crate std = "name";`
65 let actual_crate_name = match self.sess.opts.alt_std_name {
66 Some(ref s) => token::intern_and_get_ident(s.as_slice()),
67 None => token::intern_and_get_ident("std"),
70 let mut vis = vec!(ast::ViewItem {
71 node: ast::ViewItemExternCrate(token::str_to_ident("std"),
72 Some((actual_crate_name, ast::CookedStr)),
75 attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_list_item(
76 InternedString::new("phase"),
78 attr::mk_word_item(InternedString::new("plugin")),
79 attr::mk_word_item(InternedString::new("link")
85 let any_exe = self.sess.crate_types.borrow().iter().any(|ty| {
86 *ty == config::CrateTypeExecutable
88 if use_start(&krate) && any_exe {
89 vis.push(ast::ViewItem {
90 node: ast::ViewItemExternCrate(token::str_to_ident("native"),
99 // `extern crate` must be precede `use` items
100 mem::swap(&mut vis, &mut krate.module.view_items);
101 krate.module.view_items.push_all_move(vis);
103 // don't add #![no_std] here, that will block the prelude injection later.
104 // Add it during the prelude injection instead.
106 // Add #![feature(phase)] here, because we use #[phase] on extern crate std.
107 let feat_phase_attr = attr::mk_attr_inner(attr::mk_attr_id(),
109 InternedString::new("feature"),
110 vec![attr::mk_word_item(InternedString::new("phase"))],
112 // std_inject runs after feature checking so manually mark this attr
113 attr::mark_used(&feat_phase_attr);
114 krate.attrs.push(feat_phase_attr);
120 fn inject_crates_ref(sess: &Session, krate: ast::Crate) -> ast::Crate {
121 let mut fold = StandardLibraryInjector {
124 fold.fold_crate(krate)
127 struct PreludeInjector<'a>;
130 impl<'a> fold::Folder for PreludeInjector<'a> {
131 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
132 // Add #![no_std] here, so we don't re-inject when compiling pretty-printed source.
133 // This must happen here and not in StandardLibraryInjector because this
134 // fold happens second.
136 let no_std_attr = attr::mk_attr_inner(attr::mk_attr_id(),
137 attr::mk_word_item(InternedString::new("no_std")));
138 // std_inject runs after feature checking so manually mark this attr
139 attr::mark_used(&no_std_attr);
140 krate.attrs.push(no_std_attr);
142 if !no_prelude(krate.attrs.as_slice()) {
143 // only add `use std::prelude::*;` if there wasn't a
144 // `#![no_implicit_prelude]` at the crate level.
146 // fold_mod() will insert glob path.
147 let globs_attr = attr::mk_attr_inner(attr::mk_attr_id(),
149 InternedString::new("feature"),
151 attr::mk_word_item(InternedString::new("globs")),
153 // std_inject runs after feature checking so manually mark this attr
154 attr::mark_used(&globs_attr);
155 krate.attrs.push(globs_attr);
157 krate.module = self.fold_mod(&krate.module);
162 fn fold_item(&mut self, item: Gc<ast::Item>) -> SmallVector<Gc<ast::Item>> {
163 if !no_prelude(item.attrs.as_slice()) {
164 // only recur if there wasn't `#![no_implicit_prelude]`
165 // on this item, i.e. this means that the prelude is not
166 // implicitly imported though the whole subtree
167 fold::noop_fold_item(&*item, self)
169 SmallVector::one(item)
173 fn fold_mod(&mut self, module: &ast::Mod) -> ast::Mod {
174 let prelude_path = ast::Path {
179 identifier: token::str_to_ident("std"),
180 lifetimes: Vec::new(),
181 types: OwnedSlice::empty(),
184 identifier: token::str_to_ident("prelude"),
185 lifetimes: Vec::new(),
186 types: OwnedSlice::empty(),
190 let vp = box(GC) codemap::dummy_spanned(ast::ViewPathGlob(prelude_path,
191 ast::DUMMY_NODE_ID));
192 let vi2 = ast::ViewItem {
193 node: ast::ViewItemUse(vp),
199 let (crates, uses) = module.view_items.partitioned(|x| {
201 ast::ViewItemExternCrate(..) => true,
206 // add vi2 after any `extern crate` but before any `use`
207 let mut view_items = crates;
208 view_items.push(vi2);
209 view_items.push_all_move(uses);
211 let new_module = ast::Mod {
212 view_items: view_items,
215 fold::noop_fold_mod(&new_module, self)
219 fn inject_prelude(_: &Session, krate: ast::Crate) -> ast::Crate {
220 let mut fold = PreludeInjector;
221 fold.fold_crate(krate)