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