| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "CGBlocks.h" |
| 14 | #include "CGCXXABI.h" |
| 15 | #include "CGDebugInfo.h" |
| 16 | #include "CGObjCRuntime.h" |
| 17 | #include "CGOpenCLRuntime.h" |
| 18 | #include "CodeGenFunction.h" |
| 19 | #include "CodeGenModule.h" |
| 20 | #include "ConstantEmitter.h" |
| 21 | #include "TargetInfo.h" |
| 22 | #include "clang/AST/DeclObjC.h" |
| 23 | #include "clang/CodeGen/ConstantInitBuilder.h" |
| 24 | #include "llvm/ADT/SmallSet.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/Module.h" |
| 27 | #include "llvm/Support/ScopedPrinter.h" |
| 28 | #include <algorithm> |
| 29 | #include <cstdio> |
| 30 | |
| 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
| 34 | CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name) |
| 35 | : Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false), |
| 36 | HasCXXObject(false), UsesStret(false), HasCapturedVariableLayout(false), |
| 37 | CapturesNonExternalType(false), LocalAddress(Address::invalid()), |
| 38 | StructureType(nullptr), Block(block), DominatingIP(nullptr) { |
| 39 | |
| 40 | |
| 41 | |
| 42 | if (!name.empty() && name[0] == '\01') |
| 43 | name = name.substr(1); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | BlockByrefHelpers::~BlockByrefHelpers() {} |
| 48 | |
| 49 | |
| 50 | static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, |
| 51 | const CGBlockInfo &blockInfo, |
| 52 | llvm::Constant *blockFn); |
| 53 | |
| 54 | |
| 55 | static llvm::Constant *buildCopyHelper(CodeGenModule &CGM, |
| 56 | const CGBlockInfo &blockInfo) { |
| 57 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM, |
| 62 | const CGBlockInfo &blockInfo) { |
| 63 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo); |
| 64 | } |
| 65 | |
| 66 | namespace { |
| 67 | |
| 68 | |
| 69 | |
| 70 | enum class BlockCaptureEntityKind { |
| 71 | CXXRecord, |
| 72 | ARCWeak, |
| 73 | ARCStrong, |
| 74 | NonTrivialCStruct, |
| 75 | BlockObject, |
| 76 | None |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | |
| 81 | struct BlockCaptureManagedEntity { |
| 82 | BlockCaptureEntityKind CopyKind, DisposeKind; |
| 83 | BlockFieldFlags CopyFlags, DisposeFlags; |
| 84 | const BlockDecl::Capture *CI; |
| 85 | const CGBlockInfo::Capture *Capture; |
| 86 | |
| 87 | BlockCaptureManagedEntity(BlockCaptureEntityKind CopyType, |
| 88 | BlockCaptureEntityKind DisposeType, |
| 89 | BlockFieldFlags CopyFlags, |
| 90 | BlockFieldFlags DisposeFlags, |
| 91 | const BlockDecl::Capture &CI, |
| 92 | const CGBlockInfo::Capture &Capture) |
| 93 | : CopyKind(CopyType), DisposeKind(DisposeType), CopyFlags(CopyFlags), |
| 94 | DisposeFlags(DisposeFlags), CI(&CI), Capture(&Capture) {} |
| 95 | |
| 96 | bool operator<(const BlockCaptureManagedEntity &Other) const { |
| 97 | return Capture->getOffset() < Other.Capture->getOffset(); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | enum class CaptureStrKind { |
| 102 | |
| 103 | CopyHelper, |
| 104 | |
| 105 | DisposeHelper, |
| 106 | |
| 107 | Merged |
| 108 | }; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | static void findBlockCapturedManagedEntities( |
| 113 | const CGBlockInfo &BlockInfo, const LangOptions &LangOpts, |
| 114 | SmallVectorImpl<BlockCaptureManagedEntity> &ManagedCaptures); |
| 115 | |
| 116 | static std::string getBlockCaptureStr(const BlockCaptureManagedEntity &E, |
| 117 | CaptureStrKind StrKind, |
| 118 | CharUnits BlockAlignment, |
| 119 | CodeGenModule &CGM); |
| 120 | |
| 121 | static std::string getBlockDescriptorName(const CGBlockInfo &BlockInfo, |
| 122 | CodeGenModule &CGM) { |
| 123 | std::string Name = "__block_descriptor_"; |
| 124 | Name += llvm::to_string(BlockInfo.BlockSize.getQuantity()) + "_"; |
| 125 | |
| 126 | if (BlockInfo.needsCopyDisposeHelpers()) { |
| 127 | if (CGM.getLangOpts().Exceptions) |
| 128 | Name += "e"; |
| 129 | if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) |
| 130 | Name += "a"; |
| 131 | Name += llvm::to_string(BlockInfo.BlockAlign.getQuantity()) + "_"; |
| 132 | |
| 133 | SmallVector<BlockCaptureManagedEntity, 4> ManagedCaptures; |
| 134 | findBlockCapturedManagedEntities(BlockInfo, CGM.getContext().getLangOpts(), |
| 135 | ManagedCaptures); |
| 136 | |
| 137 | for (const BlockCaptureManagedEntity &E : ManagedCaptures) { |
| 138 | Name += llvm::to_string(E.Capture->getOffset().getQuantity()); |
| 139 | |
| 140 | if (E.CopyKind == E.DisposeKind) { |
| 141 | |
| 142 | |
| 143 | (0) . __assert_fail ("E.CopyKind != BlockCaptureEntityKind..None && \"shouldn't see BlockCaptureManagedEntity that is None\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 144, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E.CopyKind != BlockCaptureEntityKind::None && |
| 144 | (0) . __assert_fail ("E.CopyKind != BlockCaptureEntityKind..None && \"shouldn't see BlockCaptureManagedEntity that is None\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 144, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "shouldn't see BlockCaptureManagedEntity that is None"); |
| 145 | Name += getBlockCaptureStr(E, CaptureStrKind::Merged, |
| 146 | BlockInfo.BlockAlign, CGM); |
| 147 | } else { |
| 148 | |
| 149 | |
| 150 | |
| 151 | Name += getBlockCaptureStr(E, CaptureStrKind::CopyHelper, |
| 152 | BlockInfo.BlockAlign, CGM); |
| 153 | Name += getBlockCaptureStr(E, CaptureStrKind::DisposeHelper, |
| 154 | BlockInfo.BlockAlign, CGM); |
| 155 | } |
| 156 | } |
| 157 | Name += "_"; |
| 158 | } |
| 159 | |
| 160 | std::string TypeAtEncoding = |
| 161 | CGM.getContext().getObjCEncodingForBlock(BlockInfo.getBlockExpr()); |
| 162 | |
| 163 | |
| 164 | std::replace(TypeAtEncoding.begin(), TypeAtEncoding.end(), '@', '\1'); |
| 165 | Name += "e" + llvm::to_string(TypeAtEncoding.size()) + "_" + TypeAtEncoding; |
| 166 | Name += "l" + CGM.getObjCRuntime().getRCBlockLayoutStr(CGM, BlockInfo); |
| 167 | return Name; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM, |
| 185 | const CGBlockInfo &blockInfo) { |
| 186 | ASTContext &C = CGM.getContext(); |
| 187 | |
| 188 | llvm::IntegerType *ulong = |
| 189 | cast<llvm::IntegerType>(CGM.getTypes().ConvertType(C.UnsignedLongTy)); |
| 190 | llvm::PointerType *i8p = nullptr; |
| 191 | if (CGM.getLangOpts().OpenCL) |
| 192 | i8p = |
| 193 | llvm::Type::getInt8PtrTy( |
| 194 | CGM.getLLVMContext(), C.getTargetAddressSpace(LangAS::opencl_constant)); |
| 195 | else |
| 196 | i8p = CGM.VoidPtrTy; |
| 197 | |
| 198 | std::string descName; |
| 199 | |
| 200 | |
| 201 | if (C.getLangOpts().ObjC && |
| 202 | CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
| 203 | descName = getBlockDescriptorName(blockInfo, CGM); |
| 204 | if (llvm::GlobalValue *desc = CGM.getModule().getNamedValue(descName)) |
| 205 | return llvm::ConstantExpr::getBitCast(desc, |
| 206 | CGM.getBlockDescriptorType()); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | ConstantInitBuilder builder(CGM); |
| 212 | auto elements = builder.beginStruct(); |
| 213 | |
| 214 | |
| 215 | elements.addInt(ulong, 0); |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | elements.addInt(ulong, blockInfo.BlockSize.getQuantity()); |
| 222 | |
| 223 | |
| 224 | bool hasInternalHelper = false; |
| 225 | if (blockInfo.needsCopyDisposeHelpers()) { |
| 226 | |
| 227 | llvm::Constant *copyHelper = buildCopyHelper(CGM, blockInfo); |
| 228 | elements.add(copyHelper); |
| 229 | |
| 230 | |
| 231 | llvm::Constant *disposeHelper = buildDisposeHelper(CGM, blockInfo); |
| 232 | elements.add(disposeHelper); |
| 233 | |
| 234 | if (cast<llvm::Function>(copyHelper->getOperand(0))->hasInternalLinkage() || |
| 235 | cast<llvm::Function>(disposeHelper->getOperand(0)) |
| 236 | ->hasInternalLinkage()) |
| 237 | hasInternalHelper = true; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | std::string typeAtEncoding = |
| 242 | CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr()); |
| 243 | elements.add(llvm::ConstantExpr::getBitCast( |
| 244 | CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer(), i8p)); |
| 245 | |
| 246 | |
| 247 | if (C.getLangOpts().ObjC) { |
| 248 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) |
| 249 | elements.add(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo)); |
| 250 | else |
| 251 | elements.add(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo)); |
| 252 | } |
| 253 | else |
| 254 | elements.addNullPointer(i8p); |
| 255 | |
| 256 | unsigned AddrSpace = 0; |
| 257 | if (C.getLangOpts().OpenCL) |
| 258 | AddrSpace = C.getTargetAddressSpace(LangAS::opencl_constant); |
| 259 | |
| 260 | llvm::GlobalValue::LinkageTypes linkage; |
| 261 | if (descName.empty()) { |
| 262 | linkage = llvm::GlobalValue::InternalLinkage; |
| 263 | descName = "__block_descriptor_tmp"; |
| 264 | } else if (hasInternalHelper) { |
| 265 | |
| 266 | |
| 267 | linkage = llvm::GlobalValue::InternalLinkage; |
| 268 | } else { |
| 269 | linkage = llvm::GlobalValue::LinkOnceODRLinkage; |
| 270 | } |
| 271 | |
| 272 | llvm::GlobalVariable *global = |
| 273 | elements.finishAndCreateGlobal(descName, CGM.getPointerAlign(), |
| 274 | true, linkage, AddrSpace); |
| 275 | |
| 276 | if (linkage == llvm::GlobalValue::LinkOnceODRLinkage) { |
| 277 | global->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 278 | global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 279 | } |
| 280 | |
| 281 | return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType()); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | namespace { |
| 336 | |
| 337 | struct BlockLayoutChunk { |
| 338 | CharUnits Alignment; |
| 339 | CharUnits Size; |
| 340 | Qualifiers::ObjCLifetime Lifetime; |
| 341 | const BlockDecl::Capture *Capture; |
| 342 | llvm::Type *Type; |
| 343 | QualType FieldType; |
| 344 | |
| 345 | BlockLayoutChunk(CharUnits align, CharUnits size, |
| 346 | Qualifiers::ObjCLifetime lifetime, |
| 347 | const BlockDecl::Capture *capture, |
| 348 | llvm::Type *type, QualType fieldType) |
| 349 | : Alignment(align), Size(size), Lifetime(lifetime), |
| 350 | Capture(capture), Type(type), FieldType(fieldType) {} |
| 351 | |
| 352 | |
| 353 | void setIndex(CGBlockInfo &info, unsigned index, CharUnits offset) { |
| 354 | if (!Capture) { |
| 355 | info.CXXThisIndex = index; |
| 356 | info.CXXThisOffset = offset; |
| 357 | } else { |
| 358 | auto C = CGBlockInfo::Capture::makeIndex(index, offset, FieldType); |
| 359 | info.Captures.insert({Capture->getVariable(), C}); |
| 360 | } |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | |
| 365 | |
| 366 | bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) { |
| 367 | if (left.Alignment != right.Alignment) |
| 368 | return left.Alignment > right.Alignment; |
| 369 | |
| 370 | auto getPrefOrder = [](const BlockLayoutChunk &chunk) { |
| 371 | if (chunk.Capture && chunk.Capture->isByRef()) |
| 372 | return 1; |
| 373 | if (chunk.Lifetime == Qualifiers::OCL_Strong) |
| 374 | return 0; |
| 375 | if (chunk.Lifetime == Qualifiers::OCL_Weak) |
| 376 | return 2; |
| 377 | return 3; |
| 378 | }; |
| 379 | |
| 380 | return getPrefOrder(left) < getPrefOrder(right); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | |
| 385 | static bool isSafeForCXXConstantCapture(QualType type) { |
| 386 | const RecordType *recordType = |
| 387 | type->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 388 | |
| 389 | |
| 390 | if (!recordType) return true; |
| 391 | |
| 392 | const auto *record = cast<CXXRecordDecl>(recordType->getDecl()); |
| 393 | |
| 394 | |
| 395 | if (!record->hasTrivialDestructor()) return false; |
| 396 | if (record->hasNonTrivialCopyConstructor()) return false; |
| 397 | |
| 398 | |
| 399 | |
| 400 | return !record->hasMutableFields(); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM, |
| 410 | CodeGenFunction *CGF, |
| 411 | const VarDecl *var) { |
| 412 | |
| 413 | |
| 414 | if (isa<ParmVarDecl>(var)) |
| 415 | return nullptr; |
| 416 | |
| 417 | QualType type = var->getType(); |
| 418 | |
| 419 | |
| 420 | if (!type.isConstQualified()) return nullptr; |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type)) |
| 428 | return nullptr; |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | const Expr *init = var->getInit(); |
| 434 | if (!init) return nullptr; |
| 435 | |
| 436 | return ConstantEmitter(CGM, CGF).tryEmitAbstractForInitializer(*var); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | |
| 441 | static CharUnits getLowBit(CharUnits v) { |
| 442 | return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1)); |
| 443 | } |
| 444 | |
| 445 | static void (CodeGenModule &CGM, CGBlockInfo &info, |
| 446 | SmallVectorImpl<llvm::Type*> &elementTypes) { |
| 447 | |
| 448 | assert(elementTypes.empty()); |
| 449 | if (CGM.getLangOpts().OpenCL) { |
| 450 | |
| 451 | |
| 452 | auto GenericAS = |
| 453 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic); |
| 454 | auto GenPtrAlign = |
| 455 | CharUnits::fromQuantity(CGM.getTarget().getPointerAlign(GenericAS) / 8); |
| 456 | auto GenPtrSize = |
| 457 | CharUnits::fromQuantity(CGM.getTarget().getPointerWidth(GenericAS) / 8); |
| 458 | assert(CGM.getIntSize() <= GenPtrSize); |
| 459 | assert(CGM.getIntAlign() <= GenPtrAlign); |
| 460 | assert((2 * CGM.getIntSize()).isMultipleOf(GenPtrAlign)); |
| 461 | elementTypes.push_back(CGM.IntTy); |
| 462 | elementTypes.push_back(CGM.IntTy); |
| 463 | elementTypes.push_back( |
| 464 | CGM.getOpenCLRuntime() |
| 465 | .getGenericVoidPointerType()); |
| 466 | unsigned Offset = |
| 467 | 2 * CGM.getIntSize().getQuantity() + GenPtrSize.getQuantity(); |
| 468 | unsigned BlockAlign = GenPtrAlign.getQuantity(); |
| 469 | if (auto *Helper = |
| 470 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
| 471 | for (auto I : Helper->getCustomFieldTypes()) { |
| 472 | |
| 473 | |
| 474 | unsigned Align = CGM.getDataLayout().getABITypeAlignment(I); |
| 475 | if (BlockAlign < Align) |
| 476 | BlockAlign = Align; |
| 477 | assert(Offset % Align == 0); |
| 478 | Offset += CGM.getDataLayout().getTypeAllocSize(I); |
| 479 | elementTypes.push_back(I); |
| 480 | } |
| 481 | } |
| 482 | info.BlockAlign = CharUnits::fromQuantity(BlockAlign); |
| 483 | info.BlockSize = CharUnits::fromQuantity(Offset); |
| 484 | } else { |
| 485 | |
| 486 | |
| 487 | assert(CGM.getIntSize() <= CGM.getPointerSize()); |
| 488 | assert(CGM.getIntAlign() <= CGM.getPointerAlign()); |
| 489 | assert((2 * CGM.getIntSize()).isMultipleOf(CGM.getPointerAlign())); |
| 490 | info.BlockAlign = CGM.getPointerAlign(); |
| 491 | info.BlockSize = 3 * CGM.getPointerSize() + 2 * CGM.getIntSize(); |
| 492 | elementTypes.push_back(CGM.VoidPtrTy); |
| 493 | elementTypes.push_back(CGM.IntTy); |
| 494 | elementTypes.push_back(CGM.IntTy); |
| 495 | elementTypes.push_back(CGM.VoidPtrTy); |
| 496 | elementTypes.push_back(CGM.getBlockDescriptorType()); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | static QualType getCaptureFieldType(const CodeGenFunction &CGF, |
| 501 | const BlockDecl::Capture &CI) { |
| 502 | const VarDecl *VD = CI.getVariable(); |
| 503 | |
| 504 | |
| 505 | |
| 506 | if (CGF.BlockInfo && CI.isNested()) |
| 507 | return CGF.BlockInfo->getCapture(VD).fieldType(); |
| 508 | if (auto *FD = CGF.LambdaCaptureFields.lookup(VD)) |
| 509 | return FD->getType(); |
| 510 | |
| 511 | |
| 512 | |
| 513 | return VD->isNonEscapingByref() ? |
| 514 | CGF.getContext().getLValueReferenceType(VD->getType()) : VD->getType(); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | |
| 519 | static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF, |
| 520 | CGBlockInfo &info) { |
| 521 | ASTContext &C = CGM.getContext(); |
| 522 | const BlockDecl *block = info.getBlockDecl(); |
| 523 | |
| 524 | SmallVector<llvm::Type*, 8> elementTypes; |
| 525 | initializeForBlockHeader(CGM, info, elementTypes); |
| 526 | bool hasNonConstantCustomFields = false; |
| 527 | if (auto *OpenCLHelper = |
| 528 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) |
| 529 | hasNonConstantCustomFields = |
| 530 | !OpenCLHelper->areAllCustomFieldValuesConstant(info); |
| 531 | if (!block->hasCaptures() && !hasNonConstantCustomFields) { |
| 532 | info.StructureType = |
| 533 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 534 | info.CanBeGlobal = true; |
| 535 | return; |
| 536 | } |
| 537 | else if (C.getLangOpts().ObjC && |
| 538 | CGM.getLangOpts().getGC() == LangOptions::NonGC) |
| 539 | info.HasCapturedVariableLayout = true; |
| 540 | |
| 541 | |
| 542 | SmallVector<BlockLayoutChunk, 16> layout; |
| 543 | layout.reserve(block->capturesCXXThis() + |
| 544 | (block->capture_end() - block->capture_begin())); |
| 545 | |
| 546 | CharUnits maxFieldAlign; |
| 547 | |
| 548 | |
| 549 | if (block->capturesCXXThis()) { |
| 550 | (0) . __assert_fail ("CGF && CGF->CurFuncDecl && isa(CGF->CurFuncDecl) && \"Can't capture 'this' outside a method\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 551, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CGF && CGF->CurFuncDecl && isa<CXXMethodDecl>(CGF->CurFuncDecl) && |
| 551 | (0) . __assert_fail ("CGF && CGF->CurFuncDecl && isa(CGF->CurFuncDecl) && \"Can't capture 'this' outside a method\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 551, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Can't capture 'this' outside a method"); |
| 552 | QualType thisType = cast<CXXMethodDecl>(CGF->CurFuncDecl)->getThisType(); |
| 553 | |
| 554 | |
| 555 | |
| 556 | llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType); |
| 557 | std::pair<CharUnits,CharUnits> tinfo |
| 558 | = CGM.getContext().getTypeInfoInChars(thisType); |
| 559 | maxFieldAlign = std::max(maxFieldAlign, tinfo.second); |
| 560 | |
| 561 | layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, |
| 562 | Qualifiers::OCL_None, |
| 563 | nullptr, llvmType, thisType)); |
| 564 | } |
| 565 | |
| 566 | |
| 567 | for (const auto &CI : block->captures()) { |
| 568 | const VarDecl *variable = CI.getVariable(); |
| 569 | |
| 570 | if (CI.isEscapingByref()) { |
| 571 | |
| 572 | info.NeedsCopyDispose = true; |
| 573 | |
| 574 | |
| 575 | CharUnits align = CGM.getPointerAlign(); |
| 576 | maxFieldAlign = std::max(maxFieldAlign, align); |
| 577 | |
| 578 | |
| 579 | |
| 580 | (0) . __assert_fail ("getCaptureFieldType(*CGF, CI) == variable->getType() && \"capture type differs from the variable type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 581, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getCaptureFieldType(*CGF, CI) == variable->getType() && |
| 581 | (0) . __assert_fail ("getCaptureFieldType(*CGF, CI) == variable->getType() && \"capture type differs from the variable type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 581, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "capture type differs from the variable type"); |
| 582 | layout.push_back(BlockLayoutChunk(align, CGM.getPointerSize(), |
| 583 | Qualifiers::OCL_None, &CI, |
| 584 | CGM.VoidPtrTy, variable->getType())); |
| 585 | continue; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | |
| 590 | if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) { |
| 591 | info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant); |
| 592 | continue; |
| 593 | } |
| 594 | |
| 595 | QualType VT = getCaptureFieldType(*CGF, CI); |
| 596 | |
| 597 | |
| 598 | |
| 599 | Qualifiers::ObjCLifetime lifetime = VT.getObjCLifetime(); |
| 600 | if (lifetime) { |
| 601 | switch (lifetime) { |
| 602 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 603 | case Qualifiers::OCL_ExplicitNone: |
| 604 | case Qualifiers::OCL_Autoreleasing: |
| 605 | break; |
| 606 | |
| 607 | case Qualifiers::OCL_Strong: |
| 608 | case Qualifiers::OCL_Weak: |
| 609 | info.NeedsCopyDispose = true; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | } else if (VT->isObjCRetainableType()) { |
| 614 | |
| 615 | |
| 616 | if (VT->isObjCInertUnsafeUnretainedType()) { |
| 617 | lifetime = Qualifiers::OCL_ExplicitNone; |
| 618 | } else { |
| 619 | info.NeedsCopyDispose = true; |
| 620 | |
| 621 | lifetime = Qualifiers::OCL_Strong; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | } else if (CI.hasCopyExpr()) { |
| 626 | info.NeedsCopyDispose = true; |
| 627 | info.HasCXXObject = true; |
| 628 | if (!VT->getAsCXXRecordDecl()->isExternallyVisible()) |
| 629 | info.CapturesNonExternalType = true; |
| 630 | |
| 631 | |
| 632 | |
| 633 | } else if (VT.isNonTrivialToPrimitiveCopy() == QualType::PCK_Struct || |
| 634 | VT.isDestructedType() == QualType::DK_nontrivial_c_struct) { |
| 635 | info.NeedsCopyDispose = true; |
| 636 | |
| 637 | |
| 638 | } else if (CGM.getLangOpts().CPlusPlus) { |
| 639 | if (const CXXRecordDecl *record = VT->getAsCXXRecordDecl()) { |
| 640 | if (!record->hasTrivialDestructor()) { |
| 641 | info.HasCXXObject = true; |
| 642 | info.NeedsCopyDispose = true; |
| 643 | if (!record->isExternallyVisible()) |
| 644 | info.CapturesNonExternalType = true; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | CharUnits size = C.getTypeSizeInChars(VT); |
| 650 | CharUnits align = C.getDeclAlign(variable); |
| 651 | |
| 652 | maxFieldAlign = std::max(maxFieldAlign, align); |
| 653 | |
| 654 | llvm::Type *llvmType = |
| 655 | CGM.getTypes().ConvertTypeForMem(VT); |
| 656 | |
| 657 | layout.push_back( |
| 658 | BlockLayoutChunk(align, size, lifetime, &CI, llvmType, VT)); |
| 659 | } |
| 660 | |
| 661 | |
| 662 | if (layout.empty()) { |
| 663 | info.StructureType = |
| 664 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 665 | info.CanBeGlobal = true; |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | std::stable_sort(layout.begin(), layout.end()); |
| 673 | |
| 674 | |
| 675 | info.BlockHeaderForcedGapOffset = info.BlockSize; |
| 676 | info.BlockHeaderForcedGapSize = CharUnits::Zero(); |
| 677 | |
| 678 | CharUnits &blockSize = info.BlockSize; |
| 679 | info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign); |
| 680 | |
| 681 | |
| 682 | |
| 683 | CharUnits endAlign = getLowBit(blockSize); |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | if (endAlign < maxFieldAlign) { |
| 697 | SmallVectorImpl<BlockLayoutChunk>::iterator |
| 698 | li = layout.begin() + 1, le = layout.end(); |
| 699 | |
| 700 | |
| 701 | |
| 702 | for (; li != le && endAlign < li->Alignment; ++li) |
| 703 | ; |
| 704 | |
| 705 | |
| 706 | |
| 707 | if (li != le) { |
| 708 | SmallVectorImpl<BlockLayoutChunk>::iterator first = li; |
| 709 | for (; li != le; ++li) { |
| 710 | = li->Alignment", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 710, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(endAlign >= li->Alignment); |
| 711 | |
| 712 | li->setIndex(info, elementTypes.size(), blockSize); |
| 713 | elementTypes.push_back(li->Type); |
| 714 | blockSize += li->Size; |
| 715 | endAlign = getLowBit(blockSize); |
| 716 | |
| 717 | |
| 718 | if (endAlign >= maxFieldAlign) { |
| 719 | break; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | layout.erase(first, li); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | assert(endAlign == getLowBit(blockSize)); |
| 728 | |
| 729 | |
| 730 | |
| 731 | if (endAlign < maxFieldAlign) { |
| 732 | CharUnits newBlockSize = blockSize.alignTo(maxFieldAlign); |
| 733 | CharUnits padding = newBlockSize - blockSize; |
| 734 | |
| 735 | |
| 736 | |
| 737 | if (blockSize == info.BlockHeaderForcedGapOffset) { |
| 738 | info.BlockHeaderForcedGapSize = padding; |
| 739 | } |
| 740 | |
| 741 | elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, |
| 742 | padding.getQuantity())); |
| 743 | blockSize = newBlockSize; |
| 744 | endAlign = getLowBit(blockSize); |
| 745 | } |
| 746 | |
| 747 | = maxFieldAlign", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 747, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(endAlign >= maxFieldAlign); |
| 748 | assert(endAlign == getLowBit(blockSize)); |
| 749 | |
| 750 | |
| 751 | |
| 752 | for (SmallVectorImpl<BlockLayoutChunk>::iterator |
| 753 | li = layout.begin(), le = layout.end(); li != le; ++li) { |
| 754 | if (endAlign < li->Alignment) { |
| 755 | |
| 756 | |
| 757 | |
| 758 | CharUnits padding = li->Alignment - endAlign; |
| 759 | elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, |
| 760 | padding.getQuantity())); |
| 761 | blockSize += padding; |
| 762 | endAlign = getLowBit(blockSize); |
| 763 | } |
| 764 | = li->Alignment", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 764, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(endAlign >= li->Alignment); |
| 765 | li->setIndex(info, elementTypes.size(), blockSize); |
| 766 | elementTypes.push_back(li->Type); |
| 767 | blockSize += li->Size; |
| 768 | endAlign = getLowBit(blockSize); |
| 769 | } |
| 770 | |
| 771 | info.StructureType = |
| 772 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 773 | } |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | static void enterBlockScope(CodeGenFunction &CGF, BlockDecl *block) { |
| 779 | assert(CGF.HaveInsertPoint()); |
| 780 | |
| 781 | |
| 782 | CGBlockInfo &blockInfo = |
| 783 | *new CGBlockInfo(block, CGF.CurFn->getName()); |
| 784 | blockInfo.NextBlockInfo = CGF.FirstBlockInfo; |
| 785 | CGF.FirstBlockInfo = &blockInfo; |
| 786 | |
| 787 | |
| 788 | |
| 789 | computeBlockInfo(CGF.CGM, &CGF, blockInfo); |
| 790 | |
| 791 | |
| 792 | if (blockInfo.CanBeGlobal) return; |
| 793 | |
| 794 | |
| 795 | blockInfo.LocalAddress = CGF.CreateTempAlloca(blockInfo.StructureType, |
| 796 | blockInfo.BlockAlign, "block"); |
| 797 | |
| 798 | |
| 799 | if (!blockInfo.NeedsCopyDispose) return; |
| 800 | |
| 801 | |
| 802 | |
| 803 | for (const auto &CI : block->captures()) { |
| 804 | |
| 805 | |
| 806 | if (CI.isByRef()) continue; |
| 807 | |
| 808 | |
| 809 | const VarDecl *variable = CI.getVariable(); |
| 810 | CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 811 | if (capture.isConstant()) continue; |
| 812 | |
| 813 | |
| 814 | QualType VT = getCaptureFieldType(CGF, CI); |
| 815 | QualType::DestructionKind dtorKind = VT.isDestructedType(); |
| 816 | if (dtorKind == QualType::DK_none) continue; |
| 817 | |
| 818 | CodeGenFunction::Destroyer *destroyer; |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | if (VT.isConstQualified() && |
| 827 | VT.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 828 | CGF.CGM.getCodeGenOpts().OptimizationLevel != 0) { |
| 829 | (0) . __assert_fail ("CGF.CGM.getLangOpts().ObjCAutoRefCount && \"expected ObjC ARC to be enabled\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 830, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CGF.CGM.getLangOpts().ObjCAutoRefCount && |
| 830 | (0) . __assert_fail ("CGF.CGM.getLangOpts().ObjCAutoRefCount && \"expected ObjC ARC to be enabled\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 830, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "expected ObjC ARC to be enabled"); |
| 831 | destroyer = CodeGenFunction::emitARCIntrinsicUse; |
| 832 | } else if (dtorKind == QualType::DK_objc_strong_lifetime) { |
| 833 | destroyer = CodeGenFunction::destroyARCStrongImprecise; |
| 834 | } else { |
| 835 | destroyer = CGF.getDestroyer(dtorKind); |
| 836 | } |
| 837 | |
| 838 | |
| 839 | Address addr = |
| 840 | CGF.Builder.CreateStructGEP(blockInfo.LocalAddress, capture.getIndex()); |
| 841 | |
| 842 | |
| 843 | if (!blockInfo.DominatingIP) |
| 844 | blockInfo.DominatingIP = cast<llvm::Instruction>(addr.getPointer()); |
| 845 | |
| 846 | CleanupKind cleanupKind = InactiveNormalCleanup; |
| 847 | bool useArrayEHCleanup = CGF.needsEHCleanup(dtorKind); |
| 848 | if (useArrayEHCleanup) |
| 849 | cleanupKind = InactiveNormalAndEHCleanup; |
| 850 | |
| 851 | CGF.pushDestroy(cleanupKind, addr, VT, |
| 852 | destroyer, useArrayEHCleanup); |
| 853 | |
| 854 | |
| 855 | capture.setCleanup(CGF.EHStack.stable_begin()); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | void CodeGenFunction::enterNonTrivialFullExpression(const FullExpr *E) { |
| 863 | if (const auto EWC = dyn_cast<ExprWithCleanups>(E)) { |
| 864 | getNumObjects() != 0", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 864, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(EWC->getNumObjects() != 0); |
| 865 | for (const ExprWithCleanups::CleanupObject &C : EWC->getObjects()) |
| 866 | enterBlockScope(*this, C); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | |
| 871 | static CGBlockInfo *findAndRemoveBlockInfo(CGBlockInfo **head, |
| 872 | const BlockDecl *block) { |
| 873 | while (true) { |
| 874 | assert(head && *head); |
| 875 | CGBlockInfo *cur = *head; |
| 876 | |
| 877 | |
| 878 | if (cur->getBlockDecl() == block) { |
| 879 | *head = cur->NextBlockInfo; |
| 880 | return cur; |
| 881 | } |
| 882 | |
| 883 | head = &cur->NextBlockInfo; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
| 888 | void CodeGenFunction::destroyBlockInfos(CGBlockInfo *head) { |
| 889 | (0) . __assert_fail ("head && \"destroying an empty chain\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 889, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(head && "destroying an empty chain"); |
| 890 | do { |
| 891 | CGBlockInfo *cur = head; |
| 892 | head = cur->NextBlockInfo; |
| 893 | delete cur; |
| 894 | } while (head != nullptr); |
| 895 | } |
| 896 | |
| 897 | |
| 898 | llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) { |
| 899 | |
| 900 | |
| 901 | if (!blockExpr->getBlockDecl()->hasCaptures()) { |
| 902 | |
| 903 | |
| 904 | if (llvm::Constant *Block = CGM.getAddrOfGlobalBlockIfEmitted(blockExpr)) { |
| 905 | return Block; |
| 906 | } |
| 907 | CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName()); |
| 908 | computeBlockInfo(CGM, this, blockInfo); |
| 909 | blockInfo.BlockExpression = blockExpr; |
| 910 | return EmitBlockLiteral(blockInfo); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | std::unique_ptr<CGBlockInfo> blockInfo; |
| 915 | blockInfo.reset(findAndRemoveBlockInfo(&FirstBlockInfo, |
| 916 | blockExpr->getBlockDecl())); |
| 917 | |
| 918 | blockInfo->BlockExpression = blockExpr; |
| 919 | return EmitBlockLiteral(*blockInfo); |
| 920 | } |
| 921 | |
| 922 | llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) { |
| 923 | bool IsOpenCL = CGM.getContext().getLangOpts().OpenCL; |
| 924 | auto GenVoidPtrTy = |
| 925 | IsOpenCL ? CGM.getOpenCLRuntime().getGenericVoidPointerType() : VoidPtrTy; |
| 926 | LangAS GenVoidPtrAddr = IsOpenCL ? LangAS::opencl_generic : LangAS::Default; |
| 927 | auto GenVoidPtrSize = CharUnits::fromQuantity( |
| 928 | CGM.getTarget().getPointerWidth( |
| 929 | CGM.getContext().getTargetAddressSpace(GenVoidPtrAddr)) / |
| 930 | 8); |
| 931 | |
| 932 | bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda(); |
| 933 | CodeGenFunction BlockCGF{CGM, true}; |
| 934 | BlockCGF.SanOpts = SanOpts; |
| 935 | auto *InvokeFn = BlockCGF.GenerateBlockFunction( |
| 936 | CurGD, blockInfo, LocalDeclMap, isLambdaConv, blockInfo.CanBeGlobal); |
| 937 | auto *blockFn = llvm::ConstantExpr::getPointerCast(InvokeFn, GenVoidPtrTy); |
| 938 | |
| 939 | |
| 940 | if (blockInfo.CanBeGlobal) |
| 941 | return CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression); |
| 942 | |
| 943 | |
| 944 | |
| 945 | Address blockAddr = blockInfo.LocalAddress; |
| 946 | (0) . __assert_fail ("blockAddr.isValid() && \"block has no address!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 946, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(blockAddr.isValid() && "block has no address!"); |
| 947 | |
| 948 | llvm::Constant *isa; |
| 949 | llvm::Constant *descriptor; |
| 950 | BlockFlags flags; |
| 951 | if (!IsOpenCL) { |
| 952 | |
| 953 | |
| 954 | |
| 955 | llvm::Constant *blockISA = blockInfo.getBlockDecl()->doesNotEscape() |
| 956 | ? CGM.getNSConcreteGlobalBlock() |
| 957 | : CGM.getNSConcreteStackBlock(); |
| 958 | isa = llvm::ConstantExpr::getBitCast(blockISA, VoidPtrTy); |
| 959 | |
| 960 | |
| 961 | descriptor = buildBlockDescriptor(CGM, blockInfo); |
| 962 | |
| 963 | |
| 964 | flags = BLOCK_HAS_SIGNATURE; |
| 965 | if (blockInfo.HasCapturedVariableLayout) |
| 966 | flags |= BLOCK_HAS_EXTENDED_LAYOUT; |
| 967 | if (blockInfo.needsCopyDisposeHelpers()) |
| 968 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 969 | if (blockInfo.HasCXXObject) |
| 970 | flags |= BLOCK_HAS_CXX_OBJ; |
| 971 | if (blockInfo.UsesStret) |
| 972 | flags |= BLOCK_USE_STRET; |
| 973 | if (blockInfo.getBlockDecl()->doesNotEscape()) |
| 974 | flags |= BLOCK_IS_NOESCAPE | BLOCK_IS_GLOBAL; |
| 975 | } |
| 976 | |
| 977 | auto projectField = [&](unsigned index, const Twine &name) -> Address { |
| 978 | return Builder.CreateStructGEP(blockAddr, index, name); |
| 979 | }; |
| 980 | auto storeField = [&](llvm::Value *value, unsigned index, const Twine &name) { |
| 981 | Builder.CreateStore(value, projectField(index, name)); |
| 982 | }; |
| 983 | |
| 984 | |
| 985 | { |
| 986 | |
| 987 | unsigned index = 0; |
| 988 | CharUnits offset; |
| 989 | auto = [&](llvm::Value *value, CharUnits size, |
| 990 | const Twine &name) { |
| 991 | storeField(value, index, name); |
| 992 | offset += size; |
| 993 | index++; |
| 994 | }; |
| 995 | |
| 996 | if (!IsOpenCL) { |
| 997 | addHeaderField(isa, getPointerSize(), "block.isa"); |
| 998 | addHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()), |
| 999 | getIntSize(), "block.flags"); |
| 1000 | addHeaderField(llvm::ConstantInt::get(IntTy, 0), getIntSize(), |
| 1001 | "block.reserved"); |
| 1002 | } else { |
| 1003 | addHeaderField( |
| 1004 | llvm::ConstantInt::get(IntTy, blockInfo.BlockSize.getQuantity()), |
| 1005 | getIntSize(), "block.size"); |
| 1006 | addHeaderField( |
| 1007 | llvm::ConstantInt::get(IntTy, blockInfo.BlockAlign.getQuantity()), |
| 1008 | getIntSize(), "block.align"); |
| 1009 | } |
| 1010 | addHeaderField(blockFn, GenVoidPtrSize, "block.invoke"); |
| 1011 | if (!IsOpenCL) |
| 1012 | addHeaderField(descriptor, getPointerSize(), "block.descriptor"); |
| 1013 | else if (auto *Helper = |
| 1014 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
| 1015 | for (auto I : Helper->getCustomFieldValues(*this, blockInfo)) { |
| 1016 | addHeaderField( |
| 1017 | I.first, |
| 1018 | CharUnits::fromQuantity( |
| 1019 | CGM.getDataLayout().getTypeAllocSize(I.first->getType())), |
| 1020 | I.second); |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1027 | |
| 1028 | |
| 1029 | if (blockDecl->capturesCXXThis()) { |
| 1030 | Address addr = |
| 1031 | projectField(blockInfo.CXXThisIndex, "block.captured-this.addr"); |
| 1032 | Builder.CreateStore(LoadCXXThis(), addr); |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | for (const auto &CI : blockDecl->captures()) { |
| 1037 | const VarDecl *variable = CI.getVariable(); |
| 1038 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1039 | |
| 1040 | |
| 1041 | if (capture.isConstant()) continue; |
| 1042 | |
| 1043 | QualType type = capture.fieldType(); |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | Address blockField = projectField(capture.getIndex(), "block.captured"); |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | Address src = Address::invalid(); |
| 1052 | |
| 1053 | if (blockDecl->isConversionFromLambda()) { |
| 1054 | |
| 1055 | |
| 1056 | src = Address::invalid(); |
| 1057 | } else if (CI.isEscapingByref()) { |
| 1058 | if (BlockInfo && CI.isNested()) { |
| 1059 | |
| 1060 | const CGBlockInfo::Capture &enclosingCapture = |
| 1061 | BlockInfo->getCapture(variable); |
| 1062 | |
| 1063 | |
| 1064 | src = Builder.CreateStructGEP(LoadBlockStruct(), |
| 1065 | enclosingCapture.getIndex(), |
| 1066 | "block.capture.addr"); |
| 1067 | } else { |
| 1068 | auto I = LocalDeclMap.find(variable); |
| 1069 | assert(I != LocalDeclMap.end()); |
| 1070 | src = I->second; |
| 1071 | } |
| 1072 | } else { |
| 1073 | DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable), |
| 1074 | CI.isNested(), |
| 1075 | type.getNonReferenceType(), VK_LValue, |
| 1076 | SourceLocation()); |
| 1077 | src = EmitDeclRefLValue(&declRef).getAddress(); |
| 1078 | }; |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | if (CI.isEscapingByref()) { |
| 1085 | |
| 1086 | llvm::Value *byrefPointer; |
| 1087 | if (CI.isNested()) |
| 1088 | byrefPointer = Builder.CreateLoad(src, "byref.capture"); |
| 1089 | else |
| 1090 | byrefPointer = Builder.CreateBitCast(src.getPointer(), VoidPtrTy); |
| 1091 | |
| 1092 | |
| 1093 | Builder.CreateStore(byrefPointer, blockField); |
| 1094 | |
| 1095 | |
| 1096 | } else if (const Expr *copyExpr = CI.getCopyExpr()) { |
| 1097 | if (blockDecl->isConversionFromLambda()) { |
| 1098 | |
| 1099 | |
| 1100 | AggValueSlot Slot = |
| 1101 | AggValueSlot::forAddr(blockField, Qualifiers(), |
| 1102 | AggValueSlot::IsDestructed, |
| 1103 | AggValueSlot::DoesNotNeedGCBarriers, |
| 1104 | AggValueSlot::IsNotAliased, |
| 1105 | AggValueSlot::DoesNotOverlap); |
| 1106 | EmitAggExpr(copyExpr, Slot); |
| 1107 | } else { |
| 1108 | EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr); |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | } else if (type->isReferenceType()) { |
| 1113 | Builder.CreateStore(src.getPointer(), blockField); |
| 1114 | |
| 1115 | |
| 1116 | } else if (type.isConstQualified() && |
| 1117 | type.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 1118 | CGM.getCodeGenOpts().OptimizationLevel != 0) { |
| 1119 | llvm::Value *value = Builder.CreateLoad(src, "captured"); |
| 1120 | Builder.CreateStore(value, blockField); |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | } else if (type.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 1130 | type->isBlockPointerType()) { |
| 1131 | |
| 1132 | llvm::Value *value = Builder.CreateLoad(src, "block.captured_block"); |
| 1133 | value = EmitARCRetainNonBlock(value); |
| 1134 | |
| 1135 | |
| 1136 | Builder.CreateStore(value, blockField); |
| 1137 | |
| 1138 | |
| 1139 | } else { |
| 1140 | |
| 1141 | |
| 1142 | ImplicitParamDecl BlockFieldPseudoVar(getContext(), type, |
| 1143 | ImplicitParamDecl::Other); |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable), |
| 1148 | CI.isNested(), |
| 1149 | type, VK_LValue, SourceLocation()); |
| 1150 | |
| 1151 | ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue, |
| 1152 | &declRef, VK_RValue); |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | EmitExprAsInit(&l2r, &BlockFieldPseudoVar, |
| 1157 | MakeAddrLValue(blockField, type, AlignmentSource::Decl), |
| 1158 | false); |
| 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | if (!CI.isByRef()) { |
| 1163 | EHScopeStack::stable_iterator cleanup = capture.getCleanup(); |
| 1164 | if (cleanup.isValid()) |
| 1165 | ActivateCleanupBlock(cleanup, blockInfo.DominatingIP); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | llvm::Value *result = Builder.CreatePointerCast( |
| 1172 | blockAddr.getPointer(), ConvertType(blockInfo.getBlockExpr()->getType())); |
| 1173 | |
| 1174 | if (IsOpenCL) { |
| 1175 | CGM.getOpenCLRuntime().recordBlockInfo(blockInfo.BlockExpression, InvokeFn, |
| 1176 | result); |
| 1177 | } |
| 1178 | |
| 1179 | return result; |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | llvm::Type *CodeGenModule::getBlockDescriptorType() { |
| 1184 | if (BlockDescriptorType) |
| 1185 | return BlockDescriptorType; |
| 1186 | |
| 1187 | llvm::Type *UnsignedLongTy = |
| 1188 | getTypes().ConvertType(getContext().UnsignedLongTy); |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | BlockDescriptorType = llvm::StructType::create( |
| 1205 | "struct.__block_descriptor", UnsignedLongTy, UnsignedLongTy); |
| 1206 | |
| 1207 | |
| 1208 | unsigned AddrSpace = 0; |
| 1209 | if (getLangOpts().OpenCL) |
| 1210 | AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_constant); |
| 1211 | BlockDescriptorType = llvm::PointerType::get(BlockDescriptorType, AddrSpace); |
| 1212 | return BlockDescriptorType; |
| 1213 | } |
| 1214 | |
| 1215 | llvm::Type *CodeGenModule::getGenericBlockLiteralType() { |
| 1216 | if (GenericBlockLiteralType) |
| 1217 | return GenericBlockLiteralType; |
| 1218 | |
| 1219 | llvm::Type *BlockDescPtrTy = getBlockDescriptorType(); |
| 1220 | |
| 1221 | if (getLangOpts().OpenCL) { |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | SmallVector<llvm::Type *, 8> StructFields( |
| 1229 | {IntTy, IntTy, getOpenCLRuntime().getGenericVoidPointerType()}); |
| 1230 | if (auto *Helper = getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
| 1231 | for (auto I : Helper->getCustomFieldTypes()) |
| 1232 | StructFields.push_back(I); |
| 1233 | } |
| 1234 | GenericBlockLiteralType = llvm::StructType::create( |
| 1235 | StructFields, "struct.__opencl_block_literal_generic"); |
| 1236 | } else { |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | GenericBlockLiteralType = |
| 1245 | llvm::StructType::create("struct.__block_literal_generic", VoidPtrTy, |
| 1246 | IntTy, IntTy, VoidPtrTy, BlockDescPtrTy); |
| 1247 | } |
| 1248 | |
| 1249 | return GenericBlockLiteralType; |
| 1250 | } |
| 1251 | |
| 1252 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E, |
| 1253 | ReturnValueSlot ReturnValue) { |
| 1254 | const BlockPointerType *BPT = |
| 1255 | E->getCallee()->getType()->getAs<BlockPointerType>(); |
| 1256 | llvm::Value *BlockPtr = EmitScalarExpr(E->getCallee()); |
| 1257 | llvm::Type *GenBlockTy = CGM.getGenericBlockLiteralType(); |
| 1258 | llvm::Value *Func = nullptr; |
| 1259 | QualType FnType = BPT->getPointeeType(); |
| 1260 | ASTContext &Ctx = getContext(); |
| 1261 | CallArgList Args; |
| 1262 | |
| 1263 | if (getLangOpts().OpenCL) { |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | llvm::Value *BlockDescriptor = Builder.CreatePointerCast( |
| 1269 | BlockPtr, CGM.getOpenCLRuntime().getGenericVoidPointerType()); |
| 1270 | QualType VoidPtrQualTy = Ctx.getPointerType( |
| 1271 | Ctx.getAddrSpaceQualType(Ctx.VoidTy, LangAS::opencl_generic)); |
| 1272 | Args.add(RValue::get(BlockDescriptor), VoidPtrQualTy); |
| 1273 | |
| 1274 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments()); |
| 1275 | |
| 1276 | |
| 1277 | if (!isa<ParmVarDecl>(E->getCalleeDecl())) |
| 1278 | Func = CGM.getOpenCLRuntime().getInvokeFunction(E->getCallee()); |
| 1279 | else { |
| 1280 | llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 2); |
| 1281 | Func = Builder.CreateAlignedLoad(FuncPtr, getPointerAlign()); |
| 1282 | } |
| 1283 | } else { |
| 1284 | |
| 1285 | BlockPtr = Builder.CreatePointerCast( |
| 1286 | BlockPtr, llvm::PointerType::get(GenBlockTy, 0), "block.literal"); |
| 1287 | |
| 1288 | llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 3); |
| 1289 | |
| 1290 | |
| 1291 | BlockPtr = Builder.CreatePointerCast(BlockPtr, VoidPtrTy); |
| 1292 | Args.add(RValue::get(BlockPtr), Ctx.VoidPtrTy); |
| 1293 | |
| 1294 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments()); |
| 1295 | |
| 1296 | |
| 1297 | Func = Builder.CreateAlignedLoad(FuncPtr, getPointerAlign()); |
| 1298 | } |
| 1299 | |
| 1300 | const FunctionType *FuncTy = FnType->castAs<FunctionType>(); |
| 1301 | const CGFunctionInfo &FnInfo = |
| 1302 | CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy); |
| 1303 | |
| 1304 | |
| 1305 | llvm::Type *BlockFTy = CGM.getTypes().GetFunctionType(FnInfo); |
| 1306 | |
| 1307 | llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
| 1308 | Func = Builder.CreatePointerCast(Func, BlockFTyPtr); |
| 1309 | |
| 1310 | |
| 1311 | CGCallee Callee(CGCalleeInfo(), Func); |
| 1312 | |
| 1313 | |
| 1314 | return EmitCall(FnInfo, Callee, ReturnValue, Args); |
| 1315 | } |
| 1316 | |
| 1317 | Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable) { |
| 1318 | (0) . __assert_fail ("BlockInfo && \"evaluating block ref without block information?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1318, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BlockInfo && "evaluating block ref without block information?"); |
| 1319 | const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable); |
| 1320 | |
| 1321 | |
| 1322 | if (capture.isConstant()) return LocalDeclMap.find(variable)->second; |
| 1323 | |
| 1324 | Address addr = Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(), |
| 1325 | "block.capture.addr"); |
| 1326 | |
| 1327 | if (variable->isEscapingByref()) { |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | auto &byrefInfo = getBlockByrefInfo(variable); |
| 1332 | addr = Address(Builder.CreateLoad(addr), byrefInfo.ByrefAlignment); |
| 1333 | |
| 1334 | auto byrefPointerType = llvm::PointerType::get(byrefInfo.Type, 0); |
| 1335 | addr = Builder.CreateBitCast(addr, byrefPointerType, "byref.addr"); |
| 1336 | |
| 1337 | addr = emitBlockByrefAddress(addr, byrefInfo, true, |
| 1338 | variable->getName()); |
| 1339 | } |
| 1340 | |
| 1341 | (0) . __assert_fail ("(!variable->isNonEscapingByref() || capture.fieldType()->isReferenceType()) && \"the capture field of a non-escaping variable should have a \" \"reference type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1344, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!variable->isNonEscapingByref() || |
| 1342 | (0) . __assert_fail ("(!variable->isNonEscapingByref() || capture.fieldType()->isReferenceType()) && \"the capture field of a non-escaping variable should have a \" \"reference type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1344, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> capture.fieldType()->isReferenceType()) && |
| 1343 | (0) . __assert_fail ("(!variable->isNonEscapingByref() || capture.fieldType()->isReferenceType()) && \"the capture field of a non-escaping variable should have a \" \"reference type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1344, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "the capture field of a non-escaping variable should have a " |
| 1344 | (0) . __assert_fail ("(!variable->isNonEscapingByref() || capture.fieldType()->isReferenceType()) && \"the capture field of a non-escaping variable should have a \" \"reference type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1344, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "reference type"); |
| 1345 | if (capture.fieldType()->isReferenceType()) |
| 1346 | addr = EmitLoadOfReference(MakeAddrLValue(addr, capture.fieldType())); |
| 1347 | |
| 1348 | return addr; |
| 1349 | } |
| 1350 | |
| 1351 | void CodeGenModule::setAddrOfGlobalBlock(const BlockExpr *BE, |
| 1352 | llvm::Constant *Addr) { |
| 1353 | bool Ok = EmittedGlobalBlocks.insert(std::make_pair(BE, Addr)).second; |
| 1354 | (void)Ok; |
| 1355 | (0) . __assert_fail ("Ok && \"Trying to replace an already-existing global block!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1355, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Ok && "Trying to replace an already-existing global block!"); |
| 1356 | } |
| 1357 | |
| 1358 | llvm::Constant * |
| 1359 | CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, |
| 1360 | StringRef Name) { |
| 1361 | if (llvm::Constant *Block = getAddrOfGlobalBlockIfEmitted(BE)) |
| 1362 | return Block; |
| 1363 | |
| 1364 | CGBlockInfo blockInfo(BE->getBlockDecl(), Name); |
| 1365 | blockInfo.BlockExpression = BE; |
| 1366 | |
| 1367 | |
| 1368 | computeBlockInfo(*this, nullptr, blockInfo); |
| 1369 | |
| 1370 | |
| 1371 | { |
| 1372 | CodeGenFunction::DeclMapTy LocalDeclMap; |
| 1373 | CodeGenFunction(*this).GenerateBlockFunction( |
| 1374 | GlobalDecl(), blockInfo, LocalDeclMap, |
| 1375 | false, true); |
| 1376 | } |
| 1377 | |
| 1378 | return getAddrOfGlobalBlockIfEmitted(BE); |
| 1379 | } |
| 1380 | |
| 1381 | static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, |
| 1382 | const CGBlockInfo &blockInfo, |
| 1383 | llvm::Constant *blockFn) { |
| 1384 | assert(blockInfo.CanBeGlobal); |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | (0) . __assert_fail ("!CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression) && \"Refusing to re-emit a global block.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1389, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression) && |
| 1389 | (0) . __assert_fail ("!CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression) && \"Refusing to re-emit a global block.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1389, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Refusing to re-emit a global block."); |
| 1390 | |
| 1391 | |
| 1392 | ConstantInitBuilder builder(CGM); |
| 1393 | auto fields = builder.beginStruct(); |
| 1394 | |
| 1395 | bool IsOpenCL = CGM.getLangOpts().OpenCL; |
| 1396 | bool IsWindows = CGM.getTarget().getTriple().isOSWindows(); |
| 1397 | if (!IsOpenCL) { |
| 1398 | |
| 1399 | if (IsWindows) |
| 1400 | fields.addNullPointer(CGM.Int8PtrPtrTy); |
| 1401 | else |
| 1402 | fields.add(CGM.getNSConcreteGlobalBlock()); |
| 1403 | |
| 1404 | |
| 1405 | BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE; |
| 1406 | if (blockInfo.UsesStret) |
| 1407 | flags |= BLOCK_USE_STRET; |
| 1408 | |
| 1409 | fields.addInt(CGM.IntTy, flags.getBitMask()); |
| 1410 | |
| 1411 | |
| 1412 | fields.addInt(CGM.IntTy, 0); |
| 1413 | } else { |
| 1414 | fields.addInt(CGM.IntTy, blockInfo.BlockSize.getQuantity()); |
| 1415 | fields.addInt(CGM.IntTy, blockInfo.BlockAlign.getQuantity()); |
| 1416 | } |
| 1417 | |
| 1418 | |
| 1419 | fields.add(blockFn); |
| 1420 | |
| 1421 | if (!IsOpenCL) { |
| 1422 | |
| 1423 | fields.add(buildBlockDescriptor(CGM, blockInfo)); |
| 1424 | } else if (auto *Helper = |
| 1425 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
| 1426 | for (auto I : Helper->getCustomFieldValues(CGM, blockInfo)) { |
| 1427 | fields.add(I); |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | unsigned AddrSpace = 0; |
| 1432 | if (CGM.getContext().getLangOpts().OpenCL) |
| 1433 | AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); |
| 1434 | |
| 1435 | llvm::Constant *literal = fields.finishAndCreateGlobal( |
| 1436 | "__block_literal_global", blockInfo.BlockAlign, |
| 1437 | !IsWindows, llvm::GlobalVariable::InternalLinkage, AddrSpace); |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | if (IsWindows) { |
| 1442 | auto *Init = llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy, |
| 1443 | {}), llvm::GlobalValue::InternalLinkage, ".block_isa_init", |
| 1444 | &CGM.getModule()); |
| 1445 | llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry", |
| 1446 | Init)); |
| 1447 | b.CreateAlignedStore(CGM.getNSConcreteGlobalBlock(), |
| 1448 | b.CreateStructGEP(literal, 0), CGM.getPointerAlign().getQuantity()); |
| 1449 | b.CreateRetVoid(); |
| 1450 | |
| 1451 | |
| 1452 | auto *InitVar = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), |
| 1453 | , llvm::GlobalValue::InternalLinkage, |
| 1454 | Init, ".block_isa_init_ptr"); |
| 1455 | InitVar->setSection(".CRT$XCLa"); |
| 1456 | CGM.addUsedGlobal(InitVar); |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | llvm::Type *RequiredType = |
| 1461 | CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType()); |
| 1462 | llvm::Constant *Result = |
| 1463 | llvm::ConstantExpr::getPointerCast(literal, RequiredType); |
| 1464 | CGM.setAddrOfGlobalBlock(blockInfo.BlockExpression, Result); |
| 1465 | if (CGM.getContext().getLangOpts().OpenCL) |
| 1466 | CGM.getOpenCLRuntime().recordBlockInfo( |
| 1467 | blockInfo.BlockExpression, |
| 1468 | cast<llvm::Function>(blockFn->stripPointerCasts()), Result); |
| 1469 | return Result; |
| 1470 | } |
| 1471 | |
| 1472 | void CodeGenFunction::setBlockContextParameter(const ImplicitParamDecl *D, |
| 1473 | unsigned argNum, |
| 1474 | llvm::Value *arg) { |
| 1475 | (0) . __assert_fail ("BlockInfo && \"not emitting prologue of block invocation function?!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1475, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BlockInfo && "not emitting prologue of block invocation function?!"); |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | Address alloc = CreateMemTemp(D->getType(), D->getName() + ".addr"); |
| 1480 | Builder.CreateStore(arg, alloc); |
| 1481 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 1482 | if (CGM.getCodeGenOpts().getDebugInfo() >= |
| 1483 | codegenoptions::LimitedDebugInfo) { |
| 1484 | DI->setLocation(D->getLocation()); |
| 1485 | DI->EmitDeclareOfBlockLiteralArgVariable( |
| 1486 | *BlockInfo, D->getName(), argNum, |
| 1487 | cast<llvm::AllocaInst>(alloc.getPointer()), Builder); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | SourceLocation StartLoc = BlockInfo->getBlockExpr()->getBody()->getBeginLoc(); |
| 1492 | ApplyDebugLocation Scope(*this, StartLoc); |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | BlockPointer = Builder.CreatePointerCast( |
| 1497 | arg, |
| 1498 | BlockInfo->StructureType->getPointerTo( |
| 1499 | getContext().getLangOpts().OpenCL |
| 1500 | ? getContext().getTargetAddressSpace(LangAS::opencl_generic) |
| 1501 | : 0), |
| 1502 | "block"); |
| 1503 | } |
| 1504 | |
| 1505 | Address CodeGenFunction::LoadBlockStruct() { |
| 1506 | (0) . __assert_fail ("BlockInfo && \"not in a block invocation function!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1506, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BlockInfo && "not in a block invocation function!"); |
| 1507 | (0) . __assert_fail ("BlockPointer && \"no block pointer set!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1507, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BlockPointer && "no block pointer set!"); |
| 1508 | return Address(BlockPointer, BlockInfo->BlockAlign); |
| 1509 | } |
| 1510 | |
| 1511 | llvm::Function * |
| 1512 | CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, |
| 1513 | const CGBlockInfo &blockInfo, |
| 1514 | const DeclMapTy &ldm, |
| 1515 | bool IsLambdaConversionToBlock, |
| 1516 | bool BuildGlobalBlock) { |
| 1517 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1518 | |
| 1519 | CurGD = GD; |
| 1520 | |
| 1521 | CurEHLocation = blockInfo.getBlockExpr()->getEndLoc(); |
| 1522 | |
| 1523 | BlockInfo = &blockInfo; |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) { |
| 1529 | const auto *var = dyn_cast<VarDecl>(i->first); |
| 1530 | if (var && !var->hasLocalStorage()) |
| 1531 | setAddrOfLocalVar(var, i->second); |
| 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | FunctionArgList args; |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | QualType selfTy = getContext().VoidPtrTy; |
| 1542 | |
| 1543 | |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | if (getLangOpts().OpenCL) |
| 1548 | selfTy = getContext().getPointerType(getContext().getAddrSpaceQualType( |
| 1549 | getContext().VoidTy, LangAS::opencl_generic)); |
| 1550 | |
| 1551 | IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); |
| 1552 | |
| 1553 | ImplicitParamDecl SelfDecl(getContext(), const_cast<BlockDecl *>(blockDecl), |
| 1554 | SourceLocation(), II, selfTy, |
| 1555 | ImplicitParamDecl::ObjCSelf); |
| 1556 | args.push_back(&SelfDecl); |
| 1557 | |
| 1558 | |
| 1559 | args.append(blockDecl->param_begin(), blockDecl->param_end()); |
| 1560 | |
| 1561 | |
| 1562 | const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType(); |
| 1563 | const CGFunctionInfo &fnInfo = |
| 1564 | CGM.getTypes().arrangeBlockFunctionDeclaration(fnType, args); |
| 1565 | if (CGM.ReturnSlotInterferesWithArgs(fnInfo)) |
| 1566 | blockInfo.UsesStret = true; |
| 1567 | |
| 1568 | llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo); |
| 1569 | |
| 1570 | StringRef name = CGM.getBlockMangledName(GD, blockDecl); |
| 1571 | llvm::Function *fn = llvm::Function::Create( |
| 1572 | fnLLVMType, llvm::GlobalValue::InternalLinkage, name, &CGM.getModule()); |
| 1573 | CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo); |
| 1574 | |
| 1575 | if (BuildGlobalBlock) { |
| 1576 | auto GenVoidPtrTy = getContext().getLangOpts().OpenCL |
| 1577 | ? CGM.getOpenCLRuntime().getGenericVoidPointerType() |
| 1578 | : VoidPtrTy; |
| 1579 | buildGlobalBlock(CGM, blockInfo, |
| 1580 | llvm::ConstantExpr::getPointerCast(fn, GenVoidPtrTy)); |
| 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | StartFunction(blockDecl, fnType->getReturnType(), fn, fnInfo, args, |
| 1585 | blockDecl->getLocation(), |
| 1586 | blockInfo.getBlockExpr()->getBody()->getBeginLoc()); |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | |
| 1592 | llvm::Value *BlockPointerDbgLoc = BlockPointer; |
| 1593 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 1594 | |
| 1595 | Address Alloca = CreateTempAlloca(BlockPointer->getType(), |
| 1596 | getPointerAlign(), |
| 1597 | "block.addr"); |
| 1598 | |
| 1599 | |
| 1600 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
| 1601 | Builder.CreateStore(BlockPointer, Alloca); |
| 1602 | BlockPointerDbgLoc = Alloca.getPointer(); |
| 1603 | } |
| 1604 | |
| 1605 | |
| 1606 | |
| 1607 | if (blockDecl->capturesCXXThis()) { |
| 1608 | Address addr = Builder.CreateStructGEP( |
| 1609 | LoadBlockStruct(), blockInfo.CXXThisIndex, "block.captured-this"); |
| 1610 | CXXThisValue = Builder.CreateLoad(addr, "this"); |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | for (const auto &CI : blockDecl->captures()) { |
| 1615 | const VarDecl *variable = CI.getVariable(); |
| 1616 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1617 | if (!capture.isConstant()) continue; |
| 1618 | |
| 1619 | CharUnits align = getContext().getDeclAlign(variable); |
| 1620 | Address alloca = |
| 1621 | CreateMemTemp(variable->getType(), align, "block.captured-const"); |
| 1622 | |
| 1623 | Builder.CreateStore(capture.getConstant(), alloca); |
| 1624 | |
| 1625 | setAddrOfLocalVar(variable, alloca); |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | llvm::BasicBlock *entry = Builder.GetInsertBlock(); |
| 1630 | llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); |
| 1631 | --entry_ptr; |
| 1632 | |
| 1633 | if (IsLambdaConversionToBlock) |
| 1634 | EmitLambdaBlockInvokeBody(); |
| 1635 | else { |
| 1636 | PGO.assignRegionCounters(GlobalDecl(blockDecl), fn); |
| 1637 | incrementProfileCounter(blockDecl->getBody()); |
| 1638 | EmitStmt(blockDecl->getBody()); |
| 1639 | } |
| 1640 | |
| 1641 | |
| 1642 | llvm::BasicBlock *resume = Builder.GetInsertBlock(); |
| 1643 | |
| 1644 | |
| 1645 | ++entry_ptr; |
| 1646 | Builder.SetInsertPoint(entry, entry_ptr); |
| 1647 | |
| 1648 | |
| 1649 | |
| 1650 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 1651 | for (const auto &CI : blockDecl->captures()) { |
| 1652 | const VarDecl *variable = CI.getVariable(); |
| 1653 | DI->EmitLocation(Builder, variable->getLocation()); |
| 1654 | |
| 1655 | if (CGM.getCodeGenOpts().getDebugInfo() >= |
| 1656 | codegenoptions::LimitedDebugInfo) { |
| 1657 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1658 | if (capture.isConstant()) { |
| 1659 | auto addr = LocalDeclMap.find(variable)->second; |
| 1660 | (void)DI->EmitDeclareOfAutoVariable(variable, addr.getPointer(), |
| 1661 | Builder); |
| 1662 | continue; |
| 1663 | } |
| 1664 | |
| 1665 | DI->EmitDeclareOfBlockDeclRefVariable( |
| 1666 | variable, BlockPointerDbgLoc, Builder, blockInfo, |
| 1667 | entry_ptr == entry->end() ? nullptr : &*entry_ptr); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | DI->EmitLocation(Builder, |
| 1672 | cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); |
| 1673 | } |
| 1674 | |
| 1675 | |
| 1676 | if (resume == nullptr) |
| 1677 | Builder.ClearInsertionPoint(); |
| 1678 | else |
| 1679 | Builder.SetInsertPoint(resume); |
| 1680 | |
| 1681 | FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); |
| 1682 | |
| 1683 | return fn; |
| 1684 | } |
| 1685 | |
| 1686 | static std::pair<BlockCaptureEntityKind, BlockFieldFlags> |
| 1687 | computeCopyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, |
| 1688 | const LangOptions &LangOpts) { |
| 1689 | if (CI.getCopyExpr()) { |
| 1690 | assert(!CI.isByRef()); |
| 1691 | |
| 1692 | return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags()); |
| 1693 | } |
| 1694 | BlockFieldFlags Flags; |
| 1695 | if (CI.isEscapingByref()) { |
| 1696 | Flags = BLOCK_FIELD_IS_BYREF; |
| 1697 | if (T.isObjCGCWeak()) |
| 1698 | Flags |= BLOCK_FIELD_IS_WEAK; |
| 1699 | return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags); |
| 1700 | } |
| 1701 | |
| 1702 | Flags = BLOCK_FIELD_IS_OBJECT; |
| 1703 | bool isBlockPointer = T->isBlockPointerType(); |
| 1704 | if (isBlockPointer) |
| 1705 | Flags = BLOCK_FIELD_IS_BLOCK; |
| 1706 | |
| 1707 | switch (T.isNonTrivialToPrimitiveCopy()) { |
| 1708 | case QualType::PCK_Struct: |
| 1709 | return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct, |
| 1710 | BlockFieldFlags()); |
| 1711 | case QualType::PCK_ARCWeak: |
| 1712 | |
| 1713 | return std::make_pair(BlockCaptureEntityKind::ARCWeak, Flags); |
| 1714 | case QualType::PCK_ARCStrong: |
| 1715 | |
| 1716 | |
| 1717 | |
| 1718 | |
| 1719 | return std::make_pair(!isBlockPointer ? BlockCaptureEntityKind::ARCStrong |
| 1720 | : BlockCaptureEntityKind::BlockObject, |
| 1721 | Flags); |
| 1722 | case QualType::PCK_Trivial: |
| 1723 | case QualType::PCK_VolatileTrivial: { |
| 1724 | if (!T->isObjCRetainableType()) |
| 1725 | |
| 1726 | return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags()); |
| 1727 | |
| 1728 | |
| 1729 | Qualifiers QS = T.getQualifiers(); |
| 1730 | |
| 1731 | |
| 1732 | |
| 1733 | if (!QS.getObjCLifetime() && !LangOpts.ObjCAutoRefCount) |
| 1734 | return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags); |
| 1735 | |
| 1736 | |
| 1737 | return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags()); |
| 1738 | } |
| 1739 | } |
| 1740 | llvm_unreachable("after exhaustive PrimitiveCopyKind switch"); |
| 1741 | } |
| 1742 | |
| 1743 | static std::pair<BlockCaptureEntityKind, BlockFieldFlags> |
| 1744 | computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, |
| 1745 | const LangOptions &LangOpts); |
| 1746 | |
| 1747 | |
| 1748 | static void findBlockCapturedManagedEntities( |
| 1749 | const CGBlockInfo &BlockInfo, const LangOptions &LangOpts, |
| 1750 | SmallVectorImpl<BlockCaptureManagedEntity> &ManagedCaptures) { |
| 1751 | for (const auto &CI : BlockInfo.getBlockDecl()->captures()) { |
| 1752 | const VarDecl *Variable = CI.getVariable(); |
| 1753 | const CGBlockInfo::Capture &Capture = BlockInfo.getCapture(Variable); |
| 1754 | if (Capture.isConstant()) |
| 1755 | continue; |
| 1756 | |
| 1757 | QualType VT = Capture.fieldType(); |
| 1758 | auto CopyInfo = computeCopyInfoForBlockCapture(CI, VT, LangOpts); |
| 1759 | auto DisposeInfo = computeDestroyInfoForBlockCapture(CI, VT, LangOpts); |
| 1760 | if (CopyInfo.first != BlockCaptureEntityKind::None || |
| 1761 | DisposeInfo.first != BlockCaptureEntityKind::None) |
| 1762 | ManagedCaptures.emplace_back(CopyInfo.first, DisposeInfo.first, |
| 1763 | CopyInfo.second, DisposeInfo.second, CI, |
| 1764 | Capture); |
| 1765 | } |
| 1766 | |
| 1767 | |
| 1768 | llvm::sort(ManagedCaptures); |
| 1769 | } |
| 1770 | |
| 1771 | namespace { |
| 1772 | |
| 1773 | struct CallBlockRelease final : EHScopeStack::Cleanup { |
| 1774 | Address Addr; |
| 1775 | BlockFieldFlags FieldFlags; |
| 1776 | bool LoadBlockVarAddr, CanThrow; |
| 1777 | |
| 1778 | CallBlockRelease(Address Addr, BlockFieldFlags Flags, bool LoadValue, |
| 1779 | bool CT) |
| 1780 | : Addr(Addr), FieldFlags(Flags), LoadBlockVarAddr(LoadValue), |
| 1781 | CanThrow(CT) {} |
| 1782 | |
| 1783 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 1784 | llvm::Value *BlockVarAddr; |
| 1785 | if (LoadBlockVarAddr) { |
| 1786 | BlockVarAddr = CGF.Builder.CreateLoad(Addr); |
| 1787 | BlockVarAddr = CGF.Builder.CreateBitCast(BlockVarAddr, CGF.VoidPtrTy); |
| 1788 | } else { |
| 1789 | BlockVarAddr = Addr.getPointer(); |
| 1790 | } |
| 1791 | |
| 1792 | CGF.BuildBlockRelease(BlockVarAddr, FieldFlags, CanThrow); |
| 1793 | } |
| 1794 | }; |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | bool CodeGenFunction::cxxDestructorCanThrow(QualType T) { |
| 1799 | if (const auto *RD = T->getAsCXXRecordDecl()) |
| 1800 | if (const CXXDestructorDecl *DD = RD->getDestructor()) |
| 1801 | return DD->getType()->getAs<FunctionProtoType>()->canThrow(); |
| 1802 | return false; |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | static std::string getBlockCaptureStr(const BlockCaptureManagedEntity &E, |
| 1807 | CaptureStrKind StrKind, |
| 1808 | CharUnits BlockAlignment, |
| 1809 | CodeGenModule &CGM) { |
| 1810 | std::string Str; |
| 1811 | ASTContext &Ctx = CGM.getContext(); |
| 1812 | const BlockDecl::Capture &CI = *E.CI; |
| 1813 | QualType CaptureTy = CI.getVariable()->getType(); |
| 1814 | |
| 1815 | BlockCaptureEntityKind Kind; |
| 1816 | BlockFieldFlags Flags; |
| 1817 | |
| 1818 | |
| 1819 | |
| 1820 | (0) . __assert_fail ("(StrKind != CaptureStrKind..Merged || (E.CopyKind == E.DisposeKind && E.CopyFlags == E.DisposeFlags)) && \"different operations and flags\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1822, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((StrKind != CaptureStrKind::Merged || |
| 1821 | (0) . __assert_fail ("(StrKind != CaptureStrKind..Merged || (E.CopyKind == E.DisposeKind && E.CopyFlags == E.DisposeFlags)) && \"different operations and flags\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1822, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (E.CopyKind == E.DisposeKind && E.CopyFlags == E.DisposeFlags)) && |
| 1822 | (0) . __assert_fail ("(StrKind != CaptureStrKind..Merged || (E.CopyKind == E.DisposeKind && E.CopyFlags == E.DisposeFlags)) && \"different operations and flags\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1822, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "different operations and flags"); |
| 1823 | |
| 1824 | if (StrKind == CaptureStrKind::DisposeHelper) { |
| 1825 | Kind = E.DisposeKind; |
| 1826 | Flags = E.DisposeFlags; |
| 1827 | } else { |
| 1828 | Kind = E.CopyKind; |
| 1829 | Flags = E.CopyFlags; |
| 1830 | } |
| 1831 | |
| 1832 | switch (Kind) { |
| 1833 | case BlockCaptureEntityKind::CXXRecord: { |
| 1834 | Str += "c"; |
| 1835 | SmallString<256> TyStr; |
| 1836 | llvm::raw_svector_ostream Out(TyStr); |
| 1837 | CGM.getCXXABI().getMangleContext().mangleTypeName(CaptureTy, Out); |
| 1838 | Str += llvm::to_string(TyStr.size()) + TyStr.c_str(); |
| 1839 | break; |
| 1840 | } |
| 1841 | case BlockCaptureEntityKind::ARCWeak: |
| 1842 | Str += "w"; |
| 1843 | break; |
| 1844 | case BlockCaptureEntityKind::ARCStrong: |
| 1845 | Str += "s"; |
| 1846 | break; |
| 1847 | case BlockCaptureEntityKind::BlockObject: { |
| 1848 | const VarDecl *Var = CI.getVariable(); |
| 1849 | unsigned F = Flags.getBitMask(); |
| 1850 | if (F & BLOCK_FIELD_IS_BYREF) { |
| 1851 | Str += "r"; |
| 1852 | if (F & BLOCK_FIELD_IS_WEAK) |
| 1853 | Str += "w"; |
| 1854 | else { |
| 1855 | |
| 1856 | |
| 1857 | if (StrKind != CaptureStrKind::DisposeHelper) { |
| 1858 | if (Ctx.getBlockVarCopyInit(Var).canThrow()) |
| 1859 | Str += "c"; |
| 1860 | } |
| 1861 | if (StrKind != CaptureStrKind::CopyHelper) { |
| 1862 | if (CodeGenFunction::cxxDestructorCanThrow(CaptureTy)) |
| 1863 | Str += "d"; |
| 1864 | } |
| 1865 | } |
| 1866 | } else { |
| 1867 | (0) . __assert_fail ("(F & BLOCK_FIELD_IS_OBJECT) && \"unexpected flag value\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1867, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((F & BLOCK_FIELD_IS_OBJECT) && "unexpected flag value"); |
| 1868 | if (F == BLOCK_FIELD_IS_BLOCK) |
| 1869 | Str += "b"; |
| 1870 | else |
| 1871 | Str += "o"; |
| 1872 | } |
| 1873 | break; |
| 1874 | } |
| 1875 | case BlockCaptureEntityKind::NonTrivialCStruct: { |
| 1876 | bool IsVolatile = CaptureTy.isVolatileQualified(); |
| 1877 | CharUnits Alignment = |
| 1878 | BlockAlignment.alignmentAtOffset(E.Capture->getOffset()); |
| 1879 | |
| 1880 | Str += "n"; |
| 1881 | std::string FuncStr; |
| 1882 | if (StrKind == CaptureStrKind::DisposeHelper) |
| 1883 | FuncStr = CodeGenFunction::getNonTrivialDestructorStr( |
| 1884 | CaptureTy, Alignment, IsVolatile, Ctx); |
| 1885 | else |
| 1886 | |
| 1887 | |
| 1888 | FuncStr = CodeGenFunction::getNonTrivialCopyConstructorStr( |
| 1889 | CaptureTy, Alignment, IsVolatile, Ctx); |
| 1890 | |
| 1891 | |
| 1892 | Str += llvm::to_string(FuncStr.size()) + "_" + FuncStr; |
| 1893 | break; |
| 1894 | } |
| 1895 | case BlockCaptureEntityKind::None: |
| 1896 | break; |
| 1897 | } |
| 1898 | |
| 1899 | return Str; |
| 1900 | } |
| 1901 | |
| 1902 | static std::string getCopyDestroyHelperFuncName( |
| 1903 | const SmallVectorImpl<BlockCaptureManagedEntity> &Captures, |
| 1904 | CharUnits BlockAlignment, CaptureStrKind StrKind, CodeGenModule &CGM) { |
| 1905 | (0) . __assert_fail ("(StrKind == CaptureStrKind..CopyHelper || StrKind == CaptureStrKind..DisposeHelper) && \"unexpected CaptureStrKind\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1907, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((StrKind == CaptureStrKind::CopyHelper || |
| 1906 | (0) . __assert_fail ("(StrKind == CaptureStrKind..CopyHelper || StrKind == CaptureStrKind..DisposeHelper) && \"unexpected CaptureStrKind\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1907, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> StrKind == CaptureStrKind::DisposeHelper) && |
| 1907 | (0) . __assert_fail ("(StrKind == CaptureStrKind..CopyHelper || StrKind == CaptureStrKind..DisposeHelper) && \"unexpected CaptureStrKind\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 1907, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "unexpected CaptureStrKind"); |
| 1908 | std::string Name = StrKind == CaptureStrKind::CopyHelper |
| 1909 | ? "__copy_helper_block_" |
| 1910 | : "__destroy_helper_block_"; |
| 1911 | if (CGM.getLangOpts().Exceptions) |
| 1912 | Name += "e"; |
| 1913 | if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) |
| 1914 | Name += "a"; |
| 1915 | Name += llvm::to_string(BlockAlignment.getQuantity()) + "_"; |
| 1916 | |
| 1917 | for (const BlockCaptureManagedEntity &E : Captures) { |
| 1918 | Name += llvm::to_string(E.Capture->getOffset().getQuantity()); |
| 1919 | Name += getBlockCaptureStr(E, StrKind, BlockAlignment, CGM); |
| 1920 | } |
| 1921 | |
| 1922 | return Name; |
| 1923 | } |
| 1924 | |
| 1925 | static void pushCaptureCleanup(BlockCaptureEntityKind CaptureKind, |
| 1926 | Address Field, QualType CaptureType, |
| 1927 | BlockFieldFlags Flags, bool ForCopyHelper, |
| 1928 | VarDecl *Var, CodeGenFunction &CGF) { |
| 1929 | bool EHOnly = ForCopyHelper; |
| 1930 | |
| 1931 | switch (CaptureKind) { |
| 1932 | case BlockCaptureEntityKind::CXXRecord: |
| 1933 | case BlockCaptureEntityKind::ARCWeak: |
| 1934 | case BlockCaptureEntityKind::NonTrivialCStruct: |
| 1935 | case BlockCaptureEntityKind::ARCStrong: { |
| 1936 | if (CaptureType.isDestructedType() && |
| 1937 | (!EHOnly || CGF.needsEHCleanup(CaptureType.isDestructedType()))) { |
| 1938 | CodeGenFunction::Destroyer *Destroyer = |
| 1939 | CaptureKind == BlockCaptureEntityKind::ARCStrong |
| 1940 | ? CodeGenFunction::destroyARCStrongImprecise |
| 1941 | : CGF.getDestroyer(CaptureType.isDestructedType()); |
| 1942 | CleanupKind Kind = |
| 1943 | EHOnly ? EHCleanup |
| 1944 | : CGF.getCleanupKind(CaptureType.isDestructedType()); |
| 1945 | CGF.pushDestroy(Kind, Field, CaptureType, Destroyer, Kind & EHCleanup); |
| 1946 | } |
| 1947 | break; |
| 1948 | } |
| 1949 | case BlockCaptureEntityKind::BlockObject: { |
| 1950 | if (!EHOnly || CGF.getLangOpts().Exceptions) { |
| 1951 | CleanupKind Kind = EHOnly ? EHCleanup : NormalAndEHCleanup; |
| 1952 | |
| 1953 | |
| 1954 | |
| 1955 | bool CanThrow = |
| 1956 | !ForCopyHelper && CGF.cxxDestructorCanThrow(CaptureType); |
| 1957 | CGF.enterByrefCleanup(Kind, Field, Flags, true, |
| 1958 | CanThrow); |
| 1959 | } |
| 1960 | break; |
| 1961 | } |
| 1962 | case BlockCaptureEntityKind::None: |
| 1963 | break; |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType, |
| 1968 | llvm::Function *Fn, |
| 1969 | const CGFunctionInfo &FI, |
| 1970 | CodeGenModule &CGM) { |
| 1971 | if (CapturesNonExternalType) { |
| 1972 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
| 1973 | } else { |
| 1974 | Fn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1975 | Fn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 1976 | CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn); |
| 1977 | CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn); |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | |
| 1982 | |
| 1983 | |
| 1984 | |
| 1985 | |
| 1986 | |
| 1987 | |
| 1988 | llvm::Constant * |
| 1989 | CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) { |
| 1990 | SmallVector<BlockCaptureManagedEntity, 4> CopiedCaptures; |
| 1991 | findBlockCapturedManagedEntities(blockInfo, getLangOpts(), CopiedCaptures); |
| 1992 | std::string FuncName = |
| 1993 | getCopyDestroyHelperFuncName(CopiedCaptures, blockInfo.BlockAlign, |
| 1994 | CaptureStrKind::CopyHelper, CGM); |
| 1995 | |
| 1996 | if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName)) |
| 1997 | return llvm::ConstantExpr::getBitCast(Func, VoidPtrTy); |
| 1998 | |
| 1999 | ASTContext &C = getContext(); |
| 2000 | |
| 2001 | QualType ReturnTy = C.VoidTy; |
| 2002 | |
| 2003 | FunctionArgList args; |
| 2004 | ImplicitParamDecl DstDecl(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 2005 | args.push_back(&DstDecl); |
| 2006 | ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 2007 | args.push_back(&SrcDecl); |
| 2008 | |
| 2009 | const CGFunctionInfo &FI = |
| 2010 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
| 2011 | |
| 2012 | |
| 2013 | |
| 2014 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
| 2015 | |
| 2016 | llvm::Function *Fn = |
| 2017 | llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage, |
| 2018 | FuncName, &CGM.getModule()); |
| 2019 | if (CGM.supportsCOMDAT()) |
| 2020 | Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName)); |
| 2021 | |
| 2022 | IdentifierInfo *II = &C.Idents.get(FuncName); |
| 2023 | |
| 2024 | SmallVector<QualType, 2> ArgTys; |
| 2025 | ArgTys.push_back(C.VoidPtrTy); |
| 2026 | ArgTys.push_back(C.VoidPtrTy); |
| 2027 | QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {}); |
| 2028 | |
| 2029 | FunctionDecl *FD = FunctionDecl::Create( |
| 2030 | C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II, |
| 2031 | FunctionTy, nullptr, SC_Static, false, false); |
| 2032 | |
| 2033 | setBlockHelperAttributesVisibility(blockInfo.CapturesNonExternalType, Fn, FI, |
| 2034 | CGM); |
| 2035 | StartFunction(FD, ReturnTy, Fn, FI, args); |
| 2036 | ApplyDebugLocation NL{*this, blockInfo.getBlockExpr()->getBeginLoc()}; |
| 2037 | llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
| 2038 | |
| 2039 | Address src = GetAddrOfLocalVar(&SrcDecl); |
| 2040 | src = Address(Builder.CreateLoad(src), blockInfo.BlockAlign); |
| 2041 | src = Builder.CreateBitCast(src, structPtrTy, "block.source"); |
| 2042 | |
| 2043 | Address dst = GetAddrOfLocalVar(&DstDecl); |
| 2044 | dst = Address(Builder.CreateLoad(dst), blockInfo.BlockAlign); |
| 2045 | dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest"); |
| 2046 | |
| 2047 | for (const auto &CopiedCapture : CopiedCaptures) { |
| 2048 | const BlockDecl::Capture &CI = *CopiedCapture.CI; |
| 2049 | const CGBlockInfo::Capture &capture = *CopiedCapture.Capture; |
| 2050 | QualType captureType = CI.getVariable()->getType(); |
| 2051 | BlockFieldFlags flags = CopiedCapture.CopyFlags; |
| 2052 | |
| 2053 | unsigned index = capture.getIndex(); |
| 2054 | Address srcField = Builder.CreateStructGEP(src, index); |
| 2055 | Address dstField = Builder.CreateStructGEP(dst, index); |
| 2056 | |
| 2057 | switch (CopiedCapture.CopyKind) { |
| 2058 | case BlockCaptureEntityKind::CXXRecord: |
| 2059 | |
| 2060 | (0) . __assert_fail ("CI.getCopyExpr() && \"copy expression for variable is missing\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 2060, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CI.getCopyExpr() && "copy expression for variable is missing"); |
| 2061 | EmitSynthesizedCXXCopyCtor(dstField, srcField, CI.getCopyExpr()); |
| 2062 | break; |
| 2063 | case BlockCaptureEntityKind::ARCWeak: |
| 2064 | EmitARCCopyWeak(dstField, srcField); |
| 2065 | break; |
| 2066 | case BlockCaptureEntityKind::NonTrivialCStruct: { |
| 2067 | |
| 2068 | |
| 2069 | QualType varType = CI.getVariable()->getType(); |
| 2070 | callCStructCopyConstructor(MakeAddrLValue(dstField, varType), |
| 2071 | MakeAddrLValue(srcField, varType)); |
| 2072 | break; |
| 2073 | } |
| 2074 | case BlockCaptureEntityKind::ARCStrong: { |
| 2075 | llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); |
| 2076 | |
| 2077 | |
| 2078 | |
| 2079 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 2080 | auto *ty = cast<llvm::PointerType>(srcValue->getType()); |
| 2081 | llvm::Value *null = llvm::ConstantPointerNull::get(ty); |
| 2082 | Builder.CreateStore(null, dstField); |
| 2083 | EmitARCStoreStrongCall(dstField, srcValue, true); |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | |
| 2088 | } else { |
| 2089 | EmitARCRetainNonBlock(srcValue); |
| 2090 | |
| 2091 | |
| 2092 | |
| 2093 | |
| 2094 | if (!needsEHCleanup(captureType.isDestructedType())) |
| 2095 | cast<llvm::Instruction>(dstField.getPointer())->eraseFromParent(); |
| 2096 | } |
| 2097 | break; |
| 2098 | } |
| 2099 | case BlockCaptureEntityKind::BlockObject: { |
| 2100 | llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); |
| 2101 | srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy); |
| 2102 | llvm::Value *dstAddr = |
| 2103 | Builder.CreateBitCast(dstField.getPointer(), VoidPtrTy); |
| 2104 | llvm::Value *args[] = { |
| 2105 | dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask()) |
| 2106 | }; |
| 2107 | |
| 2108 | if (CI.isByRef() && C.getBlockVarCopyInit(CI.getVariable()).canThrow()) |
| 2109 | EmitRuntimeCallOrInvoke(CGM.getBlockObjectAssign(), args); |
| 2110 | else |
| 2111 | EmitNounwindRuntimeCall(CGM.getBlockObjectAssign(), args); |
| 2112 | break; |
| 2113 | } |
| 2114 | case BlockCaptureEntityKind::None: |
| 2115 | continue; |
| 2116 | } |
| 2117 | |
| 2118 | |
| 2119 | |
| 2120 | pushCaptureCleanup(CopiedCapture.CopyKind, dstField, captureType, flags, |
| 2121 | true, CI.getVariable(), *this); |
| 2122 | } |
| 2123 | |
| 2124 | FinishFunction(); |
| 2125 | |
| 2126 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
| 2127 | } |
| 2128 | |
| 2129 | static BlockFieldFlags |
| 2130 | getBlockFieldFlagsForObjCObjectPointer(const BlockDecl::Capture &CI, |
| 2131 | QualType T) { |
| 2132 | BlockFieldFlags Flags = BLOCK_FIELD_IS_OBJECT; |
| 2133 | if (T->isBlockPointerType()) |
| 2134 | Flags = BLOCK_FIELD_IS_BLOCK; |
| 2135 | return Flags; |
| 2136 | } |
| 2137 | |
| 2138 | static std::pair<BlockCaptureEntityKind, BlockFieldFlags> |
| 2139 | computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, |
| 2140 | const LangOptions &LangOpts) { |
| 2141 | if (CI.isEscapingByref()) { |
| 2142 | BlockFieldFlags Flags = BLOCK_FIELD_IS_BYREF; |
| 2143 | if (T.isObjCGCWeak()) |
| 2144 | Flags |= BLOCK_FIELD_IS_WEAK; |
| 2145 | return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags); |
| 2146 | } |
| 2147 | |
| 2148 | switch (T.isDestructedType()) { |
| 2149 | case QualType::DK_cxx_destructor: |
| 2150 | return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags()); |
| 2151 | case QualType::DK_objc_strong_lifetime: |
| 2152 | |
| 2153 | |
| 2154 | return std::make_pair(BlockCaptureEntityKind::ARCStrong, |
| 2155 | getBlockFieldFlagsForObjCObjectPointer(CI, T)); |
| 2156 | case QualType::DK_objc_weak_lifetime: |
| 2157 | |
| 2158 | return std::make_pair(BlockCaptureEntityKind::ARCWeak, |
| 2159 | getBlockFieldFlagsForObjCObjectPointer(CI, T)); |
| 2160 | case QualType::DK_nontrivial_c_struct: |
| 2161 | return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct, |
| 2162 | BlockFieldFlags()); |
| 2163 | case QualType::DK_none: { |
| 2164 | |
| 2165 | if (T->isObjCRetainableType() && !T.getQualifiers().hasObjCLifetime() && |
| 2166 | !LangOpts.ObjCAutoRefCount) |
| 2167 | return std::make_pair(BlockCaptureEntityKind::BlockObject, |
| 2168 | getBlockFieldFlagsForObjCObjectPointer(CI, T)); |
| 2169 | |
| 2170 | return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags()); |
| 2171 | } |
| 2172 | } |
| 2173 | llvm_unreachable("after exhaustive DestructionKind switch"); |
| 2174 | } |
| 2175 | |
| 2176 | |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | |
| 2181 | |
| 2182 | |
| 2183 | llvm::Constant * |
| 2184 | CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) { |
| 2185 | SmallVector<BlockCaptureManagedEntity, 4> DestroyedCaptures; |
| 2186 | findBlockCapturedManagedEntities(blockInfo, getLangOpts(), DestroyedCaptures); |
| 2187 | std::string FuncName = |
| 2188 | getCopyDestroyHelperFuncName(DestroyedCaptures, blockInfo.BlockAlign, |
| 2189 | CaptureStrKind::DisposeHelper, CGM); |
| 2190 | |
| 2191 | if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName)) |
| 2192 | return llvm::ConstantExpr::getBitCast(Func, VoidPtrTy); |
| 2193 | |
| 2194 | ASTContext &C = getContext(); |
| 2195 | |
| 2196 | QualType ReturnTy = C.VoidTy; |
| 2197 | |
| 2198 | FunctionArgList args; |
| 2199 | ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 2200 | args.push_back(&SrcDecl); |
| 2201 | |
| 2202 | const CGFunctionInfo &FI = |
| 2203 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
| 2204 | |
| 2205 | |
| 2206 | |
| 2207 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
| 2208 | |
| 2209 | llvm::Function *Fn = |
| 2210 | llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage, |
| 2211 | FuncName, &CGM.getModule()); |
| 2212 | if (CGM.supportsCOMDAT()) |
| 2213 | Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName)); |
| 2214 | |
| 2215 | IdentifierInfo *II = &C.Idents.get(FuncName); |
| 2216 | |
| 2217 | SmallVector<QualType, 1> ArgTys; |
| 2218 | ArgTys.push_back(C.VoidPtrTy); |
| 2219 | QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {}); |
| 2220 | |
| 2221 | FunctionDecl *FD = FunctionDecl::Create( |
| 2222 | C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II, |
| 2223 | FunctionTy, nullptr, SC_Static, false, false); |
| 2224 | |
| 2225 | setBlockHelperAttributesVisibility(blockInfo.CapturesNonExternalType, Fn, FI, |
| 2226 | CGM); |
| 2227 | StartFunction(FD, ReturnTy, Fn, FI, args); |
| 2228 | markAsIgnoreThreadCheckingAtRuntime(Fn); |
| 2229 | |
| 2230 | ApplyDebugLocation NL{*this, blockInfo.getBlockExpr()->getBeginLoc()}; |
| 2231 | |
| 2232 | llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
| 2233 | |
| 2234 | Address src = GetAddrOfLocalVar(&SrcDecl); |
| 2235 | src = Address(Builder.CreateLoad(src), blockInfo.BlockAlign); |
| 2236 | src = Builder.CreateBitCast(src, structPtrTy, "block"); |
| 2237 | |
| 2238 | CodeGenFunction::RunCleanupsScope cleanups(*this); |
| 2239 | |
| 2240 | for (const auto &DestroyedCapture : DestroyedCaptures) { |
| 2241 | const BlockDecl::Capture &CI = *DestroyedCapture.CI; |
| 2242 | const CGBlockInfo::Capture &capture = *DestroyedCapture.Capture; |
| 2243 | BlockFieldFlags flags = DestroyedCapture.DisposeFlags; |
| 2244 | |
| 2245 | Address srcField = Builder.CreateStructGEP(src, capture.getIndex()); |
| 2246 | |
| 2247 | pushCaptureCleanup(DestroyedCapture.DisposeKind, srcField, |
| 2248 | CI.getVariable()->getType(), flags, |
| 2249 | false, CI.getVariable(), *this); |
| 2250 | } |
| 2251 | |
| 2252 | cleanups.ForceCleanup(); |
| 2253 | |
| 2254 | FinishFunction(); |
| 2255 | |
| 2256 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
| 2257 | } |
| 2258 | |
| 2259 | namespace { |
| 2260 | |
| 2261 | |
| 2262 | class ObjectByrefHelpers final : public BlockByrefHelpers { |
| 2263 | BlockFieldFlags Flags; |
| 2264 | |
| 2265 | public: |
| 2266 | ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags) |
| 2267 | : BlockByrefHelpers(alignment), Flags(flags) {} |
| 2268 | |
| 2269 | void emitCopy(CodeGenFunction &CGF, Address destField, |
| 2270 | Address srcField) override { |
| 2271 | destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy); |
| 2272 | |
| 2273 | srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy); |
| 2274 | llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField); |
| 2275 | |
| 2276 | unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask(); |
| 2277 | |
| 2278 | llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags); |
| 2279 | llvm::FunctionCallee fn = CGF.CGM.getBlockObjectAssign(); |
| 2280 | |
| 2281 | llvm::Value *args[] = { destField.getPointer(), srcValue, flagsVal }; |
| 2282 | CGF.EmitNounwindRuntimeCall(fn, args); |
| 2283 | } |
| 2284 | |
| 2285 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
| 2286 | field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0)); |
| 2287 | llvm::Value *value = CGF.Builder.CreateLoad(field); |
| 2288 | |
| 2289 | CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER, false); |
| 2290 | } |
| 2291 | |
| 2292 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
| 2293 | id.AddInteger(Flags.getBitMask()); |
| 2294 | } |
| 2295 | }; |
| 2296 | |
| 2297 | |
| 2298 | class ARCWeakByrefHelpers final : public BlockByrefHelpers { |
| 2299 | public: |
| 2300 | ARCWeakByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {} |
| 2301 | |
| 2302 | void emitCopy(CodeGenFunction &CGF, Address destField, |
| 2303 | Address srcField) override { |
| 2304 | CGF.EmitARCMoveWeak(destField, srcField); |
| 2305 | } |
| 2306 | |
| 2307 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
| 2308 | CGF.EmitARCDestroyWeak(field); |
| 2309 | } |
| 2310 | |
| 2311 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
| 2312 | |
| 2313 | id.AddInteger(0); |
| 2314 | } |
| 2315 | }; |
| 2316 | |
| 2317 | |
| 2318 | |
| 2319 | class ARCStrongByrefHelpers final : public BlockByrefHelpers { |
| 2320 | public: |
| 2321 | ARCStrongByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {} |
| 2322 | |
| 2323 | void emitCopy(CodeGenFunction &CGF, Address destField, |
| 2324 | Address srcField) override { |
| 2325 | |
| 2326 | |
| 2327 | |
| 2328 | llvm::Value *value = CGF.Builder.CreateLoad(srcField); |
| 2329 | |
| 2330 | llvm::Value *null = |
| 2331 | llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType())); |
| 2332 | |
| 2333 | if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 2334 | CGF.Builder.CreateStore(null, destField); |
| 2335 | CGF.EmitARCStoreStrongCall(destField, value, true); |
| 2336 | CGF.EmitARCStoreStrongCall(srcField, null, true); |
| 2337 | return; |
| 2338 | } |
| 2339 | CGF.Builder.CreateStore(value, destField); |
| 2340 | CGF.Builder.CreateStore(null, srcField); |
| 2341 | } |
| 2342 | |
| 2343 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
| 2344 | CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime); |
| 2345 | } |
| 2346 | |
| 2347 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
| 2348 | |
| 2349 | id.AddInteger(1); |
| 2350 | } |
| 2351 | }; |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | class ARCStrongBlockByrefHelpers final : public BlockByrefHelpers { |
| 2356 | public: |
| 2357 | ARCStrongBlockByrefHelpers(CharUnits alignment) |
| 2358 | : BlockByrefHelpers(alignment) {} |
| 2359 | |
| 2360 | void emitCopy(CodeGenFunction &CGF, Address destField, |
| 2361 | Address srcField) override { |
| 2362 | |
| 2363 | |
| 2364 | |
| 2365 | llvm::Value *oldValue = CGF.Builder.CreateLoad(srcField); |
| 2366 | llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, true); |
| 2367 | CGF.Builder.CreateStore(copy, destField); |
| 2368 | } |
| 2369 | |
| 2370 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
| 2371 | CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime); |
| 2372 | } |
| 2373 | |
| 2374 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
| 2375 | |
| 2376 | id.AddInteger(2); |
| 2377 | } |
| 2378 | }; |
| 2379 | |
| 2380 | |
| 2381 | |
| 2382 | class CXXByrefHelpers final : public BlockByrefHelpers { |
| 2383 | QualType VarType; |
| 2384 | const Expr *CopyExpr; |
| 2385 | |
| 2386 | public: |
| 2387 | CXXByrefHelpers(CharUnits alignment, QualType type, |
| 2388 | const Expr *copyExpr) |
| 2389 | : BlockByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {} |
| 2390 | |
| 2391 | bool needsCopy() const override { return CopyExpr != nullptr; } |
| 2392 | void emitCopy(CodeGenFunction &CGF, Address destField, |
| 2393 | Address srcField) override { |
| 2394 | if (!CopyExpr) return; |
| 2395 | CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr); |
| 2396 | } |
| 2397 | |
| 2398 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
| 2399 | EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin(); |
| 2400 | CGF.PushDestructorCleanup(VarType, field); |
| 2401 | CGF.PopCleanupBlocks(cleanupDepth); |
| 2402 | } |
| 2403 | |
| 2404 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
| 2405 | id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr()); |
| 2406 | } |
| 2407 | }; |
| 2408 | |
| 2409 | |
| 2410 | |
| 2411 | class NonTrivialCStructByrefHelpers final : public BlockByrefHelpers { |
| 2412 | QualType VarType; |
| 2413 | |
| 2414 | public: |
| 2415 | NonTrivialCStructByrefHelpers(CharUnits alignment, QualType type) |
| 2416 | : BlockByrefHelpers(alignment), VarType(type) {} |
| 2417 | |
| 2418 | void emitCopy(CodeGenFunction &CGF, Address destField, |
| 2419 | Address srcField) override { |
| 2420 | CGF.callCStructMoveConstructor(CGF.MakeAddrLValue(destField, VarType), |
| 2421 | CGF.MakeAddrLValue(srcField, VarType)); |
| 2422 | } |
| 2423 | |
| 2424 | bool needsDispose() const override { |
| 2425 | return VarType.isDestructedType(); |
| 2426 | } |
| 2427 | |
| 2428 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
| 2429 | EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin(); |
| 2430 | CGF.pushDestroy(VarType.isDestructedType(), field, VarType); |
| 2431 | CGF.PopCleanupBlocks(cleanupDepth); |
| 2432 | } |
| 2433 | |
| 2434 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
| 2435 | id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr()); |
| 2436 | } |
| 2437 | }; |
| 2438 | } |
| 2439 | |
| 2440 | static llvm::Constant * |
| 2441 | generateByrefCopyHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, |
| 2442 | BlockByrefHelpers &generator) { |
| 2443 | ASTContext &Context = CGF.getContext(); |
| 2444 | |
| 2445 | QualType ReturnTy = Context.VoidTy; |
| 2446 | |
| 2447 | FunctionArgList args; |
| 2448 | ImplicitParamDecl Dst(Context, Context.VoidPtrTy, ImplicitParamDecl::Other); |
| 2449 | args.push_back(&Dst); |
| 2450 | |
| 2451 | ImplicitParamDecl Src(Context, Context.VoidPtrTy, ImplicitParamDecl::Other); |
| 2452 | args.push_back(&Src); |
| 2453 | |
| 2454 | const CGFunctionInfo &FI = |
| 2455 | CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
| 2456 | |
| 2457 | llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI); |
| 2458 | |
| 2459 | |
| 2460 | |
| 2461 | llvm::Function *Fn = |
| 2462 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 2463 | "__Block_byref_object_copy_", &CGF.CGM.getModule()); |
| 2464 | |
| 2465 | IdentifierInfo *II |
| 2466 | = &Context.Idents.get("__Block_byref_object_copy_"); |
| 2467 | |
| 2468 | SmallVector<QualType, 2> ArgTys; |
| 2469 | ArgTys.push_back(Context.VoidPtrTy); |
| 2470 | ArgTys.push_back(Context.VoidPtrTy); |
| 2471 | QualType FunctionTy = Context.getFunctionType(ReturnTy, ArgTys, {}); |
| 2472 | |
| 2473 | FunctionDecl *FD = FunctionDecl::Create( |
| 2474 | Context, Context.getTranslationUnitDecl(), SourceLocation(), |
| 2475 | SourceLocation(), II, FunctionTy, nullptr, SC_Static, false, false); |
| 2476 | |
| 2477 | CGF.CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
| 2478 | |
| 2479 | CGF.StartFunction(FD, ReturnTy, Fn, FI, args); |
| 2480 | |
| 2481 | if (generator.needsCopy()) { |
| 2482 | llvm::Type *byrefPtrType = byrefInfo.Type->getPointerTo(0); |
| 2483 | |
| 2484 | |
| 2485 | Address destField = CGF.GetAddrOfLocalVar(&Dst); |
| 2486 | destField = Address(CGF.Builder.CreateLoad(destField), |
| 2487 | byrefInfo.ByrefAlignment); |
| 2488 | destField = CGF.Builder.CreateBitCast(destField, byrefPtrType); |
| 2489 | destField = CGF.emitBlockByrefAddress(destField, byrefInfo, false, |
| 2490 | "dest-object"); |
| 2491 | |
| 2492 | |
| 2493 | Address srcField = CGF.GetAddrOfLocalVar(&Src); |
| 2494 | srcField = Address(CGF.Builder.CreateLoad(srcField), |
| 2495 | byrefInfo.ByrefAlignment); |
| 2496 | srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType); |
| 2497 | srcField = CGF.emitBlockByrefAddress(srcField, byrefInfo, false, |
| 2498 | "src-object"); |
| 2499 | |
| 2500 | generator.emitCopy(CGF, destField, srcField); |
| 2501 | } |
| 2502 | |
| 2503 | CGF.FinishFunction(); |
| 2504 | |
| 2505 | return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); |
| 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM, |
| 2510 | const BlockByrefInfo &byrefInfo, |
| 2511 | BlockByrefHelpers &generator) { |
| 2512 | CodeGenFunction CGF(CGM); |
| 2513 | return generateByrefCopyHelper(CGF, byrefInfo, generator); |
| 2514 | } |
| 2515 | |
| 2516 | |
| 2517 | static llvm::Constant * |
| 2518 | generateByrefDisposeHelper(CodeGenFunction &CGF, |
| 2519 | const BlockByrefInfo &byrefInfo, |
| 2520 | BlockByrefHelpers &generator) { |
| 2521 | ASTContext &Context = CGF.getContext(); |
| 2522 | QualType R = Context.VoidTy; |
| 2523 | |
| 2524 | FunctionArgList args; |
| 2525 | ImplicitParamDecl Src(CGF.getContext(), Context.VoidPtrTy, |
| 2526 | ImplicitParamDecl::Other); |
| 2527 | args.push_back(&Src); |
| 2528 | |
| 2529 | const CGFunctionInfo &FI = |
| 2530 | CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(R, args); |
| 2531 | |
| 2532 | llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI); |
| 2533 | |
| 2534 | |
| 2535 | |
| 2536 | llvm::Function *Fn = |
| 2537 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 2538 | "__Block_byref_object_dispose_", |
| 2539 | &CGF.CGM.getModule()); |
| 2540 | |
| 2541 | IdentifierInfo *II |
| 2542 | = &Context.Idents.get("__Block_byref_object_dispose_"); |
| 2543 | |
| 2544 | SmallVector<QualType, 1> ArgTys; |
| 2545 | ArgTys.push_back(Context.VoidPtrTy); |
| 2546 | QualType FunctionTy = Context.getFunctionType(R, ArgTys, {}); |
| 2547 | |
| 2548 | FunctionDecl *FD = FunctionDecl::Create( |
| 2549 | Context, Context.getTranslationUnitDecl(), SourceLocation(), |
| 2550 | SourceLocation(), II, FunctionTy, nullptr, SC_Static, false, false); |
| 2551 | |
| 2552 | CGF.CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
| 2553 | |
| 2554 | CGF.StartFunction(FD, R, Fn, FI, args); |
| 2555 | |
| 2556 | if (generator.needsDispose()) { |
| 2557 | Address addr = CGF.GetAddrOfLocalVar(&Src); |
| 2558 | addr = Address(CGF.Builder.CreateLoad(addr), byrefInfo.ByrefAlignment); |
| 2559 | auto byrefPtrType = byrefInfo.Type->getPointerTo(0); |
| 2560 | addr = CGF.Builder.CreateBitCast(addr, byrefPtrType); |
| 2561 | addr = CGF.emitBlockByrefAddress(addr, byrefInfo, false, "object"); |
| 2562 | |
| 2563 | generator.emitDispose(CGF, addr); |
| 2564 | } |
| 2565 | |
| 2566 | CGF.FinishFunction(); |
| 2567 | |
| 2568 | return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); |
| 2569 | } |
| 2570 | |
| 2571 | |
| 2572 | static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM, |
| 2573 | const BlockByrefInfo &byrefInfo, |
| 2574 | BlockByrefHelpers &generator) { |
| 2575 | CodeGenFunction CGF(CGM); |
| 2576 | return generateByrefDisposeHelper(CGF, byrefInfo, generator); |
| 2577 | } |
| 2578 | |
| 2579 | |
| 2580 | |
| 2581 | template <class T> |
| 2582 | static T *buildByrefHelpers(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, |
| 2583 | T &&generator) { |
| 2584 | llvm::FoldingSetNodeID id; |
| 2585 | generator.Profile(id); |
| 2586 | |
| 2587 | void *insertPos; |
| 2588 | BlockByrefHelpers *node |
| 2589 | = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos); |
| 2590 | if (node) return static_cast<T*>(node); |
| 2591 | |
| 2592 | generator.CopyHelper = buildByrefCopyHelper(CGM, byrefInfo, generator); |
| 2593 | generator.DisposeHelper = buildByrefDisposeHelper(CGM, byrefInfo, generator); |
| 2594 | |
| 2595 | T *copy = new (CGM.getContext()) T(std::forward<T>(generator)); |
| 2596 | CGM.ByrefHelpersCache.InsertNode(copy, insertPos); |
| 2597 | return copy; |
| 2598 | } |
| 2599 | |
| 2600 | |
| 2601 | |
| 2602 | |
| 2603 | BlockByrefHelpers * |
| 2604 | CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType, |
| 2605 | const AutoVarEmission &emission) { |
| 2606 | const VarDecl &var = *emission.Variable; |
| 2607 | (0) . __assert_fail ("var.isEscapingByref() && \"only escaping __block variables need byref helpers\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 2608, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(var.isEscapingByref() && |
| 2608 | (0) . __assert_fail ("var.isEscapingByref() && \"only escaping __block variables need byref helpers\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 2608, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "only escaping __block variables need byref helpers"); |
| 2609 | |
| 2610 | QualType type = var.getType(); |
| 2611 | |
| 2612 | auto &byrefInfo = getBlockByrefInfo(&var); |
| 2613 | |
| 2614 | |
| 2615 | |
| 2616 | CharUnits valueAlignment = |
| 2617 | byrefInfo.ByrefAlignment.alignmentAtOffset(byrefInfo.FieldOffset); |
| 2618 | |
| 2619 | if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { |
| 2620 | const Expr *copyExpr = |
| 2621 | CGM.getContext().getBlockVarCopyInit(&var).getCopyExpr(); |
| 2622 | if (!copyExpr && record->hasTrivialDestructor()) return nullptr; |
| 2623 | |
| 2624 | return ::buildByrefHelpers( |
| 2625 | CGM, byrefInfo, CXXByrefHelpers(valueAlignment, type, copyExpr)); |
| 2626 | } |
| 2627 | |
| 2628 | |
| 2629 | |
| 2630 | if (type.isNonTrivialToPrimitiveDestructiveMove() == QualType::PCK_Struct || |
| 2631 | type.isDestructedType() == QualType::DK_nontrivial_c_struct) |
| 2632 | return ::buildByrefHelpers( |
| 2633 | CGM, byrefInfo, NonTrivialCStructByrefHelpers(valueAlignment, type)); |
| 2634 | |
| 2635 | |
| 2636 | |
| 2637 | if (!type->isObjCRetainableType()) return nullptr; |
| 2638 | |
| 2639 | Qualifiers qs = type.getQualifiers(); |
| 2640 | |
| 2641 | |
| 2642 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 2643 | switch (lifetime) { |
| 2644 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 2645 | |
| 2646 | |
| 2647 | case Qualifiers::OCL_ExplicitNone: |
| 2648 | case Qualifiers::OCL_Autoreleasing: |
| 2649 | return nullptr; |
| 2650 | |
| 2651 | |
| 2652 | |
| 2653 | case Qualifiers::OCL_Weak: |
| 2654 | return ::buildByrefHelpers(CGM, byrefInfo, |
| 2655 | ARCWeakByrefHelpers(valueAlignment)); |
| 2656 | |
| 2657 | |
| 2658 | case Qualifiers::OCL_Strong: |
| 2659 | |
| 2660 | |
| 2661 | if (type->isBlockPointerType()) { |
| 2662 | return ::buildByrefHelpers(CGM, byrefInfo, |
| 2663 | ARCStrongBlockByrefHelpers(valueAlignment)); |
| 2664 | |
| 2665 | |
| 2666 | |
| 2667 | } else { |
| 2668 | return ::buildByrefHelpers(CGM, byrefInfo, |
| 2669 | ARCStrongByrefHelpers(valueAlignment)); |
| 2670 | } |
| 2671 | } |
| 2672 | llvm_unreachable("fell out of lifetime switch!"); |
| 2673 | } |
| 2674 | |
| 2675 | BlockFieldFlags flags; |
| 2676 | if (type->isBlockPointerType()) { |
| 2677 | flags |= BLOCK_FIELD_IS_BLOCK; |
| 2678 | } else if (CGM.getContext().isObjCNSObjectType(type) || |
| 2679 | type->isObjCObjectPointerType()) { |
| 2680 | flags |= BLOCK_FIELD_IS_OBJECT; |
| 2681 | } else { |
| 2682 | return nullptr; |
| 2683 | } |
| 2684 | |
| 2685 | if (type.isObjCGCWeak()) |
| 2686 | flags |= BLOCK_FIELD_IS_WEAK; |
| 2687 | |
| 2688 | return ::buildByrefHelpers(CGM, byrefInfo, |
| 2689 | ObjectByrefHelpers(valueAlignment, flags)); |
| 2690 | } |
| 2691 | |
| 2692 | Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr, |
| 2693 | const VarDecl *var, |
| 2694 | bool followForward) { |
| 2695 | auto &info = getBlockByrefInfo(var); |
| 2696 | return emitBlockByrefAddress(baseAddr, info, followForward, var->getName()); |
| 2697 | } |
| 2698 | |
| 2699 | Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr, |
| 2700 | const BlockByrefInfo &info, |
| 2701 | bool followForward, |
| 2702 | const llvm::Twine &name) { |
| 2703 | |
| 2704 | if (followForward) { |
| 2705 | Address forwardingAddr = Builder.CreateStructGEP(baseAddr, 1, "forwarding"); |
| 2706 | baseAddr = Address(Builder.CreateLoad(forwardingAddr), info.ByrefAlignment); |
| 2707 | } |
| 2708 | |
| 2709 | return Builder.CreateStructGEP(baseAddr, info.FieldIndex, name); |
| 2710 | } |
| 2711 | |
| 2712 | |
| 2713 | |
| 2714 | |
| 2715 | |
| 2716 | |
| 2717 | |
| 2718 | |
| 2719 | |
| 2720 | |
| 2721 | |
| 2722 | |
| 2723 | |
| 2724 | |
| 2725 | |
| 2726 | |
| 2727 | const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) { |
| 2728 | auto it = BlockByrefInfos.find(D); |
| 2729 | if (it != BlockByrefInfos.end()) |
| 2730 | return it->second; |
| 2731 | |
| 2732 | llvm::StructType *byrefType = |
| 2733 | llvm::StructType::create(getLLVMContext(), |
| 2734 | "struct.__block_byref_" + D->getNameAsString()); |
| 2735 | |
| 2736 | QualType Ty = D->getType(); |
| 2737 | |
| 2738 | CharUnits size; |
| 2739 | SmallVector<llvm::Type *, 8> types; |
| 2740 | |
| 2741 | |
| 2742 | types.push_back(Int8PtrTy); |
| 2743 | size += getPointerSize(); |
| 2744 | |
| 2745 | |
| 2746 | types.push_back(llvm::PointerType::getUnqual(byrefType)); |
| 2747 | size += getPointerSize(); |
| 2748 | |
| 2749 | |
| 2750 | types.push_back(Int32Ty); |
| 2751 | size += CharUnits::fromQuantity(4); |
| 2752 | |
| 2753 | |
| 2754 | types.push_back(Int32Ty); |
| 2755 | size += CharUnits::fromQuantity(4); |
| 2756 | |
| 2757 | |
| 2758 | bool hasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D); |
| 2759 | if (hasCopyAndDispose) { |
| 2760 | |
| 2761 | types.push_back(Int8PtrTy); |
| 2762 | size += getPointerSize(); |
| 2763 | |
| 2764 | |
| 2765 | types.push_back(Int8PtrTy); |
| 2766 | size += getPointerSize(); |
| 2767 | } |
| 2768 | |
| 2769 | bool HasByrefExtendedLayout = false; |
| 2770 | Qualifiers::ObjCLifetime Lifetime; |
| 2771 | if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) && |
| 2772 | HasByrefExtendedLayout) { |
| 2773 | |
| 2774 | types.push_back(Int8PtrTy); |
| 2775 | size += CharUnits::fromQuantity(PointerSizeInBytes); |
| 2776 | } |
| 2777 | |
| 2778 | |
| 2779 | llvm::Type *varTy = ConvertTypeForMem(Ty); |
| 2780 | |
| 2781 | bool packed = false; |
| 2782 | CharUnits varAlign = getContext().getDeclAlign(D); |
| 2783 | CharUnits varOffset = size.alignTo(varAlign); |
| 2784 | |
| 2785 | |
| 2786 | if (varOffset != size) { |
| 2787 | llvm::Type *paddingTy = |
| 2788 | llvm::ArrayType::get(Int8Ty, (varOffset - size).getQuantity()); |
| 2789 | |
| 2790 | types.push_back(paddingTy); |
| 2791 | size = varOffset; |
| 2792 | |
| 2793 | |
| 2794 | } else if (CGM.getDataLayout().getABITypeAlignment(varTy) |
| 2795 | > varAlign.getQuantity()) { |
| 2796 | packed = true; |
| 2797 | } |
| 2798 | types.push_back(varTy); |
| 2799 | |
| 2800 | byrefType->setBody(types, packed); |
| 2801 | |
| 2802 | BlockByrefInfo info; |
| 2803 | info.Type = byrefType; |
| 2804 | info.FieldIndex = types.size() - 1; |
| 2805 | info.FieldOffset = varOffset; |
| 2806 | info.ByrefAlignment = std::max(varAlign, getPointerAlign()); |
| 2807 | |
| 2808 | auto pair = BlockByrefInfos.insert({D, info}); |
| 2809 | (0) . __assert_fail ("pair.second && \"info was inserted recursively?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 2809, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(pair.second && "info was inserted recursively?"); |
| 2810 | return pair.first->second; |
| 2811 | } |
| 2812 | |
| 2813 | |
| 2814 | |
| 2815 | void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) { |
| 2816 | |
| 2817 | Address addr = emission.Addr; |
| 2818 | |
| 2819 | |
| 2820 | llvm::StructType *byrefType = cast<llvm::StructType>( |
| 2821 | cast<llvm::PointerType>(addr.getPointer()->getType())->getElementType()); |
| 2822 | |
| 2823 | unsigned = 0; |
| 2824 | CharUnits ; |
| 2825 | auto = [&](llvm::Value *value, CharUnits fieldSize, |
| 2826 | const Twine &name) { |
| 2827 | auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex, name); |
| 2828 | Builder.CreateStore(value, fieldAddr); |
| 2829 | |
| 2830 | nextHeaderIndex++; |
| 2831 | nextHeaderOffset += fieldSize; |
| 2832 | }; |
| 2833 | |
| 2834 | |
| 2835 | BlockByrefHelpers *helpers = buildByrefHelpers(*byrefType, emission); |
| 2836 | |
| 2837 | const VarDecl &D = *emission.Variable; |
| 2838 | QualType type = D.getType(); |
| 2839 | |
| 2840 | bool HasByrefExtendedLayout; |
| 2841 | Qualifiers::ObjCLifetime ByrefLifetime; |
| 2842 | bool ByRefHasLifetime = |
| 2843 | getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout); |
| 2844 | |
| 2845 | llvm::Value *V; |
| 2846 | |
| 2847 | |
| 2848 | int isa = 0; |
| 2849 | if (type.isObjCGCWeak()) |
| 2850 | isa = 1; |
| 2851 | V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa"); |
| 2852 | storeHeaderField(V, getPointerSize(), "byref.isa"); |
| 2853 | |
| 2854 | |
| 2855 | storeHeaderField(addr.getPointer(), getPointerSize(), "byref.forwarding"); |
| 2856 | |
| 2857 | |
| 2858 | |
| 2859 | |
| 2860 | BlockFlags flags; |
| 2861 | if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE; |
| 2862 | if (ByRefHasLifetime) { |
| 2863 | if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED; |
| 2864 | else switch (ByrefLifetime) { |
| 2865 | case Qualifiers::OCL_Strong: |
| 2866 | flags |= BLOCK_BYREF_LAYOUT_STRONG; |
| 2867 | break; |
| 2868 | case Qualifiers::OCL_Weak: |
| 2869 | flags |= BLOCK_BYREF_LAYOUT_WEAK; |
| 2870 | break; |
| 2871 | case Qualifiers::OCL_ExplicitNone: |
| 2872 | flags |= BLOCK_BYREF_LAYOUT_UNRETAINED; |
| 2873 | break; |
| 2874 | case Qualifiers::OCL_None: |
| 2875 | if (!type->isObjCObjectPointerType() && !type->isBlockPointerType()) |
| 2876 | flags |= BLOCK_BYREF_LAYOUT_NON_OBJECT; |
| 2877 | break; |
| 2878 | default: |
| 2879 | break; |
| 2880 | } |
| 2881 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2882 | printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask()); |
| 2883 | if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE) |
| 2884 | printf(" BLOCK_BYREF_HAS_COPY_DISPOSE"); |
| 2885 | if (flags & BLOCK_BYREF_LAYOUT_MASK) { |
| 2886 | BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK); |
| 2887 | if (ThisFlag == BLOCK_BYREF_LAYOUT_EXTENDED) |
| 2888 | printf(" BLOCK_BYREF_LAYOUT_EXTENDED"); |
| 2889 | if (ThisFlag == BLOCK_BYREF_LAYOUT_STRONG) |
| 2890 | printf(" BLOCK_BYREF_LAYOUT_STRONG"); |
| 2891 | if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK) |
| 2892 | printf(" BLOCK_BYREF_LAYOUT_WEAK"); |
| 2893 | if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED) |
| 2894 | printf(" BLOCK_BYREF_LAYOUT_UNRETAINED"); |
| 2895 | if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT) |
| 2896 | printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT"); |
| 2897 | } |
| 2898 | printf("\n"); |
| 2899 | } |
| 2900 | } |
| 2901 | storeHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()), |
| 2902 | getIntSize(), "byref.flags"); |
| 2903 | |
| 2904 | CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType); |
| 2905 | V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity()); |
| 2906 | storeHeaderField(V, getIntSize(), "byref.size"); |
| 2907 | |
| 2908 | if (helpers) { |
| 2909 | storeHeaderField(helpers->CopyHelper, getPointerSize(), |
| 2910 | "byref.copyHelper"); |
| 2911 | storeHeaderField(helpers->DisposeHelper, getPointerSize(), |
| 2912 | "byref.disposeHelper"); |
| 2913 | } |
| 2914 | |
| 2915 | if (ByRefHasLifetime && HasByrefExtendedLayout) { |
| 2916 | auto layoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type); |
| 2917 | storeHeaderField(layoutInfo, getPointerSize(), "byref.layout"); |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags, |
| 2922 | bool CanThrow) { |
| 2923 | llvm::FunctionCallee F = CGM.getBlockObjectDispose(); |
| 2924 | llvm::Value *args[] = { |
| 2925 | Builder.CreateBitCast(V, Int8PtrTy), |
| 2926 | llvm::ConstantInt::get(Int32Ty, flags.getBitMask()) |
| 2927 | }; |
| 2928 | |
| 2929 | if (CanThrow) |
| 2930 | EmitRuntimeCallOrInvoke(F, args); |
| 2931 | else |
| 2932 | EmitNounwindRuntimeCall(F, args); |
| 2933 | } |
| 2934 | |
| 2935 | void CodeGenFunction::enterByrefCleanup(CleanupKind Kind, Address Addr, |
| 2936 | BlockFieldFlags Flags, |
| 2937 | bool LoadBlockVarAddr, bool CanThrow) { |
| 2938 | EHStack.pushCleanup<CallBlockRelease>(Kind, Addr, Flags, LoadBlockVarAddr, |
| 2939 | CanThrow); |
| 2940 | } |
| 2941 | |
| 2942 | |
| 2943 | static void configureBlocksRuntimeObject(CodeGenModule &CGM, |
| 2944 | llvm::Constant *C) { |
| 2945 | auto *GV = cast<llvm::GlobalValue>(C->stripPointerCasts()); |
| 2946 | |
| 2947 | if (CGM.getTarget().getTriple().isOSBinFormatCOFF()) { |
| 2948 | IdentifierInfo &II = CGM.getContext().Idents.get(C->getName()); |
| 2949 | TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); |
| 2950 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 2951 | |
| 2952 | (0) . __assert_fail ("(isa(C->stripPointerCasts()) || isa(C->stripPointerCasts())) && \"expected Function or GlobalVariable\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 2954, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((isa<llvm::Function>(C->stripPointerCasts()) || |
| 2953 | (0) . __assert_fail ("(isa(C->stripPointerCasts()) || isa(C->stripPointerCasts())) && \"expected Function or GlobalVariable\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 2954, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> isa<llvm::GlobalVariable>(C->stripPointerCasts())) && |
| 2954 | (0) . __assert_fail ("(isa(C->stripPointerCasts()) || isa(C->stripPointerCasts())) && \"expected Function or GlobalVariable\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGBlocks.cpp", 2954, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "expected Function or GlobalVariable"); |
| 2955 | |
| 2956 | const NamedDecl *ND = nullptr; |
| 2957 | for (const auto &Result : DC->lookup(&II)) |
| 2958 | if ((ND = dyn_cast<FunctionDecl>(Result)) || |
| 2959 | (ND = dyn_cast<VarDecl>(Result))) |
| 2960 | break; |
| 2961 | |
| 2962 | |
| 2963 | if (GV->isDeclaration() && (!ND || !ND->hasAttr<DLLExportAttr>())) { |
| 2964 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 2965 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2966 | } else { |
| 2967 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 2968 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | if (CGM.getLangOpts().BlocksRuntimeOptional && GV->isDeclaration() && |
| 2973 | GV->hasExternalLinkage()) |
| 2974 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 2975 | |
| 2976 | CGM.setDSOLocal(GV); |
| 2977 | } |
| 2978 | |
| 2979 | llvm::FunctionCallee CodeGenModule::getBlockObjectDispose() { |
| 2980 | if (BlockObjectDispose) |
| 2981 | return BlockObjectDispose; |
| 2982 | |
| 2983 | llvm::Type *args[] = { Int8PtrTy, Int32Ty }; |
| 2984 | llvm::FunctionType *fty |
| 2985 | = llvm::FunctionType::get(VoidTy, args, false); |
| 2986 | BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose"); |
| 2987 | configureBlocksRuntimeObject( |
| 2988 | *this, cast<llvm::Constant>(BlockObjectDispose.getCallee())); |
| 2989 | return BlockObjectDispose; |
| 2990 | } |
| 2991 | |
| 2992 | llvm::FunctionCallee CodeGenModule::getBlockObjectAssign() { |
| 2993 | if (BlockObjectAssign) |
| 2994 | return BlockObjectAssign; |
| 2995 | |
| 2996 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty }; |
| 2997 | llvm::FunctionType *fty |
| 2998 | = llvm::FunctionType::get(VoidTy, args, false); |
| 2999 | BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign"); |
| 3000 | configureBlocksRuntimeObject( |
| 3001 | *this, cast<llvm::Constant>(BlockObjectAssign.getCallee())); |
| 3002 | return BlockObjectAssign; |
| 3003 | } |
| 3004 | |
| 3005 | llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { |
| 3006 | if (NSConcreteGlobalBlock) |
| 3007 | return NSConcreteGlobalBlock; |
| 3008 | |
| 3009 | NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", |
| 3010 | Int8PtrTy->getPointerTo(), |
| 3011 | nullptr); |
| 3012 | configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock); |
| 3013 | return NSConcreteGlobalBlock; |
| 3014 | } |
| 3015 | |
| 3016 | llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { |
| 3017 | if (NSConcreteStackBlock) |
| 3018 | return NSConcreteStackBlock; |
| 3019 | |
| 3020 | NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock", |
| 3021 | Int8PtrTy->getPointerTo(), |
| 3022 | nullptr); |
| 3023 | configureBlocksRuntimeObject(*this, NSConcreteStackBlock); |
| 3024 | return NSConcreteStackBlock; |
| 3025 | } |
| 3026 | |