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