Skip to content

Commit 7265f3e

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 97eda9a commit 7265f3e

18 files changed

+25
-667
lines changed

include/module.modulemap

Lines changed: 0 additions & 6 deletions
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

Lines changed: 1 addition & 34 deletions
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

Lines changed: 0 additions & 57 deletions
This file was deleted.

include/swift/Parse/Parser.h

Lines changed: 7 additions & 26 deletions
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

Lines changed: 2 additions & 147 deletions
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
@@ -81,24 +80,18 @@ struct ASTGenVisitor {
8180

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

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

10497
func generate(sourceFile node: SourceFileSyntax) -> [ASTNode] {
@@ -431,7 +424,6 @@ public func buildTopLevelASTNodes(
431424
sourceFilePtr: UnsafeMutableRawPointer,
432425
dc: BridgedDeclContext,
433426
ctx: BridgedASTContext,
434-
legacyParser: BridgedLegacyParser,
435427
outputContext: UnsafeMutableRawPointer,
436428
callback: @convention(c) (BridgedASTNode, UnsafeMutableRawPointer) -> Void
437429
) {
@@ -441,8 +433,7 @@ public func buildTopLevelASTNodes(
441433
sourceBuffer: sourceFile.pointee.buffer,
442434
declContext: dc,
443435
astContext: ctx,
444-
configuredRegions: sourceFile.pointee.configuredRegions(astContext: ctx),
445-
legacyParser: legacyParser
436+
configuredRegions: sourceFile.pointee.configuredRegions(astContext: ctx)
446437
)
447438

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

0 commit comments

Comments
 (0)