]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/standard_library_imports.rs
fix links
[rust.git] / src / libsyntax_ext / standard_library_imports.rs
1 use syntax::{ast, attr};
2 use syntax::edition::Edition;
3 use syntax::ext::hygiene::{ExpnId, MacroKind};
4 use syntax::ptr::P;
5 use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
6 use syntax::symbol::{Ident, Symbol, kw, sym};
7 use syntax::tokenstream::TokenStream;
8 use syntax_pos::DUMMY_SP;
9
10 use std::iter;
11
12 pub fn inject(
13     mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition
14 ) -> (ast::Crate, Option<Symbol>) {
15     let rust_2018 = edition >= Edition::Edition2018;
16
17     // the first name in this list is the crate name of the crate with the prelude
18     let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) {
19         return (krate, None);
20     } else if attr::contains_name(&krate.attrs, sym::no_std) {
21         if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
22             &["core"]
23         } else {
24             &["core", "compiler_builtins"]
25         }
26     } else {
27         &["std"]
28     };
29
30     // .rev() to preserve ordering above in combination with insert(0, ...)
31     let alt_std_name = alt_std_name.map(Symbol::intern);
32     for orig_name_str in names.iter().rev() {
33         // HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
34         // so they don't accidentally interfere with the new import paths.
35         let orig_name_sym = Symbol::intern(orig_name_str);
36         let orig_name_ident = Ident::with_empty_ctxt(orig_name_sym);
37         let (rename, orig_name) = if rust_2018 {
38             (orig_name_ident.gensym(), Some(orig_name_sym))
39         } else {
40             (orig_name_ident, None)
41         };
42         krate.module.items.insert(0, P(ast::Item {
43             attrs: vec![attr::mk_attr_outer(
44                 DUMMY_SP,
45                 attr::mk_attr_id(),
46                 attr::mk_word_item(ast::Ident::with_empty_ctxt(sym::macro_use))
47             )],
48             vis: dummy_spanned(ast::VisibilityKind::Inherited),
49             node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
50             ident: rename,
51             id: ast::DUMMY_NODE_ID,
52             span: DUMMY_SP,
53             tokens: None,
54         }));
55     }
56
57     // the crates have been injected, the assumption is that the first one is the one with
58     // the prelude.
59     let name = names[0];
60
61     let span = DUMMY_SP.fresh_expansion(ExpnId::root(), ExpnInfo::allow_unstable(
62         ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
63         [sym::prelude_import][..].into(),
64     ));
65
66     krate.module.items.insert(0, P(ast::Item {
67         attrs: vec![ast::Attribute {
68             style: ast::AttrStyle::Outer,
69             path: ast::Path::from_ident(ast::Ident::new(sym::prelude_import, span)),
70             tokens: TokenStream::empty(),
71             id: attr::mk_attr_id(),
72             is_sugared_doc: false,
73             span,
74         }],
75         vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
76         node: ast::ItemKind::Use(P(ast::UseTree {
77             prefix: ast::Path {
78                 segments: iter::once(ast::Ident::with_empty_ctxt(kw::PathRoot))
79                     .chain(
80                         [name, "prelude", "v1"].iter().cloned()
81                             .map(ast::Ident::from_str)
82                     ).map(ast::PathSegment::from_ident).collect(),
83                 span,
84             },
85             kind: ast::UseTreeKind::Glob,
86             span,
87         })),
88         id: ast::DUMMY_NODE_ID,
89         ident: ast::Ident::invalid(),
90         span,
91         tokens: None,
92     }));
93
94     (krate, Some(Symbol::intern(name)))
95 }