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