]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/std_inject.rs
Imported Upstream version 1.10.0+dfsg1
[rustc.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, Span, ExpnInfo, NameAndSpan, MacroAttribute};
14 use codemap;
15 use fold::Folder;
16 use fold;
17 use parse::token::{intern, InternedString, keywords};
18 use parse::{token, ParseSess};
19 use ptr::P;
20 use util::small_vector::SmallVector;
21
22 /// Craft a span that will be ignored by the stability lint's
23 /// call to codemap's is_internal check.
24 /// The expanded code uses the unstable `#[prelude_import]` attribute.
25 fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
26 let info = ExpnInfo {
27 call_site: DUMMY_SP,
28 callee: NameAndSpan {
29 format: MacroAttribute(intern("std_inject")),
30 span: None,
31 allow_internal_unstable: true,
32 }
33 };
34 let expn_id = sess.codemap().record_expansion(info);
35 let mut sp = sp;
36 sp.expn_id = expn_id;
37 return sp;
38 }
39
40 pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>)
41 -> ast::Crate {
42 if no_core(&krate) {
43 krate
44 } else {
45 let name = if no_std(&krate) {"core"} else {"std"};
46 let mut fold = CrateInjector {
47 item_name: token::str_to_ident(name),
48 crate_name: token::intern(&alt_std_name.unwrap_or(name.to_string())),
49 };
50 fold.fold_crate(krate)
51 }
52 }
53
54 pub fn maybe_inject_prelude(sess: &ParseSess, krate: ast::Crate) -> ast::Crate {
55 if no_core(&krate) {
56 krate
57 } else {
58 let name = if no_std(&krate) {"core"} else {"std"};
59 let mut fold = PreludeInjector {
60 span: ignored_span(sess, DUMMY_SP),
61 crate_identifier: token::str_to_ident(name),
62 };
63 fold.fold_crate(krate)
64 }
65 }
66
67 pub fn no_core(krate: &ast::Crate) -> bool {
68 attr::contains_name(&krate.attrs, "no_core")
69 }
70
71 pub fn no_std(krate: &ast::Crate) -> bool {
72 attr::contains_name(&krate.attrs, "no_std") || no_core(krate)
73 }
74
75 fn no_prelude(attrs: &[ast::Attribute]) -> bool {
76 attr::contains_name(attrs, "no_implicit_prelude")
77 }
78
79 struct CrateInjector {
80 item_name: ast::Ident,
81 crate_name: ast::Name,
82 }
83
84 impl fold::Folder for CrateInjector {
85 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
86 krate.module.items.insert(0, P(ast::Item {
87 id: ast::DUMMY_NODE_ID,
88 ident: self.item_name,
89 attrs: vec!(
90 attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(
91 InternedString::new("macro_use")))),
92 node: ast::ItemKind::ExternCrate(Some(self.crate_name)),
93 vis: ast::Visibility::Inherited,
94 span: DUMMY_SP
95 }));
96
97 krate
98 }
99 }
100
101 struct PreludeInjector {
102 span: Span,
103 crate_identifier: ast::Ident,
104 }
105
106 impl fold::Folder for PreludeInjector {
107 fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
108 // only add `use std::prelude::*;` if there wasn't a
109 // `#![no_implicit_prelude]` at the crate level.
110 // fold_mod() will insert glob path.
111 if !no_prelude(&krate.attrs) {
112 krate.module = self.fold_mod(krate.module);
113 }
114 krate
115 }
116
117 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
118 if !no_prelude(&item.attrs) {
119 // only recur if there wasn't `#![no_implicit_prelude]`
120 // on this item, i.e. this means that the prelude is not
121 // implicitly imported though the whole subtree
122 fold::noop_fold_item(item, self)
123 } else {
124 SmallVector::one(item)
125 }
126 }
127
128 fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
129 let prelude_path = ast::Path {
130 span: self.span,
131 global: false,
132 segments: vec![
133 ast::PathSegment {
134 identifier: self.crate_identifier,
135 parameters: ast::PathParameters::none(),
136 },
137 ast::PathSegment {
138 identifier: token::str_to_ident("prelude"),
139 parameters: ast::PathParameters::none(),
140 },
141 ast::PathSegment {
142 identifier: token::str_to_ident("v1"),
143 parameters: ast::PathParameters::none(),
144 },
145 ],
146 };
147
148 let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
149 mod_.items.insert(0, P(ast::Item {
150 id: ast::DUMMY_NODE_ID,
151 ident: keywords::Invalid.ident(),
152 node: ast::ItemKind::Use(vp),
153 attrs: vec![ast::Attribute {
154 span: self.span,
155 node: ast::Attribute_ {
156 id: attr::mk_attr_id(),
157 style: ast::AttrStyle::Outer,
158 value: P(ast::MetaItem {
159 span: self.span,
160 node: ast::MetaItemKind::Word(
161 token::intern_and_get_ident("prelude_import")
162 ),
163 }),
164 is_sugared_doc: false,
165 },
166 }],
167 vis: ast::Visibility::Inherited,
168 span: self.span,
169 }));
170
171 fold::noop_fold_mod(mod_, self)
172 }
173 }