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.
11 pub use self::RelationDir::*;
12 use self::TypeVariableValue::*;
13 use self::UndoEntry::*;
15 use middle::ty::{mod, Ty};
17 use util::snapshot_vec as sv;
19 pub struct TypeVariableTable<'tcx> {
20 values: sv::SnapshotVec<TypeVariableData<'tcx>,UndoEntry,Delegate>,
23 struct TypeVariableData<'tcx> {
24 value: TypeVariableValue<'tcx>,
28 enum TypeVariableValue<'tcx> {
30 Bounded(Vec<Relation>),
34 snapshot: sv::Snapshot
38 // The type of the var was specified.
39 SpecifyVar(ty::TyVid, Vec<Relation>),
40 Relate(ty::TyVid, ty::TyVid),
45 type Relation = (RelationDir, ty::TyVid);
47 #[deriving(PartialEq,Show)]
48 pub enum RelationDir {
49 SubtypeOf, SupertypeOf, EqTo
53 fn opposite(self) -> RelationDir {
55 SubtypeOf => SupertypeOf,
56 SupertypeOf => SubtypeOf,
62 impl<'tcx> TypeVariableTable<'tcx> {
63 pub fn new() -> TypeVariableTable<'tcx> {
64 TypeVariableTable { values: sv::SnapshotVec::new(Delegate) }
67 fn relations<'a>(&'a mut self, a: ty::TyVid) -> &'a mut Vec<Relation> {
68 relations(self.values.get_mut(a.index))
71 pub fn var_diverges<'a>(&'a self, vid: ty::TyVid) -> bool {
72 self.values.get(vid.index).diverging
75 pub fn relate_vars(&mut self, a: ty::TyVid, dir: RelationDir, b: ty::TyVid) {
77 * Records that `a <: b`, `a :> b`, or `a == b`, depending on `dir`.
79 * Precondition: neither `a` nor `b` are known.
83 self.relations(a).push((dir, b));
84 self.relations(b).push((dir.opposite(), a));
85 self.values.record(Relate(a, b));
89 pub fn instantiate_and_push(
93 stack: &mut Vec<(Ty<'tcx>, RelationDir, ty::TyVid)>)
96 * Instantiates `vid` with the type `ty` and then pushes an
97 * entry onto `stack` for each of the relations of `vid` to
98 * other variables. The relations will have the form `(ty,
99 * dir, vid1)` where `vid1` is some other variable id.
103 let value_ptr = &mut self.values.get_mut(vid.index).value;
104 mem::replace(value_ptr, Known(ty))
107 let relations = match old_value {
109 Known(_) => panic!("Asked to instantiate variable that is \
110 already instantiated")
113 for &(dir, vid) in relations.iter() {
114 stack.push((ty, dir, vid));
117 self.values.record(SpecifyVar(vid, relations));
120 pub fn new_var(&mut self, diverging: bool) -> ty::TyVid {
121 let index = self.values.push(TypeVariableData {
122 value: Bounded(vec![]),
125 ty::TyVid { index: index }
128 pub fn probe(&self, vid: ty::TyVid) -> Option<Ty<'tcx>> {
129 match self.values.get(vid.index).value {
135 pub fn replace_if_possible(&self, t: Ty<'tcx>) -> Ty<'tcx> {
137 ty::ty_infer(ty::TyVar(v)) => {
138 match self.probe(v) {
147 pub fn snapshot(&mut self) -> Snapshot {
148 Snapshot { snapshot: self.values.start_snapshot() }
151 pub fn rollback_to(&mut self, s: Snapshot) {
152 self.values.rollback_to(s.snapshot);
155 pub fn commit(&mut self, s: Snapshot) {
156 self.values.commit(s.snapshot);
160 impl<'tcx> sv::SnapshotVecDelegate<TypeVariableData<'tcx>,UndoEntry> for Delegate {
161 fn reverse(&mut self,
162 values: &mut Vec<TypeVariableData>,
165 SpecifyVar(vid, relations) => {
166 values[vid.index].value = Bounded(relations);
170 relations(&mut (*values)[a.index]).pop();
171 relations(&mut (*values)[b.index]).pop();
177 fn relations<'a>(v: &'a mut TypeVariableData) -> &'a mut Vec<Relation> {
179 Known(_) => panic!("var_sub_var: variable is known"),
180 Bounded(ref mut relations) => relations