2012-12-03 18:48:01 -06:00
|
|
|
// 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-10 17:35:21 -05:00
|
|
|
use ast;
|
|
|
|
use attr;
|
2017-12-12 13:57:58 -06:00
|
|
|
use std::cell::Cell;
|
2017-03-16 23:04:41 -05:00
|
|
|
use ext::hygiene::{Mark, SyntaxContext};
|
2016-11-16 02:21:52 -06:00
|
|
|
use symbol::{Symbol, keywords};
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::{DUMMY_SP, Span};
|
2018-02-04 06:19:14 -06:00
|
|
|
use codemap::{ExpnInfo, NameAndSpan, MacroAttribute, dummy_spanned, respan};
|
2014-09-10 17:35:21 -05:00
|
|
|
use ptr::P;
|
2017-03-03 03:23:59 -06:00
|
|
|
use tokenstream::TokenStream;
|
2012-01-26 17:20:29 -06:00
|
|
|
|
2015-06-30 22:05:17 -05:00
|
|
|
/// Craft a span that will be ignored by the stability lint's
|
2017-05-12 13:05:39 -05:00
|
|
|
/// call to codemap's `is_internal` check.
|
2015-06-30 22:05:17 -05:00
|
|
|
/// The expanded code uses the unstable `#[prelude_import]` attribute.
|
2017-03-16 23:04:41 -05:00
|
|
|
fn ignored_span(sp: Span) -> Span {
|
2017-03-22 03:39:51 -05:00
|
|
|
let mark = Mark::fresh(Mark::root());
|
2017-03-16 23:04:41 -05:00
|
|
|
mark.set_expn_info(ExpnInfo {
|
2015-06-30 22:05:17 -05:00
|
|
|
call_site: DUMMY_SP,
|
|
|
|
callee: NameAndSpan {
|
2016-11-16 02:21:52 -06:00
|
|
|
format: MacroAttribute(Symbol::intern("std_inject")),
|
2015-06-30 22:05:17 -05:00
|
|
|
span: None,
|
|
|
|
allow_internal_unstable: true,
|
2017-08-08 10:21:20 -05:00
|
|
|
allow_internal_unsafe: false,
|
2015-06-30 22:05:17 -05:00
|
|
|
}
|
2017-03-16 23:04:41 -05:00
|
|
|
});
|
2017-07-31 15:04:34 -05:00
|
|
|
sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
|
2015-06-30 22:05:17 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:57:58 -06: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);
|
2012-01-26 18:23:34 -06:00
|
|
|
}
|
2013-07-19 00:38:55 -05:00
|
|
|
|
2018-03-09 09:51:48 -06:00
|
|
|
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
|
2018-03-30 06:06:34 -05:00
|
|
|
// the first name in this list is the crate name of the crate with the prelude
|
|
|
|
let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") {
|
2017-12-12 13:57:58 -06:00
|
|
|
return krate;
|
|
|
|
} else if attr::contains_name(&krate.attrs, "no_std") {
|
2018-03-30 06:06:34 -05:00
|
|
|
if attr::contains_name(&krate.attrs, "compiler_builtins") {
|
|
|
|
&["core"]
|
|
|
|
} else {
|
|
|
|
&["core", "compiler_builtins"]
|
|
|
|
}
|
2017-12-12 13:57:58 -06:00
|
|
|
} else {
|
2018-03-30 06:06:34 -05:00
|
|
|
&["std"]
|
2016-09-28 17:28:19 -05:00
|
|
|
};
|
2013-08-29 14:10:02 -05:00
|
|
|
|
2018-03-30 06:06:34 -05:00
|
|
|
for name in names {
|
|
|
|
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")))],
|
|
|
|
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-12-12 13:57:58 -06:00
|
|
|
|
2018-03-30 06:06:34 -05:00
|
|
|
// the crates have been injected, the assumption is that the first one is the one with
|
|
|
|
// the prelude.
|
|
|
|
let name = names[0];
|
|
|
|
|
|
|
|
INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
|
2016-06-05 04:56:05 -05:00
|
|
|
|
2017-03-16 23:04:41 -05:00
|
|
|
let span = ignored_span(DUMMY_SP);
|
2016-06-05 04:56:05 -05:00
|
|
|
krate.module.items.insert(0, P(ast::Item {
|
|
|
|
attrs: vec![ast::Attribute {
|
2016-11-14 06:00:25 -06:00
|
|
|
style: ast::AttrStyle::Outer,
|
2018-03-18 19:54:56 -05:00
|
|
|
path: ast::Path::from_ident(ast::Ident::new(Symbol::intern("prelude_import"), span)),
|
2017-03-03 03:23:59 -06:00
|
|
|
tokens: TokenStream::empty(),
|
2016-11-14 06:00:25 -06:00
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
is_sugared_doc: false,
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2016-06-05 04:56:05 -05:00
|
|
|
}],
|
2018-03-10 08:45:47 -06:00
|
|
|
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
2017-09-26 16:04:00 -05:00
|
|
|
node: ast::ItemKind::Use(P(ast::UseTree {
|
|
|
|
prefix: ast::Path {
|
2018-03-09 17:02:39 -06:00
|
|
|
segments: [name, "prelude", "v1"].into_iter().map(|name| {
|
2018-03-18 19:54:56 -05:00
|
|
|
ast::PathSegment::from_ident(ast::Ident::from_str(name))
|
2017-09-26 16:04:00 -05:00
|
|
|
}).collect(),
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
kind: ast::UseTreeKind::Glob,
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2017-09-26 16:04:00 -05:00
|
|
|
})),
|
2016-06-05 04:56:05 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
ident: keywords::Invalid.ident(),
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2017-07-10 19:44:46 -05:00
|
|
|
tokens: None,
|
2016-06-05 04:56:05 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
krate
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|