]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/std_inject.rs
New upstream version 1.15.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 symbol::{Symbol, keywords};
14 use syntax_pos::{DUMMY_SP, Span};
15 use codemap::{self, ExpnInfo, NameAndSpan, MacroAttribute};
16 use parse::ParseSess;
17 use ptr::P;
18
19 /// Craft a span that will be ignored by the stability lint's
20 /// call to codemap's is_internal check.
21 /// The expanded code uses the unstable `#[prelude_import]` attribute.
22 fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
23 let info = ExpnInfo {
24 call_site: DUMMY_SP,
25 callee: NameAndSpan {
26 format: MacroAttribute(Symbol::intern("std_inject")),
27 span: None,
28 allow_internal_unstable: true,
29 }
30 };
31 let expn_id = sess.codemap().record_expansion(info);
32 let mut sp = sp;
33 sp.expn_id = expn_id;
34 return sp;
35 }
36
37 pub fn injected_crate_name(krate: &ast::Crate) -> Option<&'static str> {
38 if attr::contains_name(&krate.attrs, "no_core") {
39 None
40 } else if attr::contains_name(&krate.attrs, "no_std") {
41 Some("core")
42 } else {
43 Some("std")
44 }
45 }
46
47 pub fn maybe_inject_crates_ref(sess: &ParseSess,
48 mut krate: ast::Crate,
49 alt_std_name: Option<String>)
50 -> ast::Crate {
51 let name = match injected_crate_name(&krate) {
52 Some(name) => name,
53 None => return krate,
54 };
55
56 let crate_name = Symbol::intern(&alt_std_name.unwrap_or(name.to_string()));
57
58 krate.module.items.insert(0, P(ast::Item {
59 attrs: vec![attr::mk_attr_outer(attr::mk_attr_id(),
60 attr::mk_word_item(Symbol::intern("macro_use")))],
61 vis: ast::Visibility::Inherited,
62 node: ast::ItemKind::ExternCrate(Some(crate_name)),
63 ident: ast::Ident::from_str(name),
64 id: ast::DUMMY_NODE_ID,
65 span: DUMMY_SP,
66 }));
67
68 let span = ignored_span(sess, DUMMY_SP);
69 krate.module.items.insert(0, P(ast::Item {
70 attrs: vec![ast::Attribute {
71 style: ast::AttrStyle::Outer,
72 value: ast::MetaItem {
73 name: Symbol::intern("prelude_import"),
74 node: ast::MetaItemKind::Word,
75 span: span,
76 },
77 id: attr::mk_attr_id(),
78 is_sugared_doc: false,
79 span: span,
80 }],
81 vis: ast::Visibility::Inherited,
82 node: ast::ItemKind::Use(P(codemap::dummy_spanned(ast::ViewPathGlob(ast::Path {
83 global: false,
84 segments: vec![name, "prelude", "v1"].into_iter().map(|name| ast::PathSegment {
85 identifier: ast::Ident::from_str(name),
86 parameters: ast::PathParameters::none(),
87 }).collect(),
88 span: span,
89 })))),
90 id: ast::DUMMY_NODE_ID,
91 ident: keywords::Invalid.ident(),
92 span: span,
93 }));
94
95 krate
96 }