1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! `TryMatch` impls, to support the `matcher` module.
use rustc_target::spec::abi::Abi;
use std::rc::Rc;
use syntax::ast::*;
use syntax::ext::hygiene::SyntaxContext;
use syntax::parse::token::{DelimToken, Nonterminal, Token};
use syntax::ptr::P;
use syntax::source_map::{Span, Spanned};
use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
use syntax::ThinVec;

use crate::ast_manip::util::{macro_name, PatternSymbol};
use crate::matcher::{self, MatchCtxt, TryMatch};

impl TryMatch for Ident {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if mcx.maybe_capture_ident(self, target)? {
            return Ok(());
        }

        if self == target {
            Ok(())
        } else {
            Err(matcher::Error::SymbolMismatch)
        }
    }
}

impl TryMatch for Label {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if mcx.maybe_capture_label(self, target)? {
            return Ok(());
        }

        if self.ident == target.ident {
            Ok(())
        } else {
            Err(matcher::Error::SymbolMismatch)
        }
    }
}

impl TryMatch for Path {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if mcx.maybe_capture_path(self, target)? {
            return Ok(());
        }

        default_try_match_path(self, target, mcx)
    }
}

impl TryMatch for Expr {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if mcx.maybe_capture_expr(self, target)? {
            return Ok(());
        }

        if let ExprKind::Mac(ref mac) = self.node {
            let name = macro_name(mac);
            return match &name.as_str() as &str {
                "marked" => mcx.do_marked(
                    &mac.node.tts,
                    |p| p.parse_expr().map(|p| p.into_inner()),
                    target,
                ),
                "def" => mcx.do_def_expr(&mac.node.tts, target),
                "typed" => mcx.do_typed(
                    &mac.node.tts,
                    |p| p.parse_expr().map(|p| p.into_inner()),
                    target,
                ),
                "cast" => mcx.do_cast(&mac.node.tts, |p| p.parse_expr(), target),
                _ => Err(matcher::Error::BadSpecialPattern(name)),
            };
        }

        default_try_match_expr(self, target, mcx)
    }
}

impl TryMatch for Pat {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if mcx.maybe_capture_pat(self, target)? {
            return Ok(());
        }

        if let PatKind::Mac(ref mac) = self.node {
            let name = macro_name(mac);
            return match &name.as_str() as &str {
                "marked" => mcx.do_marked(
                    &mac.node.tts,
                    |p| p.parse_pat(None).map(|p| p.into_inner()),
                    target,
                ),
                "typed" => mcx.do_typed(
                    &mac.node.tts,
                    |p| p.parse_pat(None).map(|p| p.into_inner()),
                    target,
                ),
                _ => Err(matcher::Error::BadSpecialPattern(name)),
            };
        }

        default_try_match_pat(self, target, mcx)
    }
}

impl TryMatch for Ty {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if mcx.maybe_capture_ty(self, target)? {
            return Ok(());
        }

        if let TyKind::Mac(ref mac) = self.node {
            let name = macro_name(mac);
            return match &name.as_str() as &str {
                "marked" => mcx.do_marked(
                    &mac.node.tts,
                    |p| p.parse_ty().map(|p| p.into_inner()),
                    target,
                ),
                "def" => mcx.do_def_ty(&mac.node.tts, target),
                _ => Err(matcher::Error::BadSpecialPattern(name)),
            };
        }

        default_try_match_ty(self, target, mcx)
    }
}

impl TryMatch for Stmt {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if mcx.maybe_capture_stmt(self, target)? {
            return Ok(());
        }

        default_try_match_stmt(self, target, mcx)
    }
}

impl TryMatch for Block {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        mcx.try_match(&self.id, &target.id)?;
        mcx.try_match(&self.rules, &target.rules)?;
        mcx.try_match(&self.span, &target.span)?;

        if let Some(consumed) = matcher::match_multi_stmt(mcx, &self.stmts, &target.stmts) {
            if consumed == target.stmts.len() {
                return Ok(());
            }
        }
        Err(matcher::Error::LengthMismatch)
    }
}

impl<T: TryMatch> TryMatch for [T] {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        if self.len() != target.len() {
            return Err(matcher::Error::LengthMismatch);
        }
        for i in 0..self.len() {
            mcx.try_match(&self[i], &target[i])?;
        }
        Ok(())
    }
}

impl<T: TryMatch> TryMatch for Vec<T> {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        <[T] as TryMatch>::try_match(self, target, mcx)
    }
}

impl<T: TryMatch> TryMatch for ThinVec<T> {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        <[T] as TryMatch>::try_match(self, target, mcx)
    }
}

impl<T: TryMatch> TryMatch for P<T> {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        mcx.try_match(&**self, &**target)
    }
}

impl<T: TryMatch> TryMatch for Rc<T> {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        mcx.try_match(&**self, &**target)
    }
}

impl<T: TryMatch> TryMatch for Spanned<T> {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        mcx.try_match(&self.node, &target.node)
    }
}

#[inline]
fn default_option_match<T: TryMatch>(
    pattern: &Option<T>,
    target: &Option<T>,
    mcx: &mut MatchCtxt,
) -> matcher::Result<()> {
    match (pattern, target) {
        (&Some(ref x), &Some(ref y)) => mcx.try_match(x, y),
        (&None, &None) => Ok(()),
        (_, _) => Err(matcher::Error::VariantMismatch),
    }
}

// Default implementation for Option nodes without PatternSymbol
impl<T: TryMatch> TryMatch for Option<T> {
    default fn try_match(&self, target: &Option<T>, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        default_option_match(self, target, mcx)
    }
}

// Specialized implementation for Option<T: PatternSymbol> nodes,
// which lets us check the pattern against optional bindings
impl<T: TryMatch + PatternSymbol> TryMatch for Option<T> {
    fn try_match(&self, target: &Option<T>, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        match (self, target) {
            (&Some(ref x), None) if mcx.is_opt_binding(x) => mcx.capture_opt_none(x),
            _ => default_option_match(self, target, mcx),
        }
    }
}

impl<T: TryMatch + PatternSymbol> TryMatch for Option<P<T>> {
    fn try_match(&self, target: &Option<P<T>>, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        match (self, target) {
            (&Some(ref x), None) if mcx.is_opt_binding(&**x) => mcx.capture_opt_none(&**x),
            _ => default_option_match(self, target, mcx),
        }
    }
}

impl<A: TryMatch, B: TryMatch> TryMatch for (A, B) {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        mcx.try_match(&self.0, &target.0)?;
        mcx.try_match(&self.1, &target.1)?;
        Ok(())
    }
}

impl<A: TryMatch, B: TryMatch, C: TryMatch> TryMatch for (A, B, C) {
    fn try_match(&self, target: &Self, mcx: &mut MatchCtxt) -> matcher::Result<()> {
        mcx.try_match(&self.0, &target.0)?;
        mcx.try_match(&self.1, &target.1)?;
        mcx.try_match(&self.2, &target.2)?;
        Ok(())
    }
}

include!(concat!(env!("OUT_DIR"), "/matcher_impls_gen.inc.rs"));