Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | 0a813a3 | 2012-08-03 16:52:40 -0700 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 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 Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 17 | #include "slang_rs.h" |
| 18 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 19 | #include <cstring> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 20 | #include <list> |
Stephen Hines | 2e35b13 | 2011-07-22 02:50:19 -0700 | [diff] [blame] | 21 | #include <sstream> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <utility> |
| 24 | #include <vector> |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 25 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 26 | #include "clang/Basic/SourceLocation.h" |
| 27 | |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 29 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 30 | #include "clang/Sema/SemaDiagnostic.h" |
| 31 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 32 | #include "llvm/Support/Path.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 33 | |
Raphael | 8d5a2f6 | 2011-02-08 00:15:05 -0800 | [diff] [blame] | 34 | #include "os_sep.h" |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 35 | #include "rs_cc_options.h" |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 36 | #include "slang_rs_backend.h" |
| 37 | #include "slang_rs_context.h" |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 38 | #include "slang_rs_export_type.h" |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 39 | |
Jean-Luc Brouillet | 12fc283 | 2014-06-04 21:32:31 +0000 | [diff] [blame] | 40 | #include "slang_rs_reflection.h" |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 41 | #include "slang_rs_reflection_cpp.h" |
| 42 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 43 | namespace slang { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 44 | |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 45 | #define FS_SUFFIX "fs" |
| 46 | |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 47 | #define RS_HEADER_SUFFIX "rsh" |
| 48 | |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 49 | /* RS_HEADER_ENTRY(name) */ |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 50 | #define ENUM_RS_HEADER() \ |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 51 | RS_HEADER_ENTRY(rs_allocation) \ |
| 52 | RS_HEADER_ENTRY(rs_atomic) \ |
| 53 | RS_HEADER_ENTRY(rs_cl) \ |
| 54 | RS_HEADER_ENTRY(rs_core) \ |
Stephen Hines | a7ab54d | 2014-01-08 09:41:49 -0800 | [diff] [blame] | 55 | RS_HEADER_ENTRY(rs_core_math) \ |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 56 | RS_HEADER_ENTRY(rs_debug) \ |
Alex Sakhartchouk | 4eb4b38 | 2012-03-21 09:58:39 -0700 | [diff] [blame] | 57 | RS_HEADER_ENTRY(rs_element) \ |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 58 | RS_HEADER_ENTRY(rs_graphics) \ |
| 59 | RS_HEADER_ENTRY(rs_math) \ |
Alex Sakhartchouk | 4eb4b38 | 2012-03-21 09:58:39 -0700 | [diff] [blame] | 60 | RS_HEADER_ENTRY(rs_mesh) \ |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 61 | RS_HEADER_ENTRY(rs_matrix) \ |
| 62 | RS_HEADER_ENTRY(rs_object) \ |
Alex Sakhartchouk | 4eb4b38 | 2012-03-21 09:58:39 -0700 | [diff] [blame] | 63 | RS_HEADER_ENTRY(rs_program) \ |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 64 | RS_HEADER_ENTRY(rs_quaternion) \ |
Alex Sakhartchouk | 4eb4b38 | 2012-03-21 09:58:39 -0700 | [diff] [blame] | 65 | RS_HEADER_ENTRY(rs_sampler) \ |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 66 | RS_HEADER_ENTRY(rs_time) \ |
| 67 | RS_HEADER_ENTRY(rs_types) \ |
| 68 | |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 69 | // Returns true if \p Filename ends in ".fs". |
| 70 | bool SlangRS::isFilterscript(const char *Filename) { |
| 71 | const char *c = strrchr(Filename, '.'); |
| 72 | if (c && !strncmp(FS_SUFFIX, c + 1, strlen(FS_SUFFIX) + 1)) { |
| 73 | return true; |
| 74 | } else { |
| 75 | return false; |
| 76 | } |
| 77 | } |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 78 | |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 79 | bool SlangRS::generateJavaBitcodeAccessor(const std::string &OutputPathBase, |
Jean-Luc Brouillet | 12fc283 | 2014-06-04 21:32:31 +0000 | [diff] [blame] | 80 | const std::string &PackageName, |
| 81 | const std::string *LicenseNote) { |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 82 | RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext; |
| 83 | |
| 84 | BCAccessorContext.rsFileName = getInputFileName().c_str(); |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 85 | BCAccessorContext.bc32FileName = getOutput32FileName().c_str(); |
| 86 | BCAccessorContext.bc64FileName = getOutputFileName().c_str(); |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 87 | BCAccessorContext.reflectPath = OutputPathBase.c_str(); |
| 88 | BCAccessorContext.packageName = PackageName.c_str(); |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 89 | BCAccessorContext.licenseNote = LicenseNote; |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 90 | BCAccessorContext.bcStorage = BCST_JAVA_CODE; // Must be BCST_JAVA_CODE |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 91 | BCAccessorContext.verbose = false; |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 92 | |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 93 | return RSSlangReflectUtils::GenerateJavaBitCodeAccessor(BCAccessorContext); |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 94 | } |
| 95 | |
Zonr Chang | e86245a | 2010-10-12 21:42:13 +0800 | [diff] [blame] | 96 | bool SlangRS::checkODR(const char *CurInputFile) { |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 97 | for (RSContext::ExportableList::iterator I = mRSContext->exportable_begin(), |
| 98 | E = mRSContext->exportable_end(); |
| 99 | I != E; |
| 100 | I++) { |
Stephen Hines | a858cb6 | 2011-01-17 12:17:51 -0800 | [diff] [blame] | 101 | RSExportable *RSE = *I; |
| 102 | if (RSE->getKind() != RSExportable::EX_TYPE) |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 103 | continue; |
| 104 | |
Stephen Hines | a858cb6 | 2011-01-17 12:17:51 -0800 | [diff] [blame] | 105 | RSExportType *ET = static_cast<RSExportType *>(RSE); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 106 | if (ET->getClass() != RSExportType::ExportClassRecord) |
| 107 | continue; |
| 108 | |
| 109 | RSExportRecordType *ERT = static_cast<RSExportRecordType *>(ET); |
| 110 | |
| 111 | // Artificial record types (create by us not by user in the source) always |
| 112 | // conforms the ODR. |
| 113 | if (ERT->isArtificial()) |
| 114 | continue; |
| 115 | |
| 116 | // Key to lookup ERT in ReflectedDefinitions |
| 117 | llvm::StringRef RDKey(ERT->getName()); |
| 118 | ReflectedDefinitionListTy::const_iterator RD = |
| 119 | ReflectedDefinitions.find(RDKey); |
| 120 | |
| 121 | if (RD != ReflectedDefinitions.end()) { |
| 122 | const RSExportRecordType *Reflected = RD->getValue().first; |
| 123 | // There's a record (struct) with the same name reflected before. Enforce |
| 124 | // ODR checking - the Reflected must hold *exactly* the same "definition" |
| 125 | // as the one defined previously. We say two record types A and B have the |
| 126 | // same definition iff: |
| 127 | // |
| 128 | // struct A { struct B { |
| 129 | // Type(a1) a1, Type(b1) b1, |
| 130 | // Type(a2) a2, Type(b1) b2, |
| 131 | // ... ... |
| 132 | // Type(aN) aN Type(b3) b3, |
| 133 | // }; } |
| 134 | // Cond. #1. They have same number of fields, i.e., N = M; |
| 135 | // Cond. #2. for (i := 1 to N) |
| 136 | // Type(ai) = Type(bi) must hold; |
| 137 | // Cond. #3. for (i := 1 to N) |
| 138 | // Name(ai) = Name(bi) must hold; |
| 139 | // |
| 140 | // where, |
| 141 | // Type(F) = the type of field F and |
| 142 | // Name(F) = the field name. |
| 143 | |
| 144 | bool PassODR = false; |
| 145 | // Cond. #1 and Cond. #2 |
| 146 | if (Reflected->equals(ERT)) { |
| 147 | // Cond #3. |
| 148 | RSExportRecordType::const_field_iterator AI = Reflected->fields_begin(), |
| 149 | BI = ERT->fields_begin(); |
| 150 | |
| 151 | for (unsigned i = 0, e = Reflected->getFields().size(); i != e; i++) { |
| 152 | if ((*AI)->getName() != (*BI)->getName()) |
| 153 | break; |
| 154 | AI++; |
| 155 | BI++; |
| 156 | } |
| 157 | PassODR = (AI == (Reflected->fields_end())); |
| 158 | } |
| 159 | |
| 160 | if (!PassODR) { |
| 161 | getDiagnostics().Report(mDiagErrorODR) << Reflected->getName() |
| 162 | << getInputFileName() |
| 163 | << RD->getValue().second; |
| 164 | return false; |
| 165 | } |
| 166 | } else { |
| 167 | llvm::StringMapEntry<ReflectedDefinitionTy> *ME = |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 168 | llvm::StringMapEntry<ReflectedDefinitionTy>::Create(RDKey); |
Zonr Chang | e86245a | 2010-10-12 21:42:13 +0800 | [diff] [blame] | 169 | ME->setValue(std::make_pair(ERT, CurInputFile)); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 170 | |
| 171 | if (!ReflectedDefinitions.insert(ME)) |
| 172 | delete ME; |
| 173 | |
| 174 | // Take the ownership of ERT such that it won't be freed in ~RSContext(). |
| 175 | ERT->keep(); |
| 176 | } |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 177 | } |
| 178 | return true; |
| 179 | } |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 180 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 181 | void SlangRS::initDiagnostic() { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 182 | clang::DiagnosticsEngine &DiagEngine = getDiagnostics(); |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 183 | |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 184 | if (DiagEngine.setSeverityForGroup("implicit-function-declaration", |
| 185 | clang::diag::Severity::Error)) |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 186 | DiagEngine.Report(clang::diag::warn_unknown_warning_option) |
| 187 | << "implicit-function-declaration"; |
| 188 | |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 189 | DiagEngine.setSeverity( |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 190 | clang::diag::ext_typecheck_convert_discards_qualifiers, |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 191 | clang::diag::Severity::Error, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 192 | clang::SourceLocation()); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 193 | |
| 194 | mDiagErrorInvalidOutputDepParameter = |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 195 | DiagEngine.getCustomDiagID( |
| 196 | clang::DiagnosticsEngine::Error, |
| 197 | "invalid parameter for output dependencies files."); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 198 | |
| 199 | mDiagErrorODR = |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 200 | DiagEngine.getCustomDiagID( |
| 201 | clang::DiagnosticsEngine::Error, |
| 202 | "type '%0' in different translation unit (%1 v.s. %2) " |
| 203 | "has incompatible type definition"); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 204 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 205 | mDiagErrorTargetAPIRange = |
| 206 | DiagEngine.getCustomDiagID( |
| 207 | clang::DiagnosticsEngine::Error, |
Stephen Hines | 2e35b13 | 2011-07-22 02:50:19 -0700 | [diff] [blame] | 208 | "target API level '%0' is out of range ('%1' - '%2')"); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void SlangRS::initPreprocessor() { |
| 212 | clang::Preprocessor &PP = getPreprocessor(); |
| 213 | |
Stephen Hines | 2e35b13 | 2011-07-22 02:50:19 -0700 | [diff] [blame] | 214 | std::stringstream RSH; |
Stephen Hines | 0e4ee65 | 2014-06-10 14:32:26 -0700 | [diff] [blame] | 215 | RSH << PP.getPredefines(); |
Jean-Luc Brouillet | 2968921 | 2014-05-27 13:25:31 -0700 | [diff] [blame] | 216 | RSH << "#define RS_VERSION " << mTargetAPI << "\n"; |
| 217 | RSH << "#include \"rs_core." RS_HEADER_SUFFIX "\"\n"; |
Stephen Hines | 2e35b13 | 2011-07-22 02:50:19 -0700 | [diff] [blame] | 218 | PP.setPredefines(RSH.str()); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void SlangRS::initASTContext() { |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 222 | mRSContext = new RSContext(getPreprocessor(), |
| 223 | getASTContext(), |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 224 | getTargetInfo(), |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 225 | &mPragmas, |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 226 | mTargetAPI, |
| 227 | mVerbose); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | clang::ASTConsumer |
| 231 | *SlangRS::createBackend(const clang::CodeGenOptions& CodeGenOpts, |
| 232 | llvm::raw_ostream *OS, |
| 233 | Slang::OutputType OT) { |
| 234 | return new RSBackend(mRSContext, |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 235 | &getDiagnostics(), |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 236 | CodeGenOpts, |
| 237 | getTargetOptions(), |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 238 | &mPragmas, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 239 | OS, |
| 240 | OT, |
| 241 | getSourceManager(), |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 242 | mAllowRSPrefix, |
| 243 | mIsFilterscript); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 244 | } |
| 245 | |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 246 | bool SlangRS::IsRSHeaderFile(const char *File) { |
Stephen Hines | 2d35edd | 2011-08-09 11:33:27 -0700 | [diff] [blame] | 247 | #define RS_HEADER_ENTRY(name) \ |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 248 | if (::strcmp(File, #name "." RS_HEADER_SUFFIX) == 0) \ |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 249 | return true; |
| 250 | ENUM_RS_HEADER() |
| 251 | #undef RS_HEADER_ENTRY |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 255 | bool SlangRS::IsLocInRSHeaderFile(const clang::SourceLocation &Loc, |
| 256 | const clang::SourceManager &SourceMgr) { |
| 257 | clang::FullSourceLoc FSL(Loc, SourceMgr); |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 258 | clang::PresumedLoc PLoc = SourceMgr.getPresumedLoc(FSL); |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 259 | |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 260 | const char *Filename = PLoc.getFilename(); |
| 261 | if (!Filename) { |
| 262 | return false; |
| 263 | } else { |
| 264 | return IsRSHeaderFile(llvm::sys::path::filename(Filename).data()); |
| 265 | } |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 268 | SlangRS::SlangRS() |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 269 | : Slang(), mRSContext(NULL), mAllowRSPrefix(false), mTargetAPI(0), |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 270 | mVerbose(false), mIsFilterscript(false) { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 271 | } |
| 272 | |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 273 | bool SlangRS::compile( |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 274 | const std::list<std::pair<const char*, const char*> > &IOFiles64, |
| 275 | const std::list<std::pair<const char*, const char*> > &IOFiles32, |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 276 | const std::list<std::pair<const char*, const char*> > &DepFiles, |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 277 | const RSCCOptions &Opts) { |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 278 | if (IOFiles32.empty()) |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 279 | return true; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 280 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 281 | if (Opts.mEmitDependency && (DepFiles.size() != IOFiles32.size())) { |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 282 | getDiagnostics().Report(mDiagErrorInvalidOutputDepParameter); |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 286 | if (Opts.mEmit3264 && (IOFiles64.size() != IOFiles32.size())) { |
| 287 | slangAssert(false && "Should have equal number of 32/64-bit files"); |
| 288 | return false; |
| 289 | } |
| 290 | |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 291 | std::string RealPackageName; |
| 292 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 293 | const char *InputFile, *Output64File, *Output32File, *BCOutputFile, |
| 294 | *DepOutputFile; |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 295 | std::list<std::pair<const char*, const char*> >::const_iterator |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 296 | IOFile64Iter = IOFiles64.begin(), |
| 297 | IOFile32Iter = IOFiles32.begin(), |
| 298 | DepFileIter = DepFiles.begin(); |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 299 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 300 | setIncludePaths(Opts.mIncludePaths); |
| 301 | setOutputType(Opts.mOutputType); |
| 302 | if (Opts.mEmitDependency) { |
| 303 | setAdditionalDepTargets(Opts.mAdditionalDepTargets); |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 304 | } |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 305 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 306 | setDebugMetadataEmission(Opts.mDebugEmission); |
mkopec1 | c460b37 | 2012-01-09 11:21:50 -0500 | [diff] [blame] | 307 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 308 | setOptimizationLevel(Opts.mOptimizationLevel); |
mkopec1 | c460b37 | 2012-01-09 11:21:50 -0500 | [diff] [blame] | 309 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 310 | mAllowRSPrefix = Opts.mAllowRSPrefix; |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 311 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 312 | mTargetAPI = Opts.mTargetAPI; |
Chris Wailes | 9e3aa07 | 2014-06-16 18:52:02 -0700 | [diff] [blame] | 313 | if (mTargetAPI != SLANG_DEVELOPMENT_TARGET_API && |
| 314 | (mTargetAPI < SLANG_MINIMUM_TARGET_API || |
| 315 | mTargetAPI > SLANG_MAXIMUM_TARGET_API)) { |
Stephen Hines | 2e35b13 | 2011-07-22 02:50:19 -0700 | [diff] [blame] | 316 | getDiagnostics().Report(mDiagErrorTargetAPIRange) << mTargetAPI |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 317 | << SLANG_MINIMUM_TARGET_API << SLANG_MAXIMUM_TARGET_API; |
Stephen Hines | 2e35b13 | 2011-07-22 02:50:19 -0700 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 321 | mVerbose = Opts.mVerbose; |
| 322 | |
Stephen Hines | c632be2 | 2011-09-23 15:53:16 -0700 | [diff] [blame] | 323 | // Skip generation of warnings a second time if we are doing more than just |
| 324 | // a single pass over the input file. |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 325 | bool SuppressAllWarnings = (Opts.mOutputType != Slang::OT_Dependency); |
Stephen Hines | c632be2 | 2011-09-23 15:53:16 -0700 | [diff] [blame] | 326 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 327 | for (unsigned i = 0, e = IOFiles32.size(); i != e; i++) { |
| 328 | InputFile = IOFile64Iter->first; |
| 329 | Output64File = IOFile64Iter->second; |
| 330 | Output32File = IOFile32Iter->second; |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 331 | |
| 332 | reset(); |
| 333 | |
| 334 | if (!setInputSource(InputFile)) |
| 335 | return false; |
| 336 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 337 | if (!setOutput(Output64File)) |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 338 | return false; |
| 339 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 340 | setOutput32(Output32File); |
| 341 | |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 342 | mIsFilterscript = isFilterscript(InputFile); |
| 343 | |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 344 | if (Slang::compile() > 0) |
| 345 | return false; |
| 346 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 347 | if (!Opts.mJavaReflectionPackageName.empty()) { |
| 348 | mRSContext->setReflectJavaPackageName(Opts.mJavaReflectionPackageName); |
Stephen Hines | 925879f | 2013-07-19 18:19:04 -0700 | [diff] [blame] | 349 | } |
| 350 | const std::string &RealPackageName = |
| 351 | mRSContext->getReflectJavaPackageName(); |
| 352 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 353 | bool doReflection = true; |
| 354 | if (Opts.mEmit3264 && (Opts.mBitWidth == 32)) { |
| 355 | // Skip reflection on the 32-bit path if we are going to emit it on the |
| 356 | // 64-bit path. |
| 357 | doReflection = false; |
| 358 | } |
| 359 | if (Opts.mOutputType != Slang::OT_Dependency && doReflection) { |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 360 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 361 | if (Opts.mBitcodeStorage == BCST_CPP_CODE) { |
| 362 | RSReflectionCpp R(mRSContext, Opts.mJavaReflectionPathBase, |
| 363 | getInputFileName(), getOutputFileName()); |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 364 | if (!R.reflect()) { |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 365 | return false; |
| 366 | } |
Stephen Hines | 5c25c51 | 2012-02-27 12:58:17 -0800 | [diff] [blame] | 367 | } else { |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 368 | if (!Opts.mRSPackageName.empty()) { |
| 369 | mRSContext->setRSPackageName(Opts.mRSPackageName); |
Jean-Luc Brouillet | 12fc283 | 2014-06-04 21:32:31 +0000 | [diff] [blame] | 370 | } |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 371 | |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 372 | RSReflectionJava R(mRSContext, &mGeneratedFileNames, |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 373 | Opts.mJavaReflectionPathBase, getInputFileName(), |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 374 | getOutputFileName(), |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 375 | Opts.mBitcodeStorage == BCST_JAVA_CODE); |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 376 | if (!R.reflect()) { |
Jean-Luc Brouillet | 12fc283 | 2014-06-04 21:32:31 +0000 | [diff] [blame] | 377 | // TODO Is this needed or will the error message have been printed |
| 378 | // already? and why not for the C++ case? |
| 379 | fprintf(stderr, "RSContext::reflectToJava : failed to do reflection " |
| 380 | "(%s)\n", |
| 381 | R.getLastError()); |
Jean-Luc Brouillet | 7fda984 | 2014-06-04 19:23:55 +0000 | [diff] [blame] | 382 | return false; |
Stephen Hines | 5c25c51 | 2012-02-27 12:58:17 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | for (std::vector<std::string>::const_iterator |
| 386 | I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end(); |
| 387 | I != E; |
| 388 | I++) { |
| 389 | std::string ReflectedName = RSSlangReflectUtils::ComputePackagedPath( |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 390 | Opts.mJavaReflectionPathBase.c_str(), |
Stephen Hines | 5c25c51 | 2012-02-27 12:58:17 -0800 | [diff] [blame] | 391 | (RealPackageName + OS_PATH_SEPARATOR_STR + *I).c_str()); |
| 392 | appendGeneratedFileName(ReflectedName + ".java"); |
| 393 | } |
| 394 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 395 | if ((Opts.mOutputType == Slang::OT_Bitcode) && |
| 396 | (Opts.mBitcodeStorage == BCST_JAVA_CODE) && |
| 397 | !generateJavaBitcodeAccessor(Opts.mJavaReflectionPathBase, |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 398 | RealPackageName.c_str(), |
| 399 | mRSContext->getLicenseNote())) { |
Stephen Hines | 5c25c51 | 2012-02-27 12:58:17 -0800 | [diff] [blame] | 400 | return false; |
| 401 | } |
| 402 | } |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 405 | if (Opts.mEmitDependency) { |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 406 | BCOutputFile = DepFileIter->first; |
| 407 | DepOutputFile = DepFileIter->second; |
| 408 | |
| 409 | setDepTargetBC(BCOutputFile); |
| 410 | |
| 411 | if (!setDepOutput(DepOutputFile)) |
| 412 | return false; |
| 413 | |
Stephen Hines | c632be2 | 2011-09-23 15:53:16 -0700 | [diff] [blame] | 414 | if (SuppressAllWarnings) { |
| 415 | getDiagnostics().setSuppressAllDiagnostics(true); |
| 416 | } |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 417 | if (generateDepFile() > 0) |
| 418 | return false; |
Stephen Hines | c632be2 | 2011-09-23 15:53:16 -0700 | [diff] [blame] | 419 | if (SuppressAllWarnings) { |
| 420 | getDiagnostics().setSuppressAllDiagnostics(false); |
| 421 | } |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 422 | |
| 423 | DepFileIter++; |
| 424 | } |
| 425 | |
Zonr Chang | e86245a | 2010-10-12 21:42:13 +0800 | [diff] [blame] | 426 | if (!checkODR(InputFile)) |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 427 | return false; |
| 428 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 429 | IOFile64Iter++; |
| 430 | IOFile32Iter++; |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | return true; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 434 | } |
| 435 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 436 | void SlangRS::reset() { |
| 437 | delete mRSContext; |
| 438 | mRSContext = NULL; |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 439 | mGeneratedFileNames.clear(); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 440 | Slang::reset(); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 441 | } |
| 442 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 443 | SlangRS::~SlangRS() { |
| 444 | delete mRSContext; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 445 | for (ReflectedDefinitionListTy::iterator I = ReflectedDefinitions.begin(), |
| 446 | E = ReflectedDefinitions.end(); |
| 447 | I != E; |
| 448 | I++) { |
| 449 | delete I->getValue().first; |
| 450 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 451 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 452 | |
| 453 | } // namespace slang |