]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/std_inject.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / libsyntax / std_inject.rs
CommitLineData
1a4d82fc
JJ
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
11use ast;
12use attr;
3157f602
XL
13use syntax_pos::{DUMMY_SP, Span};
14use codemap::{self, ExpnInfo, NameAndSpan, MacroAttribute};
a7813a04 15use parse::token::{intern, InternedString, keywords};
c1a9b12d 16use parse::{token, ParseSess};
1a4d82fc 17use ptr::P;
1a4d82fc 18
c1a9b12d
SL
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.
22fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
23 let info = ExpnInfo {
24 call_site: DUMMY_SP,
25 callee: NameAndSpan {
e9174d1e 26 format: MacroAttribute(intern("std_inject")),
c1a9b12d
SL
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
c30ab7b3
SL
37pub 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 }
1a4d82fc
JJ
45}
46
3157f602
XL
47pub fn maybe_inject_crates_ref(sess: &ParseSess,
48 mut krate: ast::Crate,
49 alt_std_name: Option<String>)
50 -> ast::Crate {
c30ab7b3
SL
51 let name = match injected_crate_name(&krate) {
52 Some(name) => name,
53 None => return krate,
54 };
1a4d82fc 55
3157f602
XL
56 let crate_name = token::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(InternedString::new("macro_use")))],
61 vis: ast::Visibility::Inherited,
62 node: ast::ItemKind::ExternCrate(Some(crate_name)),
63 ident: token::str_to_ident(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 node: ast::Attribute_ {
72 style: ast::AttrStyle::Outer,
73 value: P(ast::MetaItem {
74 node: ast::MetaItemKind::Word(token::intern_and_get_ident("prelude_import")),
75 span: span,
76 }),
77 id: attr::mk_attr_id(),
78 is_sugared_doc: false,
79 },
80 span: span,
81 }],
82 vis: ast::Visibility::Inherited,
83 node: ast::ItemKind::Use(P(codemap::dummy_spanned(ast::ViewPathGlob(ast::Path {
1a4d82fc 84 global: false,
3157f602
XL
85 segments: vec![name, "prelude", "v1"].into_iter().map(|name| ast::PathSegment {
86 identifier: token::str_to_ident(name),
87 parameters: ast::PathParameters::none(),
88 }).collect(),
89 span: span,
90 })))),
91 id: ast::DUMMY_NODE_ID,
92 ident: keywords::Invalid.ident(),
93 span: span,
94 }));
95
96 krate
1a4d82fc 97}