charon_lib/ast/
names.rs

1//! Defines some utilities for the variables
2use crate::ast::*;
3use derive_generic_visitor::{Drive, DriveMut};
4use macros::{EnumAsGetters, EnumIsA};
5use serde::{Deserialize, Serialize};
6
7generate_index_type!(Disambiguator);
8
9/// See the comments for [Name]
10#[derive(
11    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Drive, DriveMut, EnumIsA, EnumAsGetters,
12)]
13#[charon::variants_prefix("Pe")]
14pub enum PathElem {
15    Ident(#[drive(skip)] String, Disambiguator),
16    Impl(ImplElem, Disambiguator),
17    /// This item was obtained by monomorphizing its parent with the given args.
18    Monomorphized(Box<GenericArgs>),
19}
20
21/// There are two kinds of `impl` blocks:
22/// - impl blocks linked to a type ("inherent" impl blocks following Rust terminology):
23///   ```text
24///   impl<T> List<T> { ...}
25///   ```
26/// - trait impl blocks:
27///   ```text
28///   impl<T> PartialEq for List<T> { ...}
29///   ```
30/// We distinguish the two.
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Drive, DriveMut)]
32#[charon::variants_prefix("ImplElem")]
33pub enum ImplElem {
34    Ty(Binder<Ty>),
35    Trait(TraitImplId),
36}
37
38/// An item name/path
39///
40/// A name really is a list of strings. However, we sometimes need to
41/// introduce unique indices to disambiguate. This mostly happens because
42/// of "impl" blocks:
43///   ```text
44///   impl<T> List<T> {
45///     ...
46///   }
47///   ```
48///
49/// A type in Rust can have several "impl" blocks, and  those blocks can
50/// contain items with similar names. For this reason, we need to disambiguate
51/// them with unique indices. Rustc calls those "disambiguators". In rustc, this
52/// gives names like this:
53/// - `betree_main::betree::NodeIdCounter{impl#0}::new`
54/// - note that impl blocks can be nested, and macros sometimes generate
55///   weird names (which require disambiguation):
56///   `betree_main::betree_utils::_#1::{impl#0}::deserialize::{impl#0}`
57///
58/// Finally, the paths used by rustc are a lot more precise and explicit than
59/// those we expose in LLBC: for instance, every identifier belongs to a specific
60/// namespace (value namespace, type namespace, etc.), and is coupled with a
61/// disambiguator.
62///
63/// On our side, we want to stay high-level and simple: we use string identifiers
64/// as much as possible, insert disambiguators only when necessary (whenever
65/// we find an "impl" block, typically) and check that the disambiguator is useless
66/// in the other situations (i.e., the disambiguator is always equal to 0).
67///
68/// Moreover, the items are uniquely disambiguated by their (integer) ids
69/// (`TypeDeclId`, etc.), and when extracting the code we have to deal with
70/// name clashes anyway. Still, we might want to be more precise in the future.
71///
72/// Also note that the first path element in the name is always the crate name.
73#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Drive, DriveMut)]
74#[serde(transparent)]
75pub struct Name {
76    pub name: Vec<PathElem>,
77}