[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
use std::{
    hash::{Hash, Hasher},
    ptr,
    sync::Arc,
};
use crate::{red::RedNode, Types};

#[derive(Debug)]
#[doc(hidden)]
pub struct SyntaxRoot<T: Types> {
    pub(crate) red: RedNode<T>,
    pub(crate) data: T::RootData,
}

/// A marker trait to distinguish between owned and borrowed
/// trees. You don't need to implement it yourself,
/// use existing `OwnedRoot` or `RefRoot`.
pub trait TreeRoot<T: Types>: Clone + Send + Sync {
    #[doc(hidden)]
    fn borrowed(&self) -> RefRoot<T>;
    #[doc(hidden)]
    fn owned(&self) -> OwnedRoot<T>;

    #[doc(hidden)]
    fn syntax_root(&self) -> &SyntaxRoot<T>;
}

/// `TreeRoot` for owned flavor of `SyntaxNode`.
#[derive(Debug)]
pub struct OwnedRoot<T: Types>(pub(crate) Arc<SyntaxRoot<T>>);

/// `TreeRoot` for borrowed flavor of `SyntaxNode`.
// TODO: shared_from_this instead of double indirection
#[derive(Debug)]
pub struct RefRoot<'a, T: Types>(&'a OwnedRoot<T>);

impl<T: Types> Clone for OwnedRoot<T> {
    fn clone(&self) -> OwnedRoot<T> {
        OwnedRoot(Arc::clone(&self.0))
    }
}

impl<'a, T: Types> Clone for RefRoot<'a, T> {
    fn clone(&self) -> RefRoot<'a, T> {
        *self
    }
}

impl<'a, T: Types> Copy for RefRoot<'a, T> {}

impl<T: Types> TreeRoot<T> for OwnedRoot<T> {
    fn borrowed(&self) -> RefRoot<T> {
        RefRoot(&self)
    }
    fn owned(&self) -> OwnedRoot<T> {
        self.clone()
    }
    fn syntax_root(&self) -> &SyntaxRoot<T> {
        &*self.0
    }
}

impl<'a, T: Types> TreeRoot<T> for RefRoot<'a, T> {
    fn borrowed(&self) -> RefRoot<T> {
        *self
    }
    fn owned(&self) -> OwnedRoot<T> {
        self.0.clone()
    }
    fn syntax_root(&self) -> &SyntaxRoot<T> {
        self.0.syntax_root()
    }
}

impl<'a, T: Types> RefRoot<'a, T> {
    pub(crate) fn syntax_root(&self) -> &'a SyntaxRoot<T> {
        self.0.syntax_root()
    }
}

#[derive(Debug)]
pub(crate) struct RedPtr<T: Types>(ptr::NonNull<RedNode<T>>);

impl<T: Types> PartialEq for RedPtr<T> {
    fn eq(&self, other: &RedPtr<T>) -> bool {
        self.0 == other.0
    }
}

impl<T: Types> Eq for RedPtr<T> {}

impl<T: Types> Hash for RedPtr<T> {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        self.0.hash(hasher)
    }
}

unsafe impl<T: Types> Send for RedPtr<T> {}
unsafe impl<T: Types> Sync for RedPtr<T> {}

impl<T: Types> Clone for RedPtr<T> {
    fn clone(&self) -> RedPtr<T> {
        *self
    }
}

impl<T: Types> Copy for RedPtr<T> {}

impl<T: Types> RedPtr<T> {
    pub(crate) fn new(red: &RedNode<T>) -> RedPtr<T> {
        RedPtr(red.into())
    }
    /// Safety: self.0 must be rooted(indirectly) by `_root`.
    pub(crate) unsafe fn get<'a>(self, _root: &'a SyntaxRoot<T>) -> &'a RedNode<T> {
        &*self.0.as_ptr()
    }
}