]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/lib.rs
Rollup merge of #52769 - sinkuu:stray_test, r=alexcrichton
[rust.git] / src / libsyntax_ext / lib.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Syntax extensions in the Rust compiler.
12
13 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
14        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
15        html_root_url = "https://doc.rust-lang.org/nightly/")]
16
17 #![feature(proc_macro_internals)]
18 #![feature(decl_macro)]
19 #![feature(str_escape)]
20
21 #![feature(rustc_diagnostic_macros)]
22
23 extern crate fmt_macros;
24 #[macro_use]
25 extern crate syntax;
26 extern crate syntax_pos;
27 extern crate proc_macro;
28 extern crate rustc_data_structures;
29 extern crate rustc_errors as errors;
30 extern crate rustc_target;
31
32 mod diagnostics;
33
34 #[macro_use]
35 // for custom_derive
36 pub mod deriving;
37
38 mod asm;
39 mod assert;
40 mod cfg;
41 mod compile_error;
42 mod concat;
43 mod concat_idents;
44 mod env;
45 mod format;
46 mod format_foreign;
47 mod global_asm;
48 mod log_syntax;
49 mod trace_macros;
50
51 pub mod proc_macro_registrar;
52
53
54 pub mod proc_macro_impl;
55
56 use rustc_data_structures::sync::Lrc;
57 use syntax::ast;
58 use syntax::ext::base::{MacroExpanderFn, NormalTT, NamedSyntaxExtension};
59 use syntax::ext::hygiene;
60 use syntax::symbol::Symbol;
61
62 pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
63                          user_exts: Vec<NamedSyntaxExtension>,
64                          enable_quotes: bool) {
65     deriving::register_builtin_derives(resolver);
66
67     let mut register = |name, ext| {
68         resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Lrc::new(ext));
69     };
70
71     macro_rules! register {
72         ($( $name:ident: $f:expr, )*) => { $(
73             register(Symbol::intern(stringify!($name)),
74                      NormalTT {
75                         expander: Box::new($f as MacroExpanderFn),
76                         def_info: None,
77                         allow_internal_unstable: false,
78                         allow_internal_unsafe: false,
79                         local_inner_macros: false,
80                         unstable_feature: None,
81                         edition: hygiene::default_edition(),
82                     });
83         )* }
84     }
85
86     if enable_quotes {
87         use syntax::ext::quote::*;
88         register! {
89             quote_tokens: expand_quote_tokens,
90             quote_expr: expand_quote_expr,
91             quote_ty: expand_quote_ty,
92             quote_item: expand_quote_item,
93             quote_pat: expand_quote_pat,
94             quote_arm: expand_quote_arm,
95             quote_stmt: expand_quote_stmt,
96             quote_attr: expand_quote_attr,
97             quote_arg: expand_quote_arg,
98             quote_block: expand_quote_block,
99             quote_meta_item: expand_quote_meta_item,
100             quote_path: expand_quote_path,
101         }
102     }
103
104     use syntax::ext::source_util::*;
105     register! {
106         line: expand_line,
107         __rust_unstable_column: expand_column_gated,
108         column: expand_column,
109         file: expand_file,
110         stringify: expand_stringify,
111         include: expand_include,
112         include_str: expand_include_str,
113         include_bytes: expand_include_bytes,
114         module_path: expand_mod,
115
116         asm: asm::expand_asm,
117         global_asm: global_asm::expand_global_asm,
118         cfg: cfg::expand_cfg,
119         concat: concat::expand_syntax_ext,
120         concat_idents: concat_idents::expand_syntax_ext,
121         env: env::expand_env,
122         option_env: env::expand_option_env,
123         log_syntax: log_syntax::expand_syntax_ext,
124         trace_macros: trace_macros::expand_trace_macros,
125         compile_error: compile_error::expand_compile_error,
126         assert: assert::expand_assert,
127     }
128
129     // format_args uses `unstable` things internally.
130     register(Symbol::intern("format_args"),
131              NormalTT {
132                 expander: Box::new(format::expand_format_args),
133                 def_info: None,
134                 allow_internal_unstable: true,
135                 allow_internal_unsafe: false,
136                 local_inner_macros: false,
137                 unstable_feature: None,
138                 edition: hygiene::default_edition(),
139             });
140     register(Symbol::intern("format_args_nl"),
141              NormalTT {
142                  expander: Box::new(format::expand_format_args_nl),
143                  def_info: None,
144                  allow_internal_unstable: true,
145                  allow_internal_unsafe: false,
146                  local_inner_macros: false,
147                  unstable_feature: None,
148                  edition: hygiene::default_edition(),
149              });
150
151     for (name, ext) in user_exts {
152         register(name, ext);
153     }
154 }