2013-03-19 08:52:10 -04:00
|
|
|
// Copyright 2012-2013 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.
|
|
|
|
|
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;
|
2013-08-31 18:13:04 +02:00
|
|
|
use codemap::{CodeMap, Span, ExpnInfo};
|
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;
|
2013-03-26 16:38:07 -04:00
|
|
|
use parse::token;
|
2013-06-04 12:21:25 -07:00
|
|
|
use parse::token::{ident_to_str, intern, str_to_ident};
|
2013-11-24 23:08:53 -08:00
|
|
|
use util::small_vector::SmallVector;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-06-24 20:40:33 -04:00
|
|
|
use std::hashmap::HashMap;
|
2013-12-25 11:10:33 -07:00
|
|
|
use std::unstable::dynamic_lib::DynamicLibrary;
|
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 {
|
2013-06-13 03:02:55 +10:00
|
|
|
name: @str,
|
2013-01-22 16:45:27 -08:00
|
|
|
ext: SyntaxExtension
|
2013-01-17 08:55:28 -08:00
|
|
|
}
|
2012-07-27 17:42:32 -07:00
|
|
|
|
2013-12-27 17:21:15 -07:00
|
|
|
pub type ItemDecorator =
|
2014-01-09 15:05:33 +02:00
|
|
|
fn(&ExtCtxt, Span, @ast::MetaItem, ~[@ast::Item]) -> ~[@ast::Item];
|
2012-07-06 14:29:50 -07:00
|
|
|
|
2013-08-30 14:40:05 -07:00
|
|
|
pub struct SyntaxExpanderTT {
|
|
|
|
expander: SyntaxExpanderTTExpander,
|
|
|
|
span: Option<Span>
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SyntaxExpanderTTTrait {
|
|
|
|
fn expand(&self,
|
2013-12-28 22:06:22 -07:00
|
|
|
ecx: &mut ExtCtxt,
|
2013-08-30 14:40:05 -07:00
|
|
|
span: Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
token_tree: &[ast::TokenTree],
|
2013-08-30 14:40:05 -07:00
|
|
|
context: ast::SyntaxContext)
|
|
|
|
-> MacResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type SyntaxExpanderTTFunNoCtxt =
|
2014-01-09 15:05:33 +02:00
|
|
|
fn(ecx: &mut ExtCtxt, span: codemap::Span, token_tree: &[ast::TokenTree])
|
2013-12-27 17:21:15 -07:00
|
|
|
-> MacResult;
|
2013-08-30 14:40:05 -07:00
|
|
|
|
|
|
|
enum SyntaxExpanderTTExpander {
|
|
|
|
SyntaxExpanderTTExpanderWithoutContext(SyntaxExpanderTTFunNoCtxt),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SyntaxExpanderTTTrait for SyntaxExpanderTT {
|
|
|
|
fn expand(&self,
|
2013-12-28 22:06:22 -07:00
|
|
|
ecx: &mut ExtCtxt,
|
2013-08-30 14:40:05 -07:00
|
|
|
span: Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
token_tree: &[ast::TokenTree],
|
2013-08-30 14:40:05 -07:00
|
|
|
_: ast::SyntaxContext)
|
|
|
|
-> MacResult {
|
|
|
|
match self.expander {
|
|
|
|
SyntaxExpanderTTExpanderWithoutContext(f) => {
|
|
|
|
f(ecx, span, token_tree)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 15:55:14 -07:00
|
|
|
|
2013-08-30 14:40:05 -07:00
|
|
|
enum SyntaxExpanderTTItemExpander {
|
|
|
|
SyntaxExpanderTTItemExpanderWithContext(SyntaxExpanderTTItemFun),
|
|
|
|
SyntaxExpanderTTItemExpanderWithoutContext(SyntaxExpanderTTItemFunNoCtxt),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SyntaxExpanderTTItem {
|
|
|
|
expander: SyntaxExpanderTTItemExpander,
|
|
|
|
span: Option<Span>
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SyntaxExpanderTTItemTrait {
|
|
|
|
fn expand(&self,
|
2013-12-28 22:06:22 -07:00
|
|
|
cx: &mut ExtCtxt,
|
2013-08-30 14:40:05 -07:00
|
|
|
sp: Span,
|
|
|
|
ident: ast::Ident,
|
2014-01-09 15:05:33 +02:00
|
|
|
token_tree: ~[ast::TokenTree],
|
2013-08-30 14:40:05 -07:00
|
|
|
context: ast::SyntaxContext)
|
|
|
|
-> MacResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem {
|
|
|
|
fn expand(&self,
|
2013-12-28 22:06:22 -07:00
|
|
|
cx: &mut ExtCtxt,
|
2013-08-30 14:40:05 -07:00
|
|
|
sp: Span,
|
|
|
|
ident: ast::Ident,
|
2014-01-09 15:05:33 +02:00
|
|
|
token_tree: ~[ast::TokenTree],
|
2013-08-30 14:40:05 -07:00
|
|
|
context: ast::SyntaxContext)
|
|
|
|
-> MacResult {
|
|
|
|
match self.expander {
|
|
|
|
SyntaxExpanderTTItemExpanderWithContext(fun) => {
|
|
|
|
fun(cx, sp, ident, token_tree, context)
|
|
|
|
}
|
|
|
|
SyntaxExpanderTTItemExpanderWithoutContext(fun) => {
|
|
|
|
fun(cx, sp, ident, token_tree)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 17:21:15 -07:00
|
|
|
pub type SyntaxExpanderTTItemFun =
|
2014-01-09 15:05:33 +02:00
|
|
|
fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::TokenTree], ast::SyntaxContext)
|
2013-12-27 17:21:15 -07:00
|
|
|
-> MacResult;
|
2013-08-30 14:40:05 -07:00
|
|
|
|
|
|
|
pub type SyntaxExpanderTTItemFunNoCtxt =
|
2014-01-09 15:05:33 +02:00
|
|
|
fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::TokenTree]) -> MacResult;
|
2013-08-30 14:40:05 -07:00
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
pub type MacroCrateRegistrationFun =
|
|
|
|
extern "Rust" fn(|ast::Name, SyntaxExtension|);
|
|
|
|
|
2013-08-30 14:40:05 -07:00
|
|
|
pub trait AnyMacro {
|
|
|
|
fn make_expr(&self) -> @ast::Expr;
|
2014-01-09 15:05:33 +02:00
|
|
|
fn make_items(&self) -> SmallVector<@ast::Item>;
|
2013-08-30 14:40:05 -07:00
|
|
|
fn make_stmt(&self) -> @ast::Stmt;
|
|
|
|
}
|
2013-07-08 15:55:14 -07:00
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub enum MacResult {
|
2013-09-02 03:45:37 +02:00
|
|
|
MRExpr(@ast::Expr),
|
2014-01-09 15:05:33 +02:00
|
|
|
MRItem(@ast::Item),
|
2013-08-30 14:40:05 -07:00
|
|
|
MRAny(@AnyMacro),
|
|
|
|
MRDef(MacroDef),
|
2012-07-06 14:29:50 -07:00
|
|
|
}
|
2014-01-18 01:53:10 +11:00
|
|
|
impl MacResult {
|
|
|
|
/// Create an empty expression MacResult; useful for satisfying
|
|
|
|
/// type signatures after emitting a non-fatal error (which stop
|
|
|
|
/// compilation well before the validity (or otherwise)) of the
|
|
|
|
/// expression are checked.
|
|
|
|
pub fn dummy_expr() -> MacResult {
|
|
|
|
MRExpr(@ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID, node: ast::ExprLogLevel, span: codemap::DUMMY_SP
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2012-07-05 12:10:33 -07:00
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub enum SyntaxExtension {
|
2013-11-06 12:16:19 +09:00
|
|
|
// #[deriving] and such
|
2013-08-30 14:40:05 -07:00
|
|
|
ItemDecorator(ItemDecorator),
|
2012-06-25 15:04:50 -07:00
|
|
|
|
2012-07-27 17:42:32 -07:00
|
|
|
// Token-tree expanders
|
2014-01-05 18:02:57 -08:00
|
|
|
NormalTT(~SyntaxExpanderTTTrait:'static, Option<Span>),
|
2012-11-08 23:12:45 -05:00
|
|
|
|
2013-02-26 10:15:29 -08:00
|
|
|
// An IdentTT is a macro that has an
|
|
|
|
// identifier in between the name of the
|
|
|
|
// macro and the argument. Currently,
|
2013-07-31 16:10:35 -07:00
|
|
|
// the only examples of this is
|
|
|
|
// macro_rules!
|
2013-02-26 10:15:29 -08:00
|
|
|
|
2012-11-08 23:12:45 -05:00
|
|
|
// perhaps macro_rules! will lose its odd special identifier argument,
|
|
|
|
// and this can go away also
|
2014-01-05 18:02:57 -08:00
|
|
|
IdentTT(~SyntaxExpanderTTItemTrait:'static, Option<Span>),
|
2011-06-20 17:26:17 -07:00
|
|
|
}
|
2011-06-04 15:41:45 -04:00
|
|
|
|
2013-05-08 15:27:29 -07:00
|
|
|
pub struct BlockInfo {
|
|
|
|
// should macros escape from this scope?
|
|
|
|
macros_escape : bool,
|
|
|
|
// what are the pending renames?
|
2013-12-25 11:10:33 -07:00
|
|
|
pending_renames : RenameList,
|
|
|
|
// references for crates loaded in this scope
|
|
|
|
macro_crates: ~[DynamicLibrary],
|
2013-12-30 17:45:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockInfo {
|
|
|
|
pub fn new() -> BlockInfo {
|
|
|
|
BlockInfo {
|
|
|
|
macros_escape: false,
|
2013-12-25 11:10:33 -07:00
|
|
|
pending_renames: ~[],
|
|
|
|
macro_crates: ~[],
|
2013-12-30 17:45:39 -08:00
|
|
|
}
|
|
|
|
}
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
2013-02-13 20:08:35 -08:00
|
|
|
|
2013-05-08 15:27:29 -07:00
|
|
|
// a list of ident->name renamings
|
2013-12-30 17:45:39 -08:00
|
|
|
pub type RenameList = ~[(ast::Ident,Name)];
|
2013-05-08 15:27:29 -07:00
|
|
|
|
2013-02-26 10:15:29 -08:00
|
|
|
// The base map of methods for expanding syntax extension
|
2011-06-04 15:41:45 -04:00
|
|
|
// AST nodes into full ASTs
|
2013-02-26 10:15:29 -08:00
|
|
|
pub fn syntax_expander_table() -> SyntaxEnv {
|
2013-02-04 13:15:17 -08:00
|
|
|
// utility function to simplify creating NormalTT syntax extensions
|
2013-08-30 14:40:05 -07:00
|
|
|
fn builtin_normal_tt_no_ctxt(f: SyntaxExpanderTTFunNoCtxt)
|
2013-12-30 17:45:39 -08:00
|
|
|
-> SyntaxExtension {
|
2014-01-05 18:02:57 -08:00
|
|
|
NormalTT(~SyntaxExpanderTT{
|
2013-08-30 14:40:05 -07:00
|
|
|
expander: SyntaxExpanderTTExpanderWithoutContext(f),
|
|
|
|
span: None,
|
2014-01-05 18:02:57 -08:00
|
|
|
},
|
2013-12-30 17:45:39 -08:00
|
|
|
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();
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"macro_rules"),
|
2014-01-05 18:02:57 -08:00
|
|
|
IdentTT(~SyntaxExpanderTTItem {
|
2013-09-16 23:37:54 -07:00
|
|
|
expander: SyntaxExpanderTTItemExpanderWithContext(
|
|
|
|
ext::tt::macro_rules::add_new_extension),
|
2013-08-30 14:40:05 -07:00
|
|
|
span: None,
|
2014-01-05 18:02:57 -08:00
|
|
|
},
|
2013-12-30 17:45:39 -08:00
|
|
|
None));
|
2013-10-18 16:01:40 -07:00
|
|
|
syntax_expanders.insert(intern(&"fmt"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::fmt::expand_syntax_ext));
|
2013-08-28 02:22:45 -07:00
|
|
|
syntax_expanders.insert(intern(&"format_args"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::format::expand_args));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"env"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::env::expand_env));
|
2013-08-07 00:50:23 -04:00
|
|
|
syntax_expanders.insert(intern(&"option_env"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::env::expand_option_env));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern("bytes"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::bytes::expand_syntax_ext));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern("concat_idents"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
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"),
|
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::concat::expand_syntax_ext));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"log_syntax"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::log_syntax::expand_syntax_ext));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"deriving"),
|
2013-12-30 17:45:39 -08:00
|
|
|
ItemDecorator(ext::deriving::expand_meta_deriving));
|
2012-11-16 14:50:35 -08:00
|
|
|
|
|
|
|
// Quasi-quoting expanders
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"quote_tokens"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::quote::expand_quote_tokens));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"quote_expr"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::quote::expand_quote_expr));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"quote_ty"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::quote::expand_quote_ty));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"quote_item"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::quote::expand_quote_item));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"quote_pat"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::quote::expand_quote_pat));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"quote_stmt"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::quote::expand_quote_stmt));
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"line"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_line));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"col"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_col));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"file"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_file));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"stringify"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_stringify));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"include"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_include));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"include_str"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_include_str));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"include_bin"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_include_bin));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"module_path"),
|
2013-07-08 15:55:14 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
2013-08-30 14:40:05 -07:00
|
|
|
ext::source_util::expand_mod));
|
2013-05-08 15:27:29 -07:00
|
|
|
syntax_expanders.insert(intern(&"asm"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::asm::expand_asm));
|
2013-08-01 23:03:03 +10:00
|
|
|
syntax_expanders.insert(intern(&"cfg"),
|
2013-08-30 14:40:05 -07:00
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
ext::cfg::expand_cfg));
|
|
|
|
syntax_expanders.insert(intern(&"trace_macros"),
|
|
|
|
builtin_normal_tt_no_ctxt(
|
|
|
|
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
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
pub struct MacroCrate {
|
|
|
|
lib: Option<Path>,
|
|
|
|
cnum: ast::CrateNum,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait CrateLoader {
|
|
|
|
fn load_crate(&mut self, crate: &ast::ViewItem) -> MacroCrate;
|
|
|
|
fn get_exported_macros(&mut self, crate_num: ast::CrateNum) -> ~[@ast::Item];
|
|
|
|
fn get_registrar_symbol(&mut self, crate_num: ast::CrateNum) -> Option<~str>;
|
|
|
|
}
|
|
|
|
|
2012-07-27 17:42:32 -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> {
|
2013-12-27 11:56:29 -08:00
|
|
|
parse_sess: @parse::ParseSess,
|
2013-07-19 07:38:55 +02:00
|
|
|
cfg: ast::CrateConfig,
|
2013-12-28 22:35:38 -07:00
|
|
|
backtrace: Option<@ExpnInfo>,
|
2013-12-25 11:10:33 -07:00
|
|
|
loader: &'a mut CrateLoader,
|
2011-07-10 17:00:28 -07:00
|
|
|
|
2013-12-28 22:35:38 -07:00
|
|
|
mod_path: ~[ast::Ident],
|
|
|
|
trace_mac: bool
|
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> {
|
|
|
|
pub fn new<'a>(parse_sess: @parse::ParseSess, cfg: ast::CrateConfig,
|
|
|
|
loader: &'a mut CrateLoader) -> ExtCtxt<'a> {
|
2013-12-27 17:17:36 -07:00
|
|
|
ExtCtxt {
|
2013-05-17 21:27:17 +10:00
|
|
|
parse_sess: parse_sess,
|
|
|
|
cfg: cfg,
|
2013-12-28 22:35:38 -07:00
|
|
|
backtrace: None,
|
2013-12-25 11:10:33 -07:00
|
|
|
loader: loader,
|
2013-12-28 22:35:38 -07:00
|
|
|
mod_path: ~[],
|
|
|
|
trace_mac: false
|
2013-05-17 21:27:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_expr(&mut self, mut e: @ast::Expr) -> @ast::Expr {
|
2013-10-05 21:15:46 -07:00
|
|
|
loop {
|
|
|
|
match e.node {
|
2013-11-28 12:22:53 -08:00
|
|
|
ast::ExprMac(..) => {
|
2013-12-27 20:34:51 -07:00
|
|
|
let mut expander = expand::MacroExpander {
|
2013-12-30 17:45:39 -08:00
|
|
|
extsbox: syntax_expander_table(),
|
2013-10-05 21:15:46 -07:00
|
|
|
cx: self,
|
|
|
|
};
|
2013-12-27 20:34:51 -07:00
|
|
|
e = expand::expand_expr(e, &mut expander);
|
2013-10-05 21:15:46 -07:00
|
|
|
}
|
|
|
|
_ => return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
|
2013-12-27 11:56:29 -08:00
|
|
|
pub fn parse_sess(&self) -> @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 {
|
2013-12-28 22:35:38 -07:00
|
|
|
match self.backtrace {
|
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")
|
2012-12-06 11:01:58 -08:00
|
|
|
}
|
2013-05-17 20:10:26 +10:00
|
|
|
}
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn print_backtrace(&self) { }
|
2013-12-28 22:35:38 -07:00
|
|
|
pub fn backtrace(&self) -> Option<@ExpnInfo> { self.backtrace }
|
|
|
|
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(); }
|
2013-12-28 22:35:38 -07:00
|
|
|
pub fn mod_path(&self) -> ~[ast::Ident] { self.mod_path.clone() }
|
|
|
|
pub fn bt_push(&mut self, ei: codemap::ExpnInfo) {
|
2013-05-17 20:10:26 +10:00
|
|
|
match ei {
|
2013-07-02 18:31:00 +09:00
|
|
|
ExpnInfo {call_site: cs, callee: ref callee} => {
|
2013-12-28 22:35:38 -07:00
|
|
|
self.backtrace =
|
2013-07-02 18:31:00 +09:00
|
|
|
Some(@ExpnInfo {
|
2013-08-31 18:13:04 +02:00
|
|
|
call_site: Span {lo: cs.lo, hi: cs.hi,
|
2013-12-28 22:35:38 -07:00
|
|
|
expn_info: self.backtrace},
|
2013-06-27 17:41:35 -07:00
|
|
|
callee: *callee});
|
2012-02-04 18:37:24 -07:00
|
|
|
}
|
2012-01-13 09:32:05 +01:00
|
|
|
}
|
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-01-03 15:08:48 -08:00
|
|
|
Some(expn_info) => self.backtrace = expn_info.call_site.expn_info,
|
2013-05-17 20:10:26 +10:00
|
|
|
_ => self.bug("tried to pop without a push")
|
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.
|
|
|
|
///
|
|
|
|
/// `span_err` should be strongly prefered where-ever possible:
|
|
|
|
/// 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);
|
|
|
|
}
|
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 str_of(&self, id: ast::Ident) -> @str {
|
2013-06-13 03:02:55 +10:00
|
|
|
ident_to_str(&id)
|
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-01-18 01:53:10 +11:00
|
|
|
/// Extract a string literal from `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.
|
|
|
|
pub fn expr_to_str(cx: &ExtCtxt, expr: @ast::Expr,
|
|
|
|
err_msg: &str) -> Option<(@str, ast::StrStyle)> {
|
2012-08-06 12:34:08 -07:00
|
|
|
match expr.node {
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::ExprLit(l) => match l.node {
|
2014-01-18 01:53:10 +11:00
|
|
|
ast::LitStr(s, style) => return Some((s, style)),
|
|
|
|
_ => 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-09 15:05:33 +02: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-01-18 01:53:10 +11: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.
|
2013-12-27 17:17:36 -07:00
|
|
|
pub fn get_single_str_from_tts(cx: &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-01-18 01:53:10 +11:00
|
|
|
-> Option<@str> {
|
2012-12-12 17:08:09 -08:00
|
|
|
if tts.len() != 1 {
|
2014-01-18 01:53:10 +11:00
|
|
|
cx.span_err(sp, format!("{} takes 1 argument.", name));
|
|
|
|
} else {
|
|
|
|
match tts[0] {
|
|
|
|
ast::TTTok(_, token::LIT_STR(ident))
|
|
|
|
| ast::TTTok(_, token::LIT_STR_RAW(ident, _)) => return Some(cx.str_of(ident)),
|
|
|
|
_ => cx.span_err(sp, format!("{} requires a string.", name)),
|
|
|
|
}
|
2012-01-31 23:50:12 -07:00
|
|
|
}
|
2014-01-18 01:53:10 +11:00
|
|
|
None
|
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.
|
2013-12-27 17:17:36 -07:00
|
|
|
pub fn get_exprs_from_tts(cx: &ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-01-18 01:53:10 +11:00
|
|
|
tts: &[ast::TokenTree]) -> Option<~[@ast::Expr]> {
|
2013-12-30 14:04:00 -08:00
|
|
|
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
|
|
|
|
cx.cfg(),
|
|
|
|
tts.to_owned());
|
2012-12-12 17:08:09 -08:00
|
|
|
let mut es = ~[];
|
2013-12-30 15:09:41 -08:00
|
|
|
while p.token != token::EOF {
|
2013-07-22 21:22:22 +03:00
|
|
|
if es.len() != 0 && !p.eat(&token::COMMA) {
|
2014-01-18 01:53:10 +11:00
|
|
|
cx.span_err(sp, "expected token: `,`");
|
|
|
|
return None;
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
|
|
|
es.push(p.parse_expr());
|
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
|
|
|
}
|
|
|
|
|
2013-02-26 10:15:29 -08:00
|
|
|
// in order to have some notion of scoping for macros,
|
|
|
|
// we want to implement the notion of a transformation
|
|
|
|
// environment.
|
|
|
|
|
2013-12-30 17:45:39 -08:00
|
|
|
// This environment maps Names to SyntaxExtensions.
|
2013-02-26 10:15:29 -08:00
|
|
|
|
|
|
|
// Actually, the following implementation is parameterized
|
|
|
|
// by both key and value types.
|
|
|
|
|
|
|
|
//impl question: how to implement it? Initially, the
|
|
|
|
// 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,
|
2013-12-25 11:10:33 -07:00
|
|
|
map: HashMap<Name, SyntaxExtension>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl Drop for MapChainFrame {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// make sure that syntax extension dtors run before we drop the libs
|
|
|
|
self.map.clear();
|
|
|
|
}
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
|
2013-12-30 17:45:39 -08:00
|
|
|
// Only generic to make it easy to test
|
2013-12-25 11:10:33 -07:00
|
|
|
pub struct SyntaxEnv {
|
|
|
|
priv chain: ~[MapChainFrame],
|
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 {
|
|
|
|
pub fn new() -> SyntaxEnv {
|
|
|
|
let mut map = SyntaxEnv { chain: ~[] };
|
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 {
|
2013-12-30 17:45:39 -08:00
|
|
|
for (i, frame) in self.chain.mut_iter().enumerate().invert() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
pub fn find<'a>(&'a self, k: &Name) -> Option<&'a SyntaxExtension> {
|
2013-12-30 17:45:39 -08:00
|
|
|
for frame in self.chain.iter().invert() {
|
|
|
|
match frame.map.find(k) {
|
|
|
|
Some(v) => return Some(v),
|
|
|
|
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) {
|
2013-12-30 17:45:39 -08:00
|
|
|
self.find_escape_frame().map.insert(k, v);
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
pub fn insert_macro_crate(&mut self, lib: DynamicLibrary) {
|
|
|
|
self.find_escape_frame().info.macro_crates.push(lib);
|
2013-05-08 15:27:29 -07:00
|
|
|
}
|
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 {
|
|
|
|
&mut self.chain[self.chain.len()-1].info
|
2013-02-26 10:15:29 -08:00
|
|
|
}
|
|
|
|
}
|