Skip to content

[libSyntax] Store Range of ParsedRawSyntaxNode in dedicated field #36274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 20 additions & 46 deletions include/swift/Parse/ParsedRawSyntaxNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ class ParsedRawSyntaxNode {

struct RecordedSyntaxNode {
OpaqueSyntaxNode OpaqueNode;
CharSourceRange Range;
};
struct DeferredLayoutNode {
MutableArrayRef<ParsedRawSyntaxNode> Children;
CharSourceRange Range;
};
struct DeferredTokenNode {
SourceLoc TokLoc;
Expand All @@ -68,6 +66,8 @@ class ParsedRawSyntaxNode {
DeferredLayoutNode DeferredLayout;
DeferredTokenNode DeferredToken;
};
/// The range of this node, including trivia.
CharSourceRange Range;
uint16_t SynKind;
uint16_t TokKind;
DataKind DK;
Expand All @@ -76,15 +76,17 @@ class ParsedRawSyntaxNode {

ParsedRawSyntaxNode(syntax::SyntaxKind k, CharSourceRange r,
MutableArrayRef<ParsedRawSyntaxNode> deferredNodes)
: DeferredLayout({deferredNodes, r}),
SynKind(uint16_t(k)), TokKind(uint16_t(tok::unknown)),
DK(DataKind::DeferredLayout) {
: DeferredLayout({deferredNodes}), Range(r), SynKind(uint16_t(k)),
TokKind(uint16_t(tok::unknown)), DK(DataKind::DeferredLayout) {
assert(getKind() == k && "Syntax kind with too large value!");
}

ParsedRawSyntaxNode(tok tokKind, SourceLoc tokLoc, unsigned tokLength,
StringRef leadingTrivia, StringRef trailingTrivia)
: DeferredToken{tokLoc, tokLength, leadingTrivia, trailingTrivia},
Range{tokLoc.getAdvancedLoc(-leadingTrivia.size()),
(unsigned)leadingTrivia.size() + tokLength +
(unsigned)trailingTrivia.size()},
SynKind(uint16_t(syntax::SyntaxKind::Token)),
TokKind(uint16_t(tokKind)), DK(DataKind::DeferredToken) {
assert(getTokenKind() == tokKind && "Token kind is too large value!");
Expand All @@ -94,19 +96,14 @@ class ParsedRawSyntaxNode {

public:
ParsedRawSyntaxNode()
: RecordedData{},
SynKind(uint16_t(syntax::SyntaxKind::Unknown)),
TokKind(uint16_t(tok::unknown)),
DK(DataKind::Null) {
}

ParsedRawSyntaxNode(syntax::SyntaxKind k, tok tokKind,
CharSourceRange r, OpaqueSyntaxNode n,
bool IsMissing = false)
: RecordedData{n, r},
SynKind(uint16_t(k)), TokKind(uint16_t(tokKind)),
DK(DataKind::Recorded),
IsMissing(IsMissing) {
: RecordedData{}, Range(), SynKind(uint16_t(syntax::SyntaxKind::Unknown)),
TokKind(uint16_t(tok::unknown)), DK(DataKind::Null) {}

ParsedRawSyntaxNode(syntax::SyntaxKind k, tok tokKind, CharSourceRange r,
OpaqueSyntaxNode n, bool IsMissing = false)
: RecordedData{n}, Range(r), SynKind(uint16_t(k)),
TokKind(uint16_t(tokKind)), DK(DataKind::Recorded),
IsMissing(IsMissing) {
assert(getKind() == k && "Syntax kind with too large value!");
assert(getTokenKind() == tokKind && "Token kind with too large value!");
}
Expand Down Expand Up @@ -138,6 +135,7 @@ class ParsedRawSyntaxNode {
DeferredToken = std::move(other.DeferredToken);
break;
}
Range = std::move(other.Range);
SynKind = std::move(other.SynKind);
TokKind = std::move(other.TokKind);
DK = std::move(other.DK);
Expand Down Expand Up @@ -196,30 +194,19 @@ class ParsedRawSyntaxNode {
case DataKind::Null:
break;
}
copy.Range = Range;
copy.SynKind = SynKind;
copy.TokKind = TokKind;
copy.DK = DK;
copy.IsMissing = IsMissing;
return copy;
}

CharSourceRange getDeferredRange() const {
switch (DK) {
case DataKind::DeferredLayout:
return getDeferredLayoutRange();
case DataKind::DeferredToken:
return getDeferredTokenRangeWithTrivia();
default:
llvm_unreachable("node not deferred");
}
}
/// Returns the range of this node including leading and trailing trivia.
CharSourceRange getRange() const { return Range; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you are here, please add a comment that this includes trivia ranges.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll do.


// Recorded Data ===========================================================//

CharSourceRange getRecordedRange() const {
assert(isRecorded());
return RecordedData.Range;
}
const OpaqueSyntaxNode &getOpaqueNode() const {
assert(isRecorded());
return RecordedData.OpaqueNode;
Expand All @@ -233,10 +220,6 @@ class ParsedRawSyntaxNode {

// Deferred Layout Data ====================================================//

CharSourceRange getDeferredLayoutRange() const {
assert(DK == DataKind::DeferredLayout);
return DeferredLayout.Range;
}
ArrayRef<ParsedRawSyntaxNode> getDeferredChildren() const {
assert(DK == DataKind::DeferredLayout);
return DeferredLayout.Children;
Expand All @@ -259,6 +242,7 @@ class ParsedRawSyntaxNode {
default:
llvm_unreachable("node not deferred");
}
copy.Range = Range;
copy.SynKind = SynKind;
copy.TokKind = TokKind;
copy.DK = DK;
Expand All @@ -268,16 +252,6 @@ class ParsedRawSyntaxNode {

// Deferred Token Data =====================================================//

CharSourceRange getDeferredTokenRangeWithTrivia() const {
assert(DK == DataKind::DeferredToken);
auto leadTriviaLen = DeferredToken.LeadingTrivia.size();
auto trailTriviaLen = DeferredToken.TrailingTrivia.size();

SourceLoc begin = DeferredToken.TokLoc.getAdvancedLoc(-leadTriviaLen);
unsigned len = leadTriviaLen + DeferredToken.TokLength + trailTriviaLen;

return CharSourceRange{begin, len};
}
CharSourceRange getDeferredTokenRange() const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, this getDeferredTokenRange()(doesn't include trivia) is used only in ParsedRawSyntaxRecorder and it's just for calculating the range with trivia after all. Can we abolish this method? If you prefer not to remove this, please add a comment to clarify this does not include trivia ranges.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’ll be gone in the next PR (here). I know that this splitting between PRs sometimes feels a little bit random. Sorry about that.

assert(DK == DataKind::DeferredToken);
return CharSourceRange{DeferredToken.TokLoc, DeferredToken.TokLength};
Expand Down
10 changes: 4 additions & 6 deletions lib/Parse/ParsedRawSyntaxRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ ParsedRawSyntaxRecorder::recordRawSyntax(SyntaxKind kind,
if (subnode.isNull()) {
subnodes.push_back(nullptr);
} else if (subnode.isRecorded()) {
localRange = subnode.getRecordedRange();
localRange = subnode.getRange();
subnodes.push_back(subnode.takeOpaqueNode());
} else {
auto recorded = getRecordedNode(subnode.copyDeferred(), *this);
localRange = recorded.getRecordedRange();
localRange = recorded.getRange();
subnodes.push_back(recorded.takeOpaqueNode());
}

Expand Down Expand Up @@ -131,7 +131,7 @@ ParsedRawSyntaxNode ParsedRawSyntaxRecorder::makeDeferred(
for (auto &node : deferredNodes) {
// Cached range.
if (!node.isNull() && !node.isMissing()) {
auto nodeRange = node.getDeferredRange();
auto nodeRange = node.getRange();
if (nodeRange.isValid()) {
if (range.isInvalid())
range = nodeRange;
Expand Down Expand Up @@ -185,9 +185,7 @@ void ParsedRawSyntaxRecorder::verifyElementRanges(ArrayRef<ParsedRawSyntaxNode>
for (const auto &elem: elements) {
if (elem.isMissing() || elem.isNull())
continue;
CharSourceRange range = elem.isRecorded()
? elem.getRecordedRange()
: elem.getDeferredRange();
CharSourceRange range = elem.getRange();
if (range.isValid()) {
assert((prevEndLoc.isInvalid() || range.getStart() == prevEndLoc)
&& "Non-contiguous child ranges?");
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/SyntaxParsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ size_t SyntaxParsingContext::lookupNode(size_t LexerOffset, SourceLoc Loc) {
return 0;
}
Mode = AccumulationMode::SkippedForIncrementalUpdate;
auto length = foundNode.getRecordedRange().getByteLength();
auto length = foundNode.getRange().getByteLength();
getStorage().push_back(std::move(foundNode));
return length;
}
Expand Down