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