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