base.rs 18.0 KB
Newer Older
1
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// 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.

11
use ast;
12
use ast::Name;
13
use codemap;
14
use codemap::{CodeMap, Span, ExpnInfo};
15
use ext;
16
use ext::expand;
17
use parse;
A
Alex Crichton 已提交
18
use parse::token;
19
use parse::token::{ident_to_str, intern, str_to_ident};
S
Steven Fackler 已提交
20
use util::small_vector::SmallVector;
21

22
use std::hashmap::HashMap;
23
use std::unstable::dynamic_lib::DynamicLibrary;
24

25 26
// new-style macro! tt code:
//
27
//    MacResult, NormalTT, IdentTT
28
//
29
// also note that ast::Mac used to have a bunch of extraneous cases and
30
// is now probably a redundant AST node, can be merged with
31
// ast::MacInvocTT.
32

33
pub struct MacroDef {
34
    name: @str,
35
    ext: SyntaxExtension
36
}
37

38
pub type ItemDecorator =
39
    fn(&ExtCtxt, Span, @ast::MetaItem, ~[@ast::Item]) -> ~[@ast::Item];
40

41 42 43 44 45 46 47
pub struct SyntaxExpanderTT {
    expander: SyntaxExpanderTTExpander,
    span: Option<Span>
}

pub trait SyntaxExpanderTTTrait {
    fn expand(&self,
S
Steven Fackler 已提交
48
              ecx: &mut ExtCtxt,
49
              span: Span,
50
              token_tree: &[ast::TokenTree],
51 52 53 54 55
              context: ast::SyntaxContext)
              -> MacResult;
}

pub type SyntaxExpanderTTFunNoCtxt =
56
    fn(ecx: &mut ExtCtxt, span: codemap::Span, token_tree: &[ast::TokenTree])
57
       -> MacResult;
58 59 60 61 62 63 64

enum SyntaxExpanderTTExpander {
    SyntaxExpanderTTExpanderWithoutContext(SyntaxExpanderTTFunNoCtxt),
}

impl SyntaxExpanderTTTrait for SyntaxExpanderTT {
    fn expand(&self,
S
Steven Fackler 已提交
65
              ecx: &mut ExtCtxt,
66
              span: Span,
67
              token_tree: &[ast::TokenTree],
68 69 70 71 72 73 74 75 76
              _: ast::SyntaxContext)
              -> MacResult {
        match self.expander {
            SyntaxExpanderTTExpanderWithoutContext(f) => {
                f(ecx, span, token_tree)
            }
        }
    }
}
J
John Clements 已提交
77

78 79 80 81 82 83 84 85 86 87 88 89
enum SyntaxExpanderTTItemExpander {
    SyntaxExpanderTTItemExpanderWithContext(SyntaxExpanderTTItemFun),
    SyntaxExpanderTTItemExpanderWithoutContext(SyntaxExpanderTTItemFunNoCtxt),
}

pub struct SyntaxExpanderTTItem {
    expander: SyntaxExpanderTTItemExpander,
    span: Option<Span>
}

pub trait SyntaxExpanderTTItemTrait {
    fn expand(&self,
S
Steven Fackler 已提交
90
              cx: &mut ExtCtxt,
91 92
              sp: Span,
              ident: ast::Ident,
93
              token_tree: ~[ast::TokenTree],
94 95 96 97 98 99
              context: ast::SyntaxContext)
              -> MacResult;
}

impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem {
    fn expand(&self,
S
Steven Fackler 已提交
100
              cx: &mut ExtCtxt,
101 102
              sp: Span,
              ident: ast::Ident,
103
              token_tree: ~[ast::TokenTree],
104 105 106 107 108 109 110 111 112 113 114 115 116
              context: ast::SyntaxContext)
              -> MacResult {
        match self.expander {
            SyntaxExpanderTTItemExpanderWithContext(fun) => {
                fun(cx, sp, ident, token_tree, context)
            }
            SyntaxExpanderTTItemExpanderWithoutContext(fun) => {
                fun(cx, sp, ident, token_tree)
            }
        }
    }
}

117
pub type SyntaxExpanderTTItemFun =
118
    fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::TokenTree], ast::SyntaxContext)
119
       -> MacResult;
120 121

pub type SyntaxExpanderTTItemFunNoCtxt =
122
    fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::TokenTree]) -> MacResult;
123

124 125 126
pub type MacroCrateRegistrationFun =
    extern "Rust" fn(|ast::Name, SyntaxExtension|);

127 128
pub trait AnyMacro {
    fn make_expr(&self) -> @ast::Expr;
129
    fn make_items(&self) -> SmallVector<@ast::Item>;
130 131
    fn make_stmt(&self) -> @ast::Stmt;
}
J
John Clements 已提交
132

133
pub enum MacResult {
134
    MRExpr(@ast::Expr),
135
    MRItem(@ast::Item),
136 137
    MRAny(@AnyMacro),
    MRDef(MacroDef),
138
}
139

140
pub enum SyntaxExtension {
K
klutzy 已提交
141
    // #[deriving] and such
142
    ItemDecorator(ItemDecorator),
143

144
    // Token-tree expanders
145
    NormalTT(~SyntaxExpanderTTTrait:'static, Option<Span>),
146

J
John Clements 已提交
147 148 149
    // An IdentTT is a macro that has an
    // identifier in between the name of the
    // macro and the argument. Currently,
B
Brian Anderson 已提交
150 151
    // the only examples of this is
    // macro_rules!
J
John Clements 已提交
152

153 154
    // perhaps macro_rules! will lose its odd special identifier argument,
    // and this can go away also
155
    IdentTT(~SyntaxExpanderTTItemTrait:'static, Option<Span>),
156
}
157

158 159 160 161
pub struct BlockInfo {
    // should macros escape from this scope?
    macros_escape : bool,
    // what are the pending renames?
162 163 164
    pending_renames : RenameList,
    // references for crates loaded in this scope
    macro_crates: ~[DynamicLibrary],
S
Steven Fackler 已提交
165 166 167 168 169 170
}

impl BlockInfo {
    pub fn new() -> BlockInfo {
        BlockInfo {
            macros_escape: false,
171 172
            pending_renames: ~[],
            macro_crates: ~[],
S
Steven Fackler 已提交
173 174
        }
    }
J
John Clements 已提交
175
}
176

177
// a list of ident->name renamings
S
Steven Fackler 已提交
178
pub type RenameList = ~[(ast::Ident,Name)];
179

J
John Clements 已提交
180
// The base map of methods for expanding syntax extension
181
// AST nodes into full ASTs
J
John Clements 已提交
182
pub fn syntax_expander_table() -> SyntaxEnv {
J
John Clements 已提交
183
    // utility function to simplify creating NormalTT syntax extensions
184
    fn builtin_normal_tt_no_ctxt(f: SyntaxExpanderTTFunNoCtxt)
S
Steven Fackler 已提交
185
                                 -> SyntaxExtension {
186
        NormalTT(~SyntaxExpanderTT{
187 188
            expander: SyntaxExpanderTTExpanderWithoutContext(f),
            span: None,
189
        },
S
Steven Fackler 已提交
190
        None)
191
    }
K
Kiet Tran 已提交
192

193
    let mut syntax_expanders = SyntaxEnv::new();
194
    syntax_expanders.insert(intern(&"macro_rules"),
195
                            IdentTT(~SyntaxExpanderTTItem {
P
Patrick Walton 已提交
196 197
                                expander: SyntaxExpanderTTItemExpanderWithContext(
                                    ext::tt::macro_rules::add_new_extension),
198
                                span: None,
199
                            },
S
Steven Fackler 已提交
200
                            None));
A
Alex Crichton 已提交
201
    syntax_expanders.insert(intern(&"fmt"),
202 203
                            builtin_normal_tt_no_ctxt(
                                ext::fmt::expand_syntax_ext));
A
Alex Crichton 已提交
204
    syntax_expanders.insert(intern(&"format_args"),
205 206
                            builtin_normal_tt_no_ctxt(
                                ext::format::expand_args));
207
    syntax_expanders.insert(intern(&"env"),
208 209
                            builtin_normal_tt_no_ctxt(
                                    ext::env::expand_env));
S
Steven Fackler 已提交
210
    syntax_expanders.insert(intern(&"option_env"),
211 212
                            builtin_normal_tt_no_ctxt(
                                    ext::env::expand_option_env));
213
    syntax_expanders.insert(intern("bytes"),
214 215
                            builtin_normal_tt_no_ctxt(
                                    ext::bytes::expand_syntax_ext));
216
    syntax_expanders.insert(intern("concat_idents"),
J
John Clements 已提交
217
                            builtin_normal_tt_no_ctxt(
218
                                    ext::concat_idents::expand_syntax_ext));
219 220 221
    syntax_expanders.insert(intern("concat"),
                            builtin_normal_tt_no_ctxt(
                                    ext::concat::expand_syntax_ext));
222
    syntax_expanders.insert(intern(&"log_syntax"),
J
John Clements 已提交
223
                            builtin_normal_tt_no_ctxt(
224
                                    ext::log_syntax::expand_syntax_ext));
225
    syntax_expanders.insert(intern(&"deriving"),
S
Steven Fackler 已提交
226
                            ItemDecorator(ext::deriving::expand_meta_deriving));
227 228

    // Quasi-quoting expanders
229
    syntax_expanders.insert(intern(&"quote_tokens"),
230 231
                       builtin_normal_tt_no_ctxt(
                            ext::quote::expand_quote_tokens));
232
    syntax_expanders.insert(intern(&"quote_expr"),
233 234
                       builtin_normal_tt_no_ctxt(
                            ext::quote::expand_quote_expr));
235
    syntax_expanders.insert(intern(&"quote_ty"),
236 237
                       builtin_normal_tt_no_ctxt(
                            ext::quote::expand_quote_ty));
238
    syntax_expanders.insert(intern(&"quote_item"),
239 240
                       builtin_normal_tt_no_ctxt(
                            ext::quote::expand_quote_item));
241
    syntax_expanders.insert(intern(&"quote_pat"),
242 243
                       builtin_normal_tt_no_ctxt(
                            ext::quote::expand_quote_pat));
244
    syntax_expanders.insert(intern(&"quote_stmt"),
245 246
                       builtin_normal_tt_no_ctxt(
                            ext::quote::expand_quote_stmt));
247

248
    syntax_expanders.insert(intern(&"line"),
J
John Clements 已提交
249
                            builtin_normal_tt_no_ctxt(
250
                                    ext::source_util::expand_line));
251
    syntax_expanders.insert(intern(&"col"),
J
John Clements 已提交
252
                            builtin_normal_tt_no_ctxt(
253
                                    ext::source_util::expand_col));
254
    syntax_expanders.insert(intern(&"file"),
J
John Clements 已提交
255
                            builtin_normal_tt_no_ctxt(
256
                                    ext::source_util::expand_file));
257
    syntax_expanders.insert(intern(&"stringify"),
J
John Clements 已提交
258
                            builtin_normal_tt_no_ctxt(
259
                                    ext::source_util::expand_stringify));
260
    syntax_expanders.insert(intern(&"include"),
J
John Clements 已提交
261
                            builtin_normal_tt_no_ctxt(
262
                                    ext::source_util::expand_include));
263
    syntax_expanders.insert(intern(&"include_str"),
J
John Clements 已提交
264
                            builtin_normal_tt_no_ctxt(
265
                                    ext::source_util::expand_include_str));
266
    syntax_expanders.insert(intern(&"include_bin"),
J
John Clements 已提交
267
                            builtin_normal_tt_no_ctxt(
268
                                    ext::source_util::expand_include_bin));
269
    syntax_expanders.insert(intern(&"module_path"),
J
John Clements 已提交
270
                            builtin_normal_tt_no_ctxt(
271
                                    ext::source_util::expand_mod));
272
    syntax_expanders.insert(intern(&"asm"),
273 274
                            builtin_normal_tt_no_ctxt(
                                    ext::asm::expand_asm));
275
    syntax_expanders.insert(intern(&"cfg"),
276 277 278 279 280
                            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));
S
Steven Fackler 已提交
281
    syntax_expanders
282
}
283

284 285 286 287 288 289 290 291 292 293 294
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>;
}

295 296 297
// 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.
298
pub struct ExtCtxt<'a> {
299
    parse_sess: @parse::ParseSess,
300
    cfg: ast::CrateConfig,
S
Steven Fackler 已提交
301
    backtrace: Option<@ExpnInfo>,
302
    loader: &'a mut CrateLoader,
303

S
Steven Fackler 已提交
304 305
    mod_path: ~[ast::Ident],
    trace_mac: bool
306
}
N
Niko Matsakis 已提交
307

308 309 310
impl<'a> ExtCtxt<'a> {
    pub fn new<'a>(parse_sess: @parse::ParseSess, cfg: ast::CrateConfig,
               loader: &'a mut CrateLoader) -> ExtCtxt<'a> {
S
Steven Fackler 已提交
311
        ExtCtxt {
312 313
            parse_sess: parse_sess,
            cfg: cfg,
S
Steven Fackler 已提交
314
            backtrace: None,
315
            loader: loader,
S
Steven Fackler 已提交
316 317
            mod_path: ~[],
            trace_mac: false
318 319 320
        }
    }

S
Steven Fackler 已提交
321
    pub fn expand_expr(&mut self, mut e: @ast::Expr) -> @ast::Expr {
322 323
        loop {
            match e.node {
A
Alex Crichton 已提交
324
                ast::ExprMac(..) => {
S
Steven Fackler 已提交
325
                    let mut expander = expand::MacroExpander {
S
Steven Fackler 已提交
326
                        extsbox: syntax_expander_table(),
327 328
                        cx: self,
                    };
S
Steven Fackler 已提交
329
                    e = expand::expand_expr(e, &mut expander);
330 331 332 333 334 335
                }
                _ => return e
            }
        }
    }

336
    pub fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
337
    pub fn parse_sess(&self) -> @parse::ParseSess { self.parse_sess }
338
    pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
339
    pub fn call_site(&self) -> Span {
S
Steven Fackler 已提交
340
        match self.backtrace {
341
            Some(expn_info) => expn_info.call_site,
342
            None => self.bug("missing top span")
343
        }
344
    }
345
    pub fn print_backtrace(&self) { }
S
Steven Fackler 已提交
346 347 348 349 350
    pub fn backtrace(&self) -> Option<@ExpnInfo> { self.backtrace }
    pub fn mod_push(&mut self, i: ast::Ident) { self.mod_path.push(i); }
    pub fn mod_pop(&mut self) { self.mod_path.pop(); }
    pub fn mod_path(&self) -> ~[ast::Ident] { self.mod_path.clone() }
    pub fn bt_push(&mut self, ei: codemap::ExpnInfo) {
351
        match ei {
352
            ExpnInfo {call_site: cs, callee: ref callee} => {
S
Steven Fackler 已提交
353
                self.backtrace =
354
                    Some(@ExpnInfo {
355
                        call_site: Span {lo: cs.lo, hi: cs.hi,
S
Steven Fackler 已提交
356
                                         expn_info: self.backtrace},
357
                        callee: *callee});
K
Kevin Atkinson 已提交
358
            }
359
        }
360
    }
S
Steven Fackler 已提交
361 362
    pub fn bt_pop(&mut self) {
        match self.backtrace {
363
            Some(expn_info) => self.backtrace = expn_info.call_site.expn_info,
364
            _ => self.bug("tried to pop without a push")
365 366
        }
    }
367
    pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
368 369 370
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_fatal(sp, msg);
    }
371
    pub fn span_err(&self, sp: Span, msg: &str) {
372 373 374
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_err(sp, msg);
    }
375
    pub fn span_warn(&self, sp: Span, msg: &str) {
376 377 378
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_warn(sp, msg);
    }
379
    pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
380 381 382
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
    }
383
    pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
384 385 386
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_bug(sp, msg);
    }
387
    pub fn bug(&self, msg: &str) -> ! {
388 389 390
        self.print_backtrace();
        self.parse_sess.span_diagnostic.handler().bug(msg);
    }
391
    pub fn trace_macros(&self) -> bool {
S
Steven Fackler 已提交
392
        self.trace_mac
393
    }
S
Steven Fackler 已提交
394 395
    pub fn set_trace_macros(&mut self, x: bool) {
        self.trace_mac = x
396
    }
397
    pub fn str_of(&self, id: ast::Ident) -> @str {
398
        ident_to_str(&id)
399
    }
400
    pub fn ident_of(&self, st: &str) -> ast::Ident {
401
        str_to_ident(st)
402 403 404
    }
}

S
Steven Fackler 已提交
405
pub fn expr_to_str(cx: &ExtCtxt, expr: @ast::Expr, err_msg: &str) -> (@str, ast::StrStyle) {
406
    match expr.node {
407 408 409 410 411
        ast::ExprLit(l) => match l.node {
            ast::LitStr(s, style) => (s, style),
            _ => cx.span_fatal(l.span, err_msg)
        },
        _ => cx.span_fatal(expr.span, err_msg)
412 413 414
    }
}

415
pub fn check_zero_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree],
416
                      name: &str) {
417
    if tts.len() != 0 {
A
Alex Crichton 已提交
418
        cx.span_fatal(sp, format!("{} takes no arguments", name));
419
    }
420 421
}

S
Steven Fackler 已提交
422
pub fn get_single_str_from_tts(cx: &ExtCtxt,
423
                               sp: Span,
424
                               tts: &[ast::TokenTree],
425 426
                               name: &str)
                               -> @str {
427
    if tts.len() != 1 {
A
Alex Crichton 已提交
428
        cx.span_fatal(sp, format!("{} takes 1 argument.", name));
429 430
    }

431
    match tts[0] {
432 433
        ast::TTTok(_, token::LIT_STR(ident))
        | ast::TTTok(_, token::LIT_STR_RAW(ident, _)) => cx.str_of(ident),
A
Alex Crichton 已提交
434
        _ => cx.span_fatal(sp, format!("{} requires a string.", name)),
435 436
    }
}
437

S
Steven Fackler 已提交
438
pub fn get_exprs_from_tts(cx: &ExtCtxt,
439
                          sp: Span,
440
                          tts: &[ast::TokenTree]) -> ~[@ast::Expr] {
441 442 443
    let mut p = parse::new_parser_from_tts(cx.parse_sess(),
                                           cx.cfg(),
                                           tts.to_owned());
444
    let mut es = ~[];
445
    while p.token != token::EOF {
446 447
        if es.len() != 0 && !p.eat(&token::COMMA) {
            cx.span_fatal(sp, "expected token: `,`");
448 449
        }
        es.push(p.parse_expr());
450
    }
451
    es
452 453
}

J
John Clements 已提交
454 455 456 457
// in order to have some notion of scoping for macros,
// we want to implement the notion of a transformation
// environment.

S
Steven Fackler 已提交
458
// This environment maps Names to SyntaxExtensions.
J
John Clements 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472

// 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.

473
struct MapChainFrame {
S
Steven Fackler 已提交
474
    info: BlockInfo,
475 476 477 478 479 480 481 482 483
    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();
    }
J
John Clements 已提交
484 485
}

S
Steven Fackler 已提交
486
// Only generic to make it easy to test
487 488
pub struct SyntaxEnv {
    priv chain: ~[MapChainFrame],
S
Steven Fackler 已提交
489
}
J
John Clements 已提交
490

491 492 493
impl SyntaxEnv {
    pub fn new() -> SyntaxEnv {
        let mut map = SyntaxEnv { chain: ~[] };
S
Steven Fackler 已提交
494 495
        map.push_frame();
        map
J
John Clements 已提交
496 497
    }

S
Steven Fackler 已提交
498 499 500 501 502
    pub fn push_frame(&mut self) {
        self.chain.push(MapChainFrame {
            info: BlockInfo::new(),
            map: HashMap::new(),
        });
J
John Clements 已提交
503 504
    }

S
Steven Fackler 已提交
505 506 507
    pub fn pop_frame(&mut self) {
        assert!(self.chain.len() > 1, "too many pops on MapChain!");
        self.chain.pop();
508 509
    }

510
    fn find_escape_frame<'a>(&'a mut self) -> &'a mut MapChainFrame {
S
Steven Fackler 已提交
511 512 513 514
        for (i, frame) in self.chain.mut_iter().enumerate().invert() {
            if !frame.info.macros_escape || i == 0 {
                return frame
            }
J
John Clements 已提交
515
        }
S
Steven Fackler 已提交
516
        unreachable!()
J
John Clements 已提交
517 518
    }

519
    pub fn find<'a>(&'a self, k: &Name) -> Option<&'a SyntaxExtension> {
S
Steven Fackler 已提交
520 521 522 523
        for frame in self.chain.iter().invert() {
            match frame.map.find(k) {
                Some(v) => return Some(v),
                None => {}
J
John Clements 已提交
524 525
            }
        }
S
Steven Fackler 已提交
526
        None
J
John Clements 已提交
527 528
    }

529
    pub fn insert(&mut self, k: Name, v: SyntaxExtension) {
S
Steven Fackler 已提交
530
        self.find_escape_frame().map.insert(k, v);
J
John Clements 已提交
531 532
    }

533 534
    pub fn insert_macro_crate(&mut self, lib: DynamicLibrary) {
        self.find_escape_frame().info.macro_crates.push(lib);
535
    }
J
John Clements 已提交
536

537 538
    pub fn info<'a>(&'a mut self) -> &'a mut BlockInfo {
        &mut self.chain[self.chain.len()-1].info
J
John Clements 已提交
539 540
    }
}