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(decl_macro)]
7#![feature(deref_patterns)]
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 offload;
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        offload_kernel: offload::expand_kernel,
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}