]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/maps/keys.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / librustc / ty / maps / keys.rs
1 // Copyright 2012-2015 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 //! Defines the set of legal keys that can be used in queries.
12
13 use hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
14 use mir::transform::{MirSuite, MirPassIndex};
15 use ty::{self, Ty, TyCtxt};
16 use ty::subst::Substs;
17 use ty::fast_reject::SimplifiedType;
18
19 use std::fmt::Debug;
20 use std::hash::Hash;
21 use syntax_pos::{Span, DUMMY_SP};
22 use syntax_pos::symbol::InternedString;
23
24 /// The `Key` trait controls what types can legally be used as the key
25 /// for a query.
26 pub trait Key: Clone + Hash + Eq + Debug {
27 /// Given an instance of this key, what crate is it referring to?
28 /// This is used to find the provider.
29 fn map_crate(&self) -> CrateNum;
30
31 /// In the event that a cycle occurs, if no explicit span has been
32 /// given for a query with key `self`, what span should we use?
33 fn default_span(&self, tcx: TyCtxt) -> Span;
34 }
35
36 impl<'tcx> Key for ty::InstanceDef<'tcx> {
37 fn map_crate(&self) -> CrateNum {
38 LOCAL_CRATE
39 }
40
41 fn default_span(&self, tcx: TyCtxt) -> Span {
42 tcx.def_span(self.def_id())
43 }
44 }
45
46 impl<'tcx> Key for ty::Instance<'tcx> {
47 fn map_crate(&self) -> CrateNum {
48 LOCAL_CRATE
49 }
50
51 fn default_span(&self, tcx: TyCtxt) -> Span {
52 tcx.def_span(self.def_id())
53 }
54 }
55
56 impl Key for CrateNum {
57 fn map_crate(&self) -> CrateNum {
58 *self
59 }
60 fn default_span(&self, _: TyCtxt) -> Span {
61 DUMMY_SP
62 }
63 }
64
65 impl Key for DefIndex {
66 fn map_crate(&self) -> CrateNum {
67 LOCAL_CRATE
68 }
69 fn default_span(&self, _tcx: TyCtxt) -> Span {
70 DUMMY_SP
71 }
72 }
73
74 impl Key for DefId {
75 fn map_crate(&self) -> CrateNum {
76 self.krate
77 }
78 fn default_span(&self, tcx: TyCtxt) -> Span {
79 tcx.def_span(*self)
80 }
81 }
82
83 impl Key for (DefId, DefId) {
84 fn map_crate(&self) -> CrateNum {
85 self.0.krate
86 }
87 fn default_span(&self, tcx: TyCtxt) -> Span {
88 self.1.default_span(tcx)
89 }
90 }
91
92 impl Key for (CrateNum, DefId) {
93 fn map_crate(&self) -> CrateNum {
94 self.0
95 }
96 fn default_span(&self, tcx: TyCtxt) -> Span {
97 self.1.default_span(tcx)
98 }
99 }
100
101 impl Key for (DefId, SimplifiedType) {
102 fn map_crate(&self) -> CrateNum {
103 self.0.krate
104 }
105 fn default_span(&self, tcx: TyCtxt) -> Span {
106 self.0.default_span(tcx)
107 }
108 }
109
110 impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
111 fn map_crate(&self) -> CrateNum {
112 self.0.krate
113 }
114 fn default_span(&self, tcx: TyCtxt) -> Span {
115 self.0.default_span(tcx)
116 }
117 }
118
119 impl Key for (MirSuite, DefId) {
120 fn map_crate(&self) -> CrateNum {
121 self.1.map_crate()
122 }
123 fn default_span(&self, tcx: TyCtxt) -> Span {
124 self.1.default_span(tcx)
125 }
126 }
127
128 impl Key for (MirSuite, MirPassIndex, DefId) {
129 fn map_crate(&self) -> CrateNum {
130 self.2.map_crate()
131 }
132 fn default_span(&self, tcx: TyCtxt) -> Span {
133 self.2.default_span(tcx)
134 }
135 }
136
137 impl<'tcx> Key for Ty<'tcx> {
138 fn map_crate(&self) -> CrateNum {
139 LOCAL_CRATE
140 }
141 fn default_span(&self, _: TyCtxt) -> Span {
142 DUMMY_SP
143 }
144 }
145
146 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
147 fn map_crate(&self) -> CrateNum {
148 self.value.map_crate()
149 }
150 fn default_span(&self, tcx: TyCtxt) -> Span {
151 self.value.default_span(tcx)
152 }
153 }
154
155 impl Key for InternedString {
156 fn map_crate(&self) -> CrateNum {
157 LOCAL_CRATE
158 }
159 fn default_span(&self, _tcx: TyCtxt) -> Span {
160 DUMMY_SP
161 }
162 }