2015-02-24 19:56:01 +01:00
|
|
|
// Copyright 2015 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;
|
2015-06-16 21:47:09 +03:00
|
|
|
use codemap::{CodeMap, Span, ExpnId, ExpnInfo, NO_EXPANSION, CompilerExpansion};
|
2012-12-23 17:41:37 -05:00
|
|
|
use ext;
|
2013-10-05 21:15:46 -07:00
|
|
|
use ext::expand;
|
2015-01-01 16:37:47 -08:00
|
|
|
use ext::tt::macro_rules;
|
2015-07-13 17:10:44 -07:00
|
|
|
use feature_gate::GatedCfg;
|
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;
|
2015-02-27 11:14:42 -08:00
|
|
|
use std::default::Default;
|
2011-06-04 15:41:45 -04:00
|
|
|
|
2014-02-27 23:49:25 -08:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Debug,Clone)]
|
2014-12-02 10:07:41 -08:00
|
|
|
pub enum Annotatable {
|
|
|
|
Item(P<ast::Item>),
|
2015-03-10 12:28:44 +02:00
|
|
|
TraitItem(P<ast::TraitItem>),
|
|
|
|
ImplItem(P<ast::ImplItem>),
|
2014-12-02 10:07:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Annotatable {
|
|
|
|
pub fn attrs(&self) -> &[ast::Attribute] {
|
|
|
|
match *self {
|
2015-02-20 14:08:14 -05:00
|
|
|
Annotatable::Item(ref i) => &i.attrs,
|
2015-03-10 12:28:44 +02:00
|
|
|
Annotatable::TraitItem(ref ti) => &ti.attrs,
|
|
|
|
Annotatable::ImplItem(ref ii) => &ii.attrs,
|
2014-12-02 10:07:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fold_attrs(self, attrs: Vec<ast::Attribute>) -> Annotatable {
|
|
|
|
match self {
|
2015-03-05 04:48:54 +02:00
|
|
|
Annotatable::Item(i) => Annotatable::Item(i.map(|i| ast::Item {
|
2014-12-02 10:07:41 -08:00
|
|
|
attrs: attrs,
|
2015-03-05 04:48:54 +02:00
|
|
|
..i
|
2014-12-02 10:07:41 -08:00
|
|
|
})),
|
2015-03-10 12:28:44 +02:00
|
|
|
Annotatable::TraitItem(i) => Annotatable::TraitItem(i.map(|ti| {
|
|
|
|
ast::TraitItem { attrs: attrs, ..ti }
|
|
|
|
})),
|
|
|
|
Annotatable::ImplItem(i) => Annotatable::ImplItem(i.map(|ii| {
|
|
|
|
ast::ImplItem { attrs: attrs, ..ii }
|
|
|
|
})),
|
2014-12-02 10:07:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expect_item(self) -> P<ast::Item> {
|
|
|
|
match self {
|
|
|
|
Annotatable::Item(i) => i,
|
|
|
|
_ => panic!("expected Item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:34:39 +12: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
pub fn expect_trait_item(self) -> P<ast::TraitItem> {
|
2014-12-02 10:07:41 -08:00
|
|
|
match self {
|
|
|
|
Annotatable::TraitItem(i) => i,
|
|
|
|
_ => panic!("expected Item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
pub fn expect_impl_item(self) -> P<ast::ImplItem> {
|
2014-12-02 10:07:41 -08:00
|
|
|
match self {
|
|
|
|
Annotatable::ImplItem(i) => i,
|
|
|
|
_ => panic!("expected Item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 22:59:41 +01:00
|
|
|
// A more flexible ItemDecorator.
|
|
|
|
pub trait MultiItemDecorator {
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
2015-05-22 21:10:14 +05:30
|
|
|
item: &Annotatable,
|
2015-04-28 17:34:39 +12:00
|
|
|
push: &mut FnMut(Annotatable));
|
2015-01-16 22:59:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> MultiItemDecorator for F
|
2015-05-22 21:10:14 +05:30
|
|
|
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &Annotatable, &mut FnMut(Annotatable))
|
2015-01-16 22:59:41 +01:00
|
|
|
{
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
2015-05-22 21:10:14 +05:30
|
|
|
item: &Annotatable,
|
2015-04-25 15:31:11 +12:00
|
|
|
push: &mut FnMut(Annotatable)) {
|
2015-01-16 22:59:41 +01:00
|
|
|
(*self)(ecx, sp, meta_item, item, push)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:07:41 -08:00
|
|
|
// A more flexible ItemModifier (ItemModifier should go away, eventually, FIXME).
|
|
|
|
// meta_item is the annotation, item is the item being modified, parent_item
|
|
|
|
// is the impl or trait item is declared in if item is part of such a thing.
|
|
|
|
// 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)
|
|
|
|
-> Annotatable;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> MultiItemModifier for F
|
|
|
|
where F: Fn(&mut ExtCtxt,
|
|
|
|
Span,
|
|
|
|
&ast::MetaItem,
|
|
|
|
Annotatable) -> Annotatable
|
|
|
|
{
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: Annotatable)
|
|
|
|
-> Annotatable {
|
|
|
|
(*self)(ecx, span, meta_item, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 11:14:42 -08:00
|
|
|
// Use a macro because forwarding to a simple function has type system issues
|
2015-04-07 08:21:18 -05:00
|
|
|
macro_rules! make_stmts_default {
|
2015-02-27 11:14:42 -08:00
|
|
|
($me:expr) => {
|
|
|
|
$me.make_expr().map(|e| {
|
2015-04-07 08:21:18 -05:00
|
|
|
SmallVector::one(P(codemap::respan(
|
|
|
|
e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID))))
|
2015-02-27 11:14:42 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 22:00:14 +10:00
|
|
|
/// The result of a macro expansion. The return values of the various
|
2015-02-27 11:14:42 -08:00
|
|
|
/// methods are spliced into the AST at the callsite of the macro.
|
2014-04-15 22:00:14 +10:00
|
|
|
pub trait MacResult {
|
|
|
|
/// 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
|
|
|
|
2015-03-11 23:38:58 +02:00
|
|
|
/// Create zero or more impl items.
|
|
|
|
fn make_impl_items(self: Box<Self>) -> Option<SmallVector<P<ast::ImplItem>>> {
|
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
|
|
|
|
2015-04-07 08:21:18 -05:00
|
|
|
/// Create zero or more statements.
|
2014-04-15 22:00:14 +10:00
|
|
|
///
|
|
|
|
/// By default this attempts to create an expression statement,
|
|
|
|
/// returning None if that fails.
|
2015-04-07 08:21:18 -05:00
|
|
|
fn make_stmts(self: Box<Self>) -> Option<SmallVector<P<ast::Stmt>>> {
|
|
|
|
make_stmts_default!(self)
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
2015-07-25 21:54:19 -07:00
|
|
|
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
|
|
None
|
|
|
|
}
|
2013-08-30 14:40:05 -07:00
|
|
|
}
|
2013-07-08 15:55:14 -07:00
|
|
|
|
2015-02-27 11:14:42 -08: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-15 20:56:16 -07:00
|
|
|
Box::new(MacEager {
|
2015-02-27 11:14:42 -08:00
|
|
|
$fld: Some(v),
|
|
|
|
..Default::default()
|
2015-04-15 20:56:16 -07:00
|
|
|
})
|
2015-02-27 11:14:42 -08:00
|
|
|
}
|
|
|
|
)*
|
2014-08-31 01:45:11 +03:00
|
|
|
}
|
|
|
|
}
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
2015-02-27 11:14:42 -08:00
|
|
|
|
|
|
|
make_MacEager! {
|
|
|
|
expr: P<ast::Expr>,
|
|
|
|
pat: P<ast::Pat>,
|
|
|
|
items: SmallVector<P<ast::Item>>,
|
2015-03-11 23:38:58 +02:00
|
|
|
impl_items: SmallVector<P<ast::ImplItem>>,
|
2015-04-07 08:21:18 -05:00
|
|
|
stmts: SmallVector<P<ast::Stmt>>,
|
2015-07-25 21:54:19 -07:00
|
|
|
ty: P<ast::Ty>,
|
2014-05-19 13:32:51 -07:00
|
|
|
}
|
2015-02-27 11:14:42 -08:00
|
|
|
|
|
|
|
impl MacResult for MacEager {
|
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
|
|
|
self.expr
|
2014-05-19 13:32:51 -07:00
|
|
|
}
|
2015-02-27 11:14:42 -08:00
|
|
|
|
|
|
|
fn make_items(self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> {
|
|
|
|
self.items
|
2014-05-19 13:32:51 -07:00
|
|
|
}
|
2014-09-13 12:55:00 +02:00
|
|
|
|
2015-03-11 23:38:58 +02:00
|
|
|
fn make_impl_items(self: Box<Self>) -> Option<SmallVector<P<ast::ImplItem>>> {
|
|
|
|
self.impl_items
|
2015-02-27 11:14:42 -08:00
|
|
|
}
|
|
|
|
|
2015-04-07 08:21:18 -05:00
|
|
|
fn make_stmts(self: Box<Self>) -> Option<SmallVector<P<ast::Stmt>>> {
|
|
|
|
match self.stmts.as_ref().map_or(0, |s| s.len()) {
|
|
|
|
0 => make_stmts_default!(self),
|
|
|
|
_ => self.stmts,
|
2015-02-27 11:14:42 -08:00
|
|
|
}
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
2014-09-13 12:55:00 +02:00
|
|
|
|
2015-02-27 11:14:42 -08: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 {
|
|
|
|
if let ast::ExprLit(_) = e.node {
|
|
|
|
return Some(P(ast::Pat {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: e.span,
|
|
|
|
node: ast::PatLit(e),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2014-04-15 22:00:14 +10:00
|
|
|
}
|
2015-07-25 21:54:19 -07:00
|
|
|
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
|
|
self.ty
|
|
|
|
}
|
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.
|
2015-03-30 09:38:59 -04:00
|
|
|
#[derive(Copy, Clone)]
|
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> {
|
2015-04-15 20:56:16 -07:00
|
|
|
Box::new(DummyResult { expr_only: false, span: sp })
|
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> {
|
2015-04-15 20:56:16 -07:00
|
|
|
Box::new(DummyResult { expr_only: true, span: sp })
|
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
|
|
|
|
2015-07-25 21:54:19 -07:00
|
|
|
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
|
|
|
|
P(ast::Ty {
|
2015-07-25 22:17:43 -07:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2015-07-25 21:54:19 -07:00
|
|
|
node: ast::TyInfer,
|
|
|
|
span: sp
|
|
|
|
})
|
2015-07-25 22:17:43 -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
|
|
|
}
|
2015-07-25 22:17:43 -07: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
|
|
|
}
|
2015-07-25 22:17:43 -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())
|
|
|
|
}
|
|
|
|
}
|
2015-07-25 22:17:43 -07:00
|
|
|
|
2015-03-11 23:38:58 +02:00
|
|
|
fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVector<P<ast::ImplItem>>> {
|
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
|
|
|
}
|
2015-07-25 22:17:43 -07:00
|
|
|
|
2015-04-07 08:21:18 -05:00
|
|
|
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Stmt>>> {
|
|
|
|
Some(SmallVector::one(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.
|
|
|
|
///
|
2015-01-20 20:28:36 +01:00
|
|
|
/// `#[derive(...)]` is a `MultiItemDecorator`.
|
2015-01-16 23:04:55 +01:00
|
|
|
MultiDecorator(Box<MultiItemDecorator + '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
|
2014-12-02 10:07:41 -08:00
|
|
|
/// in-place. More flexible version than Modifier.
|
|
|
|
MultiModifier(Box<MultiItemModifier + 'static>),
|
|
|
|
|
2014-02-27 23:49:25 -08:00
|
|
|
/// A normal, function-like syntax extension.
|
|
|
|
///
|
|
|
|
/// `bytes!` is a `NormalTT`.
|
Add #[allow_internal_unstable] to track stability for macros better.
Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.
E.g.
#[allow_internal_unstable]
macro_rules! foo {
($e: expr) => {{
$e;
unstable(); // no warning
only_called_by_foo!();
}}
}
macro_rules! only_called_by_foo {
() => { unstable() } // warning
}
foo!(unstable()) // warning
The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).
In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.
The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
2015-03-01 14:09:28 +11:00
|
|
|
///
|
|
|
|
/// The `bool` dictates whether the contents of the macro can
|
|
|
|
/// directly use `#[unstable]` things (true == yes).
|
|
|
|
NormalTT(Box<TTMacroExpander + 'static>, Option<Span>, bool),
|
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.
|
|
|
|
///
|
Add #[allow_internal_unstable] to track stability for macros better.
Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.
E.g.
#[allow_internal_unstable]
macro_rules! foo {
($e: expr) => {{
$e;
unstable(); // no warning
only_called_by_foo!();
}}
}
macro_rules! only_called_by_foo {
() => { unstable() } // warning
}
foo!(unstable()) // warning
The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).
In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.
The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
2015-03-01 14:09:28 +11:00
|
|
|
IdentTT(Box<IdentMacroExpander + 'static>, Option<Span>, bool),
|
2014-07-07 09:54:08 -07:00
|
|
|
|
2014-09-15 16:09:09 -07:00
|
|
|
/// Represents `macro_rules!` itself.
|
|
|
|
MacroRulesTT,
|
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
|
2015-02-15 21:30:45 +01:00
|
|
|
fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>)
|
|
|
|
-> 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 {
|
Add #[allow_internal_unstable] to track stability for macros better.
Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.
E.g.
#[allow_internal_unstable]
macro_rules! foo {
($e: expr) => {{
$e;
unstable(); // no warning
only_called_by_foo!();
}}
}
macro_rules! only_called_by_foo {
() => { unstable() } // warning
}
foo!(unstable()) // warning
The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).
In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.
The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
2015-03-01 14:09:28 +11:00
|
|
|
NormalTT(Box::new(f), None, false)
|
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-09-15 16:09:09 -07:00
|
|
|
syntax_expanders.insert(intern("macro_rules"), MacroRulesTT);
|
2014-04-30 16:49:12 -07:00
|
|
|
syntax_expanders.insert(intern("format_args"),
|
Add #[allow_internal_unstable] to track stability for macros better.
Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.
E.g.
#[allow_internal_unstable]
macro_rules! foo {
($e: expr) => {{
$e;
unstable(); // no warning
only_called_by_foo!();
}}
}
macro_rules! only_called_by_foo {
() => { unstable() } // warning
}
foo!(unstable()) // warning
The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).
In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.
The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
2015-03-01 14:09:28 +11:00
|
|
|
// format_args uses `unstable` things internally.
|
|
|
|
NormalTT(Box::new(ext::format::expand_format_args), None, true));
|
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("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));
|
2015-03-06 13:15:54 -08:00
|
|
|
|
|
|
|
ext::deriving::register_all(&mut syntax_expanders);
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2015-02-15 21:30:45 +01:00
|
|
|
if ecfg.enable_quotes() {
|
2014-09-26 17:14:23 -07:00
|
|
|
// 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_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));
|
2015-02-24 19:56:01 +01:00
|
|
|
syntax_expanders.insert(intern("quote_matcher"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_matcher));
|
|
|
|
syntax_expanders.insert(intern("quote_attr"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::quote::expand_quote_attr));
|
2014-09-26 17:14:23 -07:00
|
|
|
}
|
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-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));
|
2015-06-05 08:31:27 +02:00
|
|
|
syntax_expanders.insert(intern("push_unsafe"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::pushpop_safe::expand_push_unsafe));
|
|
|
|
syntax_expanders.insert(intern("pop_unsafe"),
|
|
|
|
builtin_normal_expander(
|
|
|
|
ext::pushpop_safe::expand_pop_unsafe));
|
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,
|
2015-02-15 21:30:45 +01:00
|
|
|
pub ecfg: expand::ExpansionConfig<'a>,
|
2015-07-29 17:01:14 -07:00
|
|
|
pub crate_root: Option<&'static str>,
|
2015-07-13 17:10:44 -07:00
|
|
|
pub feature_gated_cfgs: &'a mut Vec<GatedCfg>,
|
2011-07-10 17:00:28 -07:00
|
|
|
|
2014-03-27 15:39:48 -07:00
|
|
|
pub mod_path: Vec<ast::Ident> ,
|
2014-12-30 19:10:46 -08:00
|
|
|
pub exported_macros: Vec<ast::MacroDef>,
|
2014-07-19 21:34:24 +02:00
|
|
|
|
2014-07-20 16:25:35 +02:00
|
|
|
pub syntax_env: SyntaxEnv,
|
2015-01-17 23:33:05 +00:00
|
|
|
pub recursion_count: usize,
|
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,
|
2015-07-13 17:10:44 -07:00
|
|
|
ecfg: expand::ExpansionConfig<'a>,
|
|
|
|
feature_gated_cfgs: &'a mut Vec<GatedCfg>) -> 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,
|
2015-07-29 17:01:14 -07:00
|
|
|
crate_root: None,
|
2015-07-13 17:10:44 -07:00
|
|
|
feature_gated_cfgs: feature_gated_cfgs,
|
2014-07-10 15:41:11 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 12:33:46 -08:00
|
|
|
#[unstable(feature = "rustc_private")]
|
|
|
|
#[deprecated(since = "1.0.0",
|
2015-01-12 18:40:19 -08:00
|
|
|
reason = "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
|
|
|
}
|
|
|
|
|
2015-05-13 23:08:02 +03:00
|
|
|
pub fn codemap(&self) -> &'a CodeMap { self.parse_sess.codemap() }
|
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
|
|
|
}
|
2014-09-17 19:01:33 +03:00
|
|
|
pub fn backtrace(&self) -> ExpnId { self.backtrace }
|
2015-06-16 21:47:09 +03:00
|
|
|
|
|
|
|
/// Original span that caused the current exapnsion to happen.
|
2014-09-17 19:01:33 +03:00
|
|
|
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")
|
|
|
|
}
|
2015-06-16 21:47:09 +03:00
|
|
|
|
|
|
|
/// Returns span for the macro which originally caused the current expansion to happen.
|
|
|
|
///
|
|
|
|
/// Stops backtracing at include! boundary.
|
|
|
|
pub fn expansion_cause(&self) -> Span {
|
2014-09-17 19:01:33 +03:00
|
|
|
let mut expn_id = self.backtrace;
|
2015-06-16 21:47:09 +03:00
|
|
|
let mut last_macro = None;
|
2014-09-17 19:01:33 +03:00
|
|
|
loop {
|
2015-06-16 21:47:09 +03:00
|
|
|
if self.codemap().with_expn_info(expn_id, |info| {
|
|
|
|
info.map_or(None, |i| {
|
2015-09-24 23:05:02 +03:00
|
|
|
if i.callee.name().as_str() == "include" {
|
2015-06-16 21:47:09 +03:00
|
|
|
// Stop going up the backtrace once include! is encountered
|
|
|
|
return None;
|
2014-09-17 19:01:33 +03:00
|
|
|
}
|
2015-06-16 21:47:09 +03:00
|
|
|
expn_id = i.call_site.expn_id;
|
2015-08-27 05:16:05 +05:30
|
|
|
match i.callee.format {
|
|
|
|
CompilerExpansion(..) => (),
|
|
|
|
_ => last_macro = Some(i.call_site),
|
2015-06-16 21:47:09 +03:00
|
|
|
}
|
|
|
|
return Some(());
|
|
|
|
})
|
|
|
|
}).is_none() {
|
|
|
|
break
|
2014-09-17 19:01:33 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-16 21:47:09 +03:00
|
|
|
last_macro.expect("missing expansion backtrace")
|
2014-09-17 19:01:33 +03:00
|
|
|
}
|
|
|
|
|
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();
|
2015-02-20 14:08:14 -05:00
|
|
|
v.push(token::str_to_ident(&self.ecfg.crate_name));
|
2015-02-13 07:33:44 +00:00
|
|
|
v.extend(self.mod_path.iter().cloned());
|
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 {
|
2015-03-28 21:58:51 +00:00
|
|
|
panic!(self.span_fatal(ei.call_site,
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!("recursion limit reached while expanding the macro `{}`",
|
2015-08-27 05:16:05 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2015-01-01 16:37:47 -08:00
|
|
|
|
|
|
|
pub fn insert_macro(&mut self, def: ast::MacroDef) {
|
|
|
|
if def.export {
|
|
|
|
self.exported_macros.push(def.clone());
|
|
|
|
}
|
2015-01-02 12:50:45 -08:00
|
|
|
if def.use_locally {
|
|
|
|
let ext = macro_rules::compile(self, &def);
|
|
|
|
self.syntax_env.insert(def.ident.name, ext);
|
|
|
|
}
|
2015-01-01 16:37:47 -08: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) -> ! {
|
2015-03-28 21:58:51 +00:00
|
|
|
panic!(self.parse_sess.span_diagnostic.span_fatal(sp, msg));
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
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.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.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.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.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.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.parse_sess.span_diagnostic.span_help(sp, msg);
|
|
|
|
}
|
2015-02-24 16:07:54 +02:00
|
|
|
pub fn fileline_help(&self, sp: Span, msg: &str) {
|
|
|
|
self.parse_sess.span_diagnostic.fileline_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.parse_sess.span_diagnostic.handler().bug(msg);
|
|
|
|
}
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn trace_macros(&self) -> bool {
|
2015-04-14 15:36:38 +02:00
|
|
|
self.ecfg.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) {
|
2015-04-14 15:36:38 +02:00
|
|
|
self.ecfg.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
|
|
|
}
|
2015-07-29 17:01:14 -07:00
|
|
|
pub fn std_path(&self, components: &[&str]) -> Vec<ast::Ident> {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
if let Some(s) = self.crate_root {
|
|
|
|
v.push(self.ident_of(s));
|
|
|
|
}
|
|
|
|
v.extend(components.iter().map(|s| self.ident_of(s)));
|
|
|
|
return v
|
2014-09-07 14:57:26 -07: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) {
|
2015-03-24 16:54:09 -07:00
|
|
|
if !tts.is_empty() {
|
2015-02-20 14:08:14 -05: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 {
|
2015-02-20 14:08:14 -05: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 {
|
2015-02-20 14:08:14 -05: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, _)| {
|
2015-02-03 23:31:06 +01:00
|
|
|
s.to_string()
|
2014-10-20 23:04:16 -07:00
|
|
|
})
|
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()));
|
2015-03-28 21:58:51 +00:00
|
|
|
if panictry!(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
|
|
|
}
|
|
|
|
|
2015-09-24 23:05:02 +03: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() {
|
2015-09-24 23:05:02 +03: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
|
|
|
}
|
|
|
|
}
|