about summary refs log tree commit diff
path: root/src/libsyntax_ext/standard_library_imports.rs
blob: c577b1e33cfebab0f18d8fc90d33d569bcaa051f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use syntax::{ast, attr};
use syntax::edition::Edition;
use syntax::ext::expand::ExpansionConfig;
use syntax::ext::hygiene::AstPass;
use syntax::ext::base::{ExtCtxt, Resolver};
use syntax::parse::ParseSess;
use syntax::ptr::P;
use syntax::symbol::{Ident, Symbol, kw, sym};
use syntax_pos::DUMMY_SP;

pub fn inject(
    mut krate: ast::Crate,
    resolver: &mut dyn Resolver,
    sess: &ParseSess,
    alt_std_name: Option<Symbol>,
) -> (ast::Crate, Option<Symbol>) {
    let rust_2018 = sess.edition >= Edition::Edition2018;

    // the first name in this list is the crate name of the crate with the prelude
    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) {
            &[sym::core]
        } else {
            &[sym::core, sym::compiler_builtins]
        }
    } else {
        &[sym::std]
    };

    let expn_id = resolver.expansion_for_ast_pass(
        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);

    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, ...)
    for &name in names.iter().rev() {
        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),
        ));
    }

    // The crates have been injected, the assumption is that the first one is
    // the one with the prelude.
    let name = names[0];

    let import_path = if rust_2018 {
        [name, sym::prelude, sym::v1].iter()
            .map(|symbol| ast::Ident::new(*symbol, span)).collect()
    } else {
        [kw::PathRoot, name, sym::prelude, sym::v1].iter()
            .map(|symbol| ast::Ident::new(*symbol, span)).collect()
    };

    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,
        })),
    );

    krate.module.items.insert(0, use_item);

    (krate, Some(name))
}