-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathids.rs
More file actions
233 lines (205 loc) · 7.04 KB
/
ids.rs
File metadata and controls
233 lines (205 loc) · 7.04 KB
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
//! HIR item identifiers with Salsa interning.
//!
//! This module defines stable IDs for all top-level items in BAML.
//! IDs are interned via Salsa, providing:
//! - Stability across edits (content-based, not order-based)
//! - Compactness (u32 instead of full location data)
//! - Efficient comparison and hashing
use std::marker::PhantomData;
use baml_base::Name;
use rustc_hash::FxHashMap;
/// Identifier for a class definition.
pub use crate::loc::ClassLoc as ClassId;
/// Identifier for a client configuration.
pub use crate::loc::ClientLoc as ClientId;
/// Identifier for an enum definition.
pub use crate::loc::EnumLoc as EnumId;
// Note: In Salsa 2022, interned types are their own IDs.
// The #[salsa::interned] macro in loc.rs creates these types directly.
// We re-export them here as type aliases for clarity.
/// Identifier for a function (LLM or expression).
/// This is the interned `FunctionLoc` from loc.rs.
pub use crate::loc::FunctionLoc as FunctionId;
/// Identifier for a generator configuration.
pub use crate::loc::GeneratorLoc as GeneratorId;
/// Identifier for a retry policy.
pub use crate::loc::RetryPolicyLoc as RetryPolicyId;
/// Identifier for a template string definition.
pub use crate::loc::TemplateStringLoc as TemplateStringId;
/// Identifier for a test definition.
pub use crate::loc::TestLoc as TestId;
/// Identifier for a type alias.
pub use crate::loc::TypeAliasLoc as TypeAliasId;
// Manual Debug implementations for Salsa interned types
// These types don't auto-derive Debug, so we provide simple implementations
impl std::fmt::Debug for FunctionId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "FunctionId(..)")
}
}
impl std::fmt::Debug for ClassId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ClassId(..)")
}
}
impl std::fmt::Debug for EnumId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "EnumId(..)")
}
}
impl std::fmt::Debug for TypeAliasId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TypeAliasId(..)")
}
}
impl std::fmt::Debug for ClientId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ClientId(..)")
}
}
impl std::fmt::Debug for TestId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TestId(..)")
}
}
impl std::fmt::Debug for GeneratorId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "GeneratorId(..)")
}
}
impl std::fmt::Debug for TemplateStringId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TemplateStringId(..)")
}
}
impl std::fmt::Debug for RetryPolicyId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "RetryPolicyId(..)")
}
}
/// Union type for any top-level item.
///
/// Note: Salsa interned types have a `'db` lifetime, so `ItemId` must also have one.
#[derive(Clone, Copy, PartialEq, Eq, Hash, salsa::Update)]
pub enum ItemId<'db> {
Function(FunctionId<'db>),
Class(ClassId<'db>),
Enum(EnumId<'db>),
TypeAlias(TypeAliasId<'db>),
Client(ClientId<'db>),
Generator(GeneratorId<'db>),
Test(TestId<'db>),
TemplateString(TemplateStringId<'db>),
RetryPolicy(RetryPolicyId<'db>),
}
// Manual Debug impl since Salsa interned types don't auto-derive Debug
impl std::fmt::Debug for ItemId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ItemId::Function(_) => write!(f, "Function(_)"),
ItemId::Class(_) => write!(f, "Class(_)"),
ItemId::Enum(_) => write!(f, "Enum(_)"),
ItemId::TypeAlias(_) => write!(f, "TypeAlias(_)"),
ItemId::Client(_) => write!(f, "Client(_)"),
ItemId::Generator(_) => write!(f, "Generator(_)"),
ItemId::Test(_) => write!(f, "Test(_)"),
ItemId::TemplateString(_) => write!(f, "TemplateString(_)"),
ItemId::RetryPolicy(_) => write!(f, "RetryPolicy(_)"),
}
}
}
/// Local ID within an `ItemTree` (type-safe, collision-resistant).
///
/// Packs a 16-bit hash and 16-bit collision index into 32 bits.
/// This follows rust-analyzer's approach: hash for position-independence,
/// index for collision handling.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LocalItemId<T> {
/// Upper 16 bits: hash, Lower 16 bits: collision index
packed: u32,
_phantom: PhantomData<T>,
}
impl<T> LocalItemId<T> {
/// Create a new `LocalItemId` from hash and collision index.
pub const fn new(hash: u16, index: u16) -> Self {
let packed = ((hash as u32) << 16) | (index as u32);
LocalItemId {
packed,
_phantom: PhantomData,
}
}
/// Extract the hash portion (upper 16 bits).
#[allow(clippy::cast_possible_truncation)]
pub const fn hash(self) -> u16 {
(self.packed >> 16) as u16
}
/// Extract the collision index (lower 16 bits).
#[allow(clippy::cast_possible_truncation)]
pub const fn index(self) -> u16 {
self.packed as u16
}
pub const fn as_u32(self) -> u32 {
self.packed
}
}
/// Hash a name to 16 bits for use in `LocalItemId`.
pub fn hash_name(name: &baml_base::Name) -> u16 {
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
let mut hasher = DefaultHasher::new();
name.hash(&mut hasher);
#[allow(clippy::cast_possible_truncation)]
let hash = hasher.finish() as u16;
hash
}
/// Item kinds for collision tracking.
/// Used as part of the composite key `(ItemKind, hash)` in the collision map.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ItemKind {
Function,
Class,
Enum,
TypeAlias,
Client,
Generator,
Test,
TemplateString,
RetryPolicy,
}
pub(crate) fn allocate_local_id<T>(
next_index: &mut FxHashMap<(ItemKind, u16), u16>,
kind: ItemKind,
name: &Name,
) -> LocalItemId<T> {
let hash = hash_name(name);
let index = next_index.entry((kind, hash)).or_insert(0);
let id = LocalItemId::new(hash, *index);
*index += 1;
id
}
/// Allocator for `LocalItemId`s with collision handling.
///
/// Replays the same hashing and collision-indexing logic as `ItemTree`.
/// This allows queries to reproduce the same local IDs when scanning CST.
pub struct LocalIdAllocator {
next_index: FxHashMap<(ItemKind, u16), u16>,
}
impl LocalIdAllocator {
/// Create a new allocator with empty collision state.
pub fn new() -> Self {
Self {
next_index: FxHashMap::default(),
}
}
/// Allocate a `LocalItemId` for a named item, updating collision state.
pub fn alloc_id<T>(&mut self, kind: ItemKind, name: &Name) -> LocalItemId<T> {
allocate_local_id(&mut self.next_index, kind, name)
}
}
impl Default for LocalIdAllocator {
fn default() -> Self {
Self::new()
}
}