base.rs 19.5 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 16 17
use diagnostic::span_handler;
use ext;
use parse;
A
Alex Crichton 已提交
18
use parse::token;
19
use parse::token::{ident_to_str, intern, str_to_ident};
20

21 22
use std::vec;
use std::hashmap::HashMap;
23

24 25
// new-style macro! tt code:
//
26
//    SyntaxExpanderTT, SyntaxExpanderTTItem, MacResult,
J
John Clements 已提交
27
//    NormalTT, IdentTT
28
//
29 30 31
// 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.
32

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

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

44
pub struct SyntaxExpanderTT {
45 46
    expander: SyntaxExpanderTTFun,
    span: Option<span>
47 48
}

49
pub type SyntaxExpanderTTFun = @fn(@ExtCtxt,
50 51 52
                                   span,
                                   &[ast::token_tree])
                                -> MacResult;
53

54
pub struct SyntaxExpanderTTItem {
55 56
    expander: SyntaxExpanderTTItemFun,
    span: Option<span>
57 58
}

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

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

74
pub enum SyntaxExtension {
75

76
    // #[auto_encode] and such
77
    ItemDecorator(ItemDecorator),
78

79
    // Token-tree expanders
80
    NormalTT(SyntaxExpanderTT),
81

J
John Clements 已提交
82 83 84 85 86 87
    // 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!

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

93 94 95
// 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
96
pub type SyntaxEnv = @mut MapChain<Name, Transformer>;
J
John Clements 已提交
97 98 99

// Transformer : the codomain of SyntaxEnvs

100
pub enum Transformer {
J
John Clements 已提交
101 102
    // this identifier maps to a syntax extension or macro
    SE(SyntaxExtension),
103 104 105 106 107 108 109 110 111 112 113 114
    // 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 已提交
115
}
116

117 118 119
// a list of ident->name renamings
type RenameList = ~[(ast::ident,Name)];

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

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

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

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

219 220 221 222 223 224 225 226
    // 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 已提交
227

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

240 241 242 243
    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 {
244
        match *self.backtrace {
245
            Some(@ExpnInfo {call_site: cs, _}) => cs,
246
            None => self.bug("missing top span")
247
        }
248
    }
249 250 251 252 253 254
    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) {
255
        match ei {
256
            ExpnInfo {call_site: cs, callee: ref callee} => {
P
Patrick Walton 已提交
257
                *self.backtrace =
258
                    Some(@ExpnInfo {
259
                        call_site: span {lo: cs.lo, hi: cs.hi,
P
Patrick Walton 已提交
260
                                         expn_info: *self.backtrace},
261
                        callee: copy *callee});
K
Kevin Atkinson 已提交
262
            }
263
        }
264
    }
265
    pub fn bt_pop(&self) {
266
        match *self.backtrace {
267 268
            Some(@ExpnInfo {
                call_site: span {expn_info: prev, _}, _}) => {
P
Patrick Walton 已提交
269
                *self.backtrace = prev
270
            }
271
            _ => self.bug("tried to pop without a push")
272 273
        }
    }
274
    pub fn span_fatal(&self, sp: span, msg: &str) -> ! {
275 276 277
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_fatal(sp, msg);
    }
278
    pub fn span_err(&self, sp: span, msg: &str) {
279 280 281
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_err(sp, msg);
    }
282
    pub fn span_warn(&self, sp: span, msg: &str) {
283 284 285
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_warn(sp, msg);
    }
286
    pub fn span_unimpl(&self, sp: span, msg: &str) -> ! {
287 288 289
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
    }
290
    pub fn span_bug(&self, sp: span, msg: &str) -> ! {
291 292 293
        self.print_backtrace();
        self.parse_sess.span_diagnostic.span_bug(sp, msg);
    }
294
    pub fn bug(&self, msg: &str) -> ! {
295 296 297
        self.print_backtrace();
        self.parse_sess.span_diagnostic.handler().bug(msg);
    }
298
    pub fn next_id(&self) -> ast::node_id {
299 300
        parse::next_node_id(self.parse_sess)
    }
301
    pub fn trace_macros(&self) -> bool {
302 303
        *self.trace_mac
    }
304
    pub fn set_trace_macros(&self, x: bool) {
305 306
        *self.trace_mac = x
    }
307 308
    pub fn str_of(&self, id: ast::ident) -> @str {
        ident_to_str(&id)
309
    }
310
    pub fn ident_of(&self, st: &str) -> ast::ident {
311
        str_to_ident(st)
312 313 314
    }
}

315
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::expr, err_msg: ~str) -> @str {
316 317
    match expr.node {
      ast::expr_lit(l) => match l.node {
318
        ast::lit_str(s) => s,
319
        _ => cx.span_fatal(l.span, err_msg)
320
      },
321
      _ => cx.span_fatal(expr.span, err_msg)
322 323 324
    }
}

325
pub fn expr_to_ident(cx: @ExtCtxt,
326
                     expr: @ast::expr,
J
Jeong YunWon 已提交
327
                     err_msg: &str) -> ast::ident {
328
    match expr.node {
J
James Miller 已提交
329
      ast::expr_path(ref p) => {
Y
Youngmin Yoo 已提交
330
        if p.types.len() > 0u || p.idents.len() != 1u {
331 332 333
            cx.span_fatal(expr.span, err_msg);
        }
        return p.idents[0];
M
Marijn Haverbeke 已提交
334
      }
335
      _ => cx.span_fatal(expr.span, err_msg)
336 337 338
    }
}

339
pub fn check_zero_tts(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree],
340
                      name: &str) {
341 342 343
    if tts.len() != 0 {
        cx.span_fatal(sp, fmt!("%s takes no arguments", name));
    }
344 345
}

346
pub fn get_single_str_from_tts(cx: @ExtCtxt,
347 348
                               sp: span,
                               tts: &[ast::token_tree],
349
                               name: &str) -> @str {
350 351
    if tts.len() != 1 {
        cx.span_fatal(sp, fmt!("%s takes 1 argument.", name));
352 353
    }

354 355 356 357
    match tts[0] {
        ast::tt_tok(_, token::LIT_STR(ident)) => cx.str_of(ident),
        _ =>
        cx.span_fatal(sp, fmt!("%s requires a string.", name))
358 359
    }
}
360

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

J
John Clements 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
// 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
403
// also need to be managed; the &'self ... type that Maps
J
John Clements 已提交
404 405 406 407 408 409 410 411
// 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> {
412 413
    BaseMapChain(~HashMap<K,@V>),
    ConsMapChain(~HashMap<K,@V>,@mut MapChain<K,V>)
J
John Clements 已提交
414 415 416 417 418 419 420
}


// 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.
421
    fn new(init: ~HashMap<K,@V>) -> @mut MapChain<K,V> {
J
John Clements 已提交
422
        @mut BaseMapChain(init)
J
John Clements 已提交
423 424 425 426
    }

    // add a new frame to the environment (functionally)
    fn push_frame (@mut self) -> @mut MapChain<K,V> {
427
        @mut ConsMapChain(~HashMap::new() ,self)
J
John Clements 已提交
428 429 430 431 432 433 434 435
    }

// 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.
436 437 438 439 440 441 442
    fn get_map<'a>(&'a self) -> &'a HashMap<K,@V> {
        match *self {
            BaseMapChain (~ref map) => map,
            ConsMapChain (~ref map,_) => map
        }
    }

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

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

461
    fn each_value (&self, _f: &fn (&V) -> bool) {
462
        fail!("unimplemented 2013-02-15T10:02");
J
John Clements 已提交
463 464 465 466 467 468 469 470
    }

    // 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 已提交
471 472
                BaseMapChain (_) => None,
                ConsMapChain (_,ref rest) => rest.find(key)
J
John Clements 已提交
473 474 475 476
            }
        }
    }

J
John Clements 已提交
477 478 479 480 481 482 483 484 485
    fn find_in_topmost_frame(&self, key: &K) -> Option<@V> {
        let map = match *self {
            BaseMapChain(ref map) => map,
            ConsMapChain(ref map,_) => map
        };
        // strip one layer of indirection off the pointer.
        map.find(key).map(|r| {**r})
    }

J
John Clements 已提交
486
    // insert the binding into the top-level map
487
    fn insert (&mut self, key: K, ext: @V) -> bool {
J
John Clements 已提交
488 489
        // can't abstract over get_map because of flow sensitivity...
        match *self {
J
John Clements 已提交
490 491
            BaseMapChain (~ref mut map) => map.insert(key, ext),
            ConsMapChain (~ref mut map,_) => map.insert(key,ext)
J
John Clements 已提交
492 493
        }
    }
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    // 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) => {
509
                if satisfies_pred(map,&n,|v|pred(v)) {
510 511 512 513 514 515 516 517
                    map.insert(key,ext);
                } else {
                    rest.insert_into_frame(key,ext,n,pred)
                }
            }
        }
    }
}
J
John Clements 已提交
518

J
John Clements 已提交
519
// returns true if the binding for 'n' satisfies 'pred' in 'map'
520 521 522 523 524 525 526 527
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 已提交
528 529 530 531 532
}

#[cfg(test)]
mod test {
    use super::MapChain;
533
    use std::hashmap::HashMap;
J
John Clements 已提交
534 535

    #[test] fn testenv () {
536
        let mut a = HashMap::new();
537
        a.insert (@"abc",@15);
J
John Clements 已提交
538
        let m = MapChain::new(~a);
539 540 541 542 543
        m.insert (@"def",@16);
        // 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 已提交
544 545
        let n = m.push_frame();
        // old bindings are still present:
546 547 548
        assert_eq!(*(n.find(&@"abc").get()),15);
        assert_eq!(*(n.find(&@"def").get()),16);
        n.insert (@"def",@17);
J
John Clements 已提交
549
        // n shows the new binding
550 551
        assert_eq!(*(n.find(&@"abc").get()),15);
        assert_eq!(*(n.find(&@"def").get()),17);
J
John Clements 已提交
552
        // ... but m still has the old ones
553 554 555 556
        // 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 已提交
557 558
    }
}