summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_const_eval/src/util/find_self_call.rs
blob: 33ad128eeeb7503847c1c63974e5ab491de1830e (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
use rustc_middle::mir::*;
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::def_id::DefId;

/// Checks if the specified `local` is used as the `self` parameter of a method call
/// in the provided `BasicBlock`. If it is, then the `DefId` of the called method is
/// returned.
pub fn find_self_call<'tcx>(
    tcx: TyCtxt<'tcx>,
    body: &Body<'tcx>,
    local: Local,
    block: BasicBlock,
) -> Option<(DefId, SubstsRef<'tcx>)> {
    debug!("find_self_call(local={:?}): terminator={:?}", local, &body[block].terminator);
    if let Some(Terminator { kind: TerminatorKind::Call { func, args, .. }, .. }) =
        &body[block].terminator
    {
        debug!("find_self_call: func={:?}", func);
        if let Operand::Constant(box Constant { literal, .. }) = func {
            if let ty::FnDef(def_id, substs) = *literal.ty().kind() {
                if let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) =
                    tcx.opt_associated_item(def_id)
                {
                    debug!("find_self_call: args={:?}", args);
                    if let [Operand::Move(self_place) | Operand::Copy(self_place), ..] = **args {
                        if self_place.as_local() == Some(local) {
                            return Some((def_id, substs));
                        }
                    }
                }
            }
        }
    }
    None
}