[go: up one dir, main page]

  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
//! All tricky and unsafe bits of implementation are here. If you get a
//! segfault, this the module to scrutinize in the first place :-)

use std::{
    mem,
    panic::{RefUnwindSafe, UnwindSafe},
    ptr,
    hash::{Hash, Hasher},
    sync::Arc,
    any::Any,
};

use crate::{swap_cell::SwapCell, GreenElement, GreenNode, GreenIndex, TextUnit};
use colosseum::sync::Arena;

type LazyNode = SwapCell<(TextUnit, GreenIndex), SyntaxNode>;

pub(crate) struct SyntaxRoot {
    arena: Arena<LazyNode>,
    pub(crate) data: Option<Box<dyn Any + Send + Sync>>,
}

#[derive(Debug)]
pub(crate) struct ParentData {
    parent: ptr::NonNull<SyntaxNode>,
    pub(crate) start_offset: TextUnit,
    pub(crate) index_in_parent: SyntaxIndex,
    pub(crate) index_in_green: GreenIndex,
}

/// An immutable lazy constructed syntax tree with offsets and parent pointers.
///
/// The design is close to
/// https://github.com/apple/swift/tree/bc3189a2d265bf7728ea0cfeb55f032bfe5beaf1/lib/Syntax
///
/// All nodes constituting a tree share the ownership by a tree. Internally, and
/// `Arc` is used, but outside world observes nodes as `&SyntaxNode` or
/// `TreeArc<SyntaxNode>`, where a `TreeArc` is an Arc-like smart pointer.
pub struct SyntaxNode {
    // Created from `Arc`. The ref-count on root is equal to the number of live
    // `TreeArc` which point into this tree.
    root: *const SyntaxRoot,
    parent_data: Option<ParentData>,
    pub(crate) green: GreenNode,
    // replace with
    // `children: [(TextUnit, AtomSetOnce<SyntaxNode>)]`
    // once we can have var-length structs.
    // Perhaps even fold `TextUnit` into ptr using a spare bit?
    children: ptr::NonNull<[LazyNode]>,
}

// Manually send/sync impls due to `root: *const SyntaxRoot` field.
unsafe impl Send for SyntaxNode {}
unsafe impl Sync for SyntaxNode {}
impl UnwindSafe for SyntaxNode {}
impl RefUnwindSafe for SyntaxNode {}

impl ToOwned for SyntaxNode {
    type Owned = TreeArc<SyntaxNode>;
    fn to_owned(&self) -> TreeArc<SyntaxNode> {
        TreeArc::new(self)
    }
}

/// Index into a syntax node.
/// Unlike GreenIndex, it can only refer to Nodes,
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct SyntaxIndex(pub(crate) u32);

impl SyntaxIndex {
    pub(crate) fn prev(self) -> SyntaxIndex {
        SyntaxIndex(self.0.wrapping_sub(1))
    }
    pub(crate) fn next(self) -> SyntaxIndex {
        SyntaxIndex(self.0 + 1)
    }
}

/// Owned smart pointer for syntax Nodes.
/// It can be used with any type implementing `TransparentNewType<SyntaxNode>`.
// Conceptually, it also plays an `Arc<TreeRoot>` role as well.
pub struct TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
{
    inner: *const N,
}

impl<N> Clone for TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
{
    fn clone(&self) -> TreeArc<N> {
        let n: &N = &*self;
        TreeArc::new(n)
    }
}

impl<N> PartialEq<TreeArc<N>> for TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
{
    fn eq(&self, other: &TreeArc<N>) -> bool {
        ptr::eq(self.inner, other.inner)
    }
}

impl<N> Eq for TreeArc<N> where N: TransparentNewType<Repr = SyntaxNode> {}

impl<N> Hash for TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.inner.hash(state)
    }
}

impl<N> TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
{
    /// Creates a new owned node from a reference to a node, by bumping root's
    /// refcount.
    fn new(node: &N) -> TreeArc<N> {
        let node: &SyntaxNode = node.into_repr();
        let root: Arc<SyntaxRoot> = 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 }
    }

    /// Casts this ptr across equivalent reprs.
    pub fn cast<U>(this: TreeArc<N>) -> TreeArc<U>
    where
        U: TransparentNewType<Repr = SyntaxNode>,
    {
        // We can avoid an Arc bump if we want here, but lets leverage existing
        // `new`, to minimize unsafety.
        let r: &SyntaxNode = this.into_repr();
        let n = U::from_repr(r);
        TreeArc::new(n)
    }
}

impl<N> Drop for TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
{
    // Drop refcount
    fn drop(&mut self) {
        // Be careful not to leave dangling references to node.
        let root: *const SyntaxRoot = {
            let node: &N = &*self;
            let node = node.into_repr();
            node.root
        };
        drop(unsafe { Arc::from_raw(root) });
        // inner may be a dangling pointer at this point, but it is ok: it's a
        // raw pointer after all
    }
}

// Manual impls due to `inner: *const N` field
unsafe impl<N> Send for TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
    N: Send,
{
}

unsafe impl<N> Sync for TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
    N: Sync,
{
}

impl<N> std::ops::Deref for TreeArc<N>
where
    N: TransparentNewType<Repr = SyntaxNode>,
{
    type Target = N;
    fn deref(&self) -> &N {
        // We are a `TreeArc`, so underlying `SyntaxRoot` has an count at least
        // one, so it's safe to deref a node.
        unsafe { &*self.inner }
    }
}

/// A marker trait for transparent newtypes.
///
/// If you declare a struct like
///
/// ```no-run
/// #[repr(transparent)]
/// struct Wrapper(Inner)
/// ```
///
/// it is safe to add
///
/// ```no-run
/// unsafe impl TransparentNewType for Wrapper { type Repr = Inner; }
/// ```
///
/// Implementing this trait allows one to cast safely between the wrapper and
/// the underlying representation.
pub unsafe trait TransparentNewType: Sized {
    /// Underlying representation of a newtype.
    type Repr;
    /// Cast the underlying repr into a wrapper.
    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) }
    }
    /// Cast wrapper to the underlying repr.
    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 TransparentNewType for SyntaxNode {
    type Repr = SyntaxNode;

    fn from_repr(repr: &Self::Repr) -> &Self {
        repr
    }

    fn into_repr(&self) -> &Self::Repr {
        self
    }
}

impl SyntaxNode {
    pub(crate) fn new_root(green: GreenNode, data: Option<Box<Any + Send + Sync>>) -> TreeArc<SyntaxNode> {
        let root = SyntaxRoot { arena: Arena::new(), data };
        let red_node: *mut SyntaxNode = {
            let red_node = root.arena.alloc(SwapCell::new((0.into(), GreenIndex(0))));
            red_node.get_or_init(|_| SyntaxNode::new_impl(&root, None, green));
            red_node.get_mut().unwrap()
        };

        // "forget" the `root` so that we have rc equal to one once exiting this
        // function.
        let root_ptr: *const SyntaxRoot = Arc::into_raw(Arc::new(root));
        // set backreference
        unsafe {
            (*red_node).root = root_ptr;
        }
        TreeArc { inner: red_node }
    }

    #[cold]
    fn new_child(
        &self,
        start_offset: TextUnit,
        index_in_parent: SyntaxIndex,
        index_in_green: GreenIndex,
        green: GreenNode,
    ) -> SyntaxNode {
        let parent_data = ParentData {
            parent: ptr::NonNull::from(self),
            start_offset,
            index_in_green,
            index_in_parent,
        };
        SyntaxNode::new_impl(self.root(), Some(parent_data), green)
    }

    fn new_impl(
        root: &SyntaxRoot,
        parent_data: Option<ParentData>,
        green: GreenNode,
    ) -> SyntaxNode {
        let mut start_offset = parent_data.as_ref().map(|it| it.start_offset).unwrap_or(0.into());

        let children = root.arena.alloc_extend(green.children().iter().enumerate().filter_map(
            |(index_in_green, element)| match element {
                GreenElement::Token(it) => {
                    start_offset += it.text_len();
                    None
                }
                GreenElement::Node(it) => {
                    let off = start_offset;
                    start_offset += it.text_len();
                    Some(SwapCell::new((off, GreenIndex(index_in_green as u32))))
                }
            },
        ));
        SyntaxNode { root, parent_data, green, children: children.into() }
    }

    pub(crate) fn root(&self) -> &SyntaxRoot {
        // If we (the reference) not dangle, then root must be alive as well.
        unsafe { &*self.root }
    }

    pub(crate) fn parent_data(&self) -> Option<&ParentData> {
        self.parent_data.as_ref()
    }

    pub(crate) fn parent_impl(&self) -> Option<&SyntaxNode> {
        // If we (the reference) not dangle, then parent must be alive as well.
        let ptr = self.parent_data()?.parent;
        Some(unsafe { &*ptr.as_ptr() })
    }

    pub(crate) fn children_len(&self) -> SyntaxIndex {
        SyntaxIndex(self.children_impl().len() as u32)
    }

    fn children_impl(&self) -> &[LazyNode] {
        // If we (the reference) not dangle, then SyntaxRoot is alive, and so is
        // the Arena where children are allocated.
        unsafe { &*self.children.as_ptr() }
    }

    pub(crate) fn get_child(&self, index_in_parent: SyntaxIndex) -> Option<&SyntaxNode> {
        let lazy_child = self.children_impl().get(index_in_parent.0 as usize)?;
        let child = lazy_child.get_or_init(|(start_offset, index_in_green)| {
            self.new_child(
                start_offset,
                index_in_parent,
                index_in_green,
                match self.green.get_child(index_in_green) {
                    Some(GreenElement::Node(it)) => it.clone(),
                    _ => unreachable!(),
                },
            )
        });
        Some(child)
    }

    /// Number of memory bytes of occupied by subtree rooted at `self`.
    pub(crate) fn memory_size_of_red_children(&self) -> usize {
        self.children_impl()
            .iter()
            .map(|it| {
                std::mem::size_of::<LazyNode>()
                    + it.get().map_or(0, |it| it.memory_size_of_red_children())
            })
            .sum()
    }
}