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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/// Location system for precise code positioning
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Location {
pub file_path: PathBuf,
pub span: Span,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Span.
pub struct Span {
pub start: BytePos,
pub end: BytePos,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Byte pos.
pub struct BytePos(pub u32);
impl std::hash::Hash for Location {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
// Content-addressed hashing for deterministic cache keys
self.file_path.hash(state);
self.span.start.0.hash(state);
// End position omitted for prefix matching scenarios
}
}
impl Location {
/// Creates a new location from a file path and byte positions.
///
/// # Parameters
///
/// * `file_path` - The path to the source file
/// * `start` - Starting byte position in the file
/// * `end` - Ending byte position in the file
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::{Location, BytePos, Span};
/// use std::path::PathBuf;
///
/// let location = Location::new(
/// PathBuf::from("src/main.rs"),
/// 100,
/// 150
/// );
///
/// assert_eq!(location.file_path, PathBuf::from("src/main.rs"));
/// assert_eq!(location.span.start.0, 100);
/// assert_eq!(location.span.end.0, 150);
/// assert_eq!(location.span.len(), 50);
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn new(file_path: PathBuf, start: u32, end: u32) -> Self {
Self {
file_path,
span: Span {
start: BytePos(start),
end: BytePos(end),
},
}
}
/// Checks if this location completely contains another location.
///
/// Two locations must be in the same file, and this location's span
/// must completely encompass the other location's span.
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::Location;
/// use std::path::PathBuf;
///
/// let file = PathBuf::from("test.rs");
/// let outer = Location::new(file.clone(), 0, 100);
/// let inner = Location::new(file.clone(), 10, 50);
/// let separate = Location::new(PathBuf::from("other.rs"), 0, 100);
///
/// assert!(outer.contains(&inner));
/// assert!(!inner.contains(&outer));
/// assert!(!outer.contains(&separate)); // Different files
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn contains(&self, other: &Location) -> bool {
self.file_path == other.file_path
&& self.span.start <= other.span.start
&& self.span.end >= other.span.end
}
/// Checks if this location overlaps with another location.
///
/// Two locations overlap if they are in the same file and their
/// byte ranges intersect (even partially).
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::{Location, BytePos, Span};
/// use std::path::PathBuf;
///
/// let file = PathBuf::from("test.rs");
/// let loc1 = Location::new(file.clone(), 0, 50);
/// let loc2 = Location::new(file.clone(), 25, 75); // Overlaps
/// let loc3 = Location::new(file.clone(), 100, 150); // No overlap
/// let loc4 = Location::new(PathBuf::from("other.rs"), 0, 100); // Different file
///
/// assert!(loc1.overlaps(&loc2));
/// assert!(loc2.overlaps(&loc1));
/// assert!(!loc1.overlaps(&loc3));
/// assert!(!loc1.overlaps(&loc4));
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn overlaps(&self, other: &Location) -> bool {
self.file_path == other.file_path
&& self.span.start < other.span.end
&& other.span.start < self.span.end
}
}
impl Span {
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// Create a new instance.
pub fn new(start: u32, end: u32) -> Self {
Self {
start: BytePos(start),
end: BytePos(end),
}
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// Return the number of elements.
pub fn len(&self) -> u32 {
self.end.0 - self.start.0
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// Check whether the collection is empty.
pub fn is_empty(&self) -> bool {
self.start.0 >= self.end.0
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// Check whether the item is contained.
pub fn contains(&self, pos: BytePos) -> bool {
self.start <= pos && pos < self.end
}
}
impl BytePos {
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// To usize.
pub fn to_usize(self) -> usize {
self.0 as usize
}
/// Creates a `BytePos` from a usize position
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::BytePos;
///
/// let pos = BytePos::from_usize(42);
/// assert_eq!(pos.to_usize(), 42);
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn from_usize(pos: usize) -> Self {
Self(pos as u32)
}
}
/// Qualified name for symbol resolution
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct QualifiedName {
pub module_path: Vec<String>,
pub name: String,
pub disambiguator: Option<u32>, // For overloaded names
}
impl QualifiedName {
/// Creates a new qualified name from module path and name components.
///
/// # Parameters
///
/// * `module_path` - Vector of module/namespace components
/// * `name` - The final name component (function, type, etc.)
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::QualifiedName;
///
/// let qname = QualifiedName::new(
/// vec!["std".to_string(), "collections".to_string()],
/// "HashMap".to_string()
/// );
///
/// assert_eq!(qname.module_path, vec!["std", "collections"]);
/// assert_eq!(qname.name, "HashMap");
/// assert!(qname.disambiguator.is_none());
/// assert_eq!(qname.to_qualified_string(), "std::collections::HashMap");
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new(module_path: Vec<String>, name: String) -> Self {
Self {
module_path,
name,
disambiguator: None,
}
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// With disambiguator.
pub fn with_disambiguator(mut self, disambiguator: u32) -> Self {
self.disambiguator = Some(disambiguator);
self
}
/// Creates a qualified name from a string representation.
///
/// Parses strings in the format "`module::submodule::Name`" where
/// "::" separates module components from the final name.
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::QualifiedName;
///
/// // Simple name without module path
/// let simple = QualifiedName::from_string("main").expect("internal error");
/// assert_eq!(simple.name, "main");
/// assert!(simple.module_path.is_empty());
///
/// // Fully qualified name
/// let qualified = QualifiedName::from_string("std::collections::HashMap").expect("internal error");
/// assert_eq!(qualified.module_path, vec!["std", "collections"]);
/// assert_eq!(qualified.name, "HashMap");
///
/// // Error case
/// assert!(QualifiedName::from_string("").is_err());
/// ```
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn from_string(qualified_str: &str) -> Result<Self, &'static str> {
if qualified_str.is_empty() {
return Err("Empty qualified name");
}
let parts: Vec<&str> = qualified_str.split("::").collect();
let name = (*parts.last().expect("internal error")).to_string();
if name.is_empty() {
return Err("Empty qualified name");
}
let module_path = parts[..parts.len() - 1]
.iter()
.map(|s| (*s).to_string())
.collect();
Ok(Self {
module_path,
name,
disambiguator: None,
})
}
/// Converts the qualified name back to its string representation.
///
/// Creates a string in the format "`module::submodule::Name`", with
/// optional disambiguator suffix "#N" for overloaded names.
///
/// # Examples
///
/// ```rust
/// use pmat::models::unified_ast::QualifiedName;
///
/// let qname = QualifiedName::new(
/// vec!["crate".to_string(), "module".to_string()],
/// "function".to_string()
/// );
/// assert_eq!(qname.to_qualified_string(), "crate::module::function");
///
/// // With disambiguator
/// let overloaded = qname.with_disambiguator(1);
/// assert_eq!(overloaded.to_qualified_string(), "crate::module::function#1");
///
/// // Simple name without modules
/// let simple = QualifiedName::new(vec![], "main".to_string());
/// assert_eq!(simple.to_qualified_string(), "main");
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn to_qualified_string(&self) -> String {
let mut result = self.module_path.join("::");
if !result.is_empty() {
result.push_str("::");
}
result.push_str(&self.name);
if let Some(disambiguator) = self.disambiguator {
result.push_str(&format!("#{disambiguator}"));
}
result
}
}
impl std::str::FromStr for QualifiedName {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_string(s)
}
}
impl std::fmt::Display for QualifiedName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_qualified_string())
}
}