Skip to content

Commit 439b44b

Browse files
committed
[libSyntax] Re-enable dumping of ParsedRawSyntaxNode children
This requires a SyntaxParsingContext to be passed to the dump function and was thus disabled in an earlier commit
1 parent c8acecd commit 439b44b

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

include/swift/Parse/ParsedRawSyntaxNode.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ class ParsedRawSyntaxNode {
187187
getDeferredChild(size_t ChildIndex,
188188
const SyntaxParsingContext *SyntaxContext) const;
189189

190+
size_t
191+
getDeferredNumChildren(const SyntaxParsingContext *SyntaxContext) const;
192+
190193
ParsedRawSyntaxNode copyDeferred() const {
191194
assert(isDeferredLayout() || isDeferredToken() && "node not deferred");
192195
ParsedRawSyntaxNode copy;
@@ -203,8 +206,10 @@ class ParsedRawSyntaxNode {
203206
/// Dump this piece of syntax recursively for debugging or testing.
204207
SWIFT_DEBUG_DUMP;
205208

206-
/// Dump this piece of syntax recursively.
207-
void dump(raw_ostream &OS, unsigned Indent = 0) const;
209+
/// Dump this piece of syntax recursively. If \p Context is passed, this
210+
/// method is also able to traverse its children and dump them.
211+
void dump(raw_ostream &OS, const SyntaxParsingContext *Context = nullptr,
212+
unsigned Indent = 0) const;
208213

209214
static ParsedRawSyntaxNode null() {
210215
return ParsedRawSyntaxNode{};

include/swift/Parse/ParsedRawSyntaxRecorder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ class ParsedRawSyntaxRecorder final {
9898
ParsedRawSyntaxNode getDeferredChild(const ParsedRawSyntaxNode &parent,
9999
size_t ChildIndex) const;
100100

101+
/// For a deferred layout node \p node, retrieve the number of children.
102+
size_t getDeferredNumChildren(const ParsedRawSyntaxNode &node) const;
103+
101104
#ifndef NDEBUG
102105
static void verifyElementRanges(ArrayRef<ParsedRawSyntaxNode> elements);
103106
#endif

include/swift/Parse/SyntaxParseActions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ class SyntaxParseActions {
142142
size_t childIndex,
143143
SourceLoc startLoc);
144144

145+
/// Return the number of children, \p node has. These can be retrieved using
146+
/// \c getDeferredChild.
147+
virtual size_t getDeferredNumChildren(OpaqueSyntaxNode node);
148+
145149
/// Attempt to realize an opaque raw syntax node for a source file into a
146150
/// SourceFileSyntax node. This will return \c None if the parsing action
147151
/// doesn't support the realization of syntax nodes.

lib/Parse/ParsedRawSyntaxNode.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ ParsedRawSyntaxNode ParsedRawSyntaxNode::getDeferredChild(
2323
return SyntaxContext->getRecorder().getDeferredChild(*this, ChildIndex);
2424
}
2525

26+
size_t ParsedRawSyntaxNode::getDeferredNumChildren(
27+
const SyntaxParsingContext *SyntaxContext) const {
28+
assert(isDeferredLayout());
29+
return SyntaxContext->getRecorder().getDeferredNumChildren(*this);
30+
}
31+
2632
void ParsedRawSyntaxNode::dump() const {
2733
dump(llvm::errs(), /*Indent*/ 0);
2834
llvm::errs() << '\n';
2935
}
3036

31-
void ParsedRawSyntaxNode::dump(llvm::raw_ostream &OS, unsigned Indent) const {
37+
void ParsedRawSyntaxNode::dump(llvm::raw_ostream &OS,
38+
const SyntaxParsingContext *Context,
39+
unsigned Indent) const {
3240
for (decltype(Indent) i = 0; i < Indent; ++i)
3341
OS << ' ';
3442
OS << '(';
@@ -48,7 +56,17 @@ void ParsedRawSyntaxNode::dump(llvm::raw_ostream &OS, unsigned Indent) const {
4856
break;
4957
case DataKind::DeferredLayout:
5058
dumpSyntaxKind(OS, getKind());
51-
OS << " [deferred layout]";
59+
OS << " [deferred]";
60+
if (Context) {
61+
size_t numChildren = getDeferredNumChildren(Context);
62+
for (size_t i = 0; i < numChildren; ++i) {
63+
auto child = getDeferredChild(i, Context);
64+
OS << "\n";
65+
child.dump(OS, Context, Indent + 2);
66+
}
67+
} else {
68+
OS << " (unknown children)";
69+
}
5270
break;
5371
case DataKind::DeferredToken:
5472
dumpSyntaxKind(OS, getKind());

lib/Parse/ParsedRawSyntaxRecorder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ ParsedRawSyntaxRecorder::getDeferredChild(const ParsedRawSyntaxNode &parent,
223223
childInfo.IsMissing);
224224
}
225225

226+
size_t ParsedRawSyntaxRecorder::getDeferredNumChildren(
227+
const ParsedRawSyntaxNode &node) const {
228+
assert(node.isDeferredLayout());
229+
return SPActions->getDeferredNumChildren(node.getData());
230+
}
231+
226232
#ifndef NDEBUG
227233
void ParsedRawSyntaxRecorder::verifyElementRanges(ArrayRef<ParsedRawSyntaxNode> elements) {
228234
SourceLoc prevEndLoc;

lib/Parse/SyntaxParseActions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,8 @@ DeferredNodeInfo SyntaxParseActions::getDeferredChild(OpaqueSyntaxNode node,
165165
}
166166
}
167167
}
168+
169+
size_t SyntaxParseActions::getDeferredNumChildren(OpaqueSyntaxNode node) {
170+
auto Data = static_cast<const DeferredLayoutNode *>(node);
171+
return Data->Children.size();
172+
}

0 commit comments

Comments
 (0)