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
use std::marker::PhantomData;
use std::sync::Arc;

use crate::AllocId;
use crate::BlockArg;
use crate::BlockRef;
use crate::ContextRef;
use crate::ContextWRef;
use crate::OpRef;
use crate::Ty;
use crate::Type;

#[derive(Debug, Clone)]
pub enum ValueOwner {
    Op(AllocId),
    BlockArg(BlockArg),
}

#[derive(Debug, Clone)]
pub struct Value<T: Into<Type> + TryFrom<Type> + Clone = Type> {
    owner: ValueOwner,
    context: ContextWRef,
    name: Arc<String>,
    _a: PhantomData<T>,
}

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        let context = self.context.upgrade();
        let other_context = other.context.upgrade();

        if context.is_none() || other_context.is_none() {
            return false;
        }

        let context = context.unwrap();
        let other_context = other_context.unwrap();

        if Arc::as_ptr(&context) != Arc::as_ptr(&other_context) {
            return false;
        }

        match (&self.owner, &other.owner) {
            (ValueOwner::Op(id), ValueOwner::Op(other_id)) => id == other_id,
            (ValueOwner::BlockArg(arg), ValueOwner::BlockArg(other_arg)) => arg == other_arg,
            _ => false,
        }
    }
}

impl<T: Into<Type> + TryFrom<Type> + Clone> Value<T> {
    pub fn from_block_arg(context: ContextWRef, name: &str, arg: BlockArg) -> Self {
        Value {
            owner: ValueOwner::BlockArg(arg),
            context,
            name: Arc::new(name.to_string()),
            _a: PhantomData,
        }
    }

    pub fn from_op(context: ContextRef, name: &str, alloc_id: AllocId) -> Self {
        Value {
            owner: ValueOwner::Op(alloc_id),
            context: Arc::downgrade(&context),
            name: Arc::new(name.to_string()),
            _a: PhantomData,
        }
    }

    pub fn get_context(&self) -> ContextRef {
        self.context.upgrade().unwrap()
    }

    pub fn get_defining_op(&self) -> Option<OpRef> {
        match &self.owner {
            ValueOwner::Op(alloc_id) => {
                let context = self.context.upgrade().unwrap();
                context.get_op(*alloc_id)
            }
            _ => None,
        }
    }

    pub fn get_defining_block(&self) -> Option<BlockRef> {
        match &self.owner {
            ValueOwner::BlockArg(arg) => arg.get_block(),
            _ => None,
        }
    }

    pub fn get_type(&self) -> T {
        match &self.owner {
            ValueOwner::BlockArg(arg) => match arg.get_type().try_into() {
                Ok(res) => res,
                _ => unreachable!(),
            },
            ValueOwner::Op(alloc_id) => {
                let context = self.context.upgrade().unwrap();
                let op = context.get_op(*alloc_id).clone().unwrap();
                let ret_ty = op.borrow().get_return_type().unwrap();

                match ret_ty.try_into() {
                    Ok(res) => res,
                    _ => unreachable!(),
                }
            }
        }
    }

    pub fn get_name(&self) -> &str {
        &self.name
    }
}

impl Value<Type> {
    pub fn try_cast<Target: Ty + Clone + Into<Type> + TryFrom<Type>>(
        &self,
    ) -> Result<Value<Target>, ()> {
        let ty = self.get_type();

        if !ty.isa::<Target>() {
            return Err(());
        }

        Ok(Value {
            owner: self.owner.clone(),
            context: self.context.clone(),
            name: self.name.clone(),
            _a: PhantomData,
        })
    }
}

impl<T: Ty + Clone + Into<Type> + TryFrom<Type>> From<Value<T>> for Value {
    fn from(value: Value<T>) -> Self {
        Value {
            owner: value.owner,
            context: value.context,
            name: value.name,
            _a: PhantomData,
        }
    }
}