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