]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/lib.rs
Auto merge of #42588 - ishitatsuyuki:patch-1, r=petrochenkov
[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 #![deny(warnings)]
17
18 #![feature(proc_macro_internals)]
19
20 extern crate fmt_macros;
21 #[macro_use]
22 extern crate syntax;
23 extern crate syntax_pos;
24 extern crate proc_macro;
25 extern crate rustc_errors as errors;
26
27 mod asm;
28 mod cfg;
29 mod compile_error;
30 mod concat;
31 mod concat_idents;
32 mod env;
33 mod format;
34 mod format_foreign;
35 mod global_asm;
36 mod log_syntax;
37 mod trace_macros;
38
39 pub mod proc_macro_registrar;
40
41 // for custom_derive
42 pub mod deriving;
43
44 pub mod proc_macro_impl;
45
46 use std::rc::Rc;
47 use syntax::ast;
48 use syntax::ext::base::{MacroExpanderFn, NormalTT, NamedSyntaxExtension};
49 use syntax::symbol::Symbol;
50
51 pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver,
52                          user_exts: Vec<NamedSyntaxExtension>,
53                          enable_quotes: bool) {
54     deriving::register_builtin_derives(resolver);
55
56     let mut register = |name, ext| {
57         resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Rc::new(ext));
58     };
59
60     macro_rules! register {
61         ($( $name:ident: $f:expr, )*) => { $(
62             register(Symbol::intern(stringify!($name)),
63                      NormalTT {
64                         expander: Box::new($f as MacroExpanderFn),
65                         def_info: None,
66                         allow_internal_unstable: false,
67                         allow_internal_unsafe: false,
68                     });
69         )* }
70     }
71
72     if enable_quotes {
73         use syntax::ext::quote::*;
74         register! {
75             quote_tokens: expand_quote_tokens,
76             quote_expr: expand_quote_expr,
77             quote_ty: expand_quote_ty,
78             quote_item: expand_quote_item,
79             quote_pat: expand_quote_pat,
80             quote_arm: expand_quote_arm,
81             quote_stmt: expand_quote_stmt,
82             quote_attr: expand_quote_attr,
83             quote_arg: expand_quote_arg,
84             quote_block: expand_quote_block,
85             quote_meta_item: expand_quote_meta_item,
86             quote_path: expand_quote_path,
87         }
88     }
89
90     use syntax::ext::source_util::*;
91     register! {
92         line: expand_line,
93         __rust_unstable_column: expand_column_gated,
94         column: expand_column,
95         file: expand_file,
96         stringify: expand_stringify,
97         include: expand_include,
98         include_str: expand_include_str,
99         include_bytes: expand_include_bytes,
100         module_path: expand_mod,
101
102         asm: asm::expand_asm,
103         global_asm: global_asm::expand_global_asm,
104         cfg: cfg::expand_cfg,
105         concat: concat::expand_syntax_ext,
106         concat_idents: concat_idents::expand_syntax_ext,
107         env: env::expand_env,
108         option_env: env::expand_option_env,
109         log_syntax: log_syntax::expand_syntax_ext,
110         trace_macros: trace_macros::expand_trace_macros,
111         compile_error: compile_error::expand_compile_error,
112     }
113
114     // format_args uses `unstable` things internally.
115     register(Symbol::intern("format_args"),
116              NormalTT {
117                 expander: Box::new(format::expand_format_args),
118                 def_info: None,
119                 allow_internal_unstable: true,
120                 allow_internal_unsafe: false,
121             });
122
123     for (name, ext) in user_exts {
124         register(name, ext);
125     }
126 }