charon_lib/ast/
names_utils.rs

1//! Defines some utilities for [crate::names]
2//!
3//! For now, we have one function per object kind (type, trait, function,
4//! module): many of them could be factorized (will do).
5use crate::ast::*;
6
7impl PathElem {
8    fn equals_ident(&self, id: &str) -> bool {
9        match self {
10            PathElem::Ident(s, d) => s == id && d.is_zero(),
11            _ => false,
12        }
13    }
14
15    pub fn as_monomorphized(&self) -> Option<&GenericArgs> {
16        let binder = self.as_instantiated()?;
17        binder.params.is_empty().then_some(&binder.skip_binder)
18    }
19    pub fn is_monomorphized(&self) -> bool {
20        self.as_monomorphized().is_some()
21    }
22}
23
24impl Name {
25    /// Convert a path like `["std", "alloc", "Box"]` to a name. Needed on occasion when crafting
26    /// names that were not present in the original code.
27    pub fn from_path(path: &[&str]) -> Name {
28        Name {
29            name: path
30                .iter()
31                .map(|elem| PathElem::Ident(elem.to_string(), Disambiguator::ZERO))
32                .collect(),
33        }
34    }
35
36    #[allow(clippy::len_without_is_empty)]
37    pub fn len(&self) -> usize {
38        self.name.len()
39    }
40
41    /// If this item comes from monomorphization, return the arguments used.
42    pub fn mono_args(&self) -> Option<&GenericArgs> {
43        Some(self.name.last()?.as_monomorphized()?)
44    }
45
46    /// Compare the name to a constant array.
47    /// This ignores disambiguators.
48    ///
49    /// `equal`: if `true`, check that the name is equal to the ref. If `false`:
50    /// only check if the ref is a prefix of the name.
51    pub fn compare_with_ref_name(&self, equal: bool, ref_name: &[&str]) -> bool {
52        let name: Vec<&PathElem> = self.name.iter().filter(|e| e.is_ident()).collect();
53
54        if name.len() < ref_name.len() || (equal && name.len() != ref_name.len()) {
55            return false;
56        }
57
58        for i in 0..ref_name.len() {
59            if !name[i].equals_ident(ref_name[i]) {
60                return false;
61            }
62        }
63        true
64    }
65
66    /// Compare the name to a constant array.
67    /// This ignores disambiguators.
68    pub fn equals_ref_name(&self, ref_name: &[&str]) -> bool {
69        self.compare_with_ref_name(true, ref_name)
70    }
71
72    /// Created an instantiated version of this name by putting a `PathElem::Instantiated` last. If
73    /// the item was already instantiated, this merges the two instantiations.
74    pub fn instantiate(mut self, binder: Binder<GenericArgs>) -> Self {
75        if let [.., PathElem::Instantiated(box x)] = self.name.as_mut_slice() {
76            // Put the new args in place; the params are what we want but the args are wrong.
77            let old_args = std::mem::replace(x, binder);
78            // Apply the new args to the old binder to get correct args.
79            x.skip_binder = old_args.apply(&x.skip_binder);
80        } else {
81            self.name.push(PathElem::Instantiated(Box::new(binder)));
82        }
83        self
84    }
85}