]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/std_inject.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libsyntax / 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 use ast;
12 use attr;
13 use codemap::DUMMY_SP;
14 use codemap;
15 use fold::Folder;
16 use fold;
17 use parse::token::InternedString;
18 use parse::token::special_idents;
19 use parse::token;
20 use ptr::P;
21 use util::small_vector::SmallVector;
22
23 pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>)
24                                -> ast::Crate {
25     if use_std(&krate) {
26         inject_crates_ref(krate, alt_std_name)
27     } else {
28         krate
29     }
30 }
31
32 pub fn maybe_inject_prelude(krate: ast::Crate) -> ast::Crate {
33     if use_std(&krate) {
34         inject_prelude(krate)
35     } else {
36         krate
37     }
38 }
39
40 pub fn use_std(krate: &ast::Crate) -> bool {
41     !attr::contains_name(&krate.attrs[], "no_std")
42 }
43
44 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
45     attr::contains_name(attrs, "no_implicit_prelude")
46 }
47
48 struct StandardLibraryInjector {
49     alt_std_name: Option<String>,
50 }
51
52 impl fold::Folder for StandardLibraryInjector {
53     fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
54
55         // The name to use in `extern crate "name" as std;`
56         let actual_crate_name = match self.alt_std_name {
57             Some(ref s) => token::intern_and_get_ident(&s[..]),
58             None => token::intern_and_get_ident("std"),
59         };
60
61         krate.module.items.insert(0, P(ast::Item {
62             id: ast::DUMMY_NODE_ID,
63             ident: token::str_to_ident("std"),
64             attrs: vec!(
65                 attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(
66                         InternedString::new("macro_use")))),
67             node: ast::ItemExternCrate(Some((actual_crate_name, ast::CookedStr))),
68             vis: ast::Inherited,
69             span: DUMMY_SP
70         }));
71
72         krate
73     }
74 }
75
76 fn inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate {
77     let mut fold = StandardLibraryInjector {
78         alt_std_name: alt_std_name
79     };
80     fold.fold_crate(krate)
81 }
82
83 struct PreludeInjector;
84
85
86 impl fold::Folder for PreludeInjector {
87     fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
88         // only add `use std::prelude::*;` if there wasn't a
89         // `#![no_implicit_prelude]` at the crate level.
90         // fold_mod() will insert glob path.
91         if !no_prelude(&krate.attrs[]) {
92             krate.module = self.fold_mod(krate.module);
93         }
94         krate
95     }
96
97     fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
98         if !no_prelude(&item.attrs[]) {
99             // only recur if there wasn't `#![no_implicit_prelude]`
100             // on this item, i.e. this means that the prelude is not
101             // implicitly imported though the whole subtree
102             fold::noop_fold_item(item, self)
103         } else {
104             SmallVector::one(item)
105         }
106     }
107
108     fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
109         let prelude_path = ast::Path {
110             span: DUMMY_SP,
111             global: false,
112             segments: vec![
113                 ast::PathSegment {
114                     identifier: token::str_to_ident("std"),
115                     parameters: ast::PathParameters::none(),
116                 },
117                 ast::PathSegment {
118                     identifier: token::str_to_ident("prelude"),
119                     parameters: ast::PathParameters::none(),
120                 },
121                 ast::PathSegment {
122                     identifier: token::str_to_ident("v1"),
123                     parameters: ast::PathParameters::none(),
124                 },
125             ],
126         };
127
128         let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
129         mod_.items.insert(0, P(ast::Item {
130             id: ast::DUMMY_NODE_ID,
131             ident: special_idents::invalid,
132             node: ast::ItemUse(vp),
133             attrs: vec![ast::Attribute {
134                 span: DUMMY_SP,
135                 node: ast::Attribute_ {
136                     id: attr::mk_attr_id(),
137                     style: ast::AttrOuter,
138                     value: P(ast::MetaItem {
139                         span: DUMMY_SP,
140                         node: ast::MetaWord(token::get_name(
141                                 special_idents::prelude_import.name)),
142                     }),
143                     is_sugared_doc: false,
144                 },
145             }],
146             vis: ast::Inherited,
147             span: DUMMY_SP,
148         }));
149
150         fold::noop_fold_mod(mod_, self)
151     }
152 }
153
154 fn inject_prelude(krate: ast::Crate) -> ast::Crate {
155     let mut fold = PreludeInjector;
156     fold.fold_crate(krate)
157 }