-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[ASTGen] Infrastructure for implementing ASTGen incrementally #69761
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c8d7f2
[ASTGen] Generalize findSyntaxNodeInSourceFile(wantOutermost:true)
rintaro fb08a92
[ASTGen] Infrastructure to implment ASTGen Incrementally
rintaro f3f4b19
[ASTGen] Remove ASTGenTypes experimental feature
rintaro 7c195ef
[ASTGen] Use C briding instead of C++ method
rintaro d51058c
[ASTGen] Disable a test for the same ASAN failure as other ASTGen test
rintaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//===--- ParseBridging.h --------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_PARSE_PARSEBRIDGING_H | ||
#define SWIFT_PARSE_PARSEBRIDGING_H | ||
|
||
#include "swift/AST/ASTBridging.h" | ||
#include "swift/Basic/BasicBridging.h" | ||
|
||
#ifdef USED_IN_CPP_SOURC | ||
#include "swift/Parse/Parser.h" | ||
#else | ||
namespace swift { | ||
class Parser; | ||
} | ||
#endif | ||
|
||
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS | ||
|
||
class BridgedLegacyParser { | ||
swift::Parser *_Nonnull const handle; | ||
|
||
public: | ||
#ifdef USED_IN_CPP_SOURCE | ||
BridgedLegacyParser(swift::Parser &P) : handle(&P) {} | ||
|
||
swift::Parser &unbridged() const { return *handle; } | ||
#endif | ||
}; | ||
|
||
SWIFT_NAME("BridgedLegacyParser.parseExpr(self:_:_:_:)") | ||
BridgedExpr BridgedLegacyParser_parseExpr(BridgedLegacyParser, | ||
BridgedSourceLoc loc, | ||
BridgedDeclContext DC, | ||
bool isExprBasic); | ||
|
||
SWIFT_NAME("BridgedLegacyParser.parseDecl(self:_:_:)") | ||
BridgedDecl BridgedLegacyParser_parseDecl(BridgedLegacyParser, | ||
BridgedSourceLoc loc, | ||
BridgedDeclContext DC); | ||
|
||
SWIFT_NAME("BridgedLegacyParser.parseStmt(self:_:_:)") | ||
BridgedStmt BridgedLegacyParser_parseStmt(BridgedLegacyParser, | ||
BridgedSourceLoc loc, | ||
BridgedDeclContext DC); | ||
|
||
SWIFT_NAME("BridgedLegacyParser.parseType(self:_:_:)") | ||
BridgedTypeRepr BridgedLegacyParser_parseType(BridgedLegacyParser, | ||
BridgedSourceLoc loc, | ||
BridgedDeclContext DC); | ||
|
||
SWIFT_END_NULLABILITY_ANNOTATIONS | ||
|
||
#endif // SWIFT_PARSE_PARSEBRIDGING_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,6 +158,11 @@ class Parser { | |
bool InSwiftKeyPath = false; | ||
bool InFreestandingMacroArgument = false; | ||
|
||
#if SWIFT_BUILD_SWIFT_SYNTAX | ||
// This Parser is a fallback parser for ASTGen. | ||
bool IsForASTGen = false; | ||
#endif | ||
|
||
// A cached answer to | ||
// Context.LangOpts.hasFeature(Feature::NoncopyableGenerics) | ||
// to ensure there's no parsing performance regression. | ||
|
@@ -957,7 +962,8 @@ class Parser { | |
|
||
ParserResult<Decl> parseDecl(bool IsAtStartOfLineOrPreviousHadSemi, | ||
bool IfConfigsAreDeclAttrs, | ||
llvm::function_ref<void(Decl *)> Handler); | ||
llvm::function_ref<void(Decl *)> Handler, | ||
bool fromASTGen = false); | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>> | ||
parseDeclListDelayed(IterableDeclContext *IDC); | ||
|
@@ -1349,50 +1355,6 @@ class Parser { | |
/// Get the location for a type error. | ||
SourceLoc getTypeErrorLoc() const; | ||
|
||
/// Callback function used for creating a C++ AST from the syntax node at the given source location. | ||
/// | ||
/// The arguments to this callback are the source file to pass into ASTGen (the exported source file) | ||
/// and the source location pointer to pass into ASTGen (to find the syntax node). | ||
/// | ||
/// The callback returns the new AST node and the ending location of the syntax node. If the AST node | ||
/// is NULL, something went wrong. | ||
template<typename T> | ||
using ASTFromSyntaxTreeCallback = std::pair<T*, const void *>( | ||
void *sourceFile, const void *sourceLoc | ||
); | ||
|
||
/// Parse by constructing a C++ AST node from the Swift syntax tree via ASTGen. | ||
template<typename T> | ||
ParserResult<T> parseASTFromSyntaxTree( | ||
llvm::function_ref<ASTFromSyntaxTreeCallback<T>> body | ||
) { | ||
if (!Context.LangOpts.hasFeature(Feature::ASTGenTypes)) | ||
return nullptr; | ||
|
||
auto exportedSourceFile = SF.getExportedSourceFile(); | ||
if (!exportedSourceFile) | ||
return nullptr; | ||
|
||
// Perform the translation. | ||
auto sourceLoc = Tok.getLoc().getOpaquePointerValue(); | ||
T* astNode; | ||
const void *endLocPtr; | ||
std::tie(astNode, endLocPtr) = body(exportedSourceFile, sourceLoc); | ||
|
||
if (!astNode) { | ||
assert(false && "Could not build AST node from syntax tree"); | ||
return nullptr; | ||
} | ||
|
||
// Reset the lexer to the ending location. | ||
StringRef contents = | ||
SourceMgr.extractText(SourceMgr.getRangeForBuffer(L->getBufferID())); | ||
L->resetToOffset((const char *)endLocPtr - contents.data()); | ||
L->lex(Tok); | ||
|
||
return makeParserResult(astNode); | ||
} | ||
|
||
//===--------------------------------------------------------------------===// | ||
// Type Parsing | ||
|
||
|
@@ -1409,9 +1371,10 @@ class Parser { | |
ParseTypeReason reason); | ||
|
||
ParserResult<TypeRepr> parseType(); | ||
ParserResult<TypeRepr> parseType( | ||
Diag<> MessageID, | ||
ParseTypeReason reason = ParseTypeReason::Unspecified); | ||
ParserResult<TypeRepr> | ||
parseType(Diag<> MessageID, | ||
ParseTypeReason reason = ParseTypeReason::Unspecified, | ||
bool fromASTGen = false); | ||
|
||
/// Parse a type optionally prefixed by a list of named opaque parameters. If | ||
/// no params present, return 'type'. Otherwise, return 'type-named-opaque'. | ||
|
@@ -1735,7 +1698,8 @@ class Parser { | |
ParserResult<Expr> parseExprBasic(Diag<> ID) { | ||
return parseExprImpl(ID, /*isExprBasic=*/true); | ||
} | ||
ParserResult<Expr> parseExprImpl(Diag<> ID, bool isExprBasic); | ||
ParserResult<Expr> parseExprImpl(Diag<> ID, bool isExprBasic, | ||
bool fromASTGen = false); | ||
ParserResult<Expr> parseExprIs(); | ||
ParserResult<Expr> parseExprAs(); | ||
ParserResult<Expr> parseExprArrow(); | ||
|
@@ -1944,7 +1908,7 @@ class Parser { | |
|
||
bool isTerminatorForBraceItemListKind(BraceItemListKind Kind, | ||
ArrayRef<ASTNode> ParsedDecls); | ||
ParserResult<Stmt> parseStmt(); | ||
ParserResult<Stmt> parseStmt(bool fromASTGen = false); | ||
ParserStatus parseExprOrStmt(ASTNode &Result); | ||
ParserResult<Stmt> parseStmtBreak(); | ||
ParserResult<Stmt> parseStmtContinue(); | ||
|
@@ -2075,6 +2039,18 @@ class Parser { | |
bool parseLegacyTildeCopyable(SourceLoc *parseTildeCopyable, | ||
ParserStatus &Status, | ||
SourceLoc &TildeCopyableLoc); | ||
|
||
//===--------------------------------------------------------------------===// | ||
// ASTGen support. | ||
|
||
/// Parse a TypeRepr from the syntax tree. i.e. SF->getExportedSourceFile() | ||
ParserResult<TypeRepr> parseTypeReprFromSyntaxTree(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just reading the function declaration, I’m thinking: Which syntax tree is this parsing from? I think a doc comment could help explain this (or a need a better name). |
||
/// Parse a Stmt from the syntax tree. i.e. SF->getExportedSourceFile() | ||
ParserResult<Stmt> parseStmtFromSyntaxTree(); | ||
/// Parse a Decl from the syntax tree. i.e. SF->getExportedSourceFile() | ||
ParserResult<Decl> parseDeclFromSyntaxTree(); | ||
/// Parse an Expr from the syntax tree. i.e. SF->getExportedSourceFile() | ||
ParserResult<Expr> parseExprFromSyntaxTree(); | ||
}; | ||
|
||
/// Describes a parsed declaration name. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.