2014-01-30 19:05:04 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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.
|
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::SyntaxExtension::*;
|
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2013-05-08 15:27:29 -07:00
|
|
|
use ast::Name;
|
2012-12-23 17:41:37 -05:00
|
|
|
use codemap;
|
2014-09-17 19:01:33 +03:00
|
|
|
use codemap::{CodeMap, Span, ExpnId, ExpnInfo, NO_EXPANSION};
|
2012-12-23 17:41:37 -05:00
|
|
|
use ext;
|
2013-10-05 21:15:46 -07:00
|
|
|
use ext::expand;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse;
|
2014-07-03 11:42:24 +02:00
|
|
|
use parse::parser;
|
2013-03-26 16:38:07 -04:00
|
|
|
use parse::token;
|
2014-01-21 10:08:10 -08:00
|
|
|
use parse::token::{InternedString, intern, str_to_ident};
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2013-11-24 23:08:53 -08:00
|
|
|
use util::small_vector::SmallVector;
|
2014-07-02 22:38:30 -07:00
|
|
|
use ext::mtwt;
|
2014-07-20 16:25:35 +02:00
|
|
|
use fold::Folder;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2014-05-29 19:03:06 -07:00
|
|
|
use std::collections::HashMap;
|
2014-07-19 21:34:24 +02:00
|
|
|
use std::rc::Rc;
|
2011-06-04 15:41:45 -04:00
|
|
|
|
2012-07-27 17:42:32 -07:00
|
|
|
// new-style macro! tt code:
|
|
|
|
//
|
2013-05-28 14:53:38 -07:00
|
|
|
// MacResult, NormalTT, IdentTT
|
2012-07-27 17:42:32 -07:00
|
|
|
//
|
2014-01-09 15:05:33 +02:00
|
|
|
// also note that ast::Mac used to have a bunch of extraneous cases and
|
2012-12-12 12:25:40 -08:00
|
|
|
// is now probably a redundant AST node, can be merged with
|
2014-01-09 15:05:33 +02:00
|
|
|
// ast::MacInvocTT.
|
2012-07-27 17:42:32 -07:00
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub struct MacroDef {
|
2014-05-22 16:57:53 -07:00
|
|
|
pub name: String,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ext: SyntaxExtension
|
2013-01-17 08:55:28 -08:00
|
|
|
}
|
2012-07-27 17:42:32 -07:00
|
|
|
|
2014-09-09 23:57:14 -07:00
|
|
|
pub trait ItemDecorator {
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: &ast::Item,
|
|
|
|
push: |P<ast::Item>|);
|
2014-09-09 23:57:14 -07:00
|
|
|
}
|
|
|
|
|
2014-11-26 05:52:16 -05:00
|
|
|
impl<F> ItemDecorator for F
|
|
|
|
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, |P<ast::Item>|)
|
|
|
|
{
|
2014-09-09 23:57:14 -07:00
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: &ast::Item,
|
|
|
|
push: |P<ast::Item>|) {
|
2014-11-26 05:52:16 -05:00
|
|
|
(*self)(ecx, sp, meta_item, item, push)
|
2014-09-09 23:57:14 -07:00
|
|
|
}
|
|
|
|
}
|
2012-07-06 14:29:50 -07:00
|
|
|
|
2014-09-09 23:57:14 -07:00
|
|
|
pub trait ItemModifier {
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: P<ast::Item>)
|
|
|
|
-> P<ast::Item>;
|
2014-09-09 23:57:14 -07:00
|
|
|
}
|
|
|
|
|
2014-11-26 05:52:16 -05:00
|
|
|
impl<F> ItemModifier for F
|
|
|
|
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item>
|
|
|
|
{
|
2014-09-09 23:57:14 -07:00
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: P<ast::Item>)
|
|
|
|
-> P<ast::Item> {
|
2014-11-26 05:52:16 -05:00
|
|
|
(*self)(ecx, span, meta_item, item)
|
2014-09-09 23:57:14 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-27 23:49:25 -08:00
|
|
|
|
2014-07-10 12:09:56 -07:00
|
|
|
/// Represents a thing that maps token trees to Macro Results
|
|
|
|
pub trait TTMacroExpander {
|
2014-08-27 21:46:52 -04:00
|
|
|
fn expand<'cx>(&self,
|
|
|
|
ecx: &'cx mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
token_tree: &[ast::TokenTree])
|
|
|
|
-> Box<MacResult+'cx>;
|
2013-08-30 14:40:05 -07:00
|
|
|
}
|
|
|
|
|
2014-01-25 13:34:26 -08:00
|
|
|
pub type MacroExpanderFn =
|
2014-11-13 08:59:44 -05:00
|
|
|
for<'cx> fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box<MacResult+'cx>;
|
2013-08-30 14:40:05 -07:00
|
|
|
|
2014-11-26 05:52:16 -05:00
|
|
|
impl<F> TTMacroExpander for F
|
|
|
|
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box<MacResult+'cx>
|
|
|
|
{
|
2014-08-27 21:46:52 -04:00
|
|
|
fn expand<'cx>(&self,
|
|
|
|
ecx: &'cx mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
token_tree: &[ast::TokenTree])
|
|
|
|
-> Box<MacResult+'cx> {
|
2014-11-26 05:52:16 -05:00
|
|
|
(*self)(ecx, span, token_tree)
|
2013-08-30 14:40:05 -07:00
|
|
|
}
|
|
|
|
}
|
2013-07-08 15:55:14 -07:00
|
|
|
|
2014-01-25 13:34:26 -08:00
|
|
|
pub trait IdentMacroExpander {
|
2014-08-27 21:46:52 -04:00
|
|
|
fn expand<'cx>(&self,
|
|
|
|
cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
ident: ast::Ident,
|
|
|
|
token_tree: Vec<ast::TokenTree> )
|
|
|
|
-> Box<MacResult+'cx>;
|
2013-08-30 14:40:05 -07:00
|
|
|
}
|
|
|
|
|
2014-09-10 20:59:26 -07:00
|
|
|
pub type IdentMacroExpanderFn =
|
2014-11-13 08:59:44 -05:00
|
|
|
for<'cx> fn(&'cx mut ExtCtxt, Span, ast::Ident, Vec<ast::TokenTree>) -> Box<MacResult+'cx>;
|
2014-09-10 20:59:26 -07:00
|
|
|
|
2014-11-26 05:52:16 -05:00
|
|
|
impl<F> IdentMacroExpander for F
|
|
|
|
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, ast::Ident,
|
|
|
|
Vec<ast::TokenTree>) -> Box<MacResult+'cx>
|
|
|
|
{
|
2014-08-27 21:46:52 -04:00
|
|
|
fn expand<'cx>(&self,
|
|
|
|
cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
ident: ast::Ident,
|
|
|
|
token_tree: Vec<ast::TokenTree> )
|
2014-11-26 05:52:16 -05:00
|
|
|
-> Box<MacResult+'cx>
|
|
|
|
{
|
|
|
|
(*self)(cx, sp, ident, token_tree)
|
2013-08-30 14:40:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 22:00:14 +10:00
|
|
|
/// The result of a macro expansion. The return values of the various
|
|
|
|
/// methods are spliced into the AST at the callsite of the macro (or
|
|
|
|
/// just into the compiler's internal macro table, for `make_def`).
|
|
|
|
pub trait MacResult {
|
2014-09-13 19:06:01 +03:00
|
|
|
/// Attempt to define a new macro.
|
2014-07-19 21:34:24 +02:00
|
|
|
// this should go away; the idea that a macro might expand into
|
|
|
|
// either a macro definition or an expression, depending on what
|
|
|
|
// the context wants, is kind of silly.
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_def(&mut self) -> Option<MacroDef> {
|
2014-04-15 22:00:14 +10:00
|
|
|
None
|
|
|
|
}
|
|
|
|
/// Create an expression.
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
2014-04-15 22:00:14 +10:00
|
|
|
None
|
|
|
|
}
|
|
|
|
/// Create zero or more items.
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_items(self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> {
|
2014-04-15 22:00:14 +10:00
|
|
|
None
|
|
|
|
}
|
2014-07-10 17:46:09 -07:00
|
|
|
|
|
|
|
/// Create zero or more methods.
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_methods(self: Box<Self>) -> Option<SmallVector<P<ast::Method>>> {
|
2014-07-10 17:46:09 -07:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2014-05-19 13:32:51 -07:00
|
|
|
/// Create a pattern.
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> {
|
2014-05-19 13:32:51 -07:00
|
|
|
None
|
|
|
|
}
|
2014-04-15 22:00:14 +10:00
|
|
|
|
|
|
|
/// Create a statement.
|
|
|
|
///
|
|
|
|
/// By default this attempts to create an expression statement,
|
|
|
|
/// returning None if that fails.
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> {
|
2014-04-15 22:00:14 +10:00
|
|
|
self.make_expr()
|
2014-09-13 19:06:01 +03:00
|
|
|
.map(|e| P(codemap::respan(e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID))))
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
2013-08-30 14:40:05 -07:00
|
|
|
}
|
2013-07-08 15:55:14 -07:00
|
|
|
|
2014-04-15 22:00:14 +10:00
|
|
|
/// A convenience type for macros that return a single expression.
|
|
|
|
pub struct MacExpr {
|
2014-09-13 19:06:01 +03:00
|
|
|
e: P<ast::Expr>
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
|
|
|
impl MacExpr {
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn new(e: P<ast::Expr>) -> Box<MacResult+'static> {
|
2014-08-27 21:46:52 -04:00
|
|
|
box MacExpr { e: e } as Box<MacResult+'static>
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MacResult for MacExpr {
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_expr(self: Box<MacExpr>) -> Option<P<ast::Expr>> {
|
2014-04-15 22:00:14 +10:00
|
|
|
Some(self.e)
|
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_pat(self: Box<MacExpr>) -> Option<P<ast::Pat>> {
|
2014-08-31 01:45:11 +03:00
|
|
|
match self.e.node {
|
2014-09-13 19:06:01 +03:00
|
|
|
ast::ExprLit(_) => Some(P(ast::Pat {
|
2014-08-31 01:45:11 +03:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-09-13 19:06:01 +03:00
|
|
|
span: self.e.span,
|
|
|
|
node: ast::PatLit(self.e)
|
|
|
|
})),
|
2014-08-31 01:45:11 +03:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
2014-05-19 13:32:51 -07:00
|
|
|
/// A convenience type for macros that return a single pattern.
|
|
|
|
pub struct MacPat {
|
2014-09-13 19:06:01 +03:00
|
|
|
p: P<ast::Pat>
|
2014-05-19 13:32:51 -07:00
|
|
|
}
|
|
|
|
impl MacPat {
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn new(p: P<ast::Pat>) -> Box<MacResult+'static> {
|
2014-08-27 21:46:52 -04:00
|
|
|
box MacPat { p: p } as Box<MacResult+'static>
|
2014-05-19 13:32:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MacResult for MacPat {
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_pat(self: Box<MacPat>) -> Option<P<ast::Pat>> {
|
2014-05-19 13:32:51 -07:00
|
|
|
Some(self.p)
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 12:55:00 +02:00
|
|
|
/// A type for macros that return multiple items.
|
|
|
|
pub struct MacItems {
|
|
|
|
items: SmallVector<P<ast::Item>>
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
2014-09-13 12:55:00 +02:00
|
|
|
|
|
|
|
impl MacItems {
|
2014-11-06 09:32:37 -08:00
|
|
|
pub fn new<I: Iterator<P<ast::Item>>>(it: I) -> Box<MacResult+'static> {
|
2014-09-13 12:55:00 +02:00
|
|
|
box MacItems { items: it.collect() } as Box<MacResult+'static>
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 12:55:00 +02:00
|
|
|
|
|
|
|
impl MacResult for MacItems {
|
|
|
|
fn make_items(self: Box<MacItems>) -> Option<SmallVector<P<ast::Item>>> {
|
|
|
|
Some(self.items)
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
|
|
|
}
|
2014-02-18 16:14:12 +00:00
|
|
|
|
2014-04-15 22:00:14 +10:00
|
|
|
/// Fill-in macro expansion result, to allow compilation to continue
|
|
|
|
/// after hitting errors.
|
2014-12-14 23:32:24 -05:00
|
|
|
#[deriving(Copy)]
|
2014-04-15 22:00:14 +10:00
|
|
|
pub struct DummyResult {
|
|
|
|
expr_only: bool,
|
|
|
|
span: Span
|
2012-07-06 14:29:50 -07:00
|
|
|
}
|
2014-04-15 22:00:14 +10: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-27 21:46:52 -04:00
|
|
|
pub fn any(sp: Span) -> Box<MacResult+'static> {
|
|
|
|
box DummyResult { expr_only: false, span: sp } as Box<MacResult+'static>
|
2014-04-15 22:00:14 +10: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 00:00:52 -04:00
|
|
|
/// if an error is encountered internally, the user will receive
|
2014-04-15 22:00:14 +10:00
|
|
|
/// an error that they also used it in the wrong place.
|
2014-08-27 21:46:52 -04:00
|
|
|
pub fn expr(sp: Span) -> Box<MacResult+'static> {
|
|
|
|
box DummyResult { expr_only: true, span: sp } as Box<MacResult+'static>
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A plain dummy expression.
|
2014-09-13 19:06:01 +03: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,
|
2014-11-09 16:14:15 +01:00
|
|
|
node: ast::ExprLit(P(codemap::respan(sp, ast::LitBool(false)))),
|
2014-03-18 23:14:08 +11:00
|
|
|
span: sp,
|
2014-09-13 19:06:01 +03:00
|
|
|
})
|
2014-02-18 16:14:12 +00:00
|
|
|
}
|
2014-05-19 13:32:51 -07:00
|
|
|
|
|
|
|
/// A plain dummy pattern.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn raw_pat(sp: Span) -> ast::Pat {
|
|
|
|
ast::Pat {
|
2014-05-19 13:32:51 -07:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-08-06 17:04:44 +02:00
|
|
|
node: ast::PatWild(ast::PatWildSingle),
|
2014-05-19 13:32:51 -07:00
|
|
|
span: sp,
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 17:46:09 -07:00
|
|
|
|
2014-03-18 23:14:08 +11:00
|
|
|
}
|
2014-04-15 22:00:14 +10:00
|
|
|
|
|
|
|
impl MacResult for DummyResult {
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_expr(self: Box<DummyResult>) -> Option<P<ast::Expr>> {
|
2014-04-15 22:00:14 +10:00
|
|
|
Some(DummyResult::raw_expr(self.span))
|
2014-03-18 23:14:08 +11:00
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> {
|
|
|
|
Some(P(DummyResult::raw_pat(self.span)))
|
2014-05-19 13:32:51 -07:00
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_items(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Item>>> {
|
2014-07-10 17:46:09 -07:00
|
|
|
// this code needs a comment... why not always just return the Some() ?
|
|
|
|
if self.expr_only {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(SmallVector::zero())
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_methods(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Method>>> {
|
2014-04-15 22:00:14 +10:00
|
|
|
if self.expr_only {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(SmallVector::zero())
|
|
|
|
}
|
2014-03-18 23:14:08 +11:00
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
fn make_stmt(self: Box<DummyResult>) -> Option<P<ast::Stmt>> {
|
|
|
|
Some(P(codemap::respan(self.span,
|
|
|
|
ast::StmtExpr(DummyResult::raw_expr(self.span),
|
|
|
|
ast::DUMMY_NODE_ID))))
|
2014-03-18 23:14:08 +11:00
|
|
|
}
|
2014-01-18 01:53:10 +11:00
|
|
|
}
|
2012-07-05 12:10:33 -07:00
|
|
|
|
2014-02-27 23:49:25 -08:00
|
|
|
/// An enum representing the different kinds of syntax extensions.
|
2013-01-29 14:41:40 -08:00
|
|
|
pub enum SyntaxExtension {
|
2014-02-27 23:49:25 -08:00
|
|
|
/// A syntax extension that is attached to an item and creates new items
|
|
|
|
/// based upon it.
|
|
|
|
///
|
|
|
|
/// `#[deriving(...)]` is an `ItemDecorator`.
|
2014-09-11 17:07:49 +12:00
|
|
|
Decorator(Box<ItemDecorator + 'static>),
|
2012-06-25 15:04:50 -07:00
|
|
|
|
2014-02-27 23:49:25 -08:00
|
|
|
/// A syntax extension that is attached to an item and modifies it
|
|
|
|
/// in-place.
|
2014-09-11 17:07:49 +12:00
|
|
|
Modifier(Box<ItemModifier + 'static>),
|
2012-11-08 23:12:45 -05:00
|
|
|
|
2014-02-27 23:49:25 -08:00
|
|
|
/// A normal, function-like syntax extension.
|
|
|
|
///
|
|
|
|
/// `bytes!` is a `NormalTT`.
|
2014-07-10 12:09:56 -07:00
|
|
|
NormalTT(Box<TTMacroExpander + 'static>, Option<Span>),
|
2013-02-26 10:15:29 -08:00
|
|
|
|
2014-02-27 23:49:25 -08:00
|
|
|
/// A function-like syntax extension that has an extra ident before
|
|
|
|
/// the block.
|
|
|
|
///
|
2014-06-14 11:03:34 -07:00
|
|
|
IdentTT(Box<IdentMacroExpander + 'static>, Option<Span>),
|
2014-07-07 09:54:08 -07:00
|
|
|
|
|
|
|
/// An ident macro that has two properties:
|
|
|
|
/// - it adds a macro definition to the environment, and
|
|
|
|
/// - the definition it adds doesn't introduce any new
|
|
|
|
/// identifiers.
|
|
|
|
///
|
|
|
|
/// `macro_rules!` is a LetSyntaxTT
|
|
|
|
LetSyntaxTT(Box<IdentMacroExpander + 'static>, Option<Span>),
|
2011-06-20 17:26:17 -07:00
|
|
|
}
|
2011-06-04 15:41:45 -04:00
|
|
|
|
2014-05-24 16:16:10 -07:00
|
|
|
pub type NamedSyntaxExtension = (Name, SyntaxExtension);
|
|
|
|
|
2013-05-08 15:27:29 -07:00
|
|
|
pub struct BlockInfo {
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Should macros escape from this scope?
|
2014-03-27 15:39:48 -07:00
|
|
|
pub macros_escape: bool,
|
2014-06-09 13:12:30 -07:00
|
|
|
/// What are the pending renames?
|
2014-07-02 22:38:30 -07:00
|
|
|
pub pending_renames: mtwt::RenameList,
|
2013-12-30 17:45:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockInfo {
|
|
|
|
pub fn new() -> BlockInfo {
|
|
|
|
BlockInfo {
|
|
|
|
macros_escape: false,
|
2014-02-28 13:09:09 -08:00
|
|
|
pending_renames: Vec::new(),
|
2013-12-30 17:45:39 -08:00
|
|
|
}
|
|
|
|
}
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
2013-02-13 20:08:35 -08:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// The base map of methods for expanding syntax extension
|
|
|
|
/// AST nodes into full ASTs
|
2014-09-26 17:14:23 -07:00
|
|
|
fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
|
2013-02-04 13:15:17 -08:00
|
|
|
// utility function to simplify creating NormalTT syntax extensions
|
2014-01-25 13:34:26 -08:00
|
|
|
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
|
2014-09-10 20:59:26 -07:00
|
|
|
NormalTT(box f, None)
|
2012-10-07 14:56:18 -07:00
|
|
|
}
|
2013-12-08 02:55:28 -05:00
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
let mut syntax_expanders = SyntaxEnv::new();
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("macro_rules"),
|
2014-09-10 20:59:26 -07:00
|
|
|
LetSyntaxTT(box ext::tt::macro_rules::add_new_extension, None));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("fmt"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::fmt::expand_syntax_ext));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("format_args"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 13:53:40 -07:00
|
|
|
ext::format::expand_format_args));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("env"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::env::expand_env));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("option_env"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::env::expand_option_env));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern("bytes"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::bytes::expand_syntax_ext));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern("concat_idents"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::concat_idents::expand_syntax_ext));
|
2013-10-05 21:15:46 -07:00
|
|
|
syntax_expanders.insert(intern("concat"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-10-05 21:15:46 -07:00
|
|
|
ext::concat::expand_syntax_ext));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("log_syntax"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::log_syntax::expand_syntax_ext));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("deriving"),
|
2014-09-11 17:07:49 +12:00
|
|
|
Decorator(box ext::deriving::expand_meta_deriving));
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2014-09-26 17:14:23 -07:00
|
|
|
if ecfg.enable_quotes {
|
|
|
|
// Quasi-quoting expanders
|
|
|
|
syntax_expanders.insert(intern("quote_tokens"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_tokens));
|
|
|
|
syntax_expanders.insert(intern("quote_expr"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_expr));
|
|
|
|
syntax_expanders.insert(intern("quote_ty"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_ty));
|
|
|
|
syntax_expanders.insert(intern("quote_method"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_method));
|
|
|
|
syntax_expanders.insert(intern("quote_item"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_item));
|
|
|
|
syntax_expanders.insert(intern("quote_pat"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_pat));
|
|
|
|
syntax_expanders.insert(intern("quote_arm"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_arm));
|
|
|
|
syntax_expanders.insert(intern("quote_stmt"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_stmt));
|
|
|
|
}
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("line"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_line));
|
2014-11-18 23:03:58 +11:00
|
|
|
syntax_expanders.insert(intern("column"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2014-11-18 23:03:58 +11:00
|
|
|
ext::source_util::expand_column));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("file"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_file));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("stringify"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_stringify));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("include"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_include));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("include_str"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_include_str));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("include_bin"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_include_bin));
|
2014-12-22 10:57:09 +13:00
|
|
|
syntax_expanders.insert(intern("include_bytes"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::source_util::expand_include_bytes));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("module_path"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_mod));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("asm"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::asm::expand_asm));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("cfg"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::cfg::expand_cfg));
|
Add a cfg_attr syntax extension
This extends cfg-gating to attributes.
```rust
#[cfg_attr(<cfg pattern>, <attr>)]
```
will expand to
```rust
#[<attr>]
```
if the `<cfg pattern>` matches the current cfg environment, and nothing
if it does not. The grammar for the cfg pattern has a simple
recursive structure:
* `value` and `key = "value"` are cfg patterns,
* `not(<cfg pattern>)` is a cfg pattern and matches if `<cfg pattern>`
does not.
* `all(<cfg pattern>, ...)` is a cfg pattern and matches if all of the
`<cfg pattern>`s do.
* `any(<cfg pattern>, ...)` is a cfg pattern and matches if any of the
`<cfg pattern>`s do.
Examples:
```rust
// only derive Show for assert_eq! in tests
#[cfg_attr(test, deriving(Show))]
struct Foo { ... }
// only derive Show for assert_eq! in tests and debug builds
#[cfg_attr(any(test, not(ndebug)), deriving(Show))]
struct Foo { ... }
// ignore a test in certain cases
#[test]
#[cfg_attr(all(not(target_os = "linux"), target_endian = "big"), ignore)]
fn test_broken_thing() { ... }
// Avoid duplication when fixing staging issues in rustc
#[cfg_attr(not(stage0), lang="iter")]
pub trait Iterator<T> { ... }
```
2014-08-03 14:25:30 -07:00
|
|
|
syntax_expanders.insert(intern("cfg_attr"),
|
2014-09-24 00:35:42 -07:00
|
|
|
Modifier(box ext::cfg_attr::expand));
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("trace_macros"),
|
2014-01-25 13:34:26 -08:00
|
|
|
builtin_normal_expander(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::trace_macros::expand_trace_macros));
|
2013-12-30 17:45:39 -08:00
|
|
|
syntax_expanders
|
2012-10-07 14:56:18 -07:00
|
|
|
}
|
2012-07-27 17:42:32 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// One of these is made during expansion and incrementally updated as we go;
|
|
|
|
/// when a macro expansion occurs, the resulting nodes have the backtrace()
|
|
|
|
/// -> expn_info of their expansion context stored into their span.
|
2013-12-25 11:10:33 -07:00
|
|
|
pub struct ExtCtxt<'a> {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub parse_sess: &'a parse::ParseSess,
|
|
|
|
pub cfg: ast::CrateConfig,
|
2014-09-17 19:01:33 +03:00
|
|
|
pub backtrace: ExpnId,
|
2014-05-24 16:16:10 -07:00
|
|
|
pub ecfg: expand::ExpansionConfig,
|
2011-07-10 17:00:28 -07:00
|
|
|
|
2014-03-27 15:39:48 -07:00
|
|
|
pub mod_path: Vec<ast::Ident> ,
|
|
|
|
pub trace_mac: bool,
|
2014-09-13 19:06:01 +03:00
|
|
|
pub exported_macros: Vec<P<ast::Item>>,
|
2014-07-19 21:34:24 +02:00
|
|
|
|
2014-07-20 16:25:35 +02:00
|
|
|
pub syntax_env: SyntaxEnv,
|
2014-10-01 11:38:54 +02:00
|
|
|
pub recursion_count: uint,
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
2013-03-15 15:24:24 -04:00
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
impl<'a> ExtCtxt<'a> {
|
2014-12-12 11:09:32 -05:00
|
|
|
pub fn new(parse_sess: &'a parse::ParseSess, cfg: ast::CrateConfig,
|
|
|
|
ecfg: expand::ExpansionConfig) -> ExtCtxt<'a> {
|
2014-09-26 17:14:23 -07:00
|
|
|
let env = initial_syntax_expander_table(&ecfg);
|
2013-12-27 17:17:36 -07:00
|
|
|
ExtCtxt {
|
2013-05-17 21:27:17 +10:00
|
|
|
parse_sess: parse_sess,
|
|
|
|
cfg: cfg,
|
2014-09-17 19:01:33 +03:00
|
|
|
backtrace: NO_EXPANSION,
|
2014-02-28 13:09:09 -08:00
|
|
|
mod_path: Vec::new(),
|
2014-02-28 23:17:38 -08:00
|
|
|
ecfg: ecfg,
|
2014-07-10 15:41:11 -07:00
|
|
|
trace_mac: false,
|
|
|
|
exported_macros: Vec::new(),
|
2014-09-26 17:14:23 -07:00
|
|
|
syntax_env: env,
|
2014-10-01 11:38:54 +02:00
|
|
|
recursion_count: 0,
|
2013-05-17 21:27:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 16:25:35 +02:00
|
|
|
#[deprecated = "Replaced with `expander().fold_expr()`"]
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn expand_expr(&mut self, e: P<ast::Expr>) -> P<ast::Expr> {
|
2014-07-20 16:25:35 +02:00
|
|
|
self.expander().fold_expr(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `Folder` for deeply expanding all macros in a AST node.
|
|
|
|
pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
|
2014-12-14 15:42:41 +13:00
|
|
|
expand::MacroExpander::new(self)
|
2013-10-05 21:15:46 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 11:42:24 +02:00
|
|
|
pub fn new_parser_from_tts(&self, tts: &[ast::TokenTree])
|
|
|
|
-> parser::Parser<'a> {
|
2014-10-14 23:05:01 -07:00
|
|
|
parse::tts_to_parser(self.parse_sess, tts.to_vec(), self.cfg())
|
2014-07-03 11:42:24 +02:00
|
|
|
}
|
|
|
|
|
2014-03-16 20:56:24 +02:00
|
|
|
pub fn codemap(&self) -> &'a CodeMap { &self.parse_sess.span_diagnostic.cm }
|
2014-03-09 16:54:34 +02:00
|
|
|
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
|
2013-07-19 07:38:55 +02:00
|
|
|
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn call_site(&self) -> Span {
|
2014-09-17 19:01:33 +03:00
|
|
|
self.codemap().with_expn_info(self.backtrace, |ei| match ei {
|
2014-01-03 15:08:48 -08:00
|
|
|
Some(expn_info) => expn_info.call_site,
|
2013-05-17 20:10:26 +10:00
|
|
|
None => self.bug("missing top span")
|
2014-09-17 19:01:33 +03:00
|
|
|
})
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn print_backtrace(&self) { }
|
2014-09-17 19:01:33 +03:00
|
|
|
pub fn backtrace(&self) -> ExpnId { self.backtrace }
|
|
|
|
pub fn original_span(&self) -> Span {
|
|
|
|
let mut expn_id = self.backtrace;
|
|
|
|
let mut call_site = None;
|
|
|
|
loop {
|
|
|
|
match self.codemap().with_expn_info(expn_id, |ei| ei.map(|ei| ei.call_site)) {
|
|
|
|
None => break,
|
|
|
|
Some(cs) => {
|
|
|
|
call_site = Some(cs);
|
|
|
|
expn_id = cs.expn_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
call_site.expect("missing expansion backtrace")
|
|
|
|
}
|
|
|
|
pub fn original_span_in_file(&self) -> Span {
|
|
|
|
let mut expn_id = self.backtrace;
|
|
|
|
let mut call_site = None;
|
|
|
|
loop {
|
|
|
|
let expn_info = self.codemap().with_expn_info(expn_id, |ei| {
|
2014-11-27 15:00:50 -05:00
|
|
|
ei.map(|ei| (ei.call_site, ei.callee.name == "include"))
|
2014-09-17 19:01:33 +03:00
|
|
|
});
|
|
|
|
match expn_info {
|
|
|
|
None => break,
|
|
|
|
Some((cs, is_include)) => {
|
|
|
|
if is_include {
|
|
|
|
// Don't recurse into file using "include!".
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
call_site = Some(cs);
|
|
|
|
expn_id = cs.expn_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
call_site.expect("missing expansion backtrace")
|
|
|
|
}
|
|
|
|
|
2013-12-28 22:35:38 -07:00
|
|
|
pub fn mod_push(&mut self, i: ast::Ident) { self.mod_path.push(i); }
|
2013-12-23 16:20:52 +01:00
|
|
|
pub fn mod_pop(&mut self) { self.mod_path.pop().unwrap(); }
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-08 22:11:44 -08:00
|
|
|
pub fn mod_path(&self) -> Vec<ast::Ident> {
|
|
|
|
let mut v = Vec::new();
|
2014-12-10 19:46:38 -08:00
|
|
|
v.push(token::str_to_ident(self.ecfg.crate_name[]));
|
2014-03-20 14:12:56 +01:00
|
|
|
v.extend(self.mod_path.iter().map(|a| *a));
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-08 22:11:44 -08:00
|
|
|
return v;
|
|
|
|
}
|
2014-09-17 19:01:33 +03:00
|
|
|
pub fn bt_push(&mut self, ei: ExpnInfo) {
|
2014-10-01 11:38:54 +02:00
|
|
|
self.recursion_count += 1;
|
|
|
|
if self.recursion_count > self.ecfg.recursion_limit {
|
|
|
|
self.span_fatal(ei.call_site,
|
2014-10-16 21:40:21 +02:00
|
|
|
format!("recursion limit reached while expanding the macro `{}`",
|
2014-12-10 19:46:38 -08:00
|
|
|
ei.callee.name)[]);
|
2014-10-01 11:38:54 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 19:01:33 +03:00
|
|
|
let mut call_site = ei.call_site;
|
|
|
|
call_site.expn_id = self.backtrace;
|
|
|
|
self.backtrace = self.codemap().record_expansion(ExpnInfo {
|
|
|
|
call_site: call_site,
|
|
|
|
callee: ei.callee
|
|
|
|
});
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
2013-12-28 22:35:38 -07:00
|
|
|
pub fn bt_pop(&mut self) {
|
|
|
|
match self.backtrace {
|
2014-09-17 19:01:33 +03:00
|
|
|
NO_EXPANSION => self.bug("tried to pop without a push"),
|
|
|
|
expn_id => {
|
2014-10-01 11:38:54 +02:00
|
|
|
self.recursion_count -= 1;
|
2014-09-17 19:01:33 +03:00
|
|
|
self.backtrace = self.codemap().with_expn_info(expn_id, |expn_info| {
|
|
|
|
expn_info.map_or(NO_EXPANSION, |ei| ei.call_site.expn_id)
|
|
|
|
});
|
|
|
|
}
|
2012-01-13 09:32:05 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-18 01:53:10 +11:00
|
|
|
/// Emit `msg` attached to `sp`, and stop compilation immediately.
|
|
|
|
///
|
2014-07-02 21:27:07 -04:00
|
|
|
/// `span_err` should be strongly preferred where-ever possible:
|
2014-01-18 01:53:10 +11:00
|
|
|
/// this should *only* be used when
|
|
|
|
/// - 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)
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
|
2013-05-17 20:10:26 +10:00
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.span_fatal(sp, msg);
|
|
|
|
}
|
2014-01-18 01:53:10 +11: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).
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span_err(&self, sp: Span, msg: &str) {
|
2013-05-17 20:10:26 +10:00
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.span_err(sp, msg);
|
|
|
|
}
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span_warn(&self, sp: Span, msg: &str) {
|
2013-05-17 20:10:26 +10:00
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.span_warn(sp, msg);
|
|
|
|
}
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
|
2013-05-17 20:10:26 +10:00
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
|
|
|
|
}
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
|
2013-05-17 20:10:26 +10:00
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.span_bug(sp, msg);
|
|
|
|
}
|
2014-01-19 11:24:27 -08:00
|
|
|
pub fn span_note(&self, sp: Span, msg: &str) {
|
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.span_note(sp, msg);
|
|
|
|
}
|
2014-08-29 18:55:35 +12:00
|
|
|
pub fn span_help(&self, sp: Span, msg: &str) {
|
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.span_help(sp, msg);
|
|
|
|
}
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn bug(&self, msg: &str) -> ! {
|
2013-05-17 20:10:26 +10:00
|
|
|
self.print_backtrace();
|
|
|
|
self.parse_sess.span_diagnostic.handler().bug(msg);
|
|
|
|
}
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn trace_macros(&self) -> bool {
|
2013-12-28 22:35:38 -07:00
|
|
|
self.trace_mac
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
2013-12-28 22:35:38 -07:00
|
|
|
pub fn set_trace_macros(&mut self, x: bool) {
|
|
|
|
self.trace_mac = x
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
2013-09-02 02:50:59 +02:00
|
|
|
pub fn ident_of(&self, st: &str) -> ast::Ident {
|
2013-06-04 12:34:25 -07:00
|
|
|
str_to_ident(st)
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
2014-07-06 01:17:59 -07:00
|
|
|
pub fn name_of(&self, st: &str) -> ast::Name {
|
|
|
|
token::intern(st)
|
|
|
|
}
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
|
|
|
|
2014-03-02 13:38:44 -08: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.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
|
|
|
|
-> Option<(InternedString, ast::StrStyle)> {
|
2014-03-02 13:38:44 -08:00
|
|
|
// we want to be able to handle e.g. concat("foo", "bar")
|
2014-07-20 16:25:35 +02:00
|
|
|
let expr = cx.expander().fold_expr(expr);
|
2012-08-06 12:34:08 -07:00
|
|
|
match expr.node {
|
2014-09-13 19:06:01 +03:00
|
|
|
ast::ExprLit(ref l) => match l.node {
|
2014-01-21 10:08:10 -08:00
|
|
|
ast::LitStr(ref s, style) => return Some(((*s).clone(), style)),
|
2014-01-18 01:53:10 +11:00
|
|
|
_ => cx.span_err(l.span, err_msg)
|
2014-01-09 15:05:33 +02:00
|
|
|
},
|
2014-01-18 01:53:10 +11:00
|
|
|
_ => cx.span_err(expr.span, err_msg)
|
2011-06-20 17:26:17 -07:00
|
|
|
}
|
2014-01-18 01:53:10 +11:00
|
|
|
None
|
2011-06-20 17:26:17 -07:00
|
|
|
}
|
|
|
|
|
2014-01-18 01:53:10 +11: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 14:02:36 -08:00
|
|
|
pub fn check_zero_tts(cx: &ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
tts: &[ast::TokenTree],
|
2013-01-29 14:41:40 -08:00
|
|
|
name: &str) {
|
2012-12-12 17:08:09 -08:00
|
|
|
if tts.len() != 0 {
|
2014-12-10 19:46:38 -08:00
|
|
|
cx.span_err(sp, format!("{} takes no arguments", name)[]);
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
2012-05-14 15:32:32 -07:00
|
|
|
}
|
|
|
|
|
2014-01-18 01:53:10 +11:00
|
|
|
/// Extract the string literal from the first token of `tts`. If this
|
|
|
|
/// is not a string literal, emit an error and return None.
|
2014-10-20 23:04:16 -07:00
|
|
|
pub fn get_single_str_from_tts(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
tts: &[ast::TokenTree],
|
2013-08-07 09:47:28 -07:00
|
|
|
name: &str)
|
2014-05-22 16:57:53 -07:00
|
|
|
-> Option<String> {
|
2014-10-20 23:04:16 -07:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
2014-11-02 23:10:09 -08:00
|
|
|
if p.token == token::Eof {
|
2014-12-10 19:46:38 -08:00
|
|
|
cx.span_err(sp, format!("{} takes 1 argument", name)[]);
|
2014-11-02 23:10:09 -08:00
|
|
|
return None
|
|
|
|
}
|
2014-10-20 23:04:16 -07:00
|
|
|
let ret = cx.expander().fold_expr(p.parse_expr());
|
|
|
|
if p.token != token::Eof {
|
2014-12-10 19:46:38 -08:00
|
|
|
cx.span_err(sp, format!("{} takes 1 argument", name)[]);
|
2012-01-31 23:50:12 -07:00
|
|
|
}
|
2014-10-20 23:04:16 -07:00
|
|
|
expr_to_string(cx, ret, "argument must be a string literal").map(|(s, _)| {
|
|
|
|
s.get().to_string()
|
|
|
|
})
|
2012-01-31 23:50:12 -07:00
|
|
|
}
|
2011-06-20 17:26:17 -07:00
|
|
|
|
2014-01-18 01:53:10 +11:00
|
|
|
/// Extract comma-separated expressions from `tts`. If there is a
|
|
|
|
/// parsing error, emit a non-fatal error and return None.
|
2014-03-02 13:38:44 -08:00
|
|
|
pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
tts: &[ast::TokenTree]) -> Option<Vec<P<ast::Expr>>> {
|
2014-07-03 11:42:24 +02:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut es = Vec::new();
|
2014-10-27 19:22:52 +11:00
|
|
|
while p.token != token::Eof {
|
2014-07-20 16:25:35 +02:00
|
|
|
es.push(cx.expander().fold_expr(p.parse_expr()));
|
2014-10-27 19:22:52 +11:00
|
|
|
if p.eat(&token::Comma) {
|
2014-06-23 15:51:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-10-27 19:22:52 +11:00
|
|
|
if p.token != token::Eof {
|
2014-01-18 01:53:10 +11:00
|
|
|
cx.span_err(sp, "expected token: `,`");
|
|
|
|
return None;
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
2012-07-12 17:59:59 -07:00
|
|
|
}
|
2014-01-18 01:53:10 +11:00
|
|
|
Some(es)
|
2012-07-12 17:59:59 -07:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// In order to have some notion of scoping for macros,
|
|
|
|
/// we want to implement the notion of a transformation
|
|
|
|
/// environment.
|
2014-07-19 21:34:24 +02:00
|
|
|
///
|
2014-06-09 13:12:30 -07:00
|
|
|
/// This environment maps Names to SyntaxExtensions.
|
2014-07-19 21:34:24 +02:00
|
|
|
pub struct SyntaxEnv {
|
|
|
|
chain: Vec<MapChainFrame> ,
|
|
|
|
}
|
2013-02-26 10:15:29 -08:00
|
|
|
|
2014-07-19 21:34:24 +02:00
|
|
|
// impl question: how to implement it? Initially, the
|
2013-02-26 10:15:29 -08:00
|
|
|
// env will contain only macros, so it might be painful
|
|
|
|
// to add an empty frame for every context. Let's just
|
|
|
|
// get it working, first....
|
|
|
|
|
|
|
|
// NB! the mutability of the underlying maps means that
|
|
|
|
// if expansion is out-of-order, a deeper scope may be
|
|
|
|
// able to refer to a macro that was added to an enclosing
|
|
|
|
// scope lexically later than the deeper scope.
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
struct MapChainFrame {
|
2013-12-30 17:45:39 -08:00
|
|
|
info: BlockInfo,
|
2014-07-19 21:34:24 +02:00
|
|
|
map: HashMap<Name, Rc<SyntaxExtension>>,
|
2013-12-30 17:45:39 -08:00
|
|
|
}
|
2013-02-26 10:15:29 -08:00
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
impl SyntaxEnv {
|
2014-07-19 21:34:24 +02:00
|
|
|
fn new() -> SyntaxEnv {
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut map = SyntaxEnv { chain: Vec::new() };
|
2013-12-30 17:45:39 -08:00
|
|
|
map.push_frame();
|
|
|
|
map
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
|
2013-12-30 17:45:39 -08:00
|
|
|
pub fn push_frame(&mut self) {
|
|
|
|
self.chain.push(MapChainFrame {
|
|
|
|
info: BlockInfo::new(),
|
|
|
|
map: HashMap::new(),
|
|
|
|
});
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
|
2013-12-30 17:45:39 -08:00
|
|
|
pub fn pop_frame(&mut self) {
|
|
|
|
assert!(self.chain.len() > 1, "too many pops on MapChain!");
|
|
|
|
self.chain.pop();
|
2013-04-10 13:11:27 -07:00
|
|
|
}
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
fn find_escape_frame<'a>(&'a mut self) -> &'a mut MapChainFrame {
|
2014-09-14 20:27:36 -07:00
|
|
|
for (i, frame) in self.chain.iter_mut().enumerate().rev() {
|
2013-12-30 17:45:39 -08:00
|
|
|
if !frame.info.macros_escape || i == 0 {
|
|
|
|
return frame
|
|
|
|
}
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
2013-12-30 17:45:39 -08:00
|
|
|
unreachable!()
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
|
2014-07-19 21:34:24 +02:00
|
|
|
pub fn find(&self, k: &Name) -> Option<Rc<SyntaxExtension>> {
|
2014-01-23 20:41:57 +01:00
|
|
|
for frame in self.chain.iter().rev() {
|
2014-11-06 12:25:16 -05:00
|
|
|
match frame.map.get(k) {
|
2014-07-19 21:34:24 +02:00
|
|
|
Some(v) => return Some(v.clone()),
|
2013-12-30 17:45:39 -08:00
|
|
|
None => {}
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
}
|
2013-12-30 17:45:39 -08:00
|
|
|
None
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
pub fn insert(&mut self, k: Name, v: SyntaxExtension) {
|
2014-07-19 21:34:24 +02:00
|
|
|
self.find_escape_frame().map.insert(k, Rc::new(v));
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
pub fn info<'a>(&'a mut self) -> &'a mut BlockInfo {
|
2014-02-28 12:54:01 -08:00
|
|
|
let last_chain_index = self.chain.len() - 1;
|
2014-10-23 08:42:21 -07:00
|
|
|
&mut self.chain[last_chain_index].info
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
}
|