blob: ee0ee7b94109ad2ba1fea1753e06a6bda9b03793 [file] [log] [blame]
Anders Carlsson55085182007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Carlsson55085182007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Devang Patelbcbd03a2011-01-19 01:36:36 +000014#include "CGDebugInfo.h"
Ted Kremenek2979ec72008-04-09 15:51:31 +000015#include "CGObjCRuntime.h"
Anders Carlsson55085182007-08-21 17:43:55 +000016#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Daniel Dunbar85c59ed2008-08-29 08:11:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Chris Lattner16f00492009-04-26 01:32:48 +000020#include "clang/AST/StmtObjC.h"
Daniel Dunbare66f4e32008-09-03 00:27:26 +000021#include "clang/Basic/Diagnostic.h"
Anders Carlsson3d8400d2008-08-30 19:51:14 +000022#include "llvm/ADT/STLExtras.h"
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +000023#include "llvm/Target/TargetData.h"
Anders Carlsson55085182007-08-21 17:43:55 +000024using namespace clang;
25using namespace CodeGen;
26
Chris Lattner8fdf3282008-06-24 17:04:18 +000027/// Emits an instance of NSConstantString representing the object.
Mike Stump1eb44332009-09-09 15:08:12 +000028llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
Daniel Dunbar71fcec92008-11-25 21:53:21 +000029{
David Chisnall0d13f6f2010-01-23 02:40:42 +000030 llvm::Constant *C =
31 CGM.getObjCRuntime().GenerateConstantString(E->getString());
Daniel Dunbared7c6182008-08-20 00:28:19 +000032 // FIXME: This bitcast should just be made an invariant on the Runtime.
Owen Anderson3c4972d2009-07-29 18:54:39 +000033 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-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 Dunbar6d5a1c22010-02-03 20:11:42 +000042 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000043}
44
Daniel Dunbared7c6182008-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 Lattner8fdf3282008-06-24 17:04:18 +000049
50
John McCallef072fd2010-05-22 01:48:05 +000051RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
52 ReturnValueSlot Return) {
Chris Lattner8fdf3282008-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 Stump1eb44332009-09-09 15:08:12 +000056
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000057 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000058 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000059 bool isClassMessage = false;
David Chisnallc6cd5fd2010-04-28 19:33:36 +000060 ObjCInterfaceDecl *OID = 0;
Chris Lattner8fdf3282008-06-24 17:04:18 +000061 // Find the receiver
Daniel Dunbar0b647a62010-04-22 03:17:06 +000062 llvm::Value *Receiver = 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +000063 switch (E->getReceiverKind()) {
64 case ObjCMessageExpr::Instance:
65 Receiver = EmitScalarExpr(E->getInstanceReceiver());
66 break;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000067
Douglas Gregor04badcf2010-04-21 00:45:42 +000068 case ObjCMessageExpr::Class: {
John McCall3031c632010-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 Chisnallc6cd5fd2010-04-28 19:33:36 +000074 Receiver = Runtime.GetClass(Builder, OID);
Daniel Dunbarf56f1912008-08-25 08:19:24 +000075 isClassMessage = true;
Douglas Gregor04badcf2010-04-21 00:45:42 +000076 break;
77 }
78
79 case ObjCMessageExpr::SuperInstance:
Chris Lattner8fdf3282008-06-24 17:04:18 +000080 Receiver = LoadObjCSelf();
Douglas Gregor04badcf2010-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 Lattner8fdf3282008-06-24 17:04:18 +000089 }
90
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000091 CallArgList Args;
Anders Carlsson131038e2009-04-18 20:29:27 +000092 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +000093
Anders Carlsson7e70fb22010-06-21 20:59:55 +000094 QualType ResultType =
95 E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType();
96
Chris Lattner8fdf3282008-06-24 17:04:18 +000097 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000098 // super is only valid in an Objective-C method
99 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000100 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Anders Carlsson7e70fb22010-06-21 20:59:55 +0000101 return Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000102 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000103 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000104 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000105 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000106 isClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000107 Args,
108 E->getMethodDecl());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000109 }
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000110
Anders Carlsson7e70fb22010-06-21 20:59:55 +0000111 return Runtime.GenerateMessageSend(*this, Return, ResultType,
John McCallef072fd2010-05-22 01:48:05 +0000112 E->getSelector(),
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000113 Receiver, Args, OID,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000114 E->getMethodDecl());
Anders Carlsson55085182007-08-21 17:43:55 +0000115}
116
Daniel Dunbaraf05bb92008-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 Jahanian679a5022009-01-10 21:06:09 +0000120void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
121 const ObjCContainerDecl *CD) {
John McCalld26bc762011-03-09 04:27:21 +0000122 FunctionArgList args;
Devang Patel4800ea62010-04-05 21:09:15 +0000123 // Check if we should generate debug info for this method.
Devang Patelaa112892011-03-07 18:45:56 +0000124 if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
125 DebugInfo = CGM.getModuleDebugInfo();
Devang Patel4800ea62010-04-05 21:09:15 +0000126
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000127 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000128
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000129 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
130 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000131
John McCalld26bc762011-03-09 04:27:21 +0000132 args.push_back(OMD->getSelfDecl());
133 args.push_back(OMD->getCmdDecl());
Chris Lattner41110242008-06-17 18:05:57 +0000134
Chris Lattner89951a82009-02-20 18:43:26 +0000135 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
136 E = OMD->param_end(); PI != E; ++PI)
John McCalld26bc762011-03-09 04:27:21 +0000137 args.push_back(*PI);
Chris Lattner41110242008-06-17 18:05:57 +0000138
Peter Collingbourne14110472011-01-13 18:57:25 +0000139 CurGD = OMD;
140
John McCalld26bc762011-03-09 04:27:21 +0000141 StartFunction(OMD, OMD->getResultType(), Fn, FI, args, OMD->getLocStart());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000142}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000143
Fariborz Jahanian2846b972011-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;
John McCall0774cb82011-05-15 01:53:33 +0000154 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, VoidPtrTy));
Eli Friedman04c9a492011-05-02 17:57:46 +0000155 Args.add(RV, getContext().VoidPtrTy);
John McCall0774cb82011-05-15 01:53:33 +0000156 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), VoidPtrTy));
Eli Friedman04c9a492011-05-02 17:57:46 +0000157 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000158 // sizeof (Type of Ivar)
159 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
160 llvm::Value *SizeVal =
John McCall0774cb82011-05-15 01:53:33 +0000161 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000162 Size.getQuantity());
Eli Friedman04c9a492011-05-02 17:57:46 +0000163 Args.add(RValue::get(SizeVal), getContext().LongTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000164 llvm::Value *isAtomic =
165 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
166 IsAtomic ? 1 : 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000167 Args.add(RValue::get(isAtomic), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000168 llvm::Value *hasStrong =
169 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
170 IsStrong ? 1 : 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000171 Args.add(RValue::get(hasStrong), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000172 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
173 FunctionType::ExtInfo()),
174 GetCopyStructFn, ReturnValueSlot(), Args);
175}
176
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000177/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000178/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000179void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000180 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000181 EmitStmt(OMD->getBody());
182 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000183}
184
Mike Stumpf5408fe2009-05-16 07:57:57 +0000185// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
186// AST for the whole body we can just fall back to having a GenerateFunction
187// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000188
189/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000190/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
191/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000192void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
193 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000194 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000195 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000196 bool IsAtomic =
197 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000198 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
199 assert(OMD && "Invalid call to generate getter (empty method)");
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000200 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000201
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000202 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000203 // this. Non-atomic properties are directly evaluated.
204 // atomic 'copy' and 'retain' properties are also directly
205 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000206 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000207 IsAtomic &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000208 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
209 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000210 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000211 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000213 if (!GetPropertyFn) {
214 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
215 FinishFunction();
216 return;
217 }
218
219 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
220 // FIXME: Can't this be simpler? This might even be worse than the
221 // corresponding gcc code.
222 CodeGenTypes &Types = CGM.getTypes();
223 ValueDecl *Cmd = OMD->getCmdDecl();
224 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
225 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000226 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000227 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000228 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000229 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000230 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000231 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000232 Args.add(RValue::get(SelfAsId), IdTy);
233 Args.add(RValue::get(CmdVal), Cmd->getType());
234 Args.add(RValue::get(Offset), getContext().getPointerDiffType());
235 Args.add(RValue::get(True), getContext().BoolTy);
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000236 // FIXME: We shouldn't need to get the function info here, the
237 // runtime already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000238 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000239 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000240 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000241 // We need to fix the type here. Ivars with copy & retain are
242 // always objects so we don't need to worry about complex or
243 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000244 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000245 Types.ConvertType(PD->getType())));
246 EmitReturnOfRValue(RV, PD->getType());
247 } else {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000248 const llvm::Triple &Triple = getContext().Target.getTriple();
249 QualType IVART = Ivar->getType();
250 if (IsAtomic &&
251 IVART->isScalarType() &&
252 (Triple.getArch() == llvm::Triple::arm ||
253 Triple.getArch() == llvm::Triple::thumb) &&
254 (getContext().getTypeSizeInChars(IVART)
255 > CharUnits::fromQuantity(4)) &&
256 CGM.getObjCRuntime().GetGetStructFunction()) {
257 GenerateObjCGetterBody(Ivar, true, false);
258 }
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000259 else if (IsAtomic &&
260 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
261 Triple.getArch() == llvm::Triple::x86 &&
262 (getContext().getTypeSizeInChars(IVART)
263 > CharUnits::fromQuantity(4)) &&
264 CGM.getObjCRuntime().GetGetStructFunction()) {
265 GenerateObjCGetterBody(Ivar, true, false);
266 }
267 else if (IsAtomic &&
268 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
269 Triple.getArch() == llvm::Triple::x86_64 &&
270 (getContext().getTypeSizeInChars(IVART)
271 > CharUnits::fromQuantity(8)) &&
272 CGM.getObjCRuntime().GetGetStructFunction()) {
273 GenerateObjCGetterBody(Ivar, true, false);
274 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000275 else if (IVART->isAnyComplexType()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000276 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
277 Ivar, 0);
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000278 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
279 LV.isVolatileQualified());
280 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
281 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000282 else if (hasAggregateLLVMType(IVART)) {
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000283 bool IsStrong = false;
Fariborz Jahanian5fb65092011-04-05 23:01:27 +0000284 if ((IsStrong = IvarTypeWithAggrGCObjects(IVART))
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000285 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
David Chisnall8fac25d2010-12-26 22:13:16 +0000286 && CGM.getObjCRuntime().GetGetStructFunction()) {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000287 GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000288 }
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000289 else {
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000290 const CXXRecordDecl *classDecl = IVART->getAsCXXRecordDecl();
291
292 if (PID->getGetterCXXConstructor() &&
Sean Hunt023df372011-05-09 18:22:59 +0000293 classDecl && !classDecl->hasTrivialDefaultConstructor()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000294 ReturnStmt *Stmt =
295 new (getContext()) ReturnStmt(SourceLocation(),
Douglas Gregor5077c382010-05-15 06:01:05 +0000296 PID->getGetterCXXConstructor(),
297 0);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000298 EmitReturnStmt(*Stmt);
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000299 } else if (IsAtomic &&
300 !IVART->isAnyComplexType() &&
301 Triple.getArch() == llvm::Triple::x86 &&
302 (getContext().getTypeSizeInChars(IVART)
303 > CharUnits::fromQuantity(4)) &&
304 CGM.getObjCRuntime().GetGetStructFunction()) {
305 GenerateObjCGetterBody(Ivar, true, false);
306 }
307 else if (IsAtomic &&
308 !IVART->isAnyComplexType() &&
309 Triple.getArch() == llvm::Triple::x86_64 &&
310 (getContext().getTypeSizeInChars(IVART)
311 > CharUnits::fromQuantity(8)) &&
312 CGM.getObjCRuntime().GetGetStructFunction()) {
313 GenerateObjCGetterBody(Ivar, true, false);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000314 }
315 else {
316 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
317 Ivar, 0);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000318 EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000319 }
320 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000321 }
322 else {
323 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000324 Ivar, 0);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000325 if (PD->getType()->isReferenceType()) {
326 RValue RV = RValue::get(LV.getAddress());
327 EmitReturnOfRValue(RV, PD->getType());
328 }
329 else {
330 CodeGenTypes &Types = CGM.getTypes();
331 RValue RV = EmitLoadOfLValue(LV, IVART);
332 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000333 Types.ConvertType(PD->getType())));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000334 EmitReturnOfRValue(RV, PD->getType());
335 }
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000336 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000337 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000338
339 FinishFunction();
340}
341
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000342void CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
343 ObjCIvarDecl *Ivar) {
344 // objc_copyStruct (&structIvar, &Arg,
345 // sizeof (struct something), true, false);
346 llvm::Value *GetCopyStructFn =
347 CGM.getObjCRuntime().GetSetStructFunction();
348 CodeGenTypes &Types = CGM.getTypes();
349 CallArgList Args;
350 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
351 RValue RV =
352 RValue::get(Builder.CreateBitCast(LV.getAddress(),
353 Types.ConvertType(getContext().VoidPtrTy)));
Eli Friedman04c9a492011-05-02 17:57:46 +0000354 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000355 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
356 llvm::Value *ArgAsPtrTy =
357 Builder.CreateBitCast(Arg,
358 Types.ConvertType(getContext().VoidPtrTy));
359 RV = RValue::get(ArgAsPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +0000360 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000361 // sizeof (Type of Ivar)
362 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
363 llvm::Value *SizeVal =
364 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
365 Size.getQuantity());
Eli Friedman04c9a492011-05-02 17:57:46 +0000366 Args.add(RValue::get(SizeVal), getContext().LongTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000367 llvm::Value *True =
368 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Eli Friedman04c9a492011-05-02 17:57:46 +0000369 Args.add(RValue::get(True), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000370 llvm::Value *False =
371 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000372 Args.add(RValue::get(False), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000373 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
374 FunctionType::ExtInfo()),
375 GetCopyStructFn, ReturnValueSlot(), Args);
376}
377
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000378static bool
379IvarAssignHasTrvialAssignment(const ObjCPropertyImplDecl *PID,
380 QualType IvarT) {
381 bool HasTrvialAssignment = true;
382 if (PID->getSetterCXXAssignment()) {
383 const CXXRecordDecl *classDecl = IvarT->getAsCXXRecordDecl();
384 HasTrvialAssignment =
385 (!classDecl || classDecl->hasTrivialCopyAssignment());
386 }
387 return HasTrvialAssignment;
388}
389
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000390/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000391/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
392/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000393void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
394 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000395 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000396 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
397 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
398 assert(OMD && "Invalid call to generate setter (empty method)");
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000399 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000400 const llvm::Triple &Triple = getContext().Target.getTriple();
401 QualType IVART = Ivar->getType();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000402 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000403 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000404 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
405
406 // Determine if we should use an objc_setProperty call for
407 // this. Properties with 'copy' semantics always use it, as do
408 // non-atomic properties with 'release' semantics as long as we are
409 // not in gc-only mode.
410 if (IsCopy ||
411 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
412 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000413 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000414 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000416 if (!SetPropertyFn) {
417 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
418 FinishFunction();
419 return;
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
422 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000423 // <is-atomic>, <is-copy>).
424 // FIXME: Can't this be simpler? This might even be worse than the
425 // corresponding gcc code.
426 CodeGenTypes &Types = CGM.getTypes();
427 ValueDecl *Cmd = OMD->getCmdDecl();
428 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
429 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000430 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000431 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000432 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000433 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000434 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000435 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
436 Types.ConvertType(IdTy));
437 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000438 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000439 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000440 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000441 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000442 Args.add(RValue::get(SelfAsId), IdTy);
443 Args.add(RValue::get(CmdVal), Cmd->getType());
444 Args.add(RValue::get(Offset), getContext().getPointerDiffType());
445 Args.add(RValue::get(ArgAsId), IdTy);
446 Args.add(RValue::get(IsAtomic ? True : False), getContext().BoolTy);
447 Args.add(RValue::get(IsCopy ? True : False), getContext().BoolTy);
Mike Stumpf5408fe2009-05-16 07:57:57 +0000448 // FIXME: We shouldn't need to get the function info here, the runtime
449 // already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000450 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000451 FunctionType::ExtInfo()),
452 SetPropertyFn,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000453 ReturnValueSlot(), Args);
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000454 } else if (IsAtomic && hasAggregateLLVMType(IVART) &&
455 !IVART->isAnyComplexType() &&
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000456 IvarAssignHasTrvialAssignment(PID, IVART) &&
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000457 ((Triple.getArch() == llvm::Triple::x86 &&
458 (getContext().getTypeSizeInChars(IVART)
459 > CharUnits::fromQuantity(4))) ||
460 (Triple.getArch() == llvm::Triple::x86_64 &&
461 (getContext().getTypeSizeInChars(IVART)
462 > CharUnits::fromQuantity(8))))
David Chisnall8fac25d2010-12-26 22:13:16 +0000463 && CGM.getObjCRuntime().GetSetStructFunction()) {
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000464 // objc_copyStruct (&structIvar, &Arg,
465 // sizeof (struct something), true, false);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000466 GenerateObjCAtomicSetterBody(OMD, Ivar);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000467 } else if (PID->getSetterCXXAssignment()) {
John McCall2a416372010-12-05 02:00:02 +0000468 EmitIgnoredExpr(PID->getSetterCXXAssignment());
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000469 } else {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000470 if (IsAtomic &&
471 IVART->isScalarType() &&
472 (Triple.getArch() == llvm::Triple::arm ||
473 Triple.getArch() == llvm::Triple::thumb) &&
474 (getContext().getTypeSizeInChars(IVART)
475 > CharUnits::fromQuantity(4)) &&
476 CGM.getObjCRuntime().GetGetStructFunction()) {
477 GenerateObjCAtomicSetterBody(OMD, Ivar);
478 }
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000479 else if (IsAtomic &&
480 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
481 Triple.getArch() == llvm::Triple::x86 &&
482 (getContext().getTypeSizeInChars(IVART)
483 > CharUnits::fromQuantity(4)) &&
484 CGM.getObjCRuntime().GetGetStructFunction()) {
485 GenerateObjCAtomicSetterBody(OMD, Ivar);
486 }
487 else if (IsAtomic &&
488 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
489 Triple.getArch() == llvm::Triple::x86_64 &&
490 (getContext().getTypeSizeInChars(IVART)
491 > CharUnits::fromQuantity(8)) &&
492 CGM.getObjCRuntime().GetGetStructFunction()) {
493 GenerateObjCAtomicSetterBody(OMD, Ivar);
494 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000495 else {
496 // FIXME: Find a clean way to avoid AST node creation.
497 SourceLocation Loc = PD->getLocation();
498 ValueDecl *Self = OMD->getSelfDecl();
499 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
500 DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc);
501 ParmVarDecl *ArgDecl = *OMD->param_begin();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000502 QualType T = ArgDecl->getType();
503 if (T->isReferenceType())
504 T = cast<ReferenceType>(T)->getPointeeType();
505 DeclRefExpr Arg(ArgDecl, T, VK_LValue, Loc);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000506 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000507
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000508 // The property type can differ from the ivar type in some situations with
509 // Objective-C pointer types, we can always bit cast the RHS in these cases.
510 if (getContext().getCanonicalType(Ivar->getType()) !=
511 getContext().getCanonicalType(ArgDecl->getType())) {
512 ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
513 Ivar->getType(), CK_BitCast, &Arg,
514 VK_RValue);
515 BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
516 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
517 EmitStmt(&Assign);
518 } else {
519 BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
520 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
521 EmitStmt(&Assign);
522 }
Daniel Dunbar45e84232009-10-27 19:21:30 +0000523 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000524 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000525
526 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000527}
528
John McCalle81ac692011-03-22 07:05:39 +0000529// FIXME: these are stolen from CGClass.cpp, which is lame.
530namespace {
531 struct CallArrayIvarDtor : EHScopeStack::Cleanup {
532 const ObjCIvarDecl *ivar;
533 llvm::Value *self;
534 CallArrayIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
535 : ivar(ivar), self(self) {}
536
537 void Emit(CodeGenFunction &CGF, bool IsForEH) {
538 LValue lvalue =
539 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
540
541 QualType type = ivar->getType();
542 const ConstantArrayType *arrayType
543 = CGF.getContext().getAsConstantArrayType(type);
544 QualType baseType = CGF.getContext().getBaseElementType(arrayType);
545 const CXXRecordDecl *classDecl = baseType->getAsCXXRecordDecl();
546
547 llvm::Value *base
548 = CGF.Builder.CreateBitCast(lvalue.getAddress(),
549 CGF.ConvertType(baseType)->getPointerTo());
550 CGF.EmitCXXAggrDestructorCall(classDecl->getDestructor(),
551 arrayType, base);
552 }
553 };
554
555 struct CallIvarDtor : EHScopeStack::Cleanup {
556 const ObjCIvarDecl *ivar;
557 llvm::Value *self;
558 CallIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
559 : ivar(ivar), self(self) {}
560
561 void Emit(CodeGenFunction &CGF, bool IsForEH) {
562 LValue lvalue =
563 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
564
565 QualType type = ivar->getType();
566 const CXXRecordDecl *classDecl = type->getAsCXXRecordDecl();
567
568 CGF.EmitCXXDestructorCall(classDecl->getDestructor(),
569 Dtor_Complete, /*ForVirtualBase=*/false,
570 lvalue.getAddress());
571 }
572 };
573}
574
575static void emitCXXDestructMethod(CodeGenFunction &CGF,
576 ObjCImplementationDecl *impl) {
577 CodeGenFunction::RunCleanupsScope scope(CGF);
578
579 llvm::Value *self = CGF.LoadObjCSelf();
580
581 ObjCInterfaceDecl *iface
582 = const_cast<ObjCInterfaceDecl*>(impl->getClassInterface());
583 for (ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
584 ivar; ivar = ivar->getNextIvar()) {
585 QualType type = ivar->getType();
586
587 // Drill down to the base element type.
588 QualType baseType = type;
589 const ConstantArrayType *arrayType =
590 CGF.getContext().getAsConstantArrayType(baseType);
591 if (arrayType) baseType = CGF.getContext().getBaseElementType(arrayType);
592
593 // Check whether the ivar is a destructible type.
594 QualType::DestructionKind destructKind = baseType.isDestructedType();
595 assert(destructKind == type.isDestructedType());
596
597 switch (destructKind) {
598 case QualType::DK_none:
599 continue;
600
601 case QualType::DK_cxx_destructor:
602 if (arrayType)
603 CGF.EHStack.pushCleanup<CallArrayIvarDtor>(NormalAndEHCleanup,
604 ivar, self);
605 else
606 CGF.EHStack.pushCleanup<CallIvarDtor>(NormalAndEHCleanup,
607 ivar, self);
608 break;
609 }
610 }
611
612 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
613}
614
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000615void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
616 ObjCMethodDecl *MD,
617 bool ctor) {
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000618 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
619 StartObjCMethod(MD, IMP->getClassInterface());
John McCalle81ac692011-03-22 07:05:39 +0000620
621 // Emit .cxx_construct.
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000622 if (ctor) {
John McCalle81ac692011-03-22 07:05:39 +0000623 llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
624 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
625 E = IMP->init_end(); B != E; ++B) {
626 CXXCtorInitializer *IvarInit = (*B);
Francois Pichet00eb3f92010-12-04 09:14:42 +0000627 FieldDecl *Field = IvarInit->getAnyMember();
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000628 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian9b4d4fc2010-04-28 22:30:33 +0000629 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
630 LoadObjCSelf(), Ivar, 0);
John McCall558d2ab2010-09-15 10:14:12 +0000631 EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000632 }
633 // constructor returns 'self'.
634 CodeGenTypes &Types = CGM.getTypes();
635 QualType IdTy(CGM.getContext().getObjCIdType());
636 llvm::Value *SelfAsId =
637 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
638 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
John McCalle81ac692011-03-22 07:05:39 +0000639
640 // Emit .cxx_destruct.
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000641 } else {
John McCalle81ac692011-03-22 07:05:39 +0000642 emitCXXDestructMethod(*this, IMP);
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000643 }
644 FinishFunction();
645}
646
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000647bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
648 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
649 it++; it++;
650 const ABIArgInfo &AI = it->info;
651 // FIXME. Is this sufficient check?
652 return (AI.getKind() == ABIArgInfo::Indirect);
653}
654
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000655bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
656 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
657 return false;
658 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
659 return FDTTy->getDecl()->hasObjectMember();
660 return false;
661}
662
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000663llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000664 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
665 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000666}
667
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000668QualType CodeGenFunction::TypeOfSelfObject() {
669 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
670 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000671 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
672 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000673 return PTy->getPointeeType();
674}
675
John McCalle68b9842010-12-04 03:11:00 +0000676LValue
677CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
678 // This is a special l-value that just issues sends when we load or
679 // store through it.
680
681 // For certain base kinds, we need to emit the base immediately.
682 llvm::Value *Base;
683 if (E->isSuperReceiver())
684 Base = LoadObjCSelf();
685 else if (E->isClassReceiver())
686 Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
687 else
688 Base = EmitScalarExpr(E->getBase());
689 return LValue::MakePropertyRef(E, Base);
690}
691
692static RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
693 ReturnValueSlot Return,
694 QualType ResultType,
695 Selector S,
696 llvm::Value *Receiver,
697 const CallArgList &CallArgs) {
698 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000699 bool isClassMessage = OMD->isClassMethod();
700 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
John McCalle68b9842010-12-04 03:11:00 +0000701 return CGF.CGM.getObjCRuntime()
702 .GenerateMessageSendSuper(CGF, Return, ResultType,
703 S, OMD->getClassInterface(),
704 isCategoryImpl, Receiver,
705 isClassMessage, CallArgs);
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000706}
707
John McCall119a1c62010-12-04 02:32:38 +0000708RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
709 ReturnValueSlot Return) {
710 const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
Fariborz Jahanian68af13f2011-03-30 16:11:20 +0000711 QualType ResultType = E->getGetterResultType();
John McCall12f78a62010-12-02 01:19:52 +0000712 Selector S;
713 if (E->isExplicitProperty()) {
714 const ObjCPropertyDecl *Property = E->getExplicitProperty();
715 S = Property->getGetterName();
Mike Stumpb3589f42009-07-30 22:28:39 +0000716 } else {
John McCall12f78a62010-12-02 01:19:52 +0000717 const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter();
718 S = Getter->getSelector();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000719 }
John McCall12f78a62010-12-02 01:19:52 +0000720
John McCall119a1c62010-12-04 02:32:38 +0000721 llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
John McCalle68b9842010-12-04 03:11:00 +0000722
723 // Accesses to 'super' follow a different code path.
724 if (E->isSuperReceiver())
725 return GenerateMessageSendSuper(*this, Return, ResultType,
726 S, Receiver, CallArgList());
727
John McCall119a1c62010-12-04 02:32:38 +0000728 const ObjCInterfaceDecl *ReceiverClass
729 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
John McCall12f78a62010-12-02 01:19:52 +0000730 return CGM.getObjCRuntime().
731 GenerateMessageSend(*this, Return, ResultType, S,
732 Receiver, CallArgList(), ReceiverClass);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000733}
734
John McCall119a1c62010-12-04 02:32:38 +0000735void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
736 LValue Dst) {
737 const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
John McCall12f78a62010-12-02 01:19:52 +0000738 Selector S = E->getSetterSelector();
Fariborz Jahanian68af13f2011-03-30 16:11:20 +0000739 QualType ArgType = E->getSetterArgType();
740
Fariborz Jahanianb19c76e2011-02-08 22:33:23 +0000741 // FIXME. Other than scalars, AST is not adequate for setter and
742 // getter type mismatches which require conversion.
743 if (Src.isScalar()) {
744 llvm::Value *SrcVal = Src.getScalarVal();
745 QualType DstType = getContext().getCanonicalType(ArgType);
746 const llvm::Type *DstTy = ConvertType(DstType);
747 if (SrcVal->getType() != DstTy)
748 Src =
749 RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
750 }
751
John McCalle68b9842010-12-04 03:11:00 +0000752 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000753 Args.add(Src, ArgType);
John McCalle68b9842010-12-04 03:11:00 +0000754
755 llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
756 QualType ResultType = getContext().VoidTy;
757
John McCall12f78a62010-12-02 01:19:52 +0000758 if (E->isSuperReceiver()) {
John McCalle68b9842010-12-04 03:11:00 +0000759 GenerateMessageSendSuper(*this, ReturnValueSlot(),
760 ResultType, S, Receiver, Args);
John McCall12f78a62010-12-02 01:19:52 +0000761 return;
762 }
763
John McCall119a1c62010-12-04 02:32:38 +0000764 const ObjCInterfaceDecl *ReceiverClass
765 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
John McCall12f78a62010-12-02 01:19:52 +0000766
John McCall12f78a62010-12-02 01:19:52 +0000767 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
John McCalle68b9842010-12-04 03:11:00 +0000768 ResultType, S, Receiver, Args,
769 ReceiverClass);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000770}
771
Chris Lattner74391b42009-03-22 21:03:39 +0000772void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000773 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000774 CGM.getObjCRuntime().EnumerationMutationFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000776 if (!EnumerationMutationFn) {
777 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
778 return;
779 }
780
John McCall57b3b6a2011-02-22 07:16:58 +0000781 // The local variable comes into scope immediately.
782 AutoVarEmission variable = AutoVarEmission::invalid();
783 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
784 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
785
Devang Patelbcbd03a2011-01-19 01:36:36 +0000786 CGDebugInfo *DI = getDebugInfo();
787 if (DI) {
788 DI->setLocation(S.getSourceRange().getBegin());
789 DI->EmitRegionStart(Builder);
790 }
791
John McCalld88687f2011-01-07 01:49:06 +0000792 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
793 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Anders Carlssonf484c312008-08-31 02:33:12 +0000795 // Fast enumeration state.
796 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar195337d2010-02-09 02:48:28 +0000797 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson1884eb02010-05-22 17:35:42 +0000798 EmitNullInitialization(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Anders Carlssonf484c312008-08-31 02:33:12 +0000800 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000801 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
John McCalld88687f2011-01-07 01:49:06 +0000803 // Fetch the countByEnumeratingWithState:objects:count: selector.
Benjamin Kramerad468862010-03-30 11:36:44 +0000804 IdentifierInfo *II[] = {
805 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
806 &CGM.getContext().Idents.get("objects"),
807 &CGM.getContext().Idents.get("count")
808 };
809 Selector FastEnumSel =
810 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlssonf484c312008-08-31 02:33:12 +0000811
812 QualType ItemsTy =
813 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000814 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000815 ArrayType::Normal, 0);
Daniel Dunbar195337d2010-02-09 02:48:28 +0000816 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000817
John McCalld88687f2011-01-07 01:49:06 +0000818 // Emit the collection pointer.
Anders Carlssonf484c312008-08-31 02:33:12 +0000819 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000820
John McCalld88687f2011-01-07 01:49:06 +0000821 // Send it our message:
Anders Carlssonf484c312008-08-31 02:33:12 +0000822 CallArgList Args;
John McCalld88687f2011-01-07 01:49:06 +0000823
824 // The first argument is a temporary of the enumeration-state type.
Eli Friedman04c9a492011-05-02 17:57:46 +0000825 Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000826
John McCalld88687f2011-01-07 01:49:06 +0000827 // The second argument is a temporary array with space for NumItems
828 // pointers. We'll actually be loading elements from the array
829 // pointer written into the control state; this buffer is so that
830 // collections that *aren't* backed by arrays can still queue up
831 // batches of elements.
Eli Friedman04c9a492011-05-02 17:57:46 +0000832 Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000833
John McCalld88687f2011-01-07 01:49:06 +0000834 // The third argument is the capacity of that temporary array.
Anders Carlssonf484c312008-08-31 02:33:12 +0000835 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000836 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Eli Friedman04c9a492011-05-02 17:57:46 +0000837 Args.add(RValue::get(Count), getContext().UnsignedLongTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
John McCalld88687f2011-01-07 01:49:06 +0000839 // Start the enumeration.
Mike Stump1eb44332009-09-09 15:08:12 +0000840 RValue CountRV =
John McCallef072fd2010-05-22 01:48:05 +0000841 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000842 getContext().UnsignedLongTy,
843 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000844 Collection, Args);
Anders Carlssonf484c312008-08-31 02:33:12 +0000845
John McCalld88687f2011-01-07 01:49:06 +0000846 // The initial number of objects that were returned in the buffer.
847 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
John McCalld88687f2011-01-07 01:49:06 +0000849 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
850 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
Mike Stump1eb44332009-09-09 15:08:12 +0000851
John McCalld88687f2011-01-07 01:49:06 +0000852 llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000853
John McCalld88687f2011-01-07 01:49:06 +0000854 // If the limit pointer was zero to begin with, the collection is
855 // empty; skip all this.
856 Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
857 EmptyBB, LoopInitBB);
Anders Carlssonf484c312008-08-31 02:33:12 +0000858
John McCalld88687f2011-01-07 01:49:06 +0000859 // Otherwise, initialize the loop.
860 EmitBlock(LoopInitBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000861
John McCalld88687f2011-01-07 01:49:06 +0000862 // Save the initial mutations value. This is the value at an
863 // address that was written into the state object by
864 // countByEnumeratingWithState:objects:count:.
Mike Stump1eb44332009-09-09 15:08:12 +0000865 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000866 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000867 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000868 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000869
John McCalld88687f2011-01-07 01:49:06 +0000870 llvm::Value *initialMutations =
871 Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000872
John McCalld88687f2011-01-07 01:49:06 +0000873 // Start looping. This is the point we return to whenever we have a
874 // fresh, non-empty batch of objects.
875 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
876 EmitBlock(LoopBodyBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000877
John McCalld88687f2011-01-07 01:49:06 +0000878 // The current index into the buffer.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000879 llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
John McCalld88687f2011-01-07 01:49:06 +0000880 index->addIncoming(zero, LoopInitBB);
Anders Carlssonf484c312008-08-31 02:33:12 +0000881
John McCalld88687f2011-01-07 01:49:06 +0000882 // The current buffer size.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000883 llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
John McCalld88687f2011-01-07 01:49:06 +0000884 count->addIncoming(initialBufferLimit, LoopInitBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000885
John McCalld88687f2011-01-07 01:49:06 +0000886 // Check whether the mutations value has changed from where it was
887 // at start. StateMutationsPtr should actually be invariant between
888 // refreshes.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000889 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
John McCalld88687f2011-01-07 01:49:06 +0000890 llvm::Value *currentMutations
891 = Builder.CreateLoad(StateMutationsPtr, "statemutations");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000892
John McCalld88687f2011-01-07 01:49:06 +0000893 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
Dan Gohman361cf982011-03-02 22:39:34 +0000894 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000895
John McCalld88687f2011-01-07 01:49:06 +0000896 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
897 WasNotMutatedBB, WasMutatedBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000898
John McCalld88687f2011-01-07 01:49:06 +0000899 // If so, call the enumeration-mutation function.
900 EmitBlock(WasMutatedBB);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000901 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000902 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000903 ConvertType(getContext().getObjCIdType()),
904 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000905 CallArgList Args2;
Eli Friedman04c9a492011-05-02 17:57:46 +0000906 Args2.add(RValue::get(V), getContext().getObjCIdType());
Mike Stumpf5408fe2009-05-16 07:57:57 +0000907 // FIXME: We shouldn't need to get the function info here, the runtime already
908 // should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000909 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindola264ba482010-03-30 20:24:48 +0000910 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000911 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000912
John McCalld88687f2011-01-07 01:49:06 +0000913 // Otherwise, or if the mutation function returns, just continue.
914 EmitBlock(WasNotMutatedBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
John McCalld88687f2011-01-07 01:49:06 +0000916 // Initialize the element variable.
917 RunCleanupsScope elementVariableScope(*this);
John McCall57b3b6a2011-02-22 07:16:58 +0000918 bool elementIsVariable;
John McCalld88687f2011-01-07 01:49:06 +0000919 LValue elementLValue;
920 QualType elementType;
921 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
John McCall57b3b6a2011-02-22 07:16:58 +0000922 // Initialize the variable, in case it's a __block variable or something.
923 EmitAutoVarInit(variable);
John McCalld88687f2011-01-07 01:49:06 +0000924
John McCall57b3b6a2011-02-22 07:16:58 +0000925 const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
John McCalld88687f2011-01-07 01:49:06 +0000926 DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
927 VK_LValue, SourceLocation());
928 elementLValue = EmitLValue(&tempDRE);
929 elementType = D->getType();
John McCall57b3b6a2011-02-22 07:16:58 +0000930 elementIsVariable = true;
John McCalld88687f2011-01-07 01:49:06 +0000931 } else {
932 elementLValue = LValue(); // suppress warning
933 elementType = cast<Expr>(S.getElement())->getType();
John McCall57b3b6a2011-02-22 07:16:58 +0000934 elementIsVariable = false;
John McCalld88687f2011-01-07 01:49:06 +0000935 }
936 const llvm::Type *convertedElementType = ConvertType(elementType);
937
938 // Fetch the buffer out of the enumeration state.
939 // TODO: this pointer should actually be invariant between
940 // refreshes, which would help us do certain loop optimizations.
Mike Stump1eb44332009-09-09 15:08:12 +0000941 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000942 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
John McCalld88687f2011-01-07 01:49:06 +0000943 llvm::Value *EnumStateItems =
944 Builder.CreateLoad(StateItemsPtr, "stateitems");
Anders Carlssonf484c312008-08-31 02:33:12 +0000945
John McCalld88687f2011-01-07 01:49:06 +0000946 // Fetch the value at the current index from the buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000947 llvm::Value *CurrentItemPtr =
John McCalld88687f2011-01-07 01:49:06 +0000948 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
949 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000950
John McCalld88687f2011-01-07 01:49:06 +0000951 // Cast that value to the right type.
952 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
953 "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000954
John McCalld88687f2011-01-07 01:49:06 +0000955 // Make sure we have an l-value. Yes, this gets evaluated every
956 // time through the loop.
John McCall57b3b6a2011-02-22 07:16:58 +0000957 if (!elementIsVariable)
John McCalld88687f2011-01-07 01:49:06 +0000958 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000959
John McCalld88687f2011-01-07 01:49:06 +0000960 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
John McCall57b3b6a2011-02-22 07:16:58 +0000962 // If we do have an element variable, this assignment is the end of
963 // its initialization.
964 if (elementIsVariable)
965 EmitAutoVarCleanups(variable);
966
John McCalld88687f2011-01-07 01:49:06 +0000967 // Perform the loop body, setting up break and continue labels.
Anders Carlssone4b6d342009-02-10 05:52:02 +0000968 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
John McCalld88687f2011-01-07 01:49:06 +0000969 {
970 RunCleanupsScope Scope(*this);
971 EmitStmt(S.getBody());
972 }
Anders Carlssonf484c312008-08-31 02:33:12 +0000973 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000974
John McCalld88687f2011-01-07 01:49:06 +0000975 // Destroy the element variable now.
976 elementVariableScope.ForceCleanup();
977
978 // Check whether there are more elements.
John McCallff8e1152010-07-23 21:56:41 +0000979 EmitBlock(AfterBody.getBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000980
John McCalld88687f2011-01-07 01:49:06 +0000981 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000982
John McCalld88687f2011-01-07 01:49:06 +0000983 // First we check in the local buffer.
984 llvm::Value *indexPlusOne
985 = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000986
John McCalld88687f2011-01-07 01:49:06 +0000987 // If we haven't overrun the buffer yet, we can continue.
988 Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
989 LoopBodyBB, FetchMoreBB);
990
991 index->addIncoming(indexPlusOne, AfterBody.getBlock());
992 count->addIncoming(count, AfterBody.getBlock());
993
994 // Otherwise, we have to fetch more elements.
995 EmitBlock(FetchMoreBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000996
997 CountRV =
John McCallef072fd2010-05-22 01:48:05 +0000998 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000999 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +00001000 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001001 Collection, Args);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
John McCalld88687f2011-01-07 01:49:06 +00001003 // If we got a zero count, we're done.
1004 llvm::Value *refetchCount = CountRV.getScalarVal();
1005
1006 // (note that the message send might split FetchMoreBB)
1007 index->addIncoming(zero, Builder.GetInsertBlock());
1008 count->addIncoming(refetchCount, Builder.GetInsertBlock());
1009
1010 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1011 EmptyBB, LoopBodyBB);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Anders Carlssonf484c312008-08-31 02:33:12 +00001013 // No more elements.
John McCalld88687f2011-01-07 01:49:06 +00001014 EmitBlock(EmptyBB);
Anders Carlssonf484c312008-08-31 02:33:12 +00001015
John McCall57b3b6a2011-02-22 07:16:58 +00001016 if (!elementIsVariable) {
Anders Carlssonf484c312008-08-31 02:33:12 +00001017 // If the element was not a declaration, set it to be null.
1018
John McCalld88687f2011-01-07 01:49:06 +00001019 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1020 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1021 EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType);
Anders Carlssonf484c312008-08-31 02:33:12 +00001022 }
1023
Devang Patelbcbd03a2011-01-19 01:36:36 +00001024 if (DI) {
1025 DI->setLocation(S.getSourceRange().getEnd());
1026 DI->EmitRegionEnd(Builder);
1027 }
1028
John McCallff8e1152010-07-23 21:56:41 +00001029 EmitBlock(LoopEnd.getBlock());
Anders Carlsson3d8400d2008-08-30 19:51:14 +00001030}
1031
Mike Stump1eb44332009-09-09 15:08:12 +00001032void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +00001033 CGM.getObjCRuntime().EmitTryStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001034}
1035
Mike Stump1eb44332009-09-09 15:08:12 +00001036void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001037 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1038}
1039
Chris Lattner10cac6f2008-11-15 21:26:17 +00001040void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +00001041 const ObjCAtSynchronizedStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +00001042 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +00001043}
1044
Ted Kremenek2979ec72008-04-09 15:51:31 +00001045CGObjCRuntime::~CGObjCRuntime() {}