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