Skip to content

Commit 1fc00ae

Browse files
committed
[ASTGen] Remove LegacyParser ping-pong mechanism
Now that ASTGen should be able to generate most Swift code. Let's remove "legacy parser" call-in, and remove the unhealthy cyclic dependency between lib/Parse and ASTGen.
1 parent 03319e5 commit 1fc00ae

19 files changed

+23
-667
lines changed

include/module.modulemap

-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ module IDEBridging {
1616
export *
1717
}
1818

19-
module ParseBridging {
20-
header "swift/Parse/ParseBridging.h"
21-
requires cplusplus
22-
export *
23-
}
24-
2519
module SILBridging {
2620
header "swift/SIL/SILBridging.h"
2721
requires cplusplus

include/swift/Bridging/ASTGen.h

+1-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define SWIFT_BRIDGING_ASTGEN_H
1515

1616
#include "swift/AST/ASTBridging.h"
17-
#include "swift/Parse/ParseBridging.h"
1817

1918
#ifdef __cplusplus
2019
extern "C" {
@@ -59,43 +58,11 @@ int swift_ASTGen_emitParserDiagnostics(
5958
void swift_ASTGen_buildTopLevelASTNodes(
6059
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
6160
BridgedDeclContext declContext, BridgedASTContext astContext,
62-
BridgedLegacyParser legacyParser, void *_Nonnull outputContext,
61+
void *_Nonnull outputContext,
6362
void (*_Nonnull)(BridgedASTNode, void *_Nonnull));
6463

6564
void swift_ASTGen_freeBridgedString(BridgedStringRef);
6665

67-
/// Build a TypeRepr for AST node for the type at the given source location in
68-
/// the specified file.
69-
swift::TypeRepr *_Nullable swift_ASTGen_buildTypeRepr(
70-
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
71-
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
72-
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
73-
BridgedSourceLoc *_Nonnull endSourceLoc);
74-
75-
/// Build a Decl for AST node for the type at the given source location in the
76-
/// specified file.
77-
swift::Decl *_Nullable swift_ASTGen_buildDecl(
78-
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
79-
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
80-
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
81-
BridgedSourceLoc *_Nonnull endSourceLoc);
82-
83-
/// Build a Expr for AST node for the type at the given source location in the
84-
/// specified file.
85-
swift::Expr *_Nullable swift_ASTGen_buildExpr(
86-
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
87-
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
88-
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
89-
BridgedSourceLoc *_Nonnull endSourceLoc);
90-
91-
/// Build a Stmt for AST node for the type at the given source location in the
92-
/// specified file.
93-
swift::Stmt *_Nullable swift_ASTGen_buildStmt(
94-
BridgedDiagnosticEngine diagEngine, void *_Nonnull sourceFile,
95-
BridgedSourceLoc sourceLoc, BridgedDeclContext declContext,
96-
BridgedASTContext astContext, BridgedLegacyParser legacyParser,
97-
BridgedSourceLoc *_Nonnull endSourceLoc);
98-
9966
// MARK: - Regex parsing
10067

10168
bool swift_ASTGen_lexRegexLiteral(const char *_Nonnull *_Nonnull curPtrPtr,

include/swift/Parse/ParseBridging.h

-57
This file was deleted.

include/swift/Parse/Parser.h

+7-26
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ class Parser {
169169
bool InSwiftKeyPath = false;
170170
bool InFreestandingMacroArgument = false;
171171

172-
/// This Parser is a fallback parser for ASTGen.
173-
// Note: This doesn't affect anything in non-SWIFT_BUILD_SWIFT_SYNTAX envs.
174-
bool IsForASTGen = false;
175-
176172
/// Whether we should delay parsing nominal type, extension, and function
177173
/// bodies.
178174
bool isDelayedParsingEnabled() const;
@@ -957,8 +953,7 @@ class Parser {
957953

958954
ParserStatus parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
959955
bool IfConfigsAreDeclAttrs,
960-
llvm::function_ref<void(Decl *)> Handler,
961-
bool fromASTGen = false);
956+
llvm::function_ref<void(Decl *)> Handler);
962957

963958
std::pair<std::vector<Decl *>, std::optional<Fingerprint>>
964959
parseDeclListDelayed(IterableDeclContext *IDC);
@@ -1396,8 +1391,7 @@ class Parser {
13961391
ParserResult<TypeRepr> parseType();
13971392
ParserResult<TypeRepr>
13981393
parseType(Diag<> MessageID,
1399-
ParseTypeReason reason = ParseTypeReason::Unspecified,
1400-
bool fromASTGen = false);
1394+
ParseTypeReason reason = ParseTypeReason::Unspecified);
14011395

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

14151409
ParserResult<TypeRepr> parseTypeOrValue();
1416-
ParserResult<TypeRepr> parseTypeOrValue(Diag<> MessageID,
1417-
ParseTypeReason reason = ParseTypeReason::Unspecified,
1418-
bool fromASTGen = false);
1410+
ParserResult<TypeRepr>
1411+
parseTypeOrValue(Diag<> MessageID,
1412+
ParseTypeReason reason = ParseTypeReason::Unspecified);
14191413

14201414
/// Parse layout constraint.
14211415
LayoutConstraint parseLayoutConstraint(Identifier LayoutConstraintID);
@@ -1975,7 +1969,7 @@ class Parser {
19751969

19761970
bool isTerminatorForBraceItemListKind(BraceItemListKind Kind,
19771971
ArrayRef<ASTNode> ParsedDecls);
1978-
ParserResult<Stmt> parseStmt(bool fromASTGen = false);
1972+
ParserResult<Stmt> parseStmt();
19791973
ParserStatus parseExprOrStmt(ASTNode &Result);
19801974
ParserResult<Stmt> parseStmtBreak();
19811975
ParserResult<Stmt> parseStmtContinue();
@@ -2102,20 +2096,7 @@ class Parser {
21022096
//===--------------------------------------------------------------------===//
21032097
// Code completion second pass.
21042098

2105-
void performIDEInspectionSecondPassImpl(
2106-
IDEInspectionDelayedDeclState &info);
2107-
2108-
//===--------------------------------------------------------------------===//
2109-
// ASTGen support.
2110-
2111-
/// Parse a TypeRepr from the syntax tree. i.e. SF->getExportedSourceFile()
2112-
ParserResult<TypeRepr> parseTypeReprFromSyntaxTree();
2113-
/// Parse a Stmt from the syntax tree. i.e. SF->getExportedSourceFile()
2114-
ParserResult<Stmt> parseStmtFromSyntaxTree();
2115-
/// Parse a Decl from the syntax tree. i.e. SF->getExportedSourceFile()
2116-
ParserResult<Decl> parseDeclFromSyntaxTree();
2117-
/// Parse an Expr from the syntax tree. i.e. SF->getExportedSourceFile()
2118-
ParserResult<Expr> parseExprFromSyntaxTree();
2099+
void performIDEInspectionSecondPassImpl(IDEInspectionDelayedDeclState &info);
21192100
};
21202101

21212102
/// To assist debugging parser crashes, tell us the location of the

lib/ASTGen/Sources/ASTGen/ASTGen.swift

+2-147
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import ASTBridging
1414
import BasicBridging
15-
import ParseBridging
1615
import SwiftIfConfig
1716
// Needed to use BumpPtrAllocator
1817
@_spi(BumpPtrAllocator) @_spi(RawSyntax) import SwiftSyntax
@@ -82,24 +81,18 @@ struct ASTGenVisitor {
8281

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

85-
/// Fallback legacy parser used when ASTGen doesn't have the generate(_:)
86-
/// implementation for the AST node kind.
87-
let legacyParse: BridgedLegacyParser
88-
8984
init(
9085
diagnosticEngine: BridgedDiagnosticEngine,
9186
sourceBuffer: UnsafeBufferPointer<UInt8>,
9287
declContext: BridgedDeclContext,
9388
astContext: BridgedASTContext,
94-
configuredRegions: ConfiguredRegions,
95-
legacyParser: BridgedLegacyParser
89+
configuredRegions: ConfiguredRegions
9690
) {
9791
self.diagnosticEngine = diagnosticEngine
9892
self.base = sourceBuffer
9993
self.declContext = declContext
10094
self.ctx = astContext
10195
self.configuredRegions = configuredRegions
102-
self.legacyParse = legacyParser
10396
}
10497

10598
func generate(sourceFile node: SourceFileSyntax) -> [ASTNode] {
@@ -432,7 +425,6 @@ public func buildTopLevelASTNodes(
432425
sourceFilePtr: UnsafeMutableRawPointer,
433426
dc: BridgedDeclContext,
434427
ctx: BridgedASTContext,
435-
legacyParser: BridgedLegacyParser,
436428
outputContext: UnsafeMutableRawPointer,
437429
callback: @convention(c) (BridgedASTNode, UnsafeMutableRawPointer) -> Void
438430
) {
@@ -442,8 +434,7 @@ public func buildTopLevelASTNodes(
442434
sourceBuffer: sourceFile.pointee.buffer,
443435
declContext: dc,
444436
astContext: ctx,
445-
configuredRegions: sourceFile.pointee.configuredRegions(astContext: ctx),
446-
legacyParser: legacyParser
437+
configuredRegions: sourceFile.pointee.configuredRegions(astContext: ctx)
447438
)
448439

449440
switch sourceFile.pointee.syntax.as(SyntaxEnum.self) {
@@ -462,139 +453,3 @@ public func buildTopLevelASTNodes(
462453
// Diagnose any errors from evaluating #ifs.
463454
visitor.diagnoseAll(visitor.configuredRegions.diagnostics)
464455
}
465-
466-
/// Generate an AST node at the given source location. Returns the generated
467-
/// ASTNode and mutate the pointee of `endLocPtr` to the end of the node.
468-
private func _build<Node: SyntaxProtocol, Result>(
469-
generator: (ASTGenVisitor) -> (Node) -> Result?,
470-
diagEngine: BridgedDiagnosticEngine,
471-
sourceFilePtr: UnsafeMutableRawPointer,
472-
sourceLoc: BridgedSourceLoc,
473-
declContext: BridgedDeclContext,
474-
astContext: BridgedASTContext,
475-
legacyParser: BridgedLegacyParser,
476-
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
477-
) -> Result? {
478-
let sourceFile = sourceFilePtr.assumingMemoryBound(to: ExportedSourceFile.self)
479-
480-
// Find the type syntax node.
481-
guard
482-
let node = findSyntaxNodeInSourceFile(
483-
sourceFilePtr: sourceFilePtr,
484-
// FIXME: findSyntaxNodeInSourceFile should receive `BridgedSourceLoc`.
485-
sourceLocationPtr: sourceLoc.getOpaquePointerValue()?.assumingMemoryBound(to: UInt8.self),
486-
type: Node.self,
487-
wantOutermost: true
488-
)
489-
else {
490-
// FIXME: Produce an error
491-
return nil
492-
}
493-
494-
// Fill in the end location.
495-
endLocPtr.pointee = sourceLoc.advanced(by: node.totalLength.utf8Length)
496-
497-
// Convert the syntax node.
498-
return generator(
499-
ASTGenVisitor(
500-
diagnosticEngine: diagEngine,
501-
sourceBuffer: sourceFile.pointee.buffer,
502-
declContext: declContext,
503-
astContext: astContext,
504-
configuredRegions: sourceFile.pointee.configuredRegions(astContext: astContext),
505-
legacyParser: legacyParser
506-
)
507-
)(node)
508-
}
509-
510-
@_cdecl("swift_ASTGen_buildTypeRepr")
511-
@usableFromInline
512-
func buildTypeRepr(
513-
diagEngine: BridgedDiagnosticEngine,
514-
sourceFilePtr: UnsafeMutableRawPointer,
515-
sourceLoc: BridgedSourceLoc,
516-
declContext: BridgedDeclContext,
517-
astContext: BridgedASTContext,
518-
legacyParser: BridgedLegacyParser,
519-
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
520-
) -> UnsafeMutableRawPointer? {
521-
return _build(
522-
generator: ASTGenVisitor.generate(type:),
523-
diagEngine: diagEngine,
524-
sourceFilePtr: sourceFilePtr,
525-
sourceLoc: sourceLoc,
526-
declContext: declContext,
527-
astContext: astContext,
528-
legacyParser: legacyParser,
529-
endLocPtr: endLocPtr
530-
)?.raw
531-
}
532-
533-
@_cdecl("swift_ASTGen_buildDecl")
534-
@usableFromInline
535-
func buildDecl(
536-
diagEngine: BridgedDiagnosticEngine,
537-
sourceFilePtr: UnsafeMutableRawPointer,
538-
sourceLoc: BridgedSourceLoc,
539-
declContext: BridgedDeclContext,
540-
astContext: BridgedASTContext,
541-
legacyParser: BridgedLegacyParser,
542-
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
543-
) -> UnsafeMutableRawPointer? {
544-
return _build(
545-
generator: ASTGenVisitor.generate(decl:),
546-
diagEngine: diagEngine,
547-
sourceFilePtr: sourceFilePtr,
548-
sourceLoc: sourceLoc,
549-
declContext: declContext,
550-
astContext: astContext,
551-
legacyParser: legacyParser,
552-
endLocPtr: endLocPtr
553-
)?.raw
554-
}
555-
556-
@_cdecl("swift_ASTGen_buildExpr")
557-
@usableFromInline
558-
func buildExpr(
559-
diagEngine: BridgedDiagnosticEngine,
560-
sourceFilePtr: UnsafeMutableRawPointer,
561-
sourceLoc: BridgedSourceLoc,
562-
declContext: BridgedDeclContext,
563-
astContext: BridgedASTContext,
564-
legacyParser: BridgedLegacyParser,
565-
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
566-
) -> UnsafeMutableRawPointer? {
567-
return _build(
568-
generator: ASTGenVisitor.generate(expr:),
569-
diagEngine: diagEngine,
570-
sourceFilePtr: sourceFilePtr,
571-
sourceLoc: sourceLoc,
572-
declContext: declContext,
573-
astContext: astContext,
574-
legacyParser: legacyParser,
575-
endLocPtr: endLocPtr
576-
)?.raw
577-
}
578-
579-
@_cdecl("swift_ASTGen_buildStmt")
580-
@usableFromInline
581-
func buildStmt(
582-
diagEngine: BridgedDiagnosticEngine,
583-
sourceFilePtr: UnsafeMutableRawPointer,
584-
sourceLoc: BridgedSourceLoc,
585-
declContext: BridgedDeclContext,
586-
astContext: BridgedASTContext,
587-
legacyParser: BridgedLegacyParser,
588-
endLocPtr: UnsafeMutablePointer<BridgedSourceLoc>
589-
) -> UnsafeMutableRawPointer? {
590-
return _build(
591-
generator: ASTGenVisitor.generate(stmt:),
592-
diagEngine: diagEngine,
593-
sourceFilePtr: sourceFilePtr,
594-
sourceLoc: sourceLoc,
595-
declContext: declContext,
596-
astContext: astContext,
597-
legacyParser: legacyParser,
598-
endLocPtr: endLocPtr
599-
)?.raw
600-
}

lib/ASTGen/Sources/ASTGen/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ add_pure_swift_host_library(swiftASTGen STATIC CXX_INTEROP
99
DiagnosticsBridge.swift
1010
Exprs.swift
1111
Generics.swift
12-
LegacyParse.swift
1312
Literals.swift
1413
ParameterClause.swift
1514
Patterns.swift

0 commit comments

Comments
 (0)