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