blob: df0263ee129784bdf095427b4b26399213d96eea [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: {
67 const ObjCInterfaceType *IFace
68 = E->getClassReceiver()->getAs<ObjCInterfaceType>();
David Chisnall01aa4672010-04-28 19:33:36 +000069 OID = IFace->getDecl();
Douglas Gregor9a129192010-04-21 00:45:42 +000070 assert(IFace && "Invalid Objective-C class message send");
David Chisnall01aa4672010-04-28 19:33:36 +000071 Receiver = Runtime.GetClass(Builder, OID);
Daniel Dunbarca8531a2008-08-25 08:19:24 +000072 isClassMessage = true;
Douglas Gregor9a129192010-04-21 00:45:42 +000073 break;
74 }
75
76 case ObjCMessageExpr::SuperInstance:
Chris Lattnerb1d329d2008-06-24 17:04:18 +000077 Receiver = LoadObjCSelf();
Douglas Gregor9a129192010-04-21 00:45:42 +000078 isSuperMessage = true;
79 break;
80
81 case ObjCMessageExpr::SuperClass:
82 Receiver = LoadObjCSelf();
83 isSuperMessage = true;
84 isClassMessage = true;
85 break;
Chris Lattnerb1d329d2008-06-24 17:04:18 +000086 }
87
Daniel Dunbarc722b852008-08-30 03:02:31 +000088 CallArgList Args;
Anders Carlsson623dcae2009-04-18 20:29:27 +000089 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerb1d329d2008-06-24 17:04:18 +000091 if (isSuperMessage) {
Chris Lattner6cfec782008-06-26 04:42:20 +000092 // super is only valid in an Objective-C method
93 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000094 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +000095 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
96 E->getSelector(),
Daniel Dunbarca8531a2008-08-25 08:19:24 +000097 OMD->getClassInterface(),
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000098 isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +000099 Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000100 isClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000101 Args,
102 E->getMethodDecl());
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000103 }
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000104
Mike Stump11289f42009-09-09 15:08:12 +0000105 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
David Chisnall01aa4672010-04-28 19:33:36 +0000106 Receiver, Args, OID,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000107 E->getMethodDecl());
Anders Carlsson76f4a902007-08-21 17:43:55 +0000108}
109
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000110/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
111/// the LLVM function and sets the other context used by
112/// CodeGenFunction.
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000113void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
114 const ObjCContainerDecl *CD) {
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000115 FunctionArgList Args;
Devang Patela2c048e2010-04-05 21:09:15 +0000116 // Check if we should generate debug info for this method.
117 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
118 DebugInfo = CGM.getDebugInfo();
119
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000120 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000121
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000122 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
123 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner5696e7b2008-06-17 18:05:57 +0000124
Mike Stump11289f42009-09-09 15:08:12 +0000125 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000126 OMD->getSelfDecl()->getType()));
127 Args.push_back(std::make_pair(OMD->getCmdDecl(),
128 OMD->getCmdDecl()->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000129
Chris Lattnera4997152009-02-20 18:43:26 +0000130 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
131 E = OMD->param_end(); PI != E; ++PI)
132 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000133
Devang Patele8814ce2010-02-15 18:08:38 +0000134 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000135}
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000136
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000137/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump11289f42009-09-09 15:08:12 +0000138/// its pointer, name, and types registered in the class struture.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000139void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000140 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000141 EmitStmt(OMD->getBody());
142 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000143}
144
Mike Stump18bb9282009-05-16 07:57:57 +0000145// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
146// AST for the whole body we can just fall back to having a GenerateFunction
147// which takes the body Stmt.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000148
149/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000150/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
151/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000152void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
153 const ObjCPropertyImplDecl *PID) {
Daniel Dunbara08dff12008-09-24 04:04:31 +0000154 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000155 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000156 bool IsAtomic =
157 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000158 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
159 assert(OMD && "Invalid call to generate getter (empty method)");
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000160 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000161
Daniel Dunbara08dff12008-09-24 04:04:31 +0000162 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000163 // this. Non-atomic properties are directly evaluated.
164 // atomic 'copy' and 'retain' properties are also directly
165 // evaluated in gc-only mode.
Daniel Dunbara08dff12008-09-24 04:04:31 +0000166 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000167 IsAtomic &&
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000168 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
169 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000170 llvm::Value *GetPropertyFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000171 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000172
Daniel Dunbara08dff12008-09-24 04:04:31 +0000173 if (!GetPropertyFn) {
174 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
175 FinishFunction();
176 return;
177 }
178
179 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
180 // FIXME: Can't this be simpler? This might even be worse than the
181 // corresponding gcc code.
182 CodeGenTypes &Types = CGM.getTypes();
183 ValueDecl *Cmd = OMD->getCmdDecl();
184 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
185 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000186 llvm::Value *SelfAsId =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000187 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000188 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000189 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000190 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000191 CallArgList Args;
192 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
193 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
194 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
195 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbar1ef73732009-02-03 23:43:59 +0000196 // FIXME: We shouldn't need to get the function info here, the
197 // runtime already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000198 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000199 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000200 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000201 // We need to fix the type here. Ivars with copy & retain are
202 // always objects so we don't need to worry about complex or
203 // aggregates.
Mike Stump11289f42009-09-09 15:08:12 +0000204 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbara08dff12008-09-24 04:04:31 +0000205 Types.ConvertType(PD->getType())));
206 EmitReturnOfRValue(RV, PD->getType());
207 } else {
Fariborz Jahanianf9c45852010-03-25 21:56:43 +0000208 if (Ivar->getType()->isAnyComplexType()) {
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000209 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
210 Ivar, 0);
Fariborz Jahanianf9c45852010-03-25 21:56:43 +0000211 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
212 LV.isVolatileQualified());
213 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
214 }
215 else if (hasAggregateLLVMType(Ivar->getType())) {
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000216 bool IsStrong = false;
217 if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType())))
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000218 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
219 && CGM.getObjCRuntime().GetCopyStructFunction()) {
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000220 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
221 Ivar, 0);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000222 llvm::Value *GetCopyStructFn =
223 CGM.getObjCRuntime().GetCopyStructFunction();
224 CodeGenTypes &Types = CGM.getTypes();
225 // objc_copyStruct (ReturnValue, &structIvar,
226 // sizeof (Type of Ivar), isAtomic, false);
227 CallArgList Args;
228 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
229 Types.ConvertType(getContext().VoidPtrTy)));
230 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
231 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
232 Types.ConvertType(getContext().VoidPtrTy)));
233 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
234 // sizeof (Type of Ivar)
235 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
236 llvm::Value *SizeVal =
237 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
238 Args.push_back(std::make_pair(RValue::get(SizeVal),
239 getContext().LongTy));
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000240 llvm::Value *isAtomic =
Fariborz Jahanian5e575c02010-04-13 18:43:37 +0000241 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
242 IsAtomic ? 1 : 0);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000243 Args.push_back(std::make_pair(RValue::get(isAtomic),
244 getContext().BoolTy));
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000245 llvm::Value *hasStrong =
246 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
247 IsStrong ? 1 : 0);
248 Args.push_back(std::make_pair(RValue::get(hasStrong),
249 getContext().BoolTy));
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000250 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
251 FunctionType::ExtInfo()),
252 GetCopyStructFn, ReturnValueSlot(), Args);
253 }
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000254 else {
255 if (PID->getGetterCXXConstructor()) {
256 ReturnStmt *Stmt =
257 new (getContext()) ReturnStmt(SourceLocation(),
Douglas Gregor6fd1b182010-05-15 06:01:05 +0000258 PID->getGetterCXXConstructor(),
259 0);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000260 EmitReturnStmt(*Stmt);
261 }
262 else {
263 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
264 Ivar, 0);
265 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
266 }
267 }
Mike Stump658fe022009-07-30 22:28:39 +0000268 } else {
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000269 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
270 Ivar, 0);
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000271 CodeGenTypes &Types = CGM.getTypes();
272 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
273 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump11289f42009-09-09 15:08:12 +0000274 Types.ConvertType(PD->getType())));
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000275 EmitReturnOfRValue(RV, PD->getType());
276 }
Daniel Dunbara08dff12008-09-24 04:04:31 +0000277 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000278
279 FinishFunction();
280}
281
282/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000283/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
284/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000285void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
286 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000287 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000288 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
289 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
290 assert(OMD && "Invalid call to generate setter (empty method)");
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000291 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000292
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000293 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump11289f42009-09-09 15:08:12 +0000294 bool IsAtomic =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000295 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
296
297 // Determine if we should use an objc_setProperty call for
298 // this. Properties with 'copy' semantics always use it, as do
299 // non-atomic properties with 'release' semantics as long as we are
300 // not in gc-only mode.
301 if (IsCopy ||
302 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
303 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000304 llvm::Value *SetPropertyFn =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000305 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000306
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000307 if (!SetPropertyFn) {
308 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
309 FinishFunction();
310 return;
311 }
Mike Stump11289f42009-09-09 15:08:12 +0000312
313 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000314 // <is-atomic>, <is-copy>).
315 // FIXME: Can't this be simpler? This might even be worse than the
316 // corresponding gcc code.
317 CodeGenTypes &Types = CGM.getTypes();
318 ValueDecl *Cmd = OMD->getCmdDecl();
319 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
320 QualType IdTy = getContext().getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +0000321 llvm::Value *SelfAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000322 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000323 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattnera4997152009-02-20 18:43:26 +0000324 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump11289f42009-09-09 15:08:12 +0000325 llvm::Value *ArgAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000326 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
327 Types.ConvertType(IdTy));
328 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000329 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000330 llvm::Value *False =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000331 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000332 CallArgList Args;
333 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
334 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
335 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
336 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump11289f42009-09-09 15:08:12 +0000337 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000338 getContext().BoolTy));
Mike Stump11289f42009-09-09 15:08:12 +0000339 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000340 getContext().BoolTy));
Mike Stump18bb9282009-05-16 07:57:57 +0000341 // FIXME: We shouldn't need to get the function info here, the runtime
342 // already should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000343 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000344 FunctionType::ExtInfo()),
345 SetPropertyFn,
Anders Carlsson61a401c2009-12-24 19:25:24 +0000346 ReturnValueSlot(), Args);
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000347 } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) &&
348 !Ivar->getType()->isAnyComplexType() &&
349 IndirectObjCSetterArg(*CurFnInfo)
350 && CGM.getObjCRuntime().GetCopyStructFunction()) {
351 // objc_copyStruct (&structIvar, &Arg,
352 // sizeof (struct something), true, false);
353 llvm::Value *GetCopyStructFn =
354 CGM.getObjCRuntime().GetCopyStructFunction();
355 CodeGenTypes &Types = CGM.getTypes();
356 CallArgList Args;
357 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
358 RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
359 Types.ConvertType(getContext().VoidPtrTy)));
360 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
361 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
362 llvm::Value *ArgAsPtrTy =
363 Builder.CreateBitCast(Arg,
364 Types.ConvertType(getContext().VoidPtrTy));
365 RV = RValue::get(ArgAsPtrTy);
366 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
367 // sizeof (Type of Ivar)
368 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
369 llvm::Value *SizeVal =
370 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
371 Args.push_back(std::make_pair(RValue::get(SizeVal),
372 getContext().LongTy));
373 llvm::Value *True =
374 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
375 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
376 llvm::Value *False =
377 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
378 Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
379 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
380 FunctionType::ExtInfo()),
381 GetCopyStructFn, ReturnValueSlot(), Args);
Fariborz Jahanianb8993382010-05-06 15:45:36 +0000382 } else if (PID->getSetterCXXAssignment()) {
383 EmitAnyExpr(PID->getSetterCXXAssignment(), (llvm::Value *)0, false, true,
384 false);
385
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000386 } else {
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000387 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000388 SourceLocation Loc = PD->getLocation();
389 ValueDecl *Self = OMD->getSelfDecl();
390 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
391 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattnera4997152009-02-20 18:43:26 +0000392 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000393 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000394 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
395
396 // The property type can differ from the ivar type in some situations with
397 // Objective-C pointer types, we can always bit cast the RHS in these cases.
398 if (getContext().getCanonicalType(Ivar->getType()) !=
399 getContext().getCanonicalType(ArgDecl->getType())) {
400 ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
Anders Carlsson0c509ee2010-04-24 16:57:13 +0000401 CXXBaseSpecifierArray(), false);
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000402 BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
403 Ivar->getType(), Loc);
404 EmitStmt(&Assign);
405 } else {
406 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
407 Ivar->getType(), Loc);
408 EmitStmt(&Assign);
409 }
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000410 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000411
412 FinishFunction();
Chris Lattner5696e7b2008-06-17 18:05:57 +0000413}
414
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000415void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
416 ObjCMethodDecl *MD,
417 bool ctor) {
418 llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> IvarInitializers;
419 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
420 StartObjCMethod(MD, IMP->getClassInterface());
421 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
422 E = IMP->init_end(); B != E; ++B) {
423 CXXBaseOrMemberInitializer *Member = (*B);
424 IvarInitializers.push_back(Member);
425 }
426 if (ctor) {
427 for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) {
428 CXXBaseOrMemberInitializer *IvarInit = IvarInitializers[I];
429 FieldDecl *Field = IvarInit->getMember();
430 QualType FieldType = Field->getType();
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000431 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian499b9022010-04-28 22:30:33 +0000432 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
433 LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000434 EmitAggExpr(IvarInit->getInit(), LV.getAddress(),
435 LV.isVolatileQualified(), false, true);
436 }
437 // constructor returns 'self'.
438 CodeGenTypes &Types = CGM.getTypes();
439 QualType IdTy(CGM.getContext().getObjCIdType());
440 llvm::Value *SelfAsId =
441 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
442 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
Chandler Carruthf3983652010-05-06 00:20:39 +0000443 } else {
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000444 // dtor
445 for (size_t i = IvarInitializers.size(); i > 0; --i) {
446 FieldDecl *Field = IvarInitializers[i - 1]->getMember();
447 QualType FieldType = Field->getType();
Fariborz Jahanian499b9022010-04-28 22:30:33 +0000448 const ConstantArrayType *Array =
449 getContext().getAsConstantArrayType(FieldType);
450 if (Array)
451 FieldType = getContext().getBaseElementType(FieldType);
452
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000453 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
454 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
455 LoadObjCSelf(), Ivar, 0);
456 const RecordType *RT = FieldType->getAs<RecordType>();
457 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian60c7e162010-05-04 19:29:32 +0000458 CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(getContext());
Chandler Carruthf3983652010-05-06 00:20:39 +0000459 if (!Dtor->isTrivial()) {
Fariborz Jahanian60c7e162010-05-04 19:29:32 +0000460 if (Array) {
461 const llvm::Type *BasePtr = ConvertType(FieldType);
462 BasePtr = llvm::PointerType::getUnqual(BasePtr);
463 llvm::Value *BaseAddrPtr =
464 Builder.CreateBitCast(LV.getAddress(), BasePtr);
465 EmitCXXAggrDestructorCall(Dtor,
466 Array, BaseAddrPtr);
Chandler Carruthf3983652010-05-06 00:20:39 +0000467 } else {
Fariborz Jahanian60c7e162010-05-04 19:29:32 +0000468 EmitCXXDestructorCall(Dtor,
469 Dtor_Complete, /*ForVirtualBase=*/false,
470 LV.getAddress());
Chandler Carruthf3983652010-05-06 00:20:39 +0000471 }
472 }
473 }
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +0000474 }
475 FinishFunction();
476}
477
Fariborz Jahanian08b0f662010-04-13 00:38:05 +0000478bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
479 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
480 it++; it++;
481 const ABIArgInfo &AI = it->info;
482 // FIXME. Is this sufficient check?
483 return (AI.getKind() == ABIArgInfo::Indirect);
484}
485
Fariborz Jahanian7e9d52a2010-04-13 18:32:24 +0000486bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
487 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
488 return false;
489 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
490 return FDTTy->getDecl()->hasObjectMember();
491 return false;
492}
493
Daniel Dunbara08dff12008-09-24 04:04:31 +0000494llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000495 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump692c6e32009-03-20 21:53:12 +0000496 // See if we need to lazily forward self inside a block literal.
497 BlockForwardSelf();
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000498 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner5696e7b2008-06-17 18:05:57 +0000499}
500
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000501QualType CodeGenFunction::TypeOfSelfObject() {
502 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
503 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000504 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
505 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000506 return PTy->getPointeeType();
507}
508
Mike Stump11289f42009-09-09 15:08:12 +0000509RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000510 const Selector &S) {
511 llvm::Value *Receiver = LoadObjCSelf();
512 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
513 bool isClassMessage = OMD->isClassMethod();
514 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000515 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000516 Exp->getType(),
517 S,
518 OMD->getClassInterface(),
519 isCategoryImpl,
520 Receiver,
521 isClassMessage,
522 CallArgList());
Mike Stump11289f42009-09-09 15:08:12 +0000523
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000524}
525
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000526RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian1a504772009-09-01 17:02:21 +0000527 Exp = Exp->IgnoreParens();
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000528 // FIXME: Split it into two separate routines.
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000529 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
530 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000531 if (isa<ObjCSuperExpr>(E->getBase()))
532 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000533 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000534 GenerateMessageSend(*this, Exp->getType(), S,
535 EmitScalarExpr(E->getBase()),
David Chisnall01aa4672010-04-28 19:33:36 +0000536 CallArgList());
Mike Stump658fe022009-07-30 22:28:39 +0000537 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000538 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000539 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf557d832009-01-16 01:50:29 +0000540 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000541 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000542 if (KE->getInterfaceDecl()) {
543 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000544 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000545 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000546 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump11289f42009-09-09 15:08:12 +0000547 else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000548 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000549 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000550 GenerateMessageSend(*this, Exp->getType(), S,
551 Receiver,
David Chisnall01aa4672010-04-28 19:33:36 +0000552 CallArgList(), KE->getInterfaceDecl());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000553 }
Daniel Dunbar55310df2008-08-27 06:57:25 +0000554}
555
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000556void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
557 const Selector &S,
558 RValue Src) {
559 CallArgList Args;
560 llvm::Value *Receiver = LoadObjCSelf();
561 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
562 bool isClassMessage = OMD->isClassMethod();
563 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
564 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000565 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000566 Exp->getType(),
567 S,
568 OMD->getClassInterface(),
569 isCategoryImpl,
570 Receiver,
571 isClassMessage,
572 Args);
573 return;
574}
575
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000576void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000577 RValue Src) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000578 // FIXME: Split it into two separate routines.
579 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
580 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000581 if (isa<ObjCSuperExpr>(E->getBase())) {
582 EmitObjCSuperPropertySet(E, S, Src);
583 return;
Mike Stump11289f42009-09-09 15:08:12 +0000584 }
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000585 CallArgList Args;
586 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000587 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
588 EmitScalarExpr(E->getBase()),
David Chisnall01aa4672010-04-28 19:33:36 +0000589 Args);
Mike Stump11289f42009-09-09 15:08:12 +0000590 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000591 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000592 Selector S = E->getSetterMethod()->getSelector();
593 CallArgList Args;
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000594 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000595 if (E->getInterfaceDecl()) {
596 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000597 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000598 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000599 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian150abf22009-03-20 17:22:23 +0000600 return;
Mike Stump658fe022009-07-30 22:28:39 +0000601 } else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000602 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000603 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000604 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
605 Receiver,
David Chisnall01aa4672010-04-28 19:33:36 +0000606 Args, E->getInterfaceDecl());
Mike Stump658fe022009-07-30 22:28:39 +0000607 } else
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000608 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000609}
610
Chris Lattnerd4808922009-03-22 21:03:39 +0000611void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump11289f42009-09-09 15:08:12 +0000612 llvm::Constant *EnumerationMutationFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000613 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlsson75658592008-08-31 02:33:12 +0000614 llvm::Value *DeclAddress;
615 QualType ElementTy;
Mike Stump11289f42009-09-09 15:08:12 +0000616
Daniel Dunbara08dff12008-09-24 04:04:31 +0000617 if (!EnumerationMutationFn) {
618 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
619 return;
620 }
621
Anders Carlsson75658592008-08-31 02:33:12 +0000622 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
623 EmitStmt(SD);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +0000624 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner529efc72009-03-28 06:33:19 +0000625 const Decl* D = SD->getSingleDecl();
Ted Kremenek3fef3572008-10-06 20:59:48 +0000626 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000627 DeclAddress = LocalDeclMap[D];
Anders Carlsson75658592008-08-31 02:33:12 +0000628 } else {
629 ElementTy = cast<Expr>(S.getElement())->getType();
630 DeclAddress = 0;
631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Anders Carlsson75658592008-08-31 02:33:12 +0000633 // Fast enumeration state.
634 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbara7566f12010-02-09 02:48:28 +0000635 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson3f35a262008-08-31 04:05:03 +0000636 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump11289f42009-09-09 15:08:12 +0000637
Anders Carlsson75658592008-08-31 02:33:12 +0000638 // Number of elements in the items array.
Anders Carlsson3f35a262008-08-31 04:05:03 +0000639 static const unsigned NumItems = 16;
Mike Stump11289f42009-09-09 15:08:12 +0000640
Anders Carlsson75658592008-08-31 02:33:12 +0000641 // Get selector
Benjamin Kramer9e649c32010-03-30 11:36:44 +0000642 IdentifierInfo *II[] = {
643 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
644 &CGM.getContext().Idents.get("objects"),
645 &CGM.getContext().Idents.get("count")
646 };
647 Selector FastEnumSel =
648 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlsson75658592008-08-31 02:33:12 +0000649
650 QualType ItemsTy =
651 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump11289f42009-09-09 15:08:12 +0000652 llvm::APInt(32, NumItems),
Anders Carlsson75658592008-08-31 02:33:12 +0000653 ArrayType::Normal, 0);
Daniel Dunbara7566f12010-02-09 02:48:28 +0000654 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000655
Anders Carlsson75658592008-08-31 02:33:12 +0000656 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump11289f42009-09-09 15:08:12 +0000657
Anders Carlsson75658592008-08-31 02:33:12 +0000658 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000659 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000660 getContext().getPointerType(StateTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000661
662 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000663 getContext().getPointerType(ItemsTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000664
Anders Carlsson75658592008-08-31 02:33:12 +0000665 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000666 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump11289f42009-09-09 15:08:12 +0000667 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000668 getContext().UnsignedLongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000669
670 RValue CountRV =
671 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000672 getContext().UnsignedLongTy,
673 FastEnumSel,
David Chisnall01aa4672010-04-28 19:33:36 +0000674 Collection, Args);
Anders Carlsson75658592008-08-31 02:33:12 +0000675
Daniel Dunbara7566f12010-02-09 02:48:28 +0000676 llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy,
677 "limit.ptr");
Anders Carlsson75658592008-08-31 02:33:12 +0000678 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000679
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000680 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
681 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump11289f42009-09-09 15:08:12 +0000682
Anders Carlsson75658592008-08-31 02:33:12 +0000683 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Anderson0b75f232009-07-31 20:28:54 +0000684 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlsson75658592008-08-31 02:33:12 +0000685
686 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson3f35a262008-08-31 04:05:03 +0000687 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson75658592008-08-31 02:33:12 +0000688
Anders Carlsson3f35a262008-08-31 04:05:03 +0000689 EmitBlock(SetStartMutations);
Mike Stump11289f42009-09-09 15:08:12 +0000690
Daniel Dunbara7566f12010-02-09 02:48:28 +0000691 llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy);
Mike Stump11289f42009-09-09 15:08:12 +0000692
693 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson3f35a262008-08-31 04:05:03 +0000694 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000695 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000696 "mutationsptr");
Mike Stump11289f42009-09-09 15:08:12 +0000697
698 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000699 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000700
Anders Carlsson3f35a262008-08-31 04:05:03 +0000701 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000702
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000703 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlsson75658592008-08-31 02:33:12 +0000704 EmitBlock(LoopStart);
705
Daniel Dunbara7566f12010-02-09 02:48:28 +0000706 llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy,
707 "counter.ptr");
Anders Carlsson75658592008-08-31 02:33:12 +0000708 Builder.CreateStore(Zero, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000709
710 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlsson75658592008-08-31 02:33:12 +0000711 EmitBlock(LoopBody);
712
Anders Carlsson3f35a262008-08-31 04:05:03 +0000713 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
714 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
715
Mike Stump11289f42009-09-09 15:08:12 +0000716 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000717 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000718 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000719 StartMutations,
720 "tobool");
Mike Stump11289f42009-09-09 15:08:12 +0000721
722
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000723 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
724 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump11289f42009-09-09 15:08:12 +0000725
Anders Carlsson3f35a262008-08-31 04:05:03 +0000726 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000727
Anders Carlsson3f35a262008-08-31 04:05:03 +0000728 EmitBlock(WasMutated);
729 llvm::Value *V =
Mike Stump11289f42009-09-09 15:08:12 +0000730 Builder.CreateBitCast(Collection,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000731 ConvertType(getContext().getObjCIdType()),
732 "tmp");
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000733 CallArgList Args2;
Mike Stump11289f42009-09-09 15:08:12 +0000734 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000735 getContext().getObjCIdType()));
Mike Stump18bb9282009-05-16 07:57:57 +0000736 // FIXME: We shouldn't need to get the function info here, the runtime already
737 // should have computed it to build the function.
John McCallab26cfa2010-02-05 21:31:56 +0000738 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000739 FunctionType::ExtInfo()),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000740 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump11289f42009-09-09 15:08:12 +0000741
Anders Carlsson3f35a262008-08-31 04:05:03 +0000742 EmitBlock(WasNotMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000743
744 llvm::Value *StateItemsPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000745 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
746
747 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
748
749 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
750 "stateitems");
751
Mike Stump11289f42009-09-09 15:08:12 +0000752 llvm::Value *CurrentItemPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000753 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000754
Anders Carlsson75658592008-08-31 02:33:12 +0000755 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump11289f42009-09-09 15:08:12 +0000756
Anders Carlsson75658592008-08-31 02:33:12 +0000757 // Cast the item to the right type.
758 CurrentItem = Builder.CreateBitCast(CurrentItem,
759 ConvertType(ElementTy), "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000760
Anders Carlsson75658592008-08-31 02:33:12 +0000761 if (!DeclAddress) {
762 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000763
Anders Carlsson75658592008-08-31 02:33:12 +0000764 // Set the value to null.
765 Builder.CreateStore(CurrentItem, LV.getAddress());
766 } else
767 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump11289f42009-09-09 15:08:12 +0000768
Anders Carlsson75658592008-08-31 02:33:12 +0000769 // Increment the counter.
Mike Stump11289f42009-09-09 15:08:12 +0000770 Counter = Builder.CreateAdd(Counter,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000771 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlsson75658592008-08-31 02:33:12 +0000772 Builder.CreateStore(Counter, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000773
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000774 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
775 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump11289f42009-09-09 15:08:12 +0000776
Anders Carlsson33747b62009-02-10 05:52:02 +0000777 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlsson75658592008-08-31 02:33:12 +0000778
779 EmitStmt(S.getBody());
Mike Stump11289f42009-09-09 15:08:12 +0000780
Anders Carlsson75658592008-08-31 02:33:12 +0000781 BreakContinueStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000782
Anders Carlsson75658592008-08-31 02:33:12 +0000783 EmitBlock(AfterBody);
Mike Stump11289f42009-09-09 15:08:12 +0000784
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000785 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanian6e7ecc82009-01-06 18:56:31 +0000786
787 Counter = Builder.CreateLoad(CounterPtr);
788 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlsson75658592008-08-31 02:33:12 +0000789 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar56b936b2008-09-04 21:54:37 +0000790 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson75658592008-08-31 02:33:12 +0000791
792 // Fetch more elements.
793 EmitBlock(FetchMore);
Mike Stump11289f42009-09-09 15:08:12 +0000794
795 CountRV =
796 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000797 getContext().UnsignedLongTy,
Mike Stump11289f42009-09-09 15:08:12 +0000798 FastEnumSel,
David Chisnall01aa4672010-04-28 19:33:36 +0000799 Collection, Args);
Anders Carlsson75658592008-08-31 02:33:12 +0000800 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
801 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000802
Anders Carlsson75658592008-08-31 02:33:12 +0000803 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
804 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump11289f42009-09-09 15:08:12 +0000805
Anders Carlsson75658592008-08-31 02:33:12 +0000806 // No more elements.
807 EmitBlock(NoElements);
808
809 if (!DeclAddress) {
810 // If the element was not a declaration, set it to be null.
811
812 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000813
Anders Carlsson75658592008-08-31 02:33:12 +0000814 // Set the value to null.
Owen Anderson0b75f232009-07-31 20:28:54 +0000815 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlsson75658592008-08-31 02:33:12 +0000816 LV.getAddress());
817 }
818
819 EmitBlock(LoopEnd);
Anders Carlsson2e744e82008-08-30 19:51:14 +0000820}
821
Mike Stump11289f42009-09-09 15:08:12 +0000822void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000823 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000824}
825
Mike Stump11289f42009-09-09 15:08:12 +0000826void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000827 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
828}
829
Chris Lattnere132e242008-11-15 21:26:17 +0000830void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump11289f42009-09-09 15:08:12 +0000831 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000832 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattnere132e242008-11-15 21:26:17 +0000833}
834
Ted Kremenek43e06332008-04-09 15:51:31 +0000835CGObjCRuntime::~CGObjCRuntime() {}