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.
|
|
|
|
|
|
|
|
#![crate_name = "syntax_ext"]
|
2016-01-21 17:26:19 -06:00
|
|
|
#![unstable(feature = "rustc_private", issue = "27812")]
|
2015-12-10 08:23:14 -06:00
|
|
|
#![crate_type = "dylib"]
|
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![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/")]
|
2016-01-21 17:26:19 -06:00
|
|
|
#![cfg_attr(not(stage0), deny(warnings))]
|
2015-12-15 01:23:18 -06:00
|
|
|
|
2016-08-26 11:23:42 -05:00
|
|
|
#![feature(dotdot_in_tuple_patterns)]
|
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
|
|
|
#![feature(rustc_macro_lib)]
|
|
|
|
#![feature(rustc_macro_internals)]
|
2015-12-10 08:23:14 -06:00
|
|
|
#![feature(rustc_private)]
|
2015-12-15 01:23:18 -06:00
|
|
|
#![feature(staged_api)]
|
2015-12-10 08:23:14 -06:00
|
|
|
|
|
|
|
extern crate fmt_macros;
|
2016-06-06 09:52:48 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
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;
|
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
|
|
|
extern crate rustc_macro;
|
2016-06-21 17:08:13 -05:00
|
|
|
extern crate rustc_errors as errors;
|
2015-12-10 08:23:14 -06:00
|
|
|
|
|
|
|
mod asm;
|
|
|
|
mod cfg;
|
|
|
|
mod concat;
|
|
|
|
mod concat_idents;
|
|
|
|
mod env;
|
|
|
|
mod format;
|
|
|
|
mod log_syntax;
|
|
|
|
mod trace_macros;
|
|
|
|
|
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
|
|
|
pub mod rustc_macro_registrar;
|
|
|
|
|
2015-12-11 01:40:25 -06:00
|
|
|
// for custom_derive
|
|
|
|
pub mod deriving;
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
use syntax::ast;
|
2016-09-13 01:25:02 -05:00
|
|
|
use syntax::ext::base::{MacroExpanderFn, NormalTT, IdentTT, MultiModifier};
|
|
|
|
use syntax::ext::tt::macro_rules::MacroRulesExpander;
|
2016-09-07 18:21:59 -05:00
|
|
|
use syntax::parse::token::intern;
|
|
|
|
|
|
|
|
pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, enable_quotes: bool) {
|
|
|
|
let mut register = |name, ext| {
|
2016-10-02 00:49:56 -05:00
|
|
|
resolver.add_ext(ast::Ident::with_empty_ctxt(intern(name)), Rc::new(ext));
|
2016-09-07 18:21:59 -05:00
|
|
|
};
|
|
|
|
|
2016-09-13 01:25:02 -05:00
|
|
|
register("macro_rules", IdentTT(Box::new(MacroRulesExpander), None, false));
|
2016-09-07 18:21:59 -05:00
|
|
|
|
|
|
|
macro_rules! register {
|
|
|
|
($( $name:ident: $f:expr, )*) => { $(
|
|
|
|
register(stringify!($name), NormalTT(Box::new($f as MacroExpanderFn), None, false));
|
|
|
|
)* }
|
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_matcher: expand_quote_matcher,
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
// format_args uses `unstable` things internally.
|
|
|
|
register("format_args", NormalTT(Box::new(format::expand_format_args), None, true));
|
|
|
|
|
|
|
|
register("derive", MultiModifier(Box::new(deriving::expand_derive)));
|
2015-12-10 08:23:14 -06:00
|
|
|
}
|