Repository navigation
Expand file tree
/
Copy pathast_ids.rs
More file actions
409 lines (361 loc) · 12.6 KB
/
Copy pathast_ids.rs
File metadata and controls
409 lines (361 loc) · 12.6 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
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
use ruff_index::newtype_index;
use ruff_python_ast as ast;
use ruff_python_ast::ExprRef;
use crate::Db;
use crate::HasTrackedScope;
use crate::ProgramFile;
use crate::frozen::PackedIds;
use crate::member::ScopedMemberId;
use crate::place::ScopedPlaceId;
use crate::scope::FileScopeId;
use crate::semantic_index;
use crate::symbol::ScopedSymbolId;
pub use node_key::ExpressionNodeKey;
/// The scope and use IDs recorded for one AST node during indexing.
#[derive(Clone, Copy, Default, Debug)]
struct NodeIds {
scope: Option<FileScopeId>,
use_id: Option<ScopedUseId>,
/// Zero for non-expression uses, such as function names and splatted keywords.
place_plus_one: u32,
}
/// A read of a lexical place in a particular scope.
///
/// ```python
/// def f(x):
/// before = x
/// x = 1
/// return x
/// ```
///
/// Both reads of `x` share a `scope` (the body of `f`) and a `place` (the local variable `x`).
/// Each read has its own `use_id`, which identifies the bindings that can reach it.
/// The read in `before = x` sees the parameter binding, and the read in `return x` sees `x = 1`.
#[derive(Clone, Copy, Debug)]
pub struct PlaceUse {
pub scope: FileScopeId,
pub use_id: ScopedUseId,
pub place: ScopedPlaceId,
}
/// Indexed by root AST node while the flow builder visits expressions in evaluation order.
/// Inference parses quoted annotations separately and resolves their names from syntax.
#[derive(Debug)]
pub(super) struct AstIdsBuilder {
root: Box<[NodeIds]>,
}
fn root_index(node: ast::NodeIndex) -> usize {
node.as_u32()
.expect("expected an indexed node from the root AST") as usize
}
impl AstIdsBuilder {
pub(super) fn with_node_count(node_count: usize) -> Self {
Self {
root: vec![NodeIds::default(); node_count].into_boxed_slice(),
}
}
fn entry(&mut self, node: ast::NodeIndex) -> &mut NodeIds {
&mut self.root[root_index(node)]
}
pub(super) fn record_expression(&mut self, expr: &impl HasTrackedScope, scope: FileScopeId) {
self.entry(expr.node_index().load()).scope = Some(scope);
}
/// Records a use on a non-expression node, such as a function name or the `**kwargs`
/// keyword in a call. These nodes can have a use ID without an explicitly recorded scope.
pub(super) fn record_other_use(
&mut self,
key: impl Into<ExpressionNodeKey>,
use_id: ScopedUseId,
) {
self.entry(key.into().index()).use_id = Some(use_id);
}
pub(super) fn record_place_use(
&mut self,
expr: &ast::Expr,
scope: FileScopeId,
use_id: ScopedUseId,
place: ScopedPlaceId,
) {
let ordinal = match place {
ScopedPlaceId::Symbol(id) => id.as_u32(),
ScopedPlaceId::Member(id) => id.as_u32(),
};
*self.entry(ExpressionNodeKey::from(expr).index()) = NodeIds {
scope: Some(scope),
use_id: Some(use_id),
place_plus_one: ordinal + 1,
};
}
pub(super) fn try_scoped_use(
&self,
scope: FileScopeId,
key: impl Into<ExpressionNodeKey>,
) -> Option<ScopedUseId> {
let node = key.into().index();
let row = &self.root[root_index(node)];
row.use_id.filter(|_| row.scope == Some(scope))
}
/// Stores use IDs and their lexical places in node-index order.
/// The IDs themselves keep the order in which the flow builder visited uses in each scope.
pub(super) fn finish(self) -> AstIds {
let mut pages: Vec<UsePage> = Vec::new();
let mut use_ids = Vec::new();
let mut places_plus_one = Vec::new();
let mut scopes = Vec::new();
for (node, row) in (0..).map(ast::NodeIndex::from).zip(self.root) {
if let Some(scope) = row.scope {
ScopeRange::record(&mut scopes, node, scope);
}
let Some(use_id) = row.use_id else { continue };
if let Some(page) = pages.last_mut()
&& let Some(bit) = page.bit(node)
{
page.mask |= bit;
} else {
// Each set bit accounts for one distinct node in the use array.
let first_use = pages
.last()
.map_or(0, |last| last.first_use + last.mask.count_ones());
pages.push(UsePage {
first_node: node,
first_use,
mask: 1,
});
}
use_ids.push(use_id);
places_plus_one.push(row.place_plus_one);
}
AstIds {
pages: pages.into_boxed_slice(),
use_ids: use_ids.into(),
places_plus_one: places_plus_one.into(),
scopes: scopes.into_boxed_slice(),
}
}
}
/// The set bits locate uses within 64 node indices of the first use on this page.
/// Gaps between pages occupy no storage.
#[derive(Debug, get_size2::GetSize)]
struct UsePage {
first_node: ast::NodeIndex,
first_use: u32,
mask: u64,
}
impl UsePage {
fn bit(&self, node: ast::NodeIndex) -> Option<u64> {
if node == self.first_node {
return Some(1);
}
let offset = node.as_u32()?.checked_sub(self.first_node.as_u32()?)?;
1_u64.checked_shl(offset)
}
}
#[derive(Debug, get_size2::GetSize)]
struct ScopeRange {
start: ast::NodeIndex,
end: ast::NodeIndex,
scope: FileScopeId,
}
impl ScopeRange {
/// Unmarked identifiers between two expressions inherit their scope if both agree.
fn record(ranges: &mut Vec<Self>, node: ast::NodeIndex, scope: FileScopeId) {
if let Some(last) = ranges.last_mut()
&& last.scope == scope
{
last.end = node;
} else {
ranges.push(Self {
start: node,
end: node,
scope,
});
}
}
}
#[derive(Debug, get_size2::GetSize)]
pub(crate) struct AstIds {
pages: Box<[UsePage]>,
use_ids: PackedIds<ScopedUseId>,
/// Symbol or member ordinals in the same order as `use_ids`. The expression determines
/// which table to use. Keeping these in the AST index lets an edit change a read's place
/// without invalidating queries that only depend on reaching bindings.
places_plus_one: PackedIds<u32>,
scopes: Box<[ScopeRange]>,
}
impl AstIds {
/// Looks up a node in the packed use array. Each earlier set bit on the same page
/// accounts for one preceding entry in the array.
fn use_index(&self, node: ast::NodeIndex) -> Option<usize> {
let end = self.pages.partition_point(|page| page.first_node <= node);
let page = self.pages[..end].last()?;
let bit = page.bit(node)?;
if page.mask & bit == 0 {
return None;
}
let preceding = (page.mask & (bit - 1)).count_ones();
Some((page.first_use + preceding) as usize)
}
fn scope_at(&self, node: ast::NodeIndex) -> Option<FileScopeId> {
let index = self.scopes.partition_point(|range| range.end < node);
let range = self.scopes.get(index)?;
(range.start <= node).then_some(range.scope)
}
pub(super) fn expression_scope(&self, node: &impl HasTrackedScope) -> Option<FileScopeId> {
self.scope_at(node.node_index().load())
}
pub(super) fn place_use(&self, expr: ast::ExprRef<'_>) -> Option<PlaceUse> {
let node = ExpressionNodeKey::from(expr).index();
let index = self.use_index(node)?;
let ordinal = self.places_plus_one.get(index).checked_sub(1)?;
let place = match expr {
ast::ExprRef::Name(_) => ScopedPlaceId::Symbol(ScopedSymbolId::from_u32(ordinal)),
ast::ExprRef::Attribute(_) | ast::ExprRef::Subscript(_) => {
ScopedPlaceId::Member(ScopedMemberId::from_u32(ordinal))
}
_ => return None,
};
Some(PlaceUse {
scope: self.scope_at(node)?,
use_id: self.use_ids.get(index),
place,
})
}
fn use_id(&self, key: impl Into<ExpressionNodeKey>) -> ScopedUseId {
self.try_use_id(key)
.expect("the flow builder should have recorded this use")
}
pub(super) fn try_use_id(&self, key: impl Into<ExpressionNodeKey>) -> Option<ScopedUseId> {
self.use_index(key.into().index())
.map(|index| self.use_ids.get(index))
}
}
fn ast_ids<'db>(db: &'db dyn Db, file: ProgramFile<'db>) -> &'db AstIds {
semantic_index(db, file).ast_ids()
}
/// Uniquely identifies a use of a name in a [`crate::FileScopeId`].
#[newtype_index]
#[derive(Ord, PartialOrd, get_size2::GetSize)]
pub struct ScopedUseId;
pub trait HasScopedUseId {
/// Returns the ID that uniquely identifies the use in its scope.
fn scoped_use_id(&self, db: &dyn Db, file: ProgramFile<'_>) -> ScopedUseId;
}
impl HasScopedUseId for ast::Identifier {
fn scoped_use_id(&self, db: &dyn Db, file: ProgramFile<'_>) -> ScopedUseId {
let ast_ids = ast_ids(db, file);
ast_ids.use_id(self)
}
}
impl HasScopedUseId for ast::ExprName {
fn scoped_use_id(&self, db: &dyn Db, file: ProgramFile<'_>) -> ScopedUseId {
let expression_ref = ExprRef::from(self);
expression_ref.scoped_use_id(db, file)
}
}
impl HasScopedUseId for ast::ExprAttribute {
fn scoped_use_id(&self, db: &dyn Db, file: ProgramFile<'_>) -> ScopedUseId {
let expression_ref = ExprRef::from(self);
expression_ref.scoped_use_id(db, file)
}
}
impl HasScopedUseId for ast::ExprSubscript {
fn scoped_use_id(&self, db: &dyn Db, file: ProgramFile<'_>) -> ScopedUseId {
let expression_ref = ExprRef::from(self);
expression_ref.scoped_use_id(db, file)
}
}
impl HasScopedUseId for ast::Keyword {
fn scoped_use_id(&self, db: &dyn Db, file: ProgramFile<'_>) -> ScopedUseId {
let ast_ids = ast_ids(db, file);
ast_ids.use_id(self)
}
}
impl HasScopedUseId for ast::ExprRef<'_> {
fn scoped_use_id(&self, db: &dyn Db, file: ProgramFile<'_>) -> ScopedUseId {
let ast_ids = ast_ids(db, file);
ast_ids.use_id(*self)
}
}
/// Node key that can only be constructed for expressions.
pub(crate) mod node_key {
use ruff_python_ast as ast;
use crate::{ast_node_ref::AstNodeRef, node_key::NodeKey};
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
get_size2::GetSize,
salsa::SalsaValue,
)]
pub struct ExpressionNodeKey(NodeKey);
impl ExpressionNodeKey {
pub(super) fn index(self) -> ast::NodeIndex {
self.0.index()
}
}
impl From<ast::ExprRef<'_>> for ExpressionNodeKey {
fn from(value: ast::ExprRef<'_>) -> Self {
Self(NodeKey::from_node(value))
}
}
impl From<&ast::Expr> for ExpressionNodeKey {
fn from(value: &ast::Expr) -> Self {
Self(NodeKey::from_node(value))
}
}
impl From<&Box<ast::Expr>> for ExpressionNodeKey {
fn from(value: &Box<ast::Expr>) -> Self {
Self(NodeKey::from_node(&**value))
}
}
impl From<&ast::ExprCall> for ExpressionNodeKey {
fn from(value: &ast::ExprCall) -> Self {
Self(NodeKey::from_node(value))
}
}
impl From<&ast::ExprLambda> for ExpressionNodeKey {
fn from(value: &ast::ExprLambda) -> Self {
Self(NodeKey::from_node(value))
}
}
impl From<&ast::Identifier> for ExpressionNodeKey {
fn from(value: &ast::Identifier) -> Self {
Self(NodeKey::from_node(value))
}
}
impl From<&ast::Keyword> for ExpressionNodeKey {
fn from(value: &ast::Keyword) -> Self {
Self(NodeKey::from_node(value))
}
}
impl<T> From<&AstNodeRef<T>> for ExpressionNodeKey {
fn from(value: &AstNodeRef<T>) -> Self {
Self(NodeKey::from_node_ref(value))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn use_ids_across_page_boundary() {
let mut builder = AstIdsBuilder::with_node_count(129);
for (use_id, node) in [64u32, 0, 63, 128].into_iter().enumerate() {
builder.entry(node.into()).use_id = Some(ScopedUseId::from_usize(use_id));
}
let ids = builder.finish();
let lookup = |node: u32| {
ids.use_index(node.into())
.map(|index| ids.use_ids.get(index).as_u32())
};
assert_eq!(lookup(0), Some(1));
assert_eq!(lookup(62), None);
assert_eq!(lookup(63), Some(2));
assert_eq!(lookup(64), Some(0));
assert_eq!(lookup(128), Some(3));
}
}