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