base.rs 19.4 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 12
use core::prelude::*;

13
use ast;
14
use ast::Name;
15
use codemap;
A
Alex Crichton 已提交
16 17
use codemap::{CodeMap, span, ExpnInfo, ExpandedFrom};
use codemap::CallInfo;
18 19 20
use diagnostic::span_handler;
use ext;
use parse;
A
Alex Crichton 已提交
21
use parse::token;
J
John Clements 已提交
22
use parse::token::{intern, get_ident_interner};
23

24
use core::hashmap::HashMap;
25
use core::vec;
26

27 28
// new-style macro! tt code:
//
29
//    SyntaxExpanderTT, SyntaxExpanderTTItem, MacResult,
J
John Clements 已提交
30
//    NormalTT, IdentTT
31
//
32 33 34
// also note that ast::mac used to have a bunch of extraneous cases and
// is now probably a redundant AST node, can be merged with
// ast::mac_invoc_tt.
35

36
pub struct MacroDef {
37
    name: ~str,
38
    ext: SyntaxExtension
39
}
40

41
pub type ItemDecorator = @fn(@ExtCtxt,
42 43 44 45
                             span,
                             @ast::meta_item,
                             ~[@ast::item])
                          -> ~[@ast::item];
46

47
pub struct SyntaxExpanderTT {
48 49
    expander: SyntaxExpanderTTFun,
    span: Option<span>
50 51
}

52
pub type SyntaxExpanderTTFun = @fn(@ExtCtxt,
53 54 55
                                   span,
                                   &[ast::token_tree])
                                -> MacResult;
56

57
pub struct SyntaxExpanderTTItem {
58 59
    expander: SyntaxExpanderTTItemFun,
    span: Option<span>
60 61
}

62
pub type SyntaxExpanderTTItemFun = @fn(@ExtCtxt,
63 64 65 66
                                       span,
                                       ast::ident,
                                       ~[ast::token_tree])
                                    -> MacResult;
67

68
pub enum MacResult {
69 70
    MRExpr(@ast::expr),
    MRItem(@ast::item),
71 72 73
    MRAny(@fn() -> @ast::expr,
          @fn() -> Option<@ast::item>,
          @fn() -> @ast::stmt),
J
John Clements 已提交
74
    MRDef(MacroDef)
75
}
76

77
pub enum SyntaxExtension {
78

79
    // #[auto_encode] and such
80
    ItemDecorator(ItemDecorator),
81

82
    // Token-tree expanders
83
    NormalTT(SyntaxExpanderTT),
84

J
John Clements 已提交
85 86 87 88 89 90
    // An IdentTT is a macro that has an
    // identifier in between the name of the
    // macro and the argument. Currently,
    // the only examples of this are
    // macro_rules! and proto!

91 92
    // perhaps macro_rules! will lose its odd special identifier argument,
    // and this can go away also
J
John Clements 已提交
93
    IdentTT(SyntaxExpanderTTItem),
94
}
95

96 97 98
// The SyntaxEnv is the environment that's threaded through the expansion
// of macros. It contains bindings for macros, and also a special binding
// for " block" (not a legal identifier) that maps to a BlockInfo
99
pub type SyntaxEnv = @mut MapChain<Name, Transformer>;
J
John Clements 已提交
100 101 102

// Transformer : the codomain of SyntaxEnvs

103
pub enum Transformer {
J
John Clements 已提交
104 105
    // this identifier maps to a syntax extension or macro
    SE(SyntaxExtension),
106 107 108 109 110 111 112 113 114 115 116 117
    // blockinfo : this is ... well, it's simpler than threading
    // another whole data stack-structured data structure through
    // expansion. Basically, there's an invariant that every
    // map must contain a binding for " block".
    BlockInfo(BlockInfo)
}

pub struct BlockInfo {
    // should macros escape from this scope?
    macros_escape : bool,
    // what are the pending renames?
    pending_renames : @mut RenameList
J
John Clements 已提交
118
}
119

120 121 122
// a list of ident->name renamings
type RenameList = ~[(ast::ident,Name)];

J
John Clements 已提交
123
// The base map of methods for expanding syntax extension
124
// AST nodes into full ASTs
J
John Clements 已提交
125
pub fn syntax_expander_table() -> SyntaxEnv {
J
John Clements 已提交
126
    // utility function to simplify creating NormalTT syntax extensions
J
John Clements 已提交
127 128
    fn builtin_normal_tt(f: SyntaxExpanderTTFun) -> @Transformer {
        @SE(NormalTT(SyntaxExpanderTT{expander: f, span: None}))
129
    }
J
John Clements 已提交
130 131 132
    // utility function to simplify creating IdentTT syntax extensions
    fn builtin_item_tt(f: SyntaxExpanderTTItemFun) -> @Transformer {
        @SE(IdentTT(SyntaxExpanderTTItem{expander: f, span: None}))
133
    }
134
    let mut syntax_expanders = HashMap::new();
J
John Clements 已提交
135
    // NB identifier starts with space, and can't conflict with legal idents
136 137 138 139 140 141
    syntax_expanders.insert(intern(&" block"),
                            @BlockInfo(BlockInfo{
                                macros_escape : false,
                                pending_renames : @mut ~[]
                            }));
    syntax_expanders.insert(intern(&"macro_rules"),
142 143
                            builtin_item_tt(
                                ext::tt::macro_rules::add_new_extension));
144
    syntax_expanders.insert(intern(&"fmt"),
145
                            builtin_normal_tt(ext::fmt::expand_syntax_ext));
146
    syntax_expanders.insert(
147
        intern(&"auto_encode"),
J
John Clements 已提交
148
        @SE(ItemDecorator(ext::auto_encode::expand_auto_encode)));
149
    syntax_expanders.insert(
150
        intern(&"auto_decode"),
J
John Clements 已提交
151
        @SE(ItemDecorator(ext::auto_encode::expand_auto_decode)));
152
    syntax_expanders.insert(intern(&"env"),
153
                            builtin_normal_tt(ext::env::expand_syntax_ext));
154
    syntax_expanders.insert(intern("bytes"),
S
Sangeun Kim 已提交
155
                            builtin_normal_tt(ext::bytes::expand_syntax_ext));
156
    syntax_expanders.insert(intern("concat_idents"),
157 158
                            builtin_normal_tt(
                                ext::concat_idents::expand_syntax_ext));
159
    syntax_expanders.insert(intern(&"log_syntax"),
160
                            builtin_normal_tt(
161
                                ext::log_syntax::expand_syntax_ext));
162
    syntax_expanders.insert(intern(&"deriving"),
163 164
                            @SE(ItemDecorator(
                                ext::deriving::expand_meta_deriving)));
165 166

    // Quasi-quoting expanders
167
    syntax_expanders.insert(intern(&"quote_tokens"),
J
John Clements 已提交
168
                       builtin_normal_tt(ext::quote::expand_quote_tokens));
169
    syntax_expanders.insert(intern(&"quote_expr"),
J
John Clements 已提交
170
                       builtin_normal_tt(ext::quote::expand_quote_expr));
171
    syntax_expanders.insert(intern(&"quote_ty"),
J
John Clements 已提交
172
                       builtin_normal_tt(ext::quote::expand_quote_ty));
173
    syntax_expanders.insert(intern(&"quote_item"),
J
John Clements 已提交
174
                       builtin_normal_tt(ext::quote::expand_quote_item));
175
    syntax_expanders.insert(intern(&"quote_pat"),
J
John Clements 已提交
176
                       builtin_normal_tt(ext::quote::expand_quote_pat));
177
    syntax_expanders.insert(intern(&"quote_stmt"),
J
John Clements 已提交
178
                       builtin_normal_tt(ext::quote::expand_quote_stmt));
179

180
    syntax_expanders.insert(intern(&"line"),
181 182
                            builtin_normal_tt(
                                ext::source_util::expand_line));
183
    syntax_expanders.insert(intern(&"col"),
184 185
                            builtin_normal_tt(
                                ext::source_util::expand_col));
186
    syntax_expanders.insert(intern(&"file"),
187 188
                            builtin_normal_tt(
                                ext::source_util::expand_file));
189
    syntax_expanders.insert(intern(&"stringify"),
190 191
                            builtin_normal_tt(
                                ext::source_util::expand_stringify));
192
    syntax_expanders.insert(intern(&"include"),
193 194
                            builtin_normal_tt(
                                ext::source_util::expand_include));
195
    syntax_expanders.insert(intern(&"include_str"),
196 197
                            builtin_normal_tt(
                                ext::source_util::expand_include_str));
198
    syntax_expanders.insert(intern(&"include_bin"),
199 200
                            builtin_normal_tt(
                                ext::source_util::expand_include_bin));
201
    syntax_expanders.insert(intern(&"module_path"),
202 203
                            builtin_normal_tt(
                                ext::source_util::expand_mod));
204
    syntax_expanders.insert(intern(&"proto"),
205
                            builtin_item_tt(ext::pipes::expand_proto));
206
    syntax_expanders.insert(intern(&"asm"),
L
Luqman Aden 已提交
207
                            builtin_normal_tt(ext::asm::expand_asm));
208
    syntax_expanders.insert(
209
        intern(&"trace_macros"),
210
        builtin_normal_tt(ext::trace_macros::expand_trace_macros));
J
John Clements 已提交
211
    MapChain::new(~syntax_expanders)
212
}
213 214 215 216

// 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.
217
pub struct ExtCtxt {
218 219 220
    parse_sess: @mut parse::ParseSess,
    cfg: ast::crate_cfg,
    backtrace: @mut Option<@ExpnInfo>,
221

222 223 224 225 226 227 228 229
    // These two @mut's should really not be here,
    // but the self types for CtxtRepr are all wrong
    // and there are bugs in the code for object
    // types that make this hard to get right at the
    // moment. - nmatsakis
    mod_path: @mut ~[ast::ident],
    trace_mac: @mut bool
}
N
Niko Matsakis 已提交
230

231 232 233
impl ExtCtxt {
    pub fn new(parse_sess: @mut parse::ParseSess, cfg: ast::crate_cfg)
               -> @ExtCtxt {
234 235 236 237 238 239 240 241 242
        @ExtCtxt {
            parse_sess: parse_sess,
            cfg: cfg,
            backtrace: @mut None,
            mod_path: @mut ~[],
            trace_mac: @mut false
        }
    }

243 244 245 246
    pub fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
    pub fn parse_sess(&self) -> @mut parse::ParseSess { self.parse_sess }
    pub fn cfg(&self) -> ast::crate_cfg { copy self.cfg }
    pub fn call_site(&self) -> span {
247 248 249
        match *self.backtrace {
            Some(@ExpandedFrom(CallInfo {call_site: cs, _})) => cs,
            None => self.bug("missing top span")
250
        }
251
    }
252 253 254 255 256 257
    pub fn print_backtrace(&self) { }
    pub fn backtrace(&self) -> Option<@ExpnInfo> { *self.backtrace }
    pub fn mod_push(&self, i: ast::ident) { self.mod_path.push(i); }
    pub fn mod_pop(&self) { self.mod_path.pop(); }
    pub fn mod_path(&self) -> ~[ast::ident] { copy *self.mod_path }
    pub fn bt_push(&self, ei: codemap::ExpnInfo) {
258 259
        match ei {
            ExpandedFrom(CallInfo {call_site: cs, callee: ref callee}) => {
P
Patrick Walton 已提交
260
                *self.backtrace =
261
                    Some(@ExpandedFrom(CallInfo {
262
                        call_site: span {lo: cs.lo, hi: cs.hi,
P
Patrick Walton 已提交
263
                                         expn_info: *self.backtrace},
264
                        callee: copy *callee}));
K
Kevin Atkinson 已提交
265
            }
266
        }
267
    }
268
    pub fn bt_pop(&self) {
269 270 271 272 273
        match *self.backtrace {
            Some(@ExpandedFrom(
                CallInfo {
                    call_site: span {expn_info: prev, _}, _
                })) => {
P
Patrick Walton 已提交
274
                *self.backtrace = prev
275
            }
276
            _ => self.bug("tried to pop without a push")
277 278
        }
    }
279
    pub fn span_fatal(&self, sp: span, msg: &str) -> ! {
280 281 282
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_fatal(sp, msg);
    }
283
    pub fn span_err(&self, sp: span, msg: &str) {
284 285 286
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_err(sp, msg);
    }
287
    pub fn span_warn(&self, sp: span, msg: &str) {
288 289 290
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_warn(sp, msg);
    }
291
    pub fn span_unimpl(&self, sp: span, msg: &str) -> ! {
292 293 294
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
    }
295
    pub fn span_bug(&self, sp: span, msg: &str) -> ! {
296 297 298
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_bug(sp, msg);
    }
299
    pub fn bug(&self, msg: &str) -> ! {
300 301 302
        self.print_backtrace();
        self.parse_sess.span_diagnostic.handler().bug(msg);
    }
303
    pub fn next_id(&self) -> ast::node_id {
304 305
        parse::next_node_id(self.parse_sess)
    }
306
    pub fn trace_macros(&self) -> bool {
307 308
        *self.trace_mac
    }
309
    pub fn set_trace_macros(&self, x: bool) {
310 311
        *self.trace_mac = x
    }
312
    pub fn str_of(&self, id: ast::ident) -> ~str {
J
John Clements 已提交
313
        copy *get_ident_interner().get(id)
314
    }
315
    pub fn ident_of(&self, st: &str) -> ast::ident {
J
John Clements 已提交
316
        get_ident_interner().intern(st)
317 318 319
    }
}

320
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::expr, err_msg: ~str) -> ~str {
321 322
    match expr.node {
      ast::expr_lit(l) => match l.node {
323
        ast::lit_str(s) => copy *s,
324
        _ => cx.span_fatal(l.span, err_msg)
325
      },
326
      _ => cx.span_fatal(expr.span, err_msg)
327 328 329
    }
}

330
pub fn expr_to_ident(cx: @ExtCtxt,
331
                     expr: @ast::expr,
J
Jeong YunWon 已提交
332
                     err_msg: &str) -> ast::ident {
333
    match expr.node {
B
Brian Anderson 已提交
334
      ast::expr_path(p) => {
Y
Youngmin Yoo 已提交
335
        if p.types.len() > 0u || p.idents.len() != 1u {
336 337 338
            cx.span_fatal(expr.span, err_msg);
        }
        return p.idents[0];
M
Marijn Haverbeke 已提交
339
      }
340
      _ => cx.span_fatal(expr.span, err_msg)
341 342 343
    }
}

344
pub fn check_zero_tts(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree],
345
                      name: &str) {
346 347 348
    if tts.len() != 0 {
        cx.span_fatal(sp, fmt!("%s takes no arguments", name));
    }
349 350
}

351
pub fn get_single_str_from_tts(cx: @ExtCtxt,
352 353 354
                               sp: span,
                               tts: &[ast::token_tree],
                               name: &str) -> ~str {
355 356
    if tts.len() != 1 {
        cx.span_fatal(sp, fmt!("%s takes 1 argument.", name));
357 358
    }

359 360 361 362
    match tts[0] {
        ast::tt_tok(_, token::LIT_STR(ident)) => cx.str_of(ident),
        _ =>
        cx.span_fatal(sp, fmt!("%s requires a string.", name))
363 364
    }
}
365

366
pub fn get_exprs_from_tts(cx: @ExtCtxt, tts: &[ast::token_tree])
367
                       -> ~[@ast::expr] {
368 369
    let p = parse::new_parser_from_tts(cx.parse_sess(),
                                       cx.cfg(),
370
                                       vec::to_owned(tts));
371
    let mut es = ~[];
372
    while *p.token != token::EOF {
373
        if es.len() != 0 {
374
            p.eat(&token::COMMA);
375 376
        }
        es.push(p.parse_expr());
377
    }
378
    es
379 380
}

J
John Clements 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
// in order to have some notion of scoping for macros,
// we want to implement the notion of a transformation
// environment.

// This environment maps Names to Transformers.
// Initially, this includes macro definitions and
// block directives.



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

// Note on choice of representation: I've been pushed to
// use a top-level managed pointer by some difficulties
// with pushing and popping functionally, and the ownership
// issues.  As a result, the values returned by the table
408
// also need to be managed; the &'self ... type that Maps
J
John Clements 已提交
409 410 411 412 413 414 415 416
// return won't work for things that need to get outside
// of that managed pointer.  The easiest way to do this
// is just to insist that the values in the tables are
// managed to begin with.

// a transformer env is either a base map or a map on top
// of another chain.
pub enum MapChain<K,V> {
417 418
    BaseMapChain(~HashMap<K,@V>),
    ConsMapChain(~HashMap<K,@V>,@mut MapChain<K,V>)
J
John Clements 已提交
419 420 421 422 423 424 425
}


// get the map from an env frame
impl <K: Eq + Hash + IterBytes ,V: Copy> MapChain<K,V>{

    // Constructor. I don't think we need a zero-arg one.
426
    fn new(init: ~HashMap<K,@V>) -> @mut MapChain<K,V> {
J
John Clements 已提交
427
        @mut BaseMapChain(init)
J
John Clements 已提交
428 429 430 431
    }

    // add a new frame to the environment (functionally)
    fn push_frame (@mut self) -> @mut MapChain<K,V> {
432
        @mut ConsMapChain(~HashMap::new() ,self)
J
John Clements 已提交
433 434 435 436 437 438 439 440
    }

// no need for pop, it'll just be functional.

    // utility fn...

    // ugh: can't get this to compile with mut because of the
    // lack of flow sensitivity.
441 442 443 444 445 446 447
    fn get_map<'a>(&'a self) -> &'a HashMap<K,@V> {
        match *self {
            BaseMapChain (~ref map) => map,
            ConsMapChain (~ref map,_) => map
        }
    }

J
John Clements 已提交
448
// traits just don't work anywhere...?
449
//impl Map<Name,SyntaxExtension> for MapChain {
J
John Clements 已提交
450

451
    fn contains_key (&self, key: &K) -> bool {
J
John Clements 已提交
452
        match *self {
J
John Clements 已提交
453 454
            BaseMapChain (ref map) => map.contains_key(key),
            ConsMapChain (ref map,ref rest) =>
J
John Clements 已提交
455 456 457 458 459 460 461
            (map.contains_key(key)
             || rest.contains_key(key))
        }
    }
    // should each_key and each_value operate on shadowed
    // names? I think not.
    // delaying implementing this....
462
    fn each_key (&self, _f: &fn (&K)->bool) {
463
        fail!("unimplemented 2013-02-15T10:01");
J
John Clements 已提交
464 465
    }

466
    fn each_value (&self, _f: &fn (&V) -> bool) {
467
        fail!("unimplemented 2013-02-15T10:02");
J
John Clements 已提交
468 469 470 471 472 473 474 475
    }

    // Returns a copy of the value that the name maps to.
    // Goes down the chain 'til it finds one (or bottom out).
    fn find (&self, key: &K) -> Option<@V> {
        match self.get_map().find (key) {
            Some(ref v) => Some(**v),
            None => match *self {
J
John Clements 已提交
476 477
                BaseMapChain (_) => None,
                ConsMapChain (_,ref rest) => rest.find(key)
J
John Clements 已提交
478 479 480 481 482
            }
        }
    }

    // insert the binding into the top-level map
483
    fn insert (&mut self, key: K, ext: @V) -> bool {
J
John Clements 已提交
484 485
        // can't abstract over get_map because of flow sensitivity...
        match *self {
J
John Clements 已提交
486 487
            BaseMapChain (~ref mut map) => map.insert(key, ext),
            ConsMapChain (~ref mut map,_) => map.insert(key,ext)
J
John Clements 已提交
488 489
        }
    }
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
    // insert the binding into the topmost frame for which the binding
    // associated with 'n' exists and satisfies pred
    // ... there are definitely some opportunities for abstraction
    // here that I'm ignoring. (e.g., manufacturing a predicate on
    // the maps in the chain, and using an abstract "find".
    fn insert_into_frame(&mut self, key: K, ext: @V, n: K, pred: &fn(&@V)->bool) {
        match *self {
            BaseMapChain (~ref mut map) => {
                if satisfies_pred(map,&n,pred) {
                    map.insert(key,ext);
                } else {
                    fail!(~"expected map chain containing satisfying frame")
                }
            },
            ConsMapChain (~ref mut map, rest) => {
                if satisfies_pred(map,&n,pred) {
                    map.insert(key,ext);
                } else {
                    rest.insert_into_frame(key,ext,n,pred)
                }
            }
        }
    }
}
J
John Clements 已提交
514

515 516 517 518 519 520 521 522
fn satisfies_pred<K : Eq + Hash + IterBytes,V>(map : &mut HashMap<K,V>,
                                               n: &K,
                                               pred: &fn(&V)->bool)
    -> bool {
    match map.find(n) {
        Some(ref v) => (pred(*v)),
        None => false
    }
J
John Clements 已提交
523 524 525 526 527
}

#[cfg(test)]
mod test {
    use super::MapChain;
528
    use core::hashmap::HashMap;
J
John Clements 已提交
529 530

    #[test] fn testenv () {
531
        let mut a = HashMap::new();
J
John Clements 已提交
532 533 534
        a.insert (@~"abc",@15);
        let m = MapChain::new(~a);
        m.insert (@~"def",@16);
535 536 537 538
        // FIXME: #4492 (ICE)  assert_eq!(m.find(&@~"abc"),Some(@15));
        //  ....               assert_eq!(m.find(&@~"def"),Some(@16));
        assert_eq!(*(m.find(&@~"abc").get()),15);
        assert_eq!(*(m.find(&@~"def").get()),16);
J
John Clements 已提交
539 540
        let n = m.push_frame();
        // old bindings are still present:
541 542
        assert_eq!(*(n.find(&@~"abc").get()),15);
        assert_eq!(*(n.find(&@~"def").get()),16);
J
John Clements 已提交
543 544
        n.insert (@~"def",@17);
        // n shows the new binding
545 546
        assert_eq!(*(n.find(&@~"abc").get()),15);
        assert_eq!(*(n.find(&@~"def").get()),17);
J
John Clements 已提交
547
        // ... but m still has the old ones
548 549 550 551
        // FIXME: #4492: assert_eq!(m.find(&@~"abc"),Some(@15));
        // FIXME: #4492: assert_eq!(m.find(&@~"def"),Some(@16));
        assert_eq!(*(m.find(&@~"abc").get()),15);
        assert_eq!(*(m.find(&@~"def").get()),16);
J
John Clements 已提交
552 553
    }
}