]> git.lizzy.rs Git - rust.git/blob - src/librustc/macros.rs
Rollup merge of #69473 - contrun:update-llvm, r=nikomatsakis
[rust.git] / src / librustc / macros.rs
1 #[macro_export]
2 macro_rules! bug {
3     () => ( bug!("impossible case reached") );
4     ($($message:tt)*) => ({
5         $crate::util::bug::bug_fmt(file!(), line!(), format_args!($($message)*))
6     })
7 }
8
9 #[macro_export]
10 macro_rules! span_bug {
11     ($span:expr, $($message:tt)*) => ({
12         $crate::util::bug::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
13     })
14 }
15
16 ///////////////////////////////////////////////////////////////////////////
17 // Lift and TypeFoldable macros
18 //
19 // When possible, use one of these (relatively) convenient macros to write
20 // the impls for you.
21
22 #[macro_export]
23 macro_rules! CloneLiftImpls {
24     (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
25         $(
26             impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
27                 type Lifted = Self;
28                 fn lift_to_tcx(&self, _: $crate::ty::TyCtxt<$tcx>) -> Option<Self> {
29                     Some(Clone::clone(self))
30                 }
31             }
32         )+
33     };
34
35     ($($ty:ty,)+) => {
36         CloneLiftImpls! {
37             for <'tcx> {
38                 $($ty,)+
39             }
40         }
41     };
42 }
43
44 /// Used for types that are `Copy` and which **do not care arena
45 /// allocated data** (i.e., don't need to be folded).
46 #[macro_export]
47 macro_rules! CloneTypeFoldableImpls {
48     (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
49         $(
50             impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
51                 fn super_fold_with<F: $crate::ty::fold::TypeFolder<$tcx>>(
52                     &self,
53                     _: &mut F
54                 ) -> $ty {
55                     Clone::clone(self)
56                 }
57
58                 fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
59                     &self,
60                     _: &mut F)
61                     -> bool
62                 {
63                     false
64                 }
65             }
66         )+
67     };
68
69     ($($ty:ty,)+) => {
70         CloneTypeFoldableImpls! {
71             for <'tcx> {
72                 $($ty,)+
73             }
74         }
75     };
76 }
77
78 #[macro_export]
79 macro_rules! CloneTypeFoldableAndLiftImpls {
80     ($($t:tt)*) => {
81         CloneTypeFoldableImpls! { $($t)* }
82         CloneLiftImpls! { $($t)* }
83     }
84 }
85
86 #[macro_export]
87 macro_rules! EnumTypeFoldableImpl {
88     (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
89         $($variants:tt)*
90     } $(where $($wc:tt)*)*) => {
91         impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
92             $(where $($wc)*)*
93         {
94             fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
95                 &self,
96                 folder: &mut V,
97             ) -> Self {
98                 EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
99             }
100
101             fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
102                 &self,
103                 visitor: &mut V,
104             ) -> bool {
105                 EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
106             }
107         }
108     };
109
110     (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
111         match $this {
112             $($output)*
113         }
114     };
115
116     (@FoldVariants($this:expr, $folder:expr)
117      input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
118      output( $($output:tt)*) ) => {
119         EnumTypeFoldableImpl!(
120             @FoldVariants($this, $folder)
121                 input($($input)*)
122                 output(
123                     $variant ( $($variant_arg),* ) => {
124                         $variant (
125                             $($crate::ty::fold::TypeFoldable::fold_with($variant_arg, $folder)),*
126                         )
127                     }
128                     $($output)*
129                 )
130         )
131     };
132
133     (@FoldVariants($this:expr, $folder:expr)
134      input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
135      output( $($output:tt)*) ) => {
136         EnumTypeFoldableImpl!(
137             @FoldVariants($this, $folder)
138                 input($($input)*)
139                 output(
140                     $variant { $($variant_arg),* } => {
141                         $variant {
142                             $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
143                                 $variant_arg, $folder
144                             )),* }
145                     }
146                     $($output)*
147                 )
148         )
149     };
150
151     (@FoldVariants($this:expr, $folder:expr)
152      input( ($variant:path), $($input:tt)*)
153      output( $($output:tt)*) ) => {
154         EnumTypeFoldableImpl!(
155             @FoldVariants($this, $folder)
156                 input($($input)*)
157                 output(
158                     $variant => { $variant }
159                     $($output)*
160                 )
161         )
162     };
163
164     (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
165         match $this {
166             $($output)*
167         }
168     };
169
170     (@VisitVariants($this:expr, $visitor:expr)
171      input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
172      output( $($output:tt)*) ) => {
173         EnumTypeFoldableImpl!(
174             @VisitVariants($this, $visitor)
175                 input($($input)*)
176                 output(
177                     $variant ( $($variant_arg),* ) => {
178                         false $(|| $crate::ty::fold::TypeFoldable::visit_with(
179                             $variant_arg, $visitor
180                         ))*
181                     }
182                     $($output)*
183                 )
184         )
185     };
186
187     (@VisitVariants($this:expr, $visitor:expr)
188      input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
189      output( $($output:tt)*) ) => {
190         EnumTypeFoldableImpl!(
191             @VisitVariants($this, $visitor)
192                 input($($input)*)
193                 output(
194                     $variant { $($variant_arg),* } => {
195                         false $(|| $crate::ty::fold::TypeFoldable::visit_with(
196                             $variant_arg, $visitor
197                         ))*
198                     }
199                     $($output)*
200                 )
201         )
202     };
203
204     (@VisitVariants($this:expr, $visitor:expr)
205      input( ($variant:path), $($input:tt)*)
206      output( $($output:tt)*) ) => {
207         EnumTypeFoldableImpl!(
208             @VisitVariants($this, $visitor)
209                 input($($input)*)
210                 output(
211                     $variant => { false }
212                     $($output)*
213                 )
214         )
215     };
216 }