1 macro_rules! enum_from_u32 {
2 ($(#[$attr:meta])* pub enum $name:ident {
3 $($variant:ident = $e:expr,)*
11 pub fn from_u32(u: u32) -> Option<$name> {
12 $(if u == $name::$variant as u32 {
13 return Some($name::$variant)
19 ($(#[$attr:meta])* pub enum $name:ident {
28 pub fn from_u32(u: u32) -> Option<$name> {
29 $(if u == $name::$variant as u32 {
30 return Some($name::$variant)
40 () => ( bug!("impossible case reached") );
41 ($($message:tt)*) => ({
42 $crate::util::bug::bug_fmt(file!(), line!(), format_args!($($message)*))
47 macro_rules! span_bug {
48 ($span:expr, $($message:tt)*) => ({
49 $crate::util::bug::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
53 ///////////////////////////////////////////////////////////////////////////
54 // Lift and TypeFoldable macros
56 // When possible, use one of these (relatively) convenient macros to write
60 macro_rules! CloneLiftImpls {
61 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
63 impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
65 fn lift_to_tcx(&self, _: $crate::ty::TyCtxt<$tcx>) -> Option<Self> {
66 Some(Clone::clone(self))
81 /// Used for types that are `Copy` and which **do not care arena
82 /// allocated data** (i.e., don't need to be folded).
84 macro_rules! CloneTypeFoldableImpls {
85 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
87 impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
88 fn super_fold_with<F: $crate::ty::fold::TypeFolder<$tcx>>(
95 fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
107 CloneTypeFoldableImpls! {
116 macro_rules! CloneTypeFoldableAndLiftImpls {
118 CloneTypeFoldableImpls! { $($t)* }
119 CloneLiftImpls! { $($t)* }
124 macro_rules! EnumTypeFoldableImpl {
125 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
127 } $(where $($wc:tt)*)*) => {
128 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
131 fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
135 EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
138 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
142 EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
147 (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
153 (@FoldVariants($this:expr, $folder:expr)
154 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
155 output( $($output:tt)*) ) => {
156 EnumTypeFoldableImpl!(
157 @FoldVariants($this, $folder)
160 $variant ( $($variant_arg),* ) => {
162 $($crate::ty::fold::TypeFoldable::fold_with($variant_arg, $folder)),*
170 (@FoldVariants($this:expr, $folder:expr)
171 input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
172 output( $($output:tt)*) ) => {
173 EnumTypeFoldableImpl!(
174 @FoldVariants($this, $folder)
177 $variant { $($variant_arg),* } => {
179 $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
180 $variant_arg, $folder
188 (@FoldVariants($this:expr, $folder:expr)
189 input( ($variant:path), $($input:tt)*)
190 output( $($output:tt)*) ) => {
191 EnumTypeFoldableImpl!(
192 @FoldVariants($this, $folder)
195 $variant => { $variant }
201 (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
207 (@VisitVariants($this:expr, $visitor:expr)
208 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
209 output( $($output:tt)*) ) => {
210 EnumTypeFoldableImpl!(
211 @VisitVariants($this, $visitor)
214 $variant ( $($variant_arg),* ) => {
215 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
216 $variant_arg, $visitor
224 (@VisitVariants($this:expr, $visitor:expr)
225 input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
226 output( $($output:tt)*) ) => {
227 EnumTypeFoldableImpl!(
228 @VisitVariants($this, $visitor)
231 $variant { $($variant_arg),* } => {
232 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
233 $variant_arg, $visitor
241 (@VisitVariants($this:expr, $visitor:expr)
242 input( ($variant:path), $($input:tt)*)
243 output( $($output:tt)*) ) => {
244 EnumTypeFoldableImpl!(
245 @VisitVariants($this, $visitor)
248 $variant => { false }