]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/lib.rs
ee0b86963f31dcdc9f7da4b3a298b0b82ed6f8e1
[rust.git] / src / libsyntax_ext / lib.rs
1 //! Syntax extensions in the Rust compiler.
2
3 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
4
5 #![deny(rust_2018_idioms)]
6 #![cfg_attr(not(stage0), deny(internal))]
7
8 #![feature(in_band_lifetimes)]
9 #![feature(proc_macro_diagnostic)]
10 #![feature(proc_macro_internals)]
11 #![feature(proc_macro_span)]
12 #![feature(decl_macro)]
13 #![feature(nll)]
14 #![feature(rustc_diagnostic_macros)]
15
16 #![recursion_limit="256"]
17
18 extern crate proc_macro;
19
20 mod diagnostics;
21
22 mod asm;
23 mod assert;
24 mod cfg;
25 mod compile_error;
26 mod concat;
27 mod concat_idents;
28 mod env;
29 mod format;
30 mod format_foreign;
31 mod global_asm;
32 mod log_syntax;
33 mod proc_macro_server;
34 mod test;
35 mod test_case;
36 mod trace_macros;
37
38 pub mod deriving;
39 pub mod proc_macro_decls;
40 pub mod proc_macro_impl;
41
42 use rustc_data_structures::sync::Lrc;
43 use syntax::ast;
44 use syntax::ext::base::{MacroExpanderFn, NormalTT, NamedSyntaxExtension, MultiModifier};
45 use syntax::ext::hygiene;
46 use syntax::symbol::Symbol;
47
48 pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
49                          user_exts: Vec<NamedSyntaxExtension>) {
50     deriving::register_builtin_derives(resolver);
51
52     let mut register = |name, ext| {
53         resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Lrc::new(ext));
54     };
55
56     macro_rules! register {
57         ($( $name:ident: $f:expr, )*) => { $(
58             register(Symbol::intern(stringify!($name)),
59                      NormalTT {
60                         expander: Box::new($f as MacroExpanderFn),
61                         def_info: None,
62                         allow_internal_unstable: None,
63                         allow_internal_unsafe: false,
64                         local_inner_macros: false,
65                         unstable_feature: None,
66                         edition: hygiene::default_edition(),
67                     });
68         )* }
69     }
70
71     use syntax::ext::source_util::*;
72     register! {
73         line: expand_line,
74         __rust_unstable_column: expand_column_gated,
75         column: expand_column,
76         file: expand_file,
77         stringify: expand_stringify,
78         include: expand_include,
79         include_str: expand_include_str,
80         include_bytes: expand_include_bytes,
81         module_path: expand_mod,
82
83         asm: asm::expand_asm,
84         global_asm: global_asm::expand_global_asm,
85         cfg: cfg::expand_cfg,
86         concat: concat::expand_syntax_ext,
87         concat_idents: concat_idents::expand_syntax_ext,
88         env: env::expand_env,
89         option_env: env::expand_option_env,
90         log_syntax: log_syntax::expand_syntax_ext,
91         trace_macros: trace_macros::expand_trace_macros,
92         compile_error: compile_error::expand_compile_error,
93         assert: assert::expand_assert,
94     }
95
96     register(Symbol::intern("test_case"), MultiModifier(Box::new(test_case::expand)));
97     register(Symbol::intern("test"), MultiModifier(Box::new(test::expand_test)));
98     register(Symbol::intern("bench"), MultiModifier(Box::new(test::expand_bench)));
99
100     // format_args uses `unstable` things internally.
101     register(Symbol::intern("format_args"),
102              NormalTT {
103                 expander: Box::new(format::expand_format_args),
104                 def_info: None,
105                 allow_internal_unstable: Some(vec![
106                     Symbol::intern("fmt_internals"),
107                 ].into()),
108                 allow_internal_unsafe: false,
109                 local_inner_macros: false,
110                 unstable_feature: None,
111                 edition: hygiene::default_edition(),
112             });
113     register(Symbol::intern("format_args_nl"),
114              NormalTT {
115                  expander: Box::new(format::expand_format_args_nl),
116                  def_info: None,
117                  allow_internal_unstable: Some(vec![
118                      Symbol::intern("fmt_internals"),
119                  ].into()),
120                  allow_internal_unsafe: false,
121                  local_inner_macros: false,
122                  unstable_feature: None,
123                  edition: hygiene::default_edition(),
124              });
125
126     for (name, ext) in user_exts {
127         register(name, ext);
128     }
129 }