blob: 896d2207ea4bfb23a4d11c19942862ab9aefb1ca [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 const Expr *ReceiverExpr = E->getReceiver();
57 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000058 bool isClassMessage = false;
Chris Lattner8fdf3282008-06-24 17:04:18 +000059 // Find the receiver
60 llvm::Value *Receiver;
61 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000062 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
63
64 // Very special case, super send in class method. The receiver is
65 // self (the class object) and the send uses super semantics.
66 if (!OID) {
Chris Lattner92e62b02008-11-20 04:42:34 +000067 assert(E->getClassName()->isStr("super") &&
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000068 "Unexpected missing class interface in message send.");
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000069 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000070 Receiver = LoadObjCSelf();
71 } else {
72 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Daniel Dunbarf56f1912008-08-25 08:19:24 +000075 isClassMessage = true;
Douglas Gregorcd9b46e2008-11-04 14:56:14 +000076 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000077 isSuperMessage = true;
78 Receiver = LoadObjCSelf();
79 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000080 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000081 }
82
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000083 CallArgList Args;
Anders Carlsson131038e2009-04-18 20:29:27 +000084 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner8fdf3282008-06-24 17:04:18 +000086 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000087 // super is only valid in an Objective-C method
88 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000089 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000090 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
91 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000092 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000093 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000094 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000095 isClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +000096 Args,
97 E->getMethodDecl());
Chris Lattner8fdf3282008-06-24 17:04:18 +000098 }
Daniel Dunbard6c93d72009-09-17 04:01:22 +000099
Mike Stump1eb44332009-09-09 15:08:12 +0000100 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000101 Receiver, isClassMessage, Args,
102 E->getMethodDecl());
Anders Carlsson55085182007-08-21 17:43:55 +0000103}
104
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000105/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
106/// the LLVM function and sets the other context used by
107/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000108void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
109 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000110 FunctionArgList Args;
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000111 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000112
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000113 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
114 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000115
Mike Stump1eb44332009-09-09 15:08:12 +0000116 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbar7c086512008-09-09 23:14:03 +0000117 OMD->getSelfDecl()->getType()));
118 Args.push_back(std::make_pair(OMD->getCmdDecl(),
119 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000120
Chris Lattner89951a82009-02-20 18:43:26 +0000121 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
122 E = OMD->param_end(); PI != E; ++PI)
123 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000124
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000125 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000126}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000127
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000128/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000129/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000130void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel1d6a4512009-02-25 01:09:46 +0000131 // Check if we should generate debug info for this method.
Mike Stump1feade82009-08-26 22:31:08 +0000132 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
Devang Patel1d6a4512009-02-25 01:09:46 +0000133 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000134 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000135 EmitStmt(OMD->getBody());
136 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000137}
138
Mike Stumpf5408fe2009-05-16 07:57:57 +0000139// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
140// AST for the whole body we can just fall back to having a GenerateFunction
141// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000142
143/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000144/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
145/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000146void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
147 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000148 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000149 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
150 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
151 assert(OMD && "Invalid call to generate getter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000152 // FIXME: This is rather murky, we create this here since they will not have
153 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000154 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000155 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000156
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000157 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000158 // this. Non-atomic properties are directly evaluated.
159 // atomic 'copy' and 'retain' properties are also directly
160 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000161 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000162 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
163 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
164 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000165 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000166 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000168 if (!GetPropertyFn) {
169 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
170 FinishFunction();
171 return;
172 }
173
174 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
175 // FIXME: Can't this be simpler? This might even be worse than the
176 // corresponding gcc code.
177 CodeGenTypes &Types = CGM.getTypes();
178 ValueDecl *Cmd = OMD->getCmdDecl();
179 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
180 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000181 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000182 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000183 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000184 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000185 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000186 CallArgList Args;
187 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
188 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
189 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
190 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000191 // FIXME: We shouldn't need to get the function info here, the
192 // runtime already should have computed it to build the function.
Mike Stump1eb44332009-09-09 15:08:12 +0000193 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000194 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000195 // We need to fix the type here. Ivars with copy & retain are
196 // always objects so we don't need to worry about complex or
197 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000198 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000199 Types.ConvertType(PD->getType())));
200 EmitReturnOfRValue(RV, PD->getType());
201 } else {
Daniel Dunbar525c9b72009-04-21 01:19:28 +0000202 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000203 if (hasAggregateLLVMType(Ivar->getType())) {
204 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
Mike Stumpb3589f42009-07-30 22:28:39 +0000205 } else {
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000206 CodeGenTypes &Types = CGM.getTypes();
207 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
208 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump1eb44332009-09-09 15:08:12 +0000209 Types.ConvertType(PD->getType())));
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000210 EmitReturnOfRValue(RV, PD->getType());
211 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000212 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000213
214 FinishFunction();
215}
216
217/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000218/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
219/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000220void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
221 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000222 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000223 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
224 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
225 assert(OMD && "Invalid call to generate setter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000226 // FIXME: This is rather murky, we create this here since they will not have
227 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000228 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000229 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000230
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000231 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000232 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000233 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
234
235 // Determine if we should use an objc_setProperty call for
236 // this. Properties with 'copy' semantics always use it, as do
237 // non-atomic properties with 'release' semantics as long as we are
238 // not in gc-only mode.
239 if (IsCopy ||
240 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
241 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000242 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000243 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000245 if (!SetPropertyFn) {
246 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
247 FinishFunction();
248 return;
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
251 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000252 // <is-atomic>, <is-copy>).
253 // FIXME: Can't this be simpler? This might even be worse than the
254 // corresponding gcc code.
255 CodeGenTypes &Types = CGM.getTypes();
256 ValueDecl *Cmd = OMD->getCmdDecl();
257 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
258 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000259 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000260 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000261 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000262 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000263 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000264 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
265 Types.ConvertType(IdTy));
266 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000267 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000268 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000269 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000270 CallArgList Args;
271 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
272 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
273 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
274 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000275 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000276 getContext().BoolTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000277 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000278 getContext().BoolTy));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000279 // FIXME: We shouldn't need to get the function info here, the runtime
280 // already should have computed it to build the function.
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000281 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args), SetPropertyFn,
282 ReturnValueSlot(), Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000283 } else {
Daniel Dunbar45e84232009-10-27 19:21:30 +0000284 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000285 SourceLocation Loc = PD->getLocation();
286 ValueDecl *Self = OMD->getSelfDecl();
287 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
288 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000289 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000290 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000291 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
292
293 // The property type can differ from the ivar type in some situations with
294 // Objective-C pointer types, we can always bit cast the RHS in these cases.
295 if (getContext().getCanonicalType(Ivar->getType()) !=
296 getContext().getCanonicalType(ArgDecl->getType())) {
297 ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
298 false);
299 BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
300 Ivar->getType(), Loc);
301 EmitStmt(&Assign);
302 } else {
303 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
304 Ivar->getType(), Loc);
305 EmitStmt(&Assign);
306 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000307 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000308
309 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000310}
311
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000312llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000313 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000314 // See if we need to lazily forward self inside a block literal.
315 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000316 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000317}
318
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000319QualType CodeGenFunction::TypeOfSelfObject() {
320 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
321 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000322 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
323 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000324 return PTy->getPointeeType();
325}
326
Mike Stump1eb44332009-09-09 15:08:12 +0000327RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000328 const Selector &S) {
329 llvm::Value *Receiver = LoadObjCSelf();
330 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
331 bool isClassMessage = OMD->isClassMethod();
332 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000333 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000334 Exp->getType(),
335 S,
336 OMD->getClassInterface(),
337 isCategoryImpl,
338 Receiver,
339 isClassMessage,
340 CallArgList());
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000342}
343
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000344RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +0000345 Exp = Exp->IgnoreParens();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000346 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000347 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
348 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000349 if (isa<ObjCSuperExpr>(E->getBase()))
350 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000351 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000352 GenerateMessageSend(*this, Exp->getType(), S,
353 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000354 false, CallArgList());
Mike Stumpb3589f42009-07-30 22:28:39 +0000355 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000356 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000357 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000358 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000359 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000360 if (KE->getInterfaceDecl()) {
361 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000362 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000363 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000364 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump1eb44332009-09-09 15:08:12 +0000365 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000366 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000367 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000368 GenerateMessageSend(*this, Exp->getType(), S,
369 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000370 KE->getInterfaceDecl() != 0, CallArgList());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000371 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000372}
373
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000374void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
375 const Selector &S,
376 RValue Src) {
377 CallArgList Args;
378 llvm::Value *Receiver = LoadObjCSelf();
379 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
380 bool isClassMessage = OMD->isClassMethod();
381 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
382 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000383 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000384 Exp->getType(),
385 S,
386 OMD->getClassInterface(),
387 isCategoryImpl,
388 Receiver,
389 isClassMessage,
390 Args);
391 return;
392}
393
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000394void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000395 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000396 // FIXME: Split it into two separate routines.
397 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
398 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000399 if (isa<ObjCSuperExpr>(E->getBase())) {
400 EmitObjCSuperPropertySet(E, S, Src);
401 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000402 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000403 CallArgList Args;
404 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000405 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
406 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000407 false, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000408 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000409 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000410 Selector S = E->getSetterMethod()->getSelector();
411 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000412 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000413 if (E->getInterfaceDecl()) {
414 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000415 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000416 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000417 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000418 return;
Mike Stumpb3589f42009-07-30 22:28:39 +0000419 } else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000420 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000421 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000422 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
423 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000424 E->getInterfaceDecl() != 0, Args);
Mike Stumpb3589f42009-07-30 22:28:39 +0000425 } else
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000426 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000427}
428
Chris Lattner74391b42009-03-22 21:03:39 +0000429void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000430 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000431 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000432 llvm::Value *DeclAddress;
433 QualType ElementTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000435 if (!EnumerationMutationFn) {
436 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
437 return;
438 }
439
Anders Carlssonf484c312008-08-31 02:33:12 +0000440 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
441 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000442 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000443 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000444 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000445 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000446 } else {
447 ElementTy = cast<Expr>(S.getElement())->getType();
448 DeclAddress = 0;
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Anders Carlssonf484c312008-08-31 02:33:12 +0000451 // Fast enumeration state.
452 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Mike Stump1eb44332009-09-09 15:08:12 +0000453 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
Anders Carlssonf484c312008-08-31 02:33:12 +0000454 "state.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000455 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000456 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Anders Carlssonf484c312008-08-31 02:33:12 +0000458 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000459 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Anders Carlssonf484c312008-08-31 02:33:12 +0000461 // Get selector
462 llvm::SmallVector<IdentifierInfo*, 3> II;
463 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
464 II.push_back(&CGM.getContext().Idents.get("objects"));
465 II.push_back(&CGM.getContext().Idents.get("count"));
Mike Stump1eb44332009-09-09 15:08:12 +0000466 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000467 &II[0]);
468
469 QualType ItemsTy =
470 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000471 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000472 ArrayType::Normal, 0);
473 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Anders Carlssonf484c312008-08-31 02:33:12 +0000475 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Anders Carlssonf484c312008-08-31 02:33:12 +0000477 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000478 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000479 getContext().getPointerType(StateTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000480
481 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000482 getContext().getPointerType(ItemsTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlssonf484c312008-08-31 02:33:12 +0000484 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000485 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump1eb44332009-09-09 15:08:12 +0000486 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000487 getContext().UnsignedLongTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000488
489 RValue CountRV =
490 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000491 getContext().UnsignedLongTy,
492 FastEnumSel,
493 Collection, false, Args);
494
495 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
496 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Daniel Dunbar55e87422008-11-11 02:29:29 +0000498 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
499 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Anders Carlssonf484c312008-08-31 02:33:12 +0000501 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000502 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000503
504 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000505 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000506
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000507 EmitBlock(SetStartMutations);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
509 llvm::Value *StartMutationsPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000510 CreateTempAlloca(UnsignedLongLTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
512 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000513 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000514 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000515 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000516
517 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000518 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000520 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Daniel Dunbar55e87422008-11-11 02:29:29 +0000522 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000523 EmitBlock(LoopStart);
524
Anders Carlssonf484c312008-08-31 02:33:12 +0000525 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
526 Builder.CreateStore(Zero, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
528 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000529 EmitBlock(LoopBody);
530
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000531 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
532 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
533
Mike Stump1eb44332009-09-09 15:08:12 +0000534 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000535 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000536 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000537 StartMutations,
538 "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000539
540
Daniel Dunbar55e87422008-11-11 02:29:29 +0000541 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
542 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000544 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000546 EmitBlock(WasMutated);
547 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000548 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000549 ConvertType(getContext().getObjCIdType()),
550 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000551 CallArgList Args2;
Mike Stump1eb44332009-09-09 15:08:12 +0000552 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000553 getContext().getObjCIdType()));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000554 // FIXME: We shouldn't need to get the function info here, the runtime already
555 // should have computed it to build the function.
Mike Stump1eb44332009-09-09 15:08:12 +0000556 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000557 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000559 EmitBlock(WasNotMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
561 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000562 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
563
564 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
565
566 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
567 "stateitems");
568
Mike Stump1eb44332009-09-09 15:08:12 +0000569 llvm::Value *CurrentItemPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000570 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Anders Carlssonf484c312008-08-31 02:33:12 +0000572 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Anders Carlssonf484c312008-08-31 02:33:12 +0000574 // Cast the item to the right type.
575 CurrentItem = Builder.CreateBitCast(CurrentItem,
576 ConvertType(ElementTy), "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Anders Carlssonf484c312008-08-31 02:33:12 +0000578 if (!DeclAddress) {
579 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Anders Carlssonf484c312008-08-31 02:33:12 +0000581 // Set the value to null.
582 Builder.CreateStore(CurrentItem, LV.getAddress());
583 } else
584 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Anders Carlssonf484c312008-08-31 02:33:12 +0000586 // Increment the counter.
Mike Stump1eb44332009-09-09 15:08:12 +0000587 Counter = Builder.CreateAdd(Counter,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000588 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000589 Builder.CreateStore(Counter, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Daniel Dunbar55e87422008-11-11 02:29:29 +0000591 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
592 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Anders Carlssone4b6d342009-02-10 05:52:02 +0000594 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000595
596 EmitStmt(S.getBody());
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Anders Carlssonf484c312008-08-31 02:33:12 +0000598 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Anders Carlssonf484c312008-08-31 02:33:12 +0000600 EmitBlock(AfterBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Daniel Dunbar55e87422008-11-11 02:29:29 +0000602 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000603
604 Counter = Builder.CreateLoad(CounterPtr);
605 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000606 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000607 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000608
609 // Fetch more elements.
610 EmitBlock(FetchMore);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
612 CountRV =
613 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000614 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000615 FastEnumSel,
Anders Carlssonf484c312008-08-31 02:33:12 +0000616 Collection, false, Args);
617 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
618 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anders Carlssonf484c312008-08-31 02:33:12 +0000620 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
621 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Anders Carlssonf484c312008-08-31 02:33:12 +0000623 // No more elements.
624 EmitBlock(NoElements);
625
626 if (!DeclAddress) {
627 // If the element was not a declaration, set it to be null.
628
629 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Anders Carlssonf484c312008-08-31 02:33:12 +0000631 // Set the value to null.
Owen Andersonc9c88b42009-07-31 20:28:54 +0000632 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlssonf484c312008-08-31 02:33:12 +0000633 LV.getAddress());
634 }
635
636 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000637}
638
Mike Stump1eb44332009-09-09 15:08:12 +0000639void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000640 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000641}
642
Mike Stump1eb44332009-09-09 15:08:12 +0000643void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000644 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
645}
646
Chris Lattner10cac6f2008-11-15 21:26:17 +0000647void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +0000648 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000649 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000650}
651
Ted Kremenek2979ec72008-04-09 15:51:31 +0000652CGObjCRuntime::~CGObjCRuntime() {}