@@ -158,6 +158,11 @@ class Parser {
158
158
bool InSwiftKeyPath = false ;
159
159
bool InFreestandingMacroArgument = false ;
160
160
161
+ #if SWIFT_BUILD_SWIFT_SYNTAX
162
+ // This Parser is a fallback parser for ASTGen.
163
+ bool IsForASTGen = false ;
164
+ #endif
165
+
161
166
// A cached answer to
162
167
// Context.LangOpts.hasFeature(Feature::NoncopyableGenerics)
163
168
// to ensure there's no parsing performance regression.
@@ -957,7 +962,8 @@ class Parser {
957
962
958
963
ParserResult<Decl> parseDecl (bool IsAtStartOfLineOrPreviousHadSemi,
959
964
bool IfConfigsAreDeclAttrs,
960
- llvm::function_ref<void (Decl *)> Handler);
965
+ llvm::function_ref<void (Decl *)> Handler,
966
+ bool fromASTGen = false);
961
967
962
968
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
963
969
parseDeclListDelayed (IterableDeclContext *IDC);
@@ -1349,50 +1355,6 @@ class Parser {
1349
1355
// / Get the location for a type error.
1350
1356
SourceLoc getTypeErrorLoc () const ;
1351
1357
1352
- // / Callback function used for creating a C++ AST from the syntax node at the given source location.
1353
- // /
1354
- // / The arguments to this callback are the source file to pass into ASTGen (the exported source file)
1355
- // / and the source location pointer to pass into ASTGen (to find the syntax node).
1356
- // /
1357
- // / The callback returns the new AST node and the ending location of the syntax node. If the AST node
1358
- // / is NULL, something went wrong.
1359
- template <typename T>
1360
- using ASTFromSyntaxTreeCallback = std::pair<T*, const void *>(
1361
- void *sourceFile, const void *sourceLoc
1362
- );
1363
-
1364
- // / Parse by constructing a C++ AST node from the Swift syntax tree via ASTGen.
1365
- template <typename T>
1366
- ParserResult<T> parseASTFromSyntaxTree (
1367
- llvm::function_ref<ASTFromSyntaxTreeCallback<T>> body
1368
- ) {
1369
- if (!Context.LangOpts .hasFeature (Feature::ASTGenTypes))
1370
- return nullptr ;
1371
-
1372
- auto exportedSourceFile = SF.getExportedSourceFile ();
1373
- if (!exportedSourceFile)
1374
- return nullptr ;
1375
-
1376
- // Perform the translation.
1377
- auto sourceLoc = Tok.getLoc ().getOpaquePointerValue ();
1378
- T* astNode;
1379
- const void *endLocPtr;
1380
- std::tie (astNode, endLocPtr) = body (exportedSourceFile, sourceLoc);
1381
-
1382
- if (!astNode) {
1383
- assert (false && " Could not build AST node from syntax tree" );
1384
- return nullptr ;
1385
- }
1386
-
1387
- // Reset the lexer to the ending location.
1388
- StringRef contents =
1389
- SourceMgr.extractText (SourceMgr.getRangeForBuffer (L->getBufferID ()));
1390
- L->resetToOffset ((const char *)endLocPtr - contents.data ());
1391
- L->lex (Tok);
1392
-
1393
- return makeParserResult (astNode);
1394
- }
1395
-
1396
1358
// ===--------------------------------------------------------------------===//
1397
1359
// Type Parsing
1398
1360
@@ -1409,9 +1371,10 @@ class Parser {
1409
1371
ParseTypeReason reason);
1410
1372
1411
1373
ParserResult<TypeRepr> parseType ();
1412
- ParserResult<TypeRepr> parseType (
1413
- Diag<> MessageID,
1414
- ParseTypeReason reason = ParseTypeReason::Unspecified);
1374
+ ParserResult<TypeRepr>
1375
+ parseType (Diag<> MessageID,
1376
+ ParseTypeReason reason = ParseTypeReason::Unspecified,
1377
+ bool fromASTGen = false );
1415
1378
1416
1379
// / Parse a type optionally prefixed by a list of named opaque parameters. If
1417
1380
// / no params present, return 'type'. Otherwise, return 'type-named-opaque'.
@@ -1735,7 +1698,8 @@ class Parser {
1735
1698
ParserResult<Expr> parseExprBasic (Diag<> ID) {
1736
1699
return parseExprImpl (ID, /* isExprBasic=*/ true );
1737
1700
}
1738
- ParserResult<Expr> parseExprImpl (Diag<> ID, bool isExprBasic);
1701
+ ParserResult<Expr> parseExprImpl (Diag<> ID, bool isExprBasic,
1702
+ bool fromASTGen = false );
1739
1703
ParserResult<Expr> parseExprIs ();
1740
1704
ParserResult<Expr> parseExprAs ();
1741
1705
ParserResult<Expr> parseExprArrow ();
@@ -1944,7 +1908,7 @@ class Parser {
1944
1908
1945
1909
bool isTerminatorForBraceItemListKind (BraceItemListKind Kind,
1946
1910
ArrayRef<ASTNode> ParsedDecls);
1947
- ParserResult<Stmt> parseStmt ();
1911
+ ParserResult<Stmt> parseStmt (bool fromASTGen = false );
1948
1912
ParserStatus parseExprOrStmt (ASTNode &Result);
1949
1913
ParserResult<Stmt> parseStmtBreak ();
1950
1914
ParserResult<Stmt> parseStmtContinue ();
@@ -2075,6 +2039,18 @@ class Parser {
2075
2039
bool parseLegacyTildeCopyable (SourceLoc *parseTildeCopyable,
2076
2040
ParserStatus &Status,
2077
2041
SourceLoc &TildeCopyableLoc);
2042
+
2043
+ // ===--------------------------------------------------------------------===//
2044
+ // ASTGen support.
2045
+
2046
+ // / Parse a TypeRepr from the syntax tree. i.e. SF->getExportedSourceFile()
2047
+ ParserResult<TypeRepr> parseTypeReprFromSyntaxTree ();
2048
+ // / Parse a Stmt from the syntax tree. i.e. SF->getExportedSourceFile()
2049
+ ParserResult<Stmt> parseStmtFromSyntaxTree ();
2050
+ // / Parse a Decl from the syntax tree. i.e. SF->getExportedSourceFile()
2051
+ ParserResult<Decl> parseDeclFromSyntaxTree ();
2052
+ // / Parse an Expr from the syntax tree. i.e. SF->getExportedSourceFile()
2053
+ ParserResult<Expr> parseExprFromSyntaxTree ();
2078
2054
};
2079
2055
2080
2056
// / Describes a parsed declaration name.
0 commit comments