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