xref: /aosp_15_r20/external/clang/lib/CodeGen/CGObjCRuntime.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //==- CGObjCRuntime.cpp - Interface to Shared Objective-C Runtime Features ==//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This abstract class defines the interface for Objective-C runtime-specific
11*67e74705SXin Li // code generation.  It provides some concrete helper methods for functionality
12*67e74705SXin Li // shared between all (or most) of the Objective-C runtimes supported by clang.
13*67e74705SXin Li //
14*67e74705SXin Li //===----------------------------------------------------------------------===//
15*67e74705SXin Li 
16*67e74705SXin Li #include "CGObjCRuntime.h"
17*67e74705SXin Li #include "CGCleanup.h"
18*67e74705SXin Li #include "CGRecordLayout.h"
19*67e74705SXin Li #include "CodeGenFunction.h"
20*67e74705SXin Li #include "CodeGenModule.h"
21*67e74705SXin Li #include "clang/AST/RecordLayout.h"
22*67e74705SXin Li #include "clang/AST/StmtObjC.h"
23*67e74705SXin Li #include "clang/CodeGen/CGFunctionInfo.h"
24*67e74705SXin Li #include "llvm/IR/CallSite.h"
25*67e74705SXin Li 
26*67e74705SXin Li using namespace clang;
27*67e74705SXin Li using namespace CodeGen;
28*67e74705SXin Li 
LookupFieldBitOffset(CodeGen::CodeGenModule & CGM,const ObjCInterfaceDecl * OID,const ObjCImplementationDecl * ID,const ObjCIvarDecl * Ivar)29*67e74705SXin Li static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
30*67e74705SXin Li                                      const ObjCInterfaceDecl *OID,
31*67e74705SXin Li                                      const ObjCImplementationDecl *ID,
32*67e74705SXin Li                                      const ObjCIvarDecl *Ivar) {
33*67e74705SXin Li   const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
34*67e74705SXin Li 
35*67e74705SXin Li   // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
36*67e74705SXin Li   // in here; it should never be necessary because that should be the lexical
37*67e74705SXin Li   // decl context for the ivar.
38*67e74705SXin Li 
39*67e74705SXin Li   // If we know have an implementation (and the ivar is in it) then
40*67e74705SXin Li   // look up in the implementation layout.
41*67e74705SXin Li   const ASTRecordLayout *RL;
42*67e74705SXin Li   if (ID && declaresSameEntity(ID->getClassInterface(), Container))
43*67e74705SXin Li     RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
44*67e74705SXin Li   else
45*67e74705SXin Li     RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
46*67e74705SXin Li 
47*67e74705SXin Li   // Compute field index.
48*67e74705SXin Li   //
49*67e74705SXin Li   // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
50*67e74705SXin Li   // implemented. This should be fixed to get the information from the layout
51*67e74705SXin Li   // directly.
52*67e74705SXin Li   unsigned Index = 0;
53*67e74705SXin Li 
54*67e74705SXin Li   for (const ObjCIvarDecl *IVD = Container->all_declared_ivar_begin();
55*67e74705SXin Li        IVD; IVD = IVD->getNextIvar()) {
56*67e74705SXin Li     if (Ivar == IVD)
57*67e74705SXin Li       break;
58*67e74705SXin Li     ++Index;
59*67e74705SXin Li   }
60*67e74705SXin Li   assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
61*67e74705SXin Li 
62*67e74705SXin Li   return RL->getFieldOffset(Index);
63*67e74705SXin Li }
64*67e74705SXin Li 
ComputeIvarBaseOffset(CodeGen::CodeGenModule & CGM,const ObjCInterfaceDecl * OID,const ObjCIvarDecl * Ivar)65*67e74705SXin Li uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
66*67e74705SXin Li                                               const ObjCInterfaceDecl *OID,
67*67e74705SXin Li                                               const ObjCIvarDecl *Ivar) {
68*67e74705SXin Li   return LookupFieldBitOffset(CGM, OID, nullptr, Ivar) /
69*67e74705SXin Li     CGM.getContext().getCharWidth();
70*67e74705SXin Li }
71*67e74705SXin Li 
ComputeIvarBaseOffset(CodeGen::CodeGenModule & CGM,const ObjCImplementationDecl * OID,const ObjCIvarDecl * Ivar)72*67e74705SXin Li uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
73*67e74705SXin Li                                               const ObjCImplementationDecl *OID,
74*67e74705SXin Li                                               const ObjCIvarDecl *Ivar) {
75*67e74705SXin Li   return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) /
76*67e74705SXin Li     CGM.getContext().getCharWidth();
77*67e74705SXin Li }
78*67e74705SXin Li 
ComputeBitfieldBitOffset(CodeGen::CodeGenModule & CGM,const ObjCInterfaceDecl * ID,const ObjCIvarDecl * Ivar)79*67e74705SXin Li unsigned CGObjCRuntime::ComputeBitfieldBitOffset(
80*67e74705SXin Li     CodeGen::CodeGenModule &CGM,
81*67e74705SXin Li     const ObjCInterfaceDecl *ID,
82*67e74705SXin Li     const ObjCIvarDecl *Ivar) {
83*67e74705SXin Li   return LookupFieldBitOffset(CGM, ID, ID->getImplementation(), Ivar);
84*67e74705SXin Li }
85*67e74705SXin Li 
EmitValueForIvarAtOffset(CodeGen::CodeGenFunction & CGF,const ObjCInterfaceDecl * OID,llvm::Value * BaseValue,const ObjCIvarDecl * Ivar,unsigned CVRQualifiers,llvm::Value * Offset)86*67e74705SXin Li LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
87*67e74705SXin Li                                                const ObjCInterfaceDecl *OID,
88*67e74705SXin Li                                                llvm::Value *BaseValue,
89*67e74705SXin Li                                                const ObjCIvarDecl *Ivar,
90*67e74705SXin Li                                                unsigned CVRQualifiers,
91*67e74705SXin Li                                                llvm::Value *Offset) {
92*67e74705SXin Li   // Compute (type*) ( (char *) BaseValue + Offset)
93*67e74705SXin Li   QualType IvarTy = Ivar->getType();
94*67e74705SXin Li   llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
95*67e74705SXin Li   llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, CGF.Int8PtrTy);
96*67e74705SXin Li   V = CGF.Builder.CreateInBoundsGEP(V, Offset, "add.ptr");
97*67e74705SXin Li 
98*67e74705SXin Li   if (!Ivar->isBitField()) {
99*67e74705SXin Li     V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
100*67e74705SXin Li     LValue LV = CGF.MakeNaturalAlignAddrLValue(V, IvarTy);
101*67e74705SXin Li     LV.getQuals().addCVRQualifiers(CVRQualifiers);
102*67e74705SXin Li     return LV;
103*67e74705SXin Li   }
104*67e74705SXin Li 
105*67e74705SXin Li   // We need to compute an access strategy for this bit-field. We are given the
106*67e74705SXin Li   // offset to the first byte in the bit-field, the sub-byte offset is taken
107*67e74705SXin Li   // from the original layout. We reuse the normal bit-field access strategy by
108*67e74705SXin Li   // treating this as an access to a struct where the bit-field is in byte 0,
109*67e74705SXin Li   // and adjust the containing type size as appropriate.
110*67e74705SXin Li   //
111*67e74705SXin Li   // FIXME: Note that currently we make a very conservative estimate of the
112*67e74705SXin Li   // alignment of the bit-field, because (a) it is not clear what guarantees the
113*67e74705SXin Li   // runtime makes us, and (b) we don't have a way to specify that the struct is
114*67e74705SXin Li   // at an alignment plus offset.
115*67e74705SXin Li   //
116*67e74705SXin Li   // Note, there is a subtle invariant here: we can only call this routine on
117*67e74705SXin Li   // non-synthesized ivars but we may be called for synthesized ivars.  However,
118*67e74705SXin Li   // a synthesized ivar can never be a bit-field, so this is safe.
119*67e74705SXin Li   uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, nullptr, Ivar);
120*67e74705SXin Li   uint64_t BitOffset = FieldBitOffset % CGF.CGM.getContext().getCharWidth();
121*67e74705SXin Li   uint64_t AlignmentBits = CGF.CGM.getTarget().getCharAlign();
122*67e74705SXin Li   uint64_t BitFieldSize = Ivar->getBitWidthValue(CGF.getContext());
123*67e74705SXin Li   CharUnits StorageSize = CGF.CGM.getContext().toCharUnitsFromBits(
124*67e74705SXin Li       llvm::alignTo(BitOffset + BitFieldSize, AlignmentBits));
125*67e74705SXin Li   CharUnits Alignment = CGF.CGM.getContext().toCharUnitsFromBits(AlignmentBits);
126*67e74705SXin Li 
127*67e74705SXin Li   // Allocate a new CGBitFieldInfo object to describe this access.
128*67e74705SXin Li   //
129*67e74705SXin Li   // FIXME: This is incredibly wasteful, these should be uniqued or part of some
130*67e74705SXin Li   // layout object. However, this is blocked on other cleanups to the
131*67e74705SXin Li   // Objective-C code, so for now we just live with allocating a bunch of these
132*67e74705SXin Li   // objects.
133*67e74705SXin Li   CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
134*67e74705SXin Li     CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
135*67e74705SXin Li                              CGF.CGM.getContext().toBits(StorageSize),
136*67e74705SXin Li                              CharUnits::fromQuantity(0)));
137*67e74705SXin Li 
138*67e74705SXin Li   Address Addr(V, Alignment);
139*67e74705SXin Li   Addr = CGF.Builder.CreateElementBitCast(Addr,
140*67e74705SXin Li                                    llvm::Type::getIntNTy(CGF.getLLVMContext(),
141*67e74705SXin Li                                                          Info->StorageSize));
142*67e74705SXin Li   return LValue::MakeBitfield(Addr, *Info,
143*67e74705SXin Li                               IvarTy.withCVRQualifiers(CVRQualifiers),
144*67e74705SXin Li                               AlignmentSource::Decl);
145*67e74705SXin Li }
146*67e74705SXin Li 
147*67e74705SXin Li namespace {
148*67e74705SXin Li   struct CatchHandler {
149*67e74705SXin Li     const VarDecl *Variable;
150*67e74705SXin Li     const Stmt *Body;
151*67e74705SXin Li     llvm::BasicBlock *Block;
152*67e74705SXin Li     llvm::Constant *TypeInfo;
153*67e74705SXin Li   };
154*67e74705SXin Li 
155*67e74705SXin Li   struct CallObjCEndCatch final : EHScopeStack::Cleanup {
CallObjCEndCatch__anona71e1ea20111::CallObjCEndCatch156*67e74705SXin Li     CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
157*67e74705SXin Li       MightThrow(MightThrow), Fn(Fn) {}
158*67e74705SXin Li     bool MightThrow;
159*67e74705SXin Li     llvm::Value *Fn;
160*67e74705SXin Li 
Emit__anona71e1ea20111::CallObjCEndCatch161*67e74705SXin Li     void Emit(CodeGenFunction &CGF, Flags flags) override {
162*67e74705SXin Li       if (!MightThrow) {
163*67e74705SXin Li         CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
164*67e74705SXin Li         return;
165*67e74705SXin Li       }
166*67e74705SXin Li 
167*67e74705SXin Li       CGF.EmitRuntimeCallOrInvoke(Fn);
168*67e74705SXin Li     }
169*67e74705SXin Li   };
170*67e74705SXin Li }
171*67e74705SXin Li 
172*67e74705SXin Li 
EmitTryCatchStmt(CodeGenFunction & CGF,const ObjCAtTryStmt & S,llvm::Constant * beginCatchFn,llvm::Constant * endCatchFn,llvm::Constant * exceptionRethrowFn)173*67e74705SXin Li void CGObjCRuntime::EmitTryCatchStmt(CodeGenFunction &CGF,
174*67e74705SXin Li                                      const ObjCAtTryStmt &S,
175*67e74705SXin Li                                      llvm::Constant *beginCatchFn,
176*67e74705SXin Li                                      llvm::Constant *endCatchFn,
177*67e74705SXin Li                                      llvm::Constant *exceptionRethrowFn) {
178*67e74705SXin Li   // Jump destination for falling out of catch bodies.
179*67e74705SXin Li   CodeGenFunction::JumpDest Cont;
180*67e74705SXin Li   if (S.getNumCatchStmts())
181*67e74705SXin Li     Cont = CGF.getJumpDestInCurrentScope("eh.cont");
182*67e74705SXin Li 
183*67e74705SXin Li   CodeGenFunction::FinallyInfo FinallyInfo;
184*67e74705SXin Li   if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
185*67e74705SXin Li     FinallyInfo.enter(CGF, Finally->getFinallyBody(),
186*67e74705SXin Li                       beginCatchFn, endCatchFn, exceptionRethrowFn);
187*67e74705SXin Li 
188*67e74705SXin Li   SmallVector<CatchHandler, 8> Handlers;
189*67e74705SXin Li 
190*67e74705SXin Li   // Enter the catch, if there is one.
191*67e74705SXin Li   if (S.getNumCatchStmts()) {
192*67e74705SXin Li     for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
193*67e74705SXin Li       const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
194*67e74705SXin Li       const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
195*67e74705SXin Li 
196*67e74705SXin Li       Handlers.push_back(CatchHandler());
197*67e74705SXin Li       CatchHandler &Handler = Handlers.back();
198*67e74705SXin Li       Handler.Variable = CatchDecl;
199*67e74705SXin Li       Handler.Body = CatchStmt->getCatchBody();
200*67e74705SXin Li       Handler.Block = CGF.createBasicBlock("catch");
201*67e74705SXin Li 
202*67e74705SXin Li       // @catch(...) always matches.
203*67e74705SXin Li       if (!CatchDecl) {
204*67e74705SXin Li         Handler.TypeInfo = nullptr; // catch-all
205*67e74705SXin Li         // Don't consider any other catches.
206*67e74705SXin Li         break;
207*67e74705SXin Li       }
208*67e74705SXin Li 
209*67e74705SXin Li       Handler.TypeInfo = GetEHType(CatchDecl->getType());
210*67e74705SXin Li     }
211*67e74705SXin Li 
212*67e74705SXin Li     EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
213*67e74705SXin Li     for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
214*67e74705SXin Li       Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
215*67e74705SXin Li   }
216*67e74705SXin Li 
217*67e74705SXin Li   // Emit the try body.
218*67e74705SXin Li   CGF.EmitStmt(S.getTryBody());
219*67e74705SXin Li 
220*67e74705SXin Li   // Leave the try.
221*67e74705SXin Li   if (S.getNumCatchStmts())
222*67e74705SXin Li     CGF.popCatchScope();
223*67e74705SXin Li 
224*67e74705SXin Li   // Remember where we were.
225*67e74705SXin Li   CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
226*67e74705SXin Li 
227*67e74705SXin Li   // Emit the handlers.
228*67e74705SXin Li   for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
229*67e74705SXin Li     CatchHandler &Handler = Handlers[I];
230*67e74705SXin Li 
231*67e74705SXin Li     CGF.EmitBlock(Handler.Block);
232*67e74705SXin Li     llvm::Value *RawExn = CGF.getExceptionFromSlot();
233*67e74705SXin Li 
234*67e74705SXin Li     // Enter the catch.
235*67e74705SXin Li     llvm::Value *Exn = RawExn;
236*67e74705SXin Li     if (beginCatchFn) {
237*67e74705SXin Li       Exn = CGF.Builder.CreateCall(beginCatchFn, RawExn, "exn.adjusted");
238*67e74705SXin Li       cast<llvm::CallInst>(Exn)->setDoesNotThrow();
239*67e74705SXin Li     }
240*67e74705SXin Li 
241*67e74705SXin Li     CodeGenFunction::LexicalScope cleanups(CGF, Handler.Body->getSourceRange());
242*67e74705SXin Li 
243*67e74705SXin Li     if (endCatchFn) {
244*67e74705SXin Li       // Add a cleanup to leave the catch.
245*67e74705SXin Li       bool EndCatchMightThrow = (Handler.Variable == nullptr);
246*67e74705SXin Li 
247*67e74705SXin Li       CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
248*67e74705SXin Li                                                 EndCatchMightThrow,
249*67e74705SXin Li                                                 endCatchFn);
250*67e74705SXin Li     }
251*67e74705SXin Li 
252*67e74705SXin Li     // Bind the catch parameter if it exists.
253*67e74705SXin Li     if (const VarDecl *CatchParam = Handler.Variable) {
254*67e74705SXin Li       llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
255*67e74705SXin Li       llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
256*67e74705SXin Li 
257*67e74705SXin Li       CGF.EmitAutoVarDecl(*CatchParam);
258*67e74705SXin Li       EmitInitOfCatchParam(CGF, CastExn, CatchParam);
259*67e74705SXin Li     }
260*67e74705SXin Li 
261*67e74705SXin Li     CGF.ObjCEHValueStack.push_back(Exn);
262*67e74705SXin Li     CGF.EmitStmt(Handler.Body);
263*67e74705SXin Li     CGF.ObjCEHValueStack.pop_back();
264*67e74705SXin Li 
265*67e74705SXin Li     // Leave any cleanups associated with the catch.
266*67e74705SXin Li     cleanups.ForceCleanup();
267*67e74705SXin Li 
268*67e74705SXin Li     CGF.EmitBranchThroughCleanup(Cont);
269*67e74705SXin Li   }
270*67e74705SXin Li 
271*67e74705SXin Li   // Go back to the try-statement fallthrough.
272*67e74705SXin Li   CGF.Builder.restoreIP(SavedIP);
273*67e74705SXin Li 
274*67e74705SXin Li   // Pop out of the finally.
275*67e74705SXin Li   if (S.getFinallyStmt())
276*67e74705SXin Li     FinallyInfo.exit(CGF);
277*67e74705SXin Li 
278*67e74705SXin Li   if (Cont.isValid())
279*67e74705SXin Li     CGF.EmitBlock(Cont.getBlock());
280*67e74705SXin Li }
281*67e74705SXin Li 
EmitInitOfCatchParam(CodeGenFunction & CGF,llvm::Value * exn,const VarDecl * paramDecl)282*67e74705SXin Li void CGObjCRuntime::EmitInitOfCatchParam(CodeGenFunction &CGF,
283*67e74705SXin Li                                          llvm::Value *exn,
284*67e74705SXin Li                                          const VarDecl *paramDecl) {
285*67e74705SXin Li 
286*67e74705SXin Li   Address paramAddr = CGF.GetAddrOfLocalVar(paramDecl);
287*67e74705SXin Li 
288*67e74705SXin Li   switch (paramDecl->getType().getQualifiers().getObjCLifetime()) {
289*67e74705SXin Li   case Qualifiers::OCL_Strong:
290*67e74705SXin Li     exn = CGF.EmitARCRetainNonBlock(exn);
291*67e74705SXin Li     // fallthrough
292*67e74705SXin Li 
293*67e74705SXin Li   case Qualifiers::OCL_None:
294*67e74705SXin Li   case Qualifiers::OCL_ExplicitNone:
295*67e74705SXin Li   case Qualifiers::OCL_Autoreleasing:
296*67e74705SXin Li     CGF.Builder.CreateStore(exn, paramAddr);
297*67e74705SXin Li     return;
298*67e74705SXin Li 
299*67e74705SXin Li   case Qualifiers::OCL_Weak:
300*67e74705SXin Li     CGF.EmitARCInitWeak(paramAddr, exn);
301*67e74705SXin Li     return;
302*67e74705SXin Li   }
303*67e74705SXin Li   llvm_unreachable("invalid ownership qualifier");
304*67e74705SXin Li }
305*67e74705SXin Li 
306*67e74705SXin Li namespace {
307*67e74705SXin Li   struct CallSyncExit final : EHScopeStack::Cleanup {
308*67e74705SXin Li     llvm::Value *SyncExitFn;
309*67e74705SXin Li     llvm::Value *SyncArg;
CallSyncExit__anona71e1ea20211::CallSyncExit310*67e74705SXin Li     CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
311*67e74705SXin Li       : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
312*67e74705SXin Li 
Emit__anona71e1ea20211::CallSyncExit313*67e74705SXin Li     void Emit(CodeGenFunction &CGF, Flags flags) override {
314*67e74705SXin Li       CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
315*67e74705SXin Li     }
316*67e74705SXin Li   };
317*67e74705SXin Li }
318*67e74705SXin Li 
EmitAtSynchronizedStmt(CodeGenFunction & CGF,const ObjCAtSynchronizedStmt & S,llvm::Function * syncEnterFn,llvm::Function * syncExitFn)319*67e74705SXin Li void CGObjCRuntime::EmitAtSynchronizedStmt(CodeGenFunction &CGF,
320*67e74705SXin Li                                            const ObjCAtSynchronizedStmt &S,
321*67e74705SXin Li                                            llvm::Function *syncEnterFn,
322*67e74705SXin Li                                            llvm::Function *syncExitFn) {
323*67e74705SXin Li   CodeGenFunction::RunCleanupsScope cleanups(CGF);
324*67e74705SXin Li 
325*67e74705SXin Li   // Evaluate the lock operand.  This is guaranteed to dominate the
326*67e74705SXin Li   // ARC release and lock-release cleanups.
327*67e74705SXin Li   const Expr *lockExpr = S.getSynchExpr();
328*67e74705SXin Li   llvm::Value *lock;
329*67e74705SXin Li   if (CGF.getLangOpts().ObjCAutoRefCount) {
330*67e74705SXin Li     lock = CGF.EmitARCRetainScalarExpr(lockExpr);
331*67e74705SXin Li     lock = CGF.EmitObjCConsumeObject(lockExpr->getType(), lock);
332*67e74705SXin Li   } else {
333*67e74705SXin Li     lock = CGF.EmitScalarExpr(lockExpr);
334*67e74705SXin Li   }
335*67e74705SXin Li   lock = CGF.Builder.CreateBitCast(lock, CGF.VoidPtrTy);
336*67e74705SXin Li 
337*67e74705SXin Li   // Acquire the lock.
338*67e74705SXin Li   CGF.Builder.CreateCall(syncEnterFn, lock)->setDoesNotThrow();
339*67e74705SXin Li 
340*67e74705SXin Li   // Register an all-paths cleanup to release the lock.
341*67e74705SXin Li   CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup, syncExitFn, lock);
342*67e74705SXin Li 
343*67e74705SXin Li   // Emit the body of the statement.
344*67e74705SXin Li   CGF.EmitStmt(S.getSynchBody());
345*67e74705SXin Li }
346*67e74705SXin Li 
347*67e74705SXin Li /// Compute the pointer-to-function type to which a message send
348*67e74705SXin Li /// should be casted in order to correctly call the given method
349*67e74705SXin Li /// with the given arguments.
350*67e74705SXin Li ///
351*67e74705SXin Li /// \param method - may be null
352*67e74705SXin Li /// \param resultType - the result type to use if there's no method
353*67e74705SXin Li /// \param callArgs - the actual arguments, including implicit ones
354*67e74705SXin Li CGObjCRuntime::MessageSendInfo
getMessageSendInfo(const ObjCMethodDecl * method,QualType resultType,CallArgList & callArgs)355*67e74705SXin Li CGObjCRuntime::getMessageSendInfo(const ObjCMethodDecl *method,
356*67e74705SXin Li                                   QualType resultType,
357*67e74705SXin Li                                   CallArgList &callArgs) {
358*67e74705SXin Li   // If there's a method, use information from that.
359*67e74705SXin Li   if (method) {
360*67e74705SXin Li     const CGFunctionInfo &signature =
361*67e74705SXin Li       CGM.getTypes().arrangeObjCMessageSendSignature(method, callArgs[0].Ty);
362*67e74705SXin Li 
363*67e74705SXin Li     llvm::PointerType *signatureType =
364*67e74705SXin Li       CGM.getTypes().GetFunctionType(signature)->getPointerTo();
365*67e74705SXin Li 
366*67e74705SXin Li     const CGFunctionInfo &signatureForCall =
367*67e74705SXin Li       CGM.getTypes().arrangeCall(signature, callArgs);
368*67e74705SXin Li 
369*67e74705SXin Li     return MessageSendInfo(signatureForCall, signatureType);
370*67e74705SXin Li   }
371*67e74705SXin Li 
372*67e74705SXin Li   // There's no method;  just use a default CC.
373*67e74705SXin Li   const CGFunctionInfo &argsInfo =
374*67e74705SXin Li     CGM.getTypes().arrangeUnprototypedObjCMessageSend(resultType, callArgs);
375*67e74705SXin Li 
376*67e74705SXin Li   // Derive the signature to call from that.
377*67e74705SXin Li   llvm::PointerType *signatureType =
378*67e74705SXin Li     CGM.getTypes().GetFunctionType(argsInfo)->getPointerTo();
379*67e74705SXin Li   return MessageSendInfo(argsInfo, signatureType);
380*67e74705SXin Li }
381