]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/std_inject.rs
Rollup merge of #40521 - TimNN:panic-free-shift, r=alexcrichton
[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 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 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(sess: &ParseSess, sp: Span) -> Span {
24     let info = ExpnInfo {
25         call_site: DUMMY_SP,
26         callee: NameAndSpan {
27             format: MacroAttribute(Symbol::intern("std_inject")),
28             span: None,
29             allow_internal_unstable: true,
30         }
31     };
32     let expn_id = sess.codemap().record_expansion(info);
33     let mut sp = sp;
34     sp.expn_id = expn_id;
35     return sp;
36 }
37
38 pub fn injected_crate_name(krate: &ast::Crate) -> Option<&'static str> {
39     if attr::contains_name(&krate.attrs, "no_core") {
40         None
41     } else if attr::contains_name(&krate.attrs, "no_std") {
42         Some("core")
43     } else {
44         Some("std")
45     }
46 }
47
48 pub fn maybe_inject_crates_ref(sess: &ParseSess,
49                                mut krate: ast::Crate,
50                                alt_std_name: Option<String>)
51                                -> ast::Crate {
52     let name = match injected_crate_name(&krate) {
53         Some(name) => name,
54         None => return krate,
55     };
56
57     let crate_name = Symbol::intern(&alt_std_name.unwrap_or(name.to_string()));
58
59     krate.module.items.insert(0, P(ast::Item {
60         attrs: vec![attr::mk_attr_outer(DUMMY_SP,
61                                         attr::mk_attr_id(),
62                                         attr::mk_word_item(Symbol::intern("macro_use")))],
63         vis: ast::Visibility::Inherited,
64         node: ast::ItemKind::ExternCrate(Some(crate_name)),
65         ident: ast::Ident::from_str(name),
66         id: ast::DUMMY_NODE_ID,
67         span: DUMMY_SP,
68     }));
69
70     let span = ignored_span(sess, DUMMY_SP);
71     krate.module.items.insert(0, P(ast::Item {
72         attrs: vec![ast::Attribute {
73             style: ast::AttrStyle::Outer,
74             path: ast::Path::from_ident(span, ast::Ident::from_str("prelude_import")),
75             tokens: TokenStream::empty(),
76             id: attr::mk_attr_id(),
77             is_sugared_doc: false,
78             span: span,
79         }],
80         vis: ast::Visibility::Inherited,
81         node: ast::ItemKind::Use(P(codemap::dummy_spanned(ast::ViewPathGlob(ast::Path {
82             segments: ["{{root}}", name, "prelude", "v1"].into_iter().map(|name| {
83                 ast::PathSegment::from_ident(ast::Ident::from_str(name), DUMMY_SP)
84             }).collect(),
85             span: span,
86         })))),
87         id: ast::DUMMY_NODE_ID,
88         ident: keywords::Invalid.ident(),
89         span: span,
90     }));
91
92     krate
93 }