common.rs 6.4 KB
Newer Older
1 2
import std.map;
import std.map.hashmap;
3
import std._uint;
4
import std._int;
5
import std._vec;
6
import std.option.none;
7
import front.ast;
8
import util.typestate_ann.ts_ann;
9

10 11 12
import middle.fold;
import middle.fold.respan;

13 14 15 16 17 18 19
import std.io.stdout;
import std.io.str_writer;
import std.io.string_writer;
import pretty.pprust.print_block;
import pretty.pprust.print_expr;
import pretty.pprust.print_decl;
import pretty.pp.mkstate;
20 21

type filename = str;
22
type span = rec(uint lo, uint hi);
23
type spanned[T] = rec(T node, span span);
24
type flag = hashmap[str, ()];
G
Graydon Hoare 已提交
25

26
tag ty_mach {
27 28 29 30 31 32 33 34 35 36 37 38
    ty_i8;
    ty_i16;
    ty_i32;
    ty_i64;

    ty_u8;
    ty_u16;
    ty_u32;
    ty_u64;

    ty_f32;
    ty_f64;
39
}
G
Graydon Hoare 已提交
40

41 42
fn ty_mach_to_str(ty_mach tm) -> str {
    alt (tm) {
43 44 45 46 47 48 49 50 51 52 53 54
        case (ty_u8) { ret "u8"; }
        case (ty_u16) { ret "u16"; }
        case (ty_u32) { ret "u32"; }
        case (ty_u64) { ret "u64"; }

        case (ty_i8) { ret "i8"; }
        case (ty_i16) { ret "i16"; }
        case (ty_i32) { ret "i32"; }
        case (ty_i64) { ret "i64"; }

        case (ty_f32) { ret "f32"; }
        case (ty_f64) { ret "f64"; }
55 56 57
    }
}

58 59 60 61 62 63
fn new_str_hash[V]() -> std.map.hashmap[str,V] {
    let std.map.hashfn[str] hasher = std._str.hash;
    let std.map.eqfn[str] eqer = std._str.eq;
    ret std.map.mk_hashmap[str,V](hasher, eqer);
}

P
Patrick Walton 已提交
64 65 66 67
fn def_eq(&ast.def_id a, &ast.def_id b) -> bool {
    ret a._0 == b._0 && a._1 == b._1;
}

68 69 70 71 72 73
fn hash_def(&ast.def_id d) -> uint {
    auto h = 5381u;
    h = ((h << 5u) + h) ^ (d._0 as uint);
    h = ((h << 5u) + h) ^ (d._1 as uint);
    ret h;
}
74

75 76
fn new_def_hash[V]() -> std.map.hashmap[ast.def_id,V] {
    let std.map.hashfn[ast.def_id] hasher = hash_def;
P
Patrick Walton 已提交
77
    let std.map.eqfn[ast.def_id] eqer = def_eq;
78 79 80
    ret std.map.mk_hashmap[ast.def_id,V](hasher, eqer);
}

81 82 83 84 85 86 87 88
fn new_int_hash[V]() -> std.map.hashmap[int,V] {
    fn hash_int(&int x) -> uint { ret x as uint; }
    fn eq_int(&int a, &int b) -> bool { ret a == b; }
    auto hasher = hash_int;
    auto eqer = eq_int;
    ret std.map.mk_hashmap[int,V](hasher, eqer);
}

P
Patrick Walton 已提交
89 90 91 92 93 94 95 96
fn new_uint_hash[V]() -> std.map.hashmap[uint,V] {
    fn hash_uint(&uint x) -> uint { ret x; }
    fn eq_uint(&uint a, &uint b) -> bool { ret a == b; }
    auto hasher = hash_uint;
    auto eqer = eq_uint;
    ret std.map.mk_hashmap[uint,V](hasher, eqer);
}

97 98 99 100
fn istr(int i) -> str {
    ret _int.to_str(i, 10u);
}

101 102 103 104 105 106
fn uistr(uint i) -> str {
    ret _uint.to_str(i, 10u);
}

fn elt_expr(&ast.elt e) -> @ast.expr { ret e.expr; }

T
Tim Chevalier 已提交
107
fn elt_exprs(&vec[ast.elt] elts) -> vec[@ast.expr] {
108
    auto f = elt_expr;
T
Tim Chevalier 已提交
109
    ret _vec.map[ast.elt, @ast.expr](f, elts);
110 111
}

112 113 114 115 116 117 118
fn field_expr(&ast.field f) -> @ast.expr { ret f.expr; }

fn field_exprs(vec[ast.field] fields) -> vec [@ast.expr] {
    auto f = field_expr;
    ret _vec.map[ast.field, @ast.expr](f, fields);
}

119 120
fn plain_ann(middle.ty.ctxt tcx) -> ast.ann {
  ret ast.ann_type(middle.ty.mk_nil(tcx),
121
                   none[vec[middle.ty.t]], none[@ts_ann]);
122 123
}

T
Tim Chevalier 已提交
124
fn expr_to_str(&ast.expr e) -> str {
125 126 127 128 129
  let str_writer s = string_writer();
  auto out_ = mkstate(s.get_writer(), 80u);
  auto out = @rec(s=out_,
                  comments=none[vec[front.lexer.cmnt]],
                  mutable cur_cmnt=0u);
130
  print_expr(out, @e);
T
Tim Chevalier 已提交
131
  ret s.get_str();
132 133
}

T
Tim Chevalier 已提交
134 135 136 137 138 139 140 141 142
fn log_expr(&ast.expr e) -> () {
    log(expr_to_str(e));
}

fn log_expr_err(&ast.expr e) -> () {
    log_err(expr_to_str(e));
}

fn block_to_str(&ast.block b) -> str {
143 144 145 146 147 148 149
  let str_writer s = string_writer();
  auto out_ = mkstate(s.get_writer(), 80u);
  auto out = @rec(s=out_,
                  comments=none[vec[front.lexer.cmnt]],
                  mutable cur_cmnt=0u);

  print_block(out, b);
T
Tim Chevalier 已提交
150 151 152 153 154 155 156 157 158
  ret s.get_str();
}

fn log_block(&ast.block b) -> () {
    log(block_to_str(b));
}

fn log_block_err(&ast.block b) -> () {
    log_err(block_to_str(b));
159 160
}

161 162 163 164 165 166 167 168 169 170 171
fn log_ann(&ast.ann a) -> () {
    alt (a) {
        case (ast.ann_none) {
            log("ann_none");
        }
        case (ast.ann_type(_,_,_)) {
            log("ann_type");
        }
    }
}

T
Tim Chevalier 已提交
172
fn stmt_to_str(&ast.stmt st) -> str {
173 174 175 176 177 178 179 180 181 182 183 184 185 186
  let str_writer s = string_writer();
  auto out_ = mkstate(s.get_writer(), 80u);
  auto out = @rec(s=out_,
                  comments=none[vec[front.lexer.cmnt]],
                  mutable cur_cmnt=0u);
  alt (st.node) {
    case (ast.stmt_decl(?decl,_)) {
      print_decl(out, decl);
    }
    case (ast.stmt_expr(?ex,_)) {
      print_expr(out, ex);
    }
    case (_) { /* do nothing */ }
  }
T
Tim Chevalier 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  ret s.get_str();
}

fn log_stmt(&ast.stmt st) -> () {
    log(stmt_to_str(st));
}

fn log_stmt_err(&ast.stmt st) -> () {
    log_err(stmt_to_str(st));
}

fn decl_lhs(@ast.decl d) -> ast.def_id {
    alt (d.node) {
        case (ast.decl_local(?l)) {
            ret l.id;
        }
        case (ast.decl_item(?an_item)) {
            alt (an_item.node) {
                case (ast.item_const(_,_,_,?d,_)) {
                    ret d;
                }
                case (ast.item_fn(_,_,_,?d,_)) {
                    ret d;
                }
                case (ast.item_mod(_,_,?d)) {
                    ret d;
                }
                case (ast.item_native_mod(_,_,?d)) {
                    ret d;
                }
                case (ast.item_ty(_,_,_,?d,_)) {
                    ret d;
                }
                case (ast.item_tag(_,_,_,?d,_)) {
                    ret d;
                }
                case (ast.item_obj(_,_,_,?d,_)) {
                    ret d.ctor; /* This doesn't really make sense */
                }
            }
        } 
    }
229 230
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
fn has_nonlocal_exits(&ast.block b) -> bool {
    /* overkill, but just passing around a mutable bool doesn't seem
       to work in rustboot */
    auto has_exits = new_str_hash[()]();
 
   fn set_break(&flag f, &span sp, ast.ann a) -> @ast.expr {
        f.insert("foo", ());
        ret @respan(sp, ast.expr_break(a));
    }
    fn set_cont(&flag f, &span sp, ast.ann a) -> @ast.expr {
        f.insert("foo", ());
        ret @respan(sp, ast.expr_cont(a));
    }
    fn check_b(&flag f) -> bool {
        ret (f.size() == 0u);
    }

    auto fld0 = fold.new_identity_fold[flag]();

    fld0 = @rec(fold_expr_break = bind set_break(_,_,_),
                fold_expr_cont  = bind set_cont(_,_,_),
                keep_going      = bind check_b(_) with *fld0);
    fold.fold_block[flag](has_exits, fld0, b);

    ret (has_exits.size() > 0u);
}
G
Graydon Hoare 已提交
257 258 259 260 261 262 263
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
264
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
G
Graydon Hoare 已提交
265 266
// End:
//