summary refs log tree commit diff
path: root/src/librustc/middle/borrowck/move_data.rs
blob: 97fd6ca5cc439fa8b187d622ca7276f9d5549be1 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/*!

Data structures used for tracking moves. Please see the extensive
comments in the section "Moves and initialization" and in `doc.rs`.

*/


use std::hashmap::{HashMap, HashSet};
use std::uint;
use middle::borrowck::*;
use middle::dataflow::DataFlowContext;
use middle::dataflow::DataFlowOperator;
use middle::ty;
use middle::typeck;
use syntax::ast;
use syntax::ast_util;
use syntax::codemap::span;
use syntax::opt_vec::OptVec;
use syntax::opt_vec;
use util::ppaux::Repr;

pub struct MoveData {
    /// Move paths. See section "Move paths" in `doc.rs`.
    paths: ~[MovePath],

    /// Cache of loan path to move path index, for easy lookup.
    path_map: HashMap<@LoanPath, MovePathIndex>,

    /// Each move or uninitialized variable gets an entry here.
    moves: ~[Move],

    /// Assignments to a variable, like `x = foo`. These are assigned
    /// bits for dataflow, since we must track them to ensure that
    /// immutable variables are assigned at most once along each path.
    var_assignments: ~[Assignment],

    /// Assignments to a path, like `x.f = foo`. These are not
    /// assigned dataflow bits, but we track them because they still
    /// kill move bits.
    path_assignments: ~[Assignment],
    assignee_ids: HashSet<ast::node_id>,
}

pub struct FlowedMoveData {
    move_data: @mut MoveData,
    //         ^~~~~~~~~~~~~
    // It makes me sad to use @mut here, except that due to
    // the visitor design, this is what gather_loans
    // must produce.

    dfcx_moves: MoveDataFlow,

    // We could (and maybe should, for efficiency) combine both move
    // and assign data flow into one, but this way it's easier to
    // distinguish the bits that correspond to moves and assignments.
    dfcx_assign: AssignDataFlow
}

/// Index into `MoveData.paths`, used like a pointer
#[deriving(Eq)]
pub struct MovePathIndex(uint);

static InvalidMovePathIndex: MovePathIndex =
    MovePathIndex(uint::max_value);

/// Index into `MoveData.moves`, used like a pointer
#[deriving(Eq)]
pub struct MoveIndex(uint);

static InvalidMoveIndex: MoveIndex =
    MoveIndex(uint::max_value);

pub struct MovePath {
    /// Loan path corresponding to this move path
    loan_path: @LoanPath,

    /// Parent pointer, `InvalidMovePathIndex` if root
    parent: MovePathIndex,

    /// Head of linked list of moves to this path,
    /// `InvalidMoveIndex` if not moved
    first_move: MoveIndex,

    /// First node in linked list of children, `InvalidMovePathIndex` if leaf
    first_child: MovePathIndex,

    /// Next node in linked list of parent's children (siblings),
    /// `InvalidMovePathIndex` if none.
    next_sibling: MovePathIndex,
}

pub enum MoveKind {
    Declared,               // When declared, variables start out "moved".
    MoveExpr(@ast::expr),   // Expression or binding that moves a variable
    MovePat(@ast::pat),     // By-move binding
    Captured(@ast::expr),   // Closure creation that moves a value
}

pub struct Move {
    /// Path being moved.
    path: MovePathIndex,

    /// id of node that is doing the move.
    id: ast::node_id,

    /// Kind of move, for error messages.
    kind: MoveKind,

    /// Next node in linked list of moves from `path`, or `InvalidMoveIndex`
    next_move: MoveIndex,
}

pub struct Assignment {
    /// Path being assigned.
    path: MovePathIndex,

    /// id where assignment occurs
    id: ast::node_id,

    /// span of node where assignment occurs
    span: span,
}

pub struct MoveDataFlowOperator;
pub type MoveDataFlow = DataFlowContext<MoveDataFlowOperator>;

pub struct AssignDataFlowOperator;
pub type AssignDataFlow = DataFlowContext<AssignDataFlowOperator>;

impl MoveData {
    pub fn new() -> MoveData {
        MoveData {
            paths: ~[],
            path_map: HashMap::new(),
            moves: ~[],
            path_assignments: ~[],
            var_assignments: ~[],
            assignee_ids: HashSet::new(),
        }
    }

    fn path<'a>(&'a self, index: MovePathIndex) -> &'a MovePath {
        //! Type safe indexing operator
        &self.paths[*index]
    }

    fn mut_path<'a>(&'a mut self, index: MovePathIndex) -> &'a mut MovePath {
        //! Type safe indexing operator
        &mut self.paths[*index]
    }

    fn move<'a>(&'a self, index: MoveIndex) -> &'a Move {
        //! Type safe indexing operator
        &self.moves[*index]
    }

    fn is_var_path(&self, index: MovePathIndex) -> bool {
        //! True if `index` refers to a variable
        self.path(index).parent == InvalidMovePathIndex
    }

    pub fn move_path(&mut self,
                     tcx: ty::ctxt,
                     lp: @LoanPath) -> MovePathIndex {
        /*!
         * Returns the existing move path index for `lp`, if any,
         * and otherwise adds a new index for `lp` and any of its
         * base paths that do not yet have an index.
         */

        match self.path_map.find(&lp) {
            Some(&index) => {
                return index;
            }
            None => {}
        }

        let index = match *lp {
            LpVar(*) => {
                let index = MovePathIndex(self.paths.len());

                self.paths.push(MovePath {
                    loan_path: lp,
                    parent: InvalidMovePathIndex,
                    first_move: InvalidMoveIndex,
                    first_child: InvalidMovePathIndex,
                    next_sibling: InvalidMovePathIndex,
                });

                index
            }

            LpExtend(base, _, _) => {
                let parent_index = self.move_path(tcx, base);
                let index = MovePathIndex(self.paths.len());

                let next_sibling = self.path(parent_index).first_child;
                self.mut_path(parent_index).first_child = index;

                self.paths.push(MovePath {
                    loan_path: lp,
                    parent: parent_index,
                    first_move: InvalidMoveIndex,
                    first_child: InvalidMovePathIndex,
                    next_sibling: next_sibling,
                });

                index
            }
        };

        debug!("move_path(lp=%s, index=%?)",
               lp.repr(tcx),
               index);

        assert_eq!(*index, self.paths.len() - 1);
        self.path_map.insert(lp, index);
        return index;
    }

    fn existing_move_path(&self,
                          lp: @LoanPath)
                          -> Option<MovePathIndex> {
        self.path_map.find_copy(&lp)
    }

    fn existing_base_paths(&self,
                           lp: @LoanPath)
                           -> OptVec<MovePathIndex> {
        let mut result = opt_vec::Empty;
        self.add_existing_base_paths(lp, &mut result);
        result
    }

    fn add_existing_base_paths(&self,
                               lp: @LoanPath,
                               result: &mut OptVec<MovePathIndex>) {
        /*!
         * Adds any existing move path indices for `lp` and any base
         * paths of `lp` to `result`, but does not add new move paths
         */

        match self.path_map.find_copy(&lp) {
            Some(index) => {
                for self.each_base_path(index) |p| {
                    result.push(p);
                }
            }
            None => {
                match *lp {
                    LpVar(*) => { }
                    LpExtend(b, _, _) => {
                        self.add_existing_base_paths(b, result);
                    }
                }
            }
        }

    }

    pub fn add_move(&mut self,
                    tcx: ty::ctxt,
                    lp: @LoanPath,
                    id: ast::node_id,
                    kind: MoveKind) {
        /*!
         * Adds a new move entry for a move of `lp` that occurs at
         * location `id` with kind `kind`.
         */

        debug!("add_move(lp=%s, id=%?, kind=%?)",
               lp.repr(tcx),
               id,
               kind);

        let path_index = self.move_path(tcx, lp);
        let move_index = MoveIndex(self.moves.len());

        let next_move = self.path(path_index).first_move;
        self.mut_path(path_index).first_move = move_index;

        self.moves.push(Move {
            path: path_index,
            id: id,
            kind: kind,
            next_move: next_move
        });
    }

    pub fn add_assignment(&mut self,
                          tcx: ty::ctxt,
                          lp: @LoanPath,
                          assign_id: ast::node_id,
                          span: span,
                          assignee_id: ast::node_id) {
        /*!
         * Adds a new record for an assignment to `lp` that occurs at
         * location `id` with the given `span`.
         */

        debug!("add_assignment(lp=%s, assign_id=%?, assignee_id=%?",
               lp.repr(tcx), assign_id, assignee_id);

        let path_index = self.move_path(tcx, lp);

        self.assignee_ids.insert(assignee_id);

        let assignment = Assignment {
            path: path_index,
            id: assign_id,
            span: span,
        };

        if self.is_var_path(path_index) {
            debug!("add_assignment[var](lp=%s, assignment=%u, path_index=%?)",
                   lp.repr(tcx), self.var_assignments.len(), path_index);

            self.var_assignments.push(assignment);
        } else {
            debug!("add_assignment[path](lp=%s, path_index=%?)",
                   lp.repr(tcx), path_index);

            self.path_assignments.push(assignment);
        }
    }

    fn add_gen_kills(&self,
                     tcx: ty::ctxt,
                     dfcx_moves: &mut MoveDataFlow,
                     dfcx_assign: &mut AssignDataFlow) {
        /*!
         * Adds the gen/kills for the various moves and
         * assignments into the provided data flow contexts.
         * Moves are generated by moves and killed by assignments and
         * scoping. Assignments are generated by assignment to variables and
         * killed by scoping. See `doc.rs` for more details.
         */

        for self.moves.iter().enumerate().advance |(i, move)| {
            dfcx_moves.add_gen(move.id, i);
        }

        for self.var_assignments.iter().enumerate().advance |(i, assignment)| {
            dfcx_assign.add_gen(assignment.id, i);
            self.kill_moves(assignment.path, assignment.id, dfcx_moves);
        }

        for self.path_assignments.iter().advance |assignment| {
            self.kill_moves(assignment.path, assignment.id, dfcx_moves);
        }

        // Kill all moves related to a variable `x` when it goes out
        // of scope:
        for self.paths.iter().advance |path| {
            match *path.loan_path {
                LpVar(id) => {
                    let kill_id = tcx.region_maps.encl_scope(id);
                    let path = *self.path_map.get(&path.loan_path);
                    self.kill_moves(path, kill_id, dfcx_moves);
                }
                LpExtend(*) => {}
            }
        }

        // Kill all assignments when the variable goes out of scope:
        for self.var_assignments.iter().enumerate().advance |(assignment_index, assignment)| {
            match *self.path(assignment.path).loan_path {
                LpVar(id) => {
                    let kill_id = tcx.region_maps.encl_scope(id);
                    dfcx_assign.add_kill(kill_id, assignment_index);
                }
                LpExtend(*) => {
                    tcx.sess.bug("Var assignment for non var path");
                }
            }
        }
    }

    fn each_base_path(&self,
                      index: MovePathIndex,
                      f: &fn(MovePathIndex) -> bool)
                      -> bool {
        let mut p = index;
        while p != InvalidMovePathIndex {
            if !f(p) {
                return false;
            }
            p = self.path(p).parent;
        }
        return true;
    }

    fn each_extending_path(&self,
                           index: MovePathIndex,
                           f: &fn(MovePathIndex) -> bool) -> bool {
        if !f(index) {
            return false;
        }

        let mut p = self.path(index).first_child;
        while p != InvalidMovePathIndex {
            if !self.each_extending_path(p, |x| f(x)) {
                return false;
            }
            p = self.path(p).next_sibling;
        }

        return true;
    }

    fn each_applicable_move(&self,
                            index0: MovePathIndex,
                            f: &fn(MoveIndex) -> bool) -> bool {
        for self.each_extending_path(index0) |index| {
            let mut p = self.path(index).first_move;
            while p != InvalidMoveIndex {
                if !f(p) {
                    return false;
                }
                p = self.move(p).next_move;
            }
        }
        return true;
    }

    fn kill_moves(&self,
                  path: MovePathIndex,
                  kill_id: ast::node_id,
                  dfcx_moves: &mut MoveDataFlow) {
        for self.each_applicable_move(path) |move_index| {
            dfcx_moves.add_kill(kill_id, *move_index);
        }
    }
}

impl FlowedMoveData {
    pub fn new(move_data: @mut MoveData,
               tcx: ty::ctxt,
               method_map: typeck::method_map,
               id_range: ast_util::id_range,
               body: &ast::blk)
               -> FlowedMoveData
    {
        let mut dfcx_moves =
            DataFlowContext::new(tcx,
                                 method_map,
                                 MoveDataFlowOperator,
                                 id_range,
                                 move_data.moves.len());
        let mut dfcx_assign =
            DataFlowContext::new(tcx,
                                 method_map,
                                 AssignDataFlowOperator,
                                 id_range,
                                 move_data.var_assignments.len());
        move_data.add_gen_kills(tcx, &mut dfcx_moves, &mut dfcx_assign);
        dfcx_moves.propagate(body);
        dfcx_assign.propagate(body);
        FlowedMoveData {
            move_data: move_data,
            dfcx_moves: dfcx_moves,
            dfcx_assign: dfcx_assign,
        }
    }

    pub fn each_move_of(&self,
                        id: ast::node_id,
                        loan_path: @LoanPath,
                        f: &fn(&Move, @LoanPath) -> bool)
                        -> bool {
        /*!
         * Iterates through each move of `loan_path` (or some base path
         * of `loan_path`) that *may* have occurred on entry to `id` without
         * an intervening assignment. In other words, any moves that
         * would invalidate a reference to `loan_path` at location `id`.
         */

        // Bad scenarios:
        //
        // 1. Move of `a.b.c`, use of `a.b.c`
        // 2. Move of `a.b.c`, use of `a.b.c.d`
        // 3. Move of `a.b.c`, use of `a` or `a.b`
        //
        // OK scenario:
        //
        // 4. move of `a.b.c`, use of `a.b.d`

        let base_indices = self.move_data.existing_base_paths(loan_path);
        if base_indices.is_empty() {
            return true;
        }

        let opt_loan_path_index = self.move_data.existing_move_path(loan_path);

        for self.dfcx_moves.each_bit_on_entry_frozen(id) |index| {
            let move = &self.move_data.moves[index];
            let moved_path = move.path;
            if base_indices.iter().any_(|x| x == &moved_path) {
                // Scenario 1 or 2: `loan_path` or some base path of
                // `loan_path` was moved.
                if !f(move, self.move_data.path(moved_path).loan_path) {
                    return false;
                }
                loop;
            }

            for opt_loan_path_index.iter().advance |&loan_path_index| {
                for self.move_data.each_base_path(moved_path) |p| {
                    if p == loan_path_index {
                        // Scenario 3: some extension of `loan_path`
                        // was moved
                        if !f(move, self.move_data.path(moved_path).loan_path) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    pub fn is_assignee(&self,
                       id: ast::node_id)
                       -> bool {
        //! True if `id` is the id of the LHS of an assignment

        self.move_data.assignee_ids.iter().any_(|x| x == &id)
    }

    pub fn each_assignment_of(&self,
                              id: ast::node_id,
                              loan_path: @LoanPath,
                              f: &fn(&Assignment) -> bool)
                              -> bool {
        /*!
         * Iterates through every assignment to `loan_path` that
         * may have occurred on entry to `id`. `loan_path` must be
         * a single variable.
         */

        let loan_path_index = {
            match self.move_data.existing_move_path(loan_path) {
                Some(i) => i,
                None => {
                    // if there were any assignments, it'd have an index
                    return true;
                }
            }
        };

        for self.dfcx_assign.each_bit_on_entry_frozen(id) |index| {
            let assignment = &self.move_data.var_assignments[index];
            if assignment.path == loan_path_index && !f(assignment) {
                return false;
            }
        }
        return true;
    }
}

impl DataFlowOperator for MoveDataFlowOperator {
    #[inline]
    fn initial_value(&self) -> bool {
        false // no loans in scope by default
    }

    #[inline]
    fn join(&self, succ: uint, pred: uint) -> uint {
        succ | pred // moves from both preds are in scope
    }

    #[inline]
    fn walk_closures(&self) -> bool {
        true
    }
}

impl DataFlowOperator for AssignDataFlowOperator {
    #[inline]
    fn initial_value(&self) -> bool {
        false // no assignments in scope by default
    }

    #[inline]
    fn join(&self, succ: uint, pred: uint) -> uint {
        succ | pred // moves from both preds are in scope
    }

    #[inline]
    fn walk_closures(&self) -> bool {
        true
    }
}