Skip to main content

mago_codex/ttype/
resolution.rs

1use mago_word::Word;
2use mago_word::WordMap;
3use mago_word::WordSet;
4
5use crate::ttype::template::GenericTemplate;
6
7/// Holds contextual information necessary for resolving generic template types (`@template`).
8///
9/// This context typically includes the definitions of template parameters available in the current scope
10/// (e.g., from class or function `@template` tags) and any concrete types that these templates
11/// have been resolved to (e.g., when a generic class is instantiated or a generic method is called).
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct TypeResolutionContext {
15    /// Definitions of template types available in this context, including their constraints.
16    template_definitions: WordMap<Vec<GenericTemplate>>,
17
18    /// Type aliases defined in the current class scope (from @type tags).
19    type_aliases: WordSet,
20
21    /// Imported type aliases (from @import-type tags).
22    /// Maps local alias name to (source class FQCN, original type name).
23    imported_type_aliases: WordMap<(Word, Word)>,
24}
25
26/// Provides a default, empty type resolution context.
27impl Default for TypeResolutionContext {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33impl TypeResolutionContext {
34    /// Creates a new, empty `TypeResolutionContext` with no defined or resolved template types.
35    #[must_use]
36    pub fn new() -> Self {
37        Self {
38            template_definitions: WordMap::default(),
39            type_aliases: WordSet::default(),
40            imported_type_aliases: WordMap::default(),
41        }
42    }
43
44    /// Checks if this context is empty, meaning it has no template definitions or resolved types.
45    #[inline]
46    #[must_use]
47    pub fn is_empty(&self) -> bool {
48        self.template_definitions.is_empty() && self.type_aliases.is_empty() && self.imported_type_aliases.is_empty()
49    }
50
51    /// Adds a template type definition (e.g., from an `@template T of Constraint` tag).
52    ///
53    /// # Arguments
54    ///
55    /// * `name`: The name of the template parameter (e.g., `"T"`).
56    /// * `constraints`: A list of constraints for the template parameter.
57    #[must_use]
58    pub fn with_template_definition(mut self, name: Word, constraints: Vec<GenericTemplate>) -> Self {
59        self.template_definitions.insert(name, constraints);
60        self
61    }
62
63    /// Returns a mutable reference to the template definitions map.
64    #[inline]
65    pub fn get_template_definitions_mut(&mut self) -> &mut WordMap<Vec<GenericTemplate>> {
66        &mut self.template_definitions
67    }
68
69    #[inline]
70    #[must_use]
71    pub fn has_template_definitions(&self) -> bool {
72        !self.template_definitions.is_empty()
73    }
74
75    #[inline]
76    #[must_use]
77    pub fn has_type_aliases(&self) -> bool {
78        !self.type_aliases.is_empty() || !self.imported_type_aliases.is_empty()
79    }
80
81    /// Looks up the constraints for a specific template parameter defined in this context.
82    ///
83    /// # Arguments
84    ///
85    /// * `name`: The name of the template parameter (e.g., `"T"`) to look up.
86    ///
87    /// # Returns
88    ///
89    /// `Some` containing a reference to the vector of constraints if the template is defined, `None` otherwise.
90    #[must_use]
91    pub fn get_template_definition(&self, name: Word) -> Option<&Vec<GenericTemplate>> {
92        self.template_definitions.get(&name)
93    }
94
95    /// Checks if a specific template parameter is defined in this context.
96    ///
97    /// # Arguments
98    ///
99    /// * `name`: The name of the template parameter (e.g., `"T"`) to check.
100    ///
101    /// # Returns
102    ///
103    /// `true` if the template parameter is defined, `false` otherwise.
104    #[must_use]
105    pub fn has_template_definition(&self, name: Word) -> bool {
106        self.template_definitions.contains_key(&name)
107    }
108
109    /// Adds type aliases from a class to this context.
110    ///
111    /// # Arguments
112    ///
113    /// * `aliases`: A set of type alias names.
114    #[must_use]
115    pub fn with_type_aliases(mut self, aliases: WordSet) -> Self {
116        self.type_aliases = aliases;
117        self
118    }
119
120    /// Adds a single type alias to this context.
121    ///
122    /// # Arguments
123    ///
124    /// * `name`: The name of the type alias to add.
125    #[must_use]
126    pub fn with_type_alias(mut self, name: Word) -> Self {
127        self.type_aliases.insert(name);
128        self
129    }
130
131    /// Checks if a specific type alias is defined in this context.
132    ///
133    /// # Arguments
134    ///
135    /// * `name`: The name of the type alias to check.
136    #[must_use]
137    pub fn has_type_alias(&self, name: Word) -> bool {
138        self.type_aliases.contains(&name)
139    }
140
141    /// Adds an imported type alias to this context.
142    ///
143    /// # Arguments
144    ///
145    /// * `local_name`: The local name of the imported alias (possibly renamed with "as").
146    /// * `source_class`: The FQCN of the class where the type alias is defined.
147    /// * `original_name`: The original name of the type alias in the source class.
148    #[must_use]
149    pub fn with_imported_type_alias(mut self, local_name: Word, source_class: Word, original_name: Word) -> Self {
150        self.imported_type_aliases.insert(local_name, (source_class, original_name));
151        self
152    }
153
154    /// Looks up an imported type alias in this context.
155    ///
156    /// # Arguments
157    ///
158    /// * `name`: The local name of the imported alias to look up.
159    ///
160    /// # Returns
161    ///
162    /// `Some` containing a reference to (`source_class`, `original_name`) if found, `None` otherwise.
163    #[must_use]
164    pub fn get_imported_type_alias(&self, name: Word) -> Option<&(Word, Word)> {
165        self.imported_type_aliases.get(&name)
166    }
167}