| #ifndef _SLANG_COMPILER_SLANG_H |
| #define _SLANG_COMPILER_SLANG_H |
| |
| #include <cstdio> |
| #include <string> |
| #include <vector> |
| |
| #include "llvm/ADT/OwningPtr.h" |
| #include "llvm/ADT/StringRef.h" |
| #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| |
| #include "clang/Basic/TargetOptions.h" |
| |
| #include "slang_pragma_recorder.h" |
| #include "slang_diagnostic_buffer.h" |
| |
| namespace llvm { |
| class raw_ostream; |
| } |
| |
| namespace clang { |
| class Diagnostic; |
| class FileManager; |
| class SourceManager; |
| class LangOptions; |
| class Preprocessor; |
| class TargetOptions; |
| class CodeGenOptions; |
| class ASTContext; |
| class ASTConsumer; |
| class Backend; |
| class TargetInfo; |
| } |
| |
| namespace slang { |
| |
| class Slang { |
| static clang::LangOptions LangOpts; |
| static clang::CodeGenOptions CodeGenOpts; |
| |
| static bool GlobalInitialized; |
| |
| static void GlobalInitialization(); |
| |
| static void LLVMErrorHandler(void *UserData, const std::string &Message); |
| |
| public: |
| typedef enum { |
| OT_Dependency, |
| OT_Assembly, |
| OT_LLVMAssembly, |
| OT_Bitcode, |
| OT_Nothing, |
| OT_Object, |
| |
| OT_Default = OT_Bitcode |
| } OutputType; |
| |
| private: |
| // The diagnostics engine instance (for status reporting during compilation) |
| llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics; |
| // The clients of diagnostics engine. The ownership is taken by the |
| // mDiagnostics after creation. |
| DiagnosticBuffer *mDiagClient; |
| void createDiagnostic(); |
| |
| // The target being compiled for |
| clang::TargetOptions mTargetOpts; |
| llvm::OwningPtr<clang::TargetInfo> mTarget; |
| void createTarget(const char *Triple, const char *CPU, const char **Features); |
| |
| // Below is for parsing and code generation |
| |
| // The file manager (for prepocessor doing the job such as header file search) |
| llvm::OwningPtr<clang::FileManager> mFileMgr; |
| void createFileManager(); |
| |
| // The source manager (responsible for the source code handling) |
| llvm::OwningPtr<clang::SourceManager> mSourceMgr; |
| void createSourceManager(); |
| |
| // The preprocessor (source code preprocessor) |
| llvm::OwningPtr<clang::Preprocessor> mPP; |
| void createPreprocessor(); |
| |
| // The AST context (the context to hold long-lived AST nodes) |
| llvm::OwningPtr<clang::ASTContext> mASTContext; |
| void createASTContext(); |
| |
| // The AST consumer, responsible for code generation |
| llvm::OwningPtr<clang::ASTConsumer> mBackend; |
| |
| // Input file name |
| std::string mInputFileName; |
| std::string mOutputFileName; |
| std::string mDepOutputFileName; |
| std::string mDepTargetBCFileName; |
| std::string mAdditionalDepTargetFileName; |
| |
| OutputType mOT; |
| |
| // Output stream |
| llvm::OwningPtr<llvm::raw_ostream> mOS; |
| // Dependency output stream |
| llvm::OwningPtr<llvm::raw_ostream> mDOS; |
| |
| std::vector<std::string> mIncludePaths; |
| |
| protected: |
| PragmaList mPragmas; |
| |
| inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; } |
| inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; } |
| inline clang::FileManager &getFileManager() { return *mFileMgr; } |
| inline clang::SourceManager &getSourceManager() { return *mSourceMgr; } |
| inline clang::Preprocessor &getPreprocessor() { return *mPP; } |
| inline clang::ASTContext &getASTContext() { return *mASTContext; } |
| |
| inline const clang::TargetOptions &getTargetOptions() const |
| { return mTargetOpts; } |
| |
| virtual void initDiagnostic() {} |
| virtual void initPreprocessor() {} |
| virtual void initASTContext() {} |
| |
| virtual clang::ASTConsumer |
| *createBackend(const clang::CodeGenOptions& CodeGenOpts, |
| llvm::raw_ostream *OS, |
| OutputType OT); |
| |
| public: |
| static const std::string TargetDescription; |
| |
| static const llvm::StringRef PragmaMetadataName; |
| |
| Slang(const char *Triple, const char *CPU, const char **Features); |
| |
| bool setInputSource(llvm::StringRef InputFile, const char *Text, |
| size_t TextLength); |
| |
| bool setInputSource(llvm::StringRef InputFile); |
| |
| inline const std::string &getInputFileName() const { return mInputFileName; } |
| |
| inline void addIncludePath(const char *Path) { |
| mIncludePaths.push_back(Path); |
| } |
| |
| inline void setOutputType(OutputType OT) { mOT = OT; } |
| |
| bool setOutput(const char *OutputFile); |
| inline const std::string &getOutputFileName() const { |
| return mOutputFileName; |
| } |
| |
| bool setDepOutput(const char *OutputFile); |
| bool setDepTargetBC(const char *TargetBCFile); |
| bool setAdditionalDepTarget(const char* AdditionalDepTargetFileName); |
| |
| int generateDepFile(); |
| int compile(); |
| |
| inline const char *getErrorMessage() { return mDiagClient->str().c_str(); } |
| |
| // Reset the slang compiler state such that it can be reused to compile |
| // another file |
| virtual void reset(); |
| |
| virtual ~Slang(); |
| }; |
| } |
| |
| #endif // _SLANG_COMPILER_SLANG_H |