codanna 0.9.19

Code Intelligence for Large Language Models
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Kotlin-specific resolution context and inheritance resolver
//!
//! Provides scoping and inheritance tracking tailored for Kotlin's language features,
//! including package-based modules, nested classes, companion objects, and interfaces.

use crate::parsing::resolution::{ImportBinding, InheritanceResolver, ResolutionScope};
use crate::parsing::{ScopeLevel, ScopeType};
use crate::{FileId, SymbolId};
use std::collections::{HashMap, HashSet};

/// Resolution context implementing Kotlin scoping rules
pub struct KotlinResolutionContext {
    #[allow(dead_code)]
    file_id: FileId,
    /// Stack of local scopes (functions/blocks/lambdas)
    local_scopes: Vec<HashMap<String, SymbolId>>,
    /// Stack of class member scopes (for nested classes, innermost last)
    class_scopes: Vec<HashMap<String, SymbolId>>,
    /// Companion object scopes (parallel to class_scopes)
    companion_scopes: Vec<HashMap<String, SymbolId>>,
    /// File (top-level) scope
    module_scope: HashMap<String, SymbolId>,
    /// Package-level scope (imported symbols)
    import_scope: HashMap<String, SymbolId>,
    /// Global scope (stdlib, etc.)
    global_scope: HashMap<String, SymbolId>,
    /// Active scope stack for contextual decisions
    scope_stack: Vec<ScopeType>,
    /// Registered import bindings available to the file
    import_bindings: HashMap<String, ImportBinding>,
    /// Expression-to-type mappings for receiver inference
    expression_types: HashMap<String, String>,
}

impl KotlinResolutionContext {
    /// Create a new resolution context for a file
    pub fn new(file_id: FileId) -> Self {
        Self {
            file_id,
            local_scopes: Vec::new(),
            class_scopes: Vec::new(),
            companion_scopes: Vec::new(),
            module_scope: HashMap::new(),
            import_scope: HashMap::new(),
            global_scope: HashMap::new(),
            scope_stack: vec![ScopeType::Global],
            import_bindings: HashMap::new(),
            expression_types: HashMap::new(),
        }
    }

    /// Inject expression type mappings produced by the parser
    pub fn set_expression_types(&mut self, entries: HashMap<String, String>) {
        tracing::debug!(
            "[kotlin] loaded {} expression types into resolution context for file {:?}",
            entries.len(),
            self.file_id
        );
        self.expression_types = entries;
    }

    fn current_local_scope_mut(&mut self) -> &mut HashMap<String, SymbolId> {
        if self.local_scopes.is_empty() {
            self.local_scopes.push(HashMap::new());
        }
        self.local_scopes.last_mut().unwrap()
    }

    fn current_class_scope_mut(&mut self) -> Option<&mut HashMap<String, SymbolId>> {
        self.class_scopes.last_mut()
    }

    fn resolve_in_locals(&self, name: &str) -> Option<SymbolId> {
        for scope in self.local_scopes.iter().rev() {
            if let Some(&id) = scope.get(name) {
                return Some(id);
            }
        }
        None
    }

    fn resolve_in_classes(&self, name: &str) -> Option<SymbolId> {
        for scope in self.class_scopes.iter().rev() {
            if let Some(&id) = scope.get(name) {
                return Some(id);
            }
        }
        None
    }

    fn resolve_in_companions(&self, name: &str) -> Option<SymbolId> {
        for scope in self.companion_scopes.iter().rev() {
            if let Some(&id) = scope.get(name) {
                return Some(id);
            }
        }
        None
    }
}

impl ResolutionScope for KotlinResolutionContext {
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn add_symbol(&mut self, name: String, symbol_id: SymbolId, scope_level: ScopeLevel) {
        match scope_level {
            ScopeLevel::Local => {
                self.current_local_scope_mut().insert(name, symbol_id);
            }
            ScopeLevel::Module => {
                // If we're inside a class, treat as class member; otherwise file-level
                if matches!(self.scope_stack.last(), Some(ScopeType::Class)) {
                    if let Some(scope) = self.current_class_scope_mut() {
                        scope.insert(name.clone(), symbol_id);
                    }
                }
                self.module_scope.entry(name).or_insert(symbol_id);
            }
            ScopeLevel::Package => {
                self.import_scope.insert(name, symbol_id);
            }
            ScopeLevel::Global => {
                self.global_scope.insert(name.clone(), symbol_id);
                self.module_scope.entry(name).or_insert(symbol_id);
            }
        }
    }

    fn resolve(&self, name: &str) -> Option<SymbolId> {
        // Kotlin resolution order:
        // 1. Local scopes (innermost first)
        if let Some(id) = self.resolve_in_locals(name) {
            return Some(id);
        }

        // 2. Class members (from innermost class outward)
        if let Some(id) = self.resolve_in_classes(name) {
            return Some(id);
        }

        // 3. Companion object members
        if let Some(id) = self.resolve_in_companions(name) {
            return Some(id);
        }

        // 4. File-level definitions (top-level functions, properties)
        if let Some(&id) = self.module_scope.get(name) {
            return Some(id);
        }

        // 5. Imported symbols
        if let Some(&id) = self.import_scope.get(name) {
            return Some(id);
        }

        // 6. Global/stdlib
        if let Some(&id) = self.global_scope.get(name) {
            return Some(id);
        }

        // Handle qualified names like "MyClass.companion" or "Outer.Inner"
        if let Some((head, tail)) = name.split_once('.') {
            // Try to resolve the head first
            if let Some(class_id) = self.resolve(head) {
                // If head resolves, try to find the tail in class scope
                // This handles nested classes, companion objects, etc.
                if let Some(id) = self.resolve_in_classes(tail) {
                    return Some(id);
                }
                return Some(class_id);
            }
        }

        None
    }

    fn clear_local_scope(&mut self) {
        if let Some(scope) = self.local_scopes.last_mut() {
            scope.clear();
        }
    }

    fn enter_scope(&mut self, scope_type: ScopeType) {
        match scope_type {
            ScopeType::Function { .. } | ScopeType::Block => {
                self.local_scopes.push(HashMap::new());
            }
            ScopeType::Class => {
                self.class_scopes.push(HashMap::new());
                self.companion_scopes.push(HashMap::new());
            }
            _ => {}
        }
        self.scope_stack.push(scope_type);
    }

    fn exit_scope(&mut self) {
        if let Some(scope) = self.scope_stack.pop() {
            match scope {
                ScopeType::Function { .. } | ScopeType::Block => {
                    self.local_scopes.pop();
                }
                ScopeType::Class => {
                    self.class_scopes.pop();
                    self.companion_scopes.pop();
                }
                _ => {}
            }
        }
    }

    fn symbols_in_scope(&self) -> Vec<(String, SymbolId, ScopeLevel)> {
        let mut results = Vec::new();

        // Local scope
        if let Some(local) = self.local_scopes.last() {
            for (name, &id) in local {
                results.push((name.clone(), id, ScopeLevel::Local));
            }
        }

        // Class scope
        if let Some(class_scope) = self.class_scopes.last() {
            for (name, &id) in class_scope {
                results.push((name.clone(), id, ScopeLevel::Module));
            }
        }

        // Companion scope
        if let Some(companion_scope) = self.companion_scopes.last() {
            for (name, &id) in companion_scope {
                results.push((name.clone(), id, ScopeLevel::Module));
            }
        }

        // Module scope
        for (name, &id) in &self.module_scope {
            results.push((name.clone(), id, ScopeLevel::Module));
        }

        // Import scope
        for (name, &id) in &self.import_scope {
            results.push((name.clone(), id, ScopeLevel::Package));
        }

        // Global scope
        for (name, &id) in &self.global_scope {
            results.push((name.clone(), id, ScopeLevel::Global));
        }

        results
    }

    fn resolve_relationship(
        &self,
        _from_name: &str,
        to_name: &str,
        _kind: crate::RelationKind,
        _from_file: FileId,
    ) -> Option<SymbolId> {
        self.resolve(to_name)
    }

    fn populate_imports(&mut self, _imports: &[crate::parsing::Import]) {
        // Kotlin imports are resolved through the behavior's import matching
    }

    fn register_import_binding(&mut self, binding: ImportBinding) {
        if let Some(symbol_id) = binding.resolved_symbol {
            self.import_scope
                .insert(binding.exposed_name.clone(), symbol_id);
        }
        self.import_bindings
            .insert(binding.exposed_name.clone(), binding);
    }

    fn import_binding(&self, name: &str) -> Option<ImportBinding> {
        self.import_bindings.get(name).cloned()
    }

    fn resolve_expression_type(&self, expr: &str) -> Option<String> {
        self.expression_types.get(expr).cloned()
    }
}

/// Inheritance resolver for Kotlin's class hierarchy
/// Supports single inheritance (extends) and multiple interface implementation
#[derive(Default)]
pub struct KotlinInheritanceResolver {
    /// child -> immediate parents (superclass + interfaces)
    parents: HashMap<String, Vec<String>>,
    /// type -> methods defined directly on that type
    type_methods: HashMap<String, HashSet<String>>,
}

impl KotlinInheritanceResolver {
    pub fn new() -> Self {
        Self::default()
    }

    fn resolve_method_recursive(
        &self,
        ty: &str,
        method: &str,
        visited: &mut HashSet<String>,
    ) -> Option<String> {
        if !visited.insert(ty.to_string()) {
            return None; // Cycle detection
        }

        // Check if method is defined on this type
        if self
            .type_methods
            .get(ty)
            .is_some_and(|methods| methods.contains(method))
        {
            return Some(ty.to_string());
        }

        // Search in parents (superclass and interfaces)
        if let Some(parents) = self.parents.get(ty) {
            for parent in parents {
                if let Some(found) = self.resolve_method_recursive(parent, method, visited) {
                    return Some(found);
                }
            }
        }

        None
    }

    fn collect_chain(&self, ty: &str, visited: &mut HashSet<String>, out: &mut Vec<String>) {
        if !visited.insert(ty.to_string()) {
            return; // Cycle detection
        }
        if let Some(parents) = self.parents.get(ty) {
            for parent in parents {
                out.push(parent.clone());
                self.collect_chain(parent, visited, out);
            }
        }
    }

    fn gather_methods(&self, ty: &str, visited: &mut HashSet<String>, out: &mut HashSet<String>) {
        if !visited.insert(ty.to_string()) {
            return; // Cycle detection
        }

        // Add methods from this type
        if let Some(methods) = self.type_methods.get(ty) {
            out.extend(methods.iter().cloned());
        }

        // Recursively gather from parents
        if let Some(parents) = self.parents.get(ty) {
            for parent in parents {
                self.gather_methods(parent, visited, out);
            }
        }
    }
}

impl InheritanceResolver for KotlinInheritanceResolver {
    fn add_inheritance(&mut self, child: String, parent: String, _kind: &str) {
        self.parents.entry(child).or_default().push(parent);
    }

    fn add_type_methods(&mut self, type_name: String, methods: Vec<String>) {
        self.type_methods
            .entry(type_name)
            .or_default()
            .extend(methods);
    }

    fn resolve_method(&self, type_name: &str, method_name: &str) -> Option<String> {
        let mut visited = HashSet::new();
        self.resolve_method_recursive(type_name, method_name, &mut visited)
    }

    fn get_inheritance_chain(&self, type_name: &str) -> Vec<String> {
        let mut visited = HashSet::new();
        let mut chain = Vec::new();
        self.collect_chain(type_name, &mut visited, &mut chain);
        chain
    }

    fn get_all_methods(&self, type_name: &str) -> Vec<String> {
        let mut visited = HashSet::new();
        let mut methods = HashSet::new();
        self.gather_methods(type_name, &mut visited, &mut methods);
        methods.into_iter().collect()
    }

    fn is_subtype(&self, child: &str, parent: &str) -> bool {
        if child == parent {
            return true;
        }

        let mut visited = HashSet::new();
        self.is_subtype_recursive(child, parent, &mut visited)
    }
}

impl KotlinInheritanceResolver {
    fn is_subtype_recursive(
        &self,
        child: &str,
        parent: &str,
        visited: &mut HashSet<String>,
    ) -> bool {
        if !visited.insert(child.to_string()) {
            return false; // Cycle detection
        }

        if let Some(parents) = self.parents.get(child) {
            for p in parents {
                if p == parent {
                    return true;
                }
                if self.is_subtype_recursive(p, parent, visited) {
                    return true;
                }
            }
        }

        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_resolution_context() {
        let mut ctx = KotlinResolutionContext::new(FileId(1));

        // Add a module-level symbol
        ctx.add_symbol("topLevel".to_string(), SymbolId(1), ScopeLevel::Module);

        // Resolve it
        assert_eq!(ctx.resolve("topLevel"), Some(SymbolId(1)));

        // Enter a class scope
        ctx.enter_scope(ScopeType::Class);
        ctx.add_symbol("member".to_string(), SymbolId(2), ScopeLevel::Module);

        // Both should be visible
        assert_eq!(ctx.resolve("topLevel"), Some(SymbolId(1)));
        assert_eq!(ctx.resolve("member"), Some(SymbolId(2)));

        ctx.exit_scope();

        // Top-level still visible, member not visible outside class
        assert_eq!(ctx.resolve("topLevel"), Some(SymbolId(1)));
    }

    #[test]
    fn test_inheritance_resolver() {
        let mut resolver = KotlinInheritanceResolver::new();

        // Define inheritance: Child -> Parent -> GrandParent
        resolver.add_inheritance("Child".to_string(), "Parent".to_string(), "extends");
        resolver.add_inheritance("Parent".to_string(), "GrandParent".to_string(), "extends");

        // Add methods
        resolver.add_type_methods("GrandParent".to_string(), vec!["grandMethod".to_string()]);
        resolver.add_type_methods("Parent".to_string(), vec!["parentMethod".to_string()]);
        resolver.add_type_methods("Child".to_string(), vec!["childMethod".to_string()]);

        // Test method resolution
        assert_eq!(
            resolver.resolve_method("Child", "childMethod"),
            Some("Child".to_string())
        );
        assert_eq!(
            resolver.resolve_method("Child", "parentMethod"),
            Some("Parent".to_string())
        );
        assert_eq!(
            resolver.resolve_method("Child", "grandMethod"),
            Some("GrandParent".to_string())
        );

        // Test subtype checking
        assert!(resolver.is_subtype("Child", "Parent"));
        assert!(resolver.is_subtype("Child", "GrandParent"));
        assert!(!resolver.is_subtype("Parent", "Child"));
    }
}