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
// Copyright (c) 2017 Fabian Schuiki

use crate::argument::*;
use crate::block::*;
use crate::inst::*;
use crate::value::{ArgumentRef, BlockRef, Context, InstRef};

/// A context wrapping a unit.
pub trait UnitContext: Context + AsUnitContext {
    /// Resolve a `InstRef` to an actual `&Inst` reference.
    fn inst(&self, inst: InstRef) -> &Inst;
    /// Resolve a `ArgumentRef` to an actual `&Argument` reference.
    fn argument(&self, argument: ArgumentRef) -> &Argument;
}

pub trait AsUnitContext {
    fn as_unit_context(&self) -> &UnitContext;
}

impl<T: UnitContext> AsUnitContext for T {
    fn as_unit_context(&self) -> &UnitContext {
        self
    }
}

/// A context wrapping a unit that uses basic blocks to group a sequence of
/// instructions.
pub trait SequentialContext: UnitContext {
    /// Resolve a `BlockRef` to an actual `&Block` reference.
    fn block(&self, block: BlockRef) -> &Block;
}