diff --git a/include/swift/AST/ASTContext.h b/include/swift/AST/ASTContext.h index 1e83d8097ad1c..9ec62c0be1e52 100644 --- a/include/swift/AST/ASTContext.h +++ b/include/swift/AST/ASTContext.h @@ -144,10 +144,6 @@ namespace rewriting { class RewriteContext; } -namespace syntax { - class SyntaxArena; -} - namespace ide { class TypeCheckCompletionCallback; } @@ -498,9 +494,6 @@ class ASTContext final { setVector.size()); } - /// Retrieve the syntax node memory manager for this context. - llvm::IntrusiveRefCntPtr getSyntaxArena() const; - /// Set a new stats reporter. void setStatsReporter(UnifiedStatsReporter *stats); diff --git a/include/swift/AST/CMakeLists.txt b/include/swift/AST/CMakeLists.txt index 7058da0bc320c..384e192ce167f 100644 --- a/include/swift/AST/CMakeLists.txt +++ b/include/swift/AST/CMakeLists.txt @@ -5,7 +5,8 @@ else() endif() set(generated_include_sources - Attr.def.gyb) + Attr.def.gyb + TokenKinds.def.gyb) add_gyb_target(swift-ast-generated-headers "${generated_include_sources}") diff --git a/include/swift/AST/Decl.h b/include/swift/AST/Decl.h index 0d2721caa078b..72f45d8004249 100644 --- a/include/swift/AST/Decl.h +++ b/include/swift/AST/Decl.h @@ -6769,8 +6769,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl { // FIXME: Remove 'Parsed' from this list once we can always delay // parsing bodies. The -experimental-skip-*-function-bodies options // do currently skip parsing, unless disabled through other means in - // SourceFile::hasDelayedBodyParsing (eg. needing to build the full - // syntax tree due to -verify-syntax-tree). + // SourceFile::hasDelayedBodyParsing. assert(getBodyKind() == BodyKind::None || getBodyKind() == BodyKind::Unparsed || getBodyKind() == BodyKind::Parsed); diff --git a/include/swift/AST/Expr.h b/include/swift/AST/Expr.h index 86c9dcd0beadd..594981910661f 100644 --- a/include/swift/AST/Expr.h +++ b/include/swift/AST/Expr.h @@ -1108,7 +1108,7 @@ class ObjectLiteralExpr final : public LiteralExpr { /// The kind of object literal. enum LiteralKind : unsigned { #define POUND_OBJECT_LITERAL(Name, Desc, Proto) Name, -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" }; private: diff --git a/include/swift/AST/Module.h b/include/swift/AST/Module.h index a7e3121cf214e..21b48c6d655bf 100644 --- a/include/swift/AST/Module.h +++ b/include/swift/AST/Module.h @@ -75,13 +75,9 @@ namespace swift { class ValueDecl; class VarDecl; class VisibleDeclConsumer; - class SyntaxParsingCache; class ASTScope; class SourceLookupCache; - namespace syntax { - class SourceFileSyntax; -} namespace ast_scope { class ASTSourceFileScope; } diff --git a/include/swift/AST/ParseRequests.h b/include/swift/AST/ParseRequests.h index 68d0d692a549e..f8a7d7b8fd7d3 100644 --- a/include/swift/AST/ParseRequests.h +++ b/include/swift/AST/ParseRequests.h @@ -21,7 +21,6 @@ #include "swift/AST/SimpleRequest.h" #include "swift/Basic/Fingerprint.h" #include "swift/Parse/Token.h" -#include "swift/Syntax/SyntaxNodes.h" namespace swift { @@ -90,7 +89,6 @@ struct SourceFileParsingResult { ArrayRef TopLevelItems; Optional> CollectedTokens; Optional InterfaceHasher; - Optional SyntaxRoot; }; /// Parse the top-level items of a SourceFile. diff --git a/include/swift/AST/SourceFile.h b/include/swift/AST/SourceFile.h index a0b65709ee408..f17124325a12d 100644 --- a/include/swift/AST/SourceFile.h +++ b/include/swift/AST/SourceFile.h @@ -78,18 +78,15 @@ class SourceFile final : public FileUnit { /// and the associated language option. DisablePoundIfEvaluation = 1 << 1, - /// Whether to build a syntax tree. - BuildSyntaxTree = 1 << 2, - /// Whether to save the file's parsed tokens. - CollectParsedTokens = 1 << 3, + CollectParsedTokens = 1 << 2, /// Whether to compute the interface hash of the file. - EnableInterfaceHash = 1 << 4, + EnableInterfaceHash = 1 << 3, /// Whether to suppress warnings when parsing. This is set for secondary /// files, as they get parsed multiple times. - SuppressWarnings = 1 << 5, + SuppressWarnings = 1 << 4, }; using ParsingOptions = OptionSet; @@ -248,10 +245,6 @@ class SourceFile final : public FileUnit { /// code for it. Note this method returns \c false in WMO. bool isPrimary() const { return IsPrimary; } - /// A cache of syntax nodes that can be reused when creating the syntax tree - /// for this file. - swift::SyntaxParsingCache *SyntaxParsingCache = nullptr; - /// The list of local type declarations in the source file. llvm::SetVector LocalTypeDecls; @@ -633,14 +626,10 @@ class SourceFile final : public FileUnit { /// them to be accessed from \c getAllTokens. bool shouldCollectTokens() const; - bool shouldBuildSyntaxTree() const; - /// Whether the bodies of types and functions within this file can be lazily /// parsed. bool hasDelayedBodyParsing() const; - syntax::SourceFileSyntax getSyntaxRoot() const; - OpaqueTypeDecl *lookupOpaqueResultType(StringRef MangledName) override; /// Do not call when inside an inactive clause (\c @@ -660,9 +649,6 @@ class SourceFile final : public FileUnit { /// If not \c None, the underlying vector contains the parsed tokens of this /// source file. Optional> AllCollectedTokens; - - /// The root of the syntax tree representing the source file. - std::unique_ptr SyntaxRoot; }; inline SourceFile::ParsingOptions operator|(SourceFile::ParsingFlags lhs, diff --git a/include/swift/Syntax/TokenKinds.def.gyb b/include/swift/AST/TokenKinds.def.gyb similarity index 100% rename from include/swift/Syntax/TokenKinds.def.gyb rename to include/swift/AST/TokenKinds.def.gyb diff --git a/include/swift/Basic/LangOptions.h b/include/swift/Basic/LangOptions.h index 8261a0c87cecc..d875bcca1ccbc 100644 --- a/include/swift/Basic/LangOptions.h +++ b/include/swift/Basic/LangOptions.h @@ -420,19 +420,6 @@ namespace swift { /// Whether collect tokens during parsing for syntax coloring. bool CollectParsedToken = false; - /// Whether to parse syntax tree. If the syntax tree is built, the generated - /// AST may not be correct when syntax nodes are reused as part of - /// incrementals parsing. - bool BuildSyntaxTree = false; - - /// Whether parsing is occurring for creation of syntax tree only, and no typechecking will occur after - /// parsing e.g. when parsing for SwiftSyntax. This is intended to affect parsing, e.g. disable - /// unnecessary name lookups that are not useful for pure syntactic parsing. - bool ParseForSyntaxTreeOnly = false; - - /// Whether to verify the parsed syntax tree and emit related diagnostics. - bool VerifySyntaxTree = false; - /// Whether to disable the evaluation of '#if' decls, such that the bodies /// of active clauses aren't hoisted into the enclosing scope. bool DisablePoundIfEvaluation = false; diff --git a/include/swift/CMakeLists.txt b/include/swift/CMakeLists.txt index a7bc714db8818..139ecf4963344 100644 --- a/include/swift/CMakeLists.txt +++ b/include/swift/CMakeLists.txt @@ -9,7 +9,5 @@ if(SWIFT_INCLUDE_TOOLS) ESCAPE_QUOTES @ONLY) add_subdirectory(AST) add_subdirectory(Option) - add_subdirectory(Parse) - add_subdirectory(Syntax) endif() diff --git a/include/swift/Frontend/Frontend.h b/include/swift/Frontend/Frontend.h index 632f890c83c9a..f244e2a5671fb 100644 --- a/include/swift/Frontend/Frontend.h +++ b/include/swift/Frontend/Frontend.h @@ -37,7 +37,6 @@ #include "swift/Migrator/MigratorOptions.h" #include "swift/Parse/CodeCompletionCallbacks.h" #include "swift/Parse/Parser.h" -#include "swift/Parse/SyntaxParsingCache.h" #include "swift/Sema/SourceLoader.h" #include "swift/Serialization/Validation.h" #include "swift/Subsystems.h" @@ -96,10 +95,6 @@ class CompilerInvocation { IRGenOptions IRGenOpts; TBDGenOptions TBDGenOpts; ModuleInterfaceOptions ModuleInterfaceOpts; - /// The \c SyntaxParsingCache to use when parsing the main file of this - /// invocation - SyntaxParsingCache *MainFileSyntaxParsingCache = nullptr; - llvm::MemoryBuffer *CodeCompletionBuffer = nullptr; /// Code completion offset in bytes from the beginning of the main @@ -296,14 +291,6 @@ class CompilerInvocation { IRGenOptions &getIRGenOptions() { return IRGenOpts; } const IRGenOptions &getIRGenOptions() const { return IRGenOpts; } - void setMainFileSyntaxParsingCache(SyntaxParsingCache *Cache) { - MainFileSyntaxParsingCache = Cache; - } - - SyntaxParsingCache *getMainFileSyntaxParsingCache() const { - return MainFileSyntaxParsingCache; - } - void setParseStdlib() { FrontendOpts.ParseStdlib = true; } diff --git a/include/swift/Frontend/FrontendOptions.h b/include/swift/Frontend/FrontendOptions.h index fb2ef293b0dbc..cbe1b7f85b406 100644 --- a/include/swift/Frontend/FrontendOptions.h +++ b/include/swift/Frontend/FrontendOptions.h @@ -126,7 +126,6 @@ class FrontendOptions { Typecheck, ///< Parse and type-check only DumpParse, ///< Parse only and dump AST DumpInterfaceHash, ///< Parse and dump the interface token hash. - EmitSyntax, ///< Parse and dump Syntax tree as JSON DumpAST, ///< Parse, type-check, and dump AST PrintAST, ///< Parse, type-check, and pretty-print AST PrintASTDecl, ///< Parse, type-check, and pretty-print AST declarations diff --git a/include/swift/Frontend/ModuleInterfaceLoader.h b/include/swift/Frontend/ModuleInterfaceLoader.h index ea528ce3b5b09..1f477a4c68773 100644 --- a/include/swift/Frontend/ModuleInterfaceLoader.h +++ b/include/swift/Frontend/ModuleInterfaceLoader.h @@ -112,6 +112,7 @@ #include "swift/Frontend/ModuleInterfaceSupport.h" #include "swift/Serialization/SerializedModuleLoader.h" #include "llvm/Support/StringSaver.h" +#include "llvm/Support/YAMLTraits.h" namespace clang { class CompilerInstance; diff --git a/include/swift/IDE/CodeCompletionResult.h b/include/swift/IDE/CodeCompletionResult.h index 1451ed560fb45..c9f525ef5f217 100644 --- a/include/swift/IDE/CodeCompletionResult.h +++ b/include/swift/IDE/CodeCompletionResult.h @@ -184,7 +184,7 @@ enum class CodeCompletionKeywordKind : uint8_t { None, #define KEYWORD(X) kw_##X, #define POUND_KEYWORD(X) pound_##X, -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" }; enum class CompletionKind : uint8_t { diff --git a/include/swift/IDE/CompletionLookup.h b/include/swift/IDE/CompletionLookup.h index ce820f00a374c..16df538cbcf47 100644 --- a/include/swift/IDE/CompletionLookup.h +++ b/include/swift/IDE/CompletionLookup.h @@ -26,9 +26,9 @@ #include "swift/IDE/CodeCompletionResult.h" #include "swift/IDE/CodeCompletionStringPrinter.h" #include "swift/IDE/PossibleParamInfo.h" +#include "swift/Parse/CodeCompletionCallbacks.h" #include "swift/Sema/IDETypeChecking.h" #include "swift/Strings.h" -#include "swift/Syntax/SyntaxKind.h" namespace swift { namespace ide { @@ -600,7 +600,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer { void collectPrecedenceGroups(); - void getPrecedenceGroupCompletions(syntax::SyntaxKind SK); + void getPrecedenceGroupCompletions(CodeCompletionCallbacks::PrecedenceGroupCompletionKind SK); void getPoundAvailablePlatformCompletions(); diff --git a/include/swift/IDE/CompletionOverrideLookup.h b/include/swift/IDE/CompletionOverrideLookup.h index f070fbd264ae7..15293e9032817 100644 --- a/include/swift/IDE/CompletionOverrideLookup.h +++ b/include/swift/IDE/CompletionOverrideLookup.h @@ -16,7 +16,7 @@ #include "swift/AST/NameLookup.h" #include "swift/IDE/CodeCompletionResultSink.h" #include "swift/Sema/IDETypeChecking.h" -#include "swift/Syntax/TokenKinds.h" +#include "swift/Parse/Token.h" namespace swift { namespace ide { diff --git a/include/swift/Migrator/MigrationState.h b/include/swift/Migrator/MigrationState.h index be54c6034fcba..5b6365e5011cd 100644 --- a/include/swift/Migrator/MigrationState.h +++ b/include/swift/Migrator/MigrationState.h @@ -18,7 +18,6 @@ #ifndef SWIFT_MIGRATOR_MIGRATIONSTATE_H #define SWIFT_MIGRATOR_MIGRATIONSTATE_H -#include "swift/Syntax/References.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/ADT/StringRef.h" @@ -78,19 +77,19 @@ struct MigrationState : public llvm::ThreadSafeRefCountedBase { return InputBufferID == OutputBufferID; } - static RC + static llvm::IntrusiveRefCntPtr start(SourceManager &SrcMgr, const unsigned InputBufferID) { - return RC { + return llvm::IntrusiveRefCntPtr { new MigrationState { MigrationKind::Start, SrcMgr, InputBufferID, InputBufferID } }; } - static RC + static llvm::IntrusiveRefCntPtr make(MigrationKind Kind, SourceManager &SrcMgr, const unsigned InputBufferID, const unsigned OutputBufferID) { - return RC { + return llvm::IntrusiveRefCntPtr { new MigrationState { Kind, SrcMgr, diff --git a/include/swift/Migrator/Migrator.h b/include/swift/Migrator/Migrator.h index f0cb6fec184cd..b0bd64d170118 100644 --- a/include/swift/Migrator/Migrator.h +++ b/include/swift/Migrator/Migrator.h @@ -18,7 +18,7 @@ #define SWIFT_MIGRATOR_MIGRATOR_H #include "swift/Migrator/MigrationState.h" -#include "swift/Syntax/References.h" +#include "llvm/ADT/IntrusiveRefCntPtr.h" namespace swift { class CompilerInstance; @@ -39,7 +39,7 @@ struct Migrator { CompilerInstance *StartInstance; const CompilerInvocation &StartInvocation; SourceManager SrcMgr; - std::vector> States; + std::vector> States; Migrator(CompilerInstance *StartInstance, const CompilerInvocation &StartInvocation); diff --git a/include/swift/Option/FrontendOptions.td b/include/swift/Option/FrontendOptions.td index 3c94738186def..59076e475493c 100644 --- a/include/swift/Option/FrontendOptions.td +++ b/include/swift/Option/FrontendOptions.td @@ -119,9 +119,6 @@ def verify_generic_signatures : Separate<["-"], "verify-generic-signatures">, MetaVarName<"">, HelpText<"Verify the generic signatures in the given module">; -def verify_syntax_tree : Flag<["-"], "verify-syntax-tree">, - HelpText<"Verify that no unknown nodes exist in the libSyntax tree">; - def show_diagnostics_after_fatal : Flag<["-"], "show-diagnostics-after-fatal">, HelpText<"Keep emitting subsequent diagnostics after a fatal error">; @@ -835,9 +832,6 @@ def emit_sorted_sil : Flag<["-"], "emit-sorted-sil">, HelpText<"When printing SIL, print out all sil entities sorted by name to " "ease diffing">; -def emit_syntax : Flag<["-"], "emit-syntax">, - HelpText<"Parse input file(s) and emit the Syntax tree(s) as JSON">, ModeOpt; - def cxx_interop_getters_setters_as_properties : Flag<["-"], "cxx-interop-getters-setters-as-properties">, HelpText<"Import getters and setters as computed properties in Swift">, diff --git a/include/swift/Parse/CMakeLists.txt b/include/swift/Parse/CMakeLists.txt deleted file mode 100644 index 263e438572a26..0000000000000 --- a/include/swift/Parse/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) - set(SWIFT_GYB_FLAGS --line-directive "^\"#line %(line)d \\\"%(file)s\\\"^\"") -else() - set(SWIFT_GYB_FLAGS --line-directive "\'#line" "%(line)d" "\"%(file)s\"\'") -endif() - -set(generated_include_sources - ParsedSyntaxBuilders.h.gyb - ParsedSyntaxNodes.h.gyb - ParsedSyntaxRecorder.h.gyb -) - -add_gyb_target(swift-parse-syntax-generated-headers - "${generated_include_sources}") -set_property(TARGET swift-parse-syntax-generated-headers - PROPERTY FOLDER "Miscellaneous") diff --git a/include/swift/Parse/CodeCompletionCallbacks.h b/include/swift/Parse/CodeCompletionCallbacks.h index 7bdb132ccd769..7204c53e76985 100644 --- a/include/swift/Parse/CodeCompletionCallbacks.h +++ b/include/swift/Parse/CodeCompletionCallbacks.h @@ -192,9 +192,15 @@ class CodeCompletionCallbacks { /// Complete 'async' and 'throws' at effects specifier position. virtual void completeEffectsSpecifier(bool hasAsync, bool hasThrows) {}; + enum class PrecedenceGroupCompletionKind { + Relation, + Associativity, + AttributeList, + Assignment, + }; /// Complete within a precedence group decl or after a colon in an /// operator decl. - virtual void completeInPrecedenceGroup(SyntaxKind SK) {}; + virtual void completeInPrecedenceGroup(PrecedenceGroupCompletionKind SK) {}; /// Complete the platform names inside #available statements. virtual void completePoundAvailablePlatform() {}; diff --git a/include/swift/Parse/Lexer.h b/include/swift/Parse/Lexer.h index b96b135dd0a85..20664d2a82ee3 100644 --- a/include/swift/Parse/Lexer.h +++ b/include/swift/Parse/Lexer.h @@ -22,7 +22,6 @@ #include "swift/Basic/SourceManager.h" #include "swift/Parse/LexerState.h" #include "swift/Parse/Token.h" -#include "swift/Parse/ParsedTrivia.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/SaveAndRestore.h" @@ -45,11 +44,6 @@ enum class CommentRetentionMode { ReturnAsTokens, }; -enum class TriviaRetentionMode { - WithoutTrivia, - WithTrivia, -}; - enum class HashbangMode : bool { Disallowed, Allowed, @@ -131,21 +125,10 @@ class Lexer { const CommentRetentionMode RetainComments; - const TriviaRetentionMode TriviaRetention; - /// InSILBody - This is true when we're lexing the body of a SIL declaration /// in a SIL file. This enables some context-sensitive lexing. bool InSILBody = false; - /// The current leading trivia for the next token. - /// - /// The StringRef points into the source buffer that is currently being lexed. - StringRef LeadingTrivia; - - /// The current trailing trivia for the next token. - /// The StringRef points into the source buffer that is currently being lexed. - StringRef TrailingTrivia; - /// The location at which the comment of the next token starts. \c nullptr if /// the next token doesn't have a comment. const char *CommentStart; @@ -166,8 +149,8 @@ class Lexer { Lexer(const PrincipalTag &, const LangOptions &LangOpts, const SourceManager &SourceMgr, unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode, - HashbangMode HashbangAllowed, CommentRetentionMode RetainComments, - TriviaRetentionMode TriviaRetention); + HashbangMode HashbangAllowed, + CommentRetentionMode RetainComments); void initialize(unsigned Offset, unsigned EndOffset); @@ -202,14 +185,13 @@ class Lexer { const LangOptions &Options, const SourceManager &SourceMgr, unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode, HashbangMode HashbangAllowed = HashbangMode::Disallowed, - CommentRetentionMode RetainComments = CommentRetentionMode::None, - TriviaRetentionMode TriviaRetention = TriviaRetentionMode::WithoutTrivia); + CommentRetentionMode RetainComments = CommentRetentionMode::None); /// Create a lexer that scans a subrange of the source buffer. Lexer(const LangOptions &Options, const SourceManager &SourceMgr, unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode, HashbangMode HashbangAllowed, CommentRetentionMode RetainComments, - TriviaRetentionMode TriviaRetention, unsigned Offset, + unsigned Offset, unsigned EndOffset); /// Create a sub-lexer that lexes from the same buffer, but scans @@ -233,15 +215,9 @@ class Lexer { return LexMode == LexerMode::SwiftInterface; } - /// Lex a token. If \c TriviaRetentionMode is \c WithTrivia, passed pointers - /// to trivia are populated. - void lex(Token &Result, StringRef &LeadingTriviaResult, - StringRef &TrailingTriviaResult) { + /// Lex a token. + void lex(Token &Result) { Result = NextToken; - if (TriviaRetention == TriviaRetentionMode::WithTrivia) { - LeadingTriviaResult = LeadingTrivia; - TrailingTriviaResult = TrailingTrivia; - } // Emit any diagnostics recorded for this token. if (DiagQueue) DiagQueue->emit(); @@ -250,11 +226,6 @@ class Lexer { lexImpl(); } - void lex(Token &Result) { - StringRef LeadingTrivia, TrailingTrivia; - lex(Result, LeadingTrivia, TrailingTrivia); - } - /// Reset the lexer's buffer pointer to \p Offset bytes after the buffer /// start. void resetToOffset(size_t Offset) { @@ -304,8 +275,7 @@ class Lexer { /// Returns the lexer state for the beginning of the given token. /// After restoring the state, lexer will return this token and continue from /// there. - State getStateForBeginningOfToken(const Token &Tok, - const StringRef &LeadingTrivia = {}) const { + State getStateForBeginningOfToken(const Token &Tok) const { // If the token has a comment attached to it, rewind to before the comment, // not just the start of the token. This ensures that we will re-lex and @@ -314,11 +284,6 @@ class Lexer { if (TokStart.isInvalid()) TokStart = Tok.getLoc(); auto S = getStateForBeginningOfTokenLoc(TokStart); - if (TriviaRetention == TriviaRetentionMode::WithTrivia) { - S.LeadingTrivia = LeadingTrivia; - } else { - S.LeadingTrivia = StringRef(); - } return S; } @@ -340,10 +305,6 @@ class Lexer { // Don't re-emit diagnostics from readvancing the lexer. if (DiagQueue && !enableDiagnostics) DiagQueue->clear(); - - // Restore Trivia. - if (TriviaRetention == TriviaRetentionMode::WithTrivia) - LeadingTrivia = S.LeadingTrivia; } /// Restore the lexer state to a given state that is located before @@ -681,13 +642,6 @@ class Lexer { }; -/// A lexer that can lex trivia into its pieces -class TriviaLexer { -public: - /// Decompose the trivia in \p TriviaStr into their pieces. - static ParsedTrivia lexTrivia(StringRef TriviaStr); -}; - /// Given an ordered token \param Array , get the iterator pointing to the first /// token that is not before \param Loc . template diff --git a/include/swift/Parse/LexerState.h b/include/swift/Parse/LexerState.h index b5771395a96bb..31b7e272acda9 100644 --- a/include/swift/Parse/LexerState.h +++ b/include/swift/Parse/LexerState.h @@ -19,7 +19,6 @@ #include "llvm/ADT/Optional.h" #include "swift/Basic/SourceLoc.h" -#include "swift/Parse/ParsedTrivia.h" namespace swift { class Lexer; @@ -39,7 +38,6 @@ class LexerState { private: explicit LexerState(SourceLoc Loc) : Loc(Loc) {} SourceLoc Loc; - StringRef LeadingTrivia; friend class Lexer; }; diff --git a/include/swift/Parse/ParsedRawSyntaxNode.h b/include/swift/Parse/ParsedRawSyntaxNode.h deleted file mode 100644 index 0f6c4c395563f..0000000000000 --- a/include/swift/Parse/ParsedRawSyntaxNode.h +++ /dev/null @@ -1,255 +0,0 @@ -//===--- ParsedRawSyntaxNode.h - Parsed Raw Syntax Node ---------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_PARSEDRAWSYNTAXNODE_H -#define SWIFT_PARSE_PARSEDRAWSYNTAXNODE_H - -#include "swift/Basic/Debug.h" -#include "swift/Basic/SourceLoc.h" -#include "swift/Parse/ParsedTrivia.h" -#include "swift/Parse/SyntaxParseActions.h" -#include "swift/Parse/Token.h" -#include "swift/Syntax/SyntaxKind.h" -#include "llvm/Support/Debug.h" - -#ifndef NDEBUG -/// Whether \c ParsedRawSyntaxNode should keep track of its range and verify -/// that the children of layout nodes have consecutive ranges. -/// Because this significantly changes the way, \c ParsedRawSyntaxNode and -/// \c ParsedRawSyntaxNodeRecorder are being compiled, this is a separate -/// constant from \c NDEBUG, so that it can be toggled independently to \c -/// NDEBUG during development. -#define PARSEDRAWSYNTAXNODE_VERIFY_RANGES 1 -#endif - -namespace swift { - -class SyntaxParsingContext; - -/// Represents a raw syntax node formed by the parser. -/// -/// It can be either 'recorded', in which case it encapsulates an -/// \c OpaqueSyntaxNode that was returned from a \c SyntaxParseActions -/// invocation, or 'deferred' which captures the data for a -/// \c SyntaxParseActions invocation to occur later. -/// -/// An \c OpaqueSyntaxNode can represent both the result of 'recording' a token -/// as well as 'recording' a syntax layout, so there's only one -/// \c RecordedSyntaxNode structure that can represent both. -/// -/// The 'deferred' form is used for when the parser is backtracking and when -/// there are instances that it's not clear what will be the final syntax node -/// in the current parsing context. -class ParsedRawSyntaxNode { - friend class ParsedRawSyntaxRecorder; - using DataKind = RecordedOrDeferredNode::Kind; - - /// The opaque data of this node. Needs to be interpreted by the \c - /// SyntaxParseActions, which created it. - RecordedOrDeferredNode Data; - -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - /// The range of this node, including trivia. - /// Only store this as a member when it's actually needed to keep \c - /// ParsedRawSyntaxNode as small as possible, which improves performance - /// when it is being passed around. - CharSourceRange Range; -#endif - syntax::SyntaxKind SynKind; - tok TokKind; - /// Primary used for capturing a deferred missing token. - bool IsMissing = false; - - ParsedRawSyntaxNode(const ParsedRawSyntaxNode &other) = delete; - ParsedRawSyntaxNode &operator=(const ParsedRawSyntaxNode &other) = delete; - -public: - // MARK: - Constructors - - ParsedRawSyntaxNode() - : Data(nullptr, DataKind::Null), SynKind(syntax::SyntaxKind::Unknown), - TokKind(tok::unknown) {} - -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - ParsedRawSyntaxNode(RecordedOrDeferredNode Data, syntax::SyntaxKind SynKind, - tok TokKind, bool IsMissing, CharSourceRange Range) - : Data(Data), Range(Range), SynKind(SynKind), TokKind(TokKind), - IsMissing(IsMissing) { - assert(getKind() == SynKind && "Syntax kind with too large value!"); - assert(getTokenKind() == TokKind && "Token kind with too large value!"); - } - - ParsedRawSyntaxNode(OpaqueSyntaxNode Opaque, syntax::SyntaxKind SynKind, - tok TokKind, DataKind DK, bool IsMissing, - CharSourceRange Range) - : ParsedRawSyntaxNode(RecordedOrDeferredNode(Opaque, DK), SynKind, - TokKind, IsMissing, Range) {} -#else - ParsedRawSyntaxNode(RecordedOrDeferredNode Data, syntax::SyntaxKind SynKind, - tok TokKind, bool IsMissing) - : Data(Data), SynKind(SynKind), TokKind(TokKind), IsMissing(IsMissing) { - assert(getKind() == SynKind && "Syntax kind with too large value!"); - assert(getTokenKind() == TokKind && "Token kind with too large value!"); - } - - ParsedRawSyntaxNode(OpaqueSyntaxNode Opaque, syntax::SyntaxKind SynKind, - tok TokKind, DataKind DK, bool IsMissing) - : ParsedRawSyntaxNode(RecordedOrDeferredNode(Opaque, DK), SynKind, - TokKind, IsMissing) {} -#endif - - ParsedRawSyntaxNode &operator=(ParsedRawSyntaxNode &&other) { - assert(ensureDataIsNotRecorded() && - "recorded data is being destroyed by assignment"); - Data = std::move(other.Data); -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - Range = std::move(other.Range); -#endif - SynKind = std::move(other.SynKind); - TokKind = std::move(other.TokKind); - IsMissing = std::move(other.IsMissing); - other.reset(); - return *this; - } - - ParsedRawSyntaxNode(ParsedRawSyntaxNode &&other) : ParsedRawSyntaxNode() { - *this = std::move(other); - } - - static ParsedRawSyntaxNode null() { return ParsedRawSyntaxNode(); } - - ~ParsedRawSyntaxNode() { - assert(ensureDataIsNotRecorded() && "recorded data is being destructed"); - } - - // MARK: - Retrieving node kind - - /// Returns the type of this node (recorded, deferred layout, deferred token, - /// null). - DataKind getDataKind() const { return Data.getKind(); } - - bool isNull() const { return getDataKind() == DataKind::Null; } - bool isRecorded() const { return getDataKind() == DataKind::Recorded; } - bool isDeferredLayout() const { - return getDataKind() == DataKind::DeferredLayout; - } - bool isDeferredToken() const { - return getDataKind() == DataKind::DeferredToken; - } - - // MARK: - Retrieving opaque data - - /// Returns the opaque data of this node, assuming that it is deferred. This - /// must be interpreted by the \c SyntaxParseAction, which likely also needs - /// the node type (layout or token) to interpret the data. - /// The data opaque data returned by this function *must not* be used to - /// record the node, only to inspect it. - OpaqueSyntaxNode getUnsafeDeferredOpaqueData() const { - assert(isDeferredLayout() || isDeferredToken()); - return Data.getOpaque(); - } - - /// Return the opaque data of this node and reset it. - OpaqueSyntaxNode takeData() { - OpaqueSyntaxNode Data = this->Data.getOpaque(); - reset(); - return Data; - } - - RecordedOrDeferredNode takeRecordedOrDeferredNode() { - RecordedOrDeferredNode Data = this->Data; - reset(); - return Data; - } - - // MARK: - Retrieving additional node info - - syntax::SyntaxKind getKind() const { return SynKind; } - tok getTokenKind() const { return TokKind; } - - bool isToken() const { - return getKind() == syntax::SyntaxKind::Token; - } - bool isToken(tok tokKind) const { - return getTokenKind() == tokKind; - } - bool isMissing() const { return IsMissing; } - -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - /// Returns the range of this node including leading and trailing trivia. - /// - /// This method is only present if \c ParsedRawSyntaxNode is keeping track - /// of its range to verify element ranges. - CharSourceRange getRange() const { return Range; } -#endif - - size_t - getDeferredNumChildren(const SyntaxParsingContext *SyntaxContext) const; - - /// If this node is a deferred layout node, return the child at index \p - /// ChildIndex. - /// Note that this may be an expensive operation since the \c - /// SyntaxParseAction, which created the node (implicitly passed via the - /// \p SyntaxContext) needs to be consulted to retrieve the child. - ParsedRawSyntaxNode - getDeferredChild(size_t ChildIndex, - const SyntaxParsingContext *SyntaxContext) const; - - // MARK: - Miscellaneous - - void reset() { - Data = RecordedOrDeferredNode(nullptr, DataKind::Null); - SynKind = syntax::SyntaxKind::Unknown; - TokKind = tok::unknown; - IsMissing = false; -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - Range = CharSourceRange(); -#endif - } - - ParsedRawSyntaxNode unsafeCopy() const { - ParsedRawSyntaxNode copy; - copy.Data = Data; -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - copy.Range = Range; -#endif - copy.SynKind = SynKind; - copy.TokKind = TokKind; - copy.IsMissing = IsMissing; - return copy; - } - -#ifndef NDEBUG - bool ensureDataIsNotRecorded() { - if (getDataKind() != DataKind::Recorded) - return true; - llvm::dbgs() << "Leaking node: "; - dump(llvm::dbgs()); - llvm::dbgs() << "\n"; - return false; - } -#endif - - // MARK: - Printing - - /// Dump this piece of syntax recursively for debugging or testing. - SWIFT_DEBUG_DUMP; - - /// Dump this piece of syntax recursively. If \p Context is passed, this - /// method is also able to traverse its children and dump them. - void dump(raw_ostream &OS, const SyntaxParsingContext *Context = nullptr, - unsigned Indent = 0) const; -}; - -} // end namespace swift - -#endif diff --git a/include/swift/Parse/ParsedRawSyntaxRecorder.h b/include/swift/Parse/ParsedRawSyntaxRecorder.h deleted file mode 100644 index 7d11ad806f742..0000000000000 --- a/include/swift/Parse/ParsedRawSyntaxRecorder.h +++ /dev/null @@ -1,234 +0,0 @@ -//===--- ParsedRawSyntaxRecorder.h - Raw Syntax Parsing Recorder ----------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the ParsedRawSyntaxRecorder, which is the interface the -// parser is using to pass parsed syntactic elements to a SyntaxParseActions -// receiver and get a ParsedRawSyntaxNode object back. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_PARSEDRAWSYNTAXRECORDER_H -#define SWIFT_PARSE_PARSEDRAWSYNTAXRECORDER_H - -#include "swift/Basic/LLVM.h" -#include "swift/Parse/ParsedRawSyntaxNode.h" -#include "swift/Parse/SyntaxParseActions.h" -#include "swift/SyntaxParse/SyntaxTreeCreator.h" -#include - -/// Define a macro that creates a \c ParsedRawSyntaxNode. If \c -/// PARSEDRAWSYNTAXNODE_VERIFY_RANGES is defined, it passes the \c Range -/// parameter, otherwise it ignores it at the pre-processor level, which means -/// that \c Range can be an invalid expression. -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES -#define makeParsedRawSyntaxNode(Opaque, SynKind, TokKind, DataKind, IsMissing, \ - Range) \ - ParsedRawSyntaxNode(Opaque, SynKind, TokKind, DataKind, IsMissing, Range) -#else -#define makeParsedRawSyntaxNode(Opaque, SynKind, TokKind, DataKind, IsMissing, \ - Range) \ - ParsedRawSyntaxNode(Opaque, SynKind, TokKind, DataKind, IsMissing) -#endif - -namespace swift { - -class CharSourceRange; -struct ParsedTrivia; -class ParsedTriviaPiece; -class SyntaxParseActions; -class SyntaxParsingContext; -class SourceLoc; -class Token; -enum class tok : uint8_t; - -namespace syntax { -enum class SyntaxKind : uint16_t; -} - -/// The information returned from the \c lookupNode method in \c -/// SyntaxParseActions. -struct ParseLookupResult { - ParsedRawSyntaxNode Node; - - /// The length of \c Node spelled out in source, including trivia. - size_t Length; - - ParseLookupResult(ParsedRawSyntaxNode &&Node, size_t Length) - : Node(std::move(Node)), Length(Length) {} -}; - -class ParsedRawSyntaxRecorder final { - std::shared_ptr SPActions; - - /// Assuming that \p node is a deferred layout or token node, record it and - /// return the recorded node. - /// This consumes the data from \c node, which is unusable after it has been - /// recorded. The returned node should be used afterwards instead. - ParsedRawSyntaxNode recordDeferredNode(ParsedRawSyntaxNode &node) { - switch (node.getDataKind()) { - case RecordedOrDeferredNode::Kind::Null: - case RecordedOrDeferredNode::Kind::Recorded: - llvm_unreachable("Not deferred"); - case RecordedOrDeferredNode::Kind::DeferredLayout: { - OpaqueSyntaxNode Data = SPActions->recordDeferredLayout(node.takeData()); - return makeParsedRawSyntaxNode(Data, node.getKind(), node.getTokenKind(), - ParsedRawSyntaxNode::DataKind::Recorded, - node.isMissing(), node.getRange()); - } - case RecordedOrDeferredNode::Kind::DeferredToken: { - OpaqueSyntaxNode Data = SPActions->recordDeferredToken(node.takeData()); - return makeParsedRawSyntaxNode(Data, node.getKind(), node.getTokenKind(), - ParsedRawSyntaxNode::DataKind::Recorded, - node.isMissing(), node.getRange()); - } - } - } - -public: - explicit ParsedRawSyntaxRecorder(std::shared_ptr spActions) - : SPActions(std::move(spActions)) {} - - ParsedRawSyntaxNode recordToken(const Token &tok, StringRef leadingTrivia, - StringRef trailingTrivia) { - return recordToken(tok.getKind(), tok.getRange(), leadingTrivia, - trailingTrivia); - } - - ParsedRawSyntaxNode recordToken(tok tokenKind, CharSourceRange tokenRange, - StringRef leadingTrivia, - StringRef trailingTrivia) { - SourceLoc offset = - tokenRange.getStart().getAdvancedLoc(-leadingTrivia.size()); - unsigned length = leadingTrivia.size() + tokenRange.getByteLength() + - trailingTrivia.size(); - CharSourceRange range(offset, length); - OpaqueSyntaxNode n = - SPActions->recordToken(tokenKind, leadingTrivia, trailingTrivia, range); - return makeParsedRawSyntaxNode(n, syntax::SyntaxKind::Token, tokenKind, - ParsedRawSyntaxNode::DataKind::Recorded, - /*IsMissing=*/false, range); - } - - /// Record a missing token. \p loc can be invalid or an approximate location - /// of where the token would be if not missing. - ParsedRawSyntaxNode recordMissingToken(tok tokenKind, SourceLoc loc); - - /// The provided \p elements are an exact layout appropriate for the syntax - /// \p kind. Missing optional elements are represented with a null - /// ParsedRawSyntaxNode object. - ParsedRawSyntaxNode - recordRawSyntax(syntax::SyntaxKind kind, - MutableArrayRef elements) { - assert(kind != syntax::SyntaxKind::Token && - "Use recordToken to record a token"); -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - auto range = ParsedRawSyntaxRecorder::verifyElementRanges(elements); -#endif - - SmallVector subnodes; - if (!elements.empty()) { - for (auto &subnode : elements) { - switch (subnode.getDataKind()) { - case RecordedOrDeferredNode::Kind::Null: - subnodes.push_back(nullptr); - break; - case RecordedOrDeferredNode::Kind::Recorded: - subnodes.push_back(subnode.takeData()); - break; - case RecordedOrDeferredNode::Kind::DeferredLayout: - case RecordedOrDeferredNode::Kind::DeferredToken: { - auto recorded = recordDeferredNode(subnode); - subnodes.push_back(recorded.takeData()); - break; - } - } - } - } - OpaqueSyntaxNode n = SPActions->recordRawSyntax(kind, subnodes); - return makeParsedRawSyntaxNode(n, kind, tok::NUM_TOKENS, - ParsedRawSyntaxNode::DataKind::Recorded, - /*IsMissing=*/false, range); - } - - /// Record a raw syntax collection without eny elements. \p loc can be invalid - /// or an approximate location of where an element of the collection would be - /// if not missing. - ParsedRawSyntaxNode recordEmptyRawSyntaxCollection(syntax::SyntaxKind kind, - SourceLoc loc); - - /// Form a deferred syntax layout node. - /// All nodes in \p deferred nodes must be deferred. Otherwise, we'd have a - /// deferred layout node with recorded child nodes. Should we decide to - /// discard the deferred layout node, we would also need to discard its - /// recorded children, which cannot be done. - ParsedRawSyntaxNode - makeDeferred(syntax::SyntaxKind k, - MutableArrayRef deferredNodes, - SyntaxParsingContext &ctx) { -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - auto range = ParsedRawSyntaxRecorder::verifyElementRanges(deferredNodes); -#endif - - assert(llvm::none_of(deferredNodes, [](const ParsedRawSyntaxNode &node) { - return node.isRecorded(); - }) && "Cannot create a deferred layout node that has recorded children"); - - auto data = - SPActions->makeDeferredLayout(k, /*IsMissing=*/false, deferredNodes); - return makeParsedRawSyntaxNode( - data, k, tok::NUM_TOKENS, ParsedRawSyntaxNode::DataKind::DeferredLayout, - /*IsMissing=*/false, range); - } - - /// Form a deferred token node. - ParsedRawSyntaxNode makeDeferred(Token tok, StringRef leadingTrivia, - StringRef trailingTrivia) { - CharSourceRange tokRange = tok.getRange(); - CharSourceRange RangeWithTrivia = CharSourceRange( - tokRange.getStart().getAdvancedLoc(-leadingTrivia.size()), - (unsigned)leadingTrivia.size() + tokRange.getByteLength() + - (unsigned)trailingTrivia.size()); - auto Data = SPActions->makeDeferredToken(tok.getKind(), leadingTrivia, - trailingTrivia, RangeWithTrivia, - /*IsMissing=*/false); - return makeParsedRawSyntaxNode(Data, syntax::SyntaxKind::Token, - tok.getKind(), - ParsedRawSyntaxNode::DataKind::DeferredToken, - /*IsMissing=*/false, RangeWithTrivia); - } - - /// Form a deferred missing token node. - ParsedRawSyntaxNode makeDeferredMissing(tok tokKind, SourceLoc loc); - - /// Used for incremental re-parsing. - ParseLookupResult lookupNode(size_t lexerOffset, SourceLoc loc, - syntax::SyntaxKind kind); - - /// For a deferred layout node \p parent, retrieve the deferred child node - /// at \p ChildIndex. - ParsedRawSyntaxNode getDeferredChild(const ParsedRawSyntaxNode &parent, - size_t ChildIndex) const; - - /// For a deferred layout node \p node, retrieve the number of children. - size_t getDeferredNumChildren(const ParsedRawSyntaxNode &node) const; - -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - /// Verify that the ranges of \p elements are all consecutive and return the - /// range spanned by all \p elements. - static CharSourceRange - verifyElementRanges(ArrayRef elements); -#endif -}; - -} // end namespace swift - -#endif // SWIFT_PARSE_PARSEDRAWSYNTAXRECORDER_H diff --git a/include/swift/Parse/ParsedSyntax.h b/include/swift/Parse/ParsedSyntax.h deleted file mode 100644 index f377586353650..0000000000000 --- a/include/swift/Parse/ParsedSyntax.h +++ /dev/null @@ -1,93 +0,0 @@ -//===--- ParsedSyntax.h - Base class for ParsedSyntax hierarchy -*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_PARSEDSYNTAX_H -#define SWIFT_PARSE_PARSEDSYNTAX_H - -#include "swift/Parse/ParsedRawSyntaxNode.h" -#include "swift/Syntax/SyntaxKind.h" - -namespace swift { - -class ParsedSyntax { - ParsedRawSyntaxNode RawNode; - -public: - explicit ParsedSyntax(ParsedRawSyntaxNode &&rawNode) - : RawNode(std::move(rawNode)) {} - - const ParsedRawSyntaxNode &getRaw() const { return RawNode; } - ParsedRawSyntaxNode &&takeRaw() { return std::move(RawNode); } - syntax::SyntaxKind getKind() const { return RawNode.getKind(); } - - /// Returns true if the syntax node is of the given type. - template - bool is() const { - return T::classof(this); - } - - /// Cast this Syntax node to a more specific type, asserting it's of the - /// right kind. - template - T castTo() && { - assert(is() && "castTo() node of incompatible type!"); - return T(std::move(RawNode)); - } - - static bool kindof(syntax::SyntaxKind Kind) { - return true; - } - - static bool classof(const ParsedSyntax *S) { - // Trivially true. - return true; - } -}; - -class ParsedTokenSyntax final : public ParsedSyntax { -public: - explicit ParsedTokenSyntax(ParsedRawSyntaxNode &&rawNode) - : ParsedSyntax(std::move(rawNode)) {} - - tok getTokenKind() const { - return getRaw().getTokenKind(); - } - - static bool kindof(syntax::SyntaxKind Kind) { - return isTokenKind(Kind); - } - - static bool classof(const ParsedSyntax *S) { - return kindof(S->getKind()); - } -}; - -/// A generic unbounded collection of syntax nodes -template -class ParsedSyntaxCollection : public ParsedSyntax { - -public: - explicit ParsedSyntaxCollection(ParsedRawSyntaxNode rawNode) - : ParsedSyntax(std::move(rawNode)) {} - - static bool kindof(syntax::SyntaxKind Kind) { - return Kind == CollectionKind; - } - - static bool classof(const ParsedSyntax *S) { - return kindof(S->getKind()); - } -}; - -} // end namespace swift - -#endif diff --git a/include/swift/Parse/ParsedSyntaxBuilders.h.gyb b/include/swift/Parse/ParsedSyntaxBuilders.h.gyb deleted file mode 100644 index fdae418cc50da..0000000000000 --- a/include/swift/Parse/ParsedSyntaxBuilders.h.gyb +++ /dev/null @@ -1,77 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From ParsedSyntaxBuilders.h.gyb. -//// Do Not Edit Directly! -//===------------- ParsedSyntaxBuilders.h - Parsed Syntax Building --------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_PARSEDSYNTAXBUILDERS_H -#define SWIFT_PARSE_PARSEDSYNTAXBUILDERS_H - -#include "swift/Parse/ParsedRawSyntaxNode.h" -#include "swift/Parse/ParsedSyntaxNodes.h" -#include "swift/Parse/SyntaxParsingContext.h" - -namespace swift { - -class ParsedRawSyntaxRecorder; -class SyntaxParsingContext; - -% for node in SYNTAX_NODES: -% if node.is_buildable(): -% child_count = len(node.children) -class Parsed${node.name}Builder { - SyntaxParsingContext &SPCtx; - ParsedRawSyntaxNode Layout[${child_count}]; -% for child in node.children: -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child_node.collection_element_name -% child_elt_type = child_node.collection_element_type -% child_elt_name = child.name + 'Member' - SmallVector ${child_elt_name}s; -% end -% end - -public: - explicit Parsed${node.name}Builder(SyntaxParsingContext &SPCtx) - : SPCtx(SPCtx) {} - -% for child in node.children: - Parsed${node.name}Builder &use${child.name}(Parsed${child.type_name} &&${child.name}); -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child_node.collection_element_name -% child_elt_type = child_node.collection_element_type -% child_elt_name = child.name + 'Member' - Parsed${node.name}Builder &add${child_elt_name}(Parsed${child_elt_type} ${child_elt}); -% end -% end - - Parsed${node.name} build(); - -private: - Parsed${node.name} makeDeferred(); - Parsed${node.name} record(); - void finishLayout(bool deferred); -}; - -% end -% end - -} - -#endif // SWIFT_PARSE_PARSEDSYNTAXBUILDERS_H diff --git a/include/swift/Parse/ParsedSyntaxNodes.h.gyb b/include/swift/Parse/ParsedSyntaxNodes.h.gyb deleted file mode 100644 index cb33ca02dad5b..0000000000000 --- a/include/swift/Parse/ParsedSyntaxNodes.h.gyb +++ /dev/null @@ -1,92 +0,0 @@ -%{ - from gyb_syntax_support import * - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From ParsedSyntaxNodes.h.gyb. -//// Do Not Edit Directly! -//===--- ParsedSyntaxNodes.h - Parsed Syntax Node definitions -------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_PARSEDSYNTAXNODES_H -#define SWIFT_PARSE_PARSEDSYNTAXNODES_H - -#include "swift/Parse/ParsedSyntax.h" -#include "swift/Syntax/SyntaxKind.h" - -namespace swift { - -% # Emit the non-collection classes first, then emit the collection classes -% # that reference these classes. - -% for node in SYNTAX_NODES: -% if not node.is_syntax_collection(): -class Parsed${node.name}; -% end -% end - -% for node in SYNTAX_NODES: -% if node.is_syntax_collection(): -using Parsed${node.name} = - ParsedSyntaxCollection; -% end -% end - -% for node in SYNTAX_NODES: -% if not node.is_syntax_collection(): -% qualifier = "" if node.is_base() else "final" -% for line in dedented_lines(node.description): -/// ${line} -% end -class Parsed${node.name} ${qualifier} : public Parsed${node.base_type} { -% if node.is_buildable(): - friend class Parsed${node.name}Builder; -% end - -public: - explicit Parsed${node.name}(ParsedRawSyntaxNode &&rawNode) - : Parsed${node.base_type}(std::move(rawNode)) { - } - -% for child in node.children: -% for line in dedented_lines(child.description): - /// ${line} -% end -% if child.is_optional: - llvm::Optional getDeferred${child.name}( - const SyntaxParsingContext *SyntaxContext - ); -% else: - Parsed${child.type_name} getDeferred${child.name}( - const SyntaxParsingContext *SyntaxContext - ); -% end -% end - - static bool kindof(syntax::SyntaxKind Kind) { -% if node.is_base(): - return is${node.syntax_kind}Kind(Kind); -% else: - return Kind == syntax::SyntaxKind::${node.syntax_kind}; -% end - } - - static bool classof(const ParsedSyntax *S) { - return kindof(S->getKind()); - } -}; - -% end -% end -} - -#endif // SWIFT_PARSE_PARSEDSYNTAXNODES_H diff --git a/include/swift/Parse/ParsedSyntaxRecorder.h.gyb b/include/swift/Parse/ParsedSyntaxRecorder.h.gyb deleted file mode 100644 index 8b6eecd26e99c..0000000000000 --- a/include/swift/Parse/ParsedSyntaxRecorder.h.gyb +++ /dev/null @@ -1,95 +0,0 @@ -%{ - from gyb_syntax_support import * - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From ParsedSyntaxRecorder.h.gyb. -//// Do Not Edit Directly! -//===--- ParsedSyntaxRecorder.h - Parsed Syntax Recorder ------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_PARSEDSYNTAXRECORDER_H -#define SWIFT_PARSE_PARSEDSYNTAXRECORDER_H - -#include "swift/Basic/LLVM.h" -#include "swift/Parse/ParsedSyntaxNodes.h" -#include "swift/Syntax/SyntaxKind.h" - -namespace swift { - -class ParsedRawSyntaxRecorder; -class SyntaxParsingContext; - -struct ParsedSyntaxRecorder { - -% for node in SYNTAX_NODES: -% if node.children: -% child_params = [] -% for child in node.children: -% param_type = "Parsed%s" % child.type_name -% if child.is_optional: -% param_type = "Optional<%s>" % param_type -% end -% child_params.append("%s &&%s" % (param_type, child.name)) -% end -% child_params = ', '.join(child_params) -private: - static Parsed${node.name} record${node.syntax_kind}(MutableArrayRef layout, - ParsedRawSyntaxRecorder &rec); - static Parsed${node.name} defer${node.syntax_kind}(MutableArrayRef layout, - SyntaxParsingContext &SPCtx); -public: - static Parsed${node.name} make${node.syntax_kind}(${child_params}, - SyntaxParsingContext &SPCtx); -% elif node.is_syntax_collection(): -private: - static Parsed${node.name} record${node.syntax_kind}( - MutableArrayRef layout, - ParsedRawSyntaxRecorder &rec); - static Parsed${node.name} defer${node.syntax_kind}( - MutableArrayRef layout, - SyntaxParsingContext &SPCtx); -public: - static Parsed${node.name} make${node.syntax_kind}( - MutableArrayRef elts, - SyntaxParsingContext &SPCtx); - - static Parsed${node.name} makeBlank${node.syntax_kind}(SourceLoc loc, - SyntaxParsingContext &SPCtx); -% end -% end - -#pragma mark - Convenience APIs - - /// Records an unlabelled TupleTypeElementSyntax with the provided type and - /// optional trailing comma. - static ParsedTupleTypeElementSyntax - makeTupleTypeElement(ParsedTypeSyntax Type, - Optional TrailingComma, - SyntaxParsingContext &SPCtx); - - /// The provided \c elements are in the appropriate order for the syntax - /// \c kind's layout but optional elements are not be included. - /// This function will form the exact layout based on the provided elements, - /// substituting missing parts with a null ParsedRawSyntaxNode object. - /// - /// \returns true if the layout could be formed, false otherwise. - static bool - formExactLayoutFor(syntax::SyntaxKind kind, - MutableArrayRef elements, - function_ref)> - receiver); -}; -} - -#endif // SWIFT_PARSE_PARSEDSYNTAXRECORDER_H diff --git a/include/swift/Parse/ParsedTrivia.h b/include/swift/Parse/ParsedTrivia.h deleted file mode 100644 index 8bf9e9f4af826..0000000000000 --- a/include/swift/Parse/ParsedTrivia.h +++ /dev/null @@ -1,136 +0,0 @@ -//===--- ParsedTrivia.h - ParsedTrivia API ----------------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_PARSEDTRIVIA_H -#define SWIFT_PARSE_PARSEDTRIVIA_H - -#include "swift/Basic/LLVM.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/SmallVector.h" - -namespace swift { - class SourceLoc; - class SourceManager; - -namespace syntax { - enum class TriviaKind; - struct Trivia; -} - -class ParsedTriviaPiece { - syntax::TriviaKind Kind; - unsigned Length; - -public: - ParsedTriviaPiece(syntax::TriviaKind kind, unsigned length) - : Kind(kind), Length(length) {} - - syntax::TriviaKind getKind() const { return Kind; } - - /// Return the text of the trivia. - unsigned getLength() const { return Length; } - - void extendLength(unsigned len) { - Length += len; - } - - static size_t getTotalLength(ArrayRef pieces) { - size_t Len = 0; - for (auto &p : pieces) - Len += p.getLength(); - return Len; - } - - bool operator==(const ParsedTriviaPiece &Other) const { - return Kind == Other.Kind && Length == Other.Length; - } - - bool operator!=(const ParsedTriviaPiece &Other) const { - return !(*this == Other); - } - - static syntax::Trivia - convertToSyntaxTrivia(ArrayRef pieces, SourceLoc loc, - const SourceManager &SM, unsigned bufferID); -}; - -using ParsedTriviaList = SmallVector; - -struct ParsedTrivia { - ParsedTriviaList Pieces; - - /// Get the begin iterator of the pieces. - ParsedTriviaList::const_iterator begin() const { - return Pieces.begin(); - } - - /// Get the end iterator of the pieces. - ParsedTriviaList::const_iterator end() const { - return Pieces.end(); - } - - /// Clear pieces. - void clear() { - Pieces.clear(); - } - - /// Returns true if there are no pieces in this Trivia collection. - bool empty() const { - return Pieces.empty(); - } - - /// Return the number of pieces in this Trivia collection. - size_t size() const { - return Pieces.size(); - } - - size_t getLength() const { - return ParsedTriviaPiece::getTotalLength(Pieces); - } - - void push_back(syntax::TriviaKind kind, unsigned length) { - Pieces.emplace_back(kind, length); - } - - void appendOrSquash(syntax::TriviaKind kind, unsigned length) { - if (empty() || Pieces.back().getKind() != kind) { - push_back(kind, length); - } else { - Pieces.back().extendLength(length); - } - } - - bool operator==(const ParsedTrivia &Other) const { - if (Pieces.size() != Other.size()) { - return false; - } - - for (size_t i = 0; i < Pieces.size(); ++i) { - if (Pieces[i] != Other.Pieces[i]) { - return false; - } - } - - return true; - } - - bool operator!=(const ParsedTrivia &Other) const { - return !(*this == Other); - } - - syntax::Trivia convertToSyntaxTrivia(SourceLoc loc, const SourceManager &SM, - unsigned bufferID) const; -}; - -} // end namespace swift - -#endif diff --git a/include/swift/Parse/Parser.h b/include/swift/Parse/Parser.h index f9f1aadc26ae2..1d9574d4c9399 100644 --- a/include/swift/Parse/Parser.h +++ b/include/swift/Parse/Parser.h @@ -32,9 +32,8 @@ #include "swift/Parse/Token.h" #include "swift/Parse/ParserPosition.h" #include "swift/Parse/ParserResult.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Syntax/References.h" #include "swift/Config.h" +#include "llvm/ADT/IntrusiveRefCntPtr.h" namespace llvm { template class PointerUnion; @@ -47,7 +46,6 @@ namespace swift { class DiagnosticEngine; class Expr; class Lexer; - class ParsedTypeSyntax; class PersistentParserState; class RequirementRepr; class SILParserStateBase; @@ -57,11 +55,6 @@ namespace swift { class TypeLoc; struct EnumElementInfo; - - namespace syntax { - class RawSyntax; - enum class SyntaxKind : uint16_t; - }// end of syntax namespace /// Different contexts in which BraceItemList are parsed. enum class BraceItemListKind { @@ -222,14 +215,6 @@ class Parser { /// This is the current token being considered by the parser. Token Tok; - /// Leading trivia for \c Tok. - /// Always empty if !SF.shouldBuildSyntaxTree(). - StringRef LeadingTrivia; - - /// Trailing trivia for \c Tok. - /// Always empty if !SF.shouldBuildSyntaxTree(). - StringRef TrailingTrivia; - /// The receiver to collect all consumed tokens. ConsumeTokenReceiver *TokReceiver; @@ -356,22 +341,6 @@ class Parser { }; friend class StructureMarkerRAII; - /// A RAII object that tells the SyntaxParsingContext to defer Syntax nodes. - class DeferringContextRAII { - SyntaxParsingContext &Ctx; - bool WasDeferring; - - public: - explicit DeferringContextRAII(SyntaxParsingContext &SPCtx) - : Ctx(SPCtx), WasDeferring(Ctx.shouldDefer()) { - Ctx.setShouldDefer(); - } - - ~DeferringContextRAII() { - Ctx.setShouldDefer(WasDeferring); - } - }; - /// The stack of structure markers indicating the locations of /// structural elements actively being parsed, including the start /// of declarations, statements, and opening operators of various @@ -380,9 +349,6 @@ class Parser { /// This vector is managed by \c StructureMarkerRAII objects. llvm::SmallVector StructureMarkers; - /// Current syntax parsing context where call backs should be directed to. - SyntaxParsingContext *SyntaxContext; - /// Maps of macro name and version to availability specifications. typedef llvm::DenseMap> @@ -401,27 +367,17 @@ class Parser { public: Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags, - SILParserStateBase *SIL, PersistentParserState *PersistentState, - std::shared_ptr SPActions = nullptr); + SILParserStateBase *SIL, PersistentParserState *PersistentState); Parser(unsigned BufferID, SourceFile &SF, SILParserStateBase *SIL, - PersistentParserState *PersistentState = nullptr, - std::shared_ptr SPActions = nullptr); + PersistentParserState *PersistentState = nullptr); Parser(std::unique_ptr Lex, SourceFile &SF, SILParserStateBase *SIL = nullptr, - PersistentParserState *PersistentState = nullptr, - std::shared_ptr SPActions = nullptr); + PersistentParserState *PersistentState = nullptr); ~Parser(); /// Returns true if the buffer being parsed is allowed to contain SIL. bool isInSILMode() const; - /// Calling this function to finalize libSyntax tree creation without destroying - /// the parser instance. - OpaqueSyntaxNode finalizeSyntaxTree() { - assert(Tok.is(tok::eof) && "not done parsing yet"); - return SyntaxContext->finalizeRoot(); - } - /// Retrieve the token receiver from the parser once it has finished parsing. std::unique_ptr takeTokenReceiver() { assert(Tok.is(tok::eof) && "not done parsing yet"); @@ -434,7 +390,7 @@ class Parser { // Routines to save and restore parser state. ParserPosition getParserPosition() { - return ParserPosition(L->getStateForBeginningOfToken(Tok, LeadingTrivia), + return ParserPosition(L->getStateForBeginningOfToken(Tok), PreviousLoc); } @@ -444,14 +400,14 @@ class Parser { void restoreParserPosition(ParserPosition PP, bool enableDiagnostics = false) { L->restoreState(PP.LS, enableDiagnostics); - L->lex(Tok, LeadingTrivia, TrailingTrivia); + L->lex(Tok); PreviousLoc = PP.PreviousLoc; } void backtrackToPosition(ParserPosition PP) { assert(PP.isValid()); L->backtrackToState(PP.LS); - L->lex(Tok, LeadingTrivia, TrailingTrivia); + L->lex(Tok); PreviousLoc = PP.PreviousLoc; } @@ -466,9 +422,6 @@ class Parser { Parser &P; ParserPosition PP; DiagnosticTransaction DT; - /// This context immediately deconstructed with transparent accumulation - /// on cancelBacktrack(). - llvm::Optional SynContext; bool Backtrack = true; /// A token receiver used by the parser in the back tracking scope. This @@ -502,9 +455,7 @@ class Parser { BacktrackingScopeImpl(Parser &P) : P(P), PP(P.getParserPosition()), DT(P.Diags), - TempReceiver(P.TokReceiver) { - SynContext.emplace(P.SyntaxContext); - } + TempReceiver(P.TokReceiver) { } public: ~BacktrackingScopeImpl(); @@ -514,18 +465,14 @@ class Parser { /// A backtracking scope that will always backtrack when destructed. class BacktrackingScope final : public BacktrackingScopeImpl { public: - BacktrackingScope(Parser &P) : BacktrackingScopeImpl(P) { - SynContext->disable(); - } + BacktrackingScope(Parser &P) : BacktrackingScopeImpl(P) { } }; /// A backtracking scope whose backtracking can be disabled by calling /// \c cancelBacktrack. class CancellableBacktrackingScope final : public BacktrackingScopeImpl { public: - CancellableBacktrackingScope(Parser &P) : BacktrackingScopeImpl(P) { - SynContext->setBackTracking(); - } + CancellableBacktrackingScope(Parser &P) : BacktrackingScopeImpl(P) { } void cancelBacktrack(); }; @@ -589,10 +536,6 @@ class Parser { return consumeToken(); } - SourceLoc leadingTriviaLoc() { - return Tok.getLoc().getAdvancedLoc(-LeadingTrivia.size()); - } - SourceLoc consumeIdentifier(Identifier &Result, bool diagnoseDollarPrefix) { assert(Tok.isAny(tok::identifier, tok::kw_self, tok::kw_Self)); assert(Result.empty()); @@ -737,13 +680,6 @@ class Parser { /// Skip over any attribute. void skipAnyAttribute(); - /// If the parser is generating only a syntax tree, try loading the current - /// node from a previously generated syntax tree. - /// Returns \c true if the node has been loaded and inserted into the current - /// syntax tree. In this case the parser should behave as if the node has - /// successfully been created. - bool loadCurrentSyntaxNodeFromCache(); - /// Parse an #endif. bool parseEndIfDirective(SourceLoc &Loc); @@ -765,8 +701,7 @@ class Parser { /// parsed if the parser is generating only a syntax tree or if the user has /// passed the `-enable-experimental-concurrency` flag to the frontend. bool shouldParseExperimentalConcurrency() const { - return Context.LangOpts.EnableExperimentalConcurrency || - Context.LangOpts.ParseForSyntaxTreeOnly; + return Context.LangOpts.EnableExperimentalConcurrency; } public: @@ -935,13 +870,11 @@ class Parser { ParseListItemResult parseListItem(ParserStatus &Status, tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc, bool AllowSepAfterLast, - SyntaxKind ElementKind, llvm::function_ref callback); /// Parse a comma separated list of some elements. ParserStatus parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc, bool AllowSepAfterLast, Diag<> ErrorDiag, - syntax::SyntaxKind Kind, llvm::function_ref callback); void consumeTopLevelDecl(ParserPosition BeginParserPosition, @@ -1807,7 +1740,7 @@ class Parser { ParserStatus parseExprList(tok leftTok, tok rightTok, bool isArgumentList, SourceLoc &leftLoc, SmallVectorImpl &elts, - SourceLoc &rightLoc, SyntaxKind Kind); + SourceLoc &rightLoc); /// Parse an object literal. /// @@ -2055,14 +1988,6 @@ DeclNameRef formDeclNameRef(ASTContext &ctx, /// Whether a given token can be the start of a decl. bool isKeywordPossibleDeclStart(const Token &Tok); -/// Lex and return a vector of `TokenSyntax` tokens, which include -/// leading and trailing trivia. -std::vector< - std::pair> -tokenizeWithTrivia(const LangOptions &LangOpts, const SourceManager &SM, - unsigned BufferID, const RC &Arena, - unsigned Offset = 0, unsigned EndOffset = 0, - DiagnosticEngine *Diags = nullptr); } // end namespace swift #endif diff --git a/include/swift/Parse/SyntaxParseActions.h b/include/swift/Parse/SyntaxParseActions.h deleted file mode 100644 index a729ea08467c7..0000000000000 --- a/include/swift/Parse/SyntaxParseActions.h +++ /dev/null @@ -1,175 +0,0 @@ -//===--- SyntaxParseActions.h - Syntax Parsing Actions ----------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface between the parser and a receiver of -// raw syntax nodes. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_SYNTAXPARSEACTIONS_H -#define SWIFT_PARSE_SYNTAXPARSEACTIONS_H - -#include "swift/Basic/LLVM.h" -#include "swift/Basic/SourceLoc.h" -#include "swift/Subsystems.h" -#include "llvm/ADT/PointerIntPair.h" -#include "llvm/Support/Allocator.h" - -namespace swift { - -class ParsedTriviaPiece; -class SourceFile; -class SourceLoc; -enum class tok : uint8_t; -class ParsedRawSyntaxNode; - -namespace syntax { -class SourceFileSyntax; -enum class SyntaxKind : uint16_t; -} - -// MARK: - Helper types - -/// A syntax node that can either be deferred or recorded. The actual data is -/// opaque and needs to be interpreted by the \c SyntaxParseAction which created -/// it. -class RecordedOrDeferredNode { -public: - enum class Kind : uint8_t { - Null, - Recorded, - DeferredLayout, - DeferredToken, - }; - -private: - llvm::PointerIntPair Data; - -public: - RecordedOrDeferredNode(OpaqueSyntaxNode Node, Kind NodeKind) - : Data(Node, NodeKind) {} - - OpaqueSyntaxNode getOpaque() const { return Data.getPointer(); } - Kind getKind() const { return Data.getInt(); } -}; - -/// Data returned from \c getDeferredChild. This is enough data to construct -/// a \c ParsedRawSyntaxNode. We don't return \c ParsedRawSyntaxNodes from -/// \c getDeferredChild to maintain a clean dependency relationship of -/// \c ParsedRawSyntaxNode being on a higher level than \c SyntaxParseActions. -struct DeferredNodeInfo { - RecordedOrDeferredNode Data; - syntax::SyntaxKind SyntaxKind; - tok TokenKind; - bool IsMissing; - - DeferredNodeInfo(RecordedOrDeferredNode Data, syntax::SyntaxKind SyntaxKind, - tok TokenKind, bool IsMissing) - : Data(Data), SyntaxKind(SyntaxKind), TokenKind(TokenKind), - IsMissing(IsMissing) {} -}; - -// MARK: - SyntaxParseActions - -class SyntaxParseActions { - virtual void _anchor(); - -public: - virtual ~SyntaxParseActions() = default; - - virtual OpaqueSyntaxNode recordToken(tok tokenKind, StringRef leadingTrivia, - StringRef trailingTrivia, - CharSourceRange range) = 0; - - /// Record a missing token. \c loc can be invalid or an approximate location - /// of where the token would be if not missing. - virtual OpaqueSyntaxNode recordMissingToken(tok tokenKind, SourceLoc loc) = 0; - - /// The provided \c elements are an exact layout appropriate for the syntax - /// \c kind. Missing optional elements are represented with a null - /// OpaqueSyntaxNode object. - virtual OpaqueSyntaxNode - recordRawSyntax(syntax::SyntaxKind kind, - ArrayRef elements) = 0; - - /// Create a deferred token node that may or may not be recorded later using - /// \c recordDeferredToken. The \c SyntaxParseAction is responsible for - /// keeping the deferred token alive until the action is destructed. - virtual OpaqueSyntaxNode makeDeferredToken(tok tokenKind, - StringRef leadingTrivia, - StringRef trailingTrivia, - CharSourceRange range, - bool isMissing) = 0; - - /// Create a deferred layout node that may or may not be recorded later using - /// \c recordDeferredLayout. The \c SyntaxParseAction is responsible for - /// keeping the deferred token alive until it is destructed. - /// From all nodes in \p children, the underlying opaque data will be *taken* - /// which resets the nodes. - virtual OpaqueSyntaxNode - makeDeferredLayout(syntax::SyntaxKind k, bool isMissing, - const MutableArrayRef &children) = 0; - - /// Record a deferred token node that was previously created using \c - /// makeDeferredToken. The deferred data will never be used again, so it can - /// be destroyed by this method. Note that not all deferred nodes will be - /// recorded and that pending deferred nodes need to be freed when the \c - /// SyntaxParseActions is destructed. - virtual OpaqueSyntaxNode recordDeferredToken(OpaqueSyntaxNode deferred) = 0; - - /// Record a deferred layout node. See recordDeferredToken. - virtual OpaqueSyntaxNode recordDeferredLayout(OpaqueSyntaxNode deferred) = 0; - - /// Since most data of \c ParsedRawSyntax is described as opaque data, the - /// \c ParsedRawSyntax node needs to reach out to the \c SyntaxParseAction, - /// which created it, to retrieve children. - /// This method assumes that \p node represents a *deferred* layout node. - /// This methods returns all information needed to construct a \c - /// ParsedRawSyntaxNode of a child node, except for the range which can be - /// retrieved using \c getDeferredChildRange if element ranges should be - /// verified - /// \p node is the parent node for which the child at position \p ChildIndex - /// should be retrieved. Furthermore, \p node starts at \p StartLoc. - virtual DeferredNodeInfo getDeferredChild(OpaqueSyntaxNode node, - size_t childIndex) const = 0; - - /// To verify \c ParsedRawSyntaxNode element ranges, the range of child nodes - /// returned by \c getDeferredChild needs to be determined. That's what this - /// method does. - /// It assumes that \p node is a deferred layout node starting at \p startLoc - /// and returns the range of the child node at \p childIndex. - /// This method is not designed with performance in mind. Do not use in - /// performance-critical code. - virtual CharSourceRange getDeferredChildRange(OpaqueSyntaxNode node, - size_t childIndex, - SourceLoc startLoc) const = 0; - - /// Return the number of children, \p node has. These can be retrieved using - /// \c getDeferredChild. - virtual size_t getDeferredNumChildren(OpaqueSyntaxNode node) = 0; - - /// Attempt to realize an opaque raw syntax node for a source file into a - /// SourceFileSyntax node. This will return \c None if the parsing action - /// doesn't support the realization of syntax nodes. - virtual Optional - realizeSyntaxRoot(OpaqueSyntaxNode root, const SourceFile &SF) = 0; - - /// Used for incremental re-parsing. - virtual std::pair - lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) { - return std::make_pair(0, nullptr); - } -}; - -} // end namespace swift - -#endif diff --git a/include/swift/Parse/SyntaxParsingCache.h b/include/swift/Parse/SyntaxParsingCache.h deleted file mode 100644 index 55a3b76498af8..0000000000000 --- a/include/swift/Parse/SyntaxParsingCache.h +++ /dev/null @@ -1,114 +0,0 @@ -//===----------- SyntaxParsingCache.h -================----------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_SYNTAXPARSINGCACHE_H -#define SWIFT_PARSE_SYNTAXPARSINGCACHE_H - -#include "swift/Syntax/SyntaxNodes.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/raw_ostream.h" -#include - -namespace swift { - -using namespace swift::syntax; - -/// A single edit to the original source file in which a continuous range of -/// characters have been replaced by a new string -struct SourceEdit { - /// The byte offset from which on characters were replaced. - size_t Start; - - /// The byte offset to which on characters were replaced. - size_t End; - - /// The length of the string that replaced the range described above. - size_t ReplacementLength; - - SourceEdit(size_t Start, size_t End, size_t ReplacementLength) - : Start(Start), End(End), ReplacementLength(ReplacementLength){}; - - /// The length of the range that has been replaced - size_t originalLength() const { return End - Start; } - - /// Check if the characters replaced by this edit fall into the given range - /// or are directly adjacent to it - bool intersectsOrTouchesRange(size_t RangeStart, size_t RangeEnd) { - return End >= RangeStart && Start <= RangeEnd; - } -}; - -struct SyntaxReuseRegion { - AbsoluteOffsetPosition Start; - AbsoluteOffsetPosition End; -}; - -class SyntaxParsingCache { - /// The syntax tree prior to the edit - SourceFileSyntax OldSyntaxTree; - - /// The edits that were made from the source file that created this cache to - /// the source file that is now parsed incrementally - llvm::SmallVector Edits; - - /// The \c RawSyntax nodes that got reused are collected in this vector. - std::unordered_set ReusedNodes; - -public: - SyntaxParsingCache(SourceFileSyntax OldSyntaxTree) - : OldSyntaxTree(OldSyntaxTree) {} - - /// Add an edit that transformed the source file which created this cache into - /// the source file that is now being parsed incrementally. \c Start must be a - /// position from the *original* source file, and it must not overlap any - /// other edits previously added. For instance, given: - /// (aaa, bbb) - /// 0123456789 - /// When you want to turn this into: - /// (c, dddd) - /// 0123456789 - /// edits should be: { 1, 4, 1 } and { 6, 9, 4 }. - void addEdit(size_t Start, size_t End, size_t ReplacementLength); - - /// Check if a syntax node of the given kind at the given position can be - /// reused for a new syntax tree. - llvm::Optional lookUp(size_t NewPosition, SyntaxKind Kind); - - const std::unordered_set &getReusedNodes() const { - return ReusedNodes; - } - - /// Get the source regions of the new source file, represented by - /// \p SyntaxTree that have been reused as part of the incremental parse. - std::vector - getReusedRegions(const SourceFileSyntax &SyntaxTree) const; - - /// Translates a post-edit position to a pre-edit position by undoing the - /// specified edits. Returns \c None if no pre-edit position exists because - /// the post-edit position has been inserted by an edit. - /// - /// Should not be invoked externally. Only public for testing purposes. - static Optional - translateToPreEditPosition(size_t PostEditPosition, - ArrayRef Edits); - -private: - llvm::Optional lookUpFrom(const Syntax &Node, size_t NodeStart, - size_t Position, SyntaxKind Kind); - - bool nodeCanBeReused(const Syntax &Node, size_t Position, size_t NodeStart, - SyntaxKind Kind) const; -}; - -} // namespace swift - -#endif // SWIFT_SYNTAX_PARSING_CACHE_H diff --git a/include/swift/Parse/SyntaxParsingContext.h b/include/swift/Parse/SyntaxParsingContext.h deleted file mode 100644 index 3a1e5eb4fba70..0000000000000 --- a/include/swift/Parse/SyntaxParsingContext.h +++ /dev/null @@ -1,372 +0,0 @@ -//===----------- SyntaxParsingContext.h -==============----------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_PARSE_SYNTAXPARSINGCONTEXT_H -#define SWIFT_PARSE_SYNTAXPARSINGCONTEXT_H - -#include "llvm/ADT/PointerUnion.h" -#include "swift/Basic/Debug.h" -#include "swift/Basic/SourceLoc.h" -#include "swift/Parse/ParsedRawSyntaxNode.h" -#include "swift/Parse/ParsedRawSyntaxRecorder.h" - -namespace swift { - -using namespace swift::syntax; - -class ParsedSyntax; -class ParsedTokenSyntax; -struct ParsedTrivia; -class SourceFile; -enum class tok : uint8_t; -class Token; -class DiagnosticEngine; - -namespace syntax { -enum class SyntaxKind : uint16_t; -} - -enum class SyntaxContextKind { - Decl, - Stmt, - Expr, - Type, - Pattern, - Syntax, -}; - -} // end namespace swift - -namespace llvm { -raw_ostream &operator<<(raw_ostream &OS, swift::SyntaxContextKind Kind); -} // end namespace llvm - -namespace swift { - -enum class SyntaxNodeCreationKind { - /// This is for \c SyntaxParsingContext to collect the syntax data and create - /// a 'recorded' ParsedRawSyntaxNode object, which would be a result of - /// passing the index data to the \c SyntaxParseActions implementation. - Recorded, - /// This is for \c SyntaxParsingContext to collect the syntax data and create - /// a 'deferred' ParsedRawSyntaxNode object, which captures the data for a - /// \c SyntaxParseActions invocation to occur later. - /// - /// This is intended to be used for when it's not clear what will be the final - /// syntax node in the current parsing context. - Deferred, -}; - -constexpr size_t SyntaxAlignInBits = 3; - -/// RAII object which receive RawSyntax parts. On destruction, this constructs -/// a specified syntax node from received parts and propagate it to the parent -/// context. -/// -/// e.g. -/// parseExprParen() { -/// SyntaxParsingContext LocalCtxt(SyntaxContext, SyntaxKind::ParenExpr); -/// consumeToken(tok::l_paren) // In consumeToken(), a RawTokenSyntax is -/// // added to the context. -/// parseExpr(); // On returning from parseExpr(), a Expr Syntax node is -/// // created and added to the context. -/// consumeToken(tok::r_paren) -/// // Now the context holds { '(' Expr ')' }. -/// // From these parts, it creates ParenExpr node and add it to the parent. -/// } -class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext { -public: - /// The shared data for all syntax parsing contexts with the same root. - /// This should be accessible from the root context only. - struct alignas(1 << SyntaxAlignInBits) RootContextData { - // The source file under parsing. - SourceFile &SF; - - // Where to issue diagnostics. - DiagnosticEngine &Diags; - - SourceManager &SourceMgr; - - unsigned BufferID; - - // Storage for Collected parts. - std::vector Storage; - - ParsedRawSyntaxRecorder Recorder; - - llvm::BumpPtrAllocator ScratchAlloc; - - RootContextData(SourceFile &SF, DiagnosticEngine &Diags, - SourceManager &SourceMgr, unsigned BufferID, - std::shared_ptr spActions) - : SF(SF), Diags(Diags), SourceMgr(SourceMgr), BufferID(BufferID), - Recorder(std::move(spActions)) {} - }; - -private: - /// Indicates what action should be performed on the destruction of - /// SyntaxParsingContext - enum class AccumulationMode { - // Coerce the result to one of ContextKind. - // E.g. for ContextKind::Expr, passthrough if the result is CallExpr, whereas - // for non Exprs. - CoerceKind, - - // Construct a result Syntax with specified SyntaxKind. - CreateSyntax, - - /// Construct a deferred raw node, to be recorded later. - DeferSyntax, - - // Pass through all parts to the parent context. - Transparent, - - // Discard all parts in the context. - Discard, - - // The node has been found as incremental update and all parts shall be - // discarded. - SkippedForIncrementalUpdate, - - // Construct SourceFile syntax to the specified SF. - Root, - - // Invalid. - NotSet, - }; - - // When this context is a root, this points to an instance of RootContextData; - // When this context isn't a root, this points to the parent context. - const llvm::PointerUnion - RootDataOrParent; - - // Reference to the - SyntaxParsingContext *&CtxtHolder; - - RootContextData *RootData; - - // Offset for 'Storage' this context owns from. - const size_t Offset; - - // Operation on destruction. - AccumulationMode Mode = AccumulationMode::NotSet; - - // Additional info depending on \c Mode. - union { - // For AccumulationMode::CreateSyntax; desired syntax node kind. - SyntaxKind SynKind; - // For AccumulationMode::CoerceKind; desired syntax node category. - SyntaxContextKind CtxtKind; - }; - - /// true if it's in backtracking context. - bool IsBacktracking = false; - - /// true if ParsedSyntaxBuilders and ParsedSyntaxRecorder should create - /// deferred nodes - bool ShouldDefer = false; - - // If false, context does nothing. - bool Enabled; - - /// Create a syntax node using the tail \c N elements of collected parts and - /// replace those parts with the single result. - void createNodeInPlace(SyntaxKind Kind, size_t N, - SyntaxNodeCreationKind nodeCreateK); - - ArrayRef getParts() const { - return llvm::makeArrayRef(getStorage()).drop_front(Offset); - } - MutableArrayRef getParts() { - return llvm::makeMutableArrayRef(getStorage().data(), getStorage().size()).drop_front(Offset); - } - - ParsedRawSyntaxNode makeUnknownSyntax(SyntaxKind Kind, - MutableArrayRef Parts); - ParsedRawSyntaxNode createSyntaxAs(SyntaxKind Kind, - MutableArrayRef Parts, - SyntaxNodeCreationKind nodeCreateK); - Optional bridgeAs(SyntaxContextKind Kind, - MutableArrayRef Parts); - -public: - /// Construct root context. - SyntaxParsingContext(SyntaxParsingContext *&CtxtHolder, SourceFile &SF, - unsigned BufferID, - std::shared_ptr SPActions); - - /// Designated constructor for child context. - SyntaxParsingContext(SyntaxParsingContext *&CtxtHolder) - : RootDataOrParent(CtxtHolder), CtxtHolder(CtxtHolder), - RootData(CtxtHolder->RootData), Offset(RootData->Storage.size()), - IsBacktracking(CtxtHolder->IsBacktracking), - ShouldDefer(CtxtHolder->ShouldDefer), - Enabled(CtxtHolder->isEnabled()) { - assert(CtxtHolder->isTopOfContextStack() && - "SyntaxParsingContext cannot have multiple children"); - assert(CtxtHolder->Mode != AccumulationMode::SkippedForIncrementalUpdate && - "Cannot create child context for a node loaded from the cache"); - CtxtHolder = this; - } - - SyntaxParsingContext(SyntaxParsingContext *&CtxtHolder, SyntaxContextKind Kind) - : SyntaxParsingContext(CtxtHolder) { - setCoerceKind(Kind); - } - - SyntaxParsingContext(SyntaxParsingContext *&CtxtHolder, SyntaxKind Kind) - : SyntaxParsingContext(CtxtHolder) { - setCreateSyntax(Kind); - } - - SyntaxParsingContext(const SyntaxParsingContext &other) = delete; - SyntaxParsingContext &operator=(const SyntaxParsingContext &other) = delete; - - ~SyntaxParsingContext(); - - /// Try looking up if an unmodified node exists at \p LexerOffset of the same - /// kind. If a node is found, replace the node that is currently being - /// constructed by the parsing context with the found node and return the - /// number of bytes the found node took up in the original source. The lexer - /// should pretend it has read these bytes and continue from the advanced - /// offset. If nothing is found \c 0 is returned. - size_t lookupNode(size_t LexerOffset, SourceLoc Loc); - - void disable() { Enabled = false; } - bool isEnabled() const { return Enabled; } - bool isRoot() const { return RootDataOrParent.is(); } - bool isTopOfContextStack() const { return this == CtxtHolder; } - - SyntaxParsingContext *getParent() const { - return RootDataOrParent.get(); - } - - RootContextData *getRootData() { return RootData; } - - const RootContextData *getRootData() const { return RootData; } - - std::vector &getStorage() { return getRootData()->Storage; } - - const std::vector &getStorage() const { - return getRootData()->Storage; - } - - const SyntaxParsingContext *getRoot() const; - - ParsedRawSyntaxRecorder &getRecorder() { return getRootData()->Recorder; } - const ParsedRawSyntaxRecorder &getRecorder() const { - return getRootData()->Recorder; - } - - llvm::BumpPtrAllocator &getScratchAlloc() { - return getRootData()->ScratchAlloc; - } - - /// Add RawSyntax to the parts. - void addRawSyntax(ParsedRawSyntaxNode &&Raw); - - /// Add Token with Trivia to the parts. - void addToken(Token &Tok, StringRef LeadingTrivia, StringRef TrailingTrivia); - - /// Add Syntax to the parts. - void addSyntax(ParsedSyntax &&Node); - - template - llvm::Optional popIf() { - auto &Storage = getStorage(); - if (Storage.size() <= Offset) - return llvm::None; - if (!SyntaxNode::kindof(Storage.back().getKind())) - return llvm::None; - auto rawNode = std::move(Storage.back()); - Storage.pop_back(); - return SyntaxNode(std::move(rawNode)); - } - - ParsedTokenSyntax popToken(); - - /// Create a node using the tail of the collected parts. The number of parts - /// is automatically determined from \c Kind. Node: limited number of \c Kind - /// are supported. See the implementation. - void createNodeInPlace(SyntaxKind Kind, - SyntaxNodeCreationKind nodeCreateK = SyntaxNodeCreationKind::Recorded); - - /// Squashing nodes from the back of the pending syntax list to a given syntax - /// collection kind. If there're no nodes that can fit into the collection kind, - /// this function does nothing. Otherwise, it creates a collection node in place - /// to contain all sequential suitable nodes from back. - void collectNodesInPlace(SyntaxKind CollectionKind, - SyntaxNodeCreationKind nodeCreateK = SyntaxNodeCreationKind::Recorded); - - /// On destruction, construct a specified kind of syntax node consuming the - /// collected parts, then append it to the parent context. - void setCreateSyntax(SyntaxKind Kind) { - Mode = AccumulationMode::CreateSyntax; - SynKind = Kind; - } - - /// Same as \c setCreateSyntax but create a deferred node instead of a - /// recorded one. - void setDeferSyntax(SyntaxKind Kind) { - Mode = AccumulationMode::DeferSyntax; - SynKind = Kind; - } - - /// On destruction, if the parts size is 1 and it's kind of \c Kind, just - /// append it to the parent context. Otherwise, create Unknown{Kind} node from - /// the collected parts. - void setCoerceKind(SyntaxContextKind Kind) { - Mode = AccumulationMode::CoerceKind; - CtxtKind = Kind; - } - - /// Move the collected parts to the tail of parent context. - void setTransparent() { Mode = AccumulationMode::Transparent; } - - /// This context is a back tracking context, so we should discard collected - /// parts on this context. - void setBackTracking() { - Mode = AccumulationMode::Discard; - IsBacktracking = true; - } - - /// Cancels backtracking state from the top of the context stack until `this` context. - void cancelBacktrack(); - - bool isBacktracking() const { return IsBacktracking; } - - void setShouldDefer(bool Value = true) { ShouldDefer = Value; } - - bool shouldDefer() const { - return ShouldDefer || IsBacktracking || Mode == AccumulationMode::Discard; - } - - /// Explicitly finalizing syntax tree creation. - /// This function will be called during the destroying of a root syntax - /// parsing context. However, we can explicitly call this function to get - /// the syntax tree before closing the root context. - OpaqueSyntaxNode finalizeRoot(); - - /// Make a missing node corresponding to the given token kind and - /// push this node into the context. The synthesized node can help - /// the creation of valid syntax nodes. - void synthesize(tok Kind, SourceLoc Loc); - - /// Dump the nodes that are in the storage stack of the SyntaxParsingContext - SWIFT_DEBUG_DUMPER(dumpStorage()); - - void dumpStack(llvm::raw_ostream &OS) const; - SWIFT_DEBUG_DUMPER(dumpStack()) { dumpStack(llvm::errs()); } -}; - -} // namespace swift -#endif // SWIFT_SYNTAX_PARSING_CONTEXT_H diff --git a/include/swift/Parse/Token.h b/include/swift/Parse/Token.h index f48e003904f95..6a890ba52992d 100644 --- a/include/swift/Parse/Token.h +++ b/include/swift/Parse/Token.h @@ -19,13 +19,23 @@ #include "swift/Basic/SourceLoc.h" #include "swift/Basic/LLVM.h" -#include "swift/Syntax/TokenKinds.h" +#include "swift/Parse/Token.h" #include "swift/Config.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSwitch.h" namespace swift { +enum class tok : uint8_t { +#define TOKEN(X) X, +#include "swift/AST/TokenKinds.def" + + NUM_TOKENS +}; + +/// If a token kind has determined text, return the text; otherwise assert. +StringRef getTokenText(tok kind); + /// Token - This structure provides full information about a lexed token. /// It is not intended to be space efficient, it is intended to return as much /// information as possible about each returned token. This is expected to be @@ -200,7 +210,7 @@ class Token { bool isKeyword() const { switch (Kind) { #define KEYWORD(X) case tok::kw_##X: return true; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" default: return false; } } @@ -220,7 +230,7 @@ class Token { bool isPunctuation() const { switch (Kind) { #define PUNCTUATOR(Name, Str) case tok::Name: return true; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" default: return false; } } diff --git a/include/swift/Subsystems.h b/include/swift/Subsystems.h index 12fee6c0ad0fa..cc636d2c65939 100644 --- a/include/swift/Subsystems.h +++ b/include/swift/Subsystems.h @@ -60,7 +60,6 @@ namespace swift { /// \c llvm::PointerIntPair. This is in particular guaranteed for pointers /// to C/C++ objects and for pointers that were generated by Swift and passed /// to the compiler via a C API (in particular \c CLibParseActions ). - typedef const void *OpaqueSyntaxNode; class Parser; class SerializationOptions; class SILOptions; @@ -68,8 +67,6 @@ namespace swift { class SourceFile; enum class SourceFileKind; class SourceManager; - class SyntaxParseActions; - class SyntaxParsingCache; struct TBDGenOptions; class Token; class TopLevelContext; @@ -300,16 +297,14 @@ namespace swift { public: ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID, const LangOptions &LangOpts, const TypeCheckerOptions &TyOpts, - const SILOptions &SILOpts, StringRef ModuleName, - std::shared_ptr spActions = nullptr, - SyntaxParsingCache *SyntaxCache = nullptr); + const SILOptions &SILOpts, StringRef ModuleName); ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID); ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID, unsigned Offset, unsigned EndOffset); ~ParserUnit(); - OpaqueSyntaxNode parse(); + void parse(); Parser &getParser(); SourceFile &getSourceFile(); diff --git a/include/swift/Syntax/AbsoluteRawSyntax.h b/include/swift/Syntax/AbsoluteRawSyntax.h deleted file mode 100644 index df3fc96dd56d6..0000000000000 --- a/include/swift/Syntax/AbsoluteRawSyntax.h +++ /dev/null @@ -1,504 +0,0 @@ -//===--- AbsoluteRawSyntax.h ------------------------------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_ABSOLUTERAWSYNTAX_H -#define SWIFT_SYNTAX_ABSOLUTERAWSYNTAX_H - -#include "swift/Basic/SourceLoc.h" -#include "swift/Syntax/RawSyntax.h" - -#include "llvm/ADT/STLForwardCompat.h" - -namespace swift { -namespace syntax { - -/// Type-safe wrapper around a \c size_t that describes a node's index in its -/// tree. This represents the index at which the node will be encountered during -/// a depth-first traversal of the tree. -class SyntaxIndexInTree { - size_t IndexInTree; - - explicit SyntaxIndexInTree(size_t IndexInTree) : IndexInTree(IndexInTree) {} - -public: - /// Create an *uninitialized* \c SyntaxIndexInTree. - SyntaxIndexInTree() {} - static SyntaxIndexInTree zero() { return SyntaxIndexInTree(0); } - - /// Assuming that this index points to the start of \p Raw, advance it so that - /// it points to the next sibling of \p Raw. - SyntaxIndexInTree advancedBy(const RawSyntax *Raw) const { - auto NewIndexInTree = IndexInTree; - if (Raw) { - NewIndexInTree += Raw->getTotalNodes(); - } - return SyntaxIndexInTree(NewIndexInTree); - } - - /// Assuming that this index points to the next sibling of \p Raw, reverse it - /// so that it points to the start of \p Raw. - SyntaxIndexInTree reversedBy(const RawSyntax *Raw) const { - auto NewIndexInTree = IndexInTree; - if (Raw) { - NewIndexInTree -= Raw->getTotalNodes(); - } - return SyntaxIndexInTree(NewIndexInTree); - } - - /// Advance this index to point to its first immediate child. - SyntaxIndexInTree advancedToFirstChild() const { - auto NewIndexInTree = IndexInTree + 1; - return SyntaxIndexInTree(NewIndexInTree); - } - - bool operator==(SyntaxIndexInTree Other) const { - return IndexInTree == Other.IndexInTree; - } -}; - -/// A syntax identifier that globally identifies a \c Syntax node. -/// In contrast to \c NodeId of \c RawSyntax, this also takes into account the -/// node's position in the tree. For example, a source file may contain two -/// int literals with value 0. The corresponding \c RawSyntax nodes can share -/// the same \c NodeId. However, because the literals occur in different -/// locations of the syntax tree, their \c SyntaxIdentifiers are different. -class SyntaxIdentifier { -public: - using RootIdType = size_t; - -private: - static std::atomic NextUnusedRootId; - - /// An integer that identifies the tree in which the node represented by this - /// identifier lives. - RootIdType RootId; - - /// The position of the node represented by this identifier in the syntax - /// tree. - SyntaxIndexInTree IndexInTree; - -public: - /// Create an *uninitialized* \c SyntaxIdentifier. - SyntaxIdentifier() {} - - SyntaxIdentifier(RootIdType RootId, SyntaxIndexInTree IndexInTree) - : RootId(RootId), IndexInTree(IndexInTree) { - assert(RootId < NextUnusedRootId && "New RootIds should only be created " - "using the newRoot constructor."); - } - - /// Create a SyntaxIdentifier that refers to the root of a new syntax tree. - static SyntaxIdentifier newRoot() { - return SyntaxIdentifier(NextUnusedRootId++, SyntaxIndexInTree::zero()); - } - - RootIdType getRootId() const { return RootId; } - SyntaxIndexInTree getIndexInTree() const { return IndexInTree; } - - /// Assuming that this identifier points to the start of \p Raw, advance it so - /// that it points to the next sibling of \p Raw. - SyntaxIdentifier advancedBy(const RawSyntax *Raw) const { - auto NewIndexInTree = IndexInTree.advancedBy(Raw); - return SyntaxIdentifier(RootId, NewIndexInTree); - } - - /// Assuming that this identifier points to the next sibling of \p Raw, - /// reverse it so that it points to the start of \p Raw. - SyntaxIdentifier reversedBy(const RawSyntax *Raw) const { - auto NewIndexInTree = IndexInTree.reversedBy(Raw); - return SyntaxIdentifier(RootId, NewIndexInTree); - } - - /// Get the identifier of the first immediate child. - SyntaxIdentifier advancedToFirstChild() const { - auto NewIndexInTree = IndexInTree.advancedToFirstChild(); - return SyntaxIdentifier(RootId, NewIndexInTree); - } - - bool operator==(SyntaxIdentifier Other) const { - return RootId == Other.RootId && IndexInTree == Other.IndexInTree; - } -}; - -/// Represents a node's position in a syntax tree, described by its overall -/// textual offset and the position within its parent. -class AbsoluteSyntaxPosition { -public: - using OffsetType = uint32_t; - using IndexInParentType = uint32_t; - -private: - /// The text offset where this node starts within its syntax tree, counted in - /// UTF-8 bytes. - OffsetType Offset; - - /// The node's index within its parent, i.e. - /// `node.parent.childAt(IndexInParent) = node`. - IndexInParentType IndexInParent; - -public: - /// Create an *uninitialized* \c AbsoluteSyntaxPosition. - AbsoluteSyntaxPosition() {} - - AbsoluteSyntaxPosition(OffsetType Offset, IndexInParentType IndexInParent) - : Offset(Offset), IndexInParent(IndexInParent) {} - - /// Create a new \c AbsoluteSyntaxPosition that refers to the root of a syntax - /// tree. - static AbsoluteSyntaxPosition forRoot() { - return AbsoluteSyntaxPosition(0, 0); - } - - OffsetType getOffset() const { return Offset; } - IndexInParentType getIndexInParent() const { return IndexInParent; } - - /// Assuming that this position points to the start of \p Raw, advance it so - /// that it points to the next sibling of \p Raw. - AbsoluteSyntaxPosition advancedBy(const RawSyntax *Raw) const { - OffsetType NewOffset = Offset; - if (Raw) { - NewOffset += Raw->getTextLength(); - } - IndexInParentType NewIndexInParent = IndexInParent + 1; - return AbsoluteSyntaxPosition(NewOffset, NewIndexInParent); - } - - /// Assuming that this position points to the next sibling of \p Raw, reverse - /// it so that it points to the start of \p Raw. - AbsoluteSyntaxPosition reversedBy(const RawSyntax *Raw) const { - OffsetType NewOffset = Offset; - if (Raw) { - NewOffset -= Raw->getTextLength(); - } - IndexInParentType NewIndexInParent = IndexInParent - 1; - return AbsoluteSyntaxPosition(NewOffset, NewIndexInParent); - } - - /// Get the position of the node's first immediate child. - AbsoluteSyntaxPosition advancedToFirstChild() const { - return AbsoluteSyntaxPosition(Offset, 0); - } -}; - -/// A type-safe wrapper that describes a node's textual position within a source -/// file, represented by its UTF-8 byte offset from the start. -class AbsoluteOffsetPosition { - AbsoluteSyntaxPosition::OffsetType Offset; - -public: - /// Create an *uninitialized* \c AbsoluteOffsetPosition. - AbsoluteOffsetPosition() {} - explicit AbsoluteOffsetPosition(AbsoluteSyntaxPosition::OffsetType Offset) - : Offset(Offset) {} - AbsoluteOffsetPosition(AbsoluteSyntaxPosition Position) - : Offset(Position.getOffset()) {} - - AbsoluteSyntaxPosition::OffsetType getOffset() const { return Offset; } - - /// Return a position that has been advanced by \p Advance UTF-8 bytes.s - AbsoluteOffsetPosition advancedBy(int Advance) { - return AbsoluteOffsetPosition(Offset + Advance); - } -}; - -/// Various information that enrich a \c RawSyntax node with information on how -/// it's located within the syntax tree. -class AbsoluteSyntaxInfo { - AbsoluteSyntaxPosition Position; - SyntaxIdentifier NodeId; - -public: - /// Create an *uninitialized* \c AbsoluteSyntaxInfo. - AbsoluteSyntaxInfo() {} - AbsoluteSyntaxInfo(AbsoluteSyntaxPosition Position, SyntaxIdentifier NodeId) - : Position(Position), NodeId(NodeId) {} - - static AbsoluteSyntaxInfo forRoot() { - return AbsoluteSyntaxInfo(AbsoluteSyntaxPosition::forRoot(), - SyntaxIdentifier::newRoot()); - } - - AbsoluteSyntaxPosition getPosition() const { return Position; } - SyntaxIdentifier getNodeId() const { return NodeId; } - - /// Assuming that this info points to the start of \p Raw, advance it so - /// that it points to the next sibling of \p Raw. - AbsoluteSyntaxInfo advancedBy(const RawSyntax *Raw) const { - auto NewNodeId = NodeId.advancedBy(Raw); - auto NewPosition = Position.advancedBy(Raw); - return AbsoluteSyntaxInfo(NewPosition, NewNodeId); - } - - /// Assuming that this info points to the next sibling of \p Raw, reverse - /// it so that it points to the start of \p Raw. - AbsoluteSyntaxInfo reversedBy(const RawSyntax *Raw) const { - auto NewNodeId = NodeId.reversedBy(Raw); - auto NewPosition = Position.reversedBy(Raw); - return AbsoluteSyntaxInfo(NewPosition, NewNodeId); - } - - /// Get the information of the node's first immediate child. - AbsoluteSyntaxInfo advancedToFirstChild() const { - auto NewNodeId = NodeId.advancedToFirstChild(); - auto NewPosition = Position.advancedToFirstChild(); - return AbsoluteSyntaxInfo(NewPosition, NewNodeId); - } -}; - -/// A \c RawSyntax node that is enriched with information of its position -/// within the syntax tree it lives in. -class AbsoluteRawSyntax { - /// OptionalStorage is a friend so it can access the \c nullptr initializer - /// and \c isNull. - template - friend class llvm::optional_detail::OptionalStorage; - - const RawSyntax *Raw; - AbsoluteSyntaxInfo Info; - - /// Whether this is a null \c AbsoluteRawSyntax. - bool isNull() const { return Raw == nullptr; } - - /// Create a null \c AbsoluteRawSyntax. This should only be used in \c - /// AbsoluteRawSyntax's \c OptionalStorage. - explicit AbsoluteRawSyntax(std::nullptr_t) : Raw(nullptr) {} - -public: - /// Create an *uninitialized* \c AbsoluteRawSyntax. - explicit AbsoluteRawSyntax() {} - - /// Create a new \c AbsoluteRawData backed by \p Raw and with additional \p - /// Info. The caller of this constructor is responsible to ensure that the - /// Arena of \p Raw (and thus \p Raw itself) outlives this \c - /// AbsoluteRawSyntax. - AbsoluteRawSyntax(const RawSyntax *Raw, AbsoluteSyntaxInfo Info) - : Raw(Raw), Info(Info) { - assert(Raw != nullptr && - "A AbsoluteRawSyntax created through the memberwise constructor " - "should always have a RawSyntax"); - } - - /// Construct a \c AbsoluteRawSyntax for a \c RawSyntax node that represents - /// the syntax tree's root. - static AbsoluteRawSyntax forRoot(const RawSyntax *Raw) { - return AbsoluteRawSyntax(Raw, AbsoluteSyntaxInfo::forRoot()); - } - - const RawSyntax *getRaw() const { - assert(!isNull() && "Cannot get Raw of a null AbsoluteRawSyntax"); - return Raw; - } - - AbsoluteSyntaxInfo getInfo() const { - assert(!isNull() && "Cannot get Raw of a null AbsoluteRawSyntax"); - return Info; - } - - /// Get the position at which the leading trivia of this node starts. - AbsoluteSyntaxPosition getPosition() const { - return getInfo().getPosition(); - }; - - SyntaxIdentifier getNodeId() const { return getInfo().getNodeId(); }; - - AbsoluteSyntaxPosition::IndexInParentType getIndexInParent() const { - return getPosition().getIndexInParent(); - } - - size_t getNumChildren() const { return getRaw()->getLayout().size(); } - - /// Get the child at \p Index if it exists. If the node does not have a child - /// at \p Index, return \c None. Asserts that \p Index < \c NumChildren - inline Optional - getChild(AbsoluteSyntaxPosition::IndexInParentType Index) const; - - /// Get the child at \p Index, asserting that it exists. This is slightly - /// more performant than \c getChild in these cases since the \c - /// AbsoluteRawSyntax node does not have to be wrapped in an \c Optional. - AbsoluteRawSyntax - getPresentChild(AbsoluteSyntaxPosition::IndexInParentType Index) const { - assert(Index < getNumChildren() && "Index out of bounds"); - auto RawChild = getRaw()->getChild(Index); - assert(RawChild && - "Child retrieved using getPresentChild must always exist"); - - AbsoluteSyntaxPosition Position = getPosition().advancedToFirstChild(); - SyntaxIdentifier NodeId = getNodeId().advancedToFirstChild(); - - for (size_t I = 0; I < Index; ++I) { - Position = Position.advancedBy(getRaw()->getChild(I)); - NodeId = NodeId.advancedBy(getRaw()->getChild(I)); - } - - AbsoluteSyntaxInfo Info(Position, NodeId); - return AbsoluteRawSyntax(RawChild, Info); - } - - /// Get the first non-missing token node in this tree. Return \c None if - /// this node does not contain non-missing tokens. - inline Optional getFirstToken() const; - - /// Get the last non-missing token node in this tree. Return \c None if - /// this node does not contain non-missing tokens. - inline Optional getLastToken() const; - - /// Construct a new \c AbsoluteRawSyntax node that has the same info as the - /// current one, but - /// - the \p NewRaw as the backing storage - /// - the \p NewRootId as the RootId - AbsoluteRawSyntax - replacingSelf(const RawSyntax *NewRaw, - SyntaxIdentifier::RootIdType NewRootId) const { - SyntaxIdentifier NewNodeId(NewRootId, - getInfo().getNodeId().getIndexInTree()); - AbsoluteSyntaxInfo NewInfo(getInfo().getPosition(), NewNodeId); - return AbsoluteRawSyntax(NewRaw, NewInfo); - } -}; - -} // end namespace syntax -} // end namespace swift - -namespace llvm { -raw_ostream &operator<<(raw_ostream &OS, - swift::syntax::AbsoluteOffsetPosition Pos); - -namespace optional_detail { - -using swift::syntax::AbsoluteRawSyntax; - -/// A custom \c OptionalStorage implementation for \c AbsoluteRawSyntax that -/// makes \c Optional a zero-cost wrapper around \c -/// AbsoluteRawSyntax by using a special (externally not accessible) null \c -/// AbsoluteRawSyntax to represent a missing value. -template <> -class OptionalStorage { - AbsoluteRawSyntax Storage; - -public: - OptionalStorage() : Storage(nullptr) {} - OptionalStorage(OptionalStorage const &other) = default; - OptionalStorage(OptionalStorage &&other) = default; - - template - explicit OptionalStorage(llvm::in_place_t, ArgTypes &&...Args) - : Storage(std::forward(Args)...) {} - - void reset() { Storage = AbsoluteRawSyntax(nullptr); } - - bool has_value() const { return !Storage.isNull(); } - bool hasValue() const { return !Storage.isNull(); } - - AbsoluteRawSyntax &value() & { - assert(hasValue()); - return Storage; - } - AbsoluteRawSyntax &getValue() & { - assert(hasValue()); - return Storage; - } - AbsoluteRawSyntax const &value() const & { - assert(hasValue()); - return Storage; - } - AbsoluteRawSyntax const &getValue() const & { - assert(hasValue()); - return Storage; - } -#if LLVM_HAS_RVALUE_REFERENCE_THIS - AbsoluteRawSyntax &&value() &&noexcept { - assert(hasValue()); - return std::move(Storage); - } - AbsoluteRawSyntax &&getValue() &&noexcept { - assert(hasValue()); - return std::move(Storage); - } -#endif - - template - void emplace(Args &&...args) { - Storage = AbsoluteRawSyntax(std::forward(args)...); - } - - OptionalStorage &operator=(const AbsoluteRawSyntax &AbsoluteRaw) { - Storage = AbsoluteRaw; - return *this; - } - - OptionalStorage &operator=(AbsoluteRawSyntax &&AbsoluteRaw) { - Storage = std::move(AbsoluteRaw); - return *this; - } - - OptionalStorage &operator=(OptionalStorage const &other) = default; - OptionalStorage &operator=(OptionalStorage &&other) = default; -}; -} // namespace optional_detail -} // end namespace llvm - -namespace swift { -namespace syntax { - -Optional AbsoluteRawSyntax::getChild( - AbsoluteSyntaxPosition::IndexInParentType Index) const { - assert(Index < getNumChildren() && "Index out of bounds"); - if (getRaw()->getChild(Index)) { - return getPresentChild(Index); - } else { - return None; - } -} - -Optional AbsoluteRawSyntax::getFirstToken() const { - if (getRaw()->isToken() && !getRaw()->isMissing()) { - return *this; - } - - size_t NumChildren = getNumChildren(); - for (size_t I = 0; I < NumChildren; ++I) { - if (auto Child = getChild(I)) { - if (Child->getRaw()->isMissing()) { - continue; - } - - if (auto Token = Child->getFirstToken()) { - return Token; - } - } - } - return None; -} - -Optional AbsoluteRawSyntax::getLastToken() const { - if (getRaw()->isToken() && !getRaw()->isMissing()) { - return *this; - } - - for (int I = getNumChildren() - 1; I >= 0; --I) { - if (auto Child = getChild(I)) { - if (Child->getRaw()->isMissing()) { - continue; - } - - if (auto Token = Child->getLastToken()) { - return Token; - } - } - } - return None; -} - -} // end namespace syntax -} // end namespace swift - -#endif // SWIFT_SYNTAX_ABSOLUTERAWSYNTAX_H diff --git a/include/swift/Syntax/AtomicCache.h b/include/swift/Syntax/AtomicCache.h deleted file mode 100644 index e615ea90067c0..0000000000000 --- a/include/swift/Syntax/AtomicCache.h +++ /dev/null @@ -1,75 +0,0 @@ -//===------------- AtomicCache.h - Lazy Atomic Cache ------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_ATOMICCACHE_H -#define SWIFT_SYNTAX_ATOMICCACHE_H - -#include -#include "swift/Syntax/References.h" -#include "llvm/ADT/STLExtras.h" - -namespace swift { - -/// AtomicCache is an atomic cache for a reference-counted value. It maintains -/// a reference-counted pointer with a facility for atomically getting or -/// creating it with a lambda. -template -class AtomicCache { - // Whatever type is created through this cache must be pointer-sized, - // otherwise, we can't pretend it's a uintptr_t and use its - // compare_exchange_strong. - static_assert(sizeof(uintptr_t) == sizeof(RC), - "RC must be pointer sized!"); -private: - /// This must only be mutated in one place: AtomicCache::getOrCreate. - mutable RC Storage = nullptr; - -public: - /// The empty constructor initializes the storage to nullptr. - AtomicCache() {} - - /// Gets the value inside the cache, or creates it atomically using the - /// provided lambda if it doesn't already exist. - RC getOrCreate(llvm::function_ref()> Create) const { - auto &Ptr = *reinterpret_cast *>(&Storage); - - // If an atomic load gets an initialized value, then return Storage. - if (Ptr) { - return Storage; - } - - // We expect the uncached value to wrap a nullptr. If another thread - // beats us to caching the child, it'll be non-null, so we would - // leave it alone. - uintptr_t Expected = 0; - - // Make a RC at RefCount == 1, which we'll try to - // atomically swap in. - auto Data = Create(); - - // Try to swap in raw pointer value. - // If we won, then leave the RefCount == 1. - if (Ptr.compare_exchange_strong(Expected, - reinterpret_cast(Data.get()))) { - Data.resetWithoutRelease(); - } - - // Otherwise, the Data we just made is unfortunately useless. - // Let it die on this scope exit after its terminal release. - - return Storage; - } -}; - -} // end namespace swift - -#endif // SWIFT_SYNTAX_ATOMICCACHE_H diff --git a/include/swift/Syntax/CMakeLists.txt b/include/swift/Syntax/CMakeLists.txt deleted file mode 100644 index 19463fc97e6fc..0000000000000 --- a/include/swift/Syntax/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) - set(SWIFT_GYB_FLAGS --line-directive "^\"#line %(line)d \\\"%(file)s\\\"^\"") -else() - set(SWIFT_GYB_FLAGS --line-directive "\'#line" "%(line)d" "\"%(file)s\"\'") -endif() - -set(generated_include_sources - SyntaxKind.h.gyb - SyntaxNodes.h.gyb - SyntaxBuilders.h.gyb - SyntaxFactory.h.gyb - SyntaxVisitor.h.gyb - TokenKinds.def.gyb - Trivia.h.gyb) - -add_gyb_target(swift-syntax-generated-headers - "${generated_include_sources}") -set_property(TARGET swift-syntax-generated-headers - PROPERTY FOLDER "Miscellaneous") diff --git a/include/swift/Syntax/Format.h b/include/swift/Syntax/Format.h deleted file mode 100644 index 699de1513014b..0000000000000 --- a/include/swift/Syntax/Format.h +++ /dev/null @@ -1,38 +0,0 @@ -//===--- Format.h - Declaration Syntax Formatting Interface -----*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface for textually formatting Swift Syntax nodes. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_FORMAT_H -#define SWIFT_SYNTAX_FORMAT_H - -#include "swift/Syntax/Rewriter.h" - -namespace swift { -namespace syntax { - -/// A SyntaxRewriter for applying a set of formatting rules to a Syntax tree. -struct FormatSyntaxRewriter : public SyntaxRewriter { - virtual StructDeclSyntax - rewriteStructDecl(StructDeclSyntax Struct) override; -}; - -/// Format a Syntax tree with the given rules. -Syntax format(Syntax Tree); -// TODO: Representation for formatting rules, etc. This is just a figment -// for now. - -} -} -#endif diff --git a/include/swift/Syntax/RawSyntax.h b/include/swift/Syntax/RawSyntax.h deleted file mode 100644 index 6bac9ddfcfad9..0000000000000 --- a/include/swift/Syntax/RawSyntax.h +++ /dev/null @@ -1,542 +0,0 @@ -//===--- RawSyntax.h - Swift Raw Syntax Interface ---------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the RawSyntax type. -// -// These are the "backbone or "skeleton" of the Syntax tree, providing -// the recursive structure, child relationships, kind of node, etc. -// -// They are strictly immutable, so can be shared freely among Syntax nodes and -// have no specific identity. They could even in theory be shared for -// expressions like 1 + 1 + 1 + 1 - you don't need 7 syntax nodes to express -// that at this layer. -// -// These are internal implementation ONLY - do not expose anything involving -// RawSyntax publicly. Clients of lib/Syntax should not be aware that they -// exist. -// -// RawSyntax nodes always live in a SyntaxArena. The user of the RawSyntax nodes -// is responsible to ensure that the SyntaxArena stays alive while the RawSyntax -// nodes are being accessed. During tree creation this is done by the -// SyntaxTreeCreator holding on to the arena. In lib/Syntax, the root SyntaxData -// node retains the syntax arena. Should a RawSyntaxNode A reference a node B -// from a different arena, it automatically adds B's arena as a child arena of -// A's arena, thereby keeping B's arena alive as long as A's arena is alive. -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_RAWSYNTAX_H -#define SWIFT_SYNTAX_RAWSYNTAX_H - -#include "swift/Basic/Debug.h" -#include "swift/Basic/InlineBitfield.h" -#include "swift/Syntax/References.h" -#include "swift/Syntax/SyntaxArena.h" -#include "swift/Syntax/SyntaxKind.h" -#include "swift/Syntax/TokenKinds.h" -#include "swift/Syntax/Trivia.h" -#include "llvm/ADT/FoldingSet.h" -#include "llvm/ADT/IntrusiveRefCntPtr.h" -#include "llvm/ADT/PointerUnion.h" -#include "llvm/Support/Casting.h" -#include "llvm/Support/TrailingObjects.h" -#include "llvm/Support/raw_ostream.h" - -#include - -using llvm::cast; -using llvm::StringRef; - -#ifndef NDEBUG -#define syntax_assert_child_kind(Raw, Cursor, ExpectedKind) \ - do { \ - if (auto __Child = Raw->getChild(Cursor)) \ - assert(__Child->getKind() == ExpectedKind); \ - } while (false) -#else -#define syntax_assert_child_kind(Raw, Cursor, ExpectedKind) -#endif - -#ifndef NDEBUG -#define syntax_assert_child_token(Raw, CursorName, ...) \ - do { \ - bool __Found = false; \ - if (auto __Token = Raw->getChild(Cursor::CursorName)) { \ - assert(__Token->isToken()); \ - if (__Token->isPresent()) { \ - for (auto Token : {__VA_ARGS__}) { \ - if (__Token->getTokenKind() == Token) { \ - __Found = true; \ - break; \ - } \ - } \ - assert(__Found && "invalid token supplied for " #CursorName \ - ", expected one of {" #__VA_ARGS__ "}"); \ - } \ - } \ - } while (false) -#else -#define syntax_assert_child_token(Raw, CursorName, ...) -#endif - -#ifndef NDEBUG -#define syntax_assert_child_token_text(Raw, CursorName, TokenKind, ...) \ - do { \ - bool __Found = false; \ - if (auto __Child = Raw->getChild(Cursor::CursorName)) { \ - assert(__Child->isToken()); \ - if (__Child->isPresent()) { \ - assert(__Child->getTokenKind() == TokenKind); \ - for (auto __Text : {__VA_ARGS__}) { \ - if (__Child->getTokenText() == __Text) { \ - __Found = true; \ - break; \ - } \ - } \ - assert(__Found && "invalid text supplied for " #CursorName \ - ", expected one of {" #__VA_ARGS__ "}"); \ - } \ - } \ - } while (false) -#else -#define syntax_assert_child_token_text(Raw, CursorName, TokenKind, ...) -#endif - -#ifndef NDEBUG -#define syntax_assert_token_is(Tok, Kind, Text) \ - do { \ - assert(Tok.getTokenKind() == Kind); \ - assert(Tok.getText() == Text); \ - } while (false) -#else -#define syntax_assert_token_is(Tok, Kind, Text) -#endif - -namespace swift { -namespace syntax { - -class SyntaxArena; - -using CursorIndex = size_t; - -/// Get a numeric index suitable for array/vector indexing -/// from a syntax node's Cursor enum value. -template constexpr CursorIndex cursorIndex(CursorType C) { - return static_cast(C); -} - -/// An indicator of whether a Syntax node was found or written in the source. -/// -/// This is not an 'implicit' bit. -enum class SourcePresence : uint8_t { - /// The syntax was authored by a human and found, or was generated. - Present, - - /// The syntax was expected or optional, but not found in the source. - Missing, -}; - -/// The print option to specify when printing a raw syntax node. -struct SyntaxPrintOptions { - bool Visual = false; - bool PrintSyntaxKind = false; - bool PrintTrivialNodeKind = false; -}; - -/// RawSyntax - the strictly immutable, shared backing nodes for all syntax. -/// -/// This is implementation detail - do not expose it in public API. -class RawSyntax final - : private llvm::TrailingObjects { - friend TrailingObjects; - - /// The \c SyntaxArena in which this node was allocated. - SyntaxArena *Arena; - - /// Number of bytes this node takes up spelled out in the source code. - /// Always 0 if the node is missing. - uint32_t TextLength; - - /// Whether this piece of syntax was actually present in the source. - SourcePresence Presence; - - /// Whether this node is a token or layout node. Determines if \c Bits should - /// be interpreted as \c LayoutData or \c TokenData. - bool IsToken; - - struct LayoutData { - /// Number of children this "layout" node has. - uint32_t NumChildren; - /// Total number of sub nodes, i.e. number of transitive children of this - /// node. This does not include the node itself. - uint32_t TotalSubNodeCount; - /// The kind of syntax this node represents. - SyntaxKind Kind; - }; - - struct TokenData { - /// The pointers to the leading/trailing trivia and token texts. If their - /// lengths are greater than 0, these always reside in the node's \c Arena. - const char *LeadingTrivia; - const char *TokenText; - const char *TrailingTrivia; - uint32_t LeadingTriviaLength; - uint32_t TokenLength; - uint32_t TrailingTriviaLength; - /// The kind of token this "token" node represents. - tok TokenKind; - }; - - union BitsData { - LayoutData Layout; - TokenData Token; - - BitsData(const LayoutData &Layout) : Layout(Layout) {} - BitsData(const TokenData &Token) : Token(Token) {} - }; - BitsData Bits; - - size_t numTrailingObjects(OverloadToken) const { - return isToken() ? 0 : Bits.Layout.NumChildren; - } - - /// Constructor for creating layout nodes. - /// \p Children is an iterator that provides the child \c RawSyntax nodes. - /// It is only traversed once. - /// \p NumChildren is the number of elements provided by the \p Children - /// iterator. - /// If \p NodeId is \c None, the next free NodeId is used, if it is passed, - /// the caller needs to assure that the node ID has not been used yet. - template - RawSyntax(SyntaxKind Kind, ChildrenIteratorType ChildrenIt, - uint32_t NumChildren, SourcePresence Presence, - const RC &Arena) - : Arena(Arena.get()), TextLength(0 /*computed in body*/), - Presence(Presence), IsToken(false), - Bits(LayoutData{NumChildren, - /*TotalSubNodeCount=*/0 /*computed in body*/, Kind}) { - assert(Arena && "RawSyntax nodes must always be allocated in an arena"); - assert( - Kind != SyntaxKind::Token && - "'token' syntax node must be constructed with dedicated constructor"); - - const RawSyntax **TrailingChildren = - getTrailingObjects(); - for (uint32_t I = 0; I < NumChildren; - ++I, ++ChildrenIt, ++TrailingChildren) { - const RawSyntax *Child = *ChildrenIt; - if (Child) { - // Compute TextLength and TotalSubNodeCount of this node in place. - TextLength += Child->getTextLength(); - Bits.Layout.TotalSubNodeCount += Child->getTotalSubNodeCount() + 1; - - // If the child is stored in a different arena, it needs to stay alive - // as long as this node's arena is alive. - Arena->addChildArena(Child->Arena); - } - - *TrailingChildren = Child; - } - } - - /// Constructor for creating token nodes - /// \c SyntaxArena, that arena must be passed as \p Arena to retain the node's - /// underlying storage. - /// If \p NodeId is \c None, the next free NodeId is used, if it is passed, - /// the caller needs to assure that the NodeId has not been used yet. - RawSyntax(tok TokKind, StringRef Text, size_t TextLength, - StringRef LeadingTrivia, StringRef TrailingTrivia, - SourcePresence Presence, const RC &Arena) - : Arena(Arena.get()), TextLength(uint32_t(TextLength)), - Presence(Presence), IsToken(true), - Bits(TokenData{LeadingTrivia.data(), Text.data(), TrailingTrivia.data(), - uint32_t(LeadingTrivia.size()), uint32_t(Text.size()), - uint32_t(TrailingTrivia.size()), TokKind}) { - assert(Arena && "RawSyntax nodes must always be allocated in an arena"); - - if (Presence == SourcePresence::Missing) { - assert(TextLength == 0); - } else { - assert(TextLength == - LeadingTrivia.size() + Text.size() + TrailingTrivia.size()); - } - Arena->copyStringToArenaIfNecessary(Bits.Token.LeadingTrivia, - Bits.Token.LeadingTriviaLength); - Arena->copyStringToArenaIfNecessary(Bits.Token.TokenText, - Bits.Token.TokenLength); - Arena->copyStringToArenaIfNecessary(Bits.Token.TrailingTrivia, - Bits.Token.TrailingTriviaLength); - } - - /// Compute the node's text length by summing up the length of its children - size_t computeTextLength() { - size_t TextLength = 0; - for (size_t I = 0, NumChildren = getNumChildren(); I < NumChildren; ++I) { - auto ChildNode = getChild(I); - if (ChildNode && !ChildNode->isMissing()) { - TextLength += ChildNode->getTextLength(); - } - } - return TextLength; - } - -public: - /// \name Factory methods. - /// @{ - - /// Make a raw "layout" syntax node. - template - static const RawSyntax * - make(SyntaxKind Kind, ChildrenIteratorType ChildrenIt, size_t NumChildren, - SourcePresence Presence, const RC &Arena) { - assert(Arena && "RawSyntax nodes must always be allocated in an arena"); - auto size = totalSizeToAlloc(NumChildren); - void *data = Arena->Allocate(size, alignof(RawSyntax)); - return new (data) - RawSyntax(Kind, ChildrenIt, NumChildren, Presence, Arena); - } - - /// Convenience constructor to create a raw "layout" syntax node from an - /// \c llvm::ArrayRef containing the children. - static const RawSyntax * - make(SyntaxKind Kind, llvm::ArrayRef Children, - SourcePresence Presence, const RC &Arena) { - return make(Kind, Children.begin(), Children.size(), Presence, Arena); - } - - /// Make a raw "token" syntax node. - static const RawSyntax * - make(tok TokKind, StringRef Text, size_t TextLength, StringRef LeadingTrivia, - StringRef TrailingTrivia, SourcePresence Presence, - const RC &Arena) { - assert(Arena && "RawSyntax nodes must always be allocated in an arena"); - auto size = totalSizeToAlloc(0); - void *data = Arena->Allocate(size, alignof(RawSyntax)); - return new (data) RawSyntax(TokKind, Text, TextLength, LeadingTrivia, - TrailingTrivia, Presence, Arena); - } - - /// Make a raw "token" syntax node that was allocated in \p Arena. - static const RawSyntax * - makeAndCalcLength(tok TokKind, StringRef Text, StringRef LeadingTrivia, - StringRef TrailingTrivia, SourcePresence Presence, - const RC &Arena) { - size_t TextLength = 0; - if (Presence != SourcePresence::Missing) { - TextLength += LeadingTrivia.size(); - TextLength += Text.size(); - TextLength += TrailingTrivia.size(); - } - return make(TokKind, Text, TextLength, LeadingTrivia, TrailingTrivia, - Presence, Arena); - } - - /// Make a missing raw "layout" syntax node. - static const RawSyntax *missing(SyntaxKind Kind, - const RC &Arena) { - return make(Kind, {}, SourcePresence::Missing, Arena); - } - - /// Make a missing raw "token" syntax node. - static const RawSyntax *missing(tok TokKind, StringRef Text, - const RC &Arena) { - return make(TokKind, Text, /*TextLength=*/0, {}, {}, - SourcePresence::Missing, Arena); - } - - /// @} - - /// Return the arena in which this \c RawSyntax node has been allocated. - /// Keep in mind that the \c RawSyntax node *does not* retain the arena. - RC getArena() const { return RC(Arena); } - - SourcePresence getPresence() const { - return static_cast(Presence); - } - - SyntaxKind getKind() const { - if (isToken()) { - return SyntaxKind::Token; - } else { - return static_cast(Bits.Layout.Kind); - } - } - - /// Get the number of nodes included in the subtree spanned by this node. - /// This includes all transitive children and this node itself. - size_t getTotalNodes() const { return getTotalSubNodeCount() + 1; } - - /// Get the number of transitive children of this node. This does not include - /// the node itself. - size_t getTotalSubNodeCount() const { - if (isToken()) { - return 0; - } else { - return Bits.Layout.TotalSubNodeCount; - } - } - - /// Returns true if the node is "missing" in the source (i.e. it was - /// expected (or optional) but not written. - bool isMissing() const { return getPresence() == SourcePresence::Missing; } - - /// Returns true if the node is "present" in the source. - bool isPresent() const { return getPresence() == SourcePresence::Present; } - - /// Returns true if this raw syntax node is some kind of declaration. - bool isDecl() const { return isDeclKind(getKind()); } - - /// Returns true if this raw syntax node is some kind of type syntax. - bool isType() const { return isTypeKind(getKind()); } - - /// Returns true if this raw syntax node is some kind of statement. - bool isStmt() const { return isStmtKind(getKind()); } - - /// Returns true if this raw syntax node is some kind of expression. - bool isExpr() const { return isExprKind(getKind()); } - - /// Returns true if this raw syntax node is some kind of pattern. - bool isPattern() const { return isPatternKind(getKind()); } - - /// Return true is this raw syntax node is a unknown node. - bool isUnknown() const { return isUnknownKind(getKind()); } - - /// Return true if this raw syntax node is a token. - bool isToken() const { return IsToken; } - - /// \name Getter routines for SyntaxKind::Token. - /// @{ - - /// Get the kind of the token. - tok getTokenKind() const { - assert(isToken()); - return static_cast(Bits.Token.TokenKind); - } - - /// Return the text of the token as a reference. The referenced buffer may - /// disappear when the syntax node gets freed. - StringRef getTokenText() const { - assert(isToken()); - return StringRef(Bits.Token.TokenText, Bits.Token.TokenLength); - } - - /// Return the unparsed leading trivia of the token. - StringRef getLeadingTrivia() const { - assert(isToken()); - return StringRef(Bits.Token.LeadingTrivia, Bits.Token.LeadingTriviaLength); - } - - /// Return the unparsed trailing trivia of the token. - StringRef getTrailingTrivia() const { - assert(isToken()); - return StringRef(Bits.Token.TrailingTrivia, - Bits.Token.TrailingTriviaLength); - } - - /// Return pieces that make up the leading trivia of the token. - /// Note that this triggers trivia parsing which may be expensive. If the - /// trivia pieces are required multiple times, consider caching them. - Trivia getLeadingTriviaPieces() const; - - /// Return the trailing trivia list of the token. - /// Note that this triggers trivia parsing which may be expensive. If the - /// trivia pieces are required multiple times, consider caching them. - Trivia getTrailingTriviaPieces() const; - - /// Return \c true if this is the given kind of token. - bool isToken(tok K) const { return isToken() && getTokenKind() == K; } - - /// @} - - /// \name Transform routines for "token" nodes. - /// @{ - - /// Return a new token like this one, but with the given leading - /// trivia instead. - const RawSyntax *withLeadingTrivia(StringRef NewLeadingTrivia) const { - return makeAndCalcLength(getTokenKind(), getTokenText(), NewLeadingTrivia, - getTrailingTrivia(), getPresence(), Arena); - } - - /// Return a new token like this one, but with the given trailing - /// trivia instead. - const RawSyntax *withTrailingTrivia(StringRef NewTrailingTrivia) const { - return makeAndCalcLength(getTokenKind(), getTokenText(), getLeadingTrivia(), - NewTrailingTrivia, getPresence(), Arena); - } - - /// @} - - /// \name Getter routines for "layout" nodes. - /// @{ - - /// Get the child nodes. - ArrayRef getLayout() const { - if (isToken()) - return {}; - return {getTrailingObjects(), Bits.Layout.NumChildren}; - } - - size_t getNumChildren() const { - if (isToken()) - return 0; - return Bits.Layout.NumChildren; - } - - /// Get a child based on a particular node's "Cursor", indicating - /// the position of the terms in the production of the Swift grammar. - const RawSyntax *getChild(CursorIndex Index) const { - assert(Index < getNumChildren() && "Index out of bounds"); - return getLayout()[Index]; - } - - /// Return the number of bytes this node takes when spelled out in the source - /// including trivia. - size_t getTextLength() const { return TextLength; } - - /// @} - - /// \name Transform routines for "layout" nodes. - /// @{ - - /// Return a new raw syntax node with the given new layout element appended - /// to the end of the node's layout. - const RawSyntax *append(const RawSyntax *NewLayoutElement) const; - - /// Return a new raw syntax node with the given new layout element replacing - /// another at some cursor position. - const RawSyntax *replacingChild(CursorIndex Index, - const RawSyntax *NewLayoutElement) const; - - /// @} - - size_t getLeadingTriviaLength() const { return getLeadingTrivia().size(); } - - size_t getTrailingTriviaLength() const { return getTrailingTrivia().size(); } - - /// Print this piece of syntax recursively. - void print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts) const; - - /// Dump this piece of syntax recursively for debugging or testing. - SWIFT_DEBUG_DUMP; - - /// Dump this piece of syntax recursively. - void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const; - - static void Profile(llvm::FoldingSetNodeID &ID, tok TokKind, StringRef Text, - StringRef LeadingTrivia, StringRef TrailingTrivia); -}; - -} // end namespace syntax -} // end namespace swift - - -#endif // SWIFT_SYNTAX_RAWSYNTAX_H diff --git a/include/swift/Syntax/References.h b/include/swift/Syntax/References.h deleted file mode 100644 index 7eca14131da7b..0000000000000 --- a/include/swift/Syntax/References.h +++ /dev/null @@ -1,27 +0,0 @@ -//===--- References.h - Swift Syntax Reference-Counting Misc. ---*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_REFERENCES_H -#define SWIFT_SYNTAX_REFERENCES_H - -#include "llvm/ADT/IntrusiveRefCntPtr.h" - -namespace swift { - -/// A shorthand to clearly indicate that a value is a reference counted and -/// heap-allocated. -template -using RC = llvm::IntrusiveRefCntPtr; - -} // end namespace swift - -#endif // SWIFT_SYNTAX_REFERENCES_H diff --git a/include/swift/Syntax/Serialization/SyntaxDeserialization.h b/include/swift/Syntax/Serialization/SyntaxDeserialization.h deleted file mode 100644 index 92326824e02f0..0000000000000 --- a/include/swift/Syntax/Serialization/SyntaxDeserialization.h +++ /dev/null @@ -1,220 +0,0 @@ -//===--- SyntaxDeserialization.h - Swift Syntax Deserialization --*- C++-*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file provides the deserialization from JSON to RawSyntax nodes and their -// constituent parts. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_SERIALIZATION_SYNTAXDESERIALIZATION_H -#define SWIFT_SYNTAX_SERIALIZATION_SYNTAXDESERIALIZATION_H - -#include "swift/Basic/StringExtras.h" -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/SyntaxNodes.h" -#include "llvm/ADT/StringSwitch.h" -#include "llvm/Support/YAMLTraits.h" -#include - -namespace swift { -namespace json { - -/// Input to the llvm::yaml / json parser that deserialises to a \c RawSyntax -/// tree. Contains a \c SyntaxArena in which the tree shall be created. -class SyntaxInput : public llvm::yaml::Input { -public: - RC Arena; - - SyntaxInput(llvm::StringRef InputContent, const RC &Arena) - : llvm::yaml::Input(InputContent), Arena(Arena) {} - SyntaxInput(llvm::MemoryBufferRef buffer, const RC &Arena) - : llvm::yaml::Input(buffer), Arena(Arena) {} -}; -} // end namespace json -} // end namespace swift - -namespace llvm { -namespace yaml { - -using swift::json::SyntaxInput; - -/// Deserialization traits for SourcePresence. -template <> struct ScalarEnumerationTraits { - static void enumeration(IO &in, swift::SourcePresence &value) { - in.enumCase(value, "Present", swift::SourcePresence::Present); - in.enumCase(value, "Missing", swift::SourcePresence::Missing); - } -}; - -/// Deserialization traits for swift::tok. -template <> struct ScalarEnumerationTraits { - static void enumeration(IO &in, swift::tok &value) { -#define TOKEN(name) in.enumCase(value, #name, swift::tok::name); -#include "swift/Syntax/TokenKinds.def" - } -}; - -/// Deserialization traits for Trivia. -/// An array of the underlying TriviaPieces will be deserialized as Trivia. -template swift::TriviaPiece yamlize(IO &io, Context &Ctx) { - io.beginMapping(); - auto ret = MappingTraits::mapping(io); - io.endMapping(); - return ret; -} - -template -void yamlize(IO &io, std::vector &Seq, bool, Context &Ctx) { - unsigned incnt = io.beginSequence(); - for (unsigned i = 0; i < incnt; ++i) { - void *SaveInfo; - if (io.preflightElement(i, SaveInfo)) { - Seq.push_back(yamlize(io, Ctx)); - io.postflightElement(SaveInfo); - } - } - io.endSequence(); -} - -template <> struct SequenceTraits> { - static size_t size(IO &in, std::vector &seq) { - return seq.size(); - } -}; - -/// Deserialization traits for RawSyntax list. -template <> struct SequenceTraits> { - static size_t size(IO &in, std::vector &seq) { - return seq.size(); - } - static const swift::RawSyntax *& - element(IO &in, std::vector &seq, size_t index) { - if (seq.size() <= index) { - seq.resize(index + 1); - } - return const_cast(seq[index]); - } -}; - -/// An adapter struct that provides a nested structure for token content. -struct TokenDescription { - bool hasValue = false; - swift::tok Kind; - StringRef Text; -}; - -/// Deserialization traits for TokenDescription. -/// TokenDescriptions always serialized with a token kind, which is -/// the stringified version of their name in the tok:: enum. -/// ``` -/// { -/// "kind": , -/// } -/// ``` -/// -/// For tokens that have some kind of text attached, like literals or -/// identifiers, the serialized form will also have a "text" key containing -/// that text as the value. -template <> struct MappingTraits { - static void mapping(IO &in, TokenDescription &value) { - in.mapRequired("kind", value.Kind); - value.hasValue = true; - if (!swift::isTokenTextDetermined(value.Kind)) { - in.mapRequired("text", value.Text); - } else { - value.Text = swift::getTokenText(value.Kind); - } - } -}; - -/// Deserialization traits for RawSyntax *. -/// First it will check whether the node is null. -/// Then this will be different depending if the raw syntax node is a Token or -/// not. Token nodes will always have this structure: -/// ``` -/// { -/// "tokenKind": { "kind": , "text": }, -/// "leadingTrivia": [ ], -/// "trailingTrivia": [ ], -/// "presence": <"Present" or "Missing"> -/// } -/// ``` -/// All other raw syntax nodes will have this structure: -/// ``` -/// { -/// "kind": , -/// "layout": [ ], -/// "presence": <"Present" or "Missing"> -/// } -/// ``` - -template <> struct MappingTraits { - static void mapping(IO &in, const swift::RawSyntax *&value) { - TokenDescription description; - // RawSyntax trees must always be generated from a SyntaxInput. Otherwise - // we don't have an arena to create the nodes in. - auto input = static_cast(&in); - /// Check whether this is null - if (input->getCurrentNode()->getType() != Node::NodeKind::NK_Mapping) { - return; - } - in.mapOptional("tokenKind", description); - if (description.hasValue) { - swift::tok tokenKind = description.Kind; - StringRef text = description.Text; - StringRef leadingTrivia; - in.mapRequired("leadingTrivia", leadingTrivia); - StringRef trailingTrivia; - in.mapRequired("trailingTrivia", trailingTrivia); - swift::SourcePresence presence; - in.mapRequired("presence", presence); - value = swift::RawSyntax::makeAndCalcLength( - tokenKind, text, leadingTrivia, trailingTrivia, presence, - input->Arena); - } else { - swift::SyntaxKind kind; - in.mapRequired("kind", kind); - std::vector layout; - in.mapRequired("layout", layout); - swift::SourcePresence presence; - in.mapRequired("presence", presence); - value = - swift::RawSyntax::make(kind, layout, presence, input->Arena); - } - } -}; - -} // end namespace yaml -} // end namespace llvm - -namespace swift { -namespace json { -class SyntaxDeserializer { - SyntaxInput Input; - -public: - SyntaxDeserializer(llvm::StringRef InputContent, - RC Arena = SyntaxArena::make()) - : Input(InputContent, Arena) {} - SyntaxDeserializer(llvm::MemoryBufferRef buffer, - RC Arena = SyntaxArena::make()) - : Input(buffer, Arena) {} - llvm::Optional getSourceFileSyntax() { - const swift::RawSyntax *raw; - Input >> raw; - return swift::makeRoot(raw); - } -}; -} // namespace json -} // namespace swift - -#endif // SWIFT_SYNTAX_SERIALIZATION_SYNTAXDESERIALIZATION_H diff --git a/include/swift/Syntax/Serialization/SyntaxSerialization.h b/include/swift/Syntax/Serialization/SyntaxSerialization.h deleted file mode 100644 index 6efe76e96af28..0000000000000 --- a/include/swift/Syntax/Serialization/SyntaxSerialization.h +++ /dev/null @@ -1,190 +0,0 @@ -//===--- SyntaxSerialization.h - Swift Syntax Serialization -----*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file provides the serialization of RawSyntax nodes and their -// constituent parts to JSON. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_SERIALIZATION_SYNTAXSERIALIZATION_H -#define SWIFT_SYNTAX_SERIALIZATION_SYNTAXSERIALIZATION_H - -#include "swift/Basic/JSONSerialization.h" -#include "swift/Basic/StringExtras.h" -#include "swift/Syntax/RawSyntax.h" -#include "llvm/ADT/StringSwitch.h" -#include -#include - -namespace swift { -namespace json { - -/// Serialization traits for SourcePresence. -template <> -struct ScalarReferenceTraits { - static StringRef stringRef(const syntax::SourcePresence &value) { - switch (value) { - case syntax::SourcePresence::Present: - return "\"Present\""; - case syntax::SourcePresence::Missing: - return "\"Missing\""; - } - llvm_unreachable("unhandled presence"); - } - - static bool mustQuote(StringRef) { - // The string is already quoted. This is more efficient since it does not - // check for characters that need to be escaped - return false; - } -}; - -/// Serialization traits for swift::tok. -template <> -struct ScalarReferenceTraits { - static StringRef stringRef(const tok &value) { - switch (value) { -#define TOKEN(name) \ - case tok::name: return "\"" #name "\""; -#include "swift/Syntax/TokenKinds.def" - default: llvm_unreachable("Unknown token kind"); - } - } - - static bool mustQuote(StringRef) { - // The string is already quoted. This is more efficient since it does not - // check for characters that need to be escaped - return false; - } -}; - -/// Serialization traits for Trivia. -/// Trivia will serialize as an array of the underlying TriviaPieces. -template<> -struct ArrayTraits> { - static size_t size(Output &out, ArrayRef &seq) { - return seq.size(); - } - static syntax::TriviaPiece & - element(Output &out, ArrayRef &seq, size_t index) { - return const_cast(seq[index]); - } -}; - -/// Serialization traits for RawSyntax list. -template <> -struct ArrayTraits> { - static size_t size(Output &out, ArrayRef &seq) { - return seq.size(); - } - static const syntax::RawSyntax *& - element(Output &out, ArrayRef &seq, size_t index) { - return const_cast(seq[index]); - } -}; - -/// An adapter struct that provides a nested structure for token content. -struct TokenDescription { - tok Kind; - StringRef Text; -}; - -/// Serialization traits for TokenDescription. -/// TokenDescriptions always serialized with a token kind, which is -/// the stringified version of their name in the tok:: enum. -/// ``` -/// { -/// "kind": , -/// } -/// ``` -/// -/// For tokens that have some kind of text attached, like literals or -/// identifiers, the serialized form will also have a "text" key containing -/// that text as the value. -template<> -struct ObjectTraits { - static void mapping(Output &out, TokenDescription &value) { - out.mapRequired("kind", value.Kind); - if (!isTokenTextDetermined(value.Kind)) { - out.mapRequired("text", value.Text); - } - } -}; - -/// Serialization traits for RawSyntax *. -/// This will be different depending if the raw syntax node is a Token or not. -/// Token nodes will always have this structure: -/// ``` -/// { -/// "tokenKind": { "kind": , "text": }, -/// "leadingTrivia": [ ], -/// "trailingTrivia": [ ], -/// "presence": <"Present" or "Missing"> -/// } -/// ``` -/// All other raw syntax nodes will have this structure: -/// ``` -/// { -/// "kind": , -/// "layout": [ ], -/// "presence": <"Present" or "Missing"> -/// } -/// ``` -template <> -struct ObjectTraits { - static void mapping(Output &out, const syntax::RawSyntax &value) { - if (value.isToken()) { - auto tokenKind = value.getTokenKind(); - auto text = value.getTokenText(); - auto description = TokenDescription { tokenKind, text }; - out.mapRequired("tokenKind", description); - - auto leadingTrivia = value.getLeadingTrivia(); - out.mapRequired("leadingTrivia", leadingTrivia); - - auto trailingTrivia = value.getTrailingTrivia(); - out.mapRequired("trailingTrivia", trailingTrivia); - } else { - auto kind = value.getKind(); - out.mapRequired("kind", kind); - - auto layout = value.getLayout(); - out.mapRequired("layout", layout); - } - auto presence = value.getPresence(); - out.mapRequired("presence", presence); - } -}; - -template <> -struct NullableTraits { - using value_type = syntax::RawSyntax; - static bool isNull(const syntax::RawSyntax *&value) { - return value == nullptr; - } - static const syntax::RawSyntax &get(const syntax::RawSyntax *&value) { - return *value; - } -}; -} // end namespace json - -namespace serialization { - -uint16_t getNumericValue(syntax::SyntaxKind Kind); -uint8_t getNumericValue(syntax::TriviaKind Kind); -uint8_t getNumericValue(tok Value); - -} // end namespace serialization - -} // end namespace swift - -#endif // SWIFT_SYNTAX_SERIALIZATION_SYNTAXSERIALIZATION_H diff --git a/include/swift/Syntax/Syntax.h b/include/swift/Syntax/Syntax.h deleted file mode 100644 index a751f6561e06a..0000000000000 --- a/include/swift/Syntax/Syntax.h +++ /dev/null @@ -1,516 +0,0 @@ -//===--- Syntax.h - Swift Syntax Interface ----------------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the Syntax type, the main public-facing classes and -// subclasses for dealing with Swift Syntax. It essentially wraps -// SyntaxData(Ref) and provides convenience APIs (like retrieving children) -// based on the syntax kind. -// -// There are two versions of the Syntax type. -// SyntaxRef: -// SyntaxRef is designed around efficiency. It *does not* retain the -// SyntaxDataRef that stores its data - the user must guarantee that the -// SyntaxDataRef outlives the SyntaxRef that references it. Instead, -// SyntaxDataRef provides a *view* into the SyntaxDataRef and the view provides -// all convenience APIs. The advantage of this is that the underlying SyntaxData -// can be stack-allocated and does not need to be copied when the the SyntaxRef -// is being passed around or when the SyntaxRef is being casted. -// -// Syntax: -// The syntax nodes are designed for memory safety. Syntax nodes always retain -// (and ref-count) heap-allocated SyntaxData nodes. While this provides maximum -// memory safety, the heap allocations and the ref-counting has a significant -// performance overhead. -// -// Note that the two access modes can also be mixed. When a syntax tree is -// accessed by Syntax (memory-safe) nodes, they can be demoted to SyntaxRef -// nodes to perform performance-critical tasks. -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_SYNTAX_H -#define SWIFT_SYNTAX_SYNTAX_H - -#include "swift/Basic/Debug.h" -#include "swift/Syntax/SyntaxData.h" -#include "swift/Syntax/References.h" -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/Trivia.h" -#include "llvm/ADT/IntrusiveRefCntPtr.h" -#include "llvm/ADT/Optional.h" -#include "llvm/Support/raw_ostream.h" - -namespace swift { - -namespace syntax { - -struct SyntaxVisitor; -class SourceFileSyntax; - -template -SyntaxNode makeRoot(const RawSyntax *Raw) { - auto Data = SyntaxData::makeRoot(AbsoluteRawSyntax::forRoot(Raw)); - return SyntaxNode(Data); -} - -const auto NoParent = llvm::None; - -/// Marker type to construct \c SyntaxRef nodes without validation. This is used -/// to create \c SyntaxRef inside \c OwnedSyntaxRef and \c -/// OptionalOwnedSyntaxRef that point to a \c SyntaxDataRef which is yet to be -/// initialised. -/// Validation will occur in these types once the \c SyntaxRef is accessed. -struct no_validation_t {}; - -// MARK: - OwnedSyntaxRef - -/// Holds a \c SyntaxDataRef and provides a \c SyntaxRef (or one of its -/// subclasses) as an accessor to the \c SyntaxDataRef. -/// The user of this type needs to make sure that the \c OwnedSyntaxRef always -/// outlives the \c SyntaxRef provided by it, because otherwise the \c SyntaxRef -/// points to invalid memory. -/// It allows transparent access to the \c SyntaxRef through the \c -> operator. -/// -/// All methods that return a \c OwnedSyntaxRef should be inlined to avoid -/// copying the \c SyntaxDataRef, which is rather expensive because the struct -/// is rather large. -/// -/// A typical initialisation of a OwnedSyntaxRef looks as follows: -/// \code -/// OwnedSyntaxRef Result; -/// someSyntaxDataRef.getChildRef(Index, Result.getDataPtr()); -/// \endcode -/// The first line creates an empty \c OwnedSyntaxRef with uninitialised memory. -/// The second line invokes a method that fills \c Data of \c OwnedSyntaxRef. -/// This way, we directly write the \c SyntaxDataRef to the correct memory -/// location and avoid copying it around. -template -class OwnedSyntaxRef { - SyntaxDataRef Data; - SyntaxRefType Ref; - -public: - /// Create an *uninitialized* \c OwnedSyntaxRef. Its storage needs to be - /// initialised by writing a \c SyntaxDataRef to the pointer returned by - /// \c getDataPtr() - /// Implementation Note: We need to initialise \c Ref without validation, - /// because \c Data is still uninitialised. \c Ref will be validated when - /// accessed using \c getRef or \c -> . - OwnedSyntaxRef() : Data(), Ref(getDataPtr(), no_validation_t()) {} - - OwnedSyntaxRef(const OwnedSyntaxRef &Other) - : Data(Other.Data), Ref(getDataPtr(), no_validation_t()) {} - OwnedSyntaxRef(OwnedSyntaxRef &&Other) - : Data(std::move(Other.Data)), Ref(getDataPtr(), no_validation_t()) {} - - /// The pointer to the location at which \c this stores the \c Data. - /// Can be used to retroactively populate the \c Data after \c OwnedSyntaxRef - /// has been constructed with uninitialised memory. - SyntaxDataRef *getDataPtr() { return &Data; } - - const SyntaxRefType &getRef() { - assert(Ref.getDataRef() == getDataPtr() && - "Ref no longer pointing to Data?"); -#ifndef NDEBUG - // This might be the first access to Ref after Data has been modified. - // Validate the node. - Ref.validate(); -#endif - return Ref; - } - - const SyntaxRefType *operator->() { - assert(Ref.getDataRef() == getDataPtr() && - "Ref no longer pointing to Data?"); -#ifndef NDEBUG - // This might be the first access to Ref after Data has been modified. - // Validate the node. - Ref.validate(); -#endif - return &Ref; - } -}; - -/// Same as \c OwnedSyntaxRef but can be null. We don't use \c -/// Optional>>, because then we couldn't access -/// the underlying \c SyntaxRefType via the \c -> operator (the use of \c -> -/// would access the \c OwnedSyntaxRef wrapped by \c Optional and -/// not the \c SyntaxRefType wrapped by \c OwnedSyntaxRef. -template -class OptionalOwnedSyntaxRef { - Optional Data; - SyntaxRefType Ref; - -public: - OptionalOwnedSyntaxRef() : Data(), Ref(getDataPtr(), no_validation_t()) {} - - OptionalOwnedSyntaxRef(const OptionalOwnedSyntaxRef &Other) - : Data(Other.Data), Ref(getDataPtr(), no_validation_t()) {} - OptionalOwnedSyntaxRef(OptionalOwnedSyntaxRef &&Other) - : Data(std::move(Other.Data)), Ref(getDataPtr(), no_validation_t()) {} - - SyntaxDataRef *getDataPtr() { return Data.getPointer(); } - - bool hasValue() const { return Data.hasValue(); } - - explicit operator bool() const { return hasValue(); } - - const SyntaxRefType &getRef() { - assert(Ref.getDataRef() == getDataPtr() && - "Ref no longer pointing to Data?"); - assert(hasValue() && "Accessing a OptionalOwnedSyntaxRef without a value"); -#ifndef NDEBUG - // This might be the first access to Ref after Data has been populated. - // Validate the node. - Ref.validate(); -#endif - return Ref; - } - - SyntaxRefType *operator->() { - assert(Ref.getDataRef() == getDataPtr() && - "Ref no longer pointing to Data?"); - assert(hasValue() && "OptionalOwnedSyntaxRef doesn't have a value"); - return &Ref; - } -}; - -// MARK: - Syntax - -/// See comment on top of file. -class Syntax { -protected: - const RC Data; - -public: - explicit Syntax(const RC &Data) : Data(Data) { - assert(Data != nullptr && "Syntax must be backed by non-null Data"); - } - - /// Get the kind of syntax. - SyntaxKind getKind() const; - - /// Get the shared raw syntax. - const RawSyntax *getRaw() const; - - /// Get the number of child nodes in this piece of syntax, not including - /// tokens. - size_t getNumChildren() const; - - /// Get the Nth child of this piece of syntax. - llvm::Optional getChild(const size_t N) const; - - /// Returns true if the syntax node is of the given type. - template - bool is() const { - return T::classof(this); - } - - /// Get the Data for this Syntax node. - const RC &getData() const { return Data; } - - /// Cast this Syntax node to a more specific type, asserting it's of the - /// right kind. - template - T castTo() const { - assert(is() && "castTo() node of incompatible type!"); - return T(Data); - } - - /// If this Syntax node is of the right kind, cast and return it, - /// otherwise return None. - template - llvm::Optional getAs() const { - if (is()) { - return castTo(); - } - return llvm::None; - } - - /// Return the parent of this node, if it has one. - llvm::Optional getParent() const; - - /// Returns the child index of this node in its parent, - /// if it has one, otherwise 0. - CursorIndex getIndexInParent() const { return getData()->getIndexInParent(); } - - /// Return the number of bytes this node takes when spelled out in the source - size_t getTextLength() const { return getRaw()->getTextLength(); } - - /// Returns true if this syntax node represents a token. - bool isToken() const; - - /// Returns true if this syntax node represents a statement. - bool isStmt() const; - - /// Returns true if this syntax node represents a declaration. - bool isDecl() const; - - /// Returns true if this syntax node represents an expression. - bool isExpr() const; - - /// Returns true if this syntax node represents a pattern. - bool isPattern() const; - - /// Returns true if this syntax node represents a type. - bool isType() const; - - /// Returns true if this syntax is of some "unknown" kind. - bool isUnknown() const; - - /// Returns true if the node is "missing" in the source (i.e. it was - /// expected (or optional) but not written. - bool isMissing() const; - - /// Returns true if the node is "present" in the source. - bool isPresent() const; - - /// Print the syntax node with full fidelity to the given output stream. - void print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts = SyntaxPrintOptions()) const; - - /// Print a debug representation of the syntax node to the given output stream - /// and indentation level. - void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const; - - /// Print a debug representation of the syntax node to standard error. - SWIFT_DEBUG_DUMP; - - bool hasSameIdentityAs(const Syntax &Other) const { - return Data->getAbsoluteRaw().getNodeId() == - Other.Data->getAbsoluteRaw().getNodeId(); - } - - static bool kindof(SyntaxKind Kind) { - return true; - } - - static bool classof(const Syntax *S) { - // Trivially true. - return true; - } - - /// Recursively visit this node. - void accept(SyntaxVisitor &Visitor); - - /// Same as \c getAbsolutePositionAfterLeadingTrivia. - AbsoluteOffsetPosition getAbsolutePosition() const { - return getAbsolutePositionAfterLeadingTrivia(); - } - - /// Get the offset at which the leading trivia of this node starts. - AbsoluteOffsetPosition getAbsolutePositionBeforeLeadingTrivia() const { - return Data->getAbsolutePositionBeforeLeadingTrivia(); - } - - /// Get the offset at which the actual content (i.e. non-trivia) of this node - /// starts. - AbsoluteOffsetPosition getAbsolutePositionAfterLeadingTrivia() const { - return Data->getAbsolutePositionAfterLeadingTrivia(); - } - - /// Get the offset at which the trailing trivia of this node starts. - AbsoluteOffsetPosition getAbsoluteEndPositionBeforeTrailingTrivia() const { - return Data->getAbsoluteEndPositionBeforeTrailingTrivia(); - } - - /// Get the offset at which the trailing trivia of this node starts. - AbsoluteOffsetPosition getAbsoluteEndPositionAfterTrailingTrivia() const { - return Data->getAbsoluteEndPositionAfterTrailingTrivia(); - } -}; - -// MARK: - SyntaxRef - -/// See comment on top of file. -class SyntaxRef { - const SyntaxDataRef * const Data; - -public: - /// Create a \c SyntaxRef and validate that the \p Data can actually represent - /// a \c SyntaxRef. Validation in particular performs checks for derived - /// types. - explicit SyntaxRef(const SyntaxDataRef *Data) : Data(Data) { - assert(Data != nullptr && "SyntaxRef must reference Data"); - this->validate(); - } - SyntaxRef(const SyntaxDataRef *Data, no_validation_t) : Data(Data) { - assert(Data != nullptr && "SyntaxRef must reference Data"); - } - - /// Demote a \c Syntax to a \c SyntaxRef - SyntaxRef(const Syntax &Node) : SyntaxRef(Node.getData().get()) {} - - void validate() {} - - // MARK: - Get underlying data - - /// Get the \c SyntaxDataRef that stores the data of this \c SyntaxRef node. - const SyntaxDataRef *getDataRef() const { - return Data; - } - - const AbsoluteRawSyntax &getAbsoluteRaw() const { - return getDataRef()->getAbsoluteRaw(); - } - - /// Get the shared raw syntax. - const RawSyntax *getRaw() const { return getDataRef()->getRaw(); } - - /// Get the kind of syntax. - SyntaxKind getKind() const { return getRaw()->getKind(); } - - /// Return the number of bytes this node takes when spelled out in the source, - /// including trivia. - size_t getTextLength() const { return getRaw()->getTextLength(); } - - // MARK: Parents/children - - /// Return the parent of this node, if it has one, otherwise return \c None. - llvm::Optional getParentRef() const { - if (auto ParentDataRef = getDataRef()->getParentRef()) { - return SyntaxRef(ParentDataRef); - } else { - return None; - } - } - - /// Get the number of child nodes in this piece of syntax. - size_t getNumChildren() const { return getDataRef()->getNumChildren(); } - - /// Returns the child index of this node in its parent, if it has one, - /// otherwise 0. - CursorIndex getIndexInParent() const { - return getDataRef()->getIndexInParent(); - } - - /// Get the \p N -th child of this piece of syntax. - OptionalOwnedSyntaxRef getChildRef(const size_t N) const { - OptionalOwnedSyntaxRef Result; - getDataRef()->getChildRef(N, Result.getDataPtr()); - return Result; - } - - // MARK: Position - - /// Get the offset at which the leading trivia of this node starts. - AbsoluteOffsetPosition getAbsolutePositionBeforeLeadingTrivia() const { - return getDataRef()->getAbsolutePositionBeforeLeadingTrivia(); - } - - /// Get the offset at which the actual content (i.e. non-trivia) of this node - /// starts. - AbsoluteOffsetPosition getAbsolutePositionAfterLeadingTrivia() const { - return getDataRef()->getAbsolutePositionAfterLeadingTrivia(); - } - - /// Get the offset at which the trailing trivia of this node starts. - AbsoluteOffsetPosition getAbsoluteEndPositionBeforeTrailingTrivia() const { - return getDataRef()->getAbsoluteEndPositionBeforeTrailingTrivia(); - } - - /// Get the offset at which the trailing trivia of this node ends. - AbsoluteOffsetPosition getAbsoluteEndPositionAfterTrailingTrivia() const { - return getDataRef()->getAbsoluteEndPositionAfterTrailingTrivia(); - } - - // MARK: - Get node kind - - /// Returns true if this syntax node represents a token. - bool isToken() const { return getRaw()->isToken(); } - - /// Returns true if this syntax node represents a statement. - bool isStmt() const { return getRaw()->isStmt(); } - - /// Returns true if this syntax node represents a declaration. - bool isDecl() const { return getRaw()->isDecl(); } - - /// Returns true if this syntax node represents an expression. - bool isExpr() const { return getRaw()->isExpr(); } - - /// Returns true if this syntax node represents a pattern. - bool isPattern() const { return getRaw()->isPattern(); } - - /// Returns true if this syntax node represents a type. - bool isType() const { return getRaw()->isType(); } - - /// Returns true if this syntax is of some "unknown" kind. - bool isUnknown() const { return getRaw()->isUnknown(); } - - /// Returns true if the node is "missing" in the source (i.e. it was - /// expected (or optional) but not written. - bool isMissing() const { return getRaw()->isMissing(); } - - /// Returns true if the node is "present" in the source. - bool isPresent() const { return getRaw()->isPresent(); } - - // MARK: Casting - - /// Returns true if the syntax node is of the given type \p T. - template - bool is() const { - return T::classof(this); - } - - /// Cast this Syntax node to a more specific type, asserting it's of the - /// right kind \p T. - template - T castTo() const { - assert(is() && "castTo() node of incompatible type!"); - return T(getDataRef()); - } - - /// If this Syntax node is of the right kind \p T, cast and return it, - /// otherwise return None. - template - llvm::Optional getAs() const { - if (is()) { - return castTo(); - } else { - return None; - } - } - - static bool kindof(SyntaxKind Kind) { return true; } - - static bool classof(const SyntaxRef *S) { - // Trivially true. - return true; - } - - // MARK: - Miscellaneous - - /// Print the syntax node with full fidelity to the given output stream. - void print(llvm::raw_ostream &OS, - SyntaxPrintOptions Opts = SyntaxPrintOptions()) const { - if (auto Raw = getRaw()) { - Raw->print(OS, Opts); - } - } - - /// Print a debug representation of the syntax node to the given output stream - /// and indentation level. - void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const { - getRaw()->dump(OS, Indent); - } - - /// Print a debug representation of the syntax node to standard error. - SWIFT_DEBUG_DUMP { getRaw()->dump(); } - - bool hasSameIdentityAs(const SyntaxRef &Other) const { - return getDataRef()->getAbsoluteRaw().getNodeId() == - Other.getDataRef()->getAbsoluteRaw().getNodeId(); - } -}; - -} // end namespace syntax -} // end namespace swift - -#endif // SWIFT_SYNTAX_SYNTAX_H diff --git a/include/swift/Syntax/SyntaxArena.h b/include/swift/Syntax/SyntaxArena.h deleted file mode 100644 index 0e7679fb877e3..0000000000000 --- a/include/swift/Syntax/SyntaxArena.h +++ /dev/null @@ -1,133 +0,0 @@ -//===--- SyntaxArena.h - Syntax Tree Memory Allocation ----------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines SyntaxArena that is Memory manager for Syntax nodes. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_SYNTAXARENA_H -#define SWIFT_SYNTAX_SYNTAXARENA_H - -#include "swift/Syntax/References.h" -#include "llvm/ADT/IntrusiveRefCntPtr.h" -#include "llvm/ADT/SmallPtrSet.h" -#include "llvm/Support/Allocator.h" -#include "llvm/ADT/STLExtras.h" -#include - -namespace swift { -namespace syntax { - -/// Memory manager for Syntax nodes. -class SyntaxArena : public llvm::ThreadSafeRefCountedBase { - SyntaxArena(const SyntaxArena &) = delete; - void operator=(const SyntaxArena &) = delete; - - llvm::BumpPtrAllocator Allocator; - - /// \c SyntaxArenas that are kept alive as long as this arena is alive. - /// If a \c RawSyntax A node in this arena references a \c RawSyntax node B - /// from a different arena, the B's arena will be added to this list. - /// The \c SyntaxArena's in this set are manually retained when added to this - /// list by \c addChildArena and manually released in the destructor of this - /// arena. - llvm::SmallPtrSet ChildArenas; - - /// The start (inclusive) and end (exclusive) pointers of a memory region that - /// is frequently requested using \c containsPointer. Must be inside \c - /// Allocator but \c containsPointer will check this region first and will - /// thus return more quickly for pointers that lie within this region. - const void *HotUseMemoryRegionStart = nullptr; - const void *HotUseMemoryRegionEnd = nullptr; - -public: - SyntaxArena() {} - - ~SyntaxArena() { - // Release all child arenas that were manually retained in addChildArena. - for (SyntaxArena *ChildArena : ChildArenas) { - ChildArena->Release(); - } - } - - static RC make() { return RC(new SyntaxArena()); } - - /// Add an arena that is kept alive while this arena lives. See documentation - /// of \c ChildArenas for more detail. - void addChildArena(SyntaxArena *Arena) { - if (Arena == this) { - return; - } - auto DidInsert = ChildArenas.insert(Arena); - if (DidInsert.second) { - Arena->Retain(); - assert(!Arena->containsReferenceCycle({this})); - } - } - -#ifndef NDEBUG - /// Check if there are any reference cycles in the child arenas. This is done - /// by walking all child nodes from a root node, collecting all visited nodes - /// in \p VisitedArenas. If we find a node twice, there's a reference cycle. - bool - containsReferenceCycle(std::set VisitedArenas = {}) const { - if (!VisitedArenas.insert(this).second) { - // this was already in VisitedArenas -> we have a reference cycle - return true; - } - return llvm::any_of(ChildArenas, [&](const SyntaxArena *Child) { - return Child->containsReferenceCycle(VisitedArenas); - }); - } -#endif - - void setHotUseMemoryRegion(const void *Start, const void *End) { - assert(containsPointer(Start) && - "The hot use memory region should be in the Arena's bump allocator"); - HotUseMemoryRegionStart = Start; - HotUseMemoryRegionEnd = End; - } - - llvm::BumpPtrAllocator &getAllocator() { return Allocator; } - void *Allocate(size_t size, size_t alignment) { - return Allocator.Allocate(size, alignment); - } - - bool containsPointer(const void *Ptr) { - if (HotUseMemoryRegionStart <= Ptr && Ptr < HotUseMemoryRegionEnd) { - return true; - } - return getAllocator().identifyObject(Ptr) != llvm::None; - } - - /// If the \p Data is not allocated in this arena, copy it to this and adjust - /// \p Data to point to the string's copy in this arena. - void copyStringToArenaIfNecessary(const char *&Data, size_t Length) { - if (Length == 0) { - // Empty strings can live wherever they want. Nothing to do. - return; - } - if (containsPointer(Data)) { - // String already in arena. Nothing to do. - return; - } - // Copy string to arena - char *ArenaData = (char *)getAllocator().Allocate(Length); - std::memcpy(ArenaData, Data, Length); - Data = ArenaData; - } -}; - -} // namespace syntax -} // namespace swift - -#endif diff --git a/include/swift/Syntax/SyntaxBuilders.h.gyb b/include/swift/Syntax/SyntaxBuilders.h.gyb deleted file mode 100644 index 4c8750ce4590d..0000000000000 --- a/include/swift/Syntax/SyntaxBuilders.h.gyb +++ /dev/null @@ -1,68 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From SyntaxBuilders.h.gyb. -//// Do Not Edit Directly! -//===------------- SyntaxBuilders.h - Syntax Node definitions -------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_BUILDERS_H -#define SWIFT_SYNTAX_BUILDERS_H - -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/SyntaxNodes.h" - -namespace swift { -namespace syntax { - -class SyntaxArena; - -% for node in SYNTAX_NODES: -% if node.is_buildable(): -% child_count = len(node.children) -class ${node.name}Builder { - RC Arena; - const RawSyntax *Layout[${child_count}] = { -% for child in node.children: - nullptr, -% end - }; - -public: - ${node.name}Builder(const RC &Arena) : Arena(Arena) {} - -% for child in node.children: - ${node.name}Builder &use${child.name}(${child.type_name} ${child.name}); -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child.collection_element_name -% child_elt_type = child_node.collection_element_type -% if not child_elt: -% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name)) -% end - ${node.name}Builder &add${child_elt}(${child_elt_type} ${child_elt}); -% end -% end - - ${node.name} build(); -}; - -% end -% end - -} -} - -#endif // SWIFT_SYNTAX_BUILDERS_H diff --git a/include/swift/Syntax/SyntaxCollection.h b/include/swift/Syntax/SyntaxCollection.h deleted file mode 100644 index 13ad27112ea55..0000000000000 --- a/include/swift/Syntax/SyntaxCollection.h +++ /dev/null @@ -1,292 +0,0 @@ -//===--- SyntaxCollection.h -------------------------------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_SYNTAXCOLLECTION_H -#define SWIFT_SYNTAX_SYNTAXCOLLECTION_H - -#include "swift/Syntax/Syntax.h" - -#include - -namespace swift { -namespace syntax { - -// MARK: - SyntaxCollection - -template -class SyntaxCollection; - -template -struct SyntaxCollectionIterator { - const SyntaxCollection &Collection; - size_t Index; - - Element operator*() { - return Collection[Index]; - } - - SyntaxCollectionIterator &operator++() { - ++Index; - return *this; - } - - bool - operator==(const SyntaxCollectionIterator &Other) { - return Collection.hasSameIdentityAs(Other.Collection) && - Index == Other.Index; - } - - bool - operator!=(const SyntaxCollectionIterator &Other) { - return !operator==(Other); - - } -}; - -/// A generic unbounded collection of syntax nodes -template -class SyntaxCollection : public Syntax { - friend class Syntax; - -private: - static RC makeData(std::initializer_list &Elements, - const RC &Arena) { - auto RawElements = llvm::map_iterator( - Elements.begin(), - [](const Element &Elt) -> const RawSyntax * { return Elt.getRaw(); }); - auto Raw = RawSyntax::make(CollectionKind, RawElements, Elements.size(), - SourcePresence::Present, Arena); - return SyntaxData::makeRoot(AbsoluteRawSyntax::forRoot(Raw)); - } - -public: - SyntaxCollection(const RC &Data) : Syntax(Data) { - validate(); - } - - SyntaxCollection(std::initializer_list list): - SyntaxCollection(SyntaxCollection::makeData(list)) {} - - void validate() {} - - /// Returns true if the collection is empty. - bool empty() const { - return size() == 0; - } - - /// Returns the number of elements in the collection. - size_t size() const { - return getRaw()->getLayout().size(); - } - - SyntaxCollectionIterator begin() const { - return SyntaxCollectionIterator { - *this, - 0, - }; - } - - SyntaxCollectionIterator end() const { - return SyntaxCollectionIterator { - *this, - getRaw()->getLayout().size(), - }; - } - - /// Return the element at the given Index. - /// - /// Precondition: Index < size() - /// Precondition: !empty() - Element operator[](const size_t Index) const { - assert(Index < size()); - assert(!empty()); - return Element(Data->getChild(Index)); - } - - /// Return a new collection with the given element added to the end. - SyntaxCollection - appending(Element E) const { - auto OldLayout = getRaw()->getLayout(); - std::vector NewLayout; - NewLayout.reserve(OldLayout.size() + 1); - std::copy(OldLayout.begin(), OldLayout.end(), back_inserter(NewLayout)); - NewLayout.push_back(E.getRaw()); - auto Raw = RawSyntax::make(CollectionKind, NewLayout, - getRaw()->getPresence(), getRaw()->getArena()); - return SyntaxCollection(Data->replacingSelf(Raw)); - } - - /// Return a new collection with an element removed from the end. - /// - /// Precondition: !empty() - SyntaxCollection removingLast() const { - assert(!empty()); - auto NewLayout = getRaw()->getLayout().drop_back(); - auto Raw = RawSyntax::make(CollectionKind, NewLayout, - getRaw()->getPresence(), getRaw()->getArena()); - return SyntaxCollection(Data->replacingSelf(Raw)); - } - - /// Return a new collection with the given element appended to the front. - SyntaxCollection - prepending(Element E) const { - auto OldLayout = getRaw()->getLayout(); - std::vector NewLayout = {E.getRaw()}; - std::copy(OldLayout.begin(), OldLayout.end(), - std::back_inserter(NewLayout)); - auto Raw = RawSyntax::make(CollectionKind, NewLayout, - getRaw()->getPresence(), getRaw()->getArena()); - return SyntaxCollection(Data->replacingSelf(Raw)); - } - - /// Return a new collection with an element removed from the end. - /// - /// Precondition: !empty() - SyntaxCollection removingFirst() const { - assert(!empty()); - auto NewLayout = getRaw()->getLayout().drop_front(); - auto Raw = RawSyntax::make(CollectionKind, NewLayout, - getRaw()->getPresence(), getRaw()->getArena()); - return SyntaxCollection(Data->replacingSelf(Raw)); - } - - /// Return a new collection with the Element inserted at index i. - /// - /// Precondition: i <= size() - SyntaxCollection - inserting(size_t i, Element E) const { - assert(i <= size()); - auto OldLayout = getRaw()->getLayout(); - std::vector NewLayout; - NewLayout.reserve(OldLayout.size() + 1); - - std::copy(OldLayout.begin(), OldLayout.begin() + i, - std::back_inserter(NewLayout)); - NewLayout.push_back(E.getRaw()); - std::copy(OldLayout.begin() + i, OldLayout.end(), - std::back_inserter(NewLayout)); - auto Raw = RawSyntax::make(CollectionKind, NewLayout, - getRaw()->getPresence(), getRaw()->getArena()); - return SyntaxCollection(Data->replacingSelf(Raw)); - } - - /// Return a new collection with the element removed at index i. - SyntaxCollection removing(size_t i) const { - assert(i <= size()); - std::vector NewLayout = getRaw()->getLayout(); - auto iterator = NewLayout.begin(); - std::advance(iterator, i); - NewLayout.erase(iterator); - auto Raw = RawSyntax::make(CollectionKind, NewLayout, - getRaw()->getPresence(), getRaw()->getArena()); - return SyntaxCollection(Data->replacingSelf(Raw)); - } - - /// Return an empty syntax collection of this type. - SyntaxCollection cleared() const { - auto Raw = RawSyntax::make(CollectionKind, {}, getRaw()->getPresence(), - getRaw()->getArena()); - return SyntaxCollection(Data->replacingSelf(Raw)); - } - - static bool kindof(SyntaxKind Kind) { - return Kind == CollectionKind; - } - - static bool classof(const Syntax *S) { - return kindof(S->getKind()); - } -}; - -// MARK: - SyntaxCollectionRef - -template -class SyntaxCollectionRef; - -template -struct SyntaxCollectionRefIterator { - const SyntaxCollectionRef &Collection; - size_t Index; - - OwnedSyntaxRef operator*() { return Collection.getChild(Index); } - - SyntaxCollectionRefIterator &operator++() { - ++Index; - return *this; - } - - bool operator==( - const SyntaxCollectionRefIterator &Other) { - return Collection.hasSameIdentityAs(Other.Collection) && - Index == Other.Index; - } - - bool operator!=( - const SyntaxCollectionRefIterator &Other) { - return !operator==(Other); - } -}; - -/// A generic unbounded collection of \c SyntaxRef nodes. -template -class SyntaxCollectionRef : public SyntaxRef { - friend class Syntax; - -public: - SyntaxCollectionRef(const SyntaxDataRef *Data) : SyntaxRef(Data) { - validate(); - } - SyntaxCollectionRef(const SyntaxDataRef *Data, no_validation_t) - : SyntaxRef(Data) {} - - void validate() {} - - /// Returns true if the collection is empty. - bool empty() const { return size() == 0; } - - /// Returns the number of elements in the collection. - size_t size() const { return getRaw()->getLayout().size(); } - - SyntaxCollectionRefIterator begin() const { - return SyntaxCollectionRefIterator{ - /*Collection=*/*this, - /*Index=*/0, - }; - } - - SyntaxCollectionRefIterator end() const { - return SyntaxCollectionRefIterator{ - /*Collection=*/*this, - /*Index=*/getRaw()->getLayout().size(), - }; - } - - /// Return the element at the given Index. - /// - /// Precondition: Index < size() - OwnedSyntaxRef getChild(const size_t Index) const { - assert(Index < size() && "Index out of bounds"); - OwnedSyntaxRef Result; - getDataRef()->getChildRef(Index, Result.getDataPtr()); - return Result; - } - - static bool kindof(SyntaxKind Kind) { return Kind == CollectionKind; } - - static bool classof(const SyntaxRef *S) { return kindof(S->getKind()); } -}; - - -} // end namespace syntax -} // end namespace swift - -#endif // SWIFT_SYNTAX_SYNTAXCOLLECTION_H diff --git a/include/swift/Syntax/SyntaxData.h b/include/swift/Syntax/SyntaxData.h deleted file mode 100644 index c218cfc63b658..0000000000000 --- a/include/swift/Syntax/SyntaxData.h +++ /dev/null @@ -1,468 +0,0 @@ -//===--- SyntaxData.h - Swift Syntax Data Interface -------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the SyntaxData interface, the type for the instance -// data for Syntax nodes. -// -// Effectively, these provide two main things to a Syntax node - parental -// relationships and caching for its children. -// -// A SyntaxData contains at least a strong reference to the RawSyntax, -// from which most information comes, and additionally a weak reference to -// its parent and the "index" at which it occurs in its parent. These were -// originally intended to have the important public APIs for structured -// editing but now contain no significant or public API; for those, see the -// Syntax type. These are purely to contain data, hence the name. -// -// Conceptually, SyntaxData add the characteristic of specific identity in a -// piece of Swift source code. While the RawSyntax for the integer literal -// token '1' can be reused anywhere a '1' occurs and has identical formatting, -// a SyntaxData represents *a* specific '1' at a particular location in -// Swift source. -// -// These are effectively internal implementation. For all public APIs, look -// for the type without "Data" in its name. For example, a StructDeclSyntaxData -// expresses its API through the wrapping StructDeclSyntax type. -// -//===----------------------------------------------------------------------===// - - -#ifndef SWIFT_SYNTAX_SYNTAXDATA_H -#define SWIFT_SYNTAX_SYNTAXDATA_H - -#include "swift/Basic/Debug.h" -#include "swift/Syntax/AbsoluteRawSyntax.h" -#include "swift/Syntax/AtomicCache.h" -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/References.h" -#include "llvm/ADT/DenseMap.h" - -#include - -namespace swift { -namespace syntax { - -/// The class for holding parented syntax. -/// -/// \c SyntaxDataRef is designed for efficiency, not memory safety. -/// It *does not* retain the \c RawSyntax that backs the syntax node, the \c -/// SyntaxArena in which the \c RawSyntax node lives nor the parent \c -/// SyntaxDataRef node. It is the user's responsibility to ensure these values -/// outlive the \c SyntaxDataRef. -/// -/// Always use \c SyntaxData if performance is not a concern and memory safety -/// should be guaranteed. -class SyntaxDataRef { - friend class SyntaxData; - - /// OptionalStorage is a friend so it can call the memberwise initialiser - /// to construct a null \c SyntaxDataRef. - template - friend class llvm::optional_detail::OptionalStorage; - -protected: - /// The \c AbsoluteRawSyntaxNode that provides the majority of this node's - /// data. The underlying \c RawSyntax node is *not* retained by the \c - /// SyntaxDataRef. It is the user's responsibility to ensure that the \c - /// RawSyntax's \c SyntaxArena outlives this \c SyntaxDataRef. - /// - /// In \c SyntaxData, the backing \c SyntaxArena is retained via the \c Arena - /// property, lifting the responsibility to guarantee the \c RawSyntax node - /// stays alive from the user. - AbsoluteRawSyntax AbsoluteRaw; - - /// The parent of this node. The parent is *not* retained by the \c - /// SyntaxDataRef. It is the user's responsibility to ensure that the parent - /// always outlives the child. - /// - /// If this is a \c SyntaxData object, \c Parent is guaranteed to also be a - /// \c SyntaxData object, which is manually retained when \c this is created - /// and released when \c this is destroyed. - const SyntaxDataRef *Parent; - - /// Creates a \c SyntaxDataRef. \p AbsoluteRaw must not be null. If \p Parent - /// is a \c nullptr, this node represents the root of a tree. - SyntaxDataRef(const AbsoluteRawSyntax &AbsoluteRaw, - const SyntaxDataRef *Parent) - : AbsoluteRaw(AbsoluteRaw), Parent(Parent) { - } - -#ifndef NDEBUG - virtual bool isRef() const { return true; } -#endif - -public: - /// Creates an *uninitialized* \c SyntaxDataRef. - SyntaxDataRef() {} - - SyntaxDataRef(const SyntaxDataRef &DataRef) = default; - SyntaxDataRef(SyntaxDataRef &&DataRef) = default; - - SyntaxDataRef &operator=(SyntaxDataRef const &other) = default; - SyntaxDataRef &operator=(SyntaxDataRef &&other) = default; - -#ifndef NDEBUG - virtual ~SyntaxDataRef() {} -#endif - - // MARK: - Retrieving underlying data - - const AbsoluteRawSyntax &getAbsoluteRaw() const { - return AbsoluteRaw; - } - - /// Returns the raw syntax node for this syntax node. - const RawSyntax *getRaw() const { return getAbsoluteRaw().getRaw(); } - - // MARK: - Retrieving related nodes - - /// Return the parent \c SyntaxDataRef if it exists, otherwise \c nullptr. - const SyntaxDataRef *getParentRef() const { - return Parent; - } - - /// Returns true if this syntax node has a parent. - bool hasParent() const { return getParentRef() != nullptr; } - - /// Returns the number of children this SyntaxData has. - size_t getNumChildren() const { return getRaw()->getLayout().size(); } - - /// Returns the child index of this node in its parent, if it has a parent, - /// otherwise 0. - AbsoluteSyntaxPosition::IndexInParentType getIndexInParent() const { - return getAbsoluteRaw().getPosition().getIndexInParent(); - } - - /// If \c this node has a child at \p Cursor, write the child's \c - /// SyntaxDataRef to \p DataMem and return \c true. - /// If no child exists at \p Cursor, leave \p DataMem untouched and return \c - /// false. - template - bool getChildRef(CursorType Cursor, SyntaxDataRef *DataMem) const { - return getChildRef( - (AbsoluteSyntaxPosition::IndexInParentType)cursorIndex(Cursor), - DataMem); - } - - /// If \c this node has a child at \p Index, write the child's \c - /// SyntaxDataRef to \p DataMem and return \c true. If no child exists at \p - /// Index, leave \p DataMem untouched and return \c false. - bool getChildRef(AbsoluteSyntaxPosition::IndexInParentType Index, - SyntaxDataRef *DataMem) const { - auto AbsoluteRaw = getAbsoluteRaw().getChild(Index); - if (AbsoluteRaw) { - new (DataMem) SyntaxDataRef(*AbsoluteRaw, - /*Parent=*/const_cast(this)); - return true; - } else { - return false; - } - } - - /// Assuming that the child at \p Cursor exists, write its \c SyntaxData to \p - /// DataMem. - template - void getPresentChildRef(CursorType Cursor, SyntaxDataRef *DataMem) const { - return getPresentChildRef( - (AbsoluteSyntaxPosition::IndexInParentType)cursorIndex(Cursor), - DataMem); - } - - /// Assuming that the child at \p Index exists, write its \c SyntaxData to \p - /// DataMem. - void getPresentChildRef(AbsoluteSyntaxPosition::IndexInParentType Index, - SyntaxDataRef *DataMem) const { - auto AbsoluteRaw = getAbsoluteRaw().getPresentChild(Index); - new (DataMem) SyntaxDataRef(AbsoluteRaw, - /*Parent=*/const_cast(this)); - } - - // MARK: - Retrieving source locations - - /// Get the offset at which the leading trivia of this node starts. - AbsoluteOffsetPosition getAbsolutePositionBeforeLeadingTrivia() const { - return getAbsoluteRaw().getPosition(); - } - - /// Get the offset at which the content of this node (excluding leading - /// trivia) starts. - AbsoluteOffsetPosition getAbsolutePositionAfterLeadingTrivia() const { - if (auto FirstToken = getAbsoluteRaw().getFirstToken()) { - return getAbsolutePositionBeforeLeadingTrivia().advancedBy( - FirstToken->getRaw()->getLeadingTriviaLength()); - } else { - return getAbsolutePositionBeforeLeadingTrivia(); - } - } - - /// Get the offset at which the content (excluding trailing trivia) of this - /// node ends. - AbsoluteOffsetPosition getAbsoluteEndPositionBeforeTrailingTrivia() const { - if (auto LastToken = getAbsoluteRaw().getLastToken()) { - return getAbsoluteEndPositionAfterTrailingTrivia().advancedBy( - -LastToken->getRaw()->getTrailingTriviaLength()); - } else { - return getAbsoluteEndPositionAfterTrailingTrivia(); - } - } - - /// Get the offset at chiwh the trailing trivia of this node ends. - AbsoluteOffsetPosition getAbsoluteEndPositionAfterTrailingTrivia() const { - return getAbsolutePositionBeforeLeadingTrivia().advancedBy( - getRaw()->getTextLength()); - } - - // MARK: - Getting the node's kind - - /// Returns which kind of syntax node this is. - SyntaxKind getKind() const { return getRaw()->getKind(); } - - /// Returns true if the data node represents type syntax. - bool isType() const { return getRaw()->isType(); } - - /// Returns true if the data node represents statement syntax. - bool isStmt() const { return getRaw()->isStmt(); } - - /// Returns true if the data node represents declaration syntax. - bool isDecl() const { return getRaw()->isDecl(); } - - /// Returns true if the data node represents expression syntax. - bool isExpr() const { return getRaw()->isExpr(); } - - /// Returns true if the data node represents pattern syntax. - bool isPattern() const { return getRaw()->isPattern(); } - - /// Returns true if this syntax is of some "unknown" kind. - bool isUnknown() const { return getRaw()->isUnknown(); } - - // MARK: - Miscellaneous - - /// Dump a debug description of the syntax data for debugging to - /// standard error. - void dump(llvm::raw_ostream &OS) const; - - SWIFT_DEBUG_DUMP; -}; - -class SyntaxData final : public SyntaxDataRef { - /// If this node is the root of a Syntax tree (i.e. \c Parent is \c nullptr ), - /// the arena in which this node's \c RawSyntax node has been allocated. - /// This keeps this \c RawSyntax nodes referenced by this tree alive. - /// If \c Parent has a value, this is always \c nullptr. - RC Arena; - - mutable std::atomic RefCount{0}; - - /// Create a non-root node with a \p Parent. - SyntaxData(const AbsoluteRawSyntax &AbsoluteRaw, - const RC &Parent) - : SyntaxDataRef(AbsoluteRaw, Parent.get()), Arena(nullptr) { - assert( - Parent != nullptr && - "Use SyntaxData(AbsoluteRawSyntax) or makeRoot to create a root node."); - assert(!Parent->isRef() && - "Cannot create a SyntaxData as a child of a SyntaxDataRef"); - Parent->Retain(); - } - - /// Create a new root node. - SyntaxData(const AbsoluteRawSyntax &AbsoluteRaw) - : SyntaxDataRef(AbsoluteRaw, nullptr), - Arena(AbsoluteRaw.getRaw()->getArena()) {} - -#ifndef NDEBUG - virtual bool isRef() const override { return false; } -#endif - -public: - SyntaxData(const SyntaxData &DataRef) - : SyntaxDataRef(DataRef.AbsoluteRaw, DataRef.Parent), - Arena(DataRef.Arena) { - if (auto Parent = getParent()) { - assert(!Parent->isRef() && - "Parent of a SyntaxData node should always be a SyntaxData node"); - Parent->Retain(); - } - } - - ~SyntaxData() { - assert(RefCount == 0 && - "Destruction occurred when there are still references to this."); - if (auto Parent = getParent()) { - Parent->Release(); - } - } - - /// Make a new \c SyntaxData node for the tree's root. - static RC makeRoot(const AbsoluteRawSyntax &AbsoluteRaw) { - return RC(new SyntaxData(AbsoluteRaw)); - } - - void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); } - - void Release() const { - int NewRefCount = RefCount.fetch_sub(1, std::memory_order_acq_rel) - 1; - assert(NewRefCount >= 0 && "Reference count was already zero."); - if (NewRefCount == 0) { - delete this; - } - } - - // MARK: - Retrieving related nodes - - /// Return the parent syntax if there is one, otherwise return \c nullptr. - RC getParent() const { - if (auto ParentRef = getParentRef()) { - assert(!ParentRef->isRef() && - "Parent of a SyntaxData node should always be a SyntaxData node"); - return RC(static_cast(ParentRef)); - } else { - return nullptr; - } - } - - /// Gets the child at the index specified by the provided cursor if there is - /// one, otherwise returns \c nullptr. - template - RC getChild(CursorType Cursor) const { - return getChild( - (AbsoluteSyntaxPosition::IndexInParentType)cursorIndex(Cursor)); - } - - /// Gets the child at the specified \p Index if there is one, otherwise - /// returns \c nullptr. - RC - getChild(AbsoluteSyntaxPosition::IndexInParentType Index) const; - - /// Get the node immediately before this current node that does contain a - /// non-missing token. Return \c nullptr if we cannot find such node. - RC getPreviousNode() const; - - /// Get the node immediately after this current node that does contain a - /// non-missing token. Return \c nullptr if we cannot find such node. - RC getNextNode() const; - - /// Get the first non-missing token node in this tree. Return \c nullptr if - /// this node does not contain non-missing tokens. - RC getFirstToken() const; - - /// Get the last non-missing token node in this tree. Return \c nullptr if - /// this node does not contain non-missing tokens. - RC getLastToken() const; - - // MARK: - Modifying node - - /// With a new \c RawSyntax node, create a new node from this one and - /// recursively rebuild the parental chain up to the root. - RC replacingSelf(const RawSyntax *NewRaw) const; - - /// Replace a child in the raw syntax and recursively rebuild the - /// parental chain up to the root. - template - RC replacingChild(const RawSyntax *RawChild, - CursorType ChildCursor) const { - auto NewRaw = AbsoluteRaw.getRaw()->replacingChild(ChildCursor, RawChild); - return replacingSelf(NewRaw); - } -}; - -} // end namespace syntax -} // end namespace swift - -namespace llvm { -namespace optional_detail { - -using swift::syntax::SyntaxDataRef; - -/// A custom \c OptionalStorage implementation for \c SyntaxDataRef. -/// This makes \c Optional a zero-cost wrapper around \c -/// SyntaxDataRef that uses an internal null type to denote a missing value. -/// This way, we can guarantee that \c SyntaxDataRef values are always non-null. -/// Additionally, it allows writing a value into the \c Optional's storage -/// pointer, which automatically flips the \c Optional's \c hasValue property -/// to \c true. E.g. -/// \code -/// Optional optRef; -/// optRef.hasValue(); // false -/// new (optRef.getPointer()) SyntaxDataRef(...); -/// optRef.hasValue(); // true -/// \endcode -/// This behaviour is important so we can stack-allocate an optional \c -/// SyntaxDataRef and later populate its value using the \c SyntaxDataRef's -/// \c getChild method. -template <> -class OptionalStorage { - SyntaxDataRef Storage; - -public: - OptionalStorage() : Storage(AbsoluteRawSyntax(nullptr), nullptr) {} - OptionalStorage(OptionalStorage const &other) = default; - OptionalStorage(OptionalStorage &&other) = default; - - template - explicit OptionalStorage(llvm::in_place_t, ArgTypes &&...Args) - : Storage(std::forward(Args)...) {} - - void reset() { Storage = SyntaxDataRef(AbsoluteRawSyntax(nullptr), nullptr); } - - bool has_value() const { return !Storage.getAbsoluteRaw().isNull(); } - bool hasValue() const { return !Storage.getAbsoluteRaw().isNull(); } - - SyntaxDataRef &value() & { - assert(hasValue()); - return Storage; - } - SyntaxDataRef &getValue() & { - assert(hasValue()); - return Storage; - } - SyntaxDataRef const &value() const & { - assert(hasValue()); - return Storage; - } - SyntaxDataRef const &getValue() const & { - assert(hasValue()); - return Storage; - } -#if LLVM_HAS_RVALUE_REFERENCE_THIS - SyntaxDataRef &&value() &&noexcept { - assert(hasValue()); - return std::move(Storage); - } - SyntaxDataRef &&getValue() &&noexcept { - assert(hasValue()); - return std::move(Storage); - } -#endif - - template - void emplace(Args &&...args) { - Storage = SyntaxDataRef(std::forward(args)...); - } - - OptionalStorage &operator=(const SyntaxDataRef &AbsoluteRaw) { - Storage = AbsoluteRaw; - return *this; - } - - OptionalStorage &operator=(SyntaxDataRef &&AbsoluteRaw) { - Storage = std::move(AbsoluteRaw); - return *this; - } - - OptionalStorage &operator=(OptionalStorage const &other) = default; - OptionalStorage &operator=(OptionalStorage &&other) = default; -}; -} // namespace optional_detail -} // end namespace llvm - -#endif // SWIFT_SYNTAX_SYNTAXDATA_H diff --git a/include/swift/Syntax/SyntaxFactory.h.gyb b/include/swift/Syntax/SyntaxFactory.h.gyb deleted file mode 100644 index dbafcc7d6acfd..0000000000000 --- a/include/swift/Syntax/SyntaxFactory.h.gyb +++ /dev/null @@ -1,191 +0,0 @@ -%{ - from gyb_syntax_support import * - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From SyntaxFactory.h.gyb. -//// Do Not Edit Directly! -//===------------- SyntaxFactory.h - Syntax Factory definition ------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_FACTORY_H -#define SWIFT_SYNTAX_FACTORY_H - -#include "swift/Syntax/SyntaxNodes.h" -#include "swift/Syntax/Trivia.h" -#include "llvm/ADT/ArrayRef.h" - -#include - -namespace swift { -namespace syntax { - -class SyntaxArena; - -/// The Syntax factory - the one-stop shop for making new Syntax nodes. -class SyntaxFactory { - RC Arena; - -public: - explicit SyntaxFactory(const RC &Arena) : Arena(Arena) {} - - /// Make any kind of token. - TokenSyntax makeToken(tok Kind, - StringRef Text, - StringRef LeadingTrivia, - StringRef TrailingTrivia, - SourcePresence Presence - ); - - /// Collect a list of tokens into a piece of "unknown" syntax. - UnknownSyntax makeUnknownSyntax(llvm::ArrayRef Tokens); - - Optional createSyntax( - SyntaxKind Kind, - llvm::ArrayRef Elements - ); - - const RawSyntax *createRaw( - SyntaxKind Kind, - llvm::ArrayRef Elements - ); - - /// Count the number of children for a given syntax node kind, - /// returning a pair of minimum and maximum count of children. The gap - /// between these two numbers is the number of optional children. - static std::pair countChildren(SyntaxKind Kind); - - Syntax makeBlankCollectionSyntax(SyntaxKind Kind); - -% for node in SYNTAX_NODES: -% if node.children: -% child_params = [] -% for child in node.children: -% param_type = child.type_name -% if child.is_optional: -% param_type = "llvm::Optional<%s>" % param_type -% end -% child_params.append("%s %s" % (param_type, child.name)) -% end -% child_params = ', '.join(child_params) - ${node.name} make${node.syntax_kind}(${child_params}); -% elif node.is_syntax_collection(): - ${node.name} make${node.syntax_kind}( - const std::vector<${node.collection_element_type}> &elts - ); -% end - - ${node.name} makeBlank${node.syntax_kind}(); -% end - -% for token in SYNTAX_TOKENS: -% if token.is_keyword: - TokenSyntax make${token.name}Keyword( - StringRef LeadingTrivia, - StringRef TrailingTrivi - ); -% elif token.text: - TokenSyntax make${token.name}Token( - StringRef LeadingTrivia, - StringRef TrailingTrivia - ); -% else: - TokenSyntax make${token.name}( - StringRef Text, - StringRef LeadingTrivia, StringRef TrailingTrivia - ); -% end -% end - -#pragma mark - Convenience APIs - - TupleTypeSyntax makeVoidTupleType(); - - /// Creates an labelled TupleTypeElementSyntax with the provided label, - /// colon, type and optional trailing comma. - TupleTypeElementSyntax makeTupleTypeElement( - llvm::Optional Label, - llvm::Optional Colon, TypeSyntax Type, - llvm::Optional TrailingComma - ); - - /// Creates an unlabelled TupleTypeElementSyntax with the provided type and - /// optional trailing comma. - TupleTypeElementSyntax - makeTupleTypeElement( - TypeSyntax Type, - llvm::Optional TrailingComma - ); - - /// Creates a TypeIdentifierSyntax with the provided name and leading/trailing - /// trivia. - TypeSyntax makeTypeIdentifier( - StringRef TypeName, - StringRef LeadingTrivia, StringRef TrailingTrivia - ); - - /// Creates a GenericParameterSyntax with no inheritance clause and an - /// optional trailing comma. - GenericParameterSyntax - makeGenericParameter( - TokenSyntax Name, - llvm::Optional TrailingComma - ); - - /// Creates a TypeIdentifierSyntax for the `Any` type. - TypeSyntax makeAnyTypeIdentifier( - StringRef LeadingTrivia, - StringRef TrailingTrivia - ); - - /// Creates a TypeIdentifierSyntax for the `Self` type. - TypeSyntax makeSelfTypeIdentifier( - StringRef LeadingTrivia, - StringRef TrailingTrivia - ); - - /// Creates a TokenSyntax for the `Type` identifier. - TokenSyntax makeTypeToken( - StringRef LeadingTrivia, - StringRef TrailingTrivia - ); - - /// Creates a TokenSyntax for the `Protocol` identifier. - TokenSyntax makeProtocolToken( - StringRef LeadingTrivia, - StringRef TrailingTrivia - ); - - /// Creates an `==` operator token. - TokenSyntax makeEqualityOperator( - StringRef LeadingTrivia, - StringRef TrailingTrivia - ); - - /// Whether a raw node kind `MemberKind` can serve as a member in a syntax - /// collection of the given syntax collection kind. - static bool canServeAsCollectionMemberRaw(SyntaxKind CollectionKind, - SyntaxKind MemberKind); - - /// Whether a raw node `Member` can serve as a member in a syntax collection - /// of the given syntax collection kind. - static bool canServeAsCollectionMemberRaw(SyntaxKind CollectionKind, - const RawSyntax *Member); - - /// Whether a node `Member` can serve as a member in a syntax collection of - /// the given syntax collection kind. - static bool canServeAsCollectionMember(SyntaxKind CollectionKind, Syntax Member); -}; -} -} - -#endif // SWIFT_SYNTAX_FACTORY_H diff --git a/include/swift/Syntax/SyntaxKind.h.gyb b/include/swift/Syntax/SyntaxKind.h.gyb deleted file mode 100644 index 2ca52bf917c65..0000000000000 --- a/include/swift/Syntax/SyntaxKind.h.gyb +++ /dev/null @@ -1,126 +0,0 @@ -%{ - from gyb_syntax_support import * - from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS - grouped_nodes = { kind: [] for kind in SYNTAX_BASE_KINDS } - for node in SYNTAX_NODES: - grouped_nodes[node.base_kind].append(node) - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From SyntaxKind.h.gyb. -//// Do Not Edit Directly! -//===--------------- SyntaxKind.h - Syntax Kind definitions ---------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_KIND_H -#define SWIFT_SYNTAX_KIND_H - -#include "swift/Basic/InlineBitfield.h" -#include "swift/Basic/JSONSerialization.h" -#include "llvm/Support/YAMLTraits.h" - -namespace swift { -namespace syntax { - -enum class SyntaxKind : uint16_t { - Token, -% for name, nodes in grouped_nodes.items(): -% for node in nodes: - ${node.syntax_kind}, -% end -% if name not in ["Syntax", "SyntaxCollection"]: -% first_kind = nodes[0].syntax_kind -% last_kind = nodes[-1].syntax_kind - First_${name} = ${first_kind}, - Last_${name} = ${last_kind}, -% end -% end - - // NOTE: Unknown must be the last kind. - Unknown, -}; - -void dumpSyntaxKind(llvm::raw_ostream &os, const SyntaxKind kind); - -/// Whether this kind is a syntax collection. -bool isCollectionKind(SyntaxKind Kind); - -bool isDeclKind(SyntaxKind Kind); - -bool isTypeKind(SyntaxKind Kind); - -bool isStmtKind(SyntaxKind Kind); - -bool isExprKind(SyntaxKind Kind); - -bool isPatternKind(SyntaxKind Kind); - -bool isTokenKind(SyntaxKind Kind); - -bool isUnknownKind(SyntaxKind Kind); - -SyntaxKind getUnknownKind(SyntaxKind Kind); -} // end namespace syntax - -bool parserShallOmitWhenNoChildren(syntax::SyntaxKind Kind); - -namespace json { - -/// Serialization traits for SyntaxKind. -template <> -struct ScalarReferenceTraits { - static StringRef stringRef(const syntax::SyntaxKind &value) { - switch (value) { - case syntax::SyntaxKind::Token: - return "\"Token\""; - case syntax::SyntaxKind::Unknown: - return "\"Unknown\""; -% for node in SYNTAX_NODES: - case syntax::SyntaxKind::${node.syntax_kind}: - return "\"${node.syntax_kind}\""; -% end - } - llvm_unreachable("unhandled kind"); - } - - static bool mustQuote(StringRef) { - // The string is already quoted. This is more efficient since it does not - // check for characters that need to be escaped - return false; - } -}; - -} // end namespace json -} // end namespace swift - -namespace llvm { -namespace yaml { - -/// Deserialization traits for SyntaxKind. -template <> -struct ScalarEnumerationTraits { - static void enumeration(IO &in, swift::syntax::SyntaxKind &value) { - in.enumCase(value, "Token", swift::syntax::SyntaxKind::Token); - in.enumCase(value, "Unknown", swift::syntax::SyntaxKind::Unknown); -% for node in SYNTAX_NODES: - in.enumCase(value, "${node.syntax_kind}", swift::syntax::SyntaxKind::${node.syntax_kind}); -% end - } -}; - -} // end namespace yaml -} // end namespace llvm - -namespace llvm { - raw_ostream &operator<<(raw_ostream &OS, swift::syntax::SyntaxKind Kind); -} // end namespace llvm -#endif // SWIFT_SYNTAX_KIND_H diff --git a/include/swift/Syntax/SyntaxNodes.h.gyb b/include/swift/Syntax/SyntaxNodes.h.gyb deleted file mode 100644 index 7b7e28397f543..0000000000000 --- a/include/swift/Syntax/SyntaxNodes.h.gyb +++ /dev/null @@ -1,205 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From SyntaxNodes.h.gyb. -//// Do Not Edit Directly! -//===---------------- SyntaxNodes.h - Syntax Node definitions -------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_NODES_H -#define SWIFT_SYNTAX_NODES_H - -#include "swift/Syntax/Syntax.h" -#include "swift/Syntax/SyntaxCollection.h" -#include "swift/Syntax/TokenSyntax.h" -#include "swift/Syntax/UnknownSyntax.h" - -namespace swift { -namespace syntax { - -% # Emit the non-collection classes first, then emit the collection classes -% # that reference these classes. - -% for node in SYNTAX_NODES: -% if not node.is_syntax_collection(): -class ${node.name}; -class ${node.name}Ref; -% end -% end - -% for node in SYNTAX_NODES: -% if node.is_syntax_collection(): -using ${node.name} = - SyntaxCollection; -using ${node.name}Ref = - SyntaxCollectionRef; -% end -% end - -% for node in SYNTAX_NODES: -% if not node.is_syntax_collection(): -% qualifier = "" if node.is_base() else "final" -% for line in dedented_lines(node.description): -/// ${line} -% end -class ${node.name} ${qualifier} : public ${node.base_type} { -% if node.is_buildable(): - friend class ${node.name}Builder; -% end - void validate() const; - -public: -% if node.children: - enum Cursor : uint32_t { -% for child in node.children: - ${child.name}, -% end - }; -% end - - ${node.name}(const RC &Data) : ${node.base_type}(Data) { - this->validate(); - } - -% for child in node.children: -% for line in dedented_lines(child.description): - /// ${line} -% end -% if child.is_optional: - llvm::Optional<${child.type_name}> get${child.name}() const; -% else: - ${child.type_name} get${child.name}() const; -% end - -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child.collection_element_name -% child_elt_type = child_node.collection_element_type -% if not child_elt: -% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name)) -% end - /// Adds the provided `${child_elt}` to the node's `${child.name}` - /// collection. The new raw node will be created in the same arena as this - /// node. - /// - param element: The new `${child_elt}` to add to the node's - /// `${child.name}` collection. - /// - returns: A copy of the receiver with the provided `${child_elt}` - /// appended to its `${child.name}` collection. - ${node.name} add${child_elt}(${child_elt_type} ${child_elt}); -% end - - /// Returns a copy of the receiver with its `${child.name}` replaced. The new - /// raw node will be created in the same arena as this node. - /// - param newChild: The new `${child.name}` to replace the node's - /// current `${child.name}`, if present. - ${node.name} - with${child.name}(llvm::Optional<${child.type_name}> New${child.type_name}); - -% end - - static bool kindof(SyntaxKind Kind) { -% if node.is_base(): - return is${node.syntax_kind}Kind(Kind); -% else: - return Kind == SyntaxKind::${node.syntax_kind}; -% end - } - - static bool classof(const Syntax *S) { - return kindof(S->getKind()); - } -}; - -/// See doc comment on \c SyntaxDataRef for more details. -class ${node.name}Ref ${qualifier} : public ${node.base_type}Ref { - friend class ${node.name}; // Non-Ref node is a friend so it can call validate - -public: -% if node.children: - using Cursor = ${node.name}::Cursor; // Use same cursor as non-Ref node. -% end - - explicit ${node.name}Ref(const SyntaxDataRef *Data) : ${node.base_type}Ref(Data) { - this->validate(); - } - - /// Create a \c ${node.name}Ref node, but don't validate it. Useful if the - /// underlying \c SyntaxDataRef is populated later. - /// \c validate should be called after \p Data has been populated. - ${node.name}Ref(const SyntaxDataRef *Data, no_validation_t) : ${node.base_type}Ref(Data) {} - - /// Demote a \c ${node.name} node to a \c ${node.name}Ref node. - /// The \c ${node.name} node continues to hold the \c SyntaxData and must thus - /// outlive the \c ${node.name}Ref node. - ${node.name}Ref(const ${node.name} &Node) : ${node.base_type}Ref(Node.getData().get()) {} - - /// Validate the syntax node. This is automatically done when creating a node - /// unless the \c no_validation_t marker is passed. - void validate() const; - -% for child in node.children: -% for line in dedented_lines(child.description): - /// ${line} -% end -% if child.is_optional: - inline OptionalOwnedSyntaxRef<${child.type_name}Ref> get${child.name}() const; -% else: - inline OwnedSyntaxRef<${child.type_name}Ref> get${child.name}() const; -% end - -% end - static bool kindof(SyntaxKind Kind) { -% if node.is_base(): - return is${node.syntax_kind}Kind(Kind); -% else: - return Kind == SyntaxKind::${node.syntax_kind}; -% end - } - - static bool classof(const SyntaxRef *S) { - return kindof(S->getKind()); - } -}; -% end -% end - -/// Calculating an identifier for all syntax nodes' structures for verification -/// purposes. -const char* getSyntaxStructureVersioningIdentifier(); - -% for node in SYNTAX_NODES: -% for child in node.children: -% if child.is_optional: -OptionalOwnedSyntaxRef<${child.type_name}Ref> ${node.name}Ref::get${child.name}() const { - OptionalOwnedSyntaxRef<${child.type_name}Ref> Result; - getDataRef()->getChildRef(Cursor::${child.name}, Result.getDataPtr()); - return Result; -} -% else: -OwnedSyntaxRef<${child.type_name}Ref> ${node.name}Ref::get${child.name}() const { - OwnedSyntaxRef<${child.type_name}Ref> Result; - getDataRef()->getPresentChildRef(Cursor::${child.name}, Result.getDataPtr()); - return Result; -} -% end -% end -% end - -} -} - -#endif // SWIFT_SYNTAX_NODES_H diff --git a/include/swift/Syntax/SyntaxVisitor.h.gyb b/include/swift/Syntax/SyntaxVisitor.h.gyb deleted file mode 100644 index 5c172a430c693..0000000000000 --- a/include/swift/Syntax/SyntaxVisitor.h.gyb +++ /dev/null @@ -1,57 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From SyntaxVisitor.h.gyb. -//// Do Not Edit Directly! -//===---------------- SyntaxVisitor.h - SyntaxVisitor definitions ---------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_VISITOR_H -#define SWIFT_SYNTAX_VISITOR_H - -#include "swift/Syntax/Syntax.h" -#include "swift/Syntax/SyntaxCollection.h" -#include "swift/Syntax/TokenSyntax.h" -#include "swift/Syntax/UnknownSyntax.h" -#include "swift/Syntax/SyntaxNodes.h" - -namespace swift { -namespace syntax { -struct SyntaxVisitor { - virtual ~SyntaxVisitor() {} - -% for node in SYNTAX_NODES: -% if is_visitable(node): - virtual void visit(${node.name} node); -% end -% end - - virtual void visit(TokenSyntax token) {} - - virtual void visitPre(Syntax node) {} - virtual void visitPost(Syntax node) {} - virtual void visit(Syntax node); - - void visitChildren(Syntax node) { - for (unsigned i = 0, e = node.getNumChildren(); i != e; ++i) { - if (auto Child = node.getChild(i)) - visit(*Child); - } - } -}; -} -} - -#endif // SWIFT_SYNTAX_VISITOR_H diff --git a/include/swift/Syntax/TokenKinds.h b/include/swift/Syntax/TokenKinds.h deleted file mode 100644 index efaf775e3c160..0000000000000 --- a/include/swift/Syntax/TokenKinds.h +++ /dev/null @@ -1,42 +0,0 @@ -//===--- TokenKinds.h - Token Kinds Interface -------------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the Token kinds. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_TOKENKINDS_H -#define SWIFT_TOKENKINDS_H - -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/raw_ostream.h" - -namespace swift { -enum class tok : uint8_t { -#define TOKEN(X) X, -#include "swift/Syntax/TokenKinds.def" - - NUM_TOKENS -}; - -/// Check whether a token kind is known to have any specific text content. -/// e.g., tol::l_paren has determined text however tok::identifier doesn't. -bool isTokenTextDetermined(tok kind); - -/// If a token kind has determined text, return the text; otherwise assert. -StringRef getTokenText(tok kind); - -void dumpTokenKind(llvm::raw_ostream &os, tok kind); -} // end namespace swift - -#endif // SWIFT_TOKENKINDS_H - diff --git a/include/swift/Syntax/TokenSyntax.h b/include/swift/Syntax/TokenSyntax.h deleted file mode 100644 index 1b4b8289a7c61..0000000000000 --- a/include/swift/Syntax/TokenSyntax.h +++ /dev/null @@ -1,145 +0,0 @@ -//===----------- TokenSyntax.h - Swift Token Interface ----------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file contains the interface for a `TokenSyntax`, which is a token -// that includes full-fidelity leading and trailing trivia. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_TOKENSYNTAX_H -#define SWIFT_SYNTAX_TOKENSYNTAX_H - -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/References.h" -#include "swift/Syntax/Syntax.h" -#include "swift/Syntax/TokenKinds.h" -#include "swift/Syntax/Trivia.h" - -namespace swift { -namespace syntax { - -class TokenSyntax final : public Syntax { -protected: - void validate() const { - assert(getRaw()->isToken()); - } -public: - TokenSyntax(const RC &Data) : Syntax(Data) { - validate(); - } - - static TokenSyntax missingToken(const tok Kind, StringRef Text, - const RC &Arena) { - return makeRoot(RawSyntax::missing(Kind, Text, Arena)); - } - - StringRef getLeadingTrivia() const { return getRaw()->getLeadingTrivia(); } - Trivia getLeadingTriviaPieces() const { return getRaw()->getLeadingTriviaPieces(); } - - StringRef getTrailingTrivia() const { return getRaw()->getTrailingTrivia(); } - Trivia getTrailingTriviaPieces() const { - return getRaw()->getTrailingTriviaPieces(); - } - - TokenSyntax withLeadingTrivia(StringRef Trivia) const { - auto NewRaw = getRaw()->withLeadingTrivia(Trivia); - return TokenSyntax(getData()->replacingSelf(NewRaw)); - } - - TokenSyntax withTrailingTrivia(StringRef Trivia) const { - auto NewRaw = getRaw()->withTrailingTrivia(Trivia); - return TokenSyntax(getData()->replacingSelf(NewRaw)); - } - - bool isMissing() const { - return getRaw()->isMissing(); - } - - tok getTokenKind() const { - return getRaw()->getTokenKind(); - } - - StringRef getText() const { - return getRaw()->getTokenText(); - } - - StringRef getIdentifierText() const { - StringRef text = getText(); - if (text.front() == '`') { - assert(text.back() == '`'); - return text.slice(1, text.size() - 1); - } - return text; - } - - static bool kindof(SyntaxKind Kind) { - return isTokenKind(Kind); - } - - static bool classof(const Syntax *S) { - return kindof(S->getKind()); - } -}; - -/// See comment in \c SyntaxRef. -/// -/// This duplicates some code from \c TokenSyntax, but since \c TokenSyntaxRef -/// needs to inherit from \c SyntaxRef and \c TokenSyntax needs to inherit from -/// \c Syntax, there's no easy way to share the code. And the duplicate is -/// fairly small. -class TokenSyntaxRef final : public SyntaxRef { -public: - TokenSyntaxRef(const SyntaxDataRef *Data) : SyntaxRef(Data) { - validate(); - } - TokenSyntaxRef(const SyntaxDataRef *Data, no_validation_t) - : SyntaxRef(Data) {} - - void validate() const { assert(getRaw()->isToken()); } - - /// Demote a \c TokenSyntax to a \c TokenSyntaxRef - TokenSyntaxRef(const TokenSyntax &Token) : SyntaxRef(Token.getData().get()) {} - - StringRef getLeadingTrivia() const { return getRaw()->getLeadingTrivia(); } - Trivia getLeadingTriviaPieces() const { - return getRaw()->getLeadingTriviaPieces(); - } - - StringRef getTrailingTrivia() const { return getRaw()->getTrailingTrivia(); } - Trivia getTrailingTriviaPieces() const { - return getRaw()->getTrailingTriviaPieces(); - } - - bool isMissing() const { return getRaw()->isMissing(); } - - tok getTokenKind() const { return getRaw()->getTokenKind(); } - - StringRef getText() const { return getRaw()->getTokenText(); } - - StringRef getIdentifierText() const { - StringRef text = getText(); - if (text.front() == '`') { - assert(text.back() == '`'); - return text.slice(1, text.size() - 1); - } - return text; - } - - static bool kindof(SyntaxKind Kind) { return isTokenKind(Kind); } - - static bool classof(const SyntaxRef *S) { return kindof(S->getKind()); } -}; - -} // end namespace syntax -} // end namespace swift - -#endif // SWIFT_SYNTAX_TOKENSYNTAX_H diff --git a/include/swift/Syntax/Trivia.h.gyb b/include/swift/Syntax/Trivia.h.gyb deleted file mode 100644 index 7dfc24faceb16..0000000000000 --- a/include/swift/Syntax/Trivia.h.gyb +++ /dev/null @@ -1,487 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - from gyb_syntax_support.Trivia import TRIVIAS - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From Trivia.h.gyb. -//// Do Not Edit Directly! -//===--- Trivia.h - Swift Syntax Trivia Interface ---------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines a data structure representing "trivia" in the Swift -// language, such as formatting text like whitespace, or other pieces of -// syntax that don't affect program behavior, like comments. -// -// All source trivia except for comments are kind of "run-length encoded". -// For example, a token might follow 2 newlines and 2 spaces, like so: -// -// func foo() { -// var x = 2 -// } -// -// Here, the 'var' keyword would have the following "leading" trivia: -// [ Newlines(2), Spaces(2) ] -// -// and the following "trailing" trivia: -// [ Spaces(1) ] -// -// Every terminal token in the tree has "leading" and "trailing" trivia. -// -// There is one basic rule to follow when attaching trivia: -// -// 1. A token owns all of its trailing trivia up to, but not including, -// the next newline character. -// -// 2. Looking backward in the text, a token owns all of the leading trivia -// up to and including the first contiguous sequence of newlines characters. -// -// For this example again: -// -// func foo() { -// var x = 2 -// } -// -// 'func' -// - Has no leading trivia. -// - Takes up the space after because of rule 1. -// - Leading: [] Trailing: [ Space(1) ] -// -// 'foo' -// - Has no leading trivia. 'func' ate it as its trailing trivia. -// - Has no trailing trivia, because it is butted up against the next '('. -// - Leading: [] Trailing: [] -// -// '(' -// - Has no leading or trailing trivia. -// - Leading: [] Trailing: [] -// -// ')' -// - Has no leading trivia. -// - Takes up the space after because of rule 1. -// - Leading: [] Trailing: [ Space(1) ] -// -// '{' -// - Has no leading trivia. ')' ate it as its trailing trivia. -// - Has no trailing trivia. Because of Rule 1, it doesn't take the newline. -// - Leading: [] Trailing: [] -// -// 'var' -// - Takes the newline and preceding two spaces because of Rule 2. -// - Takes the single space that follows because of Rule 1. -// - Leading: [ Newline(1), Space(2) ] Trailing: [ Space(1) ] -// -// ... and so on. -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_TRIVIA_H -#define SWIFT_SYNTAX_TRIVIA_H - -#include "swift/Basic/Debug.h" -#include "swift/Basic/OwnedString.h" -#include "swift/Basic/JSONSerialization.h" -#include "llvm/ADT/FoldingSet.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/Support/YAMLTraits.h" - -#include - -namespace swift { -namespace syntax { - -/// The kind of source trivia, such as spaces, newlines, or comments. -enum class TriviaKind { -% for trivia in TRIVIAS: - // ${trivia.comment} - ${trivia.name}, -% end -}; - -bool isCommentTriviaKind(TriviaKind kind); - -/// A contiguous stretch of a single kind of trivia. The constituent part of -/// a `Trivia` collection. -/// -/// For example, four spaces would be represented by -/// { TriviaKind::Space, 4, "" }. -/// -/// All trivia except for comments don't need to store text, since they can be -/// reconstituted using their Kind and Count. -/// -/// In general, you should deal with the actual Trivia collection instead -/// of individual pieces whenever possible. -class TriviaPiece { - TriviaKind Kind; - unsigned Count; - OwnedString Text; - - TriviaPiece(const TriviaKind Kind, const OwnedString Text) - : Kind(Kind), Count(1), Text(Text) {} - TriviaPiece(const TriviaKind Kind, const unsigned Count) - : Kind(Kind), Count(Count), Text() {} - - friend struct json::ObjectTraits; - friend struct llvm::yaml::MappingTraits; - -public: - -% for trivia in TRIVIAS: -% if trivia.is_collection(): - static TriviaPiece ${trivia.lower_name}s(unsigned Count) { - return {TriviaKind::${trivia.name}, Count}; - } - static TriviaPiece ${trivia.lower_name}() { - return ${trivia.lower_name}s(1); - } -% else: - static TriviaPiece ${trivia.lower_name}(const OwnedString Text) { - return {TriviaKind::${trivia.name}, Text}; - } -% end -% end - - static TriviaPiece fromText(TriviaKind kind, StringRef text); - - /// Return kind of the trivia. - TriviaKind getKind() const { return Kind; } - - /// Return the text of the trivia. - StringRef getText() const { return Text.str(); } - - /// Return the text of the trivia. - unsigned getCount() const { return Count; } - - /// Return textual length of the trivia. - size_t getTextLength() const { - switch (Kind) { -% for trivia in TRIVIAS: - case TriviaKind::${trivia.name}: -% if trivia.is_collection(): - return Count * ${trivia.characters_len()}; -% else: - return Text.size(); -% end -% end - } - llvm_unreachable("unhandled kind"); - } - - bool isComment() const { - return isCommentTriviaKind(getKind()); - } - - /// Try to compose this and Next to one TriviaPiece. - /// It returns true if it is succeeded. - bool trySquash(const TriviaPiece &Next); - - /// Print a debug representation of this trivia piece to the provided output - /// stream and indentation level. - void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const; - - /// Print this piece of trivia to the provided output stream. - void print(llvm::raw_ostream &OS) const; - - bool operator==(const TriviaPiece &Other) const { - return Kind == Other.Kind && - Count == Other.Count && - Text.str().compare(Other.Text.str()) == 0; - } - - bool operator!=(const TriviaPiece &Other) const { - return !(*this == Other); - } - - void Profile(llvm::FoldingSetNodeID &ID) const { - ID.AddInteger(unsigned(Kind)); - switch (Kind) { -% for trivia in TRIVIAS: - case TriviaKind::${trivia.name}: -% if trivia.is_collection(): - ID.AddInteger(Count); -% else: - ID.AddString(Text.str()); -% end - break; -% end - } - } -}; - -using TriviaList = std::vector; - -/// A collection of leading or trailing trivia. This is the main data structure -/// for thinking about trivia. -struct Trivia { - TriviaList Pieces; - - /// Get the begin iterator of the pieces. - TriviaList::const_iterator begin() const { - return Pieces.begin(); - } - - /// Get the end iterator of the pieces. - TriviaList::const_iterator end() const { - return Pieces.end(); - } - - /// Add a piece to the end of the collection. - void push_back(const TriviaPiece &Piece) { - Pieces.push_back(Piece); - } - - /// Add a piece to the beginning of the collection. - void push_front(const TriviaPiece &Piece) { - Pieces.insert(Pieces.begin(), Piece); - } - - /// Clear pieces. - void clear() { - Pieces.clear(); - } - - /// Return a reference to the first piece. - /// - /// Precondition: !empty() - const TriviaPiece &front() const { - assert(!empty()); - return Pieces.front(); - } - - /// Return a reference to the last piece. - /// - /// Precondition: !empty() - const TriviaPiece &back() const { - assert(!empty()); - return Pieces.back(); - } - - /// Remove the last piece from the Trivia collection. - /// - /// Precondition: !empty() - void pop_back() { - assert(!empty()); - Pieces.pop_back(); - } - - /// Returns true if there are no pieces in this Trivia collection. - bool empty() const { - return Pieces.empty(); - } - - /// Return the number of pieces in this Trivia collection. - size_t size() const { - return Pieces.size(); - } - - size_t getTextLength() const { - size_t Len = 0; - for (auto &P : Pieces) - Len += P.getTextLength(); - return Len; - } - - /// Append Next TriviaPiece or compose last TriviaPiece and - /// Next TriviaPiece to one last TriviaPiece if it can. - void appendOrSquash(const TriviaPiece &Next); - - /// Dump a debug representation of this Trivia collection to standard error. - SWIFT_DEBUG_DUMP; - - /// Dump a debug representation of this Trivia collection to the provided - /// stream and indentation level. - void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const; - - /// Print all of the pieces to the provided output stream in source order. - void print(llvm::raw_ostream &OS) const; - - /// Return a new Trivia collection by appending pieces from `Other`. - Trivia appending(const Trivia &Other) const; - Trivia operator+(const Trivia &Other) const; - - /// Look for the first TriviaPiece with the DesiredKind. If not found, - /// returns the end iterator. - TriviaList::const_iterator find(const TriviaKind DesiredKind) const; - - /// Returns true if the Trivia collection contains a piece of the given Kind. - bool contains(const TriviaKind Kind) const { - return find(Kind) != end(); - } - - bool operator==(const Trivia &Other) const { - if (Pieces.size() != Other.size()) { - return false; - } - - for (size_t i = 0; i < Pieces.size(); ++i) { - if (Pieces[i] != Other.Pieces[i]) { - return false; - } - } - - return true; - } - - bool operator!=(const Trivia &Other) const { - return !(*this == Other); - } - -% for trivia in TRIVIAS: -% if trivia.is_collection(): - static Trivia ${trivia.lower_name}s(unsigned Count) { - if (Count == 0) { - return {}; - } - return {{TriviaPiece::${trivia.lower_name}s(Count)}}; - } - static Trivia ${trivia.lower_name}() { - return {{TriviaPiece::${trivia.lower_name}s(1)}}; - } -% else: - static Trivia ${trivia.lower_name}(const OwnedString Text) { - assert(checkTriviaText(Text.str(), TriviaKind::${trivia.name})); - return {{TriviaPiece::${trivia.lower_name}(Text)}}; - } -% end -% end - -private: - static bool checkTriviaText(StringRef Text, TriviaKind Kind) { - switch(Kind) { - case TriviaKind::LineComment: - return Text.startswith("//"); - case TriviaKind::BlockComment: - return Text.startswith("/*") && Text.endswith("*/"); - case TriviaKind::DocLineComment: - return Text.startswith("///"); - case TriviaKind::DocBlockComment: - return Text.startswith("/**") && Text.endswith("*/"); - case TriviaKind::UnexpectedText: - case TriviaKind::Shebang: - return !Text.empty(); -% for trivia in TRIVIAS: -% if trivia.is_collection(): - case TriviaKind::${trivia.name}: return true; -% end -% end - } - } -}; -} // namespace syntax - -namespace json { -/// Serialization traits for TriviaPiece. -/// - All trivia pieces will have a "kind" key that contains the serialized -/// name of the trivia kind. -/// - Comment trivia will have the associated text of the comment under the -/// "value" key. -/// - All other trivia will have the associated integer count of their -/// occurrences under the "value" key. -template<> -struct ObjectTraits { - static void mapping(Output &out, syntax::TriviaPiece &value) { - auto kind = value.getKind(); - out.mapRequired("kind", kind); - switch (kind) { -% for trivia in TRIVIAS: - case syntax::TriviaKind::${trivia.name}: { -% if trivia.is_collection(): - auto count = value.getCount(); - out.mapRequired("value", count); -% else: - auto text = value.getText(); - out.mapRequired("value", text); -% end - break; - } -% end - } - } -}; - -/// Serialization traits for TriviaKind. -template <> -struct ScalarReferenceTraits { - static StringRef stringRef(const syntax::TriviaKind &value) { - switch (value) { -% for trivia in TRIVIAS: - case syntax::TriviaKind::${trivia.name}: - return "\"${trivia.name}\""; -% end - } - llvm_unreachable("unhandled kind"); - } - - static bool mustQuote(StringRef) { - // The string is already quoted. This is more efficient since it does not - // check for characters that need to be escaped - return false; - } -}; -} // namespace json -} // namespace swift - -namespace llvm { -namespace yaml { - -/// Deserialization traits for TriviaPiece. -/// - All trivia pieces will have a "kind" key that contains the serialized -/// name of the trivia kind. -/// - Comment trivia will have the associated text of the comment under the -/// "value" key. -/// - All other trivia will have the associated integer count of their -/// occurrences under the "value" key. -template<> -struct MappingTraits { - static swift::syntax::TriviaPiece mapping(IO &in) { - swift::syntax::TriviaKind kind; - in.mapRequired("kind", kind); - switch (kind) { -% for trivia in TRIVIAS: - case swift::syntax::TriviaKind::${trivia.name}: { -% if trivia.is_collection(): - -/// FIXME: This is a workaround for existing bug from llvm yaml parser -/// which would raise error when deserializing number with trailing character -/// like "1\n". See https://bugs.llvm.org/show_bug.cgi?id=15505 - StringRef str; - in.mapRequired("value", str); - unsigned count = std::atoi(str.data()); - return swift::syntax::TriviaPiece(kind, count); -% else: - StringRef text; - in.mapRequired("value", text); - return swift::syntax::TriviaPiece( - kind, swift::OwnedString::makeRefCounted(text)); -% end - break; - } -% end - } - llvm_unreachable("covered switch"); - } -}; - -/// Deserialization traits for TriviaKind. -template <> -struct ScalarEnumerationTraits { - static void enumeration(IO &in, swift::syntax::TriviaKind &value) { -% for trivia in TRIVIAS: - in.enumCase(value, "${trivia.name}", swift::syntax::TriviaKind::${trivia.name}); -% end - } -}; - - -} // namespace yaml -} // namespace llvm -#endif // SWIFT_SYNTAX_TRIVIA_H diff --git a/include/swift/Syntax/UnknownSyntax.h b/include/swift/Syntax/UnknownSyntax.h deleted file mode 100644 index 3ddd9c427cc6e..0000000000000 --- a/include/swift/Syntax/UnknownSyntax.h +++ /dev/null @@ -1,42 +0,0 @@ -//===--- UnknownSyntax.h - Swift Unknown Syntax Interface -------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_UNKNOWNSYNTAX_H -#define SWIFT_SYNTAX_UNKNOWNSYNTAX_H - -#include "swift/Syntax/SyntaxData.h" -#include "swift/Syntax/Syntax.h" - -namespace swift { -namespace syntax { - -#pragma mark unknown-syntax API - -/// A chunk of "unknown" syntax. -/// -/// Effectively wraps a tree of RawSyntax. -/// -/// This should not be vended by SyntaxFactory. -class UnknownSyntax : public Syntax { - void validate() const; -public: - UnknownSyntax(const RC &Data) : Syntax(Data) {} - - static bool classof(const Syntax *S) { - return S->isUnknown(); - } -}; - -} // end namespace syntax -} // end namespace swift - -#endif // SWIFT_SYNTAX_UNKNOWNSYNTAX_H diff --git a/include/swift/SyntaxParse/SyntaxTreeCreator.h b/include/swift/SyntaxParse/SyntaxTreeCreator.h deleted file mode 100644 index a44cf1be52c3c..0000000000000 --- a/include/swift/SyntaxParse/SyntaxTreeCreator.h +++ /dev/null @@ -1,98 +0,0 @@ -//===--- SyntaxTreeCreator.h - Syntax Tree Creation ------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#ifndef SWIFT_SYNTAX_PARSE_SYNTAXTREECREATOR_H -#define SWIFT_SYNTAX_PARSE_SYNTAXTREECREATOR_H - -#include "swift/Parse/SyntaxParseActions.h" -#include "swift/Syntax/References.h" -#include "llvm/ADT/StringRef.h" - -namespace swift { - class SourceManager; - class SyntaxParsingCache; - class SourceFile; - -namespace syntax { -class SyntaxArena; -class SourceFileSyntax; -} - -/// Receives the parsed syntax info from the parser and constructs a persistent -/// syntax tree by converting the data into \c RawSyntax objects, allocated from -/// a \c SyntaxArena. -/// -/// It also handles caching re-usable RawSyntax objects and skipping parsed -/// nodes via consulting a \c SyntaxParsingCache. -class SyntaxTreeCreator final : public SyntaxParseActions { - SourceManager &SM; - unsigned BufferID; - RC Arena; - - /// A string allocated in \c Arena that contains an exact copy of the source - /// file for which this \c SyntaxTreeCreator creates a syntax tree. \c - /// RawSyntax nodes can safely reference text inside this buffer since they - /// retain the \c SyntaxArena which holds the buffer. - StringRef ArenaSourceBuffer; - - /// A cache of nodes that can be reused when creating the current syntax - /// tree. - SyntaxParsingCache *SyntaxCache; - -public: - SyntaxTreeCreator(SourceManager &SM, unsigned bufferID, - SyntaxParsingCache *syntaxCache, - RC arena); - ~SyntaxTreeCreator(); - - Optional - realizeSyntaxRoot(OpaqueSyntaxNode root, const SourceFile &SF) override; - -private: - OpaqueSyntaxNode recordToken(tok tokenKind, StringRef leadingTrivia, - StringRef trailingTrivia, - CharSourceRange range) override; - - OpaqueSyntaxNode recordMissingToken(tok tokenKind, SourceLoc loc) override; - - OpaqueSyntaxNode - recordRawSyntax(syntax::SyntaxKind kind, - ArrayRef elements) override; - - std::pair - lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) override; - - OpaqueSyntaxNode makeDeferredToken(tok tokenKind, StringRef leadingTrivia, - StringRef trailingTrivia, - CharSourceRange range, - bool isMissing) override; - - OpaqueSyntaxNode makeDeferredLayout( - syntax::SyntaxKind k, bool IsMissing, - const MutableArrayRef &children) override; - - OpaqueSyntaxNode recordDeferredToken(OpaqueSyntaxNode deferred) override; - OpaqueSyntaxNode recordDeferredLayout(OpaqueSyntaxNode deferred) override; - - DeferredNodeInfo getDeferredChild(OpaqueSyntaxNode node, - size_t ChildIndex) const override; - - CharSourceRange getDeferredChildRange(OpaqueSyntaxNode node, - size_t ChildIndex, - SourceLoc StartLoc) const override; - - size_t getDeferredNumChildren(OpaqueSyntaxNode node) override; -}; - -} // end namespace swift - -#endif diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index b96d682ed5f13..70b8472eb384f 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -57,10 +57,9 @@ #include "swift/Strings.h" #include "swift/Subsystems.h" #include "swift/SymbolGraphGen/SymbolGraphOptions.h" -#include "swift/Syntax/References.h" -#include "swift/Syntax/SyntaxArena.h" #include "clang/AST/Type.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringSwitch.h" @@ -558,8 +557,6 @@ struct ASTContext::Implementation { llvm::FoldingSet SILLayouts; - RC TheSyntaxArena; - llvm::DenseMap overrideSigCache; Optional Converter; @@ -582,7 +579,6 @@ struct ASTContext::Implementation { ASTContext::Implementation::Implementation() : IdentifierTable(Allocator), - TheSyntaxArena(new syntax::SyntaxArena()), IntrinsicScratchContext(new llvm::LLVMContext()) {} ASTContext::Implementation::~Implementation() { @@ -750,10 +746,6 @@ void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) { Stats = stats; } -RC ASTContext::getSyntaxArena() const { - return getImpl().TheSyntaxArena; -} - /// getIdentifier - Return the uniqued and AST-Context-owned version of the /// specified string. Identifier ASTContext::getIdentifier(StringRef Str) const { diff --git a/lib/AST/ASTNode.cpp b/lib/AST/ASTNode.cpp index 1de399bb525c2..9a33c040d1734 100644 --- a/lib/AST/ASTNode.cpp +++ b/lib/AST/ASTNode.cpp @@ -21,6 +21,7 @@ #include "swift/AST/Pattern.h" #include "swift/AST/TypeRepr.h" #include "swift/Basic/SourceLoc.h" +#include "swift/Parse/Token.h" using namespace swift; @@ -133,6 +134,16 @@ void ASTNode::dump() const { dump(llvm::errs()); } +StringRef swift::getTokenText(tok kind) { + switch(kind) { +#define KEYWORD(KW) case tok::kw_##KW: return #KW; +#define POUND_KEYWORD(KW) case tok::pound_##KW: return "#"#KW; +#define PUNCTUATOR(PUN, TEXT) case tok::PUN: return TEXT; +#include "swift/AST/TokenKinds.def" + default: return StringRef(); + } +} + #define FUNC(T) \ bool ASTNode::is##T(T##Kind Kind) const { \ if (!is()) \ diff --git a/lib/AST/ASTPrinter.cpp b/lib/AST/ASTPrinter.cpp index aef114490a1a3..2d84fccde049c 100644 --- a/lib/AST/ASTPrinter.cpp +++ b/lib/AST/ASTPrinter.cpp @@ -438,7 +438,7 @@ operator<<(llvm::raw_ostream &OS, tok keyword) { #define KEYWORD(KW) case tok::kw_##KW: OS << #KW; break; #define POUND_KEYWORD(KW) case tok::pound_##KW: OS << "#"#KW; break; #define PUNCTUATOR(PUN, TEXT) case tok::PUN: OS << TEXT; break; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" default: llvm_unreachable("unexpected keyword or punctuator kind"); } @@ -450,7 +450,7 @@ uint8_t swift::getKeywordLen(tok keyword) { #define KEYWORD(KW) case tok::kw_##KW: return StringRef(#KW).size(); #define POUND_KEYWORD(KW) case tok::pound_##KW: return StringRef("#"#KW).size(); #define PUNCTUATOR(PUN, TEXT) case tok::PUN: return StringRef(TEXT).size(); -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" default: llvm_unreachable("unexpected keyword or punctuator kind"); } @@ -472,7 +472,7 @@ static bool escapeKeywordInContext(StringRef keyword, PrintNameContext context){ bool isKeyword = llvm::StringSwitch(keyword) #define KEYWORD(KW) \ .Case(#KW, true) -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" .Default(false); switch (context) { diff --git a/lib/AST/CMakeLists.txt b/lib/AST/CMakeLists.txt index 408ae55b3f91e..4b414c4260e42 100644 --- a/lib/AST/CMakeLists.txt +++ b/lib/AST/CMakeLists.txt @@ -161,7 +161,7 @@ endif() target_link_libraries(swiftAST PUBLIC swiftBasic - PRIVATE swiftSyntax swiftMarkup) + PRIVATE swiftMarkup) # intrinsics_gen is the LLVM tablegen target that generates the include files # where intrinsics and attributes are declared. swiftAST depends on these diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 090e973f4d85a..b8725182f1678 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -1187,7 +1187,7 @@ ObjectLiteralExpr::create(ASTContext &ctx, SourceLoc poundLoc, LiteralKind kind, StringRef ObjectLiteralExpr::getLiteralKindRawName() const { switch (getLiteralKind()) { #define POUND_OBJECT_LITERAL(Name, Desc, Proto) case Name: return #Name; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" } llvm_unreachable("unspecified literal"); } @@ -1195,7 +1195,7 @@ StringRef ObjectLiteralExpr::getLiteralKindRawName() const { StringRef ObjectLiteralExpr::getLiteralKindPlainName() const { switch (getLiteralKind()) { #define POUND_OBJECT_LITERAL(Name, Desc, Proto) case Name: return Desc; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" } llvm_unreachable("unspecified literal"); } diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index 6a58bc414cfef..b105053f7d29c 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -46,7 +46,6 @@ #include "swift/Demangling/ManglingMacros.h" #include "swift/Parse/Token.h" #include "swift/Strings.h" -#include "swift/Syntax/SyntaxNodes.h" #include "clang/Basic/Module.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" @@ -59,6 +58,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/SaveAndRestore.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/YAMLTraits.h" using namespace swift; @@ -1698,14 +1698,6 @@ Fingerprint SourceFile::getInterfaceHashIncludingTypeMembers() const { return Fingerprint{std::move(hash)}; } -syntax::SourceFileSyntax SourceFile::getSyntaxRoot() const { - assert(shouldBuildSyntaxTree() && "Syntax tree disabled"); - auto &eval = getASTContext().evaluator; - auto *mutableThis = const_cast(this); - return *evaluateOrDefault(eval, ParseSourceFileRequest{mutableThis}, {}) - .SyntaxRoot; -} - void DirectOperatorLookupRequest::writeDependencySink( evaluator::DependencyCollector &reqTracker, const TinyPtrVector &ops) const { @@ -3250,8 +3242,6 @@ SourceFile::getDefaultParsingOptions(const LangOptions &langOpts) { ParsingOptions opts; if (langOpts.DisablePoundIfEvaluation) opts |= ParsingFlags::DisablePoundIfEvaluation; - if (langOpts.BuildSyntaxTree) - opts |= ParsingFlags::BuildSyntaxTree; if (langOpts.CollectParsedToken) opts |= ParsingFlags::CollectParsedTokens; return opts; @@ -3270,11 +3260,6 @@ bool SourceFile::shouldCollectTokens() const { ParsingOpts.contains(ParsingFlags::CollectParsedTokens); } -bool SourceFile::shouldBuildSyntaxTree() const { - return Kind != SourceFileKind::SIL && - ParsingOpts.contains(ParsingFlags::BuildSyntaxTree); -} - bool SourceFile::hasDelayedBodyParsing() const { if (ParsingOpts.contains(ParsingFlags::DisableDelayedBodies)) return false; @@ -3284,8 +3269,6 @@ bool SourceFile::hasDelayedBodyParsing() const { return false; if (shouldCollectTokens()) return false; - if (shouldBuildSyntaxTree()) - return false; return true; } diff --git a/lib/AST/RawComment.cpp b/lib/AST/RawComment.cpp index 411dfb2ba86cb..d75687d51a248 100644 --- a/lib/AST/RawComment.cpp +++ b/lib/AST/RawComment.cpp @@ -83,7 +83,7 @@ static RawComment toRawComment(ASTContext &Context, CharSourceRange Range) { LangOptions FakeLangOpts; Lexer L(FakeLangOpts, SM, BufferID, nullptr, LexerMode::Swift, HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens, - TriviaRetentionMode::WithoutTrivia, Offset, EndOffset); + Offset, EndOffset); SmallVector Comments; Token Tok; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ce5ac4878d86e..4697bbdf5d11e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -12,10 +12,8 @@ # directory. list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen clang-tablegen-targets) -# Add generated libSyntax headers to global dependencies. -list(APPEND LLVM_COMMON_DEPENDS swift-syntax-generated-headers) +# Add generated Swift Syntax headers to global dependencies. list(APPEND LLVM_COMMON_DEPENDS swift-ast-generated-headers) -list(APPEND LLVM_COMMON_DEPENDS swift-parse-syntax-generated-headers) add_subdirectory(CompilerPluginSupport) add_subdirectory(APIDigester) @@ -55,6 +53,4 @@ add_subdirectory(SILGen) add_subdirectory(SILOptimizer) add_subdirectory(StaticMirror) add_subdirectory(SymbolGraphGen) -add_subdirectory(Syntax) -add_subdirectory(SyntaxParse) add_subdirectory(Threading) diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 9a3ecb0dd374e..83719fa0a7577 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -1602,7 +1602,6 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args, case options::OPT_resolve_imports: case options::OPT_typecheck: case options::OPT_dump_parse: - case options::OPT_emit_syntax: case options::OPT_print_ast: case options::OPT_dump_type_refinement_contexts: case options::OPT_dump_scope_maps: diff --git a/lib/Frontend/ArgsToFrontendOptionsConverter.cpp b/lib/Frontend/ArgsToFrontendOptionsConverter.cpp index b9192545b1c51..5dc90ae81e140 100644 --- a/lib/Frontend/ArgsToFrontendOptionsConverter.cpp +++ b/lib/Frontend/ArgsToFrontendOptionsConverter.cpp @@ -488,8 +488,6 @@ ArgsToFrontendOptionsConverter::determineRequestedAction(const ArgList &args) { return FrontendOptions::ActionType::DumpParse; if (Opt.matches(OPT_dump_ast)) return FrontendOptions::ActionType::DumpAST; - if (Opt.matches(OPT_emit_syntax)) - return FrontendOptions::ActionType::EmitSyntax; if (Opt.matches(OPT_merge_modules)) return FrontendOptions::ActionType::MergeModules; if (Opt.matches(OPT_dump_scope_maps)) diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 8abe878036809..fc3dbbc93cb93 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -572,11 +572,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args, Opts.NamedLazyMemberLoading &= !Args.hasArg(OPT_disable_named_lazy_member_loading); - if (Args.hasArg(OPT_verify_syntax_tree)) { - Opts.BuildSyntaxTree = true; - Opts.VerifySyntaxTree = true; - } - if (Args.hasArg(OPT_emit_fine_grained_dependency_sourcefile_dot_files)) Opts.EmitFineGrainedDependencySourcefileDotFiles = true; @@ -985,11 +980,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args, Opts.entryPointFunctionName = A->getValue(); } - if (FrontendOpts.RequestedAction == FrontendOptions::ActionType::EmitSyntax) { - Opts.BuildSyntaxTree = true; - Opts.VerifySyntaxTree = true; - } - // Configure lexing to parse and remember comments if: // - Emitting a swiftdoc/swiftsourceinfo // - Performing index-while-building diff --git a/lib/Frontend/Frontend.cpp b/lib/Frontend/Frontend.cpp index 88e9ca8001810..13d06b22b1a00 100644 --- a/lib/Frontend/Frontend.cpp +++ b/lib/Frontend/Frontend.cpp @@ -1288,9 +1288,6 @@ SourceFile *CompilerInstance::createSourceFileForMainModule( auto *inputFile = new (*Context) SourceFile(*mod, fileKind, bufferID, opts, isPrimary); - if (isMainBuffer) - inputFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache(); - return inputFile; } diff --git a/lib/Frontend/FrontendOptions.cpp b/lib/Frontend/FrontendOptions.cpp index 4bffba49f0915..2e3e460c60687 100644 --- a/lib/Frontend/FrontendOptions.cpp +++ b/lib/Frontend/FrontendOptions.cpp @@ -36,7 +36,6 @@ bool FrontendOptions::needsProperModuleName(ActionType action) { case ActionType::Typecheck: case ActionType::DumpParse: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::DumpInterfaceHash: case ActionType::PrintAST: case ActionType::PrintASTDecl: @@ -77,7 +76,6 @@ bool FrontendOptions::shouldActionOnlyParse(ActionType action) { switch (action) { case ActionType::Parse: case ActionType::DumpParse: - case ActionType::EmitSyntax: case ActionType::DumpInterfaceHash: case ActionType::EmitImportedModules: case ActionType::ScanDependencies: @@ -94,7 +92,6 @@ bool FrontendOptions::doesActionRequireSwiftStandardLibrary(ActionType action) { case ActionType::NoneAction: case ActionType::Parse: case ActionType::DumpParse: - case ActionType::EmitSyntax: case ActionType::DumpInterfaceHash: case ActionType::EmitImportedModules: case ActionType::ScanDependencies: @@ -143,7 +140,6 @@ bool FrontendOptions::doesActionRequireInputs(ActionType action) { case ActionType::REPL: case ActionType::Parse: case ActionType::DumpParse: - case ActionType::EmitSyntax: case ActionType::DumpInterfaceHash: case ActionType::EmitImportedModules: case ActionType::ScanDependencies: @@ -189,7 +185,6 @@ bool FrontendOptions::doesActionPerformEndOfPipelineActions(ActionType action) { case ActionType::REPL: case ActionType::Parse: case ActionType::DumpParse: - case ActionType::EmitSyntax: case ActionType::DumpInterfaceHash: case ActionType::EmitImportedModules: case ActionType::ScanDependencies: @@ -256,7 +251,6 @@ FrontendOptions::formatForPrincipalOutputFileForAction(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::DumpScopeMaps: @@ -325,7 +319,6 @@ bool FrontendOptions::canActionEmitDependencies(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::DumpScopeMaps: @@ -369,7 +362,6 @@ bool FrontendOptions::canActionEmitReferenceDependencies(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::DumpScopeMaps: @@ -412,7 +404,6 @@ bool FrontendOptions::canActionEmitModuleSummary(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::EmitImportedModules: @@ -455,7 +446,6 @@ bool FrontendOptions::canActionEmitClangHeader(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::EmitPCH: @@ -497,7 +487,6 @@ bool FrontendOptions::canActionEmitLoadedModuleTrace(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::DumpScopeMaps: @@ -546,7 +535,6 @@ bool FrontendOptions::canActionEmitModuleSemanticInfo(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::EmitPCH: @@ -590,7 +578,6 @@ bool FrontendOptions::canActionEmitConstValues(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::DumpScopeMaps: @@ -633,7 +620,6 @@ bool FrontendOptions::canActionEmitModule(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::EmitPCH: @@ -679,7 +665,6 @@ bool FrontendOptions::canActionEmitInterface(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::EmitImportedModules: @@ -721,7 +706,6 @@ bool FrontendOptions::doesActionProduceOutput(ActionType action) { case ActionType::Typecheck: case ActionType::DumpParse: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::DumpInterfaceHash: case ActionType::PrintAST: case ActionType::PrintASTDecl: @@ -781,7 +765,6 @@ bool FrontendOptions::doesActionProduceTextualOutput(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::DumpScopeMaps: @@ -810,7 +793,6 @@ bool FrontendOptions::doesActionGenerateSIL(ActionType action) { case ActionType::Typecheck: case ActionType::DumpParse: case ActionType::DumpInterfaceHash: - case ActionType::EmitSyntax: case ActionType::DumpAST: case ActionType::PrintAST: case ActionType::PrintASTDecl: @@ -852,7 +834,6 @@ bool FrontendOptions::doesActionGenerateIR(ActionType action) { case ActionType::DumpParse: case ActionType::DumpInterfaceHash: case ActionType::DumpAST: - case ActionType::EmitSyntax: case ActionType::PrintAST: case ActionType::PrintASTDecl: case ActionType::DumpScopeMaps: diff --git a/lib/FrontendTool/CMakeLists.txt b/lib/FrontendTool/CMakeLists.txt index 3fcc7a6867b1e..3d399aea92a17 100644 --- a/lib/FrontendTool/CMakeLists.txt +++ b/lib/FrontendTool/CMakeLists.txt @@ -6,7 +6,6 @@ add_swift_host_library(swiftFrontendTool STATIC MakeStyleDependencies.cpp TBD.cpp) add_dependencies(swiftFrontendTool - swift-syntax-generated-headers SwiftOptions) target_link_libraries(swiftFrontendTool INTERFACE clangAPINotes diff --git a/lib/FrontendTool/FrontendTool.cpp b/lib/FrontendTool/FrontendTool.cpp index 5475359704c79..cddd5d1bff9f4 100644 --- a/lib/FrontendTool/FrontendTool.cpp +++ b/lib/FrontendTool/FrontendTool.cpp @@ -68,8 +68,6 @@ #include "swift/Serialization/SerializedModuleLoader.h" #include "swift/Subsystems.h" #include "swift/SymbolGraphGen/SymbolGraphOptions.h" -#include "swift/Syntax/Serialization/SyntaxSerialization.h" -#include "swift/Syntax/SyntaxNodes.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringMap.h" @@ -139,18 +137,6 @@ getFileOutputStream(StringRef OutputFilename, ASTContext &Ctx) { return os; } -/// Writes the Syntax tree to the given file -static bool emitSyntax(const SourceFile &SF, StringRef OutputFilename) { - auto os = getFileOutputStream(OutputFilename, SF.getASTContext()); - if (!os) return true; - - json::Output jsonOut(*os, /*UserInfo=*/{}, /*PrettyPrint=*/false); - auto Root = SF.getSyntaxRoot().getRaw(); - jsonOut << *Root; - *os << "\n"; - return false; -} - /// Writes SIL out to the given file. static bool writeSIL(SILModule &SM, ModuleDecl *M, const SILOptions &Opts, StringRef OutputFilename) { @@ -1344,9 +1330,6 @@ static bool performAction(CompilerInstance &Instance, case FrontendOptions::ActionType::DumpInterfaceHash: getPrimaryOrMainSourceFile(Instance).dumpInterfaceHash(llvm::errs()); return Context.hadError(); - case FrontendOptions::ActionType::EmitSyntax: - return emitSyntax(getPrimaryOrMainSourceFile(Instance), - opts.InputsAndOutputs.getSingleOutputFilename()); case FrontendOptions::ActionType::EmitImportedModules: return emitImportedModules(Instance.getMainModule(), opts); diff --git a/lib/IDE/CodeCompletion.cpp b/lib/IDE/CodeCompletion.cpp index 4f8b8b4842ef9..80b29fb149ffe 100644 --- a/lib/IDE/CodeCompletion.cpp +++ b/lib/IDE/CodeCompletion.cpp @@ -50,7 +50,6 @@ #include "swift/Sema/IDETypeChecking.h" #include "swift/Strings.h" #include "swift/Subsystems.h" -#include "swift/Syntax/SyntaxKind.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Attr.h" #include "clang/AST/Comment.h" @@ -124,7 +123,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks { /// In situations when \c SyntaxKind hints or determines /// completions, i.e. a precedence group attribute, this /// can be set and used to control the code completion scenario. - SyntaxKind SyntxKind; + CodeCompletionCallbacks::PrecedenceGroupCompletionKind SyntxKind; int AttrParamIndex; bool IsInSil = false; @@ -257,7 +256,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks { void completeDeclAttrBeginning(bool Sil, bool isIndependent) override; void completeDeclAttrParam(DeclAttrKind DK, int Index) override; void completeEffectsSpecifier(bool hasAsync, bool hasThrows) override; - void completeInPrecedenceGroup(SyntaxKind SK) override; + void completeInPrecedenceGroup(CodeCompletionCallbacks::PrecedenceGroupCompletionKind SK) override; void completeNominalMemberBeginning( SmallVectorImpl &Keywords, SourceLoc introducerLoc) override; void completeAccessorBeginning(CodeCompletionExpr *E) override; @@ -467,7 +466,7 @@ void CodeCompletionCallbacksImpl::completeDeclAttrBeginning( AttTargetIsIndependent = isIndependent; } -void CodeCompletionCallbacksImpl::completeInPrecedenceGroup(SyntaxKind SK) { +void CodeCompletionCallbacksImpl::completeInPrecedenceGroup(CodeCompletionCallbacks::PrecedenceGroupCompletionKind SK) { assert(P.Tok.is(tok::code_complete)); SyntxKind = SK; @@ -806,7 +805,7 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink, DeclContext *DC, #define DECL_KEYWORD(kw) \ AddDeclKeyword(#kw, CodeCompletionKeywordKind::kw_##kw, None); -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" // Manually add "actor" because it's a contextual keyword. AddDeclKeyword("actor", CodeCompletionKeywordKind::None, None); @@ -843,7 +842,7 @@ static void addStmtKeywords(CodeCompletionResultSink &Sink, DeclContext *DC, addKeyword(Sink, Name, Kind, "", flair); }; #define STMT_KEYWORD(kw) AddStmtKeyword(#kw, CodeCompletionKeywordKind::kw_##kw); -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" } static void addCaseStmtKeywords(CodeCompletionResultSink &Sink) { diff --git a/lib/IDE/CodeCompletionResult.cpp b/lib/IDE/CodeCompletionResult.cpp index 6be581f4ebc6a..4d6ab6697ccac 100644 --- a/lib/IDE/CodeCompletionResult.cpp +++ b/lib/IDE/CodeCompletionResult.cpp @@ -455,7 +455,7 @@ void CodeCompletionResult::printPrefix(raw_ostream &OS) const { case CodeCompletionKeywordKind::pound_##X: \ Prefix.append("[#" #X "]"); \ break; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" } break; case CodeCompletionResultKind::Pattern: diff --git a/lib/IDE/CodeCompletionResultBuilder.h b/lib/IDE/CodeCompletionResultBuilder.h index 4811ae7243dd7..54ef59b58a70f 100644 --- a/lib/IDE/CodeCompletionResultBuilder.h +++ b/lib/IDE/CodeCompletionResultBuilder.h @@ -371,7 +371,7 @@ class CodeCompletionResultBuilder { if (escapeAllKeywords) { #define KEYWORD(kw) .Case(#kw, true) shouldEscape = llvm::StringSwitch(Word) -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" .Default(false); } else { shouldEscape = !canBeArgumentLabel(Word); diff --git a/lib/IDE/CompletionLookup.cpp b/lib/IDE/CompletionLookup.cpp index 4b3ff38b66233..79b14895391e2 100644 --- a/lib/IDE/CompletionLookup.cpp +++ b/lib/IDE/CompletionLookup.cpp @@ -15,12 +15,9 @@ #include "ExprContextAnalysis.h" #include "swift/AST/ParameterList.h" #include "swift/AST/SourceFile.h" -#include "swift/Syntax/SyntaxKind.h" -#include "swift/Syntax/TokenKinds.h" using namespace swift; using namespace swift::ide; -using swift::syntax::SyntaxKind; namespace { @@ -3050,31 +3047,30 @@ void CompletionLookup::collectPrecedenceGroups() { } } -void CompletionLookup::getPrecedenceGroupCompletions(SyntaxKind SK) { +void CompletionLookup::getPrecedenceGroupCompletions(CodeCompletionCallbacks::PrecedenceGroupCompletionKind SK) { switch (SK) { - case SyntaxKind::PrecedenceGroupAssociativity: + case CodeCompletionCallbacks::PrecedenceGroupCompletionKind::Associativity: addKeyword(getAssociativitySpelling(Associativity::None)); addKeyword(getAssociativitySpelling(Associativity::Left)); addKeyword(getAssociativitySpelling(Associativity::Right)); - break; - case SyntaxKind::PrecedenceGroupAssignment: + return; + case CodeCompletionCallbacks::PrecedenceGroupCompletionKind::Assignment: addKeyword(getTokenText(tok::kw_false), Type(), SemanticContextKind::None, CodeCompletionKeywordKind::kw_false); addKeyword(getTokenText(tok::kw_true), Type(), SemanticContextKind::None, CodeCompletionKeywordKind::kw_true); - break; - case SyntaxKind::PrecedenceGroupAttributeList: + return; + case CodeCompletionCallbacks::PrecedenceGroupCompletionKind::AttributeList: addKeyword("associativity"); addKeyword("higherThan"); addKeyword("lowerThan"); addKeyword("assignment"); - break; - case SyntaxKind::PrecedenceGroupRelation: + return; + case CodeCompletionCallbacks::PrecedenceGroupCompletionKind::Relation: collectPrecedenceGroups(); - break; - default: - llvm_unreachable("not a precedencegroup SyntaxKind"); + return; } + llvm_unreachable("not a precedencegroup SyntaxKind"); } void CompletionLookup::getPoundAvailablePlatformCompletions() { diff --git a/lib/IDE/SyntaxModel.cpp b/lib/IDE/SyntaxModel.cpp index 2da2fcdb59630..772690017c845 100644 --- a/lib/IDE/SyntaxModel.cpp +++ b/lib/IDE/SyntaxModel.cpp @@ -153,7 +153,7 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile) switch(Tok.getKind()) { #define KEYWORD(X) case tok::kw_##X: -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" #undef KEYWORD case tok::contextual_keyword: Kind = SyntaxNodeKind::Keyword; @@ -190,20 +190,20 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile) break; #define POUND_COND_DIRECTIVE_KEYWORD(Name) case tok::pound_##Name: -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" Kind = SyntaxNodeKind::BuildConfigKeyword; break; #define POUND_DIRECTIVE_KEYWORD(Name) case tok::pound_##Name: #define POUND_COND_DIRECTIVE_KEYWORD(Name) -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" Kind = SyntaxNodeKind::PoundDirectiveKeyword; break; #define POUND_OBJECT_LITERAL(Name, Desc, Proto) #define POUND_DIRECTIVE_KEYWORD(Name) #define POUND_KEYWORD(Name) case tok::pound_##Name: -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" Kind = SyntaxNodeKind::Keyword; break; case tok::identifier: diff --git a/lib/IDETool/CompletionInstance.cpp b/lib/IDETool/CompletionInstance.cpp index ebf900ee847ba..e76b17c6f1715 100644 --- a/lib/IDETool/CompletionInstance.cpp +++ b/lib/IDETool/CompletionInstance.cpp @@ -567,10 +567,6 @@ void swift::ide::CompletionInstance::performOperation( // because they're somewhat heavy operations and aren't needed for completion. Invocation.getFrontendOptions().IgnoreSwiftSourceInfo = true; - // Disable to build syntax tree because code-completion skips some portion of - // source text. That breaks an invariant of syntax tree building. - Invocation.getLangOptions().BuildSyntaxTree = false; - // We don't need token list. Invocation.getLangOptions().CollectParsedToken = false; diff --git a/lib/Migrator/CMakeLists.txt b/lib/Migrator/CMakeLists.txt index 993933bf090af..1dfd089d21a85 100644 --- a/lib/Migrator/CMakeLists.txt +++ b/lib/Migrator/CMakeLists.txt @@ -56,8 +56,7 @@ add_swift_host_library(swiftMigrator STATIC OptionalTryMigratorPass.cpp RewriteBufferEditsReceiver.cpp) target_link_libraries(swiftMigrator PRIVATE - swiftIDE - swiftSyntax) + swiftIDE) add_dependencies(swiftMigrator "symlink_migrator_data") diff --git a/lib/Parse/CMakeLists.txt b/lib/Parse/CMakeLists.txt index d6a32fb728169..f7ca9b61f9f9b 100644 --- a/lib/Parse/CMakeLists.txt +++ b/lib/Parse/CMakeLists.txt @@ -10,9 +10,6 @@ add_swift_host_library(swiftParse STATIC Confusables.cpp Lexer.cpp ParseDecl.cpp - ParsedRawSyntaxNode.cpp - ParsedRawSyntaxRecorder.cpp - ParsedTrivia.cpp ParseExpr.cpp ParseGeneric.cpp ParseIfConfig.cpp @@ -23,17 +20,9 @@ add_swift_host_library(swiftParse STATIC ParseStmt.cpp ParseType.cpp ParseVersion.cpp - PersistentParserState.cpp - SyntaxParsingCache.cpp - SyntaxParsingContext.cpp) -_swift_gyb_target_sources(swiftParse PRIVATE - ParsedSyntaxBuilders.cpp.gyb - ParsedSyntaxNodes.cpp.gyb - ParsedSyntaxRecorder.cpp.gyb) + PersistentParserState.cpp) target_link_libraries(swiftParse PRIVATE swiftAST - swiftSyntax - swiftSyntaxParse ) if (SWIFT_SWIFT_PARSER) @@ -85,6 +74,4 @@ if (SWIFT_SWIFT_PARSER) ) endif() -add_dependencies(swiftParse swift-parse-syntax-generated-headers) - set_swift_llvm_is_available(swiftParse) diff --git a/lib/Parse/Lexer.cpp b/lib/Parse/Lexer.cpp index e5c2ff011e70e..99a958efc1975 100644 --- a/lib/Parse/Lexer.cpp +++ b/lib/Parse/Lexer.cpp @@ -21,7 +21,6 @@ #include "swift/Basic/SourceManager.h" #include "swift/Parse/Confusables.h" #include "swift/Parse/RegexParserBridging.h" -#include "swift/Syntax/Trivia.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" @@ -40,7 +39,6 @@ void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn fn) { } using namespace swift; -using namespace swift::syntax; // clang::isAsciiIdentifierStart and clang::isAsciiIdentifierContinue are // deliberately not in this list as a reminder that they are using C rules for @@ -179,12 +177,12 @@ uint32_t swift::validateUTF8CharacterAndAdvance(const char *&Ptr, Lexer::Lexer(const PrincipalTag &, const LangOptions &LangOpts, const SourceManager &SourceMgr, unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode, - HashbangMode HashbangAllowed, CommentRetentionMode RetainComments, - TriviaRetentionMode TriviaRetention) + HashbangMode HashbangAllowed, + CommentRetentionMode RetainComments) : LangOpts(LangOpts), SourceMgr(SourceMgr), BufferID(BufferID), LexMode(LexMode), IsHashbangAllowed(HashbangAllowed == HashbangMode::Allowed), - RetainComments(RetainComments), TriviaRetention(TriviaRetention) { + RetainComments(RetainComments) { if (Diags) DiagQueue.emplace(*Diags, /*emitOnDestruction*/ false); } @@ -227,10 +225,10 @@ void Lexer::initialize(unsigned Offset, unsigned EndOffset) { Lexer::Lexer(const LangOptions &Options, const SourceManager &SourceMgr, unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode, - HashbangMode HashbangAllowed, CommentRetentionMode RetainComments, - TriviaRetentionMode TriviaRetention) + HashbangMode HashbangAllowed, + CommentRetentionMode RetainComments) : Lexer(PrincipalTag(), Options, SourceMgr, BufferID, Diags, LexMode, - HashbangAllowed, RetainComments, TriviaRetention) { + HashbangAllowed, RetainComments) { unsigned EndOffset = SourceMgr.getRangeForBuffer(BufferID).getByteLength(); initialize(/*Offset=*/0, EndOffset); } @@ -238,10 +236,9 @@ Lexer::Lexer(const LangOptions &Options, const SourceManager &SourceMgr, Lexer::Lexer(const LangOptions &Options, const SourceManager &SourceMgr, unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode, HashbangMode HashbangAllowed, CommentRetentionMode RetainComments, - TriviaRetentionMode TriviaRetention, unsigned Offset, - unsigned EndOffset) + unsigned Offset, unsigned EndOffset) : Lexer(PrincipalTag(), Options, SourceMgr, BufferID, Diags, LexMode, - HashbangAllowed, RetainComments, TriviaRetention) { + HashbangAllowed, RetainComments) { initialize(Offset, EndOffset); } @@ -253,7 +250,7 @@ Lexer::Lexer(const Lexer &Parent, State BeginState, State EndState, Parent.IsHashbangAllowed ? HashbangMode::Allowed : HashbangMode::Disallowed, - Parent.RetainComments, Parent.TriviaRetention) { + Parent.RetainComments) { assert(BufferID == SourceMgr.findBufferContainingLoc(BeginState.Loc) && "state for the wrong buffer"); assert(BufferID == SourceMgr.findBufferContainingLoc(EndState.Loc) && @@ -277,8 +274,7 @@ Token Lexer::getTokenAt(SourceLoc Loc) { "location from the wrong buffer"); Lexer L(LangOpts, SourceMgr, BufferID, getUnderlyingDiags(), LexMode, - HashbangMode::Allowed, CommentRetentionMode::None, - TriviaRetentionMode::WithoutTrivia); + HashbangMode::Allowed, CommentRetentionMode::None); L.restoreState(State(Loc)); return L.peekNextToken(); } @@ -301,13 +297,6 @@ void Lexer::formToken(tok Kind, const char *TokStart) { } StringRef TokenText { TokStart, static_cast(CurPtr - TokStart) }; - - if (TriviaRetention == TriviaRetentionMode::WithTrivia && Kind != tok::eof) { - TrailingTrivia = lexTrivia(/*IsForTrailingTrivia=*/true, CurPtr); - } else { - TrailingTrivia = StringRef(); - } - NextToken.setToken(Kind, TokenText, CommentLength); } @@ -654,12 +643,12 @@ bool Lexer::isOperator(StringRef string) { tok Lexer::kindOfIdentifier(StringRef Str, bool InSILMode) { #define SIL_KEYWORD(kw) #define KEYWORD(kw) if (Str == #kw) return tok::kw_##kw; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" // SIL keywords are only active in SIL mode. if (InSILMode) { #define SIL_KEYWORD(kw) if (Str == #kw) return tok::kw_##kw; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" } return tok::identifier; } @@ -696,7 +685,7 @@ void Lexer::lexHash() { tok Kind = llvm::StringSwitch(StringRef(CurPtr, tmpPtr-CurPtr)) #define POUND_KEYWORD(id) \ .Case(#id, tok::pound_##id) -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" .Default(tok::pound); // If we didn't find a match, then just return tok::pound. This is highly @@ -2615,7 +2604,7 @@ void Lexer::lexImpl() { NextToken.setAtStartOfLine(false); } - LeadingTrivia = lexTrivia(/*IsForTrailingTrivia=*/false, LeadingTriviaStart); + lexTrivia(/*IsForTrailingTrivia=*/false, LeadingTriviaStart); // Remember the start of the token so we can form the text range. const char *TokStart = CurPtr; @@ -2955,7 +2944,7 @@ static SourceLoc getLocForStartOfTokenInBuf(SourceManager &SM, Lexer L(FakeLangOptions, SM, BufferID, nullptr, LexerMode::Swift, HashbangMode::Allowed, CommentRetentionMode::None, - TriviaRetentionMode::WithoutTrivia, BufferStart, BufferEnd); + BufferStart, BufferEnd); // Lex tokens until we find the token that contains the source location. Token Tok; @@ -3145,138 +3134,6 @@ bool tryAdvanceToEndOfConflictMarker(const char *&CurPtr, return false; } -ParsedTrivia TriviaLexer::lexTrivia(StringRef TriviaStr) { - const char *CurPtr = TriviaStr.begin(); - const char *BufferEnd = TriviaStr.end(); - - ParsedTrivia Pieces; - - while (CurPtr < BufferEnd) { - // Iterate through the trivia and lex them into pieces. In the switch - // statement in this loop we can - // - 'continue' if we have successfully lexed a trivia piece to continue - // with the next piece. In this case CurPtr points to the next character - // to be lexed (which is not part of the lexed trivia). - // - 'break' to perform the default handling defined towards the bottom of - // the loop. - - const char *TriviaStart = CurPtr; - - switch (*CurPtr++) { - case '\n': - Pieces.appendOrSquash(TriviaKind::Newline, 1); - continue; - case '\r': - if (CurPtr < BufferEnd && CurPtr[0] == '\n') { - Pieces.appendOrSquash(TriviaKind::CarriageReturnLineFeed, 2); - ++CurPtr; - continue; - } else { - Pieces.appendOrSquash(TriviaKind::CarriageReturn, 1); - continue; - } - case ' ': - Pieces.appendOrSquash(TriviaKind::Space, 1); - continue; - case '\t': - Pieces.appendOrSquash(TriviaKind::Tab, 1); - continue; - case '\v': - Pieces.appendOrSquash(TriviaKind::VerticalTab, 1); - continue; - case '\f': - Pieces.appendOrSquash(TriviaKind::Formfeed, 1); - continue; - case '/': - if (CurPtr < BufferEnd && CurPtr[0] == '/') { - // '// ...' comment. - bool isDocComment = CurPtr[1] == '/'; - advanceToEndOfLine(CurPtr, BufferEnd); - size_t Length = CurPtr - TriviaStart; - Pieces.push_back(isDocComment ? TriviaKind::DocLineComment - : TriviaKind::LineComment, - Length); - continue; - } else if (CurPtr < BufferEnd && CurPtr[0] == '*') { - // '/* ... */' comment. - bool isDocComment = CurPtr[1] == '*'; - skipToEndOfSlashStarComment(CurPtr, BufferEnd); - size_t Length = CurPtr - TriviaStart; - Pieces.push_back(isDocComment ? TriviaKind::DocBlockComment - : TriviaKind::BlockComment, - Length); - continue; - } - break; - case '#': - if (CurPtr < BufferEnd && CurPtr[0] == '!') { - // Hashbang '#!/path/to/swift'. - advanceToEndOfLine(CurPtr, BufferEnd); - size_t Length = CurPtr - TriviaStart; - Pieces.push_back(TriviaKind::Shebang, Length); - continue; - } - break; - case '<': - case '>': - if (tryAdvanceToEndOfConflictMarker(CurPtr, BufferEnd)) { - // Conflict marker. - size_t Length = CurPtr - TriviaStart; - Pieces.push_back(TriviaKind::UnexpectedText, Length); - continue; - } - break; - case '\xEF': - if ((CurPtr + 1) < BufferEnd && CurPtr[0] == '\xBB' && CurPtr[1] == '\xBF') { - // BOM marker. - CurPtr = CurPtr + 2; - size_t Length = CurPtr - TriviaStart; - Pieces.push_back(TriviaKind::UnexpectedText, Length); - continue; - } - break; - case 0: { - size_t Length = CurPtr - TriviaStart; - Pieces.push_back(TriviaKind::UnexpectedText, Length); - continue; - } - default: - break; - } - - // Default handling for anything that didn't 'continue' in the above switch - // statement. - - for (; CurPtr < BufferEnd; ++CurPtr) { - bool HasFoundNextTriviaStart = false; - switch (*CurPtr) { - case '\n': - case '\r': - case ' ': - case '\t': - case '\v': - case '\f': - case '/': - case 0: - HasFoundNextTriviaStart = true; - break; - } - if (HasFoundNextTriviaStart) { - break; - } - } - - size_t Length = CurPtr - TriviaStart; - Pieces.push_back(TriviaKind::UnexpectedText, Length); - continue; - } - - assert(Pieces.getLength() == TriviaStr.size() && - "Not all characters in the source string have been used in trivia " - "pieces"); - return Pieces; -} - ArrayRef swift:: slice_token_array(ArrayRef AllTokens, SourceLoc StartLoc, SourceLoc EndLoc) { diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index b543d76ceaef8..f6cb66cc28ded 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -31,12 +31,9 @@ #include "swift/Basic/StringExtras.h" #include "swift/Parse/CodeCompletionCallbacks.h" #include "swift/Parse/ParseSILSupport.h" -#include "swift/Parse/ParsedSyntaxRecorder.h" #include "swift/Parse/Parser.h" -#include "swift/Parse/SyntaxParsingContext.h" #include "swift/Strings.h" #include "swift/Subsystems.h" -#include "swift/Syntax/SyntaxKind.h" #include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" @@ -47,7 +44,6 @@ #include using namespace swift; -using namespace syntax; namespace { /// A RAII object for deciding whether this DeclKind needs special @@ -274,16 +270,9 @@ void Parser::parseTopLevelItems(SmallVectorImpl &items) { Tok.is(tok::pound_endif)) { diagnose(Tok.getLoc(), diag::unexpected_conditional_compilation_block_terminator); - // Create 'UnknownDecl' for orphan directives. - SyntaxParsingContext itemCtxt(SyntaxContext, SyntaxKind::CodeBlockItem); - SyntaxParsingContext declCtxt(SyntaxContext, SyntaxContextKind::Decl); - consumeToken(); } } - - // Finalize the syntax context. - SyntaxContext->addToken(Tok, LeadingTrivia, TrailingTrivia); } bool Parser::parseTopLevelSIL() { @@ -374,14 +363,9 @@ ParserResult Parser::parseExtendedAvailabilitySpecList( VersionArg Introduced, Deprecated, Obsoleted; auto PlatformAgnostic = PlatformAgnosticAvailabilityKind::None; - SyntaxParsingContext AvailabilitySpecContext( - SyntaxContext, SyntaxKind::AvailabilitySpecList); - bool HasUpcomingEntry = false; { - SyntaxParsingContext EntryContext(SyntaxContext, - SyntaxKind::AvailabilityArgument); consumeToken(); if (consumeIf(tok::comma)) { HasUpcomingEntry = true; @@ -393,8 +377,6 @@ ParserResult Parser::parseExtendedAvailabilitySpecList( int ParamIndex = 0; while (HasUpcomingEntry) { - SyntaxParsingContext EntryContext(SyntaxContext, - SyntaxKind::AvailabilityArgument); auto ArgumentLoc = Tok.getLoc(); AnyAnnotations = true; StringRef ArgumentKindStr = Tok.getText(); @@ -510,8 +492,6 @@ ParserResult Parser::parseExtendedAvailabilitySpecList( Renamed = Value.getValue(); } - SyntaxContext->createNodeInPlace(SyntaxKind::AvailabilityLabeledArgument); - break; } @@ -559,8 +539,6 @@ ParserResult Parser::parseExtendedAvailabilitySpecList( VerArg.DelimiterLoc = DelimiterLoc; diagnoseDuplicate(VerArgWasEmpty); - SyntaxContext->createNodeInPlace(SyntaxKind::AvailabilityLabeledArgument); - break; } @@ -705,19 +683,11 @@ bool Parser::parseSpecializeAttributeArguments( llvm::function_ref parseSILSIPModule) { bool isSIL = SILAvailability != nullptr; typeErasedParamsCount = 0; - SyntaxParsingContext ContentContext(SyntaxContext, - SyntaxKind::SpecializeAttributeSpecList); // Parse optional "exported" and "kind" labeled parameters. while (!Tok.is(tok::kw_where)) { bool isAvailability = false; if (Tok.is(tok::identifier)) { auto ParamLabel = Tok.getText(); - SyntaxParsingContext ArgumentContext( - SyntaxContext, ParamLabel == "target" - ? SyntaxKind::TargetFunctionEntry : - (ParamLabel == "availability" ? - SyntaxKind::AvailabilityEntry - : SyntaxKind::LabeledSpecializeEntry)); if (ParamLabel != "exported" && ParamLabel != "kind" && ParamLabel != "target" && ParamLabel != "spi" && ParamLabel != "spiModule" && ParamLabel != "availability" && @@ -809,8 +779,6 @@ bool Parser::parseSpecializeAttributeArguments( } if (ParamLabel == "target") { if (!parseSILTargetName(*this)) { - SyntaxParsingContext ContentContext(SyntaxContext, - SyntaxKind::DeclName); DeclNameLoc loc; targetFunction = parseDeclNameRef( loc, diag::attr_specialize_expected_function, @@ -1093,8 +1061,6 @@ Parser::parseImplementsAttribute(SourceLoc AtLoc, SourceLoc Loc) { DeclNameRef MemberName; ParserResult ProtocolType; { - SyntaxParsingContext ContentContext( - SyntaxContext, SyntaxKind::ImplementsAttributeArguments); ProtocolType = parseType(); Status |= ProtocolType; @@ -1210,8 +1176,6 @@ static bool errorAndSkipUntilConsumeRightParen(Parser &P, StringRef attrName, bool Parser::parseDifferentiabilityParametersClause( SmallVectorImpl ¶meters, StringRef attrName, bool allowNamedParameters) { - SyntaxParsingContext DiffParamsClauseContext( - SyntaxContext, SyntaxKind::DifferentiabilityParamsClause); consumeToken(tok::identifier); if (!consumeIf(tok::colon)) { diagnose(Tok, diag::expected_colon_after_label, "wrt"); @@ -1221,8 +1185,6 @@ bool Parser::parseDifferentiabilityParametersClause( // Function that parses a parameter into `parameters`. Returns true if error // occurred. auto parseParam = [&](bool parseTrailingComma = true) -> bool { - SyntaxParsingContext DiffParamContext(SyntaxContext, - SyntaxKind::DifferentiabilityParam); SourceLoc paramLoc; switch (Tok.getKind()) { case tok::identifier: { @@ -1264,8 +1226,6 @@ bool Parser::parseDifferentiabilityParametersClause( // Parse opening '(' of the parameter list. if (Tok.is(tok::l_paren)) { - SyntaxParsingContext DiffParamsContext(SyntaxContext, - SyntaxKind::DifferentiabilityParams); consumeToken(tok::l_paren); // Parse first parameter. At least one is required. if (parseParam()) @@ -1274,7 +1234,6 @@ bool Parser::parseDifferentiabilityParametersClause( while (Tok.isNot(tok::r_paren)) if (parseParam()) return errorAndSkipUntilConsumeRightParen(*this, attrName, 2); - SyntaxContext->collectNodesInPlace(SyntaxKind::DifferentiabilityParamList); // Parse closing ')' of the parameter list. consumeToken(tok::r_paren); } @@ -1310,8 +1269,6 @@ bool Parser::parseDifferentiableAttributeArguments( // Store starting parser position. auto startingLoc = Tok.getLoc(); - SyntaxParsingContext ContentContext( - SyntaxContext, SyntaxKind::DifferentiableAttributeArguments); // Parse optional differentiability parameters. // Parse differentiability kind (optional). @@ -1476,8 +1433,6 @@ static bool parseQualifiedDeclName(Parser &P, Diag<> nameParseError, TypeRepr *&baseType, DeclNameRefWithLoc &original) { { - SyntaxParsingContext DeclNameContext(P.SyntaxContext, - SyntaxKind::QualifiedDeclName); // Parse base type. if (parseBaseTypeForQualifiedDeclName(P, baseType)) return true; @@ -1567,8 +1522,6 @@ ParserResult Parser::parseDerivativeAttribute(SourceLoc atLoc, return makeParserError(); } { - SyntaxParsingContext ContentContext( - SyntaxContext, SyntaxKind::DerivativeRegistrationAttributeArguments); // Parse the 'of:' label and colon. if (parseSpecificIdentifier("of", diag::attr_missing_label, "of", AttrName) || @@ -1650,8 +1603,6 @@ ParserResult Parser::parseTransposeAttribute(SourceLoc atLoc, return makeParserError(); } { - SyntaxParsingContext ContentContext( - SyntaxContext, SyntaxKind::DerivativeRegistrationAttributeArguments); // Parse the 'of:' label and colon. if (parseSpecificIdentifier("of", diag::attr_missing_label, "of", AttrName) || @@ -1688,10 +1639,7 @@ void Parser::parseObjCSelector(SmallVector &Names, SmallVector &NameLocs, bool &IsNullarySelector) { IsNullarySelector = true; - SyntaxParsingContext SelectorContext(SyntaxContext, SyntaxKind::ObjCSelector); while (true) { - SyntaxParsingContext SelectorPieceContext(SyntaxContext, - SyntaxKind::ObjCSelectorPiece); // Empty selector piece. if (Tok.is(tok::colon)) { Names.push_back(Identifier()); @@ -1740,8 +1688,7 @@ void Parser::parseObjCSelector(SmallVector &Names, break; } - // We didn't parse anything, don't create a ObjCSelectorPiece - SelectorPieceContext.setTransparent(); + // We didn't parse anything, don't create a selector piece. break; } } @@ -1765,9 +1712,6 @@ Parser::parseAvailabilityMacro(SmallVectorImpl &Specs) { if (NameMatch == Map.end()) return makeParserSuccess(); // No match, it could be a standard platform. - SyntaxParsingContext VersionRestrictionContext( - SyntaxContext, SyntaxKind::AvailabilityVersionRestriction); - consumeToken(); llvm::VersionTuple Version; @@ -1809,11 +1753,6 @@ void Parser::parseAllAvailabilityMacroArguments() { SourceManager &SM = Context.SourceMgr; LangOptions LangOpts = Context.LangOpts; - // The sub-parser is not actually parsing the source file but the LangOpts - // AvailabilityMacros. No point creating a libSyntax tree for it. In fact, the - // creation of a libSyntax tree would always fail because the - // AvailabilityMacro is not valid Swift source code. - LangOpts.BuildSyntaxTree = false; for (StringRef macro: LangOpts.AvailabilityMacros) { @@ -1873,9 +1812,6 @@ void Parser::parseAllAvailabilityMacroArguments() { ParserStatus Parser::parsePlatformVersionInList(StringRef AttrName, llvm::SmallVector &PlatformAndVersions) { - SyntaxParsingContext argumentContext(SyntaxContext, - SyntaxKind::AvailabilityVersionRestriction); - // Check for availability macros first. if (peekAvailabilityMacroName()) { SmallVector Specs; @@ -1977,9 +1913,6 @@ bool Parser::parseBackDeployAttribute(DeclAttributes &Attributes, llvm::SmallVector PlatformAndVersions; { - SyntaxParsingContext SpecListListContext( - SyntaxContext, SyntaxKind::BackDeployAttributeSpecList); - // Parse 'before' ':'. if (Tok.is(tok::identifier) && Tok.getText() == "before") { consumeToken(); @@ -1993,14 +1926,10 @@ bool Parser::parseBackDeployAttribute(DeclAttributes &Attributes, // Parse the version list. if (!Tok.is(tok::r_paren)) { - SyntaxParsingContext VersionListContext( - SyntaxContext, SyntaxKind::BackDeployVersionList); - ParseListItemResult Result; do { Result = parseListItem(Status, tok::r_paren, LeftLoc, RightLoc, /*AllowSepAfterLast=*/false, - SyntaxKind::BackDeployVersionArgument, [&]() -> ParserStatus { return parsePlatformVersionInList( AtAttrName, PlatformAndVersions); @@ -2181,11 +2110,6 @@ parseSingleAttrOptionImpl(Parser &P, SourceLoc Loc, SourceRange &AttrRange, return None; } - llvm::Optional ModDetailContext; - if (DK == DAK_ReferenceOwnership) { - ModDetailContext.emplace(P.SyntaxContext, SyntaxKind::DeclModifierDetail); - } - P.consumeToken(tok::l_paren); StringRef parsedName = P.Tok.getText(); @@ -2346,8 +2270,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, EffectsKind kind = EffectsKind::Unspecified; SourceLoc customStart, customEnd; { - SyntaxParsingContext TokListContext(SyntaxContext, SyntaxKind::TokenList); - if (Tok.isNot(tok::identifier)) { diagnose(Loc, diag::error_in_effects_attribute, "expected identifier"); return false; @@ -2503,9 +2425,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, break; } - SyntaxParsingContext ModDetailContext( - SyntaxContext, SyntaxKind::DeclModifierDetail); - consumeToken(tok::l_paren); // Parse the subject. @@ -2749,7 +2668,7 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, bool SuppressLaterDiags = false; if (parseList(tok::r_paren, LeftLoc, RightLoc, false, diag::originally_defined_in_missing_rparen, - SyntaxKind::AvailabilitySpecList, [&]() -> ParserStatus { + [&]() -> ParserStatus { SWIFT_DEFER { if (NK != NextSegmentKind::PlatformVersion) { NK = (NextSegmentKind)((uint8_t)NK + (uint8_t)1); @@ -2758,8 +2677,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, switch (NK) { // Parse 'module: "original_module_name"'. case NextSegmentKind::ModuleName: { - SyntaxParsingContext argumentContext(SyntaxContext, - SyntaxKind::AvailabilityLabeledArgument); // Parse 'module' ':'. if (!Tok.is(tok::identifier) || Tok.getText() != "module" || !peekToken().is(tok::colon)) { @@ -2843,9 +2760,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, SourceLoc LParenLoc = consumeToken(tok::l_paren); Optional filename; { - SyntaxParsingContext ContentContext( - SyntaxContext, SyntaxKind::NamedAttributeStringArgument); - // Parse 'sourceFile'. if (Tok.getText() != "sourceFile") { diagnose(LParenLoc, diag::attr_private_import_expected_sourcefile); @@ -2963,9 +2877,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, SourceLoc LParenLoc = consumeToken(tok::l_paren); DeclNameRef replacedFunction; { - SyntaxParsingContext ContentContext( - SyntaxContext, SyntaxKind::NamedAttributeStringArgument); - // Parse 'for'. if (Tok.getText() != "for") { diagnose(Loc, diag::attr_dynamic_replacement_expected_for); @@ -2980,9 +2891,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, } consumeToken(tok::colon); { - SyntaxParsingContext ContentContext(SyntaxContext, - SyntaxKind::DeclName); - DeclNameLoc loc; replacedFunction = parseDeclNameRef(loc, diag::attr_dynamic_replacement_expected_function, @@ -3021,7 +2929,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, bool invalid = false; { // Parse type-eraser type - SyntaxParsingContext ContentContext(SyntaxContext, SyntaxKind::Type); ErasedType = parseType(diag::attr_type_eraser_expected_type_name); invalid = ErasedType.hasCodeCompletion() || ErasedType.isNull(); } @@ -3183,7 +3090,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc, bool Parser::parseVersionTuple(llvm::VersionTuple &Version, SourceRange &Range, const Diagnostic &D) { - SyntaxParsingContext VersionContext(SyntaxContext, SyntaxKind::VersionTuple); // A version number is either an integer (8), a float (8.1), or a // float followed by a dot and an integer (8.1.0). if (!Tok.isAny(tok::integer_literal, tok::floating_literal)) { @@ -3284,7 +3190,6 @@ bool Parser::canParseCustomAttribute() { ParserResult Parser::parseCustomAttribute( SourceLoc atLoc, PatternBindingInitializer *&initContext) { assert(Tok.is(tok::identifier)); - SyntaxContext->setCreateSyntax(SyntaxKind::CustomAttribute); // Parse a custom attribute. auto type = parseType(diag::expected_type, ParseTypeReason::CustomAttribute); @@ -3772,7 +3677,6 @@ ParserStatus Parser::parseTypeAttribute(TypeAttributes &Attributes, // Recover by eating @foo(...) when foo is not known. consumeToken(); - SyntaxParsingContext TokListContext(SyntaxContext, SyntaxKind::TokenList); if (Tok.is(tok::l_paren) && getEndOfPreviousLoc() == Tok.getLoc()) { CancellableBacktrackingScope backtrack(*this); @@ -3991,7 +3895,6 @@ ParserStatus Parser::parseDeclAttributeList( ParserStatus Status; while (Tok.isAny(tok::at_sign, tok::pound_if)) { if (Tok.is(tok::at_sign)) { - SyntaxParsingContext AttrCtx(SyntaxContext, SyntaxKind::Attribute); SourceLoc AtLoc = consumeToken(); Status |= parseDeclAttribute(Attributes, AtLoc, initContext); } else { @@ -4018,7 +3921,6 @@ ParserStatus Parser::parseDeclAttributeList( return makeParserSuccess(); PatternBindingInitializer *initContext = nullptr; - SyntaxParsingContext AttrListCtx(SyntaxContext, SyntaxKind::AttributeList); return parseDeclAttributeList(Attributes, IfConfigsAreDeclAttrs, initContext); } @@ -4058,9 +3960,7 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes, SourceLoc &StaticLoc, StaticSpellingKind &StaticSpelling, bool isFromClangAttribute) { - SyntaxParsingContext ListContext(SyntaxContext, SyntaxKind::ModifierList); bool isError = false; - bool hasModifier = false; while (true) { switch (Tok.getKind()) { @@ -4068,11 +3968,9 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes, case tok::kw_fileprivate: case tok::kw_internal: case tok::kw_public: { - SyntaxParsingContext ModContext(SyntaxContext, SyntaxKind::DeclModifier); // We still model these specifiers as attributes. isError |= parseNewDeclAttribute(Attributes, /*AtLoc=*/{}, DAK_AccessControl); - hasModifier = true; continue; } @@ -4126,11 +4024,8 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes, continue; } - SyntaxParsingContext ModContext(SyntaxContext, - SyntaxKind::DeclModifier); isError |= parseNewDeclAttribute( Attributes, /*AtLoc=*/{}, Kind, isFromClangAttribute); - hasModifier = true; continue; } @@ -4145,9 +4040,7 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes, StaticLoc = Tok.getLoc(); StaticSpelling = StaticSpellingKind::KeywordStatic; } - SyntaxParsingContext ModContext(SyntaxContext, SyntaxKind::DeclModifier); consumeToken(tok::kw_static); - hasModifier = true; continue; } @@ -4179,9 +4072,7 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes, StaticLoc = Tok.getLoc(); StaticSpelling = StaticSpellingKind::KeywordClass; } - SyntaxParsingContext ModContext(SyntaxContext, SyntaxKind::DeclModifier); consumeToken(tok::kw_class); - hasModifier = true; continue; } @@ -4189,17 +4080,12 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes, // Eat an invalid token in decl modifier context. Error tokens are // diagnosed by the lexer, so we don't need to emit another diagnostic. consumeToken(tok::unknown); - hasModifier = true; continue; default: break; } - // If we don't have any modifiers, don't bother to construct an empty list. - if (!hasModifier) - ListContext.setTransparent(); - // If we 'break' out of the switch, modifier list has ended. return isError; } @@ -4267,7 +4153,6 @@ Parser::parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier, } ParserStatus status; - SyntaxParsingContext AttrListCtx(SyntaxContext, SyntaxKind::AttributeList); while (Tok.is(tok::at_sign)) { // Ignore @substituted in SIL mode and leave it for the type parser. if (isInSILMode() && peekToken().getText() == "substituted") @@ -4275,7 +4160,6 @@ Parser::parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier, if (Attributes.AtLoc.isInvalid()) Attributes.AtLoc = Tok.getLoc(); - SyntaxParsingContext AttrCtx(SyntaxContext, SyntaxKind::Attribute); SourceLoc AtLoc = consumeToken(); status |= parseTypeAttribute(Attributes, AtLoc, initContext); if (status.isError()) @@ -4640,7 +4524,7 @@ bool Parser::isStartOfSILDecl() { return false; #define SIL_KEYWORD(Name) #define TOKEN(Name) case tok:: Name: return false; -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" } llvm_unreachable("Unhandled case in switch"); } @@ -4648,8 +4532,6 @@ bool Parser::isStartOfSILDecl() { void Parser::consumeDecl(ParserPosition BeginParserPosition, ParseDeclOptions Flags, bool IsTopLevel) { - SyntaxParsingContext Discarding(SyntaxContext); - Discarding.disable(); SourceLoc CurrentLoc = Tok.getLoc(); SourceLoc EndLoc = PreviousLoc; @@ -4740,8 +4622,6 @@ Parser::parseDecl(ParseDeclOptions Flags, [&](SmallVectorImpl &Decls, bool IsActive) { ParserStatus Status; bool PreviousHadSemi = true; - SyntaxParsingContext DeclListCtx(SyntaxContext, - SyntaxKind::MemberDeclList); while (Tok.isNot(tok::pound_else, tok::pound_endif, tok::pound_elseif, tok::eof)) { if (Tok.is(tok::r_brace)) { @@ -4783,9 +4663,6 @@ Parser::parseDecl(ParseDeclOptions Flags, return Result; } - SyntaxParsingContext DeclParsingContext(SyntaxContext, - SyntaxContextKind::Decl); - // Note that we're parsing a declaration. StructureMarkerRAII ParsingDecl(*this, Tok.getLoc(), StructureMarkerKind::Declaration); @@ -4818,7 +4695,6 @@ Parser::parseDecl(ParseDeclOptions Flags, auto parseLetOrVar = [&](bool HasLetOrVarKeyword) { // Collect all modifiers into a modifier list. - DeclParsingContext.setCreateSyntax(SyntaxKind::VariableDecl); llvm::SmallVector Entries; DeclResult = parseDeclVar(Flags, Attributes, Entries, StaticLoc, StaticSpelling, tryLoc, HasLetOrVarKeyword); @@ -4833,7 +4709,6 @@ Parser::parseDecl(ParseDeclOptions Flags, auto parseFunc = [&](bool HasFuncKeyword) { // Collect all modifiers into a modifier list. - DeclParsingContext.setCreateSyntax(SyntaxKind::FunctionDecl); DeclResult = parseDeclFunc(StaticLoc, StaticSpelling, Flags, Attributes, HasFuncKeyword); StaticLoc = SourceLoc(); // we handled static if present. @@ -4842,11 +4717,9 @@ Parser::parseDecl(ParseDeclOptions Flags, switch (Tok.getKind()) { case tok::kw_import: - DeclParsingContext.setCreateSyntax(SyntaxKind::ImportDecl); DeclResult = parseDeclImport(Flags, Attributes); break; case tok::kw_extension: - DeclParsingContext.setCreateSyntax(SyntaxKind::ExtensionDecl); DeclResult = parseDeclExtension(Flags, Attributes); break; case tok::kw_let: @@ -4855,21 +4728,17 @@ Parser::parseDecl(ParseDeclOptions Flags, break; } case tok::kw_typealias: - DeclParsingContext.setCreateSyntax(SyntaxKind::TypealiasDecl); DeclResult = parseDeclTypeAlias(Flags, Attributes); MayNeedOverrideCompletion = true; break; case tok::kw_associatedtype: - DeclParsingContext.setCreateSyntax(SyntaxKind::AssociatedtypeDecl); DeclResult = parseDeclAssociatedType(Flags, Attributes); break; case tok::kw_enum: - DeclParsingContext.setCreateSyntax(SyntaxKind::EnumDecl); DeclResult = parseDeclEnum(Flags, Attributes); break; case tok::kw_case: { llvm::SmallVector Entries; - DeclParsingContext.setCreateSyntax(SyntaxKind::EnumCaseDecl); DeclResult = parseDeclEnumCase(Flags, Attributes, Entries); if ((AttrStatus.hasCodeCompletion() || DeclResult.hasCodeCompletion()) && isCodeCompletionFirstPass()) @@ -4879,38 +4748,30 @@ Parser::parseDecl(ParseDeclOptions Flags, break; } case tok::kw_class: - DeclParsingContext.setCreateSyntax(SyntaxKind::ClassDecl); DeclResult = parseDeclClass(Flags, Attributes); break; case tok::kw_struct: - DeclParsingContext.setCreateSyntax(SyntaxKind::StructDecl); DeclResult = parseDeclStruct(Flags, Attributes); break; case tok::kw_init: - DeclParsingContext.setCreateSyntax(SyntaxKind::InitializerDecl); DeclResult = parseDeclInit(Flags, Attributes); break; case tok::kw_deinit: - DeclParsingContext.setCreateSyntax(SyntaxKind::DeinitializerDecl); DeclResult = parseDeclDeinit(Flags, Attributes); break; case tok::kw_operator: - DeclParsingContext.setCreateSyntax(SyntaxKind::OperatorDecl); DeclResult = parseDeclOperator(Flags, Attributes); break; case tok::kw_precedencegroup: - DeclParsingContext.setCreateSyntax(SyntaxKind::PrecedenceGroupDecl); DeclResult = parseDeclPrecedenceGroup(Flags, Attributes); break; case tok::kw_protocol: - DeclParsingContext.setCreateSyntax(SyntaxKind::ProtocolDecl); DeclResult = parseDeclProtocol(Flags, Attributes); break; case tok::kw_func: parseFunc(/*HasFuncKeyword=*/true); break; case tok::kw_subscript: { - DeclParsingContext.setCreateSyntax(SyntaxKind::SubscriptDecl); llvm::SmallVector Entries; DeclResult = parseDeclSubscript(StaticLoc, StaticSpelling, Flags, Attributes, Entries); @@ -4940,7 +4801,6 @@ Parser::parseDecl(ParseDeclOptions Flags, DeclResult = makeParserCodeCompletionResult(); break; } else if (Context.LangOpts.hasFeature(Feature::Macros)) { - DeclParsingContext.setCreateSyntax(SyntaxKind::MacroExpansionDecl); DeclResult = parseDeclMacroExpansion(Flags, Attributes); break; } @@ -4960,7 +4820,6 @@ Parser::parseDecl(ParseDeclOptions Flags, if (Tok.isContextualKeyword("actor") && peekToken().is(tok::identifier)) { Tok.setKind(tok::contextual_keyword); - DeclParsingContext.setCreateSyntax(SyntaxKind::ActorDecl); DeclResult = parseDeclClass(Flags, Attributes); break; } @@ -5287,8 +5146,6 @@ ParserResult Parser::parseDeclImport(ParseDeclOptions Flags, ImportPath::Builder importPath; bool HasNext; do { - SyntaxParsingContext AccessCompCtx(SyntaxContext, - SyntaxKind::AccessPathComponent); if (Tok.is(tok::code_complete)) { consumeToken(); if (CodeCompletion) { @@ -5309,9 +5166,6 @@ ParserResult Parser::parseDeclImport(ParseDeclOptions Flags, HasNext = consumeIf(tok::period); } while (HasNext); - // Collect all access path components to an import path. - SyntaxContext->collectNodesInPlace(SyntaxKind::AccessPath); - if (Tok.is(tok::code_complete)) { // We omit the code completion token if it immediately follows the module // identifiers. @@ -5363,28 +5217,20 @@ ParserResult Parser::parseDeclImport(ParseDeclOptions Flags, ParserStatus Parser::parseInheritance( SmallVectorImpl &Inherited, bool allowClassRequirement, bool allowAnyObject) { - SyntaxParsingContext InheritanceContext(SyntaxContext, - SyntaxKind::TypeInheritanceClause); - consumeToken(tok::colon); - SyntaxParsingContext TypeListContext(SyntaxContext, - SyntaxKind::InheritedTypeList); SourceLoc classRequirementLoc; ParserStatus Status; SourceLoc prevComma; bool HasNextType; do { - SyntaxParsingContext TypeContext(SyntaxContext, SyntaxKind::InheritedType); SWIFT_DEFER { // Check for a ',', which indicates that there are more protocols coming. HasNextType = consumeIf(tok::comma, prevComma); }; // Parse the 'class' keyword for a class requirement. if (Tok.is(tok::kw_class)) { - SyntaxParsingContext ClassTypeContext(SyntaxContext, - SyntaxKind::ClassRestrictionType); // If we aren't allowed to have a class requirement here, complain. auto classLoc = consumeToken(); if (!allowClassRequirement) { @@ -5565,9 +5411,6 @@ ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi, .fixItInsert(endOfPrevious, ";"); } - SyntaxParsingContext DeclContext(SyntaxContext, - SyntaxKind::MemberDeclListItem); - if (Tok.isAny(tok::pound_sourceLocation, tok::pound_line)) { auto LineDirectiveStatus = parseLineDirective(Tok.is(tok::pound_line)); if (LineDirectiveStatus.isErrorOrHasCompletion()) @@ -5575,11 +5418,7 @@ ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi, return LineDirectiveStatus; } - ParserResult Result; - if (loadCurrentSyntaxNodeFromCache()) { - return ParserStatus(); - } - Result = parseDecl( + ParserResult Result = parseDecl( Options, IsAtStartOfLineOrPreviousHadSemi, /* IfConfigsAreDeclAttrs=*/false, handler); if (Result.isParseErrorOrHasCompletion()) @@ -5662,7 +5501,6 @@ Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag, ParserStatus Status; bool PreviousHadSemi = true; { - SyntaxParsingContext ListContext(SyntaxContext, SyntaxKind::MemberDeclList); while (Tok.isNot(tok::r_brace)) { Status |= parseDeclItem(PreviousHadSemi, Options, [&](Decl *D) { decls.push_back(D); }); @@ -5673,10 +5511,7 @@ Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag, } } } - if (parseMatchingToken(tok::r_brace, RBLoc, ErrorDiag, LBLoc)) { - // Synthesize an r_brace syntax node if the token is absent - SyntaxContext->synthesize(tok::r_brace, RBLoc); - } + parseMatchingToken(tok::r_brace, RBLoc, ErrorDiag, LBLoc); // Increase counter. if (auto *stat = Context.Stats) { @@ -5787,7 +5622,6 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) { if (trailingWhereHadCodeCompletion && CodeCompletion) CodeCompletion->setParsedDecl(ext); - SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock); SourceLoc LBLoc, RBLoc; { @@ -5817,8 +5651,6 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) { ParserResult Parser::parseDeclPoundDiagnostic() { bool isError = Tok.is(tok::pound_error); - SyntaxParsingContext LocalContext(SyntaxContext, - isError ? SyntaxKind::PoundErrorDecl : SyntaxKind::PoundWarningDecl); SourceLoc startLoc = consumeToken(isError ? tok::pound_error : tok::pound_warning); @@ -5900,8 +5732,6 @@ ParserResult Parser::parseDeclPoundDiagnostic() { } ParserStatus Parser::parseLineDirective(bool isLine) { - SyntaxParsingContext PoundSourceLocation(SyntaxContext, - SyntaxKind::PoundSourceLocation); SourceLoc Loc = consumeToken(); if (isLine) { diagnose(Loc, diag::line_directive_style_deprecated) @@ -5932,9 +5762,6 @@ ParserStatus Parser::parseLineDirective(bool isLine) { } { - SyntaxParsingContext Args(SyntaxContext, - SyntaxKind::PoundSourceLocationArgs); - if (parseSpecificIdentifier("file", diag::sourceLocation_expected, "file:") || parseToken(tok::colon, diag::sourceLocation_expected, ":")) @@ -6056,9 +5883,6 @@ ParserStatus Parser::parseLineDirective(bool isLine) { ParserResult Parser:: parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) { ParserPosition startPosition = getParserPosition(); - llvm::Optional TmpCtxt; - TmpCtxt.emplace(SyntaxContext); - TmpCtxt->setBackTracking(); SourceLoc TypeAliasLoc = consumeToken(tok::kw_typealias); SourceLoc EqualLoc; @@ -6070,7 +5894,6 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) { *this, Id, IdLoc, "typealias", [](const Token &next) { return next.isAny(tok::colon, tok::equal); }); if (Status.isErrorOrHasCompletion()) { - TmpCtxt->setTransparent(); return Status; } @@ -6095,14 +5918,11 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) { } if (Flags.contains(PD_InProtocol) && !genericParams && !Tok.is(tok::equal)) { - TmpCtxt.reset(); // If we're in a protocol and don't see an '=' this looks like leftover Swift 2 // code intending to be an associatedtype. backtrackToPosition(startPosition); return parseDeclAssociatedType(Flags, Attributes); } - TmpCtxt->setTransparent(); - TmpCtxt.reset(); auto *TAD = new (Context) TypeAliasDecl(TypeAliasLoc, EqualLoc, Id, IdLoc, genericParams, CurDeclContext); @@ -6112,8 +5932,6 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) { if (Tok.is(tok::colon) || Tok.is(tok::equal)) { ContextChange CC(*this, TAD); - SyntaxParsingContext InitCtx(SyntaxContext, - SyntaxKind::TypeInitializerClause); if (Tok.is(tok::colon)) { // It is a common mistake to write "typealias A : Int" instead of = Int. // Recognize this and produce a fixit. @@ -6211,8 +6029,6 @@ ParserResult Parser::parseDeclAssociatedType(Parser::ParseDeclOptions ParserResult UnderlyingTy; if (Tok.is(tok::equal)) { - SyntaxParsingContext InitContext(SyntaxContext, - SyntaxKind::TypeInitializerClause); consumeToken(tok::equal); UnderlyingTy = parseType(diag::expected_type_in_associatedtype); Status |= UnderlyingTy; @@ -6359,7 +6175,6 @@ static ParameterList *parseOptionalAccessorArgument(SourceLoc SpecifierLoc, // If the SpecifierLoc is invalid, then the caller just wants us to synthesize // the default, not actually try to parse something. if (SpecifierLoc.isValid() && P.Tok.is(tok::l_paren)) { - SyntaxParsingContext ParamCtx(P.SyntaxContext, SyntaxKind::AccessorParameter); StartLoc = P.consumeToken(tok::l_paren); if (P.Tok.isNot(tok::identifier)) { P.diagnose(P.Tok, diag::expected_accessor_parameter_name, @@ -6394,9 +6209,6 @@ bool Parser::canDelayFunctionBodyParsing(bool &HasNestedTypeDeclarations) { if (!isDelayedParsingEnabled() && !isCodeCompletionFirstPass()) return false; - SyntaxParsingContext disabled(SyntaxContext); - SyntaxContext->disable(); - // Skip until the matching right curly bracket; If it has a potential regex // literal, we can't skip. We don't care others, so just ignore them; CancellableBacktrackingScope BackTrack(*this); @@ -6417,10 +6229,6 @@ bool Parser::canDelayFunctionBodyParsing(bool &HasNestedTypeDeclarations) { } void Parser::skipSILUntilSwiftDecl() { - // For now, create 'UnknownDecl' for all SIL declarations. - SyntaxParsingContext itemCtxt(SyntaxContext, SyntaxKind::CodeBlockItem); - SyntaxParsingContext declCtxt(SyntaxContext, SyntaxContextKind::Decl); - // Tell the lexer we're about to start lexing SIL. Lexer::SILBodyRAII sbr(*L); @@ -6567,16 +6375,12 @@ static bool parseAccessorIntroducer(Parser &P, // Parse the contextual keywords for 'mutating' and 'nonmutating' before // get and set. { - SyntaxParsingContext ModifierCtx(P.SyntaxContext, SyntaxKind::DeclModifier); - if (P.Tok.isContextualKeyword("mutating")) { P.parseNewDeclAttribute(Attributes, /*AtLoc*/ {}, DAK_Mutating); } else if (P.Tok.isContextualKeyword("nonmutating")) { P.parseNewDeclAttribute(Attributes, /*AtLoc*/ {}, DAK_NonMutating); } else if (P.Tok.isContextualKeyword("__consuming")) { P.parseNewDeclAttribute(Attributes, /*AtLoc*/ {}, DAK_Consuming); - } else { - ModifierCtx.setTransparent(); } } @@ -6681,19 +6485,10 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags, ParameterList *Indices, bool parsingLimitedSyntax = Flags.contains(PD_InProtocol) || SF.Kind == SourceFileKind::SIL; - SyntaxParsingContext AccessorListCtx(SyntaxContext, - SyntaxKind::AccessorBlock); - // If the body is completely empty, preserve it. This is at best a getter with // an implicit fallthrough off the end. if (peekToken().is(tok::r_brace)) { accessors.LBLoc = consumeToken(tok::l_brace); - // Give syntax node an empty accessor list. - if (SyntaxContext->isEnabled()) { - SourceLoc listLoc = leadingTriviaLoc(); - SyntaxContext->addSyntax( - ParsedSyntaxRecorder::makeBlankAccessorList(listLoc, *SyntaxContext)); - } accessors.RBLoc = consumeToken(tok::r_brace); // In the limited syntax, fall out and let the caller handle it. @@ -6742,9 +6537,6 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags, ParameterList *Indices, bool hasEffectfulGet = false; accessors.LBLoc = consumeToken(tok::l_brace); while (!Tok.isAny(tok::r_brace, tok::eof)) { - Optional AccessorCtx; - AccessorCtx.emplace(SyntaxContext, SyntaxKind::AccessorDecl); - // Parse introducer if possible. DeclAttributes Attributes; AccessorKind Kind = AccessorKind::Get; @@ -6752,9 +6544,6 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags, ParameterList *Indices, bool NotAccessor = parseAccessorIntroducer( *this, Attributes, Kind, Loc); if (NotAccessor) { - AccessorCtx->setTransparent(); - AccessorCtx.reset(); - if (Tok.is(tok::code_complete)) { // Handle code completion here only if it's not the first accessor. // If it's the first accessor, it's handled in function body parsing @@ -6788,7 +6577,6 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags, ParameterList *Indices, // This is an implicit getter. Cancel accessor contexts, backtrack to '{' // position. backtrack.reset(); - AccessorListCtx.setTransparent(); parseImplicitGetter(); return makeParserSuccess(); } @@ -6865,8 +6653,6 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags, ParameterList *Indices, } backtrack->cancelBacktrack(); backtrack.reset(); - // Collect all explicit accessors to a list. - AccessorListCtx.collectNodesInPlace(SyntaxKind::AccessorList); // Parse the final '}'. if (Status.isError()) skipUntil(tok::r_brace); @@ -7210,11 +6996,8 @@ Parser::parseDeclVar(ParseDeclOptions Flags, // Always return the result for PBD. return makeParserResult(Status, PBD); }; - SyntaxParsingContext PBListCtx(SyntaxContext, SyntaxKind::PatternBindingList); bool HasNext; do { - SyntaxParsingContext PatternBindingCtx(SyntaxContext, - SyntaxKind::PatternBinding); Pattern *pattern; { // In our recursive parse, remember that we're in a var/let pattern. @@ -7270,7 +7053,6 @@ Parser::parseDeclVar(ParseDeclOptions Flags, // Parse an initializer if present. if (Tok.is(tok::equal)) { - SyntaxParsingContext InitCtx(SyntaxContext, SyntaxKind::InitializerClause); // If we're not in a local context, we'll need a context to parse initializers // into (should we have one). This happens for properties and global // variables in libraries. @@ -7843,7 +7625,6 @@ ParserResult Parser::parseDeclEnum(ParseDeclOptions Flags, Status |= whereStatus; } - SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock); SourceLoc LBLoc, RBLoc; { if (parseMemberDeclList(LBLoc, RBLoc, @@ -7878,8 +7659,6 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags, SourceLoc CommaLoc; for (;;) { - SyntaxParsingContext ElementContext(SyntaxContext, - SyntaxKind::EnumCaseElement); Identifier Name; SourceLoc NameLoc; @@ -7961,9 +7740,6 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags, ParserResult RawValueExpr; LiteralExpr *LiteralRawValueExpr = nullptr; if (Tok.is(tok::equal)) { - SyntaxParsingContext InitContext(SyntaxContext, - SyntaxKind::InitializerClause); - EqualsLoc = consumeToken(); { CodeCompletionCallbacks::InEnumElementRawValueRAII @@ -8036,8 +7812,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags, break; CommaLoc = consumeToken(tok::comma); } - SyntaxContext->collectNodesInPlace(SyntaxKind::EnumCaseElementList); - + if (!(Flags & PD_AllowEnumElement)) { diagnose(CaseLoc, diag::disallowed_enum_element); // Don't add the EnumElementDecls unless the current context @@ -8123,7 +7898,6 @@ ParserResult Parser::parseDeclStruct(ParseDeclOptions Flags, } // Make the entities of the struct as a code block. - SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock); SourceLoc LBLoc, RBLoc; { // Parse the body. @@ -8239,7 +8013,6 @@ ParserResult Parser::parseDeclClass(ParseDeclOptions Flags, Status |= whereStatus; } - SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock); SourceLoc LBLoc, RBLoc; { // Parse the body. @@ -8259,9 +8032,6 @@ ParserResult Parser::parseDeclClass(ParseDeclOptions Flags, ParserStatus Parser::parsePrimaryAssociatedTypes( SmallVectorImpl &AssocTypeNames) { - SyntaxParsingContext GPSContext(SyntaxContext, - SyntaxKind::PrimaryAssociatedTypeClause); - SourceLoc LAngleLoc = consumeStartingLess(); auto Result = parsePrimaryAssociatedTypeList(AssocTypeNames); @@ -8284,14 +8054,8 @@ ParserStatus Parser::parsePrimaryAssociatedTypes( ParserStatus Parser::parsePrimaryAssociatedTypeList( SmallVectorImpl &AssocTypeNames) { ParserStatus Result; - SyntaxParsingContext PATContext(SyntaxContext, - SyntaxKind::PrimaryAssociatedTypeList); - bool HasNextParam = false; do { - SyntaxParsingContext ATContext(SyntaxContext, - SyntaxKind::PrimaryAssociatedType); - // Parse the name of the parameter. Identifier Name; SourceLoc NameLoc; @@ -8386,7 +8150,6 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) { // Parse the body. { - SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock); SourceLoc LBraceLoc; SourceLoc RBraceLoc; { @@ -8469,8 +8232,6 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc, SourceLoc ArrowLoc; ParserResult ElementTy; { - SyntaxParsingContext ReturnCtxt(SyntaxContext, SyntaxKind::ReturnClause); - // '->' if (!consumeIf(tok::arrow, ArrowLoc)) { if (!Indices.isParseErrorOrHasCompletion()) @@ -8884,13 +8645,11 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name, SourceLoc colonLoc, groupLoc; Identifier groupName; if (Tok.is(tok::colon)) { - SyntaxParsingContext GroupCtxt(SyntaxContext, - SyntaxKind::OperatorPrecedenceAndTypes); colonLoc = consumeToken(); if (Tok.is(tok::code_complete)) { if (CodeCompletion && !isPrefix && !isPostfix) { CodeCompletion->completeInPrecedenceGroup( - SyntaxKind::PrecedenceGroupRelation); + CodeCompletionCallbacks::PrecedenceGroupCompletionKind::Relation); } consumeToken(); @@ -8901,9 +8660,6 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name, diag::operator_decl_expected_precedencegroup, /*diagnoseDollarPrefix=*/false); - SyntaxParsingContext ListCtxt(SyntaxContext, - SyntaxKind::DesignatedTypeList); - if (Context.TypeCheckerOpts.EnableOperatorDesignatedTypes) { // Designated types have been removed; consume the list (mainly for source // compatibility with old swiftinterfaces) and emit a warning. @@ -8921,10 +8677,7 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name, } while (Tok.isNot(tok::eof)) { - SyntaxParsingContext ElementCtxt(SyntaxContext, - SyntaxKind::DesignatedTypeElement); if (!consumeIf(tok::comma, typesEndLoc)) { - ElementCtxt.setTransparent(); break; } @@ -9065,8 +8818,6 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags, // Empty body. if (Tok.is(tok::r_brace)) { // Create empty attribute list. - SyntaxParsingContext(SyntaxContext, - SyntaxKind::PrecedenceGroupAttributeList); rbraceLoc = consumeToken(tok::r_brace); return makeParserResult(create()); } @@ -9091,7 +8842,7 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags, } }; - auto checkCodeCompletion = [&](SyntaxKind SK) -> bool { + auto checkCodeCompletion = [&](CodeCompletionCallbacks::PrecedenceGroupCompletionKind SK) -> bool { if (Tok.is(tok::code_complete)) { if (CodeCompletion) CodeCompletion->completeInPrecedenceGroup(SK); @@ -9112,7 +8863,7 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags, // Parse the attributes in the body. while (Tok.isNot(tok::r_brace)) { - if (checkCodeCompletion(SyntaxKind::PrecedenceGroupAttributeList)) { + if (checkCodeCompletion(CodeCompletionCallbacks::PrecedenceGroupCompletionKind::AttributeList)) { hasCodeCompletion = true; continue; } else if (Tok.isNot(tok::identifier)) { @@ -9122,14 +8873,12 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags, auto attrName = Tok.getText(); if (attrName == "associativity") { - SyntaxParsingContext AttrCtxt(SyntaxContext, - SyntaxKind::PrecedenceGroupAssociativity); // "associativity" is considered as a contextual keyword. TokReceiver->registerTokenKindChange(Tok.getLoc(), tok::contextual_keyword); parseAttributePrefix(associativityKeywordLoc); - if (checkCodeCompletion(SyntaxKind::PrecedenceGroupAssociativity)) + if (checkCodeCompletion(CodeCompletionCallbacks::PrecedenceGroupCompletionKind::Associativity)) return abortBody(/*hasCodeCompletion*/true); if (Tok.isNot(tok::identifier)) { @@ -9162,14 +8911,12 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags, } if (attrName == "assignment") { - SyntaxParsingContext AttrCtxt(SyntaxContext, - SyntaxKind::PrecedenceGroupAssignment); parseAttributePrefix(assignmentKeywordLoc); // "assignment" is considered as a contextual keyword. TokReceiver->registerTokenKindChange(assignmentKeywordLoc, tok::contextual_keyword); - if (checkCodeCompletion(SyntaxKind::PrecedenceGroupAssignment)) + if (checkCodeCompletion(CodeCompletionCallbacks::PrecedenceGroupCompletionKind::Assignment)) return abortBody(/*hasCodeCompletion*/true); if (consumeIf(tok::kw_true, assignmentValueLoc)) { @@ -9188,19 +8935,15 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags, bool isLowerThan = false; if (attrName == "higherThan" || (isLowerThan = (attrName == "lowerThan"))) { - SyntaxParsingContext AttrCtxt(SyntaxContext, - SyntaxKind::PrecedenceGroupRelation); // "lowerThan" and "higherThan" are contextual keywords. TokReceiver->registerTokenKindChange(Tok.getLoc(), - tok::contextual_keyword); + tok::contextual_keyword); parseAttributePrefix(isLowerThan ? lowerThanKeywordLoc : higherThanKeywordLoc); auto &relations = (isLowerThan ? lowerThan : higherThan); do { - SyntaxParsingContext NameCtxt(SyntaxContext, - SyntaxKind::PrecedenceGroupNameElement); - if (checkCodeCompletion(SyntaxKind::PrecedenceGroupRelation)) { + if (checkCodeCompletion(CodeCompletionCallbacks::PrecedenceGroupCompletionKind::Relation)) { return abortBody(/*hasCodeCompletion*/true); } @@ -9218,14 +8961,12 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags, if (!consumeIf(tok::comma)) break; } while (true); - SyntaxContext->collectNodesInPlace(SyntaxKind::PrecedenceGroupNameList); continue; } diagnose(Tok, diag::unknown_precedencegroup_attribute, attrName); return abortBody(); } - SyntaxContext->collectNodesInPlace(SyntaxKind::PrecedenceGroupAttributeList); rbraceLoc = consumeToken(tok::r_brace); auto result = create(); diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index f45ebed74b074..9c06ef7bd8f8c 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -21,9 +21,6 @@ #include "swift/AST/TypeRepr.h" #include "swift/Basic/EditorPlaceholder.h" #include "swift/Parse/CodeCompletionCallbacks.h" -#include "swift/Parse/ParsedSyntaxRecorder.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Syntax/SyntaxKind.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" @@ -34,7 +31,6 @@ #include "llvm/Support/raw_ostream.h" using namespace swift; -using namespace swift::syntax; /// parseExpr /// @@ -44,9 +40,6 @@ using namespace swift::syntax; /// \param isExprBasic Whether we're only parsing an expr-basic. ParserResult Parser::parseExprImpl(Diag<> Message, bool isExprBasic) { - // Start a context for creating expression syntax. - SyntaxParsingContext ExprParsingContext(SyntaxContext, SyntaxContextKind::Expr); - // If we are parsing a refutable pattern, check to see if this is the start // of a let/var/is pattern. If so, parse it as an UnresolvedPatternExpr and // let pattern type checking determine its final form. @@ -60,7 +53,6 @@ ParserResult Parser::parseExprImpl(Diag<> Message, return makeParserCodeCompletionResult(); if (pattern.isNull()) return nullptr; - SyntaxContext->setCreateSyntax(SyntaxKind::UnresolvedPatternExpr); return makeParserResult(new (Context) UnresolvedPatternExpr(pattern.get())); } @@ -74,12 +66,10 @@ ParserResult Parser::parseExprImpl(Diag<> Message, ParserResult Parser::parseExprIs() { SourceLoc isLoc; { - SyntaxParsingContext IsExprCtx(SyntaxContext, SyntaxKind::UnresolvedIsExpr); isLoc = consumeToken(tok::kw_is); } ParserResult type = parseType(diag::expected_type_after_is); - SyntaxContext->createNodeInPlace(SyntaxKind::TypeExpr); if (type.hasCodeCompletion()) return makeParserCodeCompletionResult(); @@ -100,7 +90,6 @@ ParserResult Parser::parseExprAs() { SourceLoc exclaimLoc; { - SyntaxParsingContext AsExprCtx(SyntaxContext, SyntaxKind::UnresolvedAsExpr); // Parse the 'as'. asLoc = consumeToken(tok::kw_as); @@ -113,7 +102,6 @@ ParserResult Parser::parseExprAs() { } ParserResult type = parseType(diag::expected_type_after_as); - SyntaxContext->createNodeInPlace(SyntaxKind::TypeExpr); if (type.hasCodeCompletion()) return makeParserCodeCompletionResult(); @@ -189,8 +177,6 @@ ParserResult Parser::parseExprArrow() { ParserResult Parser::parseExprSequence(Diag<> Message, bool isExprBasic, bool isForConditionalDirective) { - SyntaxParsingContext ExprSequenceContext(SyntaxContext, SyntaxContextKind::Expr); - SmallVector SequencedExprs; SourceLoc startLoc = Tok.getLoc(); ParserStatus SequenceStatus; @@ -240,8 +226,6 @@ ParserResult Parser::parseExprSequence(Diag<> Message, goto done; // Parse the operator. - SyntaxParsingContext OperatorContext(SyntaxContext, - SyntaxKind::BinaryOperatorExpr); Expr *Operator = parseExprOperator(); SequencedExprs.push_back(Operator); @@ -251,8 +235,6 @@ ParserResult Parser::parseExprSequence(Diag<> Message, } case tok::question_infix: { - SyntaxParsingContext TernaryCtx(SyntaxContext, - SyntaxKind::UnresolvedTernaryExpr); // Save the '?'. SourceLoc questionLoc = consumeToken(); @@ -295,8 +277,6 @@ ParserResult Parser::parseExprSequence(Diag<> Message, // as a binary operator. if (InVarOrLetPattern) goto done; - SyntaxParsingContext AssignContext(SyntaxContext, - SyntaxKind::AssignmentExpr); SourceLoc equalsLoc = consumeToken(); auto *assign = new (Context) AssignExpr(equalsLoc); SequencedExprs.push_back(assign); @@ -349,7 +329,6 @@ ParserResult Parser::parseExprSequence(Diag<> Message, case tok::arrow: case tok::kw_throws: { - SyntaxParsingContext ArrowContext(SyntaxContext, SyntaxKind::ArrowExpr); ParserResult arrow = parseExprArrow(); if (arrow.isNull() || arrow.hasCodeCompletion()) return arrow; @@ -379,8 +358,6 @@ ParserResult Parser::parseExprSequence(Diag<> Message, if (SequencedExprs.size() == 1) return makeParserResult(SequenceStatus, SequencedExprs[0]); - ExprSequenceContext.createNodeInPlace(SyntaxKind::ExprList); - ExprSequenceContext.setCreateSyntax(SyntaxKind::SequenceExpr); return makeParserResult(SequenceStatus, SequenceExpr::create(Context, SequencedExprs)); } @@ -399,9 +376,6 @@ ParserResult Parser::parseExprSequence(Diag<> Message, /// sequence, but this isn't enforced until sequence-folding. ParserResult Parser::parseExprSequenceElement(Diag<> message, bool isExprBasic) { - SyntaxParsingContext ElementContext(SyntaxContext, - SyntaxContextKind::Expr); - // Check whether the user mistyped "async" for "await", but only in cases // where we are sure that "async" would be ill-formed as an identifier. bool isReplaceableAsync = Tok.isContextualKeyword("async") && @@ -425,7 +399,6 @@ ParserResult Parser::parseExprSequenceElement(Diag<> message, .fixItInsert(anyTry->getSubExpr()->getStartLoc(), "await "); } - ElementContext.setCreateSyntax(SyntaxKind::AwaitExpr); sub = makeParserResult(new (Context) AwaitExpr(awaitLoc, sub.get())); } @@ -439,7 +412,6 @@ ParserResult Parser::parseExprSequenceElement(Diag<> message, ParserResult sub = parseExprSequenceElement(diag::expected_expr_after_await, isExprBasic); if (!sub.hasCodeCompletion() && !sub.isNull()) { - ElementContext.setCreateSyntax(SyntaxKind::MoveExpr); sub = makeParserResult(new (Context) MoveExpr(awaitLoc, sub.get())); } @@ -462,7 +434,6 @@ ParserResult Parser::parseExprSequenceElement(Diag<> message, isType = canParseType(); } if (isType) { - SyntaxParsingContext TyExprCtx(SyntaxContext, SyntaxKind::TypeExpr); ParserResult ty = parseType(); if (ty.isNonNull()) return makeParserResult( @@ -477,7 +448,6 @@ ParserResult Parser::parseExprSequenceElement(Diag<> message, : parseExprUnary(message, isExprBasic); if (hadTry && !sub.hasCodeCompletion() && !sub.isNull()) { - ElementContext.setCreateSyntax(SyntaxKind::TryExpr); switch (trySuffix ? trySuffix->getKind() : tok::NUM_TOKENS) { case tok::exclaim_postfix: sub = makeParserResult( @@ -524,7 +494,6 @@ ParserResult Parser::parseExprSequenceElement(Diag<> message, /// '&' expr-unary(Mode) /// ParserResult Parser::parseExprUnary(Diag<> Message, bool isExprBasic) { - SyntaxParsingContext UnaryContext(SyntaxContext, SyntaxContextKind::Expr); UnresolvedDeclRefExpr *Operator; // First check to see if we have the start of a regex literal `/.../`. @@ -536,7 +505,6 @@ ParserResult Parser::parseExprUnary(Diag<> Message, bool isExprBasic) { return parseExprPostfix(Message, isExprBasic); case tok::amp_prefix: { - SyntaxParsingContext AmpCtx(SyntaxContext, SyntaxKind::InOutExpr); SourceLoc Loc = consumeToken(tok::amp_prefix); ParserResult SubExpr = parseExprUnary(Message, isExprBasic); @@ -585,9 +553,6 @@ ParserResult Parser::parseExprUnary(Diag<> Message, bool isExprBasic) { if (SubExpr.isNull()) return Status; - // We are sure we can create a prefix operator expr now. - UnaryContext.setCreateSyntax(SyntaxKind::PrefixOperatorExpr); - // Check if we have a unary '-' with number literal sub-expression, for // example, "-42" or "-1.25". if (auto *LE = dyn_cast(SubExpr.get())) { @@ -620,7 +585,6 @@ ParserResult Parser::parseExprUnary(Diag<> Message, bool isExprBasic) { /// ! /// [ expression ] ParserResult Parser::parseExprKeyPath() { - SyntaxParsingContext KeyPathCtx(SyntaxContext, SyntaxKind::OldKeyPathExpr); // Consume '\'. SourceLoc backslashLoc = consumeToken(tok::backslash); llvm::SaveAndRestore S(InSwiftKeyPath, true); @@ -640,8 +604,6 @@ ParserResult Parser::parseExprKeyPath() { bool hasLeadingDot = startsWithSymbol(Tok, '.'); if (hasLeadingDot) { - SyntaxParsingContext ExprContext(SyntaxContext, SyntaxContextKind::Expr); - auto dotLoc = Tok.getLoc(); // For uniformity, \.foo is parsed as if it were MAGIC.foo, so we need to // make sure the . is there, but parsing the ? in \.? as .? doesn't make @@ -650,8 +612,6 @@ ParserResult Parser::parseExprKeyPath() { // the token is a operator starts with '.', or the following token is '['. if ((Tok.isAnyOperator() && Tok.getLength() != 1) || peekToken().is(tok::l_square)) { - SyntaxParsingContext KeyPathBaseContext(SyntaxContext, - SyntaxKind::KeyPathBaseExpr); consumeStartingCharacterOfCurrentToken(tok::period); } @@ -696,7 +656,6 @@ ParserResult Parser::parseExprKeyPath() { /// '#keyPath' '(' unqualified-name ('.' unqualified-name) * ')' /// ParserResult Parser::parseExprKeyPathObjC() { - SyntaxParsingContext ObjcKPCtx(SyntaxContext, SyntaxKind::ObjcKeyPathExpr); // Consume '#keyPath'. SourceLoc keywordLoc = consumeToken(tok::pound_keyPath); @@ -729,7 +688,6 @@ ParserResult Parser::parseExprKeyPathObjC() { SourceLoc LastDotLoc; DeclNameOptions flags = DeclNameFlag::AllowCompoundNames; while (true) { - SyntaxParsingContext NamePieceCtx(SyntaxContext, SyntaxKind::ObjcNamePiece); // Handle code completion. if (Tok.is(tok::code_complete)) return handleCodeCompletion(LastDotLoc); @@ -762,9 +720,6 @@ ParserResult Parser::parseExprKeyPathObjC() { break; } - // Collect all name pieces to an objc name. - SyntaxContext->collectNodesInPlace(SyntaxKind::ObjcName); - // Parse the closing ')'. SourceLoc rParenLoc; if (status.isErrorOrHasCompletion()) { @@ -798,7 +753,6 @@ ParserResult Parser::parseExprKeyPathObjC() { /// '#selector' '(' 'setter' ':' expr ')' /// ParserResult Parser::parseExprSelector() { - SyntaxParsingContext ExprCtxt(SyntaxContext, SyntaxKind::ObjcSelectorExpr); // Consume '#selector'. SourceLoc keywordLoc = consumeToken(tok::pound_selector); @@ -972,10 +926,8 @@ void Parser::tryLexRegexLiteral(bool forUnappliedOperator) { /// expr-super-subscript: /// 'super' '[' expr ']' ParserResult Parser::parseExprSuper() { - SyntaxParsingContext SuperCtxt(SyntaxContext, SyntaxContextKind::Expr); // Parse the 'super' reference. SourceLoc superLoc = consumeToken(tok::kw_super); - SyntaxContext->createNodeInPlace(SyntaxKind::SuperRefExpr); // 'super.' must be followed by a member ref, explicit initializer ref, or // subscript call. @@ -1155,18 +1107,6 @@ getMagicIdentifierLiteralKind(tok Kind, const LangOptions &Opts) { } } -/// Map magic literal kinds such as #file to their SyntaxKind. -static SyntaxKind -getMagicIdentifierSyntaxKind(MagicIdentifierLiteralExpr::Kind LiteralKind) { - switch (LiteralKind) { -#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \ - case MagicIdentifierLiteralExpr::NAME: \ - return SyntaxKind::SYNTAX_KIND; -#include "swift/AST/MagicIdentifierKinds.def" - } - llvm_unreachable("not a magic literal kind"); -} - ParserResult Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, bool periodHasKeyPathBehavior, @@ -1214,7 +1154,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, if (Tok.is(tok::integer_literal)) { DeclNameRef name(Context.getIdentifier(Tok.getText())); SourceLoc nameLoc = consumeToken(tok::integer_literal); - SyntaxContext->createNodeInPlace(SyntaxKind::MemberAccessExpr); // Don't allow '.' following a numeric literal // expression (unless in #if env, for 1.2.3.4 version numbers) @@ -1237,7 +1176,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, Result = makeParserResult( Result, new (Context) DotSelfExpr(Result.get(), TokLoc, consumeToken())); - SyntaxContext->createNodeInPlace(SyntaxKind::MemberAccessExpr); continue; } @@ -1277,7 +1215,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, ErrorRange.widen(TokLoc); return makeParserErrorResult(new (Context) ErrorExpr(ErrorRange, Type(), Result.get())); } - SyntaxContext->createNodeInPlace(SyntaxKind::MemberAccessExpr); Result = makeParserResult(Result, new (Context) UnresolvedDotExpr( Result.get(), TokLoc, Name, NameLoc, /*Implicit=*/false)); @@ -1290,7 +1227,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, if (argStat.isErrorOrHasCompletion()) diagnose(LAngleLoc, diag::while_parsing_as_left_angle_bracket); - SyntaxContext->createNodeInPlace(SyntaxKind::SpecializeExpr); // The result can be empty in error cases. if (!args.empty()) { Result = makeParserResult( @@ -1305,7 +1241,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, // If there is an expr-call-suffix, parse it and form a call. if (Tok.isFollowingLParen()) { Result = parseExprCallSuffix(Result, isExprBasic); - SyntaxContext->createNodeInPlace(SyntaxKind::FunctionCallExpr); continue; } @@ -1316,7 +1251,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, auto *subscript = SubscriptExpr::create(Context, Result.get(), args.get()); Result = makeParserResult(ParserStatus(args) | Result, subscript); - SyntaxContext->createNodeInPlace(SyntaxKind::SubscriptExpr); continue; } @@ -1330,13 +1264,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, isa(callee)) break; - if (SyntaxContext->isEnabled()) { - // Add dummy blank argument list to the call expression syntax. - SyntaxContext->addSyntax( - ParsedSyntaxRecorder::makeBlankTupleExprElementList( - leadingTriviaLoc(), *SyntaxContext)); - } - SmallVector trailingClosures; auto trailingResult = parseTrailingClosures(isExprBasic, callee->getSourceRange(), @@ -1351,7 +1278,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, Result = makeParserResult( ParserStatus(Result) | trailingResult, CallExpr::create(Context, Result.get(), argList, /*implicit*/ false)); - SyntaxContext->createNodeInPlace(SyntaxKind::FunctionCallExpr); // We only allow a single trailing closure on a call. This could be // generalized in the future, but needs further design. @@ -1364,7 +1290,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, if (consumeIf(tok::question_postfix)) { Result = makeParserResult(Result, new (Context) BindOptionalExpr( Result.get(), TokLoc, /*depth*/ 0)); - SyntaxContext->createNodeInPlace(SyntaxKind::OptionalChainingExpr); hasBindOptional = true; continue; } @@ -1373,7 +1298,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, if (consumeIf(tok::exclaim_postfix)) { Result = makeParserResult( Result, new (Context) ForceValueExpr(Result.get(), TokLoc)); - SyntaxContext->createNodeInPlace(SyntaxKind::ForcedValueExpr); continue; } @@ -1388,7 +1312,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, Result = makeParserResult( Result, PostfixUnaryExpr::create(Context, oper, Result.get())); - SyntaxContext->createNodeInPlace(SyntaxKind::PostfixUnaryExpr); continue; } @@ -1437,8 +1360,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, llvm::SmallPtrSet exprsWithBindOptional; auto ICD = parseIfConfig([&](SmallVectorImpl &elements, bool isActive) { - SyntaxParsingContext postfixCtx(SyntaxContext, - SyntaxContextKind::Expr); // Although we know the '#if' body starts with period, // '#elseif'/'#else' bodies might start with invalid tokens. if (isAtStartOfPostfixExprSuffix() || Tok.is(tok::pound_if)) { @@ -1461,8 +1382,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, if (ICD.isNull()) break; - SyntaxContext->createNodeInPlace(SyntaxKind::PostfixIfConfigExpr); - auto activeElements = ICD.get()->getActiveClauseElements(); if (activeElements.empty()) // There's no active clause, or it was empty. Keep the current result. @@ -1556,7 +1475,6 @@ Parser::parseExprPostfixSuffix(ParserResult Result, bool isExprBasic, /// expr-trailing-closure /// ParserResult Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) { - SyntaxParsingContext ExprContext(SyntaxContext, SyntaxContextKind::Expr); auto Result = parseExprPrimary(ID, isExprBasic); // If we couldn't parse any expr, don't attempt to parse suffixes. if (Result.isNull()) @@ -1611,12 +1529,10 @@ ParserResult Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) { /// expr-selector /// ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { - SyntaxParsingContext ExprContext(SyntaxContext, SyntaxContextKind::Expr); switch (Tok.getKind()) { case tok::integer_literal: { StringRef Text = copyAndStripUnderscores(Tok.getText()); SourceLoc Loc = consumeToken(tok::integer_literal); - ExprContext.setCreateSyntax(SyntaxKind::IntegerLiteralExpr); return makeParserResult(new (Context) IntegerLiteralExpr(Text, Loc, /*Implicit=*/false)); @@ -1624,7 +1540,6 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { case tok::floating_literal: { StringRef Text = copyAndStripUnderscores(Tok.getText()); SourceLoc Loc = consumeToken(tok::floating_literal); - ExprContext.setCreateSyntax(SyntaxKind::FloatLiteralExpr); return makeParserResult(new (Context) FloatLiteralExpr(Text, Loc, /*Implicit=*/false)); } @@ -1647,13 +1562,11 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { return parseExprRegexLiteral(); case tok::kw_nil: - ExprContext.setCreateSyntax(SyntaxKind::NilLiteralExpr); return makeParserResult(new (Context) NilLiteralExpr(consumeToken(tok::kw_nil))); case tok::kw_true: case tok::kw_false: { - ExprContext.setCreateSyntax(SyntaxKind::BooleanLiteralExpr); bool isTrue = Tok.is(tok::kw_true); return makeParserResult(new (Context) BooleanLiteralExpr(isTrue, consumeToken())); @@ -1679,11 +1592,7 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { #include "swift/AST/MagicIdentifierKinds.def" { auto Kind = getMagicIdentifierLiteralKind(Tok.getKind(), Context.LangOpts); - SyntaxKind SKind = getMagicIdentifierSyntaxKind(Kind); - - ExprContext.setCreateSyntax(SKind); SourceLoc Loc = consumeToken(); - return makeParserResult(new (Context) MagicIdentifierLiteralExpr( Kind, Loc, /*implicit=*/false)); } @@ -1703,26 +1612,12 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { // name, not a binding, because it is the start of an enum pattern or // call pattern. peekToken().isNot(tok::period, tok::period_prefix, tok::l_paren)) { - DeferringContextRAII Deferring(*SyntaxContext); Identifier name; SourceLoc loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/false); auto introducer = (InVarOrLetPattern != IVOLP_InVar ? VarDecl::Introducer::Let : VarDecl::Introducer::Var); auto pattern = createBindingFromPattern(loc, name, introducer); - if (SyntaxContext->isEnabled()) { - ParsedPatternSyntax PatternNode = - ParsedSyntaxRecorder::makeIdentifierPattern( - /*UnexpectedNodes=*/None, - /*Identifier=*/SyntaxContext->popToken(), - /*UnexpectedNodes=*/None, *SyntaxContext); - ParsedExprSyntax ExprNode = - ParsedSyntaxRecorder::makeUnresolvedPatternExpr( - /*UnexpectedNodes=*/None, - /*Pattern=*/std::move(PatternNode), - /*UnexpectedNodes=*/None, *SyntaxContext); - SyntaxContext->addSyntax(std::move(ExprNode)); - } return makeParserResult(new (Context) UnresolvedPatternExpr(pattern)); } @@ -1740,7 +1635,6 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { return parseExprIdentifier(); case tok::kw_Any: { // Any - ExprContext.setCreateSyntax(SyntaxKind::TypeExpr); auto TyR = parseAnyType(); return makeParserResult(new (Context) TypeExpr(TyR.get())); } @@ -1749,7 +1643,6 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { return makeParserResult(parseExprAnonClosureArg()); case tok::kw__: // _ - ExprContext.setCreateSyntax(SyntaxKind::DiscardAssignmentExpr); return makeParserResult( new (Context) DiscardAssignmentExpr(consumeToken(), /*Implicit=*/false)); @@ -1805,7 +1698,6 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { DeclNameFlag::AllowKeywords | DeclNameFlag::AllowCompoundNames); if (!Name) return makeParserErrorResult(new (Context) ErrorExpr(DotLoc)); - SyntaxContext->createNodeInPlace(SyntaxKind::MemberAccessExpr); return makeParserResult(new (Context) UnresolvedMemberExpr( DotLoc, NameLoc, Name, /*implicit=*/false)); @@ -1820,7 +1712,6 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { // only one element without label. However, libSyntax tree doesn't have this // differentiation. A tuple expression node in libSyntax can have a single // element without label. - ExprContext.setCreateSyntax(SyntaxKind::TupleExpr); return parseTupleOrParenExpr(tok::l_paren, tok::r_paren); case tok::l_square: @@ -1858,7 +1749,7 @@ ParserResult Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) { #define POUND_OBJECT_LITERAL(Name, Desc, Proto) \ case tok::pound_##Name: \ return parseExprObjectLiteral(ObjectLiteralExpr::Name, isExprBasic); -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" case tok::code_complete: { auto Result = @@ -1991,35 +1882,27 @@ parseStringSegments(SmallVectorImpl &Segments, consumeExtraToken(Token(tok::string_literal, CharSourceRange(SourceMgr, TokenLoc, TokEnd).str(), CommentLength)); - SyntaxParsingContext StrSegContext(SyntaxContext, - SyntaxKind::StringSegment); // Make an unknown token to encapsulate the entire string segment and add // such token to the context. Token content(tok::string_segment, CharSourceRange(Segment.Loc, Segment.Length).str()); - SyntaxContext->addToken(content, StringRef(), StringRef()); break; } case Lexer::StringSegment::Expr: { - SyntaxParsingContext ExprContext(SyntaxContext, - SyntaxKind::ExpressionSegment); - unsigned DelimiterLen = EntireTok.getCustomDelimiterLen(); bool HasCustomDelimiter = DelimiterLen > 0; // Backslash is part of an expression segment. SourceLoc BackSlashLoc = Segment.Loc.getAdvancedLoc(-1 - DelimiterLen); Token BackSlash(tok::backslash, CharSourceRange(BackSlashLoc, 1).str()); - ExprContext.addToken(BackSlash, StringRef(), StringRef()); // Custom delimiter may be a part of an expression segment. if (HasCustomDelimiter) { SourceLoc DelimiterLoc = Segment.Loc.getAdvancedLoc(-DelimiterLen); Token Delimiter(tok::raw_string_delimiter, CharSourceRange(DelimiterLoc, DelimiterLen).str()); - ExprContext.addToken(Delimiter, StringRef(), StringRef()); } // Create a temporary lexer that lexes from the body of the string. @@ -2055,8 +1938,6 @@ parseStringSegments(SmallVectorImpl &Segments, // over, eat the remaining tokens into a token list if (Segment.getEndLoc() != L->getLocForEndOfToken(SourceMgr, Tok.getLoc())) { - SyntaxParsingContext RemainingTokens(SyntaxContext, - SyntaxKind::NonEmptyTokenList); do { consumeToken(); } while (Segment.getEndLoc() != @@ -2076,9 +1957,6 @@ parseStringSegments(SmallVectorImpl &Segments, diagnose(Tok, diag::string_interpolation_extra); } else if (Tok.getText() == ")") { Tok.setKind(tok::string_interpolation_anchor); - // We don't allow trailing trivia for this anchor, because the - // trivia is a part of the next string segment. - TrailingTrivia = StringRef(); consumeToken(); } break; @@ -2092,9 +1970,6 @@ parseStringSegments(SmallVectorImpl &Segments, /// expr-literal: /// string_literal ParserResult Parser::parseExprStringLiteral() { - SyntaxParsingContext LocalContext(SyntaxContext, - SyntaxKind::StringLiteralExpr); - SmallVector Segments; L->getStringLiteralSegments(Tok, Segments); @@ -2105,7 +1980,6 @@ ParserResult Parser::parseExprStringLiteral() { StringRef OpenDelimiterStr, OpenQuoteStr, CloseQuoteStr, CloseDelimiterStr; unsigned DelimiterLength = Tok.getCustomDelimiterLen(); - bool HasCustomDelimiter = DelimiterLength > 0; unsigned QuoteLength; tok QuoteKind; std::tie(QuoteLength, QuoteKind) = @@ -2122,51 +1996,12 @@ ParserResult Parser::parseExprStringLiteral() { // Make unknown tokens to represent the open and close quote. Token OpenQuote(QuoteKind, OpenQuoteStr); Token CloseQuote(QuoteKind, CloseQuoteStr); - StringRef EntireTrailingTrivia = TrailingTrivia; - - if (HasCustomDelimiter) { - Token OpenDelimiter(tok::raw_string_delimiter, OpenDelimiterStr); - // When a custom delimiter is present, it owns the leading trivia. - SyntaxContext->addToken(OpenDelimiter, LeadingTrivia, StringRef()); - - SyntaxContext->addToken(OpenQuote, StringRef(), StringRef()); - } else { - // Without custom delimiter the quote owns trailing trivia. - SyntaxContext->addToken(OpenQuote, LeadingTrivia, StringRef()); - } // The simple case: just a single literal segment. if (Segments.size() == 1 && Segments.front().Kind == Lexer::StringSegment::Literal) { - { - consumeExtraToken(Tok); - consumeTokenWithoutFeedingReceiver(); - - SyntaxParsingContext SegmentsCtx(SyntaxContext, - SyntaxKind::StringLiteralSegments); - - SyntaxParsingContext StrSegContext(SyntaxContext, - SyntaxKind::StringSegment); - - // Make an unknown token to encapsulate the entire string segment and add - // such token to the context. - auto Segment = Segments.front(); - Token content(tok::string_segment, - CharSourceRange(Segment.Loc, Segment.Length).str()); - SyntaxContext->addToken(content, StringRef(), StringRef()); - } - - if (HasCustomDelimiter) { - SyntaxContext->addToken(CloseQuote, StringRef(), StringRef()); - - Token CloseDelimiter(tok::raw_string_delimiter, CloseDelimiterStr); - // When a custom delimiter is present it owns the trailing trivia. - SyntaxContext->addToken(CloseDelimiter, StringRef(), - EntireTrailingTrivia); - } else { - // Without custom delimiter the quote owns trailing trivia. - SyntaxContext->addToken(CloseQuote, StringRef(), EntireTrailingTrivia); - } + consumeExtraToken(Tok); + consumeTokenWithoutFeedingReceiver(); return makeParserResult( createStringLiteralExprFromSegment(Context, L, Segments.front(), Loc)); @@ -2178,8 +2013,6 @@ ParserResult Parser::parseExprStringLiteral() { // We are going to mess with Tok to do reparsing for interpolated literals, // don't lose our 'next' token. llvm::SaveAndRestore SavedTok(Tok); - llvm::SaveAndRestore SavedLeadingTrivia(LeadingTrivia); - llvm::SaveAndRestore SavedTrailingTrivia(TrailingTrivia); // For errors, we need the real PreviousLoc, i.e. the start of the // whole InterpolatedStringLiteral. llvm::SaveAndRestore SavedPreviousLoc(PreviousLoc); @@ -2213,8 +2046,6 @@ ParserResult Parser::parseExprStringLiteral() { Stmts.push_back(InterpolationVar); // Collect all string segments. - SyntaxParsingContext SegmentsCtx(SyntaxContext, - SyntaxKind::StringLiteralSegments); Status = parseStringSegments(Segments, EntireTok, InterpolationVar, Stmts, LiteralCapacity, InterpolationCount); @@ -2223,17 +2054,6 @@ ParserResult Parser::parseExprStringLiteral() { AppendingExpr = new (Context) TapExpr(nullptr, Body); } - if (HasCustomDelimiter) { - SyntaxContext->addToken(CloseQuote, StringRef(), StringRef()); - - Token CloseDelimiter(tok::raw_string_delimiter, CloseDelimiterStr); - // When a custom delimiter is present it owns the trailing trivia. - SyntaxContext->addToken(CloseDelimiter, StringRef(), EntireTrailingTrivia); - } else { - // Without custom delimiter the quote owns trailing trivia. - SyntaxContext->addToken(CloseQuote, StringRef(), EntireTrailingTrivia); - } - if (AppendingExpr->getBody()->getNumElements() == 1) { Status.setIsParseError(); return makeParserResult(Status, new (Context) ErrorExpr(Loc)); @@ -2298,13 +2118,10 @@ static bool tryParseArgLabelList(Parser &P, Parser::DeclNameOptions flags, return false; // Try to parse a compound name. - SyntaxParsingContext ArgsCtxt(P.SyntaxContext, SyntaxKind::DeclNameArguments); Parser::CancellableBacktrackingScope backtrack(P); lparenLoc = P.consumeToken(tok::l_paren); while (P.Tok.isNot(tok::r_paren)) { - SyntaxParsingContext ArgCtxt(P.SyntaxContext, SyntaxKind::DeclNameArgument); - // If we see a ':', the user forgot the '_'; if (P.Tok.is(tok::colon)) { P.diagnose(P.Tok, diag::empty_arg_label_underscore) @@ -2323,23 +2140,12 @@ static bool tryParseArgLabelList(Parser &P, Parser::DeclNameOptions flags, } // This is not a compound name. - // FIXME: Could recover better if we "know" it's a compound name. - ArgCtxt.setBackTracking(); - ArgsCtxt.setBackTracking(); - return false; } // We have a compound name. Cancel backtracking and build that name. backtrack.cancelBacktrack(); - if (argumentLabels.empty() && P.SyntaxContext->isEnabled()) - P.SyntaxContext->addSyntax( - ParsedSyntaxRecorder::makeBlankDeclNameArgumentList( - P.leadingTriviaLoc(), *P.SyntaxContext)); - else - ArgsCtxt.collectNodesInPlace(SyntaxKind::DeclNameArgumentList); - rparenLoc = P.consumeToken(tok::r_paren); assert(argumentLabels.size() == argumentLabelLocs.size()); @@ -2411,8 +2217,6 @@ DeclNameRef Parser::parseDeclNameRef(DeclNameLoc &loc, ParserResult Parser::parseExprIdentifier() { ParserStatus status; assert(Tok.isAny(tok::identifier, tok::kw_self, tok::kw_Self)); - SyntaxParsingContext IDSyntaxContext(SyntaxContext, - SyntaxKind::IdentifierExpr); Token IdentTok = Tok; // Parse the unqualified-decl-name. @@ -2432,8 +2236,6 @@ ParserResult Parser::parseExprIdentifier() { /// period_following comma semicolon /// if (canParseAsGenericArgumentList()) { - SyntaxContext->createNodeInPlace(SyntaxKind::IdentifierExpr); - SyntaxContext->setCreateSyntax(SyntaxKind::SpecializeExpr); auto argStatus = parseGenericArguments(args, LAngleLoc, RAngleLoc); status |= argStatus; if (argStatus.isErrorOrHasCompletion()) @@ -2444,7 +2246,6 @@ ParserResult Parser::parseExprIdentifier() { } if (name.getBaseName().isEditorPlaceholder()) { - IDSyntaxContext.setCreateSyntax(SyntaxKind::EditorPlaceholderExpr); return makeParserResult( status, parseExprEditorPlaceholder(IdentTok, name.getBaseIdentifier())); } @@ -2493,8 +2294,6 @@ Expr *Parser::parseExprEditorPlaceholder(Token PlaceholderTok, // Don't feed to syntax token recorder. ConsumeTokenReceiver DisabledRec; llvm::SaveAndRestore R(TokReceiver, &DisabledRec); - SyntaxParsingContext SContext(SyntaxContext); - SContext.disable(); Tok.setKind(tok::unknown); // we might be at tok::eof now. consumeTokenWithoutFeedingReceiver(); @@ -2653,27 +2452,18 @@ ParserStatus Parser::parseClosureSignatureIfPresent( return makeParserSuccess(); } ParserStatus status; - SyntaxParsingContext ClosureSigCtx(SyntaxContext, SyntaxKind::ClosureSignature); - (void)parseDeclAttributeList(attributes); if (Tok.is(tok::l_square) && peekToken().is(tok::r_square)) { - - SyntaxParsingContext CaptureCtx(SyntaxContext, - SyntaxKind::ClosureCaptureSignature); SourceLoc lBracketLoc = consumeToken(tok::l_square); SourceLoc rBracketLoc = consumeToken(tok::r_square); bracketRange = SourceRange(lBracketLoc, rBracketLoc); } else if (Tok.is(tok::l_square) && !peekToken().is(tok::r_square)) { - SyntaxParsingContext CaptureCtx(SyntaxContext, - SyntaxKind::ClosureCaptureSignature); SourceLoc lBracketLoc = consumeToken(tok::l_square); // At this point, we know we have a closure signature. Parse the capture list // and parameters. bool HasNext; do { - SyntaxParsingContext CapturedItemCtx(SyntaxContext, - SyntaxKind::ClosureCaptureItem); SWIFT_DEFER { HasNext = consumeIf(tok::comma); }; // Check for the strength specifier: "weak", "unowned", or // "unowned(safe/unsafe)". @@ -2715,9 +2505,6 @@ ParserStatus Parser::parseClosureSignatureIfPresent( continue; } - // Squash all tokens, if any, as the specifier of the captured item. - CapturedItemCtx.collectNodesInPlace(SyntaxKind::TokenList); - // The thing being capture specified is an identifier, or as an identifier // followed by an expression. Expr *initializer; @@ -2811,7 +2598,6 @@ ParserStatus Parser::parseClosureSignatureIfPresent( captureList.push_back(CLE); } while (HasNext); - SyntaxContext->collectNodesInPlace(SyntaxKind::ClosureCaptureItemList); // The capture list needs to be closed off with a ']'. SourceLoc rBracketLoc = Tok.getLoc(); if (!consumeIf(tok::r_square)) { @@ -2833,13 +2619,10 @@ ParserStatus Parser::parseClosureSignatureIfPresent( else status.setIsParseError(); } else { - SyntaxParsingContext ClParamListCtx(SyntaxContext, - SyntaxKind::ClosureParamList); // Parse identifier (',' identifier)* SmallVector elements; bool HasNext; do { - SyntaxParsingContext ClParamCtx(SyntaxContext, SyntaxKind::ClosureParam); if (Tok.isNot(tok::identifier, tok::kw__, tok::code_complete)) { diagnose(Tok, diag::expected_closure_parameter_name); status.setIsParseError(); @@ -2875,7 +2658,6 @@ ParserStatus Parser::parseClosureSignatureIfPresent( // Parse the optional explicit return type. if (Tok.is(tok::arrow)) { - SyntaxParsingContext ReturnCtx(SyntaxContext, SyntaxKind::ReturnClause); // Consume the '->'. arrowLoc = consumeToken(); @@ -2977,7 +2759,6 @@ ParserStatus Parser::parseClosureSignatureIfPresent( ParserResult Parser::parseExprClosure() { assert(Tok.is(tok::l_brace) && "Not at a left brace?"); ParserStatus Status; - SyntaxParsingContext ClosureContext(SyntaxContext, SyntaxKind::ClosureExpr); // We may be parsing this closure expr in a matching pattern context. If so, // reset our state to not be in a pattern for any recursive pattern parses. llvm::SaveAndRestore @@ -3120,7 +2901,6 @@ ParserResult Parser::parseExprClosure() { /// expr-anon-closure-argument: /// dollarident Expr *Parser::parseExprAnonClosureArg() { - SyntaxParsingContext ExprContext(SyntaxContext, SyntaxKind::IdentifierExpr); StringRef Name = Tok.getText(); SourceLoc Loc = consumeToken(tok::dollarident); assert(Name[0] == '$' && "Not a dollarident"); @@ -3196,7 +2976,7 @@ ParserResult Parser::parseTupleOrParenExpr(tok leftTok, tok rightTok) { SourceLoc leftLoc, rightLoc; auto status = parseExprList(leftTok, rightTok, /*isArgumentList*/ false, leftLoc, elts, - rightLoc, SyntaxKind::TupleExprElementList); + rightLoc); // A tuple with a single, unlabeled element is just parentheses. if (Context.LangOpts.hasFeature(Feature::VariadicGenerics)) { @@ -3242,7 +3022,7 @@ Parser::parseArgumentList(tok leftTok, tok rightTok, bool isExprBasic, // FIXME: Introduce new SyntaxKind for ArgumentList (rdar://81786229) auto status = parseExprList(leftTok, rightTok, /*isArgumentList*/ true, leftLoc, elts, - rightLoc, SyntaxKind::TupleExprElementList); + rightLoc); SmallVector args; for (auto &elt : elts) @@ -3276,14 +3056,14 @@ Parser::parseArgumentList(tok leftTok, tok rightTok, bool isExprBasic, ParserStatus Parser::parseExprList(tok leftTok, tok rightTok, bool isArgumentList, SourceLoc &leftLoc, SmallVectorImpl &elts, - SourceLoc &rightLoc, SyntaxKind Kind) { + SourceLoc &rightLoc) { StructureMarkerRAII ParsingExprList(*this, Tok); leftLoc = consumeToken(leftTok); return parseList(rightTok, leftLoc, rightLoc, /*AllowSepAfterLast=*/false, rightTok == tok::r_paren ? diag::expected_rparen_expr_list : diag::expected_rsquare_expr_list, - Kind, [&] () -> ParserStatus { + [&] () -> ParserStatus { Identifier FieldName; SourceLoc FieldNameLoc; parseOptionalArgumentLabel(FieldName, FieldNameLoc); @@ -3305,8 +3085,6 @@ ParserStatus Parser::parseExprList(tok leftTok, tok rightTok, ParserStatus Status; Expr *SubExpr = nullptr; if (isUnappliedOperator()) { - SyntaxParsingContext operatorContext(SyntaxContext, - SyntaxKind::IdentifierExpr); DeclNameLoc Loc; auto OperName = parseDeclNameRef(Loc, diag::expected_operator_ref, DeclNameFlag::AllowOperators); @@ -3436,8 +3214,6 @@ Parser::parseTrailingClosures(bool isExprBasic, SourceRange calleeRange, continue; } - SyntaxParsingContext ClosureCtx(SyntaxContext, - SyntaxKind::MultipleTrailingClosureElement); Identifier label; auto labelLoc = consumeArgumentLabel(label, /*diagnoseDollarPrefix=*/false); consumeToken(tok::colon); @@ -3447,8 +3223,6 @@ Parser::parseTrailingClosures(bool isExprBasic, SourceRange calleeRange, } else if (Tok.is(tok::identifier)) { // Parse editor placeholder as a closure literal. assert(Identifier::isEditorPlaceholder(Tok.getText())); - SyntaxParsingContext IdCtx(SyntaxContext, - SyntaxKind::EditorPlaceholderExpr); Identifier name = Context.getIdentifier(Tok.getText()); closure = makeParserResult(parseExprEditorPlaceholder(Tok, name)); consumeToken(tok::identifier); @@ -3468,9 +3242,6 @@ Parser::parseTrailingClosures(bool isExprBasic, SourceRange calleeRange, // Don't diagnose whitespace gaps before labelled closures. } - SyntaxContext->collectNodesInPlace( - SyntaxKind::MultipleTrailingClosureElementList); - return result; } @@ -3481,8 +3252,6 @@ Parser::parseTrailingClosures(bool isExprBasic, SourceRange calleeRange, ParserResult Parser::parseExprObjectLiteral(ObjectLiteralExpr::LiteralKind LitKind, bool isExprBasic) { - SyntaxParsingContext ObjectLiteralContext(SyntaxContext, - SyntaxKind::ObjectLiteralExpr); SourceLoc PoundLoc = consumeToken(); // Parse a tuple of args if (!Tok.is(tok::l_paren)) { @@ -3634,8 +3403,6 @@ Parser::parseExprCallSuffix(ParserResult fn, bool isExprBasic) { } ParserResult Parser::parseExprMacroExpansion(bool isExprBasic) { - SyntaxParsingContext ExprContext( - SyntaxContext, SyntaxKind::MacroExpansionExpr); SourceLoc poundLoc = consumeToken(tok::pound); DeclNameLoc macroNameLoc; DeclNameRef macroNameRef = parseDeclNameRef( @@ -3683,8 +3450,6 @@ ParserResult Parser::parseExprMacroExpansion(bool isExprBasic) { /// '[' expr ':' expr (',' expr ':' expr)* ','? ']' /// '[' ':' ']' ParserResult Parser::parseExprCollection() { - SyntaxParsingContext ArrayOrDictContext(SyntaxContext, - SyntaxContextKind::Expr); SourceLoc LSquareLoc = consumeToken(tok::l_square); SourceLoc RSquareLoc; @@ -3694,11 +3459,7 @@ ParserResult Parser::parseExprCollection() { // [] is always an array. if (Tok.is(tok::r_square)) { - if (SyntaxContext->isEnabled()) - SyntaxContext->addSyntax(ParsedSyntaxRecorder::makeBlankArrayElementList( - leadingTriviaLoc(), *SyntaxContext)); RSquareLoc = consumeToken(tok::r_square); - ArrayOrDictContext.setCreateSyntax(SyntaxKind::ArrayExpr); return makeParserResult( ArrayExpr::create(Context, LSquareLoc, {}, {}, RSquareLoc)); } @@ -3707,7 +3468,6 @@ ParserResult Parser::parseExprCollection() { if (Tok.is(tok::colon) && peekToken().is(tok::r_square)) { consumeToken(tok::colon); RSquareLoc = consumeToken(tok::r_square); - ArrayOrDictContext.setCreateSyntax(SyntaxKind::DictionaryExpr); return makeParserResult( DictionaryExpr::create(Context, LSquareLoc, {}, {}, RSquareLoc)); } @@ -3717,7 +3477,6 @@ ParserResult Parser::parseExprCollection() { !peekToken().isEscapedIdentifier() && LSquareLoc.getAdvancedLoc(1) == Tok.getLoc() && Tok.getLoc().getAdvancedLoc(1) == peekToken().getLoc()) { - ArrayOrDictContext.setCoerceKind(SyntaxContextKind::Expr); return parseExprPoundUnknown(LSquareLoc); } @@ -3727,15 +3486,9 @@ ParserResult Parser::parseExprCollection() { SmallVector CommaLocs; { - SyntaxParsingContext ListCtx(SyntaxContext, SyntaxContextKind::Expr); - while (true) { - SyntaxParsingContext ElementCtx(SyntaxContext); - auto Element = parseExprCollectionElement(isDictionary); Status |= Element; - ElementCtx.setCreateSyntax(*isDictionary ? SyntaxKind::DictionaryElement - : SyntaxKind::ArrayElement); if (Element.isNonNull()) ElementExprs.push_back(Element.get()); @@ -3775,12 +3528,7 @@ ParserResult Parser::parseExprCollection() { .fixItInsertAfter(PreviousLoc, ","); Status.setIsParseError(); } - - ListCtx.setCreateSyntax(*isDictionary ? SyntaxKind::DictionaryElementList - : SyntaxKind::ArrayElementList); } - ArrayOrDictContext.setCreateSyntax(*isDictionary ? SyntaxKind::DictionaryExpr - : SyntaxKind::ArrayExpr); if (Status.isErrorOrHasCompletion()) { // If we've already got errors, don't emit missing RightK diagnostics. @@ -3921,8 +3669,6 @@ ParserResult Parser::parseAvailabilitySpec() { /// "_PackageDescription" version-tuple ParserResult Parser::parsePlatformAgnosticVersionConstraintSpec() { - SyntaxParsingContext VersionRestrictionContext( - SyntaxContext, SyntaxKind::AvailabilityVersionRestriction); SourceLoc PlatformAgnosticNameLoc; llvm::VersionTuple Version; Optional Kind; @@ -3955,9 +3701,6 @@ Parser::parsePlatformAgnosticVersionConstraintSpec() { /// identifier version-comparison version-tuple ParserResult Parser::parsePlatformVersionConstraintSpec() { - SyntaxParsingContext VersionRestrictionContext( - SyntaxContext, SyntaxKind::AvailabilityVersionRestriction); - Identifier PlatformIdentifier; SourceLoc PlatformLoc; if (Tok.is(tok::code_complete)) { diff --git a/lib/Parse/ParseGeneric.cpp b/lib/Parse/ParseGeneric.cpp index f1fcf85037091..76060d280eb5b 100644 --- a/lib/Parse/ParseGeneric.cpp +++ b/lib/Parse/ParseGeneric.cpp @@ -19,12 +19,9 @@ #include "swift/AST/GenericParamList.h" #include "swift/AST/TypeRepr.h" #include "swift/Parse/CodeCompletionCallbacks.h" -#include "swift/Parse/SyntaxParsingContext.h" #include "swift/Parse/Lexer.h" -#include "swift/Syntax/SyntaxBuilders.h" -#include "swift/Syntax/SyntaxNodes.h" + using namespace swift; -using namespace swift::syntax; /// parseGenericParameters - Parse a sequence of generic parameters, e.g., /// < T : Comparable, U : Container> along with an optional requires clause. @@ -40,7 +37,6 @@ using namespace swift::syntax; /// When parsing the generic parameters, this routine establishes a new scope /// and adds those parameters to the scope. ParserResult Parser::parseGenericParameters() { - SyntaxParsingContext GPSContext(SyntaxContext, SyntaxKind::GenericParameterClause); // Parse the opening '<'. assert(startsWithLess(Tok) && "Generic parameter list must start with '<'"); return parseGenericParameters(consumeStartingLess()); @@ -50,10 +46,8 @@ ParserStatus Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc, SmallVectorImpl &GenericParams) { ParserStatus Result; - SyntaxParsingContext GPSContext(SyntaxContext, SyntaxKind::GenericParameterList); bool HasNextParam; do { - SyntaxParsingContext GParamContext(SyntaxContext, SyntaxKind::GenericParameter); // Note that we're parsing a declaration. StructureMarkerRAII ParsingDecl(*this, Tok.getLoc(), StructureMarkerKind::Declaration); @@ -260,20 +254,11 @@ ParserStatus Parser::parseGenericWhereClause( SourceLoc &WhereLoc, SourceLoc &EndLoc, SmallVectorImpl &Requirements, bool AllowLayoutConstraints) { - SyntaxParsingContext ClauseContext(SyntaxContext, - SyntaxKind::GenericWhereClause); ParserStatus Status; // Parse the 'where'. WhereLoc = consumeToken(tok::kw_where); - SyntaxParsingContext ReqListContext(SyntaxContext, - SyntaxKind::GenericRequirementList); bool HasNextReq; do { - SyntaxParsingContext ReqContext(SyntaxContext, - SyntaxKind::GenericRequirement); - Optional BodyContext; - BodyContext.emplace(SyntaxContext); - if (Tok.is(tok::code_complete)) { if (CodeCompletion) CodeCompletion->completeGenericRequirement(); @@ -287,12 +272,10 @@ ParserStatus Parser::parseGenericWhereClause( ParserResult FirstType = parseType(); if (FirstType.hasCodeCompletion()) { - BodyContext->setTransparent(); Status.setHasCodeCompletionAndIsError(); } if (FirstType.isNull()) { - BodyContext->setTransparent(); Status.setIsParseError(); break; } @@ -303,7 +286,6 @@ ParserStatus Parser::parseGenericWhereClause( if (Tok.is(tok::identifier) && getLayoutConstraint(Context.getIdentifier(Tok.getText()), Context) ->isKnownLayout()) { - BodyContext->setCreateSyntax(SyntaxKind::LayoutRequirement); // Parse a layout constraint. Identifier LayoutName; @@ -326,7 +308,6 @@ ParserStatus Parser::parseGenericWhereClause( LayoutConstraintLoc(Layout, LayoutLoc))); } } else { - BodyContext->setCreateSyntax(SyntaxKind::ConformanceRequirement); // Parse the protocol or composition. ParserResult Protocol = parseType(); Status |= Protocol; @@ -339,7 +320,6 @@ ParserStatus Parser::parseGenericWhereClause( } } else if ((Tok.isAnyOperator() && Tok.getText() == "==") || Tok.is(tok::equal)) { - BodyContext->setCreateSyntax(SyntaxKind::SameTypeRequirement); // A same-type-requirement if (Tok.is(tok::equal)) { diagnose(Tok, diag::requires_single_equal) @@ -376,12 +356,10 @@ ParserStatus Parser::parseGenericWhereClause( Requirements.push_back(RequirementRepr::getTypeConstraint( FirstType.get(), PreviousLoc, new (Context) ErrorTypeRepr(PreviousLoc))); } else { - BodyContext->setTransparent(); diagnose(Tok, diag::expected_requirement_delim); Status.setIsParseError(); break; } - BodyContext.reset(); HasNextReq = consumeIf(tok::comma); // If there's a comma, keep parsing the list. // If there's a "&&", diagnose replace with a comma and keep parsing diff --git a/lib/Parse/ParseIfConfig.cpp b/lib/Parse/ParseIfConfig.cpp index 6f2c5e7669a56..88f8b6bedf42f 100644 --- a/lib/Parse/ParseIfConfig.cpp +++ b/lib/Parse/ParseIfConfig.cpp @@ -23,15 +23,11 @@ #include "swift/Basic/Version.h" #include "swift/Parse/Lexer.h" #include "swift/Parse/ParseVersion.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Syntax/SyntaxFactory.h" -#include "swift/Syntax/TokenSyntax.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/SaveAndRestore.h" using namespace swift; -using namespace swift::syntax; namespace { @@ -779,10 +775,6 @@ Result Parser::parseIfConfigRaw( parseElements, llvm::function_ref finish) { assert(Tok.is(tok::pound_if)); - SyntaxParsingContext IfConfigCtx(SyntaxContext, SyntaxKind::IfConfigDecl); - - Parser::StructureMarkerRAII ParsingDecl( - *this, Tok.getLoc(), Parser::StructureMarkerKind::IfConfig); // Find the region containing code completion token. SourceLoc codeCompletionClauseLoc; @@ -817,9 +809,6 @@ Result Parser::parseIfConfigRaw( bool foundActive = false; bool isVersionCondition = false; while (1) { - SyntaxParsingContext ClauseContext(SyntaxContext, - SyntaxKind::IfConfigClause); - bool isElse = Tok.is(tok::pound_else); SourceLoc ClauseLoc = consumeToken(); Expr *Condition = nullptr; @@ -835,20 +824,6 @@ Result Parser::parseIfConfigRaw( // clause unless we're doing a parse-only pass. if (isElse) { isActive = !foundActive && shouldEvaluate; - if (SyntaxContext->isEnabled()) { - // Because we use the same libSyntax node for #elseif and #else, we need - // to disambiguate whether a postfix expression is the condition of - // #elseif or a postfix expression of the #else body. - // To do this, push three empty syntax nodes onto the stack. - // - First one for unexpected nodes between the #else keyword and the - // condition - // - One for the condition itself (whcih doesn't exist) - // - And finally one for the unexpected nodes between the condition and - // the elements - SyntaxContext->addRawSyntax(ParsedRawSyntaxNode()); - SyntaxContext->addRawSyntax(ParsedRawSyntaxNode()); - SyntaxContext->addRawSyntax(ParsedRawSyntaxNode()); - } } else { llvm::SaveAndRestore S(InPoundIfEnvironment, true); ParserResult result = parseExprSequence(diag::expected_expr, @@ -900,12 +875,6 @@ Result Parser::parseIfConfigRaw( if (isActive || !isVersionCondition) { parseElements( ClauseLoc, Condition, isActive, IfConfigElementsRole::Normal); - } else if (SyntaxContext->isEnabled()) { - // We shouldn't skip code if we are building syntax tree. - // The parser will keep running and we just discard the AST part. - DiagnosticSuppression suppression(Context.Diags); - parseElements( - ClauseLoc, Condition, isActive, IfConfigElementsRole::SyntaxOnly); } else { DiagnosticTransaction DT(Diags); skipUntilConditionalBlockClose(); @@ -920,7 +889,6 @@ Result Parser::parseIfConfigRaw( if (isElse) diagnose(Tok, diag::expected_close_after_else_directive); } - SyntaxContext->collectNodesInPlace(SyntaxKind::IfConfigClauseList); SourceLoc EndLoc; bool HadMissingEnd = parseEndIfDirective(EndLoc); diff --git a/lib/Parse/ParsePattern.cpp b/lib/Parse/ParsePattern.cpp index fd76d562e4393..51fdbb3f91cf9 100644 --- a/lib/Parse/ParsePattern.cpp +++ b/lib/Parse/ParsePattern.cpp @@ -24,15 +24,11 @@ #include "swift/AST/TypeRepr.h" #include "swift/Basic/StringExtras.h" #include "swift/Parse/CodeCompletionCallbacks.h" -#include "swift/Parse/ParsedSyntaxRecorder.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Syntax/SyntaxKind.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/SaveAndRestore.h" using namespace swift; -using namespace swift::syntax; /// Determine the kind of a default argument given a parsed /// expression that has not yet been type-checked. @@ -70,8 +66,6 @@ static ParserStatus parseDefaultArgument( Parser &P, Parser::DefaultArgumentInfo *defaultArgs, unsigned argIndex, Expr *&init, bool &hasInheritedDefaultArg, Parser::ParameterContextKind paramContext) { - SyntaxParsingContext DefaultArgContext(P.SyntaxContext, - SyntaxKind::InitializerClause); assert(P.Tok.is(tok::equal) || (P.Tok.isBinaryOperator() && P.Tok.getText() == "==")); SourceLoc equalLoc = P.consumeToken(); @@ -88,7 +82,6 @@ static ParserStatus parseDefaultArgument( if (P.Tok.is(tok::kw_super) && P.peekToken().isAny(tok::comma, tok::r_paren)) { hasInheritedDefaultArg = true; P.consumeToken(tok::kw_super); - P.SyntaxContext->createNodeInPlace(SyntaxKind::SuperRefExpr); defaultArgs->HasDefaultArgument = true; return ParserStatus(); } @@ -199,17 +192,12 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc, ParameterContextKind paramContext) { assert(params.empty() && leftParenLoc.isInvalid() && rightParenLoc.isInvalid() && "Must start with empty state"); - SyntaxParsingContext ParamClauseCtx(SyntaxContext, SyntaxKind::ParameterClause); // Consume the starting '('; leftParenLoc = consumeToken(tok::l_paren); // Trivial case: empty parameter list. if (Tok.is(tok::r_paren)) { - { - SyntaxParsingContext EmptyPLContext(SyntaxContext, - SyntaxKind::FunctionParameterList); - } rightParenLoc = consumeToken(tok::r_paren); // Per SE-0155, enum elements may not have empty parameter lists. @@ -237,7 +225,6 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc, return parseList(tok::r_paren, leftParenLoc, rightParenLoc, /*AllowSepAfterLast=*/false, diag::expected_rparen_parameter, - SyntaxKind::FunctionParameterList, [&]() -> ParserStatus { ParsedParameter param; ParserStatus status; @@ -256,8 +243,6 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc, } { - SyntaxParsingContext ModifiersContext(SyntaxContext, SyntaxKind::ModifierList); - // ('inout' | '__shared' | '__owned' | isolated)? bool hasSpecifier = false; while (Tok.is(tok::kw_inout) || @@ -265,8 +250,6 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc, Tok.isContextualKeyword("__owned") || Tok.isContextualKeyword("isolated") || Tok.isContextualKeyword("_const")) { - SyntaxParsingContext ModContext(SyntaxContext, SyntaxKind::DeclModifier); - if (Tok.isContextualKeyword("isolated")) { // did we already find an 'isolated' type modifier? if (param.IsolatedLoc.isValid()) { @@ -841,7 +824,6 @@ Parser::parseFunctionSignature(DeclBaseName SimpleName, SourceLoc &throwsLoc, bool &rethrows, TypeRepr *&retType) { - SyntaxParsingContext SigContext(SyntaxContext, SyntaxKind::FunctionSignature); SmallVector NamePieces; ParserStatus Status; @@ -864,7 +846,6 @@ Parser::parseFunctionSignature(DeclBaseName SimpleName, // If there's a trailing arrow, parse the rest as the result type. SourceLoc arrowLoc; if (Tok.isAny(tok::arrow, tok::colon)) { - SyntaxParsingContext ReturnCtx(SyntaxContext, SyntaxKind::ReturnClause); if (!consumeIf(tok::arrow, arrowLoc)) { // FixIt ':' to '->'. diagnose(Tok, diag::func_decl_expected_arrow) @@ -1023,7 +1004,6 @@ ParserResult Parser::parseTypedPattern() { // Now parse an optional type annotation. if (Tok.is(tok::colon)) { - SyntaxParsingContext TypeAnnoCtx(SyntaxContext, SyntaxKind::TypeAnnotation); SourceLoc colonLoc = consumeToken(tok::colon); if (result.isNull()) { @@ -1085,7 +1065,6 @@ ParserResult Parser::parseTypedPattern() { /// pattern ::= 'let' pattern /// ParserResult Parser::parsePattern() { - SyntaxParsingContext PatternCtx(SyntaxContext, SyntaxContextKind::Pattern); auto introducer = (InVarOrLetPattern != IVOLP_InVar ? VarDecl::Introducer::Let : VarDecl::Introducer::Var); @@ -1100,13 +1079,11 @@ ParserResult Parser::parsePattern() { // VarDecl inside. if (CurDeclContext->isTypeContext() && SF.Kind == SourceFileKind::Interface) { - PatternCtx.setCreateSyntax(SyntaxKind::IdentifierPattern); auto VD = new (Context) VarDecl( /*IsStatic*/false, introducer, consumeToken(tok::kw__), Identifier(), CurDeclContext); return makeParserResult(NamedPattern::createImplicit(Context, VD)); } - PatternCtx.setCreateSyntax(SyntaxKind::WildcardPattern); const auto isAsyncLet = InPatternWithAsyncAttribute && introducer == VarDecl::Introducer::Let; @@ -1114,7 +1091,6 @@ ParserResult Parser::parsePattern() { new (Context) AnyPattern(consumeToken(tok::kw__), isAsyncLet)); } case tok::identifier: { - PatternCtx.setCreateSyntax(SyntaxKind::IdentifierPattern); Identifier name; SourceLoc loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/true); if (Tok.isIdentifierOrUnderscore() && !Tok.isContextualDeclKeyword() && @@ -1136,7 +1112,6 @@ ParserResult Parser::parsePattern() { case tok::kw_var: case tok::kw_let: { - PatternCtx.setCreateSyntax(SyntaxKind::ValueBindingPattern); bool isLet = Tok.is(tok::kw_let); SourceLoc varLoc = consumeToken(); @@ -1221,8 +1196,6 @@ Parser::parsePatternTupleElement() { /// pattern-tuple-body: /// pattern-tuple-element (',' pattern-tuple-body)* ParserResult Parser::parsePatternTuple() { - SyntaxParsingContext TuplePatternCtxt(SyntaxContext, - SyntaxKind::TuplePattern); StructureMarkerRAII ParsingPatternTuple(*this, Tok); SourceLoc LPLoc = consumeToken(tok::l_paren); SourceLoc RPLoc; @@ -1233,7 +1206,6 @@ ParserResult Parser::parsePatternTuple() { parseList(tok::r_paren, LPLoc, RPLoc, /*AllowSepAfterLast=*/false, diag::expected_rparen_tuple_pattern_list, - SyntaxKind::TuplePatternElementList, [&] () -> ParserStatus { // Parse the pattern tuple element. ParserStatus EltStatus; @@ -1264,8 +1236,6 @@ parseOptionalPatternTypeAnnotation(ParserResult result) { return result; // Parse an optional type annotation. - SyntaxParsingContext TypeAnnotationCtxt(SyntaxContext, - SyntaxKind::TypeAnnotation); consumeToken(tok::colon); if (result.isNull()) @@ -1300,11 +1270,9 @@ ParserResult Parser::parseMatchingPattern(bool isExprBasic) { // parse pattern nodes for productions shared by pattern and expression // grammar. For short-term ease of initial implementation, we always go // through the expr parser for ambiguous productions. - SyntaxParsingContext PatternCtx(SyntaxContext, SyntaxContextKind::Pattern); // Parse productions that can only be patterns. if (Tok.isAny(tok::kw_var, tok::kw_let)) { - PatternCtx.setCreateSyntax(SyntaxKind::ValueBindingPattern); assert(Tok.isAny(tok::kw_let, tok::kw_var) && "expects var or let"); bool isLet = Tok.is(tok::kw_let); SourceLoc varLoc = consumeToken(); @@ -1314,7 +1282,6 @@ ParserResult Parser::parseMatchingPattern(bool isExprBasic) { // matching-pattern ::= 'is' type if (Tok.is(tok::kw_is)) { - PatternCtx.setCreateSyntax(SyntaxKind::IsTypePattern); SourceLoc isLoc = consumeToken(tok::kw_is); ParserResult castType = parseType(); if (castType.isNull() || castType.hasCodeCompletion()) @@ -1333,13 +1300,6 @@ ParserResult Parser::parseMatchingPattern(bool isExprBasic) { if (subExpr.isNull()) return status; - if (SyntaxContext->isEnabled()) { - if (auto UPES = PatternCtx.popIf()) { - PatternCtx.addSyntax(UPES->getDeferredPattern(SyntaxContext)); - } else { - PatternCtx.setCreateSyntax(SyntaxKind::ExpressionPattern); - } - } // The most common case here is to parse something that was a lexically // obvious pattern, which will come back wrapped in an immediate // UnresolvedPatternExpr. Transform this now to simplify later code. diff --git a/lib/Parse/ParseRegex.cpp b/lib/Parse/ParseRegex.cpp index b2d4fe942b27d..8584703aba135 100644 --- a/lib/Parse/ParseRegex.cpp +++ b/lib/Parse/ParseRegex.cpp @@ -15,10 +15,7 @@ //===----------------------------------------------------------------------===// #include "swift/Basic/BridgingUtils.h" -#include "swift/Parse/ParsedSyntaxRecorder.h" #include "swift/Parse/Parser.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Syntax/SyntaxKind.h" // Regex parser delivered via Swift modules. #include "swift/Parse/RegexParserBridging.h" @@ -28,15 +25,11 @@ void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn fn) { } using namespace swift; -using namespace swift::syntax; ParserResult Parser::parseExprRegexLiteral() { assert(Tok.is(tok::regex_literal)); assert(regexLiteralParsingFn); - SyntaxParsingContext LocalContext(SyntaxContext, - SyntaxKind::RegexLiteralExpr); - auto regexText = Tok.getText(); // Let the Swift library parse the contents, returning an error, or null if diff --git a/lib/Parse/ParseRequests.cpp b/lib/Parse/ParseRequests.cpp index 16b066e3683f7..efb933bc3a55e 100644 --- a/lib/Parse/ParseRequests.cpp +++ b/lib/Parse/ParseRequests.cpp @@ -22,9 +22,6 @@ #include "swift/Basic/Defer.h" #include "swift/Parse/Parser.h" #include "swift/Subsystems.h" -#include "swift/Syntax/SyntaxArena.h" -#include "swift/Syntax/SyntaxNodes.h" -#include "swift/SyntaxParse/SyntaxTreeCreator.h" #if SWIFT_SWIFT_PARSER #include "SwiftCompilerSupport.h" @@ -81,8 +78,6 @@ ParseMembersRequest::evaluate(Evaluator &evaluator, // Lexer diagnostics have been emitted during skipping, so we disable lexer's // diagnostic engine here. Parser parser(bufferID, *sf, /*No Lexer Diags*/nullptr, nullptr, nullptr); - // Disable libSyntax creation in the delayed parsing. - parser.SyntaxContext->disable(); auto declsAndHash = parser.parseDeclListDelayed(idc); FingerprintAndMembers fingerprintAndMembers = {declsAndHash.second, declsAndHash.first}; @@ -125,7 +120,6 @@ ParseAbstractFunctionBodyRequest::evaluate(Evaluator &evaluator, unsigned bufferID = sourceMgr.findBufferContainingLoc(afd->getBodySourceRange().Start); Parser parser(bufferID, sf, /*SIL*/ nullptr); - parser.SyntaxContext->disable(); auto result = parser.parseAbstractFunctionBodyDelayed(afd); afd->setBodyKind(BodyKind::Parsed); return result; @@ -154,12 +148,6 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator, if (!bufferID) return {}; - std::shared_ptr sTreeCreator; - if (SF->shouldBuildSyntaxTree()) { - sTreeCreator = std::make_shared( - ctx.SourceMgr, *bufferID, SF->SyntaxParsingCache, ctx.getSyntaxArena()); - } - // If we've been asked to silence warnings, do so now. This is needed for // secondary files, which can be parsed multiple times. auto &diags = ctx.Diags; @@ -177,18 +165,12 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator, SF->setDelayedParserState({state, &deletePersistentParserState}); } - Parser parser(*bufferID, *SF, /*SIL*/ nullptr, state, sTreeCreator); + Parser parser(*bufferID, *SF, /*SIL*/ nullptr, state); PrettyStackTraceParser StackTrace(parser); SmallVector items; parser.parseTopLevelItems(items); - Optional syntaxRoot; - if (sTreeCreator) { - auto rawNode = parser.finalizeSyntaxTree(); - syntaxRoot.emplace(*sTreeCreator->realizeSyntaxRoot(rawNode, *SF)); - } - Optional> tokensRef; if (auto tokens = parser.takeTokenReceiver()->finalize()) tokensRef = ctx.AllocateCopy(*tokens); @@ -238,7 +220,7 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator, #endif return SourceFileParsingResult{ctx.AllocateCopy(items), tokensRef, - parser.CurrentTokenHash, syntaxRoot}; + parser.CurrentTokenHash}; } evaluator::DependencySource ParseSourceFileRequest::readDependencySource( @@ -253,12 +235,8 @@ ParseSourceFileRequest::getCachedResult() const { if (!items) return None; - Optional syntaxRoot; - if (auto &rootPtr = SF->SyntaxRoot) - syntaxRoot.emplace(*rootPtr); - return SourceFileParsingResult{*items, SF->AllCollectedTokens, - SF->InterfaceHasher, syntaxRoot}; + SF->InterfaceHasher}; } void ParseSourceFileRequest::cacheResult(SourceFileParsingResult result) const { @@ -268,9 +246,6 @@ void ParseSourceFileRequest::cacheResult(SourceFileParsingResult result) const { SF->AllCollectedTokens = result.CollectedTokens; SF->InterfaceHasher = result.InterfaceHasher; - if (auto &root = result.SyntaxRoot) - SF->SyntaxRoot = std::make_unique(std::move(*root)); - // Verify the parsed source file. verify(*SF); } diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp index b90ba7a787c27..e5d4aa16e90ec 100644 --- a/lib/Parse/ParseStmt.cpp +++ b/lib/Parse/ParseStmt.cpp @@ -22,9 +22,7 @@ #include "swift/Parse/CodeCompletionCallbacks.h" #include "swift/Parse/Lexer.h" #include "swift/Parse/Parser.h" -#include "swift/Parse/SyntaxParsingContext.h" #include "swift/Subsystems.h" -#include "swift/Syntax/TokenSyntax.h" #include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/Twine.h" @@ -32,7 +30,6 @@ #include "llvm/Support/SaveAndRestore.h" using namespace swift; -using namespace swift::syntax; /// isStartOfStmt - Return true if the current token starts a statement. /// @@ -120,7 +117,6 @@ bool Parser::isStartOfStmt() { ParserStatus Parser::parseExprOrStmt(ASTNode &Result) { if (Tok.is(tok::semi)) { - SyntaxParsingContext ErrorCtxt(SyntaxContext, SyntaxContextKind::Stmt); diagnose(Tok, diag::illegal_semi_stmt) .fixItRemove(SourceRange(Tok.getLoc())); consumeToken(); @@ -129,7 +125,6 @@ ParserStatus Parser::parseExprOrStmt(ASTNode &Result) { if (Tok.is(tok::pound) && Tok.isAtStartOfLine() && peekToken().is(tok::code_complete)) { - SyntaxParsingContext CCCtxt(SyntaxContext, SyntaxContextKind::Decl); consumeToken(); if (CodeCompletion) CodeCompletion->completeAfterPoundDirective(); @@ -156,7 +151,6 @@ ParserStatus Parser::parseExprOrStmt(ASTNode &Result) { Result = CCE; if (CodeCompletion) CodeCompletion->completeStmtOrExpr(CCE); - SyntaxParsingContext ErrorCtxt(SyntaxContext, SyntaxContextKind::Stmt); consumeToken(tok::code_complete); return makeParserCodeCompletionStatus(); } @@ -236,8 +230,6 @@ bool Parser::isTerminatorForBraceItemListKind(BraceItemListKind Kind, void Parser::consumeTopLevelDecl(ParserPosition BeginParserPosition, TopLevelCodeDecl *TLCD) { - SyntaxParsingContext Discarding(SyntaxContext); - Discarding.disable(); SourceLoc EndLoc = PreviousLoc; backtrackToPosition(BeginParserPosition); SourceLoc BeginLoc = Tok.getLoc(); @@ -276,13 +268,6 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl &Entries, BraceItemListKind Kind, BraceItemListKind ConditionalBlockKind, bool &IsFollowingGuard) { - bool isRootCtx = SyntaxContext->isRoot(); - SyntaxParsingContext ItemListContext(SyntaxContext, - SyntaxKind::CodeBlockItemList); - if (isRootCtx) { - ItemListContext.setTransparent(); - } - bool IsTopLevel = (Kind == BraceItemListKind::TopLevelCode) || (Kind == BraceItemListKind::TopLevelLibrary); bool isActiveConditionalBlock = @@ -302,13 +287,7 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl &Entries, (isConditionalBlock || !isTerminatorForBraceItemListKind(Kind, Entries))) { - SyntaxParsingContext NodeContext(SyntaxContext, SyntaxKind::CodeBlockItem); - if (loadCurrentSyntaxNodeFromCache()) { - continue; - } - if (Tok.is(tok::r_brace)) { - SyntaxParsingContext ErrContext(SyntaxContext, SyntaxContextKind::Stmt); assert(IsTopLevel); diagnose(Tok, diag::extra_rbrace) .fixItRemove(Tok.getLoc()); @@ -318,7 +297,6 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl &Entries, // Eat invalid tokens instead of allowing them to produce downstream errors. if (Tok.is(tok::unknown)) { - SyntaxParsingContext ErrContext(SyntaxContext, SyntaxContextKind::Stmt); if (Tok.getText().startswith("\"\"\"")) { // This was due to unterminated multi-line string. IsInputIncomplete = true; @@ -494,8 +472,6 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl &Entries, } if (NeedParseErrorRecovery) { - SyntaxParsingContext TokenListCtxt(SyntaxContext, - SyntaxKind::NonEmptyTokenList); // If we had a parse error, skip to the start of the next stmt or decl. // // It would be ideal to stop at the start of the next expression (e.g. @@ -528,22 +504,13 @@ ParserResult Parser::parseStmt() { // If this is a label on a loop/switch statement, consume it and pass it into // parsing logic below. - SyntaxParsingContext LabelContext(SyntaxContext, SyntaxKind::LabeledStmt); LabeledStmtInfo LabelInfo; if (Tok.is(tok::identifier) && peekToken().is(tok::colon)) { LabelInfo.Loc = consumeIdentifier(LabelInfo.Name, /*diagnoseDollarPrefix=*/true); consumeToken(tok::colon); - } else { - LabelContext.setTransparent(); } - SyntaxParsingContext LocalContext(SyntaxContext, SyntaxContextKind::Stmt); - - // Note that we're parsing a statement. - StructureMarkerRAII ParsingStmt(*this, Tok.getLoc(), - StructureMarkerKind::Statement); - SourceLoc tryLoc; (void)consumeIf(tok::kw_try, tryLoc); @@ -624,7 +591,6 @@ ParserResult Parser::parseStmt() { if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt); if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText()); - SyntaxContext->setCreateSyntax(SyntaxKind::FallthroughStmt); return makeParserResult( new (Context) FallthroughStmt(consumeToken(tok::kw_fallthrough))); } @@ -657,7 +623,6 @@ ParserResult Parser::parseBraceItemList(Diag<> ID) { !Tok.is(tok::l_brace)) return nullptr; } - SyntaxParsingContext LocalContext(SyntaxContext, SyntaxKind::CodeBlock); SourceLoc LBLoc = consumeToken(tok::l_brace); SmallVector Entries; @@ -665,11 +630,8 @@ ParserResult Parser::parseBraceItemList(Diag<> ID) { ParserStatus Status = parseBraceItems(Entries, BraceItemListKind::Brace, BraceItemListKind::Brace); - if (parseMatchingToken(tok::r_brace, RBLoc, - diag::expected_rbrace_in_brace_stmt, LBLoc)) { - // Synthesize a r-brace if the source doesn't have any. - LocalContext.synthesize(tok::r_brace, LBLoc.getAdvancedLoc(1)); - } else { + if (!parseMatchingToken(tok::r_brace, RBLoc, + diag::expected_rbrace_in_brace_stmt, LBLoc)) { // We recovered do don't propagate any error status (but still preserve // HasCodeCompletion). Status.clearIsError(); @@ -709,7 +671,6 @@ static ParserStatus parseOptionalControlTransferTarget(Parser &P, /// 'break' identifier? /// ParserResult Parser::parseStmtBreak() { - SyntaxContext->setCreateSyntax(SyntaxKind::BreakStmt); SourceLoc Loc = consumeToken(tok::kw_break); SourceLoc TargetLoc; Identifier Target; @@ -727,7 +688,6 @@ ParserResult Parser::parseStmtBreak() { /// 'continue' identifier? /// ParserResult Parser::parseStmtContinue() { - SyntaxContext->setCreateSyntax(SyntaxKind::ContinueStmt); SourceLoc Loc = consumeToken(tok::kw_continue); SourceLoc TargetLoc; Identifier Target; @@ -746,7 +706,6 @@ ParserResult Parser::parseStmtContinue() { /// 'return' expr? /// ParserResult Parser::parseStmtReturn(SourceLoc tryLoc) { - SyntaxContext->setCreateSyntax(SyntaxKind::ReturnStmt); SourceLoc ReturnLoc = consumeToken(tok::kw_return); if (Tok.is(tok::code_complete)) { @@ -814,7 +773,6 @@ ParserResult Parser::parseStmtReturn(SourceLoc tryLoc) { /// /// Note that a parenthesis always starts the second (list) grammar. ParserResult Parser::parseStmtYield(SourceLoc tryLoc) { - SyntaxContext->setCreateSyntax(SyntaxKind::YieldStmt); SourceLoc yieldLoc = consumeToken(tok::kw_yield); if (Tok.is(tok::code_complete)) { @@ -841,11 +799,9 @@ ParserResult Parser::parseStmtYield(SourceLoc tryLoc) { .fixItRemoveChars(tryLoc, yieldLoc); } - SyntaxParsingContext YieldsCtxt(SyntaxContext, SyntaxKind::YieldList); - SmallVector yieldElts; status = parseExprList(tok::l_paren, tok::r_paren, /*isArgumentList*/ false, - lpLoc, yieldElts, rpLoc, SyntaxKind::YieldExprList); + lpLoc, yieldElts, rpLoc); for (auto &elt : yieldElts) { // We don't accept labels in a list of yields. if (elt.LabelLoc.isValid()) { @@ -886,7 +842,6 @@ ParserResult Parser::parseStmtYield(SourceLoc tryLoc) { /// 'throw' expr /// ParserResult Parser::parseStmtThrow(SourceLoc tryLoc) { - SyntaxContext->setCreateSyntax(SyntaxKind::ThrowStmt); SourceLoc throwLoc = consumeToken(tok::kw_throw); SourceLoc exprLoc; if (Tok.isNot(tok::eof)) @@ -922,7 +877,6 @@ ParserResult Parser::parseStmtThrow(SourceLoc tryLoc) { /// 'defer' brace-stmt /// ParserResult Parser::parseStmtDefer() { - SyntaxContext->setCreateSyntax(SyntaxKind::DeferStmt); SourceLoc DeferLoc = consumeToken(tok::kw_defer); // Macro expand out the defer into a closure and call, which we can typecheck @@ -998,8 +952,6 @@ static void parseWhereGuard(Parser &P, GuardedPattern &result, GuardedPatternContext parsingContext, bool isExprBasic) { if (P.Tok.is(tok::kw_where)) { - SyntaxParsingContext WhereClauseCtxt(P.SyntaxContext, - SyntaxKind::WhereClause); result.WhereLoc = P.consumeToken(tok::kw_where); SourceLoc startOfGuard = P.Tok.getLoc(); @@ -1262,22 +1214,18 @@ validateAvailabilitySpecList(Parser &P, // #unavailable(...) ParserResult Parser::parseStmtConditionPoundAvailable() { tok MainToken; - SyntaxKind Kind; AvailabilitySpecSource Source; bool isUnavailability; if (Tok.is(tok::pound_available)) { MainToken = tok::pound_available; - Kind = SyntaxKind::AvailabilityCondition; Source = AvailabilitySpecSource::Available; isUnavailability = false; } else { MainToken = tok::pound_unavailable; - Kind = SyntaxKind::UnavailabilityCondition; Source = AvailabilitySpecSource::Unavailable; isUnavailability = true; } - SyntaxParsingContext ConditionCtxt(SyntaxContext, Kind); SourceLoc PoundLoc; PoundLoc = consumeToken(MainToken); @@ -1362,16 +1310,11 @@ Parser::parseAvailabilityMacroDefinition(AvailabilityMacroDefinition &Result) { ParserStatus Parser::parseAvailabilitySpecList(SmallVectorImpl &Specs, AvailabilitySpecSource Source) { - SyntaxParsingContext AvailabilitySpecContext( - SyntaxContext, SyntaxKind::AvailabilitySpecList); ParserStatus Status = makeParserSuccess(); // We don't use parseList() because we want to provide more specific // diagnostics disallowing operators in version specs. while (1) { - SyntaxParsingContext AvailabilityEntryContext( - SyntaxContext, SyntaxKind::AvailabilityArgument); - // First look for a macro as we need Specs for the expansion. bool MatchedAMacro = false; switch (Source) { @@ -1472,8 +1415,6 @@ Parser::parseAvailabilitySpecList(SmallVectorImpl &Specs, // #_hasSymbol(...) ParserResult Parser::parseStmtConditionPoundHasSymbol() { - SyntaxParsingContext ConditionCtxt(SyntaxContext, - SyntaxKind::HasSymbolCondition); SourceLoc PoundLoc = consumeToken(tok::pound__hasSymbol); if (!Tok.isFollowingLParen()) { @@ -1577,8 +1518,6 @@ Parser::parseStmtConditionElement(SmallVectorImpl &result, } } - SyntaxParsingContext ConditionCtxt(SyntaxContext); - SourceLoc IntroducerLoc; if (Tok.isAny(tok::kw_let, tok::kw_var, tok::kw_case)) { BindingKindStr = Tok.getText(); @@ -1601,7 +1540,6 @@ Parser::parseStmtConditionElement(SmallVectorImpl &result, ParserResult ThePattern; if (BindingKindStr == "case") { - ConditionCtxt.setCreateSyntax(SyntaxKind::MatchingPatternCondition); // In our recursive parse, remember that we're in a matching pattern. llvm::SaveAndRestore T(InVarOrLetPattern, IVOLP_InMatchingPattern); @@ -1611,7 +1549,6 @@ Parser::parseStmtConditionElement(SmallVectorImpl &result, ThePattern = parseMatchingPattern(/*isExprBasic*/ true); } else if (Tok.is(tok::kw_case)) { - ConditionCtxt.setCreateSyntax(SyntaxKind::Unknown); // If will probably be a common typo to write "if let case" instead of // "if case let" so detect this and produce a nice fixit. diagnose(IntroducerLoc, diag::wrong_condition_case_location, @@ -1647,7 +1584,6 @@ Parser::parseStmtConditionElement(SmallVectorImpl &result, ThePattern.setHasCodeCompletionAndIsError(); consumeToken(tok::code_complete); } else { - ConditionCtxt.setCreateSyntax(SyntaxKind::OptionalBindingCondition); // Otherwise, this is an implicit optional binding "if let". ThePattern = parseMatchingPatternAsLetOrVar(BindingKindStr == "let", IntroducerLoc, @@ -1679,7 +1615,6 @@ Parser::parseStmtConditionElement(SmallVectorImpl &result, // `let newBinding`, which is shorthand for `let newBinding = newBinding` ParserResult Init; if (Tok.is(tok::equal)) { - SyntaxParsingContext InitCtxt(SyntaxContext, SyntaxKind::InitializerClause); consumeToken(); Init = parseExprBasic(diag::expected_expr_conditional_var); } else if (!ThePattern.getPtrOrNull()->getBoundName().empty()) { @@ -1738,8 +1673,6 @@ Parser::parseStmtConditionElement(SmallVectorImpl &result, /// ParserStatus Parser::parseStmtCondition(StmtCondition &Condition, Diag<> DefaultID, StmtKind ParentKind) { - SyntaxParsingContext ConditionListCtxt(SyntaxContext, - SyntaxKind::ConditionElementList); ParserStatus Status; Condition = StmtCondition(); @@ -1753,8 +1686,6 @@ ParserStatus Parser::parseStmtCondition(StmtCondition &Condition, // a variety of common errors situations (including migrating from Swift 2 // syntax). while (true) { - SyntaxParsingContext ConditionElementCtxt(SyntaxContext, - SyntaxKind::ConditionElement); Status |= parseStmtConditionElement(result, DefaultID, ParentKind, BindingKindStr); if (Status.isErrorOrHasCompletion()) @@ -1798,7 +1729,6 @@ ParserStatus Parser::parseStmtCondition(StmtCondition &Condition, /// 'else' stmt-if ParserResult Parser::parseStmtIf(LabeledStmtInfo LabelInfo, bool IfWasImplicitlyInserted) { - SyntaxContext->setCreateSyntax(SyntaxKind::IfStmt); SourceLoc IfLoc; if (IfWasImplicitlyInserted) { // The code was invalid due to a missing 'if' (e.g. 'else x < y {') and a @@ -1880,7 +1810,6 @@ ParserResult Parser::parseStmtIf(LabeledStmtInfo LabelInfo, diagnose(ElseLoc, diag::expected_lbrace_or_if_after_else_fixit) .fixItInsertAfter(ElseLoc, " if"); } - SyntaxParsingContext ElseIfCtxt(SyntaxContext, SyntaxKind::IfStmt); ElseBody = parseStmtIf(LabeledStmtInfo(), implicitlyInsertIf); } else if (Tok.is(tok::code_complete)) { if (CodeCompletion) @@ -1903,7 +1832,6 @@ ParserResult Parser::parseStmtIf(LabeledStmtInfo LabelInfo, /// 'guard' condition 'else' stmt-brace /// ParserResult Parser::parseStmtGuard() { - SyntaxContext->setCreateSyntax(SyntaxKind::GuardStmt); SourceLoc GuardLoc = consumeToken(tok::kw_guard); ParserStatus Status; @@ -1966,7 +1894,6 @@ ParserResult Parser::parseStmtGuard() { /// stmt-while: /// (identifier ':')? 'while' expr-basic stmt-brace ParserResult Parser::parseStmtWhile(LabeledStmtInfo LabelInfo) { - SyntaxContext->setCreateSyntax(SyntaxKind::WhileStmt); SourceLoc WhileLoc = consumeToken(tok::kw_while); ParserStatus Status; @@ -2016,7 +1943,6 @@ ParserResult Parser::parseStmtWhile(LabeledStmtInfo LabelInfo) { /// stmt-repeat: /// (identifier ':')? 'repeat' stmt-brace 'while' expr ParserResult Parser::parseStmtRepeat(LabeledStmtInfo labelInfo) { - SyntaxContext->setCreateSyntax(SyntaxKind::RepeatWhileStmt); SourceLoc repeatLoc = consumeToken(tok::kw_repeat); ParserStatus status; @@ -2060,7 +1986,6 @@ ParserResult Parser::parseStmtRepeat(LabeledStmtInfo labelInfo) { /// (identifier ':')? 'do' stmt-brace stmt-catch+ ParserResult Parser::parseStmtDo(LabeledStmtInfo labelInfo, bool shouldSkipDoTokenConsume) { - SyntaxContext->setCreateSyntax(SyntaxKind::DoStmt); SourceLoc doLoc; if (shouldSkipDoTokenConsume) { @@ -2080,8 +2005,6 @@ ParserResult Parser::parseStmtDo(LabeledStmtInfo labelInfo, // If the next token is 'catch', this is a 'do'/'catch' statement. if (Tok.is(tok::kw_catch)) { - SyntaxParsingContext CatchListCtxt(SyntaxContext, - SyntaxKind::CatchClauseList); // Parse 'catch' clauses SmallVector allClauses; do { @@ -2150,7 +2073,6 @@ ParserResult Parser::parseStmtDo(LabeledStmtInfo labelInfo, /// This routine promises to return a non-null result unless there was /// a code-completion token in the pattern. ParserResult Parser::parseStmtCatch() { - SyntaxParsingContext CatchClauseCtxt(SyntaxContext, SyntaxKind::CatchClause); // A catch block has its own scope for variables bound out of the pattern. SourceLoc catchLoc = consumeToken(tok::kw_catch); @@ -2160,10 +2082,8 @@ ParserResult Parser::parseStmtCatch() { SmallVector caseLabelItems; { - SyntaxParsingContext ListContext(SyntaxContext, SyntaxKind::CatchItemList); bool isFirst = true; while (true) { - SyntaxParsingContext ItemContext(SyntaxContext, SyntaxKind::CatchItem); GuardedPattern PatternResult; parseGuardedPattern(*this, PatternResult, status, boundDecls, GuardedPatternContext::Catch, isFirst); @@ -2246,7 +2166,6 @@ static bool isStmtForCStyle(Parser &P) { /// (identifier ':')? 'for' pattern 'in' expr-basic \ /// ('where' expr-basic)? stmt-brace ParserResult Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) { - SyntaxContext->setCreateSyntax(SyntaxKind::ForInStmt); SourceLoc ForLoc = consumeToken(tok::kw_for); ParserStatus Status; ParserResult pattern; @@ -2367,8 +2286,6 @@ ParserResult Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) { ParserResult Where; SourceLoc WhereLoc; if (Tok.is(tok::kw_where)) { - SyntaxParsingContext WhereClauseCtxt(SyntaxContext, - SyntaxKind::WhereClause); WhereLoc = consumeToken(); Where = parseExprBasic(diag::expected_foreach_where_expr); if (Where.isNull()) @@ -2395,7 +2312,6 @@ ParserResult Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) { /// stmt-switch: /// (identifier ':')? 'switch' expr-basic '{' stmt-case+ '}' ParserResult Parser::parseStmtSwitch(LabeledStmtInfo LabelInfo) { - SyntaxContext->setCreateSyntax(SyntaxKind::SwitchStmt); SourceLoc SwitchLoc = consumeToken(tok::kw_switch); ParserStatus Status; @@ -2459,7 +2375,6 @@ ParserResult Parser::parseStmtSwitch(LabeledStmtInfo LabelInfo) { ParserStatus Parser::parseStmtCases(SmallVectorImpl &cases, bool IsActive) { - SyntaxParsingContext CasesContext(SyntaxContext, SyntaxKind::SwitchCaseList); ParserStatus Status; while (Tok.isNot(tok::r_brace, tok::eof, tok::pound_endif, tok::pound_elseif, tok::pound_else)) { @@ -2522,18 +2437,13 @@ parseStmtCase(Parser &P, SourceLoc &CaseLoc, SmallVectorImpl &LabelItems, SmallVectorImpl &BoundDecls, SourceLoc &ColonLoc, Optional> &CaseBodyDecls) { - SyntaxParsingContext CaseContext(P.SyntaxContext, - SyntaxKind::SwitchCaseLabel); ParserStatus Status; bool isFirst = true; CaseLoc = P.consumeToken(tok::kw_case); { - SyntaxParsingContext ListContext(P.SyntaxContext, SyntaxKind::CaseItemList); - while (true) { - SyntaxParsingContext ItemContext(P.SyntaxContext, SyntaxKind::CaseItem); GuardedPattern PatternResult; parseGuardedPattern(P, PatternResult, Status, BoundDecls, GuardedPatternContext::Case, isFirst); @@ -2574,8 +2484,6 @@ static ParserStatus parseStmtCaseDefault(Parser &P, SourceLoc &CaseLoc, SmallVectorImpl &LabelItems, SourceLoc &ColonLoc) { - SyntaxParsingContext CaseContext(P.SyntaxContext, - SyntaxKind::SwitchDefaultLabel); ParserStatus Status; CaseLoc = P.consumeToken(tok::kw_default); @@ -2651,8 +2559,6 @@ struct FallthroughFinder : ASTWalker { } // end anonymous namespace ParserResult Parser::parseStmtCase(bool IsActive) { - SyntaxParsingContext CaseContext(SyntaxContext, SyntaxKind::SwitchCase); - ParserStatus Status; SmallVector CaseLabelItems; @@ -2660,8 +2566,6 @@ ParserResult Parser::parseStmtCase(bool IsActive) { SourceLoc UnknownAttrLoc; while (Tok.is(tok::at_sign)) { - SyntaxParsingContext AttrCtx(SyntaxContext, SyntaxKind::Attribute); - if (peekToken().isContextualKeyword("unknown")) { if (!UnknownAttrLoc.isValid()) { UnknownAttrLoc = consumeToken(tok::at_sign); @@ -2672,7 +2576,6 @@ ParserResult Parser::parseStmtCase(bool IsActive) { } consumeToken(tok::identifier); - SyntaxParsingContext Args(SyntaxContext, SyntaxKind::TokenList); if (Tok.is(tok::l_paren)) { diagnose(Tok, diag::unexpected_lparen_in_attribute, "unknown"); skipSingle(); @@ -2684,7 +2587,6 @@ ParserResult Parser::parseStmtCase(bool IsActive) { diagnose(Tok, diag::unknown_attribute, Tok.getText()); consumeToken(tok::identifier); - SyntaxParsingContext Args(SyntaxContext, SyntaxKind::TokenList); if (Tok.is(tok::l_paren)) skipSingle(); } @@ -2740,8 +2642,6 @@ ParserResult Parser::parseStmtCase(bool IsActive) { /// stmt-pound-assert: /// '#assert' '(' expr (',' string_literal)? ')' ParserResult Parser::parseStmtPoundAssert() { - SyntaxContext->setCreateSyntax(SyntaxKind::PoundAssertStmt); - SourceLoc startLoc = consumeToken(tok::pound_assert); SourceLoc endLoc; @@ -2776,8 +2676,6 @@ ParserResult Parser::parseStmtPoundAssert() { return makeParserError(); } - // We check this after consuming everything, so that the SyntaxContext - // understands this statement even when the feature is disabled. if (!Context.LangOpts.hasFeature(Feature::StaticAssert)) { diagnose(startLoc, diag::pound_assert_disabled); return makeParserError(); diff --git a/lib/Parse/ParseType.cpp b/lib/Parse/ParseType.cpp index 916425ae674f2..51ae2a641d47b 100644 --- a/lib/Parse/ParseType.cpp +++ b/lib/Parse/ParseType.cpp @@ -22,9 +22,6 @@ #include "swift/AST/TypeRepr.h" #include "swift/Parse/Lexer.h" #include "swift/Parse/CodeCompletionCallbacks.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Parse/ParsedSyntaxBuilders.h" -#include "swift/Parse/ParsedSyntaxRecorder.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" @@ -32,7 +29,6 @@ #include "llvm/Support/SaveAndRestore.h" using namespace swift; -using namespace swift::syntax; TypeRepr *Parser::applyAttributeToType(TypeRepr *ty, const TypeAttributes &attrs, @@ -230,7 +226,6 @@ ParserResult Parser::parseTypeSimple( SourceLoc metatypeLoc = consumeToken(tok::identifier); ty = makeParserResult(ty, new (Context) MetatypeTypeRepr(ty.get(), metatypeLoc)); - SyntaxContext->createNodeInPlace(SyntaxKind::MetatypeType); continue; } if (peekToken().isContextualKeyword("Protocol")) { @@ -238,7 +233,6 @@ ParserResult Parser::parseTypeSimple( SourceLoc protocolLoc = consumeToken(tok::identifier); ty = makeParserResult(ty, new (Context) ProtocolTypeRepr(ty.get(), protocolLoc)); - SyntaxContext->createNodeInPlace(SyntaxKind::MetatypeType); continue; } } @@ -344,9 +338,6 @@ ParserResult Parser::parseSILBoxType(GenericParamList *generics, ParserResult Parser::parseTypeScalar( Diag<> MessageID, ParseTypeReason reason) { // Start a context for creating type syntax. - SyntaxParsingContext TypeParsingContext(SyntaxContext, - SyntaxContextKind::Type); - ParserStatus status; // Parse attributes. @@ -418,33 +409,6 @@ ParserResult Parser::parseTypeScalar( return status; } - if (SyntaxContext->isEnabled()) { - ParsedFunctionTypeSyntaxBuilder Builder(*SyntaxContext); - Builder.useReturnType(std::move(*SyntaxContext->popIf())); - Builder.useArrow(SyntaxContext->popToken()); - if (throwsLoc.isValid()) - Builder.useThrowsOrRethrowsKeyword(SyntaxContext->popToken()); - if (asyncLoc.isValid()) - Builder.useAsyncKeyword(SyntaxContext->popToken()); - - auto InputNode(std::move(*SyntaxContext->popIf())); - if (InputNode.is()) { - auto TupleTypeNode = std::move(InputNode).castTo(); - // Decompose TupleTypeSyntax and repack into FunctionType. - auto LeftParen = TupleTypeNode.getDeferredLeftParen(SyntaxContext); - auto Arguments = TupleTypeNode.getDeferredElements(SyntaxContext); - auto RightParen = TupleTypeNode.getDeferredRightParen(SyntaxContext); - Builder - .useLeftParen(std::move(LeftParen)) - .useArguments(std::move(Arguments)) - .useRightParen(std::move(RightParen)); - } else { - Builder.addArgumentsMember(ParsedSyntaxRecorder::makeTupleTypeElement( - std::move(InputNode), /*TrailingComma=*/None, *SyntaxContext)); - } - SyntaxContext->addSyntax(Builder.build()); - } - TupleTypeRepr *argsTyR = nullptr; if (auto *TTArgs = dyn_cast(tyR)) { argsTyR = TTArgs; @@ -554,9 +518,6 @@ ParserResult Parser::parseTypeScalar( if (tyR) tyR->walk(walker); } - if (specifierLoc.isValid() || isolatedLoc.isValid() || !attrs.empty() || - constLoc.isValid()) - SyntaxContext->setCreateSyntax(SyntaxKind::AttributedType); return makeParserResult( status, @@ -584,7 +545,6 @@ ParserResult Parser::parseType( SourceLoc ellipsisLoc = consumeToken(); ty = makeParserResult(ty, new (Context) PackExpansionTypeRepr(ty.get(), ellipsisLoc)); - SyntaxContext->createNodeInPlace(SyntaxKind::PackExpansionType); } return ty; @@ -658,20 +618,12 @@ SourceLoc Parser::getTypeErrorLoc() const { ParserStatus Parser::parseGenericArguments(SmallVectorImpl &Args, SourceLoc &LAngleLoc, SourceLoc &RAngleLoc) { - SyntaxParsingContext GenericArgumentsContext( - SyntaxContext, SyntaxKind::GenericArgumentClause); - // Parse the opening '<'. assert(startsWithLess(Tok) && "Generic parameter list must start with '<'"); LAngleLoc = consumeStartingLess(); { - SyntaxParsingContext ListContext(SyntaxContext, - SyntaxKind::GenericArgumentList); - while (true) { - SyntaxParsingContext ElementContext(SyntaxContext, - SyntaxKind::GenericArgument); ParserResult Ty = parseType(diag::expected_type); if (Ty.isNull() || Ty.hasCodeCompletion()) { // Skip until we hit the '>'. @@ -734,8 +686,6 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) { return nullptr; } - SyntaxParsingContext IdentTypeCtxt(SyntaxContext, SyntaxContextKind::Type); - ParserStatus Status; SmallVector ComponentsR; SourceLoc EndLoc; @@ -764,9 +714,6 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) { CompT = new (Context) SimpleIdentTypeRepr(Loc, Name); ComponentsR.push_back(CompT); } - SyntaxContext->createNodeInPlace(ComponentsR.size() == 1 - ? SyntaxKind::SimpleTypeIdentifier - : SyntaxKind::MemberTypeIdentifier); // Treat 'Foo.' as an attempt to write a dotted type // unless is 'Type'. @@ -828,8 +775,6 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) { /// type-composition '&' type-simple ParserResult Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) { - SyntaxParsingContext ConstrainedSugarTypeContext( - SyntaxContext, SyntaxKind::ConstrainedSugarType); // Check for the opaque modifier. // This is only semantically allowed in certain contexts, but we parse it // generally for diagnostics and recovery. @@ -843,11 +788,8 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) { // Treat any as a keyword. TokReceiver->registerTokenKindChange(Tok.getLoc(), tok::contextual_keyword); anyLoc = consumeToken(); - } else { - // This isn't a some type. - ConstrainedSugarTypeContext.setTransparent(); } - + auto applyOpaque = [&](TypeRepr *type) -> TypeRepr * { if (opaqueLoc.isValid() && (anyLoc.isInvalid() || SourceMgr.isBeforeInBuffer(opaqueLoc, anyLoc))) { @@ -858,7 +800,6 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) { return type; }; - SyntaxParsingContext CompositionContext(SyntaxContext, SyntaxContextKind::Type); // Parse the first type ParserResult FirstType = parseTypeSimple(MessageID, reason); if (FirstType.isNull()) @@ -886,24 +827,10 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) { }; addType(FirstType.get()); - SyntaxContext->setCreateSyntax(SyntaxKind::CompositionType); assert(Tok.isContextualPunctuator("&")); do { - if (SyntaxContext->isEnabled()) { - auto Type = SyntaxContext->popIf(); - consumeToken(); // consume '&' - if (Type) { - ParsedCompositionTypeElementSyntaxBuilder Builder(*SyntaxContext); - auto Ampersand = SyntaxContext->popToken(); - Builder - .useAmpersand(std::move(Ampersand)) - .useType(std::move(*Type)); - SyntaxContext->addSyntax(Builder.build()); - } - } else { - consumeToken(); // consume '&' - } - + consumeToken(); // consume '&' + // Diagnose invalid `some` or `any` after an ampersand. if (Tok.isContextualKeyword("some") || Tok.isContextualKeyword("any")) { @@ -941,25 +868,11 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) { addType(ty.getPtrOrNull()); } while (Tok.isContextualPunctuator("&")); - if (SyntaxContext->isEnabled()) { - if (auto synType = SyntaxContext->popIf()) { - auto LastNode = ParsedSyntaxRecorder::makeCompositionTypeElement( - /*UnexpectedNodes=*/None, - /*Type=*/std::move(*synType), - /*UnexpectedNodes=*/None, - /*Ampersand=*/None, /*UnexpectedNodes=*/None, *SyntaxContext); - SyntaxContext->addSyntax(std::move(LastNode)); - } - } - SyntaxContext->collectNodesInPlace(SyntaxKind::CompositionTypeElementList); - return makeParserResult(Status, applyOpaque(CompositionTypeRepr::create( Context, Types, FirstTypeLoc, {FirstAmpersandLoc, PreviousLoc}))); } ParserResult Parser::parseAnyType() { - SyntaxParsingContext IdentTypeCtxt(SyntaxContext, - SyntaxKind::SimpleTypeIdentifier); auto Loc = consumeToken(tok::kw_Any); auto TyR = CompositionTypeRepr::createEmptyComposition(Context, Loc); return makeParserResult(TyR); @@ -976,10 +889,6 @@ ParserResult Parser::parseAnyType() { ParserResult Parser::parseOldStyleProtocolComposition() { assert(Tok.is(tok::kw_protocol) && startsWithLess(peekToken())); - // Start a context for creating type syntax. - SyntaxParsingContext TypeParsingContext(SyntaxContext, - SyntaxContextKind::Type); - SourceLoc ProtocolLoc = consumeToken(); SourceLoc LAngleLoc = consumeStartingLess(); @@ -1082,11 +991,6 @@ static bool isMacroSignatureFile(SourceFile &sf) { /// identifier? identifier ':' type /// type ParserResult Parser::parseTypeTupleBody() { - // Force the context to create deferred nodes, as we might need to - // de-structure the tuple type to create a function type. - DeferringContextRAII Deferring(*SyntaxContext); - SyntaxParsingContext TypeContext(SyntaxContext, SyntaxKind::TupleType); - TypeContext.setCreateSyntax(SyntaxKind::TupleType); Parser::StructureMarkerRAII ParsingTypeTuple(*this, Tok); SourceLoc RPLoc, LPLoc = consumeToken(tok::l_paren); @@ -1095,7 +999,6 @@ ParserResult Parser::parseTypeTupleBody() { ParserStatus Status = parseList(tok::r_paren, LPLoc, RPLoc, /*AllowSepAfterLast=*/false, diag::expected_rparen_tuple_type_list, - SyntaxKind::TupleTypeElementList, [&] () -> ParserStatus { TupleTypeReprElement element; @@ -1172,8 +1075,6 @@ ParserResult Parser::parseTypeTupleBody() { // Parse '= expr' here so we can complain about it directly, rather // than dying when we see it. if (Tok.is(tok::equal)) { - SyntaxParsingContext InitContext(SyntaxContext, - SyntaxKind::InitializerClause); SourceLoc equalLoc = consumeToken(tok::equal); auto init = parseExpr(diag::expected_init_value); auto inFlight = diagnose(equalLoc, diag::tuple_type_init); @@ -1288,7 +1189,6 @@ ParserResult Parser::parseTypeArray(ParserResult Base) { } ParserResult Parser::parseTypeCollection() { - SyntaxParsingContext CollectionCtx(SyntaxContext, SyntaxContextKind::Type); ParserStatus Status; // Parse the leading '['. assert(Tok.is(tok::l_square)); @@ -1327,18 +1227,15 @@ ParserResult Parser::parseTypeCollection() { return makeParserError(); TypeRepr *TyR; - llvm::Optional SyntaxNode; SourceRange brackets(lsquareLoc, rsquareLoc); if (colonLoc.isValid()) { // Form the dictionary type. TyR = new (Context) DictionaryTypeRepr(firstTy.get(), secondTy.get(), colonLoc, brackets); - SyntaxContext->setCreateSyntax(SyntaxKind::DictionaryType); } else { // Form the array type. TyR = new (Context) ArrayTypeRepr(firstTy.get(), brackets); - SyntaxContext->setCreateSyntax(SyntaxKind::ArrayType); } return makeParserResult(Status, TyR); @@ -1392,7 +1289,6 @@ ParserResult Parser::parseTypeOptional(ParserResult base) { SourceLoc questionLoc = consumeOptionalToken(); auto TyR = new (Context) OptionalTypeRepr(base.get(), questionLoc); - SyntaxContext->createNodeInPlace(SyntaxKind::OptionalType); return makeParserResult(ParserStatus(base), TyR); } @@ -1403,7 +1299,6 @@ Parser::parseTypeImplicitlyUnwrappedOptional(ParserResult base) { SourceLoc exclamationLoc = consumeImplicitlyUnwrappedOptionalToken(); auto TyR = new (Context) ImplicitlyUnwrappedOptionalTypeRepr(base.get(), exclamationLoc); - SyntaxContext->createNodeInPlace(SyntaxKind::ImplicitlyUnwrappedOptionalType); return makeParserResult(ParserStatus(base), TyR); } diff --git a/lib/Parse/ParsedRawSyntaxNode.cpp b/lib/Parse/ParsedRawSyntaxNode.cpp deleted file mode 100644 index 0b87874ca8b12..0000000000000 --- a/lib/Parse/ParsedRawSyntaxNode.cpp +++ /dev/null @@ -1,78 +0,0 @@ -//===--- ParsedRawSyntaxNode.cpp - Parsed Raw Syntax Node -----------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/ParsedRawSyntaxNode.h" -#include "swift/Parse/SyntaxParsingContext.h" - -using namespace swift; -using namespace swift::syntax; -using namespace llvm; - -ParsedRawSyntaxNode ParsedRawSyntaxNode::getDeferredChild( - size_t ChildIndex, const SyntaxParsingContext *SyntaxContext) const { - assert(isDeferredLayout()); - return SyntaxContext->getRecorder().getDeferredChild(*this, ChildIndex); -} - -size_t ParsedRawSyntaxNode::getDeferredNumChildren( - const SyntaxParsingContext *SyntaxContext) const { - assert(isDeferredLayout()); - return SyntaxContext->getRecorder().getDeferredNumChildren(*this); -} - -void ParsedRawSyntaxNode::dump() const { - dump(llvm::errs(), /*Indent*/ 0); - llvm::errs() << '\n'; -} - -void ParsedRawSyntaxNode::dump(llvm::raw_ostream &OS, - const SyntaxParsingContext *Context, - unsigned Indent) const { - for (decltype(Indent) i = 0; i < Indent; ++i) - OS << ' '; - OS << '('; - - switch (getDataKind()) { - case DataKind::Null: - OS << ""; - break; - case DataKind::Recorded: - dumpSyntaxKind(OS, getKind()); - OS << " [recorded] "; - if (isToken()) { - dumpTokenKind(OS, getTokenKind()); - } else { - OS << ""; - } - break; - case DataKind::DeferredLayout: - dumpSyntaxKind(OS, getKind()); - OS << " [deferred]"; - if (Context) { - size_t numChildren = getDeferredNumChildren(Context); - for (size_t i = 0; i < numChildren; ++i) { - auto child = getDeferredChild(i, Context); - OS << "\n"; - child.dump(OS, Context, Indent + 2); - } - } else { - OS << " (unknown children)"; - } - break; - case DataKind::DeferredToken: - dumpSyntaxKind(OS, getKind()); - OS << " [deferred] "; - dumpTokenKind(OS, getTokenKind()); - break; - } - OS << ')'; -} diff --git a/lib/Parse/ParsedRawSyntaxRecorder.cpp b/lib/Parse/ParsedRawSyntaxRecorder.cpp deleted file mode 100644 index 78ed9da7cd814..0000000000000 --- a/lib/Parse/ParsedRawSyntaxRecorder.cpp +++ /dev/null @@ -1,125 +0,0 @@ -//===--- ParsedRawSyntaxRecorder.cpp - Raw Syntax Parsing Recorder --------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This file defines the ParsedRawSyntaxRecorder, which is the interface the -// parser is using to pass parsed syntactic elements to a SyntaxParseActions -// receiver and get a ParsedRawSyntaxNode object back. -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/ParsedRawSyntaxRecorder.h" -#include "swift/Parse/ParsedRawSyntaxNode.h" -#include "swift/Parse/ParsedTrivia.h" -#include "swift/Parse/SyntaxParseActions.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Parse/Token.h" -#include "swift/Syntax/SyntaxKind.h" - -using namespace swift; -using namespace swift::syntax; - -ParsedRawSyntaxNode -ParsedRawSyntaxRecorder::recordMissingToken(tok tokenKind, SourceLoc loc) { - OpaqueSyntaxNode n = SPActions->recordMissingToken(tokenKind, loc); - return makeParsedRawSyntaxNode(n, SyntaxKind::Token, tokenKind, - ParsedRawSyntaxNode::DataKind::Recorded, - /*isMissing=*/true, CharSourceRange(loc, 0)); -} - -ParsedRawSyntaxNode -ParsedRawSyntaxRecorder::recordEmptyRawSyntaxCollection(SyntaxKind kind, - SourceLoc loc) { - OpaqueSyntaxNode n = SPActions->recordRawSyntax(kind, {}); - return makeParsedRawSyntaxNode(n, kind, tok::unknown, - ParsedRawSyntaxNode::DataKind::Recorded, - /*IsMissing=*/false, CharSourceRange(loc, 0)); -} - -ParsedRawSyntaxNode -ParsedRawSyntaxRecorder::makeDeferredMissing(tok tokKind, SourceLoc loc) { - auto Data = SPActions->makeDeferredToken( - tokKind, /*leadingTrivia=*/StringRef(), - /*trailingTrivia=*/StringRef(), CharSourceRange(loc, /*Length=*/0), - /*IsMissing=*/true); - return makeParsedRawSyntaxNode(Data, SyntaxKind::Token, tokKind, - ParsedRawSyntaxNode::DataKind::DeferredToken, - /*IsMissing=*/true, - CharSourceRange(loc, /*Length=*/0)); -} - -ParseLookupResult ParsedRawSyntaxRecorder::lookupNode(size_t lexerOffset, - SourceLoc loc, - SyntaxKind kind) { - size_t length; - OpaqueSyntaxNode n; - std::tie(length, n) = SPActions->lookupNode(lexerOffset, kind); - if (length == 0) { - return ParseLookupResult(ParsedRawSyntaxNode::null(), length); - } - return ParseLookupResult( - makeParsedRawSyntaxNode( - n, kind, tok::unknown, ParsedRawSyntaxNode::DataKind::Recorded, - /*IsMissing=*/false, CharSourceRange(loc, unsigned(length))), - length); -} - -ParsedRawSyntaxNode -ParsedRawSyntaxRecorder::getDeferredChild(const ParsedRawSyntaxNode &parent, - size_t childIndex) const { - assert(parent.isDeferredLayout()); - auto childInfo = SPActions->getDeferredChild( - parent.getUnsafeDeferredOpaqueData(), childIndex); - -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES - auto range = SPActions->getDeferredChildRange( - parent.getUnsafeDeferredOpaqueData(), childIndex, - parent.getRange().getStart()); - return ParsedRawSyntaxNode(childInfo.Data, childInfo.SyntaxKind, - childInfo.TokenKind, childInfo.IsMissing, range); -#else - return ParsedRawSyntaxNode(childInfo.Data, childInfo.SyntaxKind, - childInfo.TokenKind, childInfo.IsMissing); -#endif -} - -size_t ParsedRawSyntaxRecorder::getDeferredNumChildren( - const ParsedRawSyntaxNode &node) const { - assert(node.isDeferredLayout()); - return SPActions->getDeferredNumChildren(node.getUnsafeDeferredOpaqueData()); -} - -#ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES -CharSourceRange ParsedRawSyntaxRecorder::verifyElementRanges( - ArrayRef elements) { - SourceLoc startLoc; - unsigned length = 0; - - SourceLoc prevEndLoc; - for (const auto &elem: elements) { - if (elem.isNull() || elem.isMissing()) - continue; - - CharSourceRange range = elem.getRange(); - if (range.isValid()) { - if (startLoc.isInvalid()) { - startLoc = range.getStart(); - } - length += range.getByteLength(); - - assert((prevEndLoc.isInvalid() || range.getStart() == prevEndLoc) - && "Non-contiguous child ranges?"); - prevEndLoc = range.getEnd(); - } - } - return CharSourceRange(startLoc, length); -} -#endif diff --git a/lib/Parse/ParsedSyntaxBuilders.cpp.gyb b/lib/Parse/ParsedSyntaxBuilders.cpp.gyb deleted file mode 100644 index 929fda5341ccd..0000000000000 --- a/lib/Parse/ParsedSyntaxBuilders.cpp.gyb +++ /dev/null @@ -1,128 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From ParsedSyntaxBuilders.cpp.gyb. -//// Do Not Edit Directly! -//===------------- ParsedSyntaxBuilders.cpp - Parsed Syntax Building ------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/ParsedSyntaxBuilders.h" -#include "swift/Parse/ParsedRawSyntaxRecorder.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Syntax/SyntaxNodes.h" - -using namespace swift; -using namespace swift::syntax; - -% for node in SYNTAX_NODES: -% if node.is_buildable(): -% for child in node.children: -% child_elt = None -% child_elt_type = None -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child_node.collection_element_name -% child_elt_type = child_node.collection_element_type -% child_elt_name = child.name + 'Member' -% end -Parsed${node.name}Builder & -Parsed${node.name}Builder::use${child.name}(Parsed${child.type_name} &&${child.name}) { -% if child_elt: - assert(${child_elt_name}s.empty() && "use either 'use' function or 'add', not both"); -% end - Layout[cursorIndex(${node.name}::Cursor::${child.name})] = - ${child.name}.takeRaw(); - return *this; -} -% if child_elt: -Parsed${node.name}Builder & -Parsed${node.name}Builder::add${child_elt_name}(Parsed${child_elt_type} ${child_elt}) { - assert(Layout[cursorIndex(${node.name}::Cursor::${child.name})].isNull() && "use either 'use' function or 'add', not both"); - ${child_elt_name}s.push_back(${child_elt}.takeRaw()); - return *this; -} -% end -% end - -Parsed${node.name} -Parsed${node.name}Builder::record() { - finishLayout(/*deferred=*/false); - auto &Rec = SPCtx.getRecorder(); - auto raw = Rec.recordRawSyntax(SyntaxKind::${node.syntax_kind}, Layout); - return Parsed${node.name}(std::move(raw)); -} - -Parsed${node.name} -Parsed${node.name}Builder::makeDeferred() { - finishLayout(/*deferred=*/true); - auto raw = SPCtx.getRecorder().makeDeferred(SyntaxKind::${node.syntax_kind}, - Layout, SPCtx); - return Parsed${node.name}(std::move(raw)); -} - -Parsed${node.name} -Parsed${node.name}Builder::build() { - if (SPCtx.shouldDefer()) - return makeDeferred(); - return record(); -} - -void Parsed${node.name}Builder::finishLayout(bool deferred) { - auto &Rec = SPCtx.getRecorder(); - (void)Rec; -% if node.children: -% for (idx, child) in enumerate(node.children): -% child_elt = None -% child_elt_name = child.name + 'Member' -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child_node.collection_element_name -% if child_elt: - if (!${child_elt_name}s.empty()) { - if (deferred) { - Layout[${idx}] = Rec.makeDeferred(SyntaxKind::${child_node.syntax_kind}, - ${child_elt_name}s, SPCtx); - } else { - Layout[${idx}] = Rec.recordRawSyntax(SyntaxKind::${child_node.syntax_kind}, ${child_elt_name}s); - } - } -% end -% if not child.is_optional: - if (Layout[${idx}].isNull()) { -% if child.is_token(): -% token = child.main_token() -% tok_kind = token.kind if token else "unknown" - if (deferred) { - Layout[${idx}] = Rec.makeDeferredMissing(tok::${tok_kind}, SourceLoc()); - } else { - Layout[${idx}] = Rec.recordMissingToken(tok::${tok_kind}, SourceLoc()); - } -% elif child_elt: - if (deferred) { - Layout[${idx}] = Rec.makeDeferred(SyntaxKind::${child_node.syntax_kind}, {}, SPCtx); - } else { - Layout[${idx}] = Rec.recordRawSyntax(SyntaxKind::${child_node.syntax_kind}, {}); - } -% else: - llvm_unreachable("need missing non-token nodes ?"); -% end - } -% end -% end -% end -} - -% end -% end diff --git a/lib/Parse/ParsedSyntaxNodes.cpp.gyb b/lib/Parse/ParsedSyntaxNodes.cpp.gyb deleted file mode 100644 index d16b7772d5982..0000000000000 --- a/lib/Parse/ParsedSyntaxNodes.cpp.gyb +++ /dev/null @@ -1,43 +0,0 @@ -%{ - from gyb_syntax_support import * - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From ParsedSyntaxNodes.cpp.gyb. -//// Do Not Edit Directly! -//===--- ParsedSyntaxNodes.cpp - Parsed Syntax Node definitions -----------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/ParsedSyntaxNodes.h" -#include "swift/Syntax/SyntaxNodes.h" - -using namespace swift; -using namespace swift::syntax; - -% for node in SYNTAX_NODES: -% for child in node.children: -% if child.is_optional: -Optional -Parsed${node.name}::getDeferred${child.name}(const SyntaxParsingContext *SyntaxContext) { - auto RawChild = getRaw().getDeferredChild(${node.name}::Cursor::${child.name}, SyntaxContext); - if (RawChild.isNull()) - return None; - return Parsed${child.type_name}(std::move(RawChild)); -} -% else: -Parsed${child.type_name} Parsed${node.name}::getDeferred${child.name}(const SyntaxParsingContext *SyntaxContext) { - return Parsed${child.type_name} {getRaw().getDeferredChild(${node.name}::Cursor::${child.name}, SyntaxContext)}; -} -% end - -% end -% end diff --git a/lib/Parse/ParsedSyntaxRecorder.cpp.gyb b/lib/Parse/ParsedSyntaxRecorder.cpp.gyb deleted file mode 100644 index 1d6bd7b1b688b..0000000000000 --- a/lib/Parse/ParsedSyntaxRecorder.cpp.gyb +++ /dev/null @@ -1,195 +0,0 @@ -%{ - from gyb_syntax_support import * - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From ParsedSyntaxRecorder.cpp.gyb. -//// Do Not Edit Directly! -//===--- ParsedSyntaxRecorder.cpp - Parsed Syntax Recorder ----------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/ParsedSyntaxRecorder.h" -#include "swift/Parse/ParsedRawSyntaxRecorder.h" -#include "swift/Parse/SyntaxParsingContext.h" -#include "swift/Syntax/SyntaxFactory.h" - -using namespace swift; -using namespace swift::syntax; - -bool ParsedSyntaxRecorder::formExactLayoutFor(syntax::SyntaxKind Kind, - MutableArrayRef Elements, - function_ref)> receiver) { - switch (Kind) { -% for node in SYNTAX_NODES: - case SyntaxKind::${node.syntax_kind}: { -% if node.children: -% child_count = len(node.children) - ParsedRawSyntaxNode Layout[${child_count}]; - unsigned I = 0; -% for (child_idx, child) in enumerate(node.children): - // child[${child_idx}] ${child.name} - if (I == Elements.size() || - !${check_parsed_child_condition_raw(child)}(Elements[I])) { -% if child.is_optional: - Layout[${child_idx}] = ParsedRawSyntaxNode::null(); -% else: - for (unsigned i = 0, e = ${child_count}; i != e; ++i) - Layout[i].reset(); - return false; -% end - } else { - Layout[${child_idx}] = Elements[I].unsafeCopy(); - ++I; - } -% end - if (I != Elements.size()) { - for (unsigned i = 0, e = ${child_count}; i != e; ++i) - Layout[i].reset(); - return false; - } - receiver(Kind, Layout); - for (unsigned i = 0, e = Elements.size(); i != e; ++i) - Elements[i].reset(); - return true; -% elif node.is_syntax_collection(): - for (auto &E : Elements) { - if (!SyntaxFactory::canServeAsCollectionMemberRaw(SyntaxKind::${node.syntax_kind}, E.getKind())) - return false; - } - receiver(Kind, Elements); - return true; -% else: - return false; -% end - } -% end - default: - return false; - } -} - -% for node in SYNTAX_NODES: -% if node.children: -% child_params = [] -% child_move_args = [] -% for child in node.children: -% param_type = "Parsed%s" % child.type_name -% if child.is_optional: -% param_type = "Optional<%s>" % param_type -% child_params.append("%s &&%s" % (param_type, child.name)) -% child_move_args.append("std::move(%s)" % (child.name)) -% child_params = ', '.join(child_params) -% child_move_args = ', '.join(child_move_args) -Parsed${node.name} -ParsedSyntaxRecorder::record${node.syntax_kind}(MutableArrayRef layout, - ParsedRawSyntaxRecorder &rec) { - auto raw = rec.recordRawSyntax(SyntaxKind::${node.syntax_kind}, layout); - return Parsed${node.name}(std::move(raw)); -} - -Parsed${node.name} -ParsedSyntaxRecorder::defer${node.syntax_kind}(MutableArrayRef layout, SyntaxParsingContext &SPCtx) { - auto raw = SPCtx.getRecorder().makeDeferred(SyntaxKind::${node.syntax_kind}, - layout, SPCtx); - return Parsed${node.name}(std::move(raw)); -} - -Parsed${node.name} -ParsedSyntaxRecorder::make${node.syntax_kind}(${child_params}, - SyntaxParsingContext &SPCtx) { - ParsedRawSyntaxNode layout[] = { -% for child in node.children: -% if child.is_optional: - ${child.name}.hasValue() ? ${child.name}->takeRaw() : ParsedRawSyntaxNode::null(), -% else: - ${child.name}.takeRaw(), -% end -% end - }; - if (SPCtx.shouldDefer()) - return defer${node.syntax_kind}(layout, SPCtx); - return record${node.syntax_kind}(layout, SPCtx.getRecorder()); -} - -% elif node.is_syntax_collection(): -Parsed${node.name} -ParsedSyntaxRecorder::record${node.syntax_kind}( - MutableArrayRef layout, - ParsedRawSyntaxRecorder &rec) { - auto raw = rec.recordRawSyntax(SyntaxKind::${node.syntax_kind}, layout); - return Parsed${node.name}(std::move(raw)); -} - -Parsed${node.name} -ParsedSyntaxRecorder::defer${node.syntax_kind}( - MutableArrayRef layout, - SyntaxParsingContext &SPCtx) { - auto raw = SPCtx.getRecorder().makeDeferred(SyntaxKind::${node.syntax_kind}, - layout, SPCtx); - return Parsed${node.name}(std::move(raw)); -} - -Parsed${node.name} -ParsedSyntaxRecorder::make${node.syntax_kind}( - MutableArrayRef elements, - SyntaxParsingContext &SPCtx) { - SmallVector layout; - layout.reserve(elements.size()); - for (auto &element : elements) { - layout.push_back(element.takeRaw()); - } - if (SPCtx.shouldDefer()) - return defer${node.syntax_kind}(layout, SPCtx); - return record${node.syntax_kind}(layout, SPCtx.getRecorder()); -} - -Parsed${node.name} -ParsedSyntaxRecorder::makeBlank${node.syntax_kind}(SourceLoc loc, - SyntaxParsingContext &SPCtx) { - ParsedRawSyntaxNode raw; - if (SPCtx.shouldDefer()) { - // FIXME: 'loc' is not preserved when capturing a deferred layout. - raw = SPCtx.getRecorder().makeDeferred(SyntaxKind::${node.syntax_kind}, - {}, SPCtx); - } else { - raw = SPCtx.getRecorder().recordEmptyRawSyntaxCollection(SyntaxKind::${node.syntax_kind}, loc); - } - return Parsed${node.name}(std::move(raw)); -} -% end -% end - -ParsedTupleTypeElementSyntax -ParsedSyntaxRecorder::makeTupleTypeElement(ParsedTypeSyntax Type, - llvm::Optional TrailingComma, - SyntaxParsingContext &SPCtx) { - return makeTupleTypeElement( - /*UnexpectedNodes=*/None, - /*InOut=*/None, - /*UnexpectedNodes=*/None, - /*Name=*/None, - /*UnexpectedNodes=*/None, - /*SecondName=*/None, - /*UnexpectedNodes=*/None, - /*Colon=*/None, - /*UnexpectedNodes=*/None, - std::move(Type), - /*UnexpectedNodes=*/None, - /*Ellipsis=*/None, - /*UnexpectedNodes=*/None, - /*Initializer=*/None, - /*UnexpectedNodes=*/None, - std::move(TrailingComma), - /*UnexpectedNodes=*/None, - SPCtx - ); -} diff --git a/lib/Parse/ParsedTrivia.cpp b/lib/Parse/ParsedTrivia.cpp deleted file mode 100644 index 7064ba21e6c40..0000000000000 --- a/lib/Parse/ParsedTrivia.cpp +++ /dev/null @@ -1,40 +0,0 @@ -//===--- ParsedTrivia.cpp - ParsedTrivia API ----------------------------*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/ParsedTrivia.h" -#include "swift/Syntax/Trivia.h" -#include "swift/Basic/SourceManager.h" - -using namespace swift; -using namespace swift::syntax; - -Trivia -ParsedTriviaPiece::convertToSyntaxTrivia(ArrayRef pieces, - SourceLoc loc, - const SourceManager &SM, - unsigned bufferID) { - Trivia trivia; - SourceLoc curLoc = loc; - for (const auto &piece : pieces) { - CharSourceRange range{curLoc, piece.getLength()}; - StringRef text = SM.extractText(range, bufferID); - trivia.push_back(TriviaPiece::fromText(piece.getKind(), text)); - curLoc = curLoc.getAdvancedLoc(piece.getLength()); - } - return trivia; -} - -Trivia -ParsedTrivia::convertToSyntaxTrivia(SourceLoc loc, const SourceManager &SM, - unsigned bufferID) const { - return ParsedTriviaPiece::convertToSyntaxTrivia(Pieces, loc, SM, bufferID); -} diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp index bfa9b28e040b4..9db7f04e42b6f 100644 --- a/lib/Parse/Parser.cpp +++ b/lib/Parse/Parser.cpp @@ -27,12 +27,7 @@ #include "swift/Parse/Lexer.h" #include "swift/Parse/CodeCompletionCallbacks.h" #include "swift/Parse/ParseSILSupport.h" -#include "swift/Parse/SyntaxParseActions.h" -#include "swift/Parse/SyntaxParsingContext.h" #include "swift/SymbolGraphGen/SymbolGraphOptions.h" -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/TokenSyntax.h" -#include "swift/SyntaxParse/SyntaxTreeCreator.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" @@ -51,18 +46,13 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM, unsigned BufferID, unsigned Offset, unsigned EndOffset, DiagnosticEngine * Diags, CommentRetentionMode RetainComments, - TriviaRetentionMode TriviaRetention, bool TokenizeInterpolatedString, ArrayRef SplitTokens, DF &&DestFunc) { - assert((TriviaRetention != TriviaRetentionMode::WithTrivia || - !TokenizeInterpolatedString) && - "string interpolation with trivia is not implemented yet"); - if (Offset == 0 && EndOffset == 0) EndOffset = SM.getRangeForBuffer(BufferID).getByteLength(); Lexer L(LangOpts, SM, BufferID, Diags, LexerMode::Swift, - HashbangMode::Allowed, RetainComments, TriviaRetention, Offset, + HashbangMode::Allowed, RetainComments, Offset, EndOffset); auto TokComp = [&](const Token &A, const Token &B) { @@ -75,9 +65,8 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM, } Token Tok; - StringRef LeadingTrivia, TrailingTrivia; do { - L.lex(Tok, LeadingTrivia, TrailingTrivia); + L.lex(Tok); // If the token has the same location as a reset location, // reset the token stream @@ -85,8 +74,7 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM, if (F != ResetTokens.end()) { assert(F->isNot(tok::string_literal)); - DestFunc(*F, /*LeadingTrivia=*/StringRef(), - /*TrailingTrivia=*/StringRef()); + DestFunc(*F); auto NewState = L.getStateForBeginningOfTokenLoc( F->getLoc().getAdvancedLoc(F->getLength())); @@ -98,11 +86,10 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM, std::vector StrTokens; getStringPartTokens(Tok, LangOpts, SM, BufferID, StrTokens); for (auto &StrTok : StrTokens) { - DestFunc(StrTok, /*LeadingTrivia=*/StringRef(), - /*TrailingTrivia=*/StringRef()); + DestFunc(StrTok); } } else { - DestFunc(Tok, LeadingTrivia, TrailingTrivia); + DestFunc(Tok); } } while (Tok.getKind() != tok::eof); @@ -110,7 +97,6 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM, } // namespace swift using namespace swift; -using namespace swift::syntax; using ParsingFlags = SourceFile::ParsingFlags; void SILParserStateBase::anchor() { } @@ -134,7 +120,7 @@ bool CodeCompletionSecondPassRequest::evaluate( auto &Ctx = SF->getASTContext(); auto BufferID = Ctx.SourceMgr.getCodeCompletionBufferID(); - Parser TheParser(BufferID, *SF, nullptr, parserState, nullptr); + Parser TheParser(BufferID, *SF, nullptr, parserState); std::unique_ptr CodeCompletion( Factory->createCodeCompletionCallbacks(TheParser)); @@ -146,9 +132,6 @@ bool CodeCompletionSecondPassRequest::evaluate( void Parser::performCodeCompletionSecondPassImpl( CodeCompletionDelayedDeclState &info) { - // Disable libSyntax creation in the delayed parsing. - SyntaxContext->disable(); - // Disable updating the interface hash llvm::SaveAndRestore> CurrentTokenHashSaver( CurrentTokenHash, None); @@ -229,10 +212,6 @@ void swift::Parser::CancellableBacktrackingScope::cancelBacktrack() { return; Backtrack = false; - SynContext->cancelBacktrack(); - SynContext->setTransparent(); - if (SynContext->isTopOfContextStack()) - SynContext.reset(); DT.commit(); TempReceiver.shouldTransfer = true; } @@ -314,49 +293,15 @@ std::vector swift::tokenize(const LangOptions &LangOpts, tokenize(LangOpts, SM, BufferID, Offset, EndOffset, Diags, KeepComments ? CommentRetentionMode::ReturnAsTokens : CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithoutTrivia, TokenizeInterpolatedString, + TokenizeInterpolatedString, SplitTokens, - [&](const Token &Tok, StringRef LeadingTrivia, - StringRef TrailingTrivia) { Tokens.push_back(Tok); }); + [&](const Token &Tok) { Tokens.push_back(Tok); }); assert(Tokens.back().is(tok::eof)); Tokens.pop_back(); // Remove EOF. return Tokens; } -std::vector< - std::pair> -swift::tokenizeWithTrivia(const LangOptions &LangOpts, const SourceManager &SM, - unsigned BufferID, const RC &Arena, - unsigned Offset, unsigned EndOffset, - DiagnosticEngine *Diags) { - std::vector< - std::pair> - Tokens; - syntax::AbsoluteOffsetPosition RunningPos(0); - - tokenize( - LangOpts, SM, BufferID, Offset, EndOffset, Diags, - CommentRetentionMode::AttachToNextToken, TriviaRetentionMode::WithTrivia, - /*TokenizeInterpolatedString=*/false, - /*SplitTokens=*/ArrayRef(), - [&](const Token &Tok, StringRef LeadingTrivia, StringRef TrailingTrivia) { - CharSourceRange TokRange = Tok.getRange(); - size_t TextLength = LeadingTrivia.size() + TokRange.getByteLength() + - TrailingTrivia.size(); - auto ThisToken = RawSyntax::make( - Tok.getKind(), Tok.getRawText(), TextLength, LeadingTrivia, - TrailingTrivia, SourcePresence::Present, Arena); - - auto ThisTokenPos = - RunningPos.advancedBy(ThisToken->getLeadingTriviaLength()); - Tokens.push_back({ThisToken, ThisTokenPos}); - RunningPos = RunningPos.advancedBy(ThisToken->getTextLength()); - }); - - return Tokens; -} - //===----------------------------------------------------------------------===// // Setup and Helper Methods //===----------------------------------------------------------------------===// @@ -377,15 +322,12 @@ static LexerMode sourceFileKindToLexerMode(SourceFileKind kind) { } Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserStateBase *SIL, - PersistentParserState *PersistentState, - std::shared_ptr SPActions) - : Parser(BufferID, SF, &SF.getASTContext().Diags, SIL, PersistentState, - std::move(SPActions)) {} + PersistentParserState *PersistentState) + : Parser(BufferID, SF, &SF.getASTContext().Diags, SIL, PersistentState) {} Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags, SILParserStateBase *SIL, - PersistentParserState *PersistentState, - std::shared_ptr SPActions) + PersistentParserState *PersistentState) : Parser( std::unique_ptr(new Lexer( SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr, @@ -396,11 +338,8 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, DiagnosticEngine* LexerDiags, : HashbangMode::Disallowed, SF.getASTContext().LangOpts.AttachCommentsToDecls ? CommentRetentionMode::AttachToNextToken - : CommentRetentionMode::None, - SF.shouldBuildSyntaxTree() - ? TriviaRetentionMode::WithTrivia - : TriviaRetentionMode::WithoutTrivia)), - SF, SIL, PersistentState, std::move(SPActions)) {} + : CommentRetentionMode::None)), + SF, SIL, PersistentState) {} namespace { @@ -441,7 +380,6 @@ class TokenRecorder: public ConsumeTokenReceiver { } Lexer L(Ctx.LangOpts, SM, BufferID, nullptr, LexerMode::Swift, HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens, - TriviaRetentionMode::WithoutTrivia, SM.getLocOffsetInBuffer(CommentRange.getStart(), BufferID), EndOffset); while(true) { @@ -528,16 +466,13 @@ class TokenRecorder: public ConsumeTokenReceiver { } // End of an anonymous namespace. Parser::Parser(std::unique_ptr Lex, SourceFile &SF, - SILParserStateBase *SIL, PersistentParserState *PersistentState, - std::shared_ptr SPActions) + SILParserStateBase *SIL, PersistentParserState *PersistentState) : SourceMgr(SF.getASTContext().SourceMgr), Diags(SF.getASTContext().Diags), SF(SF), L(Lex.release()), SIL(SIL), CurDeclContext(&SF), Context(SF.getASTContext()), TokReceiver(SF.shouldCollectTokens() ? new TokenRecorder(SF.getASTContext(), *L) - : new ConsumeTokenReceiver()), - SyntaxContext(new SyntaxParsingContext( - SyntaxContext, SF, L->getBufferID(), std::move(SPActions))) { + : new ConsumeTokenReceiver()) { State = PersistentState; if (!State) { OwnedState.reset(new PersistentParserState()); @@ -556,7 +491,6 @@ Parser::Parser(std::unique_ptr Lex, SourceFile &SF, Parser::~Parser() { delete L; delete TokReceiver; - delete SyntaxContext; } bool Parser::isInSILMode() const { return SF.Kind == SourceFileKind::SIL; } @@ -585,7 +519,7 @@ const Token &Parser::peekToken() { SourceLoc Parser::discardToken() { assert(Tok.isNot(tok::eof) && "Lexing past eof!"); SourceLoc Loc = Tok.getLoc(); - L->lex(Tok, LeadingTrivia, TrailingTrivia); + L->lex(Tok); return Loc; } @@ -611,7 +545,6 @@ void Parser::consumeExtraToken(Token Extra) { SourceLoc Parser::consumeToken() { TokReceiver->receive(Tok); - SyntaxContext->addToken(Tok, LeadingTrivia, TrailingTrivia); return consumeTokenWithoutFeedingReceiver(); } @@ -646,7 +579,6 @@ SourceLoc Parser::consumeStartingCharacterOfCurrentToken(tok Kind, size_t Len) { void Parser::markSplitToken(tok Kind, StringRef Txt) { SplitTokens.emplace_back(); SplitTokens.back().setToken(Kind, Txt); - SyntaxContext->addToken(SplitTokens.back(), LeadingTrivia, StringRef()); TokReceiver->receive(SplitTokens.back()); } @@ -748,7 +680,7 @@ SourceLoc Parser::skipUntilGreaterInTypeList(bool protocolComposition) { #define KEYWORD(X) case tok::kw_##X: #define POUND_KEYWORD(X) case tok::pound_##X: -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" // 'Self' can appear in types, skip it. if (Tok.is(tok::kw_Self)) break; @@ -840,26 +772,6 @@ bool Parser::skipUntilTokenOrEndOfLine(tok T1, tok T2) { return Tok.isAny(T1, T2) && !Tok.isAtStartOfLine(); } -bool Parser::loadCurrentSyntaxNodeFromCache() { - // Don't do a cache lookup when not building a syntax tree since otherwise - // the corresponding AST nodes do not get created - if (!SF.shouldBuildSyntaxTree()) { - return false; - } - unsigned LexerOffset = - SourceMgr.getLocOffsetInBuffer(Tok.getLoc(), L->getBufferID()); - unsigned LeadingTriviaLen = LeadingTrivia.size(); - unsigned LeadingTriviaOffset = LexerOffset - LeadingTriviaLen; - SourceLoc LeadingTriviaLoc = Tok.getLoc().getAdvancedLoc(-LeadingTriviaLen); - if (auto TextLength = SyntaxContext->lookupNode(LeadingTriviaOffset, - LeadingTriviaLoc)) { - L->resetToOffset(LeadingTriviaOffset + TextLength); - L->lex(Tok, LeadingTrivia, TrailingTrivia); - return true; - } - return false; -} - bool Parser::parseEndIfDirective(SourceLoc &Loc) { Loc = Tok.getLoc(); if (parseToken(tok::pound_endif, diag::expected_close_to_if_directive)) { @@ -1040,29 +952,6 @@ SourceLoc Parser::getErrorOrMissingLoc() const { return PreviousLoc; } -static SyntaxKind getListElementKind(SyntaxKind ListKind) { - switch (ListKind) { - case SyntaxKind::TupleExprElementList: - return SyntaxKind::TupleExprElement; - case SyntaxKind::ArrayElementList: - return SyntaxKind::ArrayElement; - case SyntaxKind::DictionaryElementList: - return SyntaxKind::DictionaryElement; - case SyntaxKind::FunctionParameterList: - return SyntaxKind::FunctionParameter; - case SyntaxKind::TupleTypeElementList: - return SyntaxKind::TupleTypeElement; - case SyntaxKind::TuplePatternElementList: - return SyntaxKind::TuplePatternElement; - case SyntaxKind::AvailabilitySpecList: - return SyntaxKind::AvailabilityArgument; - case SyntaxKind::YieldExprList: - return SyntaxKind::YieldExprListElement; - default: - return SyntaxKind::Unknown; - } -} - static bool tokIsStringInterpolationEOF(Token &Tok, tok RightK) { return Tok.is(tok::eof) && Tok.getText() == ")" && RightK == tok::r_paren; } @@ -1070,7 +959,6 @@ static bool tokIsStringInterpolationEOF(Token &Tok, tok RightK) { Parser::ParseListItemResult Parser::parseListItem(ParserStatus &Status, tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc, bool AllowSepAfterLast, - SyntaxKind ElementKind, llvm::function_ref callback) { while (Tok.is(tok::comma)) { diagnose(Tok, diag::unexpected_separator, ",").fixItRemove(Tok.getLoc()); @@ -1078,9 +966,6 @@ Parser::parseListItem(ParserStatus &Status, tok RightK, SourceLoc LeftLoc, } SourceLoc StartLoc = Tok.getLoc(); - SyntaxParsingContext ElementContext(SyntaxContext, ElementKind); - if (ElementKind == SyntaxKind::Unknown) - ElementContext.setTransparent(); Status |= callback(); if (Tok.is(RightK)) return ParseListItemResult::Finished; @@ -1127,17 +1012,9 @@ Parser::parseListItem(ParserStatus &Status, tok RightK, SourceLoc LeftLoc, ParserStatus Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc, - bool AllowSepAfterLast, Diag<> ErrorDiag, SyntaxKind Kind, + bool AllowSepAfterLast, Diag<> ErrorDiag, llvm::function_ref callback) { - llvm::Optional ListContext; - ListContext.emplace(SyntaxContext, Kind); - if (Kind == SyntaxKind::Unknown) - ListContext->setTransparent(); - - SyntaxKind ElementKind = getListElementKind(Kind); - if (Tok.is(RightK)) { - ListContext.reset(); RightLoc = consumeToken(RightK); return makeParserSuccess(); } @@ -1150,15 +1027,13 @@ Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc, ParseListItemResult Result; do { Result = parseListItem(Status, RightK, LeftLoc, RightLoc, AllowSepAfterLast, - ElementKind, callback); + callback); } while (Result == ParseListItemResult::Continue); if (Result == ParseListItemResult::FinishedInStringInterpolation) { return Status; } - ListContext.reset(); - if (Status.isErrorOrHasCompletion()) { // If we've already got errors, don't emit missing RightK diagnostics. if (Tok.is(RightK)) { @@ -1225,7 +1100,6 @@ bool Parser::shouldReturnSingleExpressionElement(ArrayRef Body) { } struct ParserUnit::Implementation { - std::shared_ptr SPActions; LangOptions LangOpts; TypeCheckerOptions TypeCheckerOpts; SILOptions SILOpts; @@ -1239,9 +1113,8 @@ struct ParserUnit::Implementation { Implementation(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID, const LangOptions &Opts, const TypeCheckerOptions &TyOpts, - const SILOptions &silOpts, StringRef ModuleName, - std::shared_ptr spActions) - : SPActions(std::move(spActions)), LangOpts(Opts), + const SILOptions &silOpts, StringRef ModuleName) + : LangOpts(Opts), TypeCheckerOpts(TyOpts), SILOpts(silOpts), Diags(SM), Ctx(*ASTContext::get(LangOpts, TypeCheckerOpts, SILOpts, SearchPathOpts, clangImporterOpts, symbolGraphOpts, SM, Diags)) { @@ -1255,8 +1128,6 @@ struct ParserUnit::Implementation { } ~Implementation() { - // We need to delete the parser before the context so that it can finalize - // its SourceFileSyntax while it is still alive TheParser.reset(); delete &Ctx; } @@ -1270,22 +1141,17 @@ ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind, ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID, const LangOptions &LangOpts, const TypeCheckerOptions &TypeCheckOpts, - const SILOptions &SILOpts, StringRef ModuleName, - std::shared_ptr spActions, - SyntaxParsingCache *SyntaxCache) + const SILOptions &SILOpts, StringRef ModuleName) : Impl(*new Implementation(SM, SFKind, BufferID, LangOpts, TypeCheckOpts, - SILOpts, ModuleName, std::move(spActions))) { - - Impl.SF->SyntaxParsingCache = SyntaxCache; + SILOpts, ModuleName)) { Impl.TheParser.reset(new Parser(BufferID, *Impl.SF, /*SIL=*/nullptr, - /*PersistentState=*/nullptr, Impl.SPActions)); + /*PersistentState=*/nullptr)); } ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID, unsigned Offset, unsigned EndOffset) : Impl(*new Implementation(SM, SFKind, BufferID, LangOptions(), - TypeCheckerOptions(), SILOptions(), "input", - nullptr)) { + TypeCheckerOptions(), SILOptions(), "input")) { std::unique_ptr Lex; Lex.reset(new Lexer(Impl.LangOpts, SM, @@ -1293,17 +1159,16 @@ ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind, LexerMode::Swift, HashbangMode::Allowed, CommentRetentionMode::None, - TriviaRetentionMode::WithoutTrivia, Offset, EndOffset)); Impl.TheParser.reset(new Parser(std::move(Lex), *Impl.SF, /*SIL=*/nullptr, - /*PersistentState=*/nullptr, Impl.SPActions)); + /*PersistentState=*/nullptr)); } ParserUnit::~ParserUnit() { delete &Impl; } -OpaqueSyntaxNode ParserUnit::parse() { +void ParserUnit::parse() { auto &P = getParser(); auto &ctx = P.Context; @@ -1314,17 +1179,9 @@ OpaqueSyntaxNode ParserUnit::parse() { if (auto tokens = P.takeTokenReceiver()->finalize()) tokensRef = ctx.AllocateCopy(*tokens); - auto rawNode = P.finalizeSyntaxTree(); - Optional syntaxRoot; - if (Impl.SPActions) { - if (auto root = Impl.SPActions->realizeSyntaxRoot(rawNode, *Impl.SF)) - syntaxRoot.emplace(*root); - } - auto result = SourceFileParsingResult{ctx.AllocateCopy(items), tokensRef, - P.CurrentTokenHash, syntaxRoot}; + P.CurrentTokenHash}; ctx.evaluator.cacheOutput(ParseSourceFileRequest{&P.SF}, std::move(result)); - return rawNode; } Parser &ParserUnit::getParser() { diff --git a/lib/Parse/SyntaxParsingCache.cpp b/lib/Parse/SyntaxParsingCache.cpp deleted file mode 100644 index de85927c176bc..0000000000000 --- a/lib/Parse/SyntaxParsingCache.cpp +++ /dev/null @@ -1,164 +0,0 @@ -//===--- SyntaxParsingCache.cpp - Incremental syntax parsing lookup--------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/SyntaxParsingCache.h" -#include "swift/Syntax/SyntaxVisitor.h" - -using namespace swift; -using namespace swift::syntax; - -void SyntaxParsingCache::addEdit(size_t Start, size_t End, - size_t ReplacementLength) { - assert((Edits.empty() || Edits.back().End <= Start) && - "'Start' must be greater than or equal to 'End' of the previous edit"); - Edits.emplace_back(Start, End, ReplacementLength); -} - -bool SyntaxParsingCache::nodeCanBeReused(const Syntax &Node, size_t NodeStart, - size_t Position, - SyntaxKind Kind) const { - // Computing the value of NodeStart on the fly is faster than determining a - // node's absolute position, but make sure the values match in an assertion - // build - assert(NodeStart == Node.getAbsolutePositionBeforeLeadingTrivia().getOffset()); - - if (NodeStart != Position) - return false; - if (Node.getKind() != Kind) - return false; - - // Node can also not be reused if an edit has been made in the next token's - // text, e.g. because `private struct Foo {}` parses as a CodeBlockItem with a - // StructDecl inside and `private struc Foo {}` parses as two CodeBlockItems - // one for `private` and one for `struc Foo {}` - size_t NextLeafNodeLength = 0; - if (auto NextNode = Node.getData()->getNextNode()) { - auto NextLeafNode = NextNode->getFirstToken(); - auto NextRawNode = NextLeafNode->getRaw(); - assert(NextRawNode->isPresent()); - NextLeafNodeLength += NextRawNode->getTokenText().size(); - NextLeafNodeLength += NextRawNode->getLeadingTriviaLength(); - } - - auto NodeEnd = NodeStart + Node.getTextLength(); - for (auto Edit : Edits) { - // Check if this node or the trivia of the next node has been edited. If it - // has, we cannot reuse it. - if (Edit.intersectsOrTouchesRange(NodeStart, NodeEnd + NextLeafNodeLength)) - return false; - } - - return true; -} - -llvm::Optional SyntaxParsingCache::lookUpFrom(const Syntax &Node, - size_t NodeStart, - size_t Position, - SyntaxKind Kind) { - if (nodeCanBeReused(Node, NodeStart, Position, Kind)) { - return Node; - } - - // Compute the child's position on the fly - size_t ChildStart = NodeStart; - for (size_t I = 0, E = Node.getNumChildren(); I < E; ++I) { - llvm::Optional Child = Node.getChild(I); - if (!Child.hasValue() || Child->isMissing()) { - continue; - } - auto ChildEnd = ChildStart + Child->getTextLength(); - if (ChildStart <= Position && Position < ChildEnd) { - return lookUpFrom(Child.getValue(), ChildStart, Position, Kind); - } - // The next child starts where the previous child ended - ChildStart = ChildEnd; - } - return llvm::None; -} - -Optional -SyntaxParsingCache::translateToPreEditPosition(size_t PostEditPosition, - ArrayRef Edits) { - size_t Position = PostEditPosition; - for (auto &Edit : Edits) { - if (Edit.Start > Position) - // Remaining edits doesn't affect the position. (Edits are sorted) - break; - if (Edit.Start + Edit.ReplacementLength > Position) - // This is a position inserted by the edit, and thus doesn't exist in the - // pre-edit version of the file. - return None; - - Position = Position - Edit.ReplacementLength + Edit.originalLength(); - } - return Position; -} - -llvm::Optional SyntaxParsingCache::lookUp(size_t NewPosition, - SyntaxKind Kind) { - Optional OldPosition = translateToPreEditPosition(NewPosition, Edits); - if (!OldPosition.hasValue()) - return None; - - auto Node = lookUpFrom(OldSyntaxTree, /*NodeStart=*/0, *OldPosition, Kind); - if (Node.hasValue()) { - ReusedNodes.insert(Node->getRaw()); - } - return Node; -} - -std::vector -SyntaxParsingCache::getReusedRegions(const SourceFileSyntax &SyntaxTree) const { - /// Determines the reused source regions from reused syntax node IDs - class ReusedRegionsCollector : public SyntaxVisitor { - std::unordered_set ReusedNodes; - std::vector ReusedRegions; - - bool didReuseNode(const RawSyntax *Node) { - return ReusedNodes.count(Node) > 0; - } - - public: - ReusedRegionsCollector(std::unordered_set ReusedNodes) - : ReusedNodes(ReusedNodes) {} - - const std::vector &getReusedRegions() { - std::sort(ReusedRegions.begin(), ReusedRegions.end(), - [](const SyntaxReuseRegion &Lhs, - const SyntaxReuseRegion &Rhs) -> bool { - return Lhs.Start.getOffset() < Rhs.Start.getOffset(); - }); - return ReusedRegions; - } - - void visit(Syntax Node) override { - if (didReuseNode(Node.getRaw())) { - // Node has been reused, add it to the list - auto Start = Node.getAbsolutePositionBeforeLeadingTrivia(); - auto End = Node.getAbsoluteEndPositionAfterTrailingTrivia(); - ReusedRegions.push_back({Start, End}); - } else { - SyntaxVisitor::visit(Node); - } - } - - void collectReusedRegions(SourceFileSyntax Node) { - assert(ReusedRegions.empty() && - "ReusedRegionsCollector cannot be reused"); - Node.accept(*this); - } - }; - - ReusedRegionsCollector ReuseRegionsCollector(getReusedNodes()); - ReuseRegionsCollector.collectReusedRegions(SyntaxTree); - return ReuseRegionsCollector.getReusedRegions(); -} diff --git a/lib/Parse/SyntaxParsingContext.cpp b/lib/Parse/SyntaxParsingContext.cpp deleted file mode 100644 index fad53ff7acb58..0000000000000 --- a/lib/Parse/SyntaxParsingContext.cpp +++ /dev/null @@ -1,485 +0,0 @@ -//===--- SyntaxParsingContext.cpp - Syntax Tree Parsing Support------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/SyntaxParsingContext.h" - -#include "swift/AST/ASTContext.h" -#include "swift/AST/DiagnosticEngine.h" -#include "swift/AST/DiagnosticsParse.h" -#include "swift/AST/Module.h" -#include "swift/AST/SourceFile.h" -#include "swift/Basic/Defer.h" -#include "swift/Parse/ParsedRawSyntaxRecorder.h" -#include "swift/Parse/ParsedSyntax.h" -#include "swift/Parse/ParsedSyntaxRecorder.h" -#include "swift/Parse/SyntaxParseActions.h" -#include "swift/Parse/SyntaxParsingCache.h" -#include "swift/Parse/Token.h" -#include "swift/Syntax/SyntaxFactory.h" -#include "llvm/Support/raw_ostream.h" - -using namespace swift; -using namespace swift::syntax; - -llvm::raw_ostream &llvm::operator<<(llvm::raw_ostream &OS, - SyntaxContextKind Kind) { - switch (Kind) { - case SyntaxContextKind::Decl: - OS << "Decl"; - break; - case SyntaxContextKind::Stmt: - OS << "Stmt"; - break; - case SyntaxContextKind::Expr: - OS << "Expr"; - break; - case SyntaxContextKind::Type: - OS << "Type"; - break; - case SyntaxContextKind::Pattern: - OS << "Pattern"; - break; - case SyntaxContextKind::Syntax: - OS << "Syntax"; - break; - } - return OS; -} - -void SyntaxParseActions::_anchor() {} - -using RootContextData = SyntaxParsingContext::RootContextData; - -SyntaxParsingContext::SyntaxParsingContext(SyntaxParsingContext *&CtxtHolder, - SourceFile &SF, unsigned BufferID, - std::shared_ptr SPActions) - : RootDataOrParent(new RootContextData( - SF, SF.getASTContext().Diags, SF.getASTContext().SourceMgr, BufferID, - std::move(SPActions))), - CtxtHolder(CtxtHolder), - RootData(RootDataOrParent.get()), Offset(0), - Mode(AccumulationMode::Root), Enabled(SF.shouldBuildSyntaxTree()) { - CtxtHolder = this; - getStorage().reserve(128); -} - -void SyntaxParsingContext::cancelBacktrack() { - SyntaxParsingContext *curr = CtxtHolder; - while (true) { - curr->IsBacktracking = false; - if (curr == this) { - break; - } - curr = curr->getParent(); - } -} - -size_t SyntaxParsingContext::lookupNode(size_t LexerOffset, SourceLoc Loc) { - if (!Enabled) - return 0; - - // Avoid doing lookup for a previous parsed node when we are in backtracking - // mode. This is because if the parser library client give us a node pointer - // and we discard it due to backtracking then we are violating this invariant: - // - // The parser guarantees that any \c swiftparse_client_node_t, given to the - // parser by \c swiftparse_node_handler_t or \c swiftparse_node_lookup_t, - // will be returned back to the client. - // - // which will end up likely creating a memory leak for the client because - // the semantics is that the parser accepts ownership of the object that the - // node pointer represents. - // - // Note that the fact that backtracking mode is disabling incremental parse - // node re-use is another reason that we should keep backtracking state as - // minimal as possible. - if (isBacktracking()) - return 0; - - assert(getStorage().size() == Offset && - "Cannot do lookup if nodes have already been gathered"); - assert(Mode == AccumulationMode::CreateSyntax && - "Loading from cache is only supported for mode CreateSyntax"); - auto foundNode = getRecorder().lookupNode(LexerOffset, Loc, SynKind); - if (foundNode.Node.isNull()) { - return 0; - } - Mode = AccumulationMode::SkippedForIncrementalUpdate; - getStorage().push_back(std::move(foundNode.Node)); - return foundNode.Length; -} - -ParsedRawSyntaxNode -SyntaxParsingContext::makeUnknownSyntax(SyntaxKind Kind, - MutableArrayRef Parts) { - assert(isUnknownKind(Kind)); - if (shouldDefer()) - return getRecorder().makeDeferred(Kind, Parts, *this); - else - return getRecorder().recordRawSyntax(Kind, Parts); -} - -ParsedRawSyntaxNode -SyntaxParsingContext::createSyntaxAs(SyntaxKind Kind, - MutableArrayRef Parts, - SyntaxNodeCreationKind nodeCreateK) { - // Try to create the node of the given syntax. - ParsedRawSyntaxNode rawNode; - auto &rec = getRecorder(); - auto formNode = [&](SyntaxKind kind, MutableArrayRef layout) { - if (nodeCreateK == SyntaxNodeCreationKind::Deferred || shouldDefer()) { - rawNode = getRecorder().makeDeferred(kind, layout, *this); - } else { - rawNode = rec.recordRawSyntax(kind, layout); - } - }; - if (ParsedSyntaxRecorder::formExactLayoutFor(Kind, Parts, formNode)) - return rawNode; - - // Fallback to unknown syntax for the category. - return makeUnknownSyntax(getUnknownKind(Kind), Parts); -} - -Optional -SyntaxParsingContext::bridgeAs(SyntaxContextKind Kind, - MutableArrayRef Parts) { - if (Parts.size() == 1) { - auto &RawNode = Parts.front(); - SyntaxKind RawNodeKind = RawNode.getKind(); - switch (Kind) { - case SyntaxContextKind::Stmt: - if (!isStmtKind(RawNodeKind)) - return makeUnknownSyntax(SyntaxKind::UnknownStmt, Parts); - break; - case SyntaxContextKind::Decl: - if (!isDeclKind(RawNodeKind)) - return makeUnknownSyntax(SyntaxKind::UnknownDecl, Parts); - break; - case SyntaxContextKind::Expr: - if (!isExprKind(RawNodeKind)) - return makeUnknownSyntax(SyntaxKind::UnknownExpr, Parts); - break; - case SyntaxContextKind::Type: - if (!isTypeKind(RawNodeKind)) - return makeUnknownSyntax(SyntaxKind::UnknownType, Parts); - break; - case SyntaxContextKind::Pattern: - if (!isPatternKind(RawNodeKind)) - return makeUnknownSyntax(SyntaxKind::UnknownPattern, Parts); - break; - case SyntaxContextKind::Syntax: - // We don't need to coerce in this case. - break; - } - return std::move(RawNode); - } else if (Parts.empty()) { - // Just omit the unknown node if it does not have any children - return None; - } else { - SyntaxKind UnknownKind; - switch (Kind) { - case SyntaxContextKind::Stmt: - UnknownKind = SyntaxKind::UnknownStmt; - break; - case SyntaxContextKind::Decl: - UnknownKind = SyntaxKind::UnknownDecl; - break; - case SyntaxContextKind::Expr: - UnknownKind = SyntaxKind::UnknownExpr; - break; - case SyntaxContextKind::Type: - UnknownKind = SyntaxKind::UnknownType; - break; - case SyntaxContextKind::Pattern: - UnknownKind = SyntaxKind::UnknownPattern; - break; - case SyntaxContextKind::Syntax: - UnknownKind = SyntaxKind::Unknown; - break; - } - return makeUnknownSyntax(UnknownKind, Parts); - } -} - -/// Add RawSyntax to the parts. -void SyntaxParsingContext::addRawSyntax(ParsedRawSyntaxNode &&Raw) { - getStorage().emplace_back(std::move(Raw)); -} - -const SyntaxParsingContext *SyntaxParsingContext::getRoot() const { - auto Curr = this; - while (!Curr->isRoot()) - Curr = Curr->getParent(); - return Curr; -} - -ParsedTokenSyntax SyntaxParsingContext::popToken() { - auto tok = popIf(); - return std::move(tok.getValue()); -} - -/// Add Token with Trivia to the parts. -void SyntaxParsingContext::addToken(Token &Tok, StringRef LeadingTrivia, - StringRef TrailingTrivia) { - if (!Enabled) - return; - - ParsedRawSyntaxNode raw; - if (shouldDefer()) { - raw = getRecorder().makeDeferred(Tok, LeadingTrivia, TrailingTrivia); - } else { - raw = getRecorder().recordToken(Tok, LeadingTrivia, TrailingTrivia); - } - addRawSyntax(std::move(raw)); -} - -/// Add Syntax to the parts. -void SyntaxParsingContext::addSyntax(ParsedSyntax &&Node) { - if (!Enabled) - return; - addRawSyntax(Node.takeRaw()); -} - -void SyntaxParsingContext::createNodeInPlace(SyntaxKind Kind, size_t N, - SyntaxNodeCreationKind nodeCreateK) { - if (N == 0) { - if (!parserShallOmitWhenNoChildren(Kind)) - getStorage().push_back(createSyntaxAs(Kind, {}, nodeCreateK)); - return; - } - - auto node = createSyntaxAs(Kind, getParts().take_back(N), nodeCreateK); - auto &storage = getStorage(); - getStorage().erase(storage.end() - N, getStorage().end()); - getStorage().emplace_back(std::move(node)); -} - -void SyntaxParsingContext::createNodeInPlace(SyntaxKind Kind, - SyntaxNodeCreationKind nodeCreateK) { - assert(isTopOfContextStack()); - if (!Enabled) - return; - - switch (Kind) { - case SyntaxKind::SuperRefExpr: - case SyntaxKind::OptionalChainingExpr: - case SyntaxKind::ForcedValueExpr: - case SyntaxKind::PostfixUnaryExpr: - case SyntaxKind::TernaryExpr: - case SyntaxKind::AvailabilityLabeledArgument: - case SyntaxKind::MetatypeType: - case SyntaxKind::PackExpansionType: - case SyntaxKind::OptionalType: - case SyntaxKind::ImplicitlyUnwrappedOptionalType: - case SyntaxKind::TypeExpr: { - auto Pair = SyntaxFactory::countChildren(Kind); - assert(Pair.first == Pair.second); - createNodeInPlace(Kind, Pair.first, nodeCreateK); - break; - } - case SyntaxKind::CodeBlockItem: - case SyntaxKind::IdentifierExpr: - case SyntaxKind::SpecializeExpr: - case SyntaxKind::MemberAccessExpr: - case SyntaxKind::SimpleTypeIdentifier: - case SyntaxKind::MemberTypeIdentifier: - case SyntaxKind::FunctionCallExpr: - case SyntaxKind::SubscriptExpr: - case SyntaxKind::PostfixIfConfigExpr: - case SyntaxKind::ExprList: { - createNodeInPlace(Kind, getParts().size(), nodeCreateK); - break; - } - default: - llvm_unreachable("Unrecognized node kind."); - } -} - -void SyntaxParsingContext::collectNodesInPlace(SyntaxKind CollectionKind, - SyntaxNodeCreationKind nodeCreateK) { - assert(isCollectionKind(CollectionKind)); - assert(isTopOfContextStack()); - if (!Enabled) - return; - auto Parts = getParts(); - auto Count = 0; - for (auto I = Parts.rbegin(), End = Parts.rend(); I != End; ++I) { - if (!SyntaxFactory::canServeAsCollectionMemberRaw(CollectionKind, I->getKind())) - break; - ++Count; - } - if (Count) - createNodeInPlace(CollectionKind, Count, nodeCreateK); -} - -static ParsedRawSyntaxNode finalizeSourceFile(RootContextData &RootData, - MutableArrayRef Parts) { - ParsedRawSyntaxRecorder &Recorder = RootData.Recorder; - ParsedRawSyntaxNode Layout[5]; - - assert(!Parts.empty() && Parts.back().isToken(tok::eof)); - Layout[3] = std::move(Parts.back()); - Parts = Parts.drop_back(); - - - assert(llvm::all_of(Parts, [](const ParsedRawSyntaxNode& node) { - return node.getKind() == SyntaxKind::CodeBlockItem; - }) && "all top level element must be 'CodeBlockItem'"); - - Layout[1] = Recorder.recordRawSyntax(SyntaxKind::CodeBlockItemList, Parts); - - return Recorder.recordRawSyntax(SyntaxKind::SourceFile, - llvm::makeMutableArrayRef(Layout, 5)); -} - -OpaqueSyntaxNode SyntaxParsingContext::finalizeRoot() { - if (!Enabled) - return nullptr; - assert(isTopOfContextStack() && "some sub-contexts are not destructed"); - assert(isRoot() && "only root context can finalize the tree"); - assert(Mode == AccumulationMode::Root); - auto parts = getParts(); - if (parts.empty()) { - return nullptr; // already finalized. - } - ParsedRawSyntaxNode root = finalizeSourceFile(*getRootData(), parts); - - // Clear the parts because we will call this function again when destroying - // the root context. - getStorage().clear(); - - assert(root.isRecorded() && "Root of syntax tree should be recorded"); - return root.takeData(); -} - -void SyntaxParsingContext::synthesize(tok Kind, SourceLoc Loc) { - if (!Enabled) - return; - - ParsedRawSyntaxNode raw; - if (shouldDefer()) - raw = getRecorder().makeDeferredMissing(Kind, Loc); - else - raw = getRecorder().recordMissingToken(Kind, Loc); - getStorage().push_back(std::move(raw)); -} - -void SyntaxParsingContext::dumpStorage() const { - llvm::errs() << "======================\n"; - auto &storage = getStorage(); - for (unsigned i = 0; i != storage.size(); ++i) { - storage[i].dump(llvm::errs()); - llvm::errs() << "\n"; - if (i + 1 == Offset) - llvm::errs() << "--------------\n"; - } -} - -void SyntaxParsingContext::dumpStack(llvm::raw_ostream &OS) const { - if (!isRoot()) { - getParent()->dumpStack(OS); - } - switch (Mode) { - case AccumulationMode::CreateSyntax: - llvm::errs() << "CreateSyntax (" << SynKind << ")\n"; - break; - case AccumulationMode::DeferSyntax: - llvm::errs() << "DeferSyntax (" << SynKind << ")\n"; - break; - case AccumulationMode::CoerceKind: - llvm::errs() << "CoerceKind (" << CtxtKind << ")\n"; - break; - case AccumulationMode::Transparent: - llvm::errs() << "Transparent\n"; - break; - case AccumulationMode::Discard: - llvm::errs() << "Discard\n"; - break; - case AccumulationMode::SkippedForIncrementalUpdate: - llvm::errs() << "SkippedForIncrementalUpdate\n"; - break; - case AccumulationMode::Root: - llvm::errs() << "Root\n"; - break; - case AccumulationMode::NotSet: - llvm::errs() << "NotSet\n"; - break; - } -} - -SyntaxParsingContext::~SyntaxParsingContext() { - assert(isTopOfContextStack() && "destructed in wrong order"); - - SWIFT_DEFER { - // Pop this context from the stack. - if (!isRoot()) - CtxtHolder = getParent(); - else - delete RootDataOrParent.get(); - }; - - if (!Enabled) - return; - - auto &Storage = getStorage(); - - switch (Mode) { - // Create specified Syntax node from the parts and add it to the parent. - case AccumulationMode::CreateSyntax: - case AccumulationMode::DeferSyntax: - assert(!isRoot()); - createNodeInPlace(SynKind, Storage.size() - Offset, - Mode == AccumulationMode::DeferSyntax ? - SyntaxNodeCreationKind::Deferred : SyntaxNodeCreationKind::Recorded); - break; - - // Ensure the result is specified Syntax category and add it to the parent. - case AccumulationMode::CoerceKind: { - assert(!isRoot()); - if (Storage.size() == Offset) { - if (auto BridgedNode = bridgeAs(CtxtKind, {})) { - Storage.push_back(std::move(BridgedNode.getValue())); - } - } else { - auto node(std::move(bridgeAs(CtxtKind, getParts()).getValue())); - Storage.erase(Storage.begin() + Offset, Storage.end()); - Storage.emplace_back(std::move(node)); - } - break; - } - - // Do nothing. - case AccumulationMode::Transparent: - assert(!isRoot()); - break; - - // Remove all parts in this context. - case AccumulationMode::Discard: { - auto &nodes = getStorage(); - nodes.erase(nodes.begin()+Offset, nodes.end()); - break; - } - - case AccumulationMode::SkippedForIncrementalUpdate: - break; - - // Accumulate parsed toplevel syntax. - case AccumulationMode::Root: - finalizeRoot(); - break; - - // Never. - case AccumulationMode::NotSet: - assert(!Enabled && "Cleanup mode must be specified before destruction"); - break; - } -} diff --git a/lib/SIL/IR/SILPrinter.cpp b/lib/SIL/IR/SILPrinter.cpp index f3c72c4ab9335..934aa57e07d40 100644 --- a/lib/SIL/IR/SILPrinter.cpp +++ b/lib/SIL/IR/SILPrinter.cpp @@ -261,7 +261,7 @@ static void printValueDecl(ValueDecl *Decl, raw_ostream &OS) { .Case("init", false) #define KEYWORD(kw) \ .Case(#kw, true) -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" .Default(false); if (shouldEscape) { diff --git a/lib/SIL/Parser/ParseSIL.cpp b/lib/SIL/Parser/ParseSIL.cpp index df42fc90f337d..fa77919a9f5ac 100644 --- a/lib/SIL/Parser/ParseSIL.cpp +++ b/lib/SIL/Parser/ParseSIL.cpp @@ -37,13 +37,11 @@ #include "swift/SIL/SILUndef.h" #include "swift/SIL/TypeLowering.h" #include "swift/Subsystems.h" -#include "swift/Syntax/SyntaxKind.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/SaveAndRestore.h" using namespace swift; -using namespace swift::syntax; static llvm::cl::opt ParseSerializedSIL("parse-serialized-sil", @@ -1944,7 +1942,6 @@ bool SILParser::parseSILBBArgsAtBranch(SmallVector &Args, if (P.parseList(tok::r_paren, LParenLoc, RParenLoc, /*AllowSepAfterLast=*/false, diag::sil_basicblock_arg_rparen, - SyntaxKind::Unknown, [&]() -> ParserStatus { SILValue Arg; SourceLoc ArgLoc; diff --git a/lib/Sema/TypeCheckMacros.cpp b/lib/Sema/TypeCheckMacros.cpp index b52d61738115b..69fb3a8ccd9a0 100644 --- a/lib/Sema/TypeCheckMacros.cpp +++ b/lib/Sema/TypeCheckMacros.cpp @@ -27,6 +27,7 @@ #include "swift/Basic/StringExtras.h" #include "swift/Parse/Lexer.h" #include "swift/Parse/Parser.h" +#include "swift/Subsystems.h" using namespace swift; diff --git a/lib/Sema/TypeCheckStmt.cpp b/lib/Sema/TypeCheckStmt.cpp index 1d3b0de1f94a1..b4161a38b7f64 100644 --- a/lib/Sema/TypeCheckStmt.cpp +++ b/lib/Sema/TypeCheckStmt.cpp @@ -43,7 +43,6 @@ #include "swift/Parse/LocalContext.h" #include "swift/Parse/Parser.h" #include "swift/Sema/IDETypeChecking.h" -#include "swift/Syntax/TokenKinds.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/SmallString.h" diff --git a/lib/Sema/TypeChecker.cpp b/lib/Sema/TypeChecker.cpp index c76a679f84921..5d996f3f387bf 100644 --- a/lib/Sema/TypeChecker.cpp +++ b/lib/Sema/TypeChecker.cpp @@ -146,7 +146,7 @@ ProtocolDecl *TypeChecker::getLiteralProtocol(ASTContext &Context, Expr *expr) { case ObjectLiteralExpr::Name: \ return TypeChecker::getProtocol(Context, expr->getLoc(), \ KnownProtocolKind::Protocol); -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" } } diff --git a/lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp b/lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp index 76ac521058bcd..ae9e83a708aca 100644 --- a/lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp +++ b/lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp @@ -11,7 +11,7 @@ //===----------------------------------------------------------------------===// #include "swift/AST/USRGeneration.h" -#include "swift/Syntax/TokenKinds.h" +#include "swift/Parse/Token.h" #include "DeclarationFragmentPrinter.h" #include "SymbolGraphASTWalker.h" diff --git a/lib/Syntax/.doc/SyntaxExample.graffle b/lib/Syntax/.doc/SyntaxExample.graffle deleted file mode 100644 index c18eedfa435f2..0000000000000 Binary files a/lib/Syntax/.doc/SyntaxExample.graffle and /dev/null differ diff --git a/lib/Syntax/.doc/SyntaxExample.png b/lib/Syntax/.doc/SyntaxExample.png deleted file mode 100644 index 9fb2a1b2eeb57..0000000000000 Binary files a/lib/Syntax/.doc/SyntaxExample.png and /dev/null differ diff --git a/lib/Syntax/AbsoluteRawSyntax.cpp b/lib/Syntax/AbsoluteRawSyntax.cpp deleted file mode 100644 index d3eef674d4eca..0000000000000 --- a/lib/Syntax/AbsoluteRawSyntax.cpp +++ /dev/null @@ -1,24 +0,0 @@ -//===--- AbsoluteRawSyntax.cpp ----------------------------------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/AbsoluteRawSyntax.h" - -using namespace swift; -using namespace swift::syntax; - -std::atomic SyntaxIdentifier::NextUnusedRootId(0); - -raw_ostream &llvm::operator<<(raw_ostream &OS, - swift::syntax::AbsoluteOffsetPosition Pos) { - OS << "Offset " << Pos.getOffset(); - return OS; -} diff --git a/lib/Syntax/CMakeLists.txt b/lib/Syntax/CMakeLists.txt deleted file mode 100644 index b768bf59e2945..0000000000000 --- a/lib/Syntax/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) - set(SWIFT_GYB_FLAGS --line-directive "^\"#line %(line)d \\\"%(file)s\\\"^\"") -else() - set(SWIFT_GYB_FLAGS --line-directive "\'#line" "%(line)d" "\"%(file)s\"\'") -endif() - -add_swift_host_library(swiftSyntax STATIC - AbsoluteRawSyntax.cpp - RawSyntax.cpp - Syntax.cpp - SyntaxData.cpp - UnknownSyntax.cpp) -_swift_gyb_target_sources(swiftSyntax PRIVATE - SyntaxNodes.cpp.gyb - SyntaxBuilders.cpp.gyb - SyntaxKind.cpp.gyb - SyntaxFactory.cpp.gyb - SyntaxVisitor.cpp.gyb - Trivia.cpp.gyb - SyntaxSerialization.cpp.gyb) -target_link_libraries(swiftSyntax PRIVATE - swiftParse) diff --git a/lib/Syntax/README.md b/lib/Syntax/README.md deleted file mode 100644 index 1c1edea479830..0000000000000 --- a/lib/Syntax/README.md +++ /dev/null @@ -1,563 +0,0 @@ -# Swift Syntax and Structured Editing Library - -Welcome to lib/Syntax! - -This library implements data structures and algorithms for dealing -with Swift [syntax](https://en.wikipedia.org/wiki/Syntax_\(programming_languages\)), -striving to be safe, correct, and intuitive to use. The -library emphasizes immutable, thread-safe data structures, full-fidelity -representation of source, and facilities for *structured editing*. - -What is structured editing? It's an editing strategy that is keenly aware of the -*structure* of source code, not necessarily its *representation* (i.e. -characters or bytes). This can be achieved at different granularities: replacing -an identifier, changing a global function call to a method call, or indenting -and formatting an entire source file based on declarative rules. These kinds of -diverse operations are critical to the Swift Migrator, which is the immediate -client for this library, now developed in the open. Along with that, the library -will also provide infrastructure for a first-class `swift-format` tool. - -Eventually, the goal of this library is to represent Swift syntax in all of the -compiler. Currently, lib/AST structures don't make a very clear distinction -between syntactic and semantic information. Long term, we hope to achieve the -following based on work here: - -- Adoption throughout the compiler -- Clear separation of syntactic and semantic information -- Greater stability with immutable data structures -- Lower high-water memory use due to reference counting without the need for - leak-forever memory contexts -- Incremental re-parsing -- Incremental, lazier re-type-checking, helped by separating syntactic - information - -This library is a work in progress and should be expected to be in a molten -state for some time. Don't integrate this into other areas of the compiler or -use it for anything serious just now. - -You can read more about the status of the library's implementation at the -[Syntax Status Page](Status.md). More information about opportunities to get -involved to come. - -## Design and Implementation Guidelines - -In no particular order, here is a summary of the design and implementation -points for this library: - -1. Represent Swift source with "full fidelity" - parsing a source file and - printing the syntax tree should result in the same file. -1. Provide good structured editing APIs at all granularities. -1. Make public API as intuitive as possible. If you can't quickly answer, "What - do I do next?", that's a bug. Compiler development shouldn't be black magic, - so let's make an effort so that beginners and experts alike feel welcome - here. -1. Don't crash. -1. Don't open yourself up to crashes for the sake of performance. -1. Don't hand raw pointers to clients. Give them realized values that they - either own or borrow, and indicate optionality with types. -1. If it represents authored Swift source, then the data representing it is - immutable. You don't change what an author wrote without their permission and - initiation. -1. This library is not concerned with semantic analysis. Don't store types, - symbolic references, lookup logic, and the like here. This is purely for - syntactic constructs. -1. All public APIs must be covered by tests. Whenever applicable, consider: - - C++ unit tests, at a minimum, all public C++ APIs - - Round-trip lex/parse test cases - - Exercised by the formatter/migrator - - For each grammar production, as many combinations as possible, especially - with respect to optional terms and expected by missing terms -1. All public APIs must have documentation comments. -1. Represent Swift grammar and use naming conventions in accordance with The - Swift Programming Language book as much as possible, so people know what to - look for. -1. Accommodate "bad syntax" - humans are imperfect and source code is constantly - in a state of flux in an editor. Unfortunately, we still live in a - character-centric world - the library shouldn't fall over on bad syntax just - because someone is in the middle of typing `struct`. - -## APIs - -### Make APIs - -*Make APIs* are for creating new syntax nodes in a single call. Although you -need to provide all of the pieces of syntax to these APIs, you are free to use -"missing" placeholders as substructure. Make APIs return freestanding syntax -nodes and do not establish parental relationships. - -#### The SyntaxFactory - -The `SyntaxFactory` embodies the Make APIs and is the one-stop shop for creating -new syntax nodes and tokens in a single call. There are two main Make APIs -exposed for each Syntax node: making the node with all of the pieces, or making -a blank node with all of the pieces marked as *missing*. For example, -`SyntaxFactory` has `makeStructDeclSyntax` and `makeBlankStructDeclSyntax` that -both return a `StructDeclSyntax`. - -Instead of constructors on each syntax node's class, static creation methods are -all supplied here in the `SyntaxFactory` for better code completion - you don't -need to know the exact name of the class. Just type `SyntaxFactory::make` and -let code completion show you what you can make. - -**Example** - -```c++ -// A 'typealias' keyword with one space after -auto TypeAliasKeyword = SyntaxFactory::makeTypeAliasKeyword({}, Trivia::spaces(1)); - -// The identifier "Element" with one space after -auto ElementID = SyntaxFactory::makeIdentifier("Element", {}, Trivia::spaces(1)); - -// An equal '=' token with one space after -auto Equal = SyntaxFactory::makeEqualToken({}, Trivia::spaces(1)); - -// A type identifier for "Int" -auto IntType = SyntaxFactory::makeTypeIdentifier("Int", {}, {}) - -// Finally, the actual type alias declaration syntax. -auto TypeAlias = SyntaxFactory::makeTypeAliasDecl(TypeAliasKeyword, - ElementID, - EmptyGenericParams, - Equal, - IntType); -TypeAlias.print(llvm::outs()); -``` - -```swift -typealias Element = Int -``` - -### With APIs - -*With APIs* are essentially *setters* on `Syntax` nodes you already have in hand -but, because they are immutable, return new `Syntax` nodes with only the -specified substructure replaced. Raw backing storage is shared as much as -possible. - -**Example** - -Say you have a `MyStruct` of type `StructDeclSyntax` representing: - -```swift -struct MyStruct {} -``` - -Now, let's create a new struct with a different identifier, "YourStruct". The -original struct is unharmed but identical tokens are shared. - -```c++ -auto NewIdentifier = SyntaxFactory::makeIdentifier("YourStruct", - MyStruct.getIdentifier().getLeadingTrivia(), - MyStruct.getIdentifier().getTrailingTrivia()); - -MyStruct.withIdentifier(NewIdentifier).print(llvm::outs()); -``` - -```swift -struct YourStruct {} -``` - -### Builder APIs - -*Builder APIs* are provided for building up syntax incrementally as it appears. -At any point in the building process, you can call `build()` and get a -reasonably formed Syntax node (i.e. with no raw `nullptr`s) using what you've -provided to the builder so far. Anything that you haven't supplied is marked as -*missing*. This is essentially what the parser does; so, looking forward to -future adoption, the builders are designed with the parser in mind, with the -hope that we can better specify recovery behavior and incremental (re-)parsing. - -**Example** - -```c++ -StructDeclSyntaxBuilder Builder; - -// We previously parsed a struct keyword, let's tell the builder to use it. -Builder.useStructKeyword(StructKeyword); - -// Hm, we didn't see an identifier, but saw a left brace. Let's keep going. -Builder.useLeftBrace(ParsedLeftBrace) - -// No members of the struct; we saw a right brace. -Builder.useRightBrace(ParsedRightBrace); -``` - -Let's see what we have so far. - -```c++ -auto StructWithoutIdentifier = Builder.build(); -StructWithoutIdentifier.print(llvm::outs()); -``` - -```swift -struct {} -``` - -Whoops! You forgot an identifier. Let's add one here for fun. - -```c++ -auto MyStructID = SyntaxFactory::makeIdentifier("MyStruct", {}, Trivia::spaces(1)); -Builder.useIdentifier(MyStructID); - -auto StructWithIdentifier = Builder.build(); -StructWithIdentifier.print(llvm::outs()); -``` - -```swift -struct MyStruct {} -``` - -Much better! - -Note that syntax builders own and mutate the data they will eventually use to -build a syntax node. They themselves should not be shared between threads. -However, anything the builder builds and returns to you is safe and immutable. - -### Syntax Rewriters - -`TODO`. - -## Internals - -### RawSyntax - -`RawSyntax` are the *raw* immutable backing store for all *syntax*. Essentially, -they store a kind, whether they were missing in the source, and the *layout*, -which is a list of children and represents the recursive substructure. Although -these are tree-like in nature, *they maintain no parental relationships* because -they can be shared among many nodes. Eventually, `RawSyntax` bottoms out in -tokens, represented by the `TokenSyntax` class. - -#### RawSyntax summary - -- `RawSyntax` are the immutable backing store for all syntax. -- `RawSyntax` are immutable. -- `RawSyntax` establishes the tree structure of syntax. -- `RawSyntax` store no parental relationships and can therefore be shared - among syntax nodes if they have identical content. - -### RawTokenSyntax - -These are special cases of `RawSyntax` and represent all terminals in the -grammar. Aside from the token kind and the text, they have two very important -pieces of information for full-fidelity source: leading and trailing source -*trivia* surrounding the token. - -#### RawTokenSyntax summary - -- `RawTokenSyntax` are `RawSyntax` and represent the terminals in the Swift - grammar. -- Like `RawSyntax`, `RawTokenSyntax` are immutable. -- `RawTokenSyntax` do have pointer equality, but they can be shared among syntax - nodes. -- `RawTokenSyntax` have *leading-* and *trailing trivia*, the purely syntactic - formatting information like whitespace and comments. - -### Trivia - -You've already seen some uses of `Trivia` in the examples above. These are -pieces of syntax that aren't really relevant to the semantics of the program, -such as whitespace and comments. These are modeled as collections and, with the -exception of comments, are sort of "run-length" encoded. For example, a sequence -of four spaces is represented by `{ Kind: TriviaKind::Space, Count: 4 }`, not -the literal text `"    "`. - -Some examples of the "atoms" of `Trivia`: - -- Spaces -- Tabs -- Newlines -- Single-line developer (`//`) comments -- Block developer (`/* ... */`) comments -- Single-line documentation (`///`) comments -- Block documentation (`/** ... */`) comments -- Backticks - -There are two Rules of Trivia that you should obey when parsing or constructing -new `Syntax` nodes: - -1. A token owns all of its trailing trivia up to, but not including, - the next newline character. - -2. Looking backward in the text, a token owns all of the leading trivia - up to and including the first newline character. - -In other words, a contiguous stretch of trivia between two tokens is split on the -leftmost newline. - -Let's take a look at how this shows up in practice with a small snippet of Swift -code. - -**Example** - -```swift -func foo() { - var x = 2 -} -``` - -Breaking this down token by token: - -- `func` - - Leading trivia: none. - - Trailing trivia: Takes up the space after (Rule 1). - - ```c++ - // Equivalent to: - Trivia::spaces(1) - ``` - -- `foo` - - Leading trivia: none. The previous `func` ate the space before. - - Trailing trivia: none. - -- `(` - - Leading trivia: none. - - Trailing trivia: none. - -- `)` - - Leading trivia: none. - - Trailing trivia: Takes up the space after (Rule 1). - -- `{` - - Leading trivia: none. The previous `)` ate the space before. - - Trailing trivia: none. Because of Rule 1, it doesn't take the following - newline. - -- `var` - - Leading trivia: One newline followed by two spaces because of Rule 2. - - ```c++ - // Equivalent to: - Trivia::newlines(1) + Trivia::spaces(2) - ``` - - Trailing trivia: Takes the space after (Rule 1). - -- `x` - - Leading trivia: none. The previous `var` ate the space before. - - Trailing trivia: Takes up the space after (Rule 1). - -- `=` - - Leading trivia: none. The previous `x` ate the space before. - - Trailing trivia: Takes up the space after (Rule 1). - -- `2` - - Leading trivia: none. The previous `=` ate the space before. - - Trailing trivia: none: Because of Rule 1, it doesn't take the following - newline. - -- `}` - - Leading trivia: One newline, due to Rule 2. - - Trailing trivia: none. - -- `EOF` - - Leading trivia: none. - - Trailing trivia: none. - -A couple of remarks about the `EOF` token: - -- Starting with the first newline after the last non-EOF token, `EOF` takes - all remaining trivia in the source file as its leading trivia. -- Because of this, `EOF` never has trailing trivia. - -#### Summary of Trivia - -- `Trivia` represent *source trivia*, the whitespace and comments in a Swift - source file. -- `Trivia` are immutable. -- `Trivia` don't have pointer identity - they are primitive values. - -### SyntaxData - -`SyntaxData` nodes wrap `RawSyntax` nodes with a few important pieces of -additional information: a pointer to a parent, the position in which the node -occurs in its parent, and cached children. - -For example, if we have a `SyntaxData`, wrapping a `RawSyntax` for a -struct declaration, we might ask for the generic parameter clause. At first, -this is only represented in the raw syntax. On first ask, we thaw those out by -creating a new `SyntaxData`, cache it as our child, set -its parent to `this`, and send it back to the caller. These cached children -are strong references, keeping the syntax tree alive in memory. - -You can think of `SyntaxData` as "concrete" or "realized" syntax nodes. They -represent a specific piece of source code, have an absolute location, line and -column number, etc. `RawSyntax` are more like the integer 1 - a single -theoretical entity that exists, but manifesting everywhere it occurs identically -in Swift source code. - -Beyond this, `SyntaxData` nodes have *no significant public API*. - -- `SyntaxData` are immutable. - However, they may mutate themselves in order to implement lazy instantiation - of children and caching. That caching operation is transparent and - thread-safe. -- `SyntaxData` have identity, i.e. they can be compared with "pointer equality". -- `SyntaxData` are implementation detail have no public API. - -### Syntax - -`RawSyntax` and `SyntaxData` are essentially implementation detail in order to -maintain all of those nice properties like immutability and information sharing. -Now, we get to the main players: the `Syntax` nodes. These have the interesting -public interface: the *With APIs*, getters, etc. Anyone working with the -`Syntax` library will be touching these nodes. - -Internally, they are actually packaged as a strong reference to the root of the -tree in which that node resides, and a weak reference to the `SyntaxData` -representing that node. Why a weak reference to the data? We do this to prevent -retain cycles and minimize retain/release traffic: **all strong references point -down in the tree, starting at the root**. - -Although it's important for the entire library to be easy to use and maintain in -general, it's especially important that the APIs in `Syntax` nodes remain -intuitive and do what you expect with no weird side effects, necessary contexts -to maintain, etc. If you have a handle on a `Syntax` node, you're safe to query -anything about it without other processes pulling out the rug from under you. - -### Example Object Diagram: `{ return 1 }` - -Here's an example of what you might have as a result of the following C++ -code: - -```c++ -auto LeftBrace = SyntaxFactory::makeLeftBraceToken({}, Trivia::spaces(1)); - -auto IntegerTok = SyntaxFactory::makeIntegerLiteral("1", {}, Trivia::spaces(1)); -auto Integer = SyntaxFactory::makeIntegerLiteralExpr(IntegerTok); - -auto ReturnKW = SyntaxFactory::makeReturnKeyword({}, Trivia::spaces(1)); - -// This ReturnStmtSyntax is floating, with no root. -auto Return = SyntaxFactory::makeReturnStmt(ReturnKW, Integer, - /*Semicolon=*/ None); - -auto RightBrace = SyntaxFactory::makeRightBraceToken({}, {}); - -auto Statements = SyntaxFactory::makeBlankStmtList() - .addStmt(Return); - -auto Block = SyntaxFactory::makeBlankCodeBlockStmt() - // Takes a reference of the token directly and increments the - // reference count. - .withLeftBrace(LeftBrace) - - // Only takes a strong reference to the RawSyntax of the - // ReturnStmtSyntax above. - .withStatementList(Statements) - - // Takes a reference of the token directly and increments the - // reference count. - .withRightBrace(RightBrace); - -// Returns a new ReturnStmtSyntax with the root set to the Block -// above, and the parent set to the StmtListSyntax. -auto MyReturn = Block.getChild(0); -``` - -Here's what the corresponding object diagram would look like starting with -`MyReturn`. - -![Syntax Example](.doc/SyntaxExample.png) - -Legend: -- Green: `RawSyntax` types (`RawTokenSyntax` is a `RawSyntax`) -- Red: `SyntaxData` types -- Blue: `Syntax` types -- Gray: `Trivia` -- Solid Arrows: Strong references -- Dashed Arrows: Weak references - -A couple of interesting points and reminders: -- All strong references point downward in the tree. -- One `SyntaxData` for each `RawSyntax`. - Remember, a `SyntaxData` is essentially a `RawSyntax` with a parent pointer - and cached `SyntaxData` children. -- Parent pointers are omitted here but there are weak references pointing - upward among `SyntaxData` (red) nodes. -- Clients only work with `Syntax` (blue) nodes and `Trivia` (gray), and should - never see `SyntaxData` (red) or `RawSyntax` (green) nodes. - -## API Generation - -The libSyntax APIs are generated automatically from a set of description files -written in Python. These files are located in the [swift-syntax repository](https://github.com/apple/swift-syntax/blob/main/utils/gyb_syntax_support), and -all follow the same schema. - -### Class Schema - -#### Nodes - -A `Node` represents a production in the Swift grammar that has zero or more -children. Each file contains a top-level array containing each node. The `Node` -class has the following fields: - -| Key | Type | Description | -| --- | ---- | ----------- | -| `kind` | `String` | The "base class" for this node. Must be one of `["Syntax", "SyntaxCollection", "Expr", "Stmt", "Decl", "Pattern", "Type"]`. | -| `element` | `String?` | If the node is a `SyntaxCollection`, then this is the `SyntaxKind` of the element of this collection. If this is not a `SyntaxCollection`, then this value is ignored. | -| `element_name` | `String?` | If the node is a `SyntaxCollection`, then this is a different name for the element that you wish to appear in the generated API. Some nodes cannot find a good upper-bound for the element, and so must defer to `Syntax` -- those nodes use this field to populate a better name for `add${element_name}` APIs. | -| `children` | `[[String: Child]]?` | The children of this node. | - -#### Children - -A `Child` represents a child of a given `Node` object. A `Child` has the -following fields: - -| Key | Type | Description | -| --- | ---- | ----------- | -| `kind` | `String` | The `SyntaxKind` of this child. This must have a corresponding `Node` with that kind (or corresponding `Token` in both `include/swift/Syntax/TokenKinds.def` and `SYNTAX_TOKENS`). | -| `is_optional` | `Bool?` | Whether this child is required in a fully-formed object, or if it is allowed to remain `missing`. Defaults to `false` if not present. -| `token_choices` | `[String]?` | A list of `Token`s which are considered "valid" values for `Token` children. | -| `text_choices` | `[String]?` | A list of valid textual values for tokens. If this is not provided, any textual value is accepted for tokens like `IdentifierToken`. | - -#### Tokens - -A `Token` represents one of the `tok::` enums in -`include/swift/Syntax/TokenKinds.def`. `Token.py` has a top-level array of token -declarations. The `Token` class has the following fields. - -| Key | Type | Description | -| --- | ---- | ----------- | -| `kind` | `String` | The name of the token in the C++ `tok::` namespace. This is what we use to map these nodes to C++ tokens. | -| `text` | `String?` | If the text of this node is fixed, then this field contains that text. For example, `Struct` has text `"struct"` and kind `"kw_struct"`. | -| `is_keyword` | `Bool?` | Whether this node is a keyword. Defaults to `false` if not present. | - -### C++ File Generation - -libSyntax uses Swift's `gyb` tool to generate the `Syntax` subclasses, -`SyntaxFactory` methods, `SyntaxKind` enum entry, and `SyntaxBuilder` class. -These files rely on a support library located at `utils/gyb_syntax_support/` -which holds some common logic used inside the `gyb` files. These `gyb` files -will be re-generated whenever any Python files are changed. - -## Adding new Syntax Nodes - -Here's a handy checklist when implementing a production in the grammar. -- Check if it's not already being worked on, and then - [file a Swift bug](https://bugs.swift.org/secure/CreateIssue!default.jspa), - noting which grammar productions are affected. - - **Add the `Syntax` bug label!** -- Create the `${KIND}` entry in the appropriate Python file (Expr, Stmt, - Pattern, etc.). - - Add C++ unit tests for `with` APIs for all layout elements - (e.g. `withLeftTypeIdentifier(...)`). - - Check that the resulting `Syntax` node has identical content except for - what you changed. `print` the new node and check the text. - - Add a C++ unit test for the getters for all layout elements - (e.g. `getLeftTypeIdentifier()`) - - After `get`ing the child, verify: - - The child's parent and root are correct - - The child's content is correct - - The child `print`s the expected text - - Add a C++ unit test for the `Builder` of that node. - - Add a C++ unit test for the `SyntaxFactory::make` APIs for that node. -- Add a round-trip test for the grammar production - - Create a .swift file in test/Syntax with all possible configurations of the - piece of syntax, with two `RUN` lines: - - check for a zero-diff print with `-round-trip-lex`, and - - check for a zero-diff print with `-round-trip-parse` -- Update `lib/Syntax/Status.md` if applicable. - -## Use libSyntax from Swift code -SwiftSyntax has been moved to [its own repository](https://github.com/apple/swift-syntax) as a SwiftPM package. Please follow the instructions in that repository for how to use it for a Swift tool. diff --git a/lib/Syntax/RawSyntax.cpp b/lib/Syntax/RawSyntax.cpp deleted file mode 100644 index 024aad126b784..0000000000000 --- a/lib/Syntax/RawSyntax.cpp +++ /dev/null @@ -1,216 +0,0 @@ -//===--- RawSyntax.cpp - Swift Raw Syntax Implementation ------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/RawSyntax.h" -#include "swift/Basic/ColorUtils.h" -#include "swift/Parse/Lexer.h" -#include "swift/Syntax/SyntaxArena.h" -#include "llvm/Support/Casting.h" -#include "llvm/Support/raw_ostream.h" - -#include - -using llvm::dyn_cast; -using namespace swift; -using namespace swift::syntax; - -namespace { -static bool isTrivialSyntaxKind(SyntaxKind Kind) { - if (isUnknownKind(Kind)) - return true; - if (isCollectionKind(Kind)) - return true; - switch(Kind) { - case SyntaxKind::SourceFile: - case SyntaxKind::CodeBlockItem: - case SyntaxKind::ExpressionStmt: - case SyntaxKind::DeclarationStmt: - return true; - default: - return false; - } -} - -static void printSyntaxKind(SyntaxKind Kind, llvm::raw_ostream &OS, - SyntaxPrintOptions Opts, bool Open) { - std::unique_ptr Color; - if (Opts.Visual) { - Color.reset(new swift::OSColor(OS, llvm::raw_ostream::GREEN)); - } - OS << "<"; - if (!Open) - OS << "/"; - dumpSyntaxKind(OS, Kind); - OS << ">"; -} - -} // end of anonymous namespace - -void swift::dumpTokenKind(llvm::raw_ostream &OS, tok Kind) { - switch (Kind) { -#define TOKEN(X) \ - case tok::X: \ - OS << #X; \ - break; -#include "swift/Syntax/TokenKinds.def" - case tok::NUM_TOKENS: - OS << "NUM_TOKENS (unset)"; - break; - } -} - -/// Lex the given trivia string into its pieces -Trivia lexTrivia(StringRef TriviaStr) { - // FIXME: The trivia lexer should directly create TriviaPieces so we don't - // need the conversion from ParsedTriviaPiece to TriviaPiece - - // Lex the trivia into ParsedTriviaPiece - auto TriviaPieces = TriviaLexer::lexTrivia(TriviaStr).Pieces; - - /// Convert the ParsedTriviaPiece to TriviaPiece - Trivia SyntaxTrivia; - size_t Offset = 0; - for (auto Piece : TriviaPieces) { - StringRef Text = TriviaStr.substr(Offset, Piece.getLength()); - SyntaxTrivia.push_back(TriviaPiece::fromText(Piece.getKind(), Text)); - Offset += Piece.getLength(); - } - return SyntaxTrivia; -} - -Trivia RawSyntax::getLeadingTriviaPieces() const { - return lexTrivia(getLeadingTrivia()); -} - -Trivia RawSyntax::getTrailingTriviaPieces() const { - return lexTrivia(getTrailingTrivia()); -} - -const RawSyntax *RawSyntax::append(const RawSyntax *NewLayoutElement) const { - auto Layout = getLayout(); - std::vector NewLayout; - NewLayout.reserve(Layout.size() + 1); - std::copy(Layout.begin(), Layout.end(), std::back_inserter(NewLayout)); - NewLayout.push_back(NewLayoutElement); - return RawSyntax::make(getKind(), NewLayout, SourcePresence::Present, Arena); -} - -const RawSyntax * -RawSyntax::replacingChild(CursorIndex Index, - const RawSyntax *NewLayoutElement) const { - auto Layout = getLayout(); - std::vector NewLayout; - NewLayout.reserve(Layout.size()); - - std::copy(Layout.begin(), Layout.begin() + Index, - std::back_inserter(NewLayout)); - - NewLayout.push_back(NewLayoutElement); - - std::copy(Layout.begin() + Index + 1, Layout.end(), - std::back_inserter(NewLayout)); - - return RawSyntax::make(getKind(), NewLayout, getPresence(), Arena); -} - -void RawSyntax::print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts) const { - if (isMissing()) - return; - - if (isToken()) { - OS << getLeadingTrivia(); - OS << getTokenText(); - OS << getTrailingTrivia(); - } else { - auto Kind = getKind(); - const bool PrintKind = Opts.PrintSyntaxKind && (Opts.PrintTrivialNodeKind || - !isTrivialSyntaxKind(Kind)); - if (PrintKind) - printSyntaxKind(Kind, OS, Opts, true); - - for (const auto &LE : getLayout()) - if (LE) - LE->print(OS, Opts); - - if (PrintKind) - printSyntaxKind(Kind, OS, Opts, false); - } -} - -void RawSyntax::dump() const { - dump(llvm::errs(), /*Indent*/ 0); - llvm::errs() << '\n'; -} - -void RawSyntax::dump(llvm::raw_ostream &OS, unsigned Indent) const { - auto indent = [&](unsigned Amount) { - for (decltype(Amount) i = 0; i < Amount; ++i) { - OS << ' '; - } - }; - - indent(Indent); - OS << '('; - dumpSyntaxKind(OS, getKind()); - - if (isMissing()) - OS << " [missing] "; - - if (isToken()) { - OS << " "; - dumpTokenKind(OS, getTokenKind()); - - for (auto &Leader : getLeadingTriviaPieces()) { - OS << "\n"; - Leader.dump(OS, Indent + 1); - } - - OS << "\n"; - indent(Indent + 1); - OS << "(text=\""; - OS.write_escaped(getTokenText(), /*UseHexEscapes=*/true); - OS << "\")"; - - for (auto &Trailer : getTrailingTriviaPieces()) { - OS << "\n"; - Trailer.dump(OS, Indent + 1); - } - } else { - for (auto &Child : getLayout()) { - if (!Child) - continue; - OS << "\n"; - Child->dump(OS, Indent + 1); - } - } - OS << ')'; -} - -void RawSyntax::Profile(llvm::FoldingSetNodeID &ID, tok TokKind, StringRef Text, - StringRef LeadingTrivia, StringRef TrailingTrivia) { - ID.AddInteger(unsigned(TokKind)); - ID.AddInteger(LeadingTrivia.size()); - ID.AddInteger(TrailingTrivia.size()); - switch (TokKind) { -#define TOKEN_DEFAULT(NAME) case tok::NAME: -#define PUNCTUATOR(NAME, X) TOKEN_DEFAULT(NAME) -#define KEYWORD(KW) TOKEN_DEFAULT(kw_##KW) -#define POUND_KEYWORD(KW) TOKEN_DEFAULT(pound_##KW) -#include "swift/Syntax/TokenKinds.def" - break; - default: - ID.AddString(Text); - break; - } - ID.AddString(LeadingTrivia); - ID.AddString(TrailingTrivia); -} diff --git a/lib/Syntax/Status.md b/lib/Syntax/Status.md deleted file mode 100644 index 2ffc9a3dbc380..0000000000000 --- a/lib/Syntax/Status.md +++ /dev/null @@ -1,114 +0,0 @@ -# libSyntax nodes status - -## Expression - -### Done: - * NilLiteralExpr - * IntegerLiteralExpr - * FloatLiteralExpr - * BooleanLiteralExpr - * StringLiteralExpr - * DiscardAssignmentExpr - * DeclRefExpr - * IfExpr - * AssignExpr - * TypeExpr - * UnresolvedMemberExpr - * SequenceExpr - * TupleElementExpr - * TupleExpr - * ArrayExpr - * DictionaryExpr - * PrefixUnaryExpr - * TryExpr - * ForceTryExpr - * OptionalTryExpr - * ClosureExpr - * FunctionCallExpr - * SubscriptExpr - * DotSelfExpr - * PostfixUnaryExpr - * ForcedValueExpr - * SuperRefExpr - * ImplicitMemberExpr - * KeyPathExpr - * KeyPathDotExpr - * InOutExpr - * EditorPlaceholderExpr - * ObjectLiteralExpr - * MagicIdentifierLiteralExpr - * SpecializeExpr - * UnresolvedPatternExpr - * IsExpr - * AsExpr - * ArrowExpr - * ObjCSelectorExpr - -## Declaration - -### Done: - * TopLevelCodeDecl - * ClassDecl - * StructDecl - * FuncDecl - * ProtocolDecl - * ImportDecl - * AssociatedTypeDecl - * TypeAliasDecl - * IfConfigDecl - * PatternBindingDecl - * VarDecl - * ExtensionDecl - * SubscriptDecl - * ConstructorDecl - * DestructorDecl - * EnumDecl - * EnumCaseDecl - * OperatorDecl - * PrecedenceGroupDecl - -## Statement -### Done: - * BraceStmt - * ReturnStmt - * DeferStmt - * DoStmt - * RepeatWhileStmt - * BreakStmt - * ContinueStmt - * FallthroughStmt - * ThrowStmt - * IfStmt - * GuardStmt - * WhileStmt - * ForInStmt - * SwitchStmt - * YieldStmt - -## Pattern -### Done: - * IdentifierPattern - * WildcardPattern - * TuplePattern - * ExpressionPattern - * ValueBindingPattern - * IsTypePattern - -### Not-started: - * AsTypePattern - * OptionalPattern - * EnumCasePattern - -## TypeRepr -### Done: - * SimpleTypeIdentifier - * MemberTypeIdentifier - * ArrayType - * DictionaryType - * MetatypeType - * OptionalType - * ImplicitlyUnwrappedOptionalType - * CompositionType - * TupleType - * FunctionType - * AttributedType diff --git a/lib/Syntax/Syntax.cpp b/lib/Syntax/Syntax.cpp deleted file mode 100644 index ded048dc75c17..0000000000000 --- a/lib/Syntax/Syntax.cpp +++ /dev/null @@ -1,79 +0,0 @@ -//===--- Syntax.cpp - Swift Syntax Implementation -------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/Syntax.h" -#include "swift/Syntax/SyntaxData.h" -#include "swift/Syntax/SyntaxVisitor.h" - -using namespace swift; -using namespace swift::syntax; - -const RawSyntax *Syntax::getRaw() const { return Data->getRaw(); } - -SyntaxKind Syntax::getKind() const { - return getRaw()->getKind(); -} - -void Syntax::print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts) const { - if (auto Raw = getRaw()) - Raw->print(OS, Opts); -} - -void Syntax::dump() const { - getRaw()->dump(); -} - -void Syntax::dump(llvm::raw_ostream &OS, unsigned Indent) const { - getRaw()->dump(OS, 0); -} - -bool Syntax::isType() const { return Data->isType(); } - -bool Syntax::isDecl() const { return Data->isDecl(); } - -bool Syntax::isStmt() const { return Data->isStmt(); } - -bool Syntax::isExpr() const { return Data->isExpr(); } - -bool Syntax::isToken() const { - return getRaw()->isToken(); -} - -bool Syntax::isPattern() const { return Data->isPattern(); } - -bool Syntax::isUnknown() const { return Data->isUnknown(); } - -bool Syntax::isPresent() const { - return getRaw()->isPresent(); -} - -bool Syntax::isMissing() const { - return getRaw()->isMissing(); -} - -llvm::Optional Syntax::getParent() const { - auto ParentData = getData()->getParent(); - if (!ParentData) { - return None; - } - return Syntax(ParentData); -} - -size_t Syntax::getNumChildren() const { return Data->getNumChildren(); } - -llvm::Optional Syntax::getChild(const size_t N) const { - auto ChildData = Data->getChild(N); - if (!ChildData) { - return None; - } - return Syntax(ChildData); -} diff --git a/lib/Syntax/SyntaxBuilders.cpp.gyb b/lib/Syntax/SyntaxBuilders.cpp.gyb deleted file mode 100644 index 95b0e6478c182..0000000000000 --- a/lib/Syntax/SyntaxBuilders.cpp.gyb +++ /dev/null @@ -1,73 +0,0 @@ - -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From SyntaxBuilders.cpp.gyb. -//// Do Not Edit Directly! -//===------------ SyntaxBuilders.cpp - Syntax Builder definitions ---------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - - -#include "swift/Syntax/SyntaxBuilders.h" - -using namespace swift; -using namespace swift::syntax; - -% for node in SYNTAX_NODES: -% if node.is_buildable(): -% for child in node.children: -${node.name}Builder & -${node.name}Builder::use${child.name}(${child.type_name} ${child.name}) { - Layout[cursorIndex(${node.name}::Cursor::${child.name})] = - ${child.name}.getRaw(); - return *this; -} -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child.collection_element_name -% child_elt_type = child_node.collection_element_type -% if not child_elt: -% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name)) -% end -${node.name}Builder & -${node.name}Builder::add${child_elt}(${child_elt_type} ${child_elt}) { - auto &raw = Layout[cursorIndex(${node.name}::Cursor::${child.name})]; - if (!raw) - raw = RawSyntax::make(SyntaxKind::${child_node.syntax_kind}, - {${child_elt}.getRaw()}, SourcePresence::Present, - Arena); - else - raw = raw->append(${child_elt}.getRaw()); - return *this; -} -% end -% end -${node.name} -${node.name}Builder::build() { -% if node.children: -% for (idx, child) in enumerate(node.children): -% if not child.is_optional: - if (!Layout[${idx}]) - Layout[${idx}] = ${make_missing_child(child)}; -% end -% end -% end - auto raw = RawSyntax::make(SyntaxKind::${node.syntax_kind}, Layout, - SourcePresence::Present, Arena); - return makeRoot<${node.name}>(raw); -} - -% end -% end diff --git a/lib/Syntax/SyntaxData.cpp b/lib/Syntax/SyntaxData.cpp deleted file mode 100644 index 1188a4a811d20..0000000000000 --- a/lib/Syntax/SyntaxData.cpp +++ /dev/null @@ -1,126 +0,0 @@ -//===--- SyntaxData.cpp - Swift Syntax Data Implementation ----------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/SyntaxData.h" - -using namespace swift; -using namespace swift::syntax; - -// MARK: - SyntaxDataRef - -void SyntaxDataRef::dump(llvm::raw_ostream &OS) const { - getRaw()->dump(OS, 0); - OS << '\n'; -} - -void SyntaxDataRef::dump() const { dump(llvm::errs()); } - -// MARK: - SyntaxData - -RC -SyntaxData::getChild(AbsoluteSyntaxPosition::IndexInParentType Index) const { - auto AbsoluteRaw = getAbsoluteRaw().getChild(Index); - if (AbsoluteRaw) { - return RC( - new SyntaxData(*AbsoluteRaw, /*Parent=*/RC(this))); - } else { - return nullptr; - } -} - -RC SyntaxData::getPreviousNode() const { - if (size_t N = getIndexInParent()) { - if (hasParent()) { - for (size_t I = N - 1; ; --I) { - if (auto C = getParent()->getChild(I)) { - if (C->getRaw()->isPresent() && C->getFirstToken()) - return C; - } - if (I == 0) - break; - } - } - } - return hasParent() ? getParent()->getPreviousNode() : nullptr; -} - -RC SyntaxData::getNextNode() const { - if (hasParent()) { - for (size_t I = getIndexInParent() + 1, N = getParent()->getNumChildren(); - I != N; ++I) { - if (auto C = getParent()->getChild(I)) { - if (C->getRaw()->isPresent() && C->getFirstToken()) - return C; - } - } - return getParent()->getNextNode(); - } - return nullptr; -} - -RC SyntaxData::getFirstToken() const { - /// getFirstToken and getLastToken cannot be implemented on SyntaxDataRef - /// because we might need to traverse through multiple nodes to reach the - /// first token. When returning this token, the parent nodes are being - /// discarded and thus its parent pointer would point to invalid memory. - if (getRaw()->isToken() && !getRaw()->isMissing()) { - return RC(this); - } - - for (size_t I = 0, E = getNumChildren(); I < E; ++I) { - if (auto Child = getChild(I)) { - if (Child->getRaw()->isMissing()) { - continue; - } - - if (auto Token = Child->getFirstToken()) { - return Token; - } - } - } - return nullptr; -} - -RC SyntaxData::getLastToken() const { - // Also see comment in getFirstToken. - if (getRaw()->isToken() && !getRaw()->isMissing()) { - return RC(this); - } - - if (getNumChildren() == 0) { - return nullptr; - } - for (int I = getNumChildren() - 1; I >= 0; --I) { - if (auto Child = getChild(I)) { - if (Child->getRaw()->isMissing()) { - continue; - } - - if (auto Token = Child->getLastToken()) { - return Token; - } - } - } - return nullptr; -} - -RC SyntaxData::replacingSelf(const RawSyntax *NewRaw) const { - if (hasParent()) { - auto NewParent = getParent()->replacingChild(NewRaw, getIndexInParent()); - auto NewSelf = AbsoluteRaw.replacingSelf( - NewRaw, NewParent->AbsoluteRaw.getNodeId().getRootId()); - return RC(new SyntaxData(NewSelf, NewParent)); - } else { - auto NewSelf = AbsoluteRawSyntax::forRoot(NewRaw); - return RC(new SyntaxData(NewSelf)); - } -} diff --git a/lib/Syntax/SyntaxFactory.cpp.gyb b/lib/Syntax/SyntaxFactory.cpp.gyb deleted file mode 100644 index 9867a2cd13739..0000000000000 --- a/lib/Syntax/SyntaxFactory.cpp.gyb +++ /dev/null @@ -1,368 +0,0 @@ -%{ - from gyb_syntax_support import * - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From SyntaxFactory.cpp.gyb. -//// Do Not Edit Directly! -//===--------- SyntaxFactory.cpp - Syntax Factory implementations ---------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/SyntaxFactory.h" -#include "swift/Syntax/SyntaxNodes.h" -#include "swift/Syntax/Trivia.h" -#include "llvm/ADT/ArrayRef.h" - -#include - -using namespace swift; -using namespace swift::syntax; - -TokenSyntax SyntaxFactory::makeToken(tok Kind, StringRef Text, - StringRef LeadingTrivia, - StringRef TrailingTrivia, - SourcePresence Presence) { - return makeRoot(RawSyntax::makeAndCalcLength(Kind, Text, - LeadingTrivia, TrailingTrivia, Presence, Arena)); -} - -UnknownSyntax -SyntaxFactory::makeUnknownSyntax(llvm::ArrayRef Tokens) { - auto RawTokens = llvm::map_iterator(Tokens.begin(), - [](const TokenSyntax &Token) -> const RawSyntax * { - return Token.getRaw(); - }); - auto Raw = RawSyntax::make(SyntaxKind::Unknown, RawTokens, Tokens.size(), - SourcePresence::Present, Arena); - return makeRoot(Raw); -} - -Syntax SyntaxFactory::makeBlankCollectionSyntax(SyntaxKind Kind) { - switch(Kind) { -% for node in SYNTAX_NODES: -% if node.is_syntax_collection(): - case SyntaxKind::${node.syntax_kind}: - return makeBlank${node.syntax_kind}(); -% end -% end - default: break; - } - llvm_unreachable("not collection kind."); -} - -std::pair -SyntaxFactory::countChildren(SyntaxKind Kind){ - switch(Kind) { -% for node in SYNTAX_NODES: -% if not node.is_syntax_collection(): - case SyntaxKind::${node.syntax_kind}: -% child_count = len(node.non_unexpected_children) -% non_optional_child_count = sum(0 if child.is_optional else 1 for child in node.non_unexpected_children) - return {${non_optional_child_count}, ${child_count}}; -% end -% end - default: - llvm_unreachable("bad syntax kind."); - } -} - -bool SyntaxFactory::canServeAsCollectionMemberRaw(SyntaxKind CollectionKind, - SyntaxKind MemberKind) { - switch (CollectionKind) { -% for node in SYNTAX_NODES: -% if node.is_syntax_collection(): - case SyntaxKind::${node.syntax_kind}: -% if node.collection_element_choices: -% element_checks = [] -% for choice in node.collection_element_choices: -% element_checks.append("" + choice + "Syntax::kindof(MemberKind)") -% end - return ${' || '.join(element_checks)}; -% else: - return ${node.collection_element_type}::kindof(MemberKind); -% end -% end -% end - default: - llvm_unreachable("Not collection kind."); - } -} - -bool SyntaxFactory::canServeAsCollectionMemberRaw(SyntaxKind CollectionKind, - const RawSyntax *Member) { - return canServeAsCollectionMemberRaw(CollectionKind, Member->getKind()); -} - -bool SyntaxFactory:: -canServeAsCollectionMember(SyntaxKind CollectionKind, Syntax Member) { - return canServeAsCollectionMemberRaw(CollectionKind, Member.getRaw()); -} - -const RawSyntax *SyntaxFactory::createRaw( - SyntaxKind Kind, - llvm::ArrayRef Elements -) { - switch (Kind) { -% for node in SYNTAX_NODES: - case SyntaxKind::${node.syntax_kind}: { -% if node.children: -% child_count = len(node.children) - const RawSyntax *Layout[${child_count}]; - unsigned I = 0; -% for (child_idx, child) in enumerate(node.children): - // child[${child_idx}] ${child.name} - if (I == Elements.size() || - !${check_child_condition_raw(child)}(Elements[I])) { -% if child.is_optional: - Layout[${child_idx}] = nullptr; -% else: - return nullptr; -% end - } else { - Layout[${child_idx}] = Elements[I]; - ++I; - } -% end - if (I != Elements.size()) - return nullptr; - return RawSyntax::make(Kind, Layout, SourcePresence::Present, Arena); -% elif node.is_syntax_collection(): - for (auto &E : Elements) { - if (!canServeAsCollectionMemberRaw(SyntaxKind::${node.syntax_kind}, E)) - return nullptr; - } - return RawSyntax::make(Kind, Elements, SourcePresence::Present, Arena); -% else: - return nullptr; -% end - } -% end - default: - return nullptr; - } -} - -Optional SyntaxFactory::createSyntax(SyntaxKind Kind, - llvm::ArrayRef Elements) { - std::vector Layout; - Layout.reserve(Elements.size()); - for (auto &E : Elements) - Layout.emplace_back(E.getRaw()); - - if (auto Raw = createRaw(Kind, Layout)) - return makeRoot(Raw); - else - return None; -} - -% for node in SYNTAX_NODES: -% if node.children: -% child_params = [] -% for child in node.children: -% param_type = child.type_name -% if child.is_optional: -% param_type = "llvm::Optional<%s>" % param_type -% child_params.append("%s %s" % (param_type, child.name)) -% child_params = ', '.join(child_params) -${node.name} -SyntaxFactory::make${node.syntax_kind}(${child_params}) { - auto Raw = RawSyntax::make(SyntaxKind::${node.syntax_kind}, { -% for child in node.children: -% if child.is_optional: - ${child.name}.hasValue() ? ${child.name}->getRaw() : nullptr, -% else: - ${child.name}.getRaw(), -% end -% end - }, SourcePresence::Present, Arena); - return makeRoot<${node.name}>(Raw); -} -% elif node.is_syntax_collection(): -${node.name} -SyntaxFactory::make${node.syntax_kind}( - const std::vector<${node.collection_element_type}> &elements) { - std::vector layout; - layout.reserve(elements.size()); - for (auto &element : elements) { - layout.push_back(element.getRaw()); - } - auto raw = RawSyntax::make(SyntaxKind::${node.syntax_kind}, layout, - SourcePresence::Present, Arena); - return makeRoot<${node.name}>(raw); -} -% end - -${node.name} -SyntaxFactory::makeBlank${node.syntax_kind}() { - auto raw = RawSyntax::make(SyntaxKind::${node.syntax_kind}, { -% for child in node.children: -% if child.is_optional: - nullptr, -% else: - ${make_missing_child(child)}, -% end -% end - }, SourcePresence::Present, Arena); - return makeRoot<${node.name}>(raw); -} -% end - -% for token in SYNTAX_TOKENS: -% if token.is_keyword: - TokenSyntax - SyntaxFactory::make${token.name}Keyword(StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeToken(tok::${token.kind}, "${token.text}", LeadingTrivia, - TrailingTrivia, SourcePresence::Present); - } -% elif token.text: - TokenSyntax - SyntaxFactory::make${token.name}Token(StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeToken(tok::${token.kind}, "${token.text}", LeadingTrivia, - TrailingTrivia, SourcePresence::Present); - } -% else: - TokenSyntax - SyntaxFactory::make${token.name}(StringRef Text, - StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeToken(tok::${token.kind}, Text, LeadingTrivia, TrailingTrivia, - SourcePresence::Present); - } -% end -% end - -TupleTypeSyntax SyntaxFactory::makeVoidTupleType() { - return makeTupleType( - /*UnexpectedNodes=*/None, - /*LeftParen=*/makeLeftParenToken({}, {}), - /*UnexpectedNodes=*/None, - /*Elements=*/makeBlankTupleTypeElementList(), - /*UnexpectedNodes=*/None, - /*RightParen=*/makeRightParenToken({}, {}), - /*UnexpectedNodes=*/None - ); -} - -TupleTypeElementSyntax -SyntaxFactory::makeTupleTypeElement(llvm::Optional Label, - llvm::Optional Colon, - TypeSyntax Type, - llvm::Optional TrailingComma) { - return makeTupleTypeElement( - /*UnexpectedNodes=*/None, - /*InOut=*/None, - /*UnexpectedNodes=*/None, - /*Name=*/Label, - /*UnexpectedNodes=*/None, - /*SecondName=*/None, - /*UnexpectedNodes=*/None, - /*Colon=*/Colon, - /*UnexpectedNodes=*/None, - /*Type=*/Type, - /*UnexpectedNodes=*/None, - /*Ellipsis=*/None, - /*UnexpectedNodes=*/None, - /*Intitializer=*/None, - /*UnexpectedNodes=*/None, - /*TrailingComma=*/TrailingComma, - /*UnexpectedNodes=*/None - ); -} - -TupleTypeElementSyntax -SyntaxFactory::makeTupleTypeElement(TypeSyntax Type, - llvm::Optional TrailingComma) { - return makeTupleTypeElement( - /*UnexpectedNodes=*/None, - /*InOut=*/None, - /*UnexpectedNodes=*/None, - /*Name=*/None, - /*UnexpectedNodes=*/None, - /*SecondName=*/None, - /*UnexpectedNodes=*/None, - /*Colon=*/None, - /*UnexpectedNodes=*/None, - /*Type=*/Type, - /*UnexpectedNodes=*/None, - /*Ellipsis=*/None, - /*UnexpectedNodes=*/None, - /*Initializer=*/None, - /*UnexpectedNodes=*/None, - /*TrailingComma=*/TrailingComma, - /*UnexpectedNodes=*/None - ); -} - -GenericParameterSyntax -SyntaxFactory::makeGenericParameter(TokenSyntax Name, - llvm::Optional TrailingComma) { - return makeGenericParameter( - /*UnexpectedNodes=*/None, - /*Attributes=*/None, - /*UnexpectedNodes=*/None, - /*Name=*/Name, - /*UnexpectedNodes=*/None, - /*Ellipsis=*/None, - /*UnexpectedNodes=*/None, - /*Colon=*/None, - /*UnexpectedNodes=*/None, - /*InheritedType=*/None, - /*UnexpectedNodes=*/None, - /*TrailingComma=*/TrailingComma, - /*UnexpectedNodes=*/None - ); -} - -TypeSyntax SyntaxFactory::makeTypeIdentifier(StringRef TypeName, - StringRef LeadingTrivia, - StringRef TrailingTrivia) { - auto identifier = makeIdentifier( - TypeName, - LeadingTrivia, - TrailingTrivia - ); - return makeSimpleTypeIdentifier( - /*UnexpectedNodes=*/None, - /*Name=*/identifier, - /*UnexpectedNodes=*/None, - /*GenerigArgumentClause=*/None, - /*UnexpectedNodes=*/None - ); -} - -TypeSyntax SyntaxFactory::makeAnyTypeIdentifier(StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeTypeIdentifier("Any", LeadingTrivia, TrailingTrivia); -} - -TypeSyntax SyntaxFactory::makeSelfTypeIdentifier(StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeTypeIdentifier("Self", LeadingTrivia, TrailingTrivia); -} - -TokenSyntax SyntaxFactory::makeTypeToken(StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeIdentifier("Type", LeadingTrivia, TrailingTrivia); -} - -TokenSyntax SyntaxFactory::makeProtocolToken(StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeIdentifier("Protocol", LeadingTrivia, TrailingTrivia); -} - -TokenSyntax SyntaxFactory::makeEqualityOperator(StringRef LeadingTrivia, - StringRef TrailingTrivia) { - return makeToken(tok::oper_binary_spaced, "==", LeadingTrivia, TrailingTrivia, - SourcePresence::Present); -} diff --git a/lib/Syntax/SyntaxKind.cpp.gyb b/lib/Syntax/SyntaxKind.cpp.gyb deleted file mode 100644 index 2b16108ea2067..0000000000000 --- a/lib/Syntax/SyntaxKind.cpp.gyb +++ /dev/null @@ -1,157 +0,0 @@ -%{ - from gyb_syntax_support import * - # -*- mode: C++ -*- - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From SyntaxKind.cpp.gyb. -//// Do Not Edit Directly! -//===-------------- SyntaxKind.cpp - Syntax Kind definitions --------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/SyntaxKind.h" -#include "swift/Syntax/TokenKinds.h" -#include "llvm/Support/raw_ostream.h" - -namespace swift { - -static StringRef getTokenTextInternal(tok kind) { - switch(kind) { -% for token in SYNTAX_TOKENS: -% if token.text: - case tok::${token.kind}: return "${token.text}"; -% end -% end - default: return StringRef(); - } -} - -bool isTokenTextDetermined(tok kind) { - return !getTokenTextInternal(kind).empty(); -} - -StringRef getTokenText(tok kind) { - auto text = getTokenTextInternal(kind); - assert(!text.empty() && "token kind cannot be determined"); - return text; -} - -bool parserShallOmitWhenNoChildren(syntax::SyntaxKind Kind) { - switch(Kind) { -% for node in SYNTAX_NODES: -% if node.shall_be_omitted_when_empty(): - case syntax::SyntaxKind::${node.syntax_kind}: -% end -% end - return true; - default: - return false; - } -} - -namespace syntax { - -void dumpSyntaxKind(llvm::raw_ostream &os, const SyntaxKind kind) { - switch (kind) { - case SyntaxKind::Token: - os << "Token"; - break; - case SyntaxKind::Unknown: - os << "Unknown"; - break; -% for node in SYNTAX_NODES: - case SyntaxKind::${node.syntax_kind}: - os << "${node.syntax_kind}"; - break; -% end - } -} - -bool isCollectionKind(SyntaxKind Kind) { - switch(Kind) { -% for node in SYNTAX_NODES: -% if node.is_syntax_collection(): - case SyntaxKind::${node.syntax_kind}: -% end -% end - return true; - default: - return false; - } -} - -bool isDeclKind(SyntaxKind Kind) { - return Kind >= SyntaxKind::First_Decl && Kind <= SyntaxKind::Last_Decl; -} - -bool isTypeKind(SyntaxKind Kind) { - return Kind >= SyntaxKind::First_Type && Kind <= SyntaxKind::Last_Type; -} - -bool isStmtKind(SyntaxKind Kind) { - return Kind >= SyntaxKind::First_Stmt && Kind <= SyntaxKind::Last_Stmt; -} - -bool isExprKind(SyntaxKind Kind) { - return Kind >= SyntaxKind::First_Expr && Kind <= SyntaxKind::Last_Expr; -} - -bool isPatternKind(SyntaxKind Kind) { - return Kind >= SyntaxKind::First_Pattern && - Kind <= SyntaxKind::Last_Pattern; -} - -bool isTokenKind(SyntaxKind Kind) { - return Kind == SyntaxKind::Token; -} - -bool isUnknownKind(SyntaxKind Kind) { - return Kind == SyntaxKind::Unknown || - Kind == SyntaxKind::UnknownDecl || - Kind == SyntaxKind::UnknownExpr || - Kind == SyntaxKind::UnknownStmt || - Kind == SyntaxKind::UnknownType || - Kind == SyntaxKind::UnknownPattern; -} - -SyntaxKind getUnknownKind(SyntaxKind Kind) { - if (isExprKind(Kind)) - return SyntaxKind::UnknownExpr; - if (isStmtKind(Kind)) - return SyntaxKind::UnknownStmt; - if (isDeclKind(Kind)) - return SyntaxKind::UnknownDecl; - if (isTypeKind(Kind)) - return SyntaxKind::UnknownType; - if (isPatternKind(Kind)) - return SyntaxKind::UnknownPattern; - return SyntaxKind::Unknown; -} -} // end namespace syntax -} // end namespace swift - -llvm::raw_ostream &llvm::operator<<(llvm::raw_ostream &OS, - swift::syntax::SyntaxKind Kind) { - switch (Kind) { -% for node in SYNTAX_NODES: - case swift::syntax::SyntaxKind::${node.syntax_kind}: - OS << "${node.syntax_kind}"; - break; -% end - case swift::syntax::SyntaxKind::Token: - OS << "TokenSyntax"; - break; - case swift::syntax::SyntaxKind::Unknown: - OS << "UnknownSyntax"; - break; - } - return OS; -} diff --git a/lib/Syntax/SyntaxNodes.cpp.gyb b/lib/Syntax/SyntaxNodes.cpp.gyb deleted file mode 100644 index 4c963c05f0ef9..0000000000000 --- a/lib/Syntax/SyntaxNodes.cpp.gyb +++ /dev/null @@ -1,119 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From SyntaxNodes.cpp.gyb. -//// Do Not Edit Directly! -//===---------------- SyntaxNodes.cpp - Syntax Node definitions -----------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - - -#include "swift/Syntax/SyntaxNodes.h" - -using namespace swift; -using namespace swift::syntax; - -% for node in SYNTAX_NODES: -% if not node.is_syntax_collection(): -void ${node.name}Ref::validate() const { -% if node.requires_validation(): -#ifndef NDEBUG - auto raw = getDataRef()->getRaw(); - if (isMissing()) return; - assert(raw->getLayout().size() == ${len(node.children)}); -% for child in node.children: -% if child.token_choices: -% choices = ", ".join("tok::" + choice.kind -% for choice in child.token_choices) - syntax_assert_child_token(raw, ${child.name}, ${choices}); -% end -% if child.main_token() and child.text_choices: -% token_kind = child.main_token().kind -% choices = ", ".join("\"%s\"" % choice -% for choice in child.text_choices) - syntax_assert_child_token_text(raw, ${child.name}, - tok::${token_kind}, ${choices}); -% end -% if child.node_choices: - if (auto __Child = raw->getChild(Cursor::${child.name})) - assert(${check_child_condition_raw(child)}(__Child)); -% end -% end -#endif -% end -} - -void ${node.name}::validate() const { - ${node.name}Ref(*this).validate(); -} - -% end - -% for child in node.children: -% if child.is_optional: -llvm::Optional<${child.type_name}> ${node.name}::get${child.name}() const { - auto ChildData = Data->getChild(Cursor::${child.name}); - if (!ChildData) - return llvm::None; - return ${child.type_name}(ChildData); -} -% else: -${child.type_name} ${node.name}::get${child.name}() const { - return ${child.type_name}(Data->getChild(Cursor::${child.name})); -} -% end - -% child_node = NODE_MAP.get(child.syntax_kind) -% if child_node and child_node.is_syntax_collection(): -% child_elt = child.collection_element_name -% child_elt_type = child_node.collection_element_type -% if not child_elt: -% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name)) -% end -${node.name} ${node.name}::add${child_elt}(${child_elt_type} ${child_elt}) { - const RawSyntax *raw = getRaw()->getChild(Cursor::${child.name}); - if (raw) - raw = raw->append(${child_elt}.getRaw()); - else - raw = RawSyntax::make(SyntaxKind::${child_node.syntax_kind}, - {${child_elt}.getRaw()}, SourcePresence::Present, raw->getArena()); - return ${node.name}(Data->replacingChild(raw, Cursor::${child.name})); -} -% end - -${node.name} ${node.name}::with${child.name}( - llvm::Optional<${child.type_name}> New${child.type_name}) { - const RawSyntax *raw; - if (New${child.type_name}.hasValue()) { - raw = New${child.type_name}->getRaw(); - } else { -% if child.is_optional: - raw = nullptr; -% else: - { - // make_missing_child access the 'Arena' variable. Create it. - RC Arena = getRaw()->getArena(); - raw = ${make_missing_child(child)}; - } -% end - } - return ${node.name}(Data->replacingChild(raw, Cursor::${child.name})); -} - -% end -% end - -const char* swift::syntax::getSyntaxStructureVersioningIdentifier() { - return "${calculate_node_hash()}"; -} diff --git a/lib/Syntax/SyntaxSerialization.cpp.gyb b/lib/Syntax/SyntaxSerialization.cpp.gyb deleted file mode 100644 index 05fbb25d5c20d..0000000000000 --- a/lib/Syntax/SyntaxSerialization.cpp.gyb +++ /dev/null @@ -1,85 +0,0 @@ -%{ - from gyb_syntax_support import * - from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS - grouped_nodes = { kind: [] for kind in SYNTAX_BASE_KINDS } - for node in SYNTAX_NODES: - grouped_nodes[node.base_kind].append(node) - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From SyntaxSerialization.cpp.gyb. -//// Do Not Edit Directly! -//===---------------------- SyntaxSerialization.cpp -----------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/Serialization/SyntaxSerialization.h" - -namespace swift { -namespace serialization { - -uint16_t getNumericValue(syntax::SyntaxKind Kind) { - switch (Kind) { - case syntax::SyntaxKind::Token: - return 0; - case syntax::SyntaxKind::Unknown: - return 1; -% for name, nodes in grouped_nodes.items(): -% for node in nodes: - case syntax::SyntaxKind::${node.syntax_kind}: - return ${SYNTAX_NODE_SERIALIZATION_CODES[node.syntax_kind]}; -% end -% end - } - llvm_unreachable("unhandled kind"); -} - -uint8_t getNumericValue(syntax::TriviaKind Kind) { - switch (Kind) { -% for trivia in TRIVIAS: - case syntax::TriviaKind::${trivia.name}: return ${trivia.serialization_code}; -% end - } - llvm_unreachable("unhandled kind"); -} - -uint8_t getNumericValue(tok Value) { - switch (Value) { - case tok::eof: return 0; - -% for token in SYNTAX_TOKENS: - case tok::${token.kind}: return ${token.serialization_code}; -% end - - case tok::kw_undef: - case tok::kw_sil: - case tok::kw_sil_stage: - case tok::kw_sil_property: - case tok::kw_sil_vtable: - case tok::kw_sil_moveonlydeinit: - case tok::kw_sil_global: - case tok::kw_sil_witness_table: - case tok::kw_sil_default_witness_table: - case tok::kw_sil_differentiability_witness: - case tok::kw_sil_coverage_map: - case tok::kw_sil_scope: - case tok::sil_dollar: - case tok::sil_exclamation: - case tok::code_complete: - case tok::sil_local_name: - case tok::comment: - case tok::NUM_TOKENS: - llvm_unreachable("Should not get serialized in a syntax tree"); - } - llvm_unreachable("unhandled token"); -} - -} // end namespace serialization -} // end namespace swift diff --git a/lib/Syntax/SyntaxVisitor.cpp.gyb b/lib/Syntax/SyntaxVisitor.cpp.gyb deleted file mode 100644 index 722ab5d46f4e7..0000000000000 --- a/lib/Syntax/SyntaxVisitor.cpp.gyb +++ /dev/null @@ -1,54 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - NODE_MAP = create_node_map() - # Ignore the following admonition; it applies to the resulting .cpp file only -}% -//// Automatically Generated From SyntaxVisitor.cpp.gyb. -//// Do Not Edit Directly! -//===------------- SyntaxVisitor.cpp - Syntax Visitor definitions ---------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/SyntaxVisitor.h" -#include "swift/Basic/Defer.h" - -% for node in SYNTAX_NODES: -% if is_visitable(node): -void swift::syntax::SyntaxVisitor::visit(${node.name} node) { - visitChildren(node); -} -% end -% end - -void swift::syntax::SyntaxVisitor::visit(Syntax node) { - visitPre(node); - SWIFT_DEFER { visitPost(node); }; - switch (node.getKind()) { - case SyntaxKind::Token: - visit(node.castTo()); - return; -% for node in SYNTAX_NODES: -% if is_visitable(node): - case SyntaxKind::${node.syntax_kind}: - visit(node.castTo<${node.name}>()); - return; -% end -% end - default: - visitChildren(node); - return; - } -} - -void swift::syntax::Syntax::accept(SyntaxVisitor &visitor) { - visitor.visit(*this); -} diff --git a/lib/Syntax/Trivia.cpp.gyb b/lib/Syntax/Trivia.cpp.gyb deleted file mode 100644 index 3b1abae27fbe0..0000000000000 --- a/lib/Syntax/Trivia.cpp.gyb +++ /dev/null @@ -1,155 +0,0 @@ -%{ - # -*- mode: C++ -*- - from gyb_syntax_support import * - from gyb_syntax_support.Trivia import TRIVIAS - # Ignore the following admonition; it applies to the resulting .h file only -}% -//// Automatically Generated From Trivia.cpp.gyb. -//// Do Not Edit Directly! -//===--- Trivia.cpp - Swift Syntax Trivia Implementation ------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/Trivia.h" - -using namespace swift; -using namespace swift::syntax; - -TriviaPiece TriviaPiece::fromText(TriviaKind kind, StringRef text) { - switch (kind) { -% for trivia in TRIVIAS: - case TriviaKind::${trivia.name}: -% if trivia.is_collection(): - assert(text.size() % ${trivia.characters_len()} == 0); - return TriviaPiece(kind, text.size()/${trivia.characters_len()}); -% else: - return TriviaPiece(kind, OwnedString::makeRefCounted(text)); -% end -% end - } - llvm_unreachable("Unhandled TriviaKind in switch"); -} - -void TriviaPiece::dump(llvm::raw_ostream &OS, unsigned Indent) const { - for (decltype(Count) i = 0; i < Indent; ++i) - OS << ' '; - OS << "(trivia "; - - switch (Kind) { -% for trivia in TRIVIAS: - case TriviaKind::${trivia.name}: - OS << "${trivia.lower_name} "; -% if trivia.is_collection(): - OS << Count; -% else: - OS.write_escaped(Text.str()); -% end - break; -% end - } - OS << ')'; -} - -bool syntax::isCommentTriviaKind(TriviaKind Kind) { - switch (Kind) { -% for trivia in TRIVIAS: - case TriviaKind::${trivia.name}: -% if trivia.is_comment: - return true; -% else: - return false; -% end -% end - } - llvm_unreachable("unknown kind"); -} - -bool TriviaPiece::trySquash(const TriviaPiece &Next) { - if (Kind != Next.Kind) { return false; } - switch (Kind) { -% for trivia in TRIVIAS: - case TriviaKind::${trivia.name}: -% if trivia.is_collection(): - Count+= Next.Count; - return true; -% else: - return false; -% end -% end - } - llvm_unreachable("unknown kind"); -} - -void TriviaPiece::print(llvm::raw_ostream &OS) const { - switch (Kind) { -% for trivia in TRIVIAS: - case TriviaKind::${trivia.name}: -% if trivia.is_collection(): -% joined = ''.join(trivia.characters) - for (unsigned i = 0; i < Count; i ++) - OS << "${joined}"; -% else: - OS << Text.str(); -% end - return; -% end - } -} - -#pragma mark - Trivia collection - -void Trivia::appendOrSquash(const TriviaPiece &Next) { - if (Pieces.size() > 0) { - TriviaPiece &last = Pieces.back(); - if (last.trySquash(Next)) { - return; - } - } - - push_back(Next); -} - -Trivia Trivia::appending(const Trivia &Other) const { - auto NewPieces = Pieces; - std::copy(Other.begin(), Other.end(), std::back_inserter(NewPieces)); - return { NewPieces }; -} - -void Trivia::dump(llvm::raw_ostream &OS, unsigned Indent) const { - for (const auto &Piece : Pieces) { - Piece.dump(OS, Indent); - } -} - -void Trivia::dump() const { - dump(llvm::errs()); -} - -void Trivia::print(llvm::raw_ostream &OS) const { - for (const auto &Piece : Pieces) { - Piece.print(OS); - } -} - -TriviaList::const_iterator Trivia::find(const TriviaKind DesiredKind) const { - return std::find_if(Pieces.begin(), Pieces.end(), - [=](const TriviaPiece &Piece) -> bool { - return Piece.getKind() == DesiredKind; - }); -} - -Trivia Trivia::operator+(const Trivia &Other) const { - auto NewPieces = Pieces; - std::copy(Other.Pieces.begin(), Other.Pieces.end(), - std::back_inserter(NewPieces)); - return { NewPieces }; -} diff --git a/lib/Syntax/UnknownSyntax.cpp b/lib/Syntax/UnknownSyntax.cpp deleted file mode 100644 index 4616582d0b0b3..0000000000000 --- a/lib/Syntax/UnknownSyntax.cpp +++ /dev/null @@ -1,19 +0,0 @@ -//===--- UnknownSyntax.cpp - Swift Unknown Syntax Implementation ---------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/Syntax/TokenSyntax.h" -#include "swift/Syntax/UnknownSyntax.h" - -using namespace swift; -using namespace swift::syntax; - -void UnknownSyntax::validate() const { assert(Data->getRaw()->isUnknown()); } diff --git a/lib/SyntaxParse/CMakeLists.txt b/lib/SyntaxParse/CMakeLists.txt deleted file mode 100644 index dec2fa7a789c6..0000000000000 --- a/lib/SyntaxParse/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ - -add_swift_host_library(swiftSyntaxParse STATIC - SyntaxTreeCreator.cpp) -target_link_libraries(swiftSyntaxParse PRIVATE - swiftParse - swiftSyntax) - -set_swift_llvm_is_available(swiftSyntaxParse) diff --git a/lib/SyntaxParse/SyntaxTreeCreator.cpp b/lib/SyntaxParse/SyntaxTreeCreator.cpp deleted file mode 100644 index 9fffd91a905f0..0000000000000 --- a/lib/SyntaxParse/SyntaxTreeCreator.cpp +++ /dev/null @@ -1,261 +0,0 @@ -//===--- SyntaxTreeCreator.cpp - Syntax Tree Creation ----------*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include "swift/SyntaxParse/SyntaxTreeCreator.h" -#include "swift/AST/ASTContext.h" -#include "swift/AST/DiagnosticsParse.h" -#include "swift/AST/Module.h" -#include "swift/AST/SourceFile.h" -#include "swift/Basic/OwnedString.h" -#include "swift/Basic/SourceManager.h" -#include "swift/Parse/ParsedRawSyntaxNode.h" -#include "swift/Parse/ParsedTrivia.h" -#include "swift/Parse/SyntaxParsingCache.h" -#include "swift/Parse/Token.h" -#include "swift/Syntax/RawSyntax.h" -#include "swift/Syntax/SyntaxVisitor.h" -#include "swift/Syntax/Trivia.h" - -using namespace swift; -using namespace swift::syntax; - -SyntaxTreeCreator::SyntaxTreeCreator(SourceManager &SM, unsigned bufferID, - SyntaxParsingCache *syntaxCache, - RC arena) - : SM(SM), BufferID(bufferID), Arena(std::move(arena)), - SyntaxCache(syntaxCache) { - StringRef BufferContent = SM.getEntireTextForBuffer(BufferID); - const char *Data = BufferContent.data(); - Arena->copyStringToArenaIfNecessary(Data, BufferContent.size()); - ArenaSourceBuffer = StringRef(Data, BufferContent.size()); - if (!ArenaSourceBuffer.empty()) { - Arena->setHotUseMemoryRegion(ArenaSourceBuffer.begin(), - ArenaSourceBuffer.end()); - } -} - -SyntaxTreeCreator::~SyntaxTreeCreator() = default; - -namespace { -/// This verifier traverses a syntax node to emit proper diagnostics. -class SyntaxVerifier: public SyntaxVisitor { - SourceManager &SourceMgr; - unsigned BufferID; - DiagnosticEngine &Diags; - - template - SourceLoc getSourceLoc(T Node) { - return SourceMgr.getLocForOffset(BufferID, - Node.getAbsolutePosition().getOffset()); - } -public: - SyntaxVerifier( SourceManager &SM, unsigned bufID, DiagnosticEngine &diags) - : SourceMgr(SM), BufferID(bufID), Diags(diags) {} - - void visit(UnknownDeclSyntax Node) override { - Diags.diagnose(getSourceLoc(Node), diag::unknown_syntax_entity, - "declaration"); - visitChildren(Node); - } - void visit(UnknownExprSyntax Node) override { - Diags.diagnose(getSourceLoc(Node), diag::unknown_syntax_entity, - "expression"); - visitChildren(Node); - } - void visit(UnknownStmtSyntax Node) override { - Diags.diagnose(getSourceLoc(Node), diag::unknown_syntax_entity, - "statement"); - visitChildren(Node); - } - void visit(UnknownTypeSyntax Node) override { - Diags.diagnose(getSourceLoc(Node), diag::unknown_syntax_entity, - "type"); - visitChildren(Node); - } - void visit(UnknownPatternSyntax Node) override { - Diags.diagnose(getSourceLoc(Node), diag::unknown_syntax_entity, - "pattern"); - visitChildren(Node); - } - void verify(Syntax Node) { - Node.accept(*this); - } -}; -} // anonymous namespace - -Optional -SyntaxTreeCreator::realizeSyntaxRoot(OpaqueSyntaxNode rootN, - const SourceFile &SF) { - auto raw = static_cast(rootN); - auto rootNode = makeRoot(raw); - - // Verify the tree if specified. - if (SF.getASTContext().LangOpts.VerifySyntaxTree) { - ASTContext &ctx = SF.getASTContext(); - SyntaxVerifier Verifier(ctx.SourceMgr, SF.getBufferID().getValue(), - ctx.Diags); - Verifier.verify(rootNode); - } - return rootNode; -} - -OpaqueSyntaxNode SyntaxTreeCreator::recordToken(tok tokenKind, - StringRef leadingTrivia, - StringRef trailingTrivia, - CharSourceRange range) { - unsigned tokLength = - range.getByteLength() - leadingTrivia.size() - trailingTrivia.size(); - auto leadingTriviaStartOffset = - SM.getLocOffsetInBuffer(range.getStart(), BufferID); - auto tokStartOffset = leadingTriviaStartOffset + leadingTrivia.size(); - auto trailingTriviaStartOffset = tokStartOffset + tokLength; - - // Get StringRefs of the token's texts that point into the syntax arena's - // buffer. - StringRef leadingTriviaText = - ArenaSourceBuffer.substr(leadingTriviaStartOffset, leadingTrivia.size()); - StringRef tokenText = ArenaSourceBuffer.substr(tokStartOffset, tokLength); - StringRef trailingTriviaText = ArenaSourceBuffer.substr( - trailingTriviaStartOffset, trailingTrivia.size()); - - auto raw = RawSyntax::make(tokenKind, tokenText, range.getByteLength(), - leadingTriviaText, trailingTriviaText, - SourcePresence::Present, Arena); - return static_cast(raw); -} - -OpaqueSyntaxNode -SyntaxTreeCreator::recordMissingToken(tok kind, SourceLoc loc) { - auto raw = RawSyntax::missing(kind, getTokenText(kind), Arena); - return static_cast(raw); -} - -OpaqueSyntaxNode -SyntaxTreeCreator::recordRawSyntax(syntax::SyntaxKind kind, - ArrayRef elements) { - const RawSyntax *const *rawChildren = - reinterpret_cast(elements.begin()); - auto raw = RawSyntax::make(kind, rawChildren, elements.size(), - SourcePresence::Present, Arena); - return static_cast(raw); -} - -std::pair -SyntaxTreeCreator::lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) { - if (!SyntaxCache) - return {0, nullptr}; - auto cacheLookup = SyntaxCache->lookUp(lexerOffset, kind); - if (!cacheLookup) - return {0, nullptr}; - const RawSyntax *raw = cacheLookup->getRaw(); - size_t length = raw->getTextLength(); - return {length, static_cast(raw)}; -} - -OpaqueSyntaxNode SyntaxTreeCreator::makeDeferredToken(tok tokenKind, - StringRef leadingTrivia, - StringRef trailingTrivia, - CharSourceRange range, - bool isMissing) { - // Instead of creating dedicated deferred nodes that will be recorded only if - // needed, the SyntaxTreeCreator always records all nodes and forms RawSyntax - // nodes for them. This eliminates a bunch of copies that would otherwise - // be required to record the deferred nodes. - // Should a deferred node not be recorded, its data stays alive in the - // SyntaxArena. This causes a small memory leak but since most nodes are - // being recorded, it is acceptable. - if (isMissing) { - auto Node = recordMissingToken(tokenKind, range.getStart()); - return Node; - } else { - auto Node = recordToken(tokenKind, leadingTrivia, trailingTrivia, range); - return Node; - } -} - -OpaqueSyntaxNode SyntaxTreeCreator::makeDeferredLayout( - syntax::SyntaxKind kind, bool IsMissing, - const MutableArrayRef &parsedChildren) { - assert(!IsMissing && "Missing layout nodes not implemented yet"); - - auto rawChildren = llvm::map_iterator( - parsedChildren.begin(), - [](ParsedRawSyntaxNode &parsedChild) -> const RawSyntax * { - return static_cast(parsedChild.takeData()); - }); - auto raw = RawSyntax::make(kind, rawChildren, parsedChildren.size(), - SourcePresence::Present, Arena); - return static_cast(raw); -} - -OpaqueSyntaxNode -SyntaxTreeCreator::recordDeferredToken(OpaqueSyntaxNode deferred) { - // We don't differentiate between deferred and recorded nodes. See comment in - // makeDeferredToken. - return deferred; -} - -OpaqueSyntaxNode -SyntaxTreeCreator::recordDeferredLayout(OpaqueSyntaxNode deferred) { - // We don't differentiate between deferred and recorded nodes. See comment in - // makeDeferredToken. - return deferred; -} - -DeferredNodeInfo SyntaxTreeCreator::getDeferredChild(OpaqueSyntaxNode node, - size_t ChildIndex) const { - const RawSyntax *raw = static_cast(node); - - const RawSyntax *Child = raw->getChild(ChildIndex); - if (Child == nullptr) { - return DeferredNodeInfo( - RecordedOrDeferredNode(nullptr, RecordedOrDeferredNode::Kind::Null), - syntax::SyntaxKind::Unknown, tok::NUM_TOKENS, /*IsMissing=*/false); - } else if (Child->isToken()) { - return DeferredNodeInfo( - RecordedOrDeferredNode(Child, - RecordedOrDeferredNode::Kind::DeferredToken), - syntax::SyntaxKind::Token, Child->getTokenKind(), Child->isMissing()); - } else { - return DeferredNodeInfo( - RecordedOrDeferredNode(Child, - RecordedOrDeferredNode::Kind::DeferredLayout), - Child->getKind(), tok::NUM_TOKENS, - /*IsMissing=*/false); - } -} - -CharSourceRange SyntaxTreeCreator::getDeferredChildRange( - OpaqueSyntaxNode node, size_t ChildIndex, SourceLoc StartLoc) const { - const RawSyntax *raw = static_cast(node); - - // Compute the start offset of the child node by advancing StartLoc by the - // length of all previous child nodes. - for (unsigned i = 0; i < ChildIndex; ++i) { - const RawSyntax *child = raw->getChild(i); - if (child) { - StartLoc = StartLoc.getAdvancedLoc(child->getTextLength()); - } - } - - const RawSyntax *Child = raw->getChild(ChildIndex); - if (Child == nullptr) { - return CharSourceRange(StartLoc, /*Length=*/0); - } else { - return CharSourceRange(StartLoc, Child->getTextLength()); - } -} - -size_t SyntaxTreeCreator::getDeferredNumChildren(OpaqueSyntaxNode node) { - const syntax::RawSyntax *raw = static_cast(node); - return raw->getNumChildren(); -} diff --git a/stdlib/public/CMakeLists.txt b/stdlib/public/CMakeLists.txt index d396f547bde6e..4bfc77b9cd6ab 100644 --- a/stdlib/public/CMakeLists.txt +++ b/stdlib/public/CMakeLists.txt @@ -15,8 +15,6 @@ if(SWIFT_RUNTIME_USE_SANITIZERS) endif() endif() -list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "-Xfrontend" "-verify-syntax-tree") - if(SWIFT_STDLIB_SIL_DEBUGGING) list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "-Xfrontend" "-sil-based-debuginfo") endif() diff --git a/test/AutoDiff/Syntax/Outputs/round_trip_parse_gen.swift.withkinds b/test/AutoDiff/Syntax/Outputs/round_trip_parse_gen.swift.withkinds deleted file mode 100644 index ff542eff25ad3..0000000000000 --- a/test/AutoDiff/Syntax/Outputs/round_trip_parse_gen.swift.withkinds +++ /dev/null @@ -1,122 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -// Note: RUN lines copied from test/Syntax/round_trip_parse_gen.swift. - -@differentiable(reverse) -func bar(_ x: Float, _: Float) -> Float { return 1 } - -@differentiable(reverse where T : FloatingPoint) -func bar<T : Numeric>(_ x: T, _: T) -> T { return 1 } - -@differentiable(reverse, wrt: x) -func bar(_ x: Float, _: Float) -> Float { return 1 } - -@differentiable(reverse, wrt: (self, x, y)) -func bar(_ x: Float, y: Float) -> Float { return 1 } - -@differentiable(reverse, wrt: (self, x, y) where T : FloatingPoint) -func bar<T : Numeric>(_ x: T, y: T) -> T { return 1 } - -@derivative(of: -) -func negateDerivative(_ x: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (-x, { v in -v }) -} - -@derivative(of: baz(label:_:), wrt: (x)) -func bazDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: baz(label:_:).set, wrt: (x)) -func bazSetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: baz(label:_:).get, wrt: (x)) -func bazGetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@transpose(of: -) -func negateDerivative(_ x: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (-x, { v in -v }) -} - -@derivative(of: baz(label:_:), wrt: (x)) -func bazDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: A<T>.B<U, V>.C.foo(label:_:), wrt: x) -func qualifiedDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: A<T>.B<U, V>.C.foo(label:_:).get, wrt: x) -func qualifiedGetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: A<T>.B<U, V>.C.foo(label:_:).set, wrt: x) -func qualifiedSetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@transpose(of: +) -func addTranspose(_ v: Float) -> (Float, Float) { - return (v, v) -} - -@transpose(of: -, wrt: (0, 1)) -func subtractTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: Float.-, wrt: (0, 1)) -func subtractTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: Float.-.get, wrt: (0, 1)) -func subtractGetTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: Float.-.set, wrt: (0, 1)) -func subtractSetTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@derivative(of: A<T>.B<U, V>.C.foo(label:_:), wrt: 0) -func qualifiedTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: A<T>.B<U, V>.C.foo(label:_:).get, wrt: 0) -func qualifiedGetTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@derivative(of: subscript(_:).set, wrt: (self, newValue)) -func subscriptSetterDerivative() {} - -@transpose(of: subscript(_:).set, wrt: (self, 0) -func subscriptSetterTranspose() {} diff --git a/test/AutoDiff/Syntax/round_trip_parse_gen.swift b/test/AutoDiff/Syntax/round_trip_parse_gen.swift deleted file mode 100644 index 0b6f165449cfe..0000000000000 --- a/test/AutoDiff/Syntax/round_trip_parse_gen.swift +++ /dev/null @@ -1,122 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -// Note: RUN lines copied from test/Syntax/round_trip_parse_gen.swift. - -@differentiable(reverse) -func bar(_ x: Float, _: Float) -> Float { return 1 } - -@differentiable(reverse where T : FloatingPoint) -func bar(_ x: T, _: T) -> T { return 1 } - -@differentiable(reverse, wrt: x) -func bar(_ x: Float, _: Float) -> Float { return 1 } - -@differentiable(reverse, wrt: (self, x, y)) -func bar(_ x: Float, y: Float) -> Float { return 1 } - -@differentiable(reverse, wrt: (self, x, y) where T : FloatingPoint) -func bar(_ x: T, y: T) -> T { return 1 } - -@derivative(of: -) -func negateDerivative(_ x: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (-x, { v in -v }) -} - -@derivative(of: baz(label:_:), wrt: (x)) -func bazDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: baz(label:_:).set, wrt: (x)) -func bazSetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: baz(label:_:).get, wrt: (x)) -func bazGetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@transpose(of: -) -func negateDerivative(_ x: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (-x, { v in -v }) -} - -@derivative(of: baz(label:_:), wrt: (x)) -func bazDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: A.B.C.foo(label:_:), wrt: x) -func qualifiedDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: A.B.C.foo(label:_:).get, wrt: x) -func qualifiedGetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@derivative(of: A.B.C.foo(label:_:).set, wrt: x) -func qualifiedSetDerivative(_ x: Float, y: Float) - -> (value: Float, pullback: (Float) -> Float) { - return (x, { v in v }) -} - -@transpose(of: +) -func addTranspose(_ v: Float) -> (Float, Float) { - return (v, v) -} - -@transpose(of: -, wrt: (0, 1)) -func subtractTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: Float.-, wrt: (0, 1)) -func subtractTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: Float.-.get, wrt: (0, 1)) -func subtractGetTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: Float.-.set, wrt: (0, 1)) -func subtractSetTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@derivative(of: A.B.C.foo(label:_:), wrt: 0) -func qualifiedTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@transpose(of: A.B.C.foo(label:_:).get, wrt: 0) -func qualifiedGetTranspose(_ v: Float) -> (Float, Float) { - return (v, -v) -} - -@derivative(of: subscript(_:).set, wrt: (self, newValue)) -func subscriptSetterDerivative() {} - -@transpose(of: subscript(_:).set, wrt: (self, 0) -func subscriptSetterTranspose() {} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 34759ea6dadd2..03175207e5cf6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -65,8 +65,7 @@ function(get_test_dependencies SDK result_var_name) swift-llvm-opt swift-refactor swift-reflection-dump - swift-remoteast-test - swift-syntax-test) + swift-remoteast-test) if(SWIFT_BUILD_SOURCEKIT) list(APPEND deps_binaries sourcekitd-test complete-test) diff --git a/test/Concurrency/Runtime/reasync.swift b/test/Concurrency/Runtime/reasync.swift index 8a8562eccecbc..88c0b97496c24 100644 --- a/test/Concurrency/Runtime/reasync.swift +++ b/test/Concurrency/Runtime/reasync.swift @@ -1,5 +1,5 @@ // RUN: %empty-directory(%t) -// RUN: %target-swift-frontend -emit-module-path %t/reasync.swiftmodule %S/Inputs/reasync.swift -enable-experimental-concurrency -disable-availability-checking -verify-syntax-tree +// RUN: %target-swift-frontend -emit-module-path %t/reasync.swiftmodule %S/Inputs/reasync.swift -enable-experimental-concurrency -disable-availability-checking // RUN: %target-build-swift %s -I %t -o %t/main -module-name main // RUN: %target-codesign %t/main // RUN: %target-run %t/main diff --git a/test/Frontend/emit-syntax.swift b/test/Frontend/emit-syntax.swift deleted file mode 100644 index 54f51e0318999..0000000000000 --- a/test/Frontend/emit-syntax.swift +++ /dev/null @@ -1,60 +0,0 @@ -// RUN: %target-swift-frontend -emit-syntax %s | %FileCheck %s - -// CHECK: "kind":"kw_struct" -// CHECK: "kind":"identifier", -// CHECK: "text":"Foo" -// CHECK: "kind":"l_brace" -struct Foo { - // CHECK: "kind":"kw_let" - // CHECK: "kind":"colon" - // CHECK: "kind":"identifier" - // CHECK: "text":"Int" - let x: Int -// CHECK: "kind":"r_brace" -} - -// CHECK: "kind":"kw_func" -// CHECK: "kind":"identifier" -// CHECK: "text":"Bar" -// CHECK: "kind":"l_paren" -// CHECK: "kind":"FunctionParameterList" -// CHECK: "kind":"FunctionParameter" -// CHECK: "kind":"identifier" -// CHECK: "text":"arg1" -// CHECK: "kind":"colon" -// CHECK: "kind":"identifier" -// CHECK: "text":"String" -// CHECK: null,null -// CHECK: "kind":"comma" -// CHECK: "kind":"identifier" -// CHECK: "text":"arg2" -// CHECK: "kind":"colon" -// CHECK: "kind":"identifier" -// CHECK: "text":"Int" -// CHECK: "kind":"r_paren" -// CHECK: "kind":"l_brace" -func Bar(arg1: String, arg2: Int) { -// CHECK: "kind":"r_brace" -} - -// CHECK: "kind":"kw_func" -// CHECK: "text":"CheckParameterList" -// CHECK: "kind":"l_paren" -// CHECK: "text":"arg1" -// CHECK: "text":"String" -// CHECK: "kind":"ellipsis" -// CHECK: "presence":"Present"} -// CHECK: null -// CHECK: "kind":"comma" -// CHECK: "text":"arg2" -// CHECK: "text":"Int" -// CHECK: "kind":"r_paren" -// CHECK: "kind":"l_brace" -func CheckParameterList(arg1: String..., arg2: Int) { -// CHECK: "kind":"r_brace" -} - -// CHECK: "leadingTrivia":"\n\/\/ Comment at the end of the file\n" - -func separator() {} -// Comment at the end of the file diff --git a/test/Parse/alwaysEmitConformanceMetadata_attr.swift b/test/Parse/alwaysEmitConformanceMetadata_attr.swift index 7bb216a1a9505..c577c16f19554 100644 --- a/test/Parse/alwaysEmitConformanceMetadata_attr.swift +++ b/test/Parse/alwaysEmitConformanceMetadata_attr.swift @@ -1,4 +1,4 @@ -// RUN: %target-typecheck-verify-swift -parse -parse-stdlib -verify-syntax-tree +// RUN: %target-typecheck-verify-swift -parse -parse-stdlib import Swift diff --git a/test/Parse/async-syntax.swift b/test/Parse/async-syntax.swift index b7ce5c4e5df8c..b6b4c8bcd695c 100644 --- a/test/Parse/async-syntax.swift +++ b/test/Parse/async-syntax.swift @@ -1,4 +1,4 @@ -// RUN: %target-typecheck-verify-swift -disable-availability-checking -verify-syntax-tree +// RUN: %target-typecheck-verify-swift -disable-availability-checking // REQUIRES: concurrency diff --git a/test/Parse/noimplicitcopy_attr.swift b/test/Parse/noimplicitcopy_attr.swift index 5db29eee5c13a..008ebb0294b93 100644 --- a/test/Parse/noimplicitcopy_attr.swift +++ b/test/Parse/noimplicitcopy_attr.swift @@ -1,4 +1,4 @@ -f// RUN: %target-typecheck-verify-swift -parse -parse-stdlib -disable-availability-checking -verify-syntax-tree +f// RUN: %target-typecheck-verify-swift -parse -parse-stdlib -disable-availability-checking import Swift diff --git a/test/SILGen/noimplicitcopy_attr_arg_generic_users_banned.swift b/test/SILGen/noimplicitcopy_attr_arg_generic_users_banned.swift index f6c5fe781fa75..f63d7c8a5da10 100644 --- a/test/SILGen/noimplicitcopy_attr_arg_generic_users_banned.swift +++ b/test/SILGen/noimplicitcopy_attr_arg_generic_users_banned.swift @@ -1,4 +1,4 @@ -// RUN: %target-swift-emit-sil -enable-experimental-move-only -parse-stdlib -disable-availability-checking -verify-syntax-tree -verify %s +// RUN: %target-swift-emit-sil -enable-experimental-move-only -parse-stdlib -disable-availability-checking -verify %s import Swift diff --git a/test/Sema/alwaysEmitConformanceMetadata_attr.swift b/test/Sema/alwaysEmitConformanceMetadata_attr.swift index 5b41fce4c64e1..4132586109fdb 100644 --- a/test/Sema/alwaysEmitConformanceMetadata_attr.swift +++ b/test/Sema/alwaysEmitConformanceMetadata_attr.swift @@ -1,4 +1,4 @@ -// RUN: %target-typecheck-verify-swift -parse-stdlib -verify-syntax-tree +// RUN: %target-typecheck-verify-swift -parse-stdlib import Swift diff --git a/test/Sema/moveonly_decl_attr.swift b/test/Sema/moveonly_decl_attr.swift index 9888c31f2ed4d..513ddd1eb7b8a 100644 --- a/test/Sema/moveonly_decl_attr.swift +++ b/test/Sema/moveonly_decl_attr.swift @@ -1,4 +1,4 @@ -// RUN: %target-typecheck-verify-swift -parse-stdlib -disable-availability-checking -verify-syntax-tree -enable-experimental-move-only +// RUN: %target-typecheck-verify-swift -parse-stdlib -disable-availability-checking -enable-experimental-move-only import Swift diff --git a/test/Sema/moveonly_type_attr.swift b/test/Sema/moveonly_type_attr.swift index 1904e34a421a1..050dab7d0051c 100644 --- a/test/Sema/moveonly_type_attr.swift +++ b/test/Sema/moveonly_type_attr.swift @@ -1,4 +1,4 @@ -// RUN: %target-typecheck-verify-swift -parse-stdlib -disable-availability-checking -verify-syntax-tree +// RUN: %target-typecheck-verify-swift -parse-stdlib -disable-availability-checking import Swift diff --git a/test/Sema/noimplicitcopy_attr.swift b/test/Sema/noimplicitcopy_attr.swift index 41a0cd9a8159a..db7bafe6fdd68 100644 --- a/test/Sema/noimplicitcopy_attr.swift +++ b/test/Sema/noimplicitcopy_attr.swift @@ -1,4 +1,4 @@ -// RUN: %target-typecheck-verify-swift -enable-experimental-move-only -parse-stdlib -disable-availability-checking -verify-syntax-tree +// RUN: %target-typecheck-verify-swift -enable-experimental-move-only -parse-stdlib -disable-availability-checking import Swift diff --git a/test/Sema/noimplicitcopy_attr_check_behind_flag.swift b/test/Sema/noimplicitcopy_attr_check_behind_flag.swift index ad93e6e18710f..77fddcab053bb 100644 --- a/test/Sema/noimplicitcopy_attr_check_behind_flag.swift +++ b/test/Sema/noimplicitcopy_attr_check_behind_flag.swift @@ -1,4 +1,4 @@ -// RUN: %target-typecheck-verify-swift -parse-stdlib -disable-availability-checking -verify-syntax-tree +// RUN: %target-typecheck-verify-swift -parse-stdlib -disable-availability-checking class Klass {} diff --git a/test/SourceKit/SyntaxTree/basic.swift b/test/SourceKit/SyntaxTree/basic.swift deleted file mode 100644 index 78e162b1d6fc8..0000000000000 --- a/test/SourceKit/SyntaxTree/basic.swift +++ /dev/null @@ -1,9 +0,0 @@ -// RUN: %target-swift-frontend -emit-syntax %s > %t.emit -// RUN: %sourcekitd-test -req=syntax-tree %s > %t.sourcekit -// RUN: diff %t.emit %t.sourcekit - -struct Foo { - let bar : Int - - let baz : Array < Int > - } diff --git a/test/SourceKit/SyntaxTree/pound_if.swift b/test/SourceKit/SyntaxTree/pound_if.swift deleted file mode 100644 index 27635d3895c89..0000000000000 --- a/test/SourceKit/SyntaxTree/pound_if.swift +++ /dev/null @@ -1,7 +0,0 @@ -// RUN: %target-swift-frontend -emit-syntax %s > %t.emit -// RUN: %sourcekitd-test -req=syntax-tree %s > %t.sourcekit -// RUN: diff %t.emit %t.sourcekit - -#if swift(<4) - print(1) -#endif diff --git a/test/Syntax/Inputs/invalid.sed b/test/Syntax/Inputs/invalid.sed deleted file mode 100644 index 90a753b15153f..0000000000000 --- a/test/Syntax/Inputs/invalid.sed +++ /dev/null @@ -1,32 +0,0 @@ - -# [0xC2] is utf8 2 byte character start byte. -# 0xC2 without second byte is invalid UTF-8 sequence. -# It becomes unexpected text trivia. -# Marker(1) is replaced to this sequence. -s/Z1/Â/g - -# [0xCC, 0x82] in UTF-8 is U+0302. -# This character is invalid for identifier start, but valid for identifier body. -# It becomes unknown token. -# If this type characters are contiguous, they are concatenated to one long unknown token. -# Marker(2) is replaced to this sequence. -s/Z2/Ì‚/g - -# [0xE2, 0x80, 0x9C] in UTF-8 is U+201C, left quote. -# It becomes single character unknown token. -# If this left quote and right quote enclosure text, -# they become one long unknown token. -# Marker(3) is replaced to this sequence. -s/Z3/“/g - -# [0xE2, 0x80, 0x9D] in UTF-8 is U+201D, right quote. -# It becomes single character unknown token. -# Marker(4) is replaced to this sequence. -s/Z4/â€/g - -# [0xE1, 0x9A, 0x80] in UTF-8 is U+1680. -# This character is invalid for swift source. -# It becomes unexpected trivia. -# Marker(5) is replaced to this sequence. -s/Z5/ /g - diff --git a/test/Syntax/Inputs/nbsp.sed b/test/Syntax/Inputs/nbsp.sed deleted file mode 100644 index 3d9adf5e595b5..0000000000000 --- a/test/Syntax/Inputs/nbsp.sed +++ /dev/null @@ -1 +0,0 @@ -s/Z/ /g diff --git a/test/Syntax/Inputs/serialize_class_decl.json b/test/Syntax/Inputs/serialize_class_decl.json deleted file mode 100644 index 9833e6c088d9c..0000000000000 --- a/test/Syntax/Inputs/serialize_class_decl.json +++ /dev/null @@ -1,128 +0,0 @@ -{ - "kind": "SourceFile", - "layout": [ - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "ClassDecl", - "layout": [ - null, - null, - null, - { - "kind": "ModifierList", - "layout": [ - { - "kind": "DeclModifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "contextual_keyword", - "text": "final" - }, - "leadingTrivia": "\/\/ RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t\n\/\/ RUN: diff %t %S\/Inputs\/serialize_class_decl.json -u\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "kw_class" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Foo" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - { - "kind": "MemberDeclBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "MemberDeclList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "eof", - "text": "" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" -} diff --git a/test/Syntax/Inputs/serialize_distributed_actor.json b/test/Syntax/Inputs/serialize_distributed_actor.json deleted file mode 100644 index 742d79f2e08d6..0000000000000 --- a/test/Syntax/Inputs/serialize_distributed_actor.json +++ /dev/null @@ -1,505 +0,0 @@ -{ - "kind": "SourceFile", - "layout": [ - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "ActorDecl", - "layout": [ - null, - null, - null, - { - "kind": "ModifierList", - "layout": [ - { - "kind": "DeclModifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "contextual_keyword", - "text": "distributed" - }, - "leadingTrivia": "\/\/ RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t\n\/\/ RUN: diff %t %S\/Inputs\/serialize_distributed_actor.json -u\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "contextual_keyword", - "text": "actor" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "DA" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - { - "kind": "MemberDeclBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "MemberDeclList", - "layout": [ - { - "kind": "MemberDeclListItem", - "layout": [ - null, - { - "kind": "FunctionDecl", - "layout": [ - null, - null, - null, - { - "kind": "ModifierList", - "layout": [ - { - "kind": "DeclModifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "contextual_keyword", - "text": "distributed" - }, - "leadingTrivia": "\n ", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "kw_func" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "hello" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "kind": "FunctionSignature", - "layout": [ - null, - { - "kind": "ParameterClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "FunctionParameterList", - "layout": [ - { - "kind": "FunctionParameter", - "layout": [ - null, - null, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "name" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "String" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - { - "kind": "ReturnClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "arrow" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "String" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "CodeBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "StringLiteralExpr", - "layout": [ - null, - null, - null, - { - "tokenKind": { - "kind": "string_quote" - }, - "leadingTrivia": "\n ", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "StringLiteralSegments", - "layout": [ - { - "kind": "StringSegment", - "layout": [ - null, - { - "tokenKind": { - "kind": "string_segment", - "text": "Hello " - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - }, - { - "kind": "ExpressionSegment", - "layout": [ - null, - { - "tokenKind": { - "kind": "backslash" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "TupleExprElementList", - "layout": [ - { - "kind": "TupleExprElement", - "layout": [ - null, - null, - null, - null, - null, - { - "kind": "IdentifierExpr", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "name" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "string_interpolation_anchor" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - }, - { - "kind": "StringSegment", - "layout": [ - null, - { - "tokenKind": { - "kind": "string_segment", - "text": "!" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "string_quote" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n ", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "eof", - "text": "" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" -} diff --git a/test/Syntax/Inputs/serialize_functions.json b/test/Syntax/Inputs/serialize_functions.json deleted file mode 100644 index 147d025c0c12b..0000000000000 --- a/test/Syntax/Inputs/serialize_functions.json +++ /dev/null @@ -1,826 +0,0 @@ -{ - "kind": "SourceFile", - "layout": [ - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "FunctionDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_func" - }, - "leadingTrivia": "\/\/ RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t\n\/\/ RUN: diff %S\/Inputs\/serialize_functions.json %t -u\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "oneName" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "kind": "FunctionSignature", - "layout": [ - null, - { - "kind": "ParameterClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "FunctionParameterList", - "layout": [ - { - "kind": "FunctionParameter", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "name" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "String" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "CodeBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "FunctionDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_func" - }, - "leadingTrivia": "\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "oneName" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "kind": "FunctionSignature", - "layout": [ - null, - { - "kind": "ParameterClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "FunctionParameterList", - "layout": [ - { - "kind": "FunctionParameter", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "firstName" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "secondName" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "String" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "CodeBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "FunctionDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_func" - }, - "leadingTrivia": "\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "const" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "kind": "FunctionSignature", - "layout": [ - null, - { - "kind": "ParameterClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "FunctionParameterList", - "layout": [ - { - "kind": "FunctionParameter", - "layout": [ - null, - null, - null, - { - "kind": "ModifierList", - "layout": [ - { - "kind": "DeclModifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "_const" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "kw__" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "map" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "String" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "CodeBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "FunctionDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_func" - }, - "leadingTrivia": "\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "isolated" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "kind": "FunctionSignature", - "layout": [ - null, - { - "kind": "ParameterClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "FunctionParameterList", - "layout": [ - { - "kind": "FunctionParameter", - "layout": [ - null, - null, - null, - { - "kind": "ModifierList", - "layout": [ - { - "kind": "DeclModifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "isolated" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "kw__" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "map" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "String" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "CodeBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "eof", - "text": "" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" -} diff --git a/test/Syntax/Inputs/serialize_main_actor.json b/test/Syntax/Inputs/serialize_main_actor.json deleted file mode 100644 index 44b6ba59b8ee3..0000000000000 --- a/test/Syntax/Inputs/serialize_main_actor.json +++ /dev/null @@ -1,861 +0,0 @@ -{ - "kind": "SourceFile", - "layout": [ - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "StructDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_struct" - }, - "leadingTrivia": "\/\/ RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t\n\/\/ RUN: diff %t %S\/Inputs\/serialize_main_actor.json -u\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Foo" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - { - "kind": "MemberDeclBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "MemberDeclList", - "layout": [ - { - "kind": "MemberDeclListItem", - "layout": [ - null, - { - "kind": "InitializerDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_init" - }, - "leadingTrivia": "\n ", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - null, - null, - { - "kind": "FunctionSignature", - "layout": [ - null, - { - "kind": "ParameterClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "FunctionParameterList", - "layout": [ - { - "kind": "FunctionParameter", - "layout": [ - null, - null, - null, - { - "tokenKind": { - "kind": "kw__" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "foo" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "AttributedType", - "layout": [ - null, - null, - null, - { - "kind": "AttributeList", - "layout": [ - { - "kind": "CustomAttribute", - "layout": [ - null, - { - "tokenKind": { - "kind": "at_sign" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "MainActor" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "kind": "FunctionType", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "TupleTypeElementList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "arrow" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Void" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "CodeBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n \n ", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - null - ], - "presence": "Present" - }, - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "StructDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_struct" - }, - "leadingTrivia": "\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Bar" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - { - "kind": "MemberDeclBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "MemberDeclList", - "layout": [ - { - "kind": "MemberDeclListItem", - "layout": [ - null, - { - "kind": "VariableDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_var" - }, - "leadingTrivia": "\n ", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "PatternBindingList", - "layout": [ - { - "kind": "PatternBinding", - "layout": [ - null, - { - "kind": "IdentifierPattern", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "body" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "kind": "TypeAnnotation", - "layout": [ - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Foo" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "CodeBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "FunctionCallExpr", - "layout": [ - null, - { - "kind": "IdentifierExpr", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Foo" - }, - "leadingTrivia": "\n ", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "TupleExprElementList", - "layout": [], - "presence": "Present" - }, - null, - null, - null, - { - "kind": "ClosureExpr", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "ClosureSignature", - "layout": [ - null, - { - "kind": "AttributeList", - "layout": [ - { - "kind": "CustomAttribute", - "layout": [ - null, - { - "tokenKind": { - "kind": "at_sign" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "MainActor" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_in" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "FunctionCallExpr", - "layout": [ - null, - { - "kind": "IdentifierExpr", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "print" - }, - "leadingTrivia": "\n ", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "TupleExprElementList", - "layout": [ - { - "kind": "TupleExprElement", - "layout": [ - null, - null, - null, - null, - null, - { - "kind": "StringLiteralExpr", - "layout": [ - null, - null, - null, - { - "tokenKind": { - "kind": "string_quote" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "StringLiteralSegments", - "layout": [ - { - "kind": "StringSegment", - "layout": [ - null, - { - "tokenKind": { - "kind": "string_segment", - "text": "Hi" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "string_quote" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n ", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n ", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "eof", - "text": "" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - } - ], - "presence": "Present" -} diff --git a/test/Syntax/Inputs/serialize_multiple_decls.json b/test/Syntax/Inputs/serialize_multiple_decls.json deleted file mode 100644 index f591355a142a9..0000000000000 --- a/test/Syntax/Inputs/serialize_multiple_decls.json +++ /dev/null @@ -1,184 +0,0 @@ -{ - "kind": "SourceFile", - "layout": [ - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "StructDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_struct" - }, - "leadingTrivia": "\/\/ RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t\n\/\/ RUN: diff %t %S\/Inputs\/serialize_multiple_decls.json -u\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "A" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - { - "kind": "MemberDeclBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "MemberDeclList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - }, - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "StructDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_struct" - }, - "leadingTrivia": "\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "B" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - { - "kind": "MemberDeclBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "MemberDeclList", - "layout": [], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "eof", - "text": "" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" -} diff --git a/test/Syntax/Inputs/serialize_struct_decl.json b/test/Syntax/Inputs/serialize_struct_decl.json deleted file mode 100644 index 13226f8318423..0000000000000 --- a/test/Syntax/Inputs/serialize_struct_decl.json +++ /dev/null @@ -1,375 +0,0 @@ -{ - "kind": "SourceFile", - "layout": [ - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "StructDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_struct" - }, - "leadingTrivia": "\/\/ RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t\n\/\/ RUN: diff %t %S\/Inputs\/serialize_struct_decl.json -u\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Foo" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null, - { - "kind": "MemberDeclBlock", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_brace" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "MemberDeclList", - "layout": [ - { - "kind": "MemberDeclListItem", - "layout": [ - null, - { - "kind": "VariableDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_let" - }, - "leadingTrivia": "\n ", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "PatternBindingList", - "layout": [ - { - "kind": "PatternBinding", - "layout": [ - null, - { - "kind": "IdentifierPattern", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "bar" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - { - "kind": "TypeAnnotation", - "layout": [ - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Int" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - { - "kind": "MemberDeclListItem", - "layout": [ - null, - { - "kind": "VariableDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_let" - }, - "leadingTrivia": "\n\n ", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "PatternBindingList", - "layout": [ - { - "kind": "PatternBinding", - "layout": [ - null, - { - "kind": "IdentifierPattern", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "baz" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - { - "kind": "TypeAnnotation", - "layout": [ - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Array" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "GenericArgumentClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_angle" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "GenericArgumentList", - "layout": [ - { - "kind": "GenericArgument", - "layout": [ - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Int" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_angle" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_brace" - }, - "leadingTrivia": "\n ", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "eof", - "text": "" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" -} diff --git a/test/Syntax/Outputs/round_trip_invalid.swift.withkinds b/test/Syntax/Outputs/round_trip_invalid.swift.withkinds deleted file mode 100644 index 3b09c5916dbed..0000000000000 --- a/test/Syntax/Outputs/round_trip_invalid.swift.withkinds +++ /dev/null @@ -1,22 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_invalid.swift.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -let strings: [Strin[g]? - -// Function body without closing brace token. -func foo() { - var a = 2 - -class C { -struct S { -enum E { -protocol P { -extension P { diff --git a/test/Syntax/Outputs/round_trip_module_interface.swiftinterface.withkinds b/test/Syntax/Outputs/round_trip_module_interface.swiftinterface.withkinds deleted file mode 100644 index dc73f41b3344e..0000000000000 --- a/test/Syntax/Outputs/round_trip_module_interface.swiftinterface.withkinds +++ /dev/null @@ -1,24 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_module_interface.swiftinterface.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -import Swift - -public class Bar { - public init(x: Int = 24, y: Int) -} - -public class Foo: Bar { - public override init(x: Int = super, y: Int) - public subscript(k: Int = super) -> Int { get } - public func foo(x: Int = super) - public func foo(y: Int = super.init) - public func foo(z: Int = super[1]) -} diff --git a/test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds b/test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds deleted file mode 100644 index 7505e7cedf158..0000000000000 --- a/test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds +++ /dev/null @@ -1,619 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -import ABC -import A.B.C -@objc import A.B -@objc import typealias A.B -import struct A.B - -#warning("test warning") -#error("test error") - -#if Blah -class C { - func bar(_ a: Int) {} - func bar1(_ a: Float) -> Float { return -0.6 + 0.1 - 0.3 } - func bar2(a: Int, b: Int, c:Int) -> Int { return 1 } - func bar3(a: Int) -> Int { return 1 } - func bar4(_ a: Int) -> Int { return 1 } - func foo() { - var a = /*comment*/"ab\(x)c"/*comment*/ - var b = /*comment*/+2/*comment*/ - bar(1) - bar(+10) - bar(-10) - bar1(-1.1) - bar1(1.1) - var f = /*comments*/+0.1/*comments*/ - foo() - _ = "🙂🤗🤩🤔🤨" - } - - func foo1() { - _ = bar2(a:1, b:2, c:2) - _ = bar2(a:1 + 1, b:2 * 2 + 2, c:2 + 2) - _ = bar2(a : bar2(a: 1, b: 2, c: 3), b: 2, c: 3) - _ = bar3(a : bar3(a: bar3(a: 1))) - _ = bar4(bar4(bar4(1))) - _ = [:] - _ = [] - _ = [1, 2, 3, 4] - _ = [1:1, 2:2, 3:3, 4:4] - _ = [bar3(a:1), bar3(a:1), bar3(a:1), bar3(a:1)] - _ = ["a": bar3(a:1), "b": bar3(a:1), "c": bar3(a:1), "d": bar3(a:1)] - foo(nil, nil, nil) - _ = type(of: a).self - _ = a.`self` - _ = A -> B.C<Int> - _ = [(A) throws -> B]() - } - func boolAnd() -> Bool { return true && false } - func boolOr() -> Bool { return true || false } - - func foo2() { - _ = true ? 1 : 0 - _ = (true ? 1 : 0) ? (true ? 1 : 0) : (true ? 1 : 0) - _ = (1, 2) - _ = (first: 1, second: 2) - _ = (1) - _ = (first: 1) - if !true { - return - } - } - - func foo3() { - _ = [Any]() - _ = (@convention(c) (Int) -> Void).self - _ = a.a.a - _ = a.b - _ = 1.a - (1 + 1).a.b.foo - _ = a as Bool || a as! Bool || a as? Bool - _ = a is Bool - _ = self - _ = Self - } - - func superExpr() { - _ = super.foo - super.bar() - super[12] = 1 - super.init() - } - - func implicitMember() { - _ = .foo - _ = .foo(x: 12) - _ = .foo() { 12 } - _ = .foo[12] - _ = .foo.bar - } - - init() {} - @objc private init(a: Int) - init!(a: Int) {} - init?(a: Int) {} - public init(a: Int) throws {} - init(a: Int..., b: Double...) {} - - @objc deinit {} - private deinit {} - - internal subscript(x: Int) -> Int { get {} set {} } - subscript() -> Int { return 1 } - subscript(x: Int..., y y: String...) -> Int { return 1 } - - var x: Int { - address { fatalError() } - unsafeMutableAddress { fatalError() } - } -} - -protocol PP { - associatedtype A - associatedtype B: Sequence - associatedtype C = Int - associatedtype D: Sequence = [Int] - associatedtype E: Sequence = [[Int]] where A.Element : Sequence - private associatedtype F - @objc associatedtype G -} - -#endif - -#if blah -typealias A = Any -#elseif blahblah -typealias B = (Array<Array<Any>>.Element, x: Int) -#else -typealias C = [Int] -#endif -typealias D = [Int: String] -typealias E = Int?.Protocol -typealias F = [Int]!.Type -typealias G = (a x: Int, _ y: Int ... = 1) throw -> () -> () -typealias H = () rethrows -> () -typealias I = (A & B<C>) -> C & D -typealias J = inout @autoclosure () -> Int -typealias K = (@invalidAttr Int, inout Int, __shared Int, __owned Int) -> () - -@objc private typealias T<a,b> = Int - -class Foo { - let bar: Int -} - -class Bar: Foo { - var foo: Int = 42 -} - -class C<A, B> where A: Foo, B == Bar {} - -@available(*, unavailable) -private class C {} - -struct foo { - struct foo { - struct foo { - func foo() { - } - } - } - struct foo {} -} - -struct foo { - @available(*, unavailable) - struct foo {} - public class foo { - @available(*, unavailable) - @objc(fooObjc) - private static func foo() {} - - @objc(fooObjcBar:baz:) - private static func foo(bar: String, baz: Int) - } -} - -struct S<A, B, C, @objc D> where A:B, B==C, A : C, B.C == D.A, A.B: C.D {} - -private struct S<A, B>: Base where A: B { - private struct S: A, B {} -} - -protocol P: class {} - -func foo(_ _: Int, - a b: Int = 3 + 2, - _ c: Int = 2, - d _: Int = true ? 2: 3, - @objc e: X = true, - f: inout Int, - g: Int..., - h: Bool...) throws -> [Int: String] {} - -func foo(_ a: Int) throws -> Int {} -func foo( a: Int) rethrows -> Int {} - -struct C { -@objc -@available(*, unavailable) -private static override func foo<a, b, c>(a b: Int, c: Int) throws -> [Int] where a==p1, b:p2 { ddd } -func rootView() -> Label {} -static func ==() -> bool {} -static func !=<a, b, c>() -> bool {} -} - -@objc -private protocol foo : bar where A==B {} -protocol foo { func foo() } -private protocol foo{} -@objc -public protocol foo where A:B {} - -#if blah -func tryfoo() { - try foo() - try! foo() - try? foo() - try! foo().bar().foo().bar() -} -#else -func closure() { - _ = {[weak a, - unowned(safe) self, - b = 3, - unowned(unsafe) c = foo().bar] in - } - _ = {[] in } - - _ = { [] a, b, _ -> Int in - return 2 - } - _ = { [] (a: Int, b: Int, _: Int) -> Int in - return 2 - } - _ = { [] a, b, _ throws -> Int in - return 2 - } - _ = { [] (a: Int, _ b: Int) throws -> Int in - return 2 - } - _ = { a, b in } - _ = { (a, b) in } - _ = {} - _ = { s1, s2 in s1 > s2 } - _ = { $0 > $1 } -} -#endif - -func postfix() { - foo() - foo() {} - foo {} - foo { } - arg2: {} - foo {} - foo.bar() - foo.bar() {} - foo.bar() {} - arg2: {} - in: {} - foo.bar {} - foo[] - foo[1] - foo[] {} - foo[1] {} - foo[1] {} - arg2: {} - foo[1][2,x:3] - foo?++.bar!(baz).self - foo().0 - foo<Int>.bar - foo<Int>() - foo.bar<Int>() - - foo(x:y:)() - foo(x:)<Int> { } - _ = .foo(x:y:) - _ = x.foo(x:y:) - _ = foo(&d) - _ = <#Placeholder#> + <#T##(Int) -> Int#> -} - -#if blah -#else -#endif - -class C { - var a: Int { - @objc mutating set(value) {} - mutating get { return 3 } - @objc didSet {} - willSet(newValue){ } - } - var a : Int { - return 3 - } -} - -protocol P { - var a: Int { get set } - var a: Int {} -} - -private final class D { - @objc - static private var a: Int = 3 { return 3 }, b: Int, c = 4, d : Int { get {} get {}}, (a, b): (Int, Int) - let (a, b) = (1,2), _ = 4 {} - - func patternTests() { - for let (x, _) in foo {} - for var x: Int in foo {} - } -} - -do { - switch foo { - case let a: break - case let a as Int: break - case let (a, b): break - case (let a, var b): break - case is Int: break - case let .bar(x): break - case MyEnum.foo: break - case let a as Int: break - case let a?: break - } -} - -func statementTests() { - do { - } catch (var x, let y) { - } catch where false { - } catch let e where e.foo == bar { - } catch .a(let a), .b(let b) where b == "" { - } catch { - } - repeat { } while true - LABEL: repeat { } while false - while true { } - LABEL: while true { } - LABEL: do {} - LABEL: switch foo { - case 1: - fallthrough - case 2: - break LABEL - case 3: - break - } - - for a in b { - defer { () } - if c { - throw MyError() - continue - } else { - continue LABEL - } - } - - if - foo, - let a = foo, - let b: Int = foo, - var c = foo, - case (let v, _) = foo, - case (let v, _): (Int, Int) = foo { - } else if foo { - } else { - } - - guard let a = b else {} - - guard let self = self else {} - - for var i in foo where i.foo {} - for case is Int in foo {} - - switch Foo { - case n1: - break - case n2, n3l: - break -#if FOO - case let (x, y) where !x, n3l where false: - break -#elseif BAR - case let y: - break -#else - case .foo(let x): - break -#endif - default: - break - } - - switch foo { - case 1, 2, 3: break - default: break - } - - switch foo { - case 1, 2, 3: break - @unknown default: break - } - - switch foo { - case 1, 2, 3: break - @unknown case _: break - } - - switch foo { - case 1, 2, 3: break - // This is rejected in Sema, but should be preserved by Syntax. - @unknown case (42, -42) where 1 == 2: break - @garbage case 0: break - @garbage(foobar) case -1: break - } -} - -// MARK: - ExtensionDecl - -extension ext { - var s: Int { - return 42 - } -} - -@available(*, unavailable) -fileprivate extension ext {} - -extension ext : extProtocol {} - -extension ext where A == Int, B: Numeric {} - -extension ext.a.b {} - -func foo() { - var a = "abc \(foo()) def \(a + b + "a \(3)") gh \(bar, default: 1)" - var a = """ - abc \( foo() + bar() ) - de \(3 + 3 + "abc \(foo()) def") - fg - \(bar, default: 1) - """ -} - -func keypath() { - _ = \a.?.b - _ = \a.b.c - _ = \a.b[1] - _ = \.a.b - _ = \Array<Int>.[] - _ = #keyPath(a.b.c) -} -func objcSelector() { - _ = [ - #selector(getter: Foo.bar), - #selector(setter: Foo.Bar.baz), - #selector(Foo.method(x:y:)), - #selector(foo[42].bar(x)), - #selector({ [x] in return nil }) - ] -} - -func objectLiterals() { - #fileLiteral(a) - #colorLiteral(a, b) - #imageLiteral(a, b, c) - #column - #file - #function - #dsohandle -} - -enum E1 : String { - case foo = 1 - case bar = "test", baz(x: Int, String) = 12 - indirect case qux(E1) - - indirect private enum E2<T>: String where T: SomeProtocol { - case foo, bar, baz - } -} - -precedencegroup FooPrecedence { - higherThan: DefaultPrecedence, UnknownPrecedence - assignment: false - associativity: none -} -precedencegroup BarPrecedence {} -precedencegroup BazPrecedence { - associativity: left - assignment: true - associativity: right - lowerThan: DefaultPrecedence -} - -infix operator<++>:FooPrecedence -prefix operator..<< -postfix operator <- - -func higherOrderFunc() { - let x = [1,2,3] - x.reduce(0, +) -} - -if #available(iOS 11, macOS 10.11.2, *) {} - -@_specialize(where T == Int) -@_specialize(exported: true, where T == String) -public func specializedGenericFunc<T>(_ t: T) -> T { - return t -} - -protocol Q { - func g() - var x: String { get } - func f(x:Int, y:Int) -> Int - #if FOO_BAR - var conditionalVar: String - #endif -} - -struct S : Q, Equatable { - @_implements(Q, f(x:y:)) - func h(x:Int, y:Int) -> Int { - return 6 - } - - @_implements(Equatable, ==(_:_:)) - public static func isEqual(_ lhs: S, _ rhs: S) -> Bool { - return false - } - - @_implements(P, x) - var y: String - @_implements(P, g()) - func h() { _ = \.self } - - @available(*, deprecated: 1.2, message: "ABC") - fileprivate(set) var x: String -} - -struct ReadModify { - var st0 = ("a", "b") - var rm0: (String, String) { - _read { yield (("a", "b")) } - _modify { yield &st0 } - } - var rm1: (String, String) { - _read { yield (st0) } - } -} - -@custom @_alignment(16) public struct float3 { public var x, y, z: Float } - -#sourceLocation(file: "otherFile.swift", line: 5) - -func foo() {} - -#sourceLocation() - -"abc \( } ) def" - -#assert(true) -#assert(false) -#assert(true, "hello world") - -public func anyFoo() -> some Foo {} -public func qoo() -> some O & O2 {} -func zlop() -> some C & AnyObject & P {} - -@custom(a, b,c) -func foo() {} - -@custom_attr -@custom(A: a, B: b, C:c) -func foo() {} - -"abc" -"abc \(foo)" -#"abc"# -#"abc \#(foo)"# -##"abc"## -##"abc \##(foo)"## - -foo() -#if COND1 - .bar?()! -#elseif COND2 - #if true - .call() - #elseif true - #if true - .other - #endif - #else - .before() - #if true - .after() - #endif - #endif - .member -#else - .baz() {} -#endif diff --git a/test/Syntax/diagnostics_verify.swift b/test/Syntax/diagnostics_verify.swift deleted file mode 100644 index af2ac25f53a22..0000000000000 --- a/test/Syntax/diagnostics_verify.swift +++ /dev/null @@ -1,25 +0,0 @@ -// RUN: %target-typecheck-verify-swift -verify-syntax-tree -// FIXME: Too many "unknown syntax" errors. - -if true { - try - // expected-error@-1 {{unknown expression syntax exists in the source}} -} // expected-error {{expected expression}} - -if false { - [.] - // expected-error@-1 {{expected identifier after '.' expression}} - // expected-error@-2 {{unknown expression syntax exists in the source}} -} - -class { // expected-error {{unknown declaration syntax exists in the source}} - // expected-error@-1 {{expected identifier in class declaration}} - -} - -#if swift(<1) -print("Wat") -class { // expected-error {{unknown declaration syntax exists in the source}} - -} -#endif diff --git a/test/Syntax/round_trip_concurrency.swift b/test/Syntax/round_trip_concurrency.swift deleted file mode 100644 index 690dc2952cfef..0000000000000 --- a/test/Syntax/round_trip_concurrency.swift +++ /dev/null @@ -1,81 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %round-trip-syntax-test --swift-syntax-test %swift-syntax-test --file %s - -@available(SwiftStdlib 5.1, *) -actor Counter { - private var value = 0 - private let scratchBuffer: UnsafeMutableBufferPointer - - init(maxCount: Int) { - scratchBuffer = .allocate(capacity: maxCount) - scratchBuffer.initialize(repeating: 0) - } - - func next() -> Int { - let current = value - - // Make sure we haven't produced this value before - assert(scratchBuffer[current] == 0) - scratchBuffer[current] = 1 - - value = value + 1 - return current - } -} - - -@available(SwiftStdlib 5.1, *) -func worker(identity: Int, counters: [Counter], numIterations: Int) async { - for i in 0..] = [] - for i in 0..= 2 ? Int(args[1])! : 10 - let workers = args.count >= 3 ? Int(args[2])! : 100 - let iterations = args.count >= 4 ? Int(args[3])! : 1000 - print("counters: \(counters), workers: \(workers), iterations: \(iterations)") - await runTest(numCounters: counters, numWorkers: workers, numIterations: iterations) - } -} - -struct X3 { - subscript(_ i : Int) -> Int { - get async throws {} - } -} diff --git a/test/Syntax/round_trip_fix_quote.swift b/test/Syntax/round_trip_fix_quote.swift deleted file mode 100644 index 1a1c0900a97cf..0000000000000 --- a/test/Syntax/round_trip_fix_quote.swift +++ /dev/null @@ -1,7 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t - -"abc" - -RoundedRectangle(cornerRadius: 6.0)lengthlength'Integer' field' [binding value: \(int.value)]int'Double' field [binding value: \(double.value)]double diff --git a/test/Syntax/round_trip_function.swift b/test/Syntax/round_trip_function.swift deleted file mode 100644 index d2ea8c1a6d7d2..0000000000000 --- a/test/Syntax/round_trip_function.swift +++ /dev/null @@ -1,17 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %round-trip-syntax-test --swift-syntax-test %swift-syntax-test --file %s - -func noArgs() {} -func oneArg(x: Int) {} -func oneUnlabeledArg(_ x: Int) {} - -typealias FunctionAlias = (_ x: inout Int) -> Bool -typealias FunctionAliasNoLabel = (Int) -> Bool - -func manyArgs(x: Int, y: Int, _ z: Bool, _ a: String) throws -> [Int] { - return [] -} - -func rethrowing(_ f: (Bool) throws -> Int) rethrows -> Int { - return try f(false) -} diff --git a/test/Syntax/round_trip_invalid.swift b/test/Syntax/round_trip_invalid.swift deleted file mode 100644 index 9cc7750288de3..0000000000000 --- a/test/Syntax/round_trip_invalid.swift +++ /dev/null @@ -1,22 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_invalid.swift.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -let strings: [Strin[g]? - -// Function body without closing brace token. -func foo() { - var a = 2 - -class C { -struct S { -enum E { -protocol P { -extension P { diff --git a/test/Syntax/round_trip_misc.swift b/test/Syntax/round_trip_misc.swift deleted file mode 100644 index c77729f5a72c0..0000000000000 --- a/test/Syntax/round_trip_misc.swift +++ /dev/null @@ -1,55 +0,0 @@ -// RUN: %round-trip-syntax-test --swift-syntax-test %swift-syntax-test --file %s - -class C { - // Erroneous typealias decl. - typealias Inner: Foo = Int - - // Implicit accessor with attribute at the top of its body. - var x: Int { - @objc - func f() {} - } -} -do { - typealias Alias2 = () -> (a b: [Generic @objc func -} -do { - typealias -} -do { - typealias Alias = A & B & C.D<> -} -do { - typealias boo bar = Int -} - -// Orphan '}' at top level -} - -// Orphan #elseif, #else, #endif at top level. -#elseif foobar -#else -#endif - -// Compound name. -foo(x:y:)() - -// Type identifier with erroneous component. -let a: Int.) - -// Type with unknown attribute followed by parentheses. -typealias b = @foobar() -> Void -typealias c = @foobar(a) () -> Void - -// keypath expressions. -let d = \.foo -let e = \.[1] -let f = \.?.bar - -let optionalArray: [Int]? diff --git a/test/Syntax/round_trip_module_interface.swiftinterface b/test/Syntax/round_trip_module_interface.swiftinterface deleted file mode 100644 index f9cb881edec5e..0000000000000 --- a/test/Syntax/round_trip_module_interface.swiftinterface +++ /dev/null @@ -1,24 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_module_interface.swiftinterface.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -import Swift - -public class Bar { - public init(x: Int = 24, y: Int) -} - -public class Foo: Bar { - public override init(x: Int = super, y: Int) - public subscript(k: Int = super) -> Int { get } - public func foo(x: Int = super) - public func foo(y: Int = super.init) - public func foo(z: Int = super[1]) -} diff --git a/test/Syntax/round_trip_nul.swift b/test/Syntax/round_trip_nul.swift deleted file mode 100644 index c53e9b9e7a545..0000000000000 --- a/test/Syntax/round_trip_nul.swift +++ /dev/null @@ -1,5 +0,0 @@ -// RUN: cat %s | tr '\132' '\0' > %t.tr -// RUN: cp -f %t.tr %t -// RUN: %round-trip-syntax-test --swift-syntax-test %swift-syntax-test --file %t -let a = Z3Z // nul(Z) -func b() {} diff --git a/test/Syntax/round_trip_parse_gen.swift b/test/Syntax/round_trip_parse_gen.swift deleted file mode 100644 index fb47fb0973dc3..0000000000000 --- a/test/Syntax/round_trip_parse_gen.swift +++ /dev/null @@ -1,619 +0,0 @@ -// RUN: rm -rf %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds -// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds -// RUN: %swift-syntax-test -input-source-filename %s -eof > %t -// RUN: diff -u %s %t -// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump -// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t -// RUN: diff -u %s %t - -import ABC -import A.B.C -@objc import A.B -@objc import typealias A.B -import struct A.B - -#warning("test warning") -#error("test error") - -#if Blah -class C { - func bar(_ a: Int) {} - func bar1(_ a: Float) -> Float { return -0.6 + 0.1 - 0.3 } - func bar2(a: Int, b: Int, c:Int) -> Int { return 1 } - func bar3(a: Int) -> Int { return 1 } - func bar4(_ a: Int) -> Int { return 1 } - func foo() { - var a = /*comment*/"ab\(x)c"/*comment*/ - var b = /*comment*/+2/*comment*/ - bar(1) - bar(+10) - bar(-10) - bar1(-1.1) - bar1(1.1) - var f = /*comments*/+0.1/*comments*/ - foo() - _ = "🙂🤗🤩🤔🤨" - } - - func foo1() { - _ = bar2(a:1, b:2, c:2) - _ = bar2(a:1 + 1, b:2 * 2 + 2, c:2 + 2) - _ = bar2(a : bar2(a: 1, b: 2, c: 3), b: 2, c: 3) - _ = bar3(a : bar3(a: bar3(a: 1))) - _ = bar4(bar4(bar4(1))) - _ = [:] - _ = [] - _ = [1, 2, 3, 4] - _ = [1:1, 2:2, 3:3, 4:4] - _ = [bar3(a:1), bar3(a:1), bar3(a:1), bar3(a:1)] - _ = ["a": bar3(a:1), "b": bar3(a:1), "c": bar3(a:1), "d": bar3(a:1)] - foo(nil, nil, nil) - _ = type(of: a).self - _ = a.`self` - _ = A -> B.C - _ = [(A) throws -> B]() - } - func boolAnd() -> Bool { return true && false } - func boolOr() -> Bool { return true || false } - - func foo2() { - _ = true ? 1 : 0 - _ = (true ? 1 : 0) ? (true ? 1 : 0) : (true ? 1 : 0) - _ = (1, 2) - _ = (first: 1, second: 2) - _ = (1) - _ = (first: 1) - if !true { - return - } - } - - func foo3() { - _ = [Any]() - _ = (@convention(c) (Int) -> Void).self - _ = a.a.a - _ = a.b - _ = 1.a - (1 + 1).a.b.foo - _ = a as Bool || a as! Bool || a as? Bool - _ = a is Bool - _ = self - _ = Self - } - - func superExpr() { - _ = super.foo - super.bar() - super[12] = 1 - super.init() - } - - func implicitMember() { - _ = .foo - _ = .foo(x: 12) - _ = .foo() { 12 } - _ = .foo[12] - _ = .foo.bar - } - - init() {} - @objc private init(a: Int) - init!(a: Int) {} - init?(a: Int) {} - public init(a: Int) throws {} - init(a: Int..., b: Double...) {} - - @objc deinit {} - private deinit {} - - internal subscript(x: Int) -> Int { get {} set {} } - subscript() -> Int { return 1 } - subscript(x: Int..., y y: String...) -> Int { return 1 } - - var x: Int { - address { fatalError() } - unsafeMutableAddress { fatalError() } - } -} - -protocol PP { - associatedtype A - associatedtype B: Sequence - associatedtype C = Int - associatedtype D: Sequence = [Int] - associatedtype E: Sequence = [[Int]] where A.Element : Sequence - private associatedtype F - @objc associatedtype G -} - -#endif - -#if blah -typealias A = Any -#elseif blahblah -typealias B = (Array>.Element, x: Int) -#else -typealias C = [Int] -#endif -typealias D = [Int: String] -typealias E = Int?.Protocol -typealias F = [Int]!.Type -typealias G = (a x: Int, _ y: Int ... = 1) throw -> () -> () -typealias H = () rethrows -> () -typealias I = (A & B) -> C & D -typealias J = inout @autoclosure () -> Int -typealias K = (@invalidAttr Int, inout Int, __shared Int, __owned Int) -> () - -@objc private typealias T = Int - -class Foo { - let bar: Int -} - -class Bar: Foo { - var foo: Int = 42 -} - -class C where A: Foo, B == Bar {} - -@available(*, unavailable) -private class C {} - -struct foo { - struct foo { - struct foo { - func foo() { - } - } - } - struct foo {} -} - -struct foo { - @available(*, unavailable) - struct foo {} - public class foo { - @available(*, unavailable) - @objc(fooObjc) - private static func foo() {} - - @objc(fooObjcBar:baz:) - private static func foo(bar: String, baz: Int) - } -} - -struct S where A:B, B==C, A : C, B.C == D.A, A.B: C.D {} - -private struct S: Base where A: B { - private struct S: A, B {} -} - -protocol P: class {} - -func foo(_ _: Int, - a b: Int = 3 + 2, - _ c: Int = 2, - d _: Int = true ? 2: 3, - @objc e: X = true, - f: inout Int, - g: Int..., - h: Bool...) throws -> [Int: String] {} - -func foo(_ a: Int) throws -> Int {} -func foo( a: Int) rethrows -> Int {} - -struct C { -@objc -@available(*, unavailable) -private static override func foo(a b: Int, c: Int) throws -> [Int] where a==p1, b:p2 { ddd } -func rootView() -> Label {} -static func ==() -> bool {} -static func !=() -> bool {} -} - -@objc -private protocol foo : bar where A==B {} -protocol foo { func foo() } -private protocol foo{} -@objc -public protocol foo where A:B {} - -#if blah -func tryfoo() { - try foo() - try! foo() - try? foo() - try! foo().bar().foo().bar() -} -#else -func closure() { - _ = {[weak a, - unowned(safe) self, - b = 3, - unowned(unsafe) c = foo().bar] in - } - _ = {[] in } - - _ = { [] a, b, _ -> Int in - return 2 - } - _ = { [] (a: Int, b: Int, _: Int) -> Int in - return 2 - } - _ = { [] a, b, _ throws -> Int in - return 2 - } - _ = { [] (a: Int, _ b: Int) throws -> Int in - return 2 - } - _ = { a, b in } - _ = { (a, b) in } - _ = {} - _ = { s1, s2 in s1 > s2 } - _ = { $0 > $1 } -} -#endif - -func postfix() { - foo() - foo() {} - foo {} - foo { } - arg2: {} - foo {} - foo.bar() - foo.bar() {} - foo.bar() {} - arg2: {} - in: {} - foo.bar {} - foo[] - foo[1] - foo[] {} - foo[1] {} - foo[1] {} - arg2: {} - foo[1][2,x:3] - foo?++.bar!(baz).self - foo().0 - foo.bar - foo() - foo.bar() - - foo(x:y:)() - foo(x:) { } - _ = .foo(x:y:) - _ = x.foo(x:y:) - _ = foo(&d) - _ = <#Placeholder#> + <#T##(Int) -> Int#> -} - -#if blah -#else -#endif - -class C { - var a: Int { - @objc mutating set(value) {} - mutating get { return 3 } - @objc didSet {} - willSet(newValue){ } - } - var a : Int { - return 3 - } -} - -protocol P { - var a: Int { get set } - var a: Int {} -} - -private final class D { - @objc - static private var a: Int = 3 { return 3 }, b: Int, c = 4, d : Int { get {} get {}}, (a, b): (Int, Int) - let (a, b) = (1,2), _ = 4 {} - - func patternTests() { - for let (x, _) in foo {} - for var x: Int in foo {} - } -} - -do { - switch foo { - case let a: break - case let a as Int: break - case let (a, b): break - case (let a, var b): break - case is Int: break - case let .bar(x): break - case MyEnum.foo: break - case let a as Int: break - case let a?: break - } -} - -func statementTests() { - do { - } catch (var x, let y) { - } catch where false { - } catch let e where e.foo == bar { - } catch .a(let a), .b(let b) where b == "" { - } catch { - } - repeat { } while true - LABEL: repeat { } while false - while true { } - LABEL: while true { } - LABEL: do {} - LABEL: switch foo { - case 1: - fallthrough - case 2: - break LABEL - case 3: - break - } - - for a in b { - defer { () } - if c { - throw MyError() - continue - } else { - continue LABEL - } - } - - if - foo, - let a = foo, - let b: Int = foo, - var c = foo, - case (let v, _) = foo, - case (let v, _): (Int, Int) = foo { - } else if foo { - } else { - } - - guard let a = b else {} - - guard let self = self else {} - - for var i in foo where i.foo {} - for case is Int in foo {} - - switch Foo { - case n1: - break - case n2, n3l: - break -#if FOO - case let (x, y) where !x, n3l where false: - break -#elseif BAR - case let y: - break -#else - case .foo(let x): - break -#endif - default: - break - } - - switch foo { - case 1, 2, 3: break - default: break - } - - switch foo { - case 1, 2, 3: break - @unknown default: break - } - - switch foo { - case 1, 2, 3: break - @unknown case _: break - } - - switch foo { - case 1, 2, 3: break - // This is rejected in Sema, but should be preserved by Syntax. - @unknown case (42, -42) where 1 == 2: break - @garbage case 0: break - @garbage(foobar) case -1: break - } -} - -// MARK: - ExtensionDecl - -extension ext { - var s: Int { - return 42 - } -} - -@available(*, unavailable) -fileprivate extension ext {} - -extension ext : extProtocol {} - -extension ext where A == Int, B: Numeric {} - -extension ext.a.b {} - -func foo() { - var a = "abc \(foo()) def \(a + b + "a \(3)") gh \(bar, default: 1)" - var a = """ - abc \( foo() + bar() ) - de \(3 + 3 + "abc \(foo()) def") - fg - \(bar, default: 1) - """ -} - -func keypath() { - _ = \a.?.b - _ = \a.b.c - _ = \a.b[1] - _ = \.a.b - _ = \Array.[] - _ = #keyPath(a.b.c) -} -func objcSelector() { - _ = [ - #selector(getter: Foo.bar), - #selector(setter: Foo.Bar.baz), - #selector(Foo.method(x:y:)), - #selector(foo[42].bar(x)), - #selector({ [x] in return nil }) - ] -} - -func objectLiterals() { - #fileLiteral(a) - #colorLiteral(a, b) - #imageLiteral(a, b, c) - #column - #file - #function - #dsohandle -} - -enum E1 : String { - case foo = 1 - case bar = "test", baz(x: Int, String) = 12 - indirect case qux(E1) - - indirect private enum E2: String where T: SomeProtocol { - case foo, bar, baz - } -} - -precedencegroup FooPrecedence { - higherThan: DefaultPrecedence, UnknownPrecedence - assignment: false - associativity: none -} -precedencegroup BarPrecedence {} -precedencegroup BazPrecedence { - associativity: left - assignment: true - associativity: right - lowerThan: DefaultPrecedence -} - -infix operator<++>:FooPrecedence -prefix operator..<< -postfix operator <- - -func higherOrderFunc() { - let x = [1,2,3] - x.reduce(0, +) -} - -if #available(iOS 11, macOS 10.11.2, *) {} - -@_specialize(where T == Int) -@_specialize(exported: true, where T == String) -public func specializedGenericFunc(_ t: T) -> T { - return t -} - -protocol Q { - func g() - var x: String { get } - func f(x:Int, y:Int) -> Int - #if FOO_BAR - var conditionalVar: String - #endif -} - -struct S : Q, Equatable { - @_implements(Q, f(x:y:)) - func h(x:Int, y:Int) -> Int { - return 6 - } - - @_implements(Equatable, ==(_:_:)) - public static func isEqual(_ lhs: S, _ rhs: S) -> Bool { - return false - } - - @_implements(P, x) - var y: String - @_implements(P, g()) - func h() { _ = \.self } - - @available(*, deprecated: 1.2, message: "ABC") - fileprivate(set) var x: String -} - -struct ReadModify { - var st0 = ("a", "b") - var rm0: (String, String) { - _read { yield (("a", "b")) } - _modify { yield &st0 } - } - var rm1: (String, String) { - _read { yield (st0) } - } -} - -@custom @_alignment(16) public struct float3 { public var x, y, z: Float } - -#sourceLocation(file: "otherFile.swift", line: 5) - -func foo() {} - -#sourceLocation() - -"abc \( } ) def" - -#assert(true) -#assert(false) -#assert(true, "hello world") - -public func anyFoo() -> some Foo {} -public func qoo() -> some O & O2 {} -func zlop() -> some C & AnyObject & P {} - -@custom(a, b,c) -func foo() {} - -@custom_attr -@custom(A: a, B: b, C:c) -func foo() {} - -"abc" -"abc \(foo)" -#"abc"# -#"abc \#(foo)"# -##"abc"## -##"abc \##(foo)"## - -foo() -#if COND1 - .bar?()! -#elseif COND2 - #if true - .call() - #elseif true - #if true - .other - #endif - #else - .before() - #if true - .after() - #endif - #endif - .member -#else - .baz() {} -#endif diff --git a/test/Syntax/round_trip_regex.swift b/test/Syntax/round_trip_regex.swift deleted file mode 100644 index 41b836ec83384..0000000000000 --- a/test/Syntax/round_trip_regex.swift +++ /dev/null @@ -1,34 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -fail-on-parse-error > %t/afterRoundtrip.swift -// RUN: diff -u %s %t/afterRoundtrip.swift -// REQUIRES: swift_in_compiler - -_ = /abc/ -_ = #/abc/# -_ = ##/abc/## - -func foo(_ x: T...) {} -foo(/abc/, #/abc/#, ##/abc/##) - -let arr = [/abc/, #/abc/#, ##/abc/##] - -_ = /\w+/.self -_ = #/\w+/#.self -_ = ##/\w+/##.self - -_ = /#\/\#\\/ -_ = #/#/\/\#\\/# -_ = ##/#|\|\#\\/## - -_ = #/ -multiline -/# - -_ = #/ -double -multiline -/# - -_ = #/ -\ -/# diff --git a/test/Syntax/round_trip_regex_escape_newline.swift b/test/Syntax/round_trip_regex_escape_newline.swift deleted file mode 100644 index 58561ec306236..0000000000000 --- a/test/Syntax/round_trip_regex_escape_newline.swift +++ /dev/null @@ -1,12 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: not %swift-syntax-test -input-source-filename %s -parse-gen -fail-on-parse-error > %t/afterRoundtrip.swift 2> %t/errors.swift -// RUN: diff -u %s %t/afterRoundtrip.swift -// RUN: cat %t/errors.swift | %FileCheck %s - -// REQUIRES: swift_in_compiler - -// Escaping newlines is not supported -_ = /\ -/ - -// CHECK: 10:1: error: expected expression path in Swift key path diff --git a/test/Syntax/round_trip_regex_invalid.swift b/test/Syntax/round_trip_regex_invalid.swift deleted file mode 100644 index c12507890db34..0000000000000 --- a/test/Syntax/round_trip_regex_invalid.swift +++ /dev/null @@ -1,16 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t/afterRoundtrip.swift -// RUN: diff -u %s %t/afterRoundtrip.swift - -_ = /abc -_ = #/abc -_ = #/abc/ -_ = ##/abc/# - -_ #/x -/# -_ #/ -x/# - -_ //# -_ /x/# diff --git a/test/Syntax/round_trip_stdlib.swift b/test/Syntax/round_trip_stdlib.swift deleted file mode 100644 index 0d9eac0546e4a..0000000000000 --- a/test/Syntax/round_trip_stdlib.swift +++ /dev/null @@ -1,3 +0,0 @@ -// test/Syntax/round_trip_stdlib.swift is very slow on ASAN build -// REQUIRES: no_asan -// RUN: %round-trip-syntax-test -d %swift_src_root/stdlib --swift-syntax-test %swift-syntax-test diff --git a/test/Syntax/round_trip_types.swift b/test/Syntax/round_trip_types.swift deleted file mode 100644 index f8dc543e7d8b3..0000000000000 --- a/test/Syntax/round_trip_types.swift +++ /dev/null @@ -1,12 +0,0 @@ -// RUN: %round-trip-syntax-test --swift-syntax-test %swift-syntax-test --file %s - -typealias TwoInts = (Int, Int) -typealias TwoNamedInts = (a: Int, b: Int) -typealias VoidTuple = () -typealias TupleWithTrivia = ( Int , b :Int ) - -func testAnyType() { - protocol MyProto {} - - let foo: [Int: any MyProto] -} diff --git a/test/Syntax/serialize_class_decl.swift b/test/Syntax/serialize_class_decl.swift deleted file mode 100644 index a36da335a5f4b..0000000000000 --- a/test/Syntax/serialize_class_decl.swift +++ /dev/null @@ -1,4 +0,0 @@ -// RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t -// RUN: diff %t %S/Inputs/serialize_class_decl.json -u - -final class Foo {} diff --git a/test/Syntax/serialize_functions.swift b/test/Syntax/serialize_functions.swift deleted file mode 100644 index bfca146334d0d..0000000000000 --- a/test/Syntax/serialize_functions.swift +++ /dev/null @@ -1,7 +0,0 @@ -// RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t -// RUN: diff %S/Inputs/serialize_functions.json %t -u - -func oneName(name: String) {} -func oneName(firstName secondName: String) {} -func const(_const _ map: String) {} -func isolated(isolated _ map: String) {} diff --git a/test/Syntax/serialize_multiple_decls.swift b/test/Syntax/serialize_multiple_decls.swift deleted file mode 100644 index b14902967ef7d..0000000000000 --- a/test/Syntax/serialize_multiple_decls.swift +++ /dev/null @@ -1,8 +0,0 @@ -// RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t -// RUN: diff %t %S/Inputs/serialize_multiple_decls.json -u - -struct A { -} - -struct B { -} diff --git a/test/Syntax/serialize_struct_decl.swift b/test/Syntax/serialize_struct_decl.swift deleted file mode 100644 index 6391dc5a77c8e..0000000000000 --- a/test/Syntax/serialize_struct_decl.swift +++ /dev/null @@ -1,8 +0,0 @@ -// RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t -// RUN: diff %t %S/Inputs/serialize_struct_decl.json -u - -struct Foo { - let bar : Int - - let baz : Array < Int > - } diff --git a/test/Syntax/serialize_tupletype.swift b/test/Syntax/serialize_tupletype.swift deleted file mode 100644 index 20246475453e6..0000000000000 --- a/test/Syntax/serialize_tupletype.swift +++ /dev/null @@ -1,4 +0,0 @@ -// RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t.result -// RUN: diff %t.result %s.result - -typealias x = (_ b: Int, _: String) diff --git a/test/Syntax/serialize_tupletype.swift.result b/test/Syntax/serialize_tupletype.swift.result deleted file mode 100644 index d8257c245923d..0000000000000 --- a/test/Syntax/serialize_tupletype.swift.result +++ /dev/null @@ -1,245 +0,0 @@ -{ - "kind": "SourceFile", - "layout": [ - null, - { - "kind": "CodeBlockItemList", - "layout": [ - { - "kind": "CodeBlockItem", - "layout": [ - null, - { - "kind": "TypealiasDecl", - "layout": [ - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "kw_typealias" - }, - "leadingTrivia": "\/\/ RUN: %swift-syntax-test -input-source-filename %s -serialize-raw-tree > %t.result\n\/\/ RUN: diff %t.result %s.result\n\n", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "x" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - null, - null, - { - "kind": "TypeInitializerClause", - "layout": [ - null, - { - "tokenKind": { - "kind": "equal" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "TupleType", - "layout": [ - null, - { - "tokenKind": { - "kind": "l_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "kind": "TupleTypeElementList", - "layout": [ - { - "kind": "TupleTypeElement", - "layout": [ - null, - null, - null, - { - "tokenKind": { - "kind": "kw__" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "identifier", - "text": "b" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "Int" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - { - "tokenKind": { - "kind": "comma" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - { - "kind": "TupleTypeElement", - "layout": [ - null, - null, - null, - { - "tokenKind": { - "kind": "kw__" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null, - { - "tokenKind": { - "kind": "colon" - }, - "leadingTrivia": "", - "trailingTrivia": " ", - "presence": "Present" - }, - null, - { - "kind": "SimpleTypeIdentifier", - "layout": [ - null, - { - "tokenKind": { - "kind": "identifier", - "text": "String" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "r_paren" - }, - "leadingTrivia": "", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null - ], - "presence": "Present" - }, - null, - null, - null - ], - "presence": "Present" - }, - null, - null, - null, - null, - null - ], - "presence": "Present" - } - ], - "presence": "Present" - }, - null, - { - "tokenKind": { - "kind": "eof", - "text": "" - }, - "leadingTrivia": "\n", - "trailingTrivia": "", - "presence": "Present" - }, - null - ], - "presence": "Present" -} diff --git a/test/Syntax/syntax_diagnostics.swift b/test/Syntax/syntax_diagnostics.swift deleted file mode 100644 index e97652902e6de..0000000000000 --- a/test/Syntax/syntax_diagnostics.swift +++ /dev/null @@ -1,3 +0,0 @@ -// RUN: %target-swift-frontend -emit-syntax -primary-file %s -verify - -typealias Inner: Foo // expected-error{{unknown declaration syntax exists in the source}} expected-error{{expected '=' in type alias declaration}} diff --git a/test/Syntax/tokens_escaped_identifier.swift b/test/Syntax/tokens_escaped_identifier.swift deleted file mode 100644 index 5fa4510ce46fe..0000000000000 --- a/test/Syntax/tokens_escaped_identifier.swift +++ /dev/null @@ -1,17 +0,0 @@ -// RUN: %swift-syntax-test -input-source-filename %s -dump-full-tokens | %FileCheck %s -let /*leading trivia*/ `if` = 3 -print(/*leading trivia*/ `if` ) - -// CHECK-LABEL: 2:24 -// CHECK-NEXT:(Token identifier -// CHECK-NEXT: (trivia blockComment /*leading trivia*/) -// CHECK-NEXT: (trivia space 1) -// CHECK-NEXT: (text="`if`") -// CHECK-NEXT: (trivia space 1)) - -// CHECK-LABEL: 3:26 -// CHECK-NEXT:(Token identifier -// CHECK-NEXT: (trivia blockComment /*leading trivia*/) -// CHECK-NEXT: (trivia space 1) -// CHECK-NEXT: (text="`if`") -// CHECK-NEXT: (trivia space 1)) diff --git a/test/Syntax/tokens_nonbreaking_space.swift b/test/Syntax/tokens_nonbreaking_space.swift deleted file mode 100644 index 5fcae5a51e9fc..0000000000000 --- a/test/Syntax/tokens_nonbreaking_space.swift +++ /dev/null @@ -1,44 +0,0 @@ -// RUN: cat %s | sed -f %S/Inputs/nbsp.sed > %t.tmp -// RUN: cp -f %t.tmp %t -// RUN: %swift-syntax-test -input-source-filename %t -dump-full-tokens 2>&1 | %FileCheck %t -let a =Z3Z // nbsp(Z) -let bZ= 3Z -let cZ=Z3 - -// CHECK: 4:8: warning: non-breaking space (U+00A0) used instead of regular space -// CHECK: 4:11: warning: non-breaking space (U+00A0) used instead of regular space -// CHECK: 5:6: warning: non-breaking space (U+00A0) used instead of regular space -// CHECK: 5:11: warning: non-breaking space (U+00A0) used instead of regular space -// CHECK: 6:6: warning: non-breaking space (U+00A0) used instead of regular space -// CHECK: 6:9: warning: non-breaking space (U+00A0) used instead of regular space - -// CHECK-LABEL: 4:7 -// CHECK-NEXT:(Token equal -// CHECK-NEXT: (text="=") -// CHECK-NEXT: (trivia unexpectedText \302\240)) - -// CHECK-LABEL: 4:10 -// CHECK-NEXT:(Token integer_literal -// CHECK-NEXT: (text="3") -// CHECK-NEXT: (trivia unexpectedText \302\240) -// CHECK-NEXT: (trivia space 1)) - -// CHECK-LABEL: 5:5 -// CHECK-NEXT:(Token identifier -// CHECK-NEXT: (text="b") -// CHECK-NEXT: (trivia unexpectedText \302\240)) - -// CHECK-LABEL: 5:10 -// CHECK-NEXT:(Token integer_literal -// CHECK-NEXT: (text="3") -// CHECK-NEXT: (trivia unexpectedText \302\240) - -// CHECK-LABEL: 6:5 -// CHECK-NEXT:(Token identifier -// CHECK-NEXT: (text="c") -// CHECK-NEXT: (trivia unexpectedText \302\240)) - -// CHECK-LABEL: 6:8 -// CHECK-NEXT:(Token equal -// CHECK-NEXT: (text="=") -// CHECK-NEXT: (trivia unexpectedText \302\240)) diff --git a/test/Syntax/tokens_nul.swift b/test/Syntax/tokens_nul.swift deleted file mode 100644 index d302e28f0807a..0000000000000 --- a/test/Syntax/tokens_nul.swift +++ /dev/null @@ -1,28 +0,0 @@ -// RUN: cat %s | tr '\132' '\0' > %t.tmp -// RUN: cp -f %t.tmp %t -// RUN: %swift-syntax-test -input-source-filename %t -dump-full-tokens 2>&1 | %FileCheck %t -let a = Z3Z // nul(Z) -func b() {} - -// CHECK: 4:9: warning: nul character embedded in middle of file -// CHECK: 4:11: warning: nul character embedded in middle of file -// CHECK: 4:20: warning: nul character embedded in middle of file - -// CHECK-LABEL: 4:7 -// CHECK-NEXT:(Token equal -// CHECK-NEXT: (text="=") -// CHECK-NEXT: (trivia space 1) -// CHECK-NEXT: (trivia unexpectedText \000)) - -// CHECK-LABEL: 4:10 -// CHECK-NEXT:(Token integer_literal -// CHECK-NEXT: (text="3") -// CHECK-NEXT: (trivia unexpectedText \000) -// CHECK-NEXT: (trivia space 1)) - -// CHECK-LABEL: 5:1 -// CHECK-NEXT:(Token kw_func -// CHECK-NEXT: (trivia lineComment // nul(\000)) -// CHECK-NEXT: (trivia newline 1) -// CHECK-NEXT: (text="func") -// CHECK-NEXT: (trivia space 1)) diff --git a/test/Syntax/tokens_unknown_and_invalid.swift b/test/Syntax/tokens_unknown_and_invalid.swift deleted file mode 100644 index 9f4b1451df750..0000000000000 --- a/test/Syntax/tokens_unknown_and_invalid.swift +++ /dev/null @@ -1,89 +0,0 @@ -// To embed test byte sequence, -// this source replace marker to byte sequence first in runtime. -// Marker(N) have `ZN` style format. Z is Z, N is number. -// Byte sequence is represented in escape sequence. -// To avoid replace marker in sed command by sed itself, -// marker is also represented in escape sequence. - -// RUN: cat %s | sed -f %S/Inputs/invalid.sed > %t -// RUN: %{python} -c "import sys; t = open(sys.argv[1], 'rb').read().replace(b'\r\n', b'\n'); open(sys.argv[1], 'wb').write(t)" %t -// RUN: %swift-syntax-test -input-source-filename %t -dump-full-tokens 2>&1 | %FileCheck %t -// RUN: %round-trip-syntax-test --swift-syntax-test %swift-syntax-test --file %t - -aaa -Z1 bbb Z1 - -ccc Z2 - -ddd Z2Z2Z2Z2 - -eee Z3Z3 - -fff Z3hello worldZ4 - -ggg Z4 - -hhh -Z5 iii Z5 -jjj - -// Diagnostics -// CHECK: 14:1: error: invalid UTF-8 found in source file -// CHECK: 14:7: error: invalid UTF-8 found in source file -// CHECK: 16:5: error: an identifier cannot begin with this character -// CHECK: 18:5: error: an identifier cannot begin with this character -// CHECK: 20:5: error: unicode curly quote found -// CHECK: 20:8: error: unicode curly quote found -// CHECK: 22:19: error: unicode curly quote found -// CHECK: 22:5: error: unicode curly quote found -// CHECK: 24:5: error: unicode curly quote found -// CHECK: 27:1: error: invalid character in source file -// CHECK: 27:9: error: invalid character in source file - -// Checks around bbb -// CHECK-LABEL: 14:3 -// CHECK-NEXT: (Token identifier -// CHECK-NEXT: (trivia newline 1) -// CHECK-NEXT: (trivia unexpectedText \302) -// CHECK-NEXT: (trivia space 1) -// CHECK-NEXT: (text="bbb") -// CHECK-NEXT: (trivia space 1) -// CHECK-NEXT: (trivia unexpectedText \302)) - -// Checks around ccc -// CHECK-LABEL: 16:5 -// CHECK-NEXT: (Token unknown -// CHECK-NEXT: (text="\xCC\x82")) - -// Checks around ddd -// CHECK-LABEL: 18:5 -// CHECK-NEXT: (Token unknown -// CHECK-NEXT: (text="\xCC\x82\xCC\x82\xCC\x82\xCC\x82")) - -// Checks around eee -// CHECK-LABEL: 20:5 -// CHECK-NEXT: (Token unknown -// CHECK-NEXT: (text="\xE2\x80\x9C")) -// CHECK-LABEL: 20:8 -// CHECK-NEXT: (Token unknown -// CHECK-NEXT: (text="\xE2\x80\x9C")) - -// Checks around fff -// CHECK-LABEL: 22:5 -// CHECK-NEXT: (Token unknown -// CHECK-NEXT: (text="\xE2\x80\x9Chello world\xE2\x80\x9D")) - -// Checks around ggg -// CHECK-LABEL: 24:5 -// CHECK-NEXT: (Token unknown -// CHECK-NEXT: (text="\xE2\x80\x9D")) - -// Checks around iii -// CHECK-LABEL: 27:5 -// CHECK-NEXT: (Token identifier -// CHECK-NEXT: (trivia newline 1) -// CHECK-NEXT: (trivia unexpectedText \341\232\200) -// CHECK-NEXT: (trivia space 1) -// CHECK-NEXT: (text="iii") -// CHECK-NEXT: (trivia space 1) -// CHECK-NEXT: (trivia unexpectedText \341\232\200)) diff --git a/test/expr/unary/async_await.swift b/test/expr/unary/async_await.swift index 64ffbbe5ea5c1..ea4f6f31ba03d 100644 --- a/test/expr/unary/async_await.swift +++ b/test/expr/unary/async_await.swift @@ -1,4 +1,4 @@ -// RUN: %target-swift-frontend -typecheck -verify %s -verify-syntax-tree -disable-availability-checking +// RUN: %target-swift-frontend -typecheck -verify %s -disable-availability-checking // REQUIRES: concurrency diff --git a/test/incrParse/add-close-brace-to-property.swift b/test/incrParse/add-close-brace-to-property.swift deleted file mode 100644 index f93f06a4fd5d1..0000000000000 --- a/test/incrParse/add-close-brace-to-property.swift +++ /dev/null @@ -1,9 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case REPLACE - - -var value: Int { - get { fatalError() } -<>> - -let x = 10 diff --git a/test/incrParse/add-else-to-ifconfig.swift b/test/incrParse/add-else-to-ifconfig.swift deleted file mode 100644 index e62d5914df457..0000000000000 --- a/test/incrParse/add-else-to-ifconfig.swift +++ /dev/null @@ -1,7 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case ADD_ELSE - -func container() { -#if false - <>> - diff --git a/test/incrParse/add-space-at-missing-brace.swift b/test/incrParse/add-space-at-missing-brace.swift deleted file mode 100644 index 0763d98038656..0000000000000 --- a/test/incrParse/add-space-at-missing-brace.swift +++ /dev/null @@ -1,10 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case INSERT_SPACE - -class AnimationType { - func foo(x: Blah) { - switch x { - case (. - -extension AnimationType { - public<>> diff --git a/test/incrParse/edit-string-literal.swift b/test/incrParse/edit-string-literal.swift deleted file mode 100644 index dea7cf7f63ee4..0000000000000 --- a/test/incrParse/edit-string-literal.swift +++ /dev/null @@ -1,12 +0,0 @@ -// RUN: %validate-incrparse %s - -func foo() { -} - -func bar() { - let x = "<<>> world" - let y = 1 -} - -func anotherMethod() { -} diff --git a/test/incrParse/extend-identifier-at-eof.swift b/test/incrParse/extend-identifier-at-eof.swift deleted file mode 100644 index 1341aa0125e4e..0000000000000 --- a/test/incrParse/extend-identifier-at-eof.swift +++ /dev/null @@ -1,6 +0,0 @@ -// RUN: %validate-incrparse %s - -func foo() {} -// ATTENTION: This file is testing the EOF token. -// DO NOT PUT ANYTHING AFTER THE CHANGE, NOT EVEN A NEWLINE -_ = x<<<|||x>>> \ No newline at end of file diff --git a/test/incrParse/funcs.swift b/test/incrParse/funcs.swift deleted file mode 100644 index 2b829ba37d461..0000000000000 --- a/test/incrParse/funcs.swift +++ /dev/null @@ -1,40 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case NO_CHANGES -// RUN: %validate-incrparse %s --test-case ADD_FUNC_PARENS -// RUN: %validate-incrparse %s --test-case ADD_OPENING_BRACE -// RUN: %validate-incrparse %s --test-case REMOVE_FUNC_KEYWORD -// RUN: %validate-incrparse %s --test-case ADD_PARAM_NAME -// RUN: %validate-incrparse %s --test-case ADD_PARAM_TYPE - -func start() {} - -class Bar - -let y = 1 - -class InvalidFuncDecls { - func parensAdded<>> { - } - - func openingBraceAdded() <>> - - func closingBraceAdded() { - - <>> - - <>> funcKeywordRemoved() { - - } - - func addingParamName(<>>) { - - } - - func addingParamColon(arg<>>) { - - } - - func addingParamType(arg:<>>) { - - } -} diff --git a/test/incrParse/incremental-diagnostic.swift b/test/incrParse/incremental-diagnostic.swift deleted file mode 100644 index f1c3c49f6d2bd..0000000000000 --- a/test/incrParse/incremental-diagnostic.swift +++ /dev/null @@ -1,7 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case REPLACE - -let myFont: Font = { - let myFont = Font.body - return myFont.weight(.<>>) -}() diff --git a/test/incrParse/inserted_text_starts_identifier.swift b/test/incrParse/inserted_text_starts_identifier.swift deleted file mode 100644 index a1052ebc9b33a..0000000000000 --- a/test/incrParse/inserted_text_starts_identifier.swift +++ /dev/null @@ -1,7 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case STRING - -// rdar://problem/45259469 -// https://github.com/apple/swift/issues/51498 - -self = <>>foo(1)[object1, object2] + o bar(1) diff --git a/test/incrParse/invalid.swift b/test/incrParse/invalid.swift deleted file mode 100644 index 9635c70f6920c..0000000000000 --- a/test/incrParse/invalid.swift +++ /dev/null @@ -1,17 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case NO_CHANGES -// RUN: %validate-incrparse %s --test-case NESTED_INITIALIZERS - -func start() {} - -class Bar - -let y = 1 - -class NestedInitializers { - <>> - init() { - - } - <>> -} diff --git a/test/incrParse/multi-edit-mapping.swift b/test/incrParse/multi-edit-mapping.swift deleted file mode 100644 index 1ba407660796d..0000000000000 --- a/test/incrParse/multi-edit-mapping.swift +++ /dev/null @@ -1,4 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case MULTI - -let one: Int;let two: Int; let three: Int; <>><>>let found: Int;let five: Int; diff --git a/test/incrParse/reuse.swift b/test/incrParse/reuse.swift deleted file mode 100644 index 3d7e6d87bed37..0000000000000 --- a/test/incrParse/reuse.swift +++ /dev/null @@ -1,54 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case MODIFY_ACCESSOR -// RUN: %validate-incrparse %s --test-case ADD_PROPERTY -// RUN: %validate-incrparse %s --test-case WRAP_IN_CLASS -// RUN: %validate-incrparse %s --test-case UNWRAP_CLASS -// RUN: %validate-incrparse %s --test-case NEXT_TOKEN_CALCULATION - -func start() {} - -var someprop: Int { - get { - return 0 - } - set { print(<>>) } -} - -struct Foo { - let a: Int - let b: Int - let c: Int - - let d: String - <>> - let f: Int - - let g: Int -} - -// FIXME: The functions inside the class should not need to get reparsed -<>> - func foo1() { - print("Hello Foo!") - } - - func foo2() { - print("Hello again") - } - -<>> - func bar1() { - let pi = 3.1415 - print("Pi is (approximately) \(pi)") - } - - func bar2() { - print("I can compute Pi as well:") - bar1() - } -} - -// The indentation on these lines is important for the test case - let a = "hello" - let c = "<>>world" - diff --git a/test/incrParse/simple.swift b/test/incrParse/simple.swift deleted file mode 100644 index 12d8cbf88cb38..0000000000000 --- a/test/incrParse/simple.swift +++ /dev/null @@ -1,57 +0,0 @@ -// RUN: %empty-directory(%t) -// RUN: %validate-incrparse %s --test-case REPLACE -// RUN: %validate-incrparse %s --test-case REPLACE_BY_LONGER -// RUN: %validate-incrparse %s --test-case REPLACE_BY_SHORTER -// RUN: %validate-incrparse %s --test-case INSERT -// RUN: %validate-incrparse %s --test-case REMOVE -// RUN: %validate-incrparse %s --test-case ATTACH_TO_PREV_NODE -// RUN: %validate-incrparse %s --test-case CLASS_SURROUNDING -// RUN: %validate-incrparse %s --test-case MULTI_EDIT -// RUN: %validate-incrparse %s --test-case MULTI_EDIT_SAME_LINE -// RUN: %validate-incrparse %s --test-case REPLACE_WITH_MULTI_BYTE_CHAR -// RUN: %validate-incrparse %s --test-case REPLACE_MULTI_BYTE_CHAR_WITH_SHORTER -// RUN: %validate-incrparse %s --test-case LAST_CHARACTER_OF_STRUCT -// RUN: %validate-incrparse %s --test-case ADD_ARRAY_CLOSE_BRACKET -// RUN: %validate-incrparse %s --test-case ADD_IF_OPEN_BRACE -// RUN: %validate-incrparse %s --test-case EXTEND_IDENTIFIER - -func start() {} - - -func foo() { -} - -_ = <>> -_ = <>> -_ = <>> -<>> -<>> -foo() -<>> -_ = 1 - -<>> - func method1() {} - -<>> - func method1() {} -<>> - -<>> = <>> - -let x = "<>>" -let x = "<>>" - -private struc<>> MyStruct { -} - -var computedVar: [Int] { - return [1 - <>> -} - -if true <>> - _ = 5 -} - -let y<>> = 42 diff --git a/test/lit.cfg b/test/lit.cfg index ad1e9a6873ed8..d8fb5fcdf4b32 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -315,7 +315,6 @@ config.sil_passpipeline_dumper = inferSwiftBinary('sil-passpipeline-dumper') config.lldb_moduleimport_test = inferSwiftBinary('lldb-moduleimport-test') config.swift_ide_test = inferSwiftBinary('swift-ide-test') config.swift_dependency_tool = inferSwiftBinary('swift-dependency-tool') -config.swift_syntax_test = inferSwiftBinary('swift-syntax-test') config.swift_reflection_dump = inferSwiftBinary('swift-reflection-dump') config.swift_remoteast_test = inferSwiftBinary('swift-remoteast-test') config.swift_indent = inferSwiftBinary('swift-indent') @@ -467,7 +466,6 @@ shutil.rmtree(completion_cache_path, ignore_errors=True) ccp_opt = "-completion-cache-path %r" % completion_cache_path lit_config.note("Using code completion cache: " + completion_cache_path) -config.substitutions.append( ('%validate-incrparse', '%{python} %utils/incrparse/validate_parse.py --temp-dir %t --swift-syntax-test %swift-syntax-test') ) config.substitutions.append( ('%llvm_obj_root', config.llvm_obj_root) ) config.substitutions.append( ('%swift-lib-dir', config.swift_lib_dir) ) config.substitutions.append( ('%llvm_src_root', config.llvm_src_root) ) @@ -505,7 +503,6 @@ config.substitutions.append( ('%swift-dump-pcm', "%r -dump-pcm" % config.swiftc) config.substitutions.append( ('%swift-ide-test_plain', config.swift_ide_test) ) config.substitutions.append( ('%swift-ide-test', "%r %s %s -swift-version %s %s" % (config.swift_ide_test, mcp_opt, ccp_opt, swift_version, config.resource_dir_opt)) ) config.substitutions.append( ('%swift-dependency-tool', config.swift_dependency_tool) ) -config.substitutions.append( ('%swift-syntax-test', config.swift_syntax_test) ) config.substitutions.append( ('%swift-indent', config.swift_indent) ) config.substitutions.append( ('%llvm-link', config.llvm_link) ) config.substitutions.append( ('%swift-llvm-opt', config.swift_llvm_opt) ) @@ -2316,14 +2313,14 @@ config.substitutions.append(('%target-swift-typecheck-module-from-interface\(([^ config.substitutions.append(('%target-typecheck-verify-swift', config.target_parse_verify_swift)) config.substitutions.append(('%target-swift-emit-silgen\(mock-sdk:([^)]+)\)', - SubstituteCaptures(r'%target-swift-frontend(mock-sdk:\1) -emit-silgen -verify-syntax-tree'))) -config.substitutions.append(('%target-swift-emit-silgen', '%target-swift-frontend -emit-silgen -verify-syntax-tree')) + SubstituteCaptures(r'%target-swift-frontend(mock-sdk:\1) -emit-silgen'))) +config.substitutions.append(('%target-swift-emit-silgen', '%target-swift-frontend -emit-silgen')) config.substitutions.append(('%target-swift-emit-sil\(mock-sdk:([^)]+)\)', - SubstituteCaptures(r'%target-swift-frontend(mock-sdk:\1) -emit-sil -verify-syntax-tree'))) -config.substitutions.append(('%target-swift-emit-sil', '%target-swift-frontend -emit-sil -verify-syntax-tree')) + SubstituteCaptures(r'%target-swift-frontend(mock-sdk:\1) -emit-sil'))) +config.substitutions.append(('%target-swift-emit-sil', '%target-swift-frontend -emit-sil')) config.substitutions.append(('%target-swift-emit-ir\(mock-sdk:([^)]+)\)', - SubstituteCaptures(r'%target-swift-frontend(mock-sdk:\1) -emit-ir -verify-syntax-tree'))) -config.substitutions.append(('%target-swift-emit-ir', '%target-swift-frontend -emit-ir -verify-syntax-tree')) + SubstituteCaptures(r'%target-swift-frontend(mock-sdk:\1) -emit-ir'))) +config.substitutions.append(('%target-swift-emit-ir', '%target-swift-frontend -emit-ir')) config.substitutions.append(('%target-swift-frontend\(mock-sdk:([^)]+)\)', SubstituteCaptures(r'%s \1 %s' % ( escape_for_substitute_captures(subst_target_swift_frontend_mock_sdk), diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 596eaae1dee6f..3ba277d7aa461 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -11,8 +11,7 @@ option(BUILD_SOURCEKIT_XPC_SERVICE "Whether or not the SourceKit XPC service should be built" ${BUILD_SOURCEKIT_XPC_SERVICE_default}) -# Add generated libSyntax headers to global dependencies. -list(APPEND LLVM_COMMON_DEPENDS swift-syntax-generated-headers) +# Add generated SourceKit UID header to global dependencies. if(SWIFT_BUILD_SOURCEKIT) list(APPEND LLVM_COMMON_DEPENDS generated_sourcekit_uids) endif() @@ -38,7 +37,6 @@ if(SWIFT_INCLUDE_TESTS OR SWIFT_INCLUDE_TEST_BINARIES) add_swift_tool_subdirectory(swift-ide-test) add_swift_tool_subdirectory(swift-remoteast-test) add_swift_tool_subdirectory(lldb-moduleimport-test) - add_swift_tool_subdirectory(swift-syntax-test) endif() if(LLVM_USE_SANITIZE_COVERAGE) diff --git a/tools/SourceKit/include/SourceKit/Core/LangSupport.h b/tools/SourceKit/include/SourceKit/Core/LangSupport.h index 7a305e42eea2c..ad466cd20d379 100644 --- a/tools/SourceKit/include/SourceKit/Core/LangSupport.h +++ b/tools/SourceKit/include/SourceKit/Core/LangSupport.h @@ -31,12 +31,6 @@ namespace llvm { class MemoryBuffer; } -namespace swift { -namespace syntax { -class SourceFileSyntax; -} // namespace syntax -} // namespace swift - namespace SourceKit { class GlobalConfig; @@ -252,13 +246,6 @@ struct SourceFileRange { uintptr_t End; }; -enum class SyntaxTreeTransferMode { - /// Don't transfer the syntax tree - Off, - /// Transfer the entire syntax tree - Full -}; - class EditorConsumer { virtual void anchor(); public: @@ -312,13 +299,6 @@ class EditorConsumer { virtual void handleSourceText(StringRef Text) = 0; - virtual void - handleSyntaxTree(const swift::syntax::SourceFileSyntax &SyntaxTree) = 0; - virtual bool syntaxTreeEnabled() { - return syntaxTreeTransferMode() != SyntaxTreeTransferMode::Off; - } - virtual SyntaxTreeTransferMode syntaxTreeTransferMode() = 0; - virtual void finished() {} }; diff --git a/tools/SourceKit/lib/SwiftLang/CMakeLists.txt b/tools/SourceKit/lib/SwiftLang/CMakeLists.txt index ec0915cc4d6ab..62ac4499b76b6 100644 --- a/tools/SourceKit/lib/SwiftLang/CMakeLists.txt +++ b/tools/SourceKit/lib/SwiftLang/CMakeLists.txt @@ -42,7 +42,6 @@ target_link_libraries(SourceKitSwiftLang PRIVATE swiftSema swiftBasic swiftSerialization - swiftSyntax swiftOption swiftSymbolGraphGen swiftRefactoring diff --git a/tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp b/tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp index c44a8b3f8f098..f67550503c6cf 100644 --- a/tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp +++ b/tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp @@ -585,7 +585,7 @@ getCodeCompletionKeywordKindForUID(UIdent uid) { if (uid == Keyword##kw##UID) { \ return CodeCompletionKeywordKind::kw_##kw; \ } -#include "swift/Syntax/TokenKinds.def" +#include "swift/AST/TokenKinds.def" // FIXME: should warn about unexpected keyword kind. return CodeCompletionKeywordKind::None; diff --git a/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp b/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp index 4f465d8876c86..4ec517452c567 100644 --- a/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp +++ b/tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp @@ -38,9 +38,6 @@ #include "swift/IDE/SourceEntityWalker.h" #include "swift/IDE/SyntaxModel.h" #include "swift/Subsystems.h" -#include "swift/SyntaxParse/SyntaxTreeCreator.h" -#include "swift/Syntax/Serialization/SyntaxSerialization.h" -#include "swift/Syntax/SyntaxNodes.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" @@ -695,14 +692,10 @@ class SwiftDocumentSemanticInfo : class SwiftDocumentSyntaxInfo { SourceManager SM; EditorDiagConsumer DiagConsumer; - std::shared_ptr SynTreeCreator; std::unique_ptr Parser; unsigned BufferID; std::vector Args; std::string PrimaryFile; - /// Whether or not the AST stored in the source file is up-to-date or just an - /// artifact of incremental syntax parsing - bool IncrementalParsingEnabled; bool IsParsed; public: @@ -719,17 +712,10 @@ class SwiftDocumentSyntaxInfo { BufferID = SM.addNewSourceBuffer(std::move(BufCopy)); DiagConsumer.setInputBufferIDs(BufferID); - if (CompInv.getLangOptions().BuildSyntaxTree) { - RC syntaxArena{new syntax::SyntaxArena()}; - SynTreeCreator = std::make_shared( - SM, BufferID, CompInv.getMainFileSyntaxParsingCache(), syntaxArena); - } - Parser.reset(new ParserUnit( SM, SourceFileKind::Main, BufferID, CompInv.getLangOptions(), CompInv.getTypeCheckerOptions(), CompInv.getSILOptions(), - CompInv.getModuleName(), SynTreeCreator, - CompInv.getMainFileSyntaxParsingCache())); + CompInv.getModuleName())); registerParseRequestFunctions(Parser->getParser().Context.evaluator); registerTypeCheckerRequestFunctions( @@ -737,8 +723,6 @@ class SwiftDocumentSyntaxInfo { registerClangImporterRequestFunctions(Parser->getParser().Context.evaluator); Parser->getDiagnosticEngine().addConsumer(DiagConsumer); - IncrementalParsingEnabled = - CompInv.getMainFileSyntaxParsingCache() != nullptr; IsParsed = false; } @@ -774,8 +758,6 @@ class SwiftDocumentSyntaxInfo { return SM; } - bool isIncrementalParsingEnabled() { return IncrementalParsingEnabled; } - ArrayRef getDiagnostics() { return DiagConsumer.getDiagnosticsForBuffer(BufferID); } @@ -1117,8 +1099,6 @@ struct SwiftEditorDocument::Implementation { llvm::Optional AffectedRange; /// Whether the last operation was an edit rather than a document open bool Edited; - /// The syntax tree of the document - llvm::Optional SyntaxTree; std::vector ParserDiagnostics; RefPtr SemanticInfo; @@ -2002,9 +1982,7 @@ void SwiftEditorDocument::updateSemaInfo( } void SwiftEditorDocument::resetSyntaxInfo(ImmutableTextSnapshotRef Snapshot, - SwiftLangSupport &Lang, - bool BuildSyntaxTree, - SyntaxParsingCache *SyntaxCache) { + SwiftLangSupport &Lang) { llvm::sys::ScopedLock L(Impl.AccessMtx); assert(Impl.SemanticInfo && "Impl.SemanticInfo must be set"); @@ -2027,11 +2005,9 @@ void SwiftEditorDocument::resetSyntaxInfo(ImmutableTextSnapshotRef Snapshot, Lang.getASTManager()->initCompilerInvocation( CompInv, Args, FrontendOptions::ActionType::Parse, StringRef(), Error); } - CompInv.getLangOptions().BuildSyntaxTree = BuildSyntaxTree; - CompInv.setMainFileSyntaxParsingCache(SyntaxCache); // When reuse parts of the syntax tree from a SyntaxParsingCache, not // all tokens are visited and thus token collection is invalid - CompInv.getLangOptions().CollectParsedToken = (SyntaxCache == nullptr); + CompInv.getLangOptions().CollectParsedToken = true; // Access to Impl.SyntaxInfo is guarded by Impl.AccessMtx Impl.SyntaxInfo.reset( new SwiftDocumentSyntaxInfo(CompInv, Snapshot, Args, Impl.FilePath)); @@ -2053,23 +2029,7 @@ void SwiftEditorDocument::readSyntaxInfo(EditorConsumer &Consumer, bool ReportDi SwiftSyntaxMap NewMap = SwiftSyntaxMap(Impl.SyntaxMap.Tokens.size() + 16); - if (Consumer.syntaxTreeEnabled()) { - auto SyntaxTree = Impl.SyntaxInfo->getSourceFile().getSyntaxRoot(); - Impl.SyntaxTree.emplace(SyntaxTree); - if (Consumer.syntaxMapEnabled()) { - Consumer.handleRequestError( - "Retrieving both a syntax map and a syntax tree at the same time is " - "not supported. Use the SyntaxClassifier in swiftSyntax to generate " - "the syntax map on the Swift side."); - } - if (Consumer.documentStructureEnabled()) { - Consumer.handleRequestError( - "Retrieving both the document structure and a syntax tree at the " - "same time is not supported. Use the syntax tree to compute the " - "document structure."); - } - } else if (Consumer.documentStructureEnabled() || - Consumer.syntaxMapEnabled()) { + if (Consumer.documentStructureEnabled() || Consumer.syntaxMapEnabled()) { ide::SyntaxModelContext ModelContext(Impl.SyntaxInfo->getSourceFile()); SwiftEditorSyntaxWalker SyntaxWalker( @@ -2150,17 +2110,8 @@ const CodeFormatOptions &SwiftEditorDocument::getFormatOptions() { return Impl.FormatOptions; } -const llvm::Optional & -SwiftEditorDocument::getSyntaxTree() const { - return Impl.SyntaxTree; -} - std::string SwiftEditorDocument::getFilePath() const { return Impl.FilePath; } -bool SwiftEditorDocument::isIncrementalParsingEnabled() const { - return Impl.SyntaxInfo->isIncrementalParsingEnabled(); -} - llvm::IntrusiveRefCntPtr SwiftEditorDocument::getFileSystem() const { llvm::sys::ScopedLock L(Impl.AccessMtx); @@ -2367,7 +2318,7 @@ void SwiftLangSupport::editorOpen( EditorDoc = new SwiftEditorDocument(Name, *this, fileSystem); Snapshot = EditorDoc->initializeText( Buf, Args, Consumer.needsSemanticInfo(), fileSystem); - EditorDoc->resetSyntaxInfo(Snapshot, *this, Consumer.syntaxTreeEnabled()); + EditorDoc->resetSyntaxInfo(Snapshot, *this); if (EditorDocuments->getOrUpdate(Name, *this, EditorDoc)) { // Document already exists, re-initialize it. This should only happen // if we get OPEN request while the previous document is not closed. @@ -2381,7 +2332,7 @@ void SwiftLangSupport::editorOpen( if (!Snapshot) { Snapshot = EditorDoc->initializeText( Buf, Args, Consumer.needsSemanticInfo(), fileSystem); - EditorDoc->resetSyntaxInfo(Snapshot, *this, Consumer.syntaxTreeEnabled()); + EditorDoc->resetSyntaxInfo(Snapshot, *this); } if (Consumer.needsSemanticInfo()) { @@ -2392,17 +2343,11 @@ void SwiftLangSupport::editorOpen( if (!Consumer.documentStructureEnabled() && !Consumer.syntaxMapEnabled() && - !Consumer.diagnosticsEnabled() && - !Consumer.syntaxTreeEnabled()) { + !Consumer.diagnosticsEnabled()) { return; } EditorDoc->readSyntaxInfo(Consumer, /*ReportDiags=*/true); - - if (Consumer.syntaxTreeEnabled()) { - assert(EditorDoc->getSyntaxTree().hasValue()); - Consumer.handleSyntaxTree(EditorDoc->getSyntaxTree().getValue()); - } } //===----------------------------------------------------------------------===// @@ -2427,96 +2372,10 @@ void SwiftLangSupport::editorClose(StringRef Name, bool RemoveCache) { // EditorReplaceText //===----------------------------------------------------------------------===// -void verifyIncrementalParse(SwiftEditorDocumentRef EditorDoc, - unsigned EditOffset, unsigned EditLength, - StringRef PreEditText, StringRef ReplaceText) { - // Dump the incremental syntax tree - std::string IncrTreeString; - llvm::raw_string_ostream IncrTreeStream(IncrTreeString); - swift::json::Output IncrTreeOutput(IncrTreeStream); - IncrTreeOutput << *EditorDoc->getSyntaxTree()->getRaw(); - - // Reparse the file from scratch - CompilerInvocation Invocation; - Invocation.getLangOptions().BuildSyntaxTree = true; - std::vector Args; - SwiftDocumentSyntaxInfo ScratchSyntaxInfo(Invocation, - EditorDoc->getLatestSnapshot(), - Args, EditorDoc->getFilePath()); - ScratchSyntaxInfo.parseIfNeeded(); - - // Dump the from-scratch syntax tree - std::string FromScratchTreeString; - llvm::raw_string_ostream ScratchTreeStream(FromScratchTreeString); - swift::json::Output ScratchTreeOutput(ScratchTreeStream); - auto SyntaxRoot = ScratchSyntaxInfo.getSourceFile().getSyntaxRoot(); - ScratchTreeOutput << *SyntaxRoot.getRaw(); - - // If the serialized format of the two trees doesn't match incremental parsing - // we have found an error. - if (IncrTreeStream.str().compare(ScratchTreeStream.str())) { - LOG_SECTION("Incremental Parsing", Warning) { - Log->getOS() << "Incremental parsing different to from scratch parsing\n"; - Log->getOS() << "Edit was " << EditOffset << "-" - << (EditOffset + EditLength) << "='" << ReplaceText << "'" - << " pre-edit-text: '" << PreEditText << "'\n"; - - SmallString<32> DirectoryName; - if (llvm::sys::fs::createUniqueDirectory( - "SourceKit-IncrementalParsing-Inconsistency", DirectoryName)) { - Log->getOS() << "Failed to create log directory\n"; - } - - std::error_code ErrorCode; - - // Write the incremental syntax tree - auto IncrTreeFilename = DirectoryName + "/incrementalTree.json"; - llvm::raw_fd_ostream IncrementalFilestream( - IncrTreeFilename.str(), ErrorCode, - llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write); - IncrementalFilestream << IncrTreeStream.str(); - if (ErrorCode) { - Log->getOS() << "Failed to write incremental syntax tree to " - << IncrTreeFilename << "(error code " << ErrorCode.value() - << ": " << ErrorCode.message() << ")\n"; - } else { - Log->getOS() << "Incremental syntax tree written to " - << IncrTreeFilename << '\n'; - } - - // Write from-scratch syntax tree - auto ScratchTreeFilename = DirectoryName + "/fromScratchTree.json"; - llvm::raw_fd_ostream ScratchTreeFilestream( - ScratchTreeFilename.str(), ErrorCode, - llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write); - ScratchTreeFilestream << ScratchTreeStream.str(); - if (ErrorCode) { - Log->getOS() << "Failed to write from-scratch syntax tree to " - << ScratchTreeFilename << "(error code " - << ErrorCode.value() << ": " << ErrorCode.message() - << ")\n"; - } else { - Log->getOS() << "From-scratch syntax tree written to " - << ScratchTreeFilename << '\n'; - } - - // Write source file - auto SourceFilename = DirectoryName + "/postEditSource.swift"; - llvm::raw_fd_ostream SourceFilestream(SourceFilename.str(), ErrorCode, - llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write); - auto FileBuffer = EditorDoc->getLatestSnapshot()->getBuffer(); - SourceFilestream << FileBuffer->getText(); - } - } -} - void SwiftLangSupport::editorReplaceText(StringRef Name, llvm::MemoryBuffer *Buf, unsigned Offset, unsigned Length, EditorConsumer &Consumer) { - bool LogReuseRegions = ::getenv("SOURCEKIT_LOG_INCREMENTAL_REUSE_REGIONS"); - bool ValidateSyntaxTree = ::getenv("SOURCEKIT_INCREMENTAL_PARSE_VALIDATION"); - auto EditorDoc = EditorDocuments->getByUnresolvedName(Name); if (!EditorDoc) { Consumer.handleRequestError("No associated Editor Document"); @@ -2525,13 +2384,6 @@ void SwiftLangSupport::editorReplaceText(StringRef Name, ImmutableTextSnapshotRef Snapshot; if (Length != 0 || Buf->getBufferSize() != 0) { - std::string PreEditText; - if (ValidateSyntaxTree) { - auto CurBuffer = EditorDoc->getLatestSnapshot()->getBuffer(); - auto BufferStart = CurBuffer->getInternalBuffer()->getBufferStart(); - StringRef PreEditTextRef(BufferStart + Offset, Length); - PreEditText = PreEditTextRef.str(); - } std::string error; Snapshot = EditorDoc->replaceText(Offset, Length, Buf, Consumer.needsSemanticInfo(), error); @@ -2541,60 +2393,17 @@ void SwiftLangSupport::editorReplaceText(StringRef Name, return; } - llvm::Optional SyntaxCache = llvm::None; - if (EditorDoc->getSyntaxTree().hasValue()) { - SyntaxCache.emplace(EditorDoc->getSyntaxTree().getValue()); - SyntaxCache->addEdit(Offset, Offset + Length, Buf->getBufferSize()); - } - // If client doesn't need any information, we doesn't need to parse it. - - - SyntaxParsingCache *SyntaxCachePtr = nullptr; - if (SyntaxCache.hasValue()) { - SyntaxCachePtr = SyntaxCache.getPointer(); - } - EditorDoc->resetSyntaxInfo(Snapshot, *this, Consumer.syntaxTreeEnabled(), - SyntaxCachePtr); + EditorDoc->resetSyntaxInfo(Snapshot, *this); if (!Consumer.documentStructureEnabled() && !Consumer.syntaxMapEnabled() && - !Consumer.diagnosticsEnabled() && - !Consumer.syntaxTreeEnabled()) { + !Consumer.diagnosticsEnabled()) { return; } // Do not report syntactic diagnostics; will be handled in readSemanticInfo. EditorDoc->readSyntaxInfo(Consumer, /*ReportDiags=*/false); - - // Log reuse information - if (SyntaxCache.hasValue() && LogReuseRegions) { - auto &SyntaxTree = EditorDoc->getSyntaxTree(); - auto ReuseRegions = SyntaxCache->getReusedRegions(*SyntaxTree); - LOG_SECTION("SyntaxCache", InfoHighPrio) { - Log->getOS() << "Reused "; - - bool FirstIteration = true; - for (auto ReuseRegion : ReuseRegions) { - if (!FirstIteration) { - Log->getOS() << ", "; - } else { - FirstIteration = false; - } - - Log->getOS() << ReuseRegion.Start << " - " << ReuseRegion.End; - } - } - } - - if (Consumer.syntaxTreeEnabled()) { - Consumer.handleSyntaxTree(EditorDoc->getSyntaxTree().getValue()); - } - - if (ValidateSyntaxTree) { - verifyIncrementalParse(EditorDoc, Offset, Length, PreEditText, - Buf->getBuffer()); - } } else { Snapshot = EditorDoc->getLatestSnapshot(); } @@ -2622,14 +2431,6 @@ void SwiftLangSupport::editorFormatText(StringRef Name, unsigned Line, return; } - if (EditorDoc->isIncrementalParsingEnabled()) { - // If incremental parsing is enabled, AST is not updated properly. Fall - // back to a full reparse of the file. - EditorDoc->resetSyntaxInfo(EditorDoc->getLatestSnapshot(), *this, - /*BuildSyntaxTree=*/true, - /*SyntaxCache=*/nullptr); - } - EditorDoc->formatText(Line, Length, Consumer); } @@ -2660,14 +2461,5 @@ void SwiftLangSupport::editorExpandPlaceholder(StringRef Name, unsigned Offset, Consumer.handleRequestError("No associated Editor Document"); return; } - - if (EditorDoc->isIncrementalParsingEnabled()) { - // If incremental parsing is enabled, AST is not updated properly. Fall - // back to a full reparse of the file. - EditorDoc->resetSyntaxInfo(EditorDoc->getLatestSnapshot(), *this, - /*BuildSyntaxTree=*/true, - /*SyntaxCache=*/nullptr); - } - EditorDoc->expandPlaceholder(Offset, Length, Consumer); } diff --git a/tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h b/tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h index 29778ea9c6687..59d71674b3baa 100644 --- a/tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h +++ b/tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h @@ -49,10 +49,6 @@ namespace swift { class GenericSignature; enum class AccessorKind; -namespace syntax { - class SourceFileSyntax; -} - namespace ide { class CodeCompletionCache; class CompletionInstance; @@ -112,8 +108,7 @@ class SwiftEditorDocument : ImmutableTextSnapshotRef getLatestSnapshot() const; void resetSyntaxInfo(ImmutableTextSnapshotRef Snapshot, - SwiftLangSupport &Lang, bool BuildSyntaxTree, - swift::SyntaxParsingCache *SyntaxCache = nullptr); + SwiftLangSupport &Lang); void readSyntaxInfo(EditorConsumer &consumer, bool ReportDiags); void readSemanticInfo(ImmutableTextSnapshotRef Snapshot, EditorConsumer& Consumer); @@ -127,14 +122,8 @@ class SwiftEditorDocument : static void reportDocumentStructure(swift::SourceFile &SrcFile, EditorConsumer &Consumer); - const llvm::Optional &getSyntaxTree() const; - std::string getFilePath() const; - - /// Whether or not the AST stored for this document is up-to-date or just an - /// artifact of incremental syntax parsing - bool isIncrementalParsingEnabled() const; - + /// Returns the virtual filesystem associated with this document. llvm::IntrusiveRefCntPtr getFileSystem() const; }; diff --git a/tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp b/tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp index ec82bd0ec99cf..a8327e413785f 100644 --- a/tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp +++ b/tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp @@ -121,7 +121,6 @@ bool TestOptions::parseArgs(llvm::ArrayRef Args) { .Case("cursor", SourceKitRequest::CursorInfo) .Case("related-idents", SourceKitRequest::RelatedIdents) .Case("syntax-map", SourceKitRequest::SyntaxMap) - .Case("syntax-tree", SourceKitRequest::SyntaxTree) .Case("structure", SourceKitRequest::Structure) .Case("format", SourceKitRequest::Format) .Case("expand-placeholder", SourceKitRequest::ExpandPlaceholder) @@ -176,7 +175,6 @@ bool TestOptions::parseArgs(llvm::ArrayRef Args) { << "- cursor\n" << "- related-idents\n" << "- syntax-map\n" - << "- syntax-tree\n" << "- structure\n" << "- format\n" << "- expand-placeholder\n" diff --git a/tools/SourceKit/tools/sourcekitd-test/TestOptions.h b/tools/SourceKit/tools/sourcekitd-test/TestOptions.h index f5e8835ad2c88..fd6ede96bbb00 100644 --- a/tools/SourceKit/tools/sourcekitd-test/TestOptions.h +++ b/tools/SourceKit/tools/sourcekitd-test/TestOptions.h @@ -61,7 +61,6 @@ enum class SourceKitRequest { NameTranslation, MarkupToXML, Statistics, - SyntaxTree, EnableCompileNotifications, CollectExpressionType, CollectVariableType, diff --git a/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp b/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp index 9df9e539caf2f..e6cc30dcd9af5 100644 --- a/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp +++ b/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp @@ -883,8 +883,6 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) { sourcekitd_request_dictionary_set_string(Req, KeyName, SemaName.c_str()); sourcekitd_request_dictionary_set_int64(Req, KeyEnableSyntaxMap, true); sourcekitd_request_dictionary_set_int64(Req, KeyEnableStructure, false); - sourcekitd_request_dictionary_set_uid(Req, KeySyntaxTreeTransferMode, - KindSyntaxTreeOff); sourcekitd_request_dictionary_set_int64(Req, KeySyntacticOnly, !Opts.UsedSema); break; @@ -893,8 +891,6 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) { sourcekitd_request_dictionary_set_string(Req, KeyName, SemaName.c_str()); sourcekitd_request_dictionary_set_int64(Req, KeyEnableSyntaxMap, false); sourcekitd_request_dictionary_set_int64(Req, KeyEnableStructure, true); - sourcekitd_request_dictionary_set_uid(Req, KeySyntaxTreeTransferMode, - KindSyntaxTreeOff); sourcekitd_request_dictionary_set_int64(Req, KeySyntacticOnly, !Opts.UsedSema); break; @@ -903,8 +899,6 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) { sourcekitd_request_dictionary_set_string(Req, KeyName, SemaName.c_str()); sourcekitd_request_dictionary_set_int64(Req, KeyEnableSyntaxMap, false); sourcekitd_request_dictionary_set_int64(Req, KeyEnableStructure, false); - sourcekitd_request_dictionary_set_uid(Req, KeySyntaxTreeTransferMode, - KindSyntaxTreeOff); sourcekitd_request_dictionary_set_int64(Req, KeySyntacticOnly, !Opts.UsedSema); break; @@ -929,16 +923,6 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) { } break; - case SourceKitRequest::SyntaxTree: - sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestEditorOpen); - sourcekitd_request_dictionary_set_string(Req, KeyName, SemaName.c_str()); - sourcekitd_request_dictionary_set_int64(Req, KeyEnableSyntaxMap, false); - sourcekitd_request_dictionary_set_int64(Req, KeyEnableStructure, false); - sourcekitd_request_dictionary_set_uid(Req, KeySyntaxTreeTransferMode, - KindSyntaxTreeFull); - sourcekitd_request_dictionary_set_int64(Req, KeySyntacticOnly, true); - break; - case SourceKitRequest::DocInfo: sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestDocInfo); break; @@ -1408,13 +1392,6 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts, printFoundUSR(Info, SourceBuf.get(), llvm::outs()); break; - case SourceKitRequest::SyntaxTree: { - // Print only the serialized syntax tree. - llvm::outs() << sourcekitd_variant_dictionary_get_string( - sourcekitd_response_get_value(Resp), KeySerializedSyntaxTree); - llvm::outs() << '\n'; - break; - } case SourceKitRequest::SyntaxMap: case SourceKitRequest::Structure: printRawResponse(Resp); diff --git a/tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp b/tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp index 7619f6d9c5139..04983fd984275 100644 --- a/tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp +++ b/tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp @@ -37,8 +37,6 @@ #include "swift/Basic/Version.h" #include "swift/Demangling/Demangler.h" #include "swift/Demangling/ManglingMacros.h" -#include "swift/Syntax/Serialization/SyntaxSerialization.h" -#include "swift/Syntax/SyntaxNodes.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" @@ -87,7 +85,6 @@ struct SKEditorConsumerOptions { bool EnableSyntaxMap = false; bool EnableStructure = false; bool EnableDiagnostics = false; - SyntaxTreeTransferMode SyntaxTransferMode = SyntaxTreeTransferMode::Off; bool SyntacticOnly = false; }; @@ -330,19 +327,6 @@ findRenameRanges(llvm::MemoryBuffer *InputBuf, static bool isSemanticEditorDisabled(); static void enableCompileNotifications(bool value); -static SyntaxTreeTransferMode syntaxTransferModeFromUID(sourcekitd_uid_t UID) { - if (UID == nullptr) { - // Default is no syntax tree - return SyntaxTreeTransferMode::Off; - } else if (UID == KindSyntaxTreeOff) { - return SyntaxTreeTransferMode::Off; - } else if (UID == KindSyntaxTreeFull) { - return SyntaxTreeTransferMode::Full; - } else { - llvm_unreachable("Unexpected syntax tree transfer mode"); - } -} - namespace { class SKOptionsDictionary : public OptionsDictionary { RequestDict Options; @@ -666,7 +650,6 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, Req.getInt64(KeyEnableStructure, EnableStructure, /*isOptional=*/true); int64_t EnableDiagnostics = true; Req.getInt64(KeyEnableDiagnostics, EnableDiagnostics, /*isOptional=*/true); - auto TransferModeUID = Req.getUID(KeySyntaxTreeTransferMode); int64_t SyntacticOnly = false; Req.getInt64(KeySyntacticOnly, SyntacticOnly, /*isOptional=*/true); @@ -674,7 +657,6 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, Opts.EnableSyntaxMap = EnableSyntaxMap; Opts.EnableStructure = EnableStructure; Opts.EnableDiagnostics = EnableDiagnostics; - Opts.SyntaxTransferMode = syntaxTransferModeFromUID(TransferModeUID); Opts.SyntacticOnly = SyntacticOnly; return Rec(editorOpen(*Name, InputBuf.get(), Opts, Args, std::move(vfsOptions))); } @@ -708,13 +690,11 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, Req.getInt64(KeyEnableDiagnostics, EnableDiagnostics, /*isOptional=*/true); int64_t SyntacticOnly = false; Req.getInt64(KeySyntacticOnly, SyntacticOnly, /*isOptional=*/true); - auto TransferModeUID = Req.getUID(KeySyntaxTreeTransferMode); SKEditorConsumerOptions Opts; Opts.EnableSyntaxMap = EnableSyntaxMap; Opts.EnableStructure = EnableStructure; Opts.EnableDiagnostics = EnableDiagnostics; - Opts.SyntaxTransferMode = syntaxTransferModeFromUID(TransferModeUID); Opts.SyntacticOnly = SyntacticOnly; return Rec(editorReplaceText(*Name, InputBuf.get(), Offset, Length, Opts)); @@ -2763,12 +2743,6 @@ class SKEditorConsumer : public EditorConsumer { void handleSourceText(StringRef Text) override; - void handleSyntaxTree(const swift::syntax::SourceFileSyntax &SyntaxTree) override; - - SyntaxTreeTransferMode syntaxTreeTransferMode() override { - return Opts.SyntaxTransferMode; - } - void finished() override { if (RespReceiver) RespReceiver(createResponse()); @@ -3111,37 +3085,6 @@ void SKEditorConsumer::handleSourceText(StringRef Text) { Dict.set(KeySourceText, Text); } -void serializeSyntaxTreeAsJson( - const swift::syntax::SourceFileSyntax &SyntaxTree, - ResponseBuilder::Dictionary &Dict) { - auto StartClock = clock(); - // 4096 is a heuristic buffer size that appears to usually be able to fit an - // incremental syntax tree - llvm::SmallString<4096> SyntaxTreeString; - { - llvm::raw_svector_ostream SyntaxTreeStream(SyntaxTreeString); - swift::json::Output::UserInfoMap JsonUserInfo; - swift::json::Output SyntaxTreeOutput(SyntaxTreeStream, JsonUserInfo, - /*PrettyPrint=*/false); - SyntaxTreeOutput << *SyntaxTree.getRaw(); - } - Dict.set(KeySerializedSyntaxTree, SyntaxTreeString); - - auto EndClock = clock(); - LOG_SECTION("incrParse Performance", InfoLowPrio) { - Log->getOS() << "Serialized " << SyntaxTreeString.size() - << " bytes as JSON in "; - auto Seconds = (double)(EndClock - StartClock) * 1000 / CLOCKS_PER_SEC; - llvm::write_double(Log->getOS(), Seconds, llvm::FloatStyle::Fixed, 2); - Log->getOS() << "ms"; - } -} - -void SKEditorConsumer::handleSyntaxTree( - const swift::syntax::SourceFileSyntax &SyntaxTree) { - serializeSyntaxTreeAsJson(SyntaxTree, Dict); -} - static sourcekitd_response_t editorFindUSR(StringRef DocumentName, StringRef USR) { ResponseBuilder RespBuilder; diff --git a/tools/swift-ide-test/swift-ide-test.cpp b/tools/swift-ide-test/swift-ide-test.cpp index 18a8fe711745b..4a7a6e874cb94 100644 --- a/tools/swift-ide-test/swift-ide-test.cpp +++ b/tools/swift-ide-test/swift-ide-test.cpp @@ -52,7 +52,6 @@ #include "swift/Markup/Markup.h" #include "swift/Parse/ParseVersion.h" #include "swift/Sema/IDETypeChecking.h" -#include "swift/SyntaxParse/SyntaxTreeCreator.h" #include "clang/Rewrite/Core/RewriteBuffer.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Statistic.h" @@ -1928,7 +1927,6 @@ static int doSyntaxColoring(const CompilerInvocation &InitInvok, Invocation.getLangOptions().DisableAvailabilityChecking = false; Invocation.getLangOptions().Playground = Playground; Invocation.getLangOptions().CollectParsedToken = true; - Invocation.getLangOptions().BuildSyntaxTree = true; // Display diagnostics to stderr. PrintingDiagnosticConsumer PrintDiags; @@ -1969,17 +1967,10 @@ static int doSyntaxColoring(const CompilerInvocation &InitInvok, SourceManager SM; unsigned BufferID = SM.addNewSourceBuffer(std::move(FileBuf)); - RC syntaxArena{new syntax::SyntaxArena()}; - std::shared_ptr SynTreeCreator = - std::make_shared( - SM, BufferID, Invocation.getMainFileSyntaxParsingCache(), - syntaxArena); - ParserUnit Parser( SM, SourceFileKind::Main, BufferID, Invocation.getLangOptions(), Invocation.getTypeCheckerOptions(), Invocation.getSILOptions(), - Invocation.getModuleName(), SynTreeCreator, - Invocation.getMainFileSyntaxParsingCache()); + Invocation.getModuleName()); registerParseRequestFunctions(Parser.getParser().Context.evaluator); registerTypeCheckerRequestFunctions(Parser.getParser().Context.evaluator); @@ -2194,7 +2185,6 @@ static int doStructureAnnotation(const CompilerInvocation &InitInvok, return 1; CompilerInvocation Invocation(InitInvok); - Invocation.getLangOptions().BuildSyntaxTree = true; Invocation.getLangOptions().CollectParsedToken = true; Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFilename); @@ -2206,17 +2196,10 @@ static int doStructureAnnotation(const CompilerInvocation &InitInvok, SourceManager SM; unsigned BufferID = SM.addNewSourceBuffer(std::move(FileBuf)); - RC syntaxArena{new syntax::SyntaxArena()}; - std::shared_ptr SynTreeCreator = - std::make_shared( - SM, BufferID, Invocation.getMainFileSyntaxParsingCache(), - syntaxArena); - ParserUnit Parser(SM, SourceFileKind::Main, BufferID, Invocation.getLangOptions(), Invocation.getTypeCheckerOptions(), - Invocation.getSILOptions(), Invocation.getModuleName(), - SynTreeCreator, Invocation.getMainFileSyntaxParsingCache()); + Invocation.getSILOptions(), Invocation.getModuleName()); registerParseRequestFunctions(Parser.getParser().Context.evaluator); registerTypeCheckerRequestFunctions( @@ -3939,7 +3922,6 @@ static int doPrintRangeInfo(const CompilerInvocation &InitInvok, CompilerInvocation Invocation(InitInvok); Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFileName); Invocation.getLangOptions().DisableAvailabilityChecking = false; - Invocation.getLangOptions().BuildSyntaxTree = true; Invocation.getLangOptions().CollectParsedToken = true; CompilerInstance CI; @@ -4316,7 +4298,6 @@ int main(int argc, char *argv[]) { .addMapping(SplitMap.first, SplitMap.second); } InitInvok.getLangOptions().CollectParsedToken = true; - InitInvok.getLangOptions().BuildSyntaxTree = true; InitInvok.getLangOptions().EnableCrossImportOverlays = options::EnableCrossImportOverlays; if (options::DisableObjCInterop) { diff --git a/tools/swift-refactor/swift-refactor.cpp b/tools/swift-refactor/swift-refactor.cpp index ac7df7b7a119d..b141de5e3905a 100644 --- a/tools/swift-refactor/swift-refactor.cpp +++ b/tools/swift-refactor/swift-refactor.cpp @@ -311,7 +311,6 @@ int main(int argc, char *argv[]) { Invocation.getFrontendOptions().RequestedAction = FrontendOptions::ActionType::Typecheck; Invocation.getLangOptions().AttachCommentsToDecls = true; Invocation.getLangOptions().CollectParsedToken = true; - Invocation.getLangOptions().BuildSyntaxTree = true; Invocation.getLangOptions().DisableAvailabilityChecking = true; if (options::EnableExperimentalConcurrency) diff --git a/tools/swift-syntax-test/CMakeLists.txt b/tools/swift-syntax-test/CMakeLists.txt deleted file mode 100644 index 99e242a5d6da0..0000000000000 --- a/tools/swift-syntax-test/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -add_swift_host_tool(swift-syntax-test - swift-syntax-test.cpp - HAS_SWIFT_MODULES - LLVM_LINK_COMPONENTS - Support - SWIFT_COMPONENT testsuite-tools -) -target_link_libraries(swift-syntax-test - PRIVATE - swiftAST - swiftDriver - swiftFrontend - swiftSema - swiftSyntax - swiftCompilerModules_SwiftSyntax) diff --git a/tools/swift-syntax-test/swift-syntax-test.cpp b/tools/swift-syntax-test/swift-syntax-test.cpp deleted file mode 100644 index 0835f38996609..0000000000000 --- a/tools/swift-syntax-test/swift-syntax-test.cpp +++ /dev/null @@ -1,948 +0,0 @@ -//===--- swift-syntax-test.cpp - Reflection Syntax testing application ----===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// -// -// This is a host-side tool to perform round-trip testing of "full-fidelity" -// lexing and parsing. That is, when this application ingests a .swift file, -// it should be able to create a list of full tokens, or a full-fidelity AST, -// print them, and get the same file back out. This ensures that we aren't -// losing any source information in these structures. -// -//===----------------------------------------------------------------------===// - -#include "swift/AST/DiagnosticEngine.h" -#include "swift/AST/DiagnosticsFrontend.h" -#include "swift/Basic/Defer.h" -#include "swift/Basic/InitializeSwiftModules.h" -#include "swift/Basic/LLVMInitialize.h" -#include "swift/Basic/LangOptions.h" -#include "swift/Basic/SourceManager.h" -#include "swift/Frontend/Frontend.h" -#include "swift/Frontend/PrintingDiagnosticConsumer.h" -#include "swift/Parse/Lexer.h" -#include "swift/Parse/Parser.h" -#include "swift/Subsystems.h" -#include "swift/Syntax/Serialization/SyntaxDeserialization.h" -#include "swift/Syntax/Serialization/SyntaxSerialization.h" -#include "swift/Syntax/SyntaxData.h" -#include "swift/Syntax/SyntaxNodes.h" -#include "llvm/Support/BinaryByteStream.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/FileOutputBuffer.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/Regex.h" -#include "llvm/Support/raw_ostream.h" - -using namespace swift; -using namespace swift::syntax; -using llvm::StringRef; - -enum class ActionType { - DumpRawTokenSyntax, - FullLexRoundTrip, - FullParseRoundTrip, - SerializeRawTree, - DeserializeRawTree, - ParseOnly, - ParserGen, - EOFPos, - None -}; - -namespace options { -static llvm::cl::OptionCategory Category("swift-syntax-test Options"); -static llvm::cl::opt -Action(llvm::cl::desc("Action (required):"), - llvm::cl::init(ActionType::None), - llvm::cl::values( - clEnumValN(ActionType::DumpRawTokenSyntax, - "dump-full-tokens", - "Lex the source file and dump the tokens " - "and their absolute line/column locations"), - clEnumValN(ActionType::FullLexRoundTrip, - "round-trip-lex", - "Lex the source file and print it back out for " - "comparing against the original"), - clEnumValN(ActionType::FullParseRoundTrip, - "round-trip-parse", - "Parse the source file and print it back out for " - "comparing against the input"), - clEnumValN(ActionType::ParseOnly, - "parse-only", - "Parse the source file with syntax nodes and exit"), - clEnumValN(ActionType::ParserGen, - "parse-gen", - "Parse the source file and print it back out for " - "comparing against the input"), - clEnumValN(ActionType::SerializeRawTree, - "serialize-raw-tree", - "Parse the source file and serialize the raw tree " - "to JSON"), - clEnumValN(ActionType::DeserializeRawTree, - "deserialize-raw-tree", - "Parse the JSON file from the serialized raw tree " - "to the original"), - clEnumValN(ActionType::EOFPos, - "eof", - "Parse the source file, calculate the absolute position" - "of the EOF token, and dump the buffer from the start of the" - "file to the EOF token"))); - -static llvm::cl::opt -InputSourceFilename("input-source-filename", - llvm::cl::desc("Path to the input .swift file")); - -static llvm::cl::opt -InputSourceDirectory("input-source-directory", - llvm::cl::desc("Directory to be scanned recursively and " - "run the selected action on every .swift " - "file")); - -static llvm::cl::opt -OldSyntaxTreeFilename("old-syntax-tree-filename", - llvm::cl::desc("Path to the serialized syntax tree of " - "the pre-edit file")); - -static llvm::cl::opt -OldSourceFilename("old-source-filename", - llvm::cl::desc("Path to the pre-edit source file to " - "translate line:column edits into the " - "file's byte offsets")); - -static llvm::cl::list -IncrementalEdits("incremental-edit", - llvm::cl::desc("An edit that was applied to reach the input " - "file from the source file that generated the " - "old syntax tree in the format :" - "-:=" - " where start and end are defined " - "in terms of the pre-edit file and " - " is the string that shall " - "replace the selected range. " - "Can be passed multiple times.")); - -static llvm::cl::list -ReparseRegions("reparse-region", - llvm::cl::desc("If specified, an error will be emitted if any " - "part of the file outside of the reparse region " - "gets parsed again. " - "Can be passed multiple times to specify " - "multiple reparse regions. " - "Reparse regions are specified in the form " - "-: in terms " - "of the post-edit file")); - -static llvm::cl::opt -IncrementalReuseLog("incremental-reuse-log", - llvm::cl::desc("Path to which a log should be written that " - "describes all the nodes reused during " - "incremental parsing.")); - -static llvm::cl::opt -OmitNodeIds("omit-node-ids", - llvm::cl::desc("If specified, the serialized syntax tree will not " - "include the IDs of the serialized nodes.")); - -static llvm::cl::opt -OutputFilename("output-filename", - llvm::cl::desc("Path to the output file")); - -static llvm::cl::opt -DiagsOutputFilename("diags-output-filename", - llvm::cl::desc("Path to the output file for parser diagnostics text")); - -static llvm::cl::opt -PrintVisualReuseInfo("print-visual-reuse-info", - llvm::cl::desc("Print a coloured output of which parts of " - "the source file have been reused from the " - "old syntax tree."), - llvm::cl::cat(Category), - llvm::cl::init(false)); - -static llvm::cl::opt -ForceColoredOutput("force-colored-output", - llvm::cl::desc("Print colored output even if the shell " - "does not support it."), - llvm::cl::cat(Category), - llvm::cl::init(false)); - -static llvm::cl::opt -PrintNodeKind("print-node-kind", - llvm::cl::desc("To print syntax node kind"), - llvm::cl::cat(Category), - llvm::cl::init(false)); - -static llvm::cl::opt -PrintTrivialNodeKind("print-trivial-node-kind", - llvm::cl::desc("To print trivial syntax node kind"), - llvm::cl::cat(Category), - llvm::cl::init(false)); - -static llvm::cl::opt -VerifySyntaxTree("verify-syntax-tree", - llvm::cl::desc("Emit warnings for unknown nodes"), - llvm::cl::cat(Category), - llvm::cl::init(true)); - -static llvm::cl::opt FailOnParseError( - "fail-on-parse-error", - llvm::cl::desc("Exit with a non-zero exit code if there was a parsing " - "error (including unknown syntax nodes)"), - llvm::cl::cat(Category), llvm::cl::init(false)); - -static llvm::cl::opt -Visual("v", - llvm::cl::desc("Print visually"), - llvm::cl::cat(Category), - llvm::cl::init(false)); - -} // end namespace options - -namespace { - -// A utility class to wrap a source range consisting of a byte start and end -// offset -struct ByteBasedSourceRange { - uintptr_t Start; - uintptr_t End; - - ByteBasedSourceRange(uintptr_t Start, uintptr_t End) - : Start(Start), End(End) { - assert(Start <= End); - } - ByteBasedSourceRange() : ByteBasedSourceRange(0, 0) {} - - ByteBasedSourceRange intersect(const ByteBasedSourceRange &Other) { - auto Start = std::max(this->Start, Other.Start); - auto End = std::min(this->End, Other.End); - if (Start > End) { - return {0, 0}; - } else { - return {Start, End}; - } - } - - bool empty() { return Start == End; } - - CharSourceRange toCharSourceRange(SourceManager &SourceMgr, unsigned BufferID) { - auto StartLoc = SourceMgr.getLocForOffset(BufferID, Start); - auto EndLoc = SourceMgr.getLocForOffset(BufferID, End); - return CharSourceRange(SourceMgr, StartLoc, EndLoc); - } -}; - -// The union of multiple offset-based source ranges -struct ByteBasedSourceRangeSet { - std::vector Ranges; - - ByteBasedSourceRangeSet() {} - - ByteBasedSourceRangeSet(std::vector Ranges) { - for (auto Range : Ranges) { - addRange({Range.Start.getOffset(), Range.End.getOffset()}); - } - } - - void addRange(ByteBasedSourceRange Range) { Ranges.push_back(Range); } - - ByteBasedSourceRangeSet invert(unsigned FileLength) { - ByteBasedSourceRangeSet Result; - unsigned CurrentOffset = 0; - for (auto Range : Ranges) { - assert(CurrentOffset <= Range.Start && - "Ranges must be sorted in ascending order and not be overlapping"); - if (CurrentOffset < Range.Start) { - Result.addRange({CurrentOffset, Range.Start}); - } - CurrentOffset = Range.End; - } - if (CurrentOffset < FileLength) { - Result.addRange({CurrentOffset, FileLength}); - } - - return Result; - } - - ByteBasedSourceRangeSet intersect(ByteBasedSourceRangeSet Other) { - ByteBasedSourceRangeSet Intersection; - for (auto A : Ranges) { - for (auto B : Other.Ranges) { - auto PartialIntersection = A.intersect(B); - if (!PartialIntersection.empty()) { - Intersection.addRange(PartialIntersection); - } - } - } - return Intersection; - } -}; - -int getTokensFromFile( - unsigned BufferID, LangOptions &LangOpts, SourceManager &SourceMgr, - const RC &Arena, swift::DiagnosticEngine &Diags, - std::vector> &Tokens) { - Tokens = tokenizeWithTrivia(LangOpts, SourceMgr, BufferID, Arena, - /*Offset=*/0, /*EndOffset=*/0, &Diags); - return EXIT_SUCCESS; -} - -int getTokensFromFile( - const StringRef InputFilename, LangOptions &LangOpts, - SourceManager &SourceMgr, const RC &Arena, - DiagnosticEngine &Diags, unsigned int &OutBufferID, - std::vector> &Tokens) { - auto Buffer = llvm::MemoryBuffer::getFile(InputFilename); - if (!Buffer) { - Diags.diagnose(SourceLoc(), diag::cannot_open_file, - InputFilename, Buffer.getError().message()); - return EXIT_FAILURE; - } - - OutBufferID = SourceMgr.addNewSourceBuffer(std::move(Buffer.get())); - return getTokensFromFile(OutBufferID, LangOpts, SourceMgr, Arena, Diags, - Tokens); -} - -void anchorForGetMainExecutable() {} - -/// Populates the \c ParsedRegions parameter with the regions that are expected -/// to get reparsed -bool parseReparseRegionArguments(ByteBasedSourceRangeSet &ParsedRegions, - SourceManager &SourceMgr, unsigned BufferID) { - llvm::Regex MatchRegex("([0-9]+):([0-9]+)-([0-9]+):([0-9]+)"); - // Parse the source edits - for (auto ReparsePattern : options::ReparseRegions) { - SmallVector Matches; - if (!MatchRegex.match(ReparsePattern, &Matches)) { - llvm::errs() << "Invalid reparse region pattern: " << ReparsePattern - << '\n'; - return false; - } - int ReparseStartLine, ReparseStartColumn, ReparseEndLine, ReparseEndColumn; - if (Matches[1].getAsInteger(10, ReparseStartLine)) { - llvm::errs() << "Could not parse reparse region start line as integer: " - << ReparseStartLine << '\n'; - return false; - } - if (Matches[2].getAsInteger(10, ReparseStartColumn)) { - llvm::errs() << "Could not parse reparse region start column as integer: " - << ReparseStartColumn << '\n'; - return false; - } - if (Matches[3].getAsInteger(10, ReparseEndLine)) { - llvm::errs() << "Could not parse reparse region end line as integer: " - << ReparseEndLine << '\n'; - return false; - } - if (Matches[4].getAsInteger(10, ReparseEndColumn)) { - llvm::errs() << "Could not parse reparse region end column as integer: " - << ReparseEndColumn << '\n'; - return false; - } - - auto ReparseStartLoc = SourceMgr.getLocForLineCol( - BufferID, ReparseStartLine, ReparseStartColumn); - auto ReparseEndLoc = - SourceMgr.getLocForLineCol(BufferID, ReparseEndLine, ReparseEndColumn); - auto ReparseStartOffset = - SourceMgr.getLocOffsetInBuffer(ReparseStartLoc, BufferID); - auto ReparseEndOffset = - SourceMgr.getLocOffsetInBuffer(ReparseEndLoc, BufferID); - ParsedRegions.addRange({ReparseStartOffset, ReparseEndOffset}); - } - return true; -} - -bool parseIncrementalEditArguments(SyntaxParsingCache *Cache, - StringRef OldFileName) { - // Get a source manager for the old file - InputFile OldFile = InputFile(OldFileName, true); - auto OldFileBufferOrError = llvm::MemoryBuffer::getFileOrSTDIN(OldFileName); - if (!OldFileBufferOrError) { - llvm::errs() << "Unable to open old source file"; - return false; - } - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addNewSourceBuffer(std::move(OldFileBufferOrError.get())); - - llvm::Regex MatchRegex("([0-9]+):([0-9]+)-([0-9]+):([0-9]+)=(.*)"); - // Parse the source edits - for (auto EditPattern : options::IncrementalEdits) { - SmallVector Matches; - if (!MatchRegex.match(EditPattern, &Matches)) { - llvm::errs() << "Invalid edit pattern: " << EditPattern << '\n'; - return false; - } - int EditStartLine, EditStartColumn, EditEndLine, EditEndColumn; - if (Matches[1].getAsInteger(10, EditStartLine)) { - llvm::errs() << "Could not parse edit start line as integer: " - << EditStartLine << '\n'; - return false; - } - if (Matches[2].getAsInteger(10, EditStartColumn)) { - llvm::errs() << "Could not parse edit start column as integer: " - << EditStartColumn << '\n'; - return false; - } - if (Matches[3].getAsInteger(10, EditEndLine)) { - llvm::errs() << "Could not parse edit end line as integer: " - << EditEndLine << '\n'; - return false; - } - if (Matches[4].getAsInteger(10, EditEndColumn)) { - llvm::errs() << "Could not parse edit end column as integer: " - << EditEndColumn << '\n'; - return false; - } - - auto EditStartLoc = - SourceMgr.getLocForLineCol(BufferID, EditStartLine, EditStartColumn); - auto EditEndLoc = - SourceMgr.getLocForLineCol(BufferID, EditEndLine, EditEndColumn); - auto EditStartOffset = - SourceMgr.getLocOffsetInBuffer(EditStartLoc, BufferID); - auto EditEndOffset = SourceMgr.getLocOffsetInBuffer(EditEndLoc, BufferID); - Cache->addEdit(EditStartOffset, EditEndOffset, - /*ReplacementLength=*/Matches[5].size()); - } - return true; -} - -bool useColoredOutput() { - return llvm::outs().has_colors() || options::ForceColoredOutput; -} - -void printVisualNodeReuseInformation(SourceManager &SourceMgr, - unsigned BufferID, - SyntaxParsingCache *Cache, - const SourceFileSyntax &NewSyntaxTree) { - unsigned CurrentOffset = 0; - auto SourceText = SourceMgr.getEntireTextForBuffer(BufferID); - if (useColoredOutput()) { - llvm::outs().changeColor(llvm::buffer_ostream::Colors::GREEN); - } - auto PrintReparsedRegion = [](StringRef SourceText, unsigned ReparseStart, - unsigned ReparseEnd) { - if (ReparseEnd != ReparseStart) { - if (useColoredOutput()) { - llvm::outs().changeColor(llvm::buffer_ostream::Colors::RED); - } else { - llvm::outs() << ""; - } - - llvm::outs() << SourceText.substr(ReparseStart, - ReparseEnd - ReparseStart); - - if (useColoredOutput()) { - llvm::outs().changeColor(llvm::buffer_ostream::Colors::GREEN); - } else { - llvm::outs() << ""; - } - } - }; - - for (auto ReuseRange : Cache->getReusedRegions(NewSyntaxTree)) { - auto StartOffset = ReuseRange.Start.getOffset(); - auto EndOffset = ReuseRange.End.getOffset(); - // Print region that was not reused - PrintReparsedRegion(SourceText, CurrentOffset, StartOffset); - - llvm::outs() << SourceText.substr(StartOffset, EndOffset - StartOffset); - CurrentOffset = EndOffset; - } - PrintReparsedRegion(SourceText, CurrentOffset, SourceText.size()); - if (useColoredOutput()) - llvm::outs().resetColor(); - - llvm::outs() << '\n'; -} - -void saveReuseLog(SyntaxParsingCache *Cache, - const SourceFileSyntax &NewSyntaxTree) { - std::error_code ErrorCode; - llvm::raw_fd_ostream ReuseLog(options::IncrementalReuseLog, ErrorCode, - llvm::sys::fs::FA_Read | - llvm::sys::fs::FA_Write); - assert(!ErrorCode && "Unable to open incremental usage log"); - - for (auto ReuseRange : Cache->getReusedRegions(NewSyntaxTree)) { - ReuseLog << "Reused " << ReuseRange.Start << " to " << ReuseRange.End - << '\n'; - ReuseLog << '\n'; - } -} - -bool verifyReusedRegions(ByteBasedSourceRangeSet ExpectedReparseRegions, - SyntaxParsingCache *SyntaxCache, - SourceManager &SourceMgr, unsigned BufferID, - SourceFile *SF) { - // We always expect the EOF token to be reparsed. Don't complain about it. - auto Eof = SF->getSyntaxRoot().getChild(SourceFileSyntax::Cursor::EOFToken); - auto EofNodeStart = Eof->getAbsolutePositionBeforeLeadingTrivia().getOffset(); - if (ExpectedReparseRegions.Ranges.back().End >= EofNodeStart) { - // If the last expected reparse region already covers part of the eof - // leading trivia, extended it - auto LastRange = ExpectedReparseRegions.Ranges.back(); - ExpectedReparseRegions.Ranges.pop_back(); - ByteBasedSourceRange ExtendedRange(LastRange.Start, - EofNodeStart + Eof->getTextLength()); - ExpectedReparseRegions.addRange(ExtendedRange); - } else { - ByteBasedSourceRange EofRange(EofNodeStart, - EofNodeStart + Eof->getTextLength()); - ExpectedReparseRegions.addRange(EofRange); - } - - auto FileLength = SourceMgr.getRangeForBuffer(BufferID).getByteLength(); - - // Compute the repared regions by inverting the reused regions - auto ReusedRanges = ByteBasedSourceRangeSet( - SyntaxCache->getReusedRegions(SF->getSyntaxRoot())); - auto ReparsedRegions = ReusedRanges.invert(FileLength); - - // Same for expected reuse regions - auto ExpectedReuseRegions = ExpectedReparseRegions.invert(FileLength); - - // Intersect the reparsed regions with the expected reuse regions to get - // regions that should not have been reparsed - auto UnexpectedReparseRegions = - ReparsedRegions.intersect(ExpectedReuseRegions); - - bool NoUnexpectedParse = true; - - for (auto ReparseRegion : UnexpectedReparseRegions.Ranges) { - auto ReparseRange = ReparseRegion.toCharSourceRange(SourceMgr, BufferID); - - // To improve the ergonomics when writing tests we do not want to complain - // about reparsed whitespaces. - auto RangeStr = ReparseRange.str(); - llvm::Regex WhitespaceOnlyRegex("^[ \t\r\n]*$"); - if (WhitespaceOnlyRegex.match(RangeStr)) { - continue; - } - - NoUnexpectedParse = false; - - llvm::errs() << "\nERROR: Unexpectedly reparsed following region:\n"; - ReparseRange.print(llvm::errs(), SourceMgr); - } - return NoUnexpectedParse; -} - -struct ParseInfo { - SourceFile *SF; - SyntaxParsingCache *SyntaxCache; - std::string Diags; - /// Whether parsing produced any diagnostics with severity error. - bool DidHaveError; -}; - -/// Parse the given input file (incrementally if an old syntax tree was -/// provided) and call the action specific callback with the new syntax tree -int parseFile( - const char *MainExecutablePath, const StringRef InputFileName, - llvm::function_ref ActionSpecificCallback) { - - // The cache needs to be a heap allocated pointer since we construct it inside - // an if block but need to keep it alive until the end of the function. - SyntaxParsingCache *SyntaxCache = nullptr; - SWIFT_DEFER { delete SyntaxCache; }; - // We also need to hold on to the Deserializer and buffer since they keep - // ownership of strings that are referenced from the old syntax tree - swift::json::SyntaxDeserializer *Deserializer = nullptr; - SWIFT_DEFER { delete Deserializer; }; - - auto Buffer = llvm::MemoryBuffer::getFile(options::OldSyntaxTreeFilename); - // Deserialise the old syntax tree - if (!options::OldSyntaxTreeFilename.empty()) { - Deserializer = new swift::json::SyntaxDeserializer( - llvm::MemoryBufferRef(*(Buffer.get()))); - auto OldSyntaxTree = Deserializer->getSourceFileSyntax(); - if (!OldSyntaxTree.hasValue()) { - llvm::errs() << "Could not deserialise old syntax tree."; - return EXIT_FAILURE; - } - SyntaxCache = new SyntaxParsingCache(OldSyntaxTree.getValue()); - - if (options::OldSourceFilename.empty()) { - llvm::errs() << "The old syntax file must be provided to translate " - "line:column edits to byte offsets"; - return EXIT_FAILURE; - } - if (!parseIncrementalEditArguments(SyntaxCache, - options::OldSourceFilename)) { - return EXIT_FAILURE; - } - } - - // Set up the compiler invocation - CompilerInvocation Invocation; - Invocation.getLangOptions().BuildSyntaxTree = true; - Invocation.getLangOptions().ParseForSyntaxTreeOnly = true; - Invocation.getLangOptions().VerifySyntaxTree = options::VerifySyntaxTree; - Invocation.getLangOptions().DisablePoundIfEvaluation = true; - Invocation.getLangOptions().EnableExperimentalStringProcessing = true; - Invocation.getLangOptions().Features.insert(Feature::BareSlashRegexLiterals); - - Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(InputFileName); - - Invocation.setMainExecutablePath( - llvm::sys::fs::getMainExecutable(MainExecutablePath, - reinterpret_cast(&anchorForGetMainExecutable))); - Invocation.setMainFileSyntaxParsingCache(SyntaxCache); - Invocation.setModuleName("Test"); - - std::string DiagsString; - llvm::raw_string_ostream DiagOS(DiagsString); - PrintingDiagnosticConsumer DiagConsumer(DiagOS); - CompilerInstance Instance; - Instance.addDiagnosticConsumer(&DiagConsumer); - std::string InstanceSetupError; - if (Instance.setup(Invocation, InstanceSetupError)) { - llvm::errs() << InstanceSetupError << '\n'; - return EXIT_FAILURE; - } - - // Parse incremental edit arguments - auto BufferIDs = Instance.getInputBufferIDs(); - assert(BufferIDs.size() == 1 && "Only expecting to process one source file"); - unsigned BufferID = BufferIDs.front(); - - SourceFile *SF = nullptr; - for (auto Unit : Instance.getMainModule()->getFiles()) { - SF = dyn_cast(Unit); - if (SF != nullptr) { - break; - } - } - assert(SF && "No source file"); - - // Force parsing to populate the syntax cache. - (void)SF->getSyntaxRoot(); - - // In case the action specific callback succeeds, we output this error code - int InternalExitCode = EXIT_SUCCESS; - - // If we have a syntax cache, output reuse information if requested - if (SyntaxCache) { - if (options::PrintVisualReuseInfo) { - printVisualNodeReuseInformation(Instance.getSourceMgr(), BufferID, - SyntaxCache, SF->getSyntaxRoot()); - } - if (!options::IncrementalReuseLog.empty()) { - saveReuseLog(SyntaxCache, SF->getSyntaxRoot()); - } - ByteBasedSourceRangeSet ExpectedReparseRegions; - - if (parseReparseRegionArguments(ExpectedReparseRegions, - Instance.getSourceMgr(), BufferID)) { - if (!ExpectedReparseRegions.Ranges.empty()) { - if (!verifyReusedRegions(ExpectedReparseRegions, SyntaxCache, - Instance.getSourceMgr(), BufferID, SF)) { - InternalExitCode = EXIT_FAILURE; - } - } - } - } - - int ActionSpecificExitCode = ActionSpecificCallback( - {SF, SyntaxCache, DiagOS.str(), DiagConsumer.didErrorOccur()}); - if (ActionSpecificExitCode != EXIT_SUCCESS) { - return ActionSpecificExitCode; - } else { - return InternalExitCode; - } -} - -static bool printDiags(ParseInfo info) { - if (!options::DiagsOutputFilename.empty()) { - std::error_code errorCode; - llvm::raw_fd_ostream os(options::DiagsOutputFilename, errorCode, - llvm::sys::fs::OF_None); - if (errorCode) { - llvm::errs() << "error opening file '" << options::DiagsOutputFilename - << "': " << errorCode.message() << '\n'; - return false; - } - if (!info.Diags.empty()) - os << info.Diags << '\n'; - } else { - if (!info.Diags.empty()) - llvm::errs() << info.Diags << '\n'; - } - return true; -} - -int doFullLexRoundTrip(const StringRef InputFilename) { - LangOptions LangOpts; - SourceManager SourceMgr; - DiagnosticEngine Diags(SourceMgr); - PrintingDiagnosticConsumer DiagPrinter; - Diags.addConsumer(DiagPrinter); - - unsigned int BufferID; - RC Arena = SyntaxArena::make(); - std::vector< - std::pair> - Tokens; - if (getTokensFromFile(InputFilename, LangOpts, SourceMgr, Arena, Diags, - BufferID, Tokens) == EXIT_FAILURE) { - return EXIT_FAILURE; - } - - for (auto TokAndPos : Tokens) { - TokAndPos.first->print(llvm::outs(), {}); - } - - return EXIT_SUCCESS; -} - -int doDumpRawTokenSyntax(const StringRef InputFile) { - LangOptions LangOpts; - SourceManager SourceMgr; - DiagnosticEngine Diags(SourceMgr); - PrintingDiagnosticConsumer DiagPrinter; - Diags.addConsumer(DiagPrinter); - - unsigned int BufferID; - RC Arena = SyntaxArena::make(); - std::vector< - std::pair> - Tokens; - if (getTokensFromFile(InputFile, LangOpts, SourceMgr, Arena, Diags, BufferID, - Tokens) == EXIT_FAILURE) { - return EXIT_FAILURE; - } - - for (auto TokAndPos : Tokens) { - SourceLoc Loc = - SourceMgr.getLocForOffset(BufferID, TokAndPos.second.getOffset()); - unsigned Line, Column; - std::tie(Line, Column) = SourceMgr.getLineAndColumnInBuffer(Loc); - llvm::outs() << Line << ":" << Column << "\n"; - TokAndPos.first->dump(llvm::outs()); - llvm::outs() << "\n"; - } - - return EXIT_SUCCESS; -} - -int doFullParseRoundTrip(const char *MainExecutablePath, - const StringRef InputFile) { - return parseFile(MainExecutablePath, InputFile, - [](ParseInfo info) -> int { - info.SF->getSyntaxRoot().print(llvm::outs(), {}); - if (options::FailOnParseError && info.DidHaveError) { - return EXIT_FAILURE; - } - return EXIT_SUCCESS; - }); -} - -int doSerializeRawTree(const char *MainExecutablePath, - const StringRef InputFile) { - return parseFile(MainExecutablePath, InputFile, - [](ParseInfo info) -> int { - auto SF = info.SF; - auto Root = SF->getSyntaxRoot().getRaw(); - - if (!options::OutputFilename.empty()) { - std::error_code errorCode; - llvm::raw_fd_ostream os(options::OutputFilename, errorCode, - llvm::sys::fs::OF_None); - assert(!errorCode && "Couldn't open output file"); - swift::json::Output out(os); - out << *Root; - os << "\n"; - } else { - swift::json::Output out(llvm::outs()); - out << *Root; - llvm::outs() << "\n"; - } - - if (!printDiags(info)) { - return EXIT_FAILURE; - } - - if (options::FailOnParseError && info.DidHaveError) { - return EXIT_FAILURE; - } - return EXIT_SUCCESS; - }); -} - -int doDeserializeRawTree(const char *MainExecutablePath, - const StringRef InputFile, - const StringRef OutputFileName) { - - auto Buffer = llvm::MemoryBuffer::getFile(InputFile); - std::error_code errorCode; - auto os = std::make_unique( - OutputFileName, errorCode, llvm::sys::fs::OF_None); - swift::json::SyntaxDeserializer deserializer(llvm::MemoryBufferRef(*(Buffer.get()))); - deserializer.getSourceFileSyntax()->print(*os); - - return EXIT_SUCCESS; -} - -int doParseOnly(const char *MainExecutablePath, const StringRef InputFile) { - return parseFile(MainExecutablePath, InputFile, [](ParseInfo info) { - if (!printDiags(info)) { - return EXIT_FAILURE; - } - if (options::FailOnParseError && info.DidHaveError) { - return EXIT_FAILURE; - } - return info.SF ? EXIT_SUCCESS : EXIT_FAILURE; - }); -} - -int dumpParserGen(const char *MainExecutablePath, const StringRef InputFile) { - return parseFile(MainExecutablePath, InputFile, - [](ParseInfo info) { - SyntaxPrintOptions Opts; - Opts.PrintSyntaxKind = options::PrintNodeKind; - Opts.Visual = options::Visual; - Opts.PrintTrivialNodeKind = options::PrintTrivialNodeKind; - info.SF->getSyntaxRoot().print(llvm::outs(), Opts); - if (!printDiags(info)) { - return EXIT_FAILURE; - } - if (options::FailOnParseError && info.DidHaveError) { - return EXIT_FAILURE; - } - return EXIT_SUCCESS; - }); -} - -int dumpEOFSourceLoc(const char *MainExecutablePath, - const StringRef InputFile) { - return parseFile(MainExecutablePath, InputFile, - [](ParseInfo info) -> int { - auto SF = info.SF; - auto BufferId = *SF->getBufferID(); - auto Root = SF->getSyntaxRoot(); - auto AbPos = Root.getEOFToken().getAbsolutePosition(); - - SourceManager &SourceMgr = SF->getASTContext().SourceMgr; - auto StartLoc = SourceMgr.getLocForBufferStart(BufferId); - auto EndLoc = SourceMgr.getLocForOffset(BufferId, AbPos.getOffset()); - - if (!printDiags(info)) { - return EXIT_FAILURE; - } - - // To ensure the correctness of position when translated to line & column - // pair. - if (SourceMgr.getLocOffsetInBuffer(EndLoc, BufferId) != AbPos.getOffset()) { - llvm::outs() << "locations should be identical"; - return EXIT_FAILURE; - } - llvm::outs() << CharSourceRange(SourceMgr, StartLoc, EndLoc).str(); - if (options::FailOnParseError && info.DidHaveError) { - return EXIT_FAILURE; - } - return EXIT_SUCCESS; - }); -} -}// end of anonymous namespace - -static int invokeCommand(const char *MainExecutablePath, - const StringRef InputSourceFilename) { - int ExitCode = EXIT_SUCCESS; - - switch (options::Action) { - case ActionType::DumpRawTokenSyntax: - ExitCode = doDumpRawTokenSyntax(InputSourceFilename); - break; - case ActionType::FullLexRoundTrip: - ExitCode = doFullLexRoundTrip(InputSourceFilename); - break; - case ActionType::FullParseRoundTrip: - ExitCode = doFullParseRoundTrip(MainExecutablePath, InputSourceFilename); - break; - case ActionType::SerializeRawTree: - ExitCode = doSerializeRawTree(MainExecutablePath, InputSourceFilename); - break; - case ActionType::DeserializeRawTree: - ExitCode = doDeserializeRawTree(MainExecutablePath, InputSourceFilename, - options::OutputFilename); - break; - case ActionType::ParseOnly: - ExitCode = doParseOnly(MainExecutablePath, InputSourceFilename); - break; - case ActionType::ParserGen: - ExitCode = dumpParserGen(MainExecutablePath, InputSourceFilename); - break; - case ActionType::EOFPos: - ExitCode = dumpEOFSourceLoc(MainExecutablePath, InputSourceFilename); - break; - case ActionType::None: - llvm::errs() << "an action is required\n"; - llvm::cl::PrintHelpMessage(); - ExitCode = EXIT_FAILURE; - break; - } - - return ExitCode; -} - -int main(int argc, char *argv[]) { - PROGRAM_START(argc, argv); - initializeSwiftParseModules(); - llvm::cl::ParseCommandLineOptions(argc, argv, "Swift Syntax Test\n"); - - int ExitCode = EXIT_SUCCESS; - - if (options::InputSourceFilename.empty() && - options::InputSourceDirectory.empty()) { - llvm::errs() << "input source file is required\n"; - ExitCode = EXIT_FAILURE; - } - - if (!options::InputSourceFilename.empty() && - !options::InputSourceDirectory.empty()) { - llvm::errs() << "input-source-filename and input-source-directory cannot " - "be used together\n\n"; - ExitCode = EXIT_FAILURE; - } - - if (options::Action == ActionType::None) { - llvm::errs() << "an action is required\n"; - ExitCode = EXIT_FAILURE; - } - - if (ExitCode == EXIT_FAILURE) { - llvm::cl::PrintHelpMessage(); - return ExitCode; - } - - if (!options::InputSourceFilename.empty()) { - ExitCode = invokeCommand(argv[0], options::InputSourceFilename); - } else { - assert(!options::InputSourceDirectory.empty()); - std::error_code errorCode; - llvm::sys::fs::recursive_directory_iterator DI(options::InputSourceDirectory, errorCode); - llvm::sys::fs::recursive_directory_iterator endIterator; - for (; DI != endIterator; DI.increment(errorCode)) { - auto entry = *DI; - auto path = entry.path(); - if (!llvm::sys::fs::is_directory(path) && - StringRef(path).endswith(".swift")) { - ExitCode = invokeCommand(argv[0], path); - } - } - } - - return ExitCode; -} diff --git a/unittests/AST/TestContext.cpp b/unittests/AST/TestContext.cpp index 8f4cf9e237586..0d047334fa0ae 100644 --- a/unittests/AST/TestContext.cpp +++ b/unittests/AST/TestContext.cpp @@ -57,7 +57,7 @@ TestContext::TestContext(ShouldDeclareOptionalTypes optionals) auto result = SourceFileParsingResult{ Ctx.AllocateCopy(optionalTypes), /*tokens*/ None, - /*interfaceHash*/ None, /*syntaxRoot*/ None}; + /*interfaceHash*/ None}; Ctx.evaluator.cacheOutput(ParseSourceFileRequest{FileForLookups}, std::move(result)); diff --git a/unittests/Parse/CMakeLists.txt b/unittests/Parse/CMakeLists.txt index b0383dfa72e0b..8c7888ebe4fd0 100644 --- a/unittests/Parse/CMakeLists.txt +++ b/unittests/Parse/CMakeLists.txt @@ -1,8 +1,6 @@ add_swift_unittest(SwiftParseTests BuildConfigTests.cpp LexerTests.cpp - LexerTriviaTests.cpp - SyntaxParsingCacheTests.cpp TokenizerTests.cpp ) diff --git a/unittests/Parse/LexerTests.cpp b/unittests/Parse/LexerTests.cpp index 202119e85fcb9..f4fd02038f4c6 100644 --- a/unittests/Parse/LexerTests.cpp +++ b/unittests/Parse/LexerTests.cpp @@ -4,7 +4,6 @@ #include "swift/Basic/LangOptions.h" #include "swift/Basic/SourceManager.h" #include "swift/Parse/Lexer.h" -#include "swift/Syntax/Trivia.h" #include "swift/Subsystems.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Process.h" @@ -19,7 +18,6 @@ using namespace swift; using namespace llvm; -using syntax::TriviaKind; // The test fixture. class LexerTest : public ::testing::Test { @@ -281,28 +279,22 @@ TEST_F(LexerTest, BOMNoCommentNoTrivia) { unsigned BufferID = SourceMgr.addMemBufferCopy(StringRef(Source)); Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::None, - TriviaRetentionMode::WithoutTrivia); + HashbangMode::Disallowed, CommentRetentionMode::None); Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); + + L.lex(Tok); ASSERT_EQ(tok::identifier, Tok.getKind()); ASSERT_EQ("aaa", Tok.getText()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getCommentRange().getStart()); ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - L.lex(Tok, LeadingTrivia, TrailingTrivia); + L.lex(Tok); ASSERT_EQ(tok::eof, Tok.getKind()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getCommentRange().getStart()); ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); } TEST_F(LexerTest, BOMTokenCommentNoTrivia) { @@ -313,55 +305,43 @@ TEST_F(LexerTest, BOMTokenCommentNoTrivia) { unsigned BufferID = SourceMgr.addMemBufferCopy(StringRef(Source)); Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens, - TriviaRetentionMode::WithoutTrivia); + HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens); Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); + + L.lex(Tok); ASSERT_EQ(tok::comment, Tok.getKind()); ASSERT_EQ("// comment\n", Tok.getText()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 3), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 3), Tok.getCommentRange().getStart()); ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - L.lex(Tok, LeadingTrivia, TrailingTrivia); + L.lex(Tok); ASSERT_EQ(tok::identifier, Tok.getKind()); ASSERT_EQ("aaa", Tok.getText()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getCommentRange().getStart()); ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - L.lex(Tok, LeadingTrivia, TrailingTrivia); + L.lex(Tok); ASSERT_EQ(tok::comment, Tok.getKind()); ASSERT_EQ("//xx \n", Tok.getText()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 18), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 18), Tok.getCommentRange().getStart()); ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - L.lex(Tok, LeadingTrivia, TrailingTrivia); + L.lex(Tok); ASSERT_EQ(tok::comment, Tok.getKind()); ASSERT_EQ("/* x */", Tok.getText()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 24), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 24), Tok.getCommentRange().getStart()); ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - L.lex(Tok, LeadingTrivia, TrailingTrivia); + L.lex(Tok); ASSERT_EQ(tok::eof, Tok.getKind()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getCommentRange().getStart()); ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); } TEST_F(LexerTest, BOMAttachCommentNoTrivia) { @@ -372,128 +352,22 @@ TEST_F(LexerTest, BOMAttachCommentNoTrivia) { unsigned BufferID = SourceMgr.addMemBufferCopy(StringRef(Source)); Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithoutTrivia); + HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken); Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getLoc()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 3), Tok.getCommentRange().getStart()); - ASSERT_EQ(10u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - ASSERT_EQ(tok::eof, Tok.getKind()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getLoc()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 18), Tok.getCommentRange().getStart()); - ASSERT_EQ(13u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ(StringRef(), LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); -} -TEST_F(LexerTest, BOMNoCommentTrivia) { - const char *Source = "\xEF\xBB\xBF" "// comment\naaa //xx \n/* x */"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(StringRef(Source)); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::None, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getLoc()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getCommentRange().getStart()); - ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ("\xEF\xBB\xBF" "// comment" "\n", LeadingTrivia); - ASSERT_EQ(" ", TrailingTrivia); - - ParsedTrivia LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - ParsedTrivia TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::UnexpectedText, strlen("\xEF\xBB\xBF")), - ParsedTriviaPiece(TriviaKind::LineComment, strlen("// comment")), - ParsedTriviaPiece(TriviaKind::Newline, 1) - }}), LeadingTriviaPieces); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::Space, 1) - }}), TrailingTriviaPieces); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - ASSERT_EQ(tok::eof, Tok.getKind()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getLoc()); - ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getCommentRange().getStart()); - ASSERT_EQ(0u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ("//xx \n/* x */", LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::LineComment, strlen("//xx ")), - ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::BlockComment, strlen("/* x */")) - }}), LeadingTriviaPieces); -} - -TEST_F(LexerTest, BOMAttachCommentTrivia) { - const char *Source = "\xEF\xBB\xBF" "// comment\naaa //xx \n/* x */"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(StringRef(Source)); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); + L.lex(Tok); ASSERT_EQ(tok::identifier, Tok.getKind()); ASSERT_EQ("aaa", Tok.getText()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 14), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 3), Tok.getCommentRange().getStart()); ASSERT_EQ(10u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ("\xEF\xBB\xBF" "// comment" "\n", LeadingTrivia); - ASSERT_EQ(" ", TrailingTrivia); - - ParsedTrivia LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - ParsedTrivia TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::UnexpectedText, strlen("\xEF\xBB\xBF")), - ParsedTriviaPiece(TriviaKind::LineComment, strlen("// comment")), - ParsedTriviaPiece(TriviaKind::Newline, 1) - }}), LeadingTriviaPieces); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::Space, 1) - }}), TrailingTriviaPieces); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); + + L.lex(Tok); ASSERT_EQ(tok::eof, Tok.getKind()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 31), Tok.getLoc()); ASSERT_EQ(SourceMgr.getLocForOffset(BufferID, 18), Tok.getCommentRange().getStart()); ASSERT_EQ(13u, Tok.getCommentRange().getByteLength()); - ASSERT_EQ("//xx \n/* x */", LeadingTrivia); - ASSERT_EQ(StringRef(), TrailingTrivia); - - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::LineComment, strlen("//xx ")), - ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::BlockComment, strlen("/* x */")) - }}), LeadingTriviaPieces); } TEST_F(LexerTest, RestoreBasic) { @@ -802,7 +676,7 @@ TEST_F(LexerTest, DiagnoseEmbeddedNul) { Lexer L(LangOpts, SourceMgr, BufferID, &Diags, LexerMode::Swift, HashbangMode::Disallowed, - CommentRetentionMode::None, TriviaRetentionMode::WithTrivia); + CommentRetentionMode::None); Token Tok; L.lex(Tok); @@ -827,7 +701,7 @@ TEST_F(LexerTest, DiagnoseEmbeddedNulOffset) { Lexer L(LangOpts, SourceMgr, BufferID, &Diags, LexerMode::Swift, HashbangMode::Disallowed, - CommentRetentionMode::None, TriviaRetentionMode::WithTrivia, + CommentRetentionMode::None, /*Offset=*/5, /*EndOffset=*/SourceLen); ASSERT_FALSE(containsPrefix( diff --git a/unittests/Parse/LexerTriviaTests.cpp b/unittests/Parse/LexerTriviaTests.cpp deleted file mode 100644 index 8a7763fd53755..0000000000000 --- a/unittests/Parse/LexerTriviaTests.cpp +++ /dev/null @@ -1,321 +0,0 @@ -#include "swift/Basic/LangOptions.h" -#include "swift/Basic/SourceManager.h" -#include "swift/Parse/Lexer.h" -#include "swift/Syntax/Trivia.h" -#include "swift/Subsystems.h" -#include "llvm/Support/MemoryBuffer.h" -#include "gtest/gtest.h" - -using namespace swift; -using namespace llvm; -using syntax::TriviaKind; - -class LexerTriviaTest : public ::testing::Test {}; - -TEST_F(LexerTriviaTest, RestoreWithTrivia) { - using namespace swift::syntax; - StringRef SourceStr = "aaa \n bbb /*C*/ccc"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(SourceStr); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - ParsedTrivia LeadingTriviaPieces, TrailingTriviaPieces; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, ""); - ASSERT_EQ(TrailingTrivia, " "); - ASSERT_EQ(LeadingTriviaPieces, ParsedTrivia()); - ASSERT_EQ(TrailingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece(TriviaKind::Space, 1)}})); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("bbb", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "\n "); - ASSERT_EQ(TrailingTrivia, " "); - ASSERT_EQ(LeadingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::Space, 1)}})); - ASSERT_EQ(TrailingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece(TriviaKind::Space, 1)}})); - - LexerState S = L.getStateForBeginningOfToken(Tok, LeadingTrivia); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("ccc", Tok.getText()); - ASSERT_FALSE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "/*C*/"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ(LeadingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece( - TriviaKind::BlockComment, strlen("/*C*/"))}})); - ASSERT_EQ(TrailingTriviaPieces, ParsedTrivia()); - - L.restoreState(S); - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("bbb", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "\n "); - ASSERT_EQ(TrailingTrivia, " "); - ASSERT_EQ(LeadingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::Space, 1)}})); - ASSERT_EQ(TrailingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece(TriviaKind::Space, 1)}})); -} - -TEST_F(LexerTriviaTest, TriviaHashbang) { - StringRef SourceStr = "#!/bin/swift\naaa"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(SourceStr); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - ParsedTrivia LeadingTriviaPieces, TrailingTriviaPieces; - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "#!/bin/swift\n"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ(LeadingTriviaPieces, (ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::Shebang, strlen("#!/bin/swift")), - ParsedTriviaPiece(TriviaKind::Newline, 1)}})); - ASSERT_EQ(TrailingTriviaPieces, ParsedTrivia()); -} - -TEST_F(LexerTriviaTest, TriviaHashbangAfterBOM) { - StringRef SourceStr = "\xEF\xBB\xBF" "#!/bin/swift\naaa"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(SourceStr); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - ParsedTrivia LeadingTriviaPieces, TrailingTriviaPieces; - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - - ASSERT_EQ(LeadingTrivia, "\xEF\xBB\xBF" "#!/bin/swift\n"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ(LeadingTriviaPieces, (ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::UnexpectedText, strlen("\xEF\xBB\xBF")), - ParsedTriviaPiece(TriviaKind::Shebang, strlen("#!/bin/swift")), - ParsedTriviaPiece(TriviaKind::Newline, 1)}})); - ASSERT_EQ(TrailingTriviaPieces, ParsedTrivia()); -} - -TEST_F(LexerTriviaTest, TriviaConflictMarker) { - using namespace swift::syntax; - StringRef SourceStr = - "aaa\n" - "<<<<<<< HEAD:conflict_markers.swift\n" - "new\n" - "=======\n" - "old\n" - ">>>>>>> 18844bc65229786b96b89a9fc7739c0f:conflict_markers.swift\n" - "bbb\n"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(SourceStr); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - ParsedTrivia LeadingTriviaPieces, TrailingTriviaPieces; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("bbb", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - StringRef expectedTrivia = - "<<<<<<< HEAD:conflict_markers.swift\n" - "new\n" - "=======\n" - "old\n" - ">>>>>>> 18844bc65229786b96b89a9fc7739c0f:conflict_markers.swift"; - ASSERT_EQ(LeadingTrivia, ("\n" + expectedTrivia + "\n").str()); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ(LeadingTriviaPieces, (ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::UnexpectedText, expectedTrivia.size()), - ParsedTriviaPiece(TriviaKind::Newline, 1)}})); - ASSERT_EQ(TrailingTriviaPieces, ParsedTrivia()); -} - -TEST_F(LexerTriviaTest, TriviaCarriageReturn) { - using namespace swift::syntax; - StringRef SourceStr = "aaa\r\rbbb\r"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(SourceStr); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - ParsedTrivia LeadingTriviaPieces, TrailingTriviaPieces; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, ""); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ(LeadingTriviaPieces, ParsedTrivia()); - ASSERT_EQ(TrailingTriviaPieces, ParsedTrivia()); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("bbb", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "\r\r"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ(LeadingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece(TriviaKind::CarriageReturn, 2)}})); - ASSERT_EQ(TrailingTriviaPieces, ParsedTrivia()); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::eof, Tok.getKind()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "\r"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ(LeadingTriviaPieces, - (ParsedTrivia{{ParsedTriviaPiece(TriviaKind::CarriageReturn, 1)}})); - ASSERT_EQ(TrailingTriviaPieces, ParsedTrivia()); -} - -TEST_F(LexerTriviaTest, TriviaNewLines) { - using namespace swift::syntax; - StringRef SourceStr = "\n\r\r\n\r\r\r\n\r\n\n\n" - "aaa" - "\n\r\r\n\r\r\r\n\r\n\n\n" - "bbb" - "\n\r\r\n\r\r\r\n\r\n\n\n"; - - LangOptions LangOpts; - SourceManager SourceMgr; - unsigned BufferID = SourceMgr.addMemBufferCopy(SourceStr); - - Lexer L(LangOpts, SourceMgr, BufferID, /*Diags=*/nullptr, LexerMode::Swift, - HashbangMode::Disallowed, CommentRetentionMode::AttachToNextToken, - TriviaRetentionMode::WithTrivia); - - Token Tok; - StringRef LeadingTrivia, TrailingTrivia; - ParsedTrivia LeadingTriviaPieces, TrailingTriviaPieces; - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("aaa", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "\n\r\r\n\r\r\r\n\r\n\n\n"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::CarriageReturn, 1), - ParsedTriviaPiece(TriviaKind::CarriageReturnLineFeed, 2), - ParsedTriviaPiece(TriviaKind::CarriageReturn, 2), - ParsedTriviaPiece(TriviaKind::CarriageReturnLineFeed, 4), - ParsedTriviaPiece(TriviaKind::Newline, 2), - }}), LeadingTriviaPieces); - ASSERT_EQ(ParsedTrivia(), TrailingTriviaPieces); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::identifier, Tok.getKind()); - ASSERT_EQ("bbb", Tok.getText()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "\n\r\r\n\r\r\r\n\r\n\n\n"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::CarriageReturn, 1), - ParsedTriviaPiece(TriviaKind::CarriageReturnLineFeed, 2), - ParsedTriviaPiece(TriviaKind::CarriageReturn, 2), - ParsedTriviaPiece(TriviaKind::CarriageReturnLineFeed, 4), - ParsedTriviaPiece(TriviaKind::Newline, 2), - }}), LeadingTriviaPieces); - ASSERT_EQ(ParsedTrivia(), TrailingTriviaPieces); - - L.lex(Tok, LeadingTrivia, TrailingTrivia); - LeadingTriviaPieces = TriviaLexer::lexTrivia(LeadingTrivia); - TrailingTriviaPieces = TriviaLexer::lexTrivia(TrailingTrivia); - ASSERT_EQ(tok::eof, Tok.getKind()); - ASSERT_TRUE(Tok.isAtStartOfLine()); - ASSERT_EQ(LeadingTrivia, "\n\r\r\n\r\r\r\n\r\n\n\n"); - ASSERT_EQ(TrailingTrivia, ""); - ASSERT_EQ((ParsedTrivia{{ - ParsedTriviaPiece(TriviaKind::Newline, 1), - ParsedTriviaPiece(TriviaKind::CarriageReturn, 1), - ParsedTriviaPiece(TriviaKind::CarriageReturnLineFeed, 2), - ParsedTriviaPiece(TriviaKind::CarriageReturn, 2), - ParsedTriviaPiece(TriviaKind::CarriageReturnLineFeed, 4), - ParsedTriviaPiece(TriviaKind::Newline, 2), - }}), LeadingTriviaPieces); - ASSERT_EQ(ParsedTrivia(), TrailingTriviaPieces); -} diff --git a/unittests/Parse/SyntaxParsingCacheTests.cpp b/unittests/Parse/SyntaxParsingCacheTests.cpp deleted file mode 100644 index 78173fad2a413..0000000000000 --- a/unittests/Parse/SyntaxParsingCacheTests.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "swift/Parse/SyntaxParsingCache.h" -#include "gtest/gtest.h" - -#include - -using namespace swift; -using namespace llvm; - - -namespace llvm { -template -void PrintTo(const Optional &optVal, ::std::ostream *os) { - if (optVal.hasValue()) - *os << *optVal; - else - *os << "None"; -} -} // namespace llvm - -void check(ArrayRef Edits, ArrayRef> expected) { - for (size_t Pos = 0; Pos != expected.size(); ++Pos) { - Optional PrePos = - SyntaxParsingCache::translateToPreEditPosition(Pos, Edits); - EXPECT_EQ(PrePos, expected[Pos]) << "At post-edit position " << Pos; - } -} - -class TranslateToPreEditPositionTest : public ::testing::Test {}; - -TEST_F(TranslateToPreEditPositionTest, SingleEdit1) { - // Old: ab_xy - // New: c_xy - - llvm::SmallVector Edits = { - {0, 2, 1} // ab -> c - }; - - // c _ x y - check(Edits, {None, 2, 3, 4}); -} - -TEST_F(TranslateToPreEditPositionTest, SingleEdit) { - // Old: ab_xy - // New: ablah_xy - - llvm::SmallVector Edits = { - {1, 2, 4} // b -> blah - }; - - // a b l a h _ x y - check(Edits, {0, None, None, None, None, 2, 3, 4}); -} - -TEST_F(TranslateToPreEditPositionTest, SingleInsert) { - // Old: ab_xy - // New: 0123ab_xy - - llvm::SmallVector Edits = { - {0, 0, 4} // '' -> 0123 - }; - - // 0 1 2 3 a b _ x y - check(Edits, { None, None, None, None, 0, 1, 2, 3, 4}); -} - -TEST_F(TranslateToPreEditPositionTest, SingleDelete) { - // Old: ab_xyz - // New: ab_z - - llvm::SmallVector Edits = { - {3, 5, 0} // xy -> '' - }; - - // a b _ z - check(Edits, { 0, 1, 2, 5 }); -} - -TEST_F(TranslateToPreEditPositionTest, SimpleMultiEdit) { - // Old: _ab_xy - // New: _a1b2_x3y4 - - llvm::SmallVector Edits = { - {1, 2, 2}, // a -> a1 - {2, 3, 2}, // b -> b2 - {4, 5, 2}, // x -> x3 - {5, 6, 2}, // y -> y4 - }; - - // _ a 1 b 1 _ x 3 y 4 - check(Edits, {0, None, None, None, None, 3, None, None, None, None}); -} - -TEST_F(TranslateToPreEditPositionTest, ComplexMultiEdit) { - // Old: foo_bar_baz - // New: xx_edits_baz - - llvm::SmallVector Edits = { - {0, 3, 2}, // foo -> xx - {4, 7, 0}, // bar -> '' - {7, 7, 5}, // '' -> edits - }; - - // x x _ e d i t s _ b a z - check(Edits, {None, None, 3, None, None, None, None, None, 7, 8, 9, 10}); -} diff --git a/unittests/Parse/TokenizerTests.cpp b/unittests/Parse/TokenizerTests.cpp index 63619d9813ee8..aa300c778fd61 100644 --- a/unittests/Parse/TokenizerTests.cpp +++ b/unittests/Parse/TokenizerTests.cpp @@ -39,7 +39,7 @@ class TokenizerTest : public ::testing::Test { case swift::tok::X: return #X; break; #define POUND(X, Y) \ case swift::tok::pound_##X: return "pound_" #X; break; - #include "swift/Syntax/TokenKinds.def" + #include "swift/AST/TokenKinds.def" #define OTHER(X) \ case swift::tok::X: return #X; break; diff --git a/unittests/SourceKit/SwiftLang/CursorInfoTest.cpp b/unittests/SourceKit/SwiftLang/CursorInfoTest.cpp index 2002a2f002bcd..87bf19cd770b4 100644 --- a/unittests/SourceKit/SwiftLang/CursorInfoTest.cpp +++ b/unittests/SourceKit/SwiftLang/CursorInfoTest.cpp @@ -92,11 +92,6 @@ class NullEditorConsumer : public EditorConsumer { void recordFormattedText(StringRef Text) override {} void handleSourceText(StringRef Text) override {} - void handleSyntaxTree(const swift::syntax::SourceFileSyntax &SyntaxTree) override {} - - SyntaxTreeTransferMode syntaxTreeTransferMode() override { - return SyntaxTreeTransferMode::Off; - } public: bool needsSema = false; diff --git a/unittests/SourceKit/SwiftLang/EditingTest.cpp b/unittests/SourceKit/SwiftLang/EditingTest.cpp index 929ab37e04665..1e82ef42bc8ed 100644 --- a/unittests/SourceKit/SwiftLang/EditingTest.cpp +++ b/unittests/SourceKit/SwiftLang/EditingTest.cpp @@ -107,12 +107,6 @@ class TestConsumer : public EditorConsumer { } void handleSourceText(StringRef Text) override {} - void handleSyntaxTree(const swift::syntax::SourceFileSyntax &SyntaxTree) override {} - - SyntaxTreeTransferMode syntaxTreeTransferMode() override { - return SyntaxTreeTransferMode::Off; - } - }; struct DocUpdateMutexState { diff --git a/utils/gyb_sourcekit_support/UIDs.py b/utils/gyb_sourcekit_support/UIDs.py index ca0cc734fc119..c19e2e62fdc8a 100644 --- a/utils/gyb_sourcekit_support/UIDs.py +++ b/utils/gyb_sourcekit_support/UIDs.py @@ -49,10 +49,8 @@ def __init__(self, internal_name, external_name): KEY('Offset', 'key.offset'), KEY('Length', 'key.length'), KEY('SourceFile', 'key.sourcefile'), - KEY('SerializedSyntaxTree', 'key.serialized_syntax_tree'), KEY('SourceText', 'key.sourcetext'), KEY('EnableSyntaxMap', 'key.enablesyntaxmap'), - KEY('SyntaxTreeTransferMode', 'key.syntaxtreetransfermode'), KEY('EnableStructure', 'key.enablesubstructure'), KEY('ID', 'key.id'), KEY('Description', 'key.description'), @@ -478,8 +476,6 @@ def __init__(self, internal_name, external_name): KIND('StatNumRequests', 'source.statistic.num-requests'), KIND('StatNumSemaRequests', 'source.statistic.num-semantic-requests'), KIND('StatInstructionCount', 'source.statistic.instruction-count'), - KIND('SyntaxTreeOff', 'source.syntaxtree.transfer.off'), - KIND('SyntaxTreeFull', 'source.syntaxtree.transfer.full'), KIND('Swift', 'source.lang.swift'), KIND('ObjC', 'source.lang.objc'), ] diff --git a/utils/incrparse/incr_transfer_round_trip.py b/utils/incrparse/incr_transfer_round_trip.py deleted file mode 100755 index bbcb57d762c34..0000000000000 --- a/utils/incrparse/incr_transfer_round_trip.py +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import os -import subprocess -import sys - -from test_util import TestFailedError, prepareForIncrParse, run_command - - -def main(): - parser = argparse.ArgumentParser( - formatter_class=argparse.RawDescriptionHelpFormatter, - description='Utility for testing incremental syntax parsing', - epilog=''' - Based of a single template the utility generates a pre-edit and a post-edit - file. It then verifies that incrementally parsing the post-edit file base - on the pre-edit file results in the same syntax tree as reparsing the - post-edit file from scratch. - - To generate the pre-edit and the post-edit file from the template, it - operates on markers of the form: - - <>> - - These placeholders are replaced by: - - 'pre' if a different test case than 'test_case' is run - - 'pre' for the pre-edit version of 'test_case' - - 'post' for the post-edit version of 'test_case' - ''') - parser.add_argument( - 'file', type=argparse.FileType(), - help='The template file to test') - parser.add_argument( - '--test-case', default='', - help='The test case to execute. If no test case is specified all \ - unnamed substitutions are applied') - parser.add_argument( - '--temp-dir', required=True, - help='A temporary directory where pre-edit and post-edit files can be \ - saved') - parser.add_argument( - '--swiftsyntax-lit-test-helper', required=True, - help='The path to the lit-test-helper binary of SwiftSyntax') - - args = parser.parse_args(sys.argv[1:]) - - test_file = args.file.name - test_file_name = os.path.basename(test_file) - test_case = args.test_case - temp_dir = args.temp_dir - swiftsyntax_lit_test_helper = args.swiftsyntax_lit_test_helper - - if not os.path.exists(temp_dir): - os.makedirs(temp_dir) - - pre_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \ - '.pre.swift' - post_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \ - '.post.swift' - after_roundtrip_file = temp_dir + '/' + test_file_name + '.' \ - + test_case + '.post_after_roundtrip.swift' - - # ========================================================================= - # First generate the pre-edit and post-edit Swift file and gather the edits - # and expected reparse regions. This is the parser for the special edit - # markup for testing incremental parsing - # ========================================================================= - - # Gather command line arguments for swift-syntax-test specifying the - # performed edits in this list - incremental_edit_args = [] - reparse_args = [] - try: - prepareForIncrParse(test_file=test_file, test_case=test_case, - pre_edit_file=pre_edit_file, - post_edit_file=post_edit_file, - incremental_edit_args=incremental_edit_args, - reparse_args=reparse_args) - except TestFailedError as e: - print('Test case "%s" of %s FAILed' % (test_case, test_file), - file=sys.stderr) - print(e.message, file=sys.stderr) - sys.exit(1) - - try: - run_command([swiftsyntax_lit_test_helper, '-parse-incremental'] + - ['-old-source-file', pre_edit_file] + - ['-source-file', post_edit_file] + - incremental_edit_args + reparse_args + - ['-out', after_roundtrip_file]) - except subprocess.CalledProcessError as e: - print('Test case "%s" of %s FAILed' % (test_case, test_file), - file=sys.stderr) - print('Parsing the swift file failed:\n', file=sys.stderr) - print(e.output.decode('UTF-8'), file=sys.stderr) - sys.exit(1) - - # Check if the two syntax trees are the same - try: - run_command( - [ - 'diff', '-u', - post_edit_file, - after_roundtrip_file - ]) - except subprocess.CalledProcessError as e: - print('Test case "%s" of %s FAILed' % (test_case, test_file), - file=sys.stderr) - print('Source file after incrementally parsing ' - 'does not match post-edit source file:\n\n', - file=sys.stderr) - print(e.output, file=sys.stderr) - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/utils/incrparse/test_util.py b/utils/incrparse/test_util.py deleted file mode 100755 index 3f7ad4219a93c..0000000000000 --- a/utils/incrparse/test_util.py +++ /dev/null @@ -1,319 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import io -import os -import platform -import re -import subprocess -import sys - - -class TestFailedError(Exception): - pass - - -def escapeCmdArg(arg): - if '"' in arg or ' ' in arg: - return '"%s"' % arg.replace('"', '\\"') - else: - return arg - - -def run_command(cmd): - if sys.version_info[0] < 3: - cmd = list(map(lambda s: s.encode('utf-8'), cmd)) - cmdStr = ' '.join([escapeCmdArg(arg) for arg in cmd]) - if not sys.stdout.encoding.lower().startswith('utf'): - # stdout doesn't support Unicode characters, encode them into an escape - # sequence - cmdStr = cmdStr.encode('utf-8') - print(cmdStr) - if sys.version_info[0] < 3 or platform.system() == 'Windows': - return subprocess.check_output(cmd, stderr=subprocess.STDOUT) - else: - return subprocess.check_output(list(map(lambda s: s.encode('utf-8'), cmd)), - stderr=subprocess.STDOUT) - - -def parseLine(line, line_no, test_case, incremental_edit_args, reparse_args, - current_reparse_start): - pre_edit_line = "" - post_edit_line = "" - - # We parse one tag at a time in the line while eating away a prefix of the - # line - while line: - # The regular expression to match the template markers - subst_re = re.compile(r'^(.*?)<<(.*?)<(.*?)\|\|\|(.*?)>>>(.*\n?)') - reparse_re = re.compile(r'^(.*?)<(/?)reparse ?(.*?)>(.*\n?)') - subst_match = subst_re.match(line) - reparse_match = reparse_re.match(line) - if subst_match and reparse_match: - # If both regex match use the one with the shorter prefix - if len(subst_match.group(1)) < len(reparse_match.group(1)): - reparse_match = None - else: - subst_match = None - - if subst_match: - prefix = subst_match.group(1) - match_test_case = subst_match.group(2) - pre_edit = subst_match.group(3) - post_edit = subst_match.group(4) - suffix = subst_match.group(5) - - if match_test_case == test_case: - # Compute the -incremental-edit argument for swift-syntax-test - column = len(pre_edit_line) + len(prefix) + 1 - edit_arg = '%d:%d-%d:%d=%s' % \ - (line_no, column, line_no, column + len(pre_edit.encode('utf-8')), - post_edit) - incremental_edit_args.append('-incremental-edit') - incremental_edit_args.append(edit_arg) - - pre_edit_line += prefix + pre_edit - post_edit_line += prefix + post_edit - else: - # For different test cases just take the pre-edit text - pre_edit_line += prefix + pre_edit - post_edit_line += prefix + pre_edit - - line = suffix - elif reparse_match: - prefix = reparse_match.group(1) - is_closing = len(reparse_match.group(2)) > 0 - match_test_case = reparse_match.group(3) - suffix = reparse_match.group(4) - if match_test_case == test_case: - column = len(post_edit_line) + len(prefix) + 1 - if is_closing: - if not current_reparse_start: - raise TestFailedError('Closing unopened reparse tag ' - 'in line %d' % line_no) - reparse_args.append('-reparse-region') - reparse_args.append( - '%d:%d-%d:%d' % (current_reparse_start[0], - current_reparse_start[1], - line_no, column)) - current_reparse_start = None - else: - if current_reparse_start: - raise TestFailedError('Opening nested reparse tags ' - 'for the same test case in line ' - '%d' % line_no) - current_reparse_start = [line_no, column] - - pre_edit_line += prefix - post_edit_line += prefix - line = suffix - else: - pre_edit_line += line - post_edit_line += line - # Nothing more to do - line = '' - - return (pre_edit_line.encode('utf-8'), - post_edit_line.encode('utf-8'), - current_reparse_start) - - -def prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file, - incremental_edit_args, reparse_args): - with io.open(test_file, mode='r', encoding='utf-8', - newline='\n') as test_file_handle, \ - io.open(pre_edit_file, mode='w+', encoding='utf-8', - newline='\n') as pre_edit_file_handle, \ - io.open(post_edit_file, mode='w+', encoding='utf-8', - newline='\n') as post_edit_file_handle: - - current_reparse_start = None - - line_no = 1 - for line in test_file_handle.readlines(): - parseLineRes = parseLine(line, line_no, test_case, - incremental_edit_args, - reparse_args, current_reparse_start) - (pre_edit_line, post_edit_line, current_reparse_start) = \ - parseLineRes - - pre_edit_file_handle.write(pre_edit_line.decode('utf-8')) - post_edit_file_handle.write(post_edit_line.decode('utf-8')) - - line_no += 1 - - if current_reparse_start: - raise TestFailedError('Unclosed reparse tag for test case %s' % - test_case) - - -def serializeIncrParseMarkupFile(test_file, test_case, mode, - omit_node_ids, output_file, diags_output_file, - temp_dir, swift_syntax_test, - print_visual_reuse_info): - test_file_name = os.path.basename(test_file) - pre_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \ - '.pre.swift' - post_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \ - '.post.swift' - - if not os.path.exists(temp_dir): - os.makedirs(temp_dir) - - # ========================================================================= - # First generate the pre-edit and post-edit Swift file and gather the edits - # and expected reparse regions. This is the parser for the special edit - # markup for testing incremental parsing - # ========================================================================= - - # Gather command line arguments for swift-syntax-test specifying the - # performed edits in this list - incremental_edit_args = [] - reparse_args = [] - prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file, - incremental_edit_args, reparse_args) - - # ========================================================================= - # Now generate the requested serialized file - # ========================================================================= - - # Build the command to serialize the tree depending on the command line - # arguments - - try: - command = [ - swift_syntax_test, - '-serialize-raw-tree', - '-output-filename', output_file - ] - - if diags_output_file: - command.extend(['-diags-output-filename', diags_output_file]) - - if omit_node_ids: - command.extend(['-omit-node-ids']) - - if mode == 'pre-edit': - command.extend(['-input-source-filename', pre_edit_file]) - elif mode == 'post-edit': - command.extend(['-input-source-filename', post_edit_file]) - elif mode == 'incremental': - # We need to build the syntax tree of the pre-edit file first so - # that we can pass it to swift-syntax-test to perform incremental - # parsing - pre_edit_tree_file = pre_edit_file + '.serialized.json' - - run_command([swift_syntax_test] + - ['-serialize-raw-tree'] + - ['-input-source-filename', pre_edit_file] + - ['-output-filename', pre_edit_tree_file]) - - # Then perform incremental parsing with the old syntax tree on the - # post-edit file - command.extend(['-input-source-filename', post_edit_file]) - command.extend(['-old-syntax-tree-filename', - pre_edit_tree_file]) - command.extend(['--old-source-filename', pre_edit_file]) - command.extend(incremental_edit_args) - command.extend(reparse_args) - if print_visual_reuse_info: - command.extend([ - '-print-visual-reuse-info', - '-force-colored-output' - ]) - else: - raise ValueError('Unknown mode "%s"' % mode) - - output = run_command(command) - if print_visual_reuse_info: - print(output) - except subprocess.CalledProcessError as e: - raise TestFailedError(e.output.decode('utf-8')) - - -def main(): - parser = argparse.ArgumentParser( - formatter_class=argparse.RawDescriptionHelpFormatter, - description='Utility for testing incremental syntax parsing', - epilog=''' - This utility can parse a special markup to dedicate a pre-edit and a - post-edit version of a file simultaneously and generate a serialized version - of the libSyntax tree by parsing either the pre-edit file, the post-edit - file or the edits that are required to retrieve the post-edit file from the - pre-edit file incrementally. - - To generate the pre-edit and the post-edit file from the template, it - operates on markers of the form: - - <>> - - These placeholders are replaced by: - - 'pre' if a different test case than 'test_case' is run - - 'pre' for the pre-edit version of 'test_case' - - 'post' for the post-edit version of 'test_case''') - parser.add_argument( - 'file', type=argparse.FileType(), - help='The template file to test') - parser.add_argument( - '--test-case', default='', - help='The test case to execute. If no test case is specified all \ - unnamed substitutions are applied') - parser.add_argument( - '--mode', choices=['pre-edit', 'incremental', 'post-edit'], - required=True, help=''' - The type of parsing to perform: - - pre-edit: Serialize the syntax tree when parsing the pre-edit file \ - from scratch - - incremental: Serialize the syntax tree that results from parsing the \ - edits between the pre-edit and post-edit file incrementally - - post-edit: Serialize the syntax tree that results from parsing the \ - post-edit file from scratch - ''') - parser.add_argument( - '--omit-node-ids', default=False, action='store_true', - help='Don\'t include the ids of the nodes in the serialized syntax \ - tree') - parser.add_argument( - '--output-file', required=True, - help='The file to which the serialized tree shall be written.') - parser.add_argument( - '--temp-dir', required=True, - help='A temporary directory where pre-edit and post-edit files can be \ - saved') - parser.add_argument( - '--swift-syntax-test', required=True, - help='The path to swift-syntax-test') - parser.add_argument( - '--print-visual-reuse-info', default=False, action='store_true', - help='Print visual reuse information about the incremental parse \ - instead of diffing the syntax trees. This option is intended \ - for debug purposes only.') - - args = parser.parse_args(sys.argv[1:]) - - test_file = args.file.name - test_case = args.test_case - mode = args.mode - omit_node_ids = args.omit_node_ids - output_file = args.output_file - temp_dir = args.temp_dir - swift_syntax_test = args.swift_syntax_test - visual_reuse_info = args.print_visual_reuse_info - - try: - serializeIncrParseMarkupFile(test_file=test_file, - test_case=test_case, - mode=mode, - omit_node_ids=omit_node_ids, - output_file=output_file, - diags_output_file=None, - temp_dir=temp_dir, - swift_syntax_test=swift_syntax_test, - print_visual_reuse_info=visual_reuse_info) - except TestFailedError as e: - print(e.message, file=sys.stderr) - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/utils/incrparse/validate_parse.py b/utils/incrparse/validate_parse.py deleted file mode 100755 index 826c2e7e2eb34..0000000000000 --- a/utils/incrparse/validate_parse.py +++ /dev/null @@ -1,148 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import difflib -import io -import os -import sys - -from test_util import TestFailedError, serializeIncrParseMarkupFile - - -def main(): - parser = argparse.ArgumentParser( - formatter_class=argparse.RawDescriptionHelpFormatter, - description='Utility for testing incremental syntax parsing', - epilog=''' - Based of a single template the utility generates a pre-edit and a post-edit - file. It then verifies that incrementally parsing the post-edit file base - on the pre-edit file results in the same syntax tree as reparsing the - post-edit file from scratch. - - To generate the pre-edit and the post-edit file from the template, it - operates on markers of the form: - - <>> - - These placeholders are replaced by: - - 'pre' if a different test case than 'test_case' is run - - 'pre' for the pre-edit version of 'test_case' - - 'post' for the post-edit version of 'test_case' - ''') - parser.add_argument( - 'file', type=argparse.FileType(), - help='The template file to test') - parser.add_argument( - '--test-case', default='', - help='The test case to execute. If no test case is specified all \ - unnamed substitutions are applied') - parser.add_argument( - '--temp-dir', required=True, - help='A temporary directory where pre-edit and post-edit files can be \ - saved') - parser.add_argument( - '--swift-syntax-test', required=True, - help='The path to swift-syntax-test') - parser.add_argument( - '--print-visual-reuse-info', default=False, action='store_true', - help='Print visual reuse information about the incremental parse \ - instead of diffing the syntax trees. This option is intended \ - for debug purposes only.') - - args = parser.parse_args(sys.argv[1:]) - - test_file = args.file.name - test_file_name = os.path.basename(test_file) - test_case = args.test_case - temp_dir = args.temp_dir - swift_syntax_test = args.swift_syntax_test - visual_reuse_info = args.print_visual_reuse_info - - if not os.path.exists(temp_dir): - os.makedirs(temp_dir) - - incremental_serialized_file = temp_dir + '/' + test_file_name + '.' \ - + test_case + '.postViaIncr.json' - post_edit_serialized_file = temp_dir + '/' + test_file_name + '.' \ - + test_case + '.post.json' - - incremental_diags_file = temp_dir + '/' + test_file_name + '.' \ - + test_case + '.diagsViaIncr.txt' - post_edit_diags_file = temp_dir + '/' + test_file_name + '.' \ - + test_case + '.post.diags.txt' - - # Generate the syntax tree once incrementally and once from scratch - try: - serializeIncrParseMarkupFile(test_file=test_file, - test_case=test_case, - mode='incremental', - omit_node_ids=True, - output_file=incremental_serialized_file, - diags_output_file=incremental_diags_file, - temp_dir=temp_dir + '/temp', - swift_syntax_test=swift_syntax_test, - print_visual_reuse_info=visual_reuse_info) - if visual_reuse_info: - # If we just want the reuse info, we don't need to parse the file - # from scratch or validate it - sys.exit(0) - - serializeIncrParseMarkupFile(test_file=test_file, - test_case=test_case, - mode='post-edit', - omit_node_ids=True, - output_file=post_edit_serialized_file, - diags_output_file=post_edit_diags_file, - temp_dir=temp_dir + '/temp', - swift_syntax_test=swift_syntax_test, - print_visual_reuse_info=visual_reuse_info) - except TestFailedError as e: - print('Test case "%s" of %s FAILed' % (test_case, test_file), - file=sys.stderr) - print(e.message, file=sys.stderr) - sys.exit(1) - - # Check if the two syntax trees are the same - lines = difflib.unified_diff(io.open(incremental_serialized_file, 'r', - encoding='utf-8', errors='ignore').readlines(), - io.open(post_edit_serialized_file, 'r', - encoding='utf-8', errors='ignore').readlines(), - fromfile=incremental_serialized_file, - tofile=post_edit_serialized_file) - diff = '\n'.join(line for line in lines) - if diff: - print('Test case "%s" of %s FAILed' % (test_case, test_file), - file=sys.stderr) - print('Syntax tree of incremental parsing does not match ' - 'from-scratch parsing of post-edit file:\n\n', file=sys.stderr) - print(diff, file=sys.stderr) - sys.exit(1) - - # Verify that if the incremental parse resulted in parser diagnostics, those - # diagnostics were also emitted during the full parse. - # We can't just diff the outputs because the full parse includes diagnostics - # from the whole file, while the incremental parse includes only a subset. - # Each diagnostic is searched in the full parse diagnostics array but the - # search for each diagnostic continues from where the previous search - # stopped. - incremental_diags = open(incremental_diags_file).readlines() - post_edit_diags = open(post_edit_diags_file).readlines() - full_idx = 0 - for diag in incremental_diags: - while full_idx < len(post_edit_diags): - if post_edit_diags[full_idx] == diag: - break - full_idx += 1 - if full_idx == len(post_edit_diags): - print('Test case "%s" of %s FAILed' % (test_case, test_file), - file=sys.stderr) - print('Parser diagnostic of incremental parsing was not emitted ' - 'during from-scratch parsing of post-edit file:', - file=sys.stderr) - print(diag, file=sys.stderr) - sys.exit(1) - full_idx += 1 # continue searching from the next diagnostic line. - - -if __name__ == '__main__': - main()