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 | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame^] | 18 | class tool_output_file; |
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 LLVMErrorHandler(void *UserData, const std::string &Message); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 44 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 45 | public: |
| 46 | typedef enum { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 47 | OT_Dependency, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 48 | OT_Assembly, |
| 49 | OT_LLVMAssembly, |
| 50 | OT_Bitcode, |
| 51 | OT_Nothing, |
| 52 | OT_Object, |
| 53 | |
| 54 | OT_Default = OT_Bitcode |
| 55 | } OutputType; |
| 56 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 57 | private: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 58 | // The diagnostics engine instance (for status reporting during compilation) |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 59 | llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics; |
| 60 | // The clients of diagnostics engine. The ownership is taken by the |
| 61 | // mDiagnostics after creation. |
| 62 | DiagnosticBuffer *mDiagClient; |
| 63 | void createDiagnostic(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 64 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 65 | // The target being compiled for |
| 66 | clang::TargetOptions mTargetOpts; |
| 67 | llvm::OwningPtr<clang::TargetInfo> mTarget; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 68 | void createTarget(const std::string &Triple, const std::string &CPU, |
| 69 | const std::vector<std::string> &Features); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 70 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 71 | // Below is for parsing and code generation |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 72 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 73 | // The file manager (for prepocessor doing the job such as header file search) |
| 74 | llvm::OwningPtr<clang::FileManager> mFileMgr; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 75 | void createFileManager(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 76 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 77 | // The source manager (responsible for the source code handling) |
| 78 | llvm::OwningPtr<clang::SourceManager> mSourceMgr; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 79 | void createSourceManager(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 80 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 81 | // The preprocessor (source code preprocessor) |
| 82 | llvm::OwningPtr<clang::Preprocessor> mPP; |
| 83 | void createPreprocessor(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 84 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 85 | // The AST context (the context to hold long-lived AST nodes) |
| 86 | llvm::OwningPtr<clang::ASTContext> mASTContext; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 87 | void createASTContext(); |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 88 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 89 | // The AST consumer, responsible for code generation |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 90 | llvm::OwningPtr<clang::ASTConsumer> mBackend; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 91 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 92 | // Input file name |
| 93 | std::string mInputFileName; |
| 94 | std::string mOutputFileName; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 95 | |
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 | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 98 | std::vector<std::string> mAdditionalDepTargets; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 99 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 100 | OutputType mOT; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 101 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 102 | // Output stream |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame^] | 103 | llvm::OwningPtr<llvm::tool_output_file> mOS; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 104 | // Dependency output stream |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame^] | 105 | llvm::OwningPtr<llvm::tool_output_file> mDOS; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 106 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 107 | std::vector<std::string> mIncludePaths; |
Ying Wang | e2e522f | 2010-09-01 13:24:01 -0700 | [diff] [blame] | 108 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 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 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 131 | public: |
| 132 | static const std::string TargetDescription; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 133 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 134 | static const llvm::StringRef PragmaMetadataName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 135 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 136 | static void GlobalInitialization(); |
| 137 | |
| 138 | Slang(const std::string &Triple, const std::string &CPU, |
| 139 | const std::vector<std::string> &Features); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 140 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 141 | bool setInputSource(llvm::StringRef InputFile, const char *Text, |
| 142 | size_t TextLength); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 143 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 144 | bool setInputSource(llvm::StringRef InputFile); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 145 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 146 | inline const std::string &getInputFileName() const { return mInputFileName; } |
Ying Wang | e2e522f | 2010-09-01 13:24:01 -0700 | [diff] [blame] | 147 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 148 | inline void setIncludePaths(const std::vector<std::string> &IncludePaths) { |
| 149 | mIncludePaths = IncludePaths; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 150 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 151 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 152 | inline void setOutputType(OutputType OT) { mOT = OT; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 153 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 154 | bool setOutput(const char *OutputFile); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 155 | inline const std::string &getOutputFileName() const { |
| 156 | return mOutputFileName; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 157 | } |
Kirk Stewart | 1fd8579 | 2010-07-07 09:51:23 -0700 | [diff] [blame] | 158 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 159 | bool setDepOutput(const char *OutputFile); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 160 | inline void setDepTargetBC(const char *TargetBCFile) { |
| 161 | mDepTargetBCFileName = TargetBCFile; |
| 162 | } |
| 163 | inline void setAdditionalDepTargets( |
| 164 | const std::vector<std::string> &AdditionalDepTargets) { |
| 165 | mAdditionalDepTargets = AdditionalDepTargets; |
| 166 | } |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 167 | |
| 168 | int generateDepFile(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 169 | int compile(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 170 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 171 | inline const char *getErrorMessage() { return mDiagClient->str().c_str(); } |
Shih-wei Liao | 4c9f742 | 2010-08-05 04:30:02 -0700 | [diff] [blame] | 172 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 173 | // Reset the slang compiler state such that it can be reused to compile |
| 174 | // another file |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 175 | virtual void reset(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 176 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 177 | virtual ~Slang(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 178 | }; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 179 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 180 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 181 | #endif // _SLANG_COMPILER_SLANG_H |