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