]>
Commit | Line | Data |
---|---|---|
d9579d0f AL |
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 | ||
b039eaaf | 11 | use syntax::ast; |
d9579d0f | 12 | use middle::ty::{self, IntVarValue, Ty}; |
9cc50fc6 | 13 | use rustc_data_structures::unify::{Combine, UnifyKey}; |
d9579d0f AL |
14 | |
15 | pub trait ToType<'tcx> { | |
16 | fn to_type(&self, tcx: &ty::ctxt<'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 | ||
9cc50fc6 SL |
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 | ||
d9579d0f AL |
53 | impl<'tcx> ToType<'tcx> for IntVarValue { |
54 | fn to_type(&self, tcx: &ty::ctxt<'tcx>) -> Ty<'tcx> { | |
55 | match *self { | |
c1a9b12d SL |
56 | ty::IntType(i) => tcx.mk_mach_int(i), |
57 | ty::UintType(i) => tcx.mk_mach_uint(i), | |
d9579d0f AL |
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: &ty::ctxt<'tcx>) -> Ty<'tcx> { | |
c1a9b12d | 73 | tcx.mk_mach_float(*self) |
d9579d0f AL |
74 | } |
75 | } |