]> git.lizzy.rs Git - rust.git/blob - src/librustc/macros.rs
Retire impl_stable_hash_for_spanned.
[rust.git] / src / librustc / macros.rs
1 // ignore-tidy-linelength
2
3 macro_rules! enum_from_u32 {
4     ($(#[$attr:meta])* pub enum $name:ident {
5         $($variant:ident = $e:expr,)*
6     }) => {
7         $(#[$attr])*
8         pub enum $name {
9             $($variant = $e),*
10         }
11
12         impl $name {
13             pub fn from_u32(u: u32) -> Option<$name> {
14                 $(if u == $name::$variant as u32 {
15                     return Some($name::$variant)
16                 })*
17                 None
18             }
19         }
20     };
21     ($(#[$attr:meta])* pub enum $name:ident {
22         $($variant:ident,)*
23     }) => {
24         $(#[$attr])*
25         pub enum $name {
26             $($variant,)*
27         }
28
29         impl $name {
30             pub fn from_u32(u: u32) -> Option<$name> {
31                 $(if u == $name::$variant as u32 {
32                     return Some($name::$variant)
33                 })*
34                 None
35             }
36         }
37     }
38 }
39
40 #[macro_export]
41 macro_rules! bug {
42     () => ( bug!("impossible case reached") );
43     ($($message:tt)*) => ({
44         $crate::util::bug::bug_fmt(file!(), line!(), format_args!($($message)*))
45     })
46 }
47
48 #[macro_export]
49 macro_rules! span_bug {
50     ($span:expr, $($message:tt)*) => ({
51         $crate::util::bug::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
52     })
53 }
54
55 #[macro_export]
56 macro_rules! __impl_stable_hash_field {
57     ($field:ident, $ctx:expr, $hasher:expr) => ($field.hash_stable($ctx, $hasher));
58     ($field:ident, $ctx:expr, $hasher:expr, _) => ({ let _ = $field; });
59     ($field:ident, $ctx:expr, $hasher:expr, $delegate:expr) => ($delegate.hash_stable($ctx, $hasher));
60 }
61
62 #[macro_export]
63 macro_rules! impl_stable_hash_for {
64     // Enums
65     (enum $enum_name:path {
66         $( $variant:ident
67            // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`,
68            // when it should be only one or the other
69            $( ( $($field:ident $(-> $delegate:tt)?),* ) )?
70            $( { $($named_field:ident $(-> $named_delegate:tt)?),* } )?
71         ),* $(,)?
72     }) => {
73         impl_stable_hash_for!(
74             impl<> for enum $enum_name [ $enum_name ] { $( $variant
75                 $( ( $($field $(-> $delegate)?),* ) )?
76                 $( { $($named_field $(-> $named_delegate)?),* } )?
77             ),* }
78         );
79     };
80     // We want to use the enum name both in the `impl ... for $enum_name` as well as for
81     // importing all the variants. Unfortunately it seems we have to take the name
82     // twice for this purpose
83     (impl<$($T:ident),* $(,)?>
84         for enum $enum_name:path
85         [ $enum_path:path ]
86     {
87         $( $variant:ident
88            // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`,
89            // when it should be only one or the other
90            $( ( $($field:ident $(-> $delegate:tt)?),* ) )?
91            $( { $($named_field:ident $(-> $named_delegate:tt)?),* } )?
92         ),* $(,)?
93     }) => {
94         impl<$($T,)*>
95             ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>
96             for $enum_name
97             where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
98         {
99             #[inline]
100             fn hash_stable(&self,
101                            __ctx: &mut $crate::ich::StableHashingContext<'a>,
102                            __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
103                 use $enum_path::*;
104                 ::std::mem::discriminant(self).hash_stable(__ctx, __hasher);
105
106                 match *self {
107                     $(
108                         $variant $( ( $(ref $field),* ) )? $( { $(ref $named_field),* } )? => {
109                             $($( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*)?
110                             $($( __impl_stable_hash_field!($named_field, __ctx, __hasher $(, $named_delegate)?) );*)?
111                         }
112                     )*
113                 }
114             }
115         }
116     };
117     // Structs
118     (struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => {
119         impl_stable_hash_for!(
120             impl<> for struct $struct_name { $($field $(-> $delegate)?),* }
121         );
122     };
123     (impl<$($T:ident),* $(,)?> for struct $struct_name:path {
124         $($field:ident $(-> $delegate:tt)?),* $(,)?
125     }) => {
126         impl<$($T,)*>
127             ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
128             where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
129         {
130             #[inline]
131             fn hash_stable(&self,
132                            __ctx: &mut $crate::ich::StableHashingContext<'a>,
133                            __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
134                 let $struct_name {
135                     $(ref $field),*
136                 } = *self;
137
138                 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*
139             }
140         }
141     };
142     // Tuple structs
143     // We cannot use normal parentheses here, the parser won't allow it
144     (tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),*  $(,)? }) => {
145         impl_stable_hash_for!(
146             impl<> for tuple_struct $struct_name { $($field $(-> $delegate)?),* }
147         );
148     };
149     (impl<$($T:ident),* $(,)?>
150      for tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),*  $(,)? }) => {
151         impl<$($T,)*>
152             ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
153             where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
154         {
155             #[inline]
156             fn hash_stable(&self,
157                            __ctx: &mut $crate::ich::StableHashingContext<'a>,
158                            __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
159                 let $struct_name (
160                     $(ref $field),*
161                 ) = *self;
162
163                 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*
164             }
165         }
166     };
167 }
168
169 ///////////////////////////////////////////////////////////////////////////
170 // Lift and TypeFoldable macros
171 //
172 // When possible, use one of these (relatively) convenient macros to write
173 // the impls for you.
174
175 #[macro_export]
176 macro_rules! CloneLiftImpls {
177     (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
178         $(
179             impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
180                 type Lifted = Self;
181                 fn lift_to_tcx(&self, _: $crate::ty::TyCtxt<$tcx>) -> Option<Self> {
182                     Some(Clone::clone(self))
183                 }
184             }
185         )+
186     };
187
188     ($($ty:ty,)+) => {
189         CloneLiftImpls! {
190             for <'tcx> {
191                 $($ty,)+
192             }
193         }
194     };
195 }
196
197 /// Used for types that are `Copy` and which **do not care arena
198 /// allocated data** (i.e., don't need to be folded).
199 #[macro_export]
200 macro_rules! CloneTypeFoldableImpls {
201     (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
202         $(
203             impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
204                 fn super_fold_with<F: $crate::ty::fold::TypeFolder<$tcx>>(
205                     &self,
206                     _: &mut F
207                 ) -> $ty {
208                     Clone::clone(self)
209                 }
210
211                 fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
212                     &self,
213                     _: &mut F)
214                     -> bool
215                 {
216                     false
217                 }
218             }
219         )+
220     };
221
222     ($($ty:ty,)+) => {
223         CloneTypeFoldableImpls! {
224             for <'tcx> {
225                 $($ty,)+
226             }
227         }
228     };
229 }
230
231 #[macro_export]
232 macro_rules! CloneTypeFoldableAndLiftImpls {
233     ($($t:tt)*) => {
234         CloneTypeFoldableImpls! { $($t)* }
235         CloneLiftImpls! { $($t)* }
236     }
237 }
238
239 #[macro_export]
240 macro_rules! EnumTypeFoldableImpl {
241     (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
242         $($variants:tt)*
243     } $(where $($wc:tt)*)*) => {
244         impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
245             $(where $($wc)*)*
246         {
247             fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
248                 &self,
249                 folder: &mut V,
250             ) -> Self {
251                 EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
252             }
253
254             fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
255                 &self,
256                 visitor: &mut V,
257             ) -> bool {
258                 EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
259             }
260         }
261     };
262
263     (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
264         match $this {
265             $($output)*
266         }
267     };
268
269     (@FoldVariants($this:expr, $folder:expr)
270      input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
271      output( $($output:tt)*) ) => {
272         EnumTypeFoldableImpl!(
273             @FoldVariants($this, $folder)
274                 input($($input)*)
275                 output(
276                     $variant ( $($variant_arg),* ) => {
277                         $variant (
278                             $($crate::ty::fold::TypeFoldable::fold_with($variant_arg, $folder)),*
279                         )
280                     }
281                     $($output)*
282                 )
283         )
284     };
285
286     (@FoldVariants($this:expr, $folder:expr)
287      input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
288      output( $($output:tt)*) ) => {
289         EnumTypeFoldableImpl!(
290             @FoldVariants($this, $folder)
291                 input($($input)*)
292                 output(
293                     $variant { $($variant_arg),* } => {
294                         $variant {
295                             $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
296                                 $variant_arg, $folder
297                             )),* }
298                     }
299                     $($output)*
300                 )
301         )
302     };
303
304     (@FoldVariants($this:expr, $folder:expr)
305      input( ($variant:path), $($input:tt)*)
306      output( $($output:tt)*) ) => {
307         EnumTypeFoldableImpl!(
308             @FoldVariants($this, $folder)
309                 input($($input)*)
310                 output(
311                     $variant => { $variant }
312                     $($output)*
313                 )
314         )
315     };
316
317     (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
318         match $this {
319             $($output)*
320         }
321     };
322
323     (@VisitVariants($this:expr, $visitor:expr)
324      input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
325      output( $($output:tt)*) ) => {
326         EnumTypeFoldableImpl!(
327             @VisitVariants($this, $visitor)
328                 input($($input)*)
329                 output(
330                     $variant ( $($variant_arg),* ) => {
331                         false $(|| $crate::ty::fold::TypeFoldable::visit_with(
332                             $variant_arg, $visitor
333                         ))*
334                     }
335                     $($output)*
336                 )
337         )
338     };
339
340     (@VisitVariants($this:expr, $visitor:expr)
341      input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
342      output( $($output:tt)*) ) => {
343         EnumTypeFoldableImpl!(
344             @VisitVariants($this, $visitor)
345                 input($($input)*)
346                 output(
347                     $variant { $($variant_arg),* } => {
348                         false $(|| $crate::ty::fold::TypeFoldable::visit_with(
349                             $variant_arg, $visitor
350                         ))*
351                     }
352                     $($output)*
353                 )
354         )
355     };
356
357     (@VisitVariants($this:expr, $visitor:expr)
358      input( ($variant:path), $($input:tt)*)
359      output( $($output:tt)*) ) => {
360         EnumTypeFoldableImpl!(
361             @VisitVariants($this, $visitor)
362                 input($($input)*)
363                 output(
364                     $variant => { false }
365                     $($output)*
366                 )
367         )
368     };
369 }