-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.rs
More file actions
144 lines (127 loc) · 3.67 KB
/
Copy pathtypes.rs
File metadata and controls
144 lines (127 loc) · 3.67 KB
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
//! Interned semantic types.
//!
//! A `TypeId` identifies a complete type. Nominal declarations retain their
//! separate `DefId`, so `Box[int]` and `Box[string]` share a definition while
//! remaining distinct semantic types.
use crate::ids::{DefId, TypeId};
use crate::intrinsics::IntrinsicType;
use crate::syntax::ast::BuiltinType;
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum TypeKind {
Error,
Never,
Builtin(BuiltinType),
Intrinsic(IntrinsicType),
GenericParameter(DefId),
Nominal {
definition: DefId,
arguments: Vec<TypeId>,
},
Const(TypeId),
Array {
element: TypeId,
dimensions: Vec<u64>,
},
Reference {
target: TypeId,
mutable: bool,
},
Pointer(TypeId),
Slice(TypeId),
Union(Vec<TypeId>),
Intersection(Vec<TypeId>),
Function {
parameters: Vec<TypeId>,
result: TypeId,
},
}
#[derive(Debug)]
pub struct TypeStore {
kinds: Vec<TypeKind>,
interned: HashMap<TypeKind, TypeId>,
}
impl Default for TypeStore {
fn default() -> Self {
let mut store = Self {
kinds: Vec::new(),
interned: HashMap::new(),
};
store.intern(TypeKind::Error);
store.intern(TypeKind::Never);
for builtin in [
BuiltinType::Void,
BuiltinType::Byte,
BuiltinType::Short,
BuiltinType::Int,
BuiltinType::Long,
BuiltinType::Float,
BuiltinType::Double,
BuiltinType::String,
BuiltinType::Boolean,
BuiltinType::Char,
BuiltinType::Allocator,
BuiltinType::Arena,
] {
store.intern(TypeKind::Builtin(builtin));
}
for intrinsic in IntrinsicType::ALL {
store.intern(TypeKind::Intrinsic(intrinsic));
}
store
}
}
impl TypeStore {
pub fn intern(&mut self, kind: TypeKind) -> TypeId {
if let Some(existing) = self.interned.get(&kind) {
return *existing;
}
let id = TypeId::new(self.kinds.len() as u32);
self.kinds.push(kind.clone());
self.interned.insert(kind, id);
id
}
pub fn kind(&self, id: TypeId) -> &TypeKind {
&self.kinds[id.index()]
}
pub fn error(&self) -> TypeId {
TypeId::new(0)
}
pub fn builtin(&mut self, builtin: BuiltinType) -> TypeId {
self.intern(TypeKind::Builtin(builtin))
}
pub fn intrinsic(&mut self, intrinsic: IntrinsicType) -> TypeId {
self.intern(TypeKind::Intrinsic(intrinsic))
}
pub fn function(&mut self, parameters: Vec<TypeId>, result: TypeId) -> TypeId {
self.intern(TypeKind::Function { parameters, result })
}
pub fn nominal(&mut self, definition: DefId, arguments: Vec<TypeId>) -> TypeId {
self.intern(TypeKind::Nominal {
definition,
arguments,
})
}
pub fn len(&self) -> usize {
self.kinds.len()
}
pub fn is_empty(&self) -> bool {
self.kinds.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interns_equal_types_once_and_separates_instantiations() {
let mut types = TypeStore::default();
let int = types.builtin(BuiltinType::Int);
let string = types.builtin(BuiltinType::String);
let definition = DefId::new(7);
let box_int_a = types.nominal(definition, vec![int]);
let box_int_b = types.nominal(definition, vec![int]);
let box_string = types.nominal(definition, vec![string]);
assert_eq!(box_int_a, box_int_b);
assert_ne!(box_int_a, box_string);
}
}