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