blob: e5da29a61b50db4592293f1d5cc43888b1f1ae35 [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;
Chris Lattner8fdf3282008-06-24 17:04:18 +000058 // Find the receiver
59 llvm::Value *Receiver;
Douglas Gregor04badcf2010-04-21 00:45:42 +000060 switch (E->getReceiverKind()) {
61 case ObjCMessageExpr::Instance:
62 Receiver = EmitScalarExpr(E->getInstanceReceiver());
63 break;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000064
Douglas Gregor04badcf2010-04-21 00:45:42 +000065 case ObjCMessageExpr::Class: {
66 const ObjCInterfaceType *IFace
67 = E->getClassReceiver()->getAs<ObjCInterfaceType>();
68 assert(IFace && "Invalid Objective-C class message send");
69 Receiver = Runtime.GetClass(Builder, IFace->getDecl());
Daniel Dunbarf56f1912008-08-25 08:19:24 +000070 isClassMessage = true;
Douglas Gregor04badcf2010-04-21 00:45:42 +000071 break;
72 }
73
74 case ObjCMessageExpr::SuperInstance:
Chris Lattner8fdf3282008-06-24 17:04:18 +000075 Receiver = LoadObjCSelf();
Douglas Gregor04badcf2010-04-21 00:45:42 +000076 isSuperMessage = true;
77 break;
78
79 case ObjCMessageExpr::SuperClass:
80 Receiver = LoadObjCSelf();
81 isSuperMessage = true;
82 isClassMessage = true;
83 break;
Chris Lattner8fdf3282008-06-24 17:04:18 +000084 }
85
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000086 CallArgList Args;
Anders Carlsson131038e2009-04-18 20:29:27 +000087 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner8fdf3282008-06-24 17:04:18 +000089 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000090 // super is only valid in an Objective-C method
91 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000092 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000093 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
94 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000095 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000096 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000097 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000098 isClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +000099 Args,
100 E->getMethodDecl());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000101 }
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000102
Mike Stump1eb44332009-09-09 15:08:12 +0000103 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000104 Receiver, isClassMessage, Args,
105 E->getMethodDecl());
Anders Carlsson55085182007-08-21 17:43:55 +0000106}
107
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000108/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
109/// the LLVM function and sets the other context used by
110/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000111void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
112 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000113 FunctionArgList Args;
Devang Patel4800ea62010-04-05 21:09:15 +0000114 // Check if we should generate debug info for this method.
115 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
116 DebugInfo = CGM.getDebugInfo();
117
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000118 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000119
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000120 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
121 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000122
Mike Stump1eb44332009-09-09 15:08:12 +0000123 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbar7c086512008-09-09 23:14:03 +0000124 OMD->getSelfDecl()->getType()));
125 Args.push_back(std::make_pair(OMD->getCmdDecl(),
126 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000127
Chris Lattner89951a82009-02-20 18:43:26 +0000128 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
129 E = OMD->param_end(); PI != E; ++PI)
130 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000131
Devang Patela92d6132010-02-15 18:08:38 +0000132 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000133}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000134
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000135/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000136/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000137void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000138 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000139 EmitStmt(OMD->getBody());
140 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000141}
142
Mike Stumpf5408fe2009-05-16 07:57:57 +0000143// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
144// AST for the whole body we can just fall back to having a GenerateFunction
145// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000146
147/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000148/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
149/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000150void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
151 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000152 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000153 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000154 bool IsAtomic =
155 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000156 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
157 assert(OMD && "Invalid call to generate getter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000158 // FIXME: This is rather murky, we create this here since they will not have
159 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000160 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000161 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000162
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000163 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000164 // this. Non-atomic properties are directly evaluated.
165 // atomic 'copy' and 'retain' properties are also directly
166 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000167 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000168 IsAtomic &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000169 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
170 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000171 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000172 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000174 if (!GetPropertyFn) {
175 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
176 FinishFunction();
177 return;
178 }
179
180 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
181 // FIXME: Can't this be simpler? This might even be worse than the
182 // corresponding gcc code.
183 CodeGenTypes &Types = CGM.getTypes();
184 ValueDecl *Cmd = OMD->getCmdDecl();
185 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
186 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000187 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000188 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000189 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000190 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000191 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000192 CallArgList Args;
193 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
194 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
195 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
196 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000197 // FIXME: We shouldn't need to get the function info here, the
198 // runtime already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000199 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000200 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000201 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000202 // We need to fix the type here. Ivars with copy & retain are
203 // always objects so we don't need to worry about complex or
204 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000205 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000206 Types.ConvertType(PD->getType())));
207 EmitReturnOfRValue(RV, PD->getType());
208 } else {
Daniel Dunbar525c9b72009-04-21 01:19:28 +0000209 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000210 if (Ivar->getType()->isAnyComplexType()) {
211 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()) {
220 llvm::Value *GetCopyStructFn =
221 CGM.getObjCRuntime().GetCopyStructFunction();
222 CodeGenTypes &Types = CGM.getTypes();
223 // objc_copyStruct (ReturnValue, &structIvar,
224 // sizeof (Type of Ivar), isAtomic, false);
225 CallArgList Args;
226 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
227 Types.ConvertType(getContext().VoidPtrTy)));
228 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
229 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
230 Types.ConvertType(getContext().VoidPtrTy)));
231 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
232 // sizeof (Type of Ivar)
233 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
234 llvm::Value *SizeVal =
235 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
236 Args.push_back(std::make_pair(RValue::get(SizeVal),
237 getContext().LongTy));
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000238 llvm::Value *isAtomic =
Fariborz Jahanian08adf322010-04-13 18:43:37 +0000239 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
240 IsAtomic ? 1 : 0);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000241 Args.push_back(std::make_pair(RValue::get(isAtomic),
242 getContext().BoolTy));
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000243 llvm::Value *hasStrong =
244 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
245 IsStrong ? 1 : 0);
246 Args.push_back(std::make_pair(RValue::get(hasStrong),
247 getContext().BoolTy));
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000248 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
249 FunctionType::ExtInfo()),
250 GetCopyStructFn, ReturnValueSlot(), Args);
251 }
252 else
253 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
Mike Stumpb3589f42009-07-30 22:28:39 +0000254 } else {
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000255 CodeGenTypes &Types = CGM.getTypes();
256 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
257 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump1eb44332009-09-09 15:08:12 +0000258 Types.ConvertType(PD->getType())));
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000259 EmitReturnOfRValue(RV, PD->getType());
260 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000261 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000262
263 FinishFunction();
264}
265
266/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000267/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
268/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000269void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
270 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000271 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000272 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
273 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
274 assert(OMD && "Invalid call to generate setter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000275 // FIXME: This is rather murky, we create this here since they will not have
276 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000277 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000278 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000279
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000280 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000281 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000282 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
283
284 // Determine if we should use an objc_setProperty call for
285 // this. Properties with 'copy' semantics always use it, as do
286 // non-atomic properties with 'release' semantics as long as we are
287 // not in gc-only mode.
288 if (IsCopy ||
289 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
290 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000291 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000292 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000294 if (!SetPropertyFn) {
295 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
296 FinishFunction();
297 return;
298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
300 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000301 // <is-atomic>, <is-copy>).
302 // FIXME: Can't this be simpler? This might even be worse than the
303 // corresponding gcc code.
304 CodeGenTypes &Types = CGM.getTypes();
305 ValueDecl *Cmd = OMD->getCmdDecl();
306 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
307 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000308 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000309 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000310 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000311 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000312 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000313 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
314 Types.ConvertType(IdTy));
315 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000316 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000317 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000318 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000319 CallArgList Args;
320 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
321 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
322 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
323 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000324 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000325 getContext().BoolTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000326 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000327 getContext().BoolTy));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000328 // FIXME: We shouldn't need to get the function info here, the runtime
329 // already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000330 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000331 FunctionType::ExtInfo()),
332 SetPropertyFn,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000333 ReturnValueSlot(), Args);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000334 } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) &&
335 !Ivar->getType()->isAnyComplexType() &&
336 IndirectObjCSetterArg(*CurFnInfo)
337 && CGM.getObjCRuntime().GetCopyStructFunction()) {
338 // objc_copyStruct (&structIvar, &Arg,
339 // sizeof (struct something), true, false);
340 llvm::Value *GetCopyStructFn =
341 CGM.getObjCRuntime().GetCopyStructFunction();
342 CodeGenTypes &Types = CGM.getTypes();
343 CallArgList Args;
344 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
345 RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
346 Types.ConvertType(getContext().VoidPtrTy)));
347 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
348 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
349 llvm::Value *ArgAsPtrTy =
350 Builder.CreateBitCast(Arg,
351 Types.ConvertType(getContext().VoidPtrTy));
352 RV = RValue::get(ArgAsPtrTy);
353 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
354 // sizeof (Type of Ivar)
355 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
356 llvm::Value *SizeVal =
357 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
358 Args.push_back(std::make_pair(RValue::get(SizeVal),
359 getContext().LongTy));
360 llvm::Value *True =
361 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
362 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
363 llvm::Value *False =
364 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
365 Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
366 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
367 FunctionType::ExtInfo()),
368 GetCopyStructFn, ReturnValueSlot(), Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000369 } else {
Daniel Dunbar45e84232009-10-27 19:21:30 +0000370 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000371 SourceLocation Loc = PD->getLocation();
372 ValueDecl *Self = OMD->getSelfDecl();
373 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
374 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000375 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000376 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000377 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
378
379 // The property type can differ from the ivar type in some situations with
380 // Objective-C pointer types, we can always bit cast the RHS in these cases.
381 if (getContext().getCanonicalType(Ivar->getType()) !=
382 getContext().getCanonicalType(ArgDecl->getType())) {
383 ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
384 false);
385 BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
386 Ivar->getType(), Loc);
387 EmitStmt(&Assign);
388 } else {
389 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
390 Ivar->getType(), Loc);
391 EmitStmt(&Assign);
392 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000393 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000394
395 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000396}
397
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000398bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
399 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
400 it++; it++;
401 const ABIArgInfo &AI = it->info;
402 // FIXME. Is this sufficient check?
403 return (AI.getKind() == ABIArgInfo::Indirect);
404}
405
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000406bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
407 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
408 return false;
409 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
410 return FDTTy->getDecl()->hasObjectMember();
411 return false;
412}
413
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000414llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000415 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000416 // See if we need to lazily forward self inside a block literal.
417 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000418 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000419}
420
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000421QualType CodeGenFunction::TypeOfSelfObject() {
422 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
423 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000424 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
425 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000426 return PTy->getPointeeType();
427}
428
Mike Stump1eb44332009-09-09 15:08:12 +0000429RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000430 const Selector &S) {
431 llvm::Value *Receiver = LoadObjCSelf();
432 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
433 bool isClassMessage = OMD->isClassMethod();
434 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000435 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000436 Exp->getType(),
437 S,
438 OMD->getClassInterface(),
439 isCategoryImpl,
440 Receiver,
441 isClassMessage,
442 CallArgList());
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000444}
445
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000446RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +0000447 Exp = Exp->IgnoreParens();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000448 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000449 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
450 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000451 if (isa<ObjCSuperExpr>(E->getBase()))
452 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000453 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000454 GenerateMessageSend(*this, Exp->getType(), S,
455 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000456 false, CallArgList());
Mike Stumpb3589f42009-07-30 22:28:39 +0000457 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000458 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000459 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000460 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000461 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000462 if (KE->getInterfaceDecl()) {
463 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000464 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000465 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000466 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump1eb44332009-09-09 15:08:12 +0000467 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000468 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000469 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000470 GenerateMessageSend(*this, Exp->getType(), S,
471 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000472 KE->getInterfaceDecl() != 0, CallArgList());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000473 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000474}
475
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000476void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
477 const Selector &S,
478 RValue Src) {
479 CallArgList Args;
480 llvm::Value *Receiver = LoadObjCSelf();
481 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
482 bool isClassMessage = OMD->isClassMethod();
483 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
484 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000485 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000486 Exp->getType(),
487 S,
488 OMD->getClassInterface(),
489 isCategoryImpl,
490 Receiver,
491 isClassMessage,
492 Args);
493 return;
494}
495
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000496void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000497 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000498 // FIXME: Split it into two separate routines.
499 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
500 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000501 if (isa<ObjCSuperExpr>(E->getBase())) {
502 EmitObjCSuperPropertySet(E, S, Src);
503 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000504 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000505 CallArgList Args;
506 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000507 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
508 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000509 false, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000510 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000511 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000512 Selector S = E->getSetterMethod()->getSelector();
513 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000514 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000515 if (E->getInterfaceDecl()) {
516 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000517 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000518 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000519 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000520 return;
Mike Stumpb3589f42009-07-30 22:28:39 +0000521 } else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000522 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000523 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000524 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
525 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000526 E->getInterfaceDecl() != 0, Args);
Mike Stumpb3589f42009-07-30 22:28:39 +0000527 } else
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000528 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000529}
530
Chris Lattner74391b42009-03-22 21:03:39 +0000531void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000532 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000533 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000534 llvm::Value *DeclAddress;
535 QualType ElementTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000537 if (!EnumerationMutationFn) {
538 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
539 return;
540 }
541
Anders Carlssonf484c312008-08-31 02:33:12 +0000542 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
543 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000544 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000545 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000546 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000547 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000548 } else {
549 ElementTy = cast<Expr>(S.getElement())->getType();
550 DeclAddress = 0;
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Anders Carlssonf484c312008-08-31 02:33:12 +0000553 // Fast enumeration state.
554 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar195337d2010-02-09 02:48:28 +0000555 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000556 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Anders Carlssonf484c312008-08-31 02:33:12 +0000558 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000559 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Anders Carlssonf484c312008-08-31 02:33:12 +0000561 // Get selector
Benjamin Kramerad468862010-03-30 11:36:44 +0000562 IdentifierInfo *II[] = {
563 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
564 &CGM.getContext().Idents.get("objects"),
565 &CGM.getContext().Idents.get("count")
566 };
567 Selector FastEnumSel =
568 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlssonf484c312008-08-31 02:33:12 +0000569
570 QualType ItemsTy =
571 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000572 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000573 ArrayType::Normal, 0);
Daniel Dunbar195337d2010-02-09 02:48:28 +0000574 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Anders Carlssonf484c312008-08-31 02:33:12 +0000576 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Anders Carlssonf484c312008-08-31 02:33:12 +0000578 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000579 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000580 getContext().getPointerType(StateTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000583 getContext().getPointerType(ItemsTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlssonf484c312008-08-31 02:33:12 +0000585 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000586 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump1eb44332009-09-09 15:08:12 +0000587 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000588 getContext().UnsignedLongTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000589
590 RValue CountRV =
591 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000592 getContext().UnsignedLongTy,
593 FastEnumSel,
594 Collection, false, Args);
595
Daniel Dunbar195337d2010-02-09 02:48:28 +0000596 llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy,
597 "limit.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000598 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Daniel Dunbar55e87422008-11-11 02:29:29 +0000600 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
601 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Anders Carlssonf484c312008-08-31 02:33:12 +0000603 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000604 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000605
606 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000607 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000608
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000609 EmitBlock(SetStartMutations);
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Daniel Dunbar195337d2010-02-09 02:48:28 +0000611 llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
613 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000614 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000615 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000616 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000617
618 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000619 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000621 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Daniel Dunbar55e87422008-11-11 02:29:29 +0000623 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000624 EmitBlock(LoopStart);
625
Daniel Dunbar195337d2010-02-09 02:48:28 +0000626 llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy,
627 "counter.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000628 Builder.CreateStore(Zero, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
630 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000631 EmitBlock(LoopBody);
632
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000633 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
634 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
635
Mike Stump1eb44332009-09-09 15:08:12 +0000636 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000637 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000638 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000639 StartMutations,
640 "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642
Daniel Dunbar55e87422008-11-11 02:29:29 +0000643 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
644 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000646 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000648 EmitBlock(WasMutated);
649 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000650 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000651 ConvertType(getContext().getObjCIdType()),
652 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000653 CallArgList Args2;
Mike Stump1eb44332009-09-09 15:08:12 +0000654 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000655 getContext().getObjCIdType()));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000656 // FIXME: We shouldn't need to get the function info here, the runtime already
657 // should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000658 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindola264ba482010-03-30 20:24:48 +0000659 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000660 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000662 EmitBlock(WasNotMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
664 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000665 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
666
667 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
668
669 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
670 "stateitems");
671
Mike Stump1eb44332009-09-09 15:08:12 +0000672 llvm::Value *CurrentItemPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000673 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Anders Carlssonf484c312008-08-31 02:33:12 +0000675 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Anders Carlssonf484c312008-08-31 02:33:12 +0000677 // Cast the item to the right type.
678 CurrentItem = Builder.CreateBitCast(CurrentItem,
679 ConvertType(ElementTy), "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Anders Carlssonf484c312008-08-31 02:33:12 +0000681 if (!DeclAddress) {
682 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Anders Carlssonf484c312008-08-31 02:33:12 +0000684 // Set the value to null.
685 Builder.CreateStore(CurrentItem, LV.getAddress());
686 } else
687 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Anders Carlssonf484c312008-08-31 02:33:12 +0000689 // Increment the counter.
Mike Stump1eb44332009-09-09 15:08:12 +0000690 Counter = Builder.CreateAdd(Counter,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000691 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000692 Builder.CreateStore(Counter, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Daniel Dunbar55e87422008-11-11 02:29:29 +0000694 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
695 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Anders Carlssone4b6d342009-02-10 05:52:02 +0000697 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000698
699 EmitStmt(S.getBody());
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Anders Carlssonf484c312008-08-31 02:33:12 +0000701 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Anders Carlssonf484c312008-08-31 02:33:12 +0000703 EmitBlock(AfterBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Daniel Dunbar55e87422008-11-11 02:29:29 +0000705 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000706
707 Counter = Builder.CreateLoad(CounterPtr);
708 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000709 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000710 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000711
712 // Fetch more elements.
713 EmitBlock(FetchMore);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
715 CountRV =
716 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000717 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000718 FastEnumSel,
Anders Carlssonf484c312008-08-31 02:33:12 +0000719 Collection, false, Args);
720 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
721 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Anders Carlssonf484c312008-08-31 02:33:12 +0000723 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
724 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Anders Carlssonf484c312008-08-31 02:33:12 +0000726 // No more elements.
727 EmitBlock(NoElements);
728
729 if (!DeclAddress) {
730 // If the element was not a declaration, set it to be null.
731
732 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Anders Carlssonf484c312008-08-31 02:33:12 +0000734 // Set the value to null.
Owen Andersonc9c88b42009-07-31 20:28:54 +0000735 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlssonf484c312008-08-31 02:33:12 +0000736 LV.getAddress());
737 }
738
739 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000740}
741
Mike Stump1eb44332009-09-09 15:08:12 +0000742void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000743 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000744}
745
Mike Stump1eb44332009-09-09 15:08:12 +0000746void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000747 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
748}
749
Chris Lattner10cac6f2008-11-15 21:26:17 +0000750void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +0000751 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000752 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000753}
754
Ted Kremenek2979ec72008-04-09 15:51:31 +0000755CGObjCRuntime::~CGObjCRuntime() {}