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