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