]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/lib.rs
Auto merge of #102283 - GuillaumeGomez:option-code-example-unwrap-or-default, r=thomcc
[rust.git] / compiler / rustc_builtin_macros / src / 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 #![allow(rustc::potential_query_instability)]
5 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
6 #![feature(array_windows)]
7 #![feature(box_patterns)]
8 #![feature(decl_macro)]
9 #![feature(if_let_guard)]
10 #![feature(is_sorted)]
11 #![feature(let_chains)]
12 #![feature(proc_macro_internals)]
13 #![feature(proc_macro_quote)]
14 #![recursion_limit = "256"]
15
16 extern crate proc_macro;
17
18 #[macro_use]
19 extern crate tracing;
20
21 use crate::deriving::*;
22
23 use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
24 use rustc_expand::proc_macro::BangProcMacro;
25 use rustc_span::symbol::sym;
26
27 mod assert;
28 mod cfg;
29 mod cfg_accessible;
30 mod cfg_eval;
31 mod compile_error;
32 mod concat;
33 mod concat_bytes;
34 mod concat_idents;
35 mod derive;
36 mod deriving;
37 mod edition_panic;
38 mod env;
39 mod format;
40 mod format_foreign;
41 mod global_allocator;
42 mod log_syntax;
43 mod source_util;
44 mod test;
45 mod trace_macros;
46 mod util;
47
48 pub mod asm;
49 pub mod cmdline_attrs;
50 pub mod proc_macro_harness;
51 pub mod standard_library_imports;
52 pub mod test_harness;
53
54 pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
55     let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
56     macro register_bang($($name:ident: $f:expr,)*) {
57         $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)));)*
58     }
59     macro register_attr($($name:ident: $f:expr,)*) {
60         $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Box::new($f)));)*
61     }
62     macro register_derive($($name:ident: $f:expr,)*) {
63         $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Box::new(BuiltinDerive($f))));)*
64     }
65
66     register_bang! {
67         asm: asm::expand_asm,
68         assert: assert::expand_assert,
69         cfg: cfg::expand_cfg,
70         column: source_util::expand_column,
71         compile_error: compile_error::expand_compile_error,
72         concat_bytes: concat_bytes::expand_concat_bytes,
73         concat_idents: concat_idents::expand_concat_idents,
74         concat: concat::expand_concat,
75         env: env::expand_env,
76         file: source_util::expand_file,
77         format_args_nl: format::expand_format_args_nl,
78         format_args: format::expand_format_args,
79         const_format_args: format::expand_format_args,
80         global_asm: asm::expand_global_asm,
81         include_bytes: source_util::expand_include_bytes,
82         include_str: source_util::expand_include_str,
83         include: source_util::expand_include,
84         line: source_util::expand_line,
85         log_syntax: log_syntax::expand_log_syntax,
86         module_path: source_util::expand_mod,
87         option_env: env::expand_option_env,
88         core_panic: edition_panic::expand_panic,
89         std_panic: edition_panic::expand_panic,
90         unreachable: edition_panic::expand_unreachable,
91         stringify: source_util::expand_stringify,
92         trace_macros: trace_macros::expand_trace_macros,
93     }
94
95     register_attr! {
96         bench: test::expand_bench,
97         cfg_accessible: cfg_accessible::Expander,
98         cfg_eval: cfg_eval::expand,
99         derive: derive::Expander,
100         global_allocator: global_allocator::expand,
101         test: test::expand_test,
102         test_case: test::expand_test_case,
103     }
104
105     register_derive! {
106         Clone: clone::expand_deriving_clone,
107         Copy: bounds::expand_deriving_copy,
108         Debug: debug::expand_deriving_debug,
109         Default: default::expand_deriving_default,
110         Eq: eq::expand_deriving_eq,
111         Hash: hash::expand_deriving_hash,
112         Ord: ord::expand_deriving_ord,
113         PartialEq: partial_eq::expand_deriving_partial_eq,
114         PartialOrd: partial_ord::expand_deriving_partial_ord,
115         RustcDecodable: decodable::expand_deriving_rustc_decodable,
116         RustcEncodable: encodable::expand_deriving_rustc_encodable,
117     }
118
119     let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
120     register(sym::quote, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })));
121 }