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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! Module for rewriting source text to reflect changes in the AST.
//!
//! Rewriting takes as input an old AST, a new AST, and source text that parses to the old AST, and
//! transforms that source text into text that parses to the new AST.  Rewriting is designed to
//! preserve comments and whitespace whenever possible.
//!
//! At a high level, rewriting is a recursive traversal on the old and new ASTs.  Everywhere the
//! two are equal, there is no work to do.  But where they differ, it applies a number of "rewrite
//! strategies" that attempt to turn the old text into new text.  In cases where no strategy can
//! perform the rewrite, it propagates the error upward, trying the available strategies to rewrite
//! enclosing nodes of the ASTs.
//!
//! The core of the actual implementation is the `Rewrite::rewrite(old, new, rcx) -> bool` method,
//! which attempts to rewrite the `old` AST into the `new` AST.  The implementation of this method
//! for each node type simply tries each applicable strategy for the node type until either one of
//! the strategies succeeds or it runs out of strategies to try.  `Rewrite::rewrite` is not
//! (directly) recursive - the recursive traversal is handled by the `recursive` strategy.
//!
//! There are three core rewrite strategies:
//!
//!  * `equal`: If the two nodes are equal, rewriting succeeds.  If they aren't, it fails.  In
//!    either case, this strategy performs no actual rewrites.
//!
//!    For leaf nodes, this strategy is tried first.
//!
//!  * `recursive`: If every child of the first can be rewritten to the corresponding child of the
//!    second, then rewriting succeeds.  For nodes of enum type, the two nodes must be instances of
//!    the same variant (otherwise there would be no correspondence between the old and new nodes'
//!    children).  If the variants are unequal or rewriting of any child fails, then the overall
//!    rewrite fails.  This strategy performs no rewrites beyond those performed by its recursive
//!    calls.
//!
//!    This is where the recursion happens in the actual implementation.  Since it implements a
//!    superset of `equal`'s functionality, it replaces `equal` as the first strategy to try for
//!    all non-leaf node types.
//!
//!  * `print`: Pretty-prints the new node, and performs a rewrite to replace the old source with
//!    this new source text.  This strategy always succeeds, but is only implemented for a few node
//!    types (mostly major ones such as `Item`, `Expr`, etc).
//!
//!    Since pretty-printer's output is cosmetically quite bad (it includes no comments, prints
//!    macros in expanded form, and sometimes makes questionable decisions regarding whitespace),
//!    the `print` strategy tries to replace pretty-printer output with original (user-written)
//!    source text whenever possible.  See the `rewrite::strategy::print` module docs for details.
//!
//!    Since this strategy always succeeds, but often produces bad results, it is tried last for
//!    any node types that support it.
//!
//! Since `print` and the more specialized (non-core) strategies only work for a small set of node
//! types, for most nodes `Rewrite::rewrite` simply tries `equal` (leaf nodes) or `recursive`
//! (non-leaf nodes), and fails if the strategy fails.  This failure will cause a failure in the
//! enclosing `recursive`, and will propagate upward until it reaches a node type that actually
//! does support another strategy, such as `Item`.  This is the point where rewriting actually
//! happens: when `recursive` fails, `Rewrite::rewrite` will try the next strategy (such as
//! `print`), which can perform rewrites to correct the error at this higher level.

use rustc::session::Session;
use std::collections::HashMap;
use std::mem;
use std::ops::{Deref, DerefMut};
use syntax::ast::*;
use syntax::source_map::{Span, DUMMY_SP};
use syntax::util::parser;

use crate::ast_manip::ast_map::{map_ast, AstMap};
use crate::ast_manip::{GetSpan, Visit};
use crate::driver;

mod cleanup;
pub mod files;
pub mod json;

mod base;
mod strategy;

pub use self::base::Rewrite;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TextAdjust {
    None,
    Parenthesize,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TextRewrite {
    pub old_span: Span,
    pub new_span: Span,
    /// Additional rewrites to apply after replacing the `old_span` text with the `new_span` text.
    pub rewrites: Vec<TextRewrite>,
    /// Locations of nodes within the new text.  The `Span` is a subspan of `new_span`, while the
    /// `NodeId` is the ID of the new node.
    pub nodes: Vec<(Span, NodeId)>,
    pub adjust: TextAdjust,
}

impl TextRewrite {
    pub fn new(old_span: Span, new_span: Span) -> TextRewrite {
        Self::adjusted(old_span, new_span, TextAdjust::None)
    }

    pub fn adjusted(old_span: Span, new_span: Span, adjust: TextAdjust) -> TextRewrite {
        TextRewrite {
            old_span,
            new_span,
            adjust,
            rewrites: Vec::new(),
            nodes: Vec::new(),
        }
    }
}

/// Common ID type for nodes and `Attribute`s.  Both are sequence items, but `Attribute`s have
/// their own custom ID type for some reason.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SeqItemId {
    Node(NodeId),
    Attr(AttrId),
}

trait MappableId {
    fn map_id(self, rcx: &RewriteCtxt) -> Self;
}

impl MappableId for NodeId {
    fn map_id(self, rcx: &RewriteCtxt) -> Self {
        rcx.node_id_map.get(&self).map_or(DUMMY_NODE_ID, |&x| x)
    }
}

impl MappableId for AttrId {
    fn map_id(self, _rcx: &RewriteCtxt) -> Self {
        self
    }
}

impl MappableId for SeqItemId {
    fn map_id(self, rcx: &RewriteCtxt) -> Self {
        match self {
            SeqItemId::Node(id) => SeqItemId::Node(id.map_id(rcx)),
            SeqItemId::Attr(id) => SeqItemId::Attr(id.map_id(rcx)),
        }
    }
}

/// Precedence information about the context surrounding an expression.  Used to determine whether
/// an expr needs to be parenthesized.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ExprPrec {
    /// Normal behavior.  Parenthesize expr if its precedence is less than the given value.
    Normal(i8),
    /// Conditional-like position.  Parenthesize lower precedence, and also parenthesize exprs with
    /// outer struct literals.
    Cond(i8),
    /// Callee position.  Parenthesize lower precedence, and also parenthesize struct and tuple
    /// field expressions (so the call is not mistaken for a method call).
    Callee(i8),
    /// Left of < or <<. We have to parenthesize casts in this position because
    /// the less than is interpreted as the start of generic arguments.
    LeftLess(i8),
}

pub struct RewriteCtxt<'s> {
    sess: &'s Session,
    old_nodes: AstMap<'s>,
    text_span_cache: HashMap<String, Span>,

    /// The span of the new AST the last time we entered "fresh" mode.  This lets us avoid infinite
    /// recursion - see comment in `splice_fresh`.
    fresh_start: Span,

    /// Precedence of the current expression context.  If we splice in an expression of lower
    /// precedence, it will be parenthesized.
    expr_prec: ExprPrec,

    /// Mapping from NodeIds in the new AST to corresponding NodeIds in the old AST.  This has two
    /// purposes.  (1) If `node_id_map[new_node.id] == old_node.id`, then `new_node` and `old_node`
    /// are considered "the same node" for sequence rewriting purposes.  This affects the
    /// rewriter's decisions about where to insert/delete sequence elements, as opposed to
    /// rewriting the old node to the new one.  (2) When the rewriter is in "fresh" mode and
    /// looking for recycled text to splice in, it checks `old_nodes` for a node whose ID is
    /// `node_id_map[new_node.id]`.
    node_id_map: HashMap<NodeId, NodeId>,
}

impl<'s> RewriteCtxt<'s> {
    fn new(
        sess: &'s Session,
        old_nodes: AstMap<'s>,
        node_id_map: HashMap<NodeId, NodeId>,
    ) -> RewriteCtxt<'s> {
        RewriteCtxt {
            sess,
            old_nodes,
            text_span_cache: HashMap::new(),

            fresh_start: DUMMY_SP,
            expr_prec: ExprPrec::Normal(parser::PREC_RESET),
            node_id_map,
        }
    }

    pub fn session(&self) -> &'s Session {
        self.sess
    }

    pub fn old_nodes(&self) -> &AstMap<'s> {
        &self.old_nodes
    }

    pub fn fresh_start(&self) -> Span {
        self.fresh_start
    }

    pub fn replace_fresh_start(&mut self, span: Span) -> Span {
        mem::replace(&mut self.fresh_start, span)
    }

    pub fn expr_prec(&self) -> ExprPrec {
        self.expr_prec
    }

    pub fn replace_expr_prec(&mut self, prec: ExprPrec) -> ExprPrec {
        mem::replace(&mut self.expr_prec, prec)
    }

    fn new_to_old_id<Id: MappableId>(&self, id: Id) -> Id {
        id.map_id(self)
    }

    pub fn enter<'b>(&'b mut self, rw: &'b mut TextRewrite) -> RewriteCtxtRef<'s, 'b> {
        RewriteCtxtRef { cx: self, rw }
    }

    pub fn text_span(&mut self, s: &str) -> Span {
        if let Some(&sp) = self.text_span_cache.get(s) {
            return sp;
        }

        let sp = driver::make_span_for_text(self.sess.source_map(), s);
        self.text_span_cache.insert(s.to_owned(), sp);
        sp
    }
}

pub struct RewriteCtxtRef<'s: 'a, 'a> {
    cx: &'a mut RewriteCtxt<'s>,
    rw: &'a mut TextRewrite,
}

impl<'s, 'a> Deref for RewriteCtxtRef<'s, 'a> {
    type Target = RewriteCtxt<'s>;

    fn deref(&self) -> &RewriteCtxt<'s> {
        self.cx
    }
}

impl<'s, 'a> DerefMut for RewriteCtxtRef<'s, 'a> {
    fn deref_mut(&mut self) -> &mut RewriteCtxt<'s> {
        self.cx
    }
}

impl<'s, 'a> RewriteCtxtRef<'s, 'a> {
    pub fn borrow<'b>(&'b mut self) -> RewriteCtxtRef<'s, 'b> {
        RewriteCtxtRef {
            cx: self.cx,
            rw: self.rw,
        }
    }

    pub fn enter<'b>(&'b mut self, rw: &'b mut TextRewrite) -> RewriteCtxtRef<'s, 'b> {
        RewriteCtxtRef { cx: self.cx, rw }
    }

    pub fn mark(&self) -> (usize, usize) {
        (self.rw.rewrites.len(), self.rw.nodes.len())
    }

    pub fn rewind(&mut self, mark: (usize, usize)) {
        self.rw.rewrites.truncate(mark.0);
        self.rw.nodes.truncate(mark.1);
    }

    pub fn record(&mut self, rw: TextRewrite) {
        self.rw.rewrites.push(rw);
    }

    pub fn record_text(&mut self, old_span: Span, text: &str) {
        let new_span = self.text_span(text);
        self.record(TextRewrite::new(old_span, new_span));
    }

    pub fn record_node_span(&mut self, span: Span, id: NodeId) {
        self.rw.nodes.push((span, id));
    }
}

pub fn rewrite<'s, T>(
    sess: &Session,
    old: &'s T,
    new: &T,
    node_id_map: HashMap<NodeId, NodeId>,
    map_extra_ast: impl FnOnce(&mut AstMap<'s>),
) -> TextRewrite
where
    T: Rewrite + Visit + GetSpan,
{
    let mut map = map_ast(old);
    map_extra_ast(&mut map);

    let mut rw = TextRewrite::new(DUMMY_SP, old.get_span());
    let mut rcx = RewriteCtxt::new(sess, map, node_id_map);
    let ok = Rewrite::rewrite(old, new, rcx.enter(&mut rw));
    assert!(ok, "rewriting did not complete");
    rw
}