//===-- Frontend.h - frontend utility methods ------------------*- C++ -*--===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// // // This file contains declarations of utility methods for parsing and // performing semantic on modules. // //===----------------------------------------------------------------------===// #ifndef SWIFT_FRONTEND_H #define SWIFT_FRONTEND_H #include "swift/Basic/DiagnosticConsumer.h" #include "swift/Basic/LangOptions.h" #include "swift/Basic/SourceManager.h" #include "swift/AST/DiagnosticEngine.h" #include "swift/AST/LinkLibrary.h" #include "swift/AST/Module.h" #include "swift/Parse/CodeCompletionCallbacks.h" #include "swift/Parse/Parser.h" #include "swift/ClangImporter/ClangImporter.h" #include "swift/Sema/SourceLoader.h" #include "swift/SIL/SILModule.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/Support/Host.h" #include namespace llvm { class MemoryBuffer; } namespace swift { class SerializedModuleLoader; class CompilerInvocation { std::string TargetTriple; std::string ClangModuleCachePath; std::vector ImportSearchPaths; std::vector FrameworkSearchPaths; SmallVector LinkLibraries; std::string RuntimeIncludePath; std::string SDKPath; std::string SerializedDiagnosticsPath; std::string ModuleSourceListPath; std::vector ExtraClangArgs; LangOptions LangOpts; bool ParseStdlib = false; bool ParseOnly = false; bool Immediate = false; SourceFile::SourceKind InputKind = SourceFile::Main; std::string ModuleName; std::vector InputFilenames; std::vector InputBuffers; std::string OutputFilename; llvm::MemoryBuffer *CodeCompletionBuffer = nullptr; /// \brief Code completion offset in bytes from the beginning of the main /// source file. Valid only if \c isCodeCompletion() == true. unsigned CodeCompletionOffset = ~0U; CodeCompletionCallbacksFactory *CodeCompletionFactory = nullptr; bool DelayedFunctionBodyParsing = false; public: CompilerInvocation(); /// \brief Initializes the compiler invocation for the list of arguments. /// \returns true if there was an error, false on success. bool parseArgs(ArrayRef Args, DiagnosticEngine &Diags); void setTargetTriple(StringRef Triple) { TargetTriple = Triple.str(); } StringRef getTargetTriple() const { return TargetTriple; } void setClangModuleCachePath(StringRef Path) { ClangModuleCachePath = Path.str(); } StringRef getClangModuleCachePath() const { return ClangModuleCachePath; } void setImportSearchPaths(const std::vector &Paths) { ImportSearchPaths = Paths; } ArrayRef getImportSearchPaths() const { return ImportSearchPaths; } void setFrameworkSearchPaths(const std::vector &Paths) { FrameworkSearchPaths = Paths; } ArrayRef getFrameworkSearchPaths() const { return FrameworkSearchPaths; } void setExtraClangArgs(const std::vector &Args) { ExtraClangArgs = Args; } ArrayRef getExtraClangArgs() const { return ExtraClangArgs; } void addLinkLibrary(StringRef name, LibraryKind kind) { LinkLibraries.push_back({name, kind}); } ArrayRef getLinkLibraries() const { return LinkLibraries; } void setMainExecutablePath(StringRef Path); void setRuntimeIncludePath(StringRef Path) { RuntimeIncludePath = Path; } StringRef getRuntimeIncludePath() const { return RuntimeIncludePath; } void setSDKPath(const std::string &Path) { SDKPath = Path; } StringRef getSDKPath() const { return SDKPath; } void setSerializedDiagnosticsPath(StringRef Path) { SerializedDiagnosticsPath = Path; } StringRef getSerializedDiagnosticsPath() const { return SerializedDiagnosticsPath; } void setModuleSourceListPath(StringRef Path) { ModuleSourceListPath = Path; } StringRef getModuleSourceListPath() const { return ModuleSourceListPath; } LangOptions &getLangOptions() { return LangOpts; } const LangOptions &getLangOptions() const { return LangOpts; } void setParseStdlib() { ParseStdlib = true; } bool getParseStdlib() const { return ParseStdlib; } void setParseOnly() { ParseOnly = true; } bool getParseOnly() const { return ParseOnly; } void setInputKind(SourceFile::SourceKind K) { InputKind = K; } SourceFile::SourceKind getInputKind() const { return InputKind; } void setModuleName(StringRef Name) { ModuleName = Name.str(); } StringRef getModuleName() const { return ModuleName; } void addInputFilename(StringRef Filename) { InputFilenames.push_back(Filename); } void addInputBuffer(llvm::MemoryBuffer *Buf) { InputBuffers.push_back(Buf); } void clearInputs() { InputFilenames.clear(); InputBuffers.clear(); } ArrayRef getInputFilenames() const { return InputFilenames; } ArrayRef getInputBuffers() const { return InputBuffers; } void setOutputFilename(StringRef Filename) { OutputFilename = Filename; } StringRef getOutputFilename() const { return OutputFilename; } void setCodeCompletionPoint(llvm::MemoryBuffer *Buf, unsigned Offset) { assert(Buf); CodeCompletionBuffer = Buf; CodeCompletionOffset = Offset; } std::pair getCodeCompletionPoint() const { return std::make_pair(CodeCompletionBuffer, CodeCompletionOffset); } /// \returns true if we are doing code completion. bool isCodeCompletion() const { return CodeCompletionOffset != ~0U; } void setCodeCompletionFactory(CodeCompletionCallbacksFactory *Factory) { CodeCompletionFactory = Factory; } CodeCompletionCallbacksFactory *getCodeCompletionFactory() const { return CodeCompletionFactory; } void setDelayedFunctionBodyParsing(bool Val) { DelayedFunctionBodyParsing = Val; } bool isDelayedFunctionBodyParsing() const { return DelayedFunctionBodyParsing; } void setImmediate(bool Val) { Immediate = Val; } bool isImmediate() const { return Immediate; } }; class CompilerInstance { CompilerInvocation Invocation; SourceManager SourceMgr; DiagnosticEngine Diagnostics{SourceMgr}; std::unique_ptr Context; std::unique_ptr TheSILModule; TranslationUnit *TU = nullptr; SerializedModuleLoader *SML = nullptr; std::vector BufferIDs; enum : unsigned { NO_SUCH_BUFFER = ~0U }; unsigned MainBufferIndex = NO_SUCH_BUFFER; void createSILModule(); public: SourceManager &getSourceMgr() { return SourceMgr; } DiagnosticEngine &getDiags() { return Diagnostics; } void addDiagnosticConsumer(DiagnosticConsumer *DC) { Diagnostics.addConsumer(*DC); } ASTContext &getASTContext() { return *Context; } /// Set the SIL module for this compilation instance. /// /// The CompilerInstance takes ownership of the given SILModule object. void setSILModule(std::unique_ptr M) { TheSILModule = std::move(M); } SILModule *getSILModule() { return TheSILModule.get(); } std::unique_ptr takeSILModule() { return std::move(TheSILModule); } bool hasSILModule() { return static_cast(TheSILModule); } TranslationUnit *getTU() { return TU; } SerializedModuleLoader *getSerializedModuleLoader() const { return SML; } ArrayRef getInputBufferIDs() const { return BufferIDs; } ArrayRef getLinkLibraries() const { return Invocation.getLinkLibraries(); } /// \brief Returns true if there was an error during setup. bool setup(const CompilerInvocation &Invocation); void doIt(); }; } // namespace swift #endif