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

use crate::argument::*;
use crate::block::*;
use crate::inst::*;
use crate::module::ModuleContext;
use crate::seq_body::*;
use crate::ty::*;
use crate::unit::*;
use crate::value::*;

/// A function. Sequentially executes instructions to determine a result value
/// from its inputs. Implements *control flow* and *immediate execution*.
pub struct Function {
    id: FunctionRef,
    global: bool,
    name: String,
    ty: Type,
    args: Vec<Argument>,
    body: SeqBody,
}

impl Function {
    /// Create a new function with the given name and type signature. Anonymous
    /// arguments are created for each argument in the type signature. Use the
    /// `args_mut` function to get a hold of these arguments and assign names
    /// and additional data to them.
    pub fn new(name: impl Into<String>, ty: Type) -> Function {
        let args = {
            let (arg_tys, _) = ty.unwrap_func();
            arg_tys.iter().map(|t| Argument::new(t.clone())).collect()
        };
        Function {
            id: FunctionRef::new(ValueId::alloc()),
            global: true,
            name: name.into(),
            ty: ty,
            args: args,
            body: SeqBody::new(),
        }
    }

    /// Obtain a reference to this function.
    pub fn as_ref(&self) -> FunctionRef {
        self.id
    }

    /// Get the name of the function.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the return type of the function.
    pub fn return_ty(&self) -> &Type {
        self.ty.unwrap_func().1
    }

    /// Get a graph reference to one of the arguments of the function.
    pub fn arg(&self, idx: usize) -> ArgumentRef {
        self.args[idx].as_ref()
    }

    /// Get a reference to the arguments of the function.
    pub fn args(&self) -> &[Argument] {
        &self.args
    }

    /// Get a mutable reference to the arguments of the function.
    pub fn args_mut(&mut self) -> &mut [Argument] {
        &mut self.args
    }

    /// Get a reference to the sequential body of the function.
    pub fn body(&self) -> &SeqBody {
        &self.body
    }

    /// Get a mutable reference to the sequential body of the function.
    pub fn body_mut(&mut self) -> &mut SeqBody {
        &mut self.body
    }
}

impl Value for Function {
    fn id(&self) -> ValueId {
        self.id.into()
    }

    fn ty(&self) -> Type {
        self.ty.clone()
    }

    fn name(&self) -> Option<&str> {
        Some(&self.name)
    }

    fn is_global(&self) -> bool {
        self.global
    }
}

pub struct FunctionContext<'tctx> {
    module: &'tctx ModuleContext<'tctx>,
    function: &'tctx Function,
}

impl<'tctx> FunctionContext<'tctx> {
    pub fn new(module: &'tctx ModuleContext, function: &'tctx Function) -> FunctionContext<'tctx> {
        FunctionContext {
            module: module,
            function: function,
        }
    }
}

impl<'tctx> Context for FunctionContext<'tctx> {
    fn parent(&self) -> Option<&Context> {
        Some(self.module.as_context())
    }

    fn try_value(&self, value: &ValueRef) -> Option<&Value> {
        match *value {
            ValueRef::Inst(id) => Some(self.inst(id)),
            ValueRef::Block(id) => Some(self.block(id)),
            ValueRef::Argument(id) => Some(self.argument(id)),
            _ => None,
        }
    }
}

impl<'tctx> UnitContext for FunctionContext<'tctx> {
    fn inst(&self, inst: InstRef) -> &Inst {
        self.function.body.inst(inst)
    }

    fn argument(&self, argument: ArgumentRef) -> &Argument {
        self.function
            .args
            .iter()
            .find(|x| argument == x.as_ref())
            .unwrap()
    }
}

impl<'tctx> SequentialContext for FunctionContext<'tctx> {
    fn block(&self, block: BlockRef) -> &Block {
        self.function.body.block(block)
    }
}