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