forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.rs
More file actions
228 lines (197 loc) · 4.38 KB
/
node.rs
File metadata and controls
228 lines (197 loc) · 4.38 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
use super::generated_ffi::NodeKind;
use std::convert::AsRef;
use std::marker::PhantomData;
use std::ptr::NonNull;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SMLoc {
ptr: *const u8,
}
impl SMLoc {
pub fn is_valid(self) -> bool {
!self.ptr.is_null()
}
pub fn pred(self) -> SMLoc {
SMLoc {
ptr: self.ptr.wrapping_sub(1),
}
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SMRange {
pub start: SMLoc,
pub end: SMLoc,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct StringRef {
data: *const u8,
length: usize,
}
impl StringRef {
pub fn as_slice(&self) -> &[u8] {
// Rust doesn't allow null pointer in a slice.
if self.data.is_null() {
&[]
} else {
unsafe { std::slice::from_raw_parts(self.data, self.length) }
}
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct UniqueString {
str: StringRef,
}
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct NodeLabel {
ptr: *const UniqueString,
}
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct NodeLabelOpt {
ptr: *const UniqueString,
}
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct NodeString {
ptr: *const UniqueString,
}
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct NodeStringOpt {
ptr: *const UniqueString,
}
impl NodeLabel {
pub fn as_slice(&self) -> &[u8] {
debug_assert!(!self.ptr.is_null(), "null NodeLabel");
unsafe { (*self.ptr).str.as_slice() }
}
}
impl NodeLabelOpt {
pub fn as_node_label(&self) -> Option<NodeLabel> {
if self.ptr.is_null() {
None
} else {
Some(NodeLabel { ptr: self.ptr })
}
}
}
impl NodeString {
pub fn as_slice(&self) -> &[u8] {
debug_assert!(!self.ptr.is_null(), "null NodeLabel");
unsafe { (*self.ptr).str.as_slice() }
}
}
impl NodeStringOpt {
pub fn as_node_string(&self) -> Option<NodeString> {
if self.ptr.is_null() {
None
} else {
Some(NodeString { ptr: self.ptr })
}
}
}
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct NodePtr {
ptr: NonNull<Node>,
}
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct NodePtrOpt {
ptr: *const Node,
}
impl NodePtr {
pub fn new(n: &Node) -> NodePtr {
NodePtr {
ptr: NonNull::from(n),
}
}
}
impl AsRef<Node> for NodePtr {
fn as_ref(&self) -> &Node {
debug_assert!(!self.ptr.as_ptr().is_null(), "null NodePtr");
unsafe { self.ptr.as_ref() }
}
}
impl NodePtrOpt {
pub fn as_node_ptr(&self) -> Option<NodePtr> {
if self.ptr.is_null() {
None
} else {
Some(NodePtr {
ptr: unsafe { NonNull::new_unchecked(self.ptr as *mut Node) },
})
}
}
}
#[repr(C)]
pub struct NodeList {
pub prev: *const NodeList,
pub next: *const NodeList,
}
#[repr(transparent)]
pub struct NodeListRef {
ptr: *const NodeList,
}
#[repr(transparent)]
pub struct NodeListOptRef {
ptr: *const NodeList,
}
impl NodeListRef {
pub fn iter(&self) -> NodeIterator {
NodeIterator {
head: self.ptr,
cur: self.ptr,
ph: PhantomData,
}
}
}
impl AsRef<NodeList> for NodeListRef {
fn as_ref(&self) -> &NodeList {
debug_assert!(!self.ptr.is_null(), "null NodeListRef");
unsafe { &*self.ptr }
}
}
impl NodeListOptRef {
pub fn as_node_list_ref(&self) -> Option<NodeListRef> {
if self.ptr.is_null() {
None
} else {
Some(NodeListRef { ptr: self.ptr })
}
}
}
pub struct NodeIterator<'a> {
head: *const NodeList,
cur: *const NodeList,
ph: PhantomData<&'a Node>,
}
impl<'a> std::iter::Iterator for NodeIterator<'a> {
type Item = &'a Node;
fn next(&mut self) -> Option<Self::Item> {
self.cur = unsafe { (*self.cur).next };
if self.cur == self.head {
None
} else {
Some(unsafe { &*(self.cur as *const Node) })
}
}
}
#[repr(C)]
pub struct Node {
pub link: NodeList,
pub kind: NodeKind,
pub parens: u32,
pub source_range: SMRange,
pub debug_loc: SMLoc,
}