Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #ifndef _SLANG_COMPILER_SLANG_H |
| 18 | #define _SLANG_COMPILER_SLANG_H |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 19 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 20 | #include <cstdio> |
| 21 | #include <string> |
Ying Wang | e2e522f | 2010-09-01 13:24:01 -0700 | [diff] [blame] | 22 | #include <vector> |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 23 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 24 | #include "llvm/ADT/OwningPtr.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 26 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 28 | #include "clang/Basic/TargetOptions.h" |
| 29 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 30 | #include "slang_pragma_recorder.h" |
| 31 | #include "slang_diagnostic_buffer.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 32 | |
| 33 | namespace llvm { |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 34 | class tool_output_file; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 35 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 36 | |
| 37 | namespace clang { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 38 | class Diagnostic; |
| 39 | class FileManager; |
| 40 | class SourceManager; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 41 | class LangOptions; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 42 | class Preprocessor; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 43 | class TargetOptions; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 44 | class CodeGenOptions; |
| 45 | class ASTContext; |
| 46 | class ASTConsumer; |
| 47 | class Backend; |
| 48 | class TargetInfo; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 49 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 50 | |
| 51 | namespace slang { |
| 52 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 53 | class Slang { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 54 | static clang::LangOptions LangOpts; |
| 55 | static clang::CodeGenOptions CodeGenOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 56 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 57 | static bool GlobalInitialized; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 58 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 59 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 60 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 61 | public: |
| 62 | typedef enum { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 63 | OT_Dependency, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 64 | OT_Assembly, |
| 65 | OT_LLVMAssembly, |
| 66 | OT_Bitcode, |
| 67 | OT_Nothing, |
| 68 | OT_Object, |
| 69 | |
| 70 | OT_Default = OT_Bitcode |
| 71 | } OutputType; |
| 72 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 73 | private: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 74 | // The diagnostics engine instance (for status reporting during compilation) |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 75 | llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics; |
| 76 | // The clients of diagnostics engine. The ownership is taken by the |
| 77 | // mDiagnostics after creation. |
| 78 | DiagnosticBuffer *mDiagClient; |
| 79 | void createDiagnostic(); |
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 target being compiled for |
| 82 | clang::TargetOptions mTargetOpts; |
| 83 | llvm::OwningPtr<clang::TargetInfo> mTarget; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 84 | void createTarget(const std::string &Triple, const std::string &CPU, |
| 85 | const std::vector<std::string> &Features); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 86 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 87 | // Below is for parsing and code generation |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 88 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 89 | // The file manager (for prepocessor doing the job such as header file search) |
| 90 | llvm::OwningPtr<clang::FileManager> mFileMgr; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 91 | void createFileManager(); |
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 | // The source manager (responsible for the source code handling) |
| 94 | llvm::OwningPtr<clang::SourceManager> mSourceMgr; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 95 | void createSourceManager(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 96 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 97 | // The preprocessor (source code preprocessor) |
| 98 | llvm::OwningPtr<clang::Preprocessor> mPP; |
| 99 | void createPreprocessor(); |
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 | // The AST context (the context to hold long-lived AST nodes) |
| 102 | llvm::OwningPtr<clang::ASTContext> mASTContext; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 103 | void createASTContext(); |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 104 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | // The AST consumer, responsible for code generation |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 106 | llvm::OwningPtr<clang::ASTConsumer> mBackend; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 107 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 108 | // Input file name |
| 109 | std::string mInputFileName; |
| 110 | std::string mOutputFileName; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 111 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 112 | std::string mDepOutputFileName; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 113 | std::string mDepTargetBCFileName; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 114 | std::vector<std::string> mAdditionalDepTargets; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 115 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 116 | OutputType mOT; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 117 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 118 | // Output stream |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 119 | llvm::OwningPtr<llvm::tool_output_file> mOS; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 120 | // Dependency output stream |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 121 | llvm::OwningPtr<llvm::tool_output_file> mDOS; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 122 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 123 | std::vector<std::string> mIncludePaths; |
Ying Wang | e2e522f | 2010-09-01 13:24:01 -0700 | [diff] [blame] | 124 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 125 | protected: |
| 126 | PragmaList mPragmas; |
| 127 | |
| 128 | inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; } |
| 129 | inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; } |
| 130 | inline clang::FileManager &getFileManager() { return *mFileMgr; } |
| 131 | inline clang::SourceManager &getSourceManager() { return *mSourceMgr; } |
| 132 | inline clang::Preprocessor &getPreprocessor() { return *mPP; } |
| 133 | inline clang::ASTContext &getASTContext() { return *mASTContext; } |
| 134 | |
| 135 | inline const clang::TargetOptions &getTargetOptions() const |
| 136 | { return mTargetOpts; } |
| 137 | |
| 138 | virtual void initDiagnostic() {} |
| 139 | virtual void initPreprocessor() {} |
| 140 | virtual void initASTContext() {} |
| 141 | |
| 142 | virtual clang::ASTConsumer |
| 143 | *createBackend(const clang::CodeGenOptions& CodeGenOpts, |
| 144 | llvm::raw_ostream *OS, |
| 145 | OutputType OT); |
| 146 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 147 | public: |
| 148 | static const std::string TargetDescription; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 149 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 150 | static const llvm::StringRef PragmaMetadataName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 151 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 152 | static void GlobalInitialization(); |
| 153 | |
| 154 | Slang(const std::string &Triple, const std::string &CPU, |
| 155 | const std::vector<std::string> &Features); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 156 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 157 | bool setInputSource(llvm::StringRef InputFile, const char *Text, |
| 158 | size_t TextLength); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 159 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 160 | bool setInputSource(llvm::StringRef InputFile); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 161 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 162 | inline const std::string &getInputFileName() const { return mInputFileName; } |
Ying Wang | e2e522f | 2010-09-01 13:24:01 -0700 | [diff] [blame] | 163 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 164 | inline void setIncludePaths(const std::vector<std::string> &IncludePaths) { |
| 165 | mIncludePaths = IncludePaths; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 166 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 167 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 168 | inline void setOutputType(OutputType OT) { mOT = OT; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 169 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 170 | bool setOutput(const char *OutputFile); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 171 | inline const std::string &getOutputFileName() const { |
| 172 | return mOutputFileName; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 173 | } |
Kirk Stewart | 1fd8579 | 2010-07-07 09:51:23 -0700 | [diff] [blame] | 174 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 175 | bool setDepOutput(const char *OutputFile); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 176 | inline void setDepTargetBC(const char *TargetBCFile) { |
| 177 | mDepTargetBCFileName = TargetBCFile; |
| 178 | } |
| 179 | inline void setAdditionalDepTargets( |
| 180 | const std::vector<std::string> &AdditionalDepTargets) { |
| 181 | mAdditionalDepTargets = AdditionalDepTargets; |
| 182 | } |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 183 | |
| 184 | int generateDepFile(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 185 | int compile(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 186 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 187 | inline const char *getErrorMessage() { return mDiagClient->str().c_str(); } |
Shih-wei Liao | 4c9f742 | 2010-08-05 04:30:02 -0700 | [diff] [blame] | 188 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 189 | // Reset the slang compiler state such that it can be reused to compile |
| 190 | // another file |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 191 | virtual void reset(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 192 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 193 | virtual ~Slang(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 194 | }; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 195 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 196 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 197 | #endif // _SLANG_COMPILER_SLANG_H |