impls_mir.rs 17.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright 2017 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.

//! This module contains `HashStable` implementations for various MIR data
//! types in no particular order.

use ich::StableHashingContext;
use mir;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
                                           StableHasherResult};
use std::mem;


impl_stable_hash_for!(struct mir::SourceInfo { span, scope });
impl_stable_hash_for!(enum mir::Mutability { Mut, Not });
impl_stable_hash_for!(enum mir::BorrowKind { Shared, Unique, Mut });
impl_stable_hash_for!(enum mir::LocalKind { Var, Temp, Arg, ReturnPointer });
25 26 27 28 29 30 31
impl_stable_hash_for!(struct mir::LocalDecl<'tcx> {
    mutability,
    ty,
    name,
    source_info,
    is_user_variable
});
32 33
impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref });
impl_stable_hash_for!(struct mir::BasicBlockData<'tcx> { statements, terminator, is_cleanup });
34

35 36
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::Terminator<'tcx> {
37 38
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
39
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
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
                                          hasher: &mut StableHasher<W>) {
        let mir::Terminator {
            ref kind,
            ref source_info,
        } = *self;

        let hash_spans_unconditionally = match *kind {
            mir::TerminatorKind::Assert { .. } => {
                // Assert terminators generate a panic message that contains the
                // source location, so we always have to feed its span into the
                // ICH.
                true
            }
            mir::TerminatorKind::Goto { .. } |
            mir::TerminatorKind::SwitchInt { .. } |
            mir::TerminatorKind::Resume |
            mir::TerminatorKind::Return |
            mir::TerminatorKind::Unreachable |
            mir::TerminatorKind::Drop { .. } |
            mir::TerminatorKind::DropAndReplace { .. } |
            mir::TerminatorKind::Call { .. } => false,
        };

        if hash_spans_unconditionally {
            hcx.while_hashing_spans(true, |hcx| {
                source_info.hash_stable(hcx, hasher);
            })
        } else {
            source_info.hash_stable(hcx, hasher);
        }

        kind.hash_stable(hcx, hasher);
    }
}

75

76
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Local {
77 78
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
79
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
80 81 82 83 84 85
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

86
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::BasicBlock {
87 88
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
89
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
90 91 92 93 94 95
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

96
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Field {
97 98
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
99
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
100 101 102 103 104 105
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

106 107
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::VisibilityScope {
108 109
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
110
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
111 112 113 114 115 116
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

117
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Promoted {
118 119
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
120
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
121 122 123 124 125 126
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

127 128
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::TerminatorKind<'tcx> {
129
    fn hash_stable<W: StableHasherResult>(&self,
130
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
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
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::TerminatorKind::Goto { ref target } => {
                target.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::SwitchInt { ref discr,
                                             switch_ty,
                                             ref values,
                                             ref targets } => {
                discr.hash_stable(hcx, hasher);
                switch_ty.hash_stable(hcx, hasher);
                values.hash_stable(hcx, hasher);
                targets.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::Resume |
            mir::TerminatorKind::Return |
            mir::TerminatorKind::Unreachable => {}
            mir::TerminatorKind::Drop { ref location, target, unwind } => {
                location.hash_stable(hcx, hasher);
                target.hash_stable(hcx, hasher);
                unwind.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::DropAndReplace { ref location,
                                                  ref value,
                                                  target,
                                                  unwind, } => {
                location.hash_stable(hcx, hasher);
                value.hash_stable(hcx, hasher);
                target.hash_stable(hcx, hasher);
                unwind.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::Call { ref func,
                                        ref args,
                                        ref destination,
                                        cleanup } => {
                func.hash_stable(hcx, hasher);
                args.hash_stable(hcx, hasher);
                destination.hash_stable(hcx, hasher);
                cleanup.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::Assert { ref cond,
                                          expected,
                                          ref msg,
                                          target,
                                          cleanup } => {
                cond.hash_stable(hcx, hasher);
                expected.hash_stable(hcx, hasher);
                msg.hash_stable(hcx, hasher);
                target.hash_stable(hcx, hasher);
                cleanup.hash_stable(hcx, hasher);
            }
        }
    }
}

188 189
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::AssertMessage<'tcx> {
190
    fn hash_stable<W: StableHasherResult>(&self,
191
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::AssertMessage::BoundsCheck { ref len, ref index } => {
                len.hash_stable(hcx, hasher);
                index.hash_stable(hcx, hasher);
            }
            mir::AssertMessage::Math(ref const_math_err) => {
                const_math_err.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(struct mir::Statement<'tcx> { source_info, kind });

209 210
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::StatementKind<'tcx> {
211
    fn hash_stable<W: StableHasherResult>(&self,
212
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::StatementKind::Assign(ref lvalue, ref rvalue) => {
                lvalue.hash_stable(hcx, hasher);
                rvalue.hash_stable(hcx, hasher);
            }
            mir::StatementKind::SetDiscriminant { ref lvalue, variant_index } => {
                lvalue.hash_stable(hcx, hasher);
                variant_index.hash_stable(hcx, hasher);
            }
            mir::StatementKind::StorageLive(ref lvalue) |
            mir::StatementKind::StorageDead(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
229 230 231
            mir::StatementKind::EndRegion(ref extents) => {
                extents.hash_stable(hcx, hasher);
            }
232 233 234 235 236 237 238 239 240 241
            mir::StatementKind::Nop => {}
            mir::StatementKind::InlineAsm { ref asm, ref outputs, ref inputs } => {
                asm.hash_stable(hcx, hasher);
                outputs.hash_stable(hcx, hasher);
                inputs.hash_stable(hcx, hasher);
            }
        }
    }
}

242
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Lvalue<'tcx> {
243
    fn hash_stable<W: StableHasherResult>(&self,
244
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
            mir::Lvalue::Local(ref local) => {
                local.hash_stable(hcx, hasher);
            }
            mir::Lvalue::Static(ref statik) => {
                statik.hash_stable(hcx, hasher);
            }
            mir::Lvalue::Projection(ref lvalue_projection) => {
                lvalue_projection.hash_stable(hcx, hasher);
            }
        }
    }
}

261 262 263 264
impl<'a, 'gcx, 'tcx, B, V> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::Projection<'tcx, B, V>
    where B: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
          V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
265 266
{
    fn hash_stable<W: StableHasherResult>(&self,
267
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
268 269 270 271 272 273 274 275 276 277 278
                                          hasher: &mut StableHasher<W>) {
        let mir::Projection {
            ref base,
            ref elem,
        } = *self;

        base.hash_stable(hcx, hasher);
        elem.hash_stable(hcx, hasher);
    }
}

279 280 281
impl<'a, 'gcx, 'tcx, V> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::ProjectionElem<'tcx, V>
    where V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
282 283
{
    fn hash_stable<W: StableHasherResult>(&self,
284
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
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
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
            mir::ProjectionElem::Deref => {}
            mir::ProjectionElem::Field(field, ty) => {
                field.hash_stable(hcx, hasher);
                ty.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::Index(ref value) => {
                value.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
                offset.hash_stable(hcx, hasher);
                min_length.hash_stable(hcx, hasher);
                from_end.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::Subslice { from, to } => {
                from.hash_stable(hcx, hasher);
                to.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::Downcast(adt_def, variant) => {
                adt_def.hash_stable(hcx, hasher);
                variant.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(struct mir::VisibilityScopeData { span, parent_scope });

315
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Operand<'tcx> {
316
    fn hash_stable<W: StableHasherResult>(&self,
317
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
318 319 320 321 322 323 324 325 326 327 328 329 330 331
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::Operand::Consume(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
            mir::Operand::Constant(ref constant) => {
                constant.hash_stable(hcx, hasher);
            }
        }
    }
}

332
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Rvalue<'tcx> {
333
    fn hash_stable<W: StableHasherResult>(&self,
334
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
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
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::Rvalue::Use(ref operand) => {
                operand.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Repeat(ref operand, ref val) => {
                operand.hash_stable(hcx, hasher);
                val.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Ref(region, borrow_kind, ref lvalue) => {
                region.hash_stable(hcx, hasher);
                borrow_kind.hash_stable(hcx, hasher);
                lvalue.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Len(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Cast(cast_kind, ref operand, ty) => {
                cast_kind.hash_stable(hcx, hasher);
                operand.hash_stable(hcx, hasher);
                ty.hash_stable(hcx, hasher);
            }
            mir::Rvalue::BinaryOp(op, ref operand1, ref operand2) |
            mir::Rvalue::CheckedBinaryOp(op, ref operand1, ref operand2) => {
                op.hash_stable(hcx, hasher);
                operand1.hash_stable(hcx, hasher);
                operand2.hash_stable(hcx, hasher);
            }
            mir::Rvalue::UnaryOp(op, ref operand) => {
                op.hash_stable(hcx, hasher);
                operand.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Discriminant(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
372 373
            mir::Rvalue::NullaryOp(op, ty) => {
                op.hash_stable(hcx, hasher);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
                ty.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Aggregate(ref kind, ref operands) => {
                kind.hash_stable(hcx, hasher);
                operands.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(enum mir::CastKind {
    Misc,
    ReifyFnPointer,
    ClosureFnPointer,
    UnsafeFnPointer,
    Unsize
});

392 393
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for mir::AggregateKind<'tcx> {
394
    fn hash_stable<W: StableHasherResult>(&self,
395
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
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
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
            mir::AggregateKind::Tuple => {}
            mir::AggregateKind::Array(t) => {
                t.hash_stable(hcx, hasher);
            }
            mir::AggregateKind::Adt(adt_def, idx, substs, active_field) => {
                adt_def.hash_stable(hcx, hasher);
                idx.hash_stable(hcx, hasher);
                substs.hash_stable(hcx, hasher);
                active_field.hash_stable(hcx, hasher);
            }
            mir::AggregateKind::Closure(def_id, ref substs) => {
                def_id.hash_stable(hcx, hasher);
                substs.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(enum mir::BinOp {
    Add,
    Sub,
    Mul,
    Div,
    Rem,
    BitXor,
    BitAnd,
    BitOr,
    Shl,
    Shr,
    Eq,
    Lt,
    Le,
    Ne,
    Ge,
433 434
    Gt,
    Offset
435 436 437 438 439 440 441
});

impl_stable_hash_for!(enum mir::UnOp {
    Not,
    Neg
});

442 443 444 445
impl_stable_hash_for!(enum mir::NullOp {
    Box,
    SizeOf
});
446 447 448

impl_stable_hash_for!(struct mir::Constant<'tcx> { span, ty, literal });

449
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Literal<'tcx> {
450
    fn hash_stable<W: StableHasherResult>(&self,
451
                                          hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
            mir::Literal::Item { def_id, substs } => {
                def_id.hash_stable(hcx, hasher);
                substs.hash_stable(hcx, hasher);
            }
            mir::Literal::Value { ref value } => {
                value.hash_stable(hcx, hasher);
            }
            mir::Literal::Promoted { index } => {
                index.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(struct mir::Location { block, statement_index });