Files
swift-mirror/include/swift/IDE/CompileInstance.h
2021-12-21 14:35:38 -08:00

84 lines
2.9 KiB
C++

//===--- 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<CompilerInstance> CI;
llvm::hash_code CachedArgHash;
std::atomic<bool> 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<const char *> Args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
DiagnosticConsumer *DiagC);
/// Perform Parse and Sema, potentially CI from previous compilation is
/// reused.
void performSema(llvm::ArrayRef<const char *> Args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
DiagnosticConsumer *DiagC,
std::shared_ptr<std::atomic<bool>> 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<const char *> Args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
DiagnosticConsumer *DiagC,
std::shared_ptr<std::atomic<bool>> CancellationFlag);
};
} // namespace ide
} // namespace swift
#endif // SWIFT_IDE_COMPILEINSTANCE_H