rapx/analysis/core/alias_analysis/default/
value.rs

1use crate::analysis::core::alias_analysis::default::types::ValueKind;
2use rustc_data_structures::fx::FxHashMap;
3
4/// Represents a value node in the alias analysis graph.
5/// Each value may correspond to local variables, or fields of structures temporarily crated in the alias analysis
6#[derive(Debug, Clone)]
7pub struct Value {
8    /// A unique value index, which is the same as `local` if the number within the range of local
9    /// variables; When encountering fields (e.g., 1.1), the index points to the unique field of the local.
10    pub index: usize,
11    /// Real local variable identifier in mir.
12    pub local: usize,
13    /// Indicates whether this value needs to be dropped at the end of its lifetime.
14    pub need_drop: bool,
15    /// Indicates whether this value may need to be dropped (uncertain drop semantics).
16    pub may_drop: bool,
17    /// The type of this value node (see `ValueKind`).
18    pub kind: ValueKind,
19    /// Information about this value’s parent, if it is a field of a parent node.
20    pub father: Option<FatherInfo>,
21    /// Mapping from field IDs to their value node IDs for field accesses.
22    /// First field: field id; Second field: value id;
23    pub fields: FxHashMap<usize, usize>,
24}
25
26/// Represents the relation between a field and its parent (father).
27#[derive(Debug, Clone, PartialEq)]
28pub struct FatherInfo {
29    /// The value ID of the parent node.
30    pub father_value_id: usize,
31    /// The ID of the field within the parent.
32    pub field_id: usize,
33}
34
35impl FatherInfo {
36    /// Construct a new `FatherInfo` with the specified father value ID and field ID.
37    pub fn new(father_value_id: usize, field_id: usize) -> Self {
38        FatherInfo {
39            father_value_id,
40            field_id,
41        }
42    }
43}
44
45impl Value {
46    /// Create a new value node with the provided properties.
47    /// The `kind` defaults to `ValueKind::Adt` and `father` defaults to `None`.
48    pub fn new(index: usize, local: usize, need_drop: bool, may_drop: bool) -> Self {
49        Value {
50            index,
51            local,
52            need_drop,
53            may_drop,
54            kind: ValueKind::Adt,
55            father: None,
56            fields: FxHashMap::default(),
57        }
58    }
59
60    /// Returns whether this value is a tuple type.
61    pub fn is_tuple(&self) -> bool {
62        self.kind == ValueKind::Tuple
63    }
64
65    /// Returns whether this value is a pointer (raw pointer or reference).
66    pub fn is_ptr(&self) -> bool {
67        self.kind == ValueKind::RawPtr || self.kind == ValueKind::Ref
68    }
69
70    /// Returns whether this value is a reference type.
71    pub fn is_ref(&self) -> bool {
72        self.kind == ValueKind::Ref
73    }
74
75    /// Returns whether this value is of a corner case type as defined in `ValueKind`.
76    pub fn is_ref_count(&self) -> bool {
77        self.kind == ValueKind::SpecialPtr
78    }
79}