clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CGObjC.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CGObjC.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "CGDebugInfo.h" |
14 | #include "CGObjCRuntime.h" |
15 | #include "CodeGenFunction.h" |
16 | #include "CodeGenModule.h" |
17 | #include "ConstantEmitter.h" |
18 | #include "TargetInfo.h" |
19 | #include "clang/AST/ASTContext.h" |
20 | #include "clang/AST/Attr.h" |
21 | #include "clang/AST/DeclObjC.h" |
22 | #include "clang/AST/StmtObjC.h" |
23 | #include "clang/Basic/Diagnostic.h" |
24 | #include "clang/CodeGen/CGFunctionInfo.h" |
25 | #include "llvm/ADT/STLExtras.h" |
26 | #include "llvm/Analysis/ObjCARCUtil.h" |
27 | #include "llvm/BinaryFormat/MachO.h" |
28 | #include "llvm/IR/DataLayout.h" |
29 | #include "llvm/IR/InlineAsm.h" |
30 | using namespace clang; |
31 | using namespace CodeGen; |
32 | |
33 | typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; |
34 | static TryEmitResult |
35 | tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); |
36 | static RValue AdjustObjCObjectType(CodeGenFunction &CGF, |
37 | QualType ET, |
38 | RValue Result); |
39 | |
40 | |
41 | |
42 | static llvm::Constant *getNullForVariable(Address addr) { |
43 | llvm::Type *type = addr.getElementType(); |
44 | return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); |
45 | } |
46 | |
47 | |
48 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) |
49 | { |
50 | llvm::Constant *C = |
51 | CGM.getObjCRuntime().GenerateConstantString(E->getString()).getPointer(); |
52 | |
53 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
54 | } |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | llvm::Value * |
62 | CodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
63 | |
64 | |
65 | const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); |
66 | const Expr *SubExpr = E->getSubExpr(); |
67 | |
68 | if (E->isExpressibleAsConstantInitializer()) { |
69 | ConstantEmitter ConstEmitter(CGM); |
70 | return ConstEmitter.tryEmitAbstract(E, E->getType()); |
71 | } |
72 | |
73 | assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); |
74 | Selector Sel = BoxingMethod->getSelector(); |
75 | |
76 | |
77 | |
78 | |
79 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
80 | const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); |
81 | llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); |
82 | |
83 | CallArgList Args; |
84 | const ParmVarDecl *ArgDecl = *BoxingMethod->param_begin(); |
85 | QualType ArgQT = ArgDecl->getType().getUnqualifiedType(); |
86 | |
87 | |
88 | |
89 | const QualType ValueType(SubExpr->getType().getCanonicalType()); |
90 | if (ValueType->isObjCBoxableRecordType()) { |
91 | |
92 | |
93 | Address Temporary = CreateMemTemp(SubExpr->getType()); |
94 | EmitAnyExprToMem(SubExpr, Temporary, Qualifiers(), true); |
95 | Address BitCast = Builder.CreateBitCast(Temporary, ConvertType(ArgQT)); |
96 | Args.add(RValue::get(BitCast.getPointer()), ArgQT); |
97 | |
98 | |
99 | std::string Str; |
100 | getContext().getObjCEncodingForType(ValueType, Str); |
101 | llvm::Constant *GV = CGM.GetAddrOfConstantCString(Str).getPointer(); |
102 | |
103 | |
104 | const ParmVarDecl *EncodingDecl = BoxingMethod->parameters()[1]; |
105 | QualType EncodingQT = EncodingDecl->getType().getUnqualifiedType(); |
106 | llvm::Value *Cast = Builder.CreateBitCast(GV, ConvertType(EncodingQT)); |
107 | |
108 | Args.add(RValue::get(Cast), EncodingQT); |
109 | } else { |
110 | Args.add(EmitAnyExpr(SubExpr), ArgQT); |
111 | } |
112 | |
113 | RValue result = Runtime.GenerateMessageSend( |
114 | *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver, |
115 | Args, ClassDecl, BoxingMethod); |
116 | return Builder.CreateBitCast(result.getScalarVal(), |
117 | ConvertType(E->getType())); |
118 | } |
119 | |
120 | llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, |
121 | const ObjCMethodDecl *MethodWithObjects) { |
122 | ASTContext &Context = CGM.getContext(); |
123 | const ObjCDictionaryLiteral *DLE = nullptr; |
124 | const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); |
125 | if (!ALE) |
126 | DLE = cast<ObjCDictionaryLiteral>(E); |
127 | |
128 | |
129 | uint64_t NumElements = |
130 | ALE ? ALE->getNumElements() : DLE->getNumElements(); |
131 | if (NumElements == 0 && CGM.getLangOpts().ObjCRuntime.hasEmptyCollections()) { |
132 | StringRef ConstantName = ALE ? "__NSArray0__" : "__NSDictionary0__"; |
133 | QualType IdTy(CGM.getContext().getObjCIdType()); |
134 | llvm::Constant *Constant = |
135 | CGM.CreateRuntimeVariable(ConvertType(IdTy), ConstantName); |
136 | LValue LV = MakeNaturalAlignAddrLValue(Constant, IdTy); |
137 | llvm::Value *Ptr = EmitLoadOfScalar(LV, E->getBeginLoc()); |
138 | cast<llvm::LoadInst>(Ptr)->setMetadata( |
139 | CGM.getModule().getMDKindID("invariant.load"), |
140 | llvm::MDNode::get(getLLVMContext(), None)); |
141 | return Builder.CreateBitCast(Ptr, ConvertType(E->getType())); |
142 | } |
143 | |
144 | |
145 | llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), |
146 | NumElements); |
147 | QualType ElementType = Context.getObjCIdType().withConst(); |
148 | QualType ElementArrayType |
149 | = Context.getConstantArrayType(ElementType, APNumElements, nullptr, |
150 | ArrayType::Normal, 0); |
151 | |
152 | |
153 | Address Objects = CreateMemTemp(ElementArrayType, "objects"); |
154 | Address Keys = Address::invalid(); |
155 | if (DLE) |
156 | Keys = CreateMemTemp(ElementArrayType, "keys"); |
157 | |
158 | |
159 | |
160 | SmallVector<llvm::Value *, 16> NeededObjects; |
161 | bool TrackNeededObjects = |
162 | (getLangOpts().ObjCAutoRefCount && |
163 | CGM.getCodeGenOpts().OptimizationLevel != 0); |
164 | |
165 | |
166 | for (uint64_t i = 0; i < NumElements; i++) { |
167 | if (ALE) { |
168 | |
169 | const Expr *Rhs = ALE->getElement(i); |
170 | LValue LV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i), |
171 | ElementType, AlignmentSource::Decl); |
172 | |
173 | llvm::Value *value = EmitScalarExpr(Rhs); |
174 | EmitStoreThroughLValue(RValue::get(value), LV, true); |
175 | if (TrackNeededObjects) { |
176 | NeededObjects.push_back(value); |
177 | } |
178 | } else { |
179 | |
180 | const Expr *Key = DLE->getKeyValueElement(i).Key; |
181 | LValue KeyLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Keys, i), |
182 | ElementType, AlignmentSource::Decl); |
183 | llvm::Value *keyValue = EmitScalarExpr(Key); |
184 | EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, true); |
185 | |
186 | |
187 | const Expr *Value = DLE->getKeyValueElement(i).Value; |
188 | LValue ValueLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i), |
189 | ElementType, AlignmentSource::Decl); |
190 | llvm::Value *valueValue = EmitScalarExpr(Value); |
191 | EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, true); |
192 | if (TrackNeededObjects) { |
193 | NeededObjects.push_back(keyValue); |
194 | NeededObjects.push_back(valueValue); |
195 | } |
196 | } |
197 | } |
198 | |
199 | |
200 | CallArgList Args; |
201 | ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); |
202 | const ParmVarDecl *argDecl = *PI++; |
203 | QualType ArgQT = argDecl->getType().getUnqualifiedType(); |
204 | Args.add(RValue::get(Objects.getPointer()), ArgQT); |
205 | if (DLE) { |
206 | argDecl = *PI++; |
207 | ArgQT = argDecl->getType().getUnqualifiedType(); |
208 | Args.add(RValue::get(Keys.getPointer()), ArgQT); |
209 | } |
210 | argDecl = *PI; |
211 | ArgQT = argDecl->getType().getUnqualifiedType(); |
212 | llvm::Value *Count = |
213 | llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); |
214 | Args.add(RValue::get(Count), ArgQT); |
215 | |
216 | |
217 | Selector Sel = MethodWithObjects->getSelector(); |
218 | QualType ResultType = E->getType(); |
219 | const ObjCObjectPointerType *InterfacePointerType |
220 | = ResultType->getAsObjCInterfacePointerType(); |
221 | ObjCInterfaceDecl *Class |
222 | = InterfacePointerType->getObjectType()->getInterface(); |
223 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
224 | llvm::Value *Receiver = Runtime.GetClass(*this, Class); |
225 | |
226 | |
227 | RValue result = Runtime.GenerateMessageSend( |
228 | *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel, |
229 | Receiver, Args, Class, MethodWithObjects); |
230 | |
231 | |
232 | |
233 | |
234 | |
235 | if (TrackNeededObjects) { |
236 | EmitARCIntrinsicUse(NeededObjects); |
237 | } |
238 | |
239 | return Builder.CreateBitCast(result.getScalarVal(), |
240 | ConvertType(E->getType())); |
241 | } |
242 | |
243 | llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { |
244 | return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); |
245 | } |
246 | |
247 | llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( |
248 | const ObjCDictionaryLiteral *E) { |
249 | return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); |
250 | } |
251 | |
252 | |
253 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
254 | |
255 | |
256 | |
257 | |
258 | return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); |
259 | } |
260 | |
261 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
262 | |
263 | return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); |
264 | } |
265 | |
266 | |
267 | |
268 | |
269 | static RValue AdjustObjCObjectType(CodeGenFunction &CGF, QualType ExpT, |
270 | RValue Result) { |
271 | if (!ExpT->isObjCRetainableType()) |
272 | return Result; |
273 | |
274 | |
275 | llvm::Type *ExpLLVMTy = CGF.ConvertType(ExpT); |
276 | if (ExpLLVMTy == Result.getScalarVal()->getType()) |
277 | return Result; |
278 | |
279 | |
280 | return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), |
281 | ExpLLVMTy)); |
282 | } |
283 | |
284 | |
285 | |
286 | static bool |
287 | shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { |
288 | switch (message->getReceiverKind()) { |
289 | |
290 | |
291 | |
292 | case ObjCMessageExpr::Instance: { |
293 | const Expr *receiver = message->getInstanceReceiver(); |
294 | |
295 | |
296 | if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) { |
297 | if (opaque->getSourceExpr()) |
298 | receiver = opaque->getSourceExpr()->IgnoreParens(); |
299 | } |
300 | |
301 | const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); |
302 | if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; |
303 | receiver = ice->getSubExpr()->IgnoreParens(); |
304 | |
305 | |
306 | if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) { |
307 | if (opaque->getSourceExpr()) |
308 | receiver = opaque->getSourceExpr()->IgnoreParens(); |
309 | } |
310 | |
311 | |
312 | if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
313 | return true; |
314 | |
315 | |
316 | if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) |
317 | return false; |
318 | |
319 | |
320 | const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); |
321 | if (!declRef) return true; |
322 | const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); |
323 | if (!var) return true; |
324 | |
325 | |
326 | |
327 | return (var->hasLocalStorage() && |
328 | !var->hasAttr<ObjCPreciseLifetimeAttr>()); |
329 | } |
330 | |
331 | case ObjCMessageExpr::Class: |
332 | case ObjCMessageExpr::SuperClass: |
333 | |
334 | return false; |
335 | |
336 | case ObjCMessageExpr::SuperInstance: |
337 | |
338 | return false; |
339 | } |
340 | |
341 | llvm_unreachable("invalid receiver kind"); |
342 | } |
343 | |
344 | |
345 | |
346 | static const Expr *findWeakLValue(const Expr *E) { |
347 | assert(E->getType()->isObjCRetainableType()); |
348 | E = E->IgnoreParens(); |
349 | if (auto CE = dyn_cast<CastExpr>(E)) { |
350 | if (CE->getCastKind() == CK_LValueToRValue) { |
351 | if (CE->getSubExpr()->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
352 | return CE->getSubExpr(); |
353 | } |
354 | } |
355 | |
356 | return nullptr; |
357 | } |
358 | |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | |
373 | static Optional<llvm::Value *> |
374 | tryGenerateSpecializedMessageSend(CodeGenFunction &CGF, QualType ResultType, |
375 | llvm::Value *Receiver, |
376 | const CallArgList& Args, Selector Sel, |
377 | const ObjCMethodDecl *method, |
378 | bool isClassMessage) { |
379 | auto &CGM = CGF.CGM; |
380 | if (!CGM.getCodeGenOpts().ObjCConvertMessagesToRuntimeCalls) |
381 | return None; |
382 | |
383 | auto &Runtime = CGM.getLangOpts().ObjCRuntime; |
384 | switch (Sel.getMethodFamily()) { |
385 | case OMF_alloc: |
386 | if (isClassMessage && |
387 | Runtime.shouldUseRuntimeFunctionsForAlloc() && |
388 | ResultType->isObjCObjectPointerType()) { |
389 | |
390 | |
391 | if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "alloc") |
392 | return CGF.EmitObjCAlloc(Receiver, CGF.ConvertType(ResultType)); |
393 | |
394 | |
395 | if (Sel.isKeywordSelector() && Sel.getNumArgs() == 1 && |
396 | Args.size() == 1 && Args.front().getType()->isPointerType() && |
397 | Sel.getNameForSlot(0) == "allocWithZone") { |
398 | const llvm::Value* arg = Args.front().getKnownRValue().getScalarVal(); |
399 | if (isa<llvm::ConstantPointerNull>(arg)) |
400 | return CGF.EmitObjCAllocWithZone(Receiver, |
401 | CGF.ConvertType(ResultType)); |
402 | return None; |
403 | } |
404 | } |
405 | break; |
406 | |
407 | case OMF_autorelease: |
408 | if (ResultType->isObjCObjectPointerType() && |
409 | CGM.getLangOpts().getGC() == LangOptions::NonGC && |
410 | Runtime.shouldUseARCFunctionsForRetainRelease()) |
411 | return CGF.EmitObjCAutorelease(Receiver, CGF.ConvertType(ResultType)); |
412 | break; |
413 | |
414 | case OMF_retain: |
415 | if (ResultType->isObjCObjectPointerType() && |
416 | CGM.getLangOpts().getGC() == LangOptions::NonGC && |
417 | Runtime.shouldUseARCFunctionsForRetainRelease()) |
418 | return CGF.EmitObjCRetainNonBlock(Receiver, CGF.ConvertType(ResultType)); |
419 | break; |
420 | |
421 | case OMF_release: |
422 | if (ResultType->isVoidType() && |
423 | CGM.getLangOpts().getGC() == LangOptions::NonGC && |
424 | Runtime.shouldUseARCFunctionsForRetainRelease()) { |
425 | CGF.EmitObjCRelease(Receiver, ARCPreciseLifetime); |
426 | return nullptr; |
427 | } |
428 | break; |
429 | |
430 | default: |
431 | break; |
432 | } |
433 | return None; |
434 | } |
435 | |
436 | CodeGen::RValue CGObjCRuntime::GeneratePossiblySpecializedMessageSend( |
437 | CodeGenFunction &CGF, ReturnValueSlot Return, QualType ResultType, |
438 | Selector Sel, llvm::Value *Receiver, const CallArgList &Args, |
439 | const ObjCInterfaceDecl *OID, const ObjCMethodDecl *Method, |
440 | bool isClassMessage) { |
441 | if (Optional<llvm::Value *> SpecializedResult = |
442 | tryGenerateSpecializedMessageSend(CGF, ResultType, Receiver, Args, |
443 | Sel, Method, isClassMessage)) { |
444 | return RValue::get(SpecializedResult.getValue()); |
445 | } |
446 | return GenerateMessageSend(CGF, Return, ResultType, Sel, Receiver, Args, OID, |
447 | Method); |
448 | } |
449 | |
450 | static void AppendFirstImpliedRuntimeProtocols( |
451 | const ObjCProtocolDecl *PD, |
452 | llvm::UniqueVector<const ObjCProtocolDecl *> &PDs) { |
453 | if (!PD->isNonRuntimeProtocol()) { |
454 | const auto *Can = PD->getCanonicalDecl(); |
455 | PDs.insert(Can); |
456 | return; |
457 | } |
458 | |
459 | for (const auto *ParentPD : PD->protocols()) |
460 | AppendFirstImpliedRuntimeProtocols(ParentPD, PDs); |
461 | } |
462 | |
463 | std::vector<const ObjCProtocolDecl *> |
464 | CGObjCRuntime::GetRuntimeProtocolList(ObjCProtocolDecl::protocol_iterator begin, |
465 | ObjCProtocolDecl::protocol_iterator end) { |
466 | std::vector<const ObjCProtocolDecl *> RuntimePds; |
467 | llvm::DenseSet<const ObjCProtocolDecl *> NonRuntimePDs; |
468 | |
469 | for (; begin != end; ++begin) { |
470 | const auto *It = *begin; |
471 | const auto *Can = It->getCanonicalDecl(); |
472 | if (Can->isNonRuntimeProtocol()) |
473 | NonRuntimePDs.insert(Can); |
474 | else |
475 | RuntimePds.push_back(Can); |
476 | } |
477 | |
478 | |
479 | if (NonRuntimePDs.empty()) |
480 | return RuntimePds; |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | llvm::UniqueVector<const ObjCProtocolDecl *> FirstImpliedProtos; |
487 | for (const auto *PD : NonRuntimePDs) |
488 | AppendFirstImpliedRuntimeProtocols(PD, FirstImpliedProtos); |
489 | |
490 | |
491 | |
492 | llvm::DenseSet<const ObjCProtocolDecl *> AllImpliedProtocols; |
493 | for (const auto *PD : RuntimePds) { |
494 | const auto *Can = PD->getCanonicalDecl(); |
495 | AllImpliedProtocols.insert(Can); |
496 | Can->getImpliedProtocols(AllImpliedProtocols); |
497 | } |
498 | |
499 | |
500 | |
501 | |
502 | for (const auto *PD : FirstImpliedProtos) { |
503 | PD->getImpliedProtocols(AllImpliedProtocols); |
504 | } |
505 | |
506 | |
507 | |
508 | |
509 | |
510 | for (const auto *PD : FirstImpliedProtos) { |
511 | if (!AllImpliedProtocols.contains(PD)) { |
512 | RuntimePds.push_back(PD); |
513 | } |
514 | } |
515 | |
516 | return RuntimePds; |
517 | } |
518 | |
519 | |
520 | |
521 | |
522 | static Optional<llvm::Value *> |
523 | tryEmitSpecializedAllocInit(CodeGenFunction &CGF, const ObjCMessageExpr *OME) { |
524 | auto &Runtime = CGF.getLangOpts().ObjCRuntime; |
525 | if (!Runtime.shouldUseRuntimeFunctionForCombinedAllocInit()) |
526 | return None; |
527 | |
528 | |
529 | Selector Sel = OME->getSelector(); |
530 | if (OME->getReceiverKind() != ObjCMessageExpr::Instance || |
531 | !OME->getType()->isObjCObjectPointerType() || !Sel.isUnarySelector() || |
532 | Sel.getNameForSlot(0) != "init") |
533 | return None; |
534 | |
535 | |
536 | |
537 | auto *SubOME = |
538 | dyn_cast<ObjCMessageExpr>(OME->getInstanceReceiver()->IgnoreParenCasts()); |
539 | if (!SubOME) |
540 | return None; |
541 | Selector SubSel = SubOME->getSelector(); |
542 | |
543 | if (!SubOME->getType()->isObjCObjectPointerType() || |
544 | !SubSel.isUnarySelector() || SubSel.getNameForSlot(0) != "alloc") |
545 | return None; |
546 | |
547 | llvm::Value *Receiver = nullptr; |
548 | switch (SubOME->getReceiverKind()) { |
549 | case ObjCMessageExpr::Instance: |
550 | if (!SubOME->getInstanceReceiver()->getType()->isObjCClassType()) |
551 | return None; |
552 | Receiver = CGF.EmitScalarExpr(SubOME->getInstanceReceiver()); |
553 | break; |
554 | |
555 | case ObjCMessageExpr::Class: { |
556 | QualType ReceiverType = SubOME->getClassReceiver(); |
557 | const ObjCObjectType *ObjTy = ReceiverType->castAs<ObjCObjectType>(); |
558 | const ObjCInterfaceDecl *ID = ObjTy->getInterface(); |
559 | assert(ID && "null interface should be impossible here"); |
560 | Receiver = CGF.CGM.getObjCRuntime().GetClass(CGF, ID); |
561 | break; |
562 | } |
563 | case ObjCMessageExpr::SuperInstance: |
564 | case ObjCMessageExpr::SuperClass: |
565 | return None; |
566 | } |
567 | |
568 | return CGF.EmitObjCAllocInit(Receiver, CGF.ConvertType(OME->getType())); |
569 | } |
570 | |
571 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, |
572 | ReturnValueSlot Return) { |
573 | |
574 | |
575 | |
576 | |
577 | bool isDelegateInit = E->isDelegateInitCall(); |
578 | |
579 | const ObjCMethodDecl *method = E->getMethodDecl(); |
580 | |
581 | |
582 | |
583 | if (method && E->getReceiverKind() == ObjCMessageExpr::Instance && |
584 | method->getMethodFamily() == OMF_retain) { |
585 | if (auto lvalueExpr = findWeakLValue(E->getInstanceReceiver())) { |
586 | LValue lvalue = EmitLValue(lvalueExpr); |
587 | llvm::Value *result = EmitARCLoadWeakRetained(lvalue.getAddress(*this)); |
588 | return AdjustObjCObjectType(*this, E->getType(), RValue::get(result)); |
589 | } |
590 | } |
591 | |
592 | if (Optional<llvm::Value *> Val = tryEmitSpecializedAllocInit(*this, E)) |
593 | return AdjustObjCObjectType(*this, E->getType(), RValue::get(*Val)); |
594 | |
595 | |
596 | |
597 | |
598 | |
599 | bool retainSelf = |
600 | (!isDelegateInit && |
601 | CGM.getLangOpts().ObjCAutoRefCount && |
602 | method && |
603 | method->hasAttr<NSConsumesSelfAttr>()); |
604 | |
605 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
606 | bool isSuperMessage = false; |
607 | bool isClassMessage = false; |
608 | ObjCInterfaceDecl *OID = nullptr; |
609 | |
610 | QualType ReceiverType; |
611 | llvm::Value *Receiver = nullptr; |
612 | switch (E->getReceiverKind()) { |
613 | case ObjCMessageExpr::Instance: |
614 | ReceiverType = E->getInstanceReceiver()->getType(); |
615 | isClassMessage = ReceiverType->isObjCClassType(); |
616 | if (retainSelf) { |
617 | TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, |
618 | E->getInstanceReceiver()); |
619 | Receiver = ter.getPointer(); |
620 | if (ter.getInt()) retainSelf = false; |
621 | } else |
622 | Receiver = EmitScalarExpr(E->getInstanceReceiver()); |
623 | break; |
624 | |
625 | case ObjCMessageExpr::Class: { |
626 | ReceiverType = E->getClassReceiver(); |
627 | OID = ReceiverType->castAs<ObjCObjectType>()->getInterface(); |
628 | assert(OID && "Invalid Objective-C class message send"); |
629 | Receiver = Runtime.GetClass(*this, OID); |
630 | isClassMessage = true; |
631 | break; |
632 | } |
633 | |
634 | case ObjCMessageExpr::SuperInstance: |
635 | ReceiverType = E->getSuperType(); |
636 | Receiver = LoadObjCSelf(); |
637 | isSuperMessage = true; |
638 | break; |
639 | |
640 | case ObjCMessageExpr::SuperClass: |
641 | ReceiverType = E->getSuperType(); |
642 | Receiver = LoadObjCSelf(); |
643 | isSuperMessage = true; |
644 | isClassMessage = true; |
645 | break; |
646 | } |
647 | |
648 | if (retainSelf) |
649 | Receiver = EmitARCRetainNonBlock(Receiver); |
650 | |
651 | |
652 | |
653 | |
654 | if (getLangOpts().ObjCAutoRefCount && method && |
655 | method->hasAttr<ObjCReturnsInnerPointerAttr>() && |
656 | shouldExtendReceiverForInnerPointerMessage(E)) |
657 | Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); |
658 | |
659 | QualType ResultType = method ? method->getReturnType() : E->getType(); |
660 | |
661 | CallArgList Args; |
662 | EmitCallArgs(Args, method, E->arguments(), AbstractCallee(method)); |
663 | |
664 | |
665 | |
666 | |
667 | |
668 | |
669 | |
670 | |
671 | if (isDelegateInit) { |
672 | assert(getLangOpts().ObjCAutoRefCount && |
673 | "delegate init calls should only be marked in ARC"); |
674 | |
675 | |
676 | Address selfAddr = |
677 | GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()); |
678 | Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); |
679 | } |
680 | |
681 | RValue result; |
682 | if (isSuperMessage) { |
683 | |
684 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
685 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
686 | result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, |
687 | E->getSelector(), |
688 | OMD->getClassInterface(), |
689 | isCategoryImpl, |
690 | Receiver, |
691 | isClassMessage, |
692 | Args, |
693 | method); |
694 | } else { |
695 | |
696 | result = Runtime.GeneratePossiblySpecializedMessageSend( |
697 | *this, Return, ResultType, E->getSelector(), Receiver, Args, OID, |
698 | method, isClassMessage); |
699 | } |
700 | |
701 | |
702 | |
703 | if (isDelegateInit) { |
704 | Address selfAddr = |
705 | GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()); |
706 | llvm::Value *newSelf = result.getScalarVal(); |
707 | |
708 | |
709 | |
710 | llvm::Type *selfTy = selfAddr.getElementType(); |
711 | newSelf = Builder.CreateBitCast(newSelf, selfTy); |
712 | |
713 | Builder.CreateStore(newSelf, selfAddr); |
714 | } |
715 | |
716 | return AdjustObjCObjectType(*this, E->getType(), result); |
717 | } |
718 | |
719 | namespace { |
720 | struct FinishARCDealloc final : EHScopeStack::Cleanup { |
721 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
722 | const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); |
723 | |
724 | const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); |
725 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
726 | if (!iface->getSuperClass()) return; |
727 | |
728 | bool isCategory = isa<ObjCCategoryImplDecl>(impl); |
729 | |
730 | |
731 | llvm::Value *self = CGF.LoadObjCSelf(); |
732 | |
733 | CallArgList args; |
734 | CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), |
735 | CGF.getContext().VoidTy, |
736 | method->getSelector(), |
737 | iface, |
738 | isCategory, |
739 | self, |
740 | false, |
741 | args, |
742 | method); |
743 | } |
744 | }; |
745 | } |
746 | |
747 | |
748 | |
749 | |
750 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, |
751 | const ObjCContainerDecl *CD) { |
752 | SourceLocation StartLoc = OMD->getBeginLoc(); |
753 | FunctionArgList args; |
754 | |
755 | if (OMD->hasAttr<NoDebugAttr>()) |
756 | DebugInfo = nullptr; |
757 | |
758 | llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); |
759 | |
760 | const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); |
761 | if (OMD->isDirectMethod()) { |
762 | Fn->setVisibility(llvm::Function::HiddenVisibility); |
763 | CGM.SetLLVMFunctionAttributes(OMD, FI, Fn, false); |
764 | CGM.SetLLVMFunctionAttributesForDefinition(OMD, Fn); |
765 | } else { |
766 | CGM.SetInternalFunctionAttributes(OMD, Fn, FI); |
767 | } |
768 | |
769 | args.push_back(OMD->getSelfDecl()); |
770 | args.push_back(OMD->getCmdDecl()); |
771 | |
772 | args.append(OMD->param_begin(), OMD->param_end()); |
773 | |
774 | CurGD = OMD; |
775 | CurEHLocation = OMD->getEndLoc(); |
776 | |
777 | StartFunction(OMD, OMD->getReturnType(), Fn, FI, args, |
778 | OMD->getLocation(), StartLoc); |
779 | |
780 | if (OMD->isDirectMethod()) { |
781 | |
782 | |
783 | |
784 | |
785 | CGM.getObjCRuntime().GenerateDirectMethodPrologue(*this, Fn, OMD, CD); |
786 | } |
787 | |
788 | |
789 | if (CGM.getLangOpts().ObjCAutoRefCount && |
790 | OMD->isInstanceMethod() && |
791 | OMD->getSelector().isUnarySelector()) { |
792 | const IdentifierInfo *ident = |
793 | OMD->getSelector().getIdentifierInfoForSlot(0); |
794 | if (ident->isStr("dealloc")) |
795 | EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); |
796 | } |
797 | } |
798 | |
799 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
800 | LValue lvalue, QualType type); |
801 | |
802 | |
803 | |
804 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
805 | StartObjCMethod(OMD, OMD->getClassInterface()); |
806 | PGO.assignRegionCounters(GlobalDecl(OMD), CurFn); |
807 | assert(isa<CompoundStmt>(OMD->getBody())); |
808 | incrementProfileCounter(OMD->getBody()); |
809 | EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody())); |
810 | FinishFunction(OMD->getBodyRBrace()); |
811 | } |
812 | |
813 | |
814 | |
815 | static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, |
816 | bool isAtomic, bool hasStrong) { |
817 | ASTContext &Context = CGF.getContext(); |
818 | |
819 | Address src = |
820 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) |
821 | .getAddress(CGF); |
822 | |
823 | |
824 | |
825 | CallArgList args; |
826 | |
827 | Address dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); |
828 | args.add(RValue::get(dest.getPointer()), Context.VoidPtrTy); |
829 | |
830 | src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); |
831 | args.add(RValue::get(src.getPointer()), Context.VoidPtrTy); |
832 | |
833 | CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); |
834 | args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); |
835 | args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); |
836 | args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); |
837 | |
838 | llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); |
839 | CGCallee callee = CGCallee::forDirect(fn); |
840 | CGF.EmitCall(CGF.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, args), |
841 | callee, ReturnValueSlot(), args); |
842 | } |
843 | |
844 | |
845 | |
846 | |
847 | static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { |
848 | |
849 | |
850 | return 0; |
851 | } |
852 | |
853 | |
854 | |
855 | static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, |
856 | llvm::Triple::ArchType arch) { |
857 | |
858 | |
859 | |
860 | |
861 | |
862 | return CharUnits::fromQuantity(CGM.PointerSizeInBytes); |
863 | } |
864 | |
865 | namespace { |
866 | class PropertyImplStrategy { |
867 | public: |
868 | enum StrategyKind { |
869 | |
870 | |
871 | Native, |
872 | |
873 | |
874 | GetSetProperty, |
875 | |
876 | |
877 | |
878 | SetPropertyAndExpressionGet, |
879 | |
880 | |
881 | CopyStruct, |
882 | |
883 | |
884 | |
885 | Expression |
886 | }; |
887 | |
888 | StrategyKind getKind() const { return StrategyKind(Kind); } |
889 | |
890 | bool hasStrongMember() const { return HasStrong; } |
891 | bool isAtomic() const { return IsAtomic; } |
892 | bool isCopy() const { return IsCopy; } |
893 | |
894 | CharUnits getIvarSize() const { return IvarSize; } |
895 | CharUnits getIvarAlignment() const { return IvarAlignment; } |
896 | |
897 | PropertyImplStrategy(CodeGenModule &CGM, |
898 | const ObjCPropertyImplDecl *propImpl); |
899 | |
900 | private: |
901 | unsigned Kind : 8; |
902 | unsigned IsAtomic : 1; |
903 | unsigned IsCopy : 1; |
904 | unsigned HasStrong : 1; |
905 | |
906 | CharUnits IvarSize; |
907 | CharUnits IvarAlignment; |
908 | }; |
909 | } |
910 | |
911 | |
912 | PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, |
913 | const ObjCPropertyImplDecl *propImpl) { |
914 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); |
915 | ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); |
916 | |
917 | IsCopy = (setterKind == ObjCPropertyDecl::Copy); |
918 | IsAtomic = prop->isAtomic(); |
919 | HasStrong = false; |
920 | |
921 | |
922 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
923 | QualType ivarType = ivar->getType(); |
924 | auto TInfo = CGM.getContext().getTypeInfoInChars(ivarType); |
925 | IvarSize = TInfo.Width; |
926 | IvarAlignment = TInfo.Align; |
927 | |
928 | |
929 | |
930 | |
931 | if (IsCopy) { |
932 | Kind = IsAtomic ? GetSetProperty : SetPropertyAndExpressionGet; |
933 | return; |
934 | } |
935 | |
936 | |
937 | if (setterKind == ObjCPropertyDecl::Retain) { |
938 | |
939 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
940 | |
941 | |
942 | |
943 | |
944 | |
945 | } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { |
946 | |
947 | |
948 | |
949 | |
950 | |
951 | |
952 | if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) |
953 | Kind = Expression; |
954 | else |
955 | Kind = SetPropertyAndExpressionGet; |
956 | return; |
957 | |
958 | |
959 | |
960 | |
961 | } else if (!IsAtomic) { |
962 | Kind = SetPropertyAndExpressionGet; |
963 | return; |
964 | |
965 | |
966 | } else { |
967 | Kind = GetSetProperty; |
968 | return; |
969 | } |
970 | } |
971 | |
972 | |
973 | if (!IsAtomic) { |
974 | Kind = Expression; |
975 | return; |
976 | } |
977 | |
978 | |
979 | |
980 | if (ivar->isBitField()) { |
981 | Kind = Expression; |
982 | return; |
983 | } |
984 | |
985 | |
986 | |
987 | |
988 | if (ivarType.hasNonTrivialObjCLifetime() || |
989 | (CGM.getLangOpts().getGC() && |
990 | CGM.getContext().getObjCGCAttrKind(ivarType))) { |
991 | Kind = Expression; |
992 | return; |
993 | } |
994 | |
995 | |
996 | if (CGM.getLangOpts().getGC()) |
997 | if (const RecordType *recordType = ivarType->getAs<RecordType>()) |
998 | HasStrong = recordType->getDecl()->hasObjectMember(); |
999 | |
1000 | |
1001 | |
1002 | |
1003 | if (HasStrong) { |
1004 | Kind = CopyStruct; |
1005 | return; |
1006 | } |
1007 | |
1008 | |
1009 | |
1010 | |
1011 | |
1012 | |
1013 | if (!IvarSize.isPowerOfTwo()) { |
1014 | Kind = CopyStruct; |
1015 | return; |
1016 | } |
1017 | |
1018 | llvm::Triple::ArchType arch = |
1019 | CGM.getTarget().getTriple().getArch(); |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { |
1025 | Kind = CopyStruct; |
1026 | return; |
1027 | } |
1028 | |
1029 | |
1030 | |
1031 | if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { |
1032 | Kind = CopyStruct; |
1033 | return; |
1034 | } |
1035 | |
1036 | |
1037 | Kind = Native; |
1038 | } |
1039 | |
1040 | |
1041 | |
1042 | |
1043 | |
1044 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, |
1045 | const ObjCPropertyImplDecl *PID) { |
1046 | llvm::Constant *AtomicHelperFn = |
1047 | CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID); |
1048 | ObjCMethodDecl *OMD = PID->getGetterMethodDecl(); |
1049 | assert(OMD && "Invalid call to generate getter (empty method)"); |
1050 | StartObjCMethod(OMD, IMP->getClassInterface()); |
1051 | |
1052 | generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); |
1053 | |
1054 | FinishFunction(OMD->getEndLoc()); |
1055 | } |
1056 | |
1057 | static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { |
1058 | const Expr *getter = propImpl->getGetterCXXConstructor(); |
1059 | if (!getter) return true; |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | |
1066 | |
1067 | if (getter->isGLValue()) |
1068 | return false; |
1069 | |
1070 | |
1071 | if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) |
1072 | return (construct->getConstructor()->isTrivial()); |
1073 | |
1074 | |
1075 | |
1076 | assert(isa<ExprWithCleanups>(getter)); |
1077 | return false; |
1078 | } |
1079 | |
1080 | |
1081 | |
1082 | static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, |
1083 | llvm::Value *returnAddr, |
1084 | ObjCIvarDecl *ivar, |
1085 | llvm::Constant *AtomicHelperFn) { |
1086 | |
1087 | |
1088 | CallArgList args; |
1089 | |
1090 | |
1091 | args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); |
1092 | |
1093 | |
1094 | llvm::Value *ivarAddr = |
1095 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) |
1096 | .getPointer(CGF); |
1097 | ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); |
1098 | args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); |
1099 | |
1100 | |
1101 | args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); |
1102 | |
1103 | llvm::FunctionCallee copyCppAtomicObjectFn = |
1104 | CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); |
1105 | CGCallee callee = CGCallee::forDirect(copyCppAtomicObjectFn); |
1106 | CGF.EmitCall( |
1107 | CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), |
1108 | callee, ReturnValueSlot(), args); |
1109 | } |
1110 | |
1111 | void |
1112 | CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, |
1113 | const ObjCPropertyImplDecl *propImpl, |
1114 | const ObjCMethodDecl *GetterMethodDecl, |
1115 | llvm::Constant *AtomicHelperFn) { |
1116 | |
1117 | if (!hasTrivialGetExpr(propImpl)) { |
1118 | if (!AtomicHelperFn) { |
1119 | auto *ret = ReturnStmt::Create(getContext(), SourceLocation(), |
1120 | propImpl->getGetterCXXConstructor(), |
1121 | nullptr); |
1122 | EmitReturnStmt(*ret); |
1123 | } |
1124 | else { |
1125 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
1126 | emitCPPObjectAtomicGetterCall(*this, ReturnValue.getPointer(), |
1127 | ivar, AtomicHelperFn); |
1128 | } |
1129 | return; |
1130 | } |
1131 | |
1132 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); |
1133 | QualType propType = prop->getType(); |
1134 | ObjCMethodDecl *getterMethod = propImpl->getGetterMethodDecl(); |
1135 | |
1136 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
1137 | |
1138 | |
1139 | PropertyImplStrategy strategy(CGM, propImpl); |
1140 | switch (strategy.getKind()) { |
1141 | case PropertyImplStrategy::Native: { |
1142 | |
1143 | if (strategy.getIvarSize().isZero()) |
1144 | return; |
1145 | |
1146 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); |
1147 | |
1148 | |
1149 | |
1150 | uint64_t ivarSize = getContext().toBits(strategy.getIvarSize()); |
1151 | llvm::Type *bitcastType = llvm::Type::getIntNTy(getLLVMContext(), ivarSize); |
1152 | bitcastType = bitcastType->getPointerTo(); |
1153 | |
1154 | |
1155 | Address ivarAddr = LV.getAddress(*this); |
1156 | ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); |
1157 | llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); |
1158 | load->setAtomic(llvm::AtomicOrdering::Unordered); |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | llvm::Type *retTy = ConvertType(getterMethod->getReturnType()); |
1164 | uint64_t retTySize = CGM.getDataLayout().getTypeSizeInBits(retTy); |
1165 | llvm::Value *ivarVal = load; |
1166 | if (ivarSize > retTySize) { |
1167 | llvm::Type *newTy = llvm::Type::getIntNTy(getLLVMContext(), retTySize); |
1168 | ivarVal = Builder.CreateTrunc(load, newTy); |
1169 | bitcastType = newTy->getPointerTo(); |
1170 | } |
1171 | Builder.CreateStore(ivarVal, |
1172 | Builder.CreateBitCast(ReturnValue, bitcastType)); |
1173 | |
1174 | |
1175 | AutoreleaseResult = false; |
1176 | return; |
1177 | } |
1178 | |
1179 | case PropertyImplStrategy::GetSetProperty: { |
1180 | llvm::FunctionCallee getPropertyFn = |
1181 | CGM.getObjCRuntime().GetPropertyGetFunction(); |
1182 | if (!getPropertyFn) { |
1183 | CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); |
1184 | return; |
1185 | } |
1186 | CGCallee callee = CGCallee::forDirect(getPropertyFn); |
1187 | |
1188 | |
1189 | |
1190 | |
1191 | llvm::Value *cmd = |
1192 | Builder.CreateLoad(GetAddrOfLocalVar(getterMethod->getCmdDecl()), "cmd"); |
1193 | llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); |
1194 | llvm::Value *ivarOffset = |
1195 | EmitIvarOffset(classImpl->getClassInterface(), ivar); |
1196 | |
1197 | CallArgList args; |
1198 | args.add(RValue::get(self), getContext().getObjCIdType()); |
1199 | args.add(RValue::get(cmd), getContext().getObjCSelType()); |
1200 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); |
1201 | args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), |
1202 | getContext().BoolTy); |
1203 | |
1204 | |
1205 | |
1206 | llvm::CallBase *CallInstruction; |
1207 | RValue RV = EmitCall(getTypes().arrangeBuiltinFunctionCall( |
1208 | getContext().getObjCIdType(), args), |
1209 | callee, ReturnValueSlot(), args, &CallInstruction); |
1210 | if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction)) |
1211 | call->setTailCall(); |
1212 | |
1213 | |
1214 | |
1215 | |
1216 | RV = RValue::get(Builder.CreateBitCast( |
1217 | RV.getScalarVal(), |
1218 | getTypes().ConvertType(getterMethod->getReturnType()))); |
1219 | |
1220 | EmitReturnOfRValue(RV, propType); |
1221 | |
1222 | |
1223 | AutoreleaseResult = false; |
1224 | |
1225 | return; |
1226 | } |
1227 | |
1228 | case PropertyImplStrategy::CopyStruct: |
1229 | emitStructGetterCall(*this, ivar, strategy.isAtomic(), |
1230 | strategy.hasStrongMember()); |
1231 | return; |
1232 | |
1233 | case PropertyImplStrategy::Expression: |
1234 | case PropertyImplStrategy::SetPropertyAndExpressionGet: { |
1235 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); |
1236 | |
1237 | QualType ivarType = ivar->getType(); |
1238 | switch (getEvaluationKind(ivarType)) { |
1239 | case TEK_Complex: { |
1240 | ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation()); |
1241 | EmitStoreOfComplex(pair, MakeAddrLValue(ReturnValue, ivarType), |
1242 | true); |
1243 | return; |
1244 | } |
1245 | case TEK_Aggregate: { |
1246 | |
1247 | |
1248 | |
1249 | EmitAggregateCopy( MakeAddrLValue(ReturnValue, ivarType), |
1250 | LV, ivarType, getOverlapForReturnValue()); |
1251 | return; |
1252 | } |
1253 | case TEK_Scalar: { |
1254 | llvm::Value *value; |
1255 | if (propType->isReferenceType()) { |
1256 | value = LV.getAddress(*this).getPointer(); |
1257 | } else { |
1258 | |
1259 | if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { |
1260 | if (getLangOpts().ObjCAutoRefCount) { |
1261 | value = emitARCRetainLoadOfScalar(*this, LV, ivarType); |
1262 | } else { |
1263 | value = EmitARCLoadWeak(LV.getAddress(*this)); |
1264 | } |
1265 | |
1266 | |
1267 | |
1268 | } else { |
1269 | value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal(); |
1270 | AutoreleaseResult = false; |
1271 | } |
1272 | |
1273 | value = Builder.CreateBitCast( |
1274 | value, ConvertType(GetterMethodDecl->getReturnType())); |
1275 | } |
1276 | |
1277 | EmitReturnOfRValue(RValue::get(value), propType); |
1278 | return; |
1279 | } |
1280 | } |
1281 | llvm_unreachable("bad evaluation kind"); |
1282 | } |
1283 | |
1284 | } |
1285 | llvm_unreachable("bad @property implementation strategy!"); |
1286 | } |
1287 | |
1288 | |
1289 | |
1290 | static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, |
1291 | ObjCIvarDecl *ivar) { |
1292 | |
1293 | |
1294 | CallArgList args; |
1295 | |
1296 | |
1297 | llvm::Value *ivarAddr = |
1298 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) |
1299 | .getPointer(CGF); |
1300 | ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); |
1301 | args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); |
1302 | |
1303 | |
1304 | ParmVarDecl *argVar = *OMD->param_begin(); |
1305 | DeclRefExpr argRef(CGF.getContext(), argVar, false, |
1306 | argVar->getType().getNonReferenceType(), VK_LValue, |
1307 | SourceLocation()); |
1308 | llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF); |
1309 | argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); |
1310 | args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); |
1311 | |
1312 | |
1313 | llvm::Value *size = |
1314 | CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); |
1315 | args.add(RValue::get(size), CGF.getContext().getSizeType()); |
1316 | |
1317 | |
1318 | args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); |
1319 | |
1320 | |
1321 | |
1322 | args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); |
1323 | |
1324 | llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); |
1325 | CGCallee callee = CGCallee::forDirect(fn); |
1326 | CGF.EmitCall( |
1327 | CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), |
1328 | callee, ReturnValueSlot(), args); |
1329 | } |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, |
1335 | ObjCMethodDecl *OMD, |
1336 | ObjCIvarDecl *ivar, |
1337 | llvm::Constant *AtomicHelperFn) { |
1338 | |
1339 | |
1340 | CallArgList args; |
1341 | |
1342 | |
1343 | llvm::Value *ivarAddr = |
1344 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) |
1345 | .getPointer(CGF); |
1346 | ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); |
1347 | args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); |
1348 | |
1349 | |
1350 | ParmVarDecl *argVar = *OMD->param_begin(); |
1351 | DeclRefExpr argRef(CGF.getContext(), argVar, false, |
1352 | argVar->getType().getNonReferenceType(), VK_LValue, |
1353 | SourceLocation()); |
1354 | llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF); |
1355 | argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); |
1356 | args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); |
1357 | |
1358 | |
1359 | args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); |
1360 | |
1361 | llvm::FunctionCallee fn = |
1362 | CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); |
1363 | CGCallee callee = CGCallee::forDirect(fn); |
1364 | CGF.EmitCall( |
1365 | CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), |
1366 | callee, ReturnValueSlot(), args); |
1367 | } |
1368 | |
1369 | |
1370 | static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { |
1371 | Expr *setter = PID->getSetterCXXAssignment(); |
1372 | if (!setter) return true; |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | |
1378 | |
1379 | |
1380 | |
1381 | |
1382 | if (CallExpr *call = dyn_cast<CallExpr>(setter)) { |
1383 | if (const FunctionDecl *callee |
1384 | = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) |
1385 | if (callee->isTrivial()) |
1386 | return true; |
1387 | return false; |
1388 | } |
1389 | |
1390 | assert(isa<ExprWithCleanups>(setter)); |
1391 | return false; |
1392 | } |
1393 | |
1394 | static bool UseOptimizedSetter(CodeGenModule &CGM) { |
1395 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) |
1396 | return false; |
1397 | return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); |
1398 | } |
1399 | |
1400 | void |
1401 | CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, |
1402 | const ObjCPropertyImplDecl *propImpl, |
1403 | llvm::Constant *AtomicHelperFn) { |
1404 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
1405 | ObjCMethodDecl *setterMethod = propImpl->getSetterMethodDecl(); |
1406 | |
1407 | |
1408 | |
1409 | if (!hasTrivialSetExpr(propImpl)) { |
1410 | if (!AtomicHelperFn) |
1411 | |
1412 | EmitStmt(propImpl->getSetterCXXAssignment()); |
1413 | else |
1414 | |
1415 | emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, |
1416 | AtomicHelperFn); |
1417 | return; |
1418 | } |
1419 | |
1420 | PropertyImplStrategy strategy(CGM, propImpl); |
1421 | switch (strategy.getKind()) { |
1422 | case PropertyImplStrategy::Native: { |
1423 | |
1424 | if (strategy.getIvarSize().isZero()) |
1425 | return; |
1426 | |
1427 | Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin()); |
1428 | |
1429 | LValue ivarLValue = |
1430 | EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); |
1431 | Address ivarAddr = ivarLValue.getAddress(*this); |
1432 | |
1433 | |
1434 | |
1435 | llvm::Type *bitcastType = |
1436 | llvm::Type::getIntNTy(getLLVMContext(), |
1437 | getContext().toBits(strategy.getIvarSize())); |
1438 | |
1439 | |
1440 | argAddr = Builder.CreateElementBitCast(argAddr, bitcastType); |
1441 | ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType); |
1442 | |
1443 | |
1444 | llvm::Value *load = Builder.CreateLoad(argAddr); |
1445 | |
1446 | |
1447 | llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); |
1448 | store->setAtomic(llvm::AtomicOrdering::Unordered); |
1449 | return; |
1450 | } |
1451 | |
1452 | case PropertyImplStrategy::GetSetProperty: |
1453 | case PropertyImplStrategy::SetPropertyAndExpressionGet: { |
1454 | |
1455 | llvm::FunctionCallee setOptimizedPropertyFn = nullptr; |
1456 | llvm::FunctionCallee setPropertyFn = nullptr; |
1457 | if (UseOptimizedSetter(CGM)) { |
1458 | |
1459 | setOptimizedPropertyFn = |
1460 | CGM.getObjCRuntime().GetOptimizedPropertySetFunction( |
1461 | strategy.isAtomic(), strategy.isCopy()); |
1462 | if (!setOptimizedPropertyFn) { |
1463 | CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); |
1464 | return; |
1465 | } |
1466 | } |
1467 | else { |
1468 | setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); |
1469 | if (!setPropertyFn) { |
1470 | CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); |
1471 | return; |
1472 | } |
1473 | } |
1474 | |
1475 | |
1476 | |
1477 | llvm::Value *cmd = |
1478 | Builder.CreateLoad(GetAddrOfLocalVar(setterMethod->getCmdDecl())); |
1479 | llvm::Value *self = |
1480 | Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); |
1481 | llvm::Value *ivarOffset = |
1482 | EmitIvarOffset(classImpl->getClassInterface(), ivar); |
1483 | Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin()); |
1484 | llvm::Value *arg = Builder.CreateLoad(argAddr, "arg"); |
1485 | arg = Builder.CreateBitCast(arg, VoidPtrTy); |
1486 | |
1487 | CallArgList args; |
1488 | args.add(RValue::get(self), getContext().getObjCIdType()); |
1489 | args.add(RValue::get(cmd), getContext().getObjCSelType()); |
1490 | if (setOptimizedPropertyFn) { |
1491 | args.add(RValue::get(arg), getContext().getObjCIdType()); |
1492 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); |
1493 | CGCallee callee = CGCallee::forDirect(setOptimizedPropertyFn); |
1494 | EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args), |
1495 | callee, ReturnValueSlot(), args); |
1496 | } else { |
1497 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); |
1498 | args.add(RValue::get(arg), getContext().getObjCIdType()); |
1499 | args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), |
1500 | getContext().BoolTy); |
1501 | args.add(RValue::get(Builder.getInt1(strategy.isCopy())), |
1502 | getContext().BoolTy); |
1503 | |
1504 | |
1505 | CGCallee callee = CGCallee::forDirect(setPropertyFn); |
1506 | EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args), |
1507 | callee, ReturnValueSlot(), args); |
1508 | } |
1509 | |
1510 | return; |
1511 | } |
1512 | |
1513 | case PropertyImplStrategy::CopyStruct: |
1514 | emitStructSetterCall(*this, setterMethod, ivar); |
1515 | return; |
1516 | |
1517 | case PropertyImplStrategy::Expression: |
1518 | break; |
1519 | } |
1520 | |
1521 | |
1522 | ValueDecl *selfDecl = setterMethod->getSelfDecl(); |
1523 | DeclRefExpr self(getContext(), selfDecl, false, selfDecl->getType(), |
1524 | VK_LValue, SourceLocation()); |
1525 | ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, selfDecl->getType(), |
1526 | CK_LValueToRValue, &self, VK_PRValue, |
1527 | FPOptionsOverride()); |
1528 | ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), |
1529 | SourceLocation(), SourceLocation(), |
1530 | &selfLoad, true, true); |
1531 | |
1532 | ParmVarDecl *argDecl = *setterMethod->param_begin(); |
1533 | QualType argType = argDecl->getType().getNonReferenceType(); |
1534 | DeclRefExpr arg(getContext(), argDecl, false, argType, VK_LValue, |
1535 | SourceLocation()); |
1536 | ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, |
1537 | argType.getUnqualifiedType(), CK_LValueToRValue, |
1538 | &arg, VK_PRValue, FPOptionsOverride()); |
1539 | |
1540 | |
1541 | |
1542 | |
1543 | CastKind argCK = CK_NoOp; |
1544 | if (ivarRef.getType()->isObjCObjectPointerType()) { |
1545 | if (argLoad.getType()->isObjCObjectPointerType()) |
1546 | argCK = CK_BitCast; |
1547 | else if (argLoad.getType()->isBlockPointerType()) |
1548 | argCK = CK_BlockPointerToObjCPointerCast; |
1549 | else |
1550 | argCK = CK_CPointerToObjCPointerCast; |
1551 | } else if (ivarRef.getType()->isBlockPointerType()) { |
1552 | if (argLoad.getType()->isBlockPointerType()) |
1553 | argCK = CK_BitCast; |
1554 | else |
1555 | argCK = CK_AnyPointerToBlockPointerCast; |
1556 | } else if (ivarRef.getType()->isPointerType()) { |
1557 | argCK = CK_BitCast; |
1558 | } |
1559 | ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, ivarRef.getType(), argCK, |
1560 | &argLoad, VK_PRValue, FPOptionsOverride()); |
1561 | Expr *finalArg = &argLoad; |
1562 | if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), |
1563 | argLoad.getType())) |
1564 | finalArg = &argCast; |
1565 | |
1566 | BinaryOperator *assign = BinaryOperator::Create( |
1567 | getContext(), &ivarRef, finalArg, BO_Assign, ivarRef.getType(), |
1568 | VK_PRValue, OK_Ordinary, SourceLocation(), FPOptionsOverride()); |
1569 | EmitStmt(assign); |
1570 | } |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | |
1576 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, |
1577 | const ObjCPropertyImplDecl *PID) { |
1578 | llvm::Constant *AtomicHelperFn = |
1579 | CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID); |
1580 | ObjCMethodDecl *OMD = PID->getSetterMethodDecl(); |
1581 | assert(OMD && "Invalid call to generate setter (empty method)"); |
1582 | StartObjCMethod(OMD, IMP->getClassInterface()); |
1583 | |
1584 | generateObjCSetterBody(IMP, PID, AtomicHelperFn); |
1585 | |
1586 | FinishFunction(OMD->getEndLoc()); |
1587 | } |
1588 | |
1589 | namespace { |
1590 | struct DestroyIvar final : EHScopeStack::Cleanup { |
1591 | private: |
1592 | llvm::Value *addr; |
1593 | const ObjCIvarDecl *ivar; |
1594 | CodeGenFunction::Destroyer *destroyer; |
1595 | bool useEHCleanupForArray; |
1596 | public: |
1597 | DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, |
1598 | CodeGenFunction::Destroyer *destroyer, |
1599 | bool useEHCleanupForArray) |
1600 | : addr(addr), ivar(ivar), destroyer(destroyer), |
1601 | useEHCleanupForArray(useEHCleanupForArray) {} |
1602 | |
1603 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1604 | LValue lvalue |
1605 | = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, 0); |
1606 | CGF.emitDestroy(lvalue.getAddress(CGF), ivar->getType(), destroyer, |
1607 | flags.isForNormalCleanup() && useEHCleanupForArray); |
1608 | } |
1609 | }; |
1610 | } |
1611 | |
1612 | |
1613 | static void destroyARCStrongWithStore(CodeGenFunction &CGF, |
1614 | Address addr, |
1615 | QualType type) { |
1616 | llvm::Value *null = getNullForVariable(addr); |
1617 | CGF.EmitARCStoreStrongCall(addr, null, true); |
1618 | } |
1619 | |
1620 | static void emitCXXDestructMethod(CodeGenFunction &CGF, |
1621 | ObjCImplementationDecl *impl) { |
1622 | CodeGenFunction::RunCleanupsScope scope(CGF); |
1623 | |
1624 | llvm::Value *self = CGF.LoadObjCSelf(); |
1625 | |
1626 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
1627 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
1628 | ivar; ivar = ivar->getNextIvar()) { |
1629 | QualType type = ivar->getType(); |
1630 | |
1631 | |
1632 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
1633 | if (!dtorKind) continue; |
1634 | |
1635 | CodeGenFunction::Destroyer *destroyer = nullptr; |
1636 | |
1637 | |
1638 | |
1639 | if (dtorKind == QualType::DK_objc_strong_lifetime) { |
1640 | destroyer = destroyARCStrongWithStore; |
1641 | |
1642 | |
1643 | } else { |
1644 | destroyer = CGF.getDestroyer(dtorKind); |
1645 | } |
1646 | |
1647 | CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); |
1648 | |
1649 | CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, |
1650 | cleanupKind & EHCleanup); |
1651 | } |
1652 | |
1653 | assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); |
1654 | } |
1655 | |
1656 | void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
1657 | ObjCMethodDecl *MD, |
1658 | bool ctor) { |
1659 | MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); |
1660 | StartObjCMethod(MD, IMP->getClassInterface()); |
1661 | |
1662 | |
1663 | if (ctor) { |
1664 | |
1665 | AutoreleaseResult = false; |
1666 | |
1667 | for (const auto *IvarInit : IMP->inits()) { |
1668 | FieldDecl *Field = IvarInit->getAnyMember(); |
1669 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
1670 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
1671 | LoadObjCSelf(), Ivar, 0); |
1672 | EmitAggExpr(IvarInit->getInit(), |
1673 | AggValueSlot::forLValue(LV, *this, AggValueSlot::IsDestructed, |
1674 | AggValueSlot::DoesNotNeedGCBarriers, |
1675 | AggValueSlot::IsNotAliased, |
1676 | AggValueSlot::DoesNotOverlap)); |
1677 | } |
1678 | |
1679 | CodeGenTypes &Types = CGM.getTypes(); |
1680 | QualType IdTy(CGM.getContext().getObjCIdType()); |
1681 | llvm::Value *SelfAsId = |
1682 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
1683 | EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); |
1684 | |
1685 | |
1686 | } else { |
1687 | emitCXXDestructMethod(*this, IMP); |
1688 | } |
1689 | FinishFunction(); |
1690 | } |
1691 | |
1692 | llvm::Value *CodeGenFunction::LoadObjCSelf() { |
1693 | VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); |
1694 | DeclRefExpr DRE(getContext(), Self, |
1695 | (CurFuncDecl != CurCodeDecl), |
1696 | Self->getType(), VK_LValue, SourceLocation()); |
1697 | return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation()); |
1698 | } |
1699 | |
1700 | QualType CodeGenFunction::TypeOfSelfObject() { |
1701 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
1702 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); |
1703 | const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( |
1704 | getContext().getCanonicalType(selfDecl->getType())); |
1705 | return PTy->getPointeeType(); |
1706 | } |
1707 | |
1708 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ |
1709 | llvm::FunctionCallee EnumerationMutationFnPtr = |
1710 | CGM.getObjCRuntime().EnumerationMutationFunction(); |
1711 | if (!EnumerationMutationFnPtr) { |
1712 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); |
1713 | return; |
1714 | } |
1715 | CGCallee EnumerationMutationFn = |
1716 | CGCallee::forDirect(EnumerationMutationFnPtr); |
1717 | |
1718 | CGDebugInfo *DI = getDebugInfo(); |
1719 | if (DI) |
1720 | DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); |
1721 | |
1722 | RunCleanupsScope ForScope(*this); |
1723 | |
1724 | |
1725 | AutoVarEmission variable = AutoVarEmission::invalid(); |
1726 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) |
1727 | variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); |
1728 | |
1729 | JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); |
1730 | |
1731 | |
1732 | QualType StateTy = CGM.getObjCFastEnumerationStateType(); |
1733 | Address StatePtr = CreateMemTemp(StateTy, "state.ptr"); |
1734 | EmitNullInitialization(StatePtr, StateTy); |
1735 | |
1736 | |
1737 | static const unsigned NumItems = 16; |
1738 | |
1739 | |
1740 | IdentifierInfo *II[] = { |
1741 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
1742 | &CGM.getContext().Idents.get("objects"), |
1743 | &CGM.getContext().Idents.get("count") |
1744 | }; |
1745 | Selector FastEnumSel = |
1746 | CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); |
1747 | |
1748 | QualType ItemsTy = |
1749 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
1750 | llvm::APInt(32, NumItems), nullptr, |
1751 | ArrayType::Normal, 0); |
1752 | Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); |
1753 | |
1754 | |
1755 | llvm::Value *Collection; |
1756 | if (getLangOpts().ObjCAutoRefCount) { |
1757 | Collection = EmitARCRetainScalarExpr(S.getCollection()); |
1758 | |
1759 | |
1760 | EmitObjCConsumeObject(S.getCollection()->getType(), Collection); |
1761 | } else { |
1762 | Collection = EmitScalarExpr(S.getCollection()); |
1763 | } |
1764 | |
1765 | |
1766 | |
1767 | JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); |
1768 | |
1769 | |
1770 | CallArgList Args; |
1771 | |
1772 | |
1773 | Args.add(RValue::get(StatePtr.getPointer()), |
1774 | getContext().getPointerType(StateTy)); |
1775 | |
1776 | |
1777 | |
1778 | |
1779 | |
1780 | |
1781 | Args.add(RValue::get(ItemsPtr.getPointer()), |
1782 | getContext().getPointerType(ItemsTy)); |
1783 | |
1784 | |
1785 | llvm::Type *NSUIntegerTy = ConvertType(getContext().getNSUIntegerType()); |
1786 | llvm::Constant *Count = llvm::ConstantInt::get(NSUIntegerTy, NumItems); |
1787 | Args.add(RValue::get(Count), getContext().getNSUIntegerType()); |
1788 | |
1789 | |
1790 | RValue CountRV = |
1791 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
1792 | getContext().getNSUIntegerType(), |
1793 | FastEnumSel, Collection, Args); |
1794 | |
1795 | |
1796 | llvm::Value *initialBufferLimit = CountRV.getScalarVal(); |
1797 | |
1798 | llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); |
1799 | llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); |
1800 | |
1801 | llvm::Value *zero = llvm::Constant::getNullValue(NSUIntegerTy); |
1802 | |
1803 | |
1804 | |
1805 | |
1806 | uint64_t EntryCount = getCurrentProfileCount(); |
1807 | Builder.CreateCondBr( |
1808 | Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB, |
1809 | LoopInitBB, |
1810 | createProfileWeights(EntryCount, getProfileCount(S.getBody()))); |
1811 | |
1812 | |
1813 | EmitBlock(LoopInitBB); |
1814 | |
1815 | |
1816 | |
1817 | |
1818 | Address StateMutationsPtrPtr = |
1819 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
1820 | llvm::Value *StateMutationsPtr |
1821 | = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
1822 | |
1823 | llvm::Type *UnsignedLongTy = ConvertType(getContext().UnsignedLongTy); |
1824 | llvm::Value *initialMutations = |
1825 | Builder.CreateAlignedLoad(UnsignedLongTy, StateMutationsPtr, |
1826 | getPointerAlign(), "forcoll.initial-mutations"); |
1827 | |
1828 | |
1829 | |
1830 | llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); |
1831 | EmitBlock(LoopBodyBB); |
1832 | |
1833 | |
1834 | llvm::PHINode *index = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.index"); |
1835 | index->addIncoming(zero, LoopInitBB); |
1836 | |
1837 | |
1838 | llvm::PHINode *count = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.count"); |
1839 | count->addIncoming(initialBufferLimit, LoopInitBB); |
1840 | |
1841 | incrementProfileCounter(&S); |
1842 | |
1843 | |
1844 | |
1845 | |
1846 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
1847 | llvm::Value *currentMutations |
1848 | = Builder.CreateAlignedLoad(UnsignedLongTy, StateMutationsPtr, |
1849 | getPointerAlign(), "statemutations"); |
1850 | |
1851 | llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); |
1852 | llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); |
1853 | |
1854 | Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), |
1855 | WasNotMutatedBB, WasMutatedBB); |
1856 | |
1857 | |
1858 | EmitBlock(WasMutatedBB); |
1859 | llvm::Type *ObjCIdType = ConvertType(getContext().getObjCIdType()); |
1860 | llvm::Value *V = |
1861 | Builder.CreateBitCast(Collection, ObjCIdType); |
1862 | CallArgList Args2; |
1863 | Args2.add(RValue::get(V), getContext().getObjCIdType()); |
1864 | |
1865 | |
1866 | EmitCall( |
1867 | CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2), |
1868 | EnumerationMutationFn, ReturnValueSlot(), Args2); |
1869 | |
1870 | |
1871 | EmitBlock(WasNotMutatedBB); |
1872 | |
1873 | |
1874 | RunCleanupsScope elementVariableScope(*this); |
1875 | bool elementIsVariable; |
1876 | LValue elementLValue; |
1877 | QualType elementType; |
1878 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
1879 | |
1880 | EmitAutoVarInit(variable); |
1881 | |
1882 | const VarDecl *D = cast<VarDecl>(SD->getSingleDecl()); |
1883 | DeclRefExpr tempDRE(getContext(), const_cast<VarDecl *>(D), false, |
1884 | D->getType(), VK_LValue, SourceLocation()); |
1885 | elementLValue = EmitLValue(&tempDRE); |
1886 | elementType = D->getType(); |
1887 | elementIsVariable = true; |
1888 | |
1889 | if (D->isARCPseudoStrong()) |
1890 | elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); |
1891 | } else { |
1892 | elementLValue = LValue(); |
1893 | elementType = cast<Expr>(S.getElement())->getType(); |
1894 | elementIsVariable = false; |
1895 | } |
1896 | llvm::Type *convertedElementType = ConvertType(elementType); |
1897 | |
1898 | |
1899 | |
1900 | |
1901 | Address StateItemsPtr = |
1902 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
1903 | llvm::Value *EnumStateItems = |
1904 | Builder.CreateLoad(StateItemsPtr, "stateitems"); |
1905 | |
1906 | |
1907 | llvm::Value *CurrentItemPtr = Builder.CreateGEP( |
1908 | EnumStateItems->getType()->getPointerElementType(), EnumStateItems, index, |
1909 | "currentitem.ptr"); |
1910 | llvm::Value *CurrentItem = |
1911 | Builder.CreateAlignedLoad(ObjCIdType, CurrentItemPtr, getPointerAlign()); |
1912 | |
1913 | if (SanOpts.has(SanitizerKind::ObjCCast)) { |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | |
1919 | const ObjCObjectPointerType *ObjPtrTy = |
1920 | elementType->getAsObjCInterfacePointerType(); |
1921 | const ObjCInterfaceType *InterfaceTy = |
1922 | ObjPtrTy ? ObjPtrTy->getInterfaceType() : nullptr; |
1923 | if (InterfaceTy) { |
1924 | SanitizerScope SanScope(this); |
1925 | auto &C = CGM.getContext(); |
1926 | assert(InterfaceTy->getDecl() && "No decl for ObjC interface type"); |
1927 | Selector IsKindOfClassSel = GetUnarySelector("isKindOfClass", C); |
1928 | CallArgList IsKindOfClassArgs; |
1929 | llvm::Value *Cls = |
1930 | CGM.getObjCRuntime().GetClass(*this, InterfaceTy->getDecl()); |
1931 | IsKindOfClassArgs.add(RValue::get(Cls), C.getObjCClassType()); |
1932 | llvm::Value *IsClass = |
1933 | CGM.getObjCRuntime() |
1934 | .GenerateMessageSend(*this, ReturnValueSlot(), C.BoolTy, |
1935 | IsKindOfClassSel, CurrentItem, |
1936 | IsKindOfClassArgs) |
1937 | .getScalarVal(); |
1938 | llvm::Constant *StaticData[] = { |
1939 | EmitCheckSourceLocation(S.getBeginLoc()), |
1940 | EmitCheckTypeDescriptor(QualType(InterfaceTy, 0))}; |
1941 | EmitCheck({{IsClass, SanitizerKind::ObjCCast}}, |
1942 | SanitizerHandler::InvalidObjCCast, |
1943 | ArrayRef<llvm::Constant *>(StaticData), CurrentItem); |
1944 | } |
1945 | } |
1946 | |
1947 | |
1948 | CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, |
1949 | "currentitem"); |
1950 | |
1951 | |
1952 | |
1953 | if (!elementIsVariable) { |
1954 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
1955 | EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); |
1956 | } else { |
1957 | EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, |
1958 | true); |
1959 | } |
1960 | |
1961 | |
1962 | |
1963 | if (elementIsVariable) |
1964 | EmitAutoVarCleanups(variable); |
1965 | |
1966 | |
1967 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
1968 | { |
1969 | RunCleanupsScope Scope(*this); |
1970 | EmitStmt(S.getBody()); |
1971 | } |
1972 | BreakContinueStack.pop_back(); |
1973 | |
1974 | |
1975 | elementVariableScope.ForceCleanup(); |
1976 | |
1977 | |
1978 | EmitBlock(AfterBody.getBlock()); |
1979 | |
1980 | llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); |
1981 | |
1982 | |
1983 | llvm::Value *indexPlusOne = |
1984 | Builder.CreateAdd(index, llvm::ConstantInt::get(NSUIntegerTy, 1)); |
1985 | |
1986 | |
1987 | |
1988 | |
1989 | |
1990 | Builder.CreateCondBr( |
1991 | Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB, |
1992 | createProfileWeights(getProfileCount(S.getBody()), EntryCount)); |
1993 | |
1994 | index->addIncoming(indexPlusOne, AfterBody.getBlock()); |
1995 | count->addIncoming(count, AfterBody.getBlock()); |
1996 | |
1997 | |
1998 | EmitBlock(FetchMoreBB); |
1999 | |
2000 | CountRV = |
2001 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
2002 | getContext().getNSUIntegerType(), |
2003 | FastEnumSel, Collection, Args); |
2004 | |
2005 | |
2006 | llvm::Value *refetchCount = CountRV.getScalarVal(); |
2007 | |
2008 | |
2009 | index->addIncoming(zero, Builder.GetInsertBlock()); |
2010 | count->addIncoming(refetchCount, Builder.GetInsertBlock()); |
2011 | |
2012 | Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), |
2013 | EmptyBB, LoopBodyBB); |
2014 | |
2015 | |
2016 | EmitBlock(EmptyBB); |
2017 | |
2018 | if (!elementIsVariable) { |
2019 | |
2020 | |
2021 | llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); |
2022 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
2023 | EmitStoreThroughLValue(RValue::get(null), elementLValue); |
2024 | } |
2025 | |
2026 | if (DI) |
2027 | DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); |
2028 | |
2029 | ForScope.ForceCleanup(); |
2030 | EmitBlock(LoopEnd.getBlock()); |
2031 | } |
2032 | |
2033 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { |
2034 | CGM.getObjCRuntime().EmitTryStmt(*this, S); |
2035 | } |
2036 | |
2037 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { |
2038 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
2039 | } |
2040 | |
2041 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( |
2042 | const ObjCAtSynchronizedStmt &S) { |
2043 | CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); |
2044 | } |
2045 | |
2046 | namespace { |
2047 | struct CallObjCRelease final : EHScopeStack::Cleanup { |
2048 | CallObjCRelease(llvm::Value *object) : object(object) {} |
2049 | llvm::Value *object; |
2050 | |
2051 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
2052 | |
2053 | CGF.EmitARCRelease(object, ARCImpreciseLifetime); |
2054 | } |
2055 | }; |
2056 | } |
2057 | |
2058 | |
2059 | |
2060 | llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, |
2061 | llvm::Value *object) { |
2062 | |
2063 | |
2064 | pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); |
2065 | return object; |
2066 | } |
2067 | |
2068 | llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, |
2069 | llvm::Value *value) { |
2070 | return EmitARCRetainAutorelease(type, value); |
2071 | } |
2072 | |
2073 | |
2074 | |
2075 | void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { |
2076 | llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_use; |
2077 | if (!fn) |
2078 | fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_use); |
2079 | |
2080 | |
2081 | |
2082 | EmitNounwindRuntimeCall(fn, values); |
2083 | } |
2084 | |
2085 | |
2086 | |
2087 | void CodeGenFunction::EmitARCNoopIntrinsicUse(ArrayRef<llvm::Value *> values) { |
2088 | llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_noop_use; |
2089 | if (!fn) |
2090 | fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_noop_use); |
2091 | EmitNounwindRuntimeCall(fn, values); |
2092 | } |
2093 | |
2094 | static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, llvm::Value *RTF) { |
2095 | if (auto *F = dyn_cast<llvm::Function>(RTF)) { |
2096 | |
2097 | |
2098 | |
2099 | if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC() && |
2100 | !CGM.getTriple().isOSBinFormatCOFF()) { |
2101 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
2102 | } |
2103 | } |
2104 | } |
2105 | |
2106 | static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, |
2107 | llvm::FunctionCallee RTF) { |
2108 | setARCRuntimeFunctionLinkage(CGM, RTF.getCallee()); |
2109 | } |
2110 | |
2111 | |
2112 | |
2113 | |
2114 | static llvm::Value *emitARCValueOperation( |
2115 | CodeGenFunction &CGF, llvm::Value *value, llvm::Type *returnType, |
2116 | llvm::Function *&fn, llvm::Intrinsic::ID IntID, |
2117 | llvm::CallInst::TailCallKind tailKind = llvm::CallInst::TCK_None) { |
2118 | if (isa<llvm::ConstantPointerNull>(value)) |
2119 | return value; |
2120 | |
2121 | if (!fn) { |
2122 | fn = CGF.CGM.getIntrinsic(IntID); |
2123 | setARCRuntimeFunctionLinkage(CGF.CGM, fn); |
2124 | } |
2125 | |
2126 | |
2127 | llvm::Type *origType = returnType ? returnType : value->getType(); |
2128 | value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); |
2129 | |
2130 | |
2131 | llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); |
2132 | call->setTailCallKind(tailKind); |
2133 | |
2134 | |
2135 | return CGF.Builder.CreateBitCast(call, origType); |
2136 | } |
2137 | |
2138 | |
2139 | |
2140 | static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, Address addr, |
2141 | llvm::Function *&fn, |
2142 | llvm::Intrinsic::ID IntID) { |
2143 | if (!fn) { |
2144 | fn = CGF.CGM.getIntrinsic(IntID); |
2145 | setARCRuntimeFunctionLinkage(CGF.CGM, fn); |
2146 | } |
2147 | |
2148 | |
2149 | llvm::Type *origType = addr.getElementType(); |
2150 | addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); |
2151 | |
2152 | |
2153 | llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer()); |
2154 | |
2155 | |
2156 | if (origType != CGF.Int8PtrTy) |
2157 | result = CGF.Builder.CreateBitCast(result, origType); |
2158 | |
2159 | return result; |
2160 | } |
2161 | |
2162 | |
2163 | |
2164 | static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, Address addr, |
2165 | llvm::Value *value, |
2166 | llvm::Function *&fn, |
2167 | llvm::Intrinsic::ID IntID, |
2168 | bool ignored) { |
2169 | assert(addr.getElementType() == value->getType()); |
2170 | |
2171 | if (!fn) { |
2172 | fn = CGF.CGM.getIntrinsic(IntID); |
2173 | setARCRuntimeFunctionLinkage(CGF.CGM, fn); |
2174 | } |
2175 | |
2176 | llvm::Type *origType = value->getType(); |
2177 | |
2178 | llvm::Value *args[] = { |
2179 | CGF.Builder.CreateBitCast(addr.getPointer(), CGF.Int8PtrPtrTy), |
2180 | CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) |
2181 | }; |
2182 | llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); |
2183 | |
2184 | if (ignored) return nullptr; |
2185 | |
2186 | return CGF.Builder.CreateBitCast(result, origType); |
2187 | } |
2188 | |
2189 | |
2190 | |
2191 | static void emitARCCopyOperation(CodeGenFunction &CGF, Address dst, Address src, |
2192 | llvm::Function *&fn, |
2193 | llvm::Intrinsic::ID IntID) { |
2194 | assert(dst.getType() == src.getType()); |
2195 | |
2196 | if (!fn) { |
2197 | fn = CGF.CGM.getIntrinsic(IntID); |
2198 | setARCRuntimeFunctionLinkage(CGF.CGM, fn); |
2199 | } |
2200 | |
2201 | llvm::Value *args[] = { |
2202 | CGF.Builder.CreateBitCast(dst.getPointer(), CGF.Int8PtrPtrTy), |
2203 | CGF.Builder.CreateBitCast(src.getPointer(), CGF.Int8PtrPtrTy) |
2204 | }; |
2205 | CGF.EmitNounwindRuntimeCall(fn, args); |
2206 | } |
2207 | |
2208 | |
2209 | |
2210 | |
2211 | static llvm::Value *emitObjCValueOperation(CodeGenFunction &CGF, |
2212 | llvm::Value *value, |
2213 | llvm::Type *returnType, |
2214 | llvm::FunctionCallee &fn, |
2215 | StringRef fnName) { |
2216 | if (isa<llvm::ConstantPointerNull>(value)) |
2217 | return value; |
2218 | |
2219 | if (!fn) { |
2220 | llvm::FunctionType *fnType = |
2221 | llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); |
2222 | fn = CGF.CGM.CreateRuntimeFunction(fnType, fnName); |
2223 | |
2224 | |
2225 | if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee())) |
2226 | if (fnName == "objc_retain") |
2227 | f->addFnAttr(llvm::Attribute::NonLazyBind); |
2228 | } |
2229 | |
2230 | |
2231 | llvm::Type *origType = returnType ? returnType : value->getType(); |
2232 | value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); |
2233 | |
2234 | |
2235 | llvm::CallBase *Inst = CGF.EmitCallOrInvoke(fn, value); |
2236 | |
2237 | |
2238 | |
2239 | if (fnName == "objc_autorelease") |
2240 | if (auto *Call = dyn_cast<llvm::CallInst>(Inst)) |
2241 | Call->setTailCall(); |
2242 | |
2243 | |
2244 | return CGF.Builder.CreateBitCast(Inst, origType); |
2245 | } |
2246 | |
2247 | |
2248 | |
2249 | |
2250 | llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { |
2251 | if (type->isBlockPointerType()) |
2252 | return EmitARCRetainBlock(value, false); |
2253 | else |
2254 | return EmitARCRetainNonBlock(value); |
2255 | } |
2256 | |
2257 | |
2258 | |
2259 | llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { |
2260 | return emitARCValueOperation(*this, value, nullptr, |
2261 | CGM.getObjCEntrypoints().objc_retain, |
2262 | llvm::Intrinsic::objc_retain); |
2263 | } |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | |
2269 | |
2270 | |
2271 | llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, |
2272 | bool mandatory) { |
2273 | llvm::Value *result |
2274 | = emitARCValueOperation(*this, value, nullptr, |
2275 | CGM.getObjCEntrypoints().objc_retainBlock, |
2276 | llvm::Intrinsic::objc_retainBlock); |
2277 | |
2278 | |
2279 | |
2280 | |
2281 | |
2282 | if (!mandatory && isa<llvm::Instruction>(result)) { |
2283 | llvm::CallInst *call |
2284 | = cast<llvm::CallInst>(result->stripPointerCasts()); |
2285 | assert(call->getCalledOperand() == |
2286 | CGM.getObjCEntrypoints().objc_retainBlock); |
2287 | |
2288 | call->setMetadata("clang.arc.copy_on_escape", |
2289 | llvm::MDNode::get(Builder.getContext(), None)); |
2290 | } |
2291 | |
2292 | return result; |
2293 | } |
2294 | |
2295 | static void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF) { |
2296 | |
2297 | |
2298 | llvm::InlineAsm *&marker |
2299 | = CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker; |
2300 | if (!marker) { |
2301 | StringRef assembly |
2302 | = CGF.CGM.getTargetCodeGenInfo() |
2303 | .getARCRetainAutoreleasedReturnValueMarker(); |
2304 | |
2305 | |
2306 | if (assembly.empty()) { |
2307 | |
2308 | |
2309 | |
2310 | } else if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) { |
2311 | llvm::FunctionType *type = |
2312 | llvm::FunctionType::get(CGF.VoidTy, false); |
2313 | |
2314 | marker = llvm::InlineAsm::get(type, assembly, "", true); |
2315 | |
2316 | |
2317 | |
2318 | |
2319 | } else { |
2320 | const char *retainRVMarkerKey = llvm::objcarc::getRVMarkerModuleFlagStr(); |
2321 | if (!CGF.CGM.getModule().getModuleFlag(retainRVMarkerKey)) { |
2322 | auto *str = llvm::MDString::get(CGF.getLLVMContext(), assembly); |
2323 | CGF.CGM.getModule().addModuleFlag(llvm::Module::Error, |
2324 | retainRVMarkerKey, str); |
2325 | } |
2326 | } |
2327 | } |
2328 | |
2329 | |
2330 | if (marker) |
2331 | CGF.Builder.CreateCall(marker, None, CGF.getBundlesForFunclet(marker)); |
2332 | } |
2333 | |
2334 | static llvm::Value *emitOptimizedARCReturnCall(llvm::Value *value, |
2335 | bool IsRetainRV, |
2336 | CodeGenFunction &CGF) { |
2337 | emitAutoreleasedReturnValueMarker(CGF); |
2338 | |
2339 | |
2340 | |
2341 | |
2342 | |
2343 | |
2344 | |
2345 | if (CGF.CGM.getCodeGenOpts().OptimizationLevel > 0 && |
2346 | CGF.CGM.getTarget().getTriple().isAArch64()) { |
2347 | llvm::Value *bundleArgs[] = {llvm::ConstantInt::get( |
2348 | CGF.Int64Ty, |
2349 | llvm::objcarc::getAttachedCallOperandBundleEnum(IsRetainRV))}; |
2350 | llvm::OperandBundleDef OB("clang.arc.attachedcall", bundleArgs); |
2351 | auto *oldCall = cast<llvm::CallBase>(value); |
2352 | llvm::CallBase *newCall = llvm::CallBase::addOperandBundle( |
2353 | oldCall, llvm::LLVMContext::OB_clang_arc_attachedcall, OB, oldCall); |
2354 | newCall->copyMetadata(*oldCall); |
2355 | oldCall->replaceAllUsesWith(newCall); |
2356 | oldCall->eraseFromParent(); |
2357 | CGF.EmitARCNoopIntrinsicUse(newCall); |
2358 | return newCall; |
2359 | } |
2360 | |
2361 | bool isNoTail = |
2362 | CGF.CGM.getTargetCodeGenInfo().markARCOptimizedReturnCallsAsNoTail(); |
2363 | llvm::CallInst::TailCallKind tailKind = |
2364 | isNoTail ? llvm::CallInst::TCK_NoTail : llvm::CallInst::TCK_None; |
2365 | ObjCEntrypoints &EPs = CGF.CGM.getObjCEntrypoints(); |
2366 | llvm::Function *&EP = IsRetainRV |
2367 | ? EPs.objc_retainAutoreleasedReturnValue |
2368 | : EPs.objc_unsafeClaimAutoreleasedReturnValue; |
2369 | llvm::Intrinsic::ID IID = |
2370 | IsRetainRV ? llvm::Intrinsic::objc_retainAutoreleasedReturnValue |
2371 | : llvm::Intrinsic::objc_unsafeClaimAutoreleasedReturnValue; |
2372 | return emitARCValueOperation(CGF, value, nullptr, EP, IID, tailKind); |
2373 | } |
2374 | |
2375 | |
2376 | |
2377 | |
2378 | |
2379 | |
2380 | llvm::Value * |
2381 | CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { |
2382 | return emitOptimizedARCReturnCall(value, true, *this); |
2383 | } |
2384 | |
2385 | |
2386 | |
2387 | |
2388 | |
2389 | |
2390 | |
2391 | |
2392 | llvm::Value * |
2393 | CodeGenFunction::EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value) { |
2394 | return emitOptimizedARCReturnCall(value, false, *this); |
2395 | } |
2396 | |
2397 | |
2398 | |
2399 | void CodeGenFunction::EmitARCRelease(llvm::Value *value, |
2400 | ARCPreciseLifetime_t precise) { |
2401 | if (isa<llvm::ConstantPointerNull>(value)) return; |
2402 | |
2403 | llvm::Function *&fn = CGM.getObjCEntrypoints().objc_release; |
2404 | if (!fn) { |
2405 | fn = CGM.getIntrinsic(llvm::Intrinsic::objc_release); |
2406 | setARCRuntimeFunctionLinkage(CGM, fn); |
2407 | } |
2408 | |
2409 | |
2410 | value = Builder.CreateBitCast(value, Int8PtrTy); |
2411 | |
2412 | |
2413 | llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); |
2414 | |
2415 | if (precise == ARCImpreciseLifetime) { |
2416 | call->setMetadata("clang.imprecise_release", |
2417 | llvm::MDNode::get(Builder.getContext(), None)); |
2418 | } |
2419 | } |
2420 | |
2421 | |
2422 | |
2423 | |
2424 | |
2425 | |
2426 | |
2427 | |
2428 | |
2429 | |
2430 | void CodeGenFunction::EmitARCDestroyStrong(Address addr, |
2431 | ARCPreciseLifetime_t precise) { |
2432 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
2433 | llvm::Value *null = getNullForVariable(addr); |
2434 | EmitARCStoreStrongCall(addr, null, true); |
2435 | return; |
2436 | } |
2437 | |
2438 | llvm::Value *value = Builder.CreateLoad(addr); |
2439 | EmitARCRelease(value, precise); |
2440 | } |
2441 | |
2442 | |
2443 | |
2444 | llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(Address addr, |
2445 | llvm::Value *value, |
2446 | bool ignored) { |
2447 | assert(addr.getElementType() == value->getType()); |
2448 | |
2449 | llvm::Function *&fn = CGM.getObjCEntrypoints().objc_storeStrong; |
2450 | if (!fn) { |
2451 | fn = CGM.getIntrinsic(llvm::Intrinsic::objc_storeStrong); |
2452 | setARCRuntimeFunctionLinkage(CGM, fn); |
2453 | } |
2454 | |
2455 | llvm::Value *args[] = { |
2456 | Builder.CreateBitCast(addr.getPointer(), Int8PtrPtrTy), |
2457 | Builder.CreateBitCast(value, Int8PtrTy) |
2458 | }; |
2459 | EmitNounwindRuntimeCall(fn, args); |
2460 | |
2461 | if (ignored) return nullptr; |
2462 | return value; |
2463 | } |
2464 | |
2465 | |
2466 | |
2467 | |
2468 | llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, |
2469 | llvm::Value *newValue, |
2470 | bool ignored) { |
2471 | QualType type = dst.getType(); |
2472 | bool isBlock = type->isBlockPointerType(); |
2473 | |
2474 | |
2475 | |
2476 | if (shouldUseFusedARCCalls() && |
2477 | !isBlock && |
2478 | (dst.getAlignment().isZero() || |
2479 | dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { |
2480 | return EmitARCStoreStrongCall(dst.getAddress(*this), newValue, ignored); |
2481 | } |
2482 | |
2483 | |
2484 | |
2485 | |
2486 | newValue = EmitARCRetain(type, newValue); |
2487 | |
2488 | |
2489 | llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation()); |
2490 | |
2491 | |
2492 | |
2493 | EmitStoreOfScalar(newValue, dst); |
2494 | |
2495 | |
2496 | EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); |
2497 | |
2498 | return newValue; |
2499 | } |
2500 | |
2501 | |
2502 | |
2503 | llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { |
2504 | return emitARCValueOperation(*this, value, nullptr, |
2505 | CGM.getObjCEntrypoints().objc_autorelease, |
2506 | llvm::Intrinsic::objc_autorelease); |
2507 | } |
2508 | |
2509 | |
2510 | |
2511 | llvm::Value * |
2512 | CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { |
2513 | return emitARCValueOperation(*this, value, nullptr, |
2514 | CGM.getObjCEntrypoints().objc_autoreleaseReturnValue, |
2515 | llvm::Intrinsic::objc_autoreleaseReturnValue, |
2516 | llvm::CallInst::TCK_Tail); |
2517 | } |
2518 | |
2519 | |
2520 | |
2521 | llvm::Value * |
2522 | CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { |
2523 | return emitARCValueOperation(*this, value, nullptr, |
2524 | CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue, |
2525 | llvm::Intrinsic::objc_retainAutoreleaseReturnValue, |
2526 | llvm::CallInst::TCK_Tail); |
2527 | } |
2528 | |
2529 | |
2530 | |
2531 | |
2532 | |
2533 | |
2534 | llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, |
2535 | llvm::Value *value) { |
2536 | if (!type->isBlockPointerType()) |
2537 | return EmitARCRetainAutoreleaseNonBlock(value); |
2538 | |
2539 | if (isa<llvm::ConstantPointerNull>(value)) return value; |
2540 | |
2541 | llvm::Type *origType = value->getType(); |
2542 | value = Builder.CreateBitCast(value, Int8PtrTy); |
2543 | value = EmitARCRetainBlock(value, true); |
2544 | value = EmitARCAutorelease(value); |
2545 | return Builder.CreateBitCast(value, origType); |
2546 | } |
2547 | |
2548 | |
2549 | |
2550 | llvm::Value * |
2551 | CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { |
2552 | return emitARCValueOperation(*this, value, nullptr, |
2553 | CGM.getObjCEntrypoints().objc_retainAutorelease, |
2554 | llvm::Intrinsic::objc_retainAutorelease); |
2555 | } |
2556 | |
2557 | |
2558 | |
2559 | llvm::Value *CodeGenFunction::EmitARCLoadWeak(Address addr) { |
2560 | return emitARCLoadOperation(*this, addr, |
2561 | CGM.getObjCEntrypoints().objc_loadWeak, |
2562 | llvm::Intrinsic::objc_loadWeak); |
2563 | } |
2564 | |
2565 | |
2566 | llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(Address addr) { |
2567 | return emitARCLoadOperation(*this, addr, |
2568 | CGM.getObjCEntrypoints().objc_loadWeakRetained, |
2569 | llvm::Intrinsic::objc_loadWeakRetained); |
2570 | } |
2571 | |
2572 | |
2573 | |
2574 | llvm::Value *CodeGenFunction::EmitARCStoreWeak(Address addr, |
2575 | llvm::Value *value, |
2576 | bool ignored) { |
2577 | return emitARCStoreOperation(*this, addr, value, |
2578 | CGM.getObjCEntrypoints().objc_storeWeak, |
2579 | llvm::Intrinsic::objc_storeWeak, ignored); |
2580 | } |
2581 | |
2582 | |
2583 | |
2584 | |
2585 | |
2586 | void CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) { |
2587 | |
2588 | |
2589 | |
2590 | |
2591 | if (isa<llvm::ConstantPointerNull>(value) && |
2592 | CGM.getCodeGenOpts().OptimizationLevel == 0) { |
2593 | Builder.CreateStore(value, addr); |
2594 | return; |
2595 | } |
2596 | |
2597 | emitARCStoreOperation(*this, addr, value, |
2598 | CGM.getObjCEntrypoints().objc_initWeak, |
2599 | llvm::Intrinsic::objc_initWeak, true); |
2600 | } |
2601 | |
2602 | |
2603 | |
2604 | void CodeGenFunction::EmitARCDestroyWeak(Address addr) { |
2605 | llvm::Function *&fn = CGM.getObjCEntrypoints().objc_destroyWeak; |
2606 | if (!fn) { |
2607 | fn = CGM.getIntrinsic(llvm::Intrinsic::objc_destroyWeak); |
2608 | setARCRuntimeFunctionLinkage(CGM, fn); |
2609 | } |
2610 | |
2611 | |
2612 | addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); |
2613 | |
2614 | EmitNounwindRuntimeCall(fn, addr.getPointer()); |
2615 | } |
2616 | |
2617 | |
2618 | |
2619 | |
2620 | void CodeGenFunction::EmitARCMoveWeak(Address dst, Address src) { |
2621 | emitARCCopyOperation(*this, dst, src, |
2622 | CGM.getObjCEntrypoints().objc_moveWeak, |
2623 | llvm::Intrinsic::objc_moveWeak); |
2624 | } |
2625 | |
2626 | |
2627 | |
2628 | |
2629 | void CodeGenFunction::EmitARCCopyWeak(Address dst, Address src) { |
2630 | emitARCCopyOperation(*this, dst, src, |
2631 | CGM.getObjCEntrypoints().objc_copyWeak, |
2632 | llvm::Intrinsic::objc_copyWeak); |
2633 | } |
2634 | |
2635 | void CodeGenFunction::emitARCCopyAssignWeak(QualType Ty, Address DstAddr, |
2636 | Address SrcAddr) { |
2637 | llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr); |
2638 | Object = EmitObjCConsumeObject(Ty, Object); |
2639 | EmitARCStoreWeak(DstAddr, Object, false); |
2640 | } |
2641 | |
2642 | void CodeGenFunction::emitARCMoveAssignWeak(QualType Ty, Address DstAddr, |
2643 | Address SrcAddr) { |
2644 | llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr); |
2645 | Object = EmitObjCConsumeObject(Ty, Object); |
2646 | EmitARCStoreWeak(DstAddr, Object, false); |
2647 | EmitARCDestroyWeak(SrcAddr); |
2648 | } |
2649 | |
2650 | |
2651 | |
2652 | llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { |
2653 | llvm::Function *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush; |
2654 | if (!fn) { |
2655 | fn = CGM.getIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPush); |
2656 | setARCRuntimeFunctionLinkage(CGM, fn); |
2657 | } |
2658 | |
2659 | return EmitNounwindRuntimeCall(fn); |
2660 | } |
2661 | |
2662 | |
2663 | |
2664 | void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { |
2665 | assert(value->getType() == Int8PtrTy); |
2666 | |
2667 | if (getInvokeDest()) { |
2668 | |
2669 | llvm::FunctionCallee &fn = |
2670 | CGM.getObjCEntrypoints().objc_autoreleasePoolPopInvoke; |
2671 | if (!fn) { |
2672 | llvm::FunctionType *fnType = |
2673 | llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); |
2674 | fn = CGM.CreateRuntimeFunction(fnType, "objc_autoreleasePoolPop"); |
2675 | setARCRuntimeFunctionLinkage(CGM, fn); |
2676 | } |
2677 | |
2678 | |
2679 | EmitRuntimeCallOrInvoke(fn, value); |
2680 | } else { |
2681 | llvm::FunctionCallee &fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop; |
2682 | if (!fn) { |
2683 | fn = CGM.getIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPop); |
2684 | setARCRuntimeFunctionLinkage(CGM, fn); |
2685 | } |
2686 | |
2687 | EmitRuntimeCall(fn, value); |
2688 | } |
2689 | } |
2690 | |
2691 | |
2692 | |
2693 | |
2694 | |
2695 | |
2696 | llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { |
2697 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
2698 | llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); |
2699 | |
2700 | IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); |
2701 | Selector AllocSel = getContext().Selectors.getSelector(0, &II); |
2702 | CallArgList Args; |
2703 | RValue AllocRV = |
2704 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
2705 | getContext().getObjCIdType(), |
2706 | AllocSel, Receiver, Args); |
2707 | |
2708 | |
2709 | Receiver = AllocRV.getScalarVal(); |
2710 | II = &CGM.getContext().Idents.get("init"); |
2711 | Selector InitSel = getContext().Selectors.getSelector(0, &II); |
2712 | RValue InitRV = |
2713 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
2714 | getContext().getObjCIdType(), |
2715 | InitSel, Receiver, Args); |
2716 | return InitRV.getScalarVal(); |
2717 | } |
2718 | |
2719 | |
2720 | |
2721 | llvm::Value *CodeGenFunction::EmitObjCAlloc(llvm::Value *value, |
2722 | llvm::Type *resultType) { |
2723 | return emitObjCValueOperation(*this, value, resultType, |
2724 | CGM.getObjCEntrypoints().objc_alloc, |
2725 | "objc_alloc"); |
2726 | } |
2727 | |
2728 | |
2729 | |
2730 | llvm::Value *CodeGenFunction::EmitObjCAllocWithZone(llvm::Value *value, |
2731 | llvm::Type *resultType) { |
2732 | return emitObjCValueOperation(*this, value, resultType, |
2733 | CGM.getObjCEntrypoints().objc_allocWithZone, |
2734 | "objc_allocWithZone"); |
2735 | } |
2736 | |
2737 | llvm::Value *CodeGenFunction::EmitObjCAllocInit(llvm::Value *value, |
2738 | llvm::Type *resultType) { |
2739 | return emitObjCValueOperation(*this, value, resultType, |
2740 | CGM.getObjCEntrypoints().objc_alloc_init, |
2741 | "objc_alloc_init"); |
2742 | } |
2743 | |
2744 | |
2745 | |
2746 | void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { |
2747 | IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); |
2748 | Selector DrainSel = getContext().Selectors.getSelector(0, &II); |
2749 | CallArgList Args; |
2750 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
2751 | getContext().VoidTy, DrainSel, Arg, Args); |
2752 | } |
2753 | |
2754 | void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, |
2755 | Address addr, |
2756 | QualType type) { |
2757 | CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); |
2758 | } |
2759 | |
2760 | void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, |
2761 | Address addr, |
2762 | QualType type) { |
2763 | CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); |
2764 | } |
2765 | |
2766 | void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, |
2767 | Address addr, |
2768 | QualType type) { |
2769 | CGF.EmitARCDestroyWeak(addr); |
2770 | } |
2771 | |
2772 | void CodeGenFunction::emitARCIntrinsicUse(CodeGenFunction &CGF, Address addr, |
2773 | QualType type) { |
2774 | llvm::Value *value = CGF.Builder.CreateLoad(addr); |
2775 | CGF.EmitARCIntrinsicUse(value); |
2776 | } |
2777 | |
2778 | |
2779 | |
2780 | llvm::Value *CodeGenFunction::EmitObjCAutorelease(llvm::Value *value, |
2781 | llvm::Type *returnType) { |
2782 | return emitObjCValueOperation( |
2783 | *this, value, returnType, |
2784 | CGM.getObjCEntrypoints().objc_autoreleaseRuntimeFunction, |
2785 | "objc_autorelease"); |
2786 | } |
2787 | |
2788 | |
2789 | |
2790 | llvm::Value *CodeGenFunction::EmitObjCRetainNonBlock(llvm::Value *value, |
2791 | llvm::Type *returnType) { |
2792 | return emitObjCValueOperation( |
2793 | *this, value, returnType, |
2794 | CGM.getObjCEntrypoints().objc_retainRuntimeFunction, "objc_retain"); |
2795 | } |
2796 | |
2797 | |
2798 | |
2799 | void CodeGenFunction::EmitObjCRelease(llvm::Value *value, |
2800 | ARCPreciseLifetime_t precise) { |
2801 | if (isa<llvm::ConstantPointerNull>(value)) return; |
2802 | |
2803 | llvm::FunctionCallee &fn = |
2804 | CGM.getObjCEntrypoints().objc_releaseRuntimeFunction; |
2805 | if (!fn) { |
2806 | llvm::FunctionType *fnType = |
2807 | llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); |
2808 | fn = CGM.CreateRuntimeFunction(fnType, "objc_release"); |
2809 | setARCRuntimeFunctionLinkage(CGM, fn); |
2810 | |
2811 | if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee())) |
2812 | f->addFnAttr(llvm::Attribute::NonLazyBind); |
2813 | } |
2814 | |
2815 | |
2816 | value = Builder.CreateBitCast(value, Int8PtrTy); |
2817 | |
2818 | |
2819 | llvm::CallBase *call = EmitCallOrInvoke(fn, value); |
2820 | |
2821 | if (precise == ARCImpreciseLifetime) { |
2822 | call->setMetadata("clang.imprecise_release", |
2823 | llvm::MDNode::get(Builder.getContext(), None)); |
2824 | } |
2825 | } |
2826 | |
2827 | namespace { |
2828 | struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup { |
2829 | llvm::Value *Token; |
2830 | |
2831 | CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} |
2832 | |
2833 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
2834 | CGF.EmitObjCAutoreleasePoolPop(Token); |
2835 | } |
2836 | }; |
2837 | struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup { |
2838 | llvm::Value *Token; |
2839 | |
2840 | CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} |
2841 | |
2842 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
2843 | CGF.EmitObjCMRRAutoreleasePoolPop(Token); |
2844 | } |
2845 | }; |
2846 | } |
2847 | |
2848 | void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { |
2849 | if (CGM.getLangOpts().ObjCAutoRefCount) |
2850 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); |
2851 | else |
2852 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); |
2853 | } |
2854 | |
2855 | static bool shouldRetainObjCLifetime(Qualifiers::ObjCLifetime lifetime) { |
2856 | switch (lifetime) { |
2857 | case Qualifiers::OCL_None: |
2858 | case Qualifiers::OCL_ExplicitNone: |
2859 | case Qualifiers::OCL_Strong: |
2860 | case Qualifiers::OCL_Autoreleasing: |
2861 | return true; |
2862 | |
2863 | case Qualifiers::OCL_Weak: |
2864 | return false; |
2865 | } |
2866 | |
2867 | llvm_unreachable("impossible lifetime!"); |
2868 | } |
2869 | |
2870 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
2871 | LValue lvalue, |
2872 | QualType type) { |
2873 | llvm::Value *result; |
2874 | bool shouldRetain = shouldRetainObjCLifetime(type.getObjCLifetime()); |
2875 | if (shouldRetain) { |
2876 | result = CGF.EmitLoadOfLValue(lvalue, SourceLocation()).getScalarVal(); |
2877 | } else { |
2878 | assert(type.getObjCLifetime() == Qualifiers::OCL_Weak); |
2879 | result = CGF.EmitARCLoadWeakRetained(lvalue.getAddress(CGF)); |
2880 | } |
2881 | return TryEmitResult(result, !shouldRetain); |
2882 | } |
2883 | |
2884 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
2885 | const Expr *e) { |
2886 | e = e->IgnoreParens(); |
2887 | QualType type = e->getType(); |
2888 | |
2889 | |
2890 | |
2891 | |
2892 | if (e->isXValue() && |
2893 | !type.isConstQualified() && |
2894 | type.getObjCLifetime() == Qualifiers::OCL_Strong) { |
2895 | |
2896 | LValue lv = CGF.EmitLValue(e); |
2897 | |
2898 | |
2899 | llvm::Value *result = CGF.EmitLoadOfLValue(lv, |
2900 | SourceLocation()).getScalarVal(); |
2901 | |
2902 | |
2903 | CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress(CGF)), lv); |
2904 | |
2905 | return TryEmitResult(result, true); |
2906 | } |
2907 | |
2908 | |
2909 | |
2910 | |
2911 | if (CGF.getLangOpts().CPlusPlus && |
2912 | !type.isVolatileQualified() && |
2913 | type.getObjCLifetime() == Qualifiers::OCL_Weak && |
2914 | isa<BinaryOperator>(e) && |
2915 | cast<BinaryOperator>(e)->getOpcode() == BO_Assign) |
2916 | return TryEmitResult(CGF.EmitScalarExpr(e), false); |
2917 | |
2918 | |
2919 | |
2920 | |
2921 | if (const auto *decl_expr = dyn_cast<DeclRefExpr>(e)) { |
2922 | auto *DRE = const_cast<DeclRefExpr *>(decl_expr); |
2923 | if (CodeGenFunction::ConstantEmission constant = CGF.tryEmitAsConstant(DRE)) |
2924 | return TryEmitResult(CGF.emitScalarConstant(constant, DRE), |
2925 | !shouldRetainObjCLifetime(type.getObjCLifetime())); |
2926 | } |
2927 | |
2928 | return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); |
2929 | } |
2930 | |
2931 | typedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF, |
2932 | llvm::Value *value)> |
2933 | ValueTransform; |
2934 | |
2935 | |
2936 | |
2937 | |
2938 | |
2939 | static llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF, |
2940 | llvm::Value *value, |
2941 | ValueTransform doAfterCall, |
2942 | ValueTransform doFallback) { |
2943 | CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); |
2944 | auto *callBase = dyn_cast<llvm::CallBase>(value); |
2945 | |
2946 | if (callBase && llvm::objcarc::hasAttachedCallOpBundle(callBase)) { |
2947 | |
2948 | value = doFallback(CGF, value); |
2949 | } else if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { |
2950 | |
2951 | CGF.Builder.SetInsertPoint(call->getParent(), |
2952 | ++llvm::BasicBlock::iterator(call)); |
2953 | value = doAfterCall(CGF, value); |
2954 | } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { |
2955 | |
2956 | llvm::BasicBlock *BB = invoke->getNormalDest(); |
2957 | CGF.Builder.SetInsertPoint(BB, BB->begin()); |
2958 | value = doAfterCall(CGF, value); |
2959 | |
2960 | |
2961 | |
2962 | } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { |
2963 | |
2964 | |
2965 | CGF.Builder.SetInsertPoint(bitcast->getParent(), bitcast->getIterator()); |
2966 | llvm::Value *operand = bitcast->getOperand(0); |
2967 | operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback); |
2968 | bitcast->setOperand(0, operand); |
2969 | value = bitcast; |
2970 | } else { |
2971 | auto *phi = dyn_cast<llvm::PHINode>(value); |
2972 | if (phi && phi->getNumIncomingValues() == 2 && |
2973 | isa<llvm::ConstantPointerNull>(phi->getIncomingValue(1)) && |
2974 | isa<llvm::CallBase>(phi->getIncomingValue(0))) { |
2975 | |
2976 | |
2977 | llvm::Value *inVal = phi->getIncomingValue(0); |
2978 | inVal = emitARCOperationAfterCall(CGF, inVal, doAfterCall, doFallback); |
2979 | phi->setIncomingValue(0, inVal); |
2980 | value = phi; |
2981 | } else { |
2982 | |
2983 | |
2984 | |
2985 | value = doFallback(CGF, value); |
2986 | } |
2987 | } |
2988 | |
2989 | CGF.Builder.restoreIP(ip); |
2990 | return value; |
2991 | } |
2992 | |
2993 | |
2994 | |
2995 | static llvm::Value *emitARCRetainCallResult(CodeGenFunction &CGF, |
2996 | const Expr *e) { |
2997 | llvm::Value *value = CGF.EmitScalarExpr(e); |
2998 | return emitARCOperationAfterCall(CGF, value, |
2999 | [](CodeGenFunction &CGF, llvm::Value *value) { |
3000 | return CGF.EmitARCRetainAutoreleasedReturnValue(value); |
3001 | }, |
3002 | [](CodeGenFunction &CGF, llvm::Value *value) { |
3003 | return CGF.EmitARCRetainNonBlock(value); |
3004 | }); |
3005 | } |
3006 | |
3007 | |
3008 | |
3009 | static llvm::Value *emitARCUnsafeClaimCallResult(CodeGenFunction &CGF, |
3010 | const Expr *e) { |
3011 | llvm::Value *value = CGF.EmitScalarExpr(e); |
3012 | return emitARCOperationAfterCall(CGF, value, |
3013 | [](CodeGenFunction &CGF, llvm::Value *value) { |
3014 | return CGF.EmitARCUnsafeClaimAutoreleasedReturnValue(value); |
3015 | }, |
3016 | [](CodeGenFunction &CGF, llvm::Value *value) { |
3017 | return value; |
3018 | }); |
3019 | } |
3020 | |
3021 | llvm::Value *CodeGenFunction::EmitARCReclaimReturnedObject(const Expr *E, |
3022 | bool allowUnsafeClaim) { |
3023 | if (allowUnsafeClaim && |
3024 | CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()) { |
3025 | return emitARCUnsafeClaimCallResult(*this, E); |
3026 | } else { |
3027 | llvm::Value *value = emitARCRetainCallResult(*this, E); |
3028 | return EmitObjCConsumeObject(E->getType(), value); |
3029 | } |
3030 | } |
3031 | |
3032 | |
3033 | |
3034 | |
3035 | static bool shouldEmitSeparateBlockRetain(const Expr *e) { |
3036 | assert(e->getType()->isBlockPointerType()); |
3037 | e = e->IgnoreParens(); |
3038 | |
3039 | |
3040 | |
3041 | if (isa<BlockExpr>(e)) |
3042 | return false; |
3043 | |
3044 | if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { |
3045 | switch (cast->getCastKind()) { |
3046 | |
3047 | case CK_LValueToRValue: |
3048 | case CK_ARCReclaimReturnedObject: |
3049 | case CK_ARCConsumeObject: |
3050 | case CK_ARCProduceObject: |
3051 | return false; |
3052 | |
3053 | |
3054 | case CK_NoOp: |
3055 | case CK_BitCast: |
3056 | return shouldEmitSeparateBlockRetain(cast->getSubExpr()); |
3057 | |
3058 | |
3059 | case CK_AnyPointerToBlockPointerCast: |
3060 | default: |
3061 | return true; |
3062 | } |
3063 | } |
3064 | |
3065 | return true; |
3066 | } |
3067 | |
3068 | namespace { |
3069 | |
3070 | |
3071 | template <typename Impl, typename Result> class ARCExprEmitter { |
3072 | protected: |
3073 | CodeGenFunction &CGF; |
3074 | Impl &asImpl() { return *static_cast<Impl*>(this); } |
3075 | |
3076 | ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {} |
3077 | |
3078 | public: |
3079 | Result visit(const Expr *e); |
3080 | Result visitCastExpr(const CastExpr *e); |
3081 | Result visitPseudoObjectExpr(const PseudoObjectExpr *e); |
3082 | Result visitBlockExpr(const BlockExpr *e); |
3083 | Result visitBinaryOperator(const BinaryOperator *e); |
3084 | Result visitBinAssign(const BinaryOperator *e); |
3085 | Result visitBinAssignUnsafeUnretained(const BinaryOperator *e); |
3086 | Result visitBinAssignAutoreleasing(const BinaryOperator *e); |
3087 | Result visitBinAssignWeak(const BinaryOperator *e); |
3088 | Result visitBinAssignStrong(const BinaryOperator *e); |
3089 | |
3090 | |
3091 | |
3092 | |
3093 | |
3094 | |
3095 | |
3096 | |
3097 | |
3098 | |
3099 | |
3100 | }; |
3101 | } |
3102 | |
3103 | |
3104 | |
3105 | |
3106 | template <typename Impl, typename Result> |
3107 | Result |
3108 | ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) { |
3109 | SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; |
3110 | |
3111 | |
3112 | const Expr *resultExpr = E->getResultExpr(); |
3113 | assert(resultExpr); |
3114 | Result result; |
| 17 | | 'result' declared without an initial value | |
|
3115 | |
3116 | for (PseudoObjectExpr::const_semantics_iterator |
| 18 | | Loop condition is false. Execution continues on line 3152 | |
|
3117 | i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { |
3118 | const Expr *semantic = *i; |
3119 | |
3120 | |
3121 | |
3122 | if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { |
3123 | typedef CodeGenFunction::OpaqueValueMappingData OVMA; |
3124 | OVMA opaqueData; |
3125 | |
3126 | |
3127 | |
3128 | if (ov == resultExpr) { |
3129 | assert(!OVMA::shouldBindAsLValue(ov)); |
3130 | result = asImpl().visit(ov->getSourceExpr()); |
3131 | opaqueData = OVMA::bind(CGF, ov, |
3132 | RValue::get(asImpl().getValueOfResult(result))); |
3133 | |
3134 | |
3135 | } else { |
3136 | opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); |
3137 | } |
3138 | opaques.push_back(opaqueData); |
3139 | |
3140 | |
3141 | |
3142 | } else if (semantic == resultExpr) { |
3143 | result = asImpl().visit(semantic); |
3144 | |
3145 | |
3146 | } else { |
3147 | CGF.EmitIgnoredExpr(semantic); |
3148 | } |
3149 | } |
3150 | |
3151 | |
3152 | for (unsigned i = 0, e = opaques.size(); i != e; ++i) |
| 19 | | Assuming 'i' is equal to 'e' | |
|
| 20 | | Loop condition is false. Execution continues on line 3155 | |
|
3153 | opaques[i].unbind(CGF); |
3154 | |
3155 | return result; |
| 21 | | Undefined or garbage value returned to caller |
|
3156 | } |
3157 | |
3158 | template <typename Impl, typename Result> |
3159 | Result ARCExprEmitter<Impl, Result>::visitBlockExpr(const BlockExpr *e) { |
3160 | |
3161 | return asImpl().visitExpr(e); |
3162 | } |
3163 | |
3164 | template <typename Impl, typename Result> |
3165 | Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) { |
3166 | switch (e->getCastKind()) { |
3167 | |
3168 | |
3169 | case CK_NoOp: |
3170 | return asImpl().visit(e->getSubExpr()); |
3171 | |
3172 | |
3173 | case CK_CPointerToObjCPointerCast: |
3174 | case CK_BlockPointerToObjCPointerCast: |
3175 | case CK_AnyPointerToBlockPointerCast: |
3176 | case CK_BitCast: { |
3177 | llvm::Type *resultType = CGF.ConvertType(e->getType()); |
3178 | assert(e->getSubExpr()->getType()->hasPointerRepresentation()); |
3179 | Result result = asImpl().visit(e->getSubExpr()); |
3180 | return asImpl().emitBitCast(result, resultType); |
3181 | } |
3182 | |
3183 | |
3184 | case CK_LValueToRValue: |
3185 | return asImpl().visitLValueToRValue(e->getSubExpr()); |
3186 | case CK_ARCConsumeObject: |
3187 | return asImpl().visitConsumeObject(e->getSubExpr()); |
3188 | case CK_ARCExtendBlockObject: |
3189 | return asImpl().visitExtendBlockObject(e->getSubExpr()); |
3190 | case CK_ARCReclaimReturnedObject: |
3191 | return asImpl().visitReclaimReturnedObject(e->getSubExpr()); |
3192 | |
3193 | |
3194 | default: |
3195 | return asImpl().visitExpr(e); |
3196 | } |
3197 | } |
3198 | |
3199 | template <typename Impl, typename Result> |
3200 | Result |
3201 | ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) { |
3202 | switch (e->getOpcode()) { |
3203 | case BO_Comma: |
3204 | CGF.EmitIgnoredExpr(e->getLHS()); |
3205 | CGF.EnsureInsertPoint(); |
3206 | return asImpl().visit(e->getRHS()); |
3207 | |
3208 | case BO_Assign: |
3209 | return asImpl().visitBinAssign(e); |
3210 | |
3211 | default: |
3212 | return asImpl().visitExpr(e); |
3213 | } |
3214 | } |
3215 | |
3216 | template <typename Impl, typename Result> |
3217 | Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) { |
3218 | switch (e->getLHS()->getType().getObjCLifetime()) { |
3219 | case Qualifiers::OCL_ExplicitNone: |
3220 | return asImpl().visitBinAssignUnsafeUnretained(e); |
3221 | |
3222 | case Qualifiers::OCL_Weak: |
3223 | return asImpl().visitBinAssignWeak(e); |
3224 | |
3225 | case Qualifiers::OCL_Autoreleasing: |
3226 | return asImpl().visitBinAssignAutoreleasing(e); |
3227 | |
3228 | case Qualifiers::OCL_Strong: |
3229 | return asImpl().visitBinAssignStrong(e); |
3230 | |
3231 | case Qualifiers::OCL_None: |
3232 | return asImpl().visitExpr(e); |
3233 | } |
3234 | llvm_unreachable("bad ObjC ownership qualifier"); |
3235 | } |
3236 | |
3237 | |
3238 | |
3239 | template <typename Impl, typename Result> |
3240 | Result ARCExprEmitter<Impl,Result>:: |
3241 | visitBinAssignUnsafeUnretained(const BinaryOperator *e) { |
3242 | |
3243 | |
3244 | Result result = asImpl().visit(e->getRHS()); |
3245 | |
3246 | |
3247 | LValue lvalue = |
3248 | CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store); |
3249 | CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)), |
3250 | lvalue); |
3251 | |
3252 | return result; |
3253 | } |
3254 | |
3255 | template <typename Impl, typename Result> |
3256 | Result |
3257 | ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) { |
3258 | return asImpl().visitExpr(e); |
3259 | } |
3260 | |
3261 | template <typename Impl, typename Result> |
3262 | Result |
3263 | ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) { |
3264 | return asImpl().visitExpr(e); |
3265 | } |
3266 | |
3267 | template <typename Impl, typename Result> |
3268 | Result |
3269 | ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) { |
3270 | return asImpl().visitExpr(e); |
3271 | } |
3272 | |
3273 | |
3274 | template <typename Impl, typename Result> |
3275 | Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) { |
3276 | |
3277 | |
3278 | |
3279 | |
3280 | assert(!isa<ExprWithCleanups>(e)); |
3281 | |
3282 | |
3283 | e = e->IgnoreParens(); |
3284 | |
3285 | |
3286 | if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 8 | | Assuming 'e' is not a 'CastExpr' | |
|
| |
3287 | return asImpl().visitCastExpr(ce); |
3288 | |
3289 | |
3290 | } else if (auto op = dyn_cast<BinaryOperator>(e)) { |
| 10 | | Assuming 'e' is not a 'BinaryOperator' | |
|
| |
3291 | return asImpl().visitBinaryOperator(op); |
3292 | |
3293 | |
3294 | |
3295 | |
3296 | |
3297 | |
3298 | |
3299 | } else if (isa<CallExpr>(e) || |
| 12 | | Assuming 'e' is not a 'CallExpr' | |
|
3300 | (isa<ObjCMessageExpr>(e) && |
| 13 | | Assuming 'e' is not a 'ObjCMessageExpr' | |
|
3301 | !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { |
3302 | return asImpl().visitCall(e); |
3303 | |
3304 | |
3305 | } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 14 | | Assuming 'e' is a 'PseudoObjectExpr' | |
|
| |
3306 | return asImpl().visitPseudoObjectExpr(pseudo); |
| 16 | | Calling 'ARCExprEmitter::visitPseudoObjectExpr' | |
|
3307 | } else if (auto *be = dyn_cast<BlockExpr>(e)) |
3308 | return asImpl().visitBlockExpr(be); |
3309 | |
3310 | return asImpl().visitExpr(e); |
3311 | } |
3312 | |
3313 | namespace { |
3314 | |
3315 | |
3316 | struct ARCRetainExprEmitter : |
3317 | public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> { |
3318 | |
3319 | ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {} |
3320 | |
3321 | llvm::Value *getValueOfResult(TryEmitResult result) { |
3322 | return result.getPointer(); |
3323 | } |
3324 | |
3325 | TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) { |
3326 | llvm::Value *value = result.getPointer(); |
3327 | value = CGF.Builder.CreateBitCast(value, resultType); |
3328 | result.setPointer(value); |
3329 | return result; |
3330 | } |
3331 | |
3332 | TryEmitResult visitLValueToRValue(const Expr *e) { |
3333 | return tryEmitARCRetainLoadOfScalar(CGF, e); |
3334 | } |
3335 | |
3336 | |
3337 | |
3338 | TryEmitResult visitConsumeObject(const Expr *e) { |
3339 | llvm::Value *result = CGF.EmitScalarExpr(e); |
3340 | return TryEmitResult(result, true); |
3341 | } |
3342 | |
3343 | TryEmitResult visitBlockExpr(const BlockExpr *e) { |
3344 | TryEmitResult result = visitExpr(e); |
3345 | |
3346 | |
3347 | if (e->getBlockDecl()->canAvoidCopyToHeap()) |
3348 | result.setInt(true); |
3349 | return result; |
3350 | } |
3351 | |
3352 | |
3353 | |
3354 | |
3355 | TryEmitResult visitExtendBlockObject(const Expr *e) { |
3356 | llvm::Value *result; |
3357 | |
3358 | |
3359 | |
3360 | if (shouldEmitSeparateBlockRetain(e)) { |
3361 | result = CGF.EmitScalarExpr(e); |
3362 | |
3363 | |
3364 | } else { |
3365 | TryEmitResult subresult = asImpl().visit(e); |
3366 | |
3367 | |
3368 | if (subresult.getInt()) { |
3369 | return subresult; |
3370 | } |
3371 | |
3372 | |
3373 | result = subresult.getPointer(); |
3374 | } |
3375 | |
3376 | |
3377 | result = CGF.EmitARCRetainBlock(result, true); |
3378 | return TryEmitResult(result, true); |
3379 | } |
3380 | |
3381 | |
3382 | |
3383 | TryEmitResult visitReclaimReturnedObject(const Expr *e) { |
3384 | llvm::Value *result = emitARCRetainCallResult(CGF, e); |
3385 | return TryEmitResult(result, true); |
3386 | } |
3387 | |
3388 | |
3389 | TryEmitResult visitCall(const Expr *e) { |
3390 | llvm::Value *result = emitARCRetainCallResult(CGF, e); |
3391 | return TryEmitResult(result, true); |
3392 | } |
3393 | |
3394 | |
3395 | |
3396 | TryEmitResult visitExpr(const Expr *e) { |
3397 | |
3398 | |
3399 | llvm::Value *result = CGF.EmitScalarExpr(e); |
3400 | return TryEmitResult(result, false); |
3401 | } |
3402 | }; |
3403 | } |
3404 | |
3405 | static TryEmitResult |
3406 | tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { |
3407 | return ARCRetainExprEmitter(CGF).visit(e); |
3408 | } |
3409 | |
3410 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
3411 | LValue lvalue, |
3412 | QualType type) { |
3413 | TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); |
3414 | llvm::Value *value = result.getPointer(); |
3415 | if (!result.getInt()) |
3416 | value = CGF.EmitARCRetain(type, value); |
3417 | return value; |
3418 | } |
3419 | |
3420 | |
3421 | |
3422 | |
3423 | |
3424 | llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { |
3425 | |
3426 | if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { |
3427 | RunCleanupsScope scope(*this); |
3428 | return EmitARCRetainScalarExpr(cleanups->getSubExpr()); |
3429 | } |
3430 | |
3431 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); |
3432 | llvm::Value *value = result.getPointer(); |
3433 | if (!result.getInt()) |
3434 | value = EmitARCRetain(e->getType(), value); |
3435 | return value; |
3436 | } |
3437 | |
3438 | llvm::Value * |
3439 | CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { |
3440 | |
3441 | if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { |
3442 | RunCleanupsScope scope(*this); |
3443 | return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); |
3444 | } |
3445 | |
3446 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); |
3447 | llvm::Value *value = result.getPointer(); |
3448 | if (result.getInt()) |
3449 | value = EmitARCAutorelease(value); |
3450 | else |
3451 | value = EmitARCRetainAutorelease(e->getType(), value); |
3452 | return value; |
3453 | } |
3454 | |
3455 | llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { |
3456 | llvm::Value *result; |
3457 | bool doRetain; |
3458 | |
3459 | if (shouldEmitSeparateBlockRetain(e)) { |
3460 | result = EmitScalarExpr(e); |
3461 | doRetain = true; |
3462 | } else { |
3463 | TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); |
3464 | result = subresult.getPointer(); |
3465 | doRetain = !subresult.getInt(); |
3466 | } |
3467 | |
3468 | if (doRetain) |
3469 | result = EmitARCRetainBlock(result, true); |
3470 | return EmitObjCConsumeObject(e->getType(), result); |
3471 | } |
3472 | |
3473 | llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { |
3474 | |
3475 | if (getLangOpts().ObjCAutoRefCount) { |
3476 | |
3477 | |
3478 | return EmitARCRetainAutoreleaseScalarExpr(expr); |
3479 | } |
3480 | |
3481 | |
3482 | |
3483 | |
3484 | |
3485 | |
3486 | return EmitScalarExpr(expr); |
3487 | } |
3488 | |
3489 | namespace { |
3490 | |
3491 | |
3492 | struct ARCUnsafeUnretainedExprEmitter : |
3493 | public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> { |
3494 | |
3495 | ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {} |
3496 | |
3497 | llvm::Value *getValueOfResult(llvm::Value *value) { |
3498 | return value; |
3499 | } |
3500 | |
3501 | llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) { |
3502 | return CGF.Builder.CreateBitCast(value, resultType); |
3503 | } |
3504 | |
3505 | llvm::Value *visitLValueToRValue(const Expr *e) { |
3506 | return CGF.EmitScalarExpr(e); |
3507 | } |
3508 | |
3509 | |
3510 | |
3511 | llvm::Value *visitConsumeObject(const Expr *e) { |
3512 | llvm::Value *value = CGF.EmitScalarExpr(e); |
3513 | return CGF.EmitObjCConsumeObject(e->getType(), value); |
3514 | } |
3515 | |
3516 | |
3517 | |
3518 | llvm::Value *visitExtendBlockObject(const Expr *e) { |
3519 | return CGF.EmitARCExtendBlockObject(e); |
3520 | } |
3521 | |
3522 | |
3523 | llvm::Value *visitReclaimReturnedObject(const Expr *e) { |
3524 | return CGF.EmitARCReclaimReturnedObject(e, true); |
3525 | } |
3526 | |
3527 | |
3528 | |
3529 | llvm::Value *visitCall(const Expr *e) { |
3530 | return CGF.EmitScalarExpr(e); |
3531 | } |
3532 | |
3533 | |
3534 | llvm::Value *visitExpr(const Expr *e) { |
3535 | return CGF.EmitScalarExpr(e); |
3536 | } |
3537 | }; |
3538 | } |
3539 | |
3540 | static llvm::Value *emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF, |
3541 | const Expr *e) { |
3542 | return ARCUnsafeUnretainedExprEmitter(CGF).visit(e); |
| 7 | | Calling 'ARCExprEmitter::visit' | |
|
3543 | } |
3544 | |
3545 | |
3546 | |
3547 | |
3548 | |
3549 | llvm::Value *CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr(const Expr *e) { |
3550 | |
3551 | if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { |
| 4 | | Assuming 'e' is not a 'ExprWithCleanups' | |
|
| |
3552 | RunCleanupsScope scope(*this); |
3553 | return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr()); |
3554 | } |
3555 | |
3556 | return emitARCUnsafeUnretainedScalarExpr(*this, e); |
| 6 | | Calling 'emitARCUnsafeUnretainedScalarExpr' | |
|
3557 | } |
3558 | |
3559 | std::pair<LValue,llvm::Value*> |
3560 | CodeGenFunction::EmitARCStoreUnsafeUnretained(const BinaryOperator *e, |
3561 | bool ignored) { |
3562 | |
3563 | |
3564 | llvm::Value *value; |
3565 | if (ignored) { |
| 1 | Assuming 'ignored' is true | |
|
| |
3566 | value = EmitARCUnsafeUnretainedScalarExpr(e->getRHS()); |
| 3 | | Calling 'CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr' | |
|
3567 | } else { |
3568 | value = EmitScalarExpr(e->getRHS()); |
3569 | } |
3570 | |
3571 | |
3572 | LValue lvalue = EmitLValue(e->getLHS()); |
3573 | EmitStoreOfScalar(value, lvalue); |
3574 | |
3575 | return std::pair<LValue,llvm::Value*>(std::move(lvalue), value); |
3576 | } |
3577 | |
3578 | std::pair<LValue,llvm::Value*> |
3579 | CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, |
3580 | bool ignored) { |
3581 | |
3582 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); |
3583 | llvm::Value *value = result.getPointer(); |
3584 | |
3585 | bool hasImmediateRetain = result.getInt(); |
3586 | |
3587 | |
3588 | |
3589 | |
3590 | if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { |
3591 | value = EmitARCRetainBlock(value, false); |
3592 | hasImmediateRetain = true; |
3593 | } |
3594 | |
3595 | LValue lvalue = EmitLValue(e->getLHS()); |
3596 | |
3597 | |
3598 | if (hasImmediateRetain) { |
3599 | llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation()); |
3600 | EmitStoreOfScalar(value, lvalue); |
3601 | EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); |
3602 | } else { |
3603 | value = EmitARCStoreStrong(lvalue, value, ignored); |
3604 | } |
3605 | |
3606 | return std::pair<LValue,llvm::Value*>(lvalue, value); |
3607 | } |
3608 | |
3609 | std::pair<LValue,llvm::Value*> |
3610 | CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { |
3611 | llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); |
3612 | LValue lvalue = EmitLValue(e->getLHS()); |
3613 | |
3614 | EmitStoreOfScalar(value, lvalue); |
3615 | |
3616 | return std::pair<LValue,llvm::Value*>(lvalue, value); |
3617 | } |
3618 | |
3619 | void CodeGenFunction::EmitObjCAutoreleasePoolStmt( |
3620 | const ObjCAutoreleasePoolStmt &ARPS) { |
3621 | const Stmt *subStmt = ARPS.getSubStmt(); |
3622 | const CompoundStmt &S = cast<CompoundStmt>(*subStmt); |
3623 | |
3624 | CGDebugInfo *DI = getDebugInfo(); |
3625 | if (DI) |
3626 | DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); |
3627 | |
3628 | |
3629 | RunCleanupsScope Scope(*this); |
3630 | if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { |
3631 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
3632 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); |
3633 | } else { |
3634 | llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); |
3635 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); |
3636 | } |
3637 | |
3638 | for (const auto *I : S.body()) |
3639 | EmitStmt(I); |
3640 | |
3641 | if (DI) |
3642 | DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); |
3643 | } |
3644 | |
3645 | |
3646 | |
3647 | void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { |
3648 | |
3649 | llvm::FunctionType *extenderType |
3650 | = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); |
3651 | llvm::InlineAsm *extender = llvm::InlineAsm::get(extenderType, |
3652 | "", |
3653 | "r", |
3654 | true); |
3655 | |
3656 | object = Builder.CreateBitCast(object, VoidPtrTy); |
3657 | EmitNounwindRuntimeCall(extender, object); |
3658 | } |
3659 | |
3660 | |
3661 | |
3662 | |
3663 | |
3664 | llvm::Constant * |
3665 | CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( |
3666 | const ObjCPropertyImplDecl *PID) { |
3667 | if (!getLangOpts().CPlusPlus || |
3668 | !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) |
3669 | return nullptr; |
3670 | QualType Ty = PID->getPropertyIvarDecl()->getType(); |
3671 | if (!Ty->isRecordType()) |
3672 | return nullptr; |
3673 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
3674 | if ((!(PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic))) |
3675 | return nullptr; |
3676 | llvm::Constant *HelperFn = nullptr; |
3677 | if (hasTrivialSetExpr(PID)) |
3678 | return nullptr; |
3679 | assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); |
3680 | if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) |
3681 | return HelperFn; |
3682 | |
3683 | ASTContext &C = getContext(); |
3684 | IdentifierInfo *II |
3685 | = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); |
3686 | |
3687 | QualType ReturnTy = C.VoidTy; |
3688 | QualType DestTy = C.getPointerType(Ty); |
3689 | QualType SrcTy = Ty; |
3690 | SrcTy.addConst(); |
3691 | SrcTy = C.getPointerType(SrcTy); |
3692 | |
3693 | SmallVector<QualType, 2> ArgTys; |
3694 | ArgTys.push_back(DestTy); |
3695 | ArgTys.push_back(SrcTy); |
3696 | QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {}); |
3697 | |
3698 | FunctionDecl *FD = FunctionDecl::Create( |
3699 | C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II, |
3700 | FunctionTy, nullptr, SC_Static, false, false); |
3701 | |
3702 | FunctionArgList args; |
3703 | ParmVarDecl *Params[2]; |
3704 | ParmVarDecl *DstDecl = ParmVarDecl::Create( |
3705 | C, FD, SourceLocation(), SourceLocation(), nullptr, DestTy, |
3706 | C.getTrivialTypeSourceInfo(DestTy, SourceLocation()), SC_None, |
3707 | nullptr); |
3708 | args.push_back(Params[0] = DstDecl); |
3709 | ParmVarDecl *SrcDecl = ParmVarDecl::Create( |
3710 | C, FD, SourceLocation(), SourceLocation(), nullptr, SrcTy, |
3711 | C.getTrivialTypeSourceInfo(SrcTy, SourceLocation()), SC_None, |
3712 | nullptr); |
3713 | args.push_back(Params[1] = SrcDecl); |
3714 | FD->setParams(Params); |
3715 | |
3716 | const CGFunctionInfo &FI = |
3717 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
3718 | |
3719 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
3720 | |
3721 | llvm::Function *Fn = |
3722 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
3723 | "__assign_helper_atomic_property_", |
3724 | &CGM.getModule()); |
3725 | |
3726 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
3727 | |
3728 | StartFunction(FD, ReturnTy, Fn, FI, args); |
3729 | |
3730 | DeclRefExpr DstExpr(C, DstDecl, false, DestTy, VK_PRValue, SourceLocation()); |
3731 | UnaryOperator *DST = UnaryOperator::Create( |
3732 | C, &DstExpr, UO_Deref, DestTy->getPointeeType(), VK_LValue, OK_Ordinary, |
3733 | SourceLocation(), false, FPOptionsOverride()); |
3734 | |
3735 | DeclRefExpr SrcExpr(C, SrcDecl, false, SrcTy, VK_PRValue, SourceLocation()); |
3736 | UnaryOperator *SRC = UnaryOperator::Create( |
3737 | C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary, |
3738 | SourceLocation(), false, FPOptionsOverride()); |
3739 | |
3740 | Expr *Args[2] = {DST, SRC}; |
3741 | CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); |
3742 | CXXOperatorCallExpr *TheCall = CXXOperatorCallExpr::Create( |
3743 | C, OO_Equal, CalleeExp->getCallee(), Args, DestTy->getPointeeType(), |
3744 | VK_LValue, SourceLocation(), FPOptionsOverride()); |
3745 | |
3746 | EmitStmt(TheCall); |
3747 | |
3748 | FinishFunction(); |
3749 | HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
3750 | CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); |
3751 | return HelperFn; |
3752 | } |
3753 | |
3754 | llvm::Constant * |
3755 | CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( |
3756 | const ObjCPropertyImplDecl *PID) { |
3757 | if (!getLangOpts().CPlusPlus || |
3758 | !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) |
3759 | return nullptr; |
3760 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
3761 | QualType Ty = PD->getType(); |
3762 | if (!Ty->isRecordType()) |
3763 | return nullptr; |
3764 | if ((!(PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic))) |
3765 | return nullptr; |
3766 | llvm::Constant *HelperFn = nullptr; |
3767 | if (hasTrivialGetExpr(PID)) |
3768 | return nullptr; |
3769 | assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); |
3770 | if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) |
3771 | return HelperFn; |
3772 | |
3773 | ASTContext &C = getContext(); |
3774 | IdentifierInfo *II = |
3775 | &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); |
3776 | |
3777 | QualType ReturnTy = C.VoidTy; |
3778 | QualType DestTy = C.getPointerType(Ty); |
3779 | QualType SrcTy = Ty; |
3780 | SrcTy.addConst(); |
3781 | SrcTy = C.getPointerType(SrcTy); |
3782 | |
3783 | SmallVector<QualType, 2> ArgTys; |
3784 | ArgTys.push_back(DestTy); |
3785 | ArgTys.push_back(SrcTy); |
3786 | QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {}); |
3787 | |
3788 | FunctionDecl *FD = FunctionDecl::Create( |
3789 | C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II, |
3790 | FunctionTy, nullptr, SC_Static, false, false); |
3791 | |
3792 | FunctionArgList args; |
3793 | ParmVarDecl *Params[2]; |
3794 | ParmVarDecl *DstDecl = ParmVarDecl::Create( |
3795 | C, FD, SourceLocation(), SourceLocation(), nullptr, DestTy, |
3796 | C.getTrivialTypeSourceInfo(DestTy, SourceLocation()), SC_None, |
3797 | nullptr); |
3798 | args.push_back(Params[0] = DstDecl); |
3799 | ParmVarDecl *SrcDecl = ParmVarDecl::Create( |
3800 | C, FD, SourceLocation(), SourceLocation(), nullptr, SrcTy, |
3801 | C.getTrivialTypeSourceInfo(SrcTy, SourceLocation()), SC_None, |
3802 | nullptr); |
3803 | args.push_back(Params[1] = SrcDecl); |
3804 | FD->setParams(Params); |
3805 | |
3806 | const CGFunctionInfo &FI = |
3807 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
3808 | |
3809 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
3810 | |
3811 | llvm::Function *Fn = llvm::Function::Create( |
3812 | LTy, llvm::GlobalValue::InternalLinkage, "__copy_helper_atomic_property_", |
3813 | &CGM.getModule()); |
3814 | |
3815 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
3816 | |
3817 | StartFunction(FD, ReturnTy, Fn, FI, args); |
3818 | |
3819 | DeclRefExpr SrcExpr(getContext(), SrcDecl, false, SrcTy, VK_PRValue, |
3820 | SourceLocation()); |
3821 | |
3822 | UnaryOperator *SRC = UnaryOperator::Create( |
3823 | C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary, |
3824 | SourceLocation(), false, FPOptionsOverride()); |
3825 | |
3826 | CXXConstructExpr *CXXConstExpr = |
3827 | cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); |
3828 | |
3829 | SmallVector<Expr*, 4> ConstructorArgs; |
3830 | ConstructorArgs.push_back(SRC); |
3831 | ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()), |
3832 | CXXConstExpr->arg_end()); |
3833 | |
3834 | CXXConstructExpr *TheCXXConstructExpr = |
3835 | CXXConstructExpr::Create(C, Ty, SourceLocation(), |
3836 | CXXConstExpr->getConstructor(), |
3837 | CXXConstExpr->isElidable(), |
3838 | ConstructorArgs, |
3839 | CXXConstExpr->hadMultipleCandidates(), |
3840 | CXXConstExpr->isListInitialization(), |
3841 | CXXConstExpr->isStdInitListInitialization(), |
3842 | CXXConstExpr->requiresZeroInitialization(), |
3843 | CXXConstExpr->getConstructionKind(), |
3844 | SourceRange()); |
3845 | |
3846 | DeclRefExpr DstExpr(getContext(), DstDecl, false, DestTy, VK_PRValue, |
3847 | SourceLocation()); |
3848 | |
3849 | RValue DV = EmitAnyExpr(&DstExpr); |
3850 | CharUnits Alignment |
3851 | = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); |
3852 | EmitAggExpr(TheCXXConstructExpr, |
3853 | AggValueSlot::forAddr(Address(DV.getScalarVal(), Alignment), |
3854 | Qualifiers(), |
3855 | AggValueSlot::IsDestructed, |
3856 | AggValueSlot::DoesNotNeedGCBarriers, |
3857 | AggValueSlot::IsNotAliased, |
3858 | AggValueSlot::DoesNotOverlap)); |
3859 | |
3860 | FinishFunction(); |
3861 | HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
3862 | CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); |
3863 | return HelperFn; |
3864 | } |
3865 | |
3866 | llvm::Value * |
3867 | CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { |
3868 | |
3869 | IdentifierInfo *CopyID = &getContext().Idents.get("copy"); |
3870 | Selector CopySelector = |
3871 | getContext().Selectors.getNullarySelector(CopyID); |
3872 | IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); |
3873 | Selector AutoreleaseSelector = |
3874 | getContext().Selectors.getNullarySelector(AutoreleaseID); |
3875 | |
3876 | |
3877 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
3878 | llvm::Value *Val = Block; |
3879 | RValue Result; |
3880 | Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
3881 | Ty, CopySelector, |
3882 | Val, CallArgList(), nullptr, nullptr); |
3883 | Val = Result.getScalarVal(); |
3884 | Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
3885 | Ty, AutoreleaseSelector, |
3886 | Val, CallArgList(), nullptr, nullptr); |
3887 | Val = Result.getScalarVal(); |
3888 | return Val; |
3889 | } |
3890 | |
3891 | static unsigned getBaseMachOPlatformID(const llvm::Triple &TT) { |
3892 | switch (TT.getOS()) { |
3893 | case llvm::Triple::Darwin: |
3894 | case llvm::Triple::MacOSX: |
3895 | return llvm::MachO::PLATFORM_MACOS; |
3896 | case llvm::Triple::IOS: |
3897 | return llvm::MachO::PLATFORM_IOS; |
3898 | case llvm::Triple::TvOS: |
3899 | return llvm::MachO::PLATFORM_TVOS; |
3900 | case llvm::Triple::WatchOS: |
3901 | return llvm::MachO::PLATFORM_WATCHOS; |
3902 | default: |
3903 | return 0; |
3904 | } |
3905 | } |
3906 | |
3907 | static llvm::Value *emitIsPlatformVersionAtLeast(CodeGenFunction &CGF, |
3908 | const VersionTuple &Version) { |
3909 | CodeGenModule &CGM = CGF.CGM; |
3910 | |
3911 | |
3912 | |
3913 | llvm::SmallVector<llvm::Value *, 8> Args; |
3914 | |
3915 | auto EmitArgs = [&](const VersionTuple &Version, const llvm::Triple &TT) { |
3916 | Optional<unsigned> Min = Version.getMinor(), SMin = Version.getSubminor(); |
3917 | Args.push_back( |
3918 | llvm::ConstantInt::get(CGM.Int32Ty, getBaseMachOPlatformID(TT))); |
3919 | Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, Version.getMajor())); |
3920 | Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, Min ? *Min : 0)); |
3921 | Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, SMin ? *SMin : 0)); |
3922 | }; |
3923 | |
3924 | assert(!Version.empty() && "unexpected empty version"); |
3925 | EmitArgs(Version, CGM.getTarget().getTriple()); |
3926 | |
3927 | if (!CGM.IsPlatformVersionAtLeastFn) { |
3928 | llvm::FunctionType *FTy = llvm::FunctionType::get( |
3929 | CGM.Int32Ty, {CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty}, |
3930 | false); |
3931 | CGM.IsPlatformVersionAtLeastFn = |
3932 | CGM.CreateRuntimeFunction(FTy, "__isPlatformVersionAtLeast"); |
3933 | } |
3934 | |
3935 | llvm::Value *Check = |
3936 | CGF.EmitNounwindRuntimeCall(CGM.IsPlatformVersionAtLeastFn, Args); |
3937 | return CGF.Builder.CreateICmpNE(Check, |
3938 | llvm::Constant::getNullValue(CGM.Int32Ty)); |
3939 | } |
3940 | |
3941 | llvm::Value * |
3942 | CodeGenFunction::EmitBuiltinAvailable(const VersionTuple &Version) { |
3943 | |
3944 | if (CGM.getTarget().getTriple().isOSDarwin()) |
3945 | return emitIsPlatformVersionAtLeast(*this, Version); |
3946 | |
3947 | if (!CGM.IsOSVersionAtLeastFn) { |
3948 | llvm::FunctionType *FTy = |
3949 | llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false); |
3950 | CGM.IsOSVersionAtLeastFn = |
3951 | CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast"); |
3952 | } |
3953 | |
3954 | Optional<unsigned> Min = Version.getMinor(), SMin = Version.getSubminor(); |
3955 | llvm::Value *Args[] = { |
3956 | llvm::ConstantInt::get(CGM.Int32Ty, Version.getMajor()), |
3957 | llvm::ConstantInt::get(CGM.Int32Ty, Min ? *Min : 0), |
3958 | llvm::ConstantInt::get(CGM.Int32Ty, SMin ? *SMin : 0), |
3959 | }; |
3960 | |
3961 | llvm::Value *CallRes = |
3962 | EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args); |
3963 | |
3964 | return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty)); |
3965 | } |
3966 | |
3967 | static bool isFoundationNeededForDarwinAvailabilityCheck( |
3968 | const llvm::Triple &TT, const VersionTuple &TargetVersion) { |
3969 | VersionTuple FoundationDroppedInVersion; |
3970 | switch (TT.getOS()) { |
3971 | case llvm::Triple::IOS: |
3972 | case llvm::Triple::TvOS: |
3973 | FoundationDroppedInVersion = VersionTuple(13); |
3974 | break; |
3975 | case llvm::Triple::WatchOS: |
3976 | FoundationDroppedInVersion = VersionTuple(6); |
3977 | break; |
3978 | case llvm::Triple::Darwin: |
3979 | case llvm::Triple::MacOSX: |
3980 | FoundationDroppedInVersion = VersionTuple(10, 15); |
3981 | break; |
3982 | default: |
3983 | llvm_unreachable("Unexpected OS"); |
3984 | } |
3985 | return TargetVersion < FoundationDroppedInVersion; |
3986 | } |
3987 | |
3988 | void CodeGenModule::emitAtAvailableLinkGuard() { |
3989 | if (!IsPlatformVersionAtLeastFn) |
3990 | return; |
3991 | |
3992 | if (!Target.getTriple().isOSDarwin()) |
3993 | return; |
3994 | |
3995 | |
3996 | if (!isFoundationNeededForDarwinAvailabilityCheck( |
3997 | Target.getTriple(), Target.getPlatformMinVersion())) |
3998 | return; |
3999 | |
4000 | |
4001 | |
4002 | |
4003 | auto &Context = getLLVMContext(); |
4004 | llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"), |
4005 | llvm::MDString::get(Context, "CoreFoundation")}; |
4006 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(Context, Args)); |
4007 | |
4008 | |
4009 | llvm::FunctionType *FTy = |
4010 | llvm::FunctionType::get(Int32Ty, {VoidPtrTy}, false); |
4011 | llvm::FunctionCallee CFFunc = |
4012 | CreateRuntimeFunction(FTy, "CFBundleGetVersionNumber"); |
4013 | |
4014 | llvm::FunctionType *CheckFTy = llvm::FunctionType::get(VoidTy, {}, false); |
4015 | llvm::FunctionCallee CFLinkCheckFuncRef = CreateRuntimeFunction( |
4016 | CheckFTy, "__clang_at_available_requires_core_foundation_framework", |
4017 | llvm::AttributeList(), true); |
4018 | llvm::Function *CFLinkCheckFunc = |
4019 | cast<llvm::Function>(CFLinkCheckFuncRef.getCallee()->stripPointerCasts()); |
4020 | if (CFLinkCheckFunc->empty()) { |
4021 | CFLinkCheckFunc->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); |
4022 | CFLinkCheckFunc->setVisibility(llvm::GlobalValue::HiddenVisibility); |
4023 | CodeGenFunction CGF(*this); |
4024 | CGF.Builder.SetInsertPoint(CGF.createBasicBlock("", CFLinkCheckFunc)); |
4025 | CGF.EmitNounwindRuntimeCall(CFFunc, |
4026 | llvm::Constant::getNullValue(VoidPtrTy)); |
4027 | CGF.Builder.CreateUnreachable(); |
4028 | addCompilerUsedGlobal(CFLinkCheckFunc); |
4029 | } |
4030 | } |
4031 | |
4032 | CGObjCRuntime::~CGObjCRuntime() {} |