//===--- CompileInstance.h ------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2021 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_IDE_COMPILEINSTANCE_H #define SWIFT_IDE_COMPILEINSTANCE_H #include "swift/Frontend/Frontend.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Chrono.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/VirtualFileSystem.h" namespace swift { class CompilerInstance; class CompilerInvocation; class DiagnosticConsumer; namespace ide { /// Manages \c CompilerInstance for completion like operations. class CompileInstance { const std::string &RuntimeResourcePath; const std::string &DiagnosticDocumentationPath; struct Options { unsigned MaxASTReuseCount = 100; } Opts; std::mutex mtx; std::unique_ptr CI; llvm::hash_code CachedArgHash; std::atomic CachedCIInvalidated; unsigned CachedReuseCount; /// Perform cached sema. Returns \c true if the CI is not reusable. bool performCachedSemaIfPossible(DiagnosticConsumer *DiagC); /// Setup the CI with \p Args . Returns \c true if failed. bool setupCI(llvm::ArrayRef Args, llvm::IntrusiveRefCntPtr FileSystem, DiagnosticConsumer *DiagC); /// Perform Parse and Sema, potentially CI from previous compilation is /// reused. void performSema(llvm::ArrayRef Args, llvm::IntrusiveRefCntPtr FileSystem, DiagnosticConsumer *DiagC, std::shared_ptr> CancellationFlag); public: CompileInstance(const std::string &RuntimeResourcePath, const std::string &DiagnosticDocumentationPath) : RuntimeResourcePath(RuntimeResourcePath), DiagnosticDocumentationPath(DiagnosticDocumentationPath), CachedCIInvalidated(false), CachedReuseCount(0) {} /// NOTE: \p Args is only used for checking the equaity of the invocation. /// Since this function assumes that it is already normalized, exact the same /// arguments including their order is considered as the same invocation. bool performCompile(llvm::ArrayRef Args, llvm::IntrusiveRefCntPtr FileSystem, DiagnosticConsumer *DiagC, std::shared_ptr> CancellationFlag); }; } // namespace ide } // namespace swift #endif // SWIFT_IDE_COMPILEINSTANCE_H