]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/std_inject.rs
Auto merge of #22517 - brson:relnotes, 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<'a> {
49     alt_std_name: Option<String>
50 }
51
52 impl<'a> fold::Folder for StandardLibraryInjector<'a> {
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<'a>;
84
85 impl<'a> fold::Folder for PreludeInjector<'a> {
86     fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
87         // only add `use std::prelude::*;` if there wasn't a
88         // `#![no_implicit_prelude]` at the crate level.
89         // fold_mod() will insert glob path.
90         if !no_prelude(&krate.attrs[]) {
91             krate.module = self.fold_mod(krate.module);
92         }
93         krate
94     }
95
96     fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
97         if !no_prelude(&item.attrs[]) {
98             // only recur if there wasn't `#![no_implicit_prelude]`
99             // on this item, i.e. this means that the prelude is not
100             // implicitly imported though the whole subtree
101             fold::noop_fold_item(item, self)
102         } else {
103             SmallVector::one(item)
104         }
105     }
106
107     fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
108         let prelude_path = ast::Path {
109             span: DUMMY_SP,
110             global: false,
111             segments: vec![
112                 ast::PathSegment {
113                     identifier: token::str_to_ident("std"),
114                     parameters: ast::PathParameters::none(),
115                 },
116                 ast::PathSegment {
117                     identifier: token::str_to_ident("prelude"),
118                     parameters: ast::PathParameters::none(),
119                 },
120                 ast::PathSegment {
121                     identifier: token::str_to_ident("v1"),
122                     parameters: ast::PathParameters::none(),
123                 },
124             ],
125         };
126
127         let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
128         mod_.items.insert(0, P(ast::Item {
129             id: ast::DUMMY_NODE_ID,
130             ident: special_idents::invalid,
131             node: ast::ItemUse(vp),
132             attrs: vec![ast::Attribute {
133                 span: DUMMY_SP,
134                 node: ast::Attribute_ {
135                     id: attr::mk_attr_id(),
136                     style: ast::AttrOuter,
137                     value: P(ast::MetaItem {
138                         span: DUMMY_SP,
139                         node: ast::MetaWord(token::get_name(
140                                 special_idents::prelude_import.name)),
141                     }),
142                     is_sugared_doc: false,
143                 },
144             }],
145             vis: ast::Inherited,
146             span: DUMMY_SP,
147         }));
148
149         fold::noop_fold_mod(mod_, self)
150     }
151 }
152
153 fn inject_prelude(krate: ast::Crate) -> ast::Crate {
154     let mut fold = PreludeInjector;
155     fold.fold_crate(krate)
156 }