-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathimpls.rs
57 lines (52 loc) · 1.78 KB
/
impls.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
use indexmap::map::Entry;
use indexmap::IndexMap;
use smol_str::SmolStr;
use crate::context::{Analysis, AnalyzerContext};
use crate::namespace::items::{Function, FunctionId, ImplId};
use crate::namespace::scopes::ItemScope;
use crate::namespace::types::TypeDowncast;
use crate::AnalyzerDb;
use std::rc::Rc;
pub fn impl_all_functions(db: &dyn AnalyzerDb, impl_: ImplId) -> Rc<[FunctionId]> {
let impl_data = impl_.data(db);
impl_data
.ast
.kind
.functions
.iter()
.map(|node| {
db.intern_function(Rc::new(Function::new(
db,
node,
// Not sure if setting the receiver as parent is the right thing to do. We currently do this
// so that the generated name of the YUL function will take in the receiver and avoids name collisions.
impl_.receiver(db).as_class(),
impl_data.module,
)))
})
.collect()
}
pub fn impl_function_map(
db: &dyn AnalyzerDb,
impl_: ImplId,
) -> Analysis<Rc<IndexMap<SmolStr, FunctionId>>> {
let scope = ItemScope::new(db, impl_.module(db));
let mut map = IndexMap::<SmolStr, FunctionId>::new();
for func in db.impl_all_functions(impl_).iter() {
let def_name = func.name(db);
match map.entry(def_name) {
Entry::Occupied(entry) => {
scope.duplicate_name_error(
"duplicate function names in `impl` block",
entry.key(),
entry.get().name_span(db),
func.name_span(db),
);
}
Entry::Vacant(entry) => {
entry.insert(*func);
}
}
}
Analysis::new(Rc::new(map), scope.diagnostics.take().into())
}