Skip to content

Commit e8f9272

Browse files
committed
[libSyntax] Rearrange methods in ParsedRawSyntaxNode
1 parent beadd4a commit e8f9272

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

include/swift/Parse/ParsedRawSyntaxNode.h

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class ParsedRawSyntaxNode {
5959
ParsedRawSyntaxNode &operator=(const ParsedRawSyntaxNode &other) = delete;
6060

6161
public:
62+
// MARK: - Constructors
63+
6264
ParsedRawSyntaxNode()
6365
: Data(nullptr, DataKind::Null), Range(),
6466
SynKind(uint16_t(syntax::SyntaxKind::Unknown)),
@@ -73,17 +75,6 @@ class ParsedRawSyntaxNode {
7375
assert(getTokenKind() == TokKind && "Token kind with too large value!");
7476
}
7577

76-
#ifndef NDEBUG
77-
bool ensureDataIsNotRecorded() {
78-
if (getDataKind() != DataKind::Recorded)
79-
return true;
80-
llvm::dbgs() << "Leaking node: ";
81-
dump(llvm::dbgs());
82-
llvm::dbgs() << "\n";
83-
return false;
84-
}
85-
#endif
86-
8778
ParsedRawSyntaxNode &operator=(ParsedRawSyntaxNode &&other) {
8879
assert(ensureDataIsNotRecorded() &&
8980
"recorded data is being destroyed by assignment");
@@ -95,17 +86,36 @@ class ParsedRawSyntaxNode {
9586
other.reset();
9687
return *this;
9788
}
89+
9890
ParsedRawSyntaxNode(ParsedRawSyntaxNode &&other) : ParsedRawSyntaxNode() {
9991
*this = std::move(other);
10092
}
93+
94+
static ParsedRawSyntaxNode null() {
95+
return ParsedRawSyntaxNode{};
96+
}
97+
10198
~ParsedRawSyntaxNode() {
10299
assert(ensureDataIsNotRecorded() && "recorded data is being destructed");
103100
}
104101

102+
// MARK: - Retrieving node kind
103+
105104
/// Returns the type of this node (recorded, deferred layout, deferred token,
106105
/// null).
107106
DataKind getDataKind() const { return Data.getKind(); }
108107

108+
bool isNull() const { return getDataKind() == DataKind::Null; }
109+
bool isRecorded() const { return getDataKind() == DataKind::Recorded; }
110+
bool isDeferredLayout() const {
111+
return getDataKind() == DataKind::DeferredLayout;
112+
}
113+
bool isDeferredToken() const {
114+
return getDataKind() == DataKind::DeferredToken;
115+
}
116+
117+
// MARK: - Retrieving opaque data
118+
109119
/// Returns the opaque data of this node, assuming that it is deferred. This
110120
/// must be interpreted by the \c SyntaxParseAction, which likely also needs
111121
/// the node type (layout or token) to interpret the data.
@@ -116,19 +126,21 @@ class ParsedRawSyntaxNode {
116126
return Data.getOpaque();
117127
}
118128

119-
RecordedOrDeferredNode takeRecordedOrDeferredNode() {
120-
RecordedOrDeferredNode Data = this->Data;
129+
/// Return the opaque data of this node and reset it.
130+
OpaqueSyntaxNode takeData() {
131+
OpaqueSyntaxNode Data = this->Data.getOpaque();
121132
reset();
122133
return Data;
123134
}
124135

125-
/// Return the opaque data of this node and reset it.
126-
OpaqueSyntaxNode takeData() {
127-
OpaqueSyntaxNode Data = this->Data.getOpaque();
136+
RecordedOrDeferredNode takeRecordedOrDeferredNode() {
137+
RecordedOrDeferredNode Data = this->Data;
128138
reset();
129139
return Data;
130140
}
131141

142+
// MARK: - Retrieving additional node info
143+
132144
syntax::SyntaxKind getKind() const { return syntax::SyntaxKind(SynKind); }
133145
tok getTokenKind() const { return tok(TokKind); }
134146

@@ -138,25 +150,31 @@ class ParsedRawSyntaxNode {
138150
bool isToken(tok tokKind) const {
139151
return getTokenKind() == tokKind;
140152
}
153+
bool isMissing() const { return IsMissing; }
141154

142-
bool isNull() const { return getDataKind() == DataKind::Null; }
155+
/// Returns the range of this node including leading and trailing trivia.
156+
CharSourceRange getRange() const { return Range; }
143157

144-
bool isRecorded() const { return getDataKind() == DataKind::Recorded; }
145-
bool isDeferredLayout() const {
146-
return getDataKind() == DataKind::DeferredLayout;
147-
}
148-
bool isDeferredToken() const {
149-
return getDataKind() == DataKind::DeferredToken;
150-
}
158+
size_t
159+
getDeferredNumChildren(const SyntaxParsingContext *SyntaxContext) const;
151160

152-
/// Primary used for a deferred missing token.
153-
bool isMissing() const { return IsMissing; }
161+
/// If this node is a deferred layout node, return the child at index \p
162+
/// ChildIndex.
163+
/// Note that this may be an expensive operation since the \c
164+
/// SyntaxParseAction, which created the node (implicitly passed via the
165+
/// \p SyntaxContext) needs to be consulted to retrieve the child.
166+
ParsedRawSyntaxNode
167+
getDeferredChild(size_t ChildIndex,
168+
const SyntaxParsingContext *SyntaxContext) const;
169+
170+
// MARK: - Miscellaneous
154171

155172
void reset() {
156173
Data = RecordedOrDeferredNode(nullptr, DataKind::Null);
157174
SynKind = uint16_t(syntax::SyntaxKind::Unknown);
158175
TokKind = uint16_t(tok::unknown);
159176
IsMissing = false;
177+
Range = CharSourceRange();
160178
}
161179

162180
ParsedRawSyntaxNode unsafeCopy() const {
@@ -169,23 +187,6 @@ class ParsedRawSyntaxNode {
169187
return copy;
170188
}
171189

172-
/// Returns the range of this node including leading and trailing trivia.
173-
CharSourceRange getRange() const { return Range; }
174-
175-
// Deferred Layout Data ====================================================//
176-
177-
/// If this node is a deferred layout node, return the child at index \p
178-
/// ChildIndex.
179-
/// Note that this may be an expensive operation since the \c
180-
/// SyntaxParseAction, which created the node (implicitly passed via the
181-
/// \p SyntaxContext) needs to be consulted to retrieve the child.
182-
ParsedRawSyntaxNode
183-
getDeferredChild(size_t ChildIndex,
184-
const SyntaxParsingContext *SyntaxContext) const;
185-
186-
size_t
187-
getDeferredNumChildren(const SyntaxParsingContext *SyntaxContext) const;
188-
189190
ParsedRawSyntaxNode copyDeferred() const {
190191
assert(isDeferredLayout() || isDeferredToken() && "node not deferred");
191192
ParsedRawSyntaxNode copy;
@@ -197,7 +198,18 @@ class ParsedRawSyntaxNode {
197198
return copy;
198199
}
199200

200-
//==========================================================================//
201+
#ifndef NDEBUG
202+
bool ensureDataIsNotRecorded() {
203+
if (getDataKind() != DataKind::Recorded)
204+
return true;
205+
llvm::dbgs() << "Leaking node: ";
206+
dump(llvm::dbgs());
207+
llvm::dbgs() << "\n";
208+
return false;
209+
}
210+
#endif
211+
212+
// MARK: - Printing
201213

202214
/// Dump this piece of syntax recursively for debugging or testing.
203215
SWIFT_DEBUG_DUMP;
@@ -206,10 +218,6 @@ class ParsedRawSyntaxNode {
206218
/// method is also able to traverse its children and dump them.
207219
void dump(raw_ostream &OS, const SyntaxParsingContext *Context = nullptr,
208220
unsigned Indent = 0) const;
209-
210-
static ParsedRawSyntaxNode null() {
211-
return ParsedRawSyntaxNode{};
212-
}
213221
};
214222

215223
} // end namespace swift

0 commit comments

Comments
 (0)