clang 23.0.0git
CGObjC.cpp
Go to the documentation of this file.
1//===---- CGObjC.cpp - Emit LLVM Code for Objective-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 Objective-C code as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGDebugInfo.h"
14#include "CGObjCRuntime.h"
15#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
17#include "CodeGenPGO.h"
18#include "ConstantEmitter.h"
19#include "TargetInfo.h"
21#include "clang/AST/Attr.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/NSAPI.h"
24#include "clang/AST/StmtObjC.h"
28#include "llvm/Analysis/ObjCARCUtil.h"
29#include "llvm/BinaryFormat/MachO.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/InlineAsm.h"
33#include <optional>
34using namespace clang;
35using namespace CodeGen;
36
37typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult;
38static TryEmitResult
41 QualType ET,
43
44/// Given the address of a variable of pointer type, find the correct
45/// null to store into it.
46static llvm::Constant *getNullForVariable(Address addr) {
47 llvm::Type *type = addr.getElementType();
48 return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
49}
50
51/// Emits an instance of NSConstantString representing the object.
53{
54 llvm::Constant *C =
55 CGM.getObjCRuntime().GenerateConstantString(E->getString()).getPointer();
56 return C;
57}
58
59/// EmitObjCBoxedExpr - This routine generates code to call
60/// the appropriate expression boxing method. This will either be
61/// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:],
62/// or [NSValue valueWithBytes:objCType:].
63///
64llvm::Value *
66 // If decided in Sema constant initializers are supported by the runtime, not
67 // disabled, and the contents can be emitted as a constant NSNumber subclass;
68 // use the ConstEmitter
70 ConstantEmitter ConstEmitter(CGM);
71 return ConstEmitter.tryEmitAbstract(E, E->getType());
72 }
73
74 // Generate the correct selector for this literal's concrete type.
75 // Get the method.
76 const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod();
77 const Expr *SubExpr = E->getSubExpr();
78
80 ConstantEmitter ConstEmitter(CGM);
81 return ConstEmitter.tryEmitAbstract(E, E->getType());
82 }
83
84 assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method");
85 Selector Sel = BoxingMethod->getSelector();
86
87 // Generate a reference to the class pointer, which will be the receiver.
88 // Assumes that the method was introduced in the class that should be
89 // messaged (avoids pulling it out of the result type).
90 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
91 const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface();
92 llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl);
93
94 CallArgList Args;
95 const ParmVarDecl *ArgDecl = *BoxingMethod->param_begin();
96 QualType ArgQT = ArgDecl->getType().getUnqualifiedType();
97
98 // ObjCBoxedExpr supports boxing of structs and unions
99 // via [NSValue valueWithBytes:objCType:]
100 const QualType ValueType(SubExpr->getType().getCanonicalType());
101 if (ValueType->isObjCBoxableRecordType()) {
102 // Emit CodeGen for first parameter
103 // and cast value to correct type
104 Address Temporary = CreateMemTemp(SubExpr->getType());
105 EmitAnyExprToMem(SubExpr, Temporary, Qualifiers(), /*isInit*/ true);
106 llvm::Value *BitCast = Builder.CreateBitCast(
107 Temporary.emitRawPointer(*this), ConvertType(ArgQT));
108 Args.add(RValue::get(BitCast), ArgQT);
109
110 // Create char array to store type encoding
111 std::string Str;
112 getContext().getObjCEncodingForType(ValueType, Str);
113 llvm::Constant *GV = CGM.GetAddrOfConstantCString(Str).getPointer();
114
115 // Cast type encoding to correct type
116 const ParmVarDecl *EncodingDecl = BoxingMethod->parameters()[1];
117 QualType EncodingQT = EncodingDecl->getType().getUnqualifiedType();
118 llvm::Value *Cast = Builder.CreateBitCast(GV, ConvertType(EncodingQT));
119
120 Args.add(RValue::get(Cast), EncodingQT);
121 } else {
122 Args.add(EmitAnyExpr(SubExpr), ArgQT);
123 }
124
125 RValue result = Runtime.GenerateMessageSend(
126 *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver,
127 Args, ClassDecl, BoxingMethod);
128 return Builder.CreateBitCast(result.getScalarVal(),
129 ConvertType(E->getType()));
130}
131
133 const ObjCMethodDecl *MethodWithObjects) {
134 ASTContext &Context = CGM.getContext();
135 const ObjCDictionaryLiteral *DLE = nullptr;
136 const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E);
137 if (!ALE)
139
140 const bool CanBeExpressedAsConstant =
143 if (CanBeExpressedAsConstant) {
144 ConstantEmitter ConstEmitter(CGM);
145 return ConstEmitter.tryEmitAbstract(E, E->getType());
146 }
147
148 // Optimize empty collections by referencing constants, when available and
149 // constant initializers aren't supported
150 uint64_t NumElements = ALE ? ALE->getNumElements() : DLE->getNumElements();
151
152 if (NumElements == 0 && CGM.getLangOpts().ObjCRuntime.hasEmptyCollections()) {
153 StringRef ConstantName = ALE ? "__NSArray0__" : "__NSDictionary0__";
154 QualType IdTy(CGM.getContext().getObjCIdType());
155 llvm::Constant *Constant =
156 CGM.CreateRuntimeVariable(ConvertType(IdTy), ConstantName);
157 LValue LV = MakeNaturalAlignAddrLValue(Constant, IdTy);
158 llvm::Value *Ptr = EmitLoadOfScalar(LV, E->getBeginLoc());
159 cast<llvm::LoadInst>(Ptr)->setMetadata(
160 llvm::LLVMContext::MD_invariant_load,
161 llvm::MDNode::get(getLLVMContext(), {}));
162 return Builder.CreateBitCast(Ptr, ConvertType(E->getType()));
163 }
164
165 // Compute the type of the array we're initializing.
166 llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()),
167 NumElements);
168 QualType ElementType = Context.getObjCIdType().withConst();
169 QualType ElementArrayType = Context.getConstantArrayType(
170 ElementType, APNumElements, nullptr, ArraySizeModifier::Normal,
171 /*IndexTypeQuals=*/0);
172
173 // Allocate the temporary array(s).
174 Address Objects = CreateMemTemp(ElementArrayType, "objects");
175 Address Keys = Address::invalid();
176 if (DLE)
177 Keys = CreateMemTemp(ElementArrayType, "keys");
178
179 // In ARC, we may need to do extra work to keep all the keys and
180 // values alive until after the call.
181 SmallVector<llvm::Value *, 16> NeededObjects;
182 bool TrackNeededObjects =
183 (getLangOpts().ObjCAutoRefCount &&
184 CGM.getCodeGenOpts().OptimizationLevel != 0);
185
186 // Perform the actual initialialization of the array(s).
187 for (uint64_t i = 0; i < NumElements; i++) {
188 if (ALE) {
189 // Emit the element and store it to the appropriate array slot.
190 const Expr *Rhs = ALE->getElement(i);
191 LValue LV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i),
192 ElementType, AlignmentSource::Decl);
193
194 llvm::Value *value = EmitScalarExpr(Rhs);
195 EmitStoreThroughLValue(RValue::get(value), LV, true);
196 if (TrackNeededObjects) {
197 NeededObjects.push_back(value);
198 }
199 } else {
200 // Emit the key and store it to the appropriate array slot.
201 const Expr *Key = DLE->getKeyValueElement(i).Key;
202 LValue KeyLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Keys, i),
203 ElementType, AlignmentSource::Decl);
204 llvm::Value *keyValue = EmitScalarExpr(Key);
205 EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true);
206
207 // Emit the value and store it to the appropriate array slot.
208 const Expr *Value = DLE->getKeyValueElement(i).Value;
209 LValue ValueLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i),
210 ElementType, AlignmentSource::Decl);
211 llvm::Value *valueValue = EmitScalarExpr(Value);
212 EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true);
213 if (TrackNeededObjects) {
214 NeededObjects.push_back(keyValue);
215 NeededObjects.push_back(valueValue);
216 }
217 }
218 }
219
220 // Generate the argument list.
221 CallArgList Args;
222 ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin();
223 const ParmVarDecl *argDecl = *PI++;
224 QualType ArgQT = argDecl->getType().getUnqualifiedType();
225 Args.add(RValue::get(Objects, *this), ArgQT);
226 if (DLE) {
227 argDecl = *PI++;
228 ArgQT = argDecl->getType().getUnqualifiedType();
229 Args.add(RValue::get(Keys, *this), ArgQT);
230 }
231 argDecl = *PI;
232 ArgQT = argDecl->getType().getUnqualifiedType();
233 llvm::Value *Count =
234 llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements);
235 Args.add(RValue::get(Count), ArgQT);
236
237 // Generate a reference to the class pointer, which will be the receiver.
238 Selector Sel = MethodWithObjects->getSelector();
239 QualType ResultType = E->getType();
240 const ObjCObjectPointerType *InterfacePointerType
241 = ResultType->getAsObjCInterfacePointerType();
242 assert(InterfacePointerType && "Unexpected InterfacePointerType - null");
244 = InterfacePointerType->getObjectType()->getInterface();
245 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
246 llvm::Value *Receiver = Runtime.GetClass(*this, Class);
247
248 // Generate the message send.
249 RValue result = Runtime.GenerateMessageSend(
250 *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel,
251 Receiver, Args, Class, MethodWithObjects);
252
253 // The above message send needs these objects, but in ARC they are
254 // passed in a buffer that is essentially __unsafe_unretained.
255 // Therefore we must prevent the optimizer from releasing them until
256 // after the call.
257 if (TrackNeededObjects) {
258 EmitARCIntrinsicUse(NeededObjects);
259 }
260
261 return Builder.CreateBitCast(result.getScalarVal(),
262 ConvertType(E->getType()));
263}
264
268
273
274/// Emit a selector.
276 // Untyped selector.
277 // Note that this implementation allows for non-constant strings to be passed
278 // as arguments to @selector(). Currently, the only thing preventing this
279 // behaviour is the type checking in the front end.
280 return CGM.getObjCRuntime().GetSelector(*this, E->getSelector());
281}
282
284 // FIXME: This should pass the Decl not the name.
285 return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol());
286}
287
288/// Adjust the type of an Objective-C object that doesn't match up due
289/// to type erasure at various points, e.g., related result types or the use
290/// of parameterized classes.
292 RValue Result) {
293 if (!ExpT->isObjCRetainableType())
294 return Result;
295
296 // If the converted types are the same, we're done.
297 llvm::Type *ExpLLVMTy = CGF.ConvertType(ExpT);
298 if (ExpLLVMTy == Result.getScalarVal()->getType())
299 return Result;
300
301 // We have applied a substitution. Cast the rvalue appropriately.
302 return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
303 ExpLLVMTy));
304}
305
306/// Decide whether to extend the lifetime of the receiver of a
307/// returns-inner-pointer message.
308static bool
310 switch (message->getReceiverKind()) {
311
312 // For a normal instance message, we should extend unless the
313 // receiver is loaded from a variable with precise lifetime.
315 const Expr *receiver = message->getInstanceReceiver();
316
317 // Look through OVEs.
318 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
319 if (opaque->getSourceExpr())
320 receiver = opaque->getSourceExpr()->IgnoreParens();
321 }
322
323 const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
324 if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
325 receiver = ice->getSubExpr()->IgnoreParens();
326
327 // Look through OVEs.
328 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
329 if (opaque->getSourceExpr())
330 receiver = opaque->getSourceExpr()->IgnoreParens();
331 }
332
333 // Only __strong variables.
335 return true;
336
337 // All ivars and fields have precise lifetime.
338 if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
339 return false;
340
341 // Otherwise, check for variables.
342 const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
343 if (!declRef) return true;
344 const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
345 if (!var) return true;
346
347 // All variables have precise lifetime except local variables with
348 // automatic storage duration that aren't specially marked.
349 return (var->hasLocalStorage() &&
350 !var->hasAttr<ObjCPreciseLifetimeAttr>());
351 }
352
355 // It's never necessary for class objects.
356 return false;
357
359 // We generally assume that 'self' lives throughout a method call.
360 return false;
361 }
362
363 llvm_unreachable("invalid receiver kind");
364}
365
366/// Given an expression of ObjC pointer type, check whether it was
367/// immediately loaded from an ARC __weak l-value.
368static const Expr *findWeakLValue(const Expr *E) {
369 assert(E->getType()->isObjCRetainableType());
370 E = E->IgnoreParens();
371 if (auto CE = dyn_cast<CastExpr>(E)) {
372 if (CE->getCastKind() == CK_LValueToRValue) {
373 if (CE->getSubExpr()->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
374 return CE->getSubExpr();
375 }
376 }
377
378 return nullptr;
379}
380
381/// The ObjC runtime may provide entrypoints that are likely to be faster
382/// than an ordinary message send of the appropriate selector.
383///
384/// The entrypoints are guaranteed to be equivalent to just sending the
385/// corresponding message. If the entrypoint is implemented naively as just a
386/// message send, using it is a trade-off: it sacrifices a few cycles of
387/// overhead to save a small amount of code. However, it's possible for
388/// runtimes to detect and special-case classes that use "standard"
389/// behavior; if that's dynamically a large proportion of all objects, using
390/// the entrypoint will also be faster than using a message send.
391///
392/// If the runtime does support a required entrypoint, then this method will
393/// generate a call and return the resulting value. Otherwise it will return
394/// std::nullopt and the caller can generate a msgSend instead.
395static std::optional<llvm::Value *> tryGenerateSpecializedMessageSend(
396 CodeGenFunction &CGF, QualType ResultType, llvm::Value *Receiver,
397 const CallArgList &Args, Selector Sel, const ObjCMethodDecl *method,
398 bool isClassMessage) {
399 auto &CGM = CGF.CGM;
400 if (!CGM.getCodeGenOpts().ObjCConvertMessagesToRuntimeCalls)
401 return std::nullopt;
402
403 auto &Runtime = CGM.getLangOpts().ObjCRuntime;
404 switch (Sel.getMethodFamily()) {
405 case OMF_alloc:
406 if (isClassMessage &&
407 Runtime.shouldUseRuntimeFunctionsForAlloc() &&
408 ResultType->isObjCObjectPointerType()) {
409 // [Foo alloc] -> objc_alloc(Foo) or
410 // [self alloc] -> objc_alloc(self)
411 if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "alloc")
412 return CGF.EmitObjCAlloc(Receiver, CGF.ConvertType(ResultType));
413 // [Foo allocWithZone:nil] -> objc_allocWithZone(Foo) or
414 // [self allocWithZone:nil] -> objc_allocWithZone(self)
415 if (Sel.isKeywordSelector() && Sel.getNumArgs() == 1 &&
416 Args.size() == 1 && Args.front().getType()->isPointerType() &&
417 Sel.getNameForSlot(0) == "allocWithZone") {
418 const llvm::Value* arg = Args.front().getKnownRValue().getScalarVal();
420 return CGF.EmitObjCAllocWithZone(Receiver,
421 CGF.ConvertType(ResultType));
422 return std::nullopt;
423 }
424 }
425 break;
426
427 case OMF_autorelease:
428 if (ResultType->isObjCObjectPointerType() &&
429 CGM.getLangOpts().getGC() == LangOptions::NonGC &&
430 Runtime.shouldUseARCFunctionsForRetainRelease())
431 return CGF.EmitObjCAutorelease(Receiver, CGF.ConvertType(ResultType));
432 break;
433
434 case OMF_retain:
435 if (ResultType->isObjCObjectPointerType() &&
436 CGM.getLangOpts().getGC() == LangOptions::NonGC &&
437 Runtime.shouldUseARCFunctionsForRetainRelease())
438 return CGF.EmitObjCRetainNonBlock(Receiver, CGF.ConvertType(ResultType));
439 break;
440
441 case OMF_release:
442 if (ResultType->isVoidType() &&
443 CGM.getLangOpts().getGC() == LangOptions::NonGC &&
444 Runtime.shouldUseARCFunctionsForRetainRelease()) {
445 CGF.EmitObjCRelease(Receiver, ARCPreciseLifetime);
446 return nullptr;
447 }
448 break;
449
450 default:
451 break;
452 }
453 return std::nullopt;
454}
455
457 CodeGenFunction &CGF, ReturnValueSlot Return, QualType ResultType,
458 Selector Sel, llvm::Value *Receiver, const CallArgList &Args,
459 const ObjCInterfaceDecl *OID, const ObjCMethodDecl *Method,
460 bool isClassMessage) {
461 if (std::optional<llvm::Value *> SpecializedResult =
462 tryGenerateSpecializedMessageSend(CGF, ResultType, Receiver, Args,
463 Sel, Method, isClassMessage)) {
464 return RValue::get(*SpecializedResult);
465 }
466 return GenerateMessageSend(CGF, Return, ResultType, Sel, Receiver, Args, OID,
467 Method);
468}
469
471 const ObjCProtocolDecl *PD,
472 llvm::UniqueVector<const ObjCProtocolDecl *> &PDs) {
473 if (!PD->isNonRuntimeProtocol()) {
474 const auto *Can = PD->getCanonicalDecl();
475 PDs.insert(Can);
476 return;
477 }
478
479 for (const auto *ParentPD : PD->protocols())
481}
482
483std::vector<const ObjCProtocolDecl *>
486 std::vector<const ObjCProtocolDecl *> RuntimePds;
487 llvm::DenseSet<const ObjCProtocolDecl *> NonRuntimePDs;
488
489 for (; begin != end; ++begin) {
490 const auto *It = *begin;
491 const auto *Can = It->getCanonicalDecl();
492 if (Can->isNonRuntimeProtocol())
493 NonRuntimePDs.insert(Can);
494 else
495 RuntimePds.push_back(Can);
496 }
497
498 // If there are no non-runtime protocols then we can just stop now.
499 if (NonRuntimePDs.empty())
500 return RuntimePds;
501
502 // Else we have to search through the non-runtime protocol's inheritancy
503 // hierarchy DAG stopping whenever a branch either finds a runtime protocol or
504 // a non-runtime protocol without any parents. These are the "first-implied"
505 // protocols from a non-runtime protocol.
506 llvm::UniqueVector<const ObjCProtocolDecl *> FirstImpliedProtos;
507 for (const auto *PD : NonRuntimePDs)
508 AppendFirstImpliedRuntimeProtocols(PD, FirstImpliedProtos);
509
510 // Walk the Runtime list to get all protocols implied via the inclusion of
511 // this protocol, e.g. all protocols it inherits from including itself.
512 llvm::DenseSet<const ObjCProtocolDecl *> AllImpliedProtocols;
513 for (const auto *PD : RuntimePds) {
514 const auto *Can = PD->getCanonicalDecl();
515 AllImpliedProtocols.insert(Can);
516 Can->getImpliedProtocols(AllImpliedProtocols);
517 }
518
519 // Similar to above, walk the list of first-implied protocols to find the set
520 // all the protocols implied excluding the listed protocols themselves since
521 // they are not yet a part of the `RuntimePds` list.
522 for (const auto *PD : FirstImpliedProtos) {
523 PD->getImpliedProtocols(AllImpliedProtocols);
524 }
525
526 // From the first-implied list we have to finish building the final protocol
527 // list. If a protocol in the first-implied list was already implied via some
528 // inheritance path through some other protocols then it would be redundant to
529 // add it here and so we skip over it.
530 for (const auto *PD : FirstImpliedProtos) {
531 if (!AllImpliedProtocols.contains(PD)) {
532 RuntimePds.push_back(PD);
533 }
534 }
535
536 return RuntimePds;
537}
538
539/// Instead of '[[MyClass alloc] init]', try to generate
540/// 'objc_alloc_init(MyClass)'. This provides a code size improvement on the
541/// caller side, as well as the optimized objc_alloc.
542static std::optional<llvm::Value *>
544 auto &Runtime = CGF.getLangOpts().ObjCRuntime;
545 if (!Runtime.shouldUseRuntimeFunctionForCombinedAllocInit())
546 return std::nullopt;
547
548 // Match the exact pattern '[[MyClass alloc] init]'.
549 Selector Sel = OME->getSelector();
551 !OME->getType()->isObjCObjectPointerType() || !Sel.isUnarySelector() ||
552 Sel.getNameForSlot(0) != "init")
553 return std::nullopt;
554
555 // Okay, this is '[receiver init]', check if 'receiver' is '[cls alloc]'
556 // with 'cls' a Class.
557 auto *SubOME =
558 dyn_cast<ObjCMessageExpr>(OME->getInstanceReceiver()->IgnoreParenCasts());
559 if (!SubOME)
560 return std::nullopt;
561 Selector SubSel = SubOME->getSelector();
562
563 if (!SubOME->getType()->isObjCObjectPointerType() ||
564 !SubSel.isUnarySelector() || SubSel.getNameForSlot(0) != "alloc")
565 return std::nullopt;
566
567 llvm::Value *Receiver = nullptr;
568 switch (SubOME->getReceiverKind()) {
570 if (!SubOME->getInstanceReceiver()->getType()->isObjCClassType())
571 return std::nullopt;
572 Receiver = CGF.EmitScalarExpr(SubOME->getInstanceReceiver());
573 break;
574
576 QualType ReceiverType = SubOME->getClassReceiver();
577 const ObjCObjectType *ObjTy = ReceiverType->castAs<ObjCObjectType>();
578 const ObjCInterfaceDecl *ID = ObjTy->getInterface();
579 assert(ID && "null interface should be impossible here");
580 Receiver = CGF.CGM.getObjCRuntime().GetClass(CGF, ID);
581 break;
582 }
585 return std::nullopt;
586 }
587
588 return CGF.EmitObjCAllocInit(Receiver, CGF.ConvertType(OME->getType()));
589}
590
592 ReturnValueSlot Return) {
593 // Only the lookup mechanism and first two arguments of the method
594 // implementation vary between runtimes. We can get the receiver and
595 // arguments in generic code.
596
597 bool isDelegateInit = E->isDelegateInitCall();
598
599 const ObjCMethodDecl *method = E->getMethodDecl();
600
601 // If the method is -retain, and the receiver's being loaded from
602 // a __weak variable, peephole the entire operation to objc_loadWeakRetained.
603 if (method && E->getReceiverKind() == ObjCMessageExpr::Instance &&
604 method->getMethodFamily() == OMF_retain) {
605 if (auto lvalueExpr = findWeakLValue(E->getInstanceReceiver())) {
606 LValue lvalue = EmitLValue(lvalueExpr);
607 llvm::Value *result = EmitARCLoadWeakRetained(lvalue.getAddress());
608 return AdjustObjCObjectType(*this, E->getType(), RValue::get(result));
609 }
610 }
611
612 if (std::optional<llvm::Value *> Val = tryEmitSpecializedAllocInit(*this, E))
613 return AdjustObjCObjectType(*this, E->getType(), RValue::get(*Val));
614
615 // We don't retain the receiver in delegate init calls, and this is
616 // safe because the receiver value is always loaded from 'self',
617 // which we zero out. We don't want to Block_copy block receivers,
618 // though.
619 bool retainSelf =
620 (!isDelegateInit &&
621 CGM.getLangOpts().ObjCAutoRefCount &&
622 method &&
623 method->hasAttr<NSConsumesSelfAttr>());
624
625 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
626 bool isSuperMessage = false;
627 bool isClassMessage = false;
628 ObjCInterfaceDecl *OID = nullptr;
629 // Find the receiver
630 QualType ReceiverType;
631 llvm::Value *Receiver = nullptr;
632 switch (E->getReceiverKind()) {
634 ReceiverType = E->getInstanceReceiver()->getType();
635 isClassMessage = ReceiverType->isObjCClassType();
636 if (retainSelf) {
639 Receiver = ter.getPointer();
640 if (ter.getInt()) retainSelf = false;
641 } else
642 Receiver = EmitScalarExpr(E->getInstanceReceiver());
643 break;
644
646 ReceiverType = E->getClassReceiver();
647 OID = ReceiverType->castAs<ObjCObjectType>()->getInterface();
648 assert(OID && "Invalid Objective-C class message send");
649 Receiver = Runtime.GetClass(*this, OID);
650 isClassMessage = true;
651 break;
652 }
653
655 ReceiverType = E->getSuperType();
656 Receiver = LoadObjCSelf();
657 isSuperMessage = true;
658 break;
659
661 ReceiverType = E->getSuperType();
662 Receiver = LoadObjCSelf();
663 isSuperMessage = true;
664 isClassMessage = true;
665 break;
666 }
667
668 if (retainSelf)
669 Receiver = EmitARCRetainNonBlock(Receiver);
670
671 // In ARC, we sometimes want to "extend the lifetime"
672 // (i.e. retain+autorelease) of receivers of returns-inner-pointer
673 // messages.
674 if (getLangOpts().ObjCAutoRefCount && method &&
675 method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
677 Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
678
679 QualType ResultType = method ? method->getReturnType() : E->getType();
680
681 CallArgList Args;
682 EmitCallArgs(Args, method, E->arguments(), /*AC*/AbstractCallee(method));
683
684 // For delegate init calls in ARC, do an unsafe store of null into
685 // self. This represents the call taking direct ownership of that
686 // value. We have to do this after emitting the other call
687 // arguments because they might also reference self, but we don't
688 // have to worry about any of them modifying self because that would
689 // be an undefined read and write of an object in unordered
690 // expressions.
691 if (isDelegateInit) {
692 assert(getLangOpts().ObjCAutoRefCount &&
693 "delegate init calls should only be marked in ARC");
694
695 // Do an unsafe store of null into self.
696 Address selfAddr =
698 Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
699 }
700
701 RValue result;
702 if (isSuperMessage) {
703 // super is only valid in an Objective-C method
705 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
706 result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
707 E->getSelector(),
708 OMD->getClassInterface(),
709 isCategoryImpl,
710 Receiver,
711 isClassMessage,
712 Args,
713 method);
714 } else {
715 // Call runtime methods directly if we can.
717 *this, Return, ResultType, E->getSelector(), Receiver, Args, OID,
718 method, isClassMessage);
719 }
720
721 // For delegate init calls in ARC, implicitly store the result of
722 // the call back into self. This takes ownership of the value.
723 if (isDelegateInit) {
724 Address selfAddr =
726 llvm::Value *newSelf = result.getScalarVal();
727
728 // The delegate return type isn't necessarily a matching type; in
729 // fact, it's quite likely to be 'id'.
730 llvm::Type *selfTy = selfAddr.getElementType();
731 newSelf = Builder.CreateBitCast(newSelf, selfTy);
732
733 Builder.CreateStore(newSelf, selfAddr);
734 }
735
736 return AdjustObjCObjectType(*this, E->getType(), result);
737}
738
739namespace {
740struct FinishARCDealloc final : EHScopeStack::Cleanup {
741 void Emit(CodeGenFunction &CGF, Flags flags) override {
743
744 const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
745 const ObjCInterfaceDecl *iface = impl->getClassInterface();
746 if (!iface->getSuperClass()) return;
747
748 bool isCategory = isa<ObjCCategoryImplDecl>(impl);
749
750 // Call [super dealloc] if we have a superclass.
751 llvm::Value *self = CGF.LoadObjCSelf();
752
753 CallArgList args;
755 CGF.getContext().VoidTy,
756 method->getSelector(),
757 iface,
758 isCategory,
759 self,
760 /*is class msg*/ false,
761 args,
762 method);
763 }
764};
765}
766
767/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
768/// the LLVM function and sets the other context used by
769/// CodeGenFunction.
771 const ObjCContainerDecl *CD) {
772 SourceLocation StartLoc = OMD->getBeginLoc();
773 FunctionArgList args;
774 // Check if we should generate debug info for this method.
775 if (OMD->hasAttr<NoDebugAttr>())
776 DebugInfo = nullptr; // disable debug info indefinitely for this function
777
778 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
779
780 const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
781 if (OMD->isDirectMethod()) {
782 // Default hidden visibility
783 Fn->setVisibility(llvm::Function::HiddenVisibility);
784 if (CGM.isObjCDirectPreconditionThunkEnabled()) {
785 // However, if we expose the symbol, and the decl (property or method)
786 // have visibility attribute set ...
787 const NamedDecl *Decl = OMD;
788 if (const auto *PD = OMD->findPropertyDecl()) {
789 Decl = PD;
790 }
791 // ... then respect source level visibility setting
792 if (auto V = Decl->getExplicitVisibility(NamedDecl::VisibilityForValue)) {
793 Fn->setVisibility(CGM.GetLLVMVisibility(*V));
794 }
795 }
796 CGM.SetLLVMFunctionAttributes(OMD, FI, Fn, /*IsThunk=*/false);
797 CGM.SetLLVMFunctionAttributesForDefinition(OMD, Fn);
798 } else {
799 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
800 }
801
802 args.push_back(OMD->getSelfDecl());
803 if (!OMD->isDirectMethod())
804 args.push_back(OMD->getCmdDecl());
805
806 args.append(OMD->param_begin(), OMD->param_end());
807
808 CurGD = OMD;
809 CurEHLocation = OMD->getEndLoc();
810
811 StartFunction(OMD, OMD->getReturnType(), Fn, FI, args,
812 OMD->getLocation(), StartLoc);
813
814 if (OMD->isDirectMethod()) {
815 CGM.getObjCRuntime().GenerateDirectMethodPrologue(*this, Fn, OMD, CD);
816 }
817
818 // In ARC, certain methods get an extra cleanup.
819 if (CGM.getLangOpts().ObjCAutoRefCount &&
820 OMD->isInstanceMethod() &&
821 OMD->getSelector().isUnarySelector()) {
822 const IdentifierInfo *ident =
824 if (ident->isStr("dealloc"))
825 EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
826 }
827}
828
829static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
830 LValue lvalue, QualType type);
831
832/// Generate an Objective-C method. An Objective-C method is a C function with
833/// its pointer, name, and types registered in the class structure.
836 PGO->assignRegionCounters(GlobalDecl(OMD), CurFn);
837 assert(isa<CompoundStmt>(OMD->getBody()));
841}
842
843/// emitStructGetterCall - Call the runtime function to load a property
844/// into the return value slot.
846 bool isAtomic, bool hasStrong) {
847 ASTContext &Context = CGF.getContext();
848
849 llvm::Value *src =
850 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
851 .getPointer(CGF);
852
853 // objc_copyStruct (ReturnValue, &structIvar,
854 // sizeof (Type of Ivar), isAtomic, false);
855 CallArgList args;
856
857 llvm::Value *dest = CGF.ReturnValue.emitRawPointer(CGF);
858 args.add(RValue::get(dest), Context.VoidPtrTy);
859 args.add(RValue::get(src), Context.VoidPtrTy);
860
861 CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
862 args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
863 args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
864 args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
865
866 llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
867 CGCallee callee = CGCallee::forDirect(fn);
868 CGF.EmitCall(CGF.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, args),
869 callee, ReturnValueSlot(), args);
870}
871
872/// Determine whether the given architecture supports unaligned atomic
873/// accesses. They don't have to be fast, just faster than a function
874/// call and a mutex.
875static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
876 // FIXME: Allow unaligned atomic load/store on x86. (It is not
877 // currently supported by the backend.)
878 return false;
879}
880
881/// Return the maximum size that permits atomic accesses for the given
882/// architecture.
884 llvm::Triple::ArchType arch) {
885 // ARM has 8-byte atomic accesses, but it's not clear whether we
886 // want to rely on them here.
887
888 // In the default case, just assume that any size up to a pointer is
889 // fine given adequate alignment.
891}
892
893namespace {
894 class PropertyImplStrategy {
895 public:
896 enum StrategyKind {
897 /// The 'native' strategy is to use the architecture's provided
898 /// reads and writes.
899 Native,
900
901 /// Use objc_setProperty and objc_getProperty.
902 GetSetProperty,
903
904 /// Use objc_setProperty for the setter, but use expression
905 /// evaluation for the getter.
906 SetPropertyAndExpressionGet,
907
908 /// Use objc_copyStruct.
909 CopyStruct,
910
911 /// The 'expression' strategy is to emit normal assignment or
912 /// lvalue-to-rvalue expressions.
914 };
915
916 StrategyKind getKind() const { return StrategyKind(Kind); }
917
918 bool hasStrongMember() const { return HasStrong; }
919 bool isAtomic() const { return IsAtomic; }
920 bool isCopy() const { return IsCopy; }
921
922 CharUnits getIvarSize() const { return IvarSize; }
923 CharUnits getIvarAlignment() const { return IvarAlignment; }
924
925 PropertyImplStrategy(CodeGenModule &CGM,
926 const ObjCPropertyImplDecl *propImpl);
927
928 private:
929 LLVM_PREFERRED_TYPE(StrategyKind)
930 unsigned Kind : 8;
931 LLVM_PREFERRED_TYPE(bool)
932 unsigned IsAtomic : 1;
933 LLVM_PREFERRED_TYPE(bool)
934 unsigned IsCopy : 1;
935 LLVM_PREFERRED_TYPE(bool)
936 unsigned HasStrong : 1;
937
938 CharUnits IvarSize;
939 CharUnits IvarAlignment;
940 };
941}
942
943/// Pick an implementation strategy for the given property synthesis.
944PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
945 const ObjCPropertyImplDecl *propImpl) {
946 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
947 ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
948
949 IsCopy = (setterKind == ObjCPropertyDecl::Copy);
950 IsAtomic = prop->isAtomic();
951 HasStrong = false; // doesn't matter here.
952
953 // Evaluate the ivar's size and alignment.
954 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
955 QualType ivarType = ivar->getType();
956 auto TInfo = CGM.getContext().getTypeInfoInChars(ivarType);
957 IvarSize = TInfo.Width;
958 IvarAlignment = TInfo.Align;
959
960 // If we have a copy property, we always have to use setProperty.
961 // If the property is atomic we need to use getProperty, but in
962 // the nonatomic case we can just use expression.
963 if (IsCopy) {
964 Kind = IsAtomic ? GetSetProperty : SetPropertyAndExpressionGet;
965 return;
966 }
967
968 // Handle retain.
969 if (setterKind == ObjCPropertyDecl::Retain) {
970 // In GC-only, there's nothing special that needs to be done.
971 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
972 // fallthrough
973
974 // In ARC, if the property is non-atomic, use expression emission,
975 // which translates to objc_storeStrong. This isn't required, but
976 // it's slightly nicer.
977 } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) {
978 // Using standard expression emission for the setter is only
979 // acceptable if the ivar is __strong, which won't be true if
980 // the property is annotated with __attribute__((NSObject)).
981 // TODO: falling all the way back to objc_setProperty here is
982 // just laziness, though; we could still use objc_storeStrong
983 // if we hacked it right.
984 if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong)
986 else
987 Kind = SetPropertyAndExpressionGet;
988 return;
989
990 // Otherwise, we need to at least use setProperty. However, if
991 // the property isn't atomic, we can use normal expression
992 // emission for the getter.
993 } else if (!IsAtomic) {
994 Kind = SetPropertyAndExpressionGet;
995 return;
996
997 // Otherwise, we have to use both setProperty and getProperty.
998 } else {
999 Kind = GetSetProperty;
1000 return;
1001 }
1002 }
1003
1004 // If we're not atomic, just use expression accesses.
1005 if (!IsAtomic) {
1006 Kind = Expression;
1007 return;
1008 }
1009
1010 // Properties on bitfield ivars need to be emitted using expression
1011 // accesses even if they're nominally atomic.
1012 if (ivar->isBitField()) {
1013 Kind = Expression;
1014 return;
1015 }
1016
1017 // GC-qualified or ARC-qualified ivars need to be emitted as
1018 // expressions. This actually works out to being atomic anyway,
1019 // except for ARC __strong, but that should trigger the above code.
1020 if (ivarType.hasNonTrivialObjCLifetime() ||
1021 (CGM.getLangOpts().getGC() &&
1022 CGM.getContext().getObjCGCAttrKind(ivarType))) {
1023 Kind = Expression;
1024 return;
1025 }
1026
1027 // Compute whether the ivar has strong members.
1028 if (CGM.getLangOpts().getGC())
1029 if (const auto *RD = ivarType->getAsRecordDecl())
1030 HasStrong = RD->hasObjectMember();
1031
1032 // We can never access structs with object members with a native
1033 // access, because we need to use write barriers. This is what
1034 // objc_copyStruct is for.
1035 if (HasStrong) {
1036 Kind = CopyStruct;
1037 return;
1038 }
1039
1040 // Otherwise, this is target-dependent and based on the size and
1041 // alignment of the ivar.
1042
1043 // If the size of the ivar is not a power of two, give up. We don't
1044 // want to get into the business of doing compare-and-swaps.
1045 if (!IvarSize.isPowerOfTwo()) {
1046 Kind = CopyStruct;
1047 return;
1048 }
1049
1050 llvm::Triple::ArchType arch =
1051 CGM.getTarget().getTriple().getArch();
1052
1053 // Most architectures require memory to fit within a single cache
1054 // line, so the alignment has to be at least the size of the access.
1055 // Otherwise we have to grab a lock.
1056 if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
1057 Kind = CopyStruct;
1058 return;
1059 }
1060
1061 // If the ivar's size exceeds the architecture's maximum atomic
1062 // access size, we have to use CopyStruct.
1063 if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
1064 Kind = CopyStruct;
1065 return;
1066 }
1067
1068 // Otherwise, we can use native loads and stores.
1069 Kind = Native;
1070}
1071
1072/// Generate an Objective-C property getter function.
1073///
1074/// The given Decl must be an ObjCImplementationDecl. \@synthesize
1075/// is illegal within a category.
1077 const ObjCPropertyImplDecl *PID) {
1078 llvm::Constant *AtomicHelperFn =
1079 CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID);
1080 ObjCMethodDecl *OMD = PID->getGetterMethodDecl();
1081 assert(OMD && "Invalid call to generate getter (empty method)");
1083
1084 generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn);
1085
1086 FinishFunction(OMD->getEndLoc());
1087}
1088
1089static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
1090 const Expr *getter = propImpl->getGetterCXXConstructor();
1091 if (!getter) return true;
1092
1093 // Sema only makes only of these when the ivar has a C++ class type,
1094 // so the form is pretty constrained.
1095
1096 // If the property has a reference type, we might just be binding a
1097 // reference, in which case the result will be a gl-value. We should
1098 // treat this as a non-trivial operation.
1099 if (getter->isGLValue())
1100 return false;
1101
1102 // If we selected a trivial copy-constructor, we're okay.
1103 if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
1104 return (construct->getConstructor()->isTrivial());
1105
1106 // The constructor might require cleanups (in which case it's never
1107 // trivial).
1108 assert(isa<ExprWithCleanups>(getter));
1109 return false;
1110}
1111
1112/// emitCPPObjectAtomicGetterCall - Call the runtime function to
1113/// copy the ivar into the resturn slot.
1115 llvm::Value *returnAddr,
1116 ObjCIvarDecl *ivar,
1117 llvm::Constant *AtomicHelperFn) {
1118 // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar,
1119 // AtomicHelperFn);
1120 CallArgList args;
1121
1122 // The 1st argument is the return Slot.
1123 args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy);
1124
1125 // The 2nd argument is the address of the ivar.
1126 llvm::Value *ivarAddr =
1127 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
1128 .getPointer(CGF);
1129 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1130
1131 // Third argument is the helper function.
1132 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1133
1134 llvm::FunctionCallee copyCppAtomicObjectFn =
1136 CGCallee callee = CGCallee::forDirect(copyCppAtomicObjectFn);
1137 CGF.EmitCall(
1139 callee, ReturnValueSlot(), args);
1140}
1141
1142// emitCmdValueForGetterSetterBody - Handle emitting the load necessary for
1143// the `_cmd` selector argument for getter/setter bodies. For direct methods,
1144// this returns an undefined/poison value; this matches behavior prior to `_cmd`
1145// being removed from the direct method ABI as the getter/setter caller would
1146// never load one. For non-direct methods, this emits a load of the implicit
1147// `_cmd` storage.
1149 ObjCMethodDecl *MD) {
1150 if (MD->isDirectMethod()) {
1151 // Direct methods do not have a `_cmd` argument. Emit an undefined/poison
1152 // value. This will be passed to objc_getProperty/objc_setProperty, which
1153 // has not appeared bothered by the `_cmd` argument being undefined before.
1154 llvm::Type *selType = CGF.ConvertType(CGF.getContext().getObjCSelType());
1155 return llvm::PoisonValue::get(selType);
1156 }
1157
1158 return CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(MD->getCmdDecl()), "cmd");
1159}
1160
1161void
1163 const ObjCPropertyImplDecl *propImpl,
1164 const ObjCMethodDecl *GetterMethodDecl,
1165 llvm::Constant *AtomicHelperFn) {
1166
1167 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1168
1170 if (!AtomicHelperFn) {
1171 LValue Src =
1173 LValue Dst = MakeAddrLValue(ReturnValue, ivar->getType());
1175 } else {
1176 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1177 emitCPPObjectAtomicGetterCall(*this, ReturnValue.emitRawPointer(*this),
1178 ivar, AtomicHelperFn);
1179 }
1180 return;
1181 }
1182
1183 // If there's a non-trivial 'get' expression, we just have to emit that.
1184 if (!hasTrivialGetExpr(propImpl)) {
1185 if (!AtomicHelperFn) {
1187 propImpl->getGetterCXXConstructor(),
1188 /* NRVOCandidate=*/nullptr);
1189 EmitReturnStmt(*ret);
1190 }
1191 else {
1192 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1193 emitCPPObjectAtomicGetterCall(*this, ReturnValue.emitRawPointer(*this),
1194 ivar, AtomicHelperFn);
1195 }
1196 return;
1197 }
1198
1199 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
1200 QualType propType = prop->getType();
1201 ObjCMethodDecl *getterMethod = propImpl->getGetterMethodDecl();
1202
1203 // Pick an implementation strategy.
1204 PropertyImplStrategy strategy(CGM, propImpl);
1205 switch (strategy.getKind()) {
1206 case PropertyImplStrategy::Native: {
1207 // We don't need to do anything for a zero-size struct.
1208 if (strategy.getIvarSize().isZero())
1209 return;
1210
1212
1213 // Currently, all atomic accesses have to be through integer
1214 // types, so there's no point in trying to pick a prettier type.
1215 uint64_t ivarSize = getContext().toBits(strategy.getIvarSize());
1216 llvm::Type *bitcastType = llvm::Type::getIntNTy(getLLVMContext(), ivarSize);
1217
1218 // Perform an atomic load. This does not impose ordering constraints.
1219 Address ivarAddr = LV.getAddress();
1220 ivarAddr = ivarAddr.withElementType(bitcastType);
1221 llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
1222 load->setAtomic(llvm::AtomicOrdering::Unordered);
1223 llvm::Value *ivarVal = load;
1224 if (PointerAuthQualifier PAQ = ivar->getType().getPointerAuth()) {
1225 CGPointerAuthInfo SrcInfo = EmitPointerAuthInfo(PAQ, ivarAddr);
1227 CGM.getPointerAuthInfoForType(getterMethod->getReturnType());
1228 ivarVal = emitPointerAuthResign(ivarVal, ivar->getType(), SrcInfo,
1229 TargetInfo, /*isKnownNonNull=*/false);
1230 }
1231
1232 // Store that value into the return address. Doing this with a
1233 // bitcast is likely to produce some pretty ugly IR, but it's not
1234 // the *most* terrible thing in the world.
1235 llvm::Type *retTy = ConvertType(getterMethod->getReturnType());
1236 uint64_t retTySize = CGM.getDataLayout().getTypeSizeInBits(retTy);
1237 if (ivarSize > retTySize) {
1238 bitcastType = llvm::Type::getIntNTy(getLLVMContext(), retTySize);
1239 if (getterMethod->getReturnType()->hasBooleanRepresentation() &&
1240 CGM.getCodeGenOpts().isConvertingBoolWithCmp0())
1241 ivarVal = Builder.CreateICmpNE(
1242 ivarVal, llvm::Constant::getNullValue(ivarVal->getType()));
1243 else
1244 ivarVal = Builder.CreateTrunc(ivarVal, bitcastType);
1245 }
1246 Builder.CreateStore(ivarVal, ReturnValue.withElementType(bitcastType));
1247
1248 // Make sure we don't do an autorelease.
1249 AutoreleaseResult = false;
1250 return;
1251 }
1252
1253 case PropertyImplStrategy::GetSetProperty: {
1254 llvm::FunctionCallee getPropertyFn =
1255 CGM.getObjCRuntime().GetPropertyGetFunction();
1256
1257 if (ivar->getType().getPointerAuth()) {
1258 // This currently cannot be hit, but if we ever allow objc pointers
1259 // to be signed, this will become possible. Reaching here would require
1260 // a copy, weak, etc property backed by an authenticated pointer.
1261 CGM.ErrorUnsupported(propImpl,
1262 "Obj-C getter requiring pointer authentication");
1263 return;
1264 }
1265
1266 if (!getPropertyFn) {
1267 CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
1268 return;
1269 }
1270 CGCallee callee = CGCallee::forDirect(getPropertyFn);
1271
1272 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
1273 // FIXME: Can't this be simpler? This might even be worse than the
1274 // corresponding gcc code.
1275 llvm::Value *cmd = emitCmdValueForGetterSetterBody(*this, getterMethod);
1276 llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
1277 llvm::Value *ivarOffset =
1279
1280 CallArgList args;
1281 args.add(RValue::get(self), getContext().getObjCIdType());
1282 args.add(RValue::get(cmd), getContext().getObjCSelType());
1283 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1284 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1285 getContext().BoolTy);
1286
1287 // FIXME: We shouldn't need to get the function info here, the
1288 // runtime already should have computed it to build the function.
1289 llvm::CallBase *CallInstruction;
1290 RValue RV = EmitCall(getTypes().arrangeBuiltinFunctionCall(
1291 getContext().getObjCIdType(), args),
1292 callee, ReturnValueSlot(), args, &CallInstruction);
1293 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction))
1294 call->setTailCall();
1295
1296 // We need to fix the type here. Ivars with copy & retain are
1297 // always objects so we don't need to worry about complex or
1298 // aggregates.
1299 RV = RValue::get(Builder.CreateBitCast(
1300 RV.getScalarVal(),
1301 getTypes().ConvertType(getterMethod->getReturnType())));
1302
1303 EmitReturnOfRValue(RV, propType);
1304
1305 // objc_getProperty does an autorelease, so we should suppress ours.
1306 AutoreleaseResult = false;
1307
1308 return;
1309 }
1310
1311 case PropertyImplStrategy::CopyStruct:
1312 emitStructGetterCall(*this, ivar, strategy.isAtomic(),
1313 strategy.hasStrongMember());
1314 return;
1315
1316 case PropertyImplStrategy::Expression:
1317 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1319
1320 QualType ivarType = ivar->getType();
1321 auto EvaluationKind = getEvaluationKind(ivarType);
1322 assert(!ivarType.getPointerAuth() || EvaluationKind == TEK_Scalar);
1323 switch (EvaluationKind) {
1324 case TEK_Complex: {
1327 /*init*/ true);
1328 return;
1329 }
1330 case TEK_Aggregate: {
1331 // The return value slot is guaranteed to not be aliased, but
1332 // that's not necessarily the same as "on the stack", so
1333 // we still potentially need objc_memmove_collectable.
1334 EmitAggregateCopy(/* Dest= */ MakeAddrLValue(ReturnValue, ivarType),
1335 /* Src= */ LV, ivarType, getOverlapForReturnValue());
1336 return;
1337 }
1338 case TEK_Scalar: {
1339 llvm::Value *value;
1340 if (propType->isReferenceType()) {
1341 if (ivarType.getPointerAuth()) {
1342 CGM.ErrorUnsupported(propImpl,
1343 "Obj-C getter for authenticated reference type");
1344 return;
1345 }
1346 value = LV.getAddress().emitRawPointer(*this);
1347 } else {
1348 // We want to load and autoreleaseReturnValue ARC __weak ivars.
1350 if (getLangOpts().ObjCAutoRefCount) {
1351 value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
1352 } else {
1353 value = EmitARCLoadWeak(LV.getAddress());
1354 }
1355
1356 // Otherwise we want to do a simple load, suppressing the
1357 // final autorelease.
1358 } else {
1359 if (PointerAuthQualifier PAQ = ivar->getType().getPointerAuth()) {
1360 Address ivarAddr = LV.getAddress();
1361 llvm::LoadInst *LoadInst = Builder.CreateLoad(ivarAddr, "load");
1362 llvm::Value *Load = LoadInst;
1363 auto SrcInfo = EmitPointerAuthInfo(PAQ, ivarAddr);
1364 auto TargetInfo =
1365 CGM.getPointerAuthInfoForType(getterMethod->getReturnType());
1366 Load = emitPointerAuthResign(Load, ivarType, SrcInfo, TargetInfo,
1367 /*isKnownNonNull=*/false);
1368 value = Load;
1369 } else
1371
1372 AutoreleaseResult = false;
1373 }
1374
1375 value = Builder.CreateBitCast(
1376 value, ConvertType(GetterMethodDecl->getReturnType()));
1377 }
1378
1379 EmitReturnOfRValue(RValue::get(value), propType);
1380 return;
1381 }
1382 }
1383 llvm_unreachable("bad evaluation kind");
1384 }
1385
1386 }
1387 llvm_unreachable("bad @property implementation strategy!");
1388}
1389
1390/// emitStructSetterCall - Call the runtime function to store the value
1391/// from the first formal parameter into the given ivar.
1393 ObjCIvarDecl *ivar) {
1394 // objc_copyStruct (&structIvar, &Arg,
1395 // sizeof (struct something), true, false);
1396 CallArgList args;
1397
1398 // The first argument is the address of the ivar.
1399 llvm::Value *ivarAddr =
1400 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
1401 .getPointer(CGF);
1402 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1403 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1404
1405 // The second argument is the address of the parameter variable.
1406 ParmVarDecl *argVar = *OMD->param_begin();
1407 DeclRefExpr argRef(CGF.getContext(), argVar, false,
1409 SourceLocation());
1410 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF);
1411 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1412
1413 // The third argument is the sizeof the type.
1414 llvm::Value *size =
1415 CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
1416 args.add(RValue::get(size), CGF.getContext().getSizeType());
1417
1418 // The fourth argument is the 'isAtomic' flag.
1419 args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
1420
1421 // The fifth argument is the 'hasStrong' flag.
1422 // FIXME: should this really always be false?
1423 args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
1424
1425 llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
1426 CGCallee callee = CGCallee::forDirect(fn);
1427 CGF.EmitCall(
1429 callee, ReturnValueSlot(), args);
1430}
1431
1432/// emitCPPObjectAtomicSetterCall - Call the runtime function to store
1433/// the value from the first formal parameter into the given ivar, using
1434/// the Cpp API for atomic Cpp objects with non-trivial copy assignment.
1436 ObjCMethodDecl *OMD,
1437 ObjCIvarDecl *ivar,
1438 llvm::Constant *AtomicHelperFn) {
1439 // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg,
1440 // AtomicHelperFn);
1441 CallArgList args;
1442
1443 // The first argument is the address of the ivar.
1444 llvm::Value *ivarAddr =
1445 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
1446 .getPointer(CGF);
1447 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1448
1449 // The second argument is the address of the parameter variable.
1450 ParmVarDecl *argVar = *OMD->param_begin();
1451 DeclRefExpr argRef(CGF.getContext(), argVar, false,
1453 SourceLocation());
1454 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF);
1455 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1456
1457 // Third argument is the helper function.
1458 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1459
1460 llvm::FunctionCallee fn =
1462 CGCallee callee = CGCallee::forDirect(fn);
1463 CGF.EmitCall(
1465 callee, ReturnValueSlot(), args);
1466}
1467
1468
1470 Expr *setter = PID->getSetterCXXAssignment();
1471 if (!setter) return true;
1472
1473 // Sema only makes only of these when the ivar has a C++ class type,
1474 // so the form is pretty constrained.
1475
1476 // An operator call is trivial if the function it calls is trivial.
1477 // This also implies that there's nothing non-trivial going on with
1478 // the arguments, because operator= can only be trivial if it's a
1479 // synthesized assignment operator and therefore both parameters are
1480 // references.
1481 if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
1482 if (const FunctionDecl *callee
1483 = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
1484 if (callee->isTrivial())
1485 return true;
1486 return false;
1487 }
1488
1489 assert(isa<ExprWithCleanups>(setter));
1490 return false;
1491}
1492
1494 if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
1495 return false;
1497}
1498
1499void
1501 const ObjCPropertyImplDecl *propImpl,
1502 llvm::Constant *AtomicHelperFn) {
1503 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1504 ObjCMethodDecl *setterMethod = propImpl->getSetterMethodDecl();
1505
1507 ParmVarDecl *PVD = *setterMethod->param_begin();
1508 if (!AtomicHelperFn) {
1509 // Call the move assignment operator instead of calling the copy
1510 // assignment operator and destructor.
1512 /*quals*/ 0);
1513 LValue Src = MakeAddrLValue(GetAddrOfLocalVar(PVD), ivar->getType());
1515 } else {
1516 // If atomic, assignment is called via a locking api.
1517 emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, AtomicHelperFn);
1518 }
1519 // Decativate the destructor for the setter parameter.
1520 DeactivateCleanupBlock(CalleeDestructedParamCleanups[PVD], AllocaInsertPt);
1521 return;
1522 }
1523
1524 // Just use the setter expression if Sema gave us one and it's
1525 // non-trivial.
1526 if (!hasTrivialSetExpr(propImpl)) {
1527 if (!AtomicHelperFn)
1528 // If non-atomic, assignment is called directly.
1529 EmitStmt(propImpl->getSetterCXXAssignment());
1530 else
1531 // If atomic, assignment is called via a locking api.
1532 emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar,
1533 AtomicHelperFn);
1534 return;
1535 }
1536
1537 PropertyImplStrategy strategy(CGM, propImpl);
1538 switch (strategy.getKind()) {
1539 case PropertyImplStrategy::Native: {
1540 // We don't need to do anything for a zero-size struct.
1541 if (strategy.getIvarSize().isZero())
1542 return;
1543
1544 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1545
1546 LValue ivarLValue =
1547 EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
1548 Address ivarAddr = ivarLValue.getAddress();
1549
1550 // Currently, all atomic accesses have to be through integer
1551 // types, so there's no point in trying to pick a prettier type.
1552 llvm::Type *castType = llvm::Type::getIntNTy(
1553 getLLVMContext(), getContext().toBits(strategy.getIvarSize()));
1554
1555 // Cast both arguments to the chosen operation type.
1556 argAddr = argAddr.withElementType(castType);
1557 ivarAddr = ivarAddr.withElementType(castType);
1558
1559 llvm::Value *load = Builder.CreateLoad(argAddr);
1560
1561 if (PointerAuthQualifier PAQ = ivar->getType().getPointerAuth()) {
1562 QualType PropertyType = propImpl->getPropertyDecl()->getType();
1563 CGPointerAuthInfo SrcInfo = CGM.getPointerAuthInfoForType(PropertyType);
1565 load = emitPointerAuthResign(load, ivar->getType(), SrcInfo, TargetInfo,
1566 /*isKnownNonNull=*/false);
1567 }
1568
1569 // Perform an atomic store. There are no memory ordering requirements.
1570 llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
1571 store->setAtomic(llvm::AtomicOrdering::Unordered);
1572 return;
1573 }
1574
1575 case PropertyImplStrategy::GetSetProperty:
1576 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1577
1578 llvm::FunctionCallee setOptimizedPropertyFn = nullptr;
1579 llvm::FunctionCallee setPropertyFn = nullptr;
1580 if (UseOptimizedSetter(CGM)) {
1581 // 10.8 and iOS 6.0 code and GC is off
1582 setOptimizedPropertyFn =
1583 CGM.getObjCRuntime().GetOptimizedPropertySetFunction(
1584 strategy.isAtomic(), strategy.isCopy());
1585 if (!setOptimizedPropertyFn) {
1586 CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI");
1587 return;
1588 }
1589 }
1590 else {
1591 setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction();
1592 if (!setPropertyFn) {
1593 CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
1594 return;
1595 }
1596 }
1597
1598 // Emit objc_setProperty((id) self, _cmd, offset, arg,
1599 // <is-atomic>, <is-copy>).
1600 llvm::Value *cmd = emitCmdValueForGetterSetterBody(*this, setterMethod);
1601 llvm::Value *self =
1602 Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
1603 llvm::Value *ivarOffset =
1605 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1606 llvm::Value *arg = Builder.CreateLoad(argAddr, "arg");
1607 arg = Builder.CreateBitCast(arg, VoidPtrTy);
1608
1609 CallArgList args;
1610 args.add(RValue::get(self), getContext().getObjCIdType());
1611 args.add(RValue::get(cmd), getContext().getObjCSelType());
1612 if (setOptimizedPropertyFn) {
1613 args.add(RValue::get(arg), getContext().getObjCIdType());
1614 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1615 CGCallee callee = CGCallee::forDirect(setOptimizedPropertyFn);
1616 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1617 callee, ReturnValueSlot(), args);
1618 } else {
1619 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1620 args.add(RValue::get(arg), getContext().getObjCIdType());
1621 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1622 getContext().BoolTy);
1623 args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
1624 getContext().BoolTy);
1625 // FIXME: We shouldn't need to get the function info here, the runtime
1626 // already should have computed it to build the function.
1627 CGCallee callee = CGCallee::forDirect(setPropertyFn);
1628 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1629 callee, ReturnValueSlot(), args);
1630 }
1631
1632 return;
1633 }
1634
1635 case PropertyImplStrategy::CopyStruct:
1636 emitStructSetterCall(*this, setterMethod, ivar);
1637 return;
1638
1639 case PropertyImplStrategy::Expression:
1640 break;
1641 }
1642
1643 // Otherwise, fake up some ASTs and emit a normal assignment.
1644 ValueDecl *selfDecl = setterMethod->getSelfDecl();
1645 DeclRefExpr self(getContext(), selfDecl, false, selfDecl->getType(),
1648 CK_LValueToRValue, &self, VK_PRValue,
1650 ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
1652 &selfLoad, true, true);
1653
1654 ParmVarDecl *argDecl = *setterMethod->param_begin();
1655 QualType argType = argDecl->getType().getNonReferenceType();
1656 DeclRefExpr arg(getContext(), argDecl, false, argType, VK_LValue,
1657 SourceLocation());
1659 argType.getUnqualifiedType(), CK_LValueToRValue,
1660 &arg, VK_PRValue, FPOptionsOverride());
1661
1662 // The property type can differ from the ivar type in some situations with
1663 // Objective-C pointer types, we can always bit cast the RHS in these cases.
1664 // The following absurdity is just to ensure well-formed IR.
1665 CastKind argCK = CK_NoOp;
1666 if (ivarRef.getType()->isObjCObjectPointerType()) {
1667 if (argLoad.getType()->isObjCObjectPointerType())
1668 argCK = CK_BitCast;
1669 else if (argLoad.getType()->isBlockPointerType())
1670 argCK = CK_BlockPointerToObjCPointerCast;
1671 else
1672 argCK = CK_CPointerToObjCPointerCast;
1673 } else if (ivarRef.getType()->isBlockPointerType()) {
1674 if (argLoad.getType()->isBlockPointerType())
1675 argCK = CK_BitCast;
1676 else
1677 argCK = CK_AnyPointerToBlockPointerCast;
1678 } else if (ivarRef.getType()->isPointerType()) {
1679 argCK = CK_BitCast;
1680 } else if (argLoad.getType()->isAtomicType() &&
1681 !ivarRef.getType()->isAtomicType()) {
1682 argCK = CK_AtomicToNonAtomic;
1683 } else if (!argLoad.getType()->isAtomicType() &&
1684 ivarRef.getType()->isAtomicType()) {
1685 argCK = CK_NonAtomicToAtomic;
1686 }
1687 ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, ivarRef.getType(), argCK,
1688 &argLoad, VK_PRValue, FPOptionsOverride());
1689 Expr *finalArg = &argLoad;
1690 if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
1691 argLoad.getType()))
1692 finalArg = &argCast;
1693
1695 getContext(), &ivarRef, finalArg, BO_Assign, ivarRef.getType(),
1697 EmitStmt(assign);
1698}
1699
1700/// Generate an Objective-C property setter function.
1701///
1702/// The given Decl must be an ObjCImplementationDecl. \@synthesize
1703/// is illegal within a category.
1705 const ObjCPropertyImplDecl *PID) {
1706 llvm::Constant *AtomicHelperFn =
1707 CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID);
1708 ObjCMethodDecl *OMD = PID->getSetterMethodDecl();
1709 assert(OMD && "Invalid call to generate setter (empty method)");
1711
1712 generateObjCSetterBody(IMP, PID, AtomicHelperFn);
1713
1714 FinishFunction(OMD->getEndLoc());
1715}
1716
1717namespace {
1718 struct DestroyIvar final : EHScopeStack::Cleanup {
1719 private:
1720 llvm::Value *addr;
1721 const ObjCIvarDecl *ivar;
1722 CodeGenFunction::Destroyer *destroyer;
1723 bool useEHCleanupForArray;
1724 public:
1725 DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
1726 CodeGenFunction::Destroyer *destroyer,
1727 bool useEHCleanupForArray)
1728 : addr(addr), ivar(ivar), destroyer(destroyer),
1729 useEHCleanupForArray(useEHCleanupForArray) {}
1730
1731 void Emit(CodeGenFunction &CGF, Flags flags) override {
1732 LValue lvalue
1733 = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
1734 CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
1735 flags.isForNormalCleanup() && useEHCleanupForArray);
1736 }
1737 };
1738}
1739
1740/// Like CodeGenFunction::destroyARCStrong, but do it with a call.
1742 Address addr,
1743 QualType type) {
1744 llvm::Value *null = getNullForVariable(addr);
1745 CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
1746}
1747
1749 ObjCImplementationDecl *impl) {
1751
1752 llvm::Value *self = CGF.LoadObjCSelf();
1753
1754 const ObjCInterfaceDecl *iface = impl->getClassInterface();
1755 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
1756 ivar; ivar = ivar->getNextIvar()) {
1757 QualType type = ivar->getType();
1758
1759 // Check whether the ivar is a destructible type.
1760 QualType::DestructionKind dtorKind = type.isDestructedType();
1761 if (!dtorKind) continue;
1762
1763 CodeGenFunction::Destroyer *destroyer = nullptr;
1764
1765 // Use a call to objc_storeStrong to destroy strong ivars, for the
1766 // general benefit of the tools.
1767 if (dtorKind == QualType::DK_objc_strong_lifetime) {
1768 destroyer = destroyARCStrongWithStore;
1769
1770 // Otherwise use the default for the destruction kind.
1771 } else {
1772 destroyer = CGF.getDestroyer(dtorKind);
1773 }
1774
1775 CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
1776
1777 CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
1778 cleanupKind & EHCleanup);
1779 }
1780
1781 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1782}
1783
1785 ObjCMethodDecl *MD,
1786 bool ctor) {
1787 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
1789
1790 // Emit .cxx_construct.
1791 if (ctor) {
1792 // Suppress the final autorelease in ARC.
1793 AutoreleaseResult = false;
1794
1795 for (const auto *IvarInit : IMP->inits()) {
1796 FieldDecl *Field = IvarInit->getAnyMember();
1797 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1799 LoadObjCSelf(), Ivar, 0);
1800 EmitAggExpr(IvarInit->getInit(),
1805 }
1806 // constructor returns 'self'.
1807 CodeGenTypes &Types = CGM.getTypes();
1808 QualType IdTy(CGM.getContext().getObjCIdType());
1809 llvm::Value *SelfAsId =
1810 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1811 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1812
1813 // Emit .cxx_destruct.
1814 } else {
1815 emitCXXDestructMethod(*this, IMP);
1816 }
1818}
1819
1821 VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
1823 /*is enclosing local*/ (CurFuncDecl != CurCodeDecl),
1824 Self->getType(), VK_LValue, SourceLocation());
1826}
1827
1830 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
1832 getContext().getCanonicalType(selfDecl->getType()));
1833 return PTy->getPointeeType();
1834}
1835
1837 llvm::FunctionCallee EnumerationMutationFnPtr =
1838 CGM.getObjCRuntime().EnumerationMutationFunction();
1839 if (!EnumerationMutationFnPtr) {
1840 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1841 return;
1842 }
1843 CGCallee EnumerationMutationFn =
1844 CGCallee::forDirect(EnumerationMutationFnPtr);
1845
1846 CGDebugInfo *DI = getDebugInfo();
1847 if (DI)
1849
1850 RunCleanupsScope ForScope(*this);
1851
1852 // The local variable comes into scope immediately.
1854 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
1855 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
1856
1857 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
1858
1859 // Fast enumeration state.
1860 QualType StateTy = CGM.getObjCFastEnumerationStateType();
1861 Address StatePtr = CreateMemTemp(StateTy, "state.ptr");
1862 EmitNullInitialization(StatePtr, StateTy);
1863
1864 // Number of elements in the items array.
1865 static const unsigned NumItems = 16;
1866
1867 // Fetch the countByEnumeratingWithState:objects:count: selector.
1868 const IdentifierInfo *II[] = {
1869 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1870 &CGM.getContext().Idents.get("objects"),
1871 &CGM.getContext().Idents.get("count")};
1872 Selector FastEnumSel =
1873 CGM.getContext().Selectors.getSelector(std::size(II), &II[0]);
1874
1876 getContext().getObjCIdType(), llvm::APInt(32, NumItems), nullptr,
1878 Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
1879
1880 // Emit the collection pointer. In ARC, we do a retain.
1881 llvm::Value *Collection;
1882 if (getLangOpts().ObjCAutoRefCount) {
1883 Collection = EmitARCRetainScalarExpr(S.getCollection());
1884
1885 // Enter a cleanup to do the release.
1886 EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1887 } else {
1888 Collection = EmitScalarExpr(S.getCollection());
1889 }
1890
1891 // The 'continue' label needs to appear within the cleanup for the
1892 // collection object.
1893 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
1894
1895 // Send it our message:
1896 CallArgList Args;
1897
1898 // The first argument is a temporary of the enumeration-state type.
1899 Args.add(RValue::get(StatePtr, *this), getContext().getPointerType(StateTy));
1900
1901 // The second argument is a temporary array with space for NumItems
1902 // pointers. We'll actually be loading elements from the array
1903 // pointer written into the control state; this buffer is so that
1904 // collections that *aren't* backed by arrays can still queue up
1905 // batches of elements.
1906 Args.add(RValue::get(ItemsPtr, *this), getContext().getPointerType(ItemsTy));
1907
1908 // The third argument is the capacity of that temporary array.
1909 llvm::Type *NSUIntegerTy = ConvertType(getContext().getNSUIntegerType());
1910 llvm::Constant *Count = llvm::ConstantInt::get(NSUIntegerTy, NumItems);
1911 Args.add(RValue::get(Count), getContext().getNSUIntegerType());
1912
1913 // Start the enumeration.
1914 RValue CountRV =
1915 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1916 getContext().getNSUIntegerType(),
1917 FastEnumSel, Collection, Args);
1918
1919 // The initial number of objects that were returned in the buffer.
1920 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
1921
1922 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1923 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1924
1925 llvm::Value *zero = llvm::Constant::getNullValue(NSUIntegerTy);
1926
1927 // If the limit pointer was zero to begin with, the collection is
1928 // empty; skip all this. Set the branch weight assuming this has the same
1929 // probability of exiting the loop as any other loop exit.
1930 uint64_t EntryCount = getCurrentProfileCount();
1931 Builder.CreateCondBr(
1932 Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB,
1933 LoopInitBB,
1934 createProfileWeights(EntryCount, getProfileCount(S.getBody())));
1935
1936 // Otherwise, initialize the loop.
1937 EmitBlock(LoopInitBB);
1938
1939 // Save the initial mutations value. This is the value at an
1940 // address that was written into the state object by
1941 // countByEnumeratingWithState:objects:count:.
1942 Address StateMutationsPtrPtr =
1943 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
1944 llvm::Value *StateMutationsPtr
1945 = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1946
1947 llvm::Type *UnsignedLongTy = ConvertType(getContext().UnsignedLongTy);
1948 llvm::Value *initialMutations =
1949 Builder.CreateAlignedLoad(UnsignedLongTy, StateMutationsPtr,
1950 getPointerAlign(), "forcoll.initial-mutations");
1951
1952 // Start looping. This is the point we return to whenever we have a
1953 // fresh, non-empty batch of objects.
1954 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1955 EmitBlock(LoopBodyBB);
1956
1957 // The current index into the buffer.
1958 llvm::PHINode *index = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.index");
1959 index->addIncoming(zero, LoopInitBB);
1960
1961 // The current buffer size.
1962 llvm::PHINode *count = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.count");
1963 count->addIncoming(initialBufferLimit, LoopInitBB);
1964
1966
1967 // Check whether the mutations value has changed from where it was
1968 // at start. StateMutationsPtr should actually be invariant between
1969 // refreshes.
1970 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1971 llvm::Value *currentMutations
1972 = Builder.CreateAlignedLoad(UnsignedLongTy, StateMutationsPtr,
1973 getPointerAlign(), "statemutations");
1974
1975 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1976 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
1977
1978 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1979 WasNotMutatedBB, WasMutatedBB);
1980
1981 // If so, call the enumeration-mutation function.
1982 EmitBlock(WasMutatedBB);
1983 llvm::Type *ObjCIdType = ConvertType(getContext().getObjCIdType());
1984 llvm::Value *V =
1985 Builder.CreateBitCast(Collection, ObjCIdType);
1986 CallArgList Args2;
1987 Args2.add(RValue::get(V), getContext().getObjCIdType());
1988 // FIXME: We shouldn't need to get the function info here, the runtime already
1989 // should have computed it to build the function.
1990 EmitCall(
1991 CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2),
1992 EnumerationMutationFn, ReturnValueSlot(), Args2);
1993
1994 // Otherwise, or if the mutation function returns, just continue.
1995 EmitBlock(WasNotMutatedBB);
1996
1997 // Initialize the element variable.
1998 RunCleanupsScope elementVariableScope(*this);
1999 bool elementIsVariable;
2000 LValue elementLValue;
2001 QualType elementType;
2002 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
2003 // Initialize the variable, in case it's a __block variable or something.
2004 EmitAutoVarInit(variable);
2005
2006 const VarDecl *D = cast<VarDecl>(SD->getSingleDecl());
2007 DeclRefExpr tempDRE(getContext(), const_cast<VarDecl *>(D), false,
2009 elementLValue = EmitLValue(&tempDRE);
2010 elementType = D->getType();
2011 elementIsVariable = true;
2012
2013 if (D->isARCPseudoStrong())
2015 } else {
2016 elementLValue = LValue(); // suppress warning
2017 elementType = cast<Expr>(S.getElement())->getType();
2018 elementIsVariable = false;
2019 }
2020 llvm::Type *convertedElementType = ConvertType(elementType);
2021
2022 // Fetch the buffer out of the enumeration state.
2023 // TODO: this pointer should actually be invariant between
2024 // refreshes, which would help us do certain loop optimizations.
2025 Address StateItemsPtr =
2026 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
2027 llvm::Value *EnumStateItems =
2028 Builder.CreateLoad(StateItemsPtr, "stateitems");
2029
2030 // Fetch the value at the current index from the buffer.
2031 llvm::Value *CurrentItemPtr = Builder.CreateInBoundsGEP(
2032 ObjCIdType, EnumStateItems, index, "currentitem.ptr");
2033 llvm::Value *CurrentItem =
2034 Builder.CreateAlignedLoad(ObjCIdType, CurrentItemPtr, getPointerAlign());
2035
2036 if (SanOpts.has(SanitizerKind::ObjCCast)) {
2037 // Before using an item from the collection, check that the implicit cast
2038 // from id to the element type is valid. This is done with instrumentation
2039 // roughly corresponding to:
2040 //
2041 // if (![item isKindOfClass:expectedCls]) { /* emit diagnostic */ }
2042 const ObjCObjectPointerType *ObjPtrTy =
2043 elementType->getAsObjCInterfacePointerType();
2044 const ObjCInterfaceType *InterfaceTy =
2045 ObjPtrTy ? ObjPtrTy->getInterfaceType() : nullptr;
2046 if (InterfaceTy) {
2047 auto CheckOrdinal = SanitizerKind::SO_ObjCCast;
2048 auto CheckHandler = SanitizerHandler::InvalidObjCCast;
2049 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
2050 auto &C = CGM.getContext();
2051 assert(InterfaceTy->getDecl() && "No decl for ObjC interface type");
2052 Selector IsKindOfClassSel = GetUnarySelector("isKindOfClass", C);
2053 CallArgList IsKindOfClassArgs;
2054 llvm::Value *Cls =
2055 CGM.getObjCRuntime().GetClass(*this, InterfaceTy->getDecl());
2056 IsKindOfClassArgs.add(RValue::get(Cls), C.getObjCClassType());
2057 llvm::Value *IsClass =
2058 CGM.getObjCRuntime()
2059 .GenerateMessageSend(*this, ReturnValueSlot(), C.BoolTy,
2060 IsKindOfClassSel, CurrentItem,
2061 IsKindOfClassArgs)
2062 .getScalarVal();
2063 llvm::Constant *StaticData[] = {
2065 EmitCheckTypeDescriptor(QualType(InterfaceTy, 0))};
2066 EmitCheck({{IsClass, CheckOrdinal}}, CheckHandler,
2067 ArrayRef<llvm::Constant *>(StaticData), CurrentItem);
2068 }
2069 }
2070
2071 // Cast that value to the right type.
2072 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
2073 "currentitem");
2074
2075 // Make sure we have an l-value. Yes, this gets evaluated every
2076 // time through the loop.
2077 if (!elementIsVariable) {
2078 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
2079 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
2080 } else {
2081 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue,
2082 /*isInit*/ true);
2083 }
2084
2085 // If we do have an element variable, this assignment is the end of
2086 // its initialization.
2087 if (elementIsVariable)
2088 EmitAutoVarCleanups(variable);
2089
2090 // Perform the loop body, setting up break and continue labels.
2091 BreakContinueStack.push_back(BreakContinue(S, LoopEnd, AfterBody));
2092 {
2093 RunCleanupsScope Scope(*this);
2094 EmitStmt(S.getBody());
2095 }
2096 BreakContinueStack.pop_back();
2097
2098 // Destroy the element variable now.
2099 elementVariableScope.ForceCleanup();
2100
2101 // Check whether there are more elements.
2102 EmitBlock(AfterBody.getBlock());
2103
2104 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
2105
2106 // First we check in the local buffer.
2107 llvm::Value *indexPlusOne =
2108 Builder.CreateNUWAdd(index, llvm::ConstantInt::get(NSUIntegerTy, 1));
2109
2110 // If we haven't overrun the buffer yet, we can continue.
2111 // Set the branch weights based on the simplifying assumption that this is
2112 // like a while-loop, i.e., ignoring that the false branch fetches more
2113 // elements and then returns to the loop.
2114 Builder.CreateCondBr(
2115 Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB,
2116 createProfileWeights(getProfileCount(S.getBody()), EntryCount));
2117
2118 index->addIncoming(indexPlusOne, AfterBody.getBlock());
2119 count->addIncoming(count, AfterBody.getBlock());
2120
2121 // Otherwise, we have to fetch more elements.
2122 EmitBlock(FetchMoreBB);
2123
2124 CountRV =
2125 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2126 getContext().getNSUIntegerType(),
2127 FastEnumSel, Collection, Args);
2128
2129 // If we got a zero count, we're done.
2130 llvm::Value *refetchCount = CountRV.getScalarVal();
2131
2132 // (note that the message send might split FetchMoreBB)
2133 index->addIncoming(zero, Builder.GetInsertBlock());
2134 count->addIncoming(refetchCount, Builder.GetInsertBlock());
2135
2136 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
2137 EmptyBB, LoopBodyBB);
2138
2139 // No more elements.
2140 EmitBlock(EmptyBB);
2141
2142 if (!elementIsVariable) {
2143 // If the element was not a declaration, set it to be null.
2144
2145 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
2146 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
2147 EmitStoreThroughLValue(RValue::get(null), elementLValue);
2148 }
2149
2150 if (DI)
2152
2153 ForScope.ForceCleanup();
2154 EmitBlock(LoopEnd.getBlock());
2155}
2156
2158 CGM.getObjCRuntime().EmitTryStmt(*this, S);
2159}
2160
2162 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
2163}
2164
2166 const ObjCAtSynchronizedStmt &S) {
2167 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
2168}
2169
2170namespace {
2171 struct CallObjCRelease final : EHScopeStack::Cleanup {
2172 CallObjCRelease(llvm::Value *object) : object(object) {}
2173 llvm::Value *object;
2174
2175 void Emit(CodeGenFunction &CGF, Flags flags) override {
2176 // Releases at the end of the full-expression are imprecise.
2178 }
2179 };
2180}
2181
2182/// Produce the code for a CK_ARCConsumeObject. Does a primitive
2183/// release at the end of the full-expression.
2185 llvm::Value *object) {
2186 // If we're in a conditional branch, we need to make the cleanup
2187 // conditional.
2189 return object;
2190}
2191
2193 llvm::Value *value) {
2194 return EmitARCRetainAutorelease(type, value);
2195}
2196
2197/// Given a number of pointers, inform the optimizer that they're
2198/// being intrinsically used up until this point in the program.
2200 llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_use;
2201 if (!fn)
2202 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_use);
2203
2204 // This isn't really a "runtime" function, but as an intrinsic it
2205 // doesn't really matter as long as we align things up.
2206 EmitNounwindRuntimeCall(fn, values);
2207}
2208
2209/// Emit a call to "clang.arc.noop.use", which consumes the result of a call
2210/// that has operand bundle "clang.arc.attachedcall".
2212 llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_noop_use;
2213 if (!fn)
2214 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_noop_use);
2215 EmitNounwindRuntimeCall(fn, values);
2216}
2217
2218static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, llvm::Value *RTF) {
2219 if (auto *F = dyn_cast<llvm::Function>(RTF)) {
2220 // If the target runtime doesn't naturally support ARC, emit weak
2221 // references to the runtime support library. We don't really
2222 // permit this to fail, but we need a particular relocation style.
2223 if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC() &&
2224 !CGM.getTriple().isOSBinFormatCOFF()) {
2225 F->setLinkage(llvm::Function::ExternalWeakLinkage);
2226 }
2227 }
2228}
2229
2231 llvm::FunctionCallee RTF) {
2232 setARCRuntimeFunctionLinkage(CGM, RTF.getCallee());
2233}
2234
2235static llvm::Function *getARCIntrinsic(llvm::Intrinsic::ID IntID,
2236 CodeGenModule &CGM) {
2237 llvm::Function *fn = CGM.getIntrinsic(IntID);
2239 return fn;
2240}
2241
2242/// Perform an operation having the signature
2243/// i8* (i8*)
2244/// where a null input causes a no-op and returns null.
2245static llvm::Value *emitARCValueOperation(
2246 CodeGenFunction &CGF, llvm::Value *value, llvm::Type *returnType,
2247 llvm::Function *&fn, llvm::Intrinsic::ID IntID,
2248 llvm::CallInst::TailCallKind tailKind = llvm::CallInst::TCK_None) {
2250 return value;
2251
2252 if (!fn)
2253 fn = getARCIntrinsic(IntID, CGF.CGM);
2254
2255 // Cast the argument to 'id'.
2256 llvm::Type *origType = returnType ? returnType : value->getType();
2257 value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
2258
2259 // Call the function.
2260 llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
2261 call->setTailCallKind(tailKind);
2262
2263 // Cast the result back to the original type.
2264 return CGF.Builder.CreateBitCast(call, origType);
2265}
2266
2267/// Perform an operation having the following signature:
2268/// i8* (i8**)
2269static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, Address addr,
2270 llvm::Function *&fn,
2271 llvm::Intrinsic::ID IntID) {
2272 if (!fn)
2273 fn = getARCIntrinsic(IntID, CGF.CGM);
2274
2275 return CGF.EmitNounwindRuntimeCall(fn, addr.emitRawPointer(CGF));
2276}
2277
2278/// Perform an operation having the following signature:
2279/// i8* (i8**, i8*)
2280static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, Address addr,
2281 llvm::Value *value,
2282 llvm::Function *&fn,
2283 llvm::Intrinsic::ID IntID,
2284 bool ignored) {
2285 assert(addr.getElementType() == value->getType());
2286
2287 if (!fn)
2288 fn = getARCIntrinsic(IntID, CGF.CGM);
2289
2290 llvm::Type *origType = value->getType();
2291
2292 llvm::Value *args[] = {
2293 CGF.Builder.CreateBitCast(addr.emitRawPointer(CGF), CGF.Int8PtrPtrTy),
2294 CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)};
2295 llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
2296
2297 if (ignored) return nullptr;
2298
2299 return CGF.Builder.CreateBitCast(result, origType);
2300}
2301
2302/// Perform an operation having the following signature:
2303/// void (i8**, i8**)
2305 llvm::Function *&fn,
2306 llvm::Intrinsic::ID IntID) {
2307 assert(dst.getType() == src.getType());
2308
2309 if (!fn)
2310 fn = getARCIntrinsic(IntID, CGF.CGM);
2311
2312 llvm::Value *args[] = {
2313 CGF.Builder.CreateBitCast(dst.emitRawPointer(CGF), CGF.Int8PtrPtrTy),
2314 CGF.Builder.CreateBitCast(src.emitRawPointer(CGF), CGF.Int8PtrPtrTy)};
2315 CGF.EmitNounwindRuntimeCall(fn, args);
2316}
2317
2318/// Perform an operation having the signature
2319/// i8* (i8*)
2320/// where a null input causes a no-op and returns null.
2322 llvm::Value *value,
2323 llvm::Type *returnType,
2324 llvm::FunctionCallee &fn,
2325 StringRef fnName) {
2327 return value;
2328
2329 if (!fn) {
2330 llvm::FunctionType *fnType =
2331 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
2332 fn = CGF.CGM.CreateRuntimeFunction(fnType, fnName);
2333
2334 // We have Native ARC, so set nonlazybind attribute for performance
2335 if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee()))
2336 if (fnName == "objc_retain")
2337 f->addFnAttr(llvm::Attribute::NonLazyBind);
2338 }
2339
2340 // Cast the argument to 'id'.
2341 llvm::Type *origType = returnType ? returnType : value->getType();
2342 value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
2343
2344 // Call the function.
2345 llvm::CallBase *Inst = CGF.EmitCallOrInvoke(fn, value);
2346
2347 // Mark calls to objc_autorelease as tail on the assumption that methods
2348 // overriding autorelease do not touch anything on the stack.
2349 if (fnName == "objc_autorelease")
2350 if (auto *Call = dyn_cast<llvm::CallInst>(Inst))
2351 Call->setTailCall();
2352
2353 // Cast the result back to the original type.
2354 return CGF.Builder.CreateBitCast(Inst, origType);
2355}
2356
2357/// Produce the code to do a retain. Based on the type, calls one of:
2358/// call i8* \@objc_retain(i8* %value)
2359/// call i8* \@objc_retainBlock(i8* %value)
2360llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
2361 if (type->isBlockPointerType())
2362 return EmitARCRetainBlock(value, /*mandatory*/ false);
2363 else
2364 return EmitARCRetainNonBlock(value);
2365}
2366
2367/// Retain the given object, with normal retain semantics.
2368/// call i8* \@objc_retain(i8* %value)
2369llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
2370 return emitARCValueOperation(*this, value, nullptr,
2371 CGM.getObjCEntrypoints().objc_retain,
2372 llvm::Intrinsic::objc_retain);
2373}
2374
2375/// Retain the given block, with _Block_copy semantics.
2376/// call i8* \@objc_retainBlock(i8* %value)
2377///
2378/// \param mandatory - If false, emit the call with metadata
2379/// indicating that it's okay for the optimizer to eliminate this call
2380/// if it can prove that the block never escapes except down the stack.
2381llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
2382 bool mandatory) {
2383 llvm::Value *result
2384 = emitARCValueOperation(*this, value, nullptr,
2385 CGM.getObjCEntrypoints().objc_retainBlock,
2386 llvm::Intrinsic::objc_retainBlock);
2387
2388 // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
2389 // tell the optimizer that it doesn't need to do this copy if the
2390 // block doesn't escape, where being passed as an argument doesn't
2391 // count as escaping.
2392 if (!mandatory && isa<llvm::Instruction>(result)) {
2393 llvm::CallInst *call
2394 = cast<llvm::CallInst>(result->stripPointerCasts());
2395 assert(call->getCalledOperand() ==
2396 CGM.getObjCEntrypoints().objc_retainBlock);
2397
2398 call->setMetadata("clang.arc.copy_on_escape",
2399 llvm::MDNode::get(Builder.getContext(), {}));
2400 }
2401
2402 return result;
2403}
2404
2406 // Fetch the void(void) inline asm which marks that we're going to
2407 // do something with the autoreleased return value.
2408 llvm::InlineAsm *&marker
2410 if (!marker) {
2411 StringRef assembly
2414
2415 // If we have an empty assembly string, there's nothing to do.
2416 if (assembly.empty()) {
2417
2418 // Otherwise, at -O0, build an inline asm that we're going to call
2419 // in a moment.
2420 } else if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
2421 llvm::FunctionType *type =
2422 llvm::FunctionType::get(CGF.VoidTy, /*variadic*/false);
2423
2424 marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
2425
2426 // If we're at -O1 and above, we don't want to litter the code
2427 // with this marker yet, so leave a breadcrumb for the ARC
2428 // optimizer to pick up.
2429 } else {
2430 const char *retainRVMarkerKey = llvm::objcarc::getRVMarkerModuleFlagStr();
2431 if (!CGF.CGM.getModule().getModuleFlag(retainRVMarkerKey)) {
2432 auto *str = llvm::MDString::get(CGF.getLLVMContext(), assembly);
2433 CGF.CGM.getModule().addModuleFlag(llvm::Module::Error,
2434 retainRVMarkerKey, str);
2435 }
2436 }
2437 }
2438
2439 // Call the marker asm if we made one, which we do only at -O0.
2440 if (marker)
2441 CGF.Builder.CreateCall(marker, {}, CGF.getBundlesForFunclet(marker));
2442}
2443
2444static llvm::Value *emitOptimizedARCReturnCall(llvm::Value *value,
2445 bool IsRetainRV,
2446 CodeGenFunction &CGF) {
2448
2449 // Add operand bundle "clang.arc.attachedcall" to the call instead of emitting
2450 // retainRV or claimRV calls in the IR. We currently do this only when the
2451 // optimization level isn't -O0 since global-isel, which is currently run at
2452 // -O0, doesn't know about the operand bundle.
2454 llvm::Function *&EP = IsRetainRV
2457 llvm::Intrinsic::ID IID =
2458 IsRetainRV ? llvm::Intrinsic::objc_retainAutoreleasedReturnValue
2459 : llvm::Intrinsic::objc_unsafeClaimAutoreleasedReturnValue;
2460 EP = getARCIntrinsic(IID, CGF.CGM);
2461
2462 llvm::Triple::ArchType Arch = CGF.CGM.getTriple().getArch();
2463
2464 // FIXME: Do this on all targets and at -O0 too. This can be enabled only if
2465 // the target backend knows how to handle the operand bundle.
2466 if (CGF.CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2467 (Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_32 ||
2468 Arch == llvm::Triple::x86_64)) {
2469 llvm::Value *bundleArgs[] = {EP};
2470 llvm::OperandBundleDef OB("clang.arc.attachedcall", bundleArgs);
2471 auto *oldCall = cast<llvm::CallBase>(value);
2472 llvm::CallBase *newCall = llvm::CallBase::addOperandBundle(
2473 oldCall, llvm::LLVMContext::OB_clang_arc_attachedcall, OB,
2474 oldCall->getIterator());
2475 newCall->copyMetadata(*oldCall);
2476 oldCall->replaceAllUsesWith(newCall);
2477 oldCall->eraseFromParent();
2478 CGF.EmitARCNoopIntrinsicUse(newCall);
2479 return newCall;
2480 }
2481
2482 bool isNoTail =
2484 llvm::CallInst::TailCallKind tailKind =
2485 isNoTail ? llvm::CallInst::TCK_NoTail : llvm::CallInst::TCK_None;
2486 return emitARCValueOperation(CGF, value, nullptr, EP, IID, tailKind);
2487}
2488
2489/// Retain the given object which is the result of a function call.
2490/// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
2491///
2492/// Yes, this function name is one character away from a different
2493/// call with completely different semantics.
2494llvm::Value *
2496 return emitOptimizedARCReturnCall(value, true, *this);
2497}
2498
2499/// Claim a possibly-autoreleased return value at +0. This is only
2500/// valid to do in contexts which do not rely on the retain to keep
2501/// the object valid for all of its uses; for example, when
2502/// the value is ignored, or when it is being assigned to an
2503/// __unsafe_unretained variable.
2504///
2505/// call i8* \@objc_unsafeClaimAutoreleasedReturnValue(i8* %value)
2506llvm::Value *
2508 return emitOptimizedARCReturnCall(value, false, *this);
2509}
2510
2511/// Release the given object.
2512/// call void \@objc_release(i8* %value)
2513void CodeGenFunction::EmitARCRelease(llvm::Value *value,
2514 ARCPreciseLifetime_t precise) {
2515 if (isa<llvm::ConstantPointerNull>(value)) return;
2516
2517 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_release;
2518 if (!fn)
2519 fn = getARCIntrinsic(llvm::Intrinsic::objc_release, CGM);
2520
2521 // Cast the argument to 'id'.
2522 value = Builder.CreateBitCast(value, Int8PtrTy);
2523
2524 // Call objc_release.
2525 llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
2526
2527 if (precise == ARCImpreciseLifetime) {
2528 call->setMetadata("clang.imprecise_release",
2529 llvm::MDNode::get(Builder.getContext(), {}));
2530 }
2531}
2532
2533/// Destroy a __strong variable.
2534///
2535/// At -O0, emit a call to store 'null' into the address;
2536/// instrumenting tools prefer this because the address is exposed,
2537/// but it's relatively cumbersome to optimize.
2538///
2539/// At -O1 and above, just load and call objc_release.
2540///
2541/// call void \@objc_storeStrong(i8** %addr, i8* null)
2543 ARCPreciseLifetime_t precise) {
2544 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2545 llvm::Value *null = getNullForVariable(addr);
2546 EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
2547 return;
2548 }
2549
2550 llvm::Value *value = Builder.CreateLoad(addr);
2551 EmitARCRelease(value, precise);
2552}
2553
2554/// Store into a strong object. Always calls this:
2555/// call void \@objc_storeStrong(i8** %addr, i8* %value)
2557 llvm::Value *value,
2558 bool ignored) {
2559 assert(addr.getElementType() == value->getType());
2560
2561 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_storeStrong;
2562 if (!fn)
2563 fn = getARCIntrinsic(llvm::Intrinsic::objc_storeStrong, CGM);
2564
2565 llvm::Value *args[] = {
2566 Builder.CreateBitCast(addr.emitRawPointer(*this), Int8PtrPtrTy),
2567 Builder.CreateBitCast(value, Int8PtrTy)};
2568 EmitNounwindRuntimeCall(fn, args);
2569
2570 if (ignored) return nullptr;
2571 return value;
2572}
2573
2574/// Store into a strong object. Sometimes calls this:
2575/// call void \@objc_storeStrong(i8** %addr, i8* %value)
2576/// Other times, breaks it down into components.
2578 llvm::Value *newValue,
2579 bool ignored) {
2580 QualType type = dst.getType();
2581 bool isBlock = type->isBlockPointerType();
2582
2583 // Use a store barrier at -O0 unless this is a block type or the
2584 // lvalue is inadequately aligned.
2585 if (shouldUseFusedARCCalls() &&
2586 !isBlock &&
2587 (dst.getAlignment().isZero() ||
2589 return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
2590 }
2591
2592 // Otherwise, split it out.
2593
2594 // Retain the new value.
2595 newValue = EmitARCRetain(type, newValue);
2596
2597 // Read the old value.
2598 llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation());
2599
2600 // Store. We do this before the release so that any deallocs won't
2601 // see the old value.
2602 EmitStoreOfScalar(newValue, dst);
2603
2604 // Finally, release the old value.
2605 EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
2606
2607 return newValue;
2608}
2609
2610/// Autorelease the given object.
2611/// call i8* \@objc_autorelease(i8* %value)
2612llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
2613 return emitARCValueOperation(*this, value, nullptr,
2614 CGM.getObjCEntrypoints().objc_autorelease,
2615 llvm::Intrinsic::objc_autorelease);
2616}
2617
2618/// Autorelease the given object.
2619/// call i8* \@objc_autoreleaseReturnValue(i8* %value)
2620llvm::Value *
2622 return emitARCValueOperation(*this, value, nullptr,
2623 CGM.getObjCEntrypoints().objc_autoreleaseReturnValue,
2624 llvm::Intrinsic::objc_autoreleaseReturnValue,
2625 llvm::CallInst::TCK_Tail);
2626}
2627
2628/// Do a fused retain/autorelease of the given object.
2629/// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
2630llvm::Value *
2632 return emitARCValueOperation(*this, value, nullptr,
2633 CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue,
2634 llvm::Intrinsic::objc_retainAutoreleaseReturnValue,
2635 llvm::CallInst::TCK_Tail);
2636}
2637
2638/// Do a fused retain/autorelease of the given object.
2639/// call i8* \@objc_retainAutorelease(i8* %value)
2640/// or
2641/// %retain = call i8* \@objc_retainBlock(i8* %value)
2642/// call i8* \@objc_autorelease(i8* %retain)
2644 llvm::Value *value) {
2645 if (!type->isBlockPointerType())
2647
2648 if (isa<llvm::ConstantPointerNull>(value)) return value;
2649
2650 llvm::Type *origType = value->getType();
2651 value = Builder.CreateBitCast(value, Int8PtrTy);
2652 value = EmitARCRetainBlock(value, /*mandatory*/ true);
2653 value = EmitARCAutorelease(value);
2654 return Builder.CreateBitCast(value, origType);
2655}
2656
2657/// Do a fused retain/autorelease of the given object.
2658/// call i8* \@objc_retainAutorelease(i8* %value)
2659llvm::Value *
2661 return emitARCValueOperation(*this, value, nullptr,
2662 CGM.getObjCEntrypoints().objc_retainAutorelease,
2663 llvm::Intrinsic::objc_retainAutorelease);
2664}
2665
2666/// i8* \@objc_loadWeak(i8** %addr)
2667/// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
2669 return emitARCLoadOperation(*this, addr,
2670 CGM.getObjCEntrypoints().objc_loadWeak,
2671 llvm::Intrinsic::objc_loadWeak);
2672}
2673
2674/// i8* \@objc_loadWeakRetained(i8** %addr)
2676 return emitARCLoadOperation(*this, addr,
2677 CGM.getObjCEntrypoints().objc_loadWeakRetained,
2678 llvm::Intrinsic::objc_loadWeakRetained);
2679}
2680
2681/// i8* \@objc_storeWeak(i8** %addr, i8* %value)
2682/// Returns %value.
2684 llvm::Value *value,
2685 bool ignored) {
2686 return emitARCStoreOperation(*this, addr, value,
2687 CGM.getObjCEntrypoints().objc_storeWeak,
2688 llvm::Intrinsic::objc_storeWeak, ignored);
2689}
2690
2691/// i8* \@objc_initWeak(i8** %addr, i8* %value)
2692/// Returns %value. %addr is known to not have a current weak entry.
2693/// Essentially equivalent to:
2694/// *addr = nil; objc_storeWeak(addr, value);
2695void CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) {
2696 // If we're initializing to null, just write null to memory; no need
2697 // to get the runtime involved. But don't do this if optimization
2698 // is enabled, because accounting for this would make the optimizer
2699 // much more complicated.
2700 if (isa<llvm::ConstantPointerNull>(value) &&
2701 CGM.getCodeGenOpts().OptimizationLevel == 0) {
2702 Builder.CreateStore(value, addr);
2703 return;
2704 }
2705
2706 emitARCStoreOperation(*this, addr, value,
2707 CGM.getObjCEntrypoints().objc_initWeak,
2708 llvm::Intrinsic::objc_initWeak, /*ignored*/ true);
2709}
2710
2711/// void \@objc_destroyWeak(i8** %addr)
2712/// Essentially objc_storeWeak(addr, nil).
2714 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_destroyWeak;
2715 if (!fn)
2716 fn = getARCIntrinsic(llvm::Intrinsic::objc_destroyWeak, CGM);
2717
2718 EmitNounwindRuntimeCall(fn, addr.emitRawPointer(*this));
2719}
2720
2721/// void \@objc_moveWeak(i8** %dest, i8** %src)
2722/// Disregards the current value in %dest. Leaves %src pointing to nothing.
2723/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
2725 emitARCCopyOperation(*this, dst, src,
2726 CGM.getObjCEntrypoints().objc_moveWeak,
2727 llvm::Intrinsic::objc_moveWeak);
2728}
2729
2730/// void \@objc_copyWeak(i8** %dest, i8** %src)
2731/// Disregards the current value in %dest. Essentially
2732/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
2734 emitARCCopyOperation(*this, dst, src,
2735 CGM.getObjCEntrypoints().objc_copyWeak,
2736 llvm::Intrinsic::objc_copyWeak);
2737}
2738
2740 Address SrcAddr) {
2741 llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr);
2743 EmitARCStoreWeak(DstAddr, Object, false);
2744}
2745
2747 Address SrcAddr) {
2748 llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr);
2750 EmitARCStoreWeak(DstAddr, Object, false);
2751 EmitARCDestroyWeak(SrcAddr);
2752}
2753
2754/// Produce the code to do a objc_autoreleasepool_push.
2755/// call i8* \@objc_autoreleasePoolPush(void)
2757 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush;
2758 if (!fn)
2759 fn = getARCIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPush, CGM);
2760
2761 return EmitNounwindRuntimeCall(fn);
2762}
2763
2764/// Produce the code to do a primitive release.
2765/// call void \@objc_autoreleasePoolPop(i8* %ptr)
2767 assert(value->getType() == Int8PtrTy);
2768
2769 if (getInvokeDest()) {
2770 // Call the runtime method not the intrinsic if we are handling exceptions
2771 llvm::FunctionCallee &fn =
2772 CGM.getObjCEntrypoints().objc_autoreleasePoolPopInvoke;
2773 if (!fn) {
2774 llvm::FunctionType *fnType =
2775 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2776 fn = CGM.CreateRuntimeFunction(fnType, "objc_autoreleasePoolPop");
2778 }
2779
2780 // objc_autoreleasePoolPop can throw.
2781 EmitRuntimeCallOrInvoke(fn, value);
2782 } else {
2783 llvm::FunctionCallee &fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop;
2784 if (!fn)
2785 fn = getARCIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPop, CGM);
2786
2787 EmitRuntimeCall(fn, value);
2788 }
2789}
2790
2791/// Produce the code to do an MRR version objc_autoreleasepool_push.
2792/// Which is: [[NSAutoreleasePool alloc] init];
2793/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2794/// init is declared as: - (id) init; in its NSObject super class.
2795///
2797 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2798 llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
2799 // [NSAutoreleasePool alloc]
2800 const IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2801 Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2802 CallArgList Args;
2803 RValue AllocRV =
2804 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2805 getContext().getObjCIdType(),
2806 AllocSel, Receiver, Args);
2807
2808 // [Receiver init]
2809 Receiver = AllocRV.getScalarVal();
2810 II = &CGM.getContext().Idents.get("init");
2811 Selector InitSel = getContext().Selectors.getSelector(0, &II);
2812 RValue InitRV =
2813 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2814 getContext().getObjCIdType(),
2815 InitSel, Receiver, Args);
2816 return InitRV.getScalarVal();
2817}
2818
2819/// Allocate the given objc object.
2820/// call i8* \@objc_alloc(i8* %value)
2821llvm::Value *CodeGenFunction::EmitObjCAlloc(llvm::Value *value,
2822 llvm::Type *resultType) {
2823 return emitObjCValueOperation(*this, value, resultType,
2824 CGM.getObjCEntrypoints().objc_alloc,
2825 "objc_alloc");
2826}
2827
2828/// Allocate the given objc object.
2829/// call i8* \@objc_allocWithZone(i8* %value)
2830llvm::Value *CodeGenFunction::EmitObjCAllocWithZone(llvm::Value *value,
2831 llvm::Type *resultType) {
2832 return emitObjCValueOperation(*this, value, resultType,
2833 CGM.getObjCEntrypoints().objc_allocWithZone,
2834 "objc_allocWithZone");
2835}
2836
2837llvm::Value *CodeGenFunction::EmitObjCAllocInit(llvm::Value *value,
2838 llvm::Type *resultType) {
2839 return emitObjCValueOperation(*this, value, resultType,
2840 CGM.getObjCEntrypoints().objc_alloc_init,
2841 "objc_alloc_init");
2842}
2843
2844/// Produce the code to do a primitive release.
2845/// [tmp drain];
2847 const IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2848 Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2849 CallArgList Args;
2850 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2851 getContext().VoidTy, DrainSel, Arg, Args);
2852}
2853
2855 Address addr,
2856 QualType type) {
2858}
2859
2861 Address addr,
2862 QualType type) {
2864}
2865
2867 Address addr,
2868 QualType type) {
2869 CGF.EmitARCDestroyWeak(addr);
2870}
2871
2873 QualType type) {
2874 llvm::Value *value = CGF.Builder.CreateLoad(addr);
2875 CGF.EmitARCIntrinsicUse(value);
2876}
2877
2878/// Autorelease the given object.
2879/// call i8* \@objc_autorelease(i8* %value)
2880llvm::Value *CodeGenFunction::EmitObjCAutorelease(llvm::Value *value,
2881 llvm::Type *returnType) {
2883 *this, value, returnType,
2884 CGM.getObjCEntrypoints().objc_autoreleaseRuntimeFunction,
2885 "objc_autorelease");
2886}
2887
2888/// Retain the given object, with normal retain semantics.
2889/// call i8* \@objc_retain(i8* %value)
2890llvm::Value *CodeGenFunction::EmitObjCRetainNonBlock(llvm::Value *value,
2891 llvm::Type *returnType) {
2893 *this, value, returnType,
2894 CGM.getObjCEntrypoints().objc_retainRuntimeFunction, "objc_retain");
2895}
2896
2897/// Release the given object.
2898/// call void \@objc_release(i8* %value)
2899void CodeGenFunction::EmitObjCRelease(llvm::Value *value,
2900 ARCPreciseLifetime_t precise) {
2901 if (isa<llvm::ConstantPointerNull>(value)) return;
2902
2903 llvm::FunctionCallee &fn =
2904 CGM.getObjCEntrypoints().objc_releaseRuntimeFunction;
2905 if (!fn) {
2906 llvm::FunctionType *fnType =
2907 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2908 fn = CGM.CreateRuntimeFunction(fnType, "objc_release");
2910 // We have Native ARC, so set nonlazybind attribute for performance
2911 if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee()))
2912 f->addFnAttr(llvm::Attribute::NonLazyBind);
2913 }
2914
2915 // Cast the argument to 'id'.
2916 value = Builder.CreateBitCast(value, Int8PtrTy);
2917
2918 // Call objc_release.
2919 llvm::CallBase *call = EmitCallOrInvoke(fn, value);
2920
2921 if (precise == ARCImpreciseLifetime) {
2922 call->setMetadata("clang.imprecise_release",
2923 llvm::MDNode::get(Builder.getContext(), {}));
2924 }
2925}
2926
2927namespace {
2928 struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup {
2929 llvm::Value *Token;
2930
2931 CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2932
2933 void Emit(CodeGenFunction &CGF, Flags flags) override {
2935 }
2936 };
2937 struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup {
2938 llvm::Value *Token;
2939
2940 CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2941
2942 void Emit(CodeGenFunction &CGF, Flags flags) override {
2944 }
2945 };
2946}
2947
2949 if (CGM.getLangOpts().ObjCAutoRefCount)
2950 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2951 else
2952 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2953}
2954
2956 switch (lifetime) {
2961 return true;
2962
2964 return false;
2965 }
2966
2967 llvm_unreachable("impossible lifetime!");
2968}
2969
2971 LValue lvalue,
2972 QualType type) {
2973 llvm::Value *result;
2974 bool shouldRetain = shouldRetainObjCLifetime(type.getObjCLifetime());
2975 if (shouldRetain) {
2976 result = CGF.EmitLoadOfLValue(lvalue, SourceLocation()).getScalarVal();
2977 } else {
2978 assert(type.getObjCLifetime() == Qualifiers::OCL_Weak);
2979 result = CGF.EmitARCLoadWeakRetained(lvalue.getAddress());
2980 }
2981 return TryEmitResult(result, !shouldRetain);
2982}
2983
2985 const Expr *e) {
2986 e = e->IgnoreParens();
2987 QualType type = e->getType();
2988
2989 // If we're loading retained from a __strong xvalue, we can avoid
2990 // an extra retain/release pair by zeroing out the source of this
2991 // "move" operation.
2992 if (e->isXValue() &&
2993 !type.isConstQualified() &&
2994 type.getObjCLifetime() == Qualifiers::OCL_Strong) {
2995 // Emit the lvalue.
2996 LValue lv = CGF.EmitLValue(e);
2997
2998 // Load the object pointer.
2999 llvm::Value *result = CGF.EmitLoadOfLValue(lv,
3001
3002 // Set the source pointer to NULL.
3003 CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv);
3004
3005 return TryEmitResult(result, true);
3006 }
3007
3008 // As a very special optimization, in ARC++, if the l-value is the
3009 // result of a non-volatile assignment, do a simple retain of the
3010 // result of the call to objc_storeWeak instead of reloading.
3011 if (CGF.getLangOpts().CPlusPlus &&
3012 !type.isVolatileQualified() &&
3013 type.getObjCLifetime() == Qualifiers::OCL_Weak &&
3015 cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
3016 return TryEmitResult(CGF.EmitScalarExpr(e), false);
3017
3018 // Try to emit code for scalar constant instead of emitting LValue and
3019 // loading it because we are not guaranteed to have an l-value. One of such
3020 // cases is DeclRefExpr referencing non-odr-used constant-evaluated variable.
3021 if (const auto *decl_expr = dyn_cast<DeclRefExpr>(e)) {
3022 auto *DRE = const_cast<DeclRefExpr *>(decl_expr);
3024 return TryEmitResult(CGF.emitScalarConstant(constant, DRE),
3025 !shouldRetainObjCLifetime(type.getObjCLifetime()));
3026 }
3027
3028 return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
3029}
3030
3031typedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
3032 llvm::Value *value)>
3034
3035/// Insert code immediately after a call.
3036
3037// FIXME: We should find a way to emit the runtime call immediately
3038// after the call is emitted to eliminate the need for this function.
3040 llvm::Value *value,
3041 ValueTransform doAfterCall,
3042 ValueTransform doFallback) {
3043 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
3044 auto *callBase = dyn_cast<llvm::CallBase>(value);
3045
3046 if (callBase && llvm::objcarc::hasAttachedCallOpBundle(callBase)) {
3047 // Fall back if the call base has operand bundle "clang.arc.attachedcall".
3048 value = doFallback(CGF, value);
3049 } else if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
3050 // Place the retain immediately following the call.
3051 CGF.Builder.SetInsertPoint(call->getParent(),
3052 ++llvm::BasicBlock::iterator(call));
3053 value = doAfterCall(CGF, value);
3054 } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
3055 // Place the retain at the beginning of the normal destination block.
3056 llvm::BasicBlock *BB = invoke->getNormalDest();
3057 CGF.Builder.SetInsertPoint(BB, BB->begin());
3058 value = doAfterCall(CGF, value);
3059
3060 // Bitcasts can arise because of related-result returns. Rewrite
3061 // the operand.
3062 } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
3063 // Change the insert point to avoid emitting the fall-back call after the
3064 // bitcast.
3065 CGF.Builder.SetInsertPoint(bitcast->getParent(), bitcast->getIterator());
3066 llvm::Value *operand = bitcast->getOperand(0);
3067 operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback);
3068 bitcast->setOperand(0, operand);
3069 value = bitcast;
3070 } else {
3071 auto *phi = dyn_cast<llvm::PHINode>(value);
3072 if (phi && phi->getNumIncomingValues() == 2 &&
3073 isa<llvm::ConstantPointerNull>(phi->getIncomingValue(1)) &&
3074 isa<llvm::CallBase>(phi->getIncomingValue(0))) {
3075 // Handle phi instructions that are generated when it's necessary to check
3076 // whether the receiver of a message is null.
3077 llvm::Value *inVal = phi->getIncomingValue(0);
3078 inVal = emitARCOperationAfterCall(CGF, inVal, doAfterCall, doFallback);
3079 phi->setIncomingValue(0, inVal);
3080 value = phi;
3081 } else {
3082 // Generic fall-back case.
3083 // Retain using the non-block variant: we never need to do a copy
3084 // of a block that's been returned to us.
3085 value = doFallback(CGF, value);
3086 }
3087 }
3088
3089 CGF.Builder.restoreIP(ip);
3090 return value;
3091}
3092
3093/// Given that the given expression is some sort of call (which does
3094/// not return retained), emit a retain following it.
3096 const Expr *e) {
3097 llvm::Value *value = CGF.EmitScalarExpr(e);
3098 return emitARCOperationAfterCall(CGF, value,
3099 [](CodeGenFunction &CGF, llvm::Value *value) {
3100 return CGF.EmitARCRetainAutoreleasedReturnValue(value);
3101 },
3102 [](CodeGenFunction &CGF, llvm::Value *value) {
3103 return CGF.EmitARCRetainNonBlock(value);
3104 });
3105}
3106
3107/// Given that the given expression is some sort of call (which does
3108/// not return retained), perform an unsafeClaim following it.
3110 const Expr *e) {
3111 llvm::Value *value = CGF.EmitScalarExpr(e);
3112 return emitARCOperationAfterCall(CGF, value,
3113 [](CodeGenFunction &CGF, llvm::Value *value) {
3115 },
3116 [](CodeGenFunction &CGF, llvm::Value *value) {
3117 return value;
3118 });
3119}
3120
3122 bool allowUnsafeClaim) {
3123 if (allowUnsafeClaim &&
3124 CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()) {
3125 return emitARCUnsafeClaimCallResult(*this, E);
3126 } else {
3127 llvm::Value *value = emitARCRetainCallResult(*this, E);
3128 return EmitObjCConsumeObject(E->getType(), value);
3129 }
3130}
3131
3132/// Determine whether it might be important to emit a separate
3133/// objc_retain_block on the result of the given expression, or
3134/// whether it's okay to just emit it in a +1 context.
3136 assert(e->getType()->isBlockPointerType());
3137 e = e->IgnoreParens();
3138
3139 // For future goodness, emit block expressions directly in +1
3140 // contexts if we can.
3141 if (isa<BlockExpr>(e))
3142 return false;
3143
3144 if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
3145 switch (cast->getCastKind()) {
3146 // Emitting these operations in +1 contexts is goodness.
3147 case CK_LValueToRValue:
3148 case CK_ARCReclaimReturnedObject:
3149 case CK_ARCConsumeObject:
3150 case CK_ARCProduceObject:
3151 return false;
3152
3153 // These operations preserve a block type.
3154 case CK_NoOp:
3155 case CK_BitCast:
3156 return shouldEmitSeparateBlockRetain(cast->getSubExpr());
3157
3158 // These operations are known to be bad (or haven't been considered).
3159 case CK_AnyPointerToBlockPointerCast:
3160 default:
3161 return true;
3162 }
3163 }
3164
3165 return true;
3166}
3167
3168namespace {
3169/// A CRTP base class for emitting expressions of retainable object
3170/// pointer type in ARC.
3171template <typename Impl, typename Result> class ARCExprEmitter {
3172protected:
3173 CodeGenFunction &CGF;
3174 Impl &asImpl() { return *static_cast<Impl*>(this); }
3175
3176 ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {}
3177
3178public:
3179 Result visit(const Expr *e);
3180 Result visitCastExpr(const CastExpr *e);
3181 Result visitPseudoObjectExpr(const PseudoObjectExpr *e);
3182 Result visitBlockExpr(const BlockExpr *e);
3183 Result visitBinaryOperator(const BinaryOperator *e);
3184 Result visitBinAssign(const BinaryOperator *e);
3185 Result visitBinAssignUnsafeUnretained(const BinaryOperator *e);
3186 Result visitBinAssignAutoreleasing(const BinaryOperator *e);
3187 Result visitBinAssignWeak(const BinaryOperator *e);
3188 Result visitBinAssignStrong(const BinaryOperator *e);
3189
3190 // Minimal implementation:
3191 // Result visitLValueToRValue(const Expr *e)
3192 // Result visitConsumeObject(const Expr *e)
3193 // Result visitExtendBlockObject(const Expr *e)
3194 // Result visitReclaimReturnedObject(const Expr *e)
3195 // Result visitCall(const Expr *e)
3196 // Result visitExpr(const Expr *e)
3197 //
3198 // Result emitBitCast(Result result, llvm::Type *resultType)
3199 // llvm::Value *getValueOfResult(Result result)
3200};
3201}
3202
3203/// Try to emit a PseudoObjectExpr under special ARC rules.
3204///
3205/// This massively duplicates emitPseudoObjectRValue.
3206template <typename Impl, typename Result>
3207Result
3208ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) {
3209 SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
3210
3211 // Find the result expression.
3212 const Expr *resultExpr = E->getResultExpr();
3213 assert(resultExpr);
3214 Result result;
3215
3217 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
3218 const Expr *semantic = *i;
3219
3220 // If this semantic expression is an opaque value, bind it
3221 // to the result of its source expression.
3222 if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
3224 OVMA opaqueData;
3225
3226 // If this semantic is the result of the pseudo-object
3227 // expression, try to evaluate the source as +1.
3228 if (ov == resultExpr) {
3229 assert(!OVMA::shouldBindAsLValue(ov));
3230 result = asImpl().visit(ov->getSourceExpr());
3231 opaqueData = OVMA::bind(CGF, ov,
3232 RValue::get(asImpl().getValueOfResult(result)));
3233
3234 // Otherwise, just bind it.
3235 } else {
3236 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
3237 }
3238 opaques.push_back(opaqueData);
3239
3240 // Otherwise, if the expression is the result, evaluate it
3241 // and remember the result.
3242 } else if (semantic == resultExpr) {
3243 result = asImpl().visit(semantic);
3244
3245 // Otherwise, evaluate the expression in an ignored context.
3246 } else {
3247 CGF.EmitIgnoredExpr(semantic);
3248 }
3249 }
3250
3251 // Unbind all the opaques now.
3252 for (CodeGenFunction::OpaqueValueMappingData &opaque : opaques)
3253 opaque.unbind(CGF);
3254
3255 return result;
3256}
3257
3258template <typename Impl, typename Result>
3259Result ARCExprEmitter<Impl, Result>::visitBlockExpr(const BlockExpr *e) {
3260 // The default implementation just forwards the expression to visitExpr.
3261 return asImpl().visitExpr(e);
3262}
3263
3264template <typename Impl, typename Result>
3265Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) {
3266 switch (e->getCastKind()) {
3267
3268 // No-op casts don't change the type, so we just ignore them.
3269 case CK_NoOp:
3270 return asImpl().visit(e->getSubExpr());
3271
3272 // These casts can change the type.
3273 case CK_CPointerToObjCPointerCast:
3274 case CK_BlockPointerToObjCPointerCast:
3275 case CK_AnyPointerToBlockPointerCast:
3276 case CK_BitCast: {
3277 llvm::Type *resultType = CGF.ConvertType(e->getType());
3278 assert(e->getSubExpr()->getType()->hasPointerRepresentation());
3279 Result result = asImpl().visit(e->getSubExpr());
3280 return asImpl().emitBitCast(result, resultType);
3281 }
3282
3283 // Handle some casts specially.
3284 case CK_LValueToRValue:
3285 return asImpl().visitLValueToRValue(e->getSubExpr());
3286 case CK_ARCConsumeObject:
3287 return asImpl().visitConsumeObject(e->getSubExpr());
3288 case CK_ARCExtendBlockObject:
3289 return asImpl().visitExtendBlockObject(e->getSubExpr());
3290 case CK_ARCReclaimReturnedObject:
3291 return asImpl().visitReclaimReturnedObject(e->getSubExpr());
3292
3293 // Otherwise, use the default logic.
3294 default:
3295 return asImpl().visitExpr(e);
3296 }
3297}
3298
3299template <typename Impl, typename Result>
3300Result
3301ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) {
3302 switch (e->getOpcode()) {
3303 case BO_Comma:
3304 CGF.EmitIgnoredExpr(e->getLHS());
3305 CGF.EnsureInsertPoint();
3306 return asImpl().visit(e->getRHS());
3307
3308 case BO_Assign:
3309 return asImpl().visitBinAssign(e);
3310
3311 default:
3312 return asImpl().visitExpr(e);
3313 }
3314}
3315
3316template <typename Impl, typename Result>
3317Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) {
3318 switch (e->getLHS()->getType().getObjCLifetime()) {
3320 return asImpl().visitBinAssignUnsafeUnretained(e);
3321
3323 return asImpl().visitBinAssignWeak(e);
3324
3326 return asImpl().visitBinAssignAutoreleasing(e);
3327
3329 return asImpl().visitBinAssignStrong(e);
3330
3332 return asImpl().visitExpr(e);
3333 }
3334 llvm_unreachable("bad ObjC ownership qualifier");
3335}
3336
3337/// The default rule for __unsafe_unretained emits the RHS recursively,
3338/// stores into the unsafe variable, and propagates the result outward.
3339template <typename Impl, typename Result>
3340Result ARCExprEmitter<Impl,Result>::
3341 visitBinAssignUnsafeUnretained(const BinaryOperator *e) {
3342 // Recursively emit the RHS.
3343 // For __block safety, do this before emitting the LHS.
3344 Result result = asImpl().visit(e->getRHS());
3345
3346 // Perform the store.
3347 LValue lvalue =
3348 CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store);
3349 CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)),
3350 lvalue);
3351
3352 return result;
3353}
3354
3355template <typename Impl, typename Result>
3356Result
3357ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) {
3358 return asImpl().visitExpr(e);
3359}
3360
3361template <typename Impl, typename Result>
3362Result
3363ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) {
3364 return asImpl().visitExpr(e);
3365}
3366
3367template <typename Impl, typename Result>
3368Result
3369ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) {
3370 return asImpl().visitExpr(e);
3371}
3372
3373/// The general expression-emission logic.
3374template <typename Impl, typename Result>
3375Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) {
3376 // We should *never* see a nested full-expression here, because if
3377 // we fail to emit at +1, our caller must not retain after we close
3378 // out the full-expression. This isn't as important in the unsafe
3379 // emitter.
3380 assert(!isa<ExprWithCleanups>(e));
3381
3382 // Look through parens, __extension__, generic selection, etc.
3383 e = e->IgnoreParens();
3384
3385 // Handle certain kinds of casts.
3386 if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
3387 return asImpl().visitCastExpr(ce);
3388
3389 // Handle the comma operator.
3390 } else if (auto op = dyn_cast<BinaryOperator>(e)) {
3391 return asImpl().visitBinaryOperator(op);
3392
3393 // TODO: handle conditional operators here
3394
3395 // For calls and message sends, use the retained-call logic.
3396 // Delegate inits are a special case in that they're the only
3397 // returns-retained expression that *isn't* surrounded by
3398 // a consume.
3399 } else if (isa<CallExpr>(e) ||
3401 !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
3402 return asImpl().visitCall(e);
3403
3404 // Look through pseudo-object expressions.
3405 } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
3406 return asImpl().visitPseudoObjectExpr(pseudo);
3407 } else if (auto *be = dyn_cast<BlockExpr>(e))
3408 return asImpl().visitBlockExpr(be);
3409
3410 return asImpl().visitExpr(e);
3411}
3412
3413namespace {
3414
3415/// An emitter for +1 results.
3416struct ARCRetainExprEmitter :
3417 public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> {
3418
3419 ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3420
3421 llvm::Value *getValueOfResult(TryEmitResult result) {
3422 return result.getPointer();
3423 }
3424
3425 TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) {
3426 llvm::Value *value = result.getPointer();
3427 value = CGF.Builder.CreateBitCast(value, resultType);
3428 result.setPointer(value);
3429 return result;
3430 }
3431
3432 TryEmitResult visitLValueToRValue(const Expr *e) {
3433 return tryEmitARCRetainLoadOfScalar(CGF, e);
3434 }
3435
3436 /// For consumptions, just emit the subexpression and thus elide
3437 /// the retain/release pair.
3438 TryEmitResult visitConsumeObject(const Expr *e) {
3439 llvm::Value *result = CGF.EmitScalarExpr(e);
3440 return TryEmitResult(result, true);
3441 }
3442
3443 TryEmitResult visitBlockExpr(const BlockExpr *e) {
3444 TryEmitResult result = visitExpr(e);
3445 // Avoid the block-retain if this is a block literal that doesn't need to be
3446 // copied to the heap.
3447 if (CGF.CGM.getCodeGenOpts().ObjCAvoidHeapifyLocalBlocks &&
3449 result.setInt(true);
3450 return result;
3451 }
3452
3453 /// Block extends are net +0. Naively, we could just recurse on
3454 /// the subexpression, but actually we need to ensure that the
3455 /// value is copied as a block, so there's a little filter here.
3456 TryEmitResult visitExtendBlockObject(const Expr *e) {
3457 llvm::Value *result; // will be a +0 value
3458
3459 // If we can't safely assume the sub-expression will produce a
3460 // block-copied value, emit the sub-expression at +0.
3462 result = CGF.EmitScalarExpr(e);
3463
3464 // Otherwise, try to emit the sub-expression at +1 recursively.
3465 } else {
3466 TryEmitResult subresult = asImpl().visit(e);
3467
3468 // If that produced a retained value, just use that.
3469 if (subresult.getInt()) {
3470 return subresult;
3471 }
3472
3473 // Otherwise it's +0.
3474 result = subresult.getPointer();
3475 }
3476
3477 // Retain the object as a block.
3478 result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
3479 return TryEmitResult(result, true);
3480 }
3481
3482 /// For reclaims, emit the subexpression as a retained call and
3483 /// skip the consumption.
3484 TryEmitResult visitReclaimReturnedObject(const Expr *e) {
3485 llvm::Value *result = emitARCRetainCallResult(CGF, e);
3486 return TryEmitResult(result, true);
3487 }
3488
3489 /// When we have an undecorated call, retroactively do a claim.
3490 TryEmitResult visitCall(const Expr *e) {
3491 llvm::Value *result = emitARCRetainCallResult(CGF, e);
3492 return TryEmitResult(result, true);
3493 }
3494
3495 // TODO: maybe special-case visitBinAssignWeak?
3496
3497 TryEmitResult visitExpr(const Expr *e) {
3498 // We didn't find an obvious production, so emit what we've got and
3499 // tell the caller that we didn't manage to retain.
3500 llvm::Value *result = CGF.EmitScalarExpr(e);
3501 return TryEmitResult(result, false);
3502 }
3503};
3504}
3505
3506static TryEmitResult
3508 return ARCRetainExprEmitter(CGF).visit(e);
3509}
3510
3512 LValue lvalue,
3513 QualType type) {
3514 TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
3515 llvm::Value *value = result.getPointer();
3516 if (!result.getInt())
3517 value = CGF.EmitARCRetain(type, value);
3518 return value;
3519}
3520
3521/// EmitARCRetainScalarExpr - Semantically equivalent to
3522/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
3523/// best-effort attempt to peephole expressions that naturally produce
3524/// retained objects.
3526 // The retain needs to happen within the full-expression.
3527 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3528 RunCleanupsScope scope(*this);
3529 return EmitARCRetainScalarExpr(cleanups->getSubExpr());
3530 }
3531
3532 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
3533 llvm::Value *value = result.getPointer();
3534 if (!result.getInt())
3535 value = EmitARCRetain(e->getType(), value);
3536 return value;
3537}
3538
3539llvm::Value *
3541 // The retain needs to happen within the full-expression.
3542 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3543 RunCleanupsScope scope(*this);
3544 return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
3545 }
3546
3547 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
3548 llvm::Value *value = result.getPointer();
3549 if (result.getInt())
3550 value = EmitARCAutorelease(value);
3551 else
3552 value = EmitARCRetainAutorelease(e->getType(), value);
3553 return value;
3554}
3555
3557 llvm::Value *result;
3558 bool doRetain;
3559
3561 result = EmitScalarExpr(e);
3562 doRetain = true;
3563 } else {
3564 TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
3565 result = subresult.getPointer();
3566 doRetain = !subresult.getInt();
3567 }
3568
3569 if (doRetain)
3570 result = EmitARCRetainBlock(result, /*mandatory*/ true);
3571 return EmitObjCConsumeObject(e->getType(), result);
3572}
3573
3575 // In ARC, retain and autorelease the expression.
3576 if (getLangOpts().ObjCAutoRefCount) {
3577 // Do so before running any cleanups for the full-expression.
3578 // EmitARCRetainAutoreleaseScalarExpr does this for us.
3580 }
3581
3582 // Otherwise, use the normal scalar-expression emission. The
3583 // exception machinery doesn't do anything special with the
3584 // exception like retaining it, so there's no safety associated with
3585 // only running cleanups after the throw has started, and when it
3586 // matters it tends to be substantially inferior code.
3587 return EmitScalarExpr(expr);
3588}
3589
3590namespace {
3591
3592/// An emitter for assigning into an __unsafe_unretained context.
3593struct ARCUnsafeUnretainedExprEmitter :
3594 public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> {
3595
3596 ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3597
3598 llvm::Value *getValueOfResult(llvm::Value *value) {
3599 return value;
3600 }
3601
3602 llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) {
3603 return CGF.Builder.CreateBitCast(value, resultType);
3604 }
3605
3606 llvm::Value *visitLValueToRValue(const Expr *e) {
3607 return CGF.EmitScalarExpr(e);
3608 }
3609
3610 /// For consumptions, just emit the subexpression and perform the
3611 /// consumption like normal.
3612 llvm::Value *visitConsumeObject(const Expr *e) {
3613 llvm::Value *value = CGF.EmitScalarExpr(e);
3614 return CGF.EmitObjCConsumeObject(e->getType(), value);
3615 }
3616
3617 /// No special logic for block extensions. (This probably can't
3618 /// actually happen in this emitter, though.)
3619 llvm::Value *visitExtendBlockObject(const Expr *e) {
3620 return CGF.EmitARCExtendBlockObject(e);
3621 }
3622
3623 /// For reclaims, perform an unsafeClaim if that's enabled.
3624 llvm::Value *visitReclaimReturnedObject(const Expr *e) {
3625 return CGF.EmitARCReclaimReturnedObject(e, /*unsafe*/ true);
3626 }
3627
3628 /// When we have an undecorated call, just emit it without adding
3629 /// the unsafeClaim.
3630 llvm::Value *visitCall(const Expr *e) {
3631 return CGF.EmitScalarExpr(e);
3632 }
3633
3634 /// Just do normal scalar emission in the default case.
3635 llvm::Value *visitExpr(const Expr *e) {
3636 return CGF.EmitScalarExpr(e);
3637 }
3638};
3639}
3640
3642 const Expr *e) {
3643 return ARCUnsafeUnretainedExprEmitter(CGF).visit(e);
3644}
3645
3646/// EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to
3647/// immediately releasing the resut of EmitARCRetainScalarExpr, but
3648/// avoiding any spurious retains, including by performing reclaims
3649/// with objc_unsafeClaimAutoreleasedReturnValue.
3651 // Look through full-expressions.
3652 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3653 RunCleanupsScope scope(*this);
3654 return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr());
3655 }
3656
3657 return emitARCUnsafeUnretainedScalarExpr(*this, e);
3658}
3659
3660std::pair<LValue,llvm::Value*>
3662 bool ignored) {
3663 // Evaluate the RHS first. If we're ignoring the result, assume
3664 // that we can emit at an unsafe +0.
3665 llvm::Value *value;
3666 if (ignored) {
3668 } else {
3669 value = EmitScalarExpr(e->getRHS());
3670 }
3671
3672 // Emit the LHS and perform the store.
3673 LValue lvalue = EmitLValue(e->getLHS());
3674 EmitStoreOfScalar(value, lvalue);
3675
3676 return std::pair<LValue,llvm::Value*>(std::move(lvalue), value);
3677}
3678
3679std::pair<LValue,llvm::Value*>
3681 bool ignored) {
3682 // Evaluate the RHS first.
3683 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
3684 llvm::Value *value = result.getPointer();
3685
3686 bool hasImmediateRetain = result.getInt();
3687
3688 // If we didn't emit a retained object, and the l-value is of block
3689 // type, then we need to emit the block-retain immediately in case
3690 // it invalidates the l-value.
3691 if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
3692 value = EmitARCRetainBlock(value, /*mandatory*/ false);
3693 hasImmediateRetain = true;
3694 }
3695
3696 LValue lvalue = EmitLValue(e->getLHS());
3697
3698 // If the RHS was emitted retained, expand this.
3699 if (hasImmediateRetain) {
3700 llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation());
3701 EmitStoreOfScalar(value, lvalue);
3702 EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
3703 } else {
3704 value = EmitARCStoreStrong(lvalue, value, ignored);
3705 }
3706
3707 return std::pair<LValue,llvm::Value*>(lvalue, value);
3708}
3709
3710std::pair<LValue,llvm::Value*>
3712 llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
3713 LValue lvalue = EmitLValue(e->getLHS());
3714
3715 EmitStoreOfScalar(value, lvalue);
3716
3717 return std::pair<LValue,llvm::Value*>(lvalue, value);
3718}
3719
3721 const ObjCAutoreleasePoolStmt &ARPS) {
3722 const Stmt *subStmt = ARPS.getSubStmt();
3723 const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
3724
3725 CGDebugInfo *DI = getDebugInfo();
3726 if (DI)
3728
3729 // Keep track of the current cleanup stack depth.
3730 RunCleanupsScope Scope(*this);
3731 if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
3732 llvm::Value *token = EmitObjCAutoreleasePoolPush();
3733 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
3734 } else {
3735 llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
3736 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
3737 }
3738
3739 for (const auto *I : S.body())
3740 EmitStmt(I);
3741
3742 if (DI)
3744}
3745
3746/// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
3747/// make sure it survives garbage collection until this point.
3748void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
3749 // We just use an inline assembly.
3750 llvm::FunctionType *extenderType
3751 = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
3752 llvm::InlineAsm *extender = llvm::InlineAsm::get(extenderType,
3753 /* assembly */ "",
3754 /* constraints */ "r",
3755 /* side effects */ true);
3756
3757 EmitNounwindRuntimeCall(extender, object);
3758}
3759
3760/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
3761/// non-trivial copy assignment function, produce following helper function.
3762/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
3763///
3764llvm::Constant *
3766 const ObjCPropertyImplDecl *PID) {
3767 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3769 return nullptr;
3770
3771 QualType Ty = PID->getPropertyIvarDecl()->getType();
3772 ASTContext &C = getContext();
3773
3775 // Call the move assignment operator instead of calling the copy assignment
3776 // operator and destructor.
3777 CharUnits Alignment = C.getTypeAlignInChars(Ty);
3778 llvm::Constant *Fn = getNonTrivialCStructMoveAssignmentOperator(
3779 CGM, Alignment, Alignment, Ty.isVolatileQualified(), Ty);
3780 return Fn;
3781 }
3782
3783 if (!getLangOpts().CPlusPlus ||
3785 return nullptr;
3786 if (!Ty->isRecordType())
3787 return nullptr;
3788 llvm::Constant *HelperFn = nullptr;
3789 if (hasTrivialSetExpr(PID))
3790 return nullptr;
3791 assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
3792 if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
3793 return HelperFn;
3794
3795 const IdentifierInfo *II =
3796 &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
3797
3798 QualType ReturnTy = C.VoidTy;
3799 QualType DestTy = C.getPointerType(Ty);
3800 QualType SrcTy = Ty;
3801 SrcTy.addConst();
3802 SrcTy = C.getPointerType(SrcTy);
3803
3805 ArgTys.push_back(DestTy);
3806 ArgTys.push_back(SrcTy);
3807 QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {});
3808
3810 C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
3811 FunctionTy, nullptr, SC_Static, false, false, false);
3812
3813 FunctionArgList args;
3814 ParmVarDecl *Params[2];
3816 C, FD, SourceLocation(), SourceLocation(), nullptr, DestTy,
3817 C.getTrivialTypeSourceInfo(DestTy, SourceLocation()), SC_None,
3818 /*DefArg=*/nullptr);
3819 args.push_back(Params[0] = DstDecl);
3821 C, FD, SourceLocation(), SourceLocation(), nullptr, SrcTy,
3822 C.getTrivialTypeSourceInfo(SrcTy, SourceLocation()), SC_None,
3823 /*DefArg=*/nullptr);
3824 args.push_back(Params[1] = SrcDecl);
3825 FD->setParams(Params);
3826
3827 const CGFunctionInfo &FI =
3828 CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
3829
3830 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3831
3832 llvm::Function *Fn =
3833 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
3834 "__assign_helper_atomic_property_",
3835 &CGM.getModule());
3836
3837 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
3838
3839 StartFunction(FD, ReturnTy, Fn, FI, args);
3840
3841 DeclRefExpr DstExpr(C, DstDecl, false, DestTy, VK_PRValue, SourceLocation());
3843 C, &DstExpr, UO_Deref, DestTy->getPointeeType(), VK_LValue, OK_Ordinary,
3844 SourceLocation(), false, FPOptionsOverride());
3845
3846 DeclRefExpr SrcExpr(C, SrcDecl, false, SrcTy, VK_PRValue, SourceLocation());
3848 C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary,
3849 SourceLocation(), false, FPOptionsOverride());
3850
3851 Expr *Args[2] = {DST, SRC};
3852 CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
3854 C, OO_Equal, CalleeExp->getCallee(), Args, DestTy->getPointeeType(),
3856
3857 EmitStmt(TheCall);
3858
3860 HelperFn = Fn;
3861 CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
3862 return HelperFn;
3863}
3864
3866 const ObjCPropertyImplDecl *PID) {
3867 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3869 return nullptr;
3870
3871 QualType Ty = PD->getType();
3872 ASTContext &C = getContext();
3873
3875 CharUnits Alignment = C.getTypeAlignInChars(Ty);
3876 llvm::Constant *Fn = getNonTrivialCStructCopyConstructor(
3877 CGM, Alignment, Alignment, Ty.isVolatileQualified(), Ty);
3878 return Fn;
3879 }
3880
3881 if (!getLangOpts().CPlusPlus ||
3883 return nullptr;
3884 if (!Ty->isRecordType())
3885 return nullptr;
3886 llvm::Constant *HelperFn = nullptr;
3887 if (hasTrivialGetExpr(PID))
3888 return nullptr;
3889 assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
3890 if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
3891 return HelperFn;
3892
3893 const IdentifierInfo *II =
3894 &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
3895
3896 QualType ReturnTy = C.VoidTy;
3897 QualType DestTy = C.getPointerType(Ty);
3898 QualType SrcTy = Ty;
3899 SrcTy.addConst();
3900 SrcTy = C.getPointerType(SrcTy);
3901
3903 ArgTys.push_back(DestTy);
3904 ArgTys.push_back(SrcTy);
3905 QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {});
3906
3908 C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
3909 FunctionTy, nullptr, SC_Static, false, false, false);
3910
3911 FunctionArgList args;
3912 ParmVarDecl *Params[2];
3914 C, FD, SourceLocation(), SourceLocation(), nullptr, DestTy,
3915 C.getTrivialTypeSourceInfo(DestTy, SourceLocation()), SC_None,
3916 /*DefArg=*/nullptr);
3917 args.push_back(Params[0] = DstDecl);
3919 C, FD, SourceLocation(), SourceLocation(), nullptr, SrcTy,
3920 C.getTrivialTypeSourceInfo(SrcTy, SourceLocation()), SC_None,
3921 /*DefArg=*/nullptr);
3922 args.push_back(Params[1] = SrcDecl);
3923 FD->setParams(Params);
3924
3925 const CGFunctionInfo &FI =
3926 CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
3927
3928 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3929
3930 llvm::Function *Fn = llvm::Function::Create(
3931 LTy, llvm::GlobalValue::InternalLinkage, "__copy_helper_atomic_property_",
3932 &CGM.getModule());
3933
3934 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
3935
3936 StartFunction(FD, ReturnTy, Fn, FI, args);
3937
3938 DeclRefExpr SrcExpr(getContext(), SrcDecl, false, SrcTy, VK_PRValue,
3939 SourceLocation());
3940
3942 C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary,
3943 SourceLocation(), false, FPOptionsOverride());
3944
3945 CXXConstructExpr *CXXConstExpr =
3947
3948 SmallVector<Expr*, 4> ConstructorArgs;
3949 ConstructorArgs.push_back(SRC);
3950 ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()),
3951 CXXConstExpr->arg_end());
3952
3953 CXXConstructExpr *TheCXXConstructExpr =
3955 CXXConstExpr->getConstructor(),
3956 CXXConstExpr->isElidable(),
3957 ConstructorArgs,
3958 CXXConstExpr->hadMultipleCandidates(),
3959 CXXConstExpr->isListInitialization(),
3960 CXXConstExpr->isStdInitListInitialization(),
3961 CXXConstExpr->requiresZeroInitialization(),
3962 CXXConstExpr->getConstructionKind(),
3963 SourceRange());
3964
3965 DeclRefExpr DstExpr(getContext(), DstDecl, false, DestTy, VK_PRValue,
3966 SourceLocation());
3967
3968 RValue DV = EmitAnyExpr(&DstExpr);
3969 CharUnits Alignment =
3970 getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
3971 EmitAggExpr(TheCXXConstructExpr,
3973 Address(DV.getScalarVal(), ConvertTypeForMem(Ty), Alignment),
3977
3979 HelperFn = Fn;
3980 CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
3981 return HelperFn;
3982}
3983
3984llvm::Value *
3986 // Get selectors for retain/autorelease.
3987 const IdentifierInfo *CopyID = &getContext().Idents.get("copy");
3988 Selector CopySelector =
3990 const IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
3991 Selector AutoreleaseSelector =
3992 getContext().Selectors.getNullarySelector(AutoreleaseID);
3993
3994 // Emit calls to retain/autorelease.
3995 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3996 llvm::Value *Val = Block;
3997 RValue Result;
3998 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3999 Ty, CopySelector,
4000 Val, CallArgList(), nullptr, nullptr);
4001 Val = Result.getScalarVal();
4002 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
4003 Ty, AutoreleaseSelector,
4004 Val, CallArgList(), nullptr, nullptr);
4005 Val = Result.getScalarVal();
4006 return Val;
4007}
4008
4009static unsigned getBaseMachOPlatformID(const llvm::Triple &TT) {
4010 switch (TT.getOS()) {
4011 case llvm::Triple::Darwin:
4012 case llvm::Triple::MacOSX:
4013 return llvm::MachO::PLATFORM_MACOS;
4014 case llvm::Triple::IOS:
4015 return llvm::MachO::PLATFORM_IOS;
4016 case llvm::Triple::TvOS:
4017 return llvm::MachO::PLATFORM_TVOS;
4018 case llvm::Triple::WatchOS:
4019 return llvm::MachO::PLATFORM_WATCHOS;
4020 case llvm::Triple::XROS:
4021 return llvm::MachO::PLATFORM_XROS;
4022 case llvm::Triple::DriverKit:
4023 return llvm::MachO::PLATFORM_DRIVERKIT;
4024 default:
4025 return llvm::MachO::PLATFORM_UNKNOWN;
4026 }
4027}
4028
4030 const VersionTuple &Version) {
4031 CodeGenModule &CGM = CGF.CGM;
4032 // Note: we intend to support multi-platform version checks, so reserve
4033 // the room for a dual platform checking invocation that will be
4034 // implemented in the future.
4036
4037 auto EmitArgs = [&](const VersionTuple &Version, const llvm::Triple &TT) {
4038 std::optional<unsigned> Min = Version.getMinor(),
4039 SMin = Version.getSubminor();
4040 Args.push_back(
4041 llvm::ConstantInt::get(CGM.Int32Ty, getBaseMachOPlatformID(TT)));
4042 Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, Version.getMajor()));
4043 Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, Min.value_or(0)));
4044 Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, SMin.value_or(0)));
4045 };
4046
4047 assert(!Version.empty() && "unexpected empty version");
4048 EmitArgs(Version, CGM.getTarget().getTriple());
4049
4050 if (!CGM.IsPlatformVersionAtLeastFn) {
4051 llvm::FunctionType *FTy = llvm::FunctionType::get(
4052 CGM.Int32Ty, {CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty},
4053 false);
4055 CGM.CreateRuntimeFunction(FTy, "__isPlatformVersionAtLeast");
4056 }
4057
4058 llvm::Value *Check =
4060 return CGF.Builder.CreateICmpNE(Check,
4061 llvm::Constant::getNullValue(CGM.Int32Ty));
4062}
4063
4064llvm::Value *
4065CodeGenFunction::EmitBuiltinAvailable(const VersionTuple &Version) {
4066 // Darwin uses the new __isPlatformVersionAtLeast family of routines.
4067 if (CGM.getTarget().getTriple().isOSDarwin())
4068 return emitIsPlatformVersionAtLeast(*this, Version);
4069
4070 if (!CGM.IsOSVersionAtLeastFn) {
4071 llvm::FunctionType *FTy =
4072 llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false);
4073 CGM.IsOSVersionAtLeastFn =
4074 CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast");
4075 }
4076
4077 std::optional<unsigned> Min = Version.getMinor(),
4078 SMin = Version.getSubminor();
4079 llvm::Value *Args[] = {
4080 llvm::ConstantInt::get(CGM.Int32Ty, Version.getMajor()),
4081 llvm::ConstantInt::get(CGM.Int32Ty, Min.value_or(0)),
4082 llvm::ConstantInt::get(CGM.Int32Ty, SMin.value_or(0))};
4083
4084 llvm::Value *CallRes =
4085 EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args);
4086
4087 return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty));
4088}
4089
4091 const llvm::Triple &TT, const VersionTuple &TargetVersion) {
4092 VersionTuple FoundationDroppedInVersion;
4093 switch (TT.getOS()) {
4094 case llvm::Triple::IOS:
4095 case llvm::Triple::TvOS:
4096 FoundationDroppedInVersion = VersionTuple(/*Major=*/13);
4097 break;
4098 case llvm::Triple::WatchOS:
4099 FoundationDroppedInVersion = VersionTuple(/*Major=*/6);
4100 break;
4101 case llvm::Triple::Darwin:
4102 case llvm::Triple::MacOSX:
4103 FoundationDroppedInVersion = VersionTuple(/*Major=*/10, /*Minor=*/15);
4104 break;
4105 case llvm::Triple::XROS:
4106 // XROS doesn't need Foundation.
4107 return false;
4108 case llvm::Triple::DriverKit:
4109 // DriverKit doesn't need Foundation.
4110 return false;
4111 default:
4112 llvm_unreachable("Unexpected OS");
4113 }
4114 return TargetVersion < FoundationDroppedInVersion;
4115}
4116
4117void CodeGenModule::emitAtAvailableLinkGuard() {
4119 return;
4120 // @available requires CoreFoundation only on Darwin.
4121 if (!Target.getTriple().isOSDarwin())
4122 return;
4123 // @available doesn't need Foundation on macOS 10.15+, iOS/tvOS 13+, or
4124 // watchOS 6+.
4126 Target.getTriple(), Target.getPlatformMinVersion()))
4127 return;
4128 // Add -framework CoreFoundation to the linker commands. We still want to
4129 // emit the core foundation reference down below because otherwise if
4130 // CoreFoundation is not used in the code, the linker won't link the
4131 // framework.
4132 auto &Context = getLLVMContext();
4133 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
4134 llvm::MDString::get(Context, "CoreFoundation")};
4135 LinkerOptionsMetadata.push_back(llvm::MDNode::get(Context, Args));
4136 // Emit a reference to a symbol from CoreFoundation to ensure that
4137 // CoreFoundation is linked into the final binary.
4138 llvm::FunctionType *FTy =
4139 llvm::FunctionType::get(Int32Ty, {VoidPtrTy}, false);
4140 llvm::FunctionCallee CFFunc =
4141 CreateRuntimeFunction(FTy, "CFBundleGetVersionNumber");
4142
4143 llvm::FunctionType *CheckFTy = llvm::FunctionType::get(VoidTy, {}, false);
4144 llvm::FunctionCallee CFLinkCheckFuncRef = CreateRuntimeFunction(
4145 CheckFTy, "__clang_at_available_requires_core_foundation_framework",
4146 llvm::AttributeList(), /*Local=*/true);
4147 llvm::Function *CFLinkCheckFunc =
4148 cast<llvm::Function>(CFLinkCheckFuncRef.getCallee()->stripPointerCasts());
4149 if (CFLinkCheckFunc->empty()) {
4150 CFLinkCheckFunc->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage);
4151 CFLinkCheckFunc->setVisibility(llvm::GlobalValue::HiddenVisibility);
4152 CodeGenFunction CGF(*this);
4153 CGF.Builder.SetInsertPoint(CGF.createBasicBlock("", CFLinkCheckFunc));
4154 CGF.EmitNounwindRuntimeCall(CFFunc,
4155 llvm::Constant::getNullValue(VoidPtrTy));
4156 CGF.Builder.CreateUnreachable();
4157 addCompilerUsedGlobal(CFLinkCheckFunc);
4158 }
4159}
4160
Defines the clang::ASTContext interface.
#define V(N, I)
Defines the Diagnostic-related interfaces.
static llvm::Value * emitARCUnsafeClaimCallResult(CodeGenFunction &CGF, const Expr *e)
Given that the given expression is some sort of call (which does not return retained),...
Definition CGObjC.cpp:3109
static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl)
Definition CGObjC.cpp:1089
static bool shouldRetainObjCLifetime(Qualifiers::ObjCLifetime lifetime)
Definition CGObjC.cpp:2955
static bool shouldEmitSeparateBlockRetain(const Expr *e)
Determine whether it might be important to emit a separate objc_retain_block on the result of the giv...
Definition CGObjC.cpp:3135
static std::optional< llvm::Value * > tryEmitSpecializedAllocInit(CodeGenFunction &CGF, const ObjCMessageExpr *OME)
Instead of '[[MyClass alloc] init]', try to generate 'objc_alloc_init(MyClass)'.
Definition CGObjC.cpp:543
static llvm::Value * emitObjCValueOperation(CodeGenFunction &CGF, llvm::Value *value, llvm::Type *returnType, llvm::FunctionCallee &fn, StringRef fnName)
Perform an operation having the signature i8* (i8*) where a null input causes a no-op and returns nul...
Definition CGObjC.cpp:2321
llvm::function_ref< llvm::Value *(CodeGenFunction &CGF, llvm::Value *value)> ValueTransform
Definition CGObjC.cpp:3033
static llvm::Value * emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF, const Expr *e)
Definition CGObjC.cpp:3641
static llvm::Value * emitARCLoadOperation(CodeGenFunction &CGF, Address addr, llvm::Function *&fn, llvm::Intrinsic::ID IntID)
Perform an operation having the following signature: i8* (i8**)
Definition CGObjC.cpp:2269
static llvm::Constant * getNullForVariable(Address addr)
Given the address of a variable of pointer type, find the correct null to store into it.
Definition CGObjC.cpp:46
static void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF)
Definition CGObjC.cpp:2405
static const Expr * findWeakLValue(const Expr *E)
Given an expression of ObjC pointer type, check whether it was immediately loaded from an ARC __weak ...
Definition CGObjC.cpp:368
llvm::PointerIntPair< llvm::Value *, 1, bool > TryEmitResult
Definition CGObjC.cpp:37
static bool hasUnalignedAtomics(llvm::Triple::ArchType arch)
Determine whether the given architecture supports unaligned atomic accesses.
Definition CGObjC.cpp:875
static void emitARCCopyOperation(CodeGenFunction &CGF, Address dst, Address src, llvm::Function *&fn, llvm::Intrinsic::ID IntID)
Perform an operation having the following signature: void (i8**, i8**)
Definition CGObjC.cpp:2304
static void AppendFirstImpliedRuntimeProtocols(const ObjCProtocolDecl *PD, llvm::UniqueVector< const ObjCProtocolDecl * > &PDs)
Definition CGObjC.cpp:470
static TryEmitResult tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e)
Definition CGObjC.cpp:3507
static llvm::Value * emitOptimizedARCReturnCall(llvm::Value *value, bool IsRetainRV, CodeGenFunction &CGF)
Definition CGObjC.cpp:2444
static llvm::Value * emitCmdValueForGetterSetterBody(CodeGenFunction &CGF, ObjCMethodDecl *MD)
Definition CGObjC.cpp:1148
static llvm::Function * getARCIntrinsic(llvm::Intrinsic::ID IntID, CodeGenModule &CGM)
Definition CGObjC.cpp:2235
static bool isFoundationNeededForDarwinAvailabilityCheck(const llvm::Triple &TT, const VersionTuple &TargetVersion)
Definition CGObjC.cpp:4090
static bool shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message)
Decide whether to extend the lifetime of the receiver of a returns-inner-pointer message.
Definition CGObjC.cpp:309
static llvm::Value * emitARCStoreOperation(CodeGenFunction &CGF, Address addr, llvm::Value *value, llvm::Function *&fn, llvm::Intrinsic::ID IntID, bool ignored)
Perform an operation having the following signature: i8* (i8**, i8*)
Definition CGObjC.cpp:2280
static unsigned getBaseMachOPlatformID(const llvm::Triple &TT)
Definition CGObjC.cpp:4009
static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, LValue lvalue, QualType type)
Definition CGObjC.cpp:2970
static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, llvm::Value *RTF)
Definition CGObjC.cpp:2218
static std::optional< llvm::Value * > tryGenerateSpecializedMessageSend(CodeGenFunction &CGF, QualType ResultType, llvm::Value *Receiver, const CallArgList &Args, Selector Sel, const ObjCMethodDecl *method, bool isClassMessage)
The ObjC runtime may provide entrypoints that are likely to be faster than an ordinary message send o...
Definition CGObjC.cpp:395
static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, llvm::Triple::ArchType arch)
Return the maximum size that permits atomic accesses for the given architecture.
Definition CGObjC.cpp:883
static llvm::Value * emitARCRetainCallResult(CodeGenFunction &CGF, const Expr *e)
Given that the given expression is some sort of call (which does not return retained),...
Definition CGObjC.cpp:3095
static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, llvm::Value *returnAddr, ObjCIvarDecl *ivar, llvm::Constant *AtomicHelperFn)
emitCPPObjectAtomicGetterCall - Call the runtime function to copy the ivar into the resturn slot.
Definition CGObjC.cpp:1114
static llvm::Value * emitIsPlatformVersionAtLeast(CodeGenFunction &CGF, const VersionTuple &Version)
Definition CGObjC.cpp:4029
static void destroyARCStrongWithStore(CodeGenFunction &CGF, Address addr, QualType type)
Like CodeGenFunction::destroyARCStrong, but do it with a call.
Definition CGObjC.cpp:1741
static llvm::Value * emitARCRetainLoadOfScalar(CodeGenFunction &CGF, LValue lvalue, QualType type)
Definition CGObjC.cpp:3511
static void emitCXXDestructMethod(CodeGenFunction &CGF, ObjCImplementationDecl *impl)
Definition CGObjC.cpp:1748
static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, bool isAtomic, bool hasStrong)
emitStructGetterCall - Call the runtime function to load a property into the return value slot.
Definition CGObjC.cpp:845
static llvm::Value * emitARCValueOperation(CodeGenFunction &CGF, llvm::Value *value, llvm::Type *returnType, llvm::Function *&fn, llvm::Intrinsic::ID IntID, llvm::CallInst::TailCallKind tailKind=llvm::CallInst::TCK_None)
Perform an operation having the signature i8* (i8*) where a null input causes a no-op and returns nul...
Definition CGObjC.cpp:2245
static llvm::Value * emitARCOperationAfterCall(CodeGenFunction &CGF, llvm::Value *value, ValueTransform doAfterCall, ValueTransform doFallback)
Insert code immediately after a call.
Definition CGObjC.cpp:3039
static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, ObjCIvarDecl *ivar)
emitStructSetterCall - Call the runtime function to store the value from the first formal parameter i...
Definition CGObjC.cpp:1392
static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, ObjCIvarDecl *ivar, llvm::Constant *AtomicHelperFn)
emitCPPObjectAtomicSetterCall - Call the runtime function to store the value from the first formal pa...
Definition CGObjC.cpp:1435
static RValue AdjustObjCObjectType(CodeGenFunction &CGF, QualType ET, RValue Result)
Adjust the type of an Objective-C object that doesn't match up due to type erasure at various points,...
Definition CGObjC.cpp:291
static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID)
Definition CGObjC.cpp:1469
static bool UseOptimizedSetter(CodeGenModule &CGM)
Definition CGObjC.cpp:1493
static Decl::Kind getKind(const Decl *D)
Result
Implement __builtin_bit_cast and related operations.
llvm::json::Object Object
Defines the Objective-C statement AST node classes.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
void getObjCEncodingForType(QualType T, std::string &S, const FieldDecl *Field=nullptr, QualType *NotEncodedT=nullptr) const
Emit the Objective-CC type encoding for the given type T into S.
CanQualType VoidPtrTy
IdentifierTable & Idents
Definition ASTContext.h:805
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
SelectorTable & Selectors
Definition ASTContext.h:806
Qualifiers::GC getObjCGCAttrKind(QualType Ty) const
Return one of the GCNone, Weak or Strong Objective-C garbage collection attributes.
CanQualType BoolTy
QualType getObjCSelType() const
Retrieve the type that corresponds to the predefined Objective-C 'SEL' type.
TypeInfoChars getTypeInfoInChars(const Type *T) const
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType VoidTy
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
Expr * getLHS() const
Definition Expr.h:4091
Expr * getRHS() const
Definition Expr.h:4093
static BinaryOperator * Create(const ASTContext &C, Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, ExprValueKind VK, ExprObjectKind OK, SourceLocation opLoc, FPOptionsOverride FPFeatures)
Definition Expr.cpp:5094
Opcode getOpcode() const
Definition Expr.h:4086
bool canAvoidCopyToHeap() const
Definition Decl.h:4844
const BlockDecl * getBlockDecl() const
Definition Expr.h:6683
Represents a call to a C++ constructor.
Definition ExprCXX.h:1552
arg_iterator arg_begin()
Definition ExprCXX.h:1681
bool isElidable() const
Whether this construction is elidable.
Definition ExprCXX.h:1621
bool hadMultipleCandidates() const
Whether the referred constructor was resolved from an overloaded set having size greater than 1.
Definition ExprCXX.h:1626
static CXXConstructExpr * Create(const ASTContext &Ctx, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor, bool Elidable, ArrayRef< Expr * > Args, bool HadMultipleCandidates, bool ListInitialization, bool StdInitListInitialization, bool ZeroInitialization, CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange)
Create a C++ construction expression.
Definition ExprCXX.cpp:1182
bool isStdInitListInitialization() const
Whether this constructor call was written as list-initialization, but was interpreted as forming a st...
Definition ExprCXX.h:1645
bool requiresZeroInitialization() const
Whether this construction first requires zero-initialization before the initializer is called.
Definition ExprCXX.h:1654
arg_iterator arg_end()
Definition ExprCXX.h:1682
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1615
bool isListInitialization() const
Whether this constructor call was written as list-initialization.
Definition ExprCXX.h:1634
CXXConstructionKind getConstructionKind() const
Determine whether this constructor is actually constructing a base class (rather than a complete obje...
Definition ExprCXX.h:1663
A call to an overloaded operator written using operator syntax.
Definition ExprCXX.h:85
static CXXOperatorCallExpr * Create(const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn, ArrayRef< Expr * > Args, QualType Ty, ExprValueKind VK, SourceLocation OperatorLoc, FPOptionsOverride FPFeatures, ADLCallKind UsesADL=NotADL, bool IsReversed=false)
Definition ExprCXX.cpp:624
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2946
Expr * getCallee()
Definition Expr.h:3093
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition Expr.h:3679
CastKind getCastKind() const
Definition Expr.h:3723
Expr * getSubExpr()
Definition Expr.h:3729
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
static Address invalid()
Definition Address.h:176
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:253
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition Address.h:209
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition Address.h:276
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition Address.h:204
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
Definition CGValue.h:649
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:634
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:118
All available information about a concrete callee.
Definition CGCall.h:63
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:137
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition CGDebugInfo.h:59
void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc)
Emit metadata to indicate the end of a new lexical block and pop the current block.
void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc)
Emit metadata to indicate the beginning of a new lexical block and push the block onto the stack.
CGFunctionInfo - Class to encapsulate the information about a function definition.
Implements runtime-specific code generation functions.
virtual llvm::FunctionCallee GetCppAtomicObjectGetFunction()=0
API for atomic copying of qualified aggregates with non-trivial copy assignment (c++) in getter.
virtual llvm::FunctionCallee GetCppAtomicObjectSetFunction()=0
API for atomic copying of qualified aggregates with non-trivial copy assignment (c++) in setter.
virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, ReturnValueSlot ReturnSlot, QualType ResultType, Selector Sel, llvm::Value *Receiver, const CallArgList &CallArgs, const ObjCInterfaceDecl *Class=nullptr, const ObjCMethodDecl *Method=nullptr)=0
Generate an Objective-C message send operation.
CodeGen::RValue GeneratePossiblySpecializedMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return, QualType ResultType, Selector Sel, llvm::Value *Receiver, const CallArgList &Args, const ObjCInterfaceDecl *OID, const ObjCMethodDecl *Method, bool isClassMessage)
Generate an Objective-C message send operation.
Definition CGObjC.cpp:456
virtual CodeGen::RValue GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, ReturnValueSlot ReturnSlot, QualType ResultType, Selector Sel, const ObjCInterfaceDecl *Class, bool isCategoryImpl, llvm::Value *Self, bool IsClassMessage, const CallArgList &CallArgs, const ObjCMethodDecl *Method=nullptr)=0
Generate an Objective-C message send operation to the super class initiated in a method for Class and...
virtual llvm::FunctionCallee GetGetStructFunction()=0
virtual llvm::Value * GetClass(CodeGenFunction &CGF, const ObjCInterfaceDecl *OID)=0
GetClass - Return a reference to the class for the given interface decl.
virtual llvm::Value * EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF)
virtual llvm::FunctionCallee GetSetStructFunction()=0
std::vector< const ObjCProtocolDecl * > GetRuntimeProtocolList(ObjCProtocolDecl::protocol_iterator begin, ObjCProtocolDecl::protocol_iterator end)
Walk the list of protocol references from a class, category or protocol to traverse the DAG formed fr...
Definition CGObjC.cpp:484
CallArgList - Type for representing both the value and type of arguments in a call.
Definition CGCall.h:274
void add(RValue rvalue, QualType type)
Definition CGCall.h:302
An abstract representation of regular/ObjC call/message targets.
A non-RAII class containing all the information about a bound opaque value.
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited.
void ForceCleanup(std::initializer_list< llvm::Value ** > ValuesToReload={})
Force the emission of cleanups now, instead of waiting until this object is destroyed.
bool requiresCleanups() const
Determine whether this scope requires any cleanups.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * EmitObjCConsumeObject(QualType T, llvm::Value *Ptr)
Produce the code for a CK_ARCConsumeObject.
Definition CGObjC.cpp:2184
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
llvm::Value * EmitObjCAutorelease(llvm::Value *value, llvm::Type *returnType)
Autorelease the given object.
Definition CGObjC.cpp:2880
RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, ReturnValueSlot Return=ReturnValueSlot())
Definition CGObjC.cpp:591
void EmitARCMoveWeak(Address dst, Address src)
void @objc_moveWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition CGObjC.cpp:2724
llvm::Value * EmitARCRetainAutoreleaseReturnValue(llvm::Value *value)
Do a fused retain/autorelease of the given object.
Definition CGObjC.cpp:2631
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
emitDestroy - Immediately perform the destruction of the given object.
Definition CGDecl.cpp:2415
JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target)
The given basic block lies in the current EH scope, but may be a target of a potentially scope-crossi...
llvm::Value * EmitARCReclaimReturnedObject(const Expr *e, bool allowUnsafeClaim)
Definition CGObjC.cpp:3121
std::pair< LValue, llvm::Value * > EmitARCStoreAutoreleasing(const BinaryOperator *e)
Definition CGObjC.cpp:3711
llvm::Value * EmitARCUnsafeUnretainedScalarExpr(const Expr *expr)
EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to immediately releasing the resut of Emi...
Definition CGObjC.cpp:3650
llvm::Value * EmitObjCSelectorExpr(const ObjCSelectorExpr *E)
Emit a selector.
Definition CGObjC.cpp:275
SanitizerSet SanOpts
Sanitizers enabled for this function.
void EmitNullInitialization(Address DestPtr, QualType Ty)
EmitNullInitialization - Generate code to set a value of the given type to null, If the type contains...
void EmitARCInitWeak(Address addr, llvm::Value *value)
i8* @objc_initWeak(i8** addr, i8* value) Returns value.
Definition CGObjC.cpp:2695
llvm::Value * EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E)
Definition CGObjC.cpp:269
llvm::Type * ConvertType(QualType T)
llvm::Value * EmitObjCProtocolExpr(const ObjCProtocolExpr *E)
Definition CGObjC.cpp:283
llvm::CallBase * EmitCallOrInvoke(llvm::FunctionCallee Callee, ArrayRef< llvm::Value * > Args, const Twine &Name="")
Emits a call or invoke instruction to the given function, depending on the current state of the EH st...
Definition CGCall.cpp:5213
llvm::CallBase * EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
Definition CGCall.cpp:5203
ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc)
EmitLoadOfComplex - Load a complex number from the specified l-value.
llvm::Value * EmitARCAutoreleaseReturnValue(llvm::Value *value)
Autorelease the given object.
Definition CGObjC.cpp:2621
llvm::Value * EmitARCRetain(QualType type, llvm::Value *value)
Produce the code to do a retain.
Definition CGObjC.cpp:2360
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
llvm::Value * EmitARCAutorelease(llvm::Value *value)
Autorelease the given object.
Definition CGObjC.cpp:2612
llvm::Value * EmitARCRetainAutoreleaseScalarExpr(const Expr *expr)
Definition CGObjC.cpp:3540
void EmitARCDestroyWeak(Address addr)
void @objc_destroyWeak(i8** addr) Essentially objc_storeWeak(addr, nil).
Definition CGObjC.cpp:2713
void EmitObjCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
Definition CGObjC.cpp:2899
llvm::Value * EmitObjCAllocInit(llvm::Value *value, llvm::Type *resultType)
Definition CGObjC.cpp:2837
void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
Definition CGObjC.cpp:2161
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition CGExpr.cpp:4035
void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr)
Definition CGObjC.cpp:2948
void EmitAutoVarInit(const AutoVarEmission &emission)
Definition CGDecl.cpp:1945
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
llvm::Value * EmitObjCArrayLiteral(const ObjCArrayLiteral *E)
Definition CGObjC.cpp:265
const LangOptions & getLangOpts() const
llvm::Value * EmitARCRetainAutorelease(QualType type, llvm::Value *value)
Do a fused retain/autorelease of the given object.
Definition CGObjC.cpp:2643
llvm::Value * EmitARCStoreStrong(LValue lvalue, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition CGObjC.cpp:2577
llvm::Value * EmitARCRetainAutoreleasedReturnValue(llvm::Value *value)
Retain the given object which is the result of a function call.
Definition CGObjC.cpp:2495
void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD)
StartObjCMethod - Begin emission of an ObjCMethod.
Definition CGObjC.cpp:770
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition CGDecl.cpp:1483
llvm::Value * EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value)
Claim a possibly-autoreleased return value at +0.
Definition CGObjC.cpp:2507
llvm::Value * EmitObjCMRRAutoreleasePoolPush()
Produce the code to do an MRR version objc_autoreleasepool_push.
Definition CGObjC.cpp:2796
LValue EmitLValueForIvar(QualType ObjectTy, llvm::Value *Base, const ObjCIvarDecl *Ivar, unsigned CVRQualifiers)
Definition CGExpr.cpp:6864
void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, ObjCMethodDecl *MD, bool ctor)
Definition CGObjC.cpp:1784
llvm::Value * EmitObjCAllocWithZone(llvm::Value *value, llvm::Type *returnType)
Allocate the given objc object.
Definition CGObjC.cpp:2830
@ TCK_Store
Checking the destination of a store. Must be suitably sized and aligned.
llvm::Value * EmitObjCThrowOperand(const Expr *expr)
Definition CGObjC.cpp:3574
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition CGDecl.cpp:2272
llvm::AssertingVH< llvm::Instruction > AllocaInsertPt
AllocaInsertPoint - This is an instruction in the entry block before which we prefer to insert alloca...
void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
Definition CGObjC.cpp:2513
std::pair< LValue, llvm::Value * > EmitARCStoreUnsafeUnretained(const BinaryOperator *e, bool ignored)
Definition CGObjC.cpp:3661
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
Definition CGExpr.cpp:3925
void GenerateObjCGetter(ObjCImplementationDecl *IMP, const ObjCPropertyImplDecl *PID)
GenerateObjCGetter - Synthesize an Objective-C property getter function.
Definition CGObjC.cpp:1076
llvm::Value * EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface, const ObjCIvarDecl *Ivar)
Definition CGExpr.cpp:6856
void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy, AggValueSlot::Overlap_t MayOverlap, bool isVolatile=false)
EmitAggregateCopy - Emit an aggregate copy.
Address EmitCompoundStmtWithoutScope(const CompoundStmt &S, bool GetLast=false, AggValueSlot AVS=AggValueSlot::ignored())
Definition CGStmt.cpp:572
llvm::Value * EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty)
Definition CGObjC.cpp:3985
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
Definition CGExpr.cpp:2520
void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise)
Destroy a __strong variable.
Definition CGObjC.cpp:2542
void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *DominatingIP)
DeactivateCleanupBlock - Deactivates the given cleanup block.
void pushFullExprCleanup(CleanupKind kind, As... A)
pushFullExprCleanup - Push a cleanup to be run at the end of the current full-expression.
QualType TypeOfSelfObject()
TypeOfSelfObject - Return type of object that this self represents.
Definition CGObjC.cpp:1828
void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S)
Definition CGObjC.cpp:3720
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.
llvm::Value * EmitObjCBoxedExpr(const ObjCBoxedExpr *E)
EmitObjCBoxedExpr - This routine generates code to call the appropriate expression boxing method.
Definition CGObjC.cpp:65
void generateObjCSetterBody(const ObjCImplementationDecl *classImpl, const ObjCPropertyImplDecl *propImpl, llvm::Constant *AtomicHelperFn)
Definition CGObjC.cpp:1500
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs, const TrapReason *TR=nullptr)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
Definition CGExpr.cpp:4183
void EmitExtendGCLifetime(llvm::Value *object)
EmitExtendGCLifetime - Given a pointer to an Objective-C object, make sure it survives garbage collec...
Definition CGObjC.cpp:3748
LValue EmitDeclRefLValue(const DeclRefExpr *E)
Definition CGExpr.cpp:3592
void callCStructCopyConstructor(LValue Dst, LValue Src)
llvm::Value * EmitARCRetainBlock(llvm::Value *value, bool mandatory)
Retain the given block, with _Block_copy semantics.
Definition CGObjC.cpp:2381
llvm::Value * EmitObjCCollectionLiteral(const Expr *E, const ObjCMethodDecl *MethodWithObjects)
Definition CGObjC.cpp:132
llvm::Value * emitScalarConstant(const ConstantEmission &Constant, Expr *E)
Definition CGExpr.cpp:2042
llvm::Value * EmitARCRetainScalarExpr(const Expr *expr)
EmitARCRetainScalarExpr - Semantically equivalent to EmitARCRetainObject(e->getType(),...
Definition CGObjC.cpp:3525
void EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
Definition CGObjC.cpp:2157
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5359
CGPointerAuthInfo EmitPointerAuthInfo(const PointerAuthSchema &Schema, llvm::Value *StorageAddress, GlobalDecl SchemaDecl, QualType SchemaType)
Emit the concrete pointer authentication informaton for the given authentication schema.
SmallVector< llvm::OperandBundleDef, 1 > getBundlesForFunclet(llvm::Value *Callee)
Definition CGCall.cpp:5141
llvm::Value * EmitObjCAlloc(llvm::Value *value, llvm::Type *returnType)
Allocate the given objc object.
Definition CGObjC.cpp:2821
llvm::Value * LoadObjCSelf()
LoadObjCSelf - Load the value of self.
Definition CGObjC.cpp:1820
void GenerateObjCMethod(const ObjCMethodDecl *OMD)
Generate an Objective-C method.
Definition CGObjC.cpp:834
void callCStructMoveAssignmentOperator(LValue Dst, LValue Src)
llvm::Value * EmitARCLoadWeakRetained(Address addr)
i8* @objc_loadWeakRetained(i8** addr)
Definition CGObjC.cpp:2675
llvm::Value * emitPointerAuthResign(llvm::Value *Pointer, QualType PointerType, const CGPointerAuthInfo &CurAuthInfo, const CGPointerAuthInfo &NewAuthInfo, bool IsKnownNonNull)
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
void emitARCMoveAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr)
Definition CGObjC.cpp:2746
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition CGDecl.cpp:2218
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
Definition CGExpr.cpp:2772
llvm::Constant * GenerateObjCAtomicSetterCopyHelperFunction(const ObjCPropertyImplDecl *PID)
GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with non-trivial copy assignment...
Definition CGObjC.cpp:3765
void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S)
Definition CGObjC.cpp:2165
void EmitARCNoopIntrinsicUse(ArrayRef< llvm::Value * > values)
Emit a call to "clang.arc.noop.use", which consumes the result of a call that has operand bundle "cla...
Definition CGObjC.cpp:2211
void EmitAnyExprToMem(const Expr *E, Address Location, Qualifiers Quals, bool IsInitializer)
EmitAnyExprToMem - Emits the code necessary to evaluate an arbitrary expression into the given memory...
Definition CGExpr.cpp:307
RValue EmitAnyExpr(const Expr *E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
EmitAnyExpr - Emit code to compute the specified expression which can have any type.
Definition CGExpr.cpp:278
void EmitStmt(const Stmt *S, ArrayRef< const Attr * > Attrs={})
EmitStmt - Emit the code for the statement.
Definition CGStmt.cpp:58
bool AutoreleaseResult
In ARC, whether we should autorelease the return value.
CleanupKind getCleanupKind(QualType::DestructionKind kind)
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
uint64_t getCurrentProfileCount()
Get the profiler's current count.
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition CGObjC.cpp:2369
llvm::Type * ConvertTypeForMem(QualType T)
void generateObjCGetterBody(const ObjCImplementationDecl *classImpl, const ObjCPropertyImplDecl *propImpl, const ObjCMethodDecl *GetterMothodDecl, llvm::Constant *AtomicHelperFn)
Definition CGObjC.cpp:1162
llvm::Value * EmitARCRetainAutoreleaseNonBlock(llvm::Value *value)
Do a fused retain/autorelease of the given object.
Definition CGObjC.cpp:2660
llvm::Value * EmitObjCAutoreleasePoolPush()
Produce the code to do a objc_autoreleasepool_push.
Definition CGObjC.cpp:2756
void GenerateObjCSetter(ObjCImplementationDecl *IMP, const ObjCPropertyImplDecl *PID)
GenerateObjCSetter - Synthesize an Objective-C property setter function for the given property.
Definition CGObjC.cpp:1704
llvm::Value * EmitARCLoadWeak(Address addr)
i8* @objc_loadWeak(i8** addr) Essentially objc_autorelease(objc_loadWeakRetained(addr)).
Definition CGObjC.cpp:2668
CodeGenTypes & getTypes() const
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
llvm::Constant * GenerateObjCAtomicGetterCopyHelperFunction(const ObjCPropertyImplDecl *PID)
Definition CGObjC.cpp:3865
RawAddress CreateMemTemp(QualType T, const Twine &Name="tmp", RawAddress *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition CGExpr.cpp:194
llvm::Value * EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr)
Definition CGObjC.cpp:2192
AggValueSlot::Overlap_t getOverlapForReturnValue()
Determine whether a return value slot may overlap some other object.
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
llvm::Value * EmitBuiltinAvailable(const VersionTuple &Version)
Definition CGObjC.cpp:4065
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
Definition CGCall.cpp:4796
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitReturnStmt(const ReturnStmt &S)
EmitReturnStmt - Note that due to GCC extensions, this can have an operand if the function returns vo...
Definition CGStmt.cpp:1567
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition CGObjC.cpp:2556
uint64_t getProfileCount(const Stmt *S)
Get the profiler's count for the given statement.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
llvm::Value * EmitObjCStringLiteral(const ObjCStringLiteral *E)
Emits an instance of NSConstantString representing the object.
Definition CGObjC.cpp:52
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
ConstantEmission tryEmitAsConstant(const DeclRefExpr *RefExpr)
Try to emit a reference to the given value without producing it as an l-value.
Definition CGExpr.cpp:1939
void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr)
Produce the code to do a primitive release.
Definition CGObjC.cpp:2846
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
Definition CGExpr.cpp:1712
llvm::Value * EmitARCExtendBlockObject(const Expr *expr)
Definition CGObjC.cpp:3556
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
i8* @objc_storeWeak(i8** addr, i8* value) Returns value.
Definition CGObjC.cpp:2683
llvm::LLVMContext & getLLVMContext()
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
Definition CGObjC.cpp:1836
void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr)
Produce the code to do a primitive release.
Definition CGObjC.cpp:2766
llvm::Value * EmitObjCRetainNonBlock(llvm::Value *value, llvm::Type *returnType)
Retain the given object, with normal retain semantics.
Definition CGObjC.cpp:2890
void EmitARCCopyWeak(Address dst, Address src)
void @objc_copyWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition CGObjC.cpp:2733
void emitARCCopyAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr)
Definition CGObjC.cpp:2739
void EmitARCIntrinsicUse(ArrayRef< llvm::Value * > values)
Given a number of pointers, inform the optimizer that they're being intrinsically used up until this ...
Definition CGObjC.cpp:2199
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:643
This class organizes the cross-function state that is used while generating LLVM code.
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.
void addCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
ObjCEntrypoints & getObjCEntrypoints() const
const llvm::Triple & getTriple() const
ASTContext & getContext() const
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
llvm::LLVMContext & getLLVMContext()
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
llvm::FunctionCallee IsPlatformVersionAtLeastFn
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
Definition CGCall.cpp:731
llvm::Constant * tryEmitAbstract(const Expr *E, QualType T)
Try to emit the result of the given expression as an abstract constant.
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition CGCall.h:375
LValue - This represents an lvalue references.
Definition CGValue.h:183
CharUnits getAlignment() const
Definition CGValue.h:355
llvm::Value * getPointer(CodeGenFunction &CGF) const
const Qualifiers & getQuals() const
Definition CGValue.h:350
Address getAddress() const
Definition CGValue.h:373
ARCPreciseLifetime_t isARCPreciseLifetime() const
Definition CGValue.h:324
QualType getType() const
Definition CGValue.h:303
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition CGValue.h:42
static RValue get(llvm::Value *V)
Definition CGValue.h:99
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition CGValue.h:72
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition CGCall.h:381
virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const
Retrieve the address of a function to call immediately before calling objc_retainAutoreleasedReturnVa...
Definition TargetInfo.h:232
virtual bool markARCOptimizedReturnCallsAsNoTail() const
Determine whether a call to objc_retainAutoreleasedReturnValue or objc_unsafeClaimAutoreleasedReturnV...
Definition TargetInfo.h:238
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1746
body_range body()
Definition Stmt.h:1809
SourceLocation getLBracLoc() const
Definition Stmt.h:1863
SourceLocation getRBracLoc() const
Definition Stmt.h:1864
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1273
ValueDecl * getDecl()
Definition Expr.h:1341
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1637
SourceLocation getBodyRBrace() const
getBodyRBrace - Gets the right brace of the body, if a body exists.
SourceLocation getLocation() const
Definition DeclBase.h:447
DeclContext * getDeclContext()
Definition DeclBase.h:456
bool hasAttr() const
Definition DeclBase.h:585
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition ExprCXX.h:3661
This represents one expression.
Definition Expr.h:112
bool isXValue() const
Definition Expr.h:286
bool isGLValue() const
Definition Expr.h:287
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3095
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3086
QualType getType() const
Definition Expr.h:144
Represents difference between two FPOptions values.
Represents a member of a struct/union/class.
Definition Decl.h:3178
bool isBitField() const
Determines whether this field is a bitfield.
Definition Decl.h:3281
Represents a function declaration or definition.
Definition Decl.h:2018
static FunctionDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation NLoc, DeclarationName N, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin=false, bool isInlineSpecified=false, bool hasWrittenPrototype=true, ConstexprSpecKind ConstexprKind=ConstexprSpecKind::Unspecified, const AssociatedConstraint &TrailingRequiresClause={})
Definition Decl.h:2207
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
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:3856
clang::ObjCRuntime ObjCRuntime
This represents a decl that may have a name.
Definition Decl.h:274
@ VisibilityForValue
Do an LV computation for, ultimately, a non-type declaration.
Definition Decl.h:461
ObjCArrayLiteral - used for objective-c array containers; as in: @["Hello", NSApp,...
Definition ExprObjC.h:220
Expr * getElement(unsigned Index)
getElement - Return the Element at the specified index.
Definition ExprObjC.h:265
unsigned getNumElements() const
getNumElements - Return number of elements of objective-c array literal.
Definition ExprObjC.h:257
ObjCMethodDecl * getArrayWithObjectsMethod() const
Definition ExprObjC.h:274
Represents Objective-C's @synchronized statement.
Definition StmtObjC.h:303
Represents Objective-C's @throw statement.
Definition StmtObjC.h:358
Represents Objective-C's @try ... @catch ... @finally statement.
Definition StmtObjC.h:167
Represents Objective-C's @autoreleasepool Statement.
Definition StmtObjC.h:394
const Stmt * getSubStmt() const
Definition StmtObjC.h:405
ObjCBoxedExpr - used for generalized expression boxing.
Definition ExprObjC.h:159
ObjCMethodDecl * getBoxingMethod() const
Definition ExprObjC.h:181
ObjCContainerDecl - Represents a container for method declarations.
Definition DeclObjC.h:948
ObjCDictionaryLiteral - AST node to represent objective-c dictionary literals; as in:"name" : NSUserN...
Definition ExprObjC.h:342
unsigned getNumElements() const
getNumElements - Return number of elements of objective-c dictionary literal.
Definition ExprObjC.h:392
ObjCMethodDecl * getDictWithObjectsMethod() const
Definition ExprObjC.h:409
ObjCDictionaryElement getKeyValueElement(unsigned Index) const
Definition ExprObjC.h:394
Represents Objective-C's collection statement.
Definition StmtObjC.h:23
SourceLocation getBeginLoc() const LLVM_READONLY
Definition StmtObjC.h:57
const ObjCInterfaceDecl * getClassInterface() const
Definition DeclObjC.h:2486
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition DeclObjC.h:2597
Represents an ObjC class declaration.
Definition DeclObjC.h:1154
ObjCIvarDecl * all_declared_ivar_begin()
all_declared_ivar_begin - return first ivar declared in this class, its extensions and its implementa...
ObjCInterfaceDecl * getSuperClass() const
Definition DeclObjC.cpp:349
Represents typeof(type), a C23 feature and GCC extension, or `typeof_unqual(type),...
Definition TypeBase.h:7998
ObjCInterfaceDecl * getDecl() const
Get the declaration of this interface.
Definition Type.cpp:988
ObjCIvarDecl - Represents an ObjC instance variable.
Definition DeclObjC.h:1952
ObjCIvarDecl * getNextIvar()
Definition DeclObjC.h:1987
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition ExprObjC.h:580
An expression that sends a message to the given Objective-C object or class.
Definition ExprObjC.h:971
bool isDelegateInitCall() const
isDelegateInitCall - Answers whether this message send has been tagged as a "delegate init call",...
Definition ExprObjC.h:1452
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition ExprObjC.h:1299
Selector getSelector() const
Definition ExprObjC.cpp:301
@ SuperInstance
The receiver is the instance of the superclass object.
Definition ExprObjC.h:985
@ Instance
The receiver is an object instance.
Definition ExprObjC.h:979
@ SuperClass
The receiver is a superclass.
Definition ExprObjC.h:982
@ Class
The receiver is a class.
Definition ExprObjC.h:976
QualType getClassReceiver() const
Returns the type of a class message send, or NULL if the message is not a class message.
Definition ExprObjC.h:1318
llvm::iterator_range< arg_iterator > arguments()
Definition ExprObjC.h:1500
QualType getSuperType() const
Retrieve the type referred to by 'super'.
Definition ExprObjC.h:1375
const ObjCMethodDecl * getMethodDecl() const
Definition ExprObjC.h:1395
ReceiverKind getReceiverKind() const
Determine the kind of receiver that this message is being sent to.
Definition ExprObjC.h:1260
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
ImplicitParamDecl * getSelfDecl() const
Definition DeclObjC.h:418
ArrayRef< ParmVarDecl * > parameters() const
Definition DeclObjC.h:373
const ObjCPropertyDecl * findPropertyDecl(bool CheckOverrides=true) const
Returns the property associated with this method's selector.
param_const_iterator param_end() const
Definition DeclObjC.h:358
param_const_iterator param_begin() const
Definition DeclObjC.h:354
Stmt * getBody() const override
Retrieve the body of this method, if it has one.
Definition DeclObjC.cpp:906
SourceLocation getEndLoc() const LLVM_READONLY
const ParmVarDecl *const * param_const_iterator
Definition DeclObjC.h:349
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclObjC.h:282
bool isDirectMethod() const
True if the method is tagged as objc_direct.
Definition DeclObjC.cpp:868
Selector getSelector() const
Definition DeclObjC.h:327
ImplicitParamDecl * getCmdDecl() const
Definition DeclObjC.h:420
bool isInstanceMethod() const
Definition DeclObjC.h:426
ObjCMethodFamily getMethodFamily() const
Determines the family of this method.
void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID)
createImplicitParams - Used to lazily create the self and cmd implicit parameters.
QualType getReturnType() const
Definition DeclObjC.h:329
bool isClassMethod() const
Definition DeclObjC.h:434
ObjCInterfaceDecl * getClassInterface()
bool isExpressibleAsConstantInitializer() const
Definition ExprObjC.h:68
Represents a pointer to an Objective C object.
Definition TypeBase.h:8054
const ObjCObjectType * getObjectType() const
Gets the type pointed to by this ObjC pointer.
Definition TypeBase.h:8091
QualType getPointeeType() const
Gets the type pointed to by this ObjC pointer.
Definition TypeBase.h:8066
const ObjCInterfaceType * getInterfaceType() const
If this pointer points to an Objective C @interface type, gets the type for that interface.
Definition Type.cpp:1889
Represents one property declaration in an Objective-C interface.
Definition DeclObjC.h:731
bool isAtomic() const
isAtomic - Return true if the property is atomic.
Definition DeclObjC.h:843
SetterKind getSetterKind() const
getSetterKind - Return the method used for doing assignment in the property setter.
Definition DeclObjC.h:873
QualType getType() const
Definition DeclObjC.h:804
ObjCPropertyAttribute::Kind getPropertyAttributes() const
Definition DeclObjC.h:815
ObjCPropertyImplDecl - Represents implementation declaration of a property in a class or category imp...
Definition DeclObjC.h:2805
ObjCIvarDecl * getPropertyIvarDecl() const
Definition DeclObjC.h:2879
Expr * getSetterCXXAssignment() const
Definition DeclObjC.h:2915
ObjCPropertyDecl * getPropertyDecl() const
Definition DeclObjC.h:2870
Expr * getGetterCXXConstructor() const
Definition DeclObjC.h:2907
ObjCMethodDecl * getSetterMethodDecl() const
Definition DeclObjC.h:2904
ObjCMethodDecl * getGetterMethodDecl() const
Definition DeclObjC.h:2901
Represents an Objective-C protocol declaration.
Definition DeclObjC.h:2084
bool isNonRuntimeProtocol() const
This is true iff the protocol is tagged with the objc_non_runtime_protocol attribute.
ObjCProtocolList::iterator protocol_iterator
Definition DeclObjC.h:2158
ObjCProtocolDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this Objective-C protocol.
Definition DeclObjC.h:2297
protocol_range protocols() const
Definition DeclObjC.h:2161
ObjCProtocolExpr used for protocol expression in Objective-C.
Definition ExprObjC.h:536
ObjCProtocolDecl * getProtocol() const
Definition ExprObjC.h:553
The basic abstraction for the target Objective-C runtime.
Definition ObjCRuntime.h:28
bool hasAtomicCopyHelper() const
bool hasNativeARC() const
Does this runtime natively provide the ARC entrypoints?
bool hasOptimizedSetter() const
Does this runtime supports optimized setter entrypoints?
ObjCSelectorExpr used for @selector in Objective-C.
Definition ExprObjC.h:486
Selector getSelector() const
Definition ExprObjC.h:500
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition ExprObjC.h:84
StringLiteral * getString()
Definition ExprObjC.h:96
Represents a parameter to a function.
Definition Decl.h:1808
static ParmVarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
Definition Decl.cpp:2952
Pointer-authentication qualifiers.
Definition TypeBase.h:152
semantics_iterator semantics_end()
Definition Expr.h:6868
semantics_iterator semantics_begin()
Definition Expr.h:6864
const Expr *const * const_semantics_iterator
Definition Expr.h:6863
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
Definition Expr.h:6851
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8520
PointerAuthQualifier getPointerAuth() const
Definition TypeBase.h:1464
QualType withConst() const
Definition TypeBase.h:1170
void addConst()
Add the const type qualifier to this QualType.
Definition TypeBase.h:1167
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:3084
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition TypeBase.h:1449
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition TypeBase.h:8621
QualType getCanonicalType() const
Definition TypeBase.h:8488
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8530
bool hasNonTrivialObjCLifetime() const
Definition TypeBase.h:1453
@ PCK_Struct
The type is a struct containing a field whose type is neither PCK_Trivial nor PCK_VolatileTrivial.
Definition TypeBase.h:1528
The collection of all-type qualifiers we support.
Definition TypeBase.h:331
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition TypeBase.h:361
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition TypeBase.h:354
@ OCL_None
There is no lifetime qualification on this type.
Definition TypeBase.h:350
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition TypeBase.h:364
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition TypeBase.h:367
ObjCLifetime getObjCLifetime() const
Definition TypeBase.h:545
void setObjCLifetime(ObjCLifetime type)
Definition TypeBase.h:548
static ReturnStmt * Create(const ASTContext &Ctx, SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
Create a return statement.
Definition Stmt.cpp:1290
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
Selector getNullarySelector(const IdentifierInfo *ID)
Selector getSelector(unsigned NumArgs, const IdentifierInfo **IIV)
Can create any sort of selector.
Smart pointer class that efficiently represents Objective-C method names.
StringRef getNameForSlot(unsigned argIndex) const
Retrieve the name at a given position in the selector.
const IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
bool isKeywordSelector() const
ObjCMethodFamily getMethodFamily() const
Derive the conventional family of this method.
bool isUnarySelector() const
unsigned getNumArgs() const
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
Definition Stmt.h:86
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
Exposes information about the current target.
Definition TargetInfo.h:227
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Token - This structure provides full information about a lexed token.
Definition Token.h:36
bool isBlockPointerType() const
Definition TypeBase.h:8693
bool isVoidType() const
Definition TypeBase.h:9039
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isPointerType() const
Definition TypeBase.h:8673
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9333
bool isReferenceType() const
Definition TypeBase.h:8697
const ObjCObjectPointerType * getAsObjCInterfacePointerType() const
Definition Type.cpp:1950
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isAtomicType() const
Definition TypeBase.h:8865
bool isObjCObjectPointerType() const
Definition TypeBase.h:8852
bool isObjCClassType() const
Definition TypeBase.h:8891
bool isRecordType() const
Definition TypeBase.h:8800
bool isObjCRetainableType() const
Definition Type.cpp:5416
bool hasPointerRepresentation() const
Whether this type is represented natively as a pointer.
Definition TypeBase.h:9210
bool hasBooleanRepresentation() const
Determine whether this type has a boolean representation – i.e., it is a boolean type,...
Definition Type.cpp:2444
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
static UnaryOperator * Create(const ASTContext &C, Expr *input, Opcode opc, QualType type, ExprValueKind VK, ExprObjectKind OK, SourceLocation l, bool CanOverflow, FPOptionsOverride FPFeatures)
Definition Expr.cpp:5151
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
bool isARCPseudoStrong() const
Determine whether this variable is an ARC pseudo-__strong variable.
Definition Decl.h:1560
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
llvm::Function * getNonTrivialCStructCopyConstructor(CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, bool IsVolatile, QualType QT)
Returns the copy constructor for a C struct with non-trivially copyable fields, generating it if nece...
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
llvm::Function * getNonTrivialCStructMoveAssignmentOperator(CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, bool IsVolatile, QualType QT)
Return the move assignment operator for a C struct with non-trivially copyable fields,...
ARCPreciseLifetime_t
Does an ARC strong l-value have precise lifetime?
Definition CGValue.h:136
@ ARCImpreciseLifetime
Definition CGValue.h:137
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
@ OK_Ordinary
An ordinary object is located at an address in memory.
Definition Specifiers.h:152
@ Self
'self' clause, allowed on Compute and Combined Constructs, plus 'update'.
@ SC_Static
Definition Specifiers.h:253
@ SC_None
Definition Specifiers.h:251
Selector GetUnarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing an unary selector.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
CastKind
CastKind - The kind of operation required for a conversion.
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:136
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:140
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5972
A jump destination is an abstract label, branching to which may require a jump out through normal cle...
llvm::Function * objc_retainAutoreleasedReturnValue
id objc_retainAutoreleasedReturnValue(id);
llvm::InlineAsm * retainAutoreleasedReturnValueMarker
A void(void) inline asm to use to mark that the return value of a call will be immediately retain.
llvm::Function * objc_unsafeClaimAutoreleasedReturnValue
id objc_unsafeClaimAutoreleasedReturnValue(id);
Expr * Value
The value of the dictionary element.
Definition ExprObjC.h:300
Expr * Key
The key for the dictionary element.
Definition ExprObjC.h:297