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