blob: 674f50234e550ea1464e14d3a5c5ade5bcbe7132 [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 bool isSuperMessage = false;
Daniel Dunbarca8531a2008-08-25 08:19:24 +000057 bool isClassMessage = false;
David Chisnall01aa4672010-04-28 19:33:36 +000058 ObjCInterfaceDecl *OID = 0;
Chris Lattnerb1d329d2008-06-24 17:04:18 +000059 // Find the receiver
Daniel Dunbarb2197802010-04-22 03:17:06 +000060 llvm::Value *Receiver = 0;
Douglas Gregor9a129192010-04-21 00:45:42 +000061 switch (E->getReceiverKind()) {
62 case ObjCMessageExpr::Instance:
63 Receiver = EmitScalarExpr(E->getInstanceReceiver());
64 break;
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +000065
Douglas Gregor9a129192010-04-21 00:45:42 +000066 case ObjCMessageExpr::Class: {
John McCall3e294922010-05-17 20:12:43 +000067 const ObjCObjectType *ObjTy
68 = E->getClassReceiver()->getAs<ObjCObjectType>();
69 assert(ObjTy && "Invalid Objective-C class message send");
70 OID = ObjTy->getInterface();
71 assert(OID && "Invalid Objective-C class message send");
David Chisnall01aa4672010-04-28 19:33:36 +000072 Receiver = Runtime.GetClass(Builder, OID);
Daniel Dunbarca8531a2008-08-25 08:19:24 +000073 isClassMessage = true;
Douglas Gregor9a129192010-04-21 00:45:42 +000074 break;
75 }
76
77 case ObjCMessageExpr::SuperInstance:
Chris Lattnerb1d329d2008-06-24 17:04:18 +000078 Receiver = LoadObjCSelf();
Douglas Gregor9a129192010-04-21 00:45:42 +000079 isSuperMessage = true;
80 break;
81
82 case ObjCMessageExpr::SuperClass:
83 Receiver = LoadObjCSelf();
84 isSuperMessage = true;
85 isClassMessage = true;
86 break;
Chris Lattnerb1d329d2008-06-24 17:04:18 +000087 }
88
Daniel Dunbarc722b852008-08-30 03:02:31 +000089 CallArgList Args;
Anders Carlsson623dcae2009-04-18 20:29:27 +000090 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattnerb1d329d2008-06-24 17:04:18 +000092 if (isSuperMessage) {
Chris Lattner6cfec782008-06-26 04:42:20 +000093 // super is only valid in an Objective-C method
94 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000095 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +000096 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
97 E->getSelector(),
Daniel Dunbarca8531a2008-08-25 08:19:24 +000098 OMD->getClassInterface(),
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000099 isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000100 Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000101 isClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000102 Args,
103 E->getMethodDecl());
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000104 }
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000105
Mike Stump11289f42009-09-09 15:08:12 +0000106 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
David Chisnall01aa4672010-04-28 19:33:36 +0000107 Receiver, Args, OID,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000108 E->getMethodDecl());
Anders Carlsson76f4a902007-08-21 17:43:55 +0000109}
110
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000111/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
112/// the LLVM function and sets the other context used by
113/// CodeGenFunction.
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000114void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
115 const ObjCContainerDecl *CD) {
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000116 FunctionArgList Args;
Devang Patela2c048e2010-04-05 21:09:15 +0000117 // Check if we should generate debug info for this method.
118 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
119 DebugInfo = CGM.getDebugInfo();
120
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000121 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000122
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000123 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
124 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner5696e7b2008-06-17 18:05:57 +0000125
Mike Stump11289f42009-09-09 15:08:12 +0000126 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000127 OMD->getSelfDecl()->getType()));
128 Args.push_back(std::make_pair(OMD->getCmdDecl(),
129 OMD->getCmdDecl()->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000130
Chris Lattnera4997152009-02-20 18:43:26 +0000131 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
132 E = OMD->param_end(); PI != E; ++PI)
133 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000134
Devang Patele8814ce2010-02-15 18:08:38 +0000135 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000136}
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000137
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000138/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump11289f42009-09-09 15:08:12 +0000139/// its pointer, name, and types registered in the class struture.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000140void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000141 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000142 EmitStmt(OMD->getBody());
143 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000144}
145
Mike Stump18bb9282009-05-16 07:57:57 +0000146// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
147// AST for the whole body we can just fall back to having a GenerateFunction
148// which takes the body Stmt.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000149
150/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000151/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
152/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000153void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
154 const ObjCPropertyImplDecl *PID) {
Daniel Dunbara08dff12008-09-24 04:04:31 +0000155 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000156 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000157 bool IsAtomic =
158 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000159 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
160 assert(OMD && "Invalid call to generate getter (empty method)");
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000161 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000162
Daniel Dunbara08dff12008-09-24 04:04:31 +0000163 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000164 // this. Non-atomic properties are directly evaluated.
165 // atomic 'copy' and 'retain' properties are also directly
166 // evaluated in gc-only mode.
Daniel Dunbara08dff12008-09-24 04:04:31 +0000167 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000168 IsAtomic &&
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000169 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
170 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000171 llvm::Value *GetPropertyFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000172 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000173
Daniel Dunbara08dff12008-09-24 04:04:31 +0000174 if (!GetPropertyFn) {
175 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
176 FinishFunction();
177 return;
178 }
179
180 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
181 // FIXME: Can't this be simpler? This might even be worse than the
182 // corresponding gcc code.
183 CodeGenTypes &Types = CGM.getTypes();
184 ValueDecl *Cmd = OMD->getCmdDecl();
185 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
186 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000187 llvm::Value *SelfAsId =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000188 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000189 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000190 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000191 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000192 CallArgList Args;
193 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
194 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
195 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
196 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbar1ef73732009-02-03 23:43:59 +0000197 // FIXME: We shouldn't need to get the function info here, the
198 // runtime already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000199 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000200 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000201 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000202 // We need to fix the type here. Ivars with copy & retain are
203 // always objects so we don't need to worry about complex or
204 // aggregates.
Mike Stump11289f42009-09-09 15:08:12 +0000205 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbara08dff12008-09-24 04:04:31 +0000206 Types.ConvertType(PD->getType())));
207 EmitReturnOfRValue(RV, PD->getType());
208 } else {
Fariborz Jahanianf9c45852010-03-25 21:56:43 +0000209 if (Ivar->getType()->isAnyComplexType()) {
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000210 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
211 Ivar, 0);
Fariborz Jahanianf9c45852010-03-25 21:56:43 +0000212 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
213 LV.isVolatileQualified());
214 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
215 }
216 else if (hasAggregateLLVMType(Ivar->getType())) {
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000217 bool IsStrong = false;
218 if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType())))
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000219 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
220 && CGM.getObjCRuntime().GetCopyStructFunction()) {
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000221 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
222 Ivar, 0);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000223 llvm::Value *GetCopyStructFn =
224 CGM.getObjCRuntime().GetCopyStructFunction();
225 CodeGenTypes &Types = CGM.getTypes();
226 // objc_copyStruct (ReturnValue, &structIvar,
227 // sizeof (Type of Ivar), isAtomic, false);
228 CallArgList Args;
229 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
230 Types.ConvertType(getContext().VoidPtrTy)));
231 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
232 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
233 Types.ConvertType(getContext().VoidPtrTy)));
234 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
235 // sizeof (Type of Ivar)
236 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
237 llvm::Value *SizeVal =
238 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
239 Args.push_back(std::make_pair(RValue::get(SizeVal),
240 getContext().LongTy));
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000241 llvm::Value *isAtomic =
Fariborz Jahanian5e575c02010-04-13 18:43:37 +0000242 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
243 IsAtomic ? 1 : 0);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000244 Args.push_back(std::make_pair(RValue::get(isAtomic),
245 getContext().BoolTy));
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000246 llvm::Value *hasStrong =
247 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
248 IsStrong ? 1 : 0);
249 Args.push_back(std::make_pair(RValue::get(hasStrong),
250 getContext().BoolTy));
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000251 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
252 FunctionType::ExtInfo()),
253 GetCopyStructFn, ReturnValueSlot(), Args);
254 }
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000255 else {
256 if (PID->getGetterCXXConstructor()) {
257 ReturnStmt *Stmt =
258 new (getContext()) ReturnStmt(SourceLocation(),
Douglas Gregor6fd1b182010-05-15 06:01:05 +0000259 PID->getGetterCXXConstructor(),
260 0);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000261 EmitReturnStmt(*Stmt);
262 }
263 else {
264 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
265 Ivar, 0);
266 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
267 }
268 }
Mike Stump658fe022009-07-30 22:28:39 +0000269 } else {
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000270 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
271 Ivar, 0);
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000272 CodeGenTypes &Types = CGM.getTypes();
273 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
274 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump11289f42009-09-09 15:08:12 +0000275 Types.ConvertType(PD->getType())));
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000276 EmitReturnOfRValue(RV, PD->getType());
277 }
Daniel Dunbara08dff12008-09-24 04:04:31 +0000278 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000279
280 FinishFunction();
281}
282
283/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000284/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
285/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000286void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
287 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000288 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000289 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
290 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
291 assert(OMD && "Invalid call to generate setter (empty method)");
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000292 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000293
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000294 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump11289f42009-09-09 15:08:12 +0000295 bool IsAtomic =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000296 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
297
298 // Determine if we should use an objc_setProperty call for
299 // this. Properties with 'copy' semantics always use it, as do
300 // non-atomic properties with 'release' semantics as long as we are
301 // not in gc-only mode.
302 if (IsCopy ||
303 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
304 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000305 llvm::Value *SetPropertyFn =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000306 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000307
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000308 if (!SetPropertyFn) {
309 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
310 FinishFunction();
311 return;
312 }
Mike Stump11289f42009-09-09 15:08:12 +0000313
314 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000315 // <is-atomic>, <is-copy>).
316 // FIXME: Can't this be simpler? This might even be worse than the
317 // corresponding gcc code.
318 CodeGenTypes &Types = CGM.getTypes();
319 ValueDecl *Cmd = OMD->getCmdDecl();
320 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
321 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000322 llvm::Value *SelfAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000323 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000324 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattnera4997152009-02-20 18:43:26 +0000325 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump11289f42009-09-09 15:08:12 +0000326 llvm::Value *ArgAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000327 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
328 Types.ConvertType(IdTy));
329 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000330 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000331 llvm::Value *False =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000332 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000333 CallArgList Args;
334 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
335 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
336 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
337 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump11289f42009-09-09 15:08:12 +0000338 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000339 getContext().BoolTy));
Mike Stump11289f42009-09-09 15:08:12 +0000340 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000341 getContext().BoolTy));
Mike Stump18bb9282009-05-16 07:57:57 +0000342 // FIXME: We shouldn't need to get the function info here, the runtime
343 // already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000344 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000345 FunctionType::ExtInfo()),
346 SetPropertyFn,
Anders Carlsson61a401c2009-12-24 19:25:24 +0000347 ReturnValueSlot(), Args);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000348 } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) &&
349 !Ivar->getType()->isAnyComplexType() &&
350 IndirectObjCSetterArg(*CurFnInfo)
351 && CGM.getObjCRuntime().GetCopyStructFunction()) {
352 // objc_copyStruct (&structIvar, &Arg,
353 // sizeof (struct something), true, false);
354 llvm::Value *GetCopyStructFn =
355 CGM.getObjCRuntime().GetCopyStructFunction();
356 CodeGenTypes &Types = CGM.getTypes();
357 CallArgList Args;
358 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
359 RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
360 Types.ConvertType(getContext().VoidPtrTy)));
361 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
362 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
363 llvm::Value *ArgAsPtrTy =
364 Builder.CreateBitCast(Arg,
365 Types.ConvertType(getContext().VoidPtrTy));
366 RV = RValue::get(ArgAsPtrTy);
367 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
368 // sizeof (Type of Ivar)
369 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
370 llvm::Value *SizeVal =
371 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
372 Args.push_back(std::make_pair(RValue::get(SizeVal),
373 getContext().LongTy));
374 llvm::Value *True =
375 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
376 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
377 llvm::Value *False =
378 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
379 Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
380 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
381 FunctionType::ExtInfo()),
382 GetCopyStructFn, ReturnValueSlot(), Args);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000383 } else if (PID->getSetterCXXAssignment()) {
384 EmitAnyExpr(PID->getSetterCXXAssignment(), (llvm::Value *)0, false, true,
385 false);
386
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000387 } else {
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000388 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000389 SourceLocation Loc = PD->getLocation();
390 ValueDecl *Self = OMD->getSelfDecl();
391 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
392 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattnera4997152009-02-20 18:43:26 +0000393 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000394 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000395 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
396
397 // The property type can differ from the ivar type in some situations with
398 // Objective-C pointer types, we can always bit cast the RHS in these cases.
399 if (getContext().getCanonicalType(Ivar->getType()) !=
400 getContext().getCanonicalType(ArgDecl->getType())) {
401 ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
Anders Carlsson0c509ee2010-04-24 16:57:13 +0000402 CXXBaseSpecifierArray(), false);
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000403 BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
404 Ivar->getType(), Loc);
405 EmitStmt(&Assign);
406 } else {
407 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
408 Ivar->getType(), Loc);
409 EmitStmt(&Assign);
410 }
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000411 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000412
413 FinishFunction();
Chris Lattner5696e7b2008-06-17 18:05:57 +0000414}
415
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000416void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
417 ObjCMethodDecl *MD,
418 bool ctor) {
419 llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> IvarInitializers;
420 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
421 StartObjCMethod(MD, IMP->getClassInterface());
422 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
423 E = IMP->init_end(); B != E; ++B) {
424 CXXBaseOrMemberInitializer *Member = (*B);
425 IvarInitializers.push_back(Member);
426 }
427 if (ctor) {
428 for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) {
429 CXXBaseOrMemberInitializer *IvarInit = IvarInitializers[I];
430 FieldDecl *Field = IvarInit->getMember();
431 QualType FieldType = Field->getType();
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000432 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian499b9022010-04-28 22:30:33 +0000433 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
434 LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000435 EmitAggExpr(IvarInit->getInit(), LV.getAddress(),
436 LV.isVolatileQualified(), false, true);
437 }
438 // constructor returns 'self'.
439 CodeGenTypes &Types = CGM.getTypes();
440 QualType IdTy(CGM.getContext().getObjCIdType());
441 llvm::Value *SelfAsId =
442 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
443 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
Chandler Carruthf3983652010-05-06 00:20:39 +0000444 } else {
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000445 // dtor
446 for (size_t i = IvarInitializers.size(); i > 0; --i) {
447 FieldDecl *Field = IvarInitializers[i - 1]->getMember();
448 QualType FieldType = Field->getType();
Fariborz Jahanian499b9022010-04-28 22:30:33 +0000449 const ConstantArrayType *Array =
450 getContext().getAsConstantArrayType(FieldType);
451 if (Array)
452 FieldType = getContext().getBaseElementType(FieldType);
453
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000454 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
455 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
456 LoadObjCSelf(), Ivar, 0);
457 const RecordType *RT = FieldType->getAs<RecordType>();
458 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian60c7e162010-05-04 19:29:32 +0000459 CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(getContext());
Chandler Carruthf3983652010-05-06 00:20:39 +0000460 if (!Dtor->isTrivial()) {
Fariborz Jahanian60c7e162010-05-04 19:29:32 +0000461 if (Array) {
462 const llvm::Type *BasePtr = ConvertType(FieldType);
463 BasePtr = llvm::PointerType::getUnqual(BasePtr);
464 llvm::Value *BaseAddrPtr =
465 Builder.CreateBitCast(LV.getAddress(), BasePtr);
466 EmitCXXAggrDestructorCall(Dtor,
467 Array, BaseAddrPtr);
Chandler Carruthf3983652010-05-06 00:20:39 +0000468 } else {
Fariborz Jahanian60c7e162010-05-04 19:29:32 +0000469 EmitCXXDestructorCall(Dtor,
470 Dtor_Complete, /*ForVirtualBase=*/false,
471 LV.getAddress());
Chandler Carruthf3983652010-05-06 00:20:39 +0000472 }
473 }
474 }
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000475 }
476 FinishFunction();
477}
478
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000479bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
480 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
481 it++; it++;
482 const ABIArgInfo &AI = it->info;
483 // FIXME. Is this sufficient check?
484 return (AI.getKind() == ABIArgInfo::Indirect);
485}
486
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000487bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
488 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
489 return false;
490 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
491 return FDTTy->getDecl()->hasObjectMember();
492 return false;
493}
494
Daniel Dunbara08dff12008-09-24 04:04:31 +0000495llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000496 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
497 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner5696e7b2008-06-17 18:05:57 +0000498}
499
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000500QualType CodeGenFunction::TypeOfSelfObject() {
501 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
502 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000503 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
504 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000505 return PTy->getPointeeType();
506}
507
Mike Stump11289f42009-09-09 15:08:12 +0000508RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000509 const Selector &S) {
510 llvm::Value *Receiver = LoadObjCSelf();
511 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
512 bool isClassMessage = OMD->isClassMethod();
513 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000514 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000515 Exp->getType(),
516 S,
517 OMD->getClassInterface(),
518 isCategoryImpl,
519 Receiver,
520 isClassMessage,
521 CallArgList());
Mike Stump11289f42009-09-09 15:08:12 +0000522
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000523}
524
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000525RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian1a504772009-09-01 17:02:21 +0000526 Exp = Exp->IgnoreParens();
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000527 // FIXME: Split it into two separate routines.
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000528 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
529 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000530 if (isa<ObjCSuperExpr>(E->getBase()))
531 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000532 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000533 GenerateMessageSend(*this, Exp->getType(), S,
534 EmitScalarExpr(E->getBase()),
David Chisnall01aa4672010-04-28 19:33:36 +0000535 CallArgList());
Mike Stump658fe022009-07-30 22:28:39 +0000536 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000537 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000538 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf557d832009-01-16 01:50:29 +0000539 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000540 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000541 if (KE->getInterfaceDecl()) {
542 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000543 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000544 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000545 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump11289f42009-09-09 15:08:12 +0000546 else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000547 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000548 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000549 GenerateMessageSend(*this, Exp->getType(), S,
550 Receiver,
David Chisnall01aa4672010-04-28 19:33:36 +0000551 CallArgList(), KE->getInterfaceDecl());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000552 }
Daniel Dunbar55310df2008-08-27 06:57:25 +0000553}
554
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000555void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
556 const Selector &S,
557 RValue Src) {
558 CallArgList Args;
559 llvm::Value *Receiver = LoadObjCSelf();
560 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
561 bool isClassMessage = OMD->isClassMethod();
562 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
563 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000564 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000565 Exp->getType(),
566 S,
567 OMD->getClassInterface(),
568 isCategoryImpl,
569 Receiver,
570 isClassMessage,
571 Args);
572 return;
573}
574
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000575void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000576 RValue Src) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000577 // FIXME: Split it into two separate routines.
578 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
579 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000580 if (isa<ObjCSuperExpr>(E->getBase())) {
581 EmitObjCSuperPropertySet(E, S, Src);
582 return;
Mike Stump11289f42009-09-09 15:08:12 +0000583 }
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000584 CallArgList Args;
585 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000586 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
587 EmitScalarExpr(E->getBase()),
David Chisnall01aa4672010-04-28 19:33:36 +0000588 Args);
Mike Stump11289f42009-09-09 15:08:12 +0000589 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000590 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000591 Selector S = E->getSetterMethod()->getSelector();
592 CallArgList Args;
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000593 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000594 if (E->getInterfaceDecl()) {
595 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000596 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000597 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000598 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian150abf22009-03-20 17:22:23 +0000599 return;
Mike Stump658fe022009-07-30 22:28:39 +0000600 } else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000601 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000602 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000603 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
604 Receiver,
David Chisnall01aa4672010-04-28 19:33:36 +0000605 Args, E->getInterfaceDecl());
Mike Stump658fe022009-07-30 22:28:39 +0000606 } else
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000607 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000608}
609
Chris Lattnerd4808922009-03-22 21:03:39 +0000610void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump11289f42009-09-09 15:08:12 +0000611 llvm::Constant *EnumerationMutationFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000612 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlsson75658592008-08-31 02:33:12 +0000613 llvm::Value *DeclAddress;
614 QualType ElementTy;
Mike Stump11289f42009-09-09 15:08:12 +0000615
Daniel Dunbara08dff12008-09-24 04:04:31 +0000616 if (!EnumerationMutationFn) {
617 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
618 return;
619 }
620
Anders Carlsson75658592008-08-31 02:33:12 +0000621 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
622 EmitStmt(SD);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +0000623 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner529efc72009-03-28 06:33:19 +0000624 const Decl* D = SD->getSingleDecl();
Ted Kremenek3fef3572008-10-06 20:59:48 +0000625 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000626 DeclAddress = LocalDeclMap[D];
Anders Carlsson75658592008-08-31 02:33:12 +0000627 } else {
628 ElementTy = cast<Expr>(S.getElement())->getType();
629 DeclAddress = 0;
630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
Anders Carlsson75658592008-08-31 02:33:12 +0000632 // Fast enumeration state.
633 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbara7566f12010-02-09 02:48:28 +0000634 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlssonc866eb52010-05-21 22:17:48 +0000635 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump11289f42009-09-09 15:08:12 +0000636
Anders Carlsson75658592008-08-31 02:33:12 +0000637 // Number of elements in the items array.
Anders Carlsson3f35a262008-08-31 04:05:03 +0000638 static const unsigned NumItems = 16;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Anders Carlsson75658592008-08-31 02:33:12 +0000640 // Get selector
Benjamin Kramer9e649c32010-03-30 11:36:44 +0000641 IdentifierInfo *II[] = {
642 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
643 &CGM.getContext().Idents.get("objects"),
644 &CGM.getContext().Idents.get("count")
645 };
646 Selector FastEnumSel =
647 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlsson75658592008-08-31 02:33:12 +0000648
649 QualType ItemsTy =
650 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump11289f42009-09-09 15:08:12 +0000651 llvm::APInt(32, NumItems),
Anders Carlsson75658592008-08-31 02:33:12 +0000652 ArrayType::Normal, 0);
Daniel Dunbara7566f12010-02-09 02:48:28 +0000653 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000654
Anders Carlsson75658592008-08-31 02:33:12 +0000655 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump11289f42009-09-09 15:08:12 +0000656
Anders Carlsson75658592008-08-31 02:33:12 +0000657 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000658 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000659 getContext().getPointerType(StateTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000660
661 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000662 getContext().getPointerType(ItemsTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000663
Anders Carlsson75658592008-08-31 02:33:12 +0000664 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000665 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump11289f42009-09-09 15:08:12 +0000666 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000667 getContext().UnsignedLongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000668
669 RValue CountRV =
670 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000671 getContext().UnsignedLongTy,
672 FastEnumSel,
David Chisnall01aa4672010-04-28 19:33:36 +0000673 Collection, Args);
Anders Carlsson75658592008-08-31 02:33:12 +0000674
Daniel Dunbara7566f12010-02-09 02:48:28 +0000675 llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy,
676 "limit.ptr");
Anders Carlsson75658592008-08-31 02:33:12 +0000677 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000679 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
680 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump11289f42009-09-09 15:08:12 +0000681
Anders Carlsson75658592008-08-31 02:33:12 +0000682 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Anderson0b75f232009-07-31 20:28:54 +0000683 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlsson75658592008-08-31 02:33:12 +0000684
685 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson3f35a262008-08-31 04:05:03 +0000686 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson75658592008-08-31 02:33:12 +0000687
Anders Carlsson3f35a262008-08-31 04:05:03 +0000688 EmitBlock(SetStartMutations);
Mike Stump11289f42009-09-09 15:08:12 +0000689
Daniel Dunbara7566f12010-02-09 02:48:28 +0000690 llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy);
Mike Stump11289f42009-09-09 15:08:12 +0000691
692 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson3f35a262008-08-31 04:05:03 +0000693 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000694 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000695 "mutationsptr");
Mike Stump11289f42009-09-09 15:08:12 +0000696
697 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000698 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000699
Anders Carlsson3f35a262008-08-31 04:05:03 +0000700 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000701
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000702 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlsson75658592008-08-31 02:33:12 +0000703 EmitBlock(LoopStart);
704
Daniel Dunbara7566f12010-02-09 02:48:28 +0000705 llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy,
706 "counter.ptr");
Anders Carlsson75658592008-08-31 02:33:12 +0000707 Builder.CreateStore(Zero, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000708
709 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlsson75658592008-08-31 02:33:12 +0000710 EmitBlock(LoopBody);
711
Anders Carlsson3f35a262008-08-31 04:05:03 +0000712 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
713 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
714
Mike Stump11289f42009-09-09 15:08:12 +0000715 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000716 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000717 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000718 StartMutations,
719 "tobool");
Mike Stump11289f42009-09-09 15:08:12 +0000720
721
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000722 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
723 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump11289f42009-09-09 15:08:12 +0000724
Anders Carlsson3f35a262008-08-31 04:05:03 +0000725 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000726
Anders Carlsson3f35a262008-08-31 04:05:03 +0000727 EmitBlock(WasMutated);
728 llvm::Value *V =
Mike Stump11289f42009-09-09 15:08:12 +0000729 Builder.CreateBitCast(Collection,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000730 ConvertType(getContext().getObjCIdType()),
731 "tmp");
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000732 CallArgList Args2;
Mike Stump11289f42009-09-09 15:08:12 +0000733 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000734 getContext().getObjCIdType()));
Mike Stump18bb9282009-05-16 07:57:57 +0000735 // FIXME: We shouldn't need to get the function info here, the runtime already
736 // should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000737 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000738 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000739 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump11289f42009-09-09 15:08:12 +0000740
Anders Carlsson3f35a262008-08-31 04:05:03 +0000741 EmitBlock(WasNotMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000742
743 llvm::Value *StateItemsPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000744 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
745
746 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
747
748 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
749 "stateitems");
750
Mike Stump11289f42009-09-09 15:08:12 +0000751 llvm::Value *CurrentItemPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000752 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000753
Anders Carlsson75658592008-08-31 02:33:12 +0000754 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump11289f42009-09-09 15:08:12 +0000755
Anders Carlsson75658592008-08-31 02:33:12 +0000756 // Cast the item to the right type.
757 CurrentItem = Builder.CreateBitCast(CurrentItem,
758 ConvertType(ElementTy), "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000759
Anders Carlsson75658592008-08-31 02:33:12 +0000760 if (!DeclAddress) {
761 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000762
Anders Carlsson75658592008-08-31 02:33:12 +0000763 // Set the value to null.
764 Builder.CreateStore(CurrentItem, LV.getAddress());
765 } else
766 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump11289f42009-09-09 15:08:12 +0000767
Anders Carlsson75658592008-08-31 02:33:12 +0000768 // Increment the counter.
Mike Stump11289f42009-09-09 15:08:12 +0000769 Counter = Builder.CreateAdd(Counter,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000770 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlsson75658592008-08-31 02:33:12 +0000771 Builder.CreateStore(Counter, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000772
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000773 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
774 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump11289f42009-09-09 15:08:12 +0000775
Anders Carlsson33747b62009-02-10 05:52:02 +0000776 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlsson75658592008-08-31 02:33:12 +0000777
778 EmitStmt(S.getBody());
Mike Stump11289f42009-09-09 15:08:12 +0000779
Anders Carlsson75658592008-08-31 02:33:12 +0000780 BreakContinueStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000781
Anders Carlsson75658592008-08-31 02:33:12 +0000782 EmitBlock(AfterBody);
Mike Stump11289f42009-09-09 15:08:12 +0000783
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000784 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanian6e7ecc82009-01-06 18:56:31 +0000785
786 Counter = Builder.CreateLoad(CounterPtr);
787 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlsson75658592008-08-31 02:33:12 +0000788 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar56b936b2008-09-04 21:54:37 +0000789 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson75658592008-08-31 02:33:12 +0000790
791 // Fetch more elements.
792 EmitBlock(FetchMore);
Mike Stump11289f42009-09-09 15:08:12 +0000793
794 CountRV =
795 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000796 getContext().UnsignedLongTy,
Mike Stump11289f42009-09-09 15:08:12 +0000797 FastEnumSel,
David Chisnall01aa4672010-04-28 19:33:36 +0000798 Collection, Args);
Anders Carlsson75658592008-08-31 02:33:12 +0000799 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
800 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000801
Anders Carlsson75658592008-08-31 02:33:12 +0000802 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
803 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump11289f42009-09-09 15:08:12 +0000804
Anders Carlsson75658592008-08-31 02:33:12 +0000805 // No more elements.
806 EmitBlock(NoElements);
807
808 if (!DeclAddress) {
809 // If the element was not a declaration, set it to be null.
810
811 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000812
Anders Carlsson75658592008-08-31 02:33:12 +0000813 // Set the value to null.
Owen Anderson0b75f232009-07-31 20:28:54 +0000814 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlsson75658592008-08-31 02:33:12 +0000815 LV.getAddress());
816 }
817
818 EmitBlock(LoopEnd);
Anders Carlsson2e744e82008-08-30 19:51:14 +0000819}
820
Mike Stump11289f42009-09-09 15:08:12 +0000821void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000822 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000823}
824
Mike Stump11289f42009-09-09 15:08:12 +0000825void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000826 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
827}
828
Chris Lattnere132e242008-11-15 21:26:17 +0000829void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump11289f42009-09-09 15:08:12 +0000830 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000831 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattnere132e242008-11-15 21:26:17 +0000832}
833
Ted Kremenek43e06332008-04-09 15:51:31 +0000834CGObjCRuntime::~CGObjCRuntime() {}