rust/src/rustc/util/common.rs

101 lines
2.5 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.
2012-08-22 19:24:52 -05:00
debug!(">>");
2012-04-05 22:59:07 -05:00
let r <- op();
2012-08-22 19:24:52 -05:00
debug!("<< (Result = %?)", r);
2012-08-01 19:30:05 -05:00
return r;
2012-04-05 22:59:07 -05:00
}
2012-08-15 20:46:55 -05:00
struct _indenter {
let _i: ();
new(_i: ()) { self._i = (); }
2012-08-22 19:24:52 -05:00
drop { debug!("<<"); }
2012-04-05 22:59:07 -05:00
}
fn indenter() -> _indenter {
2012-08-22 19:24:52 -05:00
debug!(">>");
2012-04-05 22:59:07 -05:00
_indenter(())
}
type flag = hashmap<~str, ()>;
2010-08-18 13:34:47 -05:00
2012-08-01 19:30:05 -05:00
fn field_expr(f: ast::field) -> @ast::expr { return f.node.expr; }
fn field_exprs(fields: ~[ast::field]) -> ~[@ast::expr] {
let mut es = ~[];
2012-06-30 18:19:07 -05:00
for fields.each |f| { vec::push(es, f.node.expr); }
2012-08-01 19:30:05 -05:00
return 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;
2012-06-30 18:19:07 -05:00
let visit_expr =
|e: @ast::expr, &&flag: @mut bool, v: visit::vt<@mut bool>| {
*flag |= p(e.node);
2012-08-06 14:34:08 -05:00
match e.node {
// Skip inner loops, since a break in the inner loop isn't a
// break inside the outer loop
2012-08-03 21:59:04 -05:00
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-08-01 19:30:05 -05:00
return *rs;
2012-06-14 14:24:56 -05:00
}
fn has_nonlocal_exits(b: ast::blk) -> bool {
2012-08-03 21:59:04 -05:00
do loop_query(b) |e| {
2012-08-06 14:34:08 -05:00
match e {
ast::expr_break(_) | ast::expr_again(_) => true,
2012-08-03 21:59:04 -05:00
_ => false
}
}
}
fn may_break(b: ast::blk) -> bool {
2012-08-03 21:59:04 -05:00
do loop_query(b) |e| {
2012-08-06 14:34:08 -05:00
match e {
ast::expr_break(_) => true,
2012-08-03 21:59:04 -05:00
_ => false
}
}
}
fn local_rhs_span(l: @ast::local, def: span) -> span {
2012-08-06 14:34:08 -05:00
match l.node.init {
2012-08-20 14:23:37 -05:00
Some(i) => return i.expr.span,
2012-08-03 21:59:04 -05:00
_ => return 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-07-18 18:18:02 -05:00
vec::last(path) == syntax::ast_map::path_name(
syntax::parse::token::special_idents::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:
//