slang.h revision 5457231d5201be02c3232ffc1c6d8bc408f276ab
1#ifndef _SLANG_COMPILER_SLANG_H 2#define _SLANG_COMPILER_SLANG_H 3 4#include <cstdio> 5#include <string> 6#include <vector> 7 8#include "llvm/ADT/OwningPtr.h" 9#include "llvm/ADT/StringRef.h" 10#include "llvm/ADT/IntrusiveRefCntPtr.h" 11 12#include "clang/Basic/TargetOptions.h" 13 14#include "slang_pragma_recorder.h" 15#include "slang_diagnostic_buffer.h" 16 17namespace llvm { 18 class raw_ostream; 19} 20 21namespace clang { 22 class Diagnostic; 23 class FileManager; 24 class SourceManager; 25 class LangOptions; 26 class Preprocessor; 27 class TargetOptions; 28 class CodeGenOptions; 29 class ASTContext; 30 class ASTConsumer; 31 class Backend; 32 class TargetInfo; 33} 34 35namespace slang { 36 37class Slang { 38 static clang::LangOptions LangOpts; 39 static clang::CodeGenOptions CodeGenOpts; 40 41 static bool GlobalInitialized; 42 43 static void GlobalInitialization(); 44 45 static void LLVMErrorHandler(void *UserData, const std::string &Message); 46 47 public: 48 typedef enum { 49 OT_Dependency, 50 OT_Assembly, 51 OT_LLVMAssembly, 52 OT_Bitcode, 53 OT_Nothing, 54 OT_Object, 55 56 OT_Default = OT_Bitcode 57 } OutputType; 58 59 private: 60 // The diagnostics engine instance (for status reporting during compilation) 61 llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics; 62 // The clients of diagnostics engine. The ownership is taken by the 63 // mDiagnostics after creation. 64 DiagnosticBuffer *mDiagClient; 65 void createDiagnostic(); 66 67 // The target being compiled for 68 clang::TargetOptions mTargetOpts; 69 llvm::OwningPtr<clang::TargetInfo> mTarget; 70 void createTarget(const char *Triple, const char *CPU, const char **Features); 71 72 // Below is for parsing and code generation 73 74 // The file manager (for prepocessor doing the job such as header file search) 75 llvm::OwningPtr<clang::FileManager> mFileMgr; 76 void createFileManager(); 77 78 // The source manager (responsible for the source code handling) 79 llvm::OwningPtr<clang::SourceManager> mSourceMgr; 80 void createSourceManager(); 81 82 // The preprocessor (source code preprocessor) 83 llvm::OwningPtr<clang::Preprocessor> mPP; 84 void createPreprocessor(); 85 86 // The AST context (the context to hold long-lived AST nodes) 87 llvm::OwningPtr<clang::ASTContext> mASTContext; 88 void createASTContext(); 89 90 // The AST consumer, responsible for code generation 91 llvm::OwningPtr<clang::ASTConsumer> mBackend; 92 93 // Input file name 94 std::string mInputFileName; 95 std::string mOutputFileName; 96 std::string mDepOutputFileName; 97 std::string mDepTargetBCFileName; 98 std::string mAdditionalDepTargetFileName; 99 100 OutputType mOT; 101 102 // Output stream 103 llvm::OwningPtr<llvm::raw_ostream> mOS; 104 // Dependency output stream 105 llvm::OwningPtr<llvm::raw_ostream> mDOS; 106 107 std::vector<std::string> mIncludePaths; 108 109 protected: 110 PragmaList mPragmas; 111 112 inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; } 113 inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; } 114 inline clang::FileManager &getFileManager() { return *mFileMgr; } 115 inline clang::SourceManager &getSourceManager() { return *mSourceMgr; } 116 inline clang::Preprocessor &getPreprocessor() { return *mPP; } 117 inline clang::ASTContext &getASTContext() { return *mASTContext; } 118 119 inline const clang::TargetOptions &getTargetOptions() const 120 { return mTargetOpts; } 121 122 virtual void initDiagnostic() {} 123 virtual void initPreprocessor() {} 124 virtual void initASTContext() {} 125 126 virtual clang::ASTConsumer 127 *createBackend(const clang::CodeGenOptions& CodeGenOpts, 128 llvm::raw_ostream *OS, 129 OutputType OT); 130 131 public: 132 static const std::string TargetDescription; 133 134 static const llvm::StringRef PragmaMetadataName; 135 136 Slang(const char *Triple, const char *CPU, const char **Features); 137 138 bool setInputSource(llvm::StringRef InputFile, const char *Text, 139 size_t TextLength); 140 141 bool setInputSource(llvm::StringRef InputFile); 142 143 inline const std::string &getInputFileName() const { return mInputFileName; } 144 145 inline void addIncludePath(const char *Path) { 146 mIncludePaths.push_back(Path); 147 } 148 149 inline void setOutputType(OutputType OT) { mOT = OT; } 150 151 bool setOutput(const char *OutputFile); 152 inline const std::string &getOutputFileName() const { 153 return mOutputFileName; 154 } 155 156 bool setDepOutput(const char *OutputFile); 157 bool setDepTargetBC(const char *TargetBCFile); 158 bool setAdditionalDepTarget(const char* AdditionalDepTargetFileName); 159 160 int generateDepFile(); 161 int compile(); 162 163 inline const char *getErrorMessage() { return mDiagClient->str().c_str(); } 164 165 // Reset the slang compiler state such that it can be reused to compile 166 // another file 167 virtual void reset(); 168 169 virtual ~Slang(); 170}; 171} 172 173#endif // _SLANG_COMPILER_SLANG_H 174