Skip to content

[libSyntax] Miscellaneous minor improvements #36229

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
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
2 changes: 1 addition & 1 deletion include/swift/Parse/ParsedRawSyntaxNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace swift {

typedef void *OpaqueSyntaxNode;
typedef const void *OpaqueSyntaxNode;
class SyntaxParsingContext;

/// Represents a raw syntax node formed by the parser.
Expand Down
2 changes: 0 additions & 2 deletions include/swift/Parse/ParsedRawSyntaxRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class ParsedRawSyntaxRecorder final {
ParsedRawSyntaxNode recordEmptyRawSyntaxCollection(syntax::SyntaxKind kind,
SourceLoc loc);

void discardRecordedNode(ParsedRawSyntaxNode &node);

/// Used for incremental re-parsing.
ParsedRawSyntaxNode lookupNode(size_t lexerOffset, SourceLoc loc,
syntax::SyntaxKind kind);
Expand Down
9 changes: 1 addition & 8 deletions include/swift/Parse/SyntaxParseActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SourceFileSyntax;
enum class SyntaxKind;
}

typedef void *OpaqueSyntaxNode;
typedef const void *OpaqueSyntaxNode;

class SyntaxParseActions {
virtual void _anchor();
Expand Down Expand Up @@ -62,13 +62,6 @@ class SyntaxParseActions {
virtual Optional<syntax::SourceFileSyntax>
realizeSyntaxRoot(OpaqueSyntaxNode root, const SourceFile &SF) = 0;

/// Discard raw syntax node.
///
/// FIXME: This breaks invariant that any recorded node will be a part of the
/// result SourceFile syntax. This method is a temporary workaround, and
/// should be removed when we fully migrate to libSyntax parsing.
virtual void discardRecordedNode(OpaqueSyntaxNode node) = 0;

/// Used for incremental re-parsing.
virtual std::pair<size_t, OpaqueSyntaxNode>
lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace swift {
class IRGenOptions;
class LangOptions;
class ModuleDecl;
typedef void *OpaqueSyntaxNode;
typedef const void *OpaqueSyntaxNode;
class Parser;
class SerializationOptions;
class SILOptions;
Expand Down
27 changes: 20 additions & 7 deletions include/swift/Syntax/SyntaxCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ class SyntaxCollection : public Syntax {
NewLayout.reserve(OldLayout.size() + 1);
std::copy(OldLayout.begin(), OldLayout.end(), back_inserter(NewLayout));
NewLayout.push_back(E.getRaw());
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout, getRaw()->getPresence(), getRaw()->getArena());
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
getRaw()->getPresence(),
getRaw()->getArena());
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
}

Expand All @@ -126,7 +128,9 @@ class SyntaxCollection : public Syntax {
SyntaxCollection<CollectionKind, Element> removingLast() const {
assert(!empty());
auto NewLayout = getRaw()->getLayout().drop_back();
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout, getRaw()->getPresence(), getRaw()->getArena());
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
getRaw()->getPresence(),
getRaw()->getArena());
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
}

Expand All @@ -137,7 +141,9 @@ class SyntaxCollection : public Syntax {
std::vector<const RawSyntax *> NewLayout = {E.getRaw()};
std::copy(OldLayout.begin(), OldLayout.end(),
std::back_inserter(NewLayout));
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout, getRaw()->getPresence(), getRaw()->getArena());
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
getRaw()->getPresence(),
getRaw()->getArena());
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
}

Expand All @@ -147,7 +153,9 @@ class SyntaxCollection : public Syntax {
SyntaxCollection<CollectionKind, Element> removingFirst() const {
assert(!empty());
auto NewLayout = getRaw()->getLayout().drop_front();
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout, getRaw()->getPresence(), getRaw()->getArena());
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
getRaw()->getPresence(),
getRaw()->getArena());
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
}

Expand All @@ -166,7 +174,9 @@ class SyntaxCollection : public Syntax {
NewLayout.push_back(E.getRaw());
std::copy(OldLayout.begin() + i, OldLayout.end(),
std::back_inserter(NewLayout));
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout, getRaw()->getPresence(), getRaw()->getArena());
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
getRaw()->getPresence(),
getRaw()->getArena());
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
}

Expand All @@ -177,13 +187,16 @@ class SyntaxCollection : public Syntax {
auto iterator = NewLayout.begin();
std::advance(iterator, i);
NewLayout.erase(iterator);
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout, getRaw()->getPresence(), getRaw()->getArena());
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
getRaw()->getPresence(),
getRaw()->getArena());
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
}

/// Return an empty syntax collection of this type.
SyntaxCollection<CollectionKind, Element> cleared() const {
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, {}, getRaw()->getPresence(), getRaw()->getArena());
auto Raw = RawSyntax::makeAndCalcLength(
CollectionKind, {}, getRaw()->getPresence(), getRaw()->getArena());
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
}

Expand Down
2 changes: 0 additions & 2 deletions include/swift/SyntaxParse/SyntaxTreeCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class SyntaxTreeCreator: public SyntaxParseActions {
ArrayRef<OpaqueSyntaxNode> elements,
CharSourceRange range) override;

void discardRecordedNode(OpaqueSyntaxNode node) override;

std::pair<size_t, OpaqueSyntaxNode>
lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) override;
};
Expand Down
4 changes: 0 additions & 4 deletions lib/Parse/ParsedRawSyntaxRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ ParsedRawSyntaxRecorder::recordEmptyRawSyntaxCollection(SyntaxKind kind,
return ParsedRawSyntaxNode{kind, tok::unknown, range, n};
}

void ParsedRawSyntaxRecorder::discardRecordedNode(ParsedRawSyntaxNode &node) {
SPActions->discardRecordedNode(node.takeOpaqueNode());
}

ParsedRawSyntaxNode
ParsedRawSyntaxRecorder::lookupNode(size_t lexerOffset, SourceLoc loc,
SyntaxKind kind) {
Expand Down
6 changes: 0 additions & 6 deletions lib/Parse/SyntaxParsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,6 @@ SyntaxParsingContext::~SyntaxParsingContext() {
// Remove all parts in this context.
case AccumulationMode::Discard: {
auto &nodes = getStorage();
for (auto i = nodes.begin()+Offset, e = nodes.end(); i != e; ++i) {
// FIXME: This should not be needed. This breaks invariant that any
// recorded node must be a part of result souce syntax tree.
if (i->isRecorded())
getRecorder().discardRecordedNode(*i);
}
nodes.erase(nodes.begin()+Offset, nodes.end());
break;
}
Expand Down
11 changes: 4 additions & 7 deletions lib/SyntaxParse/SyntaxTreeCreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ OpaqueSyntaxNode SyntaxTreeCreator::recordToken(tok tokenKind,
auto raw =
TokenCache->getToken(Arena, tokenKind, range.getByteLength(), tokenText,
leadingTriviaText, trailingTriviaText);
return static_cast<OpaqueSyntaxNode>(const_cast<RawSyntax *>(raw));
return static_cast<OpaqueSyntaxNode>(raw);
}

OpaqueSyntaxNode
SyntaxTreeCreator::recordMissingToken(tok kind, SourceLoc loc) {
auto raw = RawSyntax::missing(kind, getTokenText(kind), Arena);
return static_cast<OpaqueSyntaxNode>(const_cast<RawSyntax *>(raw));
return static_cast<OpaqueSyntaxNode>(raw);
}

OpaqueSyntaxNode
Expand All @@ -151,7 +151,7 @@ SyntaxTreeCreator::recordRawSyntax(syntax::SyntaxKind kind,
size_t TextLength = range.isValid() ? range.getByteLength() : 0;
auto raw =
RawSyntax::make(kind, parts, TextLength, SourcePresence::Present, Arena);
return static_cast<OpaqueSyntaxNode>(const_cast<RawSyntax *>(raw));
return static_cast<OpaqueSyntaxNode>(raw);
}

std::pair<size_t, OpaqueSyntaxNode>
Expand All @@ -163,8 +163,5 @@ SyntaxTreeCreator::lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) {
return {0, nullptr};
const RawSyntax *raw = cacheLookup->getRaw();
size_t length = raw->getTextLength();
return {length, static_cast<OpaqueSyntaxNode>(const_cast<RawSyntax *>(raw))};
}

void SyntaxTreeCreator::discardRecordedNode(OpaqueSyntaxNode opaqueN) {
return {length, static_cast<OpaqueSyntaxNode>(raw)};
}
9 changes: 3 additions & 6 deletions tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ class CLibParseActions : public SyntaxParseActions {
auto numValue = serialization::getNumericValue(kind);
node.kind = numValue;
assert(node.kind == numValue && "syntax kind value is too large");
node.layout_data.nodes = elements.data();
node.layout_data.nodes =
const_cast<const swiftparse_client_node_t *>(elements.data());
node.layout_data.nodes_count = elements.size();
makeCRange(node.range, range);
node.present = true;
Expand All @@ -196,10 +197,6 @@ class CLibParseActions : public SyntaxParseActions {
return None;
}

void discardRecordedNode(OpaqueSyntaxNode node) override {
// FIXME: This method should not be called at all.
}

std::pair<size_t, OpaqueSyntaxNode>
lookupNode(size_t lexerOffset, SyntaxKind kind) override {
auto NodeLookup = getNodeLookup();
Expand Down Expand Up @@ -302,7 +299,7 @@ swiftparse_client_node_t SynParser::parse(const char *source) {
pConsumer = std::make_unique<SynParserDiagConsumer>(*this, bufID);
PU.getDiagnosticEngine().addConsumer(*pConsumer);
}
return PU.parse();
return const_cast<swiftparse_client_node_t>(PU.parse());
}
}
//===--- C API ------------------------------------------------------------===//
Expand Down