]> git.lizzy.rs Git - rust.git/blob - src/librustc/macros.rs
Rollup merge of #41135 - japaric:unstable-docs, r=steveklabnik
[rust.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     (DECL IGNORED) => (_);
68     (DECL $name:ident) => (ref $name);
69     (USE IGNORED $ctx:expr, $hasher:expr) => ({});
70     (USE $name:ident, $ctx:expr, $hasher:expr) => ($name.hash_stable($ctx, $hasher));
71 }
72
73 #[macro_export]
74 macro_rules! impl_stable_hash_for {
75     (enum $enum_name:path { $( $variant:ident $( ( $($arg:ident),* ) )* ),* }) => {
76         impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'tcx>> for $enum_name {
77             #[inline]
78             fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
79                                                   __ctx: &mut $crate::ich::StableHashingContext<'a, 'tcx>,
80                                                   __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
81                 use $enum_name::*;
82                 ::std::mem::discriminant(self).hash_stable(__ctx, __hasher);
83
84                 match *self {
85                     $(
86                         $variant $( ( $( __impl_stable_hash_field!(DECL $arg) ),* ) )* => {
87                             $($( __impl_stable_hash_field!(USE $arg, __ctx, __hasher) );*)*
88                         }
89                     )*
90                 }
91             }
92         }
93     };
94     (struct $struct_name:path { $($field:ident),* }) => {
95         impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'tcx>> for $struct_name {
96             #[inline]
97             fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
98                                                   __ctx: &mut $crate::ich::StableHashingContext<'a, 'tcx>,
99                                                   __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
100                 let $struct_name {
101                     $(ref $field),*
102                 } = *self;
103
104                 $( $field.hash_stable(__ctx, __hasher));*
105             }
106         }
107     };
108     (tuple_struct $struct_name:path { $($field:ident),* }) => {
109         impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'tcx>> for $struct_name {
110             #[inline]
111             fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
112                                                   __ctx: &mut $crate::ich::StableHashingContext<'a, 'tcx>,
113                                                   __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
114                 let $struct_name (
115                     $(ref $field),*
116                 ) = *self;
117
118                 $( $field.hash_stable(__ctx, __hasher));*
119             }
120         }
121     };
122 }
123
124 #[macro_export]
125 macro_rules! impl_stable_hash_for_spanned {
126     ($T:path) => (
127
128         impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ::syntax::codemap::Spanned<$T>
129         {
130             #[inline]
131             fn hash_stable<W: StableHasherResult>(&self,
132                                                   hcx: &mut StableHashingContext<'a, 'tcx>,
133                                                   hasher: &mut StableHasher<W>) {
134                 self.node.hash_stable(hcx, hasher);
135                 self.span.hash_stable(hcx, hasher);
136             }
137         }
138     );
139 }
140