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