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