Skip to content

Commit f850935

Browse files
committed
---
yaml --- r: 2557 b: refs/heads/master c: 32b8dcb h: refs/heads/master i: 2555: 88f8dd8 v: v3
1 parent 93ce26b commit f850935

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 3760bfab87498d71f9341ffac1aa027f26028656
2+
refs/heads/master: 32b8dcb97c29b723579fdeb0d93c16917762ae08

trunk/src/comp/middle/ty.rs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import util::common::new_def_hash;
3333
import util::common::span;
3434
import util::typestate_ann::ts_ann;
3535

36+
import util::interner;
37+
3638
// Data types
3739

3840
tag mode {
@@ -159,19 +161,14 @@ const uint idx_native = 18u;
159161
const uint idx_type = 19u;
160162
const uint idx_first_others = 20u;
161163

162-
type type_store = rec(mutable vec[raw_t] others,
163-
hashmap[raw_t,uint] other_structural);
164+
type type_store = interner::interner[raw_t];
164165

165166
type ty_param_substs_opt_and_ty = tup(option::t[vec[ty::t]], ty::t);
166167
type node_type_table =
167168
@mutable vec[mutable option::t[ty::ty_param_substs_opt_and_ty]];
168169

169170
fn mk_type_store() -> @type_store {
170-
let vec[raw_t] others = vec();
171-
let hashmap[raw_t,uint] ost =
172-
map::mk_hashmap[raw_t,uint](hash_raw_ty, eq_raw_ty);
173-
174-
auto ts = @rec(mutable others=others, other_structural=ost);
171+
auto ts = @interner::mk_interner[raw_t](hash_raw_ty, eq_raw_ty);
175172

176173
intern(ts, ty_nil, none[str]);
177174
intern(ts, ty_bool, none[str]);
@@ -194,7 +191,7 @@ fn mk_type_store() -> @type_store {
194191
intern(ts, ty_native, none[str]);
195192
intern(ts, ty_type, none[str]);
196193

197-
assert _vec::len(ts.others) == idx_first_others;
194+
assert _vec::len(ts.vect) == idx_first_others;
198195

199196
ret ts;
200197
}
@@ -240,7 +237,7 @@ fn mk_raw_ty(&@type_store ts, &sty st, &option::t[str] cname) -> raw_t {
240237
&mutable bool has_vars,
241238
&mutable bool has_locals,
242239
&t tt) {
243-
auto rt = ts.others.(tt);
240+
auto rt = interner::get[raw_t](*ts, tt);
244241
has_params = has_params || rt.has_params;
245242
has_bound_params = has_bound_params || rt.has_bound_params;
246243
has_vars = has_vars || rt.has_vars;
@@ -355,32 +352,13 @@ fn mk_raw_ty(&@type_store ts, &sty st, &option::t[str] cname) -> raw_t {
355352
has_locals = has_locals);
356353
}
357354

358-
fn intern_raw_ty(&@type_store ts, &raw_t rt) {
359-
auto type_num = _vec::len[raw_t](ts.others);
360-
ts.others += vec(rt);
361-
ts.other_structural.insert(rt, type_num);
362-
}
363-
364355
fn intern(&@type_store ts, &sty st, &option::t[str] cname) {
365-
intern_raw_ty(ts, mk_raw_ty(ts, st, cname));
356+
interner::intern[raw_t](*ts, mk_raw_ty(ts, st, cname));
366357
}
367358

368359
fn gen_ty_full(&ctxt cx, &sty st, &option::t[str] cname) -> t {
369360
auto raw_type = mk_raw_ty(cx.ts, st, cname);
370-
371-
// Is it interned?
372-
alt (cx.ts.other_structural.find(raw_type)) {
373-
case (some[t](?typ)) {
374-
ret typ;
375-
}
376-
case (none[t]) {
377-
// Nope: Insert it and return.
378-
auto type_num = _vec::len[raw_t](cx.ts.others);
379-
intern_raw_ty(cx.ts, raw_type);
380-
// log_err "added: " + ty_to_str(tystore, raw_type);
381-
ret type_num;
382-
}
383-
}
361+
ret interner::intern[raw_t](*cx.ts, raw_type);
384362
}
385363

386364
// These are private constructors to this module. External users should always
@@ -484,10 +462,14 @@ fn mk_native(&ctxt cx) -> t { ret idx_native; }
484462

485463

486464
// Returns the one-level-deep type structure of the given type.
487-
fn struct(&ctxt cx, &t typ) -> sty { ret cx.ts.others.(typ).struct; }
465+
fn struct(&ctxt cx, &t typ) -> sty {
466+
ret interner::get[raw_t](*cx.ts, typ).struct;
467+
}
488468

489469
// Returns the canonical name of the given type.
490-
fn cname(&ctxt cx, &t typ) -> option::t[str] { ret cx.ts.others.(typ).cname; }
470+
fn cname(&ctxt cx, &t typ) -> option::t[str] {
471+
ret interner::get[raw_t](*cx.ts, typ).cname;
472+
}
491473

492474

493475
// Stringification
@@ -1525,19 +1507,19 @@ fn count_ty_params(ctxt cx, t ty) -> uint {
15251507
}
15261508

15271509
fn type_contains_vars(&ctxt cx, &t typ) -> bool {
1528-
ret cx.ts.others.(typ).has_vars;
1510+
ret interner::get[raw_t](*cx.ts, typ).has_vars;
15291511
}
15301512

15311513
fn type_contains_locals(&ctxt cx, &t typ) -> bool {
1532-
ret cx.ts.others.(typ).has_locals;
1514+
ret interner::get[raw_t](*cx.ts, typ).has_locals;
15331515
}
15341516

15351517
fn type_contains_params(&ctxt cx, &t typ) -> bool {
1536-
ret cx.ts.others.(typ).has_params;
1518+
ret interner::get[raw_t](*cx.ts, typ).has_params;
15371519
}
15381520

15391521
fn type_contains_bound_params(&ctxt cx, &t typ) -> bool {
1540-
ret cx.ts.others.(typ).has_bound_params;
1522+
ret interner::get[raw_t](*cx.ts, typ).has_bound_params;
15411523
}
15421524

15431525
// Type accessors for substructures of types

trunk/src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ mod driver {
5252

5353
mod util {
5454
mod common;
55+
mod interner;
5556
mod typestate_ann;
5657
}
5758

trunk/src/comp/util/interner.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// An "interner" is a data structure that associates values with uint tags and
2+
// allows bidirectional lookup; i.e. given a value, one can easily find the
3+
// type, and vice versa.
4+
5+
import std::_vec;
6+
import std::map;
7+
import std::map::hashmap;
8+
import std::map::hashfn;
9+
import std::map::eqfn;
10+
import std::option;
11+
import std::option::none;
12+
import std::option::some;
13+
14+
type interner[T] = rec(
15+
hashmap[T,uint] map,
16+
mutable vec[T] vect,
17+
hashfn[T] hasher,
18+
eqfn[T] eqer
19+
);
20+
21+
fn mk_interner[T](hashfn[T] hasher, eqfn[T] eqer) -> interner[T] {
22+
auto m = map::mk_hashmap[T,uint](hasher, eqer);
23+
let vec[T] vect = vec();
24+
ret rec(map=m, mutable vect=vect, hasher=hasher, eqer=eqer);
25+
}
26+
27+
fn intern[T](&interner[T] itr, &T val) -> uint {
28+
alt (itr.map.find(val)) {
29+
case (some[uint](?idx)) { ret idx; }
30+
case (none[uint]) {
31+
auto new_idx = _vec::len[T](itr.vect);
32+
itr.map.insert(val, new_idx);
33+
itr.vect += vec(val);
34+
ret new_idx;
35+
}
36+
}
37+
}
38+
39+
fn get[T](&interner[T] itr, uint idx) -> T {
40+
ret itr.vect.(idx);
41+
}
42+

0 commit comments

Comments
 (0)