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
use syntax::ast::Crate;
use syntax::symbol::Symbol;

use crate::command::{CommandState, Registry};
use crate::contains_mark::contains_mark;
use crate::driver::Phase;
use crate::matcher::{MatchCtxt, Subst, mut_visit_match_with};
use crate::transform::Transform;
use c2rust_ast_builder::IntoSymbol;
use crate::RefactorCtxt;


/// # `rewrite_expr` Command
/// 
/// Usage: `rewrite_expr PAT REPL [FILTER]`
/// 
/// Marks: reads `FILTER`, if set; may read other marks depending on `PAT`
/// 
/// For every expression in the crate matching `PAT`, replace it with `REPL`.
/// `PAT` and `REPL` are both Rust expressions.  `PAT` can use placeholders to
/// capture nodes from the matched AST, and `REPL` can refer to those same
/// placeholders to substitute in the captured nodes.  See the `matcher` module for
/// details on AST pattern matching.
/// 
/// If `FILTER` is provided, only expressions marked `FILTER` will be rewritten.
/// This usage is obsolete - change `PAT` to `marked!(PAT, FILTER)` to get the same
/// behavior.
/// 
/// Example:
/// 
///     fn double(x: i32) -> i32 {
///         x * 2
///     }
/// 
/// After running `rewrite_expr '$e * 2' '$e + $e'`:
/// 
///     fn double(x: i32) -> i32 {
///         x + x
///     }
/// 
/// Here `$e * 2` matches `x * 2`, capturing `x` as `$e`.  Then `x` is
/// substituted for `$e` in `$e + $e`, producing the final expression `x + x`.
pub struct RewriteExpr {
    pub pat: String,
    pub repl: String,
    pub filter: Option<Symbol>,
}

impl Transform for RewriteExpr {
    fn transform(&self, krate: &mut Crate, st: &CommandState, cx: &RefactorCtxt) {
        let mut mcx = MatchCtxt::new(st, cx);
        let pat = mcx.parse_expr(&self.pat);
        let repl = mcx.parse_expr(&self.repl);
        mut_visit_match_with(mcx, pat, krate, |ast, mcx| {
            if let Some(filter) = self.filter {
                if !contains_mark(&**ast, filter, st) {
                    return;
                }
            }

            *ast = repl.clone().subst(st, cx, &mcx.bindings);
        })
    }

    fn min_phase(&self) -> Phase {
        Phase::Phase3
    }
}


/// # `rewrite_ty` Command
/// 
/// Usage: `rewrite_ty PAT REPL [FILTER]`
/// 
/// Marks: reads `FILTER`, if set; may read other marks depending on `PAT`
/// 
/// For every type in the crate matching `PAT`, replace it with `REPL`.  `PAT` and
/// `REPL` are both Rust types.  `PAT` can use placeholders to capture nodes from
/// the matched AST, and `REPL` can refer to those same placeholders to substitute
/// in the captured nodes.  See the `matcher` module for details on AST pattern
/// matching.
/// 
/// If `FILTER` is provided, only expressions marked `FILTER` will be rewritten.
/// This usage is obsolete - change `PAT` to `marked!(PAT, FILTER)` to get the same
/// behavior.
/// 
/// See the documentation for `rewrite_expr` for an example of this style of
/// rewriting.
pub struct RewriteTy {
    pub pat: String,
    pub repl: String,
    pub filter: Option<Symbol>,
}

impl Transform for RewriteTy {
    fn transform(&self, krate: &mut Crate, st: &CommandState, cx: &RefactorCtxt) {
        let mut mcx = MatchCtxt::new(st, cx);
        let pat = mcx.parse_ty(&self.pat);
        let repl = mcx.parse_ty(&self.repl);
        mut_visit_match_with(mcx, pat, krate, |ast, mcx| {
            if let Some(filter) = self.filter {
                if !contains_mark(&**ast, filter, st) {
                    return;
                }
            }

            *ast = repl.clone().subst(st, cx, &mcx.bindings);
        })
    }

    fn min_phase(&self) -> Phase {
        Phase::Phase3
    }
}


/// # `rewrite_stmts` Command
///
/// Usage: `rewrite_stmts PAT REPL`
///
/// For every statement sequence in the crate matching `PAT`, replace it with `REPL`.  `PAT` and
/// `REPL` are both Rust statement sequences.  `PAT` can use placeholders to capture nodes from
/// the matched AST, and `REPL` can refer to those same placeholders to substitute
/// in the captured nodes.  See the `matcher` module for details on AST pattern
/// matching.
///
/// See the documentation for `rewrite_expr` for an example of this style of
/// rewriting.
pub struct RewriteStmts {
    pub pat: String,
    pub repl: String,
}

impl Transform for RewriteStmts {
    fn transform(&self, krate: &mut Crate, st: &CommandState, cx: &RefactorCtxt) {
        let mut mcx = MatchCtxt::new(st, cx);
        let pat = mcx.parse_stmts(&self.pat);
        let repl = mcx.parse_stmts(&self.repl);
        mut_visit_match_with(mcx, pat, krate, |ast, mcx| {
            *ast = repl.clone().subst(st, cx, &mcx.bindings);
        })
    }

    fn min_phase(&self) -> Phase {
        Phase::Phase3
    }
}


pub struct DebugMatchExpr {
    pub pat: String,
}

impl Transform for DebugMatchExpr {
    fn transform(&self, krate: &mut Crate, st: &CommandState, cx: &RefactorCtxt) {

        let mut init_mcx = MatchCtxt::new(st, cx);
        init_mcx.debug = true;
        let pat = init_mcx.parse_expr(&self.pat);
        mut_visit_match_with(init_mcx, pat, krate, |ast, _mcx| {
            eprintln!("matched node {:?}", ast);
        })
    }

    fn min_phase(&self) -> Phase {
        Phase::Phase3
    }
}



pub fn register_commands(reg: &mut Registry) {
    use super::mk;

    reg.register("rewrite_expr", |args| mk(RewriteExpr {
        pat: args[0].clone(),
        repl: args[1].clone(),
        filter: if args.len() >= 3 { Some((&args[2]).into_symbol()) } else { None },
    }));
    reg.register("rewrite_ty", |args| mk(RewriteTy {
        pat: args[0].clone(),
        repl: args[1].clone(),
        filter: if args.len() >= 3 { Some((&args[2]).into_symbol()) } else { None },
    }));
    reg.register("rewrite_stmts", |args| mk(RewriteStmts {
        pat: args[0].clone(),
        repl: args[1].clone(),
    }));

    reg.register("debug_match_expr", |args| mk(DebugMatchExpr {
        pat: args[0].clone(),
    }));
}