-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
mapping.rs
169 lines (156 loc) · 6.25 KB
/
mapping.rs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::{collections::HashSet, fmt};
use sway_error::handler::{ErrorEmitted, Handler};
use crate::{
engine_threading::DebugWithEngines,
language::ty::{TyTraitInterfaceItem, TyTraitItem},
Engines, TypeId, UnifyCheck,
};
use super::{AssociatedItemDeclId, InterfaceItemMap, ItemMap};
type SourceDecl = (AssociatedItemDeclId, TypeId);
type DestinationDecl = AssociatedItemDeclId;
/// The [DeclMapping] is used to create a mapping between a [SourceDecl] (LHS)
/// and a [DestinationDecl] (RHS).
#[derive(Clone)]
pub struct DeclMapping {
mapping: Vec<(SourceDecl, DestinationDecl)>,
}
impl fmt::Display for DeclMapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"DeclMapping {{ {} }}",
self.mapping
.iter()
.map(|(source_type, dest_type)| {
format!(
"{} -> {}",
source_type.0,
match dest_type {
AssociatedItemDeclId::TraitFn(decl_id) => decl_id.inner(),
AssociatedItemDeclId::Function(decl_id) => decl_id.inner(),
AssociatedItemDeclId::Constant(decl_id) => decl_id.inner(),
AssociatedItemDeclId::Type(decl_id) => decl_id.inner(),
}
)
})
.collect::<Vec<_>>()
.join(", ")
)
}
}
impl fmt::Debug for DeclMapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"DeclMapping {{ {} }}",
self.mapping
.iter()
.map(|(source_type, dest_type)| { format!("{source_type:?} -> {dest_type:?}") })
.collect::<Vec<_>>()
.join(", ")
)
}
}
impl DebugWithEngines for DeclMapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
write!(
f,
"DeclMapping {{ {} }}",
self.mapping
.iter()
.map(|(source_type, dest_type)| {
format!(
"{} -> {}",
engines.help_out(source_type.0.clone()),
engines.help_out(dest_type)
)
})
.collect::<Vec<_>>()
.join(", ")
)
}
}
impl DeclMapping {
pub(crate) fn is_empty(&self) -> bool {
self.mapping.is_empty()
}
pub(crate) fn from_interface_and_item_and_impld_decl_refs(
interface_decl_refs: InterfaceItemMap,
item_decl_refs: ItemMap,
impld_decl_refs: ItemMap,
) -> DeclMapping {
let mut mapping: Vec<(SourceDecl, DestinationDecl)> = vec![];
for (interface_decl_name, interface_item) in interface_decl_refs.into_iter() {
if let Some(new_item) = impld_decl_refs.get(&interface_decl_name) {
let interface_decl_ref = match interface_item {
TyTraitInterfaceItem::TraitFn(decl_ref) => {
(decl_ref.id().into(), interface_decl_name.1)
}
TyTraitInterfaceItem::Constant(decl_ref) => {
(decl_ref.id().into(), interface_decl_name.1)
}
TyTraitInterfaceItem::Type(decl_ref) => {
(decl_ref.id().into(), interface_decl_name.1)
}
};
let new_decl_ref = match new_item {
TyTraitItem::Fn(decl_ref) => decl_ref.id().into(),
TyTraitItem::Constant(decl_ref) => decl_ref.id().into(),
TyTraitItem::Type(decl_ref) => decl_ref.id().into(),
};
mapping.push((interface_decl_ref, new_decl_ref));
}
}
for (decl_name, item) in item_decl_refs.into_iter() {
if let Some(new_item) = impld_decl_refs.get(&decl_name) {
let interface_decl_ref = match item {
TyTraitItem::Fn(decl_ref) => (decl_ref.id().into(), decl_name.1),
TyTraitItem::Constant(decl_ref) => (decl_ref.id().into(), decl_name.1),
TyTraitItem::Type(decl_ref) => (decl_ref.id().into(), decl_name.1),
};
let new_decl_ref = match new_item {
TyTraitItem::Fn(decl_ref) => decl_ref.id().into(),
TyTraitItem::Constant(decl_ref) => decl_ref.id().into(),
TyTraitItem::Type(decl_ref) => decl_ref.id().into(),
};
mapping.push((interface_decl_ref, new_decl_ref));
}
}
DeclMapping { mapping }
}
pub(crate) fn find_match(
&self,
_handler: &Handler,
engines: &Engines,
decl_ref: AssociatedItemDeclId,
typeid: Option<TypeId>,
self_typeid: Option<TypeId>,
) -> Result<Option<DestinationDecl>, ErrorEmitted> {
let mut dest_decl_refs = HashSet::<DestinationDecl>::new();
if let Some(mut typeid) = typeid {
if engines.te().get(typeid).is_self_type() && self_typeid.is_some() {
// If typeid is `Self`, then we use the self_typeid instead.
typeid = self_typeid.unwrap();
}
for (source_decl_ref, dest_decl_ref) in self.mapping.iter() {
let unify_check = UnifyCheck::non_dynamic_equality(engines);
if source_decl_ref.0 == decl_ref && unify_check.check(source_decl_ref.1, typeid) {
dest_decl_refs.insert(dest_decl_ref.clone());
}
}
}
// At most one replacement should be found for decl_ref.
/* TODO uncomment this and close issue #5540
if dest_decl_refs.len() > 1 {
handler.emit_err(CompileError::InternalOwned(
format!(
"Multiple replacements for decl {} implemented in {}",
engines.help_out(decl_ref),
engines.help_out(typeid),
),
dest_decl_refs.iter().last().unwrap().span(engines),
));
}*/
Ok(dest_decl_refs.iter().next().cloned())
}
}