-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathnode.rs
309 lines (273 loc) · 8.99 KB
/
node.rs
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
use oxc_ast::AstKind;
use oxc_cfg::BlockNodeId;
use oxc_index::IndexVec;
use oxc_span::GetSpan;
use oxc_syntax::{
node::{NodeFlags, NodeId},
scope::ScopeId,
};
/// Semantic node contains all the semantic information about an ast node.
#[derive(Debug, Clone, Copy)]
pub struct AstNode<'a> {
id: NodeId,
/// A pointer to the ast node, which resides in the `bumpalo` memory arena.
kind: AstKind<'a>,
/// Associated Scope (initialized by binding)
scope_id: ScopeId,
/// Associated `BasicBlockId` in CFG (initialized by control_flow)
cfg_id: BlockNodeId,
flags: NodeFlags,
}
impl<'a> AstNode<'a> {
#[inline]
pub(crate) fn new(
kind: AstKind<'a>,
scope_id: ScopeId,
cfg_id: BlockNodeId,
flags: NodeFlags,
id: NodeId,
) -> Self {
Self { id, kind, scope_id, cfg_id, flags }
}
/// This node's unique identifier.
#[inline]
pub fn id(&self) -> NodeId {
self.id
}
/// ID of the control flow graph node this node is in.
///
/// See [oxc_cfg::ControlFlowGraph] for more information.
#[inline]
pub fn cfg_id(&self) -> BlockNodeId {
self.cfg_id
}
/// Access the underlying struct from [`oxc_ast`].
///
/// # Examples
///
/// ```
/// use oxc_semantic::AstNode;
///
/// fn get_function_name<'a>(node: AstNode<'a>) -> Option<&'a str> {
/// match node.kind() {
/// AstKind::Function(func) => Some(func.name()),
/// _ => None,
/// }
/// }
/// ```
#[inline]
pub fn kind(&self) -> AstKind<'a> {
self.kind
}
/// The scope in which this node was declared.
///
/// It is important to note that this is _not_ the scope created _by_ the
/// node. For example, given a function declaration, this is the scope where
/// the function is declared, not the scope created by its body.
#[inline]
pub fn scope_id(&self) -> ScopeId {
self.scope_id
}
/// Flags providing additional information about the node.
#[inline]
pub fn flags(&self) -> NodeFlags {
self.flags
}
/// Get a mutable reference to this node's flags.
#[inline]
pub fn flags_mut(&mut self) -> &mut NodeFlags {
&mut self.flags
}
}
impl GetSpan for AstNode<'_> {
#[inline]
fn span(&self) -> oxc_span::Span {
self.kind.span()
}
}
/// Untyped AST nodes flattened into an vec
#[derive(Debug, Default)]
pub struct AstNodes<'a> {
/// The root node should always point to a `Program`, which is the real
/// root of the tree. It isn't possible to statically check for this, so
/// users should beware.
root: Option<NodeId>,
nodes: IndexVec<NodeId, AstNode<'a>>,
/// `node` -> `parent`
parent_ids: IndexVec<NodeId, Option<NodeId>>,
}
impl<'a> AstNodes<'a> {
/// Iterate over all [`AstNode`]s in this AST.
pub fn iter(&self) -> impl Iterator<Item = &AstNode<'a>> + '_ {
self.nodes.iter()
}
/// Returns the number of node in this AST.
#[inline]
pub fn len(&self) -> usize {
self.nodes.len()
}
/// Returns `true` if there are no nodes in this AST.
#[inline]
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
/// Walk up the AST, iterating over each parent [`AstNode`].
///
/// The first node produced by this iterator is the first parent of the node
/// pointed to by `node_id`. The last node will usually be a `Program`.
#[inline]
pub fn ancestors(&self, node_id: NodeId) -> impl Iterator<Item = &AstNode<'a>> + Clone + '_ {
AstNodeParentIter { current_node_id: Some(node_id), nodes: self }
}
/// Walk up the AST, iterating over each parent [`AstKind`].
///
/// The first node produced by this iterator is the first parent of the node
/// pointed to by `node_id`. The last node will is a [`AstKind::Program`].
#[inline]
pub fn ancestor_kinds(
&self,
node_id: NodeId,
) -> impl Iterator<Item = AstKind<'a>> + Clone + '_ {
self.ancestors(node_id).map(AstNode::kind)
}
/// Access the underlying struct from [`oxc_ast`].
///
/// ## Example
///
/// ```
/// use oxc_semantic::AstNodes;
/// use oxc_ast::AstKind;
///
/// let ast: AstNodes<'_> = get_ast_in_some_way();
/// assert!(matches!(
/// ast.kind(ast.root().unwrap()),
/// AstKind::Program(_)
/// ));
/// ```
#[inline]
pub fn kind(&self, node_id: NodeId) -> AstKind<'a> {
self.nodes[node_id].kind
}
/// Get id of this node's parent.
#[inline]
pub fn parent_id(&self, node_id: NodeId) -> Option<NodeId> {
self.parent_ids[node_id]
}
/// Get the kind of the parent node.
pub fn parent_kind(&self, node_id: NodeId) -> Option<AstKind<'a>> {
self.parent_id(node_id).map(|node_id| self.kind(node_id))
}
/// Get a reference to a node's parent.
pub fn parent_node(&self, node_id: NodeId) -> Option<&AstNode<'a>> {
self.parent_id(node_id).map(|node_id| self.get_node(node_id))
}
#[inline]
pub fn get_node(&self, node_id: NodeId) -> &AstNode<'a> {
&self.nodes[node_id]
}
#[inline]
pub fn get_node_mut(&mut self, node_id: NodeId) -> &mut AstNode<'a> {
&mut self.nodes[node_id]
}
/// Get the root [`NodeId`]. This always points to a [`Program`] node.
///
/// Returns [`None`] if root node isn't set. This will never happen if you
/// are obtaining an [`AstNodes`] that has already been constructed.
///
/// [`Program`]: oxc_ast::ast::Program
#[inline]
pub fn root(&self) -> Option<NodeId> {
self.root
}
/// Get the root node as immutable reference, It is always guaranteed to be a [`Program`].
///
/// Returns [`None`] if root node isn't set. This will never happen if you
/// are obtaining an [`AstNodes`] that has already been constructed.
///
/// [`Program`]: oxc_ast::ast::Program
#[inline]
pub fn root_node(&self) -> Option<&AstNode<'a>> {
self.root().map(|id| self.get_node(id))
}
/// Get the root node as mutable reference, It is always guaranteed to be a [`Program`].
///
/// Returns [`None`] if root node isn't set. This will never happen if you
/// are obtaining an [`AstNodes`] that has already been constructed.
///
/// [`Program`]: oxc_ast::ast::Program
#[inline]
pub fn root_node_mut(&mut self) -> Option<&mut AstNode<'a>> {
self.root().map(|id| self.get_node_mut(id))
}
/// Walk up the AST, iterating over each parent [`NodeId`].
///
/// The first node produced by this iterator is the first parent of the node
/// pointed to by `node_id`. The last node will always be a [`Program`].
///
/// [`Program`]: oxc_ast::ast::Program
pub fn ancestor_ids(&self, node_id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
let parent_ids = &self.parent_ids;
std::iter::successors(Some(node_id), |&node_id| parent_ids[node_id])
}
/// Create and add an [`AstNode`] to the [`AstNodes`] tree and get its [`NodeId`].
/// Node must not be [`Program`]; if it is, use [`add_program_node`] instead.
///
/// [`Program`]: oxc_ast::ast::Program
/// [`add_program_node`]: AstNodes::add_program_node
#[inline]
pub fn add_node(
&mut self,
kind: AstKind<'a>,
scope_id: ScopeId,
parent_node_id: NodeId,
cfg_id: BlockNodeId,
flags: NodeFlags,
) -> NodeId {
let node_id = self.parent_ids.push(Some(parent_node_id));
let node = AstNode::new(kind, scope_id, cfg_id, flags, node_id);
self.nodes.push(node);
node_id
}
/// Create and add an [`AstNode`] to the [`AstNodes`] tree and get its [`NodeId`].
pub fn add_program_node(
&mut self,
kind: AstKind<'a>,
scope_id: ScopeId,
cfg_id: BlockNodeId,
flags: NodeFlags,
) -> NodeId {
let node_id = self.parent_ids.push(None);
self.root = Some(node_id);
let node = AstNode::new(kind, scope_id, cfg_id, flags, node_id);
self.nodes.push(node);
node_id
}
/// Reserve space for at least `additional` more nodes.
pub fn reserve(&mut self, additional: usize) {
self.nodes.reserve(additional);
self.parent_ids.reserve(additional);
}
}
impl<'a, 'n> IntoIterator for &'n AstNodes<'a> {
type IntoIter = std::slice::Iter<'n, AstNode<'a>>;
type Item = &'n AstNode<'a>;
fn into_iter(self) -> Self::IntoIter {
self.nodes.iter()
}
}
#[derive(Debug, Clone)]
pub struct AstNodeParentIter<'s, 'a> {
current_node_id: Option<NodeId>,
nodes: &'s AstNodes<'a>,
}
impl<'s, 'a> Iterator for AstNodeParentIter<'s, 'a> {
type Item = &'s AstNode<'a>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(node_id) = self.current_node_id {
self.current_node_id = self.nodes.parent_ids[node_id];
Some(self.nodes.get_node(node_id))
} else {
None
}
}
}