5151namespace swift {
5252namespace syntax {
5353
54- // / A reference counted box that can contain any type.
55- template <typename T>
56- class RefCountedBox final
57- : public llvm::ThreadSafeRefCountedBase<RefCountedBox<T>> {
58- public:
59- const T Data;
60-
61- RefCountedBox (const T Data) : Data(Data) {}
62-
63- static RC<RefCountedBox<T>> make (const T &Data) {
64- return RC<RefCountedBox<T>>{new RefCountedBox (Data)};
65- }
66- };
67-
6854// / The class for holding parented syntax.
6955// /
7056// / This structure should not contain significant public
7157// / API or internal modification API.
7258// /
7359// / It is essentially a wrapper around \c AbsoluteRawSyntax that also keeps
7460// / track of the parent.
75- class SyntaxData {
61+ class SyntaxData : public llvm ::ThreadSafeRefCountedBase<SyntaxData> {
7662 const AbsoluteRawSyntax AbsoluteRaw;
77- RC<RefCountedBox< SyntaxData> > Parent;
63+ RC<const SyntaxData> Parent;
7864
7965 // / If this node is the root of a Syntax tree (i.e. \c Parent is \c nullptr ),
8066 // / the arena in which this node's \c RawSyntax node has been allocated.
8167 // / This keeps this \c RawSyntax nodes referenced by this tree alive.
8268 RC<SyntaxArena> Arena;
8369
8470 // / Create a intermediate node with a parent.
85- SyntaxData (AbsoluteRawSyntax AbsoluteRaw,
86- const RC<RefCountedBox<SyntaxData>> &Parent)
71+ SyntaxData (AbsoluteRawSyntax AbsoluteRaw, const RC<const SyntaxData> &Parent)
8772 : AbsoluteRaw(AbsoluteRaw), Parent(Parent), Arena(nullptr ) {}
8873
8974 // / Create a new root node
@@ -94,36 +79,36 @@ class SyntaxData {
9479public:
9580 // / With a new \c RawSyntax node, create a new node from this one and
9681 // / recursively rebuild the parental chain up to the root.
97- SyntaxData replacingSelf (const RawSyntax *NewRaw) const ;
82+ RC< const SyntaxData> replacingSelf (const RawSyntax *NewRaw) const ;
9883
9984 // / Replace a child in the raw syntax and recursively rebuild the
10085 // / parental chain up to the root.
10186 template <typename CursorType>
102- SyntaxData replacingChild (const RawSyntax *RawChild,
103- CursorType ChildCursor) const {
87+ RC< const SyntaxData> replacingChild (const RawSyntax *RawChild,
88+ CursorType ChildCursor) const {
10489 auto NewRaw = AbsoluteRaw.getRaw ()->replacingChild (ChildCursor, RawChild);
10590 return replacingSelf (NewRaw);
10691 }
10792
10893 // / Get the node immediately before this current node that does contain a
109- // / non-missing token. Return \c None if we cannot find such node.
110- Optional< SyntaxData> getPreviousNode () const ;
94+ // / non-missing token. Return \c nullptr if we cannot find such node.
95+ RC< const SyntaxData> getPreviousNode () const ;
11196
11297 // / Get the node immediately after this current node that does contain a
113- // / non-missing token. Return \c None if we cannot find such node.
114- Optional< SyntaxData> getNextNode () const ;
98+ // / non-missing token. Return \c nullptr if we cannot find such node.
99+ RC< const SyntaxData> getNextNode () const ;
115100
116- // / Get the first non-missing token node in this tree. Return \c None if
101+ // / Get the first non-missing token node in this tree. Return \c nullptr if
117102 // / this node does not contain non-missing tokens.
118- Optional< SyntaxData> getFirstToken () const ;
103+ RC< const SyntaxData> getFirstToken () const ;
119104
120- // / Get the last non-missing token node in this tree. Return \c None if
105+ // / Get the last non-missing token node in this tree. Return \c nullptr if
121106 // / this node does not contain non-missing tokens.
122- Optional< SyntaxData> getLastToken () const ;
107+ RC< const SyntaxData> getLastToken () const ;
123108
124109 // / Make a new \c SyntaxData node for the tree's root.
125- static SyntaxData makeRoot (AbsoluteRawSyntax AbsoluteRaw) {
126- return SyntaxData ( AbsoluteRaw);
110+ static RC< const SyntaxData> makeRoot (AbsoluteRawSyntax AbsoluteRaw) {
111+ return RC< const SyntaxData>( new SyntaxData ( AbsoluteRaw) );
127112 }
128113
129114 const AbsoluteRawSyntax &getAbsoluteRaw () const { return AbsoluteRaw; }
@@ -135,13 +120,7 @@ class SyntaxData {
135120 SyntaxKind getKind () const { return AbsoluteRaw.getRaw ()->getKind (); }
136121
137122 // / Return the parent syntax if there is one.
138- Optional<SyntaxData> getParent () const {
139- if (Parent) {
140- return Parent->Data ;
141- } else {
142- return None;
143- }
144- }
123+ RC<const SyntaxData> getParent () const { return Parent; }
145124
146125 // / Returns true if this syntax node has a parent.
147126 bool hasParent () const {
@@ -161,13 +140,13 @@ class SyntaxData {
161140
162141 // / Gets the child at the index specified by the provided cursor.
163142 template <typename CursorType>
164- Optional< SyntaxData> getChild (CursorType Cursor) const {
143+ RC< const SyntaxData> getChild (CursorType Cursor) const {
165144 return getChild (
166145 (AbsoluteSyntaxPosition::IndexInParentType)cursorIndex (Cursor));
167146 }
168147
169148 // / Gets the child at the specified \p Index.
170- Optional< SyntaxData>
149+ RC< const SyntaxData>
171150 getChild (AbsoluteSyntaxPosition::IndexInParentType Index) const ;
172151
173152 // / Get the offset at which the leading trivia of this node starts.
0 commit comments