]> git.lizzy.rs Git - rust.git/blob - src/librustc/util/common.rs
Change finalize -> drop.
[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 use core::prelude::*;
12
13 use syntax::ast;
14 use syntax::codemap::{span};
15 use syntax::visit;
16
17 use core::hashmap::HashSet;
18 use core::io;
19 use extra;
20
21 pub fn time<T>(do_it: bool, what: ~str, thunk: &fn() -> T) -> T {
22     if !do_it { return thunk(); }
23     let start = extra::time::precise_time_s();
24     let rv = thunk();
25     let end = extra::time::precise_time_s();
26     io::println(fmt!("time: %3.3f s\t%s", end - start, what));
27     rv
28 }
29
30 pub fn indent<R>(op: &fn() -> R) -> R {
31     // Use in conjunction with the log post-processor like `src/etc/indenter`
32     // to make debug output more readable.
33     debug!(">>");
34     let r = op();
35     debug!("<< (Result = %?)", r);
36     r
37 }
38
39 pub struct _indenter {
40     _i: (),
41 }
42
43 impl Drop for _indenter {
44     fn drop(&self) { debug!("<<"); }
45 }
46
47 pub fn _indenter(_i: ()) -> _indenter {
48     _indenter {
49         _i: ()
50     }
51 }
52
53 pub fn indenter() -> _indenter {
54     debug!(">>");
55     _indenter(())
56 }
57
58 pub fn field_expr(f: ast::field) -> @ast::expr { return f.node.expr; }
59
60 pub fn field_exprs(fields: ~[ast::field]) -> ~[@ast::expr] {
61     fields.map(|f| f.node.expr)
62 }
63
64 // Takes a predicate p, returns true iff p is true for any subexpressions
65 // of b -- skipping any inner loops (loop, while, loop_body)
66 pub fn loop_query(b: &ast::blk, p: @fn(&ast::expr_) -> bool) -> bool {
67     let rs = @mut false;
68     let visit_expr: @fn(@ast::expr,
69                         (@mut bool,
70                          visit::vt<@mut bool>)) = |e, (flag, v)| {
71         *flag |= p(&e.node);
72         match e.node {
73           // Skip inner loops, since a break in the inner loop isn't a
74           // break inside the outer loop
75           ast::expr_loop(*) | ast::expr_while(*)
76           | ast::expr_loop_body(*) => {}
77           _ => visit::visit_expr(e, (flag, v))
78         }
79     };
80     let v = visit::mk_vt(@visit::Visitor {
81         visit_expr: visit_expr,
82         .. *visit::default_visitor()});
83     visit::visit_block(b, (rs, v));
84     return *rs;
85 }
86
87 // Takes a predicate p, returns true iff p is true for any subexpressions
88 // of b -- skipping any inner loops (loop, while, loop_body)
89 pub fn block_query(b: &ast::blk, p: @fn(@ast::expr) -> bool) -> bool {
90     let rs = @mut false;
91     let visit_expr: @fn(@ast::expr,
92                         (@mut bool,
93                          visit::vt<@mut bool>)) = |e, (flag, v)| {
94         *flag |= p(e);
95         visit::visit_expr(e, (flag, v))
96     };
97     let v = visit::mk_vt(@visit::Visitor{
98         visit_expr: visit_expr,
99         .. *visit::default_visitor()});
100     visit::visit_block(b, (rs, v));
101     return *rs;
102 }
103
104 pub fn local_rhs_span(l: @ast::local, def: span) -> span {
105     match l.node.init {
106       Some(i) => return i.span,
107       _ => return def
108     }
109 }
110
111 pub fn pluralize(n: uint, s: ~str) -> ~str {
112     if n == 1 { s }
113     else { fmt!("%ss", s) }
114 }
115
116 // A set of node IDs (used to keep track of which node IDs are for statements)
117 pub type stmt_set = @mut HashSet<ast::node_id>;