blob: c40fe939b655e96623cdb9f8ec6c23bfe7fb22fb [file] [log] [blame]
Anders Carlsson76f4a902007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlsson76f4a902007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek43e06332008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlsson76f4a902007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000019#include "clang/AST/StmtObjC.h"
Daniel Dunbarc5d33042008-09-03 00:27:26 +000020#include "clang/Basic/Diagnostic.h"
Anders Carlsson2e744e82008-08-30 19:51:14 +000021#include "llvm/ADT/STLExtras.h"
Daniel Dunbara08dff12008-09-24 04:04:31 +000022#include "llvm/Target/TargetData.h"
Anders Carlsson76f4a902007-08-21 17:43:55 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattnerb1d329d2008-06-24 17:04:18 +000026/// Emits an instance of NSConstantString representing the object.
Mike Stump11289f42009-09-09 15:08:12 +000027llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
Daniel Dunbar44b58a22008-11-25 21:53:21 +000028{
David Chisnall481e3a82010-01-23 02:40:42 +000029 llvm::Constant *C =
30 CGM.getObjCRuntime().GenerateConstantString(E->getString());
Daniel Dunbar66912a12008-08-20 00:28:19 +000031 // FIXME: This bitcast should just be made an invariant on the Runtime.
Owen Andersonade90fd2009-07-29 18:54:39 +000032 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattnerb1d329d2008-06-24 17:04:18 +000033}
34
35/// Emit a selector.
36llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
37 // Untyped selector.
38 // Note that this implementation allows for non-constant strings to be passed
39 // as arguments to @selector(). Currently, the only thing preventing this
40 // behaviour is the type checking in the front end.
Daniel Dunbar45858d22010-02-03 20:11:42 +000041 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattnerb1d329d2008-06-24 17:04:18 +000042}
43
Daniel Dunbar66912a12008-08-20 00:28:19 +000044llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
45 // FIXME: This should pass the Decl not the name.
46 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
47}
Chris Lattnerb1d329d2008-06-24 17:04:18 +000048
49
Daniel Dunbar97db84c2008-08-23 03:46:30 +000050RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattnerb1d329d2008-06-24 17:04:18 +000051 // Only the lookup mechanism and first two arguments of the method
52 // implementation vary between runtimes. We can get the receiver and
53 // arguments in generic code.
Mike Stump11289f42009-09-09 15:08:12 +000054
Daniel Dunbar8d480592008-08-11 18:12:00 +000055 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattnerb1d329d2008-06-24 17:04:18 +000056 const Expr *ReceiverExpr = E->getReceiver();
57 bool isSuperMessage = false;
Daniel Dunbarca8531a2008-08-25 08:19:24 +000058 bool isClassMessage = false;
Chris Lattnerb1d329d2008-06-24 17:04:18 +000059 // Find the receiver
60 llvm::Value *Receiver;
61 if (!ReceiverExpr) {
Douglas Gregorde4827d2010-03-08 16:40:19 +000062 const ObjCInterfaceDecl *OID = E->getClassInfo().Decl;
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +000063
64 // Very special case, super send in class method. The receiver is
65 // self (the class object) and the send uses super semantics.
66 if (!OID) {
Chris Lattner68e48682008-11-20 04:42:34 +000067 assert(E->getClassName()->isStr("super") &&
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +000068 "Unexpected missing class interface in message send.");
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +000069 isSuperMessage = true;
Daniel Dunbarca8531a2008-08-25 08:19:24 +000070 Receiver = LoadObjCSelf();
71 } else {
72 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattnerb1d329d2008-06-24 17:04:18 +000073 }
Mike Stump11289f42009-09-09 15:08:12 +000074
Daniel Dunbarca8531a2008-08-25 08:19:24 +000075 isClassMessage = true;
Chris Lattnerbd7b0a82010-04-10 19:33:39 +000076 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattnerb1d329d2008-06-24 17:04:18 +000077 isSuperMessage = true;
78 Receiver = LoadObjCSelf();
79 } else {
Daniel Dunbar5d715592008-08-12 05:28:47 +000080 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattnerb1d329d2008-06-24 17:04:18 +000081 }
82
Daniel Dunbarc722b852008-08-30 03:02:31 +000083 CallArgList Args;
Anders Carlsson623dcae2009-04-18 20:29:27 +000084 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnerb1d329d2008-06-24 17:04:18 +000086 if (isSuperMessage) {
Chris Lattner6cfec782008-06-26 04:42:20 +000087 // super is only valid in an Objective-C method
88 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000089 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +000090 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
91 E->getSelector(),
Daniel Dunbarca8531a2008-08-25 08:19:24 +000092 OMD->getClassInterface(),
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000093 isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +000094 Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +000095 isClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +000096 Args,
97 E->getMethodDecl());
Chris Lattnerb1d329d2008-06-24 17:04:18 +000098 }
Daniel Dunbaraff9fca2009-09-17 04:01:22 +000099
Mike Stump11289f42009-09-09 15:08:12 +0000100 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000101 Receiver, isClassMessage, Args,
102 E->getMethodDecl());
Anders Carlsson76f4a902007-08-21 17:43:55 +0000103}
104
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000105/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
106/// the LLVM function and sets the other context used by
107/// CodeGenFunction.
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000108void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
109 const ObjCContainerDecl *CD) {
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000110 FunctionArgList Args;
Devang Patela2c048e2010-04-05 21:09:15 +0000111 // Check if we should generate debug info for this method.
112 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
113 DebugInfo = CGM.getDebugInfo();
114
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000115 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000116
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000117 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
118 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner5696e7b2008-06-17 18:05:57 +0000119
Mike Stump11289f42009-09-09 15:08:12 +0000120 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000121 OMD->getSelfDecl()->getType()));
122 Args.push_back(std::make_pair(OMD->getCmdDecl(),
123 OMD->getCmdDecl()->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000124
Chris Lattnera4997152009-02-20 18:43:26 +0000125 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
126 E = OMD->param_end(); PI != E; ++PI)
127 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000128
Devang Patele8814ce2010-02-15 18:08:38 +0000129 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000130}
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000131
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000132/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump11289f42009-09-09 15:08:12 +0000133/// its pointer, name, and types registered in the class struture.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000134void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000135 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000136 EmitStmt(OMD->getBody());
137 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000138}
139
Mike Stump18bb9282009-05-16 07:57:57 +0000140// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
141// AST for the whole body we can just fall back to having a GenerateFunction
142// which takes the body Stmt.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000143
144/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000145/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
146/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000147void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
148 const ObjCPropertyImplDecl *PID) {
Daniel Dunbara08dff12008-09-24 04:04:31 +0000149 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000150 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000151 bool IsAtomic =
152 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000153 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
154 assert(OMD && "Invalid call to generate getter (empty method)");
Mike Stump18bb9282009-05-16 07:57:57 +0000155 // FIXME: This is rather murky, we create this here since they will not have
156 // been created by Sema for us.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000157 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000158 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000159
Daniel Dunbara08dff12008-09-24 04:04:31 +0000160 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000161 // this. Non-atomic properties are directly evaluated.
162 // atomic 'copy' and 'retain' properties are also directly
163 // evaluated in gc-only mode.
Daniel Dunbara08dff12008-09-24 04:04:31 +0000164 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000165 IsAtomic &&
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000166 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
167 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000168 llvm::Value *GetPropertyFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000169 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000170
Daniel Dunbara08dff12008-09-24 04:04:31 +0000171 if (!GetPropertyFn) {
172 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
173 FinishFunction();
174 return;
175 }
176
177 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
178 // FIXME: Can't this be simpler? This might even be worse than the
179 // corresponding gcc code.
180 CodeGenTypes &Types = CGM.getTypes();
181 ValueDecl *Cmd = OMD->getCmdDecl();
182 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
183 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000184 llvm::Value *SelfAsId =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000185 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000186 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000187 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000188 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000189 CallArgList Args;
190 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
191 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
192 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
193 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbar1ef73732009-02-03 23:43:59 +0000194 // FIXME: We shouldn't need to get the function info here, the
195 // runtime already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000196 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000197 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000198 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000199 // We need to fix the type here. Ivars with copy & retain are
200 // always objects so we don't need to worry about complex or
201 // aggregates.
Mike Stump11289f42009-09-09 15:08:12 +0000202 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbara08dff12008-09-24 04:04:31 +0000203 Types.ConvertType(PD->getType())));
204 EmitReturnOfRValue(RV, PD->getType());
205 } else {
Daniel Dunbar9ebf9512009-04-21 01:19:28 +0000206 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
Fariborz Jahanianf9c45852010-03-25 21:56:43 +0000207 if (Ivar->getType()->isAnyComplexType()) {
208 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
209 LV.isVolatileQualified());
210 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
211 }
212 else if (hasAggregateLLVMType(Ivar->getType())) {
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000213 bool IsStrong = false;
214 if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType())))
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000215 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
216 && CGM.getObjCRuntime().GetCopyStructFunction()) {
217 llvm::Value *GetCopyStructFn =
218 CGM.getObjCRuntime().GetCopyStructFunction();
219 CodeGenTypes &Types = CGM.getTypes();
220 // objc_copyStruct (ReturnValue, &structIvar,
221 // sizeof (Type of Ivar), isAtomic, false);
222 CallArgList Args;
223 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
224 Types.ConvertType(getContext().VoidPtrTy)));
225 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
226 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
227 Types.ConvertType(getContext().VoidPtrTy)));
228 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
229 // sizeof (Type of Ivar)
230 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
231 llvm::Value *SizeVal =
232 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
233 Args.push_back(std::make_pair(RValue::get(SizeVal),
234 getContext().LongTy));
235 // FIXME. Implement when Atomic is false; But when struct has
236 // gc'able data member!
237 llvm::Value *isAtomic =
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000238 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
239 IsAtomic ? 1 : 0);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000240 Args.push_back(std::make_pair(RValue::get(isAtomic),
241 getContext().BoolTy));
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000242 llvm::Value *hasStrong =
243 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
244 IsStrong ? 1 : 0);
245 Args.push_back(std::make_pair(RValue::get(hasStrong),
246 getContext().BoolTy));
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000247 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
248 FunctionType::ExtInfo()),
249 GetCopyStructFn, ReturnValueSlot(), Args);
250 }
251 else
252 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
Mike Stump658fe022009-07-30 22:28:39 +0000253 } else {
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000254 CodeGenTypes &Types = CGM.getTypes();
255 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
256 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump11289f42009-09-09 15:08:12 +0000257 Types.ConvertType(PD->getType())));
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000258 EmitReturnOfRValue(RV, PD->getType());
259 }
Daniel Dunbara08dff12008-09-24 04:04:31 +0000260 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000261
262 FinishFunction();
263}
264
265/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000266/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
267/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000268void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
269 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000270 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000271 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
272 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
273 assert(OMD && "Invalid call to generate setter (empty method)");
Mike Stump18bb9282009-05-16 07:57:57 +0000274 // FIXME: This is rather murky, we create this here since they will not have
275 // been created by Sema for us.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000276 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000277 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000278
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000279 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump11289f42009-09-09 15:08:12 +0000280 bool IsAtomic =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000281 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
282
283 // Determine if we should use an objc_setProperty call for
284 // this. Properties with 'copy' semantics always use it, as do
285 // non-atomic properties with 'release' semantics as long as we are
286 // not in gc-only mode.
287 if (IsCopy ||
288 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
289 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000290 llvm::Value *SetPropertyFn =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000291 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000292
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000293 if (!SetPropertyFn) {
294 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
295 FinishFunction();
296 return;
297 }
Mike Stump11289f42009-09-09 15:08:12 +0000298
299 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000300 // <is-atomic>, <is-copy>).
301 // FIXME: Can't this be simpler? This might even be worse than the
302 // corresponding gcc code.
303 CodeGenTypes &Types = CGM.getTypes();
304 ValueDecl *Cmd = OMD->getCmdDecl();
305 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
306 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000307 llvm::Value *SelfAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000308 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000309 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattnera4997152009-02-20 18:43:26 +0000310 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump11289f42009-09-09 15:08:12 +0000311 llvm::Value *ArgAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000312 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
313 Types.ConvertType(IdTy));
314 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000315 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000316 llvm::Value *False =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000317 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000318 CallArgList Args;
319 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
320 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
321 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
322 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump11289f42009-09-09 15:08:12 +0000323 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000324 getContext().BoolTy));
Mike Stump11289f42009-09-09 15:08:12 +0000325 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000326 getContext().BoolTy));
Mike Stump18bb9282009-05-16 07:57:57 +0000327 // FIXME: We shouldn't need to get the function info here, the runtime
328 // already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000329 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000330 FunctionType::ExtInfo()),
331 SetPropertyFn,
Anders Carlsson61a401c2009-12-24 19:25:24 +0000332 ReturnValueSlot(), Args);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000333 } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) &&
334 !Ivar->getType()->isAnyComplexType() &&
335 IndirectObjCSetterArg(*CurFnInfo)
336 && CGM.getObjCRuntime().GetCopyStructFunction()) {
337 // objc_copyStruct (&structIvar, &Arg,
338 // sizeof (struct something), true, false);
339 llvm::Value *GetCopyStructFn =
340 CGM.getObjCRuntime().GetCopyStructFunction();
341 CodeGenTypes &Types = CGM.getTypes();
342 CallArgList Args;
343 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
344 RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
345 Types.ConvertType(getContext().VoidPtrTy)));
346 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
347 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
348 llvm::Value *ArgAsPtrTy =
349 Builder.CreateBitCast(Arg,
350 Types.ConvertType(getContext().VoidPtrTy));
351 RV = RValue::get(ArgAsPtrTy);
352 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
353 // sizeof (Type of Ivar)
354 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
355 llvm::Value *SizeVal =
356 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
357 Args.push_back(std::make_pair(RValue::get(SizeVal),
358 getContext().LongTy));
359 llvm::Value *True =
360 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
361 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
362 llvm::Value *False =
363 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
364 Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
365 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
366 FunctionType::ExtInfo()),
367 GetCopyStructFn, ReturnValueSlot(), Args);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000368 } else {
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000369 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000370 SourceLocation Loc = PD->getLocation();
371 ValueDecl *Self = OMD->getSelfDecl();
372 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
373 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattnera4997152009-02-20 18:43:26 +0000374 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000375 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000376 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
377
378 // The property type can differ from the ivar type in some situations with
379 // Objective-C pointer types, we can always bit cast the RHS in these cases.
380 if (getContext().getCanonicalType(Ivar->getType()) !=
381 getContext().getCanonicalType(ArgDecl->getType())) {
382 ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
383 false);
384 BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
385 Ivar->getType(), Loc);
386 EmitStmt(&Assign);
387 } else {
388 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
389 Ivar->getType(), Loc);
390 EmitStmt(&Assign);
391 }
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000392 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000393
394 FinishFunction();
Chris Lattner5696e7b2008-06-17 18:05:57 +0000395}
396
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000397bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
398 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
399 it++; it++;
400 const ABIArgInfo &AI = it->info;
401 // FIXME. Is this sufficient check?
402 return (AI.getKind() == ABIArgInfo::Indirect);
403}
404
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000405bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
406 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
407 return false;
408 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
409 return FDTTy->getDecl()->hasObjectMember();
410 return false;
411}
412
Daniel Dunbara08dff12008-09-24 04:04:31 +0000413llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000414 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump692c6e32009-03-20 21:53:12 +0000415 // See if we need to lazily forward self inside a block literal.
416 BlockForwardSelf();
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000417 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner5696e7b2008-06-17 18:05:57 +0000418}
419
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000420QualType CodeGenFunction::TypeOfSelfObject() {
421 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
422 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000423 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
424 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000425 return PTy->getPointeeType();
426}
427
Mike Stump11289f42009-09-09 15:08:12 +0000428RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000429 const Selector &S) {
430 llvm::Value *Receiver = LoadObjCSelf();
431 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
432 bool isClassMessage = OMD->isClassMethod();
433 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000434 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000435 Exp->getType(),
436 S,
437 OMD->getClassInterface(),
438 isCategoryImpl,
439 Receiver,
440 isClassMessage,
441 CallArgList());
Mike Stump11289f42009-09-09 15:08:12 +0000442
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000443}
444
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000445RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian1a504772009-09-01 17:02:21 +0000446 Exp = Exp->IgnoreParens();
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000447 // FIXME: Split it into two separate routines.
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000448 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
449 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000450 if (isa<ObjCSuperExpr>(E->getBase()))
451 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000452 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000453 GenerateMessageSend(*this, Exp->getType(), S,
454 EmitScalarExpr(E->getBase()),
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000455 false, CallArgList());
Mike Stump658fe022009-07-30 22:28:39 +0000456 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000457 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000458 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf557d832009-01-16 01:50:29 +0000459 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000460 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000461 if (KE->getInterfaceDecl()) {
462 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000463 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000464 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000465 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump11289f42009-09-09 15:08:12 +0000466 else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000467 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000468 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000469 GenerateMessageSend(*this, Exp->getType(), S,
470 Receiver,
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000471 KE->getInterfaceDecl() != 0, CallArgList());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000472 }
Daniel Dunbar55310df2008-08-27 06:57:25 +0000473}
474
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000475void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
476 const Selector &S,
477 RValue Src) {
478 CallArgList Args;
479 llvm::Value *Receiver = LoadObjCSelf();
480 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
481 bool isClassMessage = OMD->isClassMethod();
482 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
483 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000484 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000485 Exp->getType(),
486 S,
487 OMD->getClassInterface(),
488 isCategoryImpl,
489 Receiver,
490 isClassMessage,
491 Args);
492 return;
493}
494
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000495void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000496 RValue Src) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000497 // FIXME: Split it into two separate routines.
498 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
499 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000500 if (isa<ObjCSuperExpr>(E->getBase())) {
501 EmitObjCSuperPropertySet(E, S, Src);
502 return;
Mike Stump11289f42009-09-09 15:08:12 +0000503 }
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000504 CallArgList Args;
505 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000506 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
507 EmitScalarExpr(E->getBase()),
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000508 false, Args);
Mike Stump11289f42009-09-09 15:08:12 +0000509 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000510 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000511 Selector S = E->getSetterMethod()->getSelector();
512 CallArgList Args;
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000513 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000514 if (E->getInterfaceDecl()) {
515 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000516 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000517 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000518 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian150abf22009-03-20 17:22:23 +0000519 return;
Mike Stump658fe022009-07-30 22:28:39 +0000520 } else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000521 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000522 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000523 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
524 Receiver,
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000525 E->getInterfaceDecl() != 0, Args);
Mike Stump658fe022009-07-30 22:28:39 +0000526 } else
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000527 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000528}
529
Chris Lattnerd4808922009-03-22 21:03:39 +0000530void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump11289f42009-09-09 15:08:12 +0000531 llvm::Constant *EnumerationMutationFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000532 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlsson75658592008-08-31 02:33:12 +0000533 llvm::Value *DeclAddress;
534 QualType ElementTy;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Daniel Dunbara08dff12008-09-24 04:04:31 +0000536 if (!EnumerationMutationFn) {
537 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
538 return;
539 }
540
Anders Carlsson75658592008-08-31 02:33:12 +0000541 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
542 EmitStmt(SD);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +0000543 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner529efc72009-03-28 06:33:19 +0000544 const Decl* D = SD->getSingleDecl();
Ted Kremenek3fef3572008-10-06 20:59:48 +0000545 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000546 DeclAddress = LocalDeclMap[D];
Anders Carlsson75658592008-08-31 02:33:12 +0000547 } else {
548 ElementTy = cast<Expr>(S.getElement())->getType();
549 DeclAddress = 0;
550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Anders Carlsson75658592008-08-31 02:33:12 +0000552 // Fast enumeration state.
553 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbara7566f12010-02-09 02:48:28 +0000554 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson3f35a262008-08-31 04:05:03 +0000555 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump11289f42009-09-09 15:08:12 +0000556
Anders Carlsson75658592008-08-31 02:33:12 +0000557 // Number of elements in the items array.
Anders Carlsson3f35a262008-08-31 04:05:03 +0000558 static const unsigned NumItems = 16;
Mike Stump11289f42009-09-09 15:08:12 +0000559
Anders Carlsson75658592008-08-31 02:33:12 +0000560 // Get selector
Benjamin Kramer9e649c32010-03-30 11:36:44 +0000561 IdentifierInfo *II[] = {
562 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
563 &CGM.getContext().Idents.get("objects"),
564 &CGM.getContext().Idents.get("count")
565 };
566 Selector FastEnumSel =
567 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlsson75658592008-08-31 02:33:12 +0000568
569 QualType ItemsTy =
570 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump11289f42009-09-09 15:08:12 +0000571 llvm::APInt(32, NumItems),
Anders Carlsson75658592008-08-31 02:33:12 +0000572 ArrayType::Normal, 0);
Daniel Dunbara7566f12010-02-09 02:48:28 +0000573 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000574
Anders Carlsson75658592008-08-31 02:33:12 +0000575 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump11289f42009-09-09 15:08:12 +0000576
Anders Carlsson75658592008-08-31 02:33:12 +0000577 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000578 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000579 getContext().getPointerType(StateTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000580
581 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000582 getContext().getPointerType(ItemsTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000583
Anders Carlsson75658592008-08-31 02:33:12 +0000584 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000585 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump11289f42009-09-09 15:08:12 +0000586 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000587 getContext().UnsignedLongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000588
589 RValue CountRV =
590 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000591 getContext().UnsignedLongTy,
592 FastEnumSel,
593 Collection, false, Args);
594
Daniel Dunbara7566f12010-02-09 02:48:28 +0000595 llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy,
596 "limit.ptr");
Anders Carlsson75658592008-08-31 02:33:12 +0000597 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000598
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000599 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
600 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump11289f42009-09-09 15:08:12 +0000601
Anders Carlsson75658592008-08-31 02:33:12 +0000602 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Anderson0b75f232009-07-31 20:28:54 +0000603 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlsson75658592008-08-31 02:33:12 +0000604
605 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson3f35a262008-08-31 04:05:03 +0000606 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson75658592008-08-31 02:33:12 +0000607
Anders Carlsson3f35a262008-08-31 04:05:03 +0000608 EmitBlock(SetStartMutations);
Mike Stump11289f42009-09-09 15:08:12 +0000609
Daniel Dunbara7566f12010-02-09 02:48:28 +0000610 llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy);
Mike Stump11289f42009-09-09 15:08:12 +0000611
612 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson3f35a262008-08-31 04:05:03 +0000613 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000614 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000615 "mutationsptr");
Mike Stump11289f42009-09-09 15:08:12 +0000616
617 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000618 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000619
Anders Carlsson3f35a262008-08-31 04:05:03 +0000620 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000622 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlsson75658592008-08-31 02:33:12 +0000623 EmitBlock(LoopStart);
624
Daniel Dunbara7566f12010-02-09 02:48:28 +0000625 llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy,
626 "counter.ptr");
Anders Carlsson75658592008-08-31 02:33:12 +0000627 Builder.CreateStore(Zero, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000628
629 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlsson75658592008-08-31 02:33:12 +0000630 EmitBlock(LoopBody);
631
Anders Carlsson3f35a262008-08-31 04:05:03 +0000632 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
633 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
634
Mike Stump11289f42009-09-09 15:08:12 +0000635 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000636 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000637 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000638 StartMutations,
639 "tobool");
Mike Stump11289f42009-09-09 15:08:12 +0000640
641
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000642 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
643 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump11289f42009-09-09 15:08:12 +0000644
Anders Carlsson3f35a262008-08-31 04:05:03 +0000645 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000646
Anders Carlsson3f35a262008-08-31 04:05:03 +0000647 EmitBlock(WasMutated);
648 llvm::Value *V =
Mike Stump11289f42009-09-09 15:08:12 +0000649 Builder.CreateBitCast(Collection,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000650 ConvertType(getContext().getObjCIdType()),
651 "tmp");
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000652 CallArgList Args2;
Mike Stump11289f42009-09-09 15:08:12 +0000653 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000654 getContext().getObjCIdType()));
Mike Stump18bb9282009-05-16 07:57:57 +0000655 // FIXME: We shouldn't need to get the function info here, the runtime already
656 // should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000657 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000658 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000659 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Anders Carlsson3f35a262008-08-31 04:05:03 +0000661 EmitBlock(WasNotMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000662
663 llvm::Value *StateItemsPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000664 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
665
666 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
667
668 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
669 "stateitems");
670
Mike Stump11289f42009-09-09 15:08:12 +0000671 llvm::Value *CurrentItemPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000672 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000673
Anders Carlsson75658592008-08-31 02:33:12 +0000674 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump11289f42009-09-09 15:08:12 +0000675
Anders Carlsson75658592008-08-31 02:33:12 +0000676 // Cast the item to the right type.
677 CurrentItem = Builder.CreateBitCast(CurrentItem,
678 ConvertType(ElementTy), "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000679
Anders Carlsson75658592008-08-31 02:33:12 +0000680 if (!DeclAddress) {
681 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000682
Anders Carlsson75658592008-08-31 02:33:12 +0000683 // Set the value to null.
684 Builder.CreateStore(CurrentItem, LV.getAddress());
685 } else
686 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump11289f42009-09-09 15:08:12 +0000687
Anders Carlsson75658592008-08-31 02:33:12 +0000688 // Increment the counter.
Mike Stump11289f42009-09-09 15:08:12 +0000689 Counter = Builder.CreateAdd(Counter,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000690 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlsson75658592008-08-31 02:33:12 +0000691 Builder.CreateStore(Counter, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000692
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000693 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
694 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump11289f42009-09-09 15:08:12 +0000695
Anders Carlsson33747b62009-02-10 05:52:02 +0000696 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlsson75658592008-08-31 02:33:12 +0000697
698 EmitStmt(S.getBody());
Mike Stump11289f42009-09-09 15:08:12 +0000699
Anders Carlsson75658592008-08-31 02:33:12 +0000700 BreakContinueStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000701
Anders Carlsson75658592008-08-31 02:33:12 +0000702 EmitBlock(AfterBody);
Mike Stump11289f42009-09-09 15:08:12 +0000703
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000704 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanian6e7ecc82009-01-06 18:56:31 +0000705
706 Counter = Builder.CreateLoad(CounterPtr);
707 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlsson75658592008-08-31 02:33:12 +0000708 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar56b936b2008-09-04 21:54:37 +0000709 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson75658592008-08-31 02:33:12 +0000710
711 // Fetch more elements.
712 EmitBlock(FetchMore);
Mike Stump11289f42009-09-09 15:08:12 +0000713
714 CountRV =
715 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000716 getContext().UnsignedLongTy,
Mike Stump11289f42009-09-09 15:08:12 +0000717 FastEnumSel,
Anders Carlsson75658592008-08-31 02:33:12 +0000718 Collection, false, Args);
719 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
720 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000721
Anders Carlsson75658592008-08-31 02:33:12 +0000722 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
723 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump11289f42009-09-09 15:08:12 +0000724
Anders Carlsson75658592008-08-31 02:33:12 +0000725 // No more elements.
726 EmitBlock(NoElements);
727
728 if (!DeclAddress) {
729 // If the element was not a declaration, set it to be null.
730
731 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000732
Anders Carlsson75658592008-08-31 02:33:12 +0000733 // Set the value to null.
Owen Anderson0b75f232009-07-31 20:28:54 +0000734 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlsson75658592008-08-31 02:33:12 +0000735 LV.getAddress());
736 }
737
738 EmitBlock(LoopEnd);
Anders Carlsson2e744e82008-08-30 19:51:14 +0000739}
740
Mike Stump11289f42009-09-09 15:08:12 +0000741void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000742 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000743}
744
Mike Stump11289f42009-09-09 15:08:12 +0000745void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000746 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
747}
748
Chris Lattnere132e242008-11-15 21:26:17 +0000749void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump11289f42009-09-09 15:08:12 +0000750 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000751 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattnere132e242008-11-15 21:26:17 +0000752}
753
Ted Kremenek43e06332008-04-09 15:51:31 +0000754CGObjCRuntime::~CGObjCRuntime() {}