]> git.lizzy.rs Git - rust.git/blob - src/librustc/util/common.rs
eb9206a52b1257e9937efaa644f3928a5d4b0cac
[rust.git] / src / librustc / util / common.rs
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.
4 //
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.
10
11
12 use syntax::ast;
13 use syntax::codemap::{Span};
14 use syntax::visit;
15 use syntax::visit::Visitor;
16
17 use std::hashmap::HashSet;
18 use extra;
19
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();
23     let rv = thunk();
24     let end = extra::time::precise_time_s();
25     printfln!("time: %3.3f s\t%s", end - start, what);
26     rv
27 }
28
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.
32     debug!(">>");
33     let r = op();
34     debug!("<< (Result = %?)", r);
35     r
36 }
37
38 pub struct _indenter {
39     _i: (),
40 }
41
42 impl Drop for _indenter {
43     fn drop(&self) { debug!("<<"); }
44 }
45
46 pub fn _indenter(_i: ()) -> _indenter {
47     _indenter {
48         _i: ()
49     }
50 }
51
52 pub fn indenter() -> _indenter {
53     debug!(">>");
54     _indenter(())
55 }
56
57 pub fn field_expr(f: ast::Field) -> @ast::Expr { return f.expr; }
58
59 pub fn field_exprs(fields: ~[ast::Field]) -> ~[@ast::Expr] {
60     fields.map(|f| f.expr)
61 }
62
63 struct LoopQueryVisitor {
64     p: @fn(&ast::Expr_) -> bool
65 }
66
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);
70         match 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)
75         }
76     }
77 }
78
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 {
82     let rs = @mut false;
83     let mut v = LoopQueryVisitor { p: p };
84     visit::walk_block(&mut v, b, rs);
85     return *rs;
86 }
87
88 struct BlockQueryVisitor {
89     p: @fn(@ast::Expr) -> bool
90 }
91
92 impl Visitor<@mut bool> for BlockQueryVisitor {
93     fn visit_expr(&mut self, e:@ast::Expr, flag:@mut bool) {
94         *flag |= (self.p)(e);
95         visit::walk_expr(self, e, flag)
96     }
97 }
98
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 {
102     let rs = @mut false;
103     let mut v = BlockQueryVisitor { p: p };
104     visit::walk_block(&mut v, b, rs);
105     return *rs;
106 }
107
108 pub fn local_rhs_span(l: @ast::Local, def: Span) -> Span {
109     match l.init {
110       Some(i) => return i.span,
111       _ => return def
112     }
113 }
114
115 pub fn pluralize(n: uint, s: ~str) -> ~str {
116     if n == 1 { s }
117     else { fmt!("%ss", s) }
118 }
119
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>;