clang 19.0.0git
CGBlocks.cpp
Go to the documentation of this file.
1//===--- CGBlocks.cpp - Emit LLVM Code for declarations ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code to emit blocks.
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/Attr.h"
23#include "clang/AST/DeclObjC.h"
25#include "llvm/ADT/SmallSet.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/Module.h"
28#include "llvm/Support/ScopedPrinter.h"
29#include <algorithm>
30#include <cstdio>
31
32using namespace clang;
33using namespace CodeGen;
34
35CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name)
36 : Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
37 NoEscape(false), HasCXXObject(false), UsesStret(false),
38 HasCapturedVariableLayout(false), CapturesNonExternalType(false),
39 LocalAddress(Address::invalid()), StructureType(nullptr), Block(block) {
40
41 // Skip asm prefix, if any. 'name' is usually taken directly from
42 // the mangled name of the enclosing function.
43 if (!name.empty() && name[0] == '\01')
44 name = name.substr(1);
45}
46
47// Anchor the vtable to this translation unit.
49
50/// Build the given block as a global block.
51static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
52 const CGBlockInfo &blockInfo,
53 llvm::Constant *blockFn);
54
55/// Build the helper function to copy a block.
56static llvm::Constant *buildCopyHelper(CodeGenModule &CGM,
57 const CGBlockInfo &blockInfo) {
58 return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo);
59}
60
61/// Build the helper function to dispose of a block.
62static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM,
63 const CGBlockInfo &blockInfo) {
65}
66
67namespace {
68
69enum class CaptureStrKind {
70 // String for the copy helper.
71 CopyHelper,
72 // String for the dispose helper.
73 DisposeHelper,
74 // Merge the strings for the copy helper and dispose helper.
75 Merged
76};
77
78} // end anonymous namespace
79
80static std::string getBlockCaptureStr(const CGBlockInfo::Capture &Cap,
81 CaptureStrKind StrKind,
82 CharUnits BlockAlignment,
83 CodeGenModule &CGM);
84
85static std::string getBlockDescriptorName(const CGBlockInfo &BlockInfo,
86 CodeGenModule &CGM) {
87 std::string Name = "__block_descriptor_";
88 Name += llvm::to_string(BlockInfo.BlockSize.getQuantity()) + "_";
89
90 if (BlockInfo.NeedsCopyDispose) {
91 if (CGM.getLangOpts().Exceptions)
92 Name += "e";
93 if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
94 Name += "a";
95 Name += llvm::to_string(BlockInfo.BlockAlign.getQuantity()) + "_";
96
97 for (auto &Cap : BlockInfo.SortedCaptures) {
98 if (Cap.isConstantOrTrivial())
99 continue;
100
101 Name += llvm::to_string(Cap.getOffset().getQuantity());
102
103 if (Cap.CopyKind == Cap.DisposeKind) {
104 // If CopyKind and DisposeKind are the same, merge the capture
105 // information.
106 assert(Cap.CopyKind != BlockCaptureEntityKind::None &&
107 "shouldn't see BlockCaptureManagedEntity that is None");
108 Name += getBlockCaptureStr(Cap, CaptureStrKind::Merged,
109 BlockInfo.BlockAlign, CGM);
110 } else {
111 // If CopyKind and DisposeKind are not the same, which can happen when
112 // either Kind is None or the captured object is a __strong block,
113 // concatenate the copy and dispose strings.
114 Name += getBlockCaptureStr(Cap, CaptureStrKind::CopyHelper,
115 BlockInfo.BlockAlign, CGM);
116 Name += getBlockCaptureStr(Cap, CaptureStrKind::DisposeHelper,
117 BlockInfo.BlockAlign, CGM);
118 }
119 }
120 Name += "_";
121 }
122
123 std::string TypeAtEncoding =
125 /// Replace occurrences of '@' with '\1'. '@' is reserved on ELF platforms as
126 /// a separator between symbol name and symbol version.
127 std::replace(TypeAtEncoding.begin(), TypeAtEncoding.end(), '@', '\1');
128 Name += "e" + llvm::to_string(TypeAtEncoding.size()) + "_" + TypeAtEncoding;
129 Name += "l" + CGM.getObjCRuntime().getRCBlockLayoutStr(CGM, BlockInfo);
130 return Name;
131}
132
133/// buildBlockDescriptor - Build the block descriptor meta-data for a block.
134/// buildBlockDescriptor is accessed from 5th field of the Block_literal
135/// meta-data and contains stationary information about the block literal.
136/// Its definition will have 4 (or optionally 6) words.
137/// \code
138/// struct Block_descriptor {
139/// unsigned long reserved;
140/// unsigned long size; // size of Block_literal metadata in bytes.
141/// void *copy_func_helper_decl; // optional copy helper.
142/// void *destroy_func_decl; // optional destructor helper.
143/// void *block_method_encoding_address; // @encode for block literal signature.
144/// void *block_layout_info; // encoding of captured block variables.
145/// };
146/// \endcode
147static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
148 const CGBlockInfo &blockInfo) {
149 ASTContext &C = CGM.getContext();
150
151 llvm::IntegerType *ulong =
152 cast<llvm::IntegerType>(CGM.getTypes().ConvertType(C.UnsignedLongTy));
153 llvm::PointerType *i8p = nullptr;
154 if (CGM.getLangOpts().OpenCL)
155 i8p = llvm::PointerType::get(
156 CGM.getLLVMContext(), C.getTargetAddressSpace(LangAS::opencl_constant));
157 else
158 i8p = CGM.VoidPtrTy;
159
160 std::string descName;
161
162 // If an equivalent block descriptor global variable exists, return it.
163 if (C.getLangOpts().ObjC &&
164 CGM.getLangOpts().getGC() == LangOptions::NonGC) {
165 descName = getBlockDescriptorName(blockInfo, CGM);
166 if (llvm::GlobalValue *desc = CGM.getModule().getNamedValue(descName))
167 return desc;
168 }
169
170 // If there isn't an equivalent block descriptor global variable, create a new
171 // one.
172 ConstantInitBuilder builder(CGM);
173 auto elements = builder.beginStruct();
174
175 // reserved
176 elements.addInt(ulong, 0);
177
178 // Size
179 // FIXME: What is the right way to say this doesn't fit? We should give
180 // a user diagnostic in that case. Better fix would be to change the
181 // API to size_t.
182 elements.addInt(ulong, blockInfo.BlockSize.getQuantity());
183
184 // Optional copy/dispose helpers.
185 bool hasInternalHelper = false;
186 if (blockInfo.NeedsCopyDispose) {
187 // copy_func_helper_decl
188 llvm::Constant *copyHelper = buildCopyHelper(CGM, blockInfo);
189 elements.add(copyHelper);
190
191 // destroy_func_decl
192 llvm::Constant *disposeHelper = buildDisposeHelper(CGM, blockInfo);
193 elements.add(disposeHelper);
194
195 if (cast<llvm::Function>(copyHelper->stripPointerCasts())
196 ->hasInternalLinkage() ||
197 cast<llvm::Function>(disposeHelper->stripPointerCasts())
198 ->hasInternalLinkage())
199 hasInternalHelper = true;
200 }
201
202 // Signature. Mandatory ObjC-style method descriptor @encode sequence.
203 std::string typeAtEncoding =
205 elements.add(CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer());
206
207 // GC layout.
208 if (C.getLangOpts().ObjC) {
209 if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
210 elements.add(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo));
211 else
212 elements.add(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo));
213 }
214 else
215 elements.addNullPointer(i8p);
216
217 unsigned AddrSpace = 0;
218 if (C.getLangOpts().OpenCL)
219 AddrSpace = C.getTargetAddressSpace(LangAS::opencl_constant);
220
221 llvm::GlobalValue::LinkageTypes linkage;
222 if (descName.empty()) {
223 linkage = llvm::GlobalValue::InternalLinkage;
224 descName = "__block_descriptor_tmp";
225 } else if (hasInternalHelper) {
226 // If either the copy helper or the dispose helper has internal linkage,
227 // the block descriptor must have internal linkage too.
228 linkage = llvm::GlobalValue::InternalLinkage;
229 } else {
230 linkage = llvm::GlobalValue::LinkOnceODRLinkage;
231 }
232
233 llvm::GlobalVariable *global =
234 elements.finishAndCreateGlobal(descName, CGM.getPointerAlign(),
235 /*constant*/ true, linkage, AddrSpace);
236
237 if (linkage == llvm::GlobalValue::LinkOnceODRLinkage) {
238 if (CGM.supportsCOMDAT())
239 global->setComdat(CGM.getModule().getOrInsertComdat(descName));
240 global->setVisibility(llvm::GlobalValue::HiddenVisibility);
241 global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
242 }
243
244 return global;
245}
246
247/*
248 Purely notional variadic template describing the layout of a block.
249
250 template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
251 struct Block_literal {
252 /// Initialized to one of:
253 /// extern void *_NSConcreteStackBlock[];
254 /// extern void *_NSConcreteGlobalBlock[];
255 ///
256 /// In theory, we could start one off malloc'ed by setting
257 /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
258 /// this isa:
259 /// extern void *_NSConcreteMallocBlock[];
260 struct objc_class *isa;
261
262 /// These are the flags (with corresponding bit number) that the
263 /// compiler is actually supposed to know about.
264 /// 23. BLOCK_IS_NOESCAPE - indicates that the block is non-escaping
265 /// 25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
266 /// descriptor provides copy and dispose helper functions
267 /// 26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
268 /// object with a nontrivial destructor or copy constructor
269 /// 28. BLOCK_IS_GLOBAL - indicates that the block is allocated
270 /// as global memory
271 /// 29. BLOCK_USE_STRET - indicates that the block function
272 /// uses stret, which objc_msgSend needs to know about
273 /// 30. BLOCK_HAS_SIGNATURE - indicates that the block has an
274 /// @encoded signature string
275 /// And we're not supposed to manipulate these:
276 /// 24. BLOCK_NEEDS_FREE - indicates that the block has been moved
277 /// to malloc'ed memory
278 /// 27. BLOCK_IS_GC - indicates that the block has been moved to
279 /// to GC-allocated memory
280 /// Additionally, the bottom 16 bits are a reference count which
281 /// should be zero on the stack.
282 int flags;
283
284 /// Reserved; should be zero-initialized.
285 int reserved;
286
287 /// Function pointer generated from block literal.
288 _ResultType (*invoke)(Block_literal *, _ParamTypes...);
289
290 /// Block description metadata generated from block literal.
291 struct Block_descriptor *block_descriptor;
292
293 /// Captured values follow.
294 _CapturesTypes captures...;
295 };
296 */
297
298namespace {
299 /// A chunk of data that we actually have to capture in the block.
300 struct BlockLayoutChunk {
301 CharUnits Alignment;
303 const BlockDecl::Capture *Capture; // null for 'this'
304 llvm::Type *Type;
305 QualType FieldType;
306 BlockCaptureEntityKind CopyKind, DisposeKind;
307 BlockFieldFlags CopyFlags, DisposeFlags;
308
309 BlockLayoutChunk(CharUnits align, CharUnits size,
310 const BlockDecl::Capture *capture, llvm::Type *type,
311 QualType fieldType, BlockCaptureEntityKind CopyKind,
312 BlockFieldFlags CopyFlags,
313 BlockCaptureEntityKind DisposeKind,
314 BlockFieldFlags DisposeFlags)
315 : Alignment(align), Size(size), Capture(capture), Type(type),
316 FieldType(fieldType), CopyKind(CopyKind), DisposeKind(DisposeKind),
317 CopyFlags(CopyFlags), DisposeFlags(DisposeFlags) {}
318
319 /// Tell the block info that this chunk has the given field index.
320 void setIndex(CGBlockInfo &info, unsigned index, CharUnits offset) {
321 if (!Capture) {
322 info.CXXThisIndex = index;
323 info.CXXThisOffset = offset;
324 } else {
326 index, offset, FieldType, CopyKind, CopyFlags, DisposeKind,
327 DisposeFlags, Capture));
328 }
329 }
330
331 bool isTrivial() const {
332 return CopyKind == BlockCaptureEntityKind::None &&
333 DisposeKind == BlockCaptureEntityKind::None;
334 }
335 };
336
337 /// Order by 1) all __strong together 2) next, all block together 3) next,
338 /// all byref together 4) next, all __weak together. Preserve descending
339 /// alignment in all situations.
340 bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) {
341 if (left.Alignment != right.Alignment)
342 return left.Alignment > right.Alignment;
343
344 auto getPrefOrder = [](const BlockLayoutChunk &chunk) {
345 switch (chunk.CopyKind) {
347 return 0;
349 switch (chunk.CopyFlags.getBitMask()) {
351 return 0;
353 return 1;
355 return 2;
356 default:
357 break;
358 }
359 break;
361 return 3;
362 default:
363 break;
364 }
365 return 4;
366 };
367
368 return getPrefOrder(left) < getPrefOrder(right);
369 }
370} // end anonymous namespace
371
372static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
374 const LangOptions &LangOpts);
375
376static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
378 const LangOptions &LangOpts);
379
380static void addBlockLayout(CharUnits align, CharUnits size,
381 const BlockDecl::Capture *capture, llvm::Type *type,
382 QualType fieldType,
384 CGBlockInfo &Info, CodeGenModule &CGM) {
385 if (!capture) {
386 // 'this' capture.
387 Layout.push_back(BlockLayoutChunk(
388 align, size, capture, type, fieldType, BlockCaptureEntityKind::None,
390 return;
391 }
392
393 const LangOptions &LangOpts = CGM.getLangOpts();
394 BlockCaptureEntityKind CopyKind, DisposeKind;
395 BlockFieldFlags CopyFlags, DisposeFlags;
396
397 std::tie(CopyKind, CopyFlags) =
398 computeCopyInfoForBlockCapture(*capture, fieldType, LangOpts);
399 std::tie(DisposeKind, DisposeFlags) =
400 computeDestroyInfoForBlockCapture(*capture, fieldType, LangOpts);
401 Layout.push_back(BlockLayoutChunk(align, size, capture, type, fieldType,
402 CopyKind, CopyFlags, DisposeKind,
403 DisposeFlags));
404
405 if (Info.NoEscape)
406 return;
407
408 if (!Layout.back().isTrivial())
409 Info.NeedsCopyDispose = true;
410}
411
412/// Determines if the given type is safe for constant capture in C++.
414 const RecordType *recordType =
415 type->getBaseElementTypeUnsafe()->getAs<RecordType>();
416
417 // Only records can be unsafe.
418 if (!recordType) return true;
419
420 const auto *record = cast<CXXRecordDecl>(recordType->getDecl());
421
422 // Maintain semantics for classes with non-trivial dtors or copy ctors.
423 if (!record->hasTrivialDestructor()) return false;
424 if (record->hasNonTrivialCopyConstructor()) return false;
425
426 // Otherwise, we just have to make sure there aren't any mutable
427 // fields that might have changed since initialization.
428 return !record->hasMutableFields();
429}
430
431/// It is illegal to modify a const object after initialization.
432/// Therefore, if a const object has a constant initializer, we don't
433/// actually need to keep storage for it in the block; we'll just
434/// rematerialize it at the start of the block function. This is
435/// acceptable because we make no promises about address stability of
436/// captured variables.
437static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM,
438 CodeGenFunction *CGF,
439 const VarDecl *var) {
440 // Return if this is a function parameter. We shouldn't try to
441 // rematerialize default arguments of function parameters.
442 if (isa<ParmVarDecl>(var))
443 return nullptr;
444
445 QualType type = var->getType();
446
447 // We can only do this if the variable is const.
448 if (!type.isConstQualified()) return nullptr;
449
450 // Furthermore, in C++ we have to worry about mutable fields:
451 // C++ [dcl.type.cv]p4:
452 // Except that any class member declared mutable can be
453 // modified, any attempt to modify a const object during its
454 // lifetime results in undefined behavior.
455 if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type))
456 return nullptr;
457
458 // If the variable doesn't have any initializer (shouldn't this be
459 // invalid?), it's not clear what we should do. Maybe capture as
460 // zero?
461 const Expr *init = var->getInit();
462 if (!init) return nullptr;
463
464 return ConstantEmitter(CGM, CGF).tryEmitAbstractForInitializer(*var);
465}
466
467/// Get the low bit of a nonzero character count. This is the
468/// alignment of the nth byte if the 0th byte is universally aligned.
470 return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1));
471}
472
474 SmallVectorImpl<llvm::Type*> &elementTypes) {
475
476 assert(elementTypes.empty());
477 if (CGM.getLangOpts().OpenCL) {
478 // The header is basically 'struct { int; int; generic void *;
479 // custom_fields; }'. Assert that struct is packed.
480 auto GenPtrAlign = CharUnits::fromQuantity(
482 auto GenPtrSize = CharUnits::fromQuantity(
484 assert(CGM.getIntSize() <= GenPtrSize);
485 assert(CGM.getIntAlign() <= GenPtrAlign);
486 assert((2 * CGM.getIntSize()).isMultipleOf(GenPtrAlign));
487 elementTypes.push_back(CGM.IntTy); /* total size */
488 elementTypes.push_back(CGM.IntTy); /* align */
489 elementTypes.push_back(
490 CGM.getOpenCLRuntime()
491 .getGenericVoidPointerType()); /* invoke function */
492 unsigned Offset =
493 2 * CGM.getIntSize().getQuantity() + GenPtrSize.getQuantity();
494 unsigned BlockAlign = GenPtrAlign.getQuantity();
495 if (auto *Helper =
497 for (auto *I : Helper->getCustomFieldTypes()) /* custom fields */ {
498 // TargetOpenCLBlockHelp needs to make sure the struct is packed.
499 // If necessary, add padding fields to the custom fields.
500 unsigned Align = CGM.getDataLayout().getABITypeAlign(I).value();
501 if (BlockAlign < Align)
502 BlockAlign = Align;
503 assert(Offset % Align == 0);
504 Offset += CGM.getDataLayout().getTypeAllocSize(I);
505 elementTypes.push_back(I);
506 }
507 }
508 info.BlockAlign = CharUnits::fromQuantity(BlockAlign);
509 info.BlockSize = CharUnits::fromQuantity(Offset);
510 } else {
511 // The header is basically 'struct { void *; int; int; void *; void *; }'.
512 // Assert that the struct is packed.
513 assert(CGM.getIntSize() <= CGM.getPointerSize());
514 assert(CGM.getIntAlign() <= CGM.getPointerAlign());
515 assert((2 * CGM.getIntSize()).isMultipleOf(CGM.getPointerAlign()));
516 info.BlockAlign = CGM.getPointerAlign();
517 info.BlockSize = 3 * CGM.getPointerSize() + 2 * CGM.getIntSize();
518 elementTypes.push_back(CGM.VoidPtrTy);
519 elementTypes.push_back(CGM.IntTy);
520 elementTypes.push_back(CGM.IntTy);
521 elementTypes.push_back(CGM.VoidPtrTy);
522 elementTypes.push_back(CGM.getBlockDescriptorType());
523 }
524}
525
527 const BlockDecl::Capture &CI) {
528 const VarDecl *VD = CI.getVariable();
529
530 // If the variable is captured by an enclosing block or lambda expression,
531 // use the type of the capture field.
532 if (CGF.BlockInfo && CI.isNested())
533 return CGF.BlockInfo->getCapture(VD).fieldType();
534 if (auto *FD = CGF.LambdaCaptureFields.lookup(VD))
535 return FD->getType();
536 // If the captured variable is a non-escaping __block variable, the field
537 // type is the reference type. If the variable is a __block variable that
538 // already has a reference type, the field type is the variable's type.
539 return VD->isNonEscapingByref() ?
541}
542
543/// Compute the layout of the given block. Attempts to lay the block
544/// out with minimal space requirements.
546 CGBlockInfo &info) {
547 ASTContext &C = CGM.getContext();
548 const BlockDecl *block = info.getBlockDecl();
549
550 SmallVector<llvm::Type*, 8> elementTypes;
551 initializeForBlockHeader(CGM, info, elementTypes);
552 bool hasNonConstantCustomFields = false;
553 if (auto *OpenCLHelper =
555 hasNonConstantCustomFields =
556 !OpenCLHelper->areAllCustomFieldValuesConstant(info);
557 if (!block->hasCaptures() && !hasNonConstantCustomFields) {
558 info.StructureType =
559 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
560 info.CanBeGlobal = true;
561 return;
562 }
563 else if (C.getLangOpts().ObjC &&
564 CGM.getLangOpts().getGC() == LangOptions::NonGC)
565 info.HasCapturedVariableLayout = true;
566
567 if (block->doesNotEscape())
568 info.NoEscape = true;
569
570 // Collect the layout chunks.
572 layout.reserve(block->capturesCXXThis() +
573 (block->capture_end() - block->capture_begin()));
574
575 CharUnits maxFieldAlign;
576
577 // First, 'this'.
578 if (block->capturesCXXThis()) {
579 assert(CGF && CGF->CurFuncDecl && isa<CXXMethodDecl>(CGF->CurFuncDecl) &&
580 "Can't capture 'this' outside a method");
581 QualType thisType = cast<CXXMethodDecl>(CGF->CurFuncDecl)->getThisType();
582
583 // Theoretically, this could be in a different address space, so
584 // don't assume standard pointer size/align.
585 llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType);
586 auto TInfo = CGM.getContext().getTypeInfoInChars(thisType);
587 maxFieldAlign = std::max(maxFieldAlign, TInfo.Align);
588
589 addBlockLayout(TInfo.Align, TInfo.Width, nullptr, llvmType, thisType,
590 layout, info, CGM);
591 }
592
593 // Next, all the block captures.
594 for (const auto &CI : block->captures()) {
595 const VarDecl *variable = CI.getVariable();
596
597 if (CI.isEscapingByref()) {
598 // Just use void* instead of a pointer to the byref type.
599 CharUnits align = CGM.getPointerAlign();
600 maxFieldAlign = std::max(maxFieldAlign, align);
601
602 // Since a __block variable cannot be captured by lambdas, its type and
603 // the capture field type should always match.
604 assert(CGF && getCaptureFieldType(*CGF, CI) == variable->getType() &&
605 "capture type differs from the variable type");
606 addBlockLayout(align, CGM.getPointerSize(), &CI, CGM.VoidPtrTy,
607 variable->getType(), layout, info, CGM);
608 continue;
609 }
610
611 // Otherwise, build a layout chunk with the size and alignment of
612 // the declaration.
613 if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) {
614 info.SortedCaptures.push_back(
616 continue;
617 }
618
619 QualType VT = getCaptureFieldType(*CGF, CI);
620
621 if (CGM.getLangOpts().CPlusPlus)
622 if (const CXXRecordDecl *record = VT->getAsCXXRecordDecl())
623 if (CI.hasCopyExpr() || !record->hasTrivialDestructor()) {
624 info.HasCXXObject = true;
625 if (!record->isExternallyVisible())
626 info.CapturesNonExternalType = true;
627 }
628
629 CharUnits size = C.getTypeSizeInChars(VT);
630 CharUnits align = C.getDeclAlign(variable);
631
632 maxFieldAlign = std::max(maxFieldAlign, align);
633
634 llvm::Type *llvmType =
635 CGM.getTypes().ConvertTypeForMem(VT);
636
637 addBlockLayout(align, size, &CI, llvmType, VT, layout, info, CGM);
638 }
639
640 // If that was everything, we're done here.
641 if (layout.empty()) {
642 info.StructureType =
643 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
644 info.CanBeGlobal = true;
645 info.buildCaptureMap();
646 return;
647 }
648
649 // Sort the layout by alignment. We have to use a stable sort here
650 // to get reproducible results. There should probably be an
651 // llvm::array_pod_stable_sort.
652 llvm::stable_sort(layout);
653
654 // Needed for blocks layout info.
657
658 CharUnits &blockSize = info.BlockSize;
659 info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign);
660
661 // Assuming that the first byte in the header is maximally aligned,
662 // get the alignment of the first byte following the header.
663 CharUnits endAlign = getLowBit(blockSize);
664
665 // If the end of the header isn't satisfactorily aligned for the
666 // maximum thing, look for things that are okay with the header-end
667 // alignment, and keep appending them until we get something that's
668 // aligned right. This algorithm is only guaranteed optimal if
669 // that condition is satisfied at some point; otherwise we can get
670 // things like:
671 // header // next byte has alignment 4
672 // something_with_size_5; // next byte has alignment 1
673 // something_with_alignment_8;
674 // which has 7 bytes of padding, as opposed to the naive solution
675 // which might have less (?).
676 if (endAlign < maxFieldAlign) {
678 li = layout.begin() + 1, le = layout.end();
679
680 // Look for something that the header end is already
681 // satisfactorily aligned for.
682 for (; li != le && endAlign < li->Alignment; ++li)
683 ;
684
685 // If we found something that's naturally aligned for the end of
686 // the header, keep adding things...
687 if (li != le) {
689 for (; li != le; ++li) {
690 assert(endAlign >= li->Alignment);
691
692 li->setIndex(info, elementTypes.size(), blockSize);
693 elementTypes.push_back(li->Type);
694 blockSize += li->Size;
695 endAlign = getLowBit(blockSize);
696
697 // ...until we get to the alignment of the maximum field.
698 if (endAlign >= maxFieldAlign) {
699 ++li;
700 break;
701 }
702 }
703 // Don't re-append everything we just appended.
704 layout.erase(first, li);
705 }
706 }
707
708 assert(endAlign == getLowBit(blockSize));
709
710 // At this point, we just have to add padding if the end align still
711 // isn't aligned right.
712 if (endAlign < maxFieldAlign) {
713 CharUnits newBlockSize = blockSize.alignTo(maxFieldAlign);
714 CharUnits padding = newBlockSize - blockSize;
715
716 // If we haven't yet added any fields, remember that there was an
717 // initial gap; this need to go into the block layout bit map.
718 if (blockSize == info.BlockHeaderForcedGapOffset) {
719 info.BlockHeaderForcedGapSize = padding;
720 }
721
722 elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
723 padding.getQuantity()));
724 blockSize = newBlockSize;
725 endAlign = getLowBit(blockSize); // might be > maxFieldAlign
726 }
727
728 assert(endAlign >= maxFieldAlign);
729 assert(endAlign == getLowBit(blockSize));
730 // Slam everything else on now. This works because they have
731 // strictly decreasing alignment and we expect that size is always a
732 // multiple of alignment.
734 li = layout.begin(), le = layout.end(); li != le; ++li) {
735 if (endAlign < li->Alignment) {
736 // size may not be multiple of alignment. This can only happen with
737 // an over-aligned variable. We will be adding a padding field to
738 // make the size be multiple of alignment.
739 CharUnits padding = li->Alignment - endAlign;
740 elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
741 padding.getQuantity()));
742 blockSize += padding;
743 endAlign = getLowBit(blockSize);
744 }
745 assert(endAlign >= li->Alignment);
746 li->setIndex(info, elementTypes.size(), blockSize);
747 elementTypes.push_back(li->Type);
748 blockSize += li->Size;
749 endAlign = getLowBit(blockSize);
750 }
751
752 info.buildCaptureMap();
753 info.StructureType =
754 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
755}
756
757/// Emit a block literal expression in the current function.
759 // If the block has no captures, we won't have a pre-computed
760 // layout for it.
761 if (!blockExpr->getBlockDecl()->hasCaptures())
762 // The block literal is emitted as a global variable, and the block invoke
763 // function has to be extracted from its initializer.
764 if (llvm::Constant *Block = CGM.getAddrOfGlobalBlockIfEmitted(blockExpr))
765 return Block;
766
767 CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName());
768 computeBlockInfo(CGM, this, blockInfo);
769 blockInfo.BlockExpression = blockExpr;
770 if (!blockInfo.CanBeGlobal)
771 blockInfo.LocalAddress = CreateTempAlloca(blockInfo.StructureType,
772 blockInfo.BlockAlign, "block");
773 return EmitBlockLiteral(blockInfo);
774}
775
776llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
777 bool IsOpenCL = CGM.getContext().getLangOpts().OpenCL;
778 auto GenVoidPtrTy =
780 LangAS GenVoidPtrAddr = IsOpenCL ? LangAS::opencl_generic : LangAS::Default;
781 auto GenVoidPtrSize = CharUnits::fromQuantity(
782 CGM.getTarget().getPointerWidth(GenVoidPtrAddr) / 8);
783 // Using the computed layout, generate the actual block function.
784 bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda();
785 CodeGenFunction BlockCGF{CGM, true};
786 BlockCGF.SanOpts = SanOpts;
787 auto *InvokeFn = BlockCGF.GenerateBlockFunction(
788 CurGD, blockInfo, LocalDeclMap, isLambdaConv, blockInfo.CanBeGlobal);
789 auto *blockFn = llvm::ConstantExpr::getPointerCast(InvokeFn, GenVoidPtrTy);
790
791 // If there is nothing to capture, we can emit this as a global block.
792 if (blockInfo.CanBeGlobal)
794
795 // Otherwise, we have to emit this as a local block.
796
797 Address blockAddr = blockInfo.LocalAddress;
798 assert(blockAddr.isValid() && "block has no address!");
799
800 llvm::Constant *isa;
801 llvm::Constant *descriptor;
802 BlockFlags flags;
803 if (!IsOpenCL) {
804 // If the block is non-escaping, set field 'isa 'to NSConcreteGlobalBlock
805 // and set the BLOCK_IS_GLOBAL bit of field 'flags'. Copying a non-escaping
806 // block just returns the original block and releasing it is a no-op.
807 llvm::Constant *blockISA = blockInfo.NoEscape
810 isa = blockISA;
811
812 // Build the block descriptor.
813 descriptor = buildBlockDescriptor(CGM, blockInfo);
814
815 // Compute the initial on-stack block flags.
816 flags = BLOCK_HAS_SIGNATURE;
817 if (blockInfo.HasCapturedVariableLayout)
819 if (blockInfo.NeedsCopyDispose)
820 flags |= BLOCK_HAS_COPY_DISPOSE;
821 if (blockInfo.HasCXXObject)
822 flags |= BLOCK_HAS_CXX_OBJ;
823 if (blockInfo.UsesStret)
824 flags |= BLOCK_USE_STRET;
825 if (blockInfo.NoEscape)
827 }
828
829 auto projectField = [&](unsigned index, const Twine &name) -> Address {
830 return Builder.CreateStructGEP(blockAddr, index, name);
831 };
832 auto storeField = [&](llvm::Value *value, unsigned index, const Twine &name) {
833 Builder.CreateStore(value, projectField(index, name));
834 };
835
836 // Initialize the block header.
837 {
838 // We assume all the header fields are densely packed.
839 unsigned index = 0;
840 CharUnits offset;
841 auto addHeaderField = [&](llvm::Value *value, CharUnits size,
842 const Twine &name) {
843 storeField(value, index, name);
844 offset += size;
845 index++;
846 };
847
848 if (!IsOpenCL) {
849 addHeaderField(isa, getPointerSize(), "block.isa");
850 addHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
851 getIntSize(), "block.flags");
852 addHeaderField(llvm::ConstantInt::get(IntTy, 0), getIntSize(),
853 "block.reserved");
854 } else {
855 addHeaderField(
856 llvm::ConstantInt::get(IntTy, blockInfo.BlockSize.getQuantity()),
857 getIntSize(), "block.size");
858 addHeaderField(
859 llvm::ConstantInt::get(IntTy, blockInfo.BlockAlign.getQuantity()),
860 getIntSize(), "block.align");
861 }
862 addHeaderField(blockFn, GenVoidPtrSize, "block.invoke");
863 if (!IsOpenCL)
864 addHeaderField(descriptor, getPointerSize(), "block.descriptor");
865 else if (auto *Helper =
867 for (auto I : Helper->getCustomFieldValues(*this, blockInfo)) {
868 addHeaderField(
869 I.first,
871 CGM.getDataLayout().getTypeAllocSize(I.first->getType())),
872 I.second);
873 }
874 }
875 }
876
877 // Finally, capture all the values into the block.
878 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
879
880 // First, 'this'.
881 if (blockDecl->capturesCXXThis()) {
882 Address addr =
883 projectField(blockInfo.CXXThisIndex, "block.captured-this.addr");
885 }
886
887 // Next, captured variables.
888 for (const auto &CI : blockDecl->captures()) {
889 const VarDecl *variable = CI.getVariable();
890 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
891
892 // Ignore constant captures.
893 if (capture.isConstant()) continue;
894
895 QualType type = capture.fieldType();
896
897 // This will be a [[type]]*, except that a byref entry will just be
898 // an i8**.
899 Address blockField = projectField(capture.getIndex(), "block.captured");
900
901 // Compute the address of the thing we're going to move into the
902 // block literal.
904
905 if (blockDecl->isConversionFromLambda()) {
906 // The lambda capture in a lambda's conversion-to-block-pointer is
907 // special; we'll simply emit it directly.
908 src = Address::invalid();
909 } else if (CI.isEscapingByref()) {
910 if (BlockInfo && CI.isNested()) {
911 // We need to use the capture from the enclosing block.
912 const CGBlockInfo::Capture &enclosingCapture =
913 BlockInfo->getCapture(variable);
914
915 // This is a [[type]]*, except that a byref entry will just be an i8**.
917 enclosingCapture.getIndex(),
918 "block.capture.addr");
919 } else {
920 auto I = LocalDeclMap.find(variable);
921 assert(I != LocalDeclMap.end());
922 src = I->second;
923 }
924 } else {
925 DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable),
926 /*RefersToEnclosingVariableOrCapture*/ CI.isNested(),
927 type.getNonReferenceType(), VK_LValue,
929 src = EmitDeclRefLValue(&declRef).getAddress(*this);
930 };
931
932 // For byrefs, we just write the pointer to the byref struct into
933 // the block field. There's no need to chase the forwarding
934 // pointer at this point, since we're building something that will
935 // live a shorter life than the stack byref anyway.
936 if (CI.isEscapingByref()) {
937 // Get a void* that points to the byref struct.
938 llvm::Value *byrefPointer;
939 if (CI.isNested())
940 byrefPointer = Builder.CreateLoad(src, "byref.capture");
941 else
942 byrefPointer = src.getPointer();
943
944 // Write that void* into the capture field.
945 Builder.CreateStore(byrefPointer, blockField);
946
947 // If we have a copy constructor, evaluate that into the block field.
948 } else if (const Expr *copyExpr = CI.getCopyExpr()) {
949 if (blockDecl->isConversionFromLambda()) {
950 // If we have a lambda conversion, emit the expression
951 // directly into the block instead.
952 AggValueSlot Slot =
953 AggValueSlot::forAddr(blockField, Qualifiers(),
958 EmitAggExpr(copyExpr, Slot);
959 } else {
960 EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
961 }
962
963 // If it's a reference variable, copy the reference into the block field.
964 } else if (type->isReferenceType()) {
965 Builder.CreateStore(src.getPointer(), blockField);
966
967 // If type is const-qualified, copy the value into the block field.
968 } else if (type.isConstQualified() &&
969 type.getObjCLifetime() == Qualifiers::OCL_Strong &&
970 CGM.getCodeGenOpts().OptimizationLevel != 0) {
971 llvm::Value *value = Builder.CreateLoad(src, "captured");
972 Builder.CreateStore(value, blockField);
973
974 // If this is an ARC __strong block-pointer variable, don't do a
975 // block copy.
976 //
977 // TODO: this can be generalized into the normal initialization logic:
978 // we should never need to do a block-copy when initializing a local
979 // variable, because the local variable's lifetime should be strictly
980 // contained within the stack block's.
981 } else if (type.getObjCLifetime() == Qualifiers::OCL_Strong &&
982 type->isBlockPointerType()) {
983 // Load the block and do a simple retain.
984 llvm::Value *value = Builder.CreateLoad(src, "block.captured_block");
985 value = EmitARCRetainNonBlock(value);
986
987 // Do a primitive store to the block field.
988 Builder.CreateStore(value, blockField);
989
990 // Otherwise, fake up a POD copy into the block field.
991 } else {
992 // Fake up a new variable so that EmitScalarInit doesn't think
993 // we're referring to the variable in its own initializer.
994 ImplicitParamDecl BlockFieldPseudoVar(getContext(), type,
996
997 // We use one of these or the other depending on whether the
998 // reference is nested.
999 DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable),
1000 /*RefersToEnclosingVariableOrCapture*/ CI.isNested(),
1002
1003 ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
1004 &declRef, VK_PRValue, FPOptionsOverride());
1005 // FIXME: Pass a specific location for the expr init so that the store is
1006 // attributed to a reasonable location - otherwise it may be attributed to
1007 // locations of subexpressions in the initialization.
1008 EmitExprAsInit(&l2r, &BlockFieldPseudoVar,
1010 /*captured by init*/ false);
1011 }
1012
1013 // Push a cleanup for the capture if necessary.
1014 if (!blockInfo.NoEscape && !blockInfo.NeedsCopyDispose)
1015 continue;
1016
1017 // Ignore __block captures; there's nothing special in the on-stack block
1018 // that we need to do for them.
1019 if (CI.isByRef())
1020 continue;
1021
1022 // Ignore objects that aren't destructed.
1023 QualType::DestructionKind dtorKind = type.isDestructedType();
1024 if (dtorKind == QualType::DK_none)
1025 continue;
1026
1027 CodeGenFunction::Destroyer *destroyer;
1028
1029 // Block captures count as local values and have imprecise semantics.
1030 // They also can't be arrays, so need to worry about that.
1031 //
1032 // For const-qualified captures, emit clang.arc.use to ensure the captured
1033 // object doesn't get released while we are still depending on its validity
1034 // within the block.
1035 if (type.isConstQualified() &&
1036 type.getObjCLifetime() == Qualifiers::OCL_Strong &&
1037 CGM.getCodeGenOpts().OptimizationLevel != 0) {
1038 assert(CGM.getLangOpts().ObjCAutoRefCount &&
1039 "expected ObjC ARC to be enabled");
1040 destroyer = emitARCIntrinsicUse;
1041 } else if (dtorKind == QualType::DK_objc_strong_lifetime) {
1042 destroyer = destroyARCStrongImprecise;
1043 } else {
1044 destroyer = getDestroyer(dtorKind);
1045 }
1046
1047 CleanupKind cleanupKind = NormalCleanup;
1048 bool useArrayEHCleanup = needsEHCleanup(dtorKind);
1049 if (useArrayEHCleanup)
1050 cleanupKind = NormalAndEHCleanup;
1051
1052 // Extend the lifetime of the capture to the end of the scope enclosing the
1053 // block expression except when the block decl is in the list of RetExpr's
1054 // cleanup objects, in which case its lifetime ends after the full
1055 // expression.
1056 auto IsBlockDeclInRetExpr = [&]() {
1057 auto *EWC = llvm::dyn_cast_or_null<ExprWithCleanups>(RetExpr);
1058 if (EWC)
1059 for (auto &C : EWC->getObjects())
1060 if (auto *BD = C.dyn_cast<BlockDecl *>())
1061 if (BD == blockDecl)
1062 return true;
1063 return false;
1064 };
1065
1066 if (IsBlockDeclInRetExpr())
1067 pushDestroy(cleanupKind, blockField, type, destroyer, useArrayEHCleanup);
1068 else
1069 pushLifetimeExtendedDestroy(cleanupKind, blockField, type, destroyer,
1070 useArrayEHCleanup);
1071 }
1072
1073 // Cast to the converted block-pointer type, which happens (somewhat
1074 // unfortunately) to be a pointer to function type.
1075 llvm::Value *result = Builder.CreatePointerCast(
1076 blockAddr.getPointer(), ConvertType(blockInfo.getBlockExpr()->getType()));
1077
1078 if (IsOpenCL) {
1080 result, blockInfo.StructureType);
1081 }
1082
1083 return result;
1084}
1085
1086
1088 if (BlockDescriptorType)
1089 return BlockDescriptorType;
1090
1091 llvm::Type *UnsignedLongTy =
1092 getTypes().ConvertType(getContext().UnsignedLongTy);
1093
1094 // struct __block_descriptor {
1095 // unsigned long reserved;
1096 // unsigned long block_size;
1097 //
1098 // // later, the following will be added
1099 //
1100 // struct {
1101 // void (*copyHelper)();
1102 // void (*copyHelper)();
1103 // } helpers; // !!! optional
1104 //
1105 // const char *signature; // the block signature
1106 // const char *layout; // reserved
1107 // };
1108 BlockDescriptorType = llvm::StructType::create(
1109 "struct.__block_descriptor", UnsignedLongTy, UnsignedLongTy);
1110
1111 // Now form a pointer to that.
1112 unsigned AddrSpace = 0;
1113 if (getLangOpts().OpenCL)
1115 BlockDescriptorType = llvm::PointerType::get(BlockDescriptorType, AddrSpace);
1116 return BlockDescriptorType;
1117}
1118
1120 if (GenericBlockLiteralType)
1121 return GenericBlockLiteralType;
1122
1123 llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
1124
1125 if (getLangOpts().OpenCL) {
1126 // struct __opencl_block_literal_generic {
1127 // int __size;
1128 // int __align;
1129 // __generic void *__invoke;
1130 // /* custom fields */
1131 // };
1132 SmallVector<llvm::Type *, 8> StructFields(
1134 if (auto *Helper = getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
1135 llvm::append_range(StructFields, Helper->getCustomFieldTypes());
1136 }
1137 GenericBlockLiteralType = llvm::StructType::create(
1138 StructFields, "struct.__opencl_block_literal_generic");
1139 } else {
1140 // struct __block_literal_generic {
1141 // void *__isa;
1142 // int __flags;
1143 // int __reserved;
1144 // void (*__invoke)(void *);
1145 // struct __block_descriptor *__descriptor;
1146 // };
1147 GenericBlockLiteralType =
1148 llvm::StructType::create("struct.__block_literal_generic", VoidPtrTy,
1149 IntTy, IntTy, VoidPtrTy, BlockDescPtrTy);
1150 }
1151
1152 return GenericBlockLiteralType;
1153}
1154
1156 ReturnValueSlot ReturnValue) {
1157 const auto *BPT = E->getCallee()->getType()->castAs<BlockPointerType>();
1158 llvm::Value *BlockPtr = EmitScalarExpr(E->getCallee());
1159 llvm::Type *GenBlockTy = CGM.getGenericBlockLiteralType();
1160 llvm::Value *Func = nullptr;
1161 QualType FnType = BPT->getPointeeType();
1162 ASTContext &Ctx = getContext();
1163 CallArgList Args;
1164
1165 if (getLangOpts().OpenCL) {
1166 // For OpenCL, BlockPtr is already casted to generic block literal.
1167
1168 // First argument of a block call is a generic block literal casted to
1169 // generic void pointer, i.e. i8 addrspace(4)*
1170 llvm::Type *GenericVoidPtrTy =
1172 llvm::Value *BlockDescriptor = Builder.CreatePointerCast(
1173 BlockPtr, GenericVoidPtrTy);
1174 QualType VoidPtrQualTy = Ctx.getPointerType(
1176 Args.add(RValue::get(BlockDescriptor), VoidPtrQualTy);
1177 // And the rest of the arguments.
1178 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments());
1179
1180 // We *can* call the block directly unless it is a function argument.
1181 if (!isa<ParmVarDecl>(E->getCalleeDecl()))
1183 else {
1184 llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 2);
1185 Func = Builder.CreateAlignedLoad(GenericVoidPtrTy, FuncPtr,
1186 getPointerAlign());
1187 }
1188 } else {
1189 // Bitcast the block literal to a generic block literal.
1190 BlockPtr =
1191 Builder.CreatePointerCast(BlockPtr, UnqualPtrTy, "block.literal");
1192 // Get pointer to the block invoke function
1193 llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 3);
1194
1195 // First argument is a block literal casted to a void pointer
1196 BlockPtr = Builder.CreatePointerCast(BlockPtr, VoidPtrTy);
1197 Args.add(RValue::get(BlockPtr), Ctx.VoidPtrTy);
1198 // And the rest of the arguments.
1199 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments());
1200
1201 // Load the function.
1203 }
1204
1205 const FunctionType *FuncTy = FnType->castAs<FunctionType>();
1206 const CGFunctionInfo &FnInfo =
1207 CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy);
1208
1209 // Prepare the callee.
1211
1212 // And call the block.
1213 return EmitCall(FnInfo, Callee, ReturnValue, Args);
1214}
1215
1217 assert(BlockInfo && "evaluating block ref without block information?");
1218 const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
1219
1220 // Handle constant captures.
1221 if (capture.isConstant()) return LocalDeclMap.find(variable)->second;
1222
1224 "block.capture.addr");
1225
1226 if (variable->isEscapingByref()) {
1227 // addr should be a void** right now. Load, then cast the result
1228 // to byref*.
1229
1230 auto &byrefInfo = getBlockByrefInfo(variable);
1231 addr = Address(Builder.CreateLoad(addr), byrefInfo.Type,
1232 byrefInfo.ByrefAlignment);
1233
1234 addr = emitBlockByrefAddress(addr, byrefInfo, /*follow*/ true,
1235 variable->getName());
1236 }
1237
1238 assert((!variable->isNonEscapingByref() ||
1239 capture.fieldType()->isReferenceType()) &&
1240 "the capture field of a non-escaping variable should have a "
1241 "reference type");
1242 if (capture.fieldType()->isReferenceType())
1243 addr = EmitLoadOfReference(MakeAddrLValue(addr, capture.fieldType()));
1244
1245 return addr;
1246}
1247
1249 llvm::Constant *Addr) {
1250 bool Ok = EmittedGlobalBlocks.insert(std::make_pair(BE, Addr)).second;
1251 (void)Ok;
1252 assert(Ok && "Trying to replace an already-existing global block!");
1253}
1254
1255llvm::Constant *
1257 StringRef Name) {
1258 if (llvm::Constant *Block = getAddrOfGlobalBlockIfEmitted(BE))
1259 return Block;
1260
1261 CGBlockInfo blockInfo(BE->getBlockDecl(), Name);
1262 blockInfo.BlockExpression = BE;
1263
1264 // Compute information about the layout, etc., of this block.
1265 computeBlockInfo(*this, nullptr, blockInfo);
1266
1267 // Using that metadata, generate the actual block function.
1268 {
1269 CodeGenFunction::DeclMapTy LocalDeclMap;
1271 GlobalDecl(), blockInfo, LocalDeclMap,
1272 /*IsLambdaConversionToBlock*/ false, /*BuildGlobalBlock*/ true);
1273 }
1274
1276}
1277
1278static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
1279 const CGBlockInfo &blockInfo,
1280 llvm::Constant *blockFn) {
1281 assert(blockInfo.CanBeGlobal);
1282 // Callers should detect this case on their own: calling this function
1283 // generally requires computing layout information, which is a waste of time
1284 // if we've already emitted this block.
1285 assert(!CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression) &&
1286 "Refusing to re-emit a global block.");
1287
1288 // Generate the constants for the block literal initializer.
1289 ConstantInitBuilder builder(CGM);
1290 auto fields = builder.beginStruct();
1291
1292 bool IsOpenCL = CGM.getLangOpts().OpenCL;
1293 bool IsWindows = CGM.getTarget().getTriple().isOSWindows();
1294 if (!IsOpenCL) {
1295 // isa
1296 if (IsWindows)
1297 fields.addNullPointer(CGM.Int8PtrPtrTy);
1298 else
1299 fields.add(CGM.getNSConcreteGlobalBlock());
1300
1301 // __flags
1303 if (blockInfo.UsesStret)
1304 flags |= BLOCK_USE_STRET;
1305
1306 fields.addInt(CGM.IntTy, flags.getBitMask());
1307
1308 // Reserved
1309 fields.addInt(CGM.IntTy, 0);
1310 } else {
1311 fields.addInt(CGM.IntTy, blockInfo.BlockSize.getQuantity());
1312 fields.addInt(CGM.IntTy, blockInfo.BlockAlign.getQuantity());
1313 }
1314
1315 // Function
1316 fields.add(blockFn);
1317
1318 if (!IsOpenCL) {
1319 // Descriptor
1320 fields.add(buildBlockDescriptor(CGM, blockInfo));
1321 } else if (auto *Helper =
1323 for (auto *I : Helper->getCustomFieldValues(CGM, blockInfo)) {
1324 fields.add(I);
1325 }
1326 }
1327
1328 unsigned AddrSpace = 0;
1329 if (CGM.getContext().getLangOpts().OpenCL)
1331
1332 llvm::GlobalVariable *literal = fields.finishAndCreateGlobal(
1333 "__block_literal_global", blockInfo.BlockAlign,
1334 /*constant*/ !IsWindows, llvm::GlobalVariable::InternalLinkage, AddrSpace);
1335
1336 literal->addAttribute("objc_arc_inert");
1337
1338 // Windows does not allow globals to be initialised to point to globals in
1339 // different DLLs. Any such variables must run code to initialise them.
1340 if (IsWindows) {
1341 auto *Init = llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy,
1342 {}), llvm::GlobalValue::InternalLinkage, ".block_isa_init",
1343 &CGM.getModule());
1344 llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry",
1345 Init));
1346 b.CreateAlignedStore(CGM.getNSConcreteGlobalBlock(),
1347 b.CreateStructGEP(literal->getValueType(), literal, 0),
1348 CGM.getPointerAlign().getAsAlign());
1349 b.CreateRetVoid();
1350 // We can't use the normal LLVM global initialisation array, because we
1351 // need to specify that this runs early in library initialisation.
1352 auto *InitVar = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
1353 /*isConstant*/true, llvm::GlobalValue::InternalLinkage,
1354 Init, ".block_isa_init_ptr");
1355 InitVar->setSection(".CRT$XCLa");
1356 CGM.addUsedGlobal(InitVar);
1357 }
1358
1359 // Return a constant of the appropriately-casted type.
1360 llvm::Type *RequiredType =
1361 CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
1362 llvm::Constant *Result =
1363 llvm::ConstantExpr::getPointerCast(literal, RequiredType);
1365 if (CGM.getContext().getLangOpts().OpenCL)
1367 blockInfo.BlockExpression,
1368 cast<llvm::Function>(blockFn->stripPointerCasts()), Result,
1369 literal->getValueType());
1370 return Result;
1371}
1372
1374 unsigned argNum,
1375 llvm::Value *arg) {
1376 assert(BlockInfo && "not emitting prologue of block invocation function?!");
1377
1378 // Allocate a stack slot like for any local variable to guarantee optimal
1379 // debug info at -O0. The mem2reg pass will eliminate it when optimizing.
1380 Address alloc = CreateMemTemp(D->getType(), D->getName() + ".addr");
1381 Builder.CreateStore(arg, alloc);
1382 if (CGDebugInfo *DI = getDebugInfo()) {
1384 DI->setLocation(D->getLocation());
1385 DI->EmitDeclareOfBlockLiteralArgVariable(
1386 *BlockInfo, D->getName(), argNum,
1387 cast<llvm::AllocaInst>(alloc.getPointer()), Builder);
1388 }
1389 }
1390
1392 ApplyDebugLocation Scope(*this, StartLoc);
1393
1394 // Instead of messing around with LocalDeclMap, just set the value
1395 // directly as BlockPointer.
1396 BlockPointer = Builder.CreatePointerCast(
1397 arg,
1398 llvm::PointerType::get(
1401 ? getContext().getTargetAddressSpace(LangAS::opencl_generic)
1402 : 0),
1403 "block");
1404}
1405
1407 assert(BlockInfo && "not in a block invocation function!");
1408 assert(BlockPointer && "no block pointer set!");
1410}
1411
1413 GlobalDecl GD, const CGBlockInfo &blockInfo, const DeclMapTy &ldm,
1414 bool IsLambdaConversionToBlock, bool BuildGlobalBlock) {
1415 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1416
1417 CurGD = GD;
1418
1419 CurEHLocation = blockInfo.getBlockExpr()->getEndLoc();
1420
1421 BlockInfo = &blockInfo;
1422
1423 // Arrange for local static and local extern declarations to appear
1424 // to be local to this function as well, in case they're directly
1425 // referenced in a block.
1426 for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
1427 const auto *var = dyn_cast<VarDecl>(i->first);
1428 if (var && !var->hasLocalStorage())
1429 setAddrOfLocalVar(var, i->second);
1430 }
1431
1432 // Begin building the function declaration.
1433
1434 // Build the argument list.
1435 FunctionArgList args;
1436
1437 // The first argument is the block pointer. Just take it as a void*
1438 // and cast it later.
1439 QualType selfTy = getContext().VoidPtrTy;
1440
1441 // For OpenCL passed block pointer can be private AS local variable or
1442 // global AS program scope variable (for the case with and without captures).
1443 // Generic AS is used therefore to be able to accommodate both private and
1444 // generic AS in one implementation.
1445 if (getLangOpts().OpenCL)
1446 selfTy = getContext().getPointerType(getContext().getAddrSpaceQualType(
1448
1449 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
1450
1451 ImplicitParamDecl SelfDecl(getContext(), const_cast<BlockDecl *>(blockDecl),
1452 SourceLocation(), II, selfTy,
1454 args.push_back(&SelfDecl);
1455
1456 // Now add the rest of the parameters.
1457 args.append(blockDecl->param_begin(), blockDecl->param_end());
1458
1459 // Create the function declaration.
1460 const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType();
1461 const CGFunctionInfo &fnInfo =
1464 blockInfo.UsesStret = true;
1465
1466 llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo);
1467
1468 StringRef name = CGM.getBlockMangledName(GD, blockDecl);
1469 llvm::Function *fn = llvm::Function::Create(
1470 fnLLVMType, llvm::GlobalValue::InternalLinkage, name, &CGM.getModule());
1472
1473 if (BuildGlobalBlock) {
1474 auto GenVoidPtrTy = getContext().getLangOpts().OpenCL
1476 : VoidPtrTy;
1477 buildGlobalBlock(CGM, blockInfo,
1478 llvm::ConstantExpr::getPointerCast(fn, GenVoidPtrTy));
1479 }
1480
1481 // Begin generating the function.
1482 StartFunction(blockDecl, fnType->getReturnType(), fn, fnInfo, args,
1483 blockDecl->getLocation(),
1484 blockInfo.getBlockExpr()->getBody()->getBeginLoc());
1485
1486 // Okay. Undo some of what StartFunction did.
1487
1488 // At -O0 we generate an explicit alloca for the BlockPointer, so the RA
1489 // won't delete the dbg.declare intrinsics for captured variables.
1490 llvm::Value *BlockPointerDbgLoc = BlockPointer;
1491 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1492 // Allocate a stack slot for it, so we can point the debugger to it
1493 Address Alloca = CreateTempAlloca(BlockPointer->getType(),
1495 "block.addr");
1496 // Set the DebugLocation to empty, so the store is recognized as a
1497 // frame setup instruction by llvm::DwarfDebug::beginFunction().
1498 auto NL = ApplyDebugLocation::CreateEmpty(*this);
1500 BlockPointerDbgLoc = Alloca.getPointer();
1501 }
1502
1503 // If we have a C++ 'this' reference, go ahead and force it into
1504 // existence now.
1505 if (blockDecl->capturesCXXThis()) {
1507 LoadBlockStruct(), blockInfo.CXXThisIndex, "block.captured-this");
1508 CXXThisValue = Builder.CreateLoad(addr, "this");
1509 }
1510
1511 // Also force all the constant captures.
1512 for (const auto &CI : blockDecl->captures()) {
1513 const VarDecl *variable = CI.getVariable();
1514 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1515 if (!capture.isConstant()) continue;
1516
1517 CharUnits align = getContext().getDeclAlign(variable);
1518 Address alloca =
1519 CreateMemTemp(variable->getType(), align, "block.captured-const");
1520
1521 Builder.CreateStore(capture.getConstant(), alloca);
1522
1523 setAddrOfLocalVar(variable, alloca);
1524 }
1525
1526 // Save a spot to insert the debug information for all the DeclRefExprs.
1527 llvm::BasicBlock *entry = Builder.GetInsertBlock();
1528 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
1529 --entry_ptr;
1530
1531 if (IsLambdaConversionToBlock)
1533 else {
1536 EmitStmt(blockDecl->getBody());
1537 }
1538
1539 // Remember where we were...
1540 llvm::BasicBlock *resume = Builder.GetInsertBlock();
1541
1542 // Go back to the entry.
1543 ++entry_ptr;
1544 Builder.SetInsertPoint(entry, entry_ptr);
1545
1546 // Emit debug information for all the DeclRefExprs.
1547 // FIXME: also for 'this'
1548 if (CGDebugInfo *DI = getDebugInfo()) {
1549 for (const auto &CI : blockDecl->captures()) {
1550 const VarDecl *variable = CI.getVariable();
1551 DI->EmitLocation(Builder, variable->getLocation());
1552
1554 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1555 if (capture.isConstant()) {
1556 auto addr = LocalDeclMap.find(variable)->second;
1557 (void)DI->EmitDeclareOfAutoVariable(variable, addr.getPointer(),
1558 Builder);
1559 continue;
1560 }
1561
1562 DI->EmitDeclareOfBlockDeclRefVariable(
1563 variable, BlockPointerDbgLoc, Builder, blockInfo,
1564 entry_ptr == entry->end() ? nullptr : &*entry_ptr);
1565 }
1566 }
1567 // Recover location if it was changed in the above loop.
1568 DI->EmitLocation(Builder,
1569 cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
1570 }
1571
1572 // And resume where we left off.
1573 if (resume == nullptr)
1574 Builder.ClearInsertionPoint();
1575 else
1576 Builder.SetInsertPoint(resume);
1577
1578 FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
1579
1580 return fn;
1581}
1582
1583static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
1585 const LangOptions &LangOpts) {
1586 if (CI.getCopyExpr()) {
1587 assert(!CI.isByRef());
1588 // don't bother computing flags
1589 return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags());
1590 }
1591 BlockFieldFlags Flags;
1592 if (CI.isEscapingByref()) {
1593 Flags = BLOCK_FIELD_IS_BYREF;
1594 if (T.isObjCGCWeak())
1595 Flags |= BLOCK_FIELD_IS_WEAK;
1596 return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
1597 }
1598
1599 Flags = BLOCK_FIELD_IS_OBJECT;
1601 if (isBlockPointer)
1602 Flags = BLOCK_FIELD_IS_BLOCK;
1603
1604 switch (T.isNonTrivialToPrimitiveCopy()) {
1606 return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct,
1607 BlockFieldFlags());
1609 // We need to register __weak direct captures with the runtime.
1610 return std::make_pair(BlockCaptureEntityKind::ARCWeak, Flags);
1612 // We need to retain the copied value for __strong direct captures.
1613 // If it's a block pointer, we have to copy the block and assign that to
1614 // the destination pointer, so we might as well use _Block_object_assign.
1615 // Otherwise we can avoid that.
1616 return std::make_pair(!isBlockPointer ? BlockCaptureEntityKind::ARCStrong
1618 Flags);
1621 if (!T->isObjCRetainableType())
1622 // For all other types, the memcpy is fine.
1623 return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
1624
1625 // Honor the inert __unsafe_unretained qualifier, which doesn't actually
1626 // make it into the type system.
1628 return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
1629
1630 // Special rules for ARC captures:
1631 Qualifiers QS = T.getQualifiers();
1632
1633 // Non-ARC captures of retainable pointers are strong and
1634 // therefore require a call to _Block_object_assign.
1635 if (!QS.getObjCLifetime() && !LangOpts.ObjCAutoRefCount)
1636 return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
1637
1638 // Otherwise the memcpy is fine.
1639 return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
1640 }
1641 }
1642 llvm_unreachable("after exhaustive PrimitiveCopyKind switch");
1643}
1644
1645namespace {
1646/// Release a __block variable.
1647struct CallBlockRelease final : EHScopeStack::Cleanup {
1648 Address Addr;
1649 BlockFieldFlags FieldFlags;
1650 bool LoadBlockVarAddr, CanThrow;
1651
1652 CallBlockRelease(Address Addr, BlockFieldFlags Flags, bool LoadValue,
1653 bool CT)
1654 : Addr(Addr), FieldFlags(Flags), LoadBlockVarAddr(LoadValue),
1655 CanThrow(CT) {}
1656
1657 void Emit(CodeGenFunction &CGF, Flags flags) override {
1658 llvm::Value *BlockVarAddr;
1659 if (LoadBlockVarAddr) {
1660 BlockVarAddr = CGF.Builder.CreateLoad(Addr);
1661 } else {
1662 BlockVarAddr = Addr.getPointer();
1663 }
1664
1665 CGF.BuildBlockRelease(BlockVarAddr, FieldFlags, CanThrow);
1666 }
1667};
1668} // end anonymous namespace
1669
1670/// Check if \p T is a C++ class that has a destructor that can throw.
1672 if (const auto *RD = T->getAsCXXRecordDecl())
1673 if (const CXXDestructorDecl *DD = RD->getDestructor())
1674 return DD->getType()->castAs<FunctionProtoType>()->canThrow();
1675 return false;
1676}
1677
1678// Return a string that has the information about a capture.
1679static std::string getBlockCaptureStr(const CGBlockInfo::Capture &Cap,
1680 CaptureStrKind StrKind,
1681 CharUnits BlockAlignment,
1682 CodeGenModule &CGM) {
1683 std::string Str;
1684 ASTContext &Ctx = CGM.getContext();
1685 const BlockDecl::Capture &CI = *Cap.Cap;
1686 QualType CaptureTy = CI.getVariable()->getType();
1687
1689 BlockFieldFlags Flags;
1690
1691 // CaptureStrKind::Merged should be passed only when the operations and the
1692 // flags are the same for copy and dispose.
1693 assert((StrKind != CaptureStrKind::Merged ||
1694 (Cap.CopyKind == Cap.DisposeKind &&
1695 Cap.CopyFlags == Cap.DisposeFlags)) &&
1696 "different operations and flags");
1697
1698 if (StrKind == CaptureStrKind::DisposeHelper) {
1699 Kind = Cap.DisposeKind;
1700 Flags = Cap.DisposeFlags;
1701 } else {
1702 Kind = Cap.CopyKind;
1703 Flags = Cap.CopyFlags;
1704 }
1705
1706 switch (Kind) {
1708 Str += "c";
1709 SmallString<256> TyStr;
1710 llvm::raw_svector_ostream Out(TyStr);
1711 CGM.getCXXABI().getMangleContext().mangleCanonicalTypeName(CaptureTy, Out);
1712 Str += llvm::to_string(TyStr.size()) + TyStr.c_str();
1713 break;
1714 }
1716 Str += "w";
1717 break;
1719 Str += "s";
1720 break;
1722 const VarDecl *Var = CI.getVariable();
1723 unsigned F = Flags.getBitMask();
1724 if (F & BLOCK_FIELD_IS_BYREF) {
1725 Str += "r";
1726 if (F & BLOCK_FIELD_IS_WEAK)
1727 Str += "w";
1728 else {
1729 // If CaptureStrKind::Merged is passed, check both the copy expression
1730 // and the destructor.
1731 if (StrKind != CaptureStrKind::DisposeHelper) {
1732 if (Ctx.getBlockVarCopyInit(Var).canThrow())
1733 Str += "c";
1734 }
1735 if (StrKind != CaptureStrKind::CopyHelper) {
1737 Str += "d";
1738 }
1739 }
1740 } else {
1741 assert((F & BLOCK_FIELD_IS_OBJECT) && "unexpected flag value");
1742 if (F == BLOCK_FIELD_IS_BLOCK)
1743 Str += "b";
1744 else
1745 Str += "o";
1746 }
1747 break;
1748 }
1750 bool IsVolatile = CaptureTy.isVolatileQualified();
1751 CharUnits Alignment = BlockAlignment.alignmentAtOffset(Cap.getOffset());
1752
1753 Str += "n";
1754 std::string FuncStr;
1755 if (StrKind == CaptureStrKind::DisposeHelper)
1757 CaptureTy, Alignment, IsVolatile, Ctx);
1758 else
1759 // If CaptureStrKind::Merged is passed, use the copy constructor string.
1760 // It has all the information that the destructor string has.
1762 CaptureTy, Alignment, IsVolatile, Ctx);
1763 // The underscore is necessary here because non-trivial copy constructor
1764 // and destructor strings can start with a number.
1765 Str += llvm::to_string(FuncStr.size()) + "_" + FuncStr;
1766 break;
1767 }
1769 break;
1770 }
1771
1772 return Str;
1773}
1774
1777 CharUnits BlockAlignment, CaptureStrKind StrKind, CodeGenModule &CGM) {
1778 assert((StrKind == CaptureStrKind::CopyHelper ||
1779 StrKind == CaptureStrKind::DisposeHelper) &&
1780 "unexpected CaptureStrKind");
1781 std::string Name = StrKind == CaptureStrKind::CopyHelper
1782 ? "__copy_helper_block_"
1783 : "__destroy_helper_block_";
1784 if (CGM.getLangOpts().Exceptions)
1785 Name += "e";
1786 if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
1787 Name += "a";
1788 Name += llvm::to_string(BlockAlignment.getQuantity()) + "_";
1789
1790 for (auto &Cap : Captures) {
1791 if (Cap.isConstantOrTrivial())
1792 continue;
1793 Name += llvm::to_string(Cap.getOffset().getQuantity());
1794 Name += getBlockCaptureStr(Cap, StrKind, BlockAlignment, CGM);
1795 }
1796
1797 return Name;
1798}
1799
1801 Address Field, QualType CaptureType,
1802 BlockFieldFlags Flags, bool ForCopyHelper,
1803 VarDecl *Var, CodeGenFunction &CGF) {
1804 bool EHOnly = ForCopyHelper;
1805
1806 switch (CaptureKind) {
1811 if (CaptureType.isDestructedType() &&
1812 (!EHOnly || CGF.needsEHCleanup(CaptureType.isDestructedType()))) {
1813 CodeGenFunction::Destroyer *Destroyer =
1816 : CGF.getDestroyer(CaptureType.isDestructedType());
1817 CleanupKind Kind =
1818 EHOnly ? EHCleanup
1819 : CGF.getCleanupKind(CaptureType.isDestructedType());
1820 CGF.pushDestroy(Kind, Field, CaptureType, Destroyer, Kind & EHCleanup);
1821 }
1822 break;
1823 }
1825 if (!EHOnly || CGF.getLangOpts().Exceptions) {
1826 CleanupKind Kind = EHOnly ? EHCleanup : NormalAndEHCleanup;
1827 // Calls to _Block_object_dispose along the EH path in the copy helper
1828 // function don't throw as newly-copied __block variables always have a
1829 // reference count of 2.
1830 bool CanThrow =
1831 !ForCopyHelper && CGF.cxxDestructorCanThrow(CaptureType);
1832 CGF.enterByrefCleanup(Kind, Field, Flags, /*LoadBlockVarAddr*/ true,
1833 CanThrow);
1834 }
1835 break;
1836 }
1838 break;
1839 }
1840}
1841
1842static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType,
1843 llvm::Function *Fn,
1844 const CGFunctionInfo &FI,
1845 CodeGenModule &CGM) {
1846 if (CapturesNonExternalType) {
1848 } else {
1849 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
1850 Fn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1851 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn, /*IsThunk=*/false);
1853 }
1854}
1855/// Generate the copy-helper function for a block closure object:
1856/// static void block_copy_helper(block_t *dst, block_t *src);
1857/// The runtime will have previously initialized 'dst' by doing a
1858/// bit-copy of 'src'.
1859///
1860/// Note that this copies an entire block closure object to the heap;
1861/// it should not be confused with a 'byref copy helper', which moves
1862/// the contents of an individual __block variable to the heap.
1863llvm::Constant *
1865 std::string FuncName = getCopyDestroyHelperFuncName(
1866 blockInfo.SortedCaptures, blockInfo.BlockAlign,
1867 CaptureStrKind::CopyHelper, CGM);
1868
1869 if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName))
1870 return Func;
1871
1872 ASTContext &C = getContext();
1873
1874 QualType ReturnTy = C.VoidTy;
1875
1876 FunctionArgList args;
1877 ImplicitParamDecl DstDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
1878 args.push_back(&DstDecl);
1879 ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
1880 args.push_back(&SrcDecl);
1881
1882 const CGFunctionInfo &FI =
1884
1885 // FIXME: it would be nice if these were mergeable with things with
1886 // identical semantics.
1887 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
1888
1889 llvm::Function *Fn =
1890 llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage,
1891 FuncName, &CGM.getModule());
1892 if (CGM.supportsCOMDAT())
1893 Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName));
1894
1896 ArgTys.push_back(C.VoidPtrTy);
1897 ArgTys.push_back(C.VoidPtrTy);
1898
1900 CGM);
1901 StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args);
1902 auto AL = ApplyDebugLocation::CreateArtificial(*this);
1903
1904 Address src = GetAddrOfLocalVar(&SrcDecl);
1905 src = Address(Builder.CreateLoad(src), blockInfo.StructureType,
1906 blockInfo.BlockAlign);
1907
1908 Address dst = GetAddrOfLocalVar(&DstDecl);
1909 dst = Address(Builder.CreateLoad(dst), blockInfo.StructureType,
1910 blockInfo.BlockAlign);
1911
1912 for (auto &capture : blockInfo.SortedCaptures) {
1913 if (capture.isConstantOrTrivial())
1914 continue;
1915
1916 const BlockDecl::Capture &CI = *capture.Cap;
1917 QualType captureType = CI.getVariable()->getType();
1918 BlockFieldFlags flags = capture.CopyFlags;
1919
1920 unsigned index = capture.getIndex();
1921 Address srcField = Builder.CreateStructGEP(src, index);
1922 Address dstField = Builder.CreateStructGEP(dst, index);
1923
1924 switch (capture.CopyKind) {
1926 // If there's an explicit copy expression, we do that.
1927 assert(CI.getCopyExpr() && "copy expression for variable is missing");
1928 EmitSynthesizedCXXCopyCtor(dstField, srcField, CI.getCopyExpr());
1929 break;
1931 EmitARCCopyWeak(dstField, srcField);
1932 break;
1934 // If this is a C struct that requires non-trivial copy construction,
1935 // emit a call to its copy constructor.
1936 QualType varType = CI.getVariable()->getType();
1937 callCStructCopyConstructor(MakeAddrLValue(dstField, varType),
1938 MakeAddrLValue(srcField, varType));
1939 break;
1940 }
1942 llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
1943 // At -O0, store null into the destination field (so that the
1944 // storeStrong doesn't over-release) and then call storeStrong.
1945 // This is a workaround to not having an initStrong call.
1946 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1947 auto *ty = cast<llvm::PointerType>(srcValue->getType());
1948 llvm::Value *null = llvm::ConstantPointerNull::get(ty);
1949 Builder.CreateStore(null, dstField);
1950 EmitARCStoreStrongCall(dstField, srcValue, true);
1951
1952 // With optimization enabled, take advantage of the fact that
1953 // the blocks runtime guarantees a memcpy of the block data, and
1954 // just emit a retain of the src field.
1955 } else {
1956 EmitARCRetainNonBlock(srcValue);
1957
1958 // Unless EH cleanup is required, we don't need this anymore, so kill
1959 // it. It's not quite worth the annoyance to avoid creating it in the
1960 // first place.
1961 if (!needsEHCleanup(captureType.isDestructedType()))
1962 cast<llvm::Instruction>(dstField.getPointer())->eraseFromParent();
1963 }
1964 break;
1965 }
1967 llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
1968 llvm::Value *dstAddr = dstField.getPointer();
1969 llvm::Value *args[] = {
1970 dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
1971 };
1972
1973 if (CI.isByRef() && C.getBlockVarCopyInit(CI.getVariable()).canThrow())
1975 else
1977 break;
1978 }
1980 continue;
1981 }
1982
1983 // Ensure that we destroy the copied object if an exception is thrown later
1984 // in the helper function.
1985 pushCaptureCleanup(capture.CopyKind, dstField, captureType, flags,
1986 /*ForCopyHelper*/ true, CI.getVariable(), *this);
1987 }
1988
1990
1991 return Fn;
1992}
1993
1994static BlockFieldFlags
1996 QualType T) {
1998 if (T->isBlockPointerType())
1999 Flags = BLOCK_FIELD_IS_BLOCK;
2000 return Flags;
2001}
2002
2003static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
2005 const LangOptions &LangOpts) {
2006 if (CI.isEscapingByref()) {
2008 if (T.isObjCGCWeak())
2009 Flags |= BLOCK_FIELD_IS_WEAK;
2010 return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
2011 }
2012
2013 switch (T.isDestructedType()) {
2015 return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags());
2017 // Use objc_storeStrong for __strong direct captures; the
2018 // dynamic tools really like it when we do this.
2019 return std::make_pair(BlockCaptureEntityKind::ARCStrong,
2022 // Support __weak direct captures.
2023 return std::make_pair(BlockCaptureEntityKind::ARCWeak,
2026 return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct,
2027 BlockFieldFlags());
2028 case QualType::DK_none: {
2029 // Non-ARC captures are strong, and we need to use _Block_object_dispose.
2030 // But honor the inert __unsafe_unretained qualifier, which doesn't actually
2031 // make it into the type system.
2033 !LangOpts.ObjCAutoRefCount && !T->isObjCInertUnsafeUnretainedType())
2034 return std::make_pair(BlockCaptureEntityKind::BlockObject,
2036 // Otherwise, we have nothing to do.
2037 return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
2038 }
2039 }
2040 llvm_unreachable("after exhaustive DestructionKind switch");
2041}
2042
2043/// Generate the destroy-helper function for a block closure object:
2044/// static void block_destroy_helper(block_t *theBlock);
2045///
2046/// Note that this destroys a heap-allocated block closure object;
2047/// it should not be confused with a 'byref destroy helper', which
2048/// destroys the heap-allocated contents of an individual __block
2049/// variable.
2050llvm::Constant *
2052 std::string FuncName = getCopyDestroyHelperFuncName(
2053 blockInfo.SortedCaptures, blockInfo.BlockAlign,
2054 CaptureStrKind::DisposeHelper, CGM);
2055
2056 if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName))
2057 return Func;
2058
2059 ASTContext &C = getContext();
2060
2061 QualType ReturnTy = C.VoidTy;
2062
2063 FunctionArgList args;
2064 ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
2065 args.push_back(&SrcDecl);
2066
2067 const CGFunctionInfo &FI =
2069
2070 // FIXME: We'd like to put these into a mergable by content, with
2071 // internal linkage.
2072 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
2073
2074 llvm::Function *Fn =
2075 llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage,
2076 FuncName, &CGM.getModule());
2077 if (CGM.supportsCOMDAT())
2078 Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName));
2079
2081 ArgTys.push_back(C.VoidPtrTy);
2082
2084 CGM);
2085 StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args);
2087
2088 auto AL = ApplyDebugLocation::CreateArtificial(*this);
2089
2090 Address src = GetAddrOfLocalVar(&SrcDecl);
2091 src = Address(Builder.CreateLoad(src), blockInfo.StructureType,
2092 blockInfo.BlockAlign);
2093
2094 CodeGenFunction::RunCleanupsScope cleanups(*this);
2095
2096 for (auto &capture : blockInfo.SortedCaptures) {
2097 if (capture.isConstantOrTrivial())
2098 continue;
2099
2100 const BlockDecl::Capture &CI = *capture.Cap;
2101 BlockFieldFlags flags = capture.DisposeFlags;
2102
2103 Address srcField = Builder.CreateStructGEP(src, capture.getIndex());
2104
2105 pushCaptureCleanup(capture.DisposeKind, srcField,
2106 CI.getVariable()->getType(), flags,
2107 /*ForCopyHelper*/ false, CI.getVariable(), *this);
2108 }
2109
2110 cleanups.ForceCleanup();
2111
2113
2114 return Fn;
2115}
2116
2117namespace {
2118
2119/// Emits the copy/dispose helper functions for a __block object of id type.
2120class ObjectByrefHelpers final : public BlockByrefHelpers {
2121 BlockFieldFlags Flags;
2122
2123public:
2124 ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
2125 : BlockByrefHelpers(alignment), Flags(flags) {}
2126
2127 void emitCopy(CodeGenFunction &CGF, Address destField,
2128 Address srcField) override {
2129 destField = destField.withElementType(CGF.Int8Ty);
2130
2131 srcField = srcField.withElementType(CGF.Int8PtrTy);
2132 llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
2133
2134 unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
2135
2136 llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
2137 llvm::FunctionCallee fn = CGF.CGM.getBlockObjectAssign();
2138
2139 llvm::Value *args[] = { destField.getPointer(), srcValue, flagsVal };
2140 CGF.EmitNounwindRuntimeCall(fn, args);
2141 }
2142
2143 void emitDispose(CodeGenFunction &CGF, Address field) override {
2144 field = field.withElementType(CGF.Int8PtrTy);
2145 llvm::Value *value = CGF.Builder.CreateLoad(field);
2146
2147 CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER, false);
2148 }
2149
2150 void profileImpl(llvm::FoldingSetNodeID &id) const override {
2151 id.AddInteger(Flags.getBitMask());
2152 }
2153};
2154
2155/// Emits the copy/dispose helpers for an ARC __block __weak variable.
2156class ARCWeakByrefHelpers final : public BlockByrefHelpers {
2157public:
2158 ARCWeakByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {}
2159
2160 void emitCopy(CodeGenFunction &CGF, Address destField,
2161 Address srcField) override {
2162 CGF.EmitARCMoveWeak(destField, srcField);
2163 }
2164
2165 void emitDispose(CodeGenFunction &CGF, Address field) override {
2166 CGF.EmitARCDestroyWeak(field);
2167 }
2168
2169 void profileImpl(llvm::FoldingSetNodeID &id) const override {
2170 // 0 is distinguishable from all pointers and byref flags
2171 id.AddInteger(0);
2172 }
2173};
2174
2175/// Emits the copy/dispose helpers for an ARC __block __strong variable
2176/// that's not of block-pointer type.
2177class ARCStrongByrefHelpers final : public BlockByrefHelpers {
2178public:
2179 ARCStrongByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {}
2180
2181 void emitCopy(CodeGenFunction &CGF, Address destField,
2182 Address srcField) override {
2183 // Do a "move" by copying the value and then zeroing out the old
2184 // variable.
2185
2186 llvm::Value *value = CGF.Builder.CreateLoad(srcField);
2187
2188 llvm::Value *null =
2189 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
2190
2191 if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
2192 CGF.Builder.CreateStore(null, destField);
2193 CGF.EmitARCStoreStrongCall(destField, value, /*ignored*/ true);
2194 CGF.EmitARCStoreStrongCall(srcField, null, /*ignored*/ true);
2195 return;
2196 }
2197 CGF.Builder.CreateStore(value, destField);
2198 CGF.Builder.CreateStore(null, srcField);
2199 }
2200
2201 void emitDispose(CodeGenFunction &CGF, Address field) override {
2203 }
2204
2205 void profileImpl(llvm::FoldingSetNodeID &id) const override {
2206 // 1 is distinguishable from all pointers and byref flags
2207 id.AddInteger(1);
2208 }
2209};
2210
2211/// Emits the copy/dispose helpers for an ARC __block __strong
2212/// variable that's of block-pointer type.
2213class ARCStrongBlockByrefHelpers final : public BlockByrefHelpers {
2214public:
2215 ARCStrongBlockByrefHelpers(CharUnits alignment)
2216 : BlockByrefHelpers(alignment) {}
2217
2218 void emitCopy(CodeGenFunction &CGF, Address destField,
2219 Address srcField) override {
2220 // Do the copy with objc_retainBlock; that's all that
2221 // _Block_object_assign would do anyway, and we'd have to pass the
2222 // right arguments to make sure it doesn't get no-op'ed.
2223 llvm::Value *oldValue = CGF.Builder.CreateLoad(srcField);
2224 llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, /*mandatory*/ true);
2225 CGF.Builder.CreateStore(copy, destField);
2226 }
2227
2228 void emitDispose(CodeGenFunction &CGF, Address field) override {
2230 }
2231
2232 void profileImpl(llvm::FoldingSetNodeID &id) const override {
2233 // 2 is distinguishable from all pointers and byref flags
2234 id.AddInteger(2);
2235 }
2236};
2237
2238/// Emits the copy/dispose helpers for a __block variable with a
2239/// nontrivial copy constructor or destructor.
2240class CXXByrefHelpers final : public BlockByrefHelpers {
2241 QualType VarType;
2242 const Expr *CopyExpr;
2243
2244public:
2245 CXXByrefHelpers(CharUnits alignment, QualType type,
2246 const Expr *copyExpr)
2247 : BlockByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
2248
2249 bool needsCopy() const override { return CopyExpr != nullptr; }
2250 void emitCopy(CodeGenFunction &CGF, Address destField,
2251 Address srcField) override {
2252 if (!CopyExpr) return;
2253 CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
2254 }
2255
2256 void emitDispose(CodeGenFunction &CGF, Address field) override {
2258 CGF.PushDestructorCleanup(VarType, field);
2259 CGF.PopCleanupBlocks(cleanupDepth);
2260 }
2261
2262 void profileImpl(llvm::FoldingSetNodeID &id) const override {
2263 id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
2264 }
2265};
2266
2267/// Emits the copy/dispose helpers for a __block variable that is a non-trivial
2268/// C struct.
2269class NonTrivialCStructByrefHelpers final : public BlockByrefHelpers {
2270 QualType VarType;
2271
2272public:
2273 NonTrivialCStructByrefHelpers(CharUnits alignment, QualType type)
2274 : BlockByrefHelpers(alignment), VarType(type) {}
2275
2276 void emitCopy(CodeGenFunction &CGF, Address destField,
2277 Address srcField) override {
2278 CGF.callCStructMoveConstructor(CGF.MakeAddrLValue(destField, VarType),
2279 CGF.MakeAddrLValue(srcField, VarType));
2280 }
2281
2282 bool needsDispose() const override {
2283 return VarType.isDestructedType();
2284 }
2285
2286 void emitDispose(CodeGenFunction &CGF, Address field) override {
2288 CGF.pushDestroy(VarType.isDestructedType(), field, VarType);
2289 CGF.PopCleanupBlocks(cleanupDepth);
2290 }
2291
2292 void profileImpl(llvm::FoldingSetNodeID &id) const override {
2293 id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
2294 }
2295};
2296} // end anonymous namespace
2297
2298static llvm::Constant *
2300 BlockByrefHelpers &generator) {
2301 ASTContext &Context = CGF.getContext();
2302
2303 QualType ReturnTy = Context.VoidTy;
2304
2305 FunctionArgList args;
2307 args.push_back(&Dst);
2308
2310 args.push_back(&Src);
2311
2312 const CGFunctionInfo &FI =
2313 CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
2314
2315 llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI);
2316
2317 // FIXME: We'd like to put these into a mergable by content, with
2318 // internal linkage.
2319 llvm::Function *Fn =
2320 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2321 "__Block_byref_object_copy_", &CGF.CGM.getModule());
2322
2324 ArgTys.push_back(Context.VoidPtrTy);
2325 ArgTys.push_back(Context.VoidPtrTy);
2326
2328
2329 CGF.StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args);
2330 // Create a scope with an artificial location for the body of this function.
2332
2333 if (generator.needsCopy()) {
2334 // dst->x
2335 Address destField = CGF.GetAddrOfLocalVar(&Dst);
2336 destField = Address(CGF.Builder.CreateLoad(destField), byrefInfo.Type,
2337 byrefInfo.ByrefAlignment);
2338 destField =
2339 CGF.emitBlockByrefAddress(destField, byrefInfo, false, "dest-object");
2340
2341 // src->x
2342 Address srcField = CGF.GetAddrOfLocalVar(&Src);
2343 srcField = Address(CGF.Builder.CreateLoad(srcField), byrefInfo.Type,
2344 byrefInfo.ByrefAlignment);
2345 srcField =
2346 CGF.emitBlockByrefAddress(srcField, byrefInfo, false, "src-object");
2347
2348 generator.emitCopy(CGF, destField, srcField);
2349 }
2350
2351 CGF.FinishFunction();
2352
2353 return Fn;
2354}
2355
2356/// Build the copy helper for a __block variable.
2357static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
2358 const BlockByrefInfo &byrefInfo,
2359 BlockByrefHelpers &generator) {
2360 CodeGenFunction CGF(CGM);
2361 return generateByrefCopyHelper(CGF, byrefInfo, generator);
2362}
2363
2364/// Generate code for a __block variable's dispose helper.
2365static llvm::Constant *
2367 const BlockByrefInfo &byrefInfo,
2368 BlockByrefHelpers &generator) {
2369 ASTContext &Context = CGF.getContext();
2370 QualType R = Context.VoidTy;
2371
2372 FunctionArgList args;
2373 ImplicitParamDecl Src(CGF.getContext(), Context.VoidPtrTy,
2375 args.push_back(&Src);
2376
2377 const CGFunctionInfo &FI =
2379
2380 llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI);
2381
2382 // FIXME: We'd like to put these into a mergable by content, with
2383 // internal linkage.
2384 llvm::Function *Fn =
2385 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2386 "__Block_byref_object_dispose_",
2387 &CGF.CGM.getModule());
2388
2390 ArgTys.push_back(Context.VoidPtrTy);
2391
2393
2394 CGF.StartFunction(GlobalDecl(), R, Fn, FI, args);
2395 // Create a scope with an artificial location for the body of this function.
2397
2398 if (generator.needsDispose()) {
2399 Address addr = CGF.GetAddrOfLocalVar(&Src);
2400 addr = Address(CGF.Builder.CreateLoad(addr), byrefInfo.Type,
2401 byrefInfo.ByrefAlignment);
2402 addr = CGF.emitBlockByrefAddress(addr, byrefInfo, false, "object");
2403
2404 generator.emitDispose(CGF, addr);
2405 }
2406
2407 CGF.FinishFunction();
2408
2409 return Fn;
2410}
2411
2412/// Build the dispose helper for a __block variable.
2413static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
2414 const BlockByrefInfo &byrefInfo,
2415 BlockByrefHelpers &generator) {
2416 CodeGenFunction CGF(CGM);
2417 return generateByrefDisposeHelper(CGF, byrefInfo, generator);
2418}
2419
2420/// Lazily build the copy and dispose helpers for a __block variable
2421/// with the given information.
2422template <class T>
2423static T *buildByrefHelpers(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo,
2424 T &&generator) {
2425 llvm::FoldingSetNodeID id;
2426 generator.Profile(id);
2427
2428 void *insertPos;
2429 BlockByrefHelpers *node
2430 = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
2431 if (node) return static_cast<T*>(node);
2432
2433 generator.CopyHelper = buildByrefCopyHelper(CGM, byrefInfo, generator);
2434 generator.DisposeHelper = buildByrefDisposeHelper(CGM, byrefInfo, generator);
2435
2436 T *copy = new (CGM.getContext()) T(std::forward<T>(generator));
2437 CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
2438 return copy;
2439}
2440
2441/// Build the copy and dispose helpers for the given __block variable
2442/// emission. Places the helpers in the global cache. Returns null
2443/// if no helpers are required.
2445CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType,
2446 const AutoVarEmission &emission) {
2447 const VarDecl &var = *emission.Variable;
2448 assert(var.isEscapingByref() &&
2449 "only escaping __block variables need byref helpers");
2450
2451 QualType type = var.getType();
2452
2453 auto &byrefInfo = getBlockByrefInfo(&var);
2454
2455 // The alignment we care about for the purposes of uniquing byref
2456 // helpers is the alignment of the actual byref value field.
2457 CharUnits valueAlignment =
2458 byrefInfo.ByrefAlignment.alignmentAtOffset(byrefInfo.FieldOffset);
2459
2460 if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
2461 const Expr *copyExpr =
2463 if (!copyExpr && record->hasTrivialDestructor()) return nullptr;
2464
2465 return ::buildByrefHelpers(
2466 CGM, byrefInfo, CXXByrefHelpers(valueAlignment, type, copyExpr));
2467 }
2468
2469 // If type is a non-trivial C struct type that is non-trivial to
2470 // destructly move or destroy, build the copy and dispose helpers.
2471 if (type.isNonTrivialToPrimitiveDestructiveMove() == QualType::PCK_Struct ||
2472 type.isDestructedType() == QualType::DK_nontrivial_c_struct)
2473 return ::buildByrefHelpers(
2474 CGM, byrefInfo, NonTrivialCStructByrefHelpers(valueAlignment, type));
2475
2476 // Otherwise, if we don't have a retainable type, there's nothing to do.
2477 // that the runtime does extra copies.
2478 if (!type->isObjCRetainableType()) return nullptr;
2479
2480 Qualifiers qs = type.getQualifiers();
2481
2482 // If we have lifetime, that dominates.
2483 if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
2484 switch (lifetime) {
2485 case Qualifiers::OCL_None: llvm_unreachable("impossible");
2486
2487 // These are just bits as far as the runtime is concerned.
2490 return nullptr;
2491
2492 // Tell the runtime that this is ARC __weak, called by the
2493 // byref routines.
2495 return ::buildByrefHelpers(CGM, byrefInfo,
2496 ARCWeakByrefHelpers(valueAlignment));
2497
2498 // ARC __strong __block variables need to be retained.
2500 // Block pointers need to be copied, and there's no direct
2501 // transfer possible.
2502 if (type->isBlockPointerType()) {
2503 return ::buildByrefHelpers(CGM, byrefInfo,
2504 ARCStrongBlockByrefHelpers(valueAlignment));
2505
2506 // Otherwise, we transfer ownership of the retain from the stack
2507 // to the heap.
2508 } else {
2509 return ::buildByrefHelpers(CGM, byrefInfo,
2510 ARCStrongByrefHelpers(valueAlignment));
2511 }
2512 }
2513 llvm_unreachable("fell out of lifetime switch!");
2514 }
2515
2516 BlockFieldFlags flags;
2517 if (type->isBlockPointerType()) {
2518 flags |= BLOCK_FIELD_IS_BLOCK;
2519 } else if (CGM.getContext().isObjCNSObjectType(type) ||
2520 type->isObjCObjectPointerType()) {
2521 flags |= BLOCK_FIELD_IS_OBJECT;
2522 } else {
2523 return nullptr;
2524 }
2525
2526 if (type.isObjCGCWeak())
2527 flags |= BLOCK_FIELD_IS_WEAK;
2528
2529 return ::buildByrefHelpers(CGM, byrefInfo,
2530 ObjectByrefHelpers(valueAlignment, flags));
2531}
2532
2534 const VarDecl *var,
2535 bool followForward) {
2536 auto &info = getBlockByrefInfo(var);
2537 return emitBlockByrefAddress(baseAddr, info, followForward, var->getName());
2538}
2539
2541 const BlockByrefInfo &info,
2542 bool followForward,
2543 const llvm::Twine &name) {
2544 // Chase the forwarding address if requested.
2545 if (followForward) {
2546 Address forwardingAddr = Builder.CreateStructGEP(baseAddr, 1, "forwarding");
2547 baseAddr = Address(Builder.CreateLoad(forwardingAddr), info.Type,
2548 info.ByrefAlignment);
2549 }
2550
2551 return Builder.CreateStructGEP(baseAddr, info.FieldIndex, name);
2552}
2553
2554/// BuildByrefInfo - This routine changes a __block variable declared as T x
2555/// into:
2556///
2557/// struct {
2558/// void *__isa;
2559/// void *__forwarding;
2560/// int32_t __flags;
2561/// int32_t __size;
2562/// void *__copy_helper; // only if needed
2563/// void *__destroy_helper; // only if needed
2564/// void *__byref_variable_layout;// only if needed
2565/// char padding[X]; // only if needed
2566/// T x;
2567/// } x
2568///
2570 auto it = BlockByrefInfos.find(D);
2571 if (it != BlockByrefInfos.end())
2572 return it->second;
2573
2574 llvm::StructType *byrefType =
2575 llvm::StructType::create(getLLVMContext(),
2576 "struct.__block_byref_" + D->getNameAsString());
2577
2578 QualType Ty = D->getType();
2579
2580 CharUnits size;
2582
2583 // void *__isa;
2584 types.push_back(VoidPtrTy);
2585 size += getPointerSize();
2586
2587 // void *__forwarding;
2588 types.push_back(VoidPtrTy);
2589 size += getPointerSize();
2590
2591 // int32_t __flags;
2592 types.push_back(Int32Ty);
2593 size += CharUnits::fromQuantity(4);
2594
2595 // int32_t __size;
2596 types.push_back(Int32Ty);
2597 size += CharUnits::fromQuantity(4);
2598
2599 // Note that this must match *exactly* the logic in buildByrefHelpers.
2600 bool hasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D);
2601 if (hasCopyAndDispose) {
2602 /// void *__copy_helper;
2603 types.push_back(VoidPtrTy);
2604 size += getPointerSize();
2605
2606 /// void *__destroy_helper;
2607 types.push_back(VoidPtrTy);
2608 size += getPointerSize();
2609 }
2610
2611 bool HasByrefExtendedLayout = false;
2613 if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) &&
2614 HasByrefExtendedLayout) {
2615 /// void *__byref_variable_layout;
2616 types.push_back(VoidPtrTy);
2618 }
2619
2620 // T x;
2621 llvm::Type *varTy = ConvertTypeForMem(Ty);
2622
2623 bool packed = false;
2624 CharUnits varAlign = getContext().getDeclAlign(D);
2625 CharUnits varOffset = size.alignTo(varAlign);
2626
2627 // We may have to insert padding.
2628 if (varOffset != size) {
2629 llvm::Type *paddingTy =
2630 llvm::ArrayType::get(Int8Ty, (varOffset - size).getQuantity());
2631
2632 types.push_back(paddingTy);
2633 size = varOffset;
2634
2635 // Conversely, we might have to prevent LLVM from inserting padding.
2636 } else if (CGM.getDataLayout().getABITypeAlign(varTy) >
2637 uint64_t(varAlign.getQuantity())) {
2638 packed = true;
2639 }
2640 types.push_back(varTy);
2641
2642 byrefType->setBody(types, packed);
2643
2644 BlockByrefInfo info;
2645 info.Type = byrefType;
2646 info.FieldIndex = types.size() - 1;
2647 info.FieldOffset = varOffset;
2648 info.ByrefAlignment = std::max(varAlign, getPointerAlign());
2649
2650 auto pair = BlockByrefInfos.insert({D, info});
2651 assert(pair.second && "info was inserted recursively?");
2652 return pair.first->second;
2653}
2654
2655/// Initialize the structural components of a __block variable, i.e.
2656/// everything but the actual object.
2657void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
2658 // Find the address of the local.
2659 Address addr = emission.Addr;
2660
2661 // That's an alloca of the byref structure type.
2662 llvm::StructType *byrefType = cast<llvm::StructType>(addr.getElementType());
2663
2664 unsigned nextHeaderIndex = 0;
2665 CharUnits nextHeaderOffset;
2666 auto storeHeaderField = [&](llvm::Value *value, CharUnits fieldSize,
2667 const Twine &name) {
2668 auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex, name);
2669 Builder.CreateStore(value, fieldAddr);
2670
2671 nextHeaderIndex++;
2672 nextHeaderOffset += fieldSize;
2673 };
2674
2675 // Build the byref helpers if necessary. This is null if we don't need any.
2676 BlockByrefHelpers *helpers = buildByrefHelpers(*byrefType, emission);
2677
2678 const VarDecl &D = *emission.Variable;
2679 QualType type = D.getType();
2680
2681 bool HasByrefExtendedLayout = false;
2683 bool ByRefHasLifetime =
2684 getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout);
2685
2686 llvm::Value *V;
2687
2688 // Initialize the 'isa', which is just 0 or 1.
2689 int isa = 0;
2690 if (type.isObjCGCWeak())
2691 isa = 1;
2692 V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
2693 storeHeaderField(V, getPointerSize(), "byref.isa");
2694
2695 // Store the address of the variable into its own forwarding pointer.
2696 storeHeaderField(addr.getPointer(), getPointerSize(), "byref.forwarding");
2697
2698 // Blocks ABI:
2699 // c) the flags field is set to either 0 if no helper functions are
2700 // needed or BLOCK_BYREF_HAS_COPY_DISPOSE if they are,
2701 BlockFlags flags;
2702 if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE;
2703 if (ByRefHasLifetime) {
2704 if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED;
2705 else switch (ByrefLifetime) {
2708 break;
2710 flags |= BLOCK_BYREF_LAYOUT_WEAK;
2711 break;
2714 break;
2716 if (!type->isObjCObjectPointerType() && !type->isBlockPointerType())
2718 break;
2719 default:
2720 break;
2721 }
2722 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2723 printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask());
2724 if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE)
2725 printf(" BLOCK_BYREF_HAS_COPY_DISPOSE");
2726 if (flags & BLOCK_BYREF_LAYOUT_MASK) {
2727 BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK);
2728 if (ThisFlag == BLOCK_BYREF_LAYOUT_EXTENDED)
2729 printf(" BLOCK_BYREF_LAYOUT_EXTENDED");
2730 if (ThisFlag == BLOCK_BYREF_LAYOUT_STRONG)
2731 printf(" BLOCK_BYREF_LAYOUT_STRONG");
2732 if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK)
2733 printf(" BLOCK_BYREF_LAYOUT_WEAK");
2734 if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED)
2735 printf(" BLOCK_BYREF_LAYOUT_UNRETAINED");
2736 if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT)
2737 printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT");
2738 }
2739 printf("\n");
2740 }
2741 }
2742 storeHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
2743 getIntSize(), "byref.flags");
2744
2745 CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
2746 V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
2747 storeHeaderField(V, getIntSize(), "byref.size");
2748
2749 if (helpers) {
2750 storeHeaderField(helpers->CopyHelper, getPointerSize(),
2751 "byref.copyHelper");
2752 storeHeaderField(helpers->DisposeHelper, getPointerSize(),
2753 "byref.disposeHelper");
2754 }
2755
2756 if (ByRefHasLifetime && HasByrefExtendedLayout) {
2757 auto layoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type);
2758 storeHeaderField(layoutInfo, getPointerSize(), "byref.layout");
2759 }
2760}
2761
2763 bool CanThrow) {
2764 llvm::FunctionCallee F = CGM.getBlockObjectDispose();
2765 llvm::Value *args[] = {V,
2766 llvm::ConstantInt::get(Int32Ty, flags.getBitMask())};
2767
2768 if (CanThrow)
2769 EmitRuntimeCallOrInvoke(F, args);
2770 else
2771 EmitNounwindRuntimeCall(F, args);
2772}
2773
2775 BlockFieldFlags Flags,
2776 bool LoadBlockVarAddr, bool CanThrow) {
2777 EHStack.pushCleanup<CallBlockRelease>(Kind, Addr, Flags, LoadBlockVarAddr,
2778 CanThrow);
2779}
2780
2781/// Adjust the declaration of something from the blocks API.
2783 llvm::Constant *C) {
2784 auto *GV = cast<llvm::GlobalValue>(C->stripPointerCasts());
2785
2786 if (CGM.getTarget().getTriple().isOSBinFormatCOFF()) {
2787 IdentifierInfo &II = CGM.getContext().Idents.get(C->getName());
2790
2791 assert((isa<llvm::Function>(C->stripPointerCasts()) ||
2792 isa<llvm::GlobalVariable>(C->stripPointerCasts())) &&
2793 "expected Function or GlobalVariable");
2794
2795 const NamedDecl *ND = nullptr;
2796 for (const auto *Result : DC->lookup(&II))
2797 if ((ND = dyn_cast<FunctionDecl>(Result)) ||
2798 (ND = dyn_cast<VarDecl>(Result)))
2799 break;
2800
2801 // TODO: support static blocks runtime
2802 if (GV->isDeclaration() && (!ND || !ND->hasAttr<DLLExportAttr>())) {
2803 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
2804 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
2805 } else {
2806 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
2807 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
2808 }
2809 }
2810
2811 if (CGM.getLangOpts().BlocksRuntimeOptional && GV->isDeclaration() &&
2812 GV->hasExternalLinkage())
2813 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2814
2815 CGM.setDSOLocal(GV);
2816}
2817
2819 if (BlockObjectDispose)
2820 return BlockObjectDispose;
2821
2822 llvm::Type *args[] = { Int8PtrTy, Int32Ty };
2823 llvm::FunctionType *fty
2824 = llvm::FunctionType::get(VoidTy, args, false);
2825 BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose");
2827 *this, cast<llvm::Constant>(BlockObjectDispose.getCallee()));
2828 return BlockObjectDispose;
2829}
2830
2832 if (BlockObjectAssign)
2833 return BlockObjectAssign;
2834
2835 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
2836 llvm::FunctionType *fty
2837 = llvm::FunctionType::get(VoidTy, args, false);
2838 BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign");
2840 *this, cast<llvm::Constant>(BlockObjectAssign.getCallee()));
2841 return BlockObjectAssign;
2842}
2843
2845 if (NSConcreteGlobalBlock)
2846 return NSConcreteGlobalBlock;
2847
2848 NSConcreteGlobalBlock = GetOrCreateLLVMGlobal(
2849 "_NSConcreteGlobalBlock", Int8PtrTy, LangAS::Default, nullptr);
2850 configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock);
2851 return NSConcreteGlobalBlock;
2852}
2853
2855 if (NSConcreteStackBlock)
2856 return NSConcreteStackBlock;
2857
2858 NSConcreteStackBlock = GetOrCreateLLVMGlobal(
2859 "_NSConcreteStackBlock", Int8PtrTy, LangAS::Default, nullptr);
2860 configureBlocksRuntimeObject(*this, NSConcreteStackBlock);
2861 return NSConcreteStackBlock;
2862}
#define V(N, I)
Definition: ASTContext.h:3259
static bool CanThrow(Expr *E, ASTContext &Ctx)
Definition: CFG.cpp:2679
static llvm::Constant * buildByrefDisposeHelper(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Build the dispose helper for a __block variable.
Definition: CGBlocks.cpp:2413
static llvm::Constant * buildBlockDescriptor(CodeGenModule &CGM, const CGBlockInfo &blockInfo)
buildBlockDescriptor - Build the block descriptor meta-data for a block.
Definition: CGBlocks.cpp:147
static void addBlockLayout(CharUnits align, CharUnits size, const BlockDecl::Capture *capture, llvm::Type *type, QualType fieldType, SmallVectorImpl< BlockLayoutChunk > &Layout, CGBlockInfo &Info, CodeGenModule &CGM)
Definition: CGBlocks.cpp:380
static llvm::Constant * generateByrefDisposeHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Generate code for a __block variable's dispose helper.
Definition: CGBlocks.cpp:2366
static QualType getCaptureFieldType(const CodeGenFunction &CGF, const BlockDecl::Capture &CI)
Definition: CGBlocks.cpp:526
static std::string getCopyDestroyHelperFuncName(const SmallVectorImpl< CGBlockInfo::Capture > &Captures, CharUnits BlockAlignment, CaptureStrKind StrKind, CodeGenModule &CGM)
Definition: CGBlocks.cpp:1775
static std::string getBlockDescriptorName(const CGBlockInfo &BlockInfo, CodeGenModule &CGM)
Definition: CGBlocks.cpp:85
static llvm::Constant * buildCopyHelper(CodeGenModule &CGM, const CGBlockInfo &blockInfo)
Build the helper function to copy a block.
Definition: CGBlocks.cpp:56
static std::string getBlockCaptureStr(const CGBlockInfo::Capture &Cap, CaptureStrKind StrKind, CharUnits BlockAlignment, CodeGenModule &CGM)
Definition: CGBlocks.cpp:1679
static llvm::Constant * tryCaptureAsConstant(CodeGenModule &CGM, CodeGenFunction *CGF, const VarDecl *var)
It is illegal to modify a const object after initialization.
Definition: CGBlocks.cpp:437
static llvm::Constant * generateByrefCopyHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Definition: CGBlocks.cpp:2299
static std::pair< BlockCaptureEntityKind, BlockFieldFlags > computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, const LangOptions &LangOpts)
Definition: CGBlocks.cpp:2004
static llvm::Constant * buildByrefCopyHelper(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Build the copy helper for a __block variable.
Definition: CGBlocks.cpp:2357
static BlockFieldFlags getBlockFieldFlagsForObjCObjectPointer(const BlockDecl::Capture &CI, QualType T)
Definition: CGBlocks.cpp:1995
static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF, CGBlockInfo &info)
Compute the layout of the given block.
Definition: CGBlocks.cpp:545
static T * buildByrefHelpers(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, T &&generator)
Lazily build the copy and dispose helpers for a __block variable with the given information.
Definition: CGBlocks.cpp:2423
static llvm::Constant * buildGlobalBlock(CodeGenModule &CGM, const CGBlockInfo &blockInfo, llvm::Constant *blockFn)
Build the given block as a global block.
Definition: CGBlocks.cpp:1278
static llvm::Constant * buildDisposeHelper(CodeGenModule &CGM, const CGBlockInfo &blockInfo)
Build the helper function to dispose of a block.
Definition: CGBlocks.cpp:62
static void configureBlocksRuntimeObject(CodeGenModule &CGM, llvm::Constant *C)
Adjust the declaration of something from the blocks API.
Definition: CGBlocks.cpp:2782
static bool isSafeForCXXConstantCapture(QualType type)
Determines if the given type is safe for constant capture in C++.
Definition: CGBlocks.cpp:413
static void pushCaptureCleanup(BlockCaptureEntityKind CaptureKind, Address Field, QualType CaptureType, BlockFieldFlags Flags, bool ForCopyHelper, VarDecl *Var, CodeGenFunction &CGF)
Definition: CGBlocks.cpp:1800
static std::pair< BlockCaptureEntityKind, BlockFieldFlags > computeCopyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, const LangOptions &LangOpts)
Definition: CGBlocks.cpp:1584
static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType, llvm::Function *Fn, const CGFunctionInfo &FI, CodeGenModule &CGM)
Definition: CGBlocks.cpp:1842
static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info, SmallVectorImpl< llvm::Type * > &elementTypes)
Definition: CGBlocks.cpp:473
static CharUnits getLowBit(CharUnits v)
Get the low bit of a nonzero character count.
Definition: CGBlocks.cpp:469
static bool isTrivial(ASTContext &Ctx, const Expr *E)
Checks if the expression is constant or does not have non-trivial function calls.
const CFGBlock * Block
Definition: HTMLLogger.cpp:153
static bool isBlockPointer(Expr *Arg)
__device__ __2f16 b
do v
Definition: arm_acle.h:83
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
bool getByrefLifetime(QualType Ty, Qualifiers::ObjCLifetime &Lifetime, bool &HasByrefExtendedLayout) const
Returns true, if given type has a known lifetime.
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1068
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
CanQualType VoidPtrTy
Definition: ASTContext.h:1113
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type.
bool BlockRequiresCopying(QualType Ty, const VarDecl *D)
Returns true iff we need copy/dispose helpers for the given type.
IdentifierTable & Idents
Definition: ASTContext.h:639
const LangOptions & getLangOpts() const
Definition: ASTContext.h:770
BlockVarCopyInit getBlockVarCopyInit(const VarDecl *VD) const
Get the copy initialization expression of the VarDecl VD, or nullptr if none exists.
TypeInfoChars getTypeInfoInChars(const Type *T) const
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
static bool isObjCNSObjectType(QualType Ty)
Return true if this is an NSObject object with its NSObject attribute set.
Definition: ASTContext.h:2295
CanQualType VoidTy
Definition: ASTContext.h:1086
std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const
Return the encoded type for this block declaration.
QualType getAddrSpaceQualType(QualType T, LangAS AddressSpace) const
Return the uniqued reference to the type for an address space qualified type with the specified type ...
unsigned getTargetAddressSpace(LangAS AS) const
A class which contains all the information about a particular captured value.
Definition: Decl.h:4465
bool isNested() const
Whether this is a nested capture, i.e.
Definition: Decl.h:4502
Expr * getCopyExpr() const
Definition: Decl.h:4505
bool isByRef() const
Whether this is a "by ref" capture, i.e.
Definition: Decl.h:4490
VarDecl * getVariable() const
The variable being captured.
Definition: Decl.h:4486
bool isEscapingByref() const
Definition: Decl.h:4492
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4459
capture_const_iterator capture_begin() const
Definition: Decl.h:4588
capture_const_iterator capture_end() const
Definition: Decl.h:4589
ArrayRef< Capture > captures() const
Definition: Decl.h:4586
bool capturesCXXThis() const
Definition: Decl.h:4591
bool doesNotEscape() const
Definition: Decl.h:4610
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
Definition: Decl.h:4578
bool isConversionFromLambda() const
Definition: Decl.h:4602
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6167
const Stmt * getBody() const
Definition: Expr.cpp:2497
SourceLocation getEndLoc() const LLVM_READONLY
Definition: Expr.h:6191
const FunctionProtoType * getFunctionType() const
getFunctionType - Return the underlying function type for this block.
Definition: Expr.cpp:2488
const BlockDecl * getBlockDecl() const
Definition: Expr.h:6179
Pointer to a block type.
Definition: Type.h:2978
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2792
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2819
Expr * getCallee()
Definition: Expr.h:2969
arg_range arguments()
Definition: Expr.h:3058
Decl * getCalleeDecl()
Definition: Expr.h:2983
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset?
Definition: CharUnits.h:207
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition: CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
Definition: CharUnits.h:201
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
bool hasReducedDebugInfo() const
Check if type and variable info should be emitted.
An aligned address.
Definition: Address.h:29
static Address invalid()
Definition: Address.h:46
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:62
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition: Address.h:100
llvm::Value * getPointer() const
Definition: Address.h:51
bool isValid() const
Definition: Address.h:47
An aggregate value slot.
Definition: CGValue.h:512
static AggValueSlot forAddr(Address addr, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
forAddr - Make a slot for an aggregate value.
Definition: CGValue.h:595
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:823
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:863
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
Definition: CGDebugInfo.h:880
A pair of helper functions for a __block variable.
virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src)=0
virtual bool needsCopy() const
virtual void emitDispose(CodeGenFunction &CGF, Address field)=0
virtual bool needsDispose() const
Information about the layout of a __block variable.
Definition: CGBlocks.h:136
llvm::StructType * Type
Definition: CGBlocks.h:138
uint32_t getBitMask() const
Definition: CGBlocks.h:110
uint32_t getBitMask() const
Definition: CGBlocks.h:66
const BlockDecl::Capture * Cap
Definition: CGBlocks.h:237
static Capture makeIndex(unsigned index, CharUnits offset, QualType FieldType, BlockCaptureEntityKind CopyKind, BlockFieldFlags CopyFlags, BlockCaptureEntityKind DisposeKind, BlockFieldFlags DisposeFlags, const BlockDecl::Capture *Cap)
Definition: CGBlocks.h:205
BlockCaptureEntityKind CopyKind
Definition: CGBlocks.h:234
BlockCaptureEntityKind DisposeKind
Definition: CGBlocks.h:235
llvm::Value * getConstant() const
Definition: CGBlocks.h:195
static Capture makeConstant(llvm::Value *value, const BlockDecl::Capture *Cap)
Definition: CGBlocks.h:221
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:156
CGBlockInfo(const BlockDecl *blockDecl, StringRef Name)
Definition: CGBlocks.cpp:35
unsigned CXXThisIndex
The field index of 'this' within the block, if there is one.
Definition: CGBlocks.h:162
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:304
llvm::StructType * StructureType
Definition: CGBlocks.h:275
CharUnits BlockHeaderForcedGapOffset
Definition: CGBlocks.h:285
bool UsesStret
UsesStret : True if the block uses an stret return.
Definition: CGBlocks.h:257
const BlockExpr * BlockExpression
Definition: CGBlocks.h:277
const BlockExpr * getBlockExpr() const
Definition: CGBlocks.h:305
bool HasCapturedVariableLayout
HasCapturedVariableLayout : True if block has captured variables and their layout meta-data has been ...
Definition: CGBlocks.h:261
bool CapturesNonExternalType
Indicates whether an object of a non-external C++ class is captured.
Definition: CGBlocks.h:266
bool NeedsCopyDispose
True if the block has captures that would necessitate custom copy or dispose helper functions if the ...
Definition: CGBlocks.h:246
bool CanBeGlobal
CanBeGlobal - True if the block can be global, i.e.
Definition: CGBlocks.h:242
bool HasCXXObject
HasCXXObject - True if the block's custom copy/dispose functions need to be run even in GC mode.
Definition: CGBlocks.h:253
CharUnits BlockHeaderForcedGapSize
Definition: CGBlocks.h:288
const Capture & getCapture(const VarDecl *var) const
Definition: CGBlocks.h:295
llvm::SmallVector< Capture, 4 > SortedCaptures
The block's captures. Non-constant captures are sorted by their offsets.
Definition: CGBlocks.h:272
bool NoEscape
Indicates whether the block is non-escaping.
Definition: CGBlocks.h:249
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:97
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:172
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:71
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:89
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:117
Abstract information about a function or function prototype.
Definition: CGCall.h:40
All available information about a concrete callee.
Definition: CGCall.h:62
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:55
CGFunctionInfo - Class to encapsulate the information about a function definition.
virtual llvm::Constant * BuildByrefLayout(CodeGen::CodeGenModule &CGM, QualType T)=0
Returns an i8* which points to the byref layout information.
virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM, const CGBlockInfo &blockInfo)
virtual llvm::Constant * BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, const CodeGen::CGBlockInfo &blockInfo)=0
virtual llvm::Constant * BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, const CodeGen::CGBlockInfo &blockInfo)=0
llvm::Function * getInvokeFunction(const Expr *E)
void recordBlockInfo(const BlockExpr *E, llvm::Function *InvokeF, llvm::Value *Block, llvm::Type *BlockTy)
Record invoke function and block literal emitted during normal codegen for a block expression.
llvm::PointerType * getGenericVoidPointerType()
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:258
void add(RValue rvalue, QualType type)
Definition: CGCall.h:282
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitARCDestroyWeak(Address addr)
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, bool LoadBlockVarAddr, bool CanThrow)
Enter a cleanup to destroy a __block variable.
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
static bool cxxDestructorCanThrow(QualType T)
Check if T is a C++ class that has a destructor that can throw.
SanitizerSet SanOpts
Sanitizers enabled for this function.
void EmitARCMoveWeak(Address dst, Address src)
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue)
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp)
void callCStructMoveConstructor(LValue Dst, LValue Src)
void callCStructCopyConstructor(LValue Dst, LValue Src)
const LangOptions & getLangOpts() const
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
const CodeGen::CGBlockInfo * BlockInfo
Address EmitLoadOfReference(LValue RefLVal, LValueBaseInfo *PointeeBaseInfo=nullptr, TBAAAccessInfo *PointeeTBAAInfo=nullptr)
void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
EmitExprAsInit - Emits the code necessary to initialize a location in memory with the given initializ...
void emitByrefStructureInit(const AutoVarEmission &emission)
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, bool IsMustTail, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type,...
void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, std::initializer_list< llvm::Value ** > ValuesToReload={})
Takes the old cleanup stack size and emits the cleanup blocks that have been added.
llvm::Type * ConvertTypeForMem(QualType T)
llvm::Value * EmitARCRetainBlock(llvm::Value *value, bool mandatory)
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
static std::string getNonTrivialDestructorStr(QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx)
llvm::DenseMap< const Decl *, Address > DeclMapTy
llvm::Constant * GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo)
const Expr * RetExpr
If a return statement is being visited, this holds the return statment's result expression.
void EmitARCCopyWeak(Address dst, Address src)
void PushDestructorCleanup(QualType T, Address Addr)
PushDestructorCleanup - Push a cleanup to call the complete-object destructor of an object of the giv...
void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, llvm::Value *ptr)
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn)
Annotate the function with an attribute that disables TSan checking at runtime.
Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, bool followForward=true)
BuildBlockByrefAddress - Computes the location of the data in a variable which is declared as __block...
LValue EmitDeclRefLValue(const DeclRefExpr *E)
llvm::Constant * GenerateCopyHelperFunction(const CGBlockInfo &blockInfo)
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
llvm::Function * GenerateBlockFunction(GlobalDecl GD, const CGBlockInfo &Info, const DeclMapTy &ldm, bool IsLambdaConversionToBlock, bool BuildGlobalBlock)
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::Value * EmitBlockLiteral(const BlockExpr *)
Emit block literal.
Address CreateMemTemp(QualType T, const Twine &Name="tmp", Address *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind.
llvm::DenseMap< const ValueDecl *, FieldDecl * > LambdaCaptureFields
CleanupKind getCleanupKind(QualType::DestructionKind kind)
llvm::Type * ConvertType(QualType T)
Address GetAddrOfBlockDecl(const VarDecl *var)
llvm::CallBase * EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args, const Twine &name="")
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
llvm::Value * LoadCXXThis()
LoadCXXThis - Load the value of 'this'.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
static Destroyer destroyARCStrongImprecise
void EmitStmt(const Stmt *S, ArrayRef< const Attr * > Attrs=std::nullopt)
EmitStmt - Emit the code for the statement.
llvm::LLVMContext & getLLVMContext()
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
static std::string getNonTrivialCopyConstructorStr(QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx)
void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags, bool CanThrow)
void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise)
This class organizes the cross-function state that is used while generating LLVM code.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
llvm::FunctionCallee getBlockObjectAssign()
Definition: CGBlocks.cpp:2831
llvm::FoldingSet< BlockByrefHelpers > ByrefHelpersCache
void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, const CGFunctionInfo &FI)
Set the attributes on the LLVM function for the given decl and function info.
void setDSOLocal(llvm::GlobalValue *GV) const
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const
Return the store size, in character units, of the given LLVM type.
void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr)
Notes that BE's global block is available via Addr.
Definition: CGBlocks.cpp:1248
llvm::Type * getBlockDescriptorType()
Fetches the type of a generic block descriptor.
Definition: CGBlocks.cpp:1087
llvm::Constant * GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name)
Gets the address of a block which requires no captures.
Definition: CGBlocks.cpp:1256
const LangOptions & getLangOpts() const
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
const TargetInfo & getTarget() const
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
const llvm::DataLayout & getDataLayout() const
llvm::Constant * getNSConcreteGlobalBlock()
Definition: CGBlocks.cpp:2844
CGCXXABI & getCXXABI() const
llvm::Constant * getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE)
Returns the address of a block which requires no caputres, or null if we've yet to emit the block for...
bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI)
Return true iff the given type uses an argument slot when 'sret' is used as a return type.
Definition: CGCall.cpp:1586
llvm::Constant * GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, LangAS AddrSpace, const VarDecl *D, ForDefinition_t IsForDefinition=NotForDefinition)
GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, create and return an llvm...
ASTContext & getContext() const
llvm::Constant * getNSConcreteStackBlock()
Definition: CGBlocks.cpp:2854
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
llvm::FunctionCallee getBlockObjectDispose()
Definition: CGBlocks.cpp:2818
llvm::LLVMContext & getLLVMContext()
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F, bool IsThunk)
Set the LLVM function attributes (sext, zext, etc).
llvm::Type * getGenericBlockLiteralType()
The type of a generic block literal.
Definition: CGBlocks.cpp:1119
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
ConstantAddress GetAddrOfConstantCString(const std::string &Str, const char *GlobalName=nullptr)
Returns a pointer to a character array containing the literal and a terminating '\0' character.
void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn)
Assign counters to regions and configure them for PGO of a given function.
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1625
const CGFunctionInfo & arrangeBlockFunctionCall(const CallArgList &args, const FunctionType *type)
A block function is essentially a free function with an extra implicit argument.
Definition: CGCall.cpp:643
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:674
const CGFunctionInfo & arrangeBlockFunctionDeclaration(const FunctionProtoType *type, const FunctionArgList &args)
Block invocation functions are C functions with an implicit parameter.
Definition: CGCall.cpp:650
llvm::Type * ConvertTypeForMem(QualType T, bool ForBitField=false)
ConvertTypeForMem - Convert type T into a llvm::Type.
llvm::Constant * getPointer() const
Definition: Address.h:132
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
StructBuilder beginStruct(llvm::StructType *structTy=nullptr)
The standard implementation of ConstantInitBuilder used in Clang.
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:141
A saved depth on the scope stack.
Definition: EHScopeStack.h:101
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
Definition: EHScopeStack.h:393
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:352
Address getAddress(CodeGenFunction &CGF) const
Definition: CGValue.h:350
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
static RValue get(llvm::Value *V)
Definition: CGValue.h:89
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:356
virtual TargetOpenCLBlockHelper * getTargetOpenCLBlockHelper() const
Definition: TargetInfo.h:342
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1446
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Definition: DeclBase.cpp:1785
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
SourceLocation getLocation() const
Definition: DeclBase.h:444
bool hasAttr() const
Definition: DeclBase.h:582
This represents one expression.
Definition: Expr.h:110
QualType getType() const
Definition: Expr.h:142
Represents difference between two FPOptions values.
Definition: LangOptions.h:870
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4199
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3799
QualType getReturnType() const
Definition: Type.h:4116
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:3649
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:418
virtual void mangleCanonicalTypeName(QualType T, raw_ostream &, bool NormalizeIntegers=false)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing.
This represents a decl that may have a name.
Definition: Decl.h:249
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:276
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition: Decl.h:292
A (possibly-)qualified type.
Definition: Type.h:737
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6985
@ DK_cxx_destructor
Definition: Type.h:1313
@ DK_nontrivial_c_struct
Definition: Type.h:1316
@ DK_objc_weak_lifetime
Definition: Type.h:1315
@ DK_objc_strong_lifetime
Definition: Type.h:1314
PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const
Check if this is a non-trivial type that would cause a C struct transitively containing this type to ...
Definition: Type.cpp:2807
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6942
bool isObjCGCWeak() const
true when Type is objc's weak.
Definition: Type.h:1219
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition: Type.h:1323
@ PCK_Struct
The type is a struct containing a field whose type is neither PCK_Trivial nor PCK_VolatileTrivial.
Definition: Type.h:1295
@ PCK_Trivial
The type does not fall into any of the following categories.
Definition: Type.h:1274
@ PCK_ARCStrong
The type is an Objective-C retainable pointer type that is qualified with the ARC __strong qualifier.
Definition: Type.h:1283
@ PCK_VolatileTrivial
The type would be trivial except that it is volatile-qualified.
Definition: Type.h:1279
@ PCK_ARCWeak
The type is an Objective-C retainable pointer type that is qualified with the ARC __weak qualifier.
Definition: Type.h:1287
The collection of all-type qualifiers we support.
Definition: Type.h:147
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition: Type.h:175
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition: Type.h:168
@ OCL_None
There is no lifetime qualification on this type.
Definition: Type.h:164
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition: Type.h:178
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition: Type.h:181
bool hasObjCLifetime() const
Definition: Type.h:351
ObjCLifetime getObjCLifetime() const
Definition: Type.h:352
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5092
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Encodes a location in the source.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:338
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1220
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:464
uint64_t getPointerAlign(LangAS AddrSpace) const
Definition: TargetInfo.h:468
The top declaration context.
Definition: Decl.h:84
static DeclContext * castToDeclContext(const TranslationUnitDecl *D)
Definition: Decl.h:130
The base class of the type hierarchy.
Definition: Type.h:1606
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1819
bool isBlockPointerType() const
Definition: Type.h:7162
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7724
bool isReferenceType() const
Definition: Type.h:7166
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:651
bool isObjCInertUnsafeUnretainedType() const
Was this type written with the special inert-in-ARC __unsafe_unretained qualifier?
Definition: Type.h:2317
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7657
bool isObjCRetainableType() const
Definition: Type.cpp:4758
QualType getType() const
Definition: Decl.h:717
Represents a variable declaration or definition.
Definition: Decl.h:918
const Expr * getInit() const
Definition: Decl.h:1352
bool isNonEscapingByref() const
Indicates the capture is a __block variable that is never captured by an escaping block.
Definition: Decl.cpp:2679
bool isEscapingByref() const
Indicates the capture is a __block variable that is captured by a block that can potentially escape (...
Definition: Decl.cpp:2675
@ BLOCK_HAS_SIGNATURE
Definition: CGBlocks.h:54
@ BLOCK_IS_NOESCAPE
Definition: CGBlocks.h:49
@ BLOCK_HAS_CXX_OBJ
Definition: CGBlocks.h:51
@ BLOCK_HAS_EXTENDED_LAYOUT
Definition: CGBlocks.h:55
@ BLOCK_HAS_COPY_DISPOSE
Definition: CGBlocks.h:50
@ BLOCK_FIELD_IS_BYREF
Definition: CGBlocks.h:92
@ BLOCK_FIELD_IS_WEAK
Definition: CGBlocks.h:94
@ BLOCK_BYREF_CALLER
Definition: CGBlocks.h:97
@ BLOCK_FIELD_IS_BLOCK
Definition: CGBlocks.h:90
@ BLOCK_FIELD_IS_OBJECT
Definition: CGBlocks.h:88
@ BLOCK_BYREF_LAYOUT_MASK
Definition: CGBlocks.h:40
@ BLOCK_BYREF_LAYOUT_WEAK
Definition: CGBlocks.h:44
@ BLOCK_BYREF_LAYOUT_STRONG
Definition: CGBlocks.h:43
@ BLOCK_BYREF_LAYOUT_EXTENDED
Definition: CGBlocks.h:41
@ BLOCK_BYREF_LAYOUT_NON_OBJECT
Definition: CGBlocks.h:42
@ BLOCK_BYREF_HAS_COPY_DISPOSE
Definition: CGBlocks.h:39
@ BLOCK_BYREF_LAYOUT_UNRETAINED
Definition: CGBlocks.h:45
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
BlockCaptureEntityKind
Represents a type of copy/destroy operation that should be performed for an entity that's captured by...
Definition: CGBlocks.h:146
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:84
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:80
@ ARCImpreciseLifetime
Definition: CGValue.h:125
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, BlockExpr > blockExpr
Matches a reference to a block.
const AstTypeMatcher< RecordType > recordType
Matches record types (e.g.
const internal::VariadicDynCastAllOfMatcher< Decl, BlockDecl > blockDecl
Matches block declarations.
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:95
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition: Address.h:155
@ OpenCL
Definition: LangStandard.h:64
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
@ Result
The result type of a method or function.
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:132
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:136
@ Other
Other implicit parameter.
@ ObjCSelf
Parameter for Objective-C 'self' argument.
unsigned long uint64_t
int printf(__constant const char *st,...) __attribute__((format(printf
unsigned long ulong
An unsigned 64-bit integer.
#define false
Definition: stdbool.h:22
Expr * getCopyExpr() const
Definition: Expr.h:6220
bool canThrow() const
Definition: Expr.h:6221
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
llvm::IntegerType * IntTy
int