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