1 // Copyright 2014 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 util::snapshot_vec as sv;
15 pub struct TypeVariableTable {
16 values: sv::SnapshotVec<TypeVariableData,UndoEntry,Delegate>,
19 struct TypeVariableData {
20 value: TypeVariableValue
23 enum TypeVariableValue {
25 Bounded(Vec<Relation>),
29 snapshot: sv::Snapshot
33 // The type of the var was specified.
34 SpecifyVar(ty::TyVid, Vec<Relation>),
35 Relate(ty::TyVid, ty::TyVid),
40 type Relation = (RelationDir, ty::TyVid);
42 #[deriving(PartialEq,Show)]
43 pub enum RelationDir {
44 SubtypeOf, SupertypeOf, EqTo
48 fn opposite(self) -> RelationDir {
50 SubtypeOf => SupertypeOf,
51 SupertypeOf => SubtypeOf,
57 impl TypeVariableTable {
58 pub fn new() -> TypeVariableTable {
59 TypeVariableTable { values: sv::SnapshotVec::new(Delegate) }
62 fn relations<'a>(&'a mut self, a: ty::TyVid) -> &'a mut Vec<Relation> {
63 relations(self.values.get_mut(a.index))
66 pub fn relate_vars(&mut self, a: ty::TyVid, dir: RelationDir, b: ty::TyVid) {
68 * Records that `a <: b`, `a :> b`, or `a == b`, depending on `dir`.
70 * Precondition: neither `a` nor `b` are known.
74 self.relations(a).push((dir, b));
75 self.relations(b).push((dir.opposite(), a));
76 self.values.record(Relate(a, b));
80 pub fn instantiate_and_push(
84 stack: &mut Vec<(ty::t, RelationDir, ty::TyVid)>)
87 * Instantiates `vid` with the type `ty` and then pushes an
88 * entry onto `stack` for each of the relations of `vid` to
89 * other variables. The relations will have the form `(ty,
90 * dir, vid1)` where `vid1` is some other variable id.
94 let value_ptr = &mut self.values.get_mut(vid.index).value;
95 mem::replace(value_ptr, Known(ty))
98 let relations = match old_value {
100 Known(_) => fail!("Asked to instantiate variable that is \
101 already instantiated")
104 for &(dir, vid) in relations.iter() {
105 stack.push((ty, dir, vid));
108 self.values.record(SpecifyVar(vid, relations));
111 pub fn new_var(&mut self) -> ty::TyVid {
114 TypeVariableData { value: Bounded(Vec::new()) });
115 ty::TyVid { index: index }
118 pub fn probe(&self, vid: ty::TyVid) -> Option<ty::t> {
119 match self.values.get(vid.index).value {
125 pub fn replace_if_possible(&self, t: ty::t) -> ty::t {
126 match ty::get(t).sty {
127 ty::ty_infer(ty::TyVar(v)) => {
128 match self.probe(v) {
137 pub fn snapshot(&mut self) -> Snapshot {
138 Snapshot { snapshot: self.values.start_snapshot() }
141 pub fn rollback_to(&mut self, s: Snapshot) {
142 self.values.rollback_to(s.snapshot);
145 pub fn commit(&mut self, s: Snapshot) {
146 self.values.commit(s.snapshot);
150 impl sv::SnapshotVecDelegate<TypeVariableData,UndoEntry> for Delegate {
151 fn reverse(&mut self,
152 values: &mut Vec<TypeVariableData>,
155 SpecifyVar(vid, relations) => {
156 values.get_mut(vid.index).value = Bounded(relations);
160 relations(values.get_mut(a.index)).pop();
161 relations(values.get_mut(b.index)).pop();
167 fn relations<'a>(v: &'a mut TypeVariableData) -> &'a mut Vec<Relation> {
169 Known(_) => fail!("var_sub_var: variable is known"),
170 Bounded(ref mut relations) => relations