2015-12-10 08:23:14 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Syntax extensions in the Rust compiler.
|
|
|
|
|
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2015-12-15 01:23:18 -06:00
|
|
|
|
2016-10-03 11:49:39 -05:00
|
|
|
#![feature(proc_macro_internals)]
|
2017-12-06 12:50:55 -06:00
|
|
|
#![feature(decl_macro)]
|
2018-08-09 14:28:39 -05:00
|
|
|
#![cfg_attr(not(stage0), feature(nll))]
|
2018-03-14 04:11:42 -05:00
|
|
|
#![feature(str_escape)]
|
2017-05-08 16:36:44 -05:00
|
|
|
|
2018-05-10 13:02:19 -05:00
|
|
|
#![feature(rustc_diagnostic_macros)]
|
2018-02-12 16:21:20 -06:00
|
|
|
|
2015-12-10 08:23:14 -06:00
|
|
|
extern crate fmt_macros;
|
2015-12-20 15:00:43 -06:00
|
|
|
#[macro_use]
|
2015-12-10 08:23:14 -06:00
|
|
|
extern crate syntax;
|
2016-06-21 17:08:13 -05:00
|
|
|
extern crate syntax_pos;
|
2016-10-03 11:49:39 -05:00
|
|
|
extern crate proc_macro;
|
2018-02-27 10:11:14 -06:00
|
|
|
extern crate rustc_data_structures;
|
2016-06-21 17:08:13 -05:00
|
|
|
extern crate rustc_errors as errors;
|
2018-04-25 11:30:39 -05:00
|
|
|
extern crate rustc_target;
|
2015-12-10 08:23:14 -06:00
|
|
|
|
2018-02-12 16:21:20 -06:00
|
|
|
mod diagnostics;
|
|
|
|
|
2018-05-17 14:17:53 -05:00
|
|
|
#[macro_use]
|
|
|
|
// for custom_derive
|
|
|
|
pub mod deriving;
|
|
|
|
|
2015-12-10 08:23:14 -06:00
|
|
|
mod asm;
|
2018-05-17 14:17:53 -05:00
|
|
|
mod assert;
|
2015-12-10 08:23:14 -06:00
|
|
|
mod cfg;
|
2017-05-06 22:26:45 -05:00
|
|
|
mod compile_error;
|
2015-12-10 08:23:14 -06:00
|
|
|
mod concat;
|
|
|
|
mod concat_idents;
|
|
|
|
mod env;
|
|
|
|
mod format;
|
2016-11-10 22:23:15 -06:00
|
|
|
mod format_foreign;
|
2017-03-15 21:27:40 -05:00
|
|
|
mod global_asm;
|
2015-12-10 08:23:14 -06:00
|
|
|
mod log_syntax;
|
|
|
|
mod trace_macros;
|
|
|
|
|
2016-10-03 11:49:39 -05:00
|
|
|
pub mod proc_macro_registrar;
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 19:07:11 -05:00
|
|
|
|
2015-12-11 01:40:25 -06:00
|
|
|
|
2017-01-09 03:31:14 -06:00
|
|
|
pub mod proc_macro_impl;
|
|
|
|
|
2018-02-27 10:11:14 -06:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2016-09-07 18:21:59 -05:00
|
|
|
use syntax::ast;
|
2017-02-01 04:33:09 -06:00
|
|
|
use syntax::ext::base::{MacroExpanderFn, NormalTT, NamedSyntaxExtension};
|
2018-05-12 19:51:46 -05:00
|
|
|
use syntax::ext::hygiene;
|
2016-11-16 02:21:52 -06:00
|
|
|
use syntax::symbol::Symbol;
|
2016-09-07 18:21:59 -05:00
|
|
|
|
2018-07-12 04:58:16 -05:00
|
|
|
pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
|
2016-09-28 17:48:55 -05:00
|
|
|
user_exts: Vec<NamedSyntaxExtension>,
|
|
|
|
enable_quotes: bool) {
|
2017-01-23 09:01:49 -06:00
|
|
|
deriving::register_builtin_derives(resolver);
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
let mut register = |name, ext| {
|
2018-02-27 10:11:14 -06:00
|
|
|
resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Lrc::new(ext));
|
2016-09-07 18:21:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
macro_rules! register {
|
|
|
|
($( $name:ident: $f:expr, )*) => { $(
|
2016-11-16 02:21:52 -06:00
|
|
|
register(Symbol::intern(stringify!($name)),
|
2017-08-08 10:21:20 -05:00
|
|
|
NormalTT {
|
|
|
|
expander: Box::new($f as MacroExpanderFn),
|
|
|
|
def_info: None,
|
|
|
|
allow_internal_unstable: false,
|
|
|
|
allow_internal_unsafe: false,
|
2018-06-11 06:21:36 -05:00
|
|
|
local_inner_macros: false,
|
2018-02-24 21:11:06 -06:00
|
|
|
unstable_feature: None,
|
2018-05-12 19:51:46 -05:00
|
|
|
edition: hygiene::default_edition(),
|
2017-08-08 10:21:20 -05:00
|
|
|
});
|
2016-09-07 18:21:59 -05:00
|
|
|
)* }
|
2015-12-10 08:23:14 -06:00
|
|
|
}
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
if enable_quotes {
|
|
|
|
use syntax::ext::quote::*;
|
|
|
|
register! {
|
|
|
|
quote_tokens: expand_quote_tokens,
|
|
|
|
quote_expr: expand_quote_expr,
|
|
|
|
quote_ty: expand_quote_ty,
|
|
|
|
quote_item: expand_quote_item,
|
|
|
|
quote_pat: expand_quote_pat,
|
|
|
|
quote_arm: expand_quote_arm,
|
|
|
|
quote_stmt: expand_quote_stmt,
|
|
|
|
quote_attr: expand_quote_attr,
|
|
|
|
quote_arg: expand_quote_arg,
|
|
|
|
quote_block: expand_quote_block,
|
|
|
|
quote_meta_item: expand_quote_meta_item,
|
|
|
|
quote_path: expand_quote_path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use syntax::ext::source_util::*;
|
|
|
|
register! {
|
|
|
|
line: expand_line,
|
2017-08-09 18:42:36 -05:00
|
|
|
__rust_unstable_column: expand_column_gated,
|
2016-09-07 18:21:59 -05:00
|
|
|
column: expand_column,
|
|
|
|
file: expand_file,
|
|
|
|
stringify: expand_stringify,
|
|
|
|
include: expand_include,
|
|
|
|
include_str: expand_include_str,
|
|
|
|
include_bytes: expand_include_bytes,
|
|
|
|
module_path: expand_mod,
|
|
|
|
|
|
|
|
asm: asm::expand_asm,
|
2017-03-15 21:27:40 -05:00
|
|
|
global_asm: global_asm::expand_global_asm,
|
2016-09-07 18:21:59 -05:00
|
|
|
cfg: cfg::expand_cfg,
|
|
|
|
concat: concat::expand_syntax_ext,
|
|
|
|
concat_idents: concat_idents::expand_syntax_ext,
|
|
|
|
env: env::expand_env,
|
|
|
|
option_env: env::expand_option_env,
|
|
|
|
log_syntax: log_syntax::expand_syntax_ext,
|
|
|
|
trace_macros: trace_macros::expand_trace_macros,
|
2017-05-06 22:26:45 -05:00
|
|
|
compile_error: compile_error::expand_compile_error,
|
2018-03-07 01:13:15 -06:00
|
|
|
assert: assert::expand_assert,
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// format_args uses `unstable` things internally.
|
2016-11-16 02:21:52 -06:00
|
|
|
register(Symbol::intern("format_args"),
|
2017-08-08 10:21:20 -05:00
|
|
|
NormalTT {
|
|
|
|
expander: Box::new(format::expand_format_args),
|
|
|
|
def_info: None,
|
|
|
|
allow_internal_unstable: true,
|
|
|
|
allow_internal_unsafe: false,
|
2018-06-11 06:21:36 -05:00
|
|
|
local_inner_macros: false,
|
2018-05-12 19:51:46 -05:00
|
|
|
unstable_feature: None,
|
|
|
|
edition: hygiene::default_edition(),
|
2017-08-08 10:21:20 -05:00
|
|
|
});
|
2018-07-19 20:53:26 -05:00
|
|
|
register(Symbol::intern("format_args_nl"),
|
|
|
|
NormalTT {
|
|
|
|
expander: Box::new(format::expand_format_args_nl),
|
|
|
|
def_info: None,
|
|
|
|
allow_internal_unstable: true,
|
|
|
|
allow_internal_unsafe: false,
|
|
|
|
local_inner_macros: false,
|
|
|
|
unstable_feature: None,
|
|
|
|
edition: hygiene::default_edition(),
|
|
|
|
});
|
2016-09-07 18:21:59 -05:00
|
|
|
|
2016-09-28 17:48:55 -05:00
|
|
|
for (name, ext) in user_exts {
|
|
|
|
register(name, ext);
|
|
|
|
}
|
2015-12-10 08:23:14 -06:00
|
|
|
}
|