rust/src/librustc_builtin_macros/standard_library_imports.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

use rustc_expand::base::{ExtCtxt, Resolver};
use rustc_expand::expand::ExpansionConfig;
use syntax::edition::Edition;
use syntax::ptr::P;
2019-10-14 03:08:26 -05:00
use syntax::sess::ParseSess;
2019-12-22 16:42:04 -06:00
use syntax::symbol::{kw, sym, Ident, Symbol};
use syntax::{ast, attr};
use syntax_pos::hygiene::AstPass;
2019-12-22 16:42:04 -06:00
use syntax_pos::DUMMY_SP;
2015-06-30 22:05:17 -05:00
pub fn inject(
2019-08-25 15:03:24 -05:00
mut krate: ast::Crate,
resolver: &mut dyn Resolver,
2019-09-05 09:05:58 -05:00
sess: &ParseSess,
2019-08-25 15:03:24 -05:00
alt_std_name: Option<Symbol>,
) -> (ast::Crate, Option<Symbol>) {
2019-09-05 09:05:58 -05:00
let rust_2018 = sess.edition >= Edition::Edition2018;
// the first name in this list is the crate name of the crate with the prelude
2019-08-25 15:03:24 -05:00
let names: &[Symbol] = if attr::contains_name(&krate.attrs, sym::no_core) {
return (krate, None);
} else if attr::contains_name(&krate.attrs, sym::no_std) {
if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
2019-08-25 15:03:24 -05:00
&[sym::core]
} else {
2019-08-25 15:03:24 -05:00
&[sym::core, sym::compiler_builtins]
}
} else {
2019-08-25 15:03:24 -05:00
&[sym::std]
};
let expn_id = resolver.expansion_for_ast_pass(
2019-08-25 15:03:24 -05:00
DUMMY_SP,
AstPass::StdImports,
&[sym::prelude_import],
None,
);
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id);
2019-08-25 15:03:24 -05:00
2019-09-05 09:05:58 -05:00
let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
let cx = ExtCtxt::new(sess, ecfg, resolver);
// .rev() to preserve ordering above in combination with insert(0, ...)
2019-09-05 09:05:58 -05:00
for &name in names.iter().rev() {
2019-12-22 16:42:04 -06:00
let ident = if rust_2018 { Ident::new(name, span) } else { Ident::new(name, call_site) };
krate.module.items.insert(
0,
cx.item(
span,
ident,
vec![cx.attribute(cx.meta_word(span, sym::macro_use))],
ast::ItemKind::ExternCrate(alt_std_name),
),
);
}
2019-09-05 09:05:58 -05:00
// The crates have been injected, the assumption is that the first one is
// the one with the prelude.
let name = names[0];
2019-09-05 09:05:58 -05:00
let import_path = if rust_2018 {
2019-12-22 16:42:04 -06:00
[name, sym::prelude, sym::v1].iter().map(|symbol| ast::Ident::new(*symbol, span)).collect()
2019-08-25 15:03:24 -05:00
} else {
2019-12-22 16:42:04 -06:00
[kw::PathRoot, name, sym::prelude, sym::v1]
.iter()
.map(|symbol| ast::Ident::new(*symbol, span))
.collect()
2019-08-25 15:03:24 -05:00
};
2019-09-05 09:05:58 -05:00
let use_item = cx.item(
span,
ast::Ident::invalid(),
vec![cx.attribute(cx.meta_word(span, sym::prelude_import))],
ast::ItemKind::Use(P(ast::UseTree {
prefix: cx.path(span, import_path),
kind: ast::UseTreeKind::Glob,
span,
})),
2019-09-05 09:05:58 -05:00
);
2019-08-25 15:03:24 -05:00
2019-09-05 09:05:58 -05:00
krate.module.items.insert(0, use_item);
2019-08-25 15:03:24 -05:00
(krate, Some(name))
}