blob: cadba328bf12ab92658c0d2c5bbfd0247e5a738b [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{
Steve Naroff33fdb732009-03-31 16:53:37 +000029 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(E);
Daniel Dunbared7c6182008-08-20 00:28:19 +000030 // FIXME: This bitcast should just be made an invariant on the Runtime.
Owen Anderson3c4972d2009-07-29 18:54:39 +000031 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-06-24 17:04:18 +000032}
33
34/// Emit a selector.
35llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
36 // Untyped selector.
37 // Note that this implementation allows for non-constant strings to be passed
38 // as arguments to @selector(). Currently, the only thing preventing this
39 // behaviour is the type checking in the front end.
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000040 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000041}
42
Daniel Dunbared7c6182008-08-20 00:28:19 +000043llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
44 // FIXME: This should pass the Decl not the name.
45 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
46}
Chris Lattner8fdf3282008-06-24 17:04:18 +000047
48
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000049RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000050 // Only the lookup mechanism and first two arguments of the method
51 // implementation vary between runtimes. We can get the receiver and
52 // arguments in generic code.
Mike Stump1eb44332009-09-09 15:08:12 +000053
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000054 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000055 const Expr *ReceiverExpr = E->getReceiver();
56 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;
60 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000061 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
62
63 // Very special case, super send in class method. The receiver is
64 // self (the class object) and the send uses super semantics.
65 if (!OID) {
Chris Lattner92e62b02008-11-20 04:42:34 +000066 assert(E->getClassName()->isStr("super") &&
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000067 "Unexpected missing class interface in message send.");
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000068 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000069 Receiver = LoadObjCSelf();
70 } else {
71 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000072 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Daniel Dunbarf56f1912008-08-25 08:19:24 +000074 isClassMessage = true;
Douglas Gregorcd9b46e2008-11-04 14:56:14 +000075 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000076 isSuperMessage = true;
77 Receiver = LoadObjCSelf();
78 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000079 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000080 }
81
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000082 CallArgList Args;
Anders Carlsson131038e2009-04-18 20:29:27 +000083 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner8fdf3282008-06-24 17:04:18 +000085 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000086 // super is only valid in an Objective-C method
87 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000088 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000089 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
90 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000091 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000092 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000093 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000094 isClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +000095 Args,
96 E->getMethodDecl());
Chris Lattner8fdf3282008-06-24 17:04:18 +000097 }
Daniel Dunbard6c93d72009-09-17 04:01:22 +000098
Mike Stump1eb44332009-09-09 15:08:12 +000099 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000100 Receiver, isClassMessage, Args,
101 E->getMethodDecl());
Anders Carlsson55085182007-08-21 17:43:55 +0000102}
103
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000104/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
105/// the LLVM function and sets the other context used by
106/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000107void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
108 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000109 FunctionArgList Args;
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000110 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000111
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000112 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
113 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000114
Mike Stump1eb44332009-09-09 15:08:12 +0000115 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbar7c086512008-09-09 23:14:03 +0000116 OMD->getSelfDecl()->getType()));
117 Args.push_back(std::make_pair(OMD->getCmdDecl(),
118 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000119
Chris Lattner89951a82009-02-20 18:43:26 +0000120 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
121 E = OMD->param_end(); PI != E; ++PI)
122 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000123
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000124 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000125}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000126
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000127/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000128/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000129void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel1d6a4512009-02-25 01:09:46 +0000130 // Check if we should generate debug info for this method.
Mike Stump1feade82009-08-26 22:31:08 +0000131 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
Devang Patel1d6a4512009-02-25 01:09:46 +0000132 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000133 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000134 EmitStmt(OMD->getBody());
135 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000136}
137
Mike Stumpf5408fe2009-05-16 07:57:57 +0000138// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
139// AST for the whole body we can just fall back to having a GenerateFunction
140// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000141
142/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000143/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
144/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000145void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
146 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000147 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000148 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
149 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
150 assert(OMD && "Invalid call to generate getter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000151 // FIXME: This is rather murky, we create this here since they will not have
152 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000153 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000154 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000155
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000156 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000157 // this. Non-atomic properties are directly evaluated.
158 // atomic 'copy' and 'retain' properties are also directly
159 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000160 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000161 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
162 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
163 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000164 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000165 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000167 if (!GetPropertyFn) {
168 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
169 FinishFunction();
170 return;
171 }
172
173 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
174 // FIXME: Can't this be simpler? This might even be worse than the
175 // corresponding gcc code.
176 CodeGenTypes &Types = CGM.getTypes();
177 ValueDecl *Cmd = OMD->getCmdDecl();
178 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
179 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000180 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000181 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000182 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000183 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000184 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000185 CallArgList Args;
186 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
187 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
188 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
189 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000190 // FIXME: We shouldn't need to get the function info here, the
191 // runtime already should have computed it to build the function.
Mike Stump1eb44332009-09-09 15:08:12 +0000192 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar88b53962009-02-02 22:03:45 +0000193 GetPropertyFn, Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000194 // We need to fix the type here. Ivars with copy & retain are
195 // always objects so we don't need to worry about complex or
196 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000197 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000198 Types.ConvertType(PD->getType())));
199 EmitReturnOfRValue(RV, PD->getType());
200 } else {
Daniel Dunbar525c9b72009-04-21 01:19:28 +0000201 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000202 if (hasAggregateLLVMType(Ivar->getType())) {
203 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
Mike Stumpb3589f42009-07-30 22:28:39 +0000204 } else {
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000205 CodeGenTypes &Types = CGM.getTypes();
206 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
207 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump1eb44332009-09-09 15:08:12 +0000208 Types.ConvertType(PD->getType())));
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000209 EmitReturnOfRValue(RV, PD->getType());
210 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000211 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000212
213 FinishFunction();
214}
215
216/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000217/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
218/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000219void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
220 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000221 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000222 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
223 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
224 assert(OMD && "Invalid call to generate setter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000225 // FIXME: This is rather murky, we create this here since they will not have
226 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000227 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000228 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000229
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000230 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000231 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000232 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
233
234 // Determine if we should use an objc_setProperty call for
235 // this. Properties with 'copy' semantics always use it, as do
236 // non-atomic properties with 'release' semantics as long as we are
237 // not in gc-only mode.
238 if (IsCopy ||
239 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
240 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000241 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000242 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000244 if (!SetPropertyFn) {
245 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
246 FinishFunction();
247 return;
248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
250 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000251 // <is-atomic>, <is-copy>).
252 // FIXME: Can't this be simpler? This might even be worse than the
253 // corresponding gcc code.
254 CodeGenTypes &Types = CGM.getTypes();
255 ValueDecl *Cmd = OMD->getCmdDecl();
256 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
257 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000258 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000259 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000260 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000261 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000262 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000263 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
264 Types.ConvertType(IdTy));
265 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000266 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000267 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000268 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000269 CallArgList Args;
270 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
271 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
272 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
273 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000274 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000275 getContext().BoolTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000276 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000277 getContext().BoolTy));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000278 // FIXME: We shouldn't need to get the function info here, the runtime
279 // already should have computed it to build the function.
Mike Stump1eb44332009-09-09 15:08:12 +0000280 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000281 SetPropertyFn, Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000282 } else {
283 SourceLocation Loc = PD->getLocation();
284 ValueDecl *Self = OMD->getSelfDecl();
285 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
286 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000287 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000288 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000289 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000290 true, true);
291 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
292 Ivar->getType(), Loc);
293 EmitStmt(&Assign);
294 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000295
296 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000297}
298
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000299llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000300 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000301 // See if we need to lazily forward self inside a block literal.
302 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000303 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000304}
305
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000306QualType CodeGenFunction::TypeOfSelfObject() {
307 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
308 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000309 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
310 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000311 return PTy->getPointeeType();
312}
313
Mike Stump1eb44332009-09-09 15:08:12 +0000314RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000315 const Selector &S) {
316 llvm::Value *Receiver = LoadObjCSelf();
317 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
318 bool isClassMessage = OMD->isClassMethod();
319 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000320 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000321 Exp->getType(),
322 S,
323 OMD->getClassInterface(),
324 isCategoryImpl,
325 Receiver,
326 isClassMessage,
327 CallArgList());
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000329}
330
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000331RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +0000332 Exp = Exp->IgnoreParens();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000333 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000334 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
335 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000336 if (isa<ObjCSuperExpr>(E->getBase()))
337 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000338 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000339 GenerateMessageSend(*this, Exp->getType(), S,
340 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000341 false, CallArgList());
Mike Stumpb3589f42009-07-30 22:28:39 +0000342 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000343 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000344 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000345 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000346 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000347 if (KE->getInterfaceDecl()) {
348 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000349 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000350 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000351 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump1eb44332009-09-09 15:08:12 +0000352 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000353 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000354 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000355 GenerateMessageSend(*this, Exp->getType(), S,
356 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000357 KE->getInterfaceDecl() != 0, CallArgList());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000358 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000359}
360
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000361void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
362 const Selector &S,
363 RValue Src) {
364 CallArgList Args;
365 llvm::Value *Receiver = LoadObjCSelf();
366 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
367 bool isClassMessage = OMD->isClassMethod();
368 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
369 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000370 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000371 Exp->getType(),
372 S,
373 OMD->getClassInterface(),
374 isCategoryImpl,
375 Receiver,
376 isClassMessage,
377 Args);
378 return;
379}
380
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000381void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000382 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000383 // FIXME: Split it into two separate routines.
384 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
385 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000386 if (isa<ObjCSuperExpr>(E->getBase())) {
387 EmitObjCSuperPropertySet(E, S, Src);
388 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000389 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000390 CallArgList Args;
391 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000392 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
393 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000394 false, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000395 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000396 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000397 Selector S = E->getSetterMethod()->getSelector();
398 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000399 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000400 if (E->getInterfaceDecl()) {
401 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000402 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000403 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000404 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000405 return;
Mike Stumpb3589f42009-07-30 22:28:39 +0000406 } else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000407 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000408 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000409 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
410 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000411 E->getInterfaceDecl() != 0, Args);
Mike Stumpb3589f42009-07-30 22:28:39 +0000412 } else
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000413 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000414}
415
Chris Lattner74391b42009-03-22 21:03:39 +0000416void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000417 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000418 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000419 llvm::Value *DeclAddress;
420 QualType ElementTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000422 if (!EnumerationMutationFn) {
423 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
424 return;
425 }
426
Anders Carlssonf484c312008-08-31 02:33:12 +0000427 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
428 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000429 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000430 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000431 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000432 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000433 } else {
434 ElementTy = cast<Expr>(S.getElement())->getType();
435 DeclAddress = 0;
436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlssonf484c312008-08-31 02:33:12 +0000438 // Fast enumeration state.
439 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Mike Stump1eb44332009-09-09 15:08:12 +0000440 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
Anders Carlssonf484c312008-08-31 02:33:12 +0000441 "state.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000442 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000443 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Anders Carlssonf484c312008-08-31 02:33:12 +0000445 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000446 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Anders Carlssonf484c312008-08-31 02:33:12 +0000448 // Get selector
449 llvm::SmallVector<IdentifierInfo*, 3> II;
450 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
451 II.push_back(&CGM.getContext().Idents.get("objects"));
452 II.push_back(&CGM.getContext().Idents.get("count"));
Mike Stump1eb44332009-09-09 15:08:12 +0000453 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000454 &II[0]);
455
456 QualType ItemsTy =
457 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000458 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000459 ArrayType::Normal, 0);
460 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Anders Carlssonf484c312008-08-31 02:33:12 +0000462 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Anders Carlssonf484c312008-08-31 02:33:12 +0000464 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000465 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000466 getContext().getPointerType(StateTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000467
468 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000469 getContext().getPointerType(ItemsTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlssonf484c312008-08-31 02:33:12 +0000471 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000472 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump1eb44332009-09-09 15:08:12 +0000473 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000474 getContext().UnsignedLongTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000475
476 RValue CountRV =
477 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000478 getContext().UnsignedLongTy,
479 FastEnumSel,
480 Collection, false, Args);
481
482 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
483 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Daniel Dunbar55e87422008-11-11 02:29:29 +0000485 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
486 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anders Carlssonf484c312008-08-31 02:33:12 +0000488 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000489 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000490
491 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000492 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000493
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000494 EmitBlock(SetStartMutations);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
496 llvm::Value *StartMutationsPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000497 CreateTempAlloca(UnsignedLongLTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000498
499 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000500 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000501 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000502 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000503
504 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000505 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000507 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Daniel Dunbar55e87422008-11-11 02:29:29 +0000509 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000510 EmitBlock(LoopStart);
511
Anders Carlssonf484c312008-08-31 02:33:12 +0000512 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
513 Builder.CreateStore(Zero, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000514
515 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000516 EmitBlock(LoopBody);
517
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000518 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
519 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
520
Mike Stump1eb44332009-09-09 15:08:12 +0000521 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000522 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000523 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000524 StartMutations,
525 "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000526
527
Daniel Dunbar55e87422008-11-11 02:29:29 +0000528 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
529 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000531 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000533 EmitBlock(WasMutated);
534 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000535 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000536 ConvertType(getContext().getObjCIdType()),
537 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000538 CallArgList Args2;
Mike Stump1eb44332009-09-09 15:08:12 +0000539 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000540 getContext().getObjCIdType()));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000541 // FIXME: We shouldn't need to get the function info here, the runtime already
542 // should have computed it to build the function.
Mike Stump1eb44332009-09-09 15:08:12 +0000543 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000544 EnumerationMutationFn, Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000546 EmitBlock(WasNotMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
548 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000549 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
550
551 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
552
553 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
554 "stateitems");
555
Mike Stump1eb44332009-09-09 15:08:12 +0000556 llvm::Value *CurrentItemPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000557 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Anders Carlssonf484c312008-08-31 02:33:12 +0000559 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Anders Carlssonf484c312008-08-31 02:33:12 +0000561 // Cast the item to the right type.
562 CurrentItem = Builder.CreateBitCast(CurrentItem,
563 ConvertType(ElementTy), "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Anders Carlssonf484c312008-08-31 02:33:12 +0000565 if (!DeclAddress) {
566 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Anders Carlssonf484c312008-08-31 02:33:12 +0000568 // Set the value to null.
569 Builder.CreateStore(CurrentItem, LV.getAddress());
570 } else
571 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Anders Carlssonf484c312008-08-31 02:33:12 +0000573 // Increment the counter.
Mike Stump1eb44332009-09-09 15:08:12 +0000574 Counter = Builder.CreateAdd(Counter,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000575 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000576 Builder.CreateStore(Counter, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Daniel Dunbar55e87422008-11-11 02:29:29 +0000578 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
579 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Anders Carlssone4b6d342009-02-10 05:52:02 +0000581 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000582
583 EmitStmt(S.getBody());
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlssonf484c312008-08-31 02:33:12 +0000585 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Anders Carlssonf484c312008-08-31 02:33:12 +0000587 EmitBlock(AfterBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Daniel Dunbar55e87422008-11-11 02:29:29 +0000589 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000590
591 Counter = Builder.CreateLoad(CounterPtr);
592 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000593 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000594 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000595
596 // Fetch more elements.
597 EmitBlock(FetchMore);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
599 CountRV =
600 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000601 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000602 FastEnumSel,
Anders Carlssonf484c312008-08-31 02:33:12 +0000603 Collection, false, Args);
604 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
605 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Anders Carlssonf484c312008-08-31 02:33:12 +0000607 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
608 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlssonf484c312008-08-31 02:33:12 +0000610 // No more elements.
611 EmitBlock(NoElements);
612
613 if (!DeclAddress) {
614 // If the element was not a declaration, set it to be null.
615
616 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlssonf484c312008-08-31 02:33:12 +0000618 // Set the value to null.
Owen Andersonc9c88b42009-07-31 20:28:54 +0000619 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlssonf484c312008-08-31 02:33:12 +0000620 LV.getAddress());
621 }
622
623 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000624}
625
Mike Stump1eb44332009-09-09 15:08:12 +0000626void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000627 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000628}
629
Mike Stump1eb44332009-09-09 15:08:12 +0000630void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000631 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
632}
633
Chris Lattner10cac6f2008-11-15 21:26:17 +0000634void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +0000635 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000636 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000637}
638
Ted Kremenek2979ec72008-04-09 15:51:31 +0000639CGObjCRuntime::~CGObjCRuntime() {}