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