@@ -158,6 +158,11 @@ class Parser {
158158 bool InSwiftKeyPath = false ;
159159 bool InFreestandingMacroArgument = false ;
160160
161+ #if SWIFT_BUILD_SWIFT_SYNTAX
162+ // This Parser is a fallback parser for ASTGen.
163+ bool IsForASTGen = false ;
164+ #endif
165+
161166 // A cached answer to
162167 // Context.LangOpts.hasFeature(Feature::NoncopyableGenerics)
163168 // to ensure there's no parsing performance regression.
@@ -957,7 +962,8 @@ class Parser {
957962
958963 ParserResult<Decl> parseDecl (bool IsAtStartOfLineOrPreviousHadSemi,
959964 bool IfConfigsAreDeclAttrs,
960- llvm::function_ref<void (Decl *)> Handler);
965+ llvm::function_ref<void (Decl *)> Handler,
966+ bool fromASTGen = false);
961967
962968 std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
963969 parseDeclListDelayed (IterableDeclContext *IDC);
@@ -1349,50 +1355,6 @@ class Parser {
13491355 // / Get the location for a type error.
13501356 SourceLoc getTypeErrorLoc () const ;
13511357
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-
13961358 // ===--------------------------------------------------------------------===//
13971359 // Type Parsing
13981360
@@ -1409,9 +1371,10 @@ class Parser {
14091371 ParseTypeReason reason);
14101372
14111373 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 );
14151378
14161379 // / Parse a type optionally prefixed by a list of named opaque parameters. If
14171380 // / no params present, return 'type'. Otherwise, return 'type-named-opaque'.
@@ -1735,7 +1698,8 @@ class Parser {
17351698 ParserResult<Expr> parseExprBasic (Diag<> ID) {
17361699 return parseExprImpl (ID, /* isExprBasic=*/ true );
17371700 }
1738- ParserResult<Expr> parseExprImpl (Diag<> ID, bool isExprBasic);
1701+ ParserResult<Expr> parseExprImpl (Diag<> ID, bool isExprBasic,
1702+ bool fromASTGen = false );
17391703 ParserResult<Expr> parseExprIs ();
17401704 ParserResult<Expr> parseExprAs ();
17411705 ParserResult<Expr> parseExprArrow ();
@@ -1944,7 +1908,7 @@ class Parser {
19441908
19451909 bool isTerminatorForBraceItemListKind (BraceItemListKind Kind,
19461910 ArrayRef<ASTNode> ParsedDecls);
1947- ParserResult<Stmt> parseStmt ();
1911+ ParserResult<Stmt> parseStmt (bool fromASTGen = false );
19481912 ParserStatus parseExprOrStmt (ASTNode &Result);
19491913 ParserResult<Stmt> parseStmtBreak ();
19501914 ParserResult<Stmt> parseStmtContinue ();
@@ -2075,6 +2039,18 @@ class Parser {
20752039 bool parseLegacyTildeCopyable (SourceLoc *parseTildeCopyable,
20762040 ParserStatus &Status,
20772041 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 ();
20782054};
20792055
20802056// / Describes a parsed declaration name.
0 commit comments