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
/*
* rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
* Copyright (c) 2026 Alexander Panasenko
*
* Contact: alex@prod.codes
* Author: https://prod.codes/about/
* Project: https://github.com/alex09x/rust-intelligence
* SPDX-License-Identifier: MIT OR Apache-2.0
*/
//! Simplified `impl` target keys, indexed `impl` descriptors, and in-memory [`OverlaySourceDb`]
//! for dirty-file overrides over an existing [`SourceDb`].
use std::collections::HashMap;
use super::db::{FileId, GlobalDefId, SourceDb};
use super::mod_tree::join_norm_path;
use super::per_ns::PrimTy;
/// Arena index into [`super::def_map::CrateDefMap::local_impls`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LocalImplId(pub u32);
impl LocalImplId {
pub const NONE: Self = Self(u32::MAX);
#[inline]
pub const fn is_some(self) -> bool {
self.0 != u32::MAX
}
}
impl Default for LocalImplId {
#[inline]
fn default() -> Self {
Self::NONE
}
}
/// Canonical simplified self-type head of an `impl` block (matches `.ridx` `v5` `SimplifiedType` buckets).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ImplTargetKey {
#[default]
Unknown,
/// `struct`, `enum`, or `union` identified by compiler or `LOCAL_OVERLAY` [`GlobalDefId`].
Adt(GlobalDefId),
/// Builtin primitive (`u32`, `str`, `bool`, ...).
Prim(PrimTy),
/// `[T]` slice type.
Slice,
/// `[T; N]` fixed-size array type.
Array,
/// `(T0, T1, ...)` tuple of the given arity (`0` for unit `()`).
Tuple(u16),
/// `&T` (`is_mut = false`) or `&mut T` (`is_mut = true`).
Ref { is_mut: bool },
/// `*const T` (`is_mut = false`) or `*mut T` (`is_mut = true`).
Ptr { is_mut: bool },
/// `fn(A, B) -> R` function pointer of the given parameter count.
FnPtr { arity: u16 },
/// `dyn Trait + ...` trait object keyed by principal trait [`GlobalDefId`].
Dyn(GlobalDefId),
/// `!` never type.
Never,
/// Blanket generic parameter self type (`impl<T> Trait for T`).
Param,
}
/// One `impl` block entry in [`super::db::ItemDb`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IndexedImpl {
pub id: GlobalDefId,
/// Defining workspace [`FileId`] (`FileId::NONE` for external dependency crates).
/// When `file` is in `dirty_files`, this indexed `impl` is suppressed in favor of the dirty CST.
pub file: FileId,
pub target: ImplTargetKey,
/// Implemented trait [`GlobalDefId`] (`GlobalDefId::NONE` for inherent `impl` blocks).
pub trait_def: GlobalDefId,
pub is_negative: bool,
}
impl IndexedImpl {
#[inline]
pub const fn is_inherent(&self) -> bool {
!self.trait_def.is_some()
}
}
/// Overlay [`SourceDb`] layering unsaved/edited file contents (and newly created unsaved files)
/// on top of an immutable base [`SourceDb`] without copying clean files.
pub struct OverlaySourceDb<'a> {
base: &'a dyn SourceDb,
base_file_count: u32,
overrides: HashMap<FileId, String>,
added_files: Vec<(String, String)>,
added_by_path: HashMap<String, FileId>,
dirty_list: Vec<FileId>,
}
impl<'a> OverlaySourceDb<'a> {
pub fn new(base: &'a dyn SourceDb, base_file_count: u32) -> Self {
Self { base, base_file_count, overrides: HashMap::new(), added_files: Vec::new(), added_by_path: HashMap::new(), dirty_list: Vec::new() }
}
/// Overrides the text of an existing `file` and marks `file` dirty.
pub fn set_file_override(&mut self, file: FileId, new_text: impl Into<String>) {
self.overrides.insert(file, new_text.into());
if !self.dirty_list.contains(&file) {
self.dirty_list.push(file);
}
}
/// Overrides an existing path in `base` or registers a brand-new unsaved virtual file at `rel_path`.
pub fn upsert_path(&mut self, rel_path: &str, new_text: impl Into<String>) -> FileId {
let norm = join_norm_path("", rel_path);
if let Some(existing) = self.base.resolve_path(&norm) {
self.set_file_override(existing, new_text);
return existing;
}
if let Some(&added_id) = self.added_by_path.get(&norm) {
let idx = (added_id.0 - self.base_file_count) as usize;
self.added_files[idx].1 = new_text.into();
return added_id;
}
let id = FileId(self.base_file_count + self.added_files.len() as u32);
self.added_files.push((norm.clone(), new_text.into()));
self.added_by_path.insert(norm, id);
self.dirty_list.push(id);
id
}
/// Slice of all dirty [`FileId`]s modified or added in this overlay.
#[inline]
pub fn dirty_files(&self) -> &[FileId] {
&self.dirty_list
}
}
impl SourceDb for OverlaySourceDb<'_> {
fn file_path(&self, file: FileId) -> &str {
if file.0 >= self.base_file_count {
if let Some((p, _)) = self.added_files.get((file.0 - self.base_file_count) as usize) {
return p.as_str();
}
}
self.base.file_path(file)
}
fn file_text(&self, file: FileId) -> &str {
if let Some(ov) = self.overrides.get(&file) {
return ov.as_str();
}
if file.0 >= self.base_file_count {
if let Some((_, t)) = self.added_files.get((file.0 - self.base_file_count) as usize) {
return t.as_str();
}
}
self.base.file_text(file)
}
fn resolve_path(&self, rel_path: &str) -> Option<FileId> {
let norm = join_norm_path("", rel_path);
self.added_by_path.get(&norm).copied().or_else(|| self.base.resolve_path(&norm))
}
}