slang.h revision 9ae18b2bbee0b08afd400542e863dd665ff76059
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 17#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ // NOLINT 18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ 19 20#include <cstdio> 21#include <string> 22#include <vector> 23 24// Terrible workaround for TargetOptions.h not using llvm::RefCountedBase! 25#include "llvm/ADT/IntrusiveRefCntPtr.h" 26using llvm::RefCountedBase; 27 28#include "clang/Basic/TargetOptions.h" 29#include "clang/Lex/ModuleLoader.h" 30 31#include "llvm/ADT/OwningPtr.h" 32#include "llvm/ADT/StringRef.h" 33 34#include "llvm/Target/TargetMachine.h" 35 36#include "slang_diagnostic_buffer.h" 37#include "slang_pragma_recorder.h" 38 39namespace llvm { 40 class tool_output_file; 41} 42 43namespace clang { 44 class ASTConsumer; 45 class ASTContext; 46 class Backend; 47 class CodeGenOptions; 48 class Diagnostic; 49 class DiagnosticsEngine; 50 class FileManager; 51 class FileSystemOptions; 52 class LangOptions; 53 class Preprocessor; 54 class SourceManager; 55 class TargetInfo; 56} // namespace clang 57 58namespace slang { 59 60class Slang : public clang::ModuleLoader { 61 static clang::LangOptions LangOpts; 62 static clang::CodeGenOptions CodeGenOpts; 63 64 static bool GlobalInitialized; 65 66 static void LLVMErrorHandler(void *UserData, const std::string &Message, 67 bool GenCrashDialog); 68 69 public: 70 enum OutputType { 71 OT_Dependency, 72 OT_Assembly, 73 OT_LLVMAssembly, 74 OT_Bitcode, 75 OT_Nothing, 76 OT_Object, 77 78 OT_Default = OT_Bitcode 79 }; 80 81 private: 82 bool mInitialized; 83 84 // Diagnostics Mediator (An interface for both Producer and Consumer) 85 llvm::OwningPtr<clang::Diagnostic> mDiag; 86 87 // Diagnostics Engine (Producer and Diagnostics Reporter) 88 clang::DiagnosticsEngine *mDiagEngine; 89 90 // Diagnostics Consumer 91 // NOTE: The ownership is taken by mDiagEngine after creation. 92 DiagnosticBuffer *mDiagClient; 93 94 // The target being compiled for 95 llvm::IntrusiveRefCntPtr<clang::TargetOptions> mTargetOpts; 96 llvm::OwningPtr<clang::TargetInfo> mTarget; 97 void createTarget(uint32_t BitWidth); 98 99 100 // File manager (for prepocessor doing the job such as header file search) 101 llvm::OwningPtr<clang::FileManager> mFileMgr; 102 llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt; 103 void createFileManager(); 104 105 106 // Source manager (responsible for the source code handling) 107 llvm::OwningPtr<clang::SourceManager> mSourceMgr; 108 void createSourceManager(); 109 110 111 // Preprocessor (source code preprocessor) 112 llvm::OwningPtr<clang::Preprocessor> mPP; 113 void createPreprocessor(); 114 115 116 // AST context (the context to hold long-lived AST nodes) 117 llvm::OwningPtr<clang::ASTContext> mASTContext; 118 void createASTContext(); 119 120 121 // AST consumer, responsible for code generation 122 llvm::OwningPtr<clang::ASTConsumer> mBackend; 123 124 125 // File names 126 std::string mInputFileName; 127 std::string mOutputFileName; 128 std::string mOutput32FileName; 129 130 std::string mDepOutputFileName; 131 std::string mDepTargetBCFileName; 132 std::vector<std::string> mAdditionalDepTargets; 133 std::vector<std::string> mGeneratedFileNames; 134 135 OutputType mOT; 136 137 // Output stream 138 llvm::OwningPtr<llvm::tool_output_file> mOS; 139 140 // Dependency output stream 141 llvm::OwningPtr<llvm::tool_output_file> mDOS; 142 143 std::vector<std::string> mIncludePaths; 144 145 protected: 146 PragmaList mPragmas; 147 148 clang::DiagnosticsEngine &getDiagnostics() { return *mDiagEngine; } 149 clang::TargetInfo const &getTargetInfo() const { return *mTarget; } 150 clang::FileManager &getFileManager() { return *mFileMgr; } 151 clang::SourceManager &getSourceManager() { return *mSourceMgr; } 152 clang::Preprocessor &getPreprocessor() { return *mPP; } 153 clang::ASTContext &getASTContext() { return *mASTContext; } 154 155 inline clang::TargetOptions const &getTargetOptions() const 156 { return *mTargetOpts.getPtr(); } 157 158 virtual void initDiagnostic() {} 159 virtual void initPreprocessor() {} 160 virtual void initASTContext() {} 161 162 virtual clang::ASTConsumer * 163 createBackend(const clang::CodeGenOptions& CodeGenOpts, 164 llvm::raw_ostream *OS, 165 OutputType OT); 166 167 public: 168 static const llvm::StringRef PragmaMetadataName; 169 170 static void GlobalInitialization(); 171 172 Slang(); 173 174 void init(uint32_t BitWidth, clang::DiagnosticsEngine *DiagEngine, 175 DiagnosticBuffer *DiagClient); 176 177 virtual clang::ModuleLoadResult loadModule( 178 clang::SourceLocation ImportLoc, 179 clang::ModuleIdPath Path, 180 clang::Module::NameVisibilityKind VK, 181 bool IsInclusionDirective); 182 183 bool setInputSource(llvm::StringRef InputFile, const char *Text, 184 size_t TextLength); 185 186 bool setInputSource(llvm::StringRef InputFile); 187 188 std::string const &getInputFileName() const { return mInputFileName; } 189 190 void setIncludePaths(const std::vector<std::string> &IncludePaths) { 191 mIncludePaths = IncludePaths; 192 } 193 194 void setOutputType(OutputType OT) { mOT = OT; } 195 196 bool setOutput(const char *OutputFile); 197 198 // For use with 64-bit compilation/reflection. This only sets the filename of 199 // the 32-bit bitcode file, and doesn't actually verify it already exists. 200 void setOutput32(const char *OutputFile) { 201 mOutput32FileName = OutputFile; 202 } 203 204 std::string const &getOutputFileName() const { 205 return mOutputFileName; 206 } 207 208 std::string const &getOutput32FileName() const { 209 return mOutput32FileName; 210 } 211 212 bool setDepOutput(const char *OutputFile); 213 214 void setDepTargetBC(const char *TargetBCFile) { 215 mDepTargetBCFileName = TargetBCFile; 216 } 217 218 void setAdditionalDepTargets( 219 std::vector<std::string> const &AdditionalDepTargets) { 220 mAdditionalDepTargets = AdditionalDepTargets; 221 } 222 223 void appendGeneratedFileName(std::string const &GeneratedFileName) { 224 mGeneratedFileNames.push_back(GeneratedFileName); 225 } 226 227 int generateDepFile(); 228 229 int compile(); 230 231 char const *getErrorMessage() { return mDiagClient->str().c_str(); } 232 233 void setDebugMetadataEmission(bool EmitDebug); 234 235 void setOptimizationLevel(llvm::CodeGenOpt::Level OptimizationLevel); 236 237 // Reset the slang compiler state such that it can be reused to compile 238 // another file 239 virtual void reset(); 240 241 virtual ~Slang(); 242}; 243 244} // namespace slang 245 246#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ NOLINT 247