2015-02-24 18:56:01 +00:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-08-07 08:22:28 +00:00
|
|
|
pub use self::SyntaxExtension::*;
|
2014-11-06 08:05:53 +00:00
|
|
|
|
2017-01-23 15:01:49 +00:00
|
|
|
use ast::{self, Attribute, Name, PatKind, MetaItem};
|
2016-06-12 12:45:16 +00:00
|
|
|
use attr::HasAttrs;
|
2017-03-17 04:04:41 +00:00
|
|
|
use codemap::{self, CodeMap, Spanned, respan};
|
2018-01-16 05:38:12 +00:00
|
|
|
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
|
2018-02-12 22:21:20 +00:00
|
|
|
use errors::{DiagnosticBuilder, DiagnosticId};
|
2017-03-01 08:44:05 +00:00
|
|
|
use ext::expand::{self, Expansion, Invocation};
|
2017-03-17 04:04:41 +00:00
|
|
|
use ext::hygiene::{Mark, SyntaxContext};
|
2016-09-21 06:25:09 +00:00
|
|
|
use fold::{self, Folder};
|
2016-11-05 04:16:26 +00:00
|
|
|
use parse::{self, parser, DirectoryOwnership};
|
2013-03-26 20:38:07 +00:00
|
|
|
use parse::token;
|
2014-09-13 16:06:01 +00:00
|
|
|
use ptr::P;
|
2017-12-06 18:50:55 +00:00
|
|
|
use symbol::{keywords, Ident, Symbol};
|
2013-11-25 07:08:53 +00:00
|
|
|
use util::small_vector::SmallVector;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2017-05-06 04:49:59 +00:00
|
|
|
use std::collections::HashMap;
|
2017-12-06 18:50:55 +00:00
|
|
|
use std::iter;
|
2016-08-31 09:02:45 +00:00
|
|
|
use std::path::PathBuf;
|
2014-07-19 19:34:24 +00:00
|
|
|
use std::rc::Rc;
|
2018-03-03 05:24:49 +00:00
|
|
|
use rustc_data_structures::sync::{self, Lrc};
|
2016-08-29 04:16:43 +00:00
|
|
|
use std::default::Default;
|
|
|
|
use tokenstream::{self, TokenStream};
|
2011-06-04 19:41:45 +00:00
|
|
|
|
2014-02-28 07:49:25 +00:00
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Debug,Clone)]
|
2014-12-02 18:07:41 +00:00
|
|
|
pub enum Annotatable {
|
|
|
|
Item(P<ast::Item>),
|
2015-03-10 10:28:44 +00:00
|
|
|
TraitItem(P<ast::TraitItem>),
|
|
|
|
ImplItem(P<ast::ImplItem>),
|
2018-03-11 02:16:26 +00:00
|
|
|
ForeignItem(P<ast::ForeignItem>),
|
2018-03-16 06:20:56 +00:00
|
|
|
Stmt(P<ast::Stmt>),
|
|
|
|
Expr(P<ast::Expr>),
|
2014-12-02 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 12:45:16 +00:00
|
|
|
impl HasAttrs for Annotatable {
|
2016-09-07 23:21:59 +00:00
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2014-12-02 18:07:41 +00:00
|
|
|
match *self {
|
2016-06-12 12:45:16 +00:00
|
|
|
Annotatable::Item(ref item) => &item.attrs,
|
|
|
|
Annotatable::TraitItem(ref trait_item) => &trait_item.attrs,
|
|
|
|
Annotatable::ImplItem(ref impl_item) => &impl_item.attrs,
|
2018-03-11 02:16:26 +00:00
|
|
|
Annotatable::ForeignItem(ref foreign_item) => &foreign_item.attrs,
|
2018-03-16 06:20:56 +00:00
|
|
|
Annotatable::Stmt(ref stmt) => stmt.attrs(),
|
|
|
|
Annotatable::Expr(ref expr) => &expr.attrs,
|
2014-12-02 18:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
|
2014-12-02 18:07:41 +00:00
|
|
|
match self {
|
2016-06-12 12:45:16 +00:00
|
|
|
Annotatable::Item(item) => Annotatable::Item(item.map_attrs(f)),
|
|
|
|
Annotatable::TraitItem(trait_item) => Annotatable::TraitItem(trait_item.map_attrs(f)),
|
|
|
|
Annotatable::ImplItem(impl_item) => Annotatable::ImplItem(impl_item.map_attrs(f)),
|
2018-03-11 02:16:26 +00:00
|
|
|
Annotatable::ForeignItem(foreign_item) =>
|
|
|
|
Annotatable::ForeignItem(foreign_item.map_attrs(f)),
|
2018-03-16 06:20:56 +00:00
|
|
|
Annotatable::Stmt(stmt) => Annotatable::Stmt(stmt.map_attrs(f)),
|
|
|
|
Annotatable::Expr(expr) => Annotatable::Expr(expr.map_attrs(f)),
|
2014-12-02 18:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-12 12:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Annotatable {
|
2017-03-17 04:04:41 +00:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match *self {
|
|
|
|
Annotatable::Item(ref item) => item.span,
|
|
|
|
Annotatable::TraitItem(ref trait_item) => trait_item.span,
|
|
|
|
Annotatable::ImplItem(ref impl_item) => impl_item.span,
|
2018-03-11 02:16:26 +00:00
|
|
|
Annotatable::ForeignItem(ref foreign_item) => foreign_item.span,
|
2018-03-16 06:20:56 +00:00
|
|
|
Annotatable::Stmt(ref stmt) => stmt.span,
|
|
|
|
Annotatable::Expr(ref expr) => expr.span,
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-02 18:07:41 +00:00
|
|
|
pub fn expect_item(self) -> P<ast::Item> {
|
|
|
|
match self {
|
|
|
|
Annotatable::Item(i) => i,
|
|
|
|
_ => panic!("expected Item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:34:39 +00:00
|
|
|
pub fn map_item_or<F, G>(self, mut f: F, mut or: G) -> Annotatable
|
|
|
|
where F: FnMut(P<ast::Item>) -> P<ast::Item>,
|
|
|
|
G: FnMut(Annotatable) -> Annotatable
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
Annotatable::Item(i) => Annotatable::Item(f(i)),
|
|
|
|
_ => or(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:33:09 +00:00
|
|
|
pub fn expect_trait_item(self) -> ast::TraitItem {
|
2014-12-02 18:07:41 +00:00
|
|
|
match self {
|
2017-12-16 23:21:29 +00:00
|
|
|
Annotatable::TraitItem(i) => i.into_inner(),
|
2014-12-02 18:07:41 +00:00
|
|
|
_ => panic!("expected Item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:33:09 +00:00
|
|
|
pub fn expect_impl_item(self) -> ast::ImplItem {
|
2014-12-02 18:07:41 +00:00
|
|
|
match self {
|
2017-12-16 23:21:29 +00:00
|
|
|
Annotatable::ImplItem(i) => i.into_inner(),
|
2014-12-02 18:07:41 +00:00
|
|
|
_ => panic!("expected Item")
|
|
|
|
}
|
|
|
|
}
|
2017-12-26 07:47:32 +00:00
|
|
|
|
2018-03-11 02:16:26 +00:00
|
|
|
pub fn expect_foreign_item(self) -> ast::ForeignItem {
|
|
|
|
match self {
|
|
|
|
Annotatable::ForeignItem(i) => i.into_inner(),
|
|
|
|
_ => panic!("expected foreign item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 06:19:21 +00:00
|
|
|
pub fn expect_stmt(self) -> ast::Stmt {
|
|
|
|
match self {
|
|
|
|
Annotatable::Stmt(stmt) => stmt.into_inner(),
|
|
|
|
_ => panic!("expected statement"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expect_expr(self) -> P<ast::Expr> {
|
|
|
|
match self {
|
|
|
|
Annotatable::Expr(expr) => expr,
|
|
|
|
_ => panic!("expected expression"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 07:47:32 +00:00
|
|
|
pub fn derive_allowed(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Annotatable::Item(ref item) => match item.node {
|
|
|
|
ast::ItemKind::Struct(..) |
|
|
|
|
ast::ItemKind::Enum(..) |
|
|
|
|
ast::ItemKind::Union(..) => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 21:59:41 +00:00
|
|
|
// A more flexible ItemDecorator.
|
|
|
|
pub trait MultiItemDecorator {
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
2015-05-22 15:40:14 +00:00
|
|
|
item: &Annotatable,
|
2015-04-28 05:34:39 +00:00
|
|
|
push: &mut FnMut(Annotatable));
|
2015-01-16 21:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> MultiItemDecorator for F
|
2015-05-22 15:40:14 +00:00
|
|
|
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &Annotatable, &mut FnMut(Annotatable))
|
2015-01-16 21:59:41 +00:00
|
|
|
{
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
2015-05-22 15:40:14 +00:00
|
|
|
item: &Annotatable,
|
2015-04-25 03:31:11 +00:00
|
|
|
push: &mut FnMut(Annotatable)) {
|
2015-01-16 21:59:41 +00:00
|
|
|
(*self)(ecx, sp, meta_item, item, push)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:05:19 +00:00
|
|
|
// `meta_item` is the annotation, and `item` is the item being modified.
|
2014-12-02 18:07:41 +00:00
|
|
|
// FIXME Decorators should follow the same pattern too.
|
|
|
|
pub trait MultiItemModifier {
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: Annotatable)
|
2016-06-12 16:05:19 +00:00
|
|
|
-> Vec<Annotatable>;
|
2014-12-02 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 16:05:19 +00:00
|
|
|
impl<F, T> MultiItemModifier for F
|
|
|
|
where F: Fn(&mut ExtCtxt, Span, &ast::MetaItem, Annotatable) -> T,
|
|
|
|
T: Into<Vec<Annotatable>>,
|
2014-12-02 18:07:41 +00:00
|
|
|
{
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: Annotatable)
|
2016-06-12 16:05:19 +00:00
|
|
|
-> Vec<Annotatable> {
|
|
|
|
(*self)(ecx, span, meta_item, item).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Vec<Annotatable>> for Annotatable {
|
|
|
|
fn into(self) -> Vec<Annotatable> {
|
|
|
|
vec![self]
|
2014-12-02 18:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 04:16:43 +00:00
|
|
|
pub trait ProcMacro {
|
|
|
|
fn expand<'cx>(&self,
|
|
|
|
ecx: &'cx mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
ts: TokenStream)
|
2016-09-06 05:57:58 +00:00
|
|
|
-> TokenStream;
|
2016-08-29 04:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> ProcMacro for F
|
|
|
|
where F: Fn(TokenStream) -> TokenStream
|
|
|
|
{
|
|
|
|
fn expand<'cx>(&self,
|
2016-09-06 05:57:58 +00:00
|
|
|
_ecx: &'cx mut ExtCtxt,
|
|
|
|
_span: Span,
|
2016-08-29 04:16:43 +00:00
|
|
|
ts: TokenStream)
|
2016-09-06 05:57:58 +00:00
|
|
|
-> TokenStream {
|
2016-08-29 04:16:43 +00:00
|
|
|
// FIXME setup implicit context in TLS before calling self.
|
2016-09-06 05:57:58 +00:00
|
|
|
(*self)(ts)
|
2016-08-29 04:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait AttrProcMacro {
|
|
|
|
fn expand<'cx>(&self,
|
|
|
|
ecx: &'cx mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
annotation: TokenStream,
|
|
|
|
annotated: TokenStream)
|
2016-09-06 05:57:58 +00:00
|
|
|
-> TokenStream;
|
2016-08-29 04:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> AttrProcMacro for F
|
|
|
|
where F: Fn(TokenStream, TokenStream) -> TokenStream
|
|
|
|
{
|
|
|
|
fn expand<'cx>(&self,
|
2016-09-06 05:57:58 +00:00
|
|
|
_ecx: &'cx mut ExtCtxt,
|
|
|
|
_span: Span,
|
2016-08-29 04:16:43 +00:00
|
|
|
annotation: TokenStream,
|
|
|
|
annotated: TokenStream)
|
2016-09-06 05:57:58 +00:00
|
|
|
-> TokenStream {
|
2016-08-29 04:16:43 +00:00
|
|
|
// FIXME setup implicit context in TLS before calling self.
|
2016-09-06 05:57:58 +00:00
|
|
|
(*self)(annotation, annotated)
|
2016-08-29 04:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:09:56 +00:00
|
|
|
/// Represents a thing that maps token trees to Macro Results
|
|
|
|
pub trait TTMacroExpander {
|
2017-02-21 05:05:59 +00:00
|
|
|
fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream)
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<MacResult+'cx>;
|
2013-08-30 21:40:05 +00:00
|
|
|
}
|
|
|
|
|
2014-01-25 21:34:26 +00:00
|
|
|
pub type MacroExpanderFn =
|
2016-06-20 15:49:33 +00:00
|
|
|
for<'cx> fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree])
|
|
|
|
-> Box<MacResult+'cx>;
|
2013-08-30 21:40:05 +00:00
|
|
|
|
2014-11-26 10:52:16 +00:00
|
|
|
impl<F> TTMacroExpander for F
|
2017-02-21 05:05:59 +00:00
|
|
|
where F: for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) -> Box<MacResult+'cx>
|
2014-11-26 10:52:16 +00:00
|
|
|
{
|
2017-02-21 05:05:59 +00:00
|
|
|
fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream)
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<MacResult+'cx> {
|
2017-03-29 07:17:18 +00:00
|
|
|
struct AvoidInterpolatedIdents;
|
|
|
|
|
|
|
|
impl Folder for AvoidInterpolatedIdents {
|
|
|
|
fn fold_tt(&mut self, tt: tokenstream::TokenTree) -> tokenstream::TokenTree {
|
|
|
|
if let tokenstream::TokenTree::Token(_, token::Interpolated(ref nt)) = tt {
|
2018-03-10 05:56:40 +00:00
|
|
|
if let token::NtIdent(ident, is_raw) = nt.0 {
|
|
|
|
return tokenstream::TokenTree::Token(ident.span,
|
2018-03-18 13:47:09 +00:00
|
|
|
token::Ident(ident, is_raw));
|
2017-03-29 07:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fold::noop_fold_tt(tt, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
fold::noop_fold_mac(mac, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let input: Vec<_> =
|
|
|
|
input.trees().map(|tt| AvoidInterpolatedIdents.fold_tt(tt)).collect();
|
|
|
|
(*self)(ecx, span, &input)
|
2013-08-30 21:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-08 22:55:14 +00:00
|
|
|
|
2014-01-25 21:34:26 +00:00
|
|
|
pub trait IdentMacroExpander {
|
2014-08-28 01:46:52 +00:00
|
|
|
fn expand<'cx>(&self,
|
|
|
|
cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
ident: ast::Ident,
|
2016-12-01 11:54:01 +00:00
|
|
|
token_tree: Vec<tokenstream::TokenTree>)
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<MacResult+'cx>;
|
2013-08-30 21:40:05 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 03:59:26 +00:00
|
|
|
pub type IdentMacroExpanderFn =
|
2016-06-20 15:49:33 +00:00
|
|
|
for<'cx> fn(&'cx mut ExtCtxt, Span, ast::Ident, Vec<tokenstream::TokenTree>)
|
|
|
|
-> Box<MacResult+'cx>;
|
2014-09-11 03:59:26 +00:00
|
|
|
|
2014-11-26 10:52:16 +00:00
|
|
|
impl<F> IdentMacroExpander for F
|
|
|
|
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, ast::Ident,
|
2016-06-20 15:49:33 +00:00
|
|
|
Vec<tokenstream::TokenTree>) -> Box<MacResult+'cx>
|
2014-11-26 10:52:16 +00:00
|
|
|
{
|
2014-08-28 01:46:52 +00:00
|
|
|
fn expand<'cx>(&self,
|
|
|
|
cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
ident: ast::Ident,
|
2016-12-01 11:54:01 +00:00
|
|
|
token_tree: Vec<tokenstream::TokenTree>)
|
2014-11-26 10:52:16 +00:00
|
|
|
-> Box<MacResult+'cx>
|
|
|
|
{
|
|
|
|
(*self)(cx, sp, ident, token_tree)
|
2013-08-30 21:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 19:14:42 +00:00
|
|
|
// Use a macro because forwarding to a simple function has type system issues
|
2015-04-07 13:21:18 +00:00
|
|
|
macro_rules! make_stmts_default {
|
2015-02-27 19:14:42 +00:00
|
|
|
($me:expr) => {
|
2016-06-17 02:30:01 +00:00
|
|
|
$me.make_expr().map(|e| SmallVector::one(ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: e.span,
|
|
|
|
node: ast::StmtKind::Expr(e),
|
|
|
|
}))
|
2015-02-27 19:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 12:00:14 +00:00
|
|
|
/// The result of a macro expansion. The return values of the various
|
2015-02-27 19:14:42 +00:00
|
|
|
/// methods are spliced into the AST at the callsite of the macro.
|
2014-04-15 12:00:14 +00:00
|
|
|
pub trait MacResult {
|
|
|
|
/// Create an expression.
|
2014-09-13 16:06:01 +00:00
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
2014-04-15 12:00:14 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
/// Create zero or more items.
|
2014-09-13 16:06:01 +00:00
|
|
|
fn make_items(self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> {
|
2014-04-15 12:00:14 +00:00
|
|
|
None
|
|
|
|
}
|
2014-07-11 00:46:09 +00:00
|
|
|
|
2015-03-11 21:38:58 +00:00
|
|
|
/// Create zero or more impl items.
|
2016-02-11 20:33:09 +00:00
|
|
|
fn make_impl_items(self: Box<Self>) -> Option<SmallVector<ast::ImplItem>> {
|
2014-07-11 00:46:09 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-06-11 01:00:07 +00:00
|
|
|
/// Create zero or more trait items.
|
|
|
|
fn make_trait_items(self: Box<Self>) -> Option<SmallVector<ast::TraitItem>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-03-11 02:16:26 +00:00
|
|
|
/// Create zero or more items in an `extern {}` block
|
|
|
|
fn make_foreign_items(self: Box<Self>) -> Option<SmallVector<ast::ForeignItem>> { None }
|
|
|
|
|
2014-05-19 20:32:51 +00:00
|
|
|
/// Create a pattern.
|
2014-09-13 16:06:01 +00:00
|
|
|
fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> {
|
2014-05-19 20:32:51 +00:00
|
|
|
None
|
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
|
2015-04-07 13:21:18 +00:00
|
|
|
/// Create zero or more statements.
|
2014-04-15 12:00:14 +00:00
|
|
|
///
|
|
|
|
/// By default this attempts to create an expression statement,
|
|
|
|
/// returning None if that fails.
|
2016-02-11 20:33:09 +00:00
|
|
|
fn make_stmts(self: Box<Self>) -> Option<SmallVector<ast::Stmt>> {
|
2015-04-07 13:21:18 +00:00
|
|
|
make_stmts_default!(self)
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
2015-07-26 04:54:19 +00:00
|
|
|
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
|
|
None
|
|
|
|
}
|
2013-08-30 21:40:05 +00:00
|
|
|
}
|
2013-07-08 22:55:14 +00:00
|
|
|
|
2015-02-27 19:14:42 +00:00
|
|
|
macro_rules! make_MacEager {
|
|
|
|
( $( $fld:ident: $t:ty, )* ) => {
|
|
|
|
/// `MacResult` implementation for the common case where you've already
|
|
|
|
/// built each form of AST that you might return.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct MacEager {
|
|
|
|
$(
|
|
|
|
pub $fld: Option<$t>,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MacEager {
|
|
|
|
$(
|
|
|
|
pub fn $fld(v: $t) -> Box<MacResult> {
|
2015-04-16 03:56:16 +00:00
|
|
|
Box::new(MacEager {
|
2015-02-27 19:14:42 +00:00
|
|
|
$fld: Some(v),
|
|
|
|
..Default::default()
|
2015-04-16 03:56:16 +00:00
|
|
|
})
|
2015-02-27 19:14:42 +00:00
|
|
|
}
|
|
|
|
)*
|
2014-08-30 22:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
2015-02-27 19:14:42 +00:00
|
|
|
|
|
|
|
make_MacEager! {
|
|
|
|
expr: P<ast::Expr>,
|
|
|
|
pat: P<ast::Pat>,
|
|
|
|
items: SmallVector<P<ast::Item>>,
|
2016-02-11 20:33:09 +00:00
|
|
|
impl_items: SmallVector<ast::ImplItem>,
|
2016-06-11 01:00:07 +00:00
|
|
|
trait_items: SmallVector<ast::TraitItem>,
|
2018-03-11 02:16:26 +00:00
|
|
|
foreign_items: SmallVector<ast::ForeignItem>,
|
2016-02-11 20:33:09 +00:00
|
|
|
stmts: SmallVector<ast::Stmt>,
|
2015-07-26 04:54:19 +00:00
|
|
|
ty: P<ast::Ty>,
|
2014-05-19 20:32:51 +00:00
|
|
|
}
|
2015-02-27 19:14:42 +00:00
|
|
|
|
|
|
|
impl MacResult for MacEager {
|
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
|
|
|
self.expr
|
2014-05-19 20:32:51 +00:00
|
|
|
}
|
2015-02-27 19:14:42 +00:00
|
|
|
|
|
|
|
fn make_items(self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> {
|
|
|
|
self.items
|
2014-05-19 20:32:51 +00:00
|
|
|
}
|
2014-09-13 10:55:00 +00:00
|
|
|
|
2016-02-11 20:33:09 +00:00
|
|
|
fn make_impl_items(self: Box<Self>) -> Option<SmallVector<ast::ImplItem>> {
|
2015-03-11 21:38:58 +00:00
|
|
|
self.impl_items
|
2015-02-27 19:14:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 01:00:07 +00:00
|
|
|
fn make_trait_items(self: Box<Self>) -> Option<SmallVector<ast::TraitItem>> {
|
|
|
|
self.trait_items
|
|
|
|
}
|
|
|
|
|
2018-03-11 02:16:26 +00:00
|
|
|
fn make_foreign_items(self: Box<Self>) -> Option<SmallVector<ast::ForeignItem>> {
|
|
|
|
self.foreign_items
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:33:09 +00:00
|
|
|
fn make_stmts(self: Box<Self>) -> Option<SmallVector<ast::Stmt>> {
|
2015-04-07 13:21:18 +00:00
|
|
|
match self.stmts.as_ref().map_or(0, |s| s.len()) {
|
|
|
|
0 => make_stmts_default!(self),
|
|
|
|
_ => self.stmts,
|
2015-02-27 19:14:42 +00:00
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
2014-09-13 10:55:00 +00:00
|
|
|
|
2015-02-27 19:14:42 +00:00
|
|
|
fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> {
|
|
|
|
if let Some(p) = self.pat {
|
|
|
|
return Some(p);
|
|
|
|
}
|
|
|
|
if let Some(e) = self.expr {
|
2016-02-08 15:05:05 +00:00
|
|
|
if let ast::ExprKind::Lit(_) = e.node {
|
2015-02-27 19:14:42 +00:00
|
|
|
return Some(P(ast::Pat {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: e.span,
|
2016-02-11 18:16:33 +00:00
|
|
|
node: PatKind::Lit(e),
|
2015-02-27 19:14:42 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
2015-07-26 04:54:19 +00:00
|
|
|
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
|
|
self.ty
|
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
2014-02-18 16:14:12 +00:00
|
|
|
|
2014-04-15 12:00:14 +00:00
|
|
|
/// Fill-in macro expansion result, to allow compilation to continue
|
|
|
|
/// after hitting errors.
|
2015-03-30 13:38:59 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2014-04-15 12:00:14 +00:00
|
|
|
pub struct DummyResult {
|
|
|
|
expr_only: bool,
|
|
|
|
span: Span
|
2012-07-06 21:29:50 +00:00
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
|
|
|
|
impl DummyResult {
|
|
|
|
/// Create a default MacResult that can be anything.
|
|
|
|
///
|
|
|
|
/// Use this as a return value after hitting any errors and
|
|
|
|
/// calling `span_err`.
|
2014-08-28 01:46:52 +00:00
|
|
|
pub fn any(sp: Span) -> Box<MacResult+'static> {
|
2015-04-16 03:56:16 +00:00
|
|
|
Box::new(DummyResult { expr_only: false, span: sp })
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a default MacResult that can only be an expression.
|
|
|
|
///
|
|
|
|
/// Use this for macros that must expand to an expression, so even
|
2014-06-09 04:00:52 +00:00
|
|
|
/// if an error is encountered internally, the user will receive
|
2014-04-15 12:00:14 +00:00
|
|
|
/// an error that they also used it in the wrong place.
|
2014-08-28 01:46:52 +00:00
|
|
|
pub fn expr(sp: Span) -> Box<MacResult+'static> {
|
2015-04-16 03:56:16 +00:00
|
|
|
Box::new(DummyResult { expr_only: true, span: sp })
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A plain dummy expression.
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn raw_expr(sp: Span) -> P<ast::Expr> {
|
|
|
|
P(ast::Expr {
|
2014-02-18 16:14:12 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-08 16:06:20 +00:00
|
|
|
node: ast::ExprKind::Lit(P(codemap::respan(sp, ast::LitKind::Bool(false)))),
|
2014-03-18 12:14:08 +00:00
|
|
|
span: sp,
|
2016-06-18 04:01:57 +00:00
|
|
|
attrs: ast::ThinVec::new(),
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2014-02-18 16:14:12 +00:00
|
|
|
}
|
2014-05-19 20:32:51 +00:00
|
|
|
|
|
|
|
/// A plain dummy pattern.
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn raw_pat(sp: Span) -> ast::Pat {
|
|
|
|
ast::Pat {
|
2014-05-19 20:32:51 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-11 18:16:33 +00:00
|
|
|
node: PatKind::Wild,
|
2014-05-19 20:32:51 +00:00
|
|
|
span: sp,
|
|
|
|
}
|
|
|
|
}
|
2014-07-11 00:46:09 +00:00
|
|
|
|
2015-07-26 04:54:19 +00:00
|
|
|
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
|
|
|
|
P(ast::Ty {
|
2015-07-26 05:17:43 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-08 15:53:21 +00:00
|
|
|
node: ast::TyKind::Infer,
|
2015-07-26 04:54:19 +00:00
|
|
|
span: sp
|
|
|
|
})
|
2015-07-26 05:17:43 +00:00
|
|
|
}
|
2014-03-18 12:14:08 +00:00
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
|
|
|
|
impl MacResult for DummyResult {
|
2014-09-13 16:06:01 +00:00
|
|
|
fn make_expr(self: Box<DummyResult>) -> Option<P<ast::Expr>> {
|
2014-04-15 12:00:14 +00:00
|
|
|
Some(DummyResult::raw_expr(self.span))
|
2014-03-18 12:14:08 +00:00
|
|
|
}
|
2015-07-26 05:17:43 +00:00
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> {
|
|
|
|
Some(P(DummyResult::raw_pat(self.span)))
|
2014-05-19 20:32:51 +00:00
|
|
|
}
|
2015-07-26 05:17:43 +00:00
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
fn make_items(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Item>>> {
|
2014-07-11 00:46:09 +00:00
|
|
|
// this code needs a comment... why not always just return the Some() ?
|
|
|
|
if self.expr_only {
|
|
|
|
None
|
|
|
|
} else {
|
2016-11-03 04:33:35 +00:00
|
|
|
Some(SmallVector::new())
|
2014-07-11 00:46:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-26 05:17:43 +00:00
|
|
|
|
2016-02-11 20:33:09 +00:00
|
|
|
fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVector<ast::ImplItem>> {
|
2014-04-15 12:00:14 +00:00
|
|
|
if self.expr_only {
|
|
|
|
None
|
|
|
|
} else {
|
2016-11-03 04:33:35 +00:00
|
|
|
Some(SmallVector::new())
|
2014-04-15 12:00:14 +00:00
|
|
|
}
|
2014-03-18 12:14:08 +00:00
|
|
|
}
|
2015-07-26 05:17:43 +00:00
|
|
|
|
2016-06-11 01:00:07 +00:00
|
|
|
fn make_trait_items(self: Box<DummyResult>) -> Option<SmallVector<ast::TraitItem>> {
|
|
|
|
if self.expr_only {
|
2018-03-11 02:16:26 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(SmallVector::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_foreign_items(self: Box<Self>) -> Option<SmallVector<ast::ForeignItem>> {
|
|
|
|
if self.expr_only {
|
2016-06-11 01:00:07 +00:00
|
|
|
None
|
|
|
|
} else {
|
2016-11-03 04:33:35 +00:00
|
|
|
Some(SmallVector::new())
|
2016-06-11 01:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:33:09 +00:00
|
|
|
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVector<ast::Stmt>> {
|
2016-06-17 02:30:01 +00:00
|
|
|
Some(SmallVector::one(ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::StmtKind::Expr(DummyResult::raw_expr(self.span)),
|
|
|
|
span: self.span,
|
|
|
|
}))
|
2014-03-18 12:14:08 +00:00
|
|
|
}
|
2016-06-17 11:02:42 +00:00
|
|
|
|
|
|
|
fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> {
|
|
|
|
Some(DummyResult::raw_ty(self.span))
|
|
|
|
}
|
2014-01-17 14:53:10 +00:00
|
|
|
}
|
2012-07-05 19:10:33 +00:00
|
|
|
|
2017-01-23 15:01:49 +00:00
|
|
|
pub type BuiltinDeriveFn =
|
|
|
|
for<'cx> fn(&'cx mut ExtCtxt, Span, &MetaItem, &Annotatable, &mut FnMut(Annotatable));
|
|
|
|
|
2017-02-06 11:44:38 +00:00
|
|
|
/// Represents different kinds of macro invocations that can be resolved.
|
2017-02-23 09:42:33 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2017-02-06 11:44:38 +00:00
|
|
|
pub enum MacroKind {
|
|
|
|
/// A bang macro - foo!()
|
|
|
|
Bang,
|
|
|
|
/// An attribute macro - #[foo]
|
|
|
|
Attr,
|
|
|
|
/// A derive attribute macro - #[derive(Foo)]
|
|
|
|
Derive,
|
|
|
|
}
|
|
|
|
|
2014-02-28 07:49:25 +00:00
|
|
|
/// An enum representing the different kinds of syntax extensions.
|
2013-01-29 22:41:40 +00:00
|
|
|
pub enum SyntaxExtension {
|
2014-02-28 07:49:25 +00:00
|
|
|
/// A syntax extension that is attached to an item and creates new items
|
|
|
|
/// based upon it.
|
|
|
|
///
|
2015-01-20 19:28:36 +00:00
|
|
|
/// `#[derive(...)]` is a `MultiItemDecorator`.
|
2016-08-29 04:16:43 +00:00
|
|
|
///
|
|
|
|
/// Prefer ProcMacro or MultiModifier since they are more flexible.
|
2018-03-03 05:24:49 +00:00
|
|
|
MultiDecorator(Box<MultiItemDecorator + sync::Sync + sync::Send>),
|
2012-06-25 22:04:50 +00:00
|
|
|
|
2014-02-28 07:49:25 +00:00
|
|
|
/// A syntax extension that is attached to an item and modifies it
|
2016-08-29 04:16:43 +00:00
|
|
|
/// in-place. Also allows decoration, i.e., creating new items.
|
2018-03-03 05:24:49 +00:00
|
|
|
MultiModifier(Box<MultiItemModifier + sync::Sync + sync::Send>),
|
2016-08-29 04:16:43 +00:00
|
|
|
|
|
|
|
/// A function-like procedural macro. TokenStream -> TokenStream.
|
2018-03-03 05:24:49 +00:00
|
|
|
ProcMacro(Box<ProcMacro + sync::Sync + sync::Send>),
|
2016-08-29 04:16:43 +00:00
|
|
|
|
|
|
|
/// An attribute-like procedural macro. TokenStream, TokenStream -> TokenStream.
|
|
|
|
/// The first TokenSteam is the attribute, the second is the annotated item.
|
|
|
|
/// Allows modification of the input items and adding new items, similar to
|
|
|
|
/// MultiModifier, but uses TokenStreams, rather than AST nodes.
|
2018-03-03 05:24:49 +00:00
|
|
|
AttrProcMacro(Box<AttrProcMacro + sync::Sync + sync::Send>),
|
2014-12-02 18:07:41 +00:00
|
|
|
|
2014-02-28 07:49:25 +00:00
|
|
|
/// A normal, function-like syntax extension.
|
|
|
|
///
|
|
|
|
/// `bytes!` is a `NormalTT`.
|
2017-08-08 15:21:20 +00:00
|
|
|
NormalTT {
|
2018-03-03 05:24:49 +00:00
|
|
|
expander: Box<TTMacroExpander + sync::Sync + sync::Send>,
|
2017-08-08 15:21:20 +00:00
|
|
|
def_info: Option<(ast::NodeId, Span)>,
|
|
|
|
/// Whether the contents of the macro can
|
|
|
|
/// directly use `#[unstable]` things (true == yes).
|
|
|
|
allow_internal_unstable: bool,
|
|
|
|
/// Whether the contents of the macro can use `unsafe`
|
|
|
|
/// without triggering the `unsafe_code` lint.
|
|
|
|
allow_internal_unsafe: bool,
|
2018-02-25 03:11:06 +00:00
|
|
|
/// The macro's feature name if it is unstable, and the stability feature
|
|
|
|
unstable_feature: Option<(Symbol, u32)>,
|
2017-08-08 15:21:20 +00:00
|
|
|
},
|
2013-02-26 18:15:29 +00:00
|
|
|
|
2014-02-28 07:49:25 +00:00
|
|
|
/// A function-like syntax extension that has an extra ident before
|
|
|
|
/// the block.
|
|
|
|
///
|
2018-03-03 05:24:49 +00:00
|
|
|
IdentTT(Box<IdentMacroExpander + sync::Sync + sync::Send>, Option<Span>, bool),
|
2016-10-15 20:50:30 +00:00
|
|
|
|
2017-01-23 22:25:08 +00:00
|
|
|
/// An attribute-like procedural macro. TokenStream -> TokenStream.
|
|
|
|
/// The input is the annotated item.
|
|
|
|
/// Allows generating code to implement a Trait for a given struct
|
|
|
|
/// or enum item.
|
2018-03-03 05:24:49 +00:00
|
|
|
ProcMacroDerive(Box<MultiItemModifier +
|
|
|
|
sync::Sync +
|
|
|
|
sync::Send>, Vec<Symbol> /* inert attribute names */),
|
2017-01-23 15:01:49 +00:00
|
|
|
|
|
|
|
/// An attribute-like procedural macro that derives a builtin trait.
|
|
|
|
BuiltinDerive(BuiltinDeriveFn),
|
2017-03-22 08:39:51 +00:00
|
|
|
|
|
|
|
/// A declarative macro, e.g. `macro m() {}`.
|
2017-05-31 15:03:41 +00:00
|
|
|
///
|
|
|
|
/// The second element is the definition site span.
|
2018-03-03 05:24:49 +00:00
|
|
|
DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>),
|
2011-06-21 00:26:17 +00:00
|
|
|
}
|
2011-06-04 19:41:45 +00:00
|
|
|
|
2017-02-06 11:44:38 +00:00
|
|
|
impl SyntaxExtension {
|
|
|
|
/// Return which kind of macro calls this syntax extension.
|
|
|
|
pub fn kind(&self) -> MacroKind {
|
|
|
|
match *self {
|
2017-03-22 08:39:51 +00:00
|
|
|
SyntaxExtension::DeclMacro(..) |
|
2017-08-08 15:21:20 +00:00
|
|
|
SyntaxExtension::NormalTT { .. } |
|
2017-02-06 11:44:38 +00:00
|
|
|
SyntaxExtension::IdentTT(..) |
|
|
|
|
SyntaxExtension::ProcMacro(..) =>
|
|
|
|
MacroKind::Bang,
|
|
|
|
SyntaxExtension::MultiDecorator(..) |
|
|
|
|
SyntaxExtension::MultiModifier(..) |
|
|
|
|
SyntaxExtension::AttrProcMacro(..) =>
|
|
|
|
MacroKind::Attr,
|
|
|
|
SyntaxExtension::ProcMacroDerive(..) |
|
|
|
|
SyntaxExtension::BuiltinDerive(..) =>
|
|
|
|
MacroKind::Derive,
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
|
|
|
pub fn is_modern(&self) -> bool {
|
|
|
|
match *self {
|
2017-03-17 23:41:09 +00:00
|
|
|
SyntaxExtension::DeclMacro(..) |
|
|
|
|
SyntaxExtension::ProcMacro(..) |
|
|
|
|
SyntaxExtension::AttrProcMacro(..) |
|
|
|
|
SyntaxExtension::ProcMacroDerive(..) => true,
|
2017-03-22 08:39:51 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2017-02-06 11:44:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 23:16:10 +00:00
|
|
|
pub type NamedSyntaxExtension = (Name, SyntaxExtension);
|
|
|
|
|
2016-09-05 03:46:05 +00:00
|
|
|
pub trait Resolver {
|
2016-09-05 00:10:27 +00:00
|
|
|
fn next_node_id(&mut self) -> ast::NodeId;
|
2016-09-23 07:23:01 +00:00
|
|
|
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark;
|
2016-11-08 03:16:54 +00:00
|
|
|
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item>;
|
2016-12-22 06:03:19 +00:00
|
|
|
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]);
|
2018-02-27 16:11:14 +00:00
|
|
|
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>);
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
fn resolve_imports(&mut self);
|
2017-02-02 07:01:15 +00:00
|
|
|
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
|
2018-04-18 06:19:21 +00:00
|
|
|
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<Attribute>, allow_derive: bool)
|
|
|
|
-> Option<Attribute>;
|
|
|
|
|
2017-03-01 08:44:05 +00:00
|
|
|
fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
|
2018-02-27 16:11:14 +00:00
|
|
|
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy>;
|
2017-03-01 08:44:05 +00:00
|
|
|
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
|
2018-02-27 16:11:14 +00:00
|
|
|
-> Result<Lrc<SyntaxExtension>, Determinacy>;
|
2017-05-11 08:26:07 +00:00
|
|
|
fn check_unused_macros(&self);
|
2016-06-02 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 21:33:15 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2016-10-11 03:41:48 +00:00
|
|
|
pub enum Determinacy {
|
|
|
|
Determined,
|
|
|
|
Undetermined,
|
|
|
|
}
|
|
|
|
|
2016-09-05 03:46:05 +00:00
|
|
|
pub struct DummyResolver;
|
2016-09-05 00:10:27 +00:00
|
|
|
|
2016-09-05 03:46:05 +00:00
|
|
|
impl Resolver for DummyResolver {
|
2016-09-05 00:10:27 +00:00
|
|
|
fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID }
|
2016-09-23 07:23:01 +00:00
|
|
|
fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() }
|
2016-11-08 03:16:54 +00:00
|
|
|
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> { item }
|
2016-12-22 06:03:19 +00:00
|
|
|
fn is_whitelisted_legacy_custom_derive(&self, _name: Name) -> bool { false }
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion, _derives: &[Mark]) {}
|
2018-02-27 16:11:14 +00:00
|
|
|
fn add_builtin(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
fn resolve_imports(&mut self) {}
|
2018-04-18 06:19:21 +00:00
|
|
|
fn find_legacy_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>, _allow_derive: bool)
|
|
|
|
-> Option<Attribute> { None }
|
2017-03-01 08:44:05 +00:00
|
|
|
fn resolve_invoc(&mut self, _invoc: &mut Invocation, _scope: Mark, _force: bool)
|
2018-02-27 16:11:14 +00:00
|
|
|
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
|
2017-03-01 08:44:05 +00:00
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
2017-02-06 11:44:38 +00:00
|
|
|
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _kind: MacroKind,
|
2018-02-27 16:11:14 +00:00
|
|
|
_force: bool) -> Result<Lrc<SyntaxExtension>, Determinacy> {
|
2017-02-01 10:33:09 +00:00
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
2017-05-11 08:26:07 +00:00
|
|
|
fn check_unused_macros(&self) {}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ModuleData {
|
|
|
|
pub mod_path: Vec<ast::Ident>,
|
|
|
|
pub directory: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ExpansionData {
|
|
|
|
pub mark: Mark,
|
|
|
|
pub depth: usize,
|
|
|
|
pub module: Rc<ModuleData>,
|
2016-11-05 04:16:26 +00:00
|
|
|
pub directory_ownership: DirectoryOwnership,
|
2018-02-25 03:11:06 +00:00
|
|
|
pub crate_span: Option<Span>,
|
2016-06-02 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// One of these is made during expansion and incrementally updated as we go;
|
2017-05-13 19:40:06 +00:00
|
|
|
/// when a macro expansion occurs, the resulting nodes have the `backtrace()
|
|
|
|
/// -> expn_info` of their expansion context stored into their span.
|
2013-12-25 18:10:33 +00:00
|
|
|
pub struct ExtCtxt<'a> {
|
2014-03-27 22:39:48 +00:00
|
|
|
pub parse_sess: &'a parse::ParseSess,
|
2015-02-15 20:30:45 +00:00
|
|
|
pub ecfg: expand::ExpansionConfig<'a>,
|
2017-09-22 03:37:00 +00:00
|
|
|
pub root_path: PathBuf,
|
2016-09-05 03:46:05 +00:00
|
|
|
pub resolver: &'a mut Resolver,
|
2016-09-20 06:33:42 +00:00
|
|
|
pub resolve_err_count: usize,
|
2016-09-07 23:21:59 +00:00
|
|
|
pub current_expansion: ExpansionData,
|
2017-05-06 04:49:59 +00:00
|
|
|
pub expansions: HashMap<Span, Vec<String>>,
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
2013-03-15 19:24:24 +00:00
|
|
|
|
2013-12-25 18:10:33 +00:00
|
|
|
impl<'a> ExtCtxt<'a> {
|
2016-10-27 06:36:56 +00:00
|
|
|
pub fn new(parse_sess: &'a parse::ParseSess,
|
2015-07-14 00:10:44 +00:00
|
|
|
ecfg: expand::ExpansionConfig<'a>,
|
2016-09-05 03:46:05 +00:00
|
|
|
resolver: &'a mut Resolver)
|
2016-06-02 01:14:33 +00:00
|
|
|
-> ExtCtxt<'a> {
|
2013-12-28 00:17:36 +00:00
|
|
|
ExtCtxt {
|
2017-08-07 05:54:09 +00:00
|
|
|
parse_sess,
|
|
|
|
ecfg,
|
2017-09-22 03:37:00 +00:00
|
|
|
root_path: PathBuf::new(),
|
2017-08-07 05:54:09 +00:00
|
|
|
resolver,
|
2016-09-20 06:33:42 +00:00
|
|
|
resolve_err_count: 0,
|
2016-09-07 23:21:59 +00:00
|
|
|
current_expansion: ExpansionData {
|
|
|
|
mark: Mark::root(),
|
|
|
|
depth: 0,
|
|
|
|
module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }),
|
2017-11-28 02:14:24 +00:00
|
|
|
directory_ownership: DirectoryOwnership::Owned { relative: None },
|
2018-02-25 03:11:06 +00:00
|
|
|
crate_span: None,
|
2016-09-07 23:21:59 +00:00
|
|
|
},
|
2017-05-06 04:49:59 +00:00
|
|
|
expansions: HashMap::new(),
|
2013-05-17 11:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:25 +00:00
|
|
|
/// Returns a `Folder` for deeply expanding all macros in an AST node.
|
2014-07-20 14:25:35 +00:00
|
|
|
pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
|
2016-09-06 05:42:45 +00:00
|
|
|
expand::MacroExpander::new(self, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `Folder` that deeply expands all macros and assigns all node ids in an AST node.
|
|
|
|
/// Once node ids are assigned, the node may not be expanded, removed, or otherwise modified.
|
|
|
|
pub fn monotonic_expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
|
|
|
|
expand::MacroExpander::new(self, true)
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:05:59 +00:00
|
|
|
pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree]) -> parser::Parser<'a> {
|
|
|
|
parse::stream_to_parser(self.parse_sess, tts.iter().cloned().collect())
|
2014-07-03 09:42:24 +00:00
|
|
|
}
|
2015-05-13 20:08:02 +00:00
|
|
|
pub fn codemap(&self) -> &'a CodeMap { self.parse_sess.codemap() }
|
2014-03-09 14:54:34 +00:00
|
|
|
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
|
2016-10-27 06:36:56 +00:00
|
|
|
pub fn cfg(&self) -> &ast::CrateConfig { &self.parse_sess.config }
|
2013-08-31 16:13:04 +00:00
|
|
|
pub fn call_site(&self) -> Span {
|
2017-03-17 04:04:41 +00:00
|
|
|
match self.current_expansion.mark.expn_info() {
|
2014-01-03 23:08:48 +00:00
|
|
|
Some(expn_info) => expn_info.call_site,
|
2017-03-17 04:04:41 +00:00
|
|
|
None => DUMMY_SP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn backtrace(&self) -> SyntaxContext {
|
|
|
|
SyntaxContext::empty().apply_mark(self.current_expansion.mark)
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
2015-06-16 18:47:09 +00:00
|
|
|
|
|
|
|
/// Returns span for the macro which originally caused the current expansion to happen.
|
|
|
|
///
|
|
|
|
/// Stops backtracing at include! boundary.
|
2017-05-15 09:41:05 +00:00
|
|
|
pub fn expansion_cause(&self) -> Option<Span> {
|
2017-03-17 04:04:41 +00:00
|
|
|
let mut ctxt = self.backtrace();
|
2015-06-16 18:47:09 +00:00
|
|
|
let mut last_macro = None;
|
2014-09-17 16:01:33 +00:00
|
|
|
loop {
|
2017-03-17 04:04:41 +00:00
|
|
|
if ctxt.outer().expn_info().map_or(None, |info| {
|
|
|
|
if info.callee.name() == "include" {
|
|
|
|
// Stop going up the backtrace once include! is encountered
|
|
|
|
return None;
|
|
|
|
}
|
2017-07-31 20:04:34 +00:00
|
|
|
ctxt = info.call_site.ctxt();
|
2017-03-17 04:04:41 +00:00
|
|
|
last_macro = Some(info.call_site);
|
2017-05-12 18:05:39 +00:00
|
|
|
Some(())
|
2015-06-16 18:47:09 +00:00
|
|
|
}).is_none() {
|
|
|
|
break
|
2014-09-17 16:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 09:41:05 +00:00
|
|
|
last_macro
|
2014-09-17 16:01:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn struct_span_warn<S: Into<MultiSpan>>(&self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
2015-12-20 21:00:43 +00:00
|
|
|
self.parse_sess.span_diagnostic.struct_span_warn(sp, msg)
|
|
|
|
}
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn struct_span_err<S: Into<MultiSpan>>(&self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
2015-12-20 21:00:43 +00:00
|
|
|
self.parse_sess.span_diagnostic.struct_span_err(sp, msg)
|
|
|
|
}
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn struct_span_fatal<S: Into<MultiSpan>>(&self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
2015-12-20 21:00:43 +00:00
|
|
|
self.parse_sess.span_diagnostic.struct_span_fatal(sp, msg)
|
|
|
|
}
|
|
|
|
|
2014-01-17 14:53:10 +00:00
|
|
|
/// Emit `msg` attached to `sp`, and stop compilation immediately.
|
|
|
|
///
|
2014-07-03 01:27:07 +00:00
|
|
|
/// `span_err` should be strongly preferred where-ever possible:
|
2017-12-31 16:17:01 +00:00
|
|
|
/// this should *only* be used when:
|
|
|
|
///
|
2014-01-17 14:53:10 +00:00
|
|
|
/// - continuing has a high risk of flow-on errors (e.g. errors in
|
|
|
|
/// declaring a macro would cause all uses of that macro to
|
|
|
|
/// complain about "undefined macro"), or
|
|
|
|
/// - there is literally nothing else that can be done (however,
|
|
|
|
/// in most cases one can construct a dummy expression/item to
|
|
|
|
/// substitute; we never hit resolve/type-checking so the dummy
|
|
|
|
/// value doesn't have to match anything)
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
|
2018-01-21 11:47:58 +00:00
|
|
|
self.parse_sess.span_diagnostic.span_fatal(sp, msg).raise();
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
2014-01-17 14:53:10 +00:00
|
|
|
|
|
|
|
/// Emit `msg` attached to `sp`, without immediately stopping
|
|
|
|
/// compilation.
|
|
|
|
///
|
|
|
|
/// Compilation will be stopped in the near future (at the end of
|
|
|
|
/// the macro expansion phase).
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
|
2013-05-17 10:10:26 +00:00
|
|
|
self.parse_sess.span_diagnostic.span_err(sp, msg);
|
|
|
|
}
|
2018-02-12 22:21:20 +00:00
|
|
|
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
|
|
|
|
self.parse_sess.span_diagnostic.span_err_with_code(sp, msg, code);
|
|
|
|
}
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn mut_span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str)
|
2017-09-22 03:29:29 +00:00
|
|
|
-> DiagnosticBuilder<'a> {
|
|
|
|
self.parse_sess.span_diagnostic.mut_span_err(sp, msg)
|
|
|
|
}
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
|
2013-05-17 10:10:26 +00:00
|
|
|
self.parse_sess.span_diagnostic.span_warn(sp, msg);
|
|
|
|
}
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
|
2013-05-17 10:10:26 +00:00
|
|
|
self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
|
|
|
|
}
|
2018-01-16 05:38:12 +00:00
|
|
|
pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
|
2013-05-17 10:10:26 +00:00
|
|
|
self.parse_sess.span_diagnostic.span_bug(sp, msg);
|
|
|
|
}
|
2017-09-02 16:13:25 +00:00
|
|
|
pub fn trace_macros_diag(&mut self) {
|
2017-05-06 04:49:59 +00:00
|
|
|
for (sp, notes) in self.expansions.iter() {
|
2017-05-12 18:05:39 +00:00
|
|
|
let mut db = self.parse_sess.span_diagnostic.span_note_diag(*sp, "trace_macro");
|
2017-05-06 04:49:59 +00:00
|
|
|
for note in notes {
|
2017-05-12 18:05:39 +00:00
|
|
|
db.note(note);
|
2017-05-06 04:49:59 +00:00
|
|
|
}
|
|
|
|
db.emit();
|
|
|
|
}
|
2017-09-02 16:13:25 +00:00
|
|
|
// Fixme: does this result in errors?
|
|
|
|
self.expansions.clear();
|
2017-04-24 23:27:07 +00:00
|
|
|
}
|
2013-05-31 22:17:22 +00:00
|
|
|
pub fn bug(&self, msg: &str) -> ! {
|
2015-12-13 22:17:55 +00:00
|
|
|
self.parse_sess.span_diagnostic.bug(msg);
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
2013-05-31 22:17:22 +00:00
|
|
|
pub fn trace_macros(&self) -> bool {
|
2015-04-14 13:36:38 +00:00
|
|
|
self.ecfg.trace_mac
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
2013-12-29 05:35:38 +00:00
|
|
|
pub fn set_trace_macros(&mut self, x: bool) {
|
2015-04-14 13:36:38 +00:00
|
|
|
self.ecfg.trace_mac = x
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
2013-09-02 00:50:59 +00:00
|
|
|
pub fn ident_of(&self, st: &str) -> ast::Ident {
|
2016-11-16 08:21:52 +00:00
|
|
|
ast::Ident::from_str(st)
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
2015-07-30 00:01:14 +00:00
|
|
|
pub fn std_path(&self, components: &[&str]) -> Vec<ast::Ident> {
|
2018-03-17 23:57:23 +00:00
|
|
|
let def_site = DUMMY_SP.apply_mark(self.current_expansion.mark);
|
|
|
|
iter::once(Ident::new(keywords::DollarCrate.name(), def_site))
|
2017-12-06 18:50:55 +00:00
|
|
|
.chain(components.iter().map(|s| self.ident_of(s)))
|
|
|
|
.collect()
|
2014-09-07 21:57:26 +00:00
|
|
|
}
|
2014-07-06 08:17:59 +00:00
|
|
|
pub fn name_of(&self, st: &str) -> ast::Name {
|
2016-11-16 08:21:52 +00:00
|
|
|
Symbol::intern(st)
|
2014-07-06 08:17:59 +00:00
|
|
|
}
|
2017-05-11 08:26:07 +00:00
|
|
|
|
|
|
|
pub fn check_unused_macros(&self) {
|
|
|
|
self.resolver.check_unused_macros();
|
|
|
|
}
|
2013-05-17 10:10:26 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 21:38:44 +00:00
|
|
|
/// Extract a string literal from the macro expanded version of `expr`,
|
|
|
|
/// emitting `err_msg` if `expr` is not a string literal. This does not stop
|
|
|
|
/// compilation on error, merely emits a non-fatal error and returns None.
|
2016-09-02 22:01:35 +00:00
|
|
|
pub fn expr_to_spanned_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
|
2016-11-16 10:52:37 +00:00
|
|
|
-> Option<Spanned<(Symbol, ast::StrStyle)>> {
|
2017-03-17 04:04:41 +00:00
|
|
|
// Update `expr.span`'s ctxt now in case expr is an `include!` macro invocation.
|
2016-07-14 08:55:48 +00:00
|
|
|
let expr = expr.map(|mut expr| {
|
2018-03-18 20:51:53 +00:00
|
|
|
expr.span = expr.span.apply_mark(cx.current_expansion.mark);
|
2016-07-14 08:55:48 +00:00
|
|
|
expr
|
|
|
|
});
|
|
|
|
|
2014-03-02 21:38:44 +00:00
|
|
|
// we want to be able to handle e.g. concat("foo", "bar")
|
2014-07-20 14:25:35 +00:00
|
|
|
let expr = cx.expander().fold_expr(expr);
|
2012-08-06 19:34:08 +00:00
|
|
|
match expr.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Lit(ref l) => match l.node {
|
2016-11-16 10:52:37 +00:00
|
|
|
ast::LitKind::Str(s, style) => return Some(respan(expr.span, (s, style))),
|
2014-01-17 14:53:10 +00:00
|
|
|
_ => cx.span_err(l.span, err_msg)
|
2014-01-09 13:05:33 +00:00
|
|
|
},
|
2014-01-17 14:53:10 +00:00
|
|
|
_ => cx.span_err(expr.span, err_msg)
|
2011-06-21 00:26:17 +00:00
|
|
|
}
|
2014-01-17 14:53:10 +00:00
|
|
|
None
|
2011-06-21 00:26:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 22:01:35 +00:00
|
|
|
pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
|
2016-11-16 10:52:37 +00:00
|
|
|
-> Option<(Symbol, ast::StrStyle)> {
|
2016-09-02 22:01:35 +00:00
|
|
|
expr_to_spanned_string(cx, expr, err_msg).map(|s| s.node)
|
|
|
|
}
|
|
|
|
|
2014-01-17 14:53:10 +00:00
|
|
|
/// Non-fatally assert that `tts` is empty. Note that this function
|
|
|
|
/// returns even when `tts` is non-empty, macros that *need* to stop
|
|
|
|
/// compilation should call
|
|
|
|
/// `cx.parse_sess.span_diagnostic.abort_if_errors()` (this should be
|
|
|
|
/// done as rarely as possible).
|
2014-01-10 22:02:36 +00:00
|
|
|
pub fn check_zero_tts(cx: &ExtCtxt,
|
|
|
|
sp: Span,
|
2016-06-20 15:49:33 +00:00
|
|
|
tts: &[tokenstream::TokenTree],
|
2013-01-29 22:41:40 +00:00
|
|
|
name: &str) {
|
2015-03-24 23:54:09 +00:00
|
|
|
if !tts.is_empty() {
|
2015-02-20 19:08:14 +00:00
|
|
|
cx.span_err(sp, &format!("{} takes no arguments", name));
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
2012-05-14 22:32:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 14:32:26 +00:00
|
|
|
/// Interpreting `tts` as a comma-separated sequence of expressions,
|
|
|
|
/// expect exactly one string literal, or emit an error and return None.
|
2014-10-21 06:04:16 +00:00
|
|
|
pub fn get_single_str_from_tts(cx: &mut ExtCtxt,
|
2013-08-31 16:13:04 +00:00
|
|
|
sp: Span,
|
2016-06-20 15:49:33 +00:00
|
|
|
tts: &[tokenstream::TokenTree],
|
2013-08-07 16:47:28 +00:00
|
|
|
name: &str)
|
2014-05-22 23:57:53 +00:00
|
|
|
-> Option<String> {
|
2014-10-21 06:04:16 +00:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
2014-11-03 07:10:09 +00:00
|
|
|
if p.token == token::Eof {
|
2015-02-20 19:08:14 +00:00
|
|
|
cx.span_err(sp, &format!("{} takes 1 argument", name));
|
2014-11-03 07:10:09 +00:00
|
|
|
return None
|
|
|
|
}
|
2016-09-02 22:01:35 +00:00
|
|
|
let ret = panictry!(p.parse_expr());
|
2018-02-07 14:32:26 +00:00
|
|
|
let _ = p.eat(&token::Comma);
|
|
|
|
|
2014-10-21 06:04:16 +00:00
|
|
|
if p.token != token::Eof {
|
2015-02-20 19:08:14 +00:00
|
|
|
cx.span_err(sp, &format!("{} takes 1 argument", name));
|
2012-02-01 06:50:12 +00:00
|
|
|
}
|
2014-10-21 06:04:16 +00:00
|
|
|
expr_to_string(cx, ret, "argument must be a string literal").map(|(s, _)| {
|
2015-02-03 22:31:06 +00:00
|
|
|
s.to_string()
|
2014-10-21 06:04:16 +00:00
|
|
|
})
|
2012-02-01 06:50:12 +00:00
|
|
|
}
|
2011-06-21 00:26:17 +00:00
|
|
|
|
2014-01-17 14:53:10 +00:00
|
|
|
/// Extract comma-separated expressions from `tts`. If there is a
|
|
|
|
/// parsing error, emit a non-fatal error and return None.
|
2014-03-02 21:38:44 +00:00
|
|
|
pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
|
2013-08-31 16:13:04 +00:00
|
|
|
sp: Span,
|
2016-06-20 15:49:33 +00:00
|
|
|
tts: &[tokenstream::TokenTree]) -> Option<Vec<P<ast::Expr>>> {
|
2014-07-03 09:42:24 +00:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut es = Vec::new();
|
2014-10-27 08:22:52 +00:00
|
|
|
while p.token != token::Eof {
|
2015-11-11 00:08:26 +00:00
|
|
|
es.push(cx.expander().fold_expr(panictry!(p.parse_expr())));
|
2015-12-30 23:11:53 +00:00
|
|
|
if p.eat(&token::Comma) {
|
2014-06-23 15:51:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
if p.token != token::Eof {
|
2014-01-17 14:53:10 +00:00
|
|
|
cx.span_err(sp, "expected token: `,`");
|
|
|
|
return None;
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
2012-07-13 00:59:59 +00:00
|
|
|
}
|
2014-01-17 14:53:10 +00:00
|
|
|
Some(es)
|
2012-07-13 00:59:59 +00:00
|
|
|
}
|