Skip to main content

mago_codex/
signature.rs

1use mago_word::Word;
2
3/// Represents a signature node for a definition (function, class, method, constant, etc.).
4///
5/// This structure forms a hierarchical tree where top-level symbols (classes, functions)
6/// can have children (methods, properties within classes).
7///
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct DefSignatureNode {
11    /// The name of the symbol (e.g., "Foo" for class Foo, "bar" for method bar)
12    pub name: Word,
13
14    /// Whether this node represents a function or method
15    pub is_function: bool,
16
17    /// Nested symbols (e.g., methods and properties within a class)
18    pub children: Vec<DefSignatureNode>,
19
20    /// Position-insensitive fingerprint hash covering the entire definition.
21    /// Any change to signature, body, modifiers, or attributes will change this hash.
22    pub hash: u64,
23
24    /// Signature-only fingerprint hash, excluding function/method bodies.
25    /// Used by the differ to determine cascade invalidation: if only the body changed
26    /// (signature_hash unchanged), dependents are not invalidated — only the changed
27    /// file itself is re-analyzed.
28    pub signature_hash: u64,
29}
30
31impl DefSignatureNode {
32    /// Creates a new `DefSignatureNode` with the given parameters.
33    #[inline]
34    #[must_use]
35    pub fn new(name: Word, is_function: bool, hash: u64, signature_hash: u64) -> Self {
36        Self { name, is_function, children: Vec::new(), hash, signature_hash }
37    }
38}
39
40/// Represents the signature of an entire file.
41///
42/// This contains all top-level definitions (classes, interfaces, traits, enums,
43/// functions, constants) in the file as a flat vector. Nested definitions
44/// (methods, properties) are stored within the `children` of their parent nodes.
45#[derive(Debug, Clone, PartialEq, Eq, Default)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47pub struct FileSignature {
48    pub hash: u64,
49    pub ast_nodes: Vec<DefSignatureNode>,
50}