blob: 78305c6ddc69dc93983c8510a2366d27b4ee6903 [file] [log] [blame]
Anders Carlsson76f4a902007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlsson76f4a902007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Devang Pateld2d66652011-01-19 01:36:36 +000014#include "CGDebugInfo.h"
Ted Kremenek43e06332008-04-09 15:51:31 +000015#include "CGObjCRuntime.h"
Anders Carlsson76f4a902007-08-21 17:43:55 +000016#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000020#include "clang/AST/StmtObjC.h"
Daniel Dunbarc5d33042008-09-03 00:27:26 +000021#include "clang/Basic/Diagnostic.h"
Anders Carlsson2e744e82008-08-30 19:51:14 +000022#include "llvm/ADT/STLExtras.h"
Daniel Dunbara08dff12008-09-24 04:04:31 +000023#include "llvm/Target/TargetData.h"
Anders Carlsson76f4a902007-08-21 17:43:55 +000024using namespace clang;
25using namespace CodeGen;
26
Chris Lattnerb1d329d2008-06-24 17:04:18 +000027/// Emits an instance of NSConstantString representing the object.
Mike Stump11289f42009-09-09 15:08:12 +000028llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
Daniel Dunbar44b58a22008-11-25 21:53:21 +000029{
David Chisnall481e3a82010-01-23 02:40:42 +000030 llvm::Constant *C =
31 CGM.getObjCRuntime().GenerateConstantString(E->getString());
Daniel Dunbar66912a12008-08-20 00:28:19 +000032 // FIXME: This bitcast should just be made an invariant on the Runtime.
Owen Andersonade90fd2009-07-29 18:54:39 +000033 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattnerb1d329d2008-06-24 17:04:18 +000034}
35
36/// Emit a selector.
37llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
38 // Untyped selector.
39 // Note that this implementation allows for non-constant strings to be passed
40 // as arguments to @selector(). Currently, the only thing preventing this
41 // behaviour is the type checking in the front end.
Daniel Dunbar45858d22010-02-03 20:11:42 +000042 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattnerb1d329d2008-06-24 17:04:18 +000043}
44
Daniel Dunbar66912a12008-08-20 00:28:19 +000045llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
46 // FIXME: This should pass the Decl not the name.
47 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
48}
Chris Lattnerb1d329d2008-06-24 17:04:18 +000049
50
John McCall78a15112010-05-22 01:48:05 +000051RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
52 ReturnValueSlot Return) {
Chris Lattnerb1d329d2008-06-24 17:04:18 +000053 // Only the lookup mechanism and first two arguments of the method
54 // implementation vary between runtimes. We can get the receiver and
55 // arguments in generic code.
Mike Stump11289f42009-09-09 15:08:12 +000056
Daniel Dunbar8d480592008-08-11 18:12:00 +000057 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattnerb1d329d2008-06-24 17:04:18 +000058 bool isSuperMessage = false;
Daniel Dunbarca8531a2008-08-25 08:19:24 +000059 bool isClassMessage = false;
David Chisnall01aa4672010-04-28 19:33:36 +000060 ObjCInterfaceDecl *OID = 0;
Chris Lattnerb1d329d2008-06-24 17:04:18 +000061 // Find the receiver
Daniel Dunbarb2197802010-04-22 03:17:06 +000062 llvm::Value *Receiver = 0;
Douglas Gregor9a129192010-04-21 00:45:42 +000063 switch (E->getReceiverKind()) {
64 case ObjCMessageExpr::Instance:
65 Receiver = EmitScalarExpr(E->getInstanceReceiver());
66 break;
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +000067
Douglas Gregor9a129192010-04-21 00:45:42 +000068 case ObjCMessageExpr::Class: {
John McCall3e294922010-05-17 20:12:43 +000069 const ObjCObjectType *ObjTy
70 = E->getClassReceiver()->getAs<ObjCObjectType>();
71 assert(ObjTy && "Invalid Objective-C class message send");
72 OID = ObjTy->getInterface();
73 assert(OID && "Invalid Objective-C class message send");
David Chisnall01aa4672010-04-28 19:33:36 +000074 Receiver = Runtime.GetClass(Builder, OID);
Daniel Dunbarca8531a2008-08-25 08:19:24 +000075 isClassMessage = true;
Douglas Gregor9a129192010-04-21 00:45:42 +000076 break;
77 }
78
79 case ObjCMessageExpr::SuperInstance:
Chris Lattnerb1d329d2008-06-24 17:04:18 +000080 Receiver = LoadObjCSelf();
Douglas Gregor9a129192010-04-21 00:45:42 +000081 isSuperMessage = true;
82 break;
83
84 case ObjCMessageExpr::SuperClass:
85 Receiver = LoadObjCSelf();
86 isSuperMessage = true;
87 isClassMessage = true;
88 break;
Chris Lattnerb1d329d2008-06-24 17:04:18 +000089 }
90
Daniel Dunbarc722b852008-08-30 03:02:31 +000091 CallArgList Args;
Anders Carlsson623dcae2009-04-18 20:29:27 +000092 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump11289f42009-09-09 15:08:12 +000093
Anders Carlsson280e61f12010-06-21 20:59:55 +000094 QualType ResultType =
95 E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType();
96
Chris Lattnerb1d329d2008-06-24 17:04:18 +000097 if (isSuperMessage) {
Chris Lattner6cfec782008-06-26 04:42:20 +000098 // super is only valid in an Objective-C method
99 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000100 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Anders Carlsson280e61f12010-06-21 20:59:55 +0000101 return Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000102 E->getSelector(),
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000103 OMD->getClassInterface(),
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000104 isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000105 Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000106 isClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000107 Args,
108 E->getMethodDecl());
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000109 }
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000110
Anders Carlsson280e61f12010-06-21 20:59:55 +0000111 return Runtime.GenerateMessageSend(*this, Return, ResultType,
John McCall78a15112010-05-22 01:48:05 +0000112 E->getSelector(),
David Chisnall01aa4672010-04-28 19:33:36 +0000113 Receiver, Args, OID,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000114 E->getMethodDecl());
Anders Carlsson76f4a902007-08-21 17:43:55 +0000115}
116
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000117/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
118/// the LLVM function and sets the other context used by
119/// CodeGenFunction.
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000120void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
121 const ObjCContainerDecl *CD) {
John McCalla738c252011-03-09 04:27:21 +0000122 FunctionArgList args;
Devang Patela2c048e2010-04-05 21:09:15 +0000123 // Check if we should generate debug info for this method.
Devang Pateld6ffebb2011-03-07 18:45:56 +0000124 if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
125 DebugInfo = CGM.getModuleDebugInfo();
Devang Patela2c048e2010-04-05 21:09:15 +0000126
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000127 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000128
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000129 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
130 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner5696e7b2008-06-17 18:05:57 +0000131
John McCalla738c252011-03-09 04:27:21 +0000132 args.push_back(OMD->getSelfDecl());
133 args.push_back(OMD->getCmdDecl());
Chris Lattner5696e7b2008-06-17 18:05:57 +0000134
Chris Lattnera4997152009-02-20 18:43:26 +0000135 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
136 E = OMD->param_end(); PI != E; ++PI)
John McCalla738c252011-03-09 04:27:21 +0000137 args.push_back(*PI);
Chris Lattner5696e7b2008-06-17 18:05:57 +0000138
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000139 CurGD = OMD;
140
John McCalla738c252011-03-09 04:27:21 +0000141 StartFunction(OMD, OMD->getResultType(), Fn, FI, args, OMD->getLocStart());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000142}
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000143
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000144void CodeGenFunction::GenerateObjCGetterBody(ObjCIvarDecl *Ivar,
145 bool IsAtomic, bool IsStrong) {
146 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
147 Ivar, 0);
148 llvm::Value *GetCopyStructFn =
149 CGM.getObjCRuntime().GetGetStructFunction();
150 CodeGenTypes &Types = CGM.getTypes();
151 // objc_copyStruct (ReturnValue, &structIvar,
152 // sizeof (Type of Ivar), isAtomic, false);
153 CallArgList Args;
154 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
155 Types.ConvertType(getContext().VoidPtrTy)));
156 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
157 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
158 Types.ConvertType(getContext().VoidPtrTy)));
159 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
160 // sizeof (Type of Ivar)
161 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
162 llvm::Value *SizeVal =
163 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
164 Size.getQuantity());
165 Args.push_back(std::make_pair(RValue::get(SizeVal),
166 getContext().LongTy));
167 llvm::Value *isAtomic =
168 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
169 IsAtomic ? 1 : 0);
170 Args.push_back(std::make_pair(RValue::get(isAtomic),
171 getContext().BoolTy));
172 llvm::Value *hasStrong =
173 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
174 IsStrong ? 1 : 0);
175 Args.push_back(std::make_pair(RValue::get(hasStrong),
176 getContext().BoolTy));
177 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
178 FunctionType::ExtInfo()),
179 GetCopyStructFn, ReturnValueSlot(), Args);
180}
181
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000182/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump11289f42009-09-09 15:08:12 +0000183/// its pointer, name, and types registered in the class struture.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000184void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000185 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000186 EmitStmt(OMD->getBody());
187 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000188}
189
Mike Stump18bb9282009-05-16 07:57:57 +0000190// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
191// AST for the whole body we can just fall back to having a GenerateFunction
192// which takes the body Stmt.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000193
194/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000195/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
196/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000197void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
198 const ObjCPropertyImplDecl *PID) {
Daniel Dunbara08dff12008-09-24 04:04:31 +0000199 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000200 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000201 bool IsAtomic =
202 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000203 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
204 assert(OMD && "Invalid call to generate getter (empty method)");
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000205 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000206
Daniel Dunbara08dff12008-09-24 04:04:31 +0000207 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000208 // this. Non-atomic properties are directly evaluated.
209 // atomic 'copy' and 'retain' properties are also directly
210 // evaluated in gc-only mode.
Daniel Dunbara08dff12008-09-24 04:04:31 +0000211 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000212 IsAtomic &&
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000213 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
214 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000215 llvm::Value *GetPropertyFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000216 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000217
Daniel Dunbara08dff12008-09-24 04:04:31 +0000218 if (!GetPropertyFn) {
219 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
220 FinishFunction();
221 return;
222 }
223
224 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
225 // FIXME: Can't this be simpler? This might even be worse than the
226 // corresponding gcc code.
227 CodeGenTypes &Types = CGM.getTypes();
228 ValueDecl *Cmd = OMD->getCmdDecl();
229 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
230 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000231 llvm::Value *SelfAsId =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000232 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000233 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000234 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000235 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000236 CallArgList Args;
237 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
238 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
David Chisnall08a45252011-03-22 20:03:13 +0000239 Args.push_back(std::make_pair(RValue::get(Offset),
240 getContext().getPointerDiffType()));
Daniel Dunbara08dff12008-09-24 04:04:31 +0000241 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbar1ef73732009-02-03 23:43:59 +0000242 // FIXME: We shouldn't need to get the function info here, the
243 // runtime already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000244 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000245 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000246 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000247 // We need to fix the type here. Ivars with copy & retain are
248 // always objects so we don't need to worry about complex or
249 // aggregates.
Mike Stump11289f42009-09-09 15:08:12 +0000250 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbara08dff12008-09-24 04:04:31 +0000251 Types.ConvertType(PD->getType())));
252 EmitReturnOfRValue(RV, PD->getType());
253 } else {
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000254 const llvm::Triple &Triple = getContext().Target.getTriple();
255 QualType IVART = Ivar->getType();
256 if (IsAtomic &&
257 IVART->isScalarType() &&
258 (Triple.getArch() == llvm::Triple::arm ||
259 Triple.getArch() == llvm::Triple::thumb) &&
260 (getContext().getTypeSizeInChars(IVART)
261 > CharUnits::fromQuantity(4)) &&
262 CGM.getObjCRuntime().GetGetStructFunction()) {
263 GenerateObjCGetterBody(Ivar, true, false);
264 }
Fariborz Jahanian0f4c7112011-04-05 21:41:23 +0000265 else if (IsAtomic &&
266 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
267 Triple.getArch() == llvm::Triple::x86 &&
268 (getContext().getTypeSizeInChars(IVART)
269 > CharUnits::fromQuantity(4)) &&
270 CGM.getObjCRuntime().GetGetStructFunction()) {
271 GenerateObjCGetterBody(Ivar, true, false);
272 }
273 else if (IsAtomic &&
274 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
275 Triple.getArch() == llvm::Triple::x86_64 &&
276 (getContext().getTypeSizeInChars(IVART)
277 > CharUnits::fromQuantity(8)) &&
278 CGM.getObjCRuntime().GetGetStructFunction()) {
279 GenerateObjCGetterBody(Ivar, true, false);
280 }
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000281 else if (IVART->isAnyComplexType()) {
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000282 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
283 Ivar, 0);
Fariborz Jahanianf9c45852010-03-25 21:56:43 +0000284 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
285 LV.isVolatileQualified());
286 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
287 }
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000288 else if (hasAggregateLLVMType(IVART)) {
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000289 bool IsStrong = false;
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000290 if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(IVART)))
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000291 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
David Chisnall168b80f2010-12-26 22:13:16 +0000292 && CGM.getObjCRuntime().GetGetStructFunction()) {
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000293 GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000294 }
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000295 else {
296 if (PID->getGetterCXXConstructor()) {
297 ReturnStmt *Stmt =
298 new (getContext()) ReturnStmt(SourceLocation(),
Douglas Gregor6fd1b182010-05-15 06:01:05 +0000299 PID->getGetterCXXConstructor(),
300 0);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000301 EmitReturnStmt(*Stmt);
Fariborz Jahanian0f4c7112011-04-05 21:41:23 +0000302 } else if (IsAtomic &&
303 !IVART->isAnyComplexType() &&
304 Triple.getArch() == llvm::Triple::x86 &&
305 (getContext().getTypeSizeInChars(IVART)
306 > CharUnits::fromQuantity(4)) &&
307 CGM.getObjCRuntime().GetGetStructFunction()) {
308 GenerateObjCGetterBody(Ivar, true, false);
309 }
310 else if (IsAtomic &&
311 !IVART->isAnyComplexType() &&
312 Triple.getArch() == llvm::Triple::x86_64 &&
313 (getContext().getTypeSizeInChars(IVART)
314 > CharUnits::fromQuantity(8)) &&
315 CGM.getObjCRuntime().GetGetStructFunction()) {
316 GenerateObjCGetterBody(Ivar, true, false);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000317 }
318 else {
319 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
320 Ivar, 0);
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000321 EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000322 }
323 }
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000324 }
325 else {
326 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000327 Ivar, 0);
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000328 if (PD->getType()->isReferenceType()) {
329 RValue RV = RValue::get(LV.getAddress());
330 EmitReturnOfRValue(RV, PD->getType());
331 }
332 else {
333 CodeGenTypes &Types = CGM.getTypes();
334 RValue RV = EmitLoadOfLValue(LV, IVART);
335 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000336 Types.ConvertType(PD->getType())));
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000337 EmitReturnOfRValue(RV, PD->getType());
338 }
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000339 }
Daniel Dunbara08dff12008-09-24 04:04:31 +0000340 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000341
342 FinishFunction();
343}
344
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000345void CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
346 ObjCIvarDecl *Ivar) {
347 // objc_copyStruct (&structIvar, &Arg,
348 // sizeof (struct something), true, false);
349 llvm::Value *GetCopyStructFn =
350 CGM.getObjCRuntime().GetSetStructFunction();
351 CodeGenTypes &Types = CGM.getTypes();
352 CallArgList Args;
353 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
354 RValue RV =
355 RValue::get(Builder.CreateBitCast(LV.getAddress(),
356 Types.ConvertType(getContext().VoidPtrTy)));
357 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
358 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
359 llvm::Value *ArgAsPtrTy =
360 Builder.CreateBitCast(Arg,
361 Types.ConvertType(getContext().VoidPtrTy));
362 RV = RValue::get(ArgAsPtrTy);
363 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
364 // sizeof (Type of Ivar)
365 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
366 llvm::Value *SizeVal =
367 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
368 Size.getQuantity());
369 Args.push_back(std::make_pair(RValue::get(SizeVal),
370 getContext().LongTy));
371 llvm::Value *True =
372 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
373 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
374 llvm::Value *False =
375 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
376 Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
377 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
378 FunctionType::ExtInfo()),
379 GetCopyStructFn, ReturnValueSlot(), Args);
380}
381
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000382/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000383/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
384/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000385void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
386 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000387 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000388 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
389 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
390 assert(OMD && "Invalid call to generate setter (empty method)");
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000391 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian0f4c7112011-04-05 21:41:23 +0000392 const llvm::Triple &Triple = getContext().Target.getTriple();
393 QualType IVART = Ivar->getType();
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000394 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump11289f42009-09-09 15:08:12 +0000395 bool IsAtomic =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000396 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
397
398 // Determine if we should use an objc_setProperty call for
399 // this. Properties with 'copy' semantics always use it, as do
400 // non-atomic properties with 'release' semantics as long as we are
401 // not in gc-only mode.
402 if (IsCopy ||
403 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
404 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000405 llvm::Value *SetPropertyFn =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000406 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000407
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000408 if (!SetPropertyFn) {
409 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
410 FinishFunction();
411 return;
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
414 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000415 // <is-atomic>, <is-copy>).
416 // FIXME: Can't this be simpler? This might even be worse than the
417 // corresponding gcc code.
418 CodeGenTypes &Types = CGM.getTypes();
419 ValueDecl *Cmd = OMD->getCmdDecl();
420 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
421 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000422 llvm::Value *SelfAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000423 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000424 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattnera4997152009-02-20 18:43:26 +0000425 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump11289f42009-09-09 15:08:12 +0000426 llvm::Value *ArgAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000427 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
428 Types.ConvertType(IdTy));
429 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000430 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000431 llvm::Value *False =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000432 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000433 CallArgList Args;
434 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
435 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
David Chisnall08a45252011-03-22 20:03:13 +0000436 Args.push_back(std::make_pair(RValue::get(Offset),
437 getContext().getPointerDiffType()));
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000438 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump11289f42009-09-09 15:08:12 +0000439 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000440 getContext().BoolTy));
Mike Stump11289f42009-09-09 15:08:12 +0000441 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000442 getContext().BoolTy));
Mike Stump18bb9282009-05-16 07:57:57 +0000443 // FIXME: We shouldn't need to get the function info here, the runtime
444 // already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000445 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000446 FunctionType::ExtInfo()),
447 SetPropertyFn,
Anders Carlsson61a401c2009-12-24 19:25:24 +0000448 ReturnValueSlot(), Args);
Fariborz Jahanian0f4c7112011-04-05 21:41:23 +0000449 } else if (IsAtomic && hasAggregateLLVMType(IVART) &&
450 !IVART->isAnyComplexType() &&
451 ((Triple.getArch() == llvm::Triple::x86 &&
452 (getContext().getTypeSizeInChars(IVART)
453 > CharUnits::fromQuantity(4))) ||
454 (Triple.getArch() == llvm::Triple::x86_64 &&
455 (getContext().getTypeSizeInChars(IVART)
456 > CharUnits::fromQuantity(8))))
David Chisnall168b80f2010-12-26 22:13:16 +0000457 && CGM.getObjCRuntime().GetSetStructFunction()) {
Fariborz Jahanian0f4c7112011-04-05 21:41:23 +0000458 // objc_copyStruct (&structIvar, &Arg,
459 // sizeof (struct something), true, false);
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000460 GenerateObjCAtomicSetterBody(OMD, Ivar);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000461 } else if (PID->getSetterCXXAssignment()) {
John McCalla2342eb2010-12-05 02:00:02 +0000462 EmitIgnoredExpr(PID->getSetterCXXAssignment());
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000463 } else {
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000464 if (IsAtomic &&
465 IVART->isScalarType() &&
466 (Triple.getArch() == llvm::Triple::arm ||
467 Triple.getArch() == llvm::Triple::thumb) &&
468 (getContext().getTypeSizeInChars(IVART)
469 > CharUnits::fromQuantity(4)) &&
470 CGM.getObjCRuntime().GetGetStructFunction()) {
471 GenerateObjCAtomicSetterBody(OMD, Ivar);
472 }
Fariborz Jahanian0f4c7112011-04-05 21:41:23 +0000473 else if (IsAtomic &&
474 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
475 Triple.getArch() == llvm::Triple::x86 &&
476 (getContext().getTypeSizeInChars(IVART)
477 > CharUnits::fromQuantity(4)) &&
478 CGM.getObjCRuntime().GetGetStructFunction()) {
479 GenerateObjCAtomicSetterBody(OMD, Ivar);
480 }
481 else if (IsAtomic &&
482 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
483 Triple.getArch() == llvm::Triple::x86_64 &&
484 (getContext().getTypeSizeInChars(IVART)
485 > CharUnits::fromQuantity(8)) &&
486 CGM.getObjCRuntime().GetGetStructFunction()) {
487 GenerateObjCAtomicSetterBody(OMD, Ivar);
488 }
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000489 else {
490 // FIXME: Find a clean way to avoid AST node creation.
491 SourceLocation Loc = PD->getLocation();
492 ValueDecl *Self = OMD->getSelfDecl();
493 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
494 DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc);
495 ParmVarDecl *ArgDecl = *OMD->param_begin();
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000496 QualType T = ArgDecl->getType();
497 if (T->isReferenceType())
498 T = cast<ReferenceType>(T)->getPointeeType();
499 DeclRefExpr Arg(ArgDecl, T, VK_LValue, Loc);
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000500 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000501
Fariborz Jahanian302a3d42011-02-18 19:15:13 +0000502 // The property type can differ from the ivar type in some situations with
503 // Objective-C pointer types, we can always bit cast the RHS in these cases.
504 if (getContext().getCanonicalType(Ivar->getType()) !=
505 getContext().getCanonicalType(ArgDecl->getType())) {
506 ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
507 Ivar->getType(), CK_BitCast, &Arg,
508 VK_RValue);
509 BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
510 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
511 EmitStmt(&Assign);
512 } else {
513 BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
514 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
515 EmitStmt(&Assign);
516 }
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000517 }
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000518 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000519
520 FinishFunction();
Chris Lattner5696e7b2008-06-17 18:05:57 +0000521}
522
John McCall6a4fa522011-03-22 07:05:39 +0000523// FIXME: these are stolen from CGClass.cpp, which is lame.
524namespace {
525 struct CallArrayIvarDtor : EHScopeStack::Cleanup {
526 const ObjCIvarDecl *ivar;
527 llvm::Value *self;
528 CallArrayIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
529 : ivar(ivar), self(self) {}
530
531 void Emit(CodeGenFunction &CGF, bool IsForEH) {
532 LValue lvalue =
533 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
534
535 QualType type = ivar->getType();
536 const ConstantArrayType *arrayType
537 = CGF.getContext().getAsConstantArrayType(type);
538 QualType baseType = CGF.getContext().getBaseElementType(arrayType);
539 const CXXRecordDecl *classDecl = baseType->getAsCXXRecordDecl();
540
541 llvm::Value *base
542 = CGF.Builder.CreateBitCast(lvalue.getAddress(),
543 CGF.ConvertType(baseType)->getPointerTo());
544 CGF.EmitCXXAggrDestructorCall(classDecl->getDestructor(),
545 arrayType, base);
546 }
547 };
548
549 struct CallIvarDtor : EHScopeStack::Cleanup {
550 const ObjCIvarDecl *ivar;
551 llvm::Value *self;
552 CallIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
553 : ivar(ivar), self(self) {}
554
555 void Emit(CodeGenFunction &CGF, bool IsForEH) {
556 LValue lvalue =
557 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
558
559 QualType type = ivar->getType();
560 const CXXRecordDecl *classDecl = type->getAsCXXRecordDecl();
561
562 CGF.EmitCXXDestructorCall(classDecl->getDestructor(),
563 Dtor_Complete, /*ForVirtualBase=*/false,
564 lvalue.getAddress());
565 }
566 };
567}
568
569static void emitCXXDestructMethod(CodeGenFunction &CGF,
570 ObjCImplementationDecl *impl) {
571 CodeGenFunction::RunCleanupsScope scope(CGF);
572
573 llvm::Value *self = CGF.LoadObjCSelf();
574
575 ObjCInterfaceDecl *iface
576 = const_cast<ObjCInterfaceDecl*>(impl->getClassInterface());
577 for (ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
578 ivar; ivar = ivar->getNextIvar()) {
579 QualType type = ivar->getType();
580
581 // Drill down to the base element type.
582 QualType baseType = type;
583 const ConstantArrayType *arrayType =
584 CGF.getContext().getAsConstantArrayType(baseType);
585 if (arrayType) baseType = CGF.getContext().getBaseElementType(arrayType);
586
587 // Check whether the ivar is a destructible type.
588 QualType::DestructionKind destructKind = baseType.isDestructedType();
589 assert(destructKind == type.isDestructedType());
590
591 switch (destructKind) {
592 case QualType::DK_none:
593 continue;
594
595 case QualType::DK_cxx_destructor:
596 if (arrayType)
597 CGF.EHStack.pushCleanup<CallArrayIvarDtor>(NormalAndEHCleanup,
598 ivar, self);
599 else
600 CGF.EHStack.pushCleanup<CallIvarDtor>(NormalAndEHCleanup,
601 ivar, self);
602 break;
603 }
604 }
605
606 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
607}
608
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000609void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
610 ObjCMethodDecl *MD,
611 bool ctor) {
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000612 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
613 StartObjCMethod(MD, IMP->getClassInterface());
John McCall6a4fa522011-03-22 07:05:39 +0000614
615 // Emit .cxx_construct.
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000616 if (ctor) {
John McCall6a4fa522011-03-22 07:05:39 +0000617 llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
618 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
619 E = IMP->init_end(); B != E; ++B) {
620 CXXCtorInitializer *IvarInit = (*B);
Francois Pichetd583da02010-12-04 09:14:42 +0000621 FieldDecl *Field = IvarInit->getAnyMember();
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000622 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian499b9022010-04-28 22:30:33 +0000623 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
624 LoadObjCSelf(), Ivar, 0);
John McCall7a626f62010-09-15 10:14:12 +0000625 EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000626 }
627 // constructor returns 'self'.
628 CodeGenTypes &Types = CGM.getTypes();
629 QualType IdTy(CGM.getContext().getObjCIdType());
630 llvm::Value *SelfAsId =
631 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
632 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
John McCall6a4fa522011-03-22 07:05:39 +0000633
634 // Emit .cxx_destruct.
Chandler Carruthf3983652010-05-06 00:20:39 +0000635 } else {
John McCall6a4fa522011-03-22 07:05:39 +0000636 emitCXXDestructMethod(*this, IMP);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000637 }
638 FinishFunction();
639}
640
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000641bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
642 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
643 it++; it++;
644 const ABIArgInfo &AI = it->info;
645 // FIXME. Is this sufficient check?
646 return (AI.getKind() == ABIArgInfo::Indirect);
647}
648
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000649bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
650 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
651 return false;
652 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
653 return FDTTy->getDecl()->hasObjectMember();
654 return false;
655}
656
Daniel Dunbara08dff12008-09-24 04:04:31 +0000657llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000658 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
659 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner5696e7b2008-06-17 18:05:57 +0000660}
661
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000662QualType CodeGenFunction::TypeOfSelfObject() {
663 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
664 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000665 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
666 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000667 return PTy->getPointeeType();
668}
669
John McCall0692a322010-12-04 03:11:00 +0000670LValue
671CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
672 // This is a special l-value that just issues sends when we load or
673 // store through it.
674
675 // For certain base kinds, we need to emit the base immediately.
676 llvm::Value *Base;
677 if (E->isSuperReceiver())
678 Base = LoadObjCSelf();
679 else if (E->isClassReceiver())
680 Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
681 else
682 Base = EmitScalarExpr(E->getBase());
683 return LValue::MakePropertyRef(E, Base);
684}
685
686static RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
687 ReturnValueSlot Return,
688 QualType ResultType,
689 Selector S,
690 llvm::Value *Receiver,
691 const CallArgList &CallArgs) {
692 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000693 bool isClassMessage = OMD->isClassMethod();
694 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
John McCall0692a322010-12-04 03:11:00 +0000695 return CGF.CGM.getObjCRuntime()
696 .GenerateMessageSendSuper(CGF, Return, ResultType,
697 S, OMD->getClassInterface(),
698 isCategoryImpl, Receiver,
699 isClassMessage, CallArgs);
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000700}
701
John McCallf3eb96f2010-12-04 02:32:38 +0000702RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
703 ReturnValueSlot Return) {
704 const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
Fariborz Jahanian7a26ba42011-03-30 16:11:20 +0000705 QualType ResultType = E->getGetterResultType();
John McCallb7bd14f2010-12-02 01:19:52 +0000706 Selector S;
707 if (E->isExplicitProperty()) {
708 const ObjCPropertyDecl *Property = E->getExplicitProperty();
709 S = Property->getGetterName();
Mike Stump658fe022009-07-30 22:28:39 +0000710 } else {
John McCallb7bd14f2010-12-02 01:19:52 +0000711 const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter();
712 S = Getter->getSelector();
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000713 }
John McCallb7bd14f2010-12-02 01:19:52 +0000714
John McCallf3eb96f2010-12-04 02:32:38 +0000715 llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
John McCall0692a322010-12-04 03:11:00 +0000716
717 // Accesses to 'super' follow a different code path.
718 if (E->isSuperReceiver())
719 return GenerateMessageSendSuper(*this, Return, ResultType,
720 S, Receiver, CallArgList());
721
John McCallf3eb96f2010-12-04 02:32:38 +0000722 const ObjCInterfaceDecl *ReceiverClass
723 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
John McCallb7bd14f2010-12-02 01:19:52 +0000724 return CGM.getObjCRuntime().
725 GenerateMessageSend(*this, Return, ResultType, S,
726 Receiver, CallArgList(), ReceiverClass);
Daniel Dunbar55310df2008-08-27 06:57:25 +0000727}
728
John McCallf3eb96f2010-12-04 02:32:38 +0000729void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
730 LValue Dst) {
731 const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
John McCallb7bd14f2010-12-02 01:19:52 +0000732 Selector S = E->getSetterSelector();
Fariborz Jahanian7a26ba42011-03-30 16:11:20 +0000733 QualType ArgType = E->getSetterArgType();
734
Fariborz Jahanian701f0942011-02-08 22:33:23 +0000735 // FIXME. Other than scalars, AST is not adequate for setter and
736 // getter type mismatches which require conversion.
737 if (Src.isScalar()) {
738 llvm::Value *SrcVal = Src.getScalarVal();
739 QualType DstType = getContext().getCanonicalType(ArgType);
740 const llvm::Type *DstTy = ConvertType(DstType);
741 if (SrcVal->getType() != DstTy)
742 Src =
743 RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
744 }
745
John McCall0692a322010-12-04 03:11:00 +0000746 CallArgList Args;
747 Args.push_back(std::make_pair(Src, ArgType));
748
749 llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
750 QualType ResultType = getContext().VoidTy;
751
John McCallb7bd14f2010-12-02 01:19:52 +0000752 if (E->isSuperReceiver()) {
John McCall0692a322010-12-04 03:11:00 +0000753 GenerateMessageSendSuper(*this, ReturnValueSlot(),
754 ResultType, S, Receiver, Args);
John McCallb7bd14f2010-12-02 01:19:52 +0000755 return;
756 }
757
John McCallf3eb96f2010-12-04 02:32:38 +0000758 const ObjCInterfaceDecl *ReceiverClass
759 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
John McCallb7bd14f2010-12-02 01:19:52 +0000760
John McCallb7bd14f2010-12-02 01:19:52 +0000761 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
John McCall0692a322010-12-04 03:11:00 +0000762 ResultType, S, Receiver, Args,
763 ReceiverClass);
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000764}
765
Chris Lattnerd4808922009-03-22 21:03:39 +0000766void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump11289f42009-09-09 15:08:12 +0000767 llvm::Constant *EnumerationMutationFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000768 CGM.getObjCRuntime().EnumerationMutationFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000769
Daniel Dunbara08dff12008-09-24 04:04:31 +0000770 if (!EnumerationMutationFn) {
771 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
772 return;
773 }
774
John McCall9e2e22f2011-02-22 07:16:58 +0000775 // The local variable comes into scope immediately.
776 AutoVarEmission variable = AutoVarEmission::invalid();
777 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
778 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
779
Devang Pateld2d66652011-01-19 01:36:36 +0000780 CGDebugInfo *DI = getDebugInfo();
781 if (DI) {
782 DI->setLocation(S.getSourceRange().getBegin());
783 DI->EmitRegionStart(Builder);
784 }
785
John McCall1c926b72011-01-07 01:49:06 +0000786 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
787 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
Mike Stump11289f42009-09-09 15:08:12 +0000788
Anders Carlsson75658592008-08-31 02:33:12 +0000789 // Fast enumeration state.
790 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbara7566f12010-02-09 02:48:28 +0000791 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlssonc0964b62010-05-22 17:35:42 +0000792 EmitNullInitialization(StatePtr, StateTy);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Anders Carlsson75658592008-08-31 02:33:12 +0000794 // Number of elements in the items array.
Anders Carlsson3f35a262008-08-31 04:05:03 +0000795 static const unsigned NumItems = 16;
Mike Stump11289f42009-09-09 15:08:12 +0000796
John McCall1c926b72011-01-07 01:49:06 +0000797 // Fetch the countByEnumeratingWithState:objects:count: selector.
Benjamin Kramer9e649c32010-03-30 11:36:44 +0000798 IdentifierInfo *II[] = {
799 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
800 &CGM.getContext().Idents.get("objects"),
801 &CGM.getContext().Idents.get("count")
802 };
803 Selector FastEnumSel =
804 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlsson75658592008-08-31 02:33:12 +0000805
806 QualType ItemsTy =
807 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump11289f42009-09-09 15:08:12 +0000808 llvm::APInt(32, NumItems),
Anders Carlsson75658592008-08-31 02:33:12 +0000809 ArrayType::Normal, 0);
Daniel Dunbara7566f12010-02-09 02:48:28 +0000810 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000811
John McCall1c926b72011-01-07 01:49:06 +0000812 // Emit the collection pointer.
Anders Carlsson75658592008-08-31 02:33:12 +0000813 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump11289f42009-09-09 15:08:12 +0000814
John McCall1c926b72011-01-07 01:49:06 +0000815 // Send it our message:
Anders Carlsson75658592008-08-31 02:33:12 +0000816 CallArgList Args;
John McCall1c926b72011-01-07 01:49:06 +0000817
818 // The first argument is a temporary of the enumeration-state type.
Mike Stump11289f42009-09-09 15:08:12 +0000819 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000820 getContext().getPointerType(StateTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000821
John McCall1c926b72011-01-07 01:49:06 +0000822 // The second argument is a temporary array with space for NumItems
823 // pointers. We'll actually be loading elements from the array
824 // pointer written into the control state; this buffer is so that
825 // collections that *aren't* backed by arrays can still queue up
826 // batches of elements.
Mike Stump11289f42009-09-09 15:08:12 +0000827 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000828 getContext().getPointerType(ItemsTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000829
John McCall1c926b72011-01-07 01:49:06 +0000830 // The third argument is the capacity of that temporary array.
Anders Carlsson75658592008-08-31 02:33:12 +0000831 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000832 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump11289f42009-09-09 15:08:12 +0000833 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000834 getContext().UnsignedLongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000835
John McCall1c926b72011-01-07 01:49:06 +0000836 // Start the enumeration.
Mike Stump11289f42009-09-09 15:08:12 +0000837 RValue CountRV =
John McCall78a15112010-05-22 01:48:05 +0000838 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlsson75658592008-08-31 02:33:12 +0000839 getContext().UnsignedLongTy,
840 FastEnumSel,
David Chisnall01aa4672010-04-28 19:33:36 +0000841 Collection, Args);
Anders Carlsson75658592008-08-31 02:33:12 +0000842
John McCall1c926b72011-01-07 01:49:06 +0000843 // The initial number of objects that were returned in the buffer.
844 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
Mike Stump11289f42009-09-09 15:08:12 +0000845
John McCall1c926b72011-01-07 01:49:06 +0000846 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
847 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
Mike Stump11289f42009-09-09 15:08:12 +0000848
John McCall1c926b72011-01-07 01:49:06 +0000849 llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlsson75658592008-08-31 02:33:12 +0000850
John McCall1c926b72011-01-07 01:49:06 +0000851 // If the limit pointer was zero to begin with, the collection is
852 // empty; skip all this.
853 Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
854 EmptyBB, LoopInitBB);
Anders Carlsson75658592008-08-31 02:33:12 +0000855
John McCall1c926b72011-01-07 01:49:06 +0000856 // Otherwise, initialize the loop.
857 EmitBlock(LoopInitBB);
Mike Stump11289f42009-09-09 15:08:12 +0000858
John McCall1c926b72011-01-07 01:49:06 +0000859 // Save the initial mutations value. This is the value at an
860 // address that was written into the state object by
861 // countByEnumeratingWithState:objects:count:.
Mike Stump11289f42009-09-09 15:08:12 +0000862 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson3f35a262008-08-31 04:05:03 +0000863 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000864 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000865 "mutationsptr");
Mike Stump11289f42009-09-09 15:08:12 +0000866
John McCall1c926b72011-01-07 01:49:06 +0000867 llvm::Value *initialMutations =
868 Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000869
John McCall1c926b72011-01-07 01:49:06 +0000870 // Start looping. This is the point we return to whenever we have a
871 // fresh, non-empty batch of objects.
872 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
873 EmitBlock(LoopBodyBB);
Mike Stump11289f42009-09-09 15:08:12 +0000874
John McCall1c926b72011-01-07 01:49:06 +0000875 // The current index into the buffer.
Jay Foad20c0f022011-03-30 11:28:58 +0000876 llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
John McCall1c926b72011-01-07 01:49:06 +0000877 index->addIncoming(zero, LoopInitBB);
Anders Carlsson75658592008-08-31 02:33:12 +0000878
John McCall1c926b72011-01-07 01:49:06 +0000879 // The current buffer size.
Jay Foad20c0f022011-03-30 11:28:58 +0000880 llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
John McCall1c926b72011-01-07 01:49:06 +0000881 count->addIncoming(initialBufferLimit, LoopInitBB);
Mike Stump11289f42009-09-09 15:08:12 +0000882
John McCall1c926b72011-01-07 01:49:06 +0000883 // Check whether the mutations value has changed from where it was
884 // at start. StateMutationsPtr should actually be invariant between
885 // refreshes.
Anders Carlsson3f35a262008-08-31 04:05:03 +0000886 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
John McCall1c926b72011-01-07 01:49:06 +0000887 llvm::Value *currentMutations
888 = Builder.CreateLoad(StateMutationsPtr, "statemutations");
Anders Carlsson3f35a262008-08-31 04:05:03 +0000889
John McCall1c926b72011-01-07 01:49:06 +0000890 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
Dan Gohman6ca99822011-03-02 22:39:34 +0000891 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
Mike Stump11289f42009-09-09 15:08:12 +0000892
John McCall1c926b72011-01-07 01:49:06 +0000893 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
894 WasNotMutatedBB, WasMutatedBB);
Mike Stump11289f42009-09-09 15:08:12 +0000895
John McCall1c926b72011-01-07 01:49:06 +0000896 // If so, call the enumeration-mutation function.
897 EmitBlock(WasMutatedBB);
Anders Carlsson3f35a262008-08-31 04:05:03 +0000898 llvm::Value *V =
Mike Stump11289f42009-09-09 15:08:12 +0000899 Builder.CreateBitCast(Collection,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000900 ConvertType(getContext().getObjCIdType()),
901 "tmp");
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000902 CallArgList Args2;
Mike Stump11289f42009-09-09 15:08:12 +0000903 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000904 getContext().getObjCIdType()));
Mike Stump18bb9282009-05-16 07:57:57 +0000905 // FIXME: We shouldn't need to get the function info here, the runtime already
906 // should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000907 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000908 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000909 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump11289f42009-09-09 15:08:12 +0000910
John McCall1c926b72011-01-07 01:49:06 +0000911 // Otherwise, or if the mutation function returns, just continue.
912 EmitBlock(WasNotMutatedBB);
Mike Stump11289f42009-09-09 15:08:12 +0000913
John McCall1c926b72011-01-07 01:49:06 +0000914 // Initialize the element variable.
915 RunCleanupsScope elementVariableScope(*this);
John McCall9e2e22f2011-02-22 07:16:58 +0000916 bool elementIsVariable;
John McCall1c926b72011-01-07 01:49:06 +0000917 LValue elementLValue;
918 QualType elementType;
919 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
John McCall9e2e22f2011-02-22 07:16:58 +0000920 // Initialize the variable, in case it's a __block variable or something.
921 EmitAutoVarInit(variable);
John McCall1c926b72011-01-07 01:49:06 +0000922
John McCall9e2e22f2011-02-22 07:16:58 +0000923 const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
John McCall1c926b72011-01-07 01:49:06 +0000924 DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
925 VK_LValue, SourceLocation());
926 elementLValue = EmitLValue(&tempDRE);
927 elementType = D->getType();
John McCall9e2e22f2011-02-22 07:16:58 +0000928 elementIsVariable = true;
John McCall1c926b72011-01-07 01:49:06 +0000929 } else {
930 elementLValue = LValue(); // suppress warning
931 elementType = cast<Expr>(S.getElement())->getType();
John McCall9e2e22f2011-02-22 07:16:58 +0000932 elementIsVariable = false;
John McCall1c926b72011-01-07 01:49:06 +0000933 }
934 const llvm::Type *convertedElementType = ConvertType(elementType);
935
936 // Fetch the buffer out of the enumeration state.
937 // TODO: this pointer should actually be invariant between
938 // refreshes, which would help us do certain loop optimizations.
Mike Stump11289f42009-09-09 15:08:12 +0000939 llvm::Value *StateItemsPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000940 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
John McCall1c926b72011-01-07 01:49:06 +0000941 llvm::Value *EnumStateItems =
942 Builder.CreateLoad(StateItemsPtr, "stateitems");
Anders Carlsson75658592008-08-31 02:33:12 +0000943
John McCall1c926b72011-01-07 01:49:06 +0000944 // Fetch the value at the current index from the buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000945 llvm::Value *CurrentItemPtr =
John McCall1c926b72011-01-07 01:49:06 +0000946 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
947 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000948
John McCall1c926b72011-01-07 01:49:06 +0000949 // Cast that value to the right type.
950 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
951 "currentitem");
Mike Stump11289f42009-09-09 15:08:12 +0000952
John McCall1c926b72011-01-07 01:49:06 +0000953 // Make sure we have an l-value. Yes, this gets evaluated every
954 // time through the loop.
John McCall9e2e22f2011-02-22 07:16:58 +0000955 if (!elementIsVariable)
John McCall1c926b72011-01-07 01:49:06 +0000956 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000957
John McCall1c926b72011-01-07 01:49:06 +0000958 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType);
Mike Stump11289f42009-09-09 15:08:12 +0000959
John McCall9e2e22f2011-02-22 07:16:58 +0000960 // If we do have an element variable, this assignment is the end of
961 // its initialization.
962 if (elementIsVariable)
963 EmitAutoVarCleanups(variable);
964
John McCall1c926b72011-01-07 01:49:06 +0000965 // Perform the loop body, setting up break and continue labels.
Anders Carlsson33747b62009-02-10 05:52:02 +0000966 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
John McCall1c926b72011-01-07 01:49:06 +0000967 {
968 RunCleanupsScope Scope(*this);
969 EmitStmt(S.getBody());
970 }
Anders Carlsson75658592008-08-31 02:33:12 +0000971 BreakContinueStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000972
John McCall1c926b72011-01-07 01:49:06 +0000973 // Destroy the element variable now.
974 elementVariableScope.ForceCleanup();
975
976 // Check whether there are more elements.
John McCallad5d61e2010-07-23 21:56:41 +0000977 EmitBlock(AfterBody.getBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000978
John McCall1c926b72011-01-07 01:49:06 +0000979 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
Fariborz Jahanian6e7ecc82009-01-06 18:56:31 +0000980
John McCall1c926b72011-01-07 01:49:06 +0000981 // First we check in the local buffer.
982 llvm::Value *indexPlusOne
983 = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlsson75658592008-08-31 02:33:12 +0000984
John McCall1c926b72011-01-07 01:49:06 +0000985 // If we haven't overrun the buffer yet, we can continue.
986 Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
987 LoopBodyBB, FetchMoreBB);
988
989 index->addIncoming(indexPlusOne, AfterBody.getBlock());
990 count->addIncoming(count, AfterBody.getBlock());
991
992 // Otherwise, we have to fetch more elements.
993 EmitBlock(FetchMoreBB);
Mike Stump11289f42009-09-09 15:08:12 +0000994
995 CountRV =
John McCall78a15112010-05-22 01:48:05 +0000996 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlsson75658592008-08-31 02:33:12 +0000997 getContext().UnsignedLongTy,
Mike Stump11289f42009-09-09 15:08:12 +0000998 FastEnumSel,
David Chisnall01aa4672010-04-28 19:33:36 +0000999 Collection, Args);
Mike Stump11289f42009-09-09 15:08:12 +00001000
John McCall1c926b72011-01-07 01:49:06 +00001001 // If we got a zero count, we're done.
1002 llvm::Value *refetchCount = CountRV.getScalarVal();
1003
1004 // (note that the message send might split FetchMoreBB)
1005 index->addIncoming(zero, Builder.GetInsertBlock());
1006 count->addIncoming(refetchCount, Builder.GetInsertBlock());
1007
1008 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1009 EmptyBB, LoopBodyBB);
Mike Stump11289f42009-09-09 15:08:12 +00001010
Anders Carlsson75658592008-08-31 02:33:12 +00001011 // No more elements.
John McCall1c926b72011-01-07 01:49:06 +00001012 EmitBlock(EmptyBB);
Anders Carlsson75658592008-08-31 02:33:12 +00001013
John McCall9e2e22f2011-02-22 07:16:58 +00001014 if (!elementIsVariable) {
Anders Carlsson75658592008-08-31 02:33:12 +00001015 // If the element was not a declaration, set it to be null.
1016
John McCall1c926b72011-01-07 01:49:06 +00001017 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1018 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1019 EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType);
Anders Carlsson75658592008-08-31 02:33:12 +00001020 }
1021
Devang Pateld2d66652011-01-19 01:36:36 +00001022 if (DI) {
1023 DI->setLocation(S.getSourceRange().getEnd());
1024 DI->EmitRegionEnd(Builder);
1025 }
1026
John McCallad5d61e2010-07-23 21:56:41 +00001027 EmitBlock(LoopEnd.getBlock());
Anders Carlsson2e744e82008-08-30 19:51:14 +00001028}
1029
Mike Stump11289f42009-09-09 15:08:12 +00001030void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
John McCallbd309292010-07-06 01:34:17 +00001031 CGM.getObjCRuntime().EmitTryStmt(*this, S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001032}
1033
Mike Stump11289f42009-09-09 15:08:12 +00001034void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001035 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1036}
1037
Chris Lattnere132e242008-11-15 21:26:17 +00001038void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump11289f42009-09-09 15:08:12 +00001039 const ObjCAtSynchronizedStmt &S) {
John McCallbd309292010-07-06 01:34:17 +00001040 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
Chris Lattnere132e242008-11-15 21:26:17 +00001041}
1042
Ted Kremenek43e06332008-04-09 15:51:31 +00001043CGObjCRuntime::~CGObjCRuntime() {}