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;
17 use syntax::codemap::DUMMY_SP;
19 use syntax::fold::ast_fold;
22 use syntax::util::small_vector::SmallVector;
24 pub static VERSION: &'static str = "0.9-pre";
26 pub fn maybe_inject_libstd_ref(sess: Session, crate: ast::Crate)
29 inject_libstd_ref(sess, crate)
35 fn use_std(crate: &ast::Crate) -> bool {
36 !attr::contains_name(crate.attrs, "no_std")
39 fn use_uv(crate: &ast::Crate) -> bool {
40 !attr::contains_name(crate.attrs, "no_uv")
43 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
44 attr::contains_name(attrs, "no_implicit_prelude")
47 fn spanned<T>(x: T) -> codemap::Spanned<T> {
54 struct StandardLibraryInjector {
58 impl fold::ast_fold for StandardLibraryInjector {
59 fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate {
60 let mut vis = ~[ast::view_item {
61 node: ast::view_item_extern_mod(self.sess.ident_of("std"),
62 Some((format!("std\\#{}", VERSION).to_managed(),
70 if use_uv(&crate) && !self.sess.building_library.get() {
71 vis.push(ast::view_item {
72 node: ast::view_item_extern_mod(self.sess.ident_of("green"),
73 Some((format!("green\\#{}", VERSION).to_managed(),
80 vis.push(ast::view_item {
81 node: ast::view_item_extern_mod(self.sess.ident_of("rustuv"),
82 Some((format!("rustuv\\#{}", VERSION).to_managed(),
91 vis.push_all(crate.module.view_items);
92 let mut new_module = ast::_mod {
94 ..crate.module.clone()
97 if !no_prelude(crate.attrs) {
98 // only add `use std::prelude::*;` if there wasn't a
99 // `#[no_implicit_prelude];` at the crate level.
100 new_module = self.fold_mod(&new_module);
109 fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> {
110 if !no_prelude(item.attrs) {
111 // only recur if there wasn't `#[no_implicit_prelude];`
112 // on this item, i.e. this means that the prelude is not
113 // implicitly imported though the whole subtree
114 fold::noop_fold_item(item, self)
116 SmallVector::one(item)
120 fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
121 let prelude_path = ast::Path {
126 identifier: self.sess.ident_of("std"),
127 lifetimes: opt_vec::Empty,
128 types: opt_vec::Empty,
131 identifier: self.sess.ident_of("prelude"),
132 lifetimes: opt_vec::Empty,
133 types: opt_vec::Empty,
138 let vp = @spanned(ast::view_path_glob(prelude_path,
139 ast::DUMMY_NODE_ID));
140 let vi2 = ast::view_item {
141 node: ast::view_item_use(~[vp]),
147 let vis = vec::append(~[vi2], module.view_items);
149 // FIXME #2543: Bad copy.
150 let new_module = ast::_mod {
154 fold::noop_fold_mod(&new_module, self)
158 fn inject_libstd_ref(sess: Session, crate: ast::Crate) -> ast::Crate {
159 let mut fold = StandardLibraryInjector {
162 fold.fold_crate(crate)