blob: b3e9dc525134e0a9bc803dd310e4e1fc63a23e37 [file] [log] [blame]
Anders Carlsson55085182007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlsson55085182007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek2979ec72008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlsson55085182007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbar85c59ed2008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/StmtObjC.h"
Daniel Dunbare66f4e32008-09-03 00:27:26 +000020#include "clang/Basic/Diagnostic.h"
Anders Carlsson3d8400d2008-08-30 19:51:14 +000021#include "llvm/ADT/STLExtras.h"
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +000022#include "llvm/Target/TargetData.h"
Anders Carlsson55085182007-08-21 17:43:55 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattner8fdf3282008-06-24 17:04:18 +000026/// Emits an instance of NSConstantString representing the object.
Mike Stump1eb44332009-09-09 15:08:12 +000027llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
Daniel Dunbar71fcec92008-11-25 21:53:21 +000028{
David Chisnall0d13f6f2010-01-23 02:40:42 +000029 llvm::Constant *C =
30 CGM.getObjCRuntime().GenerateConstantString(E->getString());
Daniel Dunbared7c6182008-08-20 00:28:19 +000031 // FIXME: This bitcast should just be made an invariant on the Runtime.
Owen Anderson3c4972d2009-07-29 18:54:39 +000032 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-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 Dunbar6d5a1c22010-02-03 20:11:42 +000041 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000042}
43
Daniel Dunbared7c6182008-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 Lattner8fdf3282008-06-24 17:04:18 +000048
49
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000050RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-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 Stump1eb44332009-09-09 15:08:12 +000054
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000055 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000056 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000057 bool isClassMessage = false;
David Chisnallc6cd5fd2010-04-28 19:33:36 +000058 ObjCInterfaceDecl *OID = 0;
Chris Lattner8fdf3282008-06-24 17:04:18 +000059 // Find the receiver
Daniel Dunbar0b647a62010-04-22 03:17:06 +000060 llvm::Value *Receiver = 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +000061 switch (E->getReceiverKind()) {
62 case ObjCMessageExpr::Instance:
63 Receiver = EmitScalarExpr(E->getInstanceReceiver());
64 break;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000065
Douglas Gregor04badcf2010-04-21 00:45:42 +000066 case ObjCMessageExpr::Class: {
67 const ObjCInterfaceType *IFace
68 = E->getClassReceiver()->getAs<ObjCInterfaceType>();
David Chisnallc6cd5fd2010-04-28 19:33:36 +000069 OID = IFace->getDecl();
Douglas Gregor04badcf2010-04-21 00:45:42 +000070 assert(IFace && "Invalid Objective-C class message send");
David Chisnallc6cd5fd2010-04-28 19:33:36 +000071 Receiver = Runtime.GetClass(Builder, OID);
Daniel Dunbarf56f1912008-08-25 08:19:24 +000072 isClassMessage = true;
Douglas Gregor04badcf2010-04-21 00:45:42 +000073 break;
74 }
75
76 case ObjCMessageExpr::SuperInstance:
Chris Lattner8fdf3282008-06-24 17:04:18 +000077 Receiver = LoadObjCSelf();
Douglas Gregor04badcf2010-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 Lattner8fdf3282008-06-24 17:04:18 +000086 }
87
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000088 CallArgList Args;
Anders Carlsson131038e2009-04-18 20:29:27 +000089 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner8fdf3282008-06-24 17:04:18 +000091 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000092 // super is only valid in an Objective-C method
93 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000094 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000095 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
96 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000097 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000098 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000099 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000100 isClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000101 Args,
102 E->getMethodDecl());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000103 }
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000104
Mike Stump1eb44332009-09-09 15:08:12 +0000105 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000106 Receiver, Args, OID,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000107 E->getMethodDecl());
Anders Carlsson55085182007-08-21 17:43:55 +0000108}
109
Daniel Dunbaraf05bb92008-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 Jahanian679a5022009-01-10 21:06:09 +0000113void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
114 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000115 FunctionArgList Args;
Devang Patel4800ea62010-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 Jahanian679a5022009-01-10 21:06:09 +0000120 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000121
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000122 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
123 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000124
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbar7c086512008-09-09 23:14:03 +0000126 OMD->getSelfDecl()->getType()));
127 Args.push_back(std::make_pair(OMD->getCmdDecl(),
128 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000129
Chris Lattner89951a82009-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 Lattner41110242008-06-17 18:05:57 +0000133
Devang Patela92d6132010-02-15 18:08:38 +0000134 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000135}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000136
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000137/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000138/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000139void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000140 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000141 EmitStmt(OMD->getBody());
142 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000143}
144
Mike Stumpf5408fe2009-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 Dunbaraf05bb92008-08-26 08:29:31 +0000148
149/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000150/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
151/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000152void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
153 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000154 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000155 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000156 bool IsAtomic =
157 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000158 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
159 assert(OMD && "Invalid call to generate getter (empty method)");
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000160 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000161
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000162 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-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 Dunbarc1cf4a52008-09-24 04:04:31 +0000166 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000167 IsAtomic &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000168 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
169 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000170 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000171 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Daniel Dunbarc1cf4a52008-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 Stump1eb44332009-09-09 15:08:12 +0000186 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000187 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000188 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000189 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000190 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-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 Dunbare4be5a62009-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 McCall04a67a62010-02-05 21:31:56 +0000198 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000199 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000200 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-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 Stump1eb44332009-09-09 15:08:12 +0000204 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000205 Types.ConvertType(PD->getType())));
206 EmitReturnOfRValue(RV, PD->getType());
207 } else {
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000208 if (Ivar->getType()->isAnyComplexType()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000209 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
210 Ivar, 0);
Fariborz Jahanian1b23fe62010-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 Jahanian15bd5882010-04-13 18:32:24 +0000216 bool IsStrong = false;
217 if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType())))
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000218 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
219 && CGM.getObjCRuntime().GetCopyStructFunction()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000220 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
221 Ivar, 0);
Fariborz Jahanian0b2bd472010-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 Jahanian0b2bd472010-04-13 00:38:05 +0000240 llvm::Value *isAtomic =
Fariborz Jahanian08adf322010-04-13 18:43:37 +0000241 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
242 IsAtomic ? 1 : 0);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000243 Args.push_back(std::make_pair(RValue::get(isAtomic),
244 getContext().BoolTy));
Fariborz Jahanian15bd5882010-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 Jahanian0b2bd472010-04-13 00:38:05 +0000250 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
251 FunctionType::ExtInfo()),
252 GetCopyStructFn, ReturnValueSlot(), Args);
253 }
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000254 else {
255 if (PID->getGetterCXXConstructor()) {
256 ReturnStmt *Stmt =
257 new (getContext()) ReturnStmt(SourceLocation(),
258 PID->getGetterCXXConstructor());
259 EmitReturnStmt(*Stmt);
260 }
261 else {
262 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
263 Ivar, 0);
264 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
265 }
266 }
Mike Stumpb3589f42009-07-30 22:28:39 +0000267 } else {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000268 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
269 Ivar, 0);
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000270 CodeGenTypes &Types = CGM.getTypes();
271 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
272 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump1eb44332009-09-09 15:08:12 +0000273 Types.ConvertType(PD->getType())));
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000274 EmitReturnOfRValue(RV, PD->getType());
275 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000276 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000277
278 FinishFunction();
279}
280
281/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000282/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
283/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000284void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
285 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000286 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000287 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
288 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
289 assert(OMD && "Invalid call to generate setter (empty method)");
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000290 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000291
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000292 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000293 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000294 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
295
296 // Determine if we should use an objc_setProperty call for
297 // this. Properties with 'copy' semantics always use it, as do
298 // non-atomic properties with 'release' semantics as long as we are
299 // not in gc-only mode.
300 if (IsCopy ||
301 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
302 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000303 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000304 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000306 if (!SetPropertyFn) {
307 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
308 FinishFunction();
309 return;
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
312 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000313 // <is-atomic>, <is-copy>).
314 // FIXME: Can't this be simpler? This might even be worse than the
315 // corresponding gcc code.
316 CodeGenTypes &Types = CGM.getTypes();
317 ValueDecl *Cmd = OMD->getCmdDecl();
318 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
319 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000320 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000321 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000322 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000323 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000324 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000325 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
326 Types.ConvertType(IdTy));
327 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000328 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000329 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000330 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000331 CallArgList Args;
332 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
333 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
334 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
335 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000336 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000337 getContext().BoolTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000338 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000339 getContext().BoolTy));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000340 // FIXME: We shouldn't need to get the function info here, the runtime
341 // already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000342 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000343 FunctionType::ExtInfo()),
344 SetPropertyFn,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000345 ReturnValueSlot(), Args);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000346 } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) &&
347 !Ivar->getType()->isAnyComplexType() &&
348 IndirectObjCSetterArg(*CurFnInfo)
349 && CGM.getObjCRuntime().GetCopyStructFunction()) {
350 // objc_copyStruct (&structIvar, &Arg,
351 // sizeof (struct something), true, false);
352 llvm::Value *GetCopyStructFn =
353 CGM.getObjCRuntime().GetCopyStructFunction();
354 CodeGenTypes &Types = CGM.getTypes();
355 CallArgList Args;
356 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
357 RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
358 Types.ConvertType(getContext().VoidPtrTy)));
359 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
360 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
361 llvm::Value *ArgAsPtrTy =
362 Builder.CreateBitCast(Arg,
363 Types.ConvertType(getContext().VoidPtrTy));
364 RV = RValue::get(ArgAsPtrTy);
365 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
366 // sizeof (Type of Ivar)
367 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
368 llvm::Value *SizeVal =
369 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
370 Args.push_back(std::make_pair(RValue::get(SizeVal),
371 getContext().LongTy));
372 llvm::Value *True =
373 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
374 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
375 llvm::Value *False =
376 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
377 Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
378 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
379 FunctionType::ExtInfo()),
380 GetCopyStructFn, ReturnValueSlot(), Args);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000381 } else if (PID->getSetterCXXAssignment()) {
382 EmitAnyExpr(PID->getSetterCXXAssignment(), (llvm::Value *)0, false, true,
383 false);
384
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000385 } else {
Daniel Dunbar45e84232009-10-27 19:21:30 +0000386 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000387 SourceLocation Loc = PD->getLocation();
388 ValueDecl *Self = OMD->getSelfDecl();
389 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
390 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000391 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000392 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000393 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
394
395 // The property type can differ from the ivar type in some situations with
396 // Objective-C pointer types, we can always bit cast the RHS in these cases.
397 if (getContext().getCanonicalType(Ivar->getType()) !=
398 getContext().getCanonicalType(ArgDecl->getType())) {
399 ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
Anders Carlssonf1b48b72010-04-24 16:57:13 +0000400 CXXBaseSpecifierArray(), false);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000401 BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
402 Ivar->getType(), Loc);
403 EmitStmt(&Assign);
404 } else {
405 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
406 Ivar->getType(), Loc);
407 EmitStmt(&Assign);
408 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000409 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000410
411 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000412}
413
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000414void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
415 ObjCMethodDecl *MD,
416 bool ctor) {
417 llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> IvarInitializers;
418 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
419 StartObjCMethod(MD, IMP->getClassInterface());
420 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
421 E = IMP->init_end(); B != E; ++B) {
422 CXXBaseOrMemberInitializer *Member = (*B);
423 IvarInitializers.push_back(Member);
424 }
425 if (ctor) {
426 for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) {
427 CXXBaseOrMemberInitializer *IvarInit = IvarInitializers[I];
428 FieldDecl *Field = IvarInit->getMember();
429 QualType FieldType = Field->getType();
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000430 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian9b4d4fc2010-04-28 22:30:33 +0000431 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
432 LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000433 EmitAggExpr(IvarInit->getInit(), LV.getAddress(),
434 LV.isVolatileQualified(), false, true);
435 }
436 // constructor returns 'self'.
437 CodeGenTypes &Types = CGM.getTypes();
438 QualType IdTy(CGM.getContext().getObjCIdType());
439 llvm::Value *SelfAsId =
440 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
441 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000442 } else {
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000443 // dtor
444 for (size_t i = IvarInitializers.size(); i > 0; --i) {
445 FieldDecl *Field = IvarInitializers[i - 1]->getMember();
446 QualType FieldType = Field->getType();
Fariborz Jahanian9b4d4fc2010-04-28 22:30:33 +0000447 const ConstantArrayType *Array =
448 getContext().getAsConstantArrayType(FieldType);
449 if (Array)
450 FieldType = getContext().getBaseElementType(FieldType);
451
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000452 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
453 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
454 LoadObjCSelf(), Ivar, 0);
455 const RecordType *RT = FieldType->getAs<RecordType>();
456 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian8b688ed2010-05-04 19:29:32 +0000457 CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(getContext());
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000458 if (!Dtor->isTrivial()) {
Fariborz Jahanian8b688ed2010-05-04 19:29:32 +0000459 if (Array) {
460 const llvm::Type *BasePtr = ConvertType(FieldType);
461 BasePtr = llvm::PointerType::getUnqual(BasePtr);
462 llvm::Value *BaseAddrPtr =
463 Builder.CreateBitCast(LV.getAddress(), BasePtr);
464 EmitCXXAggrDestructorCall(Dtor,
465 Array, BaseAddrPtr);
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000466 } else {
Fariborz Jahanian8b688ed2010-05-04 19:29:32 +0000467 EmitCXXDestructorCall(Dtor,
468 Dtor_Complete, /*ForVirtualBase=*/false,
469 LV.getAddress());
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000470 }
471 }
472 }
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000473 }
474 FinishFunction();
475}
476
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000477bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
478 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
479 it++; it++;
480 const ABIArgInfo &AI = it->info;
481 // FIXME. Is this sufficient check?
482 return (AI.getKind() == ABIArgInfo::Indirect);
483}
484
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000485bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
486 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
487 return false;
488 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
489 return FDTTy->getDecl()->hasObjectMember();
490 return false;
491}
492
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000493llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000494 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000495 // See if we need to lazily forward self inside a block literal.
496 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000497 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000498}
499
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000500QualType CodeGenFunction::TypeOfSelfObject() {
501 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
502 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000503 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
504 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000505 return PTy->getPointeeType();
506}
507
Mike Stump1eb44332009-09-09 15:08:12 +0000508RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanianf4695572009-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 Stump1eb44332009-09-09 15:08:12 +0000514 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000515 Exp->getType(),
516 S,
517 OMD->getClassInterface(),
518 isCategoryImpl,
519 Receiver,
520 isClassMessage,
521 CallArgList());
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000523}
524
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000525RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +0000526 Exp = Exp->IgnoreParens();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000527 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000528 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
529 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000530 if (isa<ObjCSuperExpr>(E->getBase()))
531 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000532 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000533 GenerateMessageSend(*this, Exp->getType(), S,
534 EmitScalarExpr(E->getBase()),
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000535 CallArgList());
Mike Stumpb3589f42009-07-30 22:28:39 +0000536 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000537 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000538 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000539 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000540 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000541 if (KE->getInterfaceDecl()) {
542 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000543 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000544 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000545 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump1eb44332009-09-09 15:08:12 +0000546 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000547 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000548 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000549 GenerateMessageSend(*this, Exp->getType(), S,
550 Receiver,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000551 CallArgList(), KE->getInterfaceDecl());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000552 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000553}
554
Fariborz Jahanianf4695572009-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 Stump1eb44332009-09-09 15:08:12 +0000564 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-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 Jahanian43f44702008-11-22 22:30:21 +0000575void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000576 RValue Src) {
Fariborz Jahanian43f44702008-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 Jahanianf4695572009-03-20 19:18:21 +0000580 if (isa<ObjCSuperExpr>(E->getBase())) {
581 EmitObjCSuperPropertySet(E, S, Src);
582 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000583 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000584 CallArgList Args;
585 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000586 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
587 EmitScalarExpr(E->getBase()),
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000588 Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000589 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000590 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000591 Selector S = E->getSetterMethod()->getSelector();
592 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000593 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000594 if (E->getInterfaceDecl()) {
595 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000596 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000597 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000598 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000599 return;
Mike Stumpb3589f42009-07-30 22:28:39 +0000600 } else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000601 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000602 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000603 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
604 Receiver,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000605 Args, E->getInterfaceDecl());
Mike Stumpb3589f42009-07-30 22:28:39 +0000606 } else
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000607 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000608}
609
Chris Lattner74391b42009-03-22 21:03:39 +0000610void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000611 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000612 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000613 llvm::Value *DeclAddress;
614 QualType ElementTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000616 if (!EnumerationMutationFn) {
617 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
618 return;
619 }
620
Anders Carlssonf484c312008-08-31 02:33:12 +0000621 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
622 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000623 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000624 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000625 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000626 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000627 } else {
628 ElementTy = cast<Expr>(S.getElement())->getType();
629 DeclAddress = 0;
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Anders Carlssonf484c312008-08-31 02:33:12 +0000632 // Fast enumeration state.
633 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar195337d2010-02-09 02:48:28 +0000634 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000635 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlssonf484c312008-08-31 02:33:12 +0000637 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000638 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Anders Carlssonf484c312008-08-31 02:33:12 +0000640 // Get selector
Benjamin Kramerad468862010-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 Carlssonf484c312008-08-31 02:33:12 +0000648
649 QualType ItemsTy =
650 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000651 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000652 ArrayType::Normal, 0);
Daniel Dunbar195337d2010-02-09 02:48:28 +0000653 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Anders Carlssonf484c312008-08-31 02:33:12 +0000655 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Anders Carlssonf484c312008-08-31 02:33:12 +0000657 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000658 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000659 getContext().getPointerType(StateTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000660
661 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000662 getContext().getPointerType(ItemsTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Anders Carlssonf484c312008-08-31 02:33:12 +0000664 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000665 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump1eb44332009-09-09 15:08:12 +0000666 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000667 getContext().UnsignedLongTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000668
669 RValue CountRV =
670 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000671 getContext().UnsignedLongTy,
672 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000673 Collection, Args);
Anders Carlssonf484c312008-08-31 02:33:12 +0000674
Daniel Dunbar195337d2010-02-09 02:48:28 +0000675 llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy,
676 "limit.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000677 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Daniel Dunbar55e87422008-11-11 02:29:29 +0000679 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
680 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Anders Carlssonf484c312008-08-31 02:33:12 +0000682 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000683 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000684
685 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000686 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000687
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000688 EmitBlock(SetStartMutations);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Daniel Dunbar195337d2010-02-09 02:48:28 +0000690 llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
692 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000693 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000694 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000695 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000696
697 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000698 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000700 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Daniel Dunbar55e87422008-11-11 02:29:29 +0000702 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000703 EmitBlock(LoopStart);
704
Daniel Dunbar195337d2010-02-09 02:48:28 +0000705 llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy,
706 "counter.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000707 Builder.CreateStore(Zero, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
709 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000710 EmitBlock(LoopBody);
711
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000712 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
713 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
714
Mike Stump1eb44332009-09-09 15:08:12 +0000715 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000716 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000717 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000718 StartMutations,
719 "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000720
721
Daniel Dunbar55e87422008-11-11 02:29:29 +0000722 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
723 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000725 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000727 EmitBlock(WasMutated);
728 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000729 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000730 ConvertType(getContext().getObjCIdType()),
731 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000732 CallArgList Args2;
Mike Stump1eb44332009-09-09 15:08:12 +0000733 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000734 getContext().getObjCIdType()));
Mike Stumpf5408fe2009-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 McCall04a67a62010-02-05 21:31:56 +0000737 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindola264ba482010-03-30 20:24:48 +0000738 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000739 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000741 EmitBlock(WasNotMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000742
743 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-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 Stump1eb44332009-09-09 15:08:12 +0000751 llvm::Value *CurrentItemPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000752 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Anders Carlssonf484c312008-08-31 02:33:12 +0000754 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Anders Carlssonf484c312008-08-31 02:33:12 +0000756 // Cast the item to the right type.
757 CurrentItem = Builder.CreateBitCast(CurrentItem,
758 ConvertType(ElementTy), "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Anders Carlssonf484c312008-08-31 02:33:12 +0000760 if (!DeclAddress) {
761 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Anders Carlssonf484c312008-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 Stump1eb44332009-09-09 15:08:12 +0000767
Anders Carlssonf484c312008-08-31 02:33:12 +0000768 // Increment the counter.
Mike Stump1eb44332009-09-09 15:08:12 +0000769 Counter = Builder.CreateAdd(Counter,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000770 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000771 Builder.CreateStore(Counter, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Daniel Dunbar55e87422008-11-11 02:29:29 +0000773 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
774 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Anders Carlssone4b6d342009-02-10 05:52:02 +0000776 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000777
778 EmitStmt(S.getBody());
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Anders Carlssonf484c312008-08-31 02:33:12 +0000780 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Anders Carlssonf484c312008-08-31 02:33:12 +0000782 EmitBlock(AfterBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Daniel Dunbar55e87422008-11-11 02:29:29 +0000784 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000785
786 Counter = Builder.CreateLoad(CounterPtr);
787 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000788 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000789 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000790
791 // Fetch more elements.
792 EmitBlock(FetchMore);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
794 CountRV =
795 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000796 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000797 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000798 Collection, Args);
Anders Carlssonf484c312008-08-31 02:33:12 +0000799 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
800 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Anders Carlssonf484c312008-08-31 02:33:12 +0000802 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
803 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Anders Carlssonf484c312008-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 Stump1eb44332009-09-09 15:08:12 +0000812
Anders Carlssonf484c312008-08-31 02:33:12 +0000813 // Set the value to null.
Owen Andersonc9c88b42009-07-31 20:28:54 +0000814 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlssonf484c312008-08-31 02:33:12 +0000815 LV.getAddress());
816 }
817
818 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000819}
820
Mike Stump1eb44332009-09-09 15:08:12 +0000821void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000822 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000823}
824
Mike Stump1eb44332009-09-09 15:08:12 +0000825void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000826 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
827}
828
Chris Lattner10cac6f2008-11-15 21:26:17 +0000829void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +0000830 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000831 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000832}
833
Ted Kremenek2979ec72008-04-09 15:51:31 +0000834CGObjCRuntime::~CGObjCRuntime() {}