llvm_ir_analysis/
functions_by_type.rs

1use llvm_ir::{Module, TypeRef};
2use std::collections::{HashMap, HashSet};
3
4/// Allows you to iterate over all the functions in the analyzed `Module`(s) that
5/// have a specified type.
6///
7/// To construct a `FunctionsByType`, use [`ModuleAnalysis`](struct.ModuleAnalysis.html)
8/// or [`CrossModuleAnalysis`](struct.CrossModuleAnalysis.html).
9pub struct FunctionsByType<'m> {
10    map: HashMap<TypeRef, HashSet<&'m str>>,
11}
12
13impl<'m> FunctionsByType<'m> {
14    pub(crate) fn new(modules: impl IntoIterator<Item = &'m Module>) -> Self {
15        let mut map: HashMap<TypeRef, HashSet<&'m str>> = HashMap::new();
16        for module in modules {
17            for func in &module.functions {
18                map.entry(module.type_of(func))
19                    .or_default()
20                    .insert(&func.name);
21            }
22        }
23        Self { map }
24    }
25
26    /// Iterate over all of the functions in the analyzed `Module`(s) that have
27    /// the specified type
28    pub fn functions_with_type<'s>(&'s self, ty: &TypeRef) -> impl Iterator<Item = &'m str> + 's {
29        self.map
30            .get(ty)
31            .into_iter()
32            .map(|hs| hs.iter().copied())
33            .flatten()
34    }
35}