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 | #include "slang.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 21 | #include <cstring> |
| 22 | #include <list> |
| 23 | #include <sstream> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 24 | #include <string> |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 25 | #include <utility> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
| 28 | #include "clang/AST/ASTConsumer.h" |
| 29 | #include "clang/AST/ASTContext.h" |
| 30 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 31 | #include "clang/Basic/DiagnosticIDs.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 32 | #include "clang/Basic/DiagnosticOptions.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 33 | #include "clang/Basic/FileManager.h" |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame] | 34 | #include "clang/Basic/FileSystemOptions.h" |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 35 | #include "clang/Basic/SourceLocation.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 36 | #include "clang/Basic/SourceManager.h" |
| 37 | #include "clang/Basic/TargetInfo.h" |
| 38 | #include "clang/Basic/TargetOptions.h" |
| 39 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 40 | #include "clang/Frontend/DependencyOutputOptions.h" |
| 41 | #include "clang/Frontend/FrontendDiagnostic.h" |
Stephen Hines | 0e4ee65 | 2014-06-10 14:32:26 -0700 | [diff] [blame] | 42 | #include "clang/Frontend/FrontendOptions.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 43 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 44 | #include "clang/Frontend/Utils.h" |
| 45 | |
| 46 | #include "clang/Lex/Preprocessor.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 47 | #include "clang/Lex/PreprocessorOptions.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 48 | #include "clang/Lex/HeaderSearch.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 49 | #include "clang/Lex/HeaderSearchOptions.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 50 | |
| 51 | #include "clang/Parse/ParseAST.h" |
| 52 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 53 | #include "clang/Sema/SemaDiagnostic.h" |
| 54 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 55 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 56 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 57 | #include "llvm/Bitcode/ReaderWriter.h" |
| 58 | |
| 59 | // More force linking |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 60 | #include "llvm/Linker/Linker.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 61 | |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 62 | // Force linking all passes/vmcore stuffs to libslang.so |
Stephen Hines | 0da7f6c | 2013-03-05 15:19:02 -0800 | [diff] [blame] | 63 | #include "llvm/LinkAllIR.h" |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 64 | #include "llvm/LinkAllPasses.h" |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 65 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 66 | #include "llvm/Support/raw_ostream.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 67 | #include "llvm/Support/MemoryBuffer.h" |
| 68 | #include "llvm/Support/ErrorHandling.h" |
| 69 | #include "llvm/Support/ManagedStatic.h" |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 70 | #include "llvm/Support/Path.h" |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 71 | #include "llvm/Support/TargetSelect.h" |
| 72 | #include "llvm/Support/ToolOutputFile.h" |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 73 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 74 | #include "os_sep.h" |
| 75 | #include "rs_cc_options.h" |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 76 | #include "slang_assert.h" |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 77 | #include "slang_backend.h" |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 78 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 79 | #include "slang_rs_context.h" |
| 80 | #include "slang_rs_export_type.h" |
| 81 | |
| 82 | #include "slang_rs_reflection.h" |
| 83 | #include "slang_rs_reflection_cpp.h" |
| 84 | |
| 85 | |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 86 | namespace { |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 87 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 88 | static const char *kRSTriple32 = "armv7-none-linux-gnueabi"; |
| 89 | static const char *kRSTriple64 = "aarch64-none-linux-gnueabi"; |
| 90 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 91 | } // namespace |
| 92 | |
| 93 | namespace slang { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 94 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 95 | |
| 96 | #define FS_SUFFIX "fs" |
| 97 | |
| 98 | #define RS_HEADER_SUFFIX "rsh" |
| 99 | |
| 100 | /* RS_HEADER_ENTRY(name) */ |
| 101 | #define ENUM_RS_HEADER() \ |
| 102 | RS_HEADER_ENTRY(rs_allocation_data) \ |
| 103 | RS_HEADER_ENTRY(rs_atomic) \ |
| 104 | RS_HEADER_ENTRY(rs_convert) \ |
| 105 | RS_HEADER_ENTRY(rs_core) \ |
| 106 | RS_HEADER_ENTRY(rs_debug) \ |
| 107 | RS_HEADER_ENTRY(rs_for_each) \ |
| 108 | RS_HEADER_ENTRY(rs_graphics) \ |
| 109 | RS_HEADER_ENTRY(rs_graphics_types) \ |
| 110 | RS_HEADER_ENTRY(rs_io) \ |
| 111 | RS_HEADER_ENTRY(rs_math) \ |
| 112 | RS_HEADER_ENTRY(rs_matrix) \ |
| 113 | RS_HEADER_ENTRY(rs_object_info) \ |
| 114 | RS_HEADER_ENTRY(rs_object_types) \ |
| 115 | RS_HEADER_ENTRY(rs_quaternion) \ |
| 116 | RS_HEADER_ENTRY(rs_time) \ |
| 117 | RS_HEADER_ENTRY(rs_value_types) \ |
| 118 | RS_HEADER_ENTRY(rs_vector_math) \ |
| 119 | |
| 120 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 121 | // The named of metadata node that pragma resides (should be synced with |
| 122 | // bcc.cpp) |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 123 | const llvm::StringRef Slang::PragmaMetadataName = "#pragma"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 124 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 125 | static inline llvm::tool_output_file * |
| 126 | OpenOutputFile(const char *OutputFile, |
Stephen Hines | a1f95ee | 2013-08-09 01:26:08 -0700 | [diff] [blame] | 127 | llvm::sys::fs::OpenFlags Flags, |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 128 | std::error_code &EC, |
Stephen Hines | e67239d | 2012-02-24 15:08:36 -0800 | [diff] [blame] | 129 | clang::DiagnosticsEngine *DiagEngine) { |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 130 | slangAssert((OutputFile != nullptr) && |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 131 | (DiagEngine != nullptr) && "Invalid parameter!"); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 132 | |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 133 | EC = llvm::sys::fs::create_directories( |
| 134 | llvm::sys::path::parent_path(OutputFile)); |
| 135 | if (!EC) { |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 136 | llvm::tool_output_file *F = |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 137 | new llvm::tool_output_file(OutputFile, EC, Flags); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 138 | if (F != nullptr) |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 139 | return F; |
| 140 | } |
| 141 | |
| 142 | // Report error here. |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 143 | DiagEngine->Report(clang::diag::err_fe_error_opening) |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 144 | << OutputFile << EC.message(); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 145 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 146 | return nullptr; |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 147 | } |
| 148 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 149 | void Slang::createTarget(uint32_t BitWidth) { |
| 150 | std::vector<std::string> features; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 151 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 152 | if (BitWidth == 64) { |
| 153 | mTargetOpts->Triple = kRSTriple64; |
| 154 | } else { |
| 155 | mTargetOpts->Triple = kRSTriple32; |
| 156 | // Treat long as a 64-bit type for our 32-bit RS code. |
| 157 | features.push_back("+long64"); |
| 158 | mTargetOpts->FeaturesAsWritten = features; |
| 159 | } |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 160 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 161 | mTarget.reset(clang::TargetInfo::CreateTargetInfo(*mDiagEngine, |
Stephen Hines | 7ac9d0d | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 162 | mTargetOpts)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 165 | void Slang::createFileManager() { |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame] | 166 | mFileSysOpt.reset(new clang::FileSystemOptions()); |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 167 | mFileMgr.reset(new clang::FileManager(*mFileSysOpt)); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void Slang::createSourceManager() { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 171 | mSourceMgr.reset(new clang::SourceManager(*mDiagEngine, *mFileMgr)); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 174 | void Slang::createPreprocessor() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 175 | // Default only search header file in current dir |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 176 | llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> HSOpts = |
| 177 | new clang::HeaderSearchOptions(); |
| 178 | clang::HeaderSearch *HeaderInfo = new clang::HeaderSearch(HSOpts, |
Stephen Hines | eeb251e | 2014-02-11 02:08:05 -0800 | [diff] [blame] | 179 | *mSourceMgr, |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 180 | *mDiagEngine, |
| 181 | LangOpts, |
| 182 | mTarget.get()); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 183 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 184 | llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions> PPOpts = |
| 185 | new clang::PreprocessorOptions(); |
| 186 | mPP.reset(new clang::Preprocessor(PPOpts, |
| 187 | *mDiagEngine, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 188 | LangOpts, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 189 | *mSourceMgr, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 190 | *HeaderInfo, |
| 191 | *this, |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 192 | nullptr, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 193 | /* OwnsHeaderSearch = */true)); |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 194 | // Initialize the preprocessor |
Stephen Hines | 75d4718 | 2014-05-28 09:43:18 -0700 | [diff] [blame] | 195 | mPP->Initialize(getTargetInfo()); |
Stephen Hines | 0e4ee65 | 2014-06-10 14:32:26 -0700 | [diff] [blame] | 196 | clang::FrontendOptions FEOpts; |
Stephen Hines | 7ac9d0d | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 197 | clang::InitializePreprocessor(*mPP, *PPOpts, FEOpts); |
Stephen Hines | 0e4ee65 | 2014-06-10 14:32:26 -0700 | [diff] [blame] | 198 | |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 199 | mPragmas.clear(); |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 200 | mPP->AddPragmaHandler(new PragmaRecorder(&mPragmas)); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 201 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 202 | std::vector<clang::DirectoryLookup> SearchList; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 203 | for (unsigned i = 0, e = mIncludePaths.size(); i != e; i++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 204 | if (const clang::DirectoryEntry *DE = |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 205 | mFileMgr->getDirectory(mIncludePaths[i])) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 206 | SearchList.push_back(clang::DirectoryLookup(DE, |
| 207 | clang::SrcMgr::C_System, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 208 | false)); |
| 209 | } |
Shih-wei Liao | 9089828 | 2010-07-19 18:38:57 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 212 | HeaderInfo->SetSearchPaths(SearchList, |
| 213 | /* angledDirIdx = */1, |
| 214 | /* systemDixIdx = */1, |
| 215 | /* noCurDirSearch = */false); |
Shih-wei Liao | 9089828 | 2010-07-19 18:38:57 -0700 | [diff] [blame] | 216 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 217 | initPreprocessor(); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 220 | void Slang::createASTContext() { |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 221 | mASTContext.reset( |
| 222 | new clang::ASTContext(LangOpts, *mSourceMgr, mPP->getIdentifierTable(), |
| 223 | mPP->getSelectorTable(), mPP->getBuiltinInfo())); |
Stephen Hines | 75d4718 | 2014-05-28 09:43:18 -0700 | [diff] [blame] | 224 | mASTContext->InitBuiltinTypes(getTargetInfo()); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 225 | initASTContext(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 228 | clang::ASTConsumer * |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 229 | Slang::createBackend(const clang::CodeGenOptions &CodeGenOpts, |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 230 | llvm::raw_ostream *OS, OutputType OT) { |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 231 | return new Backend(mRSContext, &getDiagnostics(), CodeGenOpts, |
| 232 | getTargetOptions(), &mPragmas, OS, OT, getSourceManager(), |
| 233 | mAllowRSPrefix, mIsFilterscript); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 234 | } |
| 235 | |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 236 | Slang::Slang(uint32_t BitWidth, clang::DiagnosticsEngine *DiagEngine, |
| 237 | DiagnosticBuffer *DiagClient) |
| 238 | : mDiagEngine(DiagEngine), mDiagClient(DiagClient), |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 239 | mTargetOpts(new clang::TargetOptions()), mOT(OT_Default), |
| 240 | mRSContext(nullptr), mAllowRSPrefix(false), mTargetAPI(0), |
| 241 | mVerbose(false), mIsFilterscript(false) { |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 242 | // Please refer to include/clang/Basic/LangOptions.h to setup |
| 243 | // the options. |
| 244 | LangOpts.RTTI = 0; // Turn off the RTTI information support |
| 245 | LangOpts.LineComment = 1; |
| 246 | LangOpts.C99 = 1; |
| 247 | LangOpts.Renderscript = 1; |
| 248 | LangOpts.LaxVectorConversions = 0; // Do not bitcast vectors! |
| 249 | LangOpts.CharIsSigned = 1; // Signed char is our default. |
| 250 | |
| 251 | CodeGenOpts.OptimizationLevel = 3; |
| 252 | |
| 253 | createTarget(BitWidth); |
| 254 | createFileManager(); |
| 255 | createSourceManager(); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 256 | } |
| 257 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 258 | Slang::~Slang() { |
| 259 | delete mRSContext; |
| 260 | for (ReflectedDefinitionListTy::iterator I = ReflectedDefinitions.begin(), |
| 261 | E = ReflectedDefinitions.end(); |
| 262 | I != E; I++) { |
| 263 | delete I->getValue().first; |
| 264 | } |
| 265 | } |
| 266 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 267 | clang::ModuleLoadResult Slang::loadModule( |
| 268 | clang::SourceLocation ImportLoc, |
| 269 | clang::ModuleIdPath Path, |
| 270 | clang::Module::NameVisibilityKind Visibility, |
| 271 | bool IsInclusionDirective) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 272 | slangAssert(0 && "Not implemented"); |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 273 | return clang::ModuleLoadResult(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 276 | bool Slang::setInputSource(llvm::StringRef InputFile) { |
| 277 | mInputFileName = InputFile.str(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 278 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 279 | mSourceMgr->clearIDTables(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 280 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 281 | const clang::FileEntry *File = mFileMgr->getFile(InputFile); |
Stephen Hines | 75d4718 | 2014-05-28 09:43:18 -0700 | [diff] [blame] | 282 | if (File) { |
| 283 | mSourceMgr->setMainFileID(mSourceMgr->createFileID(File, |
| 284 | clang::SourceLocation(), clang::SrcMgr::C_User)); |
| 285 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 286 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 287 | if (mSourceMgr->getMainFileID().isInvalid()) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 288 | mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 289 | return false; |
| 290 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 291 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 292 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 295 | bool Slang::setOutput(const char *OutputFile) { |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 296 | std::error_code EC; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 297 | llvm::tool_output_file *OS = nullptr; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 298 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 299 | switch (mOT) { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 300 | case OT_Dependency: |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 301 | case OT_Assembly: |
| 302 | case OT_LLVMAssembly: { |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 303 | OS = OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, EC, mDiagEngine); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 304 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 305 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 306 | case OT_Nothing: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 307 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 308 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 309 | case OT_Object: |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 310 | case OT_Bitcode: { |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 311 | OS = OpenOutputFile(OutputFile, llvm::sys::fs::F_None, EC, mDiagEngine); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 312 | break; |
| 313 | } |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 314 | default: { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 315 | llvm_unreachable("Unknown compiler output type"); |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 316 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 317 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 318 | |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 319 | if (EC) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 320 | return false; |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 321 | |
| 322 | mOS.reset(OS); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 323 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 324 | mOutputFileName = OutputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 325 | |
| 326 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 329 | bool Slang::setDepOutput(const char *OutputFile) { |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 330 | std::error_code EC; |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 331 | |
Stephen Hines | a1f95ee | 2013-08-09 01:26:08 -0700 | [diff] [blame] | 332 | mDOS.reset( |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 333 | OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, EC, mDiagEngine)); |
| 334 | if (EC || (mDOS.get() == nullptr)) |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 335 | return false; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 336 | |
| 337 | mDepOutputFileName = OutputFile; |
| 338 | |
| 339 | return true; |
| 340 | } |
| 341 | |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 342 | int Slang::generateDepFile() { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 343 | if (mDiagEngine->hasErrorOccurred()) |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 344 | return 1; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 345 | if (mDOS.get() == nullptr) |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 346 | return 1; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 347 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 348 | // Initialize options for generating dependency file |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 349 | clang::DependencyOutputOptions DepOpts; |
| 350 | DepOpts.IncludeSystemHeaders = 1; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 351 | DepOpts.OutputFile = mDepOutputFileName; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 352 | DepOpts.Targets = mAdditionalDepTargets; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 353 | DepOpts.Targets.push_back(mDepTargetBCFileName); |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 354 | for (std::vector<std::string>::const_iterator |
| 355 | I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end(); |
| 356 | I != E; |
| 357 | I++) { |
| 358 | DepOpts.Targets.push_back(*I); |
| 359 | } |
| 360 | mGeneratedFileNames.clear(); |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 361 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 362 | // Per-compilation needed initialization |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 363 | createPreprocessor(); |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 364 | clang::DependencyFileGenerator::CreateAndAttachToPreprocessor(*mPP.get(), DepOpts); |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 365 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 366 | // Inform the diagnostic client we are processing a source file |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 367 | mDiagClient->BeginSourceFile(LangOpts, mPP.get()); |
| 368 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 369 | // Go through the source file (no operations necessary) |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 370 | clang::Token Tok; |
| 371 | mPP->EnterMainSourceFile(); |
| 372 | do { |
| 373 | mPP->Lex(Tok); |
| 374 | } while (Tok.isNot(clang::tok::eof)); |
| 375 | |
| 376 | mPP->EndSourceFile(); |
| 377 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 378 | // Declare success if no error |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 379 | if (!mDiagEngine->hasErrorOccurred()) |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 380 | mDOS->keep(); |
| 381 | |
| 382 | // Clean up after compilation |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 383 | mPP.reset(); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 384 | mDOS.reset(); |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 385 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 386 | return mDiagEngine->hasErrorOccurred() ? 1 : 0; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 389 | int Slang::compile() { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 390 | if (mDiagEngine->hasErrorOccurred()) |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 391 | return 1; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 392 | if (mOS.get() == nullptr) |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 393 | return 1; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 394 | |
| 395 | // Here is per-compilation needed initialization |
| 396 | createPreprocessor(); |
| 397 | createASTContext(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 398 | |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame] | 399 | mBackend.reset(createBackend(CodeGenOpts, &mOS->os(), mOT)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 400 | |
| 401 | // Inform the diagnostic client we are processing a source file |
| 402 | mDiagClient->BeginSourceFile(LangOpts, mPP.get()); |
| 403 | |
| 404 | // The core of the slang compiler |
| 405 | ParseAST(*mPP, mBackend.get(), *mASTContext); |
| 406 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 407 | // Inform the diagnostic client we are done with previous source file |
| 408 | mDiagClient->EndSourceFile(); |
| 409 | |
| 410 | // Declare success if no error |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 411 | if (!mDiagEngine->hasErrorOccurred()) |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 412 | mOS->keep(); |
| 413 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 414 | // The compilation ended, clear |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 415 | mBackend.reset(); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 416 | mOS.reset(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 417 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 418 | return mDiagEngine->hasErrorOccurred() ? 1 : 0; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 419 | } |
| 420 | |
mkopec1 | c460b37 | 2012-01-09 11:21:50 -0500 | [diff] [blame] | 421 | void Slang::setDebugMetadataEmission(bool EmitDebug) { |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 422 | if (EmitDebug) |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 423 | CodeGenOpts.setDebugInfo(clang::CodeGenOptions::FullDebugInfo); |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 424 | else |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 425 | CodeGenOpts.setDebugInfo(clang::CodeGenOptions::NoDebugInfo); |
mkopec1 | c460b37 | 2012-01-09 11:21:50 -0500 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void Slang::setOptimizationLevel(llvm::CodeGenOpt::Level OptimizationLevel) { |
| 429 | CodeGenOpts.OptimizationLevel = OptimizationLevel; |
| 430 | } |
| 431 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 432 | bool Slang::isFilterscript(const char *Filename) { |
| 433 | const char *c = strrchr(Filename, '.'); |
| 434 | if (c && !strncmp(FS_SUFFIX, c + 1, strlen(FS_SUFFIX) + 1)) { |
| 435 | return true; |
| 436 | } else { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | bool Slang::generateJavaBitcodeAccessor(const std::string &OutputPathBase, |
| 442 | const std::string &PackageName, |
| 443 | const std::string *LicenseNote) { |
| 444 | RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext; |
| 445 | |
| 446 | BCAccessorContext.rsFileName = getInputFileName().c_str(); |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 447 | BCAccessorContext.bc32FileName = mOutput32FileName.c_str(); |
| 448 | BCAccessorContext.bc64FileName = mOutputFileName.c_str(); |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 449 | BCAccessorContext.reflectPath = OutputPathBase.c_str(); |
| 450 | BCAccessorContext.packageName = PackageName.c_str(); |
| 451 | BCAccessorContext.licenseNote = LicenseNote; |
| 452 | BCAccessorContext.bcStorage = BCST_JAVA_CODE; // Must be BCST_JAVA_CODE |
| 453 | BCAccessorContext.verbose = false; |
| 454 | |
| 455 | return RSSlangReflectUtils::GenerateJavaBitCodeAccessor(BCAccessorContext); |
| 456 | } |
| 457 | |
| 458 | bool Slang::checkODR(const char *CurInputFile) { |
| 459 | for (RSContext::ExportableList::iterator I = mRSContext->exportable_begin(), |
| 460 | E = mRSContext->exportable_end(); |
| 461 | I != E; |
| 462 | I++) { |
| 463 | RSExportable *RSE = *I; |
| 464 | if (RSE->getKind() != RSExportable::EX_TYPE) |
| 465 | continue; |
| 466 | |
| 467 | RSExportType *ET = static_cast<RSExportType *>(RSE); |
| 468 | if (ET->getClass() != RSExportType::ExportClassRecord) |
| 469 | continue; |
| 470 | |
| 471 | RSExportRecordType *ERT = static_cast<RSExportRecordType *>(ET); |
| 472 | |
| 473 | // Artificial record types (create by us not by user in the source) always |
| 474 | // conforms the ODR. |
| 475 | if (ERT->isArtificial()) |
| 476 | continue; |
| 477 | |
| 478 | // Key to lookup ERT in ReflectedDefinitions |
| 479 | llvm::StringRef RDKey(ERT->getName()); |
| 480 | ReflectedDefinitionListTy::const_iterator RD = |
| 481 | ReflectedDefinitions.find(RDKey); |
| 482 | |
| 483 | if (RD != ReflectedDefinitions.end()) { |
| 484 | const RSExportRecordType *Reflected = RD->getValue().first; |
| 485 | // There's a record (struct) with the same name reflected before. Enforce |
| 486 | // ODR checking - the Reflected must hold *exactly* the same "definition" |
| 487 | // as the one defined previously. We say two record types A and B have the |
| 488 | // same definition iff: |
| 489 | // |
| 490 | // struct A { struct B { |
| 491 | // Type(a1) a1, Type(b1) b1, |
| 492 | // Type(a2) a2, Type(b1) b2, |
| 493 | // ... ... |
| 494 | // Type(aN) aN Type(b3) b3, |
| 495 | // }; } |
| 496 | // Cond. #1. They have same number of fields, i.e., N = M; |
| 497 | // Cond. #2. for (i := 1 to N) |
| 498 | // Type(ai) = Type(bi) must hold; |
| 499 | // Cond. #3. for (i := 1 to N) |
| 500 | // Name(ai) = Name(bi) must hold; |
| 501 | // |
| 502 | // where, |
| 503 | // Type(F) = the type of field F and |
| 504 | // Name(F) = the field name. |
| 505 | |
| 506 | bool PassODR = false; |
| 507 | // Cond. #1 and Cond. #2 |
| 508 | if (Reflected->equals(ERT)) { |
| 509 | // Cond #3. |
| 510 | RSExportRecordType::const_field_iterator AI = Reflected->fields_begin(), |
| 511 | BI = ERT->fields_begin(); |
| 512 | |
| 513 | for (unsigned i = 0, e = Reflected->getFields().size(); i != e; i++) { |
| 514 | if ((*AI)->getName() != (*BI)->getName()) |
| 515 | break; |
| 516 | AI++; |
| 517 | BI++; |
| 518 | } |
| 519 | PassODR = (AI == (Reflected->fields_end())); |
| 520 | } |
| 521 | |
| 522 | if (!PassODR) { |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 523 | unsigned DiagID = mDiagEngine->getCustomDiagID( |
| 524 | clang::DiagnosticsEngine::Error, |
| 525 | "type '%0' in different translation unit (%1 v.s. %2) " |
| 526 | "has incompatible type definition"); |
| 527 | getDiagnostics().Report(DiagID) << Reflected->getName() |
| 528 | << getInputFileName() |
| 529 | << RD->getValue().second; |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | } else { |
| 533 | llvm::StringMapEntry<ReflectedDefinitionTy> *ME = |
| 534 | llvm::StringMapEntry<ReflectedDefinitionTy>::Create(RDKey); |
| 535 | ME->setValue(std::make_pair(ERT, CurInputFile)); |
| 536 | |
| 537 | if (!ReflectedDefinitions.insert(ME)) |
| 538 | delete ME; |
| 539 | |
| 540 | // Take the ownership of ERT such that it won't be freed in ~RSContext(). |
| 541 | ERT->keep(); |
| 542 | } |
| 543 | } |
| 544 | return true; |
| 545 | } |
| 546 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 547 | void Slang::initPreprocessor() { |
| 548 | clang::Preprocessor &PP = getPreprocessor(); |
| 549 | |
| 550 | std::stringstream RSH; |
| 551 | RSH << PP.getPredefines(); |
| 552 | RSH << "#define RS_VERSION " << mTargetAPI << "\n"; |
| 553 | RSH << "#include \"rs_core." RS_HEADER_SUFFIX "\"\n"; |
| 554 | PP.setPredefines(RSH.str()); |
| 555 | } |
| 556 | |
| 557 | void Slang::initASTContext() { |
| 558 | mRSContext = new RSContext(getPreprocessor(), |
| 559 | getASTContext(), |
| 560 | getTargetInfo(), |
| 561 | &mPragmas, |
| 562 | mTargetAPI, |
| 563 | mVerbose); |
| 564 | } |
| 565 | |
| 566 | bool Slang::IsRSHeaderFile(const char *File) { |
| 567 | #define RS_HEADER_ENTRY(name) \ |
| 568 | if (::strcmp(File, #name "." RS_HEADER_SUFFIX) == 0) \ |
| 569 | return true; |
| 570 | ENUM_RS_HEADER() |
| 571 | #undef RS_HEADER_ENTRY |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | bool Slang::IsLocInRSHeaderFile(const clang::SourceLocation &Loc, |
| 576 | const clang::SourceManager &SourceMgr) { |
| 577 | clang::FullSourceLoc FSL(Loc, SourceMgr); |
| 578 | clang::PresumedLoc PLoc = SourceMgr.getPresumedLoc(FSL); |
| 579 | |
| 580 | const char *Filename = PLoc.getFilename(); |
| 581 | if (!Filename) { |
| 582 | return false; |
| 583 | } else { |
| 584 | return IsRSHeaderFile(llvm::sys::path::filename(Filename).data()); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | bool Slang::compile( |
| 589 | const std::list<std::pair<const char*, const char*> > &IOFiles64, |
| 590 | const std::list<std::pair<const char*, const char*> > &IOFiles32, |
| 591 | const std::list<std::pair<const char*, const char*> > &DepFiles, |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 592 | const RSCCOptions &Opts, |
| 593 | clang::DiagnosticOptions &DiagOpts) { |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 594 | if (IOFiles32.empty()) |
| 595 | return true; |
| 596 | |
| 597 | if (Opts.mEmitDependency && (DepFiles.size() != IOFiles32.size())) { |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 598 | unsigned DiagID = mDiagEngine->getCustomDiagID( |
| 599 | clang::DiagnosticsEngine::Error, |
| 600 | "invalid parameter for output dependencies files."); |
| 601 | getDiagnostics().Report(DiagID); |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 602 | return false; |
| 603 | } |
| 604 | |
| 605 | if (Opts.mEmit3264 && (IOFiles64.size() != IOFiles32.size())) { |
| 606 | slangAssert(false && "Should have equal number of 32/64-bit files"); |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | std::string RealPackageName; |
| 611 | |
| 612 | const char *InputFile, *Output64File, *Output32File, *BCOutputFile, |
| 613 | *DepOutputFile; |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 614 | |
| 615 | setIncludePaths(Opts.mIncludePaths); |
| 616 | setOutputType(Opts.mOutputType); |
| 617 | if (Opts.mEmitDependency) { |
| 618 | setAdditionalDepTargets(Opts.mAdditionalDepTargets); |
| 619 | } |
| 620 | |
| 621 | setDebugMetadataEmission(Opts.mDebugEmission); |
| 622 | |
| 623 | setOptimizationLevel(Opts.mOptimizationLevel); |
| 624 | |
| 625 | mAllowRSPrefix = Opts.mAllowRSPrefix; |
| 626 | |
| 627 | mTargetAPI = Opts.mTargetAPI; |
| 628 | if (mTargetAPI != SLANG_DEVELOPMENT_TARGET_API && |
| 629 | (mTargetAPI < SLANG_MINIMUM_TARGET_API || |
| 630 | mTargetAPI > SLANG_MAXIMUM_TARGET_API)) { |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 631 | unsigned DiagID = mDiagEngine->getCustomDiagID( |
| 632 | clang::DiagnosticsEngine::Error, |
| 633 | "target API level '%0' is out of range ('%1' - '%2')"); |
| 634 | getDiagnostics().Report(DiagID) << mTargetAPI << SLANG_MINIMUM_TARGET_API |
| 635 | << SLANG_MAXIMUM_TARGET_API; |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 636 | return false; |
| 637 | } |
| 638 | |
Stephen Hines | b3e022a | 2015-05-21 09:44:14 -0700 | [diff] [blame] | 639 | if (mTargetAPI >= SLANG_M_TARGET_API) { |
Pirama Arumuga Nainar | 7966bcf | 2015-04-07 11:09:14 -0700 | [diff] [blame] | 640 | LangOpts.NativeHalfType = 1; |
| 641 | LangOpts.HalfArgsAndReturns = 1; |
| 642 | } |
| 643 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 644 | mVerbose = Opts.mVerbose; |
| 645 | |
| 646 | // Skip generation of warnings a second time if we are doing more than just |
| 647 | // a single pass over the input file. |
| 648 | bool SuppressAllWarnings = (Opts.mOutputType != Slang::OT_Dependency); |
| 649 | |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 650 | std::list<std::pair<const char*, const char*> >::const_iterator |
| 651 | IOFile64Iter = IOFiles64.begin(), |
| 652 | IOFile32Iter = IOFiles32.begin(), |
| 653 | DepFileIter = DepFiles.begin(); |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 654 | |
| 655 | for (unsigned i = 0, e = IOFiles32.size(); i != e; i++) { |
| 656 | InputFile = IOFile64Iter->first; |
| 657 | Output64File = IOFile64Iter->second; |
| 658 | Output32File = IOFile32Iter->second; |
| 659 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 660 | if (!setInputSource(InputFile)) |
| 661 | return false; |
| 662 | |
| 663 | if (!setOutput(Output64File)) |
| 664 | return false; |
| 665 | |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 666 | // For use with 64-bit compilation/reflection. This only sets the filename of |
| 667 | // the 32-bit bitcode file, and doesn't actually verify it already exists. |
| 668 | mOutput32FileName = Output32File; |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 669 | |
| 670 | mIsFilterscript = isFilterscript(InputFile); |
| 671 | |
| 672 | if (Slang::compile() > 0) |
| 673 | return false; |
| 674 | |
| 675 | if (!Opts.mJavaReflectionPackageName.empty()) { |
| 676 | mRSContext->setReflectJavaPackageName(Opts.mJavaReflectionPackageName); |
| 677 | } |
| 678 | const std::string &RealPackageName = |
| 679 | mRSContext->getReflectJavaPackageName(); |
| 680 | |
| 681 | bool doReflection = true; |
| 682 | if (Opts.mEmit3264 && (Opts.mBitWidth == 32)) { |
| 683 | // Skip reflection on the 32-bit path if we are going to emit it on the |
| 684 | // 64-bit path. |
| 685 | doReflection = false; |
| 686 | } |
| 687 | if (Opts.mOutputType != Slang::OT_Dependency && doReflection) { |
| 688 | |
| 689 | if (Opts.mBitcodeStorage == BCST_CPP_CODE) { |
| 690 | const std::string &outputFileName = (Opts.mBitWidth == 64) ? |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 691 | mOutputFileName : mOutput32FileName; |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 692 | RSReflectionCpp R(mRSContext, Opts.mJavaReflectionPathBase, |
| 693 | getInputFileName(), outputFileName); |
| 694 | if (!R.reflect()) { |
| 695 | return false; |
| 696 | } |
| 697 | } else { |
| 698 | if (!Opts.mRSPackageName.empty()) { |
| 699 | mRSContext->setRSPackageName(Opts.mRSPackageName); |
| 700 | } |
| 701 | |
Jean-Luc Brouillet | 6394dc7 | 2015-05-05 21:16:44 -0700 | [diff] [blame] | 702 | std::vector<std::string> generatedFileNames; |
| 703 | RSReflectionJava R(mRSContext, &generatedFileNames, |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 704 | Opts.mJavaReflectionPathBase, getInputFileName(), |
Jean-Luc Brouillet | 666b0a7 | 2015-05-01 16:13:11 -0700 | [diff] [blame] | 705 | mOutputFileName, |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 706 | Opts.mBitcodeStorage == BCST_JAVA_CODE); |
| 707 | if (!R.reflect()) { |
| 708 | // TODO Is this needed or will the error message have been printed |
| 709 | // already? and why not for the C++ case? |
| 710 | fprintf(stderr, "RSContext::reflectToJava : failed to do reflection " |
| 711 | "(%s)\n", |
| 712 | R.getLastError()); |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | for (std::vector<std::string>::const_iterator |
Jean-Luc Brouillet | 6394dc7 | 2015-05-05 21:16:44 -0700 | [diff] [blame] | 717 | I = generatedFileNames.begin(), E = generatedFileNames.end(); |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 718 | I != E; |
| 719 | I++) { |
| 720 | std::string ReflectedName = RSSlangReflectUtils::ComputePackagedPath( |
| 721 | Opts.mJavaReflectionPathBase.c_str(), |
| 722 | (RealPackageName + OS_PATH_SEPARATOR_STR + *I).c_str()); |
| 723 | appendGeneratedFileName(ReflectedName + ".java"); |
| 724 | } |
| 725 | |
| 726 | if ((Opts.mOutputType == Slang::OT_Bitcode) && |
| 727 | (Opts.mBitcodeStorage == BCST_JAVA_CODE) && |
| 728 | !generateJavaBitcodeAccessor(Opts.mJavaReflectionPathBase, |
| 729 | RealPackageName.c_str(), |
| 730 | mRSContext->getLicenseNote())) { |
| 731 | return false; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if (Opts.mEmitDependency) { |
| 737 | BCOutputFile = DepFileIter->first; |
| 738 | DepOutputFile = DepFileIter->second; |
| 739 | |
| 740 | setDepTargetBC(BCOutputFile); |
| 741 | |
| 742 | if (!setDepOutput(DepOutputFile)) |
| 743 | return false; |
| 744 | |
| 745 | if (SuppressAllWarnings) { |
| 746 | getDiagnostics().setSuppressAllDiagnostics(true); |
| 747 | } |
| 748 | if (generateDepFile() > 0) |
| 749 | return false; |
| 750 | if (SuppressAllWarnings) { |
| 751 | getDiagnostics().setSuppressAllDiagnostics(false); |
| 752 | } |
| 753 | |
| 754 | DepFileIter++; |
| 755 | } |
| 756 | |
| 757 | if (!checkODR(InputFile)) |
| 758 | return false; |
| 759 | |
| 760 | IOFile64Iter++; |
| 761 | IOFile32Iter++; |
| 762 | } |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 763 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 764 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 765 | |
| 766 | } // namespace slang |