]> git.proxmox.com Git - rustc.git/blob - src/librustc/infer/unify_key.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc / infer / unify_key.rs
1 // Copyright 2012-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.
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 syntax::ast;
12 use ty::{self, IntVarValue, Ty, TyCtxt};
13 use rustc_data_structures::unify::{Combine, UnifyKey};
14
15 pub trait ToType<'tcx> {
16 fn to_type(&self, tcx: &TyCtxt<'tcx>) -> Ty<'tcx>;
17 }
18
19 impl UnifyKey for ty::IntVid {
20 type Value = Option<IntVarValue>;
21 fn index(&self) -> u32 { self.index }
22 fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } }
23 fn tag(_: Option<ty::IntVid>) -> &'static str { "IntVid" }
24 }
25
26 #[derive(PartialEq, Copy, Clone, Debug)]
27 pub struct RegionVidKey {
28 /// The minimum region vid in the unification set. This is needed
29 /// to have a canonical name for a type to prevent infinite
30 /// recursion.
31 pub min_vid: ty::RegionVid
32 }
33
34 impl Combine for RegionVidKey {
35 fn combine(&self, other: &RegionVidKey) -> RegionVidKey {
36 let min_vid = if self.min_vid.index < other.min_vid.index {
37 self.min_vid
38 } else {
39 other.min_vid
40 };
41
42 RegionVidKey { min_vid: min_vid }
43 }
44 }
45
46 impl UnifyKey for ty::RegionVid {
47 type Value = RegionVidKey;
48 fn index(&self) -> u32 { self.index }
49 fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid { index: i } }
50 fn tag(_: Option<ty::RegionVid>) -> &'static str { "RegionVid" }
51 }
52
53 impl<'tcx> ToType<'tcx> for IntVarValue {
54 fn to_type(&self, tcx: &TyCtxt<'tcx>) -> Ty<'tcx> {
55 match *self {
56 ty::IntType(i) => tcx.mk_mach_int(i),
57 ty::UintType(i) => tcx.mk_mach_uint(i),
58 }
59 }
60 }
61
62 // Floating point type keys
63
64 impl UnifyKey for ty::FloatVid {
65 type Value = Option<ast::FloatTy>;
66 fn index(&self) -> u32 { self.index }
67 fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
68 fn tag(_: Option<ty::FloatVid>) -> &'static str { "FloatVid" }
69 }
70
71 impl<'tcx> ToType<'tcx> for ast::FloatTy {
72 fn to_type(&self, tcx: &TyCtxt<'tcx>) -> Ty<'tcx> {
73 tcx.mk_mach_float(*self)
74 }
75 }
76
77 impl UnifyKey for ty::TyVid {
78 type Value = ();
79 fn index(&self) -> u32 { self.index }
80 fn from_index(i: u32) -> ty::TyVid { ty::TyVid { index: i } }
81 fn tag(_: Option<ty::TyVid>) -> &'static str { "TyVid" }
82 }