blob: 28e7e42885785c5ec4cf672b2e71bf6c0f780ba8 [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,
Devang Patel8d3f8972011-05-19 23:37:41 +0000121 const ObjCContainerDecl *CD,
122 SourceLocation StartLoc) {
John McCalld26bc762011-03-09 04:27:21 +0000123 FunctionArgList args;
Devang Patel4800ea62010-04-05 21:09:15 +0000124 // Check if we should generate debug info for this method.
Devang Patelaa112892011-03-07 18:45:56 +0000125 if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
126 DebugInfo = CGM.getModuleDebugInfo();
Devang Patel4800ea62010-04-05 21:09:15 +0000127
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000128 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000129
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000130 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
131 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000132
John McCalld26bc762011-03-09 04:27:21 +0000133 args.push_back(OMD->getSelfDecl());
134 args.push_back(OMD->getCmdDecl());
Chris Lattner41110242008-06-17 18:05:57 +0000135
Chris Lattner89951a82009-02-20 18:43:26 +0000136 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
137 E = OMD->param_end(); PI != E; ++PI)
John McCalld26bc762011-03-09 04:27:21 +0000138 args.push_back(*PI);
Chris Lattner41110242008-06-17 18:05:57 +0000139
Peter Collingbourne14110472011-01-13 18:57:25 +0000140 CurGD = OMD;
141
Devang Patel8d3f8972011-05-19 23:37:41 +0000142 StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000143}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000144
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000145void CodeGenFunction::GenerateObjCGetterBody(ObjCIvarDecl *Ivar,
146 bool IsAtomic, bool IsStrong) {
147 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
148 Ivar, 0);
149 llvm::Value *GetCopyStructFn =
150 CGM.getObjCRuntime().GetGetStructFunction();
151 CodeGenTypes &Types = CGM.getTypes();
152 // objc_copyStruct (ReturnValue, &structIvar,
153 // sizeof (Type of Ivar), isAtomic, false);
154 CallArgList Args;
John McCall0774cb82011-05-15 01:53:33 +0000155 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, VoidPtrTy));
Eli Friedman04c9a492011-05-02 17:57:46 +0000156 Args.add(RV, getContext().VoidPtrTy);
John McCall0774cb82011-05-15 01:53:33 +0000157 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), VoidPtrTy));
Eli Friedman04c9a492011-05-02 17:57:46 +0000158 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000159 // sizeof (Type of Ivar)
160 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
161 llvm::Value *SizeVal =
John McCall0774cb82011-05-15 01:53:33 +0000162 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000163 Size.getQuantity());
Eli Friedman04c9a492011-05-02 17:57:46 +0000164 Args.add(RValue::get(SizeVal), getContext().LongTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000165 llvm::Value *isAtomic =
166 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
167 IsAtomic ? 1 : 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000168 Args.add(RValue::get(isAtomic), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000169 llvm::Value *hasStrong =
170 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
171 IsStrong ? 1 : 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000172 Args.add(RValue::get(hasStrong), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000173 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
174 FunctionType::ExtInfo()),
175 GetCopyStructFn, ReturnValueSlot(), Args);
176}
177
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000178/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000179/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000180void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel8d3f8972011-05-19 23:37:41 +0000181 StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000182 EmitStmt(OMD->getBody());
183 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000184}
185
Mike Stumpf5408fe2009-05-16 07:57:57 +0000186// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
187// AST for the whole body we can just fall back to having a GenerateFunction
188// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000189
190/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000191/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
192/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000193void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
194 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000195 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000196 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000197 bool IsAtomic =
198 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000199 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
200 assert(OMD && "Invalid call to generate getter (empty method)");
Devang Patel8d3f8972011-05-19 23:37:41 +0000201 StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000202
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000203 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000204 // this. Non-atomic properties are directly evaluated.
205 // atomic 'copy' and 'retain' properties are also directly
206 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000207 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000208 IsAtomic &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000209 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
210 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000211 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000212 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000214 if (!GetPropertyFn) {
215 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
216 FinishFunction();
217 return;
218 }
219
220 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
221 // FIXME: Can't this be simpler? This might even be worse than the
222 // corresponding gcc code.
223 CodeGenTypes &Types = CGM.getTypes();
224 ValueDecl *Cmd = OMD->getCmdDecl();
225 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
226 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000227 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000228 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000229 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000230 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000231 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000232 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000233 Args.add(RValue::get(SelfAsId), IdTy);
234 Args.add(RValue::get(CmdVal), Cmd->getType());
235 Args.add(RValue::get(Offset), getContext().getPointerDiffType());
236 Args.add(RValue::get(True), getContext().BoolTy);
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000237 // FIXME: We shouldn't need to get the function info here, the
238 // runtime already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000239 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000240 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000241 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000242 // We need to fix the type here. Ivars with copy & retain are
243 // always objects so we don't need to worry about complex or
244 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000245 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000246 Types.ConvertType(PD->getType())));
247 EmitReturnOfRValue(RV, PD->getType());
248 } else {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000249 const llvm::Triple &Triple = getContext().Target.getTriple();
250 QualType IVART = Ivar->getType();
251 if (IsAtomic &&
252 IVART->isScalarType() &&
253 (Triple.getArch() == llvm::Triple::arm ||
254 Triple.getArch() == llvm::Triple::thumb) &&
255 (getContext().getTypeSizeInChars(IVART)
256 > CharUnits::fromQuantity(4)) &&
257 CGM.getObjCRuntime().GetGetStructFunction()) {
258 GenerateObjCGetterBody(Ivar, true, false);
259 }
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000260 else if (IsAtomic &&
261 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
262 Triple.getArch() == llvm::Triple::x86 &&
263 (getContext().getTypeSizeInChars(IVART)
264 > CharUnits::fromQuantity(4)) &&
265 CGM.getObjCRuntime().GetGetStructFunction()) {
266 GenerateObjCGetterBody(Ivar, true, false);
267 }
268 else if (IsAtomic &&
269 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
270 Triple.getArch() == llvm::Triple::x86_64 &&
271 (getContext().getTypeSizeInChars(IVART)
272 > CharUnits::fromQuantity(8)) &&
273 CGM.getObjCRuntime().GetGetStructFunction()) {
274 GenerateObjCGetterBody(Ivar, true, false);
275 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000276 else if (IVART->isAnyComplexType()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000277 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
278 Ivar, 0);
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000279 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
280 LV.isVolatileQualified());
281 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
282 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000283 else if (hasAggregateLLVMType(IVART)) {
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000284 bool IsStrong = false;
Fariborz Jahanian5fb65092011-04-05 23:01:27 +0000285 if ((IsStrong = IvarTypeWithAggrGCObjects(IVART))
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000286 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
David Chisnall8fac25d2010-12-26 22:13:16 +0000287 && CGM.getObjCRuntime().GetGetStructFunction()) {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000288 GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000289 }
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000290 else {
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000291 const CXXRecordDecl *classDecl = IVART->getAsCXXRecordDecl();
292
293 if (PID->getGetterCXXConstructor() &&
Sean Hunt023df372011-05-09 18:22:59 +0000294 classDecl && !classDecl->hasTrivialDefaultConstructor()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000295 ReturnStmt *Stmt =
296 new (getContext()) ReturnStmt(SourceLocation(),
Douglas Gregor5077c382010-05-15 06:01:05 +0000297 PID->getGetterCXXConstructor(),
298 0);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000299 EmitReturnStmt(*Stmt);
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000300 } else if (IsAtomic &&
301 !IVART->isAnyComplexType() &&
302 Triple.getArch() == llvm::Triple::x86 &&
303 (getContext().getTypeSizeInChars(IVART)
304 > CharUnits::fromQuantity(4)) &&
305 CGM.getObjCRuntime().GetGetStructFunction()) {
306 GenerateObjCGetterBody(Ivar, true, false);
307 }
308 else if (IsAtomic &&
309 !IVART->isAnyComplexType() &&
310 Triple.getArch() == llvm::Triple::x86_64 &&
311 (getContext().getTypeSizeInChars(IVART)
312 > CharUnits::fromQuantity(8)) &&
313 CGM.getObjCRuntime().GetGetStructFunction()) {
314 GenerateObjCGetterBody(Ivar, true, false);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000315 }
316 else {
317 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
318 Ivar, 0);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000319 EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000320 }
321 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000322 }
323 else {
324 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000325 Ivar, 0);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000326 if (PD->getType()->isReferenceType()) {
327 RValue RV = RValue::get(LV.getAddress());
328 EmitReturnOfRValue(RV, PD->getType());
329 }
330 else {
331 CodeGenTypes &Types = CGM.getTypes();
332 RValue RV = EmitLoadOfLValue(LV, IVART);
333 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000334 Types.ConvertType(PD->getType())));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000335 EmitReturnOfRValue(RV, PD->getType());
336 }
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000337 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000338 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000339
340 FinishFunction();
341}
342
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000343void CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
344 ObjCIvarDecl *Ivar) {
345 // objc_copyStruct (&structIvar, &Arg,
346 // sizeof (struct something), true, false);
347 llvm::Value *GetCopyStructFn =
348 CGM.getObjCRuntime().GetSetStructFunction();
349 CodeGenTypes &Types = CGM.getTypes();
350 CallArgList Args;
351 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
352 RValue RV =
353 RValue::get(Builder.CreateBitCast(LV.getAddress(),
354 Types.ConvertType(getContext().VoidPtrTy)));
Eli Friedman04c9a492011-05-02 17:57:46 +0000355 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000356 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
357 llvm::Value *ArgAsPtrTy =
358 Builder.CreateBitCast(Arg,
359 Types.ConvertType(getContext().VoidPtrTy));
360 RV = RValue::get(ArgAsPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +0000361 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000362 // sizeof (Type of Ivar)
363 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
364 llvm::Value *SizeVal =
365 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
366 Size.getQuantity());
Eli Friedman04c9a492011-05-02 17:57:46 +0000367 Args.add(RValue::get(SizeVal), getContext().LongTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000368 llvm::Value *True =
369 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Eli Friedman04c9a492011-05-02 17:57:46 +0000370 Args.add(RValue::get(True), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000371 llvm::Value *False =
372 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000373 Args.add(RValue::get(False), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000374 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
375 FunctionType::ExtInfo()),
376 GetCopyStructFn, ReturnValueSlot(), Args);
377}
378
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000379static bool
380IvarAssignHasTrvialAssignment(const ObjCPropertyImplDecl *PID,
381 QualType IvarT) {
382 bool HasTrvialAssignment = true;
383 if (PID->getSetterCXXAssignment()) {
384 const CXXRecordDecl *classDecl = IvarT->getAsCXXRecordDecl();
385 HasTrvialAssignment =
386 (!classDecl || classDecl->hasTrivialCopyAssignment());
387 }
388 return HasTrvialAssignment;
389}
390
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000391/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000392/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
393/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000394void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
395 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000396 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000397 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
398 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
399 assert(OMD && "Invalid call to generate setter (empty method)");
Devang Patel8d3f8972011-05-19 23:37:41 +0000400 StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000401 const llvm::Triple &Triple = getContext().Target.getTriple();
402 QualType IVART = Ivar->getType();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000403 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000404 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000405 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
406
407 // Determine if we should use an objc_setProperty call for
408 // this. Properties with 'copy' semantics always use it, as do
409 // non-atomic properties with 'release' semantics as long as we are
410 // not in gc-only mode.
411 if (IsCopy ||
412 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
413 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000414 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000415 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000417 if (!SetPropertyFn) {
418 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
419 FinishFunction();
420 return;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
423 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000424 // <is-atomic>, <is-copy>).
425 // FIXME: Can't this be simpler? This might even be worse than the
426 // corresponding gcc code.
427 CodeGenTypes &Types = CGM.getTypes();
428 ValueDecl *Cmd = OMD->getCmdDecl();
429 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
430 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000431 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000432 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000433 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000434 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000435 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000436 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
437 Types.ConvertType(IdTy));
438 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000439 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000440 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000441 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000442 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000443 Args.add(RValue::get(SelfAsId), IdTy);
444 Args.add(RValue::get(CmdVal), Cmd->getType());
445 Args.add(RValue::get(Offset), getContext().getPointerDiffType());
446 Args.add(RValue::get(ArgAsId), IdTy);
447 Args.add(RValue::get(IsAtomic ? True : False), getContext().BoolTy);
448 Args.add(RValue::get(IsCopy ? True : False), getContext().BoolTy);
Mike Stumpf5408fe2009-05-16 07:57:57 +0000449 // FIXME: We shouldn't need to get the function info here, the runtime
450 // already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000451 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000452 FunctionType::ExtInfo()),
453 SetPropertyFn,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000454 ReturnValueSlot(), Args);
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000455 } else if (IsAtomic && hasAggregateLLVMType(IVART) &&
456 !IVART->isAnyComplexType() &&
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000457 IvarAssignHasTrvialAssignment(PID, IVART) &&
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000458 ((Triple.getArch() == llvm::Triple::x86 &&
459 (getContext().getTypeSizeInChars(IVART)
460 > CharUnits::fromQuantity(4))) ||
461 (Triple.getArch() == llvm::Triple::x86_64 &&
462 (getContext().getTypeSizeInChars(IVART)
463 > CharUnits::fromQuantity(8))))
David Chisnall8fac25d2010-12-26 22:13:16 +0000464 && CGM.getObjCRuntime().GetSetStructFunction()) {
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000465 // objc_copyStruct (&structIvar, &Arg,
466 // sizeof (struct something), true, false);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000467 GenerateObjCAtomicSetterBody(OMD, Ivar);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000468 } else if (PID->getSetterCXXAssignment()) {
John McCall2a416372010-12-05 02:00:02 +0000469 EmitIgnoredExpr(PID->getSetterCXXAssignment());
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000470 } else {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000471 if (IsAtomic &&
472 IVART->isScalarType() &&
473 (Triple.getArch() == llvm::Triple::arm ||
474 Triple.getArch() == llvm::Triple::thumb) &&
475 (getContext().getTypeSizeInChars(IVART)
476 > CharUnits::fromQuantity(4)) &&
477 CGM.getObjCRuntime().GetGetStructFunction()) {
478 GenerateObjCAtomicSetterBody(OMD, Ivar);
479 }
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000480 else if (IsAtomic &&
481 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
482 Triple.getArch() == llvm::Triple::x86 &&
483 (getContext().getTypeSizeInChars(IVART)
484 > CharUnits::fromQuantity(4)) &&
485 CGM.getObjCRuntime().GetGetStructFunction()) {
486 GenerateObjCAtomicSetterBody(OMD, Ivar);
487 }
488 else if (IsAtomic &&
489 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
490 Triple.getArch() == llvm::Triple::x86_64 &&
491 (getContext().getTypeSizeInChars(IVART)
492 > CharUnits::fromQuantity(8)) &&
493 CGM.getObjCRuntime().GetGetStructFunction()) {
494 GenerateObjCAtomicSetterBody(OMD, Ivar);
495 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000496 else {
497 // FIXME: Find a clean way to avoid AST node creation.
Devang Patel8d3f8972011-05-19 23:37:41 +0000498 SourceLocation Loc = PID->getLocStart();
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000499 ValueDecl *Self = OMD->getSelfDecl();
500 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
501 DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc);
502 ParmVarDecl *ArgDecl = *OMD->param_begin();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000503 QualType T = ArgDecl->getType();
504 if (T->isReferenceType())
505 T = cast<ReferenceType>(T)->getPointeeType();
506 DeclRefExpr Arg(ArgDecl, T, VK_LValue, Loc);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000507 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000508
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000509 // The property type can differ from the ivar type in some situations with
510 // Objective-C pointer types, we can always bit cast the RHS in these cases.
511 if (getContext().getCanonicalType(Ivar->getType()) !=
512 getContext().getCanonicalType(ArgDecl->getType())) {
513 ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
514 Ivar->getType(), CK_BitCast, &Arg,
515 VK_RValue);
516 BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
517 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
518 EmitStmt(&Assign);
519 } else {
520 BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
521 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
522 EmitStmt(&Assign);
523 }
Daniel Dunbar45e84232009-10-27 19:21:30 +0000524 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000525 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000526
527 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000528}
529
John McCalle81ac692011-03-22 07:05:39 +0000530// FIXME: these are stolen from CGClass.cpp, which is lame.
531namespace {
532 struct CallArrayIvarDtor : EHScopeStack::Cleanup {
533 const ObjCIvarDecl *ivar;
534 llvm::Value *self;
535 CallArrayIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
536 : ivar(ivar), self(self) {}
537
538 void Emit(CodeGenFunction &CGF, bool IsForEH) {
539 LValue lvalue =
540 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
541
542 QualType type = ivar->getType();
543 const ConstantArrayType *arrayType
544 = CGF.getContext().getAsConstantArrayType(type);
545 QualType baseType = CGF.getContext().getBaseElementType(arrayType);
546 const CXXRecordDecl *classDecl = baseType->getAsCXXRecordDecl();
547
548 llvm::Value *base
549 = CGF.Builder.CreateBitCast(lvalue.getAddress(),
550 CGF.ConvertType(baseType)->getPointerTo());
551 CGF.EmitCXXAggrDestructorCall(classDecl->getDestructor(),
552 arrayType, base);
553 }
554 };
555
556 struct CallIvarDtor : EHScopeStack::Cleanup {
557 const ObjCIvarDecl *ivar;
558 llvm::Value *self;
559 CallIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
560 : ivar(ivar), self(self) {}
561
562 void Emit(CodeGenFunction &CGF, bool IsForEH) {
563 LValue lvalue =
564 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
565
566 QualType type = ivar->getType();
567 const CXXRecordDecl *classDecl = type->getAsCXXRecordDecl();
568
569 CGF.EmitCXXDestructorCall(classDecl->getDestructor(),
570 Dtor_Complete, /*ForVirtualBase=*/false,
571 lvalue.getAddress());
572 }
573 };
574}
575
576static void emitCXXDestructMethod(CodeGenFunction &CGF,
577 ObjCImplementationDecl *impl) {
578 CodeGenFunction::RunCleanupsScope scope(CGF);
579
580 llvm::Value *self = CGF.LoadObjCSelf();
581
582 ObjCInterfaceDecl *iface
583 = const_cast<ObjCInterfaceDecl*>(impl->getClassInterface());
584 for (ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
585 ivar; ivar = ivar->getNextIvar()) {
586 QualType type = ivar->getType();
587
588 // Drill down to the base element type.
589 QualType baseType = type;
590 const ConstantArrayType *arrayType =
591 CGF.getContext().getAsConstantArrayType(baseType);
592 if (arrayType) baseType = CGF.getContext().getBaseElementType(arrayType);
593
594 // Check whether the ivar is a destructible type.
595 QualType::DestructionKind destructKind = baseType.isDestructedType();
596 assert(destructKind == type.isDestructedType());
597
598 switch (destructKind) {
599 case QualType::DK_none:
600 continue;
601
602 case QualType::DK_cxx_destructor:
603 if (arrayType)
604 CGF.EHStack.pushCleanup<CallArrayIvarDtor>(NormalAndEHCleanup,
605 ivar, self);
606 else
607 CGF.EHStack.pushCleanup<CallIvarDtor>(NormalAndEHCleanup,
608 ivar, self);
609 break;
610 }
611 }
612
613 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
614}
615
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000616void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
617 ObjCMethodDecl *MD,
618 bool ctor) {
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000619 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
Devang Patel8d3f8972011-05-19 23:37:41 +0000620 StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart());
John McCalle81ac692011-03-22 07:05:39 +0000621
622 // Emit .cxx_construct.
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000623 if (ctor) {
John McCalle81ac692011-03-22 07:05:39 +0000624 llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
625 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
626 E = IMP->init_end(); B != E; ++B) {
627 CXXCtorInitializer *IvarInit = (*B);
Francois Pichet00eb3f92010-12-04 09:14:42 +0000628 FieldDecl *Field = IvarInit->getAnyMember();
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000629 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian9b4d4fc2010-04-28 22:30:33 +0000630 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
631 LoadObjCSelf(), Ivar, 0);
John McCall558d2ab2010-09-15 10:14:12 +0000632 EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000633 }
634 // constructor returns 'self'.
635 CodeGenTypes &Types = CGM.getTypes();
636 QualType IdTy(CGM.getContext().getObjCIdType());
637 llvm::Value *SelfAsId =
638 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
639 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
John McCalle81ac692011-03-22 07:05:39 +0000640
641 // Emit .cxx_destruct.
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000642 } else {
John McCalle81ac692011-03-22 07:05:39 +0000643 emitCXXDestructMethod(*this, IMP);
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000644 }
645 FinishFunction();
646}
647
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000648bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
649 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
650 it++; it++;
651 const ABIArgInfo &AI = it->info;
652 // FIXME. Is this sufficient check?
653 return (AI.getKind() == ABIArgInfo::Indirect);
654}
655
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000656bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
657 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
658 return false;
659 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
660 return FDTTy->getDecl()->hasObjectMember();
661 return false;
662}
663
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000664llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000665 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
666 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000667}
668
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000669QualType CodeGenFunction::TypeOfSelfObject() {
670 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
671 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000672 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
673 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000674 return PTy->getPointeeType();
675}
676
John McCalle68b9842010-12-04 03:11:00 +0000677LValue
678CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
679 // This is a special l-value that just issues sends when we load or
680 // store through it.
681
682 // For certain base kinds, we need to emit the base immediately.
683 llvm::Value *Base;
684 if (E->isSuperReceiver())
685 Base = LoadObjCSelf();
686 else if (E->isClassReceiver())
687 Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
688 else
689 Base = EmitScalarExpr(E->getBase());
690 return LValue::MakePropertyRef(E, Base);
691}
692
693static RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
694 ReturnValueSlot Return,
695 QualType ResultType,
696 Selector S,
697 llvm::Value *Receiver,
698 const CallArgList &CallArgs) {
699 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000700 bool isClassMessage = OMD->isClassMethod();
701 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
John McCalle68b9842010-12-04 03:11:00 +0000702 return CGF.CGM.getObjCRuntime()
703 .GenerateMessageSendSuper(CGF, Return, ResultType,
704 S, OMD->getClassInterface(),
705 isCategoryImpl, Receiver,
706 isClassMessage, CallArgs);
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000707}
708
John McCall119a1c62010-12-04 02:32:38 +0000709RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
710 ReturnValueSlot Return) {
711 const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
Fariborz Jahanian68af13f2011-03-30 16:11:20 +0000712 QualType ResultType = E->getGetterResultType();
John McCall12f78a62010-12-02 01:19:52 +0000713 Selector S;
714 if (E->isExplicitProperty()) {
715 const ObjCPropertyDecl *Property = E->getExplicitProperty();
716 S = Property->getGetterName();
Mike Stumpb3589f42009-07-30 22:28:39 +0000717 } else {
John McCall12f78a62010-12-02 01:19:52 +0000718 const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter();
719 S = Getter->getSelector();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000720 }
John McCall12f78a62010-12-02 01:19:52 +0000721
John McCall119a1c62010-12-04 02:32:38 +0000722 llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
John McCalle68b9842010-12-04 03:11:00 +0000723
724 // Accesses to 'super' follow a different code path.
725 if (E->isSuperReceiver())
726 return GenerateMessageSendSuper(*this, Return, ResultType,
727 S, Receiver, CallArgList());
728
John McCall119a1c62010-12-04 02:32:38 +0000729 const ObjCInterfaceDecl *ReceiverClass
730 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
John McCall12f78a62010-12-02 01:19:52 +0000731 return CGM.getObjCRuntime().
732 GenerateMessageSend(*this, Return, ResultType, S,
733 Receiver, CallArgList(), ReceiverClass);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000734}
735
John McCall119a1c62010-12-04 02:32:38 +0000736void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
737 LValue Dst) {
738 const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
John McCall12f78a62010-12-02 01:19:52 +0000739 Selector S = E->getSetterSelector();
Fariborz Jahanian68af13f2011-03-30 16:11:20 +0000740 QualType ArgType = E->getSetterArgType();
741
Fariborz Jahanianb19c76e2011-02-08 22:33:23 +0000742 // FIXME. Other than scalars, AST is not adequate for setter and
743 // getter type mismatches which require conversion.
744 if (Src.isScalar()) {
745 llvm::Value *SrcVal = Src.getScalarVal();
746 QualType DstType = getContext().getCanonicalType(ArgType);
747 const llvm::Type *DstTy = ConvertType(DstType);
748 if (SrcVal->getType() != DstTy)
749 Src =
750 RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
751 }
752
John McCalle68b9842010-12-04 03:11:00 +0000753 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000754 Args.add(Src, ArgType);
John McCalle68b9842010-12-04 03:11:00 +0000755
756 llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
757 QualType ResultType = getContext().VoidTy;
758
John McCall12f78a62010-12-02 01:19:52 +0000759 if (E->isSuperReceiver()) {
John McCalle68b9842010-12-04 03:11:00 +0000760 GenerateMessageSendSuper(*this, ReturnValueSlot(),
761 ResultType, S, Receiver, Args);
John McCall12f78a62010-12-02 01:19:52 +0000762 return;
763 }
764
John McCall119a1c62010-12-04 02:32:38 +0000765 const ObjCInterfaceDecl *ReceiverClass
766 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
John McCall12f78a62010-12-02 01:19:52 +0000767
John McCall12f78a62010-12-02 01:19:52 +0000768 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
John McCalle68b9842010-12-04 03:11:00 +0000769 ResultType, S, Receiver, Args,
770 ReceiverClass);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000771}
772
Chris Lattner74391b42009-03-22 21:03:39 +0000773void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000774 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000775 CGM.getObjCRuntime().EnumerationMutationFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000777 if (!EnumerationMutationFn) {
778 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
779 return;
780 }
781
John McCall57b3b6a2011-02-22 07:16:58 +0000782 // The local variable comes into scope immediately.
783 AutoVarEmission variable = AutoVarEmission::invalid();
784 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
785 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
786
Devang Patelbcbd03a2011-01-19 01:36:36 +0000787 CGDebugInfo *DI = getDebugInfo();
788 if (DI) {
789 DI->setLocation(S.getSourceRange().getBegin());
790 DI->EmitRegionStart(Builder);
791 }
792
John McCalld88687f2011-01-07 01:49:06 +0000793 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
794 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Anders Carlssonf484c312008-08-31 02:33:12 +0000796 // Fast enumeration state.
797 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar195337d2010-02-09 02:48:28 +0000798 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson1884eb02010-05-22 17:35:42 +0000799 EmitNullInitialization(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Anders Carlssonf484c312008-08-31 02:33:12 +0000801 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000802 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
John McCalld88687f2011-01-07 01:49:06 +0000804 // Fetch the countByEnumeratingWithState:objects:count: selector.
Benjamin Kramerad468862010-03-30 11:36:44 +0000805 IdentifierInfo *II[] = {
806 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
807 &CGM.getContext().Idents.get("objects"),
808 &CGM.getContext().Idents.get("count")
809 };
810 Selector FastEnumSel =
811 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlssonf484c312008-08-31 02:33:12 +0000812
813 QualType ItemsTy =
814 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000815 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000816 ArrayType::Normal, 0);
Daniel Dunbar195337d2010-02-09 02:48:28 +0000817 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000818
John McCalld88687f2011-01-07 01:49:06 +0000819 // Emit the collection pointer.
Anders Carlssonf484c312008-08-31 02:33:12 +0000820 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000821
John McCalld88687f2011-01-07 01:49:06 +0000822 // Send it our message:
Anders Carlssonf484c312008-08-31 02:33:12 +0000823 CallArgList Args;
John McCalld88687f2011-01-07 01:49:06 +0000824
825 // The first argument is a temporary of the enumeration-state type.
Eli Friedman04c9a492011-05-02 17:57:46 +0000826 Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000827
John McCalld88687f2011-01-07 01:49:06 +0000828 // The second argument is a temporary array with space for NumItems
829 // pointers. We'll actually be loading elements from the array
830 // pointer written into the control state; this buffer is so that
831 // collections that *aren't* backed by arrays can still queue up
832 // batches of elements.
Eli Friedman04c9a492011-05-02 17:57:46 +0000833 Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000834
John McCalld88687f2011-01-07 01:49:06 +0000835 // The third argument is the capacity of that temporary array.
Anders Carlssonf484c312008-08-31 02:33:12 +0000836 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000837 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Eli Friedman04c9a492011-05-02 17:57:46 +0000838 Args.add(RValue::get(Count), getContext().UnsignedLongTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000839
John McCalld88687f2011-01-07 01:49:06 +0000840 // Start the enumeration.
Mike Stump1eb44332009-09-09 15:08:12 +0000841 RValue CountRV =
John McCallef072fd2010-05-22 01:48:05 +0000842 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000843 getContext().UnsignedLongTy,
844 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000845 Collection, Args);
Anders Carlssonf484c312008-08-31 02:33:12 +0000846
John McCalld88687f2011-01-07 01:49:06 +0000847 // The initial number of objects that were returned in the buffer.
848 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000849
John McCalld88687f2011-01-07 01:49:06 +0000850 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
851 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
Mike Stump1eb44332009-09-09 15:08:12 +0000852
John McCalld88687f2011-01-07 01:49:06 +0000853 llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000854
John McCalld88687f2011-01-07 01:49:06 +0000855 // If the limit pointer was zero to begin with, the collection is
856 // empty; skip all this.
857 Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
858 EmptyBB, LoopInitBB);
Anders Carlssonf484c312008-08-31 02:33:12 +0000859
John McCalld88687f2011-01-07 01:49:06 +0000860 // Otherwise, initialize the loop.
861 EmitBlock(LoopInitBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000862
John McCalld88687f2011-01-07 01:49:06 +0000863 // Save the initial mutations value. This is the value at an
864 // address that was written into the state object by
865 // countByEnumeratingWithState:objects:count:.
Mike Stump1eb44332009-09-09 15:08:12 +0000866 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000867 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000868 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000869 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000870
John McCalld88687f2011-01-07 01:49:06 +0000871 llvm::Value *initialMutations =
872 Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000873
John McCalld88687f2011-01-07 01:49:06 +0000874 // Start looping. This is the point we return to whenever we have a
875 // fresh, non-empty batch of objects.
876 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
877 EmitBlock(LoopBodyBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
John McCalld88687f2011-01-07 01:49:06 +0000879 // The current index into the buffer.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000880 llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
John McCalld88687f2011-01-07 01:49:06 +0000881 index->addIncoming(zero, LoopInitBB);
Anders Carlssonf484c312008-08-31 02:33:12 +0000882
John McCalld88687f2011-01-07 01:49:06 +0000883 // The current buffer size.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000884 llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
John McCalld88687f2011-01-07 01:49:06 +0000885 count->addIncoming(initialBufferLimit, LoopInitBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000886
John McCalld88687f2011-01-07 01:49:06 +0000887 // Check whether the mutations value has changed from where it was
888 // at start. StateMutationsPtr should actually be invariant between
889 // refreshes.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000890 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
John McCalld88687f2011-01-07 01:49:06 +0000891 llvm::Value *currentMutations
892 = Builder.CreateLoad(StateMutationsPtr, "statemutations");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000893
John McCalld88687f2011-01-07 01:49:06 +0000894 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
Dan Gohman361cf982011-03-02 22:39:34 +0000895 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000896
John McCalld88687f2011-01-07 01:49:06 +0000897 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
898 WasNotMutatedBB, WasMutatedBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000899
John McCalld88687f2011-01-07 01:49:06 +0000900 // If so, call the enumeration-mutation function.
901 EmitBlock(WasMutatedBB);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000902 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000903 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000904 ConvertType(getContext().getObjCIdType()),
905 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000906 CallArgList Args2;
Eli Friedman04c9a492011-05-02 17:57:46 +0000907 Args2.add(RValue::get(V), getContext().getObjCIdType());
Mike Stumpf5408fe2009-05-16 07:57:57 +0000908 // FIXME: We shouldn't need to get the function info here, the runtime already
909 // should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000910 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindola264ba482010-03-30 20:24:48 +0000911 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000912 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
John McCalld88687f2011-01-07 01:49:06 +0000914 // Otherwise, or if the mutation function returns, just continue.
915 EmitBlock(WasNotMutatedBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
John McCalld88687f2011-01-07 01:49:06 +0000917 // Initialize the element variable.
918 RunCleanupsScope elementVariableScope(*this);
John McCall57b3b6a2011-02-22 07:16:58 +0000919 bool elementIsVariable;
John McCalld88687f2011-01-07 01:49:06 +0000920 LValue elementLValue;
921 QualType elementType;
922 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
John McCall57b3b6a2011-02-22 07:16:58 +0000923 // Initialize the variable, in case it's a __block variable or something.
924 EmitAutoVarInit(variable);
John McCalld88687f2011-01-07 01:49:06 +0000925
John McCall57b3b6a2011-02-22 07:16:58 +0000926 const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
John McCalld88687f2011-01-07 01:49:06 +0000927 DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
928 VK_LValue, SourceLocation());
929 elementLValue = EmitLValue(&tempDRE);
930 elementType = D->getType();
John McCall57b3b6a2011-02-22 07:16:58 +0000931 elementIsVariable = true;
John McCalld88687f2011-01-07 01:49:06 +0000932 } else {
933 elementLValue = LValue(); // suppress warning
934 elementType = cast<Expr>(S.getElement())->getType();
John McCall57b3b6a2011-02-22 07:16:58 +0000935 elementIsVariable = false;
John McCalld88687f2011-01-07 01:49:06 +0000936 }
937 const llvm::Type *convertedElementType = ConvertType(elementType);
938
939 // Fetch the buffer out of the enumeration state.
940 // TODO: this pointer should actually be invariant between
941 // refreshes, which would help us do certain loop optimizations.
Mike Stump1eb44332009-09-09 15:08:12 +0000942 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000943 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
John McCalld88687f2011-01-07 01:49:06 +0000944 llvm::Value *EnumStateItems =
945 Builder.CreateLoad(StateItemsPtr, "stateitems");
Anders Carlssonf484c312008-08-31 02:33:12 +0000946
John McCalld88687f2011-01-07 01:49:06 +0000947 // Fetch the value at the current index from the buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000948 llvm::Value *CurrentItemPtr =
John McCalld88687f2011-01-07 01:49:06 +0000949 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
950 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
John McCalld88687f2011-01-07 01:49:06 +0000952 // Cast that value to the right type.
953 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
954 "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000955
John McCalld88687f2011-01-07 01:49:06 +0000956 // Make sure we have an l-value. Yes, this gets evaluated every
957 // time through the loop.
John McCall57b3b6a2011-02-22 07:16:58 +0000958 if (!elementIsVariable)
John McCalld88687f2011-01-07 01:49:06 +0000959 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000960
John McCalld88687f2011-01-07 01:49:06 +0000961 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
John McCall57b3b6a2011-02-22 07:16:58 +0000963 // If we do have an element variable, this assignment is the end of
964 // its initialization.
965 if (elementIsVariable)
966 EmitAutoVarCleanups(variable);
967
John McCalld88687f2011-01-07 01:49:06 +0000968 // Perform the loop body, setting up break and continue labels.
Anders Carlssone4b6d342009-02-10 05:52:02 +0000969 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
John McCalld88687f2011-01-07 01:49:06 +0000970 {
971 RunCleanupsScope Scope(*this);
972 EmitStmt(S.getBody());
973 }
Anders Carlssonf484c312008-08-31 02:33:12 +0000974 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000975
John McCalld88687f2011-01-07 01:49:06 +0000976 // Destroy the element variable now.
977 elementVariableScope.ForceCleanup();
978
979 // Check whether there are more elements.
John McCallff8e1152010-07-23 21:56:41 +0000980 EmitBlock(AfterBody.getBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000981
John McCalld88687f2011-01-07 01:49:06 +0000982 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000983
John McCalld88687f2011-01-07 01:49:06 +0000984 // First we check in the local buffer.
985 llvm::Value *indexPlusOne
986 = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000987
John McCalld88687f2011-01-07 01:49:06 +0000988 // If we haven't overrun the buffer yet, we can continue.
989 Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
990 LoopBodyBB, FetchMoreBB);
991
992 index->addIncoming(indexPlusOne, AfterBody.getBlock());
993 count->addIncoming(count, AfterBody.getBlock());
994
995 // Otherwise, we have to fetch more elements.
996 EmitBlock(FetchMoreBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
998 CountRV =
John McCallef072fd2010-05-22 01:48:05 +0000999 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +00001000 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +00001001 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001002 Collection, Args);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
John McCalld88687f2011-01-07 01:49:06 +00001004 // If we got a zero count, we're done.
1005 llvm::Value *refetchCount = CountRV.getScalarVal();
1006
1007 // (note that the message send might split FetchMoreBB)
1008 index->addIncoming(zero, Builder.GetInsertBlock());
1009 count->addIncoming(refetchCount, Builder.GetInsertBlock());
1010
1011 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1012 EmptyBB, LoopBodyBB);
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Anders Carlssonf484c312008-08-31 02:33:12 +00001014 // No more elements.
John McCalld88687f2011-01-07 01:49:06 +00001015 EmitBlock(EmptyBB);
Anders Carlssonf484c312008-08-31 02:33:12 +00001016
John McCall57b3b6a2011-02-22 07:16:58 +00001017 if (!elementIsVariable) {
Anders Carlssonf484c312008-08-31 02:33:12 +00001018 // If the element was not a declaration, set it to be null.
1019
John McCalld88687f2011-01-07 01:49:06 +00001020 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1021 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1022 EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType);
Anders Carlssonf484c312008-08-31 02:33:12 +00001023 }
1024
Devang Patelbcbd03a2011-01-19 01:36:36 +00001025 if (DI) {
1026 DI->setLocation(S.getSourceRange().getEnd());
1027 DI->EmitRegionEnd(Builder);
1028 }
1029
John McCallff8e1152010-07-23 21:56:41 +00001030 EmitBlock(LoopEnd.getBlock());
Anders Carlsson3d8400d2008-08-30 19:51:14 +00001031}
1032
Mike Stump1eb44332009-09-09 15:08:12 +00001033void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +00001034 CGM.getObjCRuntime().EmitTryStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001035}
1036
Mike Stump1eb44332009-09-09 15:08:12 +00001037void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001038 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1039}
1040
Chris Lattner10cac6f2008-11-15 21:26:17 +00001041void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +00001042 const ObjCAtSynchronizedStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +00001043 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +00001044}
1045
Ted Kremenek2979ec72008-04-09 15:51:31 +00001046CGObjCRuntime::~CGObjCRuntime() {}