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