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
use std::{
mem,
panic::{RefUnwindSafe, UnwindSafe},
ptr,
sync::Arc,
};
use crate::{swap_cell::SwapCell, GreenNode, TextUnit, Types};
use colosseum::sync::Arena;
type LazyNode<T> = SwapCell<TextUnit, SyntaxNode<T>>;
pub(crate) struct SyntaxRoot<T: Types> {
pub(crate) arena: Arena<LazyNode<T>>,
pub(crate) data: T::RootData,
}
#[derive(Debug)]
pub(crate) struct ParentData<T: Types> {
pub(crate) parent: ptr::NonNull<SyntaxNode<T>>,
pub(crate) start_offset: TextUnit,
pub(crate) index_in_parent: u32,
}
pub struct SyntaxNode<T: Types> {
root: *const SyntaxRoot<T>,
pub(crate) parent: Option<ParentData<T>>,
pub(crate) green: GreenNode<T>,
pub(crate) children: ptr::NonNull<[LazyNode<T>]>,
}
unsafe impl<T> Send for SyntaxNode<T>
where
T: Types,
T::RootData: Send,
T::Kind: Send,
{
}
unsafe impl<T> Sync for SyntaxNode<T>
where
T: Types,
T::RootData: Sync + Send,
T::Kind: Sync + Send,
{
}
impl<T> UnwindSafe for SyntaxNode<T>
where
T: Types,
T::RootData: UnwindSafe,
T::Kind: UnwindSafe,
{
}
impl<T> RefUnwindSafe for SyntaxNode<T>
where
T: Types,
T::RootData: RefUnwindSafe + UnwindSafe,
T::Kind: RefUnwindSafe + UnwindSafe,
{
}
pub struct TreeArc<T, N>
where
T: Types,
N: TransparentNewType<Repr = SyntaxNode<T>>,
{
pub(crate) inner: *const N,
}
pub unsafe trait TransparentNewType: Sized {
type Repr;
fn from_repr(repr: &Self::Repr) -> &Self {
assert!(mem::size_of::<Self>() == mem::size_of::<Self::Repr>());
unsafe { &*(repr as *const Self::Repr as *const Self) }
}
fn into_repr(&self) -> &Self::Repr {
assert!(mem::size_of::<Self>() == mem::size_of::<Self::Repr>());
unsafe { &*(self as *const Self as *const Self::Repr) }
}
}
unsafe impl<T: Types> TransparentNewType for SyntaxNode<T> {
type Repr = SyntaxNode<T>;
fn from_repr(repr: &Self::Repr) -> &Self {
repr
}
fn into_repr(&self) -> &Self::Repr {
self
}
}
impl<T, N> TreeArc<T, N>
where
T: Types,
N: TransparentNewType<Repr = SyntaxNode<T>>,
{
pub(crate) fn new(node: &N) -> TreeArc<T, N> {
let node: &SyntaxNode<T> = node.into_repr();
let root: Arc<SyntaxRoot<T>> = unsafe { Arc::from_raw(node.root) };
std::mem::forget(Arc::clone(&root));
std::mem::forget(root);
TreeArc {
inner: N::from_repr(node) as *const N,
}
}
pub fn cast<U>(this: TreeArc<T, N>) -> TreeArc<T, U>
where
U: TransparentNewType<Repr = SyntaxNode<T>>,
{
let r: &SyntaxNode<T> = this.into_repr();
let n = U::from_repr(r);
TreeArc::new(n)
}
}
impl<T, N> Drop for TreeArc<T, N>
where
T: Types,
N: TransparentNewType<Repr = SyntaxNode<T>>,
{
fn drop(&mut self) {
let root: *const SyntaxRoot<T> = {
let node: &N = &*self;
let node = node.into_repr();
node.root
};
drop(unsafe { Arc::from_raw(root) });
}
}
unsafe impl<T, N> Send for TreeArc<T, N>
where
T: Types,
N: TransparentNewType<Repr = SyntaxNode<T>>,
N: Send,
{
}
unsafe impl<T: Sync, N> Sync for TreeArc<T, N>
where
T: Types,
N: TransparentNewType<Repr = SyntaxNode<T>>,
N: Sync,
{
}
impl<T, N> std::ops::Deref for TreeArc<T, N>
where
T: Types,
N: TransparentNewType<Repr = SyntaxNode<T>>,
{
type Target = N;
fn deref(&self) -> &N {
unsafe { &*self.inner }
}
}
impl<T: Types> SyntaxNode<T> {
pub(crate) fn new_root(green: GreenNode<T>, data: T::RootData) -> TreeArc<T, SyntaxNode<T>> {
let root = SyntaxRoot {
arena: Arena::new(),
data,
};
let red_node: *mut SyntaxNode<T> = {
let mut red_node = root.arena.alloc(SwapCell::new(0.into()));
red_node.get_or_init(|_| SyntaxNode::new_impl(&root, None, green));
red_node.get_mut().unwrap()
};
let root_ptr: *const SyntaxRoot<T> = Arc::into_raw(Arc::new(root));
unsafe {
(*red_node).root = root_ptr;
}
TreeArc { inner: red_node }
}
fn new_child(
&self,
start_offset: TextUnit,
index_in_parent: usize,
green: GreenNode<T>,
) -> SyntaxNode<T> {
let parent_data = ParentData {
parent: ptr::NonNull::from(self),
start_offset,
index_in_parent: index_in_parent as u32,
};
SyntaxNode::new_impl(self.root(), Some(parent_data), green)
}
fn new_impl(
root: &SyntaxRoot<T>,
parent: Option<ParentData<T>>,
green: GreenNode<T>,
) -> SyntaxNode<T> {
let mut start_offset = parent
.as_ref()
.map(|it| it.start_offset)
.unwrap_or(0.into());
let children = root
.arena
.alloc_extend(green.children().iter().map(|child| {
let off = start_offset;
start_offset += child.text_len();
SwapCell::new(off)
}));
SyntaxNode {
root,
parent,
green,
children: children.into(),
}
}
pub(crate) fn root(&self) -> &SyntaxRoot<T> {
unsafe { &*self.root }
}
pub(crate) fn parent_impl(&self) -> Option<&SyntaxNode<T>> {
let ptr = self.parent.as_ref()?.parent;
Some(unsafe { &*ptr.as_ptr() })
}
pub(crate) fn children_impl(&self) -> &[LazyNode<T>] {
unsafe { &*self.children.as_ptr() }
}
pub(crate) fn get_child(&self, idx: usize) -> Option<&SyntaxNode<T>> {
if idx >= self.n_children() {
return None;
}
let child = self.children_impl()[idx].get_or_init(|start_offset| {
let green_children = self.green.children();
self.new_child(start_offset, idx, green_children[idx].clone())
});
Some(child)
}
pub fn memory_size_of_red_children(&self) -> usize {
self.children_impl()
.iter()
.map(|it| {
std::mem::size_of::<LazyNode<T>>()
+ it.get().map_or(0, |it| it.memory_size_of_red_children())
})
.sum()
}
}