leo_passes/common/
item_walkers.rs1use indexmap::IndexMap;
26use leo_ast::{Composite, Function, Library, Location, Program, Stub};
27use leo_span::Symbol;
28
29pub fn items_at_path<'a, V: Clone + 'a>(
39 map: &'a IndexMap<Location, V>,
40 program: Symbol,
41 path_prefix: &'a [Symbol],
42) -> impl Iterator<Item = (Symbol, V)> + 'a {
43 map.iter().filter_map(move |(loc, v)| {
44 loc.path
45 .split_last()
46 .filter(|(_, rest)| *rest == path_prefix && loc.program == program)
47 .map(|(last, _)| (*last, v.clone()))
48 })
49}
50
51pub fn program_functions(program: &Program) -> impl Iterator<Item = (Location, &Function)> {
54 let scopes = program.program_scopes.iter().flat_map(|(_, scope)| {
55 let prog = scope.program_id.as_symbol();
56 scope.functions.iter().map(move |(name, f)| (Location::new(prog, vec![*name]), f))
57 });
58 let modules = program.modules.iter().flat_map(module_functions);
59 scopes.chain(modules)
60}
61
62pub fn program_composites(program: &Program) -> impl Iterator<Item = (Location, &Composite)> {
65 let scopes = program.program_scopes.iter().flat_map(|(_, scope)| {
66 let prog = scope.program_id.as_symbol();
67 scope.composites.iter().map(move |(name, c)| (Location::new(prog, vec![*name]), c))
68 });
69 let modules = program.modules.iter().flat_map(module_composites);
70 scopes.chain(modules)
71}
72
73pub fn library_functions(library: &Library) -> impl Iterator<Item = (Location, &Function)> {
76 let name = library.name;
77 let top = library.functions.iter().map(move |(sym, f)| (Location::new(name, vec![*sym]), f));
78 let modules = library.modules.iter().flat_map(module_functions);
79 top.chain(modules)
80}
81
82pub fn library_composites(library: &Library) -> impl Iterator<Item = (Location, &Composite)> {
85 let name = library.name;
86 let top = library.structs.iter().map(move |(sym, c)| (Location::new(name, vec![*sym]), c));
87 let modules = library.modules.iter().flat_map(module_composites);
88 top.chain(modules)
89}
90
91pub fn stub_functions(stub: &Stub) -> Box<dyn Iterator<Item = (Location, &Function)> + '_> {
94 match stub {
95 Stub::FromLeo { program, .. } => Box::new(program_functions(program)),
96 Stub::FromLibrary { library, .. } => Box::new(library_functions(library)),
97 Stub::FromAleo { .. } => Box::new(std::iter::empty()),
98 }
99}
100
101pub fn stub_composites(stub: &Stub) -> Box<dyn Iterator<Item = (Location, &Composite)> + '_> {
104 match stub {
105 Stub::FromLeo { program, .. } => Box::new(program_composites(program)),
106 Stub::FromLibrary { library, .. } => Box::new(library_composites(library)),
107 Stub::FromAleo { .. } => Box::new(std::iter::empty()),
108 }
109}
110
111fn module_functions<'a>(
112 (path, m): (&'a Vec<Symbol>, &'a leo_ast::Module),
113) -> impl Iterator<Item = (Location, &'a Function)> {
114 m.functions.iter().map(move |(name, f)| {
115 let full: Vec<Symbol> = path.iter().copied().chain(std::iter::once(*name)).collect();
116 (Location::new(m.unit_name, full), f)
117 })
118}
119
120fn module_composites<'a>(
121 (path, m): (&'a Vec<Symbol>, &'a leo_ast::Module),
122) -> impl Iterator<Item = (Location, &'a Composite)> {
123 m.composites.iter().map(move |(name, c)| {
124 let full: Vec<Symbol> = path.iter().copied().chain(std::iter::once(*name)).collect();
125 (Location::new(m.unit_name, full), c)
126 })
127}