]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/lib.rs
Rollup merge of #43819 - frewsxcv:frewsxcv-include, r=QuietMisdreavus
[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 {
68                         expander: Box::new($f as MacroExpanderFn),
69                         def_info: None,
70                         allow_internal_unstable: false,
71                         allow_internal_unsafe: false,
72                     });
73         )* }
74     }
75
76     if enable_quotes {
77         use syntax::ext::quote::*;
78         register! {
79             quote_tokens: expand_quote_tokens,
80             quote_expr: expand_quote_expr,
81             quote_ty: expand_quote_ty,
82             quote_item: expand_quote_item,
83             quote_pat: expand_quote_pat,
84             quote_arm: expand_quote_arm,
85             quote_stmt: expand_quote_stmt,
86             quote_attr: expand_quote_attr,
87             quote_arg: expand_quote_arg,
88             quote_block: expand_quote_block,
89             quote_meta_item: expand_quote_meta_item,
90             quote_path: expand_quote_path,
91         }
92     }
93
94     use syntax::ext::source_util::*;
95     register! {
96         line: expand_line,
97         __rust_unstable_column: expand_column_gated,
98         column: expand_column,
99         file: expand_file,
100         stringify: expand_stringify,
101         include: expand_include,
102         include_str: expand_include_str,
103         include_bytes: expand_include_bytes,
104         module_path: expand_mod,
105
106         asm: asm::expand_asm,
107         global_asm: global_asm::expand_global_asm,
108         cfg: cfg::expand_cfg,
109         concat: concat::expand_syntax_ext,
110         concat_idents: concat_idents::expand_syntax_ext,
111         env: env::expand_env,
112         option_env: env::expand_option_env,
113         log_syntax: log_syntax::expand_syntax_ext,
114         trace_macros: trace_macros::expand_trace_macros,
115         compile_error: compile_error::expand_compile_error,
116     }
117
118     // format_args uses `unstable` things internally.
119     register(Symbol::intern("format_args"),
120              NormalTT {
121                 expander: Box::new(format::expand_format_args),
122                 def_info: None,
123                 allow_internal_unstable: true,
124                 allow_internal_unsafe: false,
125             });
126
127     for (name, ext) in user_exts {
128         register(name, ext);
129     }
130 }