Skip to main content

rustc_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// tidy-alphabetical-start
5#![allow(internal_features)]
6#![cfg_attr(bootstrap, feature(assert_matches))]
7#![feature(box_patterns)]
8#![feature(decl_macro)]
9#![feature(if_let_guard)]
10#![feature(iter_order_by)]
11#![feature(proc_macro_internals)]
12#![feature(proc_macro_quote)]
13#![feature(try_blocks)]
14#![recursion_limit = "256"]
15// tidy-alphabetical-end
16
17use std::sync::Arc;
18
19use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
20use rustc_expand::proc_macro::BangProcMacro;
21use rustc_span::sym;
22
23use crate::deriving::*;
24
25mod alloc_error_handler;
26mod assert;
27mod autodiff;
28mod cfg;
29mod cfg_accessible;
30mod cfg_eval;
31mod cfg_select;
32mod compile_error;
33mod concat;
34mod concat_bytes;
35mod define_opaque;
36mod derive;
37mod deriving;
38mod edition_panic;
39mod eii;
40mod env;
41mod errors;
42mod format;
43mod format_foreign;
44mod global_allocator;
45mod iter;
46mod log_syntax;
47mod pattern_type;
48mod source_util;
49mod test;
50mod trace_macros;
51
52pub mod asm;
53pub mod cmdline_attrs;
54pub mod contracts;
55pub mod proc_macro_harness;
56pub mod standard_library_imports;
57pub mod test_harness;
58pub mod util;
59
60pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
61    let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
62    macro register_bang($($name:ident: $f:expr,)*) {
63        $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Arc::new($f as MacroExpanderFn)));)*
64    }
65    macro register_attr($($name:ident: $f:expr,)*) {
66        $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Arc::new($f)));)*
67    }
68    macro register_derive($($name:ident: $f:expr,)*) {
69        $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive($f))));)*
70    }
71
72    register(sym::unreachable,
    SyntaxExtensionKind::LegacyBang(Arc::new(edition_panic::expand_unreachable
                as MacroExpanderFn)));register_bang! {
73        // tidy-alphabetical-start
74        asm: asm::expand_asm,
75        assert: assert::expand_assert,
76        cfg: cfg::expand_cfg,
77        cfg_select: cfg_select::expand_cfg_select,
78        column: source_util::expand_column,
79        compile_error: compile_error::expand_compile_error,
80        concat: concat::expand_concat,
81        concat_bytes: concat_bytes::expand_concat_bytes,
82        const_format_args: format::expand_format_args,
83        core_panic: edition_panic::expand_panic,
84        env: env::expand_env,
85        file: source_util::expand_file,
86        format_args: format::expand_format_args,
87        format_args_nl: format::expand_format_args_nl,
88        global_asm: asm::expand_global_asm,
89        include: source_util::expand_include,
90        include_bytes: source_util::expand_include_bytes,
91        include_str: source_util::expand_include_str,
92        iter: iter::expand,
93        line: source_util::expand_line,
94        log_syntax: log_syntax::expand_log_syntax,
95        module_path: source_util::expand_mod,
96        naked_asm: asm::expand_naked_asm,
97        option_env: env::expand_option_env,
98        pattern_type: pattern_type::expand,
99        std_panic: edition_panic::expand_panic,
100        stringify: source_util::expand_stringify,
101        trace_macros: trace_macros::expand_trace_macros,
102        unreachable: edition_panic::expand_unreachable,
103        // tidy-alphabetical-end
104    }
105
106    register(sym::unsafe_eii,
    SyntaxExtensionKind::LegacyAttr(Arc::new(eii::unsafe_eii)));register_attr! {
107        // tidy-alphabetical-start
108        alloc_error_handler: alloc_error_handler::expand,
109        autodiff_forward: autodiff::expand_forward,
110        autodiff_reverse: autodiff::expand_reverse,
111        bench: test::expand_bench,
112        cfg_accessible: cfg_accessible::Expander,
113        cfg_eval: cfg_eval::expand,
114        define_opaque: define_opaque::expand,
115        derive: derive::Expander { is_const: false },
116        derive_const: derive::Expander { is_const: true },
117        eii: eii::eii,
118        eii_declaration: eii::eii_declaration,
119        eii_shared_macro: eii::eii_shared_macro,
120        global_allocator: global_allocator::expand,
121        test: test::expand_test,
122        test_case: test::expand_test_case,
123        unsafe_eii: eii::unsafe_eii,
124        // tidy-alphabetical-end
125    }
126
127    register(sym::From,
    SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive(from::expand_deriving_from))));register_derive! {
128        Clone: clone::expand_deriving_clone,
129        Copy: bounds::expand_deriving_copy,
130        ConstParamTy: bounds::expand_deriving_const_param_ty,
131        Debug: debug::expand_deriving_debug,
132        Default: default::expand_deriving_default,
133        Eq: eq::expand_deriving_eq,
134        Hash: hash::expand_deriving_hash,
135        Ord: ord::expand_deriving_ord,
136        PartialEq: partial_eq::expand_deriving_partial_eq,
137        PartialOrd: partial_ord::expand_deriving_partial_ord,
138        CoercePointee: coerce_pointee::expand_deriving_coerce_pointee,
139        From: from::expand_deriving_from,
140    }
141
142    let client = rustc_proc_macro::bridge::client::Client::expand1(rustc_proc_macro::quote);
143    register(sym::quote, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })));
144    let requires = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandRequires));
145    register(sym::contracts_requires, requires);
146    let ensures = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandEnsures));
147    register(sym::contracts_ensures, ensures);
148}