Skip to content

[ASTGen] Remove LegacyParser ping-pong mechanism #77619

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
Nov 15, 2024
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
6 changes: 0 additions & 6 deletions include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ module IDEBridging {
export *
}

module ParseBridging {
header "swift/Parse/ParseBridging.h"
requires cplusplus
export *
}

module SILBridging {
header "swift/SIL/SILBridging.h"
requires cplusplus
Expand Down
35 changes: 1 addition & 34 deletions include/swift/Bridging/ASTGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#define SWIFT_BRIDGING_ASTGEN_H

#include "swift/AST/ASTBridging.h"
#include "swift/Parse/ParseBridging.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -59,43 +58,11 @@ int swift_ASTGen_emitParserDiagnostics(
void swift_ASTGen_buildTopLevelASTNodes(
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
BridgedDeclContext declContext, BridgedASTContext astContext,
BridgedLegacyParser legacyParser, void *_Nonnull outputContext,
void *_Nonnull outputContext,
void (*_Nonnull)(BridgedASTNode, void *_Nonnull));

void swift_ASTGen_freeBridgedString(BridgedStringRef);

/// Build a TypeRepr for AST node for the type at the given source location in
/// the specified file.
swift::TypeRepr *_Nullable swift_ASTGen_buildTypeRepr(
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
BridgedSourceLoc *_Nonnull endSourceLoc);

/// Build a Decl for AST node for the type at the given source location in the
/// specified file.
swift::Decl *_Nullable swift_ASTGen_buildDecl(
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
BridgedSourceLoc *_Nonnull endSourceLoc);

/// Build a Expr for AST node for the type at the given source location in the
/// specified file.
swift::Expr *_Nullable swift_ASTGen_buildExpr(
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
BridgedSourceLoc *_Nonnull endSourceLoc);

/// Build a Stmt for AST node for the type at the given source location in the
/// specified file.
swift::Stmt *_Nullable swift_ASTGen_buildStmt(
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
BridgedSourceLoc *_Nonnull endSourceLoc);

// MARK: - Regex parsing

bool swift_ASTGen_lexRegexLiteral(const char *_Nonnull *_Nonnull curPtrPtr,
Expand Down
57 changes: 0 additions & 57 deletions include/swift/Parse/ParseBridging.h

This file was deleted.

33 changes: 7 additions & 26 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ class Parser {
bool InSwiftKeyPath = false;
bool InFreestandingMacroArgument = false;

/// This Parser is a fallback parser for ASTGen.
// Note: This doesn't affect anything in non-SWIFT_BUILD_SWIFT_SYNTAX envs.
bool IsForASTGen = false;

/// Whether we should delay parsing nominal type, extension, and function
/// bodies.
bool isDelayedParsingEnabled() const;
Expand Down Expand Up @@ -957,8 +953,7 @@ class Parser {

ParserStatus parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
bool IfConfigsAreDeclAttrs,
llvm::function_ref<void(Decl *)> Handler,
bool fromASTGen = false);
llvm::function_ref<void(Decl *)> Handler);

std::pair<std::vector<Decl *>, std::optional<Fingerprint>>
parseDeclListDelayed(IterableDeclContext *IDC);
Expand Down Expand Up @@ -1396,8 +1391,7 @@ class Parser {
ParserResult<TypeRepr> parseType();
ParserResult<TypeRepr>
parseType(Diag<> MessageID,
ParseTypeReason reason = ParseTypeReason::Unspecified,
bool fromASTGen = false);
ParseTypeReason reason = ParseTypeReason::Unspecified);

/// Parse a type optionally prefixed by a list of named opaque parameters. If
/// no params present, return 'type'. Otherwise, return 'type-named-opaque'.
Expand All @@ -1413,9 +1407,9 @@ class Parser {
Diag<> MessageID, ParseTypeReason reason);

ParserResult<TypeRepr> parseTypeOrValue();
ParserResult<TypeRepr> parseTypeOrValue(Diag<> MessageID,
ParseTypeReason reason = ParseTypeReason::Unspecified,
bool fromASTGen = false);
ParserResult<TypeRepr>
parseTypeOrValue(Diag<> MessageID,
ParseTypeReason reason = ParseTypeReason::Unspecified);

/// Parse layout constraint.
LayoutConstraint parseLayoutConstraint(Identifier LayoutConstraintID);
Expand Down Expand Up @@ -1975,7 +1969,7 @@ class Parser {

bool isTerminatorForBraceItemListKind(BraceItemListKind Kind,
ArrayRef<ASTNode> ParsedDecls);
ParserResult<Stmt> parseStmt(bool fromASTGen = false);
ParserResult<Stmt> parseStmt();
ParserStatus parseExprOrStmt(ASTNode &Result);
ParserResult<Stmt> parseStmtBreak();
ParserResult<Stmt> parseStmtContinue();
Expand Down Expand Up @@ -2102,20 +2096,7 @@ class Parser {
//===--------------------------------------------------------------------===//
// Code completion second pass.

void performIDEInspectionSecondPassImpl(
IDEInspectionDelayedDeclState &info);

//===--------------------------------------------------------------------===//
// ASTGen support.

/// Parse a TypeRepr from the syntax tree. i.e. SF->getExportedSourceFile()
ParserResult<TypeRepr> parseTypeReprFromSyntaxTree();
/// 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();
void performIDEInspectionSecondPassImpl(IDEInspectionDelayedDeclState &info);
};

/// To assist debugging parser crashes, tell us the location of the
Expand Down
149 changes: 2 additions & 147 deletions lib/ASTGen/Sources/ASTGen/ASTGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import ASTBridging
import BasicBridging
import ParseBridging
import SwiftIfConfig
// Needed to use BumpPtrAllocator
@_spi(BumpPtrAllocator) @_spi(RawSyntax) import SwiftSyntax
Expand Down Expand Up @@ -82,24 +81,18 @@ struct ASTGenVisitor {

fileprivate let allocator: SwiftSyntax.BumpPtrAllocator = .init(initialSlabSize: 256)

/// Fallback legacy parser used when ASTGen doesn't have the generate(_:)
/// implementation for the AST node kind.
let legacyParse: BridgedLegacyParser

init(
diagnosticEngine: BridgedDiagnosticEngine,
sourceBuffer: UnsafeBufferPointer<UInt8>,
declContext: BridgedDeclContext,
astContext: BridgedASTContext,
configuredRegions: ConfiguredRegions,
legacyParser: BridgedLegacyParser
configuredRegions: ConfiguredRegions
) {
self.diagnosticEngine = diagnosticEngine
self.base = sourceBuffer
self.declContext = declContext
self.ctx = astContext
self.configuredRegions = configuredRegions
self.legacyParse = legacyParser
}

func generate(sourceFile node: SourceFileSyntax) -> [ASTNode] {
Expand Down Expand Up @@ -432,7 +425,6 @@ public func buildTopLevelASTNodes(
sourceFilePtr: UnsafeMutableRawPointer,
dc: BridgedDeclContext,
ctx: BridgedASTContext,
legacyParser: BridgedLegacyParser,
outputContext: UnsafeMutableRawPointer,
callback: @convention(c) (BridgedASTNode, UnsafeMutableRawPointer) -> Void
) {
Expand All @@ -442,8 +434,7 @@ public func buildTopLevelASTNodes(
sourceBuffer: sourceFile.pointee.buffer,
declContext: dc,
astContext: ctx,
configuredRegions: sourceFile.pointee.configuredRegions(astContext: ctx),
legacyParser: legacyParser
configuredRegions: sourceFile.pointee.configuredRegions(astContext: ctx)
)

switch sourceFile.pointee.syntax.as(SyntaxEnum.self) {
Expand All @@ -462,139 +453,3 @@ public func buildTopLevelASTNodes(
// Diagnose any errors from evaluating #ifs.
visitor.diagnoseAll(visitor.configuredRegions.diagnostics)
}

/// Generate an AST node at the given source location. Returns the generated
/// ASTNode and mutate the pointee of `endLocPtr` to the end of the node.
private func _build<Node: SyntaxProtocol, Result>(
generator: (ASTGenVisitor) -> (Node) -> Result?,
diagEngine: BridgedDiagnosticEngine,
sourceFilePtr: UnsafeMutableRawPointer,
sourceLoc: BridgedSourceLoc,
declContext: BridgedDeclContext,
astContext: BridgedASTContext,
legacyParser: BridgedLegacyParser,
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
) -> Result? {
let sourceFile = sourceFilePtr.assumingMemoryBound(to: ExportedSourceFile.self)

// Find the type syntax node.
guard
let node = findSyntaxNodeInSourceFile(
sourceFilePtr: sourceFilePtr,
// FIXME: findSyntaxNodeInSourceFile should receive `BridgedSourceLoc`.
sourceLocationPtr: sourceLoc.getOpaquePointerValue()?.assumingMemoryBound(to: UInt8.self),
type: Node.self,
wantOutermost: true
)
else {
// FIXME: Produce an error
return nil
}

// Fill in the end location.
endLocPtr.pointee = sourceLoc.advanced(by: node.totalLength.utf8Length)

// Convert the syntax node.
return generator(
ASTGenVisitor(
diagnosticEngine: diagEngine,
sourceBuffer: sourceFile.pointee.buffer,
declContext: declContext,
astContext: astContext,
configuredRegions: sourceFile.pointee.configuredRegions(astContext: astContext),
legacyParser: legacyParser
)
)(node)
}

@_cdecl("swift_ASTGen_buildTypeRepr")
@usableFromInline
func buildTypeRepr(
diagEngine: BridgedDiagnosticEngine,
sourceFilePtr: UnsafeMutableRawPointer,
sourceLoc: BridgedSourceLoc,
declContext: BridgedDeclContext,
astContext: BridgedASTContext,
legacyParser: BridgedLegacyParser,
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
) -> UnsafeMutableRawPointer? {
return _build(
generator: ASTGenVisitor.generate(type:),
diagEngine: diagEngine,
sourceFilePtr: sourceFilePtr,
sourceLoc: sourceLoc,
declContext: declContext,
astContext: astContext,
legacyParser: legacyParser,
endLocPtr: endLocPtr
)?.raw
}

@_cdecl("swift_ASTGen_buildDecl")
@usableFromInline
func buildDecl(
diagEngine: BridgedDiagnosticEngine,
sourceFilePtr: UnsafeMutableRawPointer,
sourceLoc: BridgedSourceLoc,
declContext: BridgedDeclContext,
astContext: BridgedASTContext,
legacyParser: BridgedLegacyParser,
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
) -> UnsafeMutableRawPointer? {
return _build(
generator: ASTGenVisitor.generate(decl:),
diagEngine: diagEngine,
sourceFilePtr: sourceFilePtr,
sourceLoc: sourceLoc,
declContext: declContext,
astContext: astContext,
legacyParser: legacyParser,
endLocPtr: endLocPtr
)?.raw
}

@_cdecl("swift_ASTGen_buildExpr")
@usableFromInline
func buildExpr(
diagEngine: BridgedDiagnosticEngine,
sourceFilePtr: UnsafeMutableRawPointer,
sourceLoc: BridgedSourceLoc,
declContext: BridgedDeclContext,
astContext: BridgedASTContext,
legacyParser: BridgedLegacyParser,
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
) -> UnsafeMutableRawPointer? {
return _build(
generator: ASTGenVisitor.generate(expr:),
diagEngine: diagEngine,
sourceFilePtr: sourceFilePtr,
sourceLoc: sourceLoc,
declContext: declContext,
astContext: astContext,
legacyParser: legacyParser,
endLocPtr: endLocPtr
)?.raw
}

@_cdecl("swift_ASTGen_buildStmt")
@usableFromInline
func buildStmt(
diagEngine: BridgedDiagnosticEngine,
sourceFilePtr: UnsafeMutableRawPointer,
sourceLoc: BridgedSourceLoc,
declContext: BridgedDeclContext,
astContext: BridgedASTContext,
legacyParser: BridgedLegacyParser,
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
) -> UnsafeMutableRawPointer? {
return _build(
generator: ASTGenVisitor.generate(stmt:),
diagEngine: diagEngine,
sourceFilePtr: sourceFilePtr,
sourceLoc: sourceLoc,
declContext: declContext,
astContext: astContext,
legacyParser: legacyParser,
endLocPtr: endLocPtr
)?.raw
}
1 change: 0 additions & 1 deletion lib/ASTGen/Sources/ASTGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ add_pure_swift_host_library(swiftASTGen STATIC CXX_INTEROP
DiagnosticsBridge.swift
Exprs.swift
Generics.swift
LegacyParse.swift
Literals.swift
ParameterClause.swift
Patterns.swift
Expand Down
Loading