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