]> git.lizzy.rs Git - rust.git/blob - src/librustc/macros.rs
Rollup merge of #66493 - JohnTitor:ping-me-rustc-guide, r=spastorino
[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 #[macro_export]
170 macro_rules! impl_stable_hash_for_spanned {
171     ($T:path) => (
172
173         impl HashStable<StableHashingContext<'a>> for ::syntax::source_map::Spanned<$T>
174         {
175             #[inline]
176             fn hash_stable(&self,
177                            hcx: &mut StableHashingContext<'a>,
178                            hasher: &mut StableHasher) {
179                 self.node.hash_stable(hcx, hasher);
180                 self.span.hash_stable(hcx, hasher);
181             }
182         }
183     );
184 }
185
186 ///////////////////////////////////////////////////////////////////////////
187 // Lift and TypeFoldable macros
188 //
189 // When possible, use one of these (relatively) convenient macros to write
190 // the impls for you.
191
192 #[macro_export]
193 macro_rules! CloneLiftImpls {
194     (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
195         $(
196             impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
197                 type Lifted = Self;
198                 fn lift_to_tcx(&self, _: $crate::ty::TyCtxt<$tcx>) -> Option<Self> {
199                     Some(Clone::clone(self))
200                 }
201             }
202         )+
203     };
204
205     ($($ty:ty,)+) => {
206         CloneLiftImpls! {
207             for <'tcx> {
208                 $($ty,)+
209             }
210         }
211     };
212 }
213
214 /// Used for types that are `Copy` and which **do not care arena
215 /// allocated data** (i.e., don't need to be folded).
216 #[macro_export]
217 macro_rules! CloneTypeFoldableImpls {
218     (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
219         $(
220             impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
221                 fn super_fold_with<F: $crate::ty::fold::TypeFolder<$tcx>>(
222                     &self,
223                     _: &mut F
224                 ) -> $ty {
225                     Clone::clone(self)
226                 }
227
228                 fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
229                     &self,
230                     _: &mut F)
231                     -> bool
232                 {
233                     false
234                 }
235             }
236         )+
237     };
238
239     ($($ty:ty,)+) => {
240         CloneTypeFoldableImpls! {
241             for <'tcx> {
242                 $($ty,)+
243             }
244         }
245     };
246 }
247
248 #[macro_export]
249 macro_rules! CloneTypeFoldableAndLiftImpls {
250     ($($t:tt)*) => {
251         CloneTypeFoldableImpls! { $($t)* }
252         CloneLiftImpls! { $($t)* }
253     }
254 }
255
256 #[macro_export]
257 macro_rules! BraceStructLiftImpl {
258     (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
259         type Lifted = $lifted:ty;
260         $($field:ident),* $(,)?
261     } $(where $($wc:tt)*)*) => {
262         impl<$($p),*> $crate::ty::Lift<$tcx> for $s
263             $(where $($wc)*)*
264         {
265             type Lifted = $lifted;
266
267             fn lift_to_tcx(&self, tcx: TyCtxt<$tcx>) -> Option<$lifted> {
268                 $(let $field = tcx.lift(&self.$field)?;)*
269                 Some(Self::Lifted { $($field),* })
270             }
271         }
272     };
273 }
274
275 #[macro_export]
276 macro_rules! EnumLiftImpl {
277     (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
278         type Lifted = $lifted:ty;
279         $($variants:tt)*
280     } $(where $($wc:tt)*)*) => {
281         impl<$($p),*> $crate::ty::Lift<$tcx> for $s
282             $(where $($wc)*)*
283         {
284             type Lifted = $lifted;
285
286             fn lift_to_tcx(&self, tcx: TyCtxt<$tcx>) -> Option<$lifted> {
287                 EnumLiftImpl!(@Variants(self, tcx) input($($variants)*) output())
288             }
289         }
290     };
291
292     (@Variants($this:expr, $tcx:expr) input() output($($output:tt)*)) => {
293         match $this {
294             $($output)*
295         }
296     };
297
298     (@Variants($this:expr, $tcx:expr)
299      input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
300      output( $($output:tt)*) ) => {
301         EnumLiftImpl!(
302             @Variants($this, $tcx)
303                 input($($input)*)
304                 output(
305                     $variant ( $($variant_arg),* ) => {
306                         Some($variant ( $($tcx.lift($variant_arg)?),* ))
307                     }
308                     $($output)*
309                 )
310         )
311     };
312
313     (@Variants($this:expr, $tcx:expr)
314      input( ($variant:path), $($input:tt)*)
315      output( $($output:tt)*) ) => {
316         EnumLiftImpl!(
317             @Variants($this, $tcx)
318                 input($($input)*)
319                 output(
320                     $variant => { Some($variant) }
321                     $($output)*
322                 )
323         )
324     };
325 }
326
327 #[macro_export]
328 macro_rules! EnumTypeFoldableImpl {
329     (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
330         $($variants:tt)*
331     } $(where $($wc:tt)*)*) => {
332         impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
333             $(where $($wc)*)*
334         {
335             fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
336                 &self,
337                 folder: &mut V,
338             ) -> Self {
339                 EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
340             }
341
342             fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
343                 &self,
344                 visitor: &mut V,
345             ) -> bool {
346                 EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
347             }
348         }
349     };
350
351     (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
352         match $this {
353             $($output)*
354         }
355     };
356
357     (@FoldVariants($this:expr, $folder:expr)
358      input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
359      output( $($output:tt)*) ) => {
360         EnumTypeFoldableImpl!(
361             @FoldVariants($this, $folder)
362                 input($($input)*)
363                 output(
364                     $variant ( $($variant_arg),* ) => {
365                         $variant (
366                             $($crate::ty::fold::TypeFoldable::fold_with($variant_arg, $folder)),*
367                         )
368                     }
369                     $($output)*
370                 )
371         )
372     };
373
374     (@FoldVariants($this:expr, $folder:expr)
375      input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
376      output( $($output:tt)*) ) => {
377         EnumTypeFoldableImpl!(
378             @FoldVariants($this, $folder)
379                 input($($input)*)
380                 output(
381                     $variant { $($variant_arg),* } => {
382                         $variant {
383                             $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
384                                 $variant_arg, $folder
385                             )),* }
386                     }
387                     $($output)*
388                 )
389         )
390     };
391
392     (@FoldVariants($this:expr, $folder:expr)
393      input( ($variant:path), $($input:tt)*)
394      output( $($output:tt)*) ) => {
395         EnumTypeFoldableImpl!(
396             @FoldVariants($this, $folder)
397                 input($($input)*)
398                 output(
399                     $variant => { $variant }
400                     $($output)*
401                 )
402         )
403     };
404
405     (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
406         match $this {
407             $($output)*
408         }
409     };
410
411     (@VisitVariants($this:expr, $visitor:expr)
412      input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
413      output( $($output:tt)*) ) => {
414         EnumTypeFoldableImpl!(
415             @VisitVariants($this, $visitor)
416                 input($($input)*)
417                 output(
418                     $variant ( $($variant_arg),* ) => {
419                         false $(|| $crate::ty::fold::TypeFoldable::visit_with(
420                             $variant_arg, $visitor
421                         ))*
422                     }
423                     $($output)*
424                 )
425         )
426     };
427
428     (@VisitVariants($this:expr, $visitor:expr)
429      input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
430      output( $($output:tt)*) ) => {
431         EnumTypeFoldableImpl!(
432             @VisitVariants($this, $visitor)
433                 input($($input)*)
434                 output(
435                     $variant { $($variant_arg),* } => {
436                         false $(|| $crate::ty::fold::TypeFoldable::visit_with(
437                             $variant_arg, $visitor
438                         ))*
439                     }
440                     $($output)*
441                 )
442         )
443     };
444
445     (@VisitVariants($this:expr, $visitor:expr)
446      input( ($variant:path), $($input:tt)*)
447      output( $($output:tt)*) ) => {
448         EnumTypeFoldableImpl!(
449             @VisitVariants($this, $visitor)
450                 input($($input)*)
451                 output(
452                     $variant => { false }
453                     $($output)*
454                 )
455         )
456     };
457 }