visit.rs 16.7 KB
Newer Older
N
Niko Matsakis 已提交
1 2 3 4 5 6 7 8 9 10
// Copyright 2012-2014 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.

11
use middle::def_id::DefId;
12 13
use middle::ty::Region;
use mir::repr::*;
14
use rustc_data_structures::tuple_slice::TupleSlice;
15
use syntax::codemap::Span;
N
Niko Matsakis 已提交
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

pub trait Visitor<'tcx> {
    // Override these, and call `self.super_xxx` to revert back to the
    // default behavior.

    fn visit_mir(&mut self, mir: &Mir<'tcx>) {
        self.super_mir(mir);
    }

    fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
        self.super_basic_block_data(block, data);
    }

    fn visit_statement(&mut self, block: BasicBlock, statement: &Statement<'tcx>) {
        self.super_statement(block, statement);
    }

    fn visit_assign(&mut self, block: BasicBlock, lvalue: &Lvalue<'tcx>, rvalue: &Rvalue<'tcx>) {
        self.super_assign(block, lvalue, rvalue);
    }

    fn visit_terminator(&mut self, block: BasicBlock, terminator: &Terminator<'tcx>) {
        self.super_terminator(block, terminator);
    }

    fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
        self.super_rvalue(rvalue);
    }

    fn visit_operand(&mut self, operand: &Operand<'tcx>) {
        self.super_operand(operand);
    }

    fn visit_lvalue(&mut self, lvalue: &Lvalue<'tcx>, context: LvalueContext) {
        self.super_lvalue(lvalue, context);
    }

    fn visit_branch(&mut self, source: BasicBlock, target: BasicBlock) {
        self.super_branch(source, target);
    }

    fn visit_constant(&mut self, constant: &Constant<'tcx>) {
        self.super_constant(constant);
    }

61 62 63 64 65 66 67 68 69 70 71 72
    fn visit_literal(&mut self, literal: &Literal<'tcx>) {
        self.super_literal(literal);
    }

    fn visit_def_id(&mut self, def_id: DefId) {
        self.super_def_id(def_id);
    }

    fn visit_span(&mut self, span: Span) {
        self.super_span(span);
    }

N
Niko Matsakis 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    // The `super_xxx` methods comprise the default behavior and are
    // not meant to be overidden.

    fn super_mir(&mut self, mir: &Mir<'tcx>) {
        for block in mir.all_basic_blocks() {
            let data = mir.basic_block_data(block);
            self.visit_basic_block_data(block, data);
        }
    }

    fn super_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
        for statement in &data.statements {
            self.visit_statement(block, statement);
        }
        self.visit_terminator(block, &data.terminator);
    }

    fn super_statement(&mut self, block: BasicBlock, statement: &Statement<'tcx>) {
91 92
        self.visit_span(statement.span);

N
Niko Matsakis 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        match statement.kind {
            StatementKind::Assign(ref lvalue, ref rvalue) => {
                self.visit_assign(block, lvalue, rvalue);
            }
            StatementKind::Drop(_, ref lvalue) => {
                self.visit_lvalue(lvalue, LvalueContext::Drop);
            }
        }
    }

    fn super_assign(&mut self, _block: BasicBlock, lvalue: &Lvalue<'tcx>, rvalue: &Rvalue<'tcx>) {
        self.visit_lvalue(lvalue, LvalueContext::Store);
        self.visit_rvalue(rvalue);
    }

    fn super_terminator(&mut self, block: BasicBlock, terminator: &Terminator<'tcx>) {
        match *terminator {
            Terminator::Goto { target } |
            Terminator::Panic { target } => {
                self.visit_branch(block, target);
            }

            Terminator::If { ref cond, ref targets } => {
                self.visit_operand(cond);
117
                for &target in targets.as_slice() {
N
Niko Matsakis 已提交
118 119 120 121 122 123 124 125 126 127 128
                    self.visit_branch(block, target);
                }
            }

            Terminator::Switch { ref discr, adt_def: _, ref targets } => {
                self.visit_lvalue(discr, LvalueContext::Inspect);
                for &target in targets {
                    self.visit_branch(block, target);
                }
            }

129 130 131 132 133 134 135
            Terminator::SwitchInt { ref discr, switch_ty: _, values: _, ref targets } => {
                self.visit_lvalue(discr, LvalueContext::Inspect);
                for &target in targets {
                    self.visit_branch(block, target);
                }
            }

N
Niko Matsakis 已提交
136 137 138 139 140 141 142 143 144 145
            Terminator::Diverge |
            Terminator::Return => {
            }

            Terminator::Call { ref data, ref targets } => {
                self.visit_lvalue(&data.destination, LvalueContext::Store);
                self.visit_operand(&data.func);
                for arg in &data.args {
                    self.visit_operand(arg);
                }
146
                for &target in targets.as_slice() {
N
Niko Matsakis 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160
                    self.visit_branch(block, target);
                }
            }
        }
    }

    fn super_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
        match *rvalue {
            Rvalue::Use(ref operand) => {
                self.visit_operand(operand);
            }

            Rvalue::Repeat(ref value, ref len) => {
                self.visit_operand(value);
161
                self.visit_constant(len);
N
Niko Matsakis 已提交
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
            }

            Rvalue::Ref(r, bk, ref path) => {
                self.visit_lvalue(path, LvalueContext::Borrow {
                    region: r,
                    kind: bk
                });
            }

            Rvalue::Len(ref path) => {
                self.visit_lvalue(path, LvalueContext::Inspect);
            }

            Rvalue::Cast(_, ref operand, _) => {
                self.visit_operand(operand);
            }

            Rvalue::BinaryOp(_, ref lhs, ref rhs) => {
                self.visit_operand(lhs);
                self.visit_operand(rhs);
            }

            Rvalue::UnaryOp(_, ref op) => {
                self.visit_operand(op);
            }

            Rvalue::Box(_) => {
            }

            Rvalue::Aggregate(_, ref operands) => {
                for operand in operands {
                    self.visit_operand(operand);
                }
            }

            Rvalue::Slice { ref input, from_start, from_end } => {
                self.visit_lvalue(input, LvalueContext::Slice {
                    from_start: from_start,
                    from_end: from_end,
                });
            }

            Rvalue::InlineAsm(_) => {
            }
        }
    }

    fn super_operand(&mut self, operand: &Operand<'tcx>) {
        match *operand {
            Operand::Consume(ref lvalue) => {
                self.visit_lvalue(lvalue, LvalueContext::Consume);
            }
            Operand::Constant(ref constant) => {
                self.visit_constant(constant);
            }
        }
    }

    fn super_lvalue(&mut self, lvalue: &Lvalue<'tcx>, _context: LvalueContext) {
        match *lvalue {
            Lvalue::Var(_) |
            Lvalue::Temp(_) |
            Lvalue::Arg(_) |
            Lvalue::Static(_) |
            Lvalue::ReturnPointer => {
            }
            Lvalue::Projection(ref proj) => {
                self.visit_lvalue(&proj.base, LvalueContext::Projection);
            }
        }
    }

    fn super_branch(&mut self, _source: BasicBlock, _target: BasicBlock) {
    }

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    fn super_constant(&mut self, constant: &Constant<'tcx>) {
        self.visit_span(constant.span);
        self.visit_literal(&constant.literal);
    }

    fn super_literal(&mut self, literal: &Literal<'tcx>) {
        match *literal {
            Literal::Item { def_id, .. } => {
                self.visit_def_id(def_id);
            },
            Literal::Value { .. } => {
                // Nothing to do
            }
        }
    }

    fn super_def_id(&mut self, _def_id: DefId) {
    }

    fn super_span(&mut self, _span: Span) {
N
Niko Matsakis 已提交
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
    }
}

#[derive(Copy, Clone, Debug)]
pub enum LvalueContext {
    // Appears as LHS of an assignment or as dest of a call
    Store,

    // Being dropped
    Drop,

    // Being inspected in some way, like loading a len
    Inspect,

    // Being borrowed
    Borrow { region: Region, kind: BorrowKind },

    // Being sliced -- this should be same as being borrowed, probably
    Slice { from_start: usize, from_end: usize },

    // Used as base for another lvalue, e.g. `x` in `x.y`
    Projection,

    // Consumed as part of an operand
    Consume,
}
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

pub trait MutVisitor<'tcx> {
    // Override these, and call `self.super_xxx` to revert back to the
    // default behavior.

    fn visit_mir(&mut self, mir: &mut Mir<'tcx>) {
        self.super_mir(mir);
    }

    fn visit_basic_block_data(&mut self,
                              block: BasicBlock,
                              data: &mut BasicBlockData<'tcx>) {
        self.super_basic_block_data(block, data);
    }

    fn visit_statement(&mut self,
                       block: BasicBlock,
                       statement: &mut Statement<'tcx>) {
        self.super_statement(block, statement);
    }

    fn visit_assign(&mut self,
                    block: BasicBlock,
                    lvalue: &mut Lvalue<'tcx>,
                    rvalue: &mut Rvalue<'tcx>) {
        self.super_assign(block, lvalue, rvalue);
    }

    fn visit_terminator(&mut self,
                        block: BasicBlock,
                        terminator: &mut Terminator<'tcx>) {
        self.super_terminator(block, terminator);
    }

    fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>) {
        self.super_rvalue(rvalue);
    }

    fn visit_operand(&mut self, operand: &mut Operand<'tcx>) {
        self.super_operand(operand);
    }

    fn visit_lvalue(&mut self,
                    lvalue: &mut Lvalue<'tcx>,
                    context: LvalueContext) {
        self.super_lvalue(lvalue, context);
    }

    fn visit_branch(&mut self, source: BasicBlock, target: BasicBlock) {
        self.super_branch(source, target);
    }

    fn visit_constant(&mut self, constant: &mut Constant<'tcx>) {
        self.super_constant(constant);
    }

    fn visit_literal(&mut self, literal: &mut Literal<'tcx>) {
        self.super_literal(literal);
    }

    fn visit_def_id(&mut self, def_id: &mut DefId) {
        self.super_def_id(def_id);
    }

    fn visit_span(&mut self, span: &mut Span) {
        self.super_span(span);
    }

    // The `super_xxx` methods comprise the default behavior and are
    // not meant to be overidden.

    fn super_mir(&mut self, mir: &mut Mir<'tcx>) {
        for block in mir.all_basic_blocks() {
            let data = mir.basic_block_data_mut(block);
            self.visit_basic_block_data(block, data);
        }
    }

    fn super_basic_block_data(&mut self,
                              block: BasicBlock,
                              data: &mut BasicBlockData<'tcx>) {
        for statement in &mut data.statements {
            self.visit_statement(block, statement);
        }
        self.visit_terminator(block, &mut data.terminator);
    }

    fn super_statement(&mut self,
                       block: BasicBlock,
                       statement: &mut Statement<'tcx>) {
        self.visit_span(&mut statement.span);

        match statement.kind {
            StatementKind::Assign(ref mut lvalue, ref mut rvalue) => {
                self.visit_assign(block, lvalue, rvalue);
            }
            StatementKind::Drop(_, ref mut lvalue) => {
                self.visit_lvalue(lvalue, LvalueContext::Drop);
            }
        }
    }

    fn super_assign(&mut self,
                    _block: BasicBlock,
                    lvalue: &mut Lvalue<'tcx>,
                    rvalue: &mut Rvalue<'tcx>) {
        self.visit_lvalue(lvalue, LvalueContext::Store);
        self.visit_rvalue(rvalue);
    }

    fn super_terminator(&mut self,
                        block: BasicBlock,
                        terminator: &mut Terminator<'tcx>) {
        match *terminator {
            Terminator::Goto { target } |
            Terminator::Panic { target } => {
                self.visit_branch(block, target);
            }

            Terminator::If { ref mut cond, ref mut targets } => {
                self.visit_operand(cond);
                for &target in targets.as_slice() {
                    self.visit_branch(block, target);
                }
            }

            Terminator::Switch { ref mut discr, adt_def: _, ref targets } => {
                self.visit_lvalue(discr, LvalueContext::Inspect);
                for &target in targets {
                    self.visit_branch(block, target);
                }
            }

            Terminator::SwitchInt { ref mut discr, switch_ty: _, values: _, ref targets } => {
                self.visit_lvalue(discr, LvalueContext::Inspect);
                for &target in targets {
                    self.visit_branch(block, target);
                }
            }

            Terminator::Diverge |
            Terminator::Return => {
            }

            Terminator::Call { ref mut data, ref mut targets } => {
                self.visit_lvalue(&mut data.destination, LvalueContext::Store);
                self.visit_operand(&mut data.func);
                for arg in &mut data.args {
                    self.visit_operand(arg);
                }
                for &target in targets.as_slice() {
                    self.visit_branch(block, target);
                }
            }
        }
    }

    fn super_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>) {
        match *rvalue {
            Rvalue::Use(ref mut operand) => {
                self.visit_operand(operand);
            }

            Rvalue::Repeat(ref mut value, ref mut len) => {
                self.visit_operand(value);
                self.visit_constant(len);
            }

            Rvalue::Ref(r, bk, ref mut path) => {
                self.visit_lvalue(path, LvalueContext::Borrow {
                    region: r,
                    kind: bk
                });
            }

            Rvalue::Len(ref mut path) => {
                self.visit_lvalue(path, LvalueContext::Inspect);
            }

            Rvalue::Cast(_, ref mut operand, _) => {
                self.visit_operand(operand);
            }

            Rvalue::BinaryOp(_, ref mut lhs, ref mut rhs) => {
                self.visit_operand(lhs);
                self.visit_operand(rhs);
            }

            Rvalue::UnaryOp(_, ref mut op) => {
                self.visit_operand(op);
            }

            Rvalue::Box(_) => {
            }

            Rvalue::Aggregate(ref mut kind, ref mut operands) => {
                match *kind {
                    AggregateKind::Closure(ref mut def_id, _) => {
                        self.visit_def_id(def_id);
                    }
                    _ => { /* nothing to do */ }
                }

                for operand in &mut operands[..] {
                    self.visit_operand(operand);
                }
            }

            Rvalue::Slice { ref mut input, from_start, from_end } => {
                self.visit_lvalue(input, LvalueContext::Slice {
                    from_start: from_start,
                    from_end: from_end,
                });
            }

            Rvalue::InlineAsm(_) => {
            }
        }
    }

    fn super_operand(&mut self, operand: &mut Operand<'tcx>) {
        match *operand {
            Operand::Consume(ref mut lvalue) => {
                self.visit_lvalue(lvalue, LvalueContext::Consume);
            }
            Operand::Constant(ref mut constant) => {
                self.visit_constant(constant);
            }
        }
    }

    fn super_lvalue(&mut self,
                    lvalue: &mut Lvalue<'tcx>,
                    _context: LvalueContext) {
        match *lvalue {
            Lvalue::Var(_) |
            Lvalue::Temp(_) |
            Lvalue::Arg(_) |
            Lvalue::ReturnPointer => {
            }
            Lvalue::Static(ref mut def_id) => {
                self.visit_def_id(def_id);
            }
            Lvalue::Projection(ref mut proj) => {
                self.visit_lvalue(&mut proj.base, LvalueContext::Projection);
            }
        }
    }

    fn super_branch(&mut self, _source: BasicBlock, _target: BasicBlock) {
    }

    fn super_constant(&mut self, constant: &mut Constant<'tcx>) {
        self.visit_span(&mut constant.span);
        self.visit_literal(&mut constant.literal);
    }

    fn super_literal(&mut self, literal: &mut Literal<'tcx>) {
        match *literal {
            Literal::Item { ref mut def_id, .. } => {
                self.visit_def_id(def_id);
            },
            Literal::Value { .. } => {
                // Nothing to do
            }
        }
    }

    fn super_def_id(&mut self, _def_id: &mut DefId) {
    }

    fn super_span(&mut self, _span: &mut Span) {
    }
}