slang_rs.cpp revision 9ae18b2bbee0b08afd400542e863dd665ff76059
1/* 2 * Copyright 2010-2012, 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 17#include "slang_rs.h" 18 19#include <cstring> 20#include <list> 21#include <sstream> 22#include <string> 23#include <utility> 24#include <vector> 25 26#include "clang/Basic/SourceLocation.h" 27 28#include "clang/Frontend/FrontendDiagnostic.h" 29 30#include "clang/Sema/SemaDiagnostic.h" 31 32#include "llvm/Support/Path.h" 33 34#include "os_sep.h" 35#include "rs_cc_options.h" 36#include "slang_rs_backend.h" 37#include "slang_rs_context.h" 38#include "slang_rs_export_type.h" 39 40#include "slang_rs_reflection.h" 41#include "slang_rs_reflection_cpp.h" 42 43namespace slang { 44 45#define FS_SUFFIX "fs" 46 47#define RS_HEADER_SUFFIX "rsh" 48 49/* RS_HEADER_ENTRY(name) */ 50#define ENUM_RS_HEADER() \ 51 RS_HEADER_ENTRY(rs_allocation) \ 52 RS_HEADER_ENTRY(rs_atomic) \ 53 RS_HEADER_ENTRY(rs_cl) \ 54 RS_HEADER_ENTRY(rs_core) \ 55 RS_HEADER_ENTRY(rs_core_math) \ 56 RS_HEADER_ENTRY(rs_debug) \ 57 RS_HEADER_ENTRY(rs_element) \ 58 RS_HEADER_ENTRY(rs_graphics) \ 59 RS_HEADER_ENTRY(rs_math) \ 60 RS_HEADER_ENTRY(rs_mesh) \ 61 RS_HEADER_ENTRY(rs_matrix) \ 62 RS_HEADER_ENTRY(rs_object) \ 63 RS_HEADER_ENTRY(rs_program) \ 64 RS_HEADER_ENTRY(rs_quaternion) \ 65 RS_HEADER_ENTRY(rs_sampler) \ 66 RS_HEADER_ENTRY(rs_time) \ 67 RS_HEADER_ENTRY(rs_types) \ 68 69// Returns true if \p Filename ends in ".fs". 70bool 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} 78 79bool SlangRS::generateJavaBitcodeAccessor(const std::string &OutputPathBase, 80 const std::string &PackageName, 81 const std::string *LicenseNote) { 82 RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext; 83 84 BCAccessorContext.rsFileName = getInputFileName().c_str(); 85 BCAccessorContext.bc32FileName = getOutput32FileName().c_str(); 86 BCAccessorContext.bc64FileName = getOutputFileName().c_str(); 87 BCAccessorContext.reflectPath = OutputPathBase.c_str(); 88 BCAccessorContext.packageName = PackageName.c_str(); 89 BCAccessorContext.licenseNote = LicenseNote; 90 BCAccessorContext.bcStorage = BCST_JAVA_CODE; // Must be BCST_JAVA_CODE 91 BCAccessorContext.verbose = false; 92 93 return RSSlangReflectUtils::GenerateJavaBitCodeAccessor(BCAccessorContext); 94} 95 96bool SlangRS::checkODR(const char *CurInputFile) { 97 for (RSContext::ExportableList::iterator I = mRSContext->exportable_begin(), 98 E = mRSContext->exportable_end(); 99 I != E; 100 I++) { 101 RSExportable *RSE = *I; 102 if (RSE->getKind() != RSExportable::EX_TYPE) 103 continue; 104 105 RSExportType *ET = static_cast<RSExportType *>(RSE); 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 = 168 llvm::StringMapEntry<ReflectedDefinitionTy>::Create(RDKey.begin(), 169 RDKey.end()); 170 ME->setValue(std::make_pair(ERT, CurInputFile)); 171 172 if (!ReflectedDefinitions.insert(ME)) 173 delete ME; 174 175 // Take the ownership of ERT such that it won't be freed in ~RSContext(). 176 ERT->keep(); 177 } 178 } 179 return true; 180} 181 182void SlangRS::initDiagnostic() { 183 clang::DiagnosticsEngine &DiagEngine = getDiagnostics(); 184 185 if (DiagEngine.setDiagnosticGroupMapping("implicit-function-declaration", 186 clang::diag::MAP_ERROR)) 187 DiagEngine.Report(clang::diag::warn_unknown_warning_option) 188 << "implicit-function-declaration"; 189 190 DiagEngine.setDiagnosticMapping( 191 clang::diag::ext_typecheck_convert_discards_qualifiers, 192 clang::diag::MAP_ERROR, 193 clang::SourceLocation()); 194 195 mDiagErrorInvalidOutputDepParameter = 196 DiagEngine.getCustomDiagID( 197 clang::DiagnosticsEngine::Error, 198 "invalid parameter for output dependencies files."); 199 200 mDiagErrorODR = 201 DiagEngine.getCustomDiagID( 202 clang::DiagnosticsEngine::Error, 203 "type '%0' in different translation unit (%1 v.s. %2) " 204 "has incompatible type definition"); 205 206 mDiagErrorTargetAPIRange = 207 DiagEngine.getCustomDiagID( 208 clang::DiagnosticsEngine::Error, 209 "target API level '%0' is out of range ('%1' - '%2')"); 210} 211 212void SlangRS::initPreprocessor() { 213 clang::Preprocessor &PP = getPreprocessor(); 214 215 std::stringstream RSH; 216 RSH << PP.getPredefines(); 217 RSH << "#define RS_VERSION " << mTargetAPI << "\n"; 218 RSH << "#include \"rs_core." RS_HEADER_SUFFIX "\"\n"; 219 PP.setPredefines(RSH.str()); 220} 221 222void SlangRS::initASTContext() { 223 mRSContext = new RSContext(getPreprocessor(), 224 getASTContext(), 225 getTargetInfo(), 226 &mPragmas, 227 mTargetAPI, 228 mVerbose); 229} 230 231clang::ASTConsumer 232*SlangRS::createBackend(const clang::CodeGenOptions& CodeGenOpts, 233 llvm::raw_ostream *OS, 234 Slang::OutputType OT) { 235 return new RSBackend(mRSContext, 236 &getDiagnostics(), 237 CodeGenOpts, 238 getTargetOptions(), 239 &mPragmas, 240 OS, 241 OT, 242 getSourceManager(), 243 mAllowRSPrefix, 244 mIsFilterscript); 245} 246 247bool SlangRS::IsRSHeaderFile(const char *File) { 248#define RS_HEADER_ENTRY(name) \ 249 if (::strcmp(File, #name "." RS_HEADER_SUFFIX) == 0) \ 250 return true; 251ENUM_RS_HEADER() 252#undef RS_HEADER_ENTRY 253 return false; 254} 255 256bool SlangRS::IsLocInRSHeaderFile(const clang::SourceLocation &Loc, 257 const clang::SourceManager &SourceMgr) { 258 clang::FullSourceLoc FSL(Loc, SourceMgr); 259 clang::PresumedLoc PLoc = SourceMgr.getPresumedLoc(FSL); 260 261 const char *Filename = PLoc.getFilename(); 262 if (!Filename) { 263 return false; 264 } else { 265 return IsRSHeaderFile(llvm::sys::path::filename(Filename).data()); 266 } 267} 268 269SlangRS::SlangRS() 270 : Slang(), mRSContext(NULL), mAllowRSPrefix(false), mTargetAPI(0), 271 mVerbose(false), mIsFilterscript(false) { 272} 273 274bool SlangRS::compile( 275 const std::list<std::pair<const char*, const char*> > &IOFiles64, 276 const std::list<std::pair<const char*, const char*> > &IOFiles32, 277 const std::list<std::pair<const char*, const char*> > &DepFiles, 278 const RSCCOptions &Opts) { 279 if (IOFiles32.empty()) 280 return true; 281 282 if (Opts.mEmitDependency && (DepFiles.size() != IOFiles32.size())) { 283 getDiagnostics().Report(mDiagErrorInvalidOutputDepParameter); 284 return false; 285 } 286 287 if (Opts.mEmit3264 && (IOFiles64.size() != IOFiles32.size())) { 288 slangAssert(false && "Should have equal number of 32/64-bit files"); 289 return false; 290 } 291 292 std::string RealPackageName; 293 294 const char *InputFile, *Output64File, *Output32File, *BCOutputFile, 295 *DepOutputFile; 296 std::list<std::pair<const char*, const char*> >::const_iterator 297 IOFile64Iter = IOFiles64.begin(), 298 IOFile32Iter = IOFiles32.begin(), 299 DepFileIter = DepFiles.begin(); 300 301 setIncludePaths(Opts.mIncludePaths); 302 setOutputType(Opts.mOutputType); 303 if (Opts.mEmitDependency) { 304 setAdditionalDepTargets(Opts.mAdditionalDepTargets); 305 } 306 307 setDebugMetadataEmission(Opts.mDebugEmission); 308 309 setOptimizationLevel(Opts.mOptimizationLevel); 310 311 mAllowRSPrefix = Opts.mAllowRSPrefix; 312 313 mTargetAPI = Opts.mTargetAPI; 314 if (mTargetAPI != SLANG_DEVELOPMENT_TARGET_API && 315 (mTargetAPI < SLANG_MINIMUM_TARGET_API || 316 mTargetAPI > SLANG_MAXIMUM_TARGET_API)) { 317 getDiagnostics().Report(mDiagErrorTargetAPIRange) << mTargetAPI 318 << SLANG_MINIMUM_TARGET_API << SLANG_MAXIMUM_TARGET_API; 319 return false; 320 } 321 322 mVerbose = Opts.mVerbose; 323 324 // Skip generation of warnings a second time if we are doing more than just 325 // a single pass over the input file. 326 bool SuppressAllWarnings = (Opts.mOutputType != Slang::OT_Dependency); 327 328 for (unsigned i = 0, e = IOFiles32.size(); i != e; i++) { 329 InputFile = IOFile64Iter->first; 330 Output64File = IOFile64Iter->second; 331 Output32File = IOFile32Iter->second; 332 333 reset(); 334 335 if (!setInputSource(InputFile)) 336 return false; 337 338 if (!setOutput(Output64File)) 339 return false; 340 341 setOutput32(Output32File); 342 343 mIsFilterscript = isFilterscript(InputFile); 344 345 if (Slang::compile() > 0) 346 return false; 347 348 if (!Opts.mJavaReflectionPackageName.empty()) { 349 mRSContext->setReflectJavaPackageName(Opts.mJavaReflectionPackageName); 350 } 351 const std::string &RealPackageName = 352 mRSContext->getReflectJavaPackageName(); 353 354 bool doReflection = true; 355 if (Opts.mEmit3264 && (Opts.mBitWidth == 32)) { 356 // Skip reflection on the 32-bit path if we are going to emit it on the 357 // 64-bit path. 358 doReflection = false; 359 } 360 if (Opts.mOutputType != Slang::OT_Dependency && doReflection) { 361 362 if (Opts.mBitcodeStorage == BCST_CPP_CODE) { 363 RSReflectionCpp R(mRSContext, Opts.mJavaReflectionPathBase, 364 getInputFileName(), getOutputFileName()); 365 if (!R.reflect()) { 366 return false; 367 } 368 } else { 369 if (!Opts.mRSPackageName.empty()) { 370 mRSContext->setRSPackageName(Opts.mRSPackageName); 371 } 372 373 RSReflectionJava R(mRSContext, &mGeneratedFileNames, 374 Opts.mJavaReflectionPathBase, getInputFileName(), 375 getOutputFileName(), 376 Opts.mBitcodeStorage == BCST_JAVA_CODE); 377 if (!R.reflect()) { 378 // TODO Is this needed or will the error message have been printed 379 // already? and why not for the C++ case? 380 fprintf(stderr, "RSContext::reflectToJava : failed to do reflection " 381 "(%s)\n", 382 R.getLastError()); 383 return false; 384 } 385 386 for (std::vector<std::string>::const_iterator 387 I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end(); 388 I != E; 389 I++) { 390 std::string ReflectedName = RSSlangReflectUtils::ComputePackagedPath( 391 Opts.mJavaReflectionPathBase.c_str(), 392 (RealPackageName + OS_PATH_SEPARATOR_STR + *I).c_str()); 393 appendGeneratedFileName(ReflectedName + ".java"); 394 } 395 396 if ((Opts.mOutputType == Slang::OT_Bitcode) && 397 (Opts.mBitcodeStorage == BCST_JAVA_CODE) && 398 !generateJavaBitcodeAccessor(Opts.mJavaReflectionPathBase, 399 RealPackageName.c_str(), 400 mRSContext->getLicenseNote())) { 401 return false; 402 } 403 } 404 } 405 406 if (Opts.mEmitDependency) { 407 BCOutputFile = DepFileIter->first; 408 DepOutputFile = DepFileIter->second; 409 410 setDepTargetBC(BCOutputFile); 411 412 if (!setDepOutput(DepOutputFile)) 413 return false; 414 415 if (SuppressAllWarnings) { 416 getDiagnostics().setSuppressAllDiagnostics(true); 417 } 418 if (generateDepFile() > 0) 419 return false; 420 if (SuppressAllWarnings) { 421 getDiagnostics().setSuppressAllDiagnostics(false); 422 } 423 424 DepFileIter++; 425 } 426 427 if (!checkODR(InputFile)) 428 return false; 429 430 IOFile64Iter++; 431 IOFile32Iter++; 432 } 433 434 return true; 435} 436 437void SlangRS::reset() { 438 delete mRSContext; 439 mRSContext = NULL; 440 mGeneratedFileNames.clear(); 441 Slang::reset(); 442} 443 444SlangRS::~SlangRS() { 445 delete mRSContext; 446 for (ReflectedDefinitionListTy::iterator I = ReflectedDefinitions.begin(), 447 E = ReflectedDefinitions.end(); 448 I != E; 449 I++) { 450 delete I->getValue().first; 451 } 452} 453 454} // namespace slang 455