]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/lib.rs
Rollup merge of #74294 - msirringhaus:master, r=pietroalbini
[rust.git] / src / librustc_builtin_macros / lib.rs
1 //! This crate contains implementations of built-in macros and other code generating facilities
2 //! injecting code into the crate before it is lowered to HIR.
3
4 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
5 #![feature(bool_to_option)]
6 #![feature(crate_visibility_modifier)]
7 #![feature(decl_macro)]
8 #![feature(inner_deref)]
9 #![feature(nll)]
10 #![feature(or_patterns)]
11 #![feature(proc_macro_internals)]
12 #![feature(proc_macro_quote)]
13
14 extern crate proc_macro;
15
16 use crate::deriving::*;
17
18 use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtension, SyntaxExtensionKind};
19 use rustc_expand::proc_macro::BangProcMacro;
20 use rustc_span::edition::Edition;
21 use rustc_span::symbol::{sym, Ident};
22
23 mod asm;
24 mod assert;
25 mod cfg;
26 mod cfg_accessible;
27 mod compile_error;
28 mod concat;
29 mod concat_idents;
30 mod deriving;
31 mod env;
32 mod format;
33 mod format_foreign;
34 mod global_allocator;
35 mod global_asm;
36 mod llvm_asm;
37 mod log_syntax;
38 mod source_util;
39 mod test;
40 mod trace_macros;
41 mod util;
42
43 pub mod cmdline_attrs;
44 pub mod proc_macro_harness;
45 pub mod standard_library_imports;
46 pub mod test_harness;
47
48 pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand, edition: Edition) {
49     let mut register = |name, kind| {
50         resolver.register_builtin_macro(
51             Ident::with_dummy_span(name),
52             SyntaxExtension { is_builtin: true, ..SyntaxExtension::default(kind, edition) },
53         )
54     };
55     macro register_bang($($name:ident: $f:expr,)*) {
56         $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)));)*
57     }
58     macro register_attr($($name:ident: $f:expr,)*) {
59         $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Box::new($f)));)*
60     }
61     macro register_derive($($name:ident: $f:expr,)*) {
62         $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Box::new(BuiltinDerive($f))));)*
63     }
64
65     register_bang! {
66         asm: asm::expand_asm,
67         assert: assert::expand_assert,
68         cfg: cfg::expand_cfg,
69         column: source_util::expand_column,
70         compile_error: compile_error::expand_compile_error,
71         concat_idents: concat_idents::expand_concat_idents,
72         concat: concat::expand_concat,
73         env: env::expand_env,
74         file: source_util::expand_file,
75         format_args_nl: format::expand_format_args_nl,
76         format_args: format::expand_format_args,
77         global_asm: global_asm::expand_global_asm,
78         include_bytes: source_util::expand_include_bytes,
79         include_str: source_util::expand_include_str,
80         include: source_util::expand_include,
81         line: source_util::expand_line,
82         llvm_asm: llvm_asm::expand_llvm_asm,
83         log_syntax: log_syntax::expand_log_syntax,
84         module_path: source_util::expand_mod,
85         option_env: env::expand_option_env,
86         stringify: source_util::expand_stringify,
87         trace_macros: trace_macros::expand_trace_macros,
88     }
89
90     register_attr! {
91         bench: test::expand_bench,
92         cfg_accessible: cfg_accessible::Expander,
93         global_allocator: global_allocator::expand,
94         test: test::expand_test,
95         test_case: test::expand_test_case,
96     }
97
98     register_derive! {
99         Clone: clone::expand_deriving_clone,
100         Copy: bounds::expand_deriving_copy,
101         Debug: debug::expand_deriving_debug,
102         Default: default::expand_deriving_default,
103         Eq: eq::expand_deriving_eq,
104         Hash: hash::expand_deriving_hash,
105         Ord: ord::expand_deriving_ord,
106         PartialEq: partial_eq::expand_deriving_partial_eq,
107         PartialOrd: partial_ord::expand_deriving_partial_ord,
108         RustcDecodable: decodable::expand_deriving_rustc_decodable,
109         RustcEncodable: encodable::expand_deriving_rustc_encodable,
110     }
111
112     let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
113     register(sym::quote, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })));
114 }