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 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
5 #![feature(box_patterns)]
6 #![feature(bool_to_option)]
7 #![feature(crate_visibility_modifier)]
8 #![feature(decl_macro)]
9 #![feature(iter_zip)]
10 #![feature(nll)]
11 #![feature(proc_macro_internals)]
12 #![feature(proc_macro_quote)]
13 #![recursion_limit = "256"]
14
15 extern crate proc_macro;
16
17 use crate::deriving::*;
18
19 use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
20 use rustc_expand::proc_macro::BangProcMacro;
21 use rustc_span::symbol::sym;
22
23 mod asm;
24 mod assert;
25 mod cfg;
26 mod cfg_accessible;
27 mod cfg_eval;
28 mod compile_error;
29 mod concat;
30 mod concat_idents;
31 mod derive;
32 mod deriving;
33 mod env;
34 mod format;
35 mod format_foreign;
36 mod global_allocator;
37 mod llvm_asm;
38 mod log_syntax;
39 mod panic;
40 mod source_util;
41 mod test;
42 mod trace_macros;
43 mod util;
44
45 pub mod cmdline_attrs;
46 pub mod proc_macro_harness;
47 pub mod standard_library_imports;
48 pub mod test_harness;
49
register_builtin_macros(resolver: &mut dyn ResolverExpand)50 pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
51 let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
52 macro register_bang($($name:ident: $f:expr,)*) {
53 $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)));)*
54 }
55 macro register_attr($($name:ident: $f:expr,)*) {
56 $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Box::new($f)));)*
57 }
58 macro register_derive($($name:ident: $f:expr,)*) {
59 $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Box::new(BuiltinDerive($f))));)*
60 }
61
62 register_bang! {
63 asm: asm::expand_asm,
64 assert: assert::expand_assert,
65 cfg: cfg::expand_cfg,
66 column: source_util::expand_column,
67 compile_error: compile_error::expand_compile_error,
68 concat_idents: concat_idents::expand_concat_idents,
69 concat: concat::expand_concat,
70 env: env::expand_env,
71 file: source_util::expand_file,
72 format_args_nl: format::expand_format_args_nl,
73 format_args: format::expand_format_args,
74 const_format_args: format::expand_format_args,
75 global_asm: asm::expand_global_asm,
76 include_bytes: source_util::expand_include_bytes,
77 include_str: source_util::expand_include_str,
78 include: source_util::expand_include,
79 line: source_util::expand_line,
80 llvm_asm: llvm_asm::expand_llvm_asm,
81 log_syntax: log_syntax::expand_log_syntax,
82 module_path: source_util::expand_mod,
83 option_env: env::expand_option_env,
84 core_panic: panic::expand_panic,
85 std_panic: panic::expand_panic,
86 stringify: source_util::expand_stringify,
87 trace_macros: trace_macros::expand_trace_macros,
88 }
89
90 register_attr! {
91 bench: test::expand_bench,
92 cfg_accessible: cfg_accessible::Expander,
93 cfg_eval: cfg_eval::expand,
94 derive: derive::Expander,
95 global_allocator: global_allocator::expand,
96 test: test::expand_test,
97 test_case: test::expand_test_case,
98 }
99
100 register_derive! {
101 Clone: clone::expand_deriving_clone,
102 Copy: bounds::expand_deriving_copy,
103 Debug: debug::expand_deriving_debug,
104 Default: default::expand_deriving_default,
105 Eq: eq::expand_deriving_eq,
106 Hash: hash::expand_deriving_hash,
107 Ord: ord::expand_deriving_ord,
108 PartialEq: partial_eq::expand_deriving_partial_eq,
109 PartialOrd: partial_ord::expand_deriving_partial_ord,
110 RustcDecodable: decodable::expand_deriving_rustc_decodable,
111 RustcEncodable: encodable::expand_deriving_rustc_encodable,
112 }
113
114 let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
115 register(sym::quote, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })));
116 }
117