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
#![deny(missing_docs)]
use crate::{Type, Value, ValueId, ValueRef, INLINE_VALUE_ID};
use std::sync::Arc;
pub type Aggregate = Arc<AggregateKind>;
impl Into<ValueRef> for Aggregate {
fn into(self) -> ValueRef {
ValueRef::Aggregate(self)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AggregateKind {
Struct(StructAggregate),
Array(ArrayAggregate),
}
impl From<StructAggregate> for AggregateKind {
fn from(a: StructAggregate) -> AggregateKind {
AggregateKind::Struct(a)
}
}
impl From<ArrayAggregate> for AggregateKind {
fn from(a: ArrayAggregate) -> AggregateKind {
AggregateKind::Array(a)
}
}
impl Value for AggregateKind {
fn id(&self) -> ValueId {
INLINE_VALUE_ID
}
fn ty(&self) -> Type {
match *self {
AggregateKind::Struct(ref a) => a.ty().clone(),
AggregateKind::Array(ref a) => a.ty().clone(),
}
}
fn name(&self) -> Option<&str> {
None
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructAggregate {
ty: Type,
fields: Vec<ValueRef>,
}
impl StructAggregate {
pub fn new(ty: Type, fields: Vec<ValueRef>) -> StructAggregate {
assert!(ty.is_struct());
assert_eq!(ty.unwrap_struct().len(), fields.len());
StructAggregate { ty, fields }
}
pub fn ty(&self) -> &Type {
&self.ty
}
pub fn fields(&self) -> &[ValueRef] {
&self.fields
}
pub fn fields_mut(&mut self) -> &mut [ValueRef] {
&mut self.fields
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArrayAggregate {
ty: Type,
elements: Vec<ValueRef>,
}
impl ArrayAggregate {
pub fn new(ty: Type, elements: Vec<ValueRef>) -> ArrayAggregate {
assert!(ty.is_array());
assert_eq!(ty.unwrap_array().0, elements.len());
ArrayAggregate { ty, elements }
}
pub fn ty(&self) -> &Type {
&self.ty
}
pub fn elements(&self) -> &[ValueRef] {
&self.elements
}
pub fn fields_mut(&mut self) -> &mut [ValueRef] {
&mut self.elements
}
}