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 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 17 | #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ // NOLINT |
| 18 | #define _FRAMEWORKS_COMPILE_SLANG_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 "clang/Basic/TargetOptions.h" |
| 25 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 26 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 27 | #include "llvm/ADT/OwningPtr.h" |
| 28 | #include "llvm/ADT/StringRef.h" |
| 29 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 30 | #include "slang_diagnostic_buffer.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 31 | #include "slang_pragma_recorder.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; |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame^] | 40 | class FileSystemOptions; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 41 | class SourceManager; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 42 | class LangOptions; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 43 | class Preprocessor; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 44 | class TargetOptions; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 45 | class CodeGenOptions; |
| 46 | class ASTContext; |
| 47 | class ASTConsumer; |
| 48 | class Backend; |
| 49 | class TargetInfo; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 51 | |
| 52 | namespace slang { |
| 53 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 54 | class Slang { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 55 | static clang::LangOptions LangOpts; |
| 56 | static clang::CodeGenOptions CodeGenOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 57 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 58 | static bool GlobalInitialized; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 59 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 60 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 61 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 62 | public: |
| 63 | typedef enum { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 64 | OT_Dependency, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 65 | OT_Assembly, |
| 66 | OT_LLVMAssembly, |
| 67 | OT_Bitcode, |
| 68 | OT_Nothing, |
| 69 | OT_Object, |
| 70 | |
| 71 | OT_Default = OT_Bitcode |
| 72 | } OutputType; |
| 73 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 74 | private: |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 75 | bool mInitialized; |
| 76 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 77 | // The diagnostics engine instance (for status reporting during compilation) |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 78 | llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics; |
| 79 | // The clients of diagnostics engine. The ownership is taken by the |
| 80 | // mDiagnostics after creation. |
| 81 | DiagnosticBuffer *mDiagClient; |
| 82 | void createDiagnostic(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 83 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 84 | // The target being compiled for |
| 85 | clang::TargetOptions mTargetOpts; |
| 86 | llvm::OwningPtr<clang::TargetInfo> mTarget; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 87 | void createTarget(const std::string &Triple, const std::string &CPU, |
| 88 | const std::vector<std::string> &Features); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 89 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 90 | // Below is for parsing and code generation |
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 | // The file manager (for prepocessor doing the job such as header file search) |
| 93 | llvm::OwningPtr<clang::FileManager> mFileMgr; |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame^] | 94 | llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 95 | void createFileManager(); |
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 source manager (responsible for the source code handling) |
| 98 | llvm::OwningPtr<clang::SourceManager> mSourceMgr; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 99 | void createSourceManager(); |
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 preprocessor (source code preprocessor) |
| 102 | llvm::OwningPtr<clang::Preprocessor> mPP; |
| 103 | void createPreprocessor(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 104 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | // The AST context (the context to hold long-lived AST nodes) |
| 106 | llvm::OwningPtr<clang::ASTContext> mASTContext; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 107 | void createASTContext(); |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 108 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 109 | // The AST consumer, responsible for code generation |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 110 | llvm::OwningPtr<clang::ASTConsumer> mBackend; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 111 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 112 | // Input file name |
| 113 | std::string mInputFileName; |
| 114 | std::string mOutputFileName; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 115 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 116 | std::string mDepOutputFileName; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 117 | std::string mDepTargetBCFileName; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 118 | std::vector<std::string> mAdditionalDepTargets; |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 119 | std::vector<std::string> mGeneratedFileNames; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 120 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 121 | OutputType mOT; |
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 | // Output stream |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 124 | llvm::OwningPtr<llvm::tool_output_file> mOS; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 125 | // Dependency output stream |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 126 | llvm::OwningPtr<llvm::tool_output_file> mDOS; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 127 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 128 | std::vector<std::string> mIncludePaths; |
Ying Wang | e2e522f | 2010-09-01 13:24:01 -0700 | [diff] [blame] | 129 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 130 | protected: |
| 131 | PragmaList mPragmas; |
| 132 | |
| 133 | inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; } |
| 134 | inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; } |
| 135 | inline clang::FileManager &getFileManager() { return *mFileMgr; } |
| 136 | inline clang::SourceManager &getSourceManager() { return *mSourceMgr; } |
| 137 | inline clang::Preprocessor &getPreprocessor() { return *mPP; } |
| 138 | inline clang::ASTContext &getASTContext() { return *mASTContext; } |
| 139 | |
| 140 | inline const clang::TargetOptions &getTargetOptions() const |
| 141 | { return mTargetOpts; } |
| 142 | |
| 143 | virtual void initDiagnostic() {} |
| 144 | virtual void initPreprocessor() {} |
| 145 | virtual void initASTContext() {} |
| 146 | |
| 147 | virtual clang::ASTConsumer |
| 148 | *createBackend(const clang::CodeGenOptions& CodeGenOpts, |
| 149 | llvm::raw_ostream *OS, |
| 150 | OutputType OT); |
| 151 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 152 | public: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 153 | static const llvm::StringRef PragmaMetadataName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 154 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 155 | static void GlobalInitialization(); |
| 156 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 157 | Slang(); |
| 158 | |
| 159 | void init(const std::string &Triple, const std::string &CPU, |
| 160 | const std::vector<std::string> &Features); |
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 | bool setInputSource(llvm::StringRef InputFile, const char *Text, |
| 163 | size_t TextLength); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 164 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 165 | bool setInputSource(llvm::StringRef InputFile); |
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 | inline const std::string &getInputFileName() const { return mInputFileName; } |
Ying Wang | e2e522f | 2010-09-01 13:24:01 -0700 | [diff] [blame] | 168 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 169 | inline void setIncludePaths(const std::vector<std::string> &IncludePaths) { |
| 170 | mIncludePaths = IncludePaths; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 171 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 172 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 173 | inline void setOutputType(OutputType OT) { mOT = OT; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 174 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 175 | bool setOutput(const char *OutputFile); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 176 | inline const std::string &getOutputFileName() const { |
| 177 | return mOutputFileName; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 178 | } |
Kirk Stewart | 1fd8579 | 2010-07-07 09:51:23 -0700 | [diff] [blame] | 179 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 180 | bool setDepOutput(const char *OutputFile); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 181 | inline void setDepTargetBC(const char *TargetBCFile) { |
| 182 | mDepTargetBCFileName = TargetBCFile; |
| 183 | } |
| 184 | inline void setAdditionalDepTargets( |
| 185 | const std::vector<std::string> &AdditionalDepTargets) { |
| 186 | mAdditionalDepTargets = AdditionalDepTargets; |
| 187 | } |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 188 | inline void appendGeneratedFileName( |
| 189 | const std::string &GeneratedFileName) { |
| 190 | mGeneratedFileNames.push_back(GeneratedFileName); |
| 191 | } |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 192 | |
| 193 | int generateDepFile(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 194 | int compile(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 195 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 196 | inline const char *getErrorMessage() { return mDiagClient->str().c_str(); } |
Shih-wei Liao | 4c9f742 | 2010-08-05 04:30:02 -0700 | [diff] [blame] | 197 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 198 | // Reset the slang compiler state such that it can be reused to compile |
| 199 | // another file |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 200 | virtual void reset(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 201 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 202 | virtual ~Slang(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 203 | }; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 204 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 205 | } // namespace slang |
| 206 | |
| 207 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ NOLINT |