]> git.lizzy.rs Git - rust.git/blob - src/librustc/front/std_inject.rs
auto merge of #11098 : erickt/rust/collect, r=alexcrichton
[rust.git] / src / librustc / front / std_inject.rs
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.
4 //
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.
10
11
12 use driver::session::Session;
13
14 use std::vec;
15 use syntax::ast;
16 use syntax::attr;
17 use syntax::codemap::dummy_sp;
18 use syntax::codemap;
19 use syntax::fold::ast_fold;
20 use syntax::fold;
21 use syntax::opt_vec;
22 use syntax::util::small_vector::SmallVector;
23
24 static STD_VERSION: &'static str = "0.9-pre";
25
26 pub fn maybe_inject_libstd_ref(sess: Session, crate: ast::Crate)
27                                -> ast::Crate {
28     if use_std(&crate) {
29         inject_libstd_ref(sess, crate)
30     } else {
31         crate
32     }
33 }
34
35 fn use_std(crate: &ast::Crate) -> bool {
36     !attr::contains_name(crate.attrs, "no_std")
37 }
38
39 fn use_uv(crate: &ast::Crate) -> bool {
40     !attr::contains_name(crate.attrs, "no_uv")
41 }
42
43 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
44     attr::contains_name(attrs, "no_implicit_prelude")
45 }
46
47 fn spanned<T>(x: T) -> codemap::Spanned<T> {
48     codemap::Spanned {
49         node: x,
50         span: dummy_sp(),
51     }
52 }
53
54 struct StandardLibraryInjector {
55     sess: Session,
56 }
57
58 impl fold::ast_fold for StandardLibraryInjector {
59     fn fold_crate(&self, crate: ast::Crate) -> ast::Crate {
60         let version = STD_VERSION.to_managed();
61         let vers_item = attr::mk_name_value_item_str(@"vers", version);
62         let mut vis = ~[ast::view_item {
63             node: ast::view_item_extern_mod(self.sess.ident_of("std"),
64                                             None,
65                                             ~[vers_item.clone()],
66                                             ast::DUMMY_NODE_ID),
67             attrs: ~[],
68             vis: ast::private,
69             span: dummy_sp()
70         }];
71
72         if use_uv(&crate) && !self.sess.building_library.get() {
73             vis.push(ast::view_item {
74                 node: ast::view_item_extern_mod(self.sess.ident_of("green"),
75                                                 None,
76                                                 ~[vers_item],
77                                                 ast::DUMMY_NODE_ID),
78                 attrs: ~[],
79                 vis: ast::private,
80                 span: dummy_sp()
81             });
82             vis.push(ast::view_item {
83                 node: ast::view_item_extern_mod(self.sess.ident_of("rustuv"),
84                                                 None,
85                                                 ~[vers_item],
86                                                 ast::DUMMY_NODE_ID),
87                 attrs: ~[],
88                 vis: ast::private,
89                 span: dummy_sp()
90             });
91         }
92
93         vis.push_all(crate.module.view_items);
94         let mut new_module = ast::_mod {
95             view_items: vis,
96             ..crate.module.clone()
97         };
98
99         if !no_prelude(crate.attrs) {
100             // only add `use std::prelude::*;` if there wasn't a
101             // `#[no_implicit_prelude];` at the crate level.
102             new_module = self.fold_mod(&new_module);
103         }
104
105         ast::Crate {
106             module: new_module,
107             ..crate
108         }
109     }
110
111     fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
112         if !no_prelude(item.attrs) {
113             // only recur if there wasn't `#[no_implicit_prelude];`
114             // on this item, i.e. this means that the prelude is not
115             // implicitly imported though the whole subtree
116             fold::noop_fold_item(item, self)
117         } else {
118             SmallVector::one(item)
119         }
120     }
121
122     fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
123         let prelude_path = ast::Path {
124             span: dummy_sp(),
125             global: false,
126             segments: ~[
127                 ast::PathSegment {
128                     identifier: self.sess.ident_of("std"),
129                     lifetimes: opt_vec::Empty,
130                     types: opt_vec::Empty,
131                 },
132                 ast::PathSegment {
133                     identifier: self.sess.ident_of("prelude"),
134                     lifetimes: opt_vec::Empty,
135                     types: opt_vec::Empty,
136                 },
137             ],
138         };
139
140         let vp = @spanned(ast::view_path_glob(prelude_path,
141                                               ast::DUMMY_NODE_ID));
142         let vi2 = ast::view_item {
143             node: ast::view_item_use(~[vp]),
144             attrs: ~[],
145             vis: ast::private,
146             span: dummy_sp(),
147         };
148
149         let vis = vec::append(~[vi2], module.view_items);
150
151         // FIXME #2543: Bad copy.
152         let new_module = ast::_mod {
153             view_items: vis,
154             ..(*module).clone()
155         };
156         fold::noop_fold_mod(&new_module, self)
157     }
158 }
159
160 fn inject_libstd_ref(sess: Session, crate: ast::Crate) -> ast::Crate {
161     let fold = StandardLibraryInjector {
162         sess: sess,
163     };
164     fold.fold_crate(crate)
165 }