Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1 | #ifndef _SLANG_COMPILER_SLANG_HPP |
| 2 | # define _SLANG_COMPILER_SLANG_HPP |
| 3 | |
| 4 | #include "slang_backend.hpp" |
| 5 | #include "slang_rs_context.hpp" |
| 6 | #include "slang_rs_backend.hpp" |
| 7 | #include "slang_pragma_recorder.hpp" |
| 8 | #include "slang_diagnostic_buffer.hpp" |
| 9 | |
| 10 | #include <cstdio> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "llvm/Support/raw_ostream.h" /* for class llvm::raw_ostream */ |
| 14 | |
| 15 | #include "llvm/ADT/OwningPtr.h" /* for class llvm::OwningPtr */ |
| 16 | #include "llvm/ADT/StringRef.h" /* for class llvm::StringRef */ |
| 17 | |
| 18 | #include "clang/AST/ASTConsumer.h" /* for class clang::ASTConsumer */ |
| 19 | #include "clang/AST/ASTContext.h" /* for class clang::ASTContext */ |
| 20 | |
| 21 | #include "clang/Lex/Preprocessor.h" /* for class clang::Preprocessor */ |
| 22 | #include "clang/Lex/HeaderSearch.h" /* for class clang::HeaderSearch */ |
| 23 | |
| 24 | #include "clang/Basic/Diagnostic.h" /* for class clang::Diagnostic, class clang::DiagnosticClient, class clang::DiagnosticInfo */ |
Kirk Stewart | b0cadb3 | 2010-07-20 17:26:06 -0700 | [diff] [blame] | 25 | #include "clang/Sema/SemaDiagnostic.h" /* for members of clang::diag */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 26 | #include "clang/Basic/FileManager.h" /* for class clang::FileManager and class clang::FileEntry */ |
| 27 | #include "clang/Basic/TargetOptions.h" /* for class clang::TargetOptions */ |
| 28 | |
| 29 | namespace llvm { |
| 30 | |
| 31 | class Twine; |
| 32 | class TargetInfo; |
| 33 | |
| 34 | } /* namespace llvm */ |
| 35 | |
| 36 | namespace clang { |
| 37 | |
| 38 | class LangOptions; |
| 39 | class CodeGenOptions; |
| 40 | |
| 41 | } /* namespace clang */ |
| 42 | |
| 43 | namespace slang { |
| 44 | |
| 45 | using namespace clang; |
| 46 | |
| 47 | class Slang { |
| 48 | static LangOptions LangOpts; |
| 49 | static CodeGenOptions CodeGenOpts; |
| 50 | |
| 51 | static bool GlobalInitialized; |
| 52 | |
| 53 | static void GlobalInitialization(); |
| 54 | |
| 55 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
| 56 | |
| 57 | private: |
| 58 | PragmaList mPragmas; |
| 59 | |
| 60 | /* The diagnostics engine instance (for status reporting during compilation) */ |
| 61 | llvm::OwningPtr<Diagnostic> mDiagnostics; |
| 62 | |
| 63 | llvm::OwningPtr<DiagnosticBuffer> mDiagClient; |
| 64 | inline void createDiagnostic() { |
| 65 | mDiagClient.reset(new DiagnosticBuffer()); |
| 66 | mDiagnostics.reset(new Diagnostic(mDiagClient.get())); |
Kirk Stewart | 6322c93 | 2010-06-10 16:34:34 -0700 | [diff] [blame] | 67 | bool optionNotFound = mDiagnostics->setDiagnosticGroupMapping("implicit-function-declaration", clang::diag::MAP_ERROR); |
| 68 | assert(!optionNotFound && "Unable find option group implicit-function-declaration"); |
Kirk Stewart | b0cadb3 | 2010-07-20 17:26:06 -0700 | [diff] [blame] | 69 | mDiagnostics->setDiagnosticMapping(clang::diag::ext_typecheck_convert_discards_qualifiers, clang::diag::MAP_ERROR); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
| 73 | /* The target being compiled for */ |
| 74 | TargetOptions mTargetOpts; |
| 75 | llvm::OwningPtr<TargetInfo> mTarget; |
| 76 | void createTarget(const char* Triple, const char* CPU, const char** Features); |
| 77 | |
| 78 | /**** Below is for parsing ****/ |
| 79 | |
| 80 | /* The file manager (for prepocessor doing the job such as header file search) */ |
| 81 | llvm::OwningPtr<FileManager> mFileMgr; |
| 82 | inline void createFileManager() { mFileMgr.reset(new FileManager()); return; } |
| 83 | |
| 84 | /* The source manager (responsible for the source code handling) */ |
| 85 | llvm::OwningPtr<SourceManager> mSourceMgr; /* The source manager */ |
| 86 | inline void createSourceManager() { mSourceMgr.reset(new SourceManager(*mDiagnostics)); return; } |
| 87 | |
| 88 | /* The preprocessor (source code preprocessor) */ |
| 89 | llvm::OwningPtr<Preprocessor> mPP; |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 90 | void createPreprocessor(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 91 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 92 | /* The AST context (the context to hold long-lived AST nodes) */ |
| 93 | llvm::OwningPtr<ASTContext> mASTContext; |
| 94 | inline void createASTContext() { |
| 95 | mASTContext.reset(new ASTContext(LangOpts, |
| 96 | *mSourceMgr, |
| 97 | *mTarget, |
| 98 | mPP->getIdentifierTable(), |
| 99 | mPP->getSelectorTable(), |
| 100 | mPP->getBuiltinInfo())); |
| 101 | return; |
| 102 | } |
| 103 | |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 104 | /* Context for RenderScript */ |
| 105 | llvm::OwningPtr<RSContext> mRSContext; |
| 106 | inline void createRSContext() { |
| 107 | mRSContext.reset(new RSContext(mPP.get(), |
| 108 | mASTContext.get(), |
| 109 | mTarget.get())); |
| 110 | return; |
| 111 | } |
| 112 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 113 | /* The AST consumer, responsible for code generation */ |
| 114 | llvm::OwningPtr<Backend> mBackend; |
| 115 | inline void createBackend() { |
| 116 | mBackend.reset(new Backend(*mDiagnostics, |
| 117 | CodeGenOpts, |
| 118 | mTargetOpts, |
| 119 | mPragmas, |
| 120 | mOS.take(), |
Kirk Stewart | 6b22674 | 2010-06-11 10:51:12 -0700 | [diff] [blame] | 121 | mOutputType, |
Kirk Stewart | 1fd8579 | 2010-07-07 09:51:23 -0700 | [diff] [blame] | 122 | *mSourceMgr, |
| 123 | mAllowRSPrefix)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 124 | |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | inline void createRSBackend() { |
| 129 | mBackend.reset(new RSBackend(mRSContext.get(), |
| 130 | *mDiagnostics, |
| 131 | CodeGenOpts, |
| 132 | mTargetOpts, |
| 133 | mPragmas, |
| 134 | mOS.take(), |
Kirk Stewart | 6b22674 | 2010-06-11 10:51:12 -0700 | [diff] [blame] | 135 | mOutputType, |
Kirk Stewart | 1fd8579 | 2010-07-07 09:51:23 -0700 | [diff] [blame] | 136 | *mSourceMgr, |
| 137 | mAllowRSPrefix)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 138 | |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | /* Input file name */ |
| 143 | std::string mInputFileName; |
| 144 | std::string mOutputFileName; |
| 145 | |
| 146 | SlangCompilerOutputTy mOutputType; |
| 147 | |
| 148 | /* Output stream */ |
| 149 | llvm::OwningPtr<llvm::raw_ostream> mOS; |
| 150 | |
Kirk Stewart | 1fd8579 | 2010-07-07 09:51:23 -0700 | [diff] [blame] | 151 | bool mAllowRSPrefix; |
| 152 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 153 | public: |
| 154 | static const std::string TargetDescription; |
| 155 | |
| 156 | static const llvm::Twine PragmaMetadataName; |
| 157 | |
| 158 | Slang(const char* Triple, const char* CPU, const char** Features); |
| 159 | |
| 160 | bool setInputSource(llvm::StringRef inputFile, const char* text, size_t textLength); |
| 161 | |
| 162 | bool setInputSource(llvm::StringRef inputFile); |
| 163 | |
| 164 | void setOutputType(SlangCompilerOutputTy outputType); |
| 165 | |
| 166 | inline bool setOutput(FILE* stream) { |
| 167 | if(stream == NULL) |
| 168 | return false; |
| 169 | |
| 170 | mOS.reset( new llvm::raw_fd_ostream(fileno(stream), /* shouldClose */false) ); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | bool setOutput(const char* outputFile); |
| 175 | |
Kirk Stewart | 1fd8579 | 2010-07-07 09:51:23 -0700 | [diff] [blame] | 176 | inline void allowRSPrefix() { |
| 177 | mAllowRSPrefix = true; |
| 178 | } |
| 179 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 180 | int compile(); |
| 181 | |
| 182 | bool reflectToJava(const char* outputPackageName); |
Shih-wei Liao | 6de8927 | 2010-07-15 15:26:20 -0700 | [diff] [blame] | 183 | bool reflectToJavaPath(const char* outputPathName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 184 | |
| 185 | inline const char* getErrorMessage() { |
| 186 | return mDiagClient->str().c_str(); |
| 187 | } |
| 188 | |
| 189 | void getPragmas(size_t* actualStringCount, size_t maxStringCount, char** strings); |
| 190 | |
Shih-wei Liao | 4c9f742 | 2010-08-05 04:30:02 -0700 | [diff] [blame^] | 191 | const char* exportFuncs(); |
| 192 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 193 | /* Reset the slang compiler state such that it can be reused to compile another file */ |
| 194 | inline void reset() { |
| 195 | /* Seems there's no way to clear the diagnostics. We just re-create it. */ |
| 196 | createDiagnostic(); |
| 197 | mOutputType = SlangCompilerOutput_Default; |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | ~Slang(); |
| 202 | }; /* class Slang */ |
| 203 | |
| 204 | } /* namespace slang */ |
| 205 | |
| 206 | #endif /* _SLANG_COMPILER_SLANG_HPP */ |