base.rs 17.6 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 14
use ast;
use codemap;
A
Alex Crichton 已提交
15 16
use codemap::{CodeMap, span, ExpnInfo, ExpandedFrom};
use codemap::CallInfo;
17 18 19
use diagnostic::span_handler;
use ext;
use parse;
A
Alex Crichton 已提交
20
use parse::token;
21

22
use core::hashmap::HashMap;
23
use core::vec;
24

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

34
pub struct MacroDef {
35
    name: ~str,
36
    ext: SyntaxExtension
37
}
38

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

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

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

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

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

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

75
pub enum SyntaxExtension {
76

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

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

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

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

94
pub type SyntaxEnv = @mut MapChain<Name, Transformer>;
J
John Clements 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

// Name : the domain of SyntaxEnvs
// want to change these to uints....
// note that we use certain strings that are not legal as identifiers
// to indicate, for instance, how blocks are supposed to behave.
type Name = @~str;

// Transformer : the codomain of SyntaxEnvs

// NB: it may seem crazy to lump both of these into one environment;
// what would it mean to bind "foo" to BlockLimit(true)? The idea
// is that this follows the lead of MTWT, and accommodates growth
// toward a more uniform syntax syntax (sorry) where blocks are just
// another kind of transformer.

110
pub enum Transformer {
J
John Clements 已提交
111 112 113 114 115
    // this identifier maps to a syntax extension or macro
    SE(SyntaxExtension),
    // should blocks occurring here limit macro scopes?
    ScopeMacros(bool)
}
116

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

    // Quasi-quoting expanders
158
    syntax_expanders.insert(@~"quote_tokens",
J
John Clements 已提交
159
                       builtin_normal_tt(ext::quote::expand_quote_tokens));
160
    syntax_expanders.insert(@~"quote_expr",
J
John Clements 已提交
161
                       builtin_normal_tt(ext::quote::expand_quote_expr));
162
    syntax_expanders.insert(@~"quote_ty",
J
John Clements 已提交
163
                       builtin_normal_tt(ext::quote::expand_quote_ty));
164
    syntax_expanders.insert(@~"quote_item",
J
John Clements 已提交
165
                       builtin_normal_tt(ext::quote::expand_quote_item));
166
    syntax_expanders.insert(@~"quote_pat",
J
John Clements 已提交
167
                       builtin_normal_tt(ext::quote::expand_quote_pat));
168
    syntax_expanders.insert(@~"quote_stmt",
J
John Clements 已提交
169
                       builtin_normal_tt(ext::quote::expand_quote_stmt));
170

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

// 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.
208
pub struct ExtCtxt {
209 210 211
    parse_sess: @mut parse::ParseSess,
    cfg: ast::crate_cfg,
    backtrace: @mut Option<@ExpnInfo>,
212

213 214 215 216 217 218 219 220
    // 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 已提交
221

222 223 224
impl ExtCtxt {
    pub fn new(parse_sess: @mut parse::ParseSess, cfg: ast::crate_cfg)
               -> @ExtCtxt {
225 226 227 228 229 230 231 232 233
        @ExtCtxt {
            parse_sess: parse_sess,
            cfg: cfg,
            backtrace: @mut None,
            mod_path: @mut ~[],
            trace_mac: @mut false
        }
    }

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

311
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::expr, err_msg: ~str) -> ~str {
312 313
    match expr.node {
      ast::expr_lit(l) => match l.node {
314
        ast::lit_str(s) => copy *s,
315
        _ => cx.span_fatal(l.span, err_msg)
316
      },
317
      _ => cx.span_fatal(expr.span, err_msg)
318 319 320
    }
}

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

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

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

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

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

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


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

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

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

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

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

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

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

    // insert the binding into the top-level map
474
    fn insert (&mut self, key: K, ext: @V) -> bool {
J
John Clements 已提交
475 476
        // can't abstract over get_map because of flow sensitivity...
        match *self {
J
John Clements 已提交
477 478
            BaseMapChain (~ref mut map) => map.insert(key, ext),
            ConsMapChain (~ref mut map,_) => map.insert(key,ext)
J
John Clements 已提交
479 480 481 482 483 484 485 486
        }
    }

}

#[cfg(test)]
mod test {
    use super::MapChain;
487
    use core::hashmap::HashMap;
J
John Clements 已提交
488 489

    #[test] fn testenv () {
490
        let mut a = HashMap::new();
J
John Clements 已提交
491 492 493
        a.insert (@~"abc",@15);
        let m = MapChain::new(~a);
        m.insert (@~"def",@16);
494 495 496 497
        // 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 已提交
498 499
        let n = m.push_frame();
        // old bindings are still present:
500 501
        assert_eq!(*(n.find(&@~"abc").get()),15);
        assert_eq!(*(n.find(&@~"def").get()),16);
J
John Clements 已提交
502 503
        n.insert (@~"def",@17);
        // n shows the new binding
504 505
        assert_eq!(*(n.find(&@~"abc").get()),15);
        assert_eq!(*(n.find(&@~"def").get()),17);
J
John Clements 已提交
506
        // ... but m still has the old ones
507 508 509 510
        // 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 已提交
511 512
    }
}