]> git.lizzy.rs Git - rust.git/blob - src/librustc/front/std_inject.rs
auto merge of #9450 : jzelinskie/rust/tutorial-tasks-result-signature, 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
23 static STD_VERSION: &'static str = "0.8";
24
25 pub fn maybe_inject_libstd_ref(sess: Session, crate: @ast::Crate)
26                                -> @ast::Crate {
27     if use_std(crate) {
28         inject_libstd_ref(sess, crate)
29     } else {
30         crate
31     }
32 }
33
34 fn use_std(crate: &ast::Crate) -> bool {
35     !attr::contains_name(crate.attrs, "no_std")
36 }
37
38 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
39     attr::contains_name(attrs, "no_implicit_prelude")
40 }
41
42 fn spanned<T>(x: T) -> codemap::Spanned<T> {
43     codemap::Spanned {
44         node: x,
45         span: dummy_sp(),
46     }
47 }
48
49 struct StandardLibraryInjector {
50     sess: Session,
51 }
52
53 impl fold::ast_fold for StandardLibraryInjector {
54     fn fold_crate(&self, crate: &ast::Crate) -> ast::Crate {
55         let version = STD_VERSION.to_managed();
56         let vi1 = ast::view_item {
57             node: ast::view_item_extern_mod(self.sess.ident_of("std"),
58                                             None,
59                                             ~[],
60                                             ast::DUMMY_NODE_ID),
61             attrs: ~[
62                 attr::mk_attr(attr::mk_name_value_item_str(@"vers", version))
63             ],
64             vis: ast::private,
65             span: dummy_sp()
66         };
67
68         let vis = vec::append(~[vi1], crate.module.view_items);
69         let mut new_module = ast::_mod {
70             view_items: vis,
71             ..crate.module.clone()
72         };
73
74         if !no_prelude(crate.attrs) {
75             // only add `use std::prelude::*;` if there wasn't a
76             // `#[no_implicit_prelude];` at the crate level.
77             new_module = self.fold_mod(&new_module);
78         }
79
80         // FIXME #2543: Bad copy.
81         ast::Crate {
82             module: new_module,
83             ..(*crate).clone()
84         }
85     }
86
87     fn fold_item(&self, item: @ast::item) -> Option<@ast::item> {
88         if !no_prelude(item.attrs) {
89             // only recur if there wasn't `#[no_implicit_prelude];`
90             // on this item, i.e. this means that the prelude is not
91             // implicitly imported though the whole subtree
92             fold::noop_fold_item(item, self)
93         } else {
94             Some(item)
95         }
96     }
97
98     fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
99         let prelude_path = ast::Path {
100             span: dummy_sp(),
101             global: false,
102             segments: ~[
103                 ast::PathSegment {
104                     identifier: self.sess.ident_of("std"),
105                     lifetime: None,
106                     types: opt_vec::Empty,
107                 },
108                 ast::PathSegment {
109                     identifier: self.sess.ident_of("prelude"),
110                     lifetime: None,
111                     types: opt_vec::Empty,
112                 },
113             ],
114         };
115
116         let vp = @spanned(ast::view_path_glob(prelude_path,
117                                               ast::DUMMY_NODE_ID));
118         let vi2 = ast::view_item {
119             node: ast::view_item_use(~[vp]),
120             attrs: ~[],
121             vis: ast::private,
122             span: dummy_sp(),
123         };
124
125         let vis = vec::append(~[vi2], module.view_items);
126
127         // FIXME #2543: Bad copy.
128         let new_module = ast::_mod {
129             view_items: vis,
130             ..(*module).clone()
131         };
132         fold::noop_fold_mod(&new_module, self)
133     }
134 }
135
136 fn inject_libstd_ref(sess: Session, crate: &ast::Crate) -> @ast::Crate {
137     let fold = StandardLibraryInjector {
138         sess: sess,
139     };
140     @fold.fold_crate(crate)
141 }