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