]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/query/keys.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / librustc / ty / query / 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 traits::query::{CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal};
15 use ty::{self, Ty, TyCtxt};
16 use ty::subst::Substs;
17 use ty::fast_reject::SimplifiedType;
18 use mir;
19
20 use std::fmt::Debug;
21 use std::hash::Hash;
22 use syntax_pos::{Span, DUMMY_SP};
23 use syntax_pos::symbol::InternedString;
24
25 /// The `Key` trait controls what types can legally be used as the key
26 /// for a query.
27 pub(super) trait Key: Clone + Hash + Eq + Debug {
28 /// Given an instance of this key, what crate is it referring to?
29 /// This is used to find the provider.
30 fn query_crate(&self) -> CrateNum;
31
32 /// In the event that a cycle occurs, if no explicit span has been
33 /// given for a query with key `self`, what span should we use?
34 fn default_span(&self, tcx: TyCtxt) -> Span;
35 }
36
37 impl<'tcx> Key for ty::InstanceDef<'tcx> {
38 fn query_crate(&self) -> CrateNum {
39 LOCAL_CRATE
40 }
41
42 fn default_span(&self, tcx: TyCtxt) -> Span {
43 tcx.def_span(self.def_id())
44 }
45 }
46
47 impl<'tcx> Key for ty::Instance<'tcx> {
48 fn query_crate(&self) -> CrateNum {
49 LOCAL_CRATE
50 }
51
52 fn default_span(&self, tcx: TyCtxt) -> Span {
53 tcx.def_span(self.def_id())
54 }
55 }
56
57 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
58 fn query_crate(&self) -> CrateNum {
59 self.instance.query_crate()
60 }
61
62 fn default_span(&self, tcx: TyCtxt) -> Span {
63 self.instance.default_span(tcx)
64 }
65 }
66
67 impl Key for CrateNum {
68 fn query_crate(&self) -> CrateNum {
69 *self
70 }
71 fn default_span(&self, _: TyCtxt) -> Span {
72 DUMMY_SP
73 }
74 }
75
76 impl Key for DefIndex {
77 fn query_crate(&self) -> CrateNum {
78 LOCAL_CRATE
79 }
80 fn default_span(&self, _tcx: TyCtxt) -> Span {
81 DUMMY_SP
82 }
83 }
84
85 impl Key for DefId {
86 fn query_crate(&self) -> CrateNum {
87 self.krate
88 }
89 fn default_span(&self, tcx: TyCtxt) -> Span {
90 tcx.def_span(*self)
91 }
92 }
93
94 impl Key for (DefId, DefId) {
95 fn query_crate(&self) -> CrateNum {
96 self.0.krate
97 }
98 fn default_span(&self, tcx: TyCtxt) -> Span {
99 self.1.default_span(tcx)
100 }
101 }
102
103 impl Key for (CrateNum, DefId) {
104 fn query_crate(&self) -> CrateNum {
105 self.0
106 }
107 fn default_span(&self, tcx: TyCtxt) -> Span {
108 self.1.default_span(tcx)
109 }
110 }
111
112 impl Key for (DefId, SimplifiedType) {
113 fn query_crate(&self) -> CrateNum {
114 self.0.krate
115 }
116 fn default_span(&self, tcx: TyCtxt) -> Span {
117 self.0.default_span(tcx)
118 }
119 }
120
121 impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
122 fn query_crate(&self) -> CrateNum {
123 self.0.krate
124 }
125 fn default_span(&self, tcx: TyCtxt) -> Span {
126 self.0.default_span(tcx)
127 }
128 }
129
130 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
131 fn query_crate(&self) -> CrateNum {
132 self.1.def_id().krate
133 }
134 fn default_span(&self, tcx: TyCtxt) -> Span {
135 tcx.def_span(self.1.def_id())
136 }
137 }
138
139 impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
140 fn query_crate(&self) -> CrateNum {
141 self.def_id().krate
142 }
143 fn default_span(&self, tcx: TyCtxt) -> Span {
144 tcx.def_span(self.def_id())
145 }
146 }
147
148 impl<'tcx> Key for (mir::interpret::ConstValue<'tcx>, Ty<'tcx>) {
149 fn query_crate(&self) -> CrateNum {
150 LOCAL_CRATE
151 }
152 fn default_span(&self, _: TyCtxt) -> Span {
153 DUMMY_SP
154 }
155 }
156
157 impl<'tcx> Key for Ty<'tcx> {
158 fn query_crate(&self) -> CrateNum {
159 LOCAL_CRATE
160 }
161 fn default_span(&self, _: TyCtxt) -> Span {
162 DUMMY_SP
163 }
164 }
165
166 impl<'tcx> Key for ty::ParamEnv<'tcx> {
167 fn query_crate(&self) -> CrateNum {
168 LOCAL_CRATE
169 }
170 fn default_span(&self, _: TyCtxt) -> Span {
171 DUMMY_SP
172 }
173 }
174
175 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
176 fn query_crate(&self) -> CrateNum {
177 self.value.query_crate()
178 }
179 fn default_span(&self, tcx: TyCtxt) -> Span {
180 self.value.default_span(tcx)
181 }
182 }
183
184 impl Key for InternedString {
185 fn query_crate(&self) -> CrateNum {
186 LOCAL_CRATE
187 }
188 fn default_span(&self, _tcx: TyCtxt) -> Span {
189 DUMMY_SP
190 }
191 }
192
193 impl<'tcx> Key for CanonicalProjectionGoal<'tcx> {
194 fn query_crate(&self) -> CrateNum {
195 LOCAL_CRATE
196 }
197
198 fn default_span(&self, _tcx: TyCtxt) -> Span {
199 DUMMY_SP
200 }
201 }
202
203 impl<'tcx> Key for CanonicalTyGoal<'tcx> {
204 fn query_crate(&self) -> CrateNum {
205 LOCAL_CRATE
206 }
207
208 fn default_span(&self, _tcx: TyCtxt) -> Span {
209 DUMMY_SP
210 }
211 }
212
213 impl<'tcx> Key for CanonicalPredicateGoal<'tcx> {
214 fn query_crate(&self) -> CrateNum {
215 LOCAL_CRATE
216 }
217
218 fn default_span(&self, _tcx: TyCtxt) -> Span {
219 DUMMY_SP
220 }
221 }