rerast 0.1.48

An AST (abstract syntax tree) based search replace tool for code written in Rust
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::rule_finder::StartMatch;
use rustc::hir::{self, HirId};
use std::collections::{HashMap, HashSet};
use std::vec::Vec;
use syntax::symbol::Symbol;

#[derive(Debug)]
pub(crate) struct Rule<'gcx, T: StartMatch> {
    pub(crate) search: &'gcx T,
    pub(crate) replace: &'gcx T,
    // The method in which the rule is defined.
    pub(crate) body_id: hir::BodyId,
    // Maps from the names of declared variables (which must be unique within the search pattern) to
    // their HirId. This is used to pair up variables in the search pattern with their counterparts
    // in the replacement pattern. This is necessary since as far as rustc is concerned, they're
    // completely unrelated definitions. It isn't needed for expression placeholders since they're
    // declared as arguments to the function, so the search and replace pattern can both reference
    // the same placeholder variable.
    pub(crate) declared_name_hir_ids: HashMap<Symbol, HirId>,
    // IDs of the arguments to the function in which the rule was declared. When references to these
    // IDs are encountered in the search pattern, they should be treated as placeholders.
    pub(crate) placeholder_ids: HashSet<HirId>,
}

#[derive(Debug)]
pub(crate) struct Rules<'gcx> {
    pub(crate) expr_rules: Vec<Rule<'gcx, hir::Expr>>,
    pub(crate) pattern_rules: Vec<Rule<'gcx, hir::Pat>>,
    pub(crate) type_rules: Vec<Rule<'gcx, hir::Ty>>,
    pub(crate) trait_ref_rules: Vec<Rule<'gcx, hir::TraitRef>>,
}

impl<'gcx> Rules<'gcx> {
    pub(crate) fn new() -> Rules<'gcx> {
        Rules {
            expr_rules: Vec::new(),
            pattern_rules: Vec::new(),
            type_rules: Vec::new(),
            trait_ref_rules: Vec::new(),
        }
    }

    pub(crate) fn len(&self) -> usize {
        self.expr_rules.len()
            + self.pattern_rules.len()
            + self.type_rules.len()
            + self.trait_ref_rules.len()
    }
}