blob: fa42cd1f36ab9a367491310ab757e84ae0a2946d [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
Douglas Gregor926df6c2011-06-11 01:09:30 +000050/// \brief Adjust the type of the result of an Objective-C message send
51/// expression when the method has a related result type.
52static RValue AdjustRelatedResultType(CodeGenFunction &CGF,
53 const Expr *E,
54 const ObjCMethodDecl *Method,
55 RValue Result) {
56 if (!Method)
57 return Result;
58 if (!Method->hasRelatedResultType() ||
59 CGF.getContext().hasSameType(E->getType(), Method->getResultType()) ||
60 !Result.isScalar())
61 return Result;
62
63 // We have applied a related result type. Cast the rvalue appropriately.
64 return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
65 CGF.ConvertType(E->getType())));
66}
Chris Lattner8fdf3282008-06-24 17:04:18 +000067
John McCallef072fd2010-05-22 01:48:05 +000068RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
69 ReturnValueSlot Return) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000070 // Only the lookup mechanism and first two arguments of the method
71 // implementation vary between runtimes. We can get the receiver and
72 // arguments in generic code.
Mike Stump1eb44332009-09-09 15:08:12 +000073
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000074 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000075 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000076 bool isClassMessage = false;
David Chisnallc6cd5fd2010-04-28 19:33:36 +000077 ObjCInterfaceDecl *OID = 0;
Chris Lattner8fdf3282008-06-24 17:04:18 +000078 // Find the receiver
Douglas Gregor926df6c2011-06-11 01:09:30 +000079 QualType ReceiverType;
Daniel Dunbar0b647a62010-04-22 03:17:06 +000080 llvm::Value *Receiver = 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +000081 switch (E->getReceiverKind()) {
82 case ObjCMessageExpr::Instance:
83 Receiver = EmitScalarExpr(E->getInstanceReceiver());
Douglas Gregor926df6c2011-06-11 01:09:30 +000084 ReceiverType = E->getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +000085 break;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000086
Douglas Gregor04badcf2010-04-21 00:45:42 +000087 case ObjCMessageExpr::Class: {
Douglas Gregor926df6c2011-06-11 01:09:30 +000088 ReceiverType = E->getClassReceiver();
89 const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
John McCall3031c632010-05-17 20:12:43 +000090 assert(ObjTy && "Invalid Objective-C class message send");
91 OID = ObjTy->getInterface();
92 assert(OID && "Invalid Objective-C class message send");
David Chisnallc6cd5fd2010-04-28 19:33:36 +000093 Receiver = Runtime.GetClass(Builder, OID);
Daniel Dunbarf56f1912008-08-25 08:19:24 +000094 isClassMessage = true;
Douglas Gregor04badcf2010-04-21 00:45:42 +000095 break;
96 }
97
98 case ObjCMessageExpr::SuperInstance:
Douglas Gregor926df6c2011-06-11 01:09:30 +000099 ReceiverType = E->getSuperType();
Chris Lattner8fdf3282008-06-24 17:04:18 +0000100 Receiver = LoadObjCSelf();
Douglas Gregor04badcf2010-04-21 00:45:42 +0000101 isSuperMessage = true;
102 break;
103
104 case ObjCMessageExpr::SuperClass:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000105 ReceiverType = E->getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +0000106 Receiver = LoadObjCSelf();
107 isSuperMessage = true;
108 isClassMessage = true;
109 break;
Chris Lattner8fdf3282008-06-24 17:04:18 +0000110 }
111
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000112 CallArgList Args;
Anders Carlsson131038e2009-04-18 20:29:27 +0000113 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Anders Carlsson7e70fb22010-06-21 20:59:55 +0000115 QualType ResultType =
116 E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType();
117
Douglas Gregor926df6c2011-06-11 01:09:30 +0000118 RValue result;
Chris Lattner8fdf3282008-06-24 17:04:18 +0000119 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +0000120 // super is only valid in an Objective-C method
121 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000122 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
124 E->getSelector(),
125 OMD->getClassInterface(),
126 isCategoryImpl,
127 Receiver,
128 isClassMessage,
129 Args,
130 E->getMethodDecl());
131 } else {
132 result = Runtime.GenerateMessageSend(*this, Return, ResultType,
133 E->getSelector(),
134 Receiver, Args, OID,
135 E->getMethodDecl());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000136 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000137
138 return AdjustRelatedResultType(*this, E, E->getMethodDecl(), result);
Anders Carlsson55085182007-08-21 17:43:55 +0000139}
140
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000141/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
142/// the LLVM function and sets the other context used by
143/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000144void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
Devang Patel8d3f8972011-05-19 23:37:41 +0000145 const ObjCContainerDecl *CD,
146 SourceLocation StartLoc) {
John McCalld26bc762011-03-09 04:27:21 +0000147 FunctionArgList args;
Devang Patel4800ea62010-04-05 21:09:15 +0000148 // Check if we should generate debug info for this method.
Devang Patelaa112892011-03-07 18:45:56 +0000149 if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
150 DebugInfo = CGM.getModuleDebugInfo();
Devang Patel4800ea62010-04-05 21:09:15 +0000151
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000152 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000153
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000154 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
155 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000156
John McCalld26bc762011-03-09 04:27:21 +0000157 args.push_back(OMD->getSelfDecl());
158 args.push_back(OMD->getCmdDecl());
Chris Lattner41110242008-06-17 18:05:57 +0000159
Chris Lattner89951a82009-02-20 18:43:26 +0000160 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
161 E = OMD->param_end(); PI != E; ++PI)
John McCalld26bc762011-03-09 04:27:21 +0000162 args.push_back(*PI);
Chris Lattner41110242008-06-17 18:05:57 +0000163
Peter Collingbourne14110472011-01-13 18:57:25 +0000164 CurGD = OMD;
165
Devang Patel8d3f8972011-05-19 23:37:41 +0000166 StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000167}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000168
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000169void CodeGenFunction::GenerateObjCGetterBody(ObjCIvarDecl *Ivar,
170 bool IsAtomic, bool IsStrong) {
171 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
172 Ivar, 0);
173 llvm::Value *GetCopyStructFn =
174 CGM.getObjCRuntime().GetGetStructFunction();
175 CodeGenTypes &Types = CGM.getTypes();
176 // objc_copyStruct (ReturnValue, &structIvar,
177 // sizeof (Type of Ivar), isAtomic, false);
178 CallArgList Args;
John McCall0774cb82011-05-15 01:53:33 +0000179 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, VoidPtrTy));
Eli Friedman04c9a492011-05-02 17:57:46 +0000180 Args.add(RV, getContext().VoidPtrTy);
John McCall0774cb82011-05-15 01:53:33 +0000181 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), VoidPtrTy));
Eli Friedman04c9a492011-05-02 17:57:46 +0000182 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000183 // sizeof (Type of Ivar)
184 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
185 llvm::Value *SizeVal =
John McCall0774cb82011-05-15 01:53:33 +0000186 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000187 Size.getQuantity());
Eli Friedman04c9a492011-05-02 17:57:46 +0000188 Args.add(RValue::get(SizeVal), getContext().LongTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000189 llvm::Value *isAtomic =
190 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
191 IsAtomic ? 1 : 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000192 Args.add(RValue::get(isAtomic), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000193 llvm::Value *hasStrong =
194 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
195 IsStrong ? 1 : 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000196 Args.add(RValue::get(hasStrong), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000197 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
198 FunctionType::ExtInfo()),
199 GetCopyStructFn, ReturnValueSlot(), Args);
200}
201
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000202/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000203/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000204void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel8d3f8972011-05-19 23:37:41 +0000205 StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000206 EmitStmt(OMD->getBody());
207 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000208}
209
Mike Stumpf5408fe2009-05-16 07:57:57 +0000210// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
211// AST for the whole body we can just fall back to having a GenerateFunction
212// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000213
214/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000215/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
216/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000217void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
218 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000219 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000220 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000221 bool IsAtomic =
222 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000223 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
224 assert(OMD && "Invalid call to generate getter (empty method)");
Devang Patel8d3f8972011-05-19 23:37:41 +0000225 StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000226
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000227 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000228 // this. Non-atomic properties are directly evaluated.
229 // atomic 'copy' and 'retain' properties are also directly
230 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000231 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000232 IsAtomic &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000233 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
234 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000235 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000236 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000238 if (!GetPropertyFn) {
239 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
240 FinishFunction();
241 return;
242 }
243
244 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
245 // FIXME: Can't this be simpler? This might even be worse than the
246 // corresponding gcc code.
247 CodeGenTypes &Types = CGM.getTypes();
248 ValueDecl *Cmd = OMD->getCmdDecl();
249 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
250 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000251 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000252 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000253 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000254 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000255 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000256 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000257 Args.add(RValue::get(SelfAsId), IdTy);
258 Args.add(RValue::get(CmdVal), Cmd->getType());
259 Args.add(RValue::get(Offset), getContext().getPointerDiffType());
260 Args.add(RValue::get(True), getContext().BoolTy);
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000261 // FIXME: We shouldn't need to get the function info here, the
262 // runtime already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000263 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000264 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000265 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000266 // We need to fix the type here. Ivars with copy & retain are
267 // always objects so we don't need to worry about complex or
268 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000269 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000270 Types.ConvertType(PD->getType())));
271 EmitReturnOfRValue(RV, PD->getType());
272 } else {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000273 const llvm::Triple &Triple = getContext().Target.getTriple();
274 QualType IVART = Ivar->getType();
275 if (IsAtomic &&
276 IVART->isScalarType() &&
277 (Triple.getArch() == llvm::Triple::arm ||
278 Triple.getArch() == llvm::Triple::thumb) &&
279 (getContext().getTypeSizeInChars(IVART)
280 > CharUnits::fromQuantity(4)) &&
281 CGM.getObjCRuntime().GetGetStructFunction()) {
282 GenerateObjCGetterBody(Ivar, true, false);
283 }
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000284 else if (IsAtomic &&
285 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
286 Triple.getArch() == llvm::Triple::x86 &&
287 (getContext().getTypeSizeInChars(IVART)
288 > CharUnits::fromQuantity(4)) &&
289 CGM.getObjCRuntime().GetGetStructFunction()) {
290 GenerateObjCGetterBody(Ivar, true, false);
291 }
292 else if (IsAtomic &&
293 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
294 Triple.getArch() == llvm::Triple::x86_64 &&
295 (getContext().getTypeSizeInChars(IVART)
296 > CharUnits::fromQuantity(8)) &&
297 CGM.getObjCRuntime().GetGetStructFunction()) {
298 GenerateObjCGetterBody(Ivar, true, false);
299 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000300 else if (IVART->isAnyComplexType()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000301 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
302 Ivar, 0);
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000303 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
304 LV.isVolatileQualified());
305 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
306 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000307 else if (hasAggregateLLVMType(IVART)) {
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000308 bool IsStrong = false;
Fariborz Jahanian5fb65092011-04-05 23:01:27 +0000309 if ((IsStrong = IvarTypeWithAggrGCObjects(IVART))
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000310 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
David Chisnall8fac25d2010-12-26 22:13:16 +0000311 && CGM.getObjCRuntime().GetGetStructFunction()) {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000312 GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000313 }
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000314 else {
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000315 const CXXRecordDecl *classDecl = IVART->getAsCXXRecordDecl();
316
317 if (PID->getGetterCXXConstructor() &&
Sean Hunt023df372011-05-09 18:22:59 +0000318 classDecl && !classDecl->hasTrivialDefaultConstructor()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000319 ReturnStmt *Stmt =
320 new (getContext()) ReturnStmt(SourceLocation(),
Douglas Gregor5077c382010-05-15 06:01:05 +0000321 PID->getGetterCXXConstructor(),
322 0);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000323 EmitReturnStmt(*Stmt);
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000324 } else if (IsAtomic &&
325 !IVART->isAnyComplexType() &&
326 Triple.getArch() == llvm::Triple::x86 &&
327 (getContext().getTypeSizeInChars(IVART)
328 > CharUnits::fromQuantity(4)) &&
329 CGM.getObjCRuntime().GetGetStructFunction()) {
330 GenerateObjCGetterBody(Ivar, true, false);
331 }
332 else if (IsAtomic &&
333 !IVART->isAnyComplexType() &&
334 Triple.getArch() == llvm::Triple::x86_64 &&
335 (getContext().getTypeSizeInChars(IVART)
336 > CharUnits::fromQuantity(8)) &&
337 CGM.getObjCRuntime().GetGetStructFunction()) {
338 GenerateObjCGetterBody(Ivar, true, false);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000339 }
340 else {
341 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
342 Ivar, 0);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000343 EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000344 }
345 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000346 }
347 else {
348 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000349 Ivar, 0);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000350 if (PD->getType()->isReferenceType()) {
351 RValue RV = RValue::get(LV.getAddress());
352 EmitReturnOfRValue(RV, PD->getType());
353 }
354 else {
355 CodeGenTypes &Types = CGM.getTypes();
356 RValue RV = EmitLoadOfLValue(LV, IVART);
357 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000358 Types.ConvertType(PD->getType())));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000359 EmitReturnOfRValue(RV, PD->getType());
360 }
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000361 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000362 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000363
364 FinishFunction();
365}
366
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000367void CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
368 ObjCIvarDecl *Ivar) {
369 // objc_copyStruct (&structIvar, &Arg,
370 // sizeof (struct something), true, false);
371 llvm::Value *GetCopyStructFn =
372 CGM.getObjCRuntime().GetSetStructFunction();
373 CodeGenTypes &Types = CGM.getTypes();
374 CallArgList Args;
375 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
376 RValue RV =
377 RValue::get(Builder.CreateBitCast(LV.getAddress(),
378 Types.ConvertType(getContext().VoidPtrTy)));
Eli Friedman04c9a492011-05-02 17:57:46 +0000379 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000380 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
381 llvm::Value *ArgAsPtrTy =
382 Builder.CreateBitCast(Arg,
383 Types.ConvertType(getContext().VoidPtrTy));
384 RV = RValue::get(ArgAsPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +0000385 Args.add(RV, getContext().VoidPtrTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000386 // sizeof (Type of Ivar)
387 CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
388 llvm::Value *SizeVal =
389 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
390 Size.getQuantity());
Eli Friedman04c9a492011-05-02 17:57:46 +0000391 Args.add(RValue::get(SizeVal), getContext().LongTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000392 llvm::Value *True =
393 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Eli Friedman04c9a492011-05-02 17:57:46 +0000394 Args.add(RValue::get(True), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000395 llvm::Value *False =
396 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Eli Friedman04c9a492011-05-02 17:57:46 +0000397 Args.add(RValue::get(False), getContext().BoolTy);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000398 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
399 FunctionType::ExtInfo()),
400 GetCopyStructFn, ReturnValueSlot(), Args);
401}
402
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000403static bool
404IvarAssignHasTrvialAssignment(const ObjCPropertyImplDecl *PID,
405 QualType IvarT) {
406 bool HasTrvialAssignment = true;
407 if (PID->getSetterCXXAssignment()) {
408 const CXXRecordDecl *classDecl = IvarT->getAsCXXRecordDecl();
409 HasTrvialAssignment =
410 (!classDecl || classDecl->hasTrivialCopyAssignment());
411 }
412 return HasTrvialAssignment;
413}
414
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000415/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000416/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
417/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000418void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
419 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000420 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000421 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
422 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
423 assert(OMD && "Invalid call to generate setter (empty method)");
Devang Patel8d3f8972011-05-19 23:37:41 +0000424 StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000425 const llvm::Triple &Triple = getContext().Target.getTriple();
426 QualType IVART = Ivar->getType();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000427 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000428 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000429 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
430
431 // Determine if we should use an objc_setProperty call for
432 // this. Properties with 'copy' semantics always use it, as do
433 // non-atomic properties with 'release' semantics as long as we are
434 // not in gc-only mode.
435 if (IsCopy ||
436 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
437 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000438 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000439 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000441 if (!SetPropertyFn) {
442 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
443 FinishFunction();
444 return;
445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
447 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000448 // <is-atomic>, <is-copy>).
449 // FIXME: Can't this be simpler? This might even be worse than the
450 // corresponding gcc code.
451 CodeGenTypes &Types = CGM.getTypes();
452 ValueDecl *Cmd = OMD->getCmdDecl();
453 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
454 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000455 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000456 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000457 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000458 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000459 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000460 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
461 Types.ConvertType(IdTy));
462 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000463 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000464 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000465 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000466 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000467 Args.add(RValue::get(SelfAsId), IdTy);
468 Args.add(RValue::get(CmdVal), Cmd->getType());
469 Args.add(RValue::get(Offset), getContext().getPointerDiffType());
470 Args.add(RValue::get(ArgAsId), IdTy);
471 Args.add(RValue::get(IsAtomic ? True : False), getContext().BoolTy);
472 Args.add(RValue::get(IsCopy ? True : False), getContext().BoolTy);
Mike Stumpf5408fe2009-05-16 07:57:57 +0000473 // FIXME: We shouldn't need to get the function info here, the runtime
474 // already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000475 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000476 FunctionType::ExtInfo()),
477 SetPropertyFn,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000478 ReturnValueSlot(), Args);
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000479 } else if (IsAtomic && hasAggregateLLVMType(IVART) &&
480 !IVART->isAnyComplexType() &&
Fariborz Jahanian01cb3072011-04-06 16:05:26 +0000481 IvarAssignHasTrvialAssignment(PID, IVART) &&
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000482 ((Triple.getArch() == llvm::Triple::x86 &&
483 (getContext().getTypeSizeInChars(IVART)
484 > CharUnits::fromQuantity(4))) ||
485 (Triple.getArch() == llvm::Triple::x86_64 &&
486 (getContext().getTypeSizeInChars(IVART)
487 > CharUnits::fromQuantity(8))))
David Chisnall8fac25d2010-12-26 22:13:16 +0000488 && CGM.getObjCRuntime().GetSetStructFunction()) {
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000489 // objc_copyStruct (&structIvar, &Arg,
490 // sizeof (struct something), true, false);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000491 GenerateObjCAtomicSetterBody(OMD, Ivar);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000492 } else if (PID->getSetterCXXAssignment()) {
John McCall2a416372010-12-05 02:00:02 +0000493 EmitIgnoredExpr(PID->getSetterCXXAssignment());
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000494 } else {
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000495 if (IsAtomic &&
496 IVART->isScalarType() &&
497 (Triple.getArch() == llvm::Triple::arm ||
498 Triple.getArch() == llvm::Triple::thumb) &&
499 (getContext().getTypeSizeInChars(IVART)
500 > CharUnits::fromQuantity(4)) &&
501 CGM.getObjCRuntime().GetGetStructFunction()) {
502 GenerateObjCAtomicSetterBody(OMD, Ivar);
503 }
Fariborz Jahanian1d3a61a2011-04-05 21:41:23 +0000504 else if (IsAtomic &&
505 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
506 Triple.getArch() == llvm::Triple::x86 &&
507 (getContext().getTypeSizeInChars(IVART)
508 > CharUnits::fromQuantity(4)) &&
509 CGM.getObjCRuntime().GetGetStructFunction()) {
510 GenerateObjCAtomicSetterBody(OMD, Ivar);
511 }
512 else if (IsAtomic &&
513 (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
514 Triple.getArch() == llvm::Triple::x86_64 &&
515 (getContext().getTypeSizeInChars(IVART)
516 > CharUnits::fromQuantity(8)) &&
517 CGM.getObjCRuntime().GetGetStructFunction()) {
518 GenerateObjCAtomicSetterBody(OMD, Ivar);
519 }
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000520 else {
521 // FIXME: Find a clean way to avoid AST node creation.
Devang Patel8d3f8972011-05-19 23:37:41 +0000522 SourceLocation Loc = PID->getLocStart();
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000523 ValueDecl *Self = OMD->getSelfDecl();
524 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
525 DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc);
526 ParmVarDecl *ArgDecl = *OMD->param_begin();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000527 QualType T = ArgDecl->getType();
528 if (T->isReferenceType())
529 T = cast<ReferenceType>(T)->getPointeeType();
530 DeclRefExpr Arg(ArgDecl, T, VK_LValue, Loc);
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000531 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000532
Fariborz Jahanian2846b972011-02-18 19:15:13 +0000533 // The property type can differ from the ivar type in some situations with
534 // Objective-C pointer types, we can always bit cast the RHS in these cases.
535 if (getContext().getCanonicalType(Ivar->getType()) !=
536 getContext().getCanonicalType(ArgDecl->getType())) {
537 ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
538 Ivar->getType(), CK_BitCast, &Arg,
539 VK_RValue);
540 BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
541 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
542 EmitStmt(&Assign);
543 } else {
544 BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
545 Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
546 EmitStmt(&Assign);
547 }
Daniel Dunbar45e84232009-10-27 19:21:30 +0000548 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000549 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000550
551 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000552}
553
John McCalle81ac692011-03-22 07:05:39 +0000554// FIXME: these are stolen from CGClass.cpp, which is lame.
555namespace {
556 struct CallArrayIvarDtor : EHScopeStack::Cleanup {
557 const ObjCIvarDecl *ivar;
558 llvm::Value *self;
559 CallArrayIvarDtor(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 ConstantArrayType *arrayType
568 = CGF.getContext().getAsConstantArrayType(type);
569 QualType baseType = CGF.getContext().getBaseElementType(arrayType);
570 const CXXRecordDecl *classDecl = baseType->getAsCXXRecordDecl();
571
572 llvm::Value *base
573 = CGF.Builder.CreateBitCast(lvalue.getAddress(),
574 CGF.ConvertType(baseType)->getPointerTo());
575 CGF.EmitCXXAggrDestructorCall(classDecl->getDestructor(),
576 arrayType, base);
577 }
578 };
579
580 struct CallIvarDtor : EHScopeStack::Cleanup {
581 const ObjCIvarDecl *ivar;
582 llvm::Value *self;
583 CallIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
584 : ivar(ivar), self(self) {}
585
586 void Emit(CodeGenFunction &CGF, bool IsForEH) {
587 LValue lvalue =
588 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
589
590 QualType type = ivar->getType();
591 const CXXRecordDecl *classDecl = type->getAsCXXRecordDecl();
592
593 CGF.EmitCXXDestructorCall(classDecl->getDestructor(),
594 Dtor_Complete, /*ForVirtualBase=*/false,
595 lvalue.getAddress());
596 }
597 };
598}
599
600static void emitCXXDestructMethod(CodeGenFunction &CGF,
601 ObjCImplementationDecl *impl) {
602 CodeGenFunction::RunCleanupsScope scope(CGF);
603
604 llvm::Value *self = CGF.LoadObjCSelf();
605
606 ObjCInterfaceDecl *iface
607 = const_cast<ObjCInterfaceDecl*>(impl->getClassInterface());
608 for (ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
609 ivar; ivar = ivar->getNextIvar()) {
610 QualType type = ivar->getType();
611
612 // Drill down to the base element type.
613 QualType baseType = type;
614 const ConstantArrayType *arrayType =
615 CGF.getContext().getAsConstantArrayType(baseType);
616 if (arrayType) baseType = CGF.getContext().getBaseElementType(arrayType);
617
618 // Check whether the ivar is a destructible type.
619 QualType::DestructionKind destructKind = baseType.isDestructedType();
620 assert(destructKind == type.isDestructedType());
621
622 switch (destructKind) {
623 case QualType::DK_none:
624 continue;
625
626 case QualType::DK_cxx_destructor:
627 if (arrayType)
628 CGF.EHStack.pushCleanup<CallArrayIvarDtor>(NormalAndEHCleanup,
629 ivar, self);
630 else
631 CGF.EHStack.pushCleanup<CallIvarDtor>(NormalAndEHCleanup,
632 ivar, self);
633 break;
634 }
635 }
636
637 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
638}
639
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000640void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
641 ObjCMethodDecl *MD,
642 bool ctor) {
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000643 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
Devang Patel8d3f8972011-05-19 23:37:41 +0000644 StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart());
John McCalle81ac692011-03-22 07:05:39 +0000645
646 // Emit .cxx_construct.
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000647 if (ctor) {
John McCalle81ac692011-03-22 07:05:39 +0000648 llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
649 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
650 E = IMP->init_end(); B != E; ++B) {
651 CXXCtorInitializer *IvarInit = (*B);
Francois Pichet00eb3f92010-12-04 09:14:42 +0000652 FieldDecl *Field = IvarInit->getAnyMember();
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000653 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian9b4d4fc2010-04-28 22:30:33 +0000654 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
655 LoadObjCSelf(), Ivar, 0);
John McCall558d2ab2010-09-15 10:14:12 +0000656 EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000657 }
658 // constructor returns 'self'.
659 CodeGenTypes &Types = CGM.getTypes();
660 QualType IdTy(CGM.getContext().getObjCIdType());
661 llvm::Value *SelfAsId =
662 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
663 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
John McCalle81ac692011-03-22 07:05:39 +0000664
665 // Emit .cxx_destruct.
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000666 } else {
John McCalle81ac692011-03-22 07:05:39 +0000667 emitCXXDestructMethod(*this, IMP);
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000668 }
669 FinishFunction();
670}
671
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000672bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
673 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
674 it++; it++;
675 const ABIArgInfo &AI = it->info;
676 // FIXME. Is this sufficient check?
677 return (AI.getKind() == ABIArgInfo::Indirect);
678}
679
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000680bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
681 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
682 return false;
683 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
684 return FDTTy->getDecl()->hasObjectMember();
685 return false;
686}
687
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000688llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000689 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
690 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000691}
692
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000693QualType CodeGenFunction::TypeOfSelfObject() {
694 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
695 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000696 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
697 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000698 return PTy->getPointeeType();
699}
700
John McCalle68b9842010-12-04 03:11:00 +0000701LValue
702CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
703 // This is a special l-value that just issues sends when we load or
704 // store through it.
705
706 // For certain base kinds, we need to emit the base immediately.
707 llvm::Value *Base;
708 if (E->isSuperReceiver())
709 Base = LoadObjCSelf();
710 else if (E->isClassReceiver())
711 Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
712 else
713 Base = EmitScalarExpr(E->getBase());
714 return LValue::MakePropertyRef(E, Base);
715}
716
717static RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
718 ReturnValueSlot Return,
719 QualType ResultType,
720 Selector S,
721 llvm::Value *Receiver,
722 const CallArgList &CallArgs) {
723 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000724 bool isClassMessage = OMD->isClassMethod();
725 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
John McCalle68b9842010-12-04 03:11:00 +0000726 return CGF.CGM.getObjCRuntime()
727 .GenerateMessageSendSuper(CGF, Return, ResultType,
728 S, OMD->getClassInterface(),
729 isCategoryImpl, Receiver,
730 isClassMessage, CallArgs);
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000731}
732
John McCall119a1c62010-12-04 02:32:38 +0000733RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
734 ReturnValueSlot Return) {
735 const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
Fariborz Jahanian68af13f2011-03-30 16:11:20 +0000736 QualType ResultType = E->getGetterResultType();
John McCall12f78a62010-12-02 01:19:52 +0000737 Selector S;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000738 const ObjCMethodDecl *method;
John McCall12f78a62010-12-02 01:19:52 +0000739 if (E->isExplicitProperty()) {
740 const ObjCPropertyDecl *Property = E->getExplicitProperty();
741 S = Property->getGetterName();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000742 method = Property->getGetterMethodDecl();
Mike Stumpb3589f42009-07-30 22:28:39 +0000743 } else {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000744 method = E->getImplicitPropertyGetter();
745 S = method->getSelector();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000746 }
John McCall12f78a62010-12-02 01:19:52 +0000747
John McCall119a1c62010-12-04 02:32:38 +0000748 llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
John McCalle68b9842010-12-04 03:11:00 +0000749
750 // Accesses to 'super' follow a different code path.
751 if (E->isSuperReceiver())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000752 return AdjustRelatedResultType(*this, E, method,
753 GenerateMessageSendSuper(*this, Return,
754 ResultType,
755 S, Receiver,
756 CallArgList()));
John McCall119a1c62010-12-04 02:32:38 +0000757 const ObjCInterfaceDecl *ReceiverClass
758 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000759 return AdjustRelatedResultType(*this, E, method,
760 CGM.getObjCRuntime().
761 GenerateMessageSend(*this, Return, ResultType, S,
762 Receiver, CallArgList(), ReceiverClass));
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000763}
764
John McCall119a1c62010-12-04 02:32:38 +0000765void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
766 LValue Dst) {
767 const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
John McCall12f78a62010-12-02 01:19:52 +0000768 Selector S = E->getSetterSelector();
Fariborz Jahanian68af13f2011-03-30 16:11:20 +0000769 QualType ArgType = E->getSetterArgType();
770
Fariborz Jahanianb19c76e2011-02-08 22:33:23 +0000771 // FIXME. Other than scalars, AST is not adequate for setter and
772 // getter type mismatches which require conversion.
773 if (Src.isScalar()) {
774 llvm::Value *SrcVal = Src.getScalarVal();
775 QualType DstType = getContext().getCanonicalType(ArgType);
776 const llvm::Type *DstTy = ConvertType(DstType);
777 if (SrcVal->getType() != DstTy)
778 Src =
779 RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
780 }
781
John McCalle68b9842010-12-04 03:11:00 +0000782 CallArgList Args;
Eli Friedman04c9a492011-05-02 17:57:46 +0000783 Args.add(Src, ArgType);
John McCalle68b9842010-12-04 03:11:00 +0000784
785 llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
786 QualType ResultType = getContext().VoidTy;
787
John McCall12f78a62010-12-02 01:19:52 +0000788 if (E->isSuperReceiver()) {
John McCalle68b9842010-12-04 03:11:00 +0000789 GenerateMessageSendSuper(*this, ReturnValueSlot(),
790 ResultType, S, Receiver, Args);
John McCall12f78a62010-12-02 01:19:52 +0000791 return;
792 }
793
John McCall119a1c62010-12-04 02:32:38 +0000794 const ObjCInterfaceDecl *ReceiverClass
795 = (E->isClassReceiver() ? E->getClassReceiver() : 0);
John McCall12f78a62010-12-02 01:19:52 +0000796
John McCall12f78a62010-12-02 01:19:52 +0000797 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
John McCalle68b9842010-12-04 03:11:00 +0000798 ResultType, S, Receiver, Args,
799 ReceiverClass);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000800}
801
Chris Lattner74391b42009-03-22 21:03:39 +0000802void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000803 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000804 CGM.getObjCRuntime().EnumerationMutationFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000806 if (!EnumerationMutationFn) {
807 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
808 return;
809 }
810
John McCall57b3b6a2011-02-22 07:16:58 +0000811 // The local variable comes into scope immediately.
812 AutoVarEmission variable = AutoVarEmission::invalid();
813 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
814 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
815
Devang Patelbcbd03a2011-01-19 01:36:36 +0000816 CGDebugInfo *DI = getDebugInfo();
817 if (DI) {
818 DI->setLocation(S.getSourceRange().getBegin());
819 DI->EmitRegionStart(Builder);
820 }
821
John McCalld88687f2011-01-07 01:49:06 +0000822 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
823 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Anders Carlssonf484c312008-08-31 02:33:12 +0000825 // Fast enumeration state.
826 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar195337d2010-02-09 02:48:28 +0000827 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson1884eb02010-05-22 17:35:42 +0000828 EmitNullInitialization(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Anders Carlssonf484c312008-08-31 02:33:12 +0000830 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000831 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000832
John McCalld88687f2011-01-07 01:49:06 +0000833 // Fetch the countByEnumeratingWithState:objects:count: selector.
Benjamin Kramerad468862010-03-30 11:36:44 +0000834 IdentifierInfo *II[] = {
835 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
836 &CGM.getContext().Idents.get("objects"),
837 &CGM.getContext().Idents.get("count")
838 };
839 Selector FastEnumSel =
840 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlssonf484c312008-08-31 02:33:12 +0000841
842 QualType ItemsTy =
843 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000844 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000845 ArrayType::Normal, 0);
Daniel Dunbar195337d2010-02-09 02:48:28 +0000846 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000847
John McCalld88687f2011-01-07 01:49:06 +0000848 // Emit the collection pointer.
Anders Carlssonf484c312008-08-31 02:33:12 +0000849 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000850
John McCalld88687f2011-01-07 01:49:06 +0000851 // Send it our message:
Anders Carlssonf484c312008-08-31 02:33:12 +0000852 CallArgList Args;
John McCalld88687f2011-01-07 01:49:06 +0000853
854 // The first argument is a temporary of the enumeration-state type.
Eli Friedman04c9a492011-05-02 17:57:46 +0000855 Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000856
John McCalld88687f2011-01-07 01:49:06 +0000857 // The second argument is a temporary array with space for NumItems
858 // pointers. We'll actually be loading elements from the array
859 // pointer written into the control state; this buffer is so that
860 // collections that *aren't* backed by arrays can still queue up
861 // batches of elements.
Eli Friedman04c9a492011-05-02 17:57:46 +0000862 Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000863
John McCalld88687f2011-01-07 01:49:06 +0000864 // The third argument is the capacity of that temporary array.
Anders Carlssonf484c312008-08-31 02:33:12 +0000865 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000866 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Eli Friedman04c9a492011-05-02 17:57:46 +0000867 Args.add(RValue::get(Count), getContext().UnsignedLongTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000868
John McCalld88687f2011-01-07 01:49:06 +0000869 // Start the enumeration.
Mike Stump1eb44332009-09-09 15:08:12 +0000870 RValue CountRV =
John McCallef072fd2010-05-22 01:48:05 +0000871 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000872 getContext().UnsignedLongTy,
873 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000874 Collection, Args);
Anders Carlssonf484c312008-08-31 02:33:12 +0000875
John McCalld88687f2011-01-07 01:49:06 +0000876 // The initial number of objects that were returned in the buffer.
877 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
John McCalld88687f2011-01-07 01:49:06 +0000879 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
880 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
Mike Stump1eb44332009-09-09 15:08:12 +0000881
John McCalld88687f2011-01-07 01:49:06 +0000882 llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000883
John McCalld88687f2011-01-07 01:49:06 +0000884 // If the limit pointer was zero to begin with, the collection is
885 // empty; skip all this.
886 Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
887 EmptyBB, LoopInitBB);
Anders Carlssonf484c312008-08-31 02:33:12 +0000888
John McCalld88687f2011-01-07 01:49:06 +0000889 // Otherwise, initialize the loop.
890 EmitBlock(LoopInitBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000891
John McCalld88687f2011-01-07 01:49:06 +0000892 // Save the initial mutations value. This is the value at an
893 // address that was written into the state object by
894 // countByEnumeratingWithState:objects:count:.
Mike Stump1eb44332009-09-09 15:08:12 +0000895 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000896 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000897 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000898 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000899
John McCalld88687f2011-01-07 01:49:06 +0000900 llvm::Value *initialMutations =
901 Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000902
John McCalld88687f2011-01-07 01:49:06 +0000903 // Start looping. This is the point we return to whenever we have a
904 // fresh, non-empty batch of objects.
905 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
906 EmitBlock(LoopBodyBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
John McCalld88687f2011-01-07 01:49:06 +0000908 // The current index into the buffer.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000909 llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
John McCalld88687f2011-01-07 01:49:06 +0000910 index->addIncoming(zero, LoopInitBB);
Anders Carlssonf484c312008-08-31 02:33:12 +0000911
John McCalld88687f2011-01-07 01:49:06 +0000912 // The current buffer size.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000913 llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
John McCalld88687f2011-01-07 01:49:06 +0000914 count->addIncoming(initialBufferLimit, LoopInitBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
John McCalld88687f2011-01-07 01:49:06 +0000916 // Check whether the mutations value has changed from where it was
917 // at start. StateMutationsPtr should actually be invariant between
918 // refreshes.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000919 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
John McCalld88687f2011-01-07 01:49:06 +0000920 llvm::Value *currentMutations
921 = Builder.CreateLoad(StateMutationsPtr, "statemutations");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000922
John McCalld88687f2011-01-07 01:49:06 +0000923 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
Dan Gohman361cf982011-03-02 22:39:34 +0000924 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000925
John McCalld88687f2011-01-07 01:49:06 +0000926 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
927 WasNotMutatedBB, WasMutatedBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
John McCalld88687f2011-01-07 01:49:06 +0000929 // If so, call the enumeration-mutation function.
930 EmitBlock(WasMutatedBB);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000931 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000932 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000933 ConvertType(getContext().getObjCIdType()),
934 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000935 CallArgList Args2;
Eli Friedman04c9a492011-05-02 17:57:46 +0000936 Args2.add(RValue::get(V), getContext().getObjCIdType());
Mike Stumpf5408fe2009-05-16 07:57:57 +0000937 // FIXME: We shouldn't need to get the function info here, the runtime already
938 // should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000939 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindola264ba482010-03-30 20:24:48 +0000940 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000941 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
John McCalld88687f2011-01-07 01:49:06 +0000943 // Otherwise, or if the mutation function returns, just continue.
944 EmitBlock(WasNotMutatedBB);
Mike Stump1eb44332009-09-09 15:08:12 +0000945
John McCalld88687f2011-01-07 01:49:06 +0000946 // Initialize the element variable.
947 RunCleanupsScope elementVariableScope(*this);
John McCall57b3b6a2011-02-22 07:16:58 +0000948 bool elementIsVariable;
John McCalld88687f2011-01-07 01:49:06 +0000949 LValue elementLValue;
950 QualType elementType;
951 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
John McCall57b3b6a2011-02-22 07:16:58 +0000952 // Initialize the variable, in case it's a __block variable or something.
953 EmitAutoVarInit(variable);
John McCalld88687f2011-01-07 01:49:06 +0000954
John McCall57b3b6a2011-02-22 07:16:58 +0000955 const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
John McCalld88687f2011-01-07 01:49:06 +0000956 DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
957 VK_LValue, SourceLocation());
958 elementLValue = EmitLValue(&tempDRE);
959 elementType = D->getType();
John McCall57b3b6a2011-02-22 07:16:58 +0000960 elementIsVariable = true;
John McCalld88687f2011-01-07 01:49:06 +0000961 } else {
962 elementLValue = LValue(); // suppress warning
963 elementType = cast<Expr>(S.getElement())->getType();
John McCall57b3b6a2011-02-22 07:16:58 +0000964 elementIsVariable = false;
John McCalld88687f2011-01-07 01:49:06 +0000965 }
966 const llvm::Type *convertedElementType = ConvertType(elementType);
967
968 // Fetch the buffer out of the enumeration state.
969 // TODO: this pointer should actually be invariant between
970 // refreshes, which would help us do certain loop optimizations.
Mike Stump1eb44332009-09-09 15:08:12 +0000971 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000972 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
John McCalld88687f2011-01-07 01:49:06 +0000973 llvm::Value *EnumStateItems =
974 Builder.CreateLoad(StateItemsPtr, "stateitems");
Anders Carlssonf484c312008-08-31 02:33:12 +0000975
John McCalld88687f2011-01-07 01:49:06 +0000976 // Fetch the value at the current index from the buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000977 llvm::Value *CurrentItemPtr =
John McCalld88687f2011-01-07 01:49:06 +0000978 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
979 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000980
John McCalld88687f2011-01-07 01:49:06 +0000981 // Cast that value to the right type.
982 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
983 "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000984
John McCalld88687f2011-01-07 01:49:06 +0000985 // Make sure we have an l-value. Yes, this gets evaluated every
986 // time through the loop.
John McCall57b3b6a2011-02-22 07:16:58 +0000987 if (!elementIsVariable)
John McCalld88687f2011-01-07 01:49:06 +0000988 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000989
John McCalld88687f2011-01-07 01:49:06 +0000990 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
John McCall57b3b6a2011-02-22 07:16:58 +0000992 // If we do have an element variable, this assignment is the end of
993 // its initialization.
994 if (elementIsVariable)
995 EmitAutoVarCleanups(variable);
996
John McCalld88687f2011-01-07 01:49:06 +0000997 // Perform the loop body, setting up break and continue labels.
Anders Carlssone4b6d342009-02-10 05:52:02 +0000998 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
John McCalld88687f2011-01-07 01:49:06 +0000999 {
1000 RunCleanupsScope Scope(*this);
1001 EmitStmt(S.getBody());
1002 }
Anders Carlssonf484c312008-08-31 02:33:12 +00001003 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +00001004
John McCalld88687f2011-01-07 01:49:06 +00001005 // Destroy the element variable now.
1006 elementVariableScope.ForceCleanup();
1007
1008 // Check whether there are more elements.
John McCallff8e1152010-07-23 21:56:41 +00001009 EmitBlock(AfterBody.getBlock());
Mike Stump1eb44332009-09-09 15:08:12 +00001010
John McCalld88687f2011-01-07 01:49:06 +00001011 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +00001012
John McCalld88687f2011-01-07 01:49:06 +00001013 // First we check in the local buffer.
1014 llvm::Value *indexPlusOne
1015 = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +00001016
John McCalld88687f2011-01-07 01:49:06 +00001017 // If we haven't overrun the buffer yet, we can continue.
1018 Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
1019 LoopBodyBB, FetchMoreBB);
1020
1021 index->addIncoming(indexPlusOne, AfterBody.getBlock());
1022 count->addIncoming(count, AfterBody.getBlock());
1023
1024 // Otherwise, we have to fetch more elements.
1025 EmitBlock(FetchMoreBB);
Mike Stump1eb44332009-09-09 15:08:12 +00001026
1027 CountRV =
John McCallef072fd2010-05-22 01:48:05 +00001028 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +00001029 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +00001030 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001031 Collection, Args);
Mike Stump1eb44332009-09-09 15:08:12 +00001032
John McCalld88687f2011-01-07 01:49:06 +00001033 // If we got a zero count, we're done.
1034 llvm::Value *refetchCount = CountRV.getScalarVal();
1035
1036 // (note that the message send might split FetchMoreBB)
1037 index->addIncoming(zero, Builder.GetInsertBlock());
1038 count->addIncoming(refetchCount, Builder.GetInsertBlock());
1039
1040 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1041 EmptyBB, LoopBodyBB);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Anders Carlssonf484c312008-08-31 02:33:12 +00001043 // No more elements.
John McCalld88687f2011-01-07 01:49:06 +00001044 EmitBlock(EmptyBB);
Anders Carlssonf484c312008-08-31 02:33:12 +00001045
John McCall57b3b6a2011-02-22 07:16:58 +00001046 if (!elementIsVariable) {
Anders Carlssonf484c312008-08-31 02:33:12 +00001047 // If the element was not a declaration, set it to be null.
1048
John McCalld88687f2011-01-07 01:49:06 +00001049 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1050 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1051 EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType);
Anders Carlssonf484c312008-08-31 02:33:12 +00001052 }
1053
Devang Patelbcbd03a2011-01-19 01:36:36 +00001054 if (DI) {
1055 DI->setLocation(S.getSourceRange().getEnd());
1056 DI->EmitRegionEnd(Builder);
1057 }
1058
John McCallff8e1152010-07-23 21:56:41 +00001059 EmitBlock(LoopEnd.getBlock());
Anders Carlsson3d8400d2008-08-30 19:51:14 +00001060}
1061
Mike Stump1eb44332009-09-09 15:08:12 +00001062void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +00001063 CGM.getObjCRuntime().EmitTryStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001064}
1065
Mike Stump1eb44332009-09-09 15:08:12 +00001066void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001067 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1068}
1069
Chris Lattner10cac6f2008-11-15 21:26:17 +00001070void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +00001071 const ObjCAtSynchronizedStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +00001072 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +00001073}
1074
Ted Kremenek2979ec72008-04-09 15:51:31 +00001075CGObjCRuntime::~CGObjCRuntime() {}