1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use syntax::codemap::{Span};
15 use syntax::visit::Visitor;
17 use std::hashmap::HashSet;
20 pub fn time<T>(do_it: bool, what: ~str, thunk: &fn() -> T) -> T {
21 if !do_it { return thunk(); }
22 let start = extra::time::precise_time_s();
24 let end = extra::time::precise_time_s();
25 printfln!("time: %3.3f s\t%s", end - start, what);
29 pub fn indent<R>(op: &fn() -> R) -> R {
30 // Use in conjunction with the log post-processor like `src/etc/indenter`
31 // to make debug output more readable.
34 debug!("<< (Result = %?)", r);
38 pub struct _indenter {
42 impl Drop for _indenter {
43 fn drop(&self) { debug!("<<"); }
46 pub fn _indenter(_i: ()) -> _indenter {
52 pub fn indenter() -> _indenter {
57 pub fn field_expr(f: ast::Field) -> @ast::Expr { return f.expr; }
59 pub fn field_exprs(fields: ~[ast::Field]) -> ~[@ast::Expr] {
60 fields.map(|f| f.expr)
63 struct LoopQueryVisitor {
64 p: @fn(&ast::Expr_) -> bool
67 impl Visitor<@mut bool> for LoopQueryVisitor {
68 fn visit_expr(&mut self, e:@ast::Expr, flag:@mut bool) {
69 *flag |= (self.p)(&e.node);
71 // Skip inner loops, since a break in the inner loop isn't a
72 // break inside the outer loop
73 ast::ExprLoop(*) | ast::ExprWhile(*) => {}
74 _ => visit::walk_expr(self, e, flag)
79 // Takes a predicate p, returns true iff p is true for any subexpressions
80 // of b -- skipping any inner loops (loop, while, loop_body)
81 pub fn loop_query(b: &ast::Block, p: @fn(&ast::Expr_) -> bool) -> bool {
83 let mut v = LoopQueryVisitor { p: p };
84 visit::walk_block(&mut v, b, rs);
88 struct BlockQueryVisitor {
89 p: @fn(@ast::Expr) -> bool
92 impl Visitor<@mut bool> for BlockQueryVisitor {
93 fn visit_expr(&mut self, e:@ast::Expr, flag:@mut bool) {
95 visit::walk_expr(self, e, flag)
99 // Takes a predicate p, returns true iff p is true for any subexpressions
100 // of b -- skipping any inner loops (loop, while, loop_body)
101 pub fn block_query(b: &ast::Block, p: @fn(@ast::Expr) -> bool) -> bool {
103 let mut v = BlockQueryVisitor { p: p };
104 visit::walk_block(&mut v, b, rs);
108 pub fn local_rhs_span(l: @ast::Local, def: Span) -> Span {
110 Some(i) => return i.span,
115 pub fn pluralize(n: uint, s: ~str) -> ~str {
117 else { fmt!("%ss", s) }
120 // A set of node IDs (used to keep track of which node IDs are for statements)
121 pub type stmt_set = @mut HashSet<ast::NodeId>;