rust/src/rustc/util/common.rs

79 lines
1.9 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;
}
resource _indenter(_i: ()) {
#debug["<<"];
}
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| 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
fn block_expr_query(b: ast::blk, p: fn@(ast::expr_) -> bool) -> bool {
let rs = @mut false;
let visit_expr = {|flag: @mut bool, e: @ast::expr| *flag |= p(e.node)};
2011-07-27 07:19:39 -05:00
let v =
2012-06-19 21:34:01 -05:00
visit::mk_simple_visitor(@{visit_expr: {|a|visit_expr(rs, a)}
2011-07-27 07:19:39 -05:00
with *visit::default_simple_visitor()});
visit::visit_block(b, (), v);
2012-06-14 14:24:56 -05:00
ret *rs;
}
fn has_nonlocal_exits(b: ast::blk) -> bool {
block_expr_query(b) {|e| alt e {
ast::expr_break | ast::expr_cont { true }
_ { false }}}
}
fn may_break(b: ast::blk) -> bool {
2012-06-14 14:24:56 -05:00
block_expr_query(b) {|e| alt e {
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: path should be a constrained type, so we know
2012-06-14 14:24:56 -05:00
// the call to last doesn't fail (#34)
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:
//