]> git.proxmox.com Git - rustc.git/blob - src/librustc/macros.rs
New upstream version 1.29.0+dfsg1
[rustc.git] / src / librustc / macros.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-tidy-linelength
12
13 macro_rules! enum_from_u32 {
14 ($(#[$attr:meta])* pub enum $name:ident {
15 $($variant:ident = $e:expr,)*
16 }) => {
17 $(#[$attr])*
18 pub enum $name {
19 $($variant = $e),*
20 }
21
22 impl $name {
23 pub fn from_u32(u: u32) -> Option<$name> {
24 $(if u == $name::$variant as u32 {
25 return Some($name::$variant)
26 })*
27 None
28 }
29 }
30 };
31 ($(#[$attr:meta])* pub enum $name:ident {
32 $($variant:ident,)*
33 }) => {
34 $(#[$attr])*
35 pub enum $name {
36 $($variant,)*
37 }
38
39 impl $name {
40 pub fn from_u32(u: u32) -> Option<$name> {
41 $(if u == $name::$variant as u32 {
42 return Some($name::$variant)
43 })*
44 None
45 }
46 }
47 }
48 }
49
50 #[macro_export]
51 macro_rules! bug {
52 () => ( bug!("impossible case reached") );
53 ($($message:tt)*) => ({
54 $crate::session::bug_fmt(file!(), line!(), format_args!($($message)*))
55 })
56 }
57
58 #[macro_export]
59 macro_rules! span_bug {
60 ($span:expr, $($message:tt)*) => ({
61 $crate::session::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
62 })
63 }
64
65 #[macro_export]
66 macro_rules! __impl_stable_hash_field {
67 ($field:ident, $ctx:expr, $hasher:expr) => ($field.hash_stable($ctx, $hasher));
68 ($field:ident, $ctx:expr, $hasher:expr, _) => ({ let _ = $field; });
69 ($field:ident, $ctx:expr, $hasher:expr, $delegate:expr) => ($delegate.hash_stable($ctx, $hasher));
70 }
71
72 #[macro_export]
73 macro_rules! impl_stable_hash_for {
74 // FIXME(mark-i-m): Some of these should be `?` rather than `*`. See the git blame and change
75 // them back when `?` is supported again.
76 (enum $enum_name:path { $( $variant:ident $( ( $($field:ident $(-> $delegate:tt)*),* ) )* ),* $(,)* }) => {
77 impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $enum_name {
78 #[inline]
79 fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
80 __ctx: &mut $crate::ich::StableHashingContext<'a>,
81 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
82 use $enum_name::*;
83 ::std::mem::discriminant(self).hash_stable(__ctx, __hasher);
84
85 match *self {
86 $(
87 $variant $( ( $(ref $field),* ) )* => {
88 $($( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*)*
89 }
90 )*
91 }
92 }
93 }
94 };
95 // FIXME(mark-i-m): same here.
96 (struct $struct_name:path { $($field:ident $(-> $delegate:tt)*),* $(,)* }) => {
97 impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name {
98 #[inline]
99 fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
100 __ctx: &mut $crate::ich::StableHashingContext<'a>,
101 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
102 let $struct_name {
103 $(ref $field),*
104 } = *self;
105
106 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*
107 }
108 }
109 };
110 // FIXME(mark-i-m): same here.
111 (tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)*),* $(,)* }) => {
112 impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name {
113 #[inline]
114 fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
115 __ctx: &mut $crate::ich::StableHashingContext<'a>,
116 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
117 let $struct_name (
118 $(ref $field),*
119 ) = *self;
120
121 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*
122 }
123 }
124 };
125
126 (impl<$tcx:lifetime $(, $T:ident)*> for struct $struct_name:path {
127 $($field:ident),* $(,)*
128 }) => {
129 impl<'a, $tcx, $($T,)*> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
130 where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
131 {
132 #[inline]
133 fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
134 __ctx: &mut $crate::ich::StableHashingContext<'a>,
135 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
136 let $struct_name {
137 $(ref $field),*
138 } = *self;
139
140 $( $field.hash_stable(__ctx, __hasher));*
141 }
142 }
143 };
144 }
145
146 #[macro_export]
147 macro_rules! impl_stable_hash_for_spanned {
148 ($T:path) => (
149
150 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ::syntax::codemap::Spanned<$T>
151 {
152 #[inline]
153 fn hash_stable<W: StableHasherResult>(&self,
154 hcx: &mut StableHashingContext<'a>,
155 hasher: &mut StableHasher<W>) {
156 self.node.hash_stable(hcx, hasher);
157 self.span.hash_stable(hcx, hasher);
158 }
159 }
160 );
161 }
162
163 ///////////////////////////////////////////////////////////////////////////
164 // Lift and TypeFoldable macros
165 //
166 // When possible, use one of these (relatively) convenient macros to write
167 // the impls for you.
168
169 #[macro_export]
170 macro_rules! CloneLiftImpls {
171 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
172 $(
173 impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
174 type Lifted = Self;
175 fn lift_to_tcx<'a, 'gcx>(&self, _: $crate::ty::TyCtxt<'a, 'gcx, $tcx>) -> Option<Self> {
176 Some(Clone::clone(self))
177 }
178 }
179 )+
180 };
181
182 ($($ty:ty,)+) => {
183 CloneLiftImpls! {
184 for <'tcx> {
185 $($ty,)+
186 }
187 }
188 };
189 }
190
191 /// Used for types that are `Copy` and which **do not care arena
192 /// allocated data** (i.e., don't need to be folded).
193 #[macro_export]
194 macro_rules! CloneTypeFoldableImpls {
195 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
196 $(
197 impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
198 fn super_fold_with<'gcx: $tcx, F: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
199 &self,
200 _: &mut F
201 ) -> $ty {
202 Clone::clone(self)
203 }
204
205 fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
206 &self,
207 _: &mut F)
208 -> bool
209 {
210 false
211 }
212 }
213 )+
214 };
215
216 ($($ty:ty,)+) => {
217 CloneTypeFoldableImpls! {
218 for <'tcx> {
219 $($ty,)+
220 }
221 }
222 };
223 }
224
225 #[macro_export]
226 macro_rules! CloneTypeFoldableAndLiftImpls {
227 ($($t:tt)*) => {
228 CloneTypeFoldableImpls! { $($t)* }
229 CloneLiftImpls! { $($t)* }
230 }
231 }
232
233 #[macro_export]
234 macro_rules! BraceStructLiftImpl {
235 (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
236 type Lifted = $lifted:ty;
237 $($field:ident),* $(,)*
238 } $(where $($wc:tt)*)*) => {
239 impl<$($p),*> $crate::ty::Lift<$tcx> for $s
240 $(where $($wc)*)*
241 {
242 type Lifted = $lifted;
243
244 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<$lifted> {
245 $(let $field = tcx.lift(&self.$field)?;)*
246 Some(Self::Lifted { $($field),* })
247 }
248 }
249 };
250 }
251
252 #[macro_export]
253 macro_rules! EnumLiftImpl {
254 (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
255 type Lifted = $lifted:ty;
256 $($variants:tt)*
257 } $(where $($wc:tt)*)*) => {
258 impl<$($p),*> $crate::ty::Lift<$tcx> for $s
259 $(where $($wc)*)*
260 {
261 type Lifted = $lifted;
262
263 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<$lifted> {
264 EnumLiftImpl!(@Variants(self, tcx) input($($variants)*) output())
265 }
266 }
267 };
268
269 (@Variants($this:expr, $tcx:expr) input() output($($output:tt)*)) => {
270 match $this {
271 $($output)*
272 }
273 };
274
275 (@Variants($this:expr, $tcx:expr)
276 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
277 output( $($output:tt)*) ) => {
278 EnumLiftImpl!(
279 @Variants($this, $tcx)
280 input($($input)*)
281 output(
282 $variant ( $($variant_arg),* ) => {
283 Some($variant ( $($tcx.lift($variant_arg)?),* ))
284 }
285 $($output)*
286 )
287 )
288 };
289
290 (@Variants($this:expr, $tcx:expr)
291 input( ($variant:path), $($input:tt)*)
292 output( $($output:tt)*) ) => {
293 EnumLiftImpl!(
294 @Variants($this, $tcx)
295 input($($input)*)
296 output(
297 $variant => { Some($variant) }
298 $($output)*
299 )
300 )
301 };
302 }
303
304 #[macro_export]
305 macro_rules! BraceStructTypeFoldableImpl {
306 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
307 $($field:ident),* $(,)*
308 } $(where $($wc:tt)*)*) => {
309 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
310 $(where $($wc)*)*
311 {
312 fn super_fold_with<'gcx: $tcx, V: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
313 &self,
314 folder: &mut V,
315 ) -> Self {
316 let $s { $($field,)* } = self;
317 $s { $($field: $crate::ty::fold::TypeFoldable::fold_with($field, folder),)* }
318 }
319
320 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
321 &self,
322 visitor: &mut V,
323 ) -> bool {
324 let $s { $($field,)* } = self;
325 false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
326 }
327 }
328 };
329 }
330
331 #[macro_export]
332 macro_rules! TupleStructTypeFoldableImpl {
333 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
334 $($field:ident),* $(,)*
335 } $(where $($wc:tt)*)*) => {
336 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
337 $(where $($wc)*)*
338 {
339 fn super_fold_with<'gcx: $tcx, V: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
340 &self,
341 folder: &mut V,
342 ) -> Self {
343 let $s($($field,)*)= self;
344 $s($($crate::ty::fold::TypeFoldable::fold_with($field, folder),)*)
345 }
346
347 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
348 &self,
349 visitor: &mut V,
350 ) -> bool {
351 let $s($($field,)*) = self;
352 false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
353 }
354 }
355 };
356 }
357
358 #[macro_export]
359 macro_rules! EnumTypeFoldableImpl {
360 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
361 $($variants:tt)*
362 } $(where $($wc:tt)*)*) => {
363 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
364 $(where $($wc)*)*
365 {
366 fn super_fold_with<'gcx: $tcx, V: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
367 &self,
368 folder: &mut V,
369 ) -> Self {
370 EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
371 }
372
373 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
374 &self,
375 visitor: &mut V,
376 ) -> bool {
377 EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
378 }
379 }
380 };
381
382 (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
383 match $this {
384 $($output)*
385 }
386 };
387
388 (@FoldVariants($this:expr, $folder:expr)
389 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
390 output( $($output:tt)*) ) => {
391 EnumTypeFoldableImpl!(
392 @FoldVariants($this, $folder)
393 input($($input)*)
394 output(
395 $variant ( $($variant_arg),* ) => {
396 $variant (
397 $($crate::ty::fold::TypeFoldable::fold_with($variant_arg, $folder)),*
398 )
399 }
400 $($output)*
401 )
402 )
403 };
404
405 (@FoldVariants($this:expr, $folder:expr)
406 input( ($variant:path) { $($variant_arg:ident),* $(,)* } , $($input:tt)*)
407 output( $($output:tt)*) ) => {
408 EnumTypeFoldableImpl!(
409 @FoldVariants($this, $folder)
410 input($($input)*)
411 output(
412 $variant { $($variant_arg),* } => {
413 $variant {
414 $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
415 $variant_arg, $folder
416 )),* }
417 }
418 $($output)*
419 )
420 )
421 };
422
423 (@FoldVariants($this:expr, $folder:expr)
424 input( ($variant:path), $($input:tt)*)
425 output( $($output:tt)*) ) => {
426 EnumTypeFoldableImpl!(
427 @FoldVariants($this, $folder)
428 input($($input)*)
429 output(
430 $variant => { $variant }
431 $($output)*
432 )
433 )
434 };
435
436 (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
437 match $this {
438 $($output)*
439 }
440 };
441
442 (@VisitVariants($this:expr, $visitor:expr)
443 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
444 output( $($output:tt)*) ) => {
445 EnumTypeFoldableImpl!(
446 @VisitVariants($this, $visitor)
447 input($($input)*)
448 output(
449 $variant ( $($variant_arg),* ) => {
450 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
451 $variant_arg, $visitor
452 ))*
453 }
454 $($output)*
455 )
456 )
457 };
458
459 (@VisitVariants($this:expr, $visitor:expr)
460 input( ($variant:path) { $($variant_arg:ident),* $(,)* } , $($input:tt)*)
461 output( $($output:tt)*) ) => {
462 EnumTypeFoldableImpl!(
463 @VisitVariants($this, $visitor)
464 input($($input)*)
465 output(
466 $variant { $($variant_arg),* } => {
467 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
468 $variant_arg, $visitor
469 ))*
470 }
471 $($output)*
472 )
473 )
474 };
475
476 (@VisitVariants($this:expr, $visitor:expr)
477 input( ($variant:path), $($input:tt)*)
478 output( $($output:tt)*) ) => {
479 EnumTypeFoldableImpl!(
480 @VisitVariants($this, $visitor)
481 input($($input)*)
482 output(
483 $variant => { false }
484 $($output)*
485 )
486 )
487 };
488 }
489