rust/src/rustc/util/common.rs

89 lines
2.3 KiB
Rust
Raw Normal View History

import std::map::hashmap;
import syntax::ast;
2011-11-10 10:41:42 -06:00
import ast::{ty, pat};
import syntax::codemap::{span};
import syntax::visit;
import syntax::print;
2012-04-05 22:59:07 -05:00
fn indent<R>(op: fn() -> R) -> R {
// Use in conjunction with the log post-processor like `src/etc/indenter`
// to make debug output more readable.
#debug[">>"];
let r <- op();
#debug["<< (Result = %?)", r];
ret r;
}
class _indenter {
let _i: ();
new(_i: ()) { self._i = (); }
drop { #debug["<<"]; }
2012-04-05 22:59:07 -05:00
}
fn indenter() -> _indenter {
#debug[">>"];
_indenter(())
}
type flag = hashmap<str, ()>;
2010-08-18 13:34:47 -05:00
fn field_expr(f: ast::field) -> @ast::expr { ret f.node.expr; }
fn field_exprs(fields: [ast::field]/~) -> [@ast::expr]/~ {
let mut es = []/~;
for fields.each {|f| vec::push(es, f.node.expr); }
ret es;
}
2012-06-14 14:24:56 -05:00
// Takes a predicate p, returns true iff p is true for any subexpressions
// of b -- skipping any inner loops (loop, while, loop_body)
fn loop_query(b: ast::blk, p: fn@(ast::expr_) -> bool) -> bool {
2012-06-14 14:24:56 -05:00
let rs = @mut false;
let visit_expr = {|e: @ast::expr, &&flag: @mut bool,
v: visit::vt<@mut bool>|
*flag |= p(e.node);
alt e.node {
// Skip inner loops, since a break in the inner loop isn't a
// break inside the outer loop
ast::expr_loop(*) | ast::expr_while(*) | ast::expr_loop_body(*) {}
_ { visit::visit_expr(e, flag, v); }
}
};
let v = visit::mk_vt(@{visit_expr: visit_expr
with *visit::default_visitor()});
visit::visit_block(b, rs, v);
2012-06-14 14:24:56 -05:00
ret *rs;
}
fn has_nonlocal_exits(b: ast::blk) -> bool {
loop_query(b) {|e| alt e {
2012-06-14 14:24:56 -05:00
ast::expr_break | ast::expr_cont { true }
_ { false }}}
}
fn may_break(b: ast::blk) -> bool {
loop_query(b) {|e| alt e {
2012-06-14 14:24:56 -05:00
ast::expr_break { true }
_ { false }}}
}
fn local_rhs_span(l: @ast::local, def: span) -> span {
2011-07-27 07:19:39 -05:00
alt l.node.init { some(i) { ret i.expr.span; } _ { ret def; } }
2011-05-18 17:38:38 -05:00
}
fn is_main_name(path: syntax::ast_map::path) -> bool {
// FIXME (#34): path should be a constrained type, so we know
// the call to last doesn't fail.
2012-06-10 02:49:59 -05:00
vec::last(path) == syntax::ast_map::path_name(@"main")
}
2010-08-18 13:34:47 -05:00
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//