1
Fork 0
rust/src/libsyntax/std_inject.rs

98 lines
3.3 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2014-09-11 10:35:21 +12:00
use ast;
use attr;
use std::cell::Cell;
2017-03-17 04:04:41 +00:00
use ext::hygiene::{Mark, SyntaxContext};
use symbol::{Symbol, keywords};
use syntax_pos::{DUMMY_SP, Span};
2018-02-04 21:19:14 +09:00
use codemap::{ExpnInfo, NameAndSpan, MacroAttribute, dummy_spanned, respan};
2014-09-11 10:35:21 +12:00
use ptr::P;
use tokenstream::TokenStream;
2015-07-01 06:05:17 +03:00
/// Craft a span that will be ignored by the stability lint's
/// call to codemap's `is_internal` check.
2015-07-01 06:05:17 +03:00
/// The expanded code uses the unstable `#[prelude_import]` attribute.
2017-03-17 04:04:41 +00:00
fn ignored_span(sp: Span) -> Span {
2017-03-22 08:39:51 +00:00
let mark = Mark::fresh(Mark::root());
2017-03-17 04:04:41 +00:00
mark.set_expn_info(ExpnInfo {
2015-07-01 06:05:17 +03:00
call_site: DUMMY_SP,
callee: NameAndSpan {
format: MacroAttribute(Symbol::intern("std_inject")),
2015-07-01 06:05:17 +03:00
span: None,
allow_internal_unstable: true,
allow_internal_unsafe: false,
2015-07-01 06:05:17 +03:00
}
2017-03-17 04:04:41 +00:00
});
2017-07-31 23:04:34 +03:00
sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
2015-07-01 06:05:17 +03:00
}
pub fn injected_crate_name() -> Option<&'static str> {
INJECTED_CRATE_NAME.with(|name| name.get())
}
thread_local! {
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
}
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
let name = if attr::contains_name(&krate.attrs, "no_core") {
return krate;
} else if attr::contains_name(&krate.attrs, "no_std") {
"core"
} else {
"std"
};
INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
krate.module.items.insert(0, P(ast::Item {
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
attr::mk_attr_id(),
attr::mk_word_item(ast::Ident::from_str("macro_use")))],
2018-01-29 14:12:09 +09:00
vis: dummy_spanned(ast::VisibilityKind::Inherited),
node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)),
ident: ast::Ident::from_str(name),
id: ast::DUMMY_NODE_ID,
span: DUMMY_SP,
tokens: None,
}));
2017-03-17 04:04:41 +00:00
let span = ignored_span(DUMMY_SP);
krate.module.items.insert(0, P(ast::Item {
attrs: vec![ast::Attribute {
2016-11-14 12:00:25 +00:00
style: ast::AttrStyle::Outer,
2018-03-19 03:54:56 +03:00
path: ast::Path::from_ident(ast::Ident::new(Symbol::intern("prelude_import"), span)),
tokens: TokenStream::empty(),
2016-11-14 12:00:25 +00:00
id: attr::mk_attr_id(),
is_sugared_doc: false,
span,
}],
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
node: ast::ItemKind::Use(P(ast::UseTree {
prefix: ast::Path {
segments: [name, "prelude", "v1"].into_iter().map(|name| {
2018-03-19 03:54:56 +03:00
ast::PathSegment::from_ident(ast::Ident::from_str(name))
}).collect(),
span,
},
kind: ast::UseTreeKind::Glob,
span,
})),
id: ast::DUMMY_NODE_ID,
ident: keywords::Invalid.ident(),
span,
tokens: None,
}));
krate
}