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
use crate::{error::*, CallPath, CompileResult, TypeInfo, TypedFunctionDeclaration};
use std::collections::HashMap;
type TraitName = CallPath;
type TraitMapInner = im::Vector<((TraitName, TypeInfo), TraitMethods)>;
type TraitMethods = im::HashMap<String, TypedFunctionDeclaration>;
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct TraitMap {
trait_map: TraitMapInner,
}
impl TraitMap {
pub(crate) fn insert(
&mut self,
trait_name: CallPath,
type_implementing_for: TypeInfo,
methods: Vec<TypedFunctionDeclaration>,
) -> CompileResult<()> {
let warnings = vec![];
let errors = vec![];
let mut methods_map = im::HashMap::new();
for method in methods.into_iter() {
let method_name = method.name.as_str().to_string();
methods_map.insert(method_name, method);
}
self.trait_map
.push_back(((trait_name, type_implementing_for), methods_map));
ok((), warnings, errors)
}
pub(crate) fn extend(&mut self, other: TraitMap) -> CompileResult<()> {
let mut warnings = vec![];
let mut errors = vec![];
for ((trait_name, type_implementing_for), methods) in other.trait_map.into_iter() {
check!(
self.insert(
trait_name,
type_implementing_for,
methods.values().cloned().collect()
),
(),
warnings,
errors
);
}
ok((), warnings, errors)
}
pub(crate) fn get_call_path_and_type_info(
&self,
r#type: TypeInfo,
) -> Vec<((CallPath, TypeInfo), Vec<TypedFunctionDeclaration>)> {
let mut ret = vec![];
for ((call_path, type_info), methods) in self.trait_map.iter() {
if type_info.clone() == r#type {
ret.push((
(call_path.clone(), type_info.clone()),
methods.values().cloned().collect(),
));
}
}
ret
}
pub(crate) fn get_methods_for_type(&self, r#type: TypeInfo) -> Vec<TypedFunctionDeclaration> {
let mut methods = vec![];
for ((_, type_info), l_methods) in self.trait_map.iter() {
if *type_info == r#type {
methods.append(&mut l_methods.values().cloned().collect());
}
}
methods
}
pub(crate) fn get_methods_for_type_by_trait(
&self,
r#type: TypeInfo,
) -> HashMap<TraitName, Vec<TypedFunctionDeclaration>> {
let mut methods: HashMap<TraitName, Vec<TypedFunctionDeclaration>> = HashMap::new();
for ((trait_name, type_info), trait_methods) in self.trait_map.iter() {
if *type_info == r#type {
methods.insert(
(*trait_name).clone(),
trait_methods.values().cloned().collect(),
);
}
}
methods
}
}