blob: 206d438ced676811292411e6fb0c585c833e89ce [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) {
Douglas Gregorc2350e52010-03-08 16:40:19 +000062 const ObjCInterfaceDecl *OID = E->getClassInfo().Decl;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000063
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;
Devang Patel4800ea62010-04-05 21:09:15 +0000111 // Check if we should generate debug info for this method.
112 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
113 DebugInfo = CGM.getDebugInfo();
114
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000115 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000116
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000117 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
118 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000119
Mike Stump1eb44332009-09-09 15:08:12 +0000120 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbar7c086512008-09-09 23:14:03 +0000121 OMD->getSelfDecl()->getType()));
122 Args.push_back(std::make_pair(OMD->getCmdDecl(),
123 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000124
Chris Lattner89951a82009-02-20 18:43:26 +0000125 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
126 E = OMD->param_end(); PI != E; ++PI)
127 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000128
Devang Patela92d6132010-02-15 18:08:38 +0000129 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000130}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000131
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000132/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000133/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000134void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000135 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000136 EmitStmt(OMD->getBody());
137 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000138}
139
Mike Stumpf5408fe2009-05-16 07:57:57 +0000140// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
141// AST for the whole body we can just fall back to having a GenerateFunction
142// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000143
144/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000145/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
146/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000147void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
148 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000149 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000150 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
151 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
152 assert(OMD && "Invalid call to generate getter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000153 // FIXME: This is rather murky, we create this here since they will not have
154 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000155 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000156 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000157
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000158 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000159 // this. Non-atomic properties are directly evaluated.
160 // atomic 'copy' and 'retain' properties are also directly
161 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000162 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000163 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
164 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
165 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000166 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000167 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000169 if (!GetPropertyFn) {
170 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
171 FinishFunction();
172 return;
173 }
174
175 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
176 // FIXME: Can't this be simpler? This might even be worse than the
177 // corresponding gcc code.
178 CodeGenTypes &Types = CGM.getTypes();
179 ValueDecl *Cmd = OMD->getCmdDecl();
180 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
181 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000182 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000183 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000184 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000185 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000186 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000187 CallArgList Args;
188 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
189 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
190 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
191 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000192 // FIXME: We shouldn't need to get the function info here, the
193 // runtime already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000194 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000195 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000196 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000197 // We need to fix the type here. Ivars with copy & retain are
198 // always objects so we don't need to worry about complex or
199 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000200 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000201 Types.ConvertType(PD->getType())));
202 EmitReturnOfRValue(RV, PD->getType());
203 } else {
Daniel Dunbar525c9b72009-04-21 01:19:28 +0000204 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000205 if (Ivar->getType()->isAnyComplexType()) {
206 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
207 LV.isVolatileQualified());
208 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
209 }
210 else if (hasAggregateLLVMType(Ivar->getType())) {
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000211 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
Mike Stumpb3589f42009-07-30 22:28:39 +0000212 } else {
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000213 CodeGenTypes &Types = CGM.getTypes();
214 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
215 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump1eb44332009-09-09 15:08:12 +0000216 Types.ConvertType(PD->getType())));
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000217 EmitReturnOfRValue(RV, PD->getType());
218 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000219 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000220
221 FinishFunction();
222}
223
224/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000225/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
226/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000227void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
228 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000229 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000230 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
231 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
232 assert(OMD && "Invalid call to generate setter (empty method)");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000233 // FIXME: This is rather murky, we create this here since they will not have
234 // been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000235 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000236 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000237
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000238 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000239 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000240 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
241
242 // Determine if we should use an objc_setProperty call for
243 // this. Properties with 'copy' semantics always use it, as do
244 // non-atomic properties with 'release' semantics as long as we are
245 // not in gc-only mode.
246 if (IsCopy ||
247 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
248 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000249 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000250 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000252 if (!SetPropertyFn) {
253 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
254 FinishFunction();
255 return;
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
258 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000259 // <is-atomic>, <is-copy>).
260 // FIXME: Can't this be simpler? This might even be worse than the
261 // corresponding gcc code.
262 CodeGenTypes &Types = CGM.getTypes();
263 ValueDecl *Cmd = OMD->getCmdDecl();
264 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
265 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000266 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000267 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000268 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000269 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000270 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000271 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
272 Types.ConvertType(IdTy));
273 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000274 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000275 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000276 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000277 CallArgList Args;
278 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
279 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
280 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
281 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000282 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000283 getContext().BoolTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000284 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000285 getContext().BoolTy));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000286 // FIXME: We shouldn't need to get the function info here, the runtime
287 // already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000288 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000289 FunctionType::ExtInfo()),
290 SetPropertyFn,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000291 ReturnValueSlot(), Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000292 } else {
Daniel Dunbar45e84232009-10-27 19:21:30 +0000293 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000294 SourceLocation Loc = PD->getLocation();
295 ValueDecl *Self = OMD->getSelfDecl();
296 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
297 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000298 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000299 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000300 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
301
302 // The property type can differ from the ivar type in some situations with
303 // Objective-C pointer types, we can always bit cast the RHS in these cases.
304 if (getContext().getCanonicalType(Ivar->getType()) !=
305 getContext().getCanonicalType(ArgDecl->getType())) {
306 ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
307 false);
308 BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
309 Ivar->getType(), Loc);
310 EmitStmt(&Assign);
311 } else {
312 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
313 Ivar->getType(), Loc);
314 EmitStmt(&Assign);
315 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000316 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000317
318 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000319}
320
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000321llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000322 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000323 // See if we need to lazily forward self inside a block literal.
324 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000325 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000326}
327
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000328QualType CodeGenFunction::TypeOfSelfObject() {
329 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
330 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000331 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
332 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000333 return PTy->getPointeeType();
334}
335
Mike Stump1eb44332009-09-09 15:08:12 +0000336RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000337 const Selector &S) {
338 llvm::Value *Receiver = LoadObjCSelf();
339 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
340 bool isClassMessage = OMD->isClassMethod();
341 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000342 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000343 Exp->getType(),
344 S,
345 OMD->getClassInterface(),
346 isCategoryImpl,
347 Receiver,
348 isClassMessage,
349 CallArgList());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000351}
352
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000353RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +0000354 Exp = Exp->IgnoreParens();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000355 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000356 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
357 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000358 if (isa<ObjCSuperExpr>(E->getBase()))
359 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000360 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000361 GenerateMessageSend(*this, Exp->getType(), S,
362 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000363 false, CallArgList());
Mike Stumpb3589f42009-07-30 22:28:39 +0000364 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000365 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000366 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000367 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000368 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000369 if (KE->getInterfaceDecl()) {
370 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000371 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000372 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000373 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump1eb44332009-09-09 15:08:12 +0000374 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000375 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000376 return CGM.getObjCRuntime().
Mike Stump1eb44332009-09-09 15:08:12 +0000377 GenerateMessageSend(*this, Exp->getType(), S,
378 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000379 KE->getInterfaceDecl() != 0, CallArgList());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000380 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000381}
382
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000383void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
384 const Selector &S,
385 RValue Src) {
386 CallArgList Args;
387 llvm::Value *Receiver = LoadObjCSelf();
388 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
389 bool isClassMessage = OMD->isClassMethod();
390 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
391 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000392 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000393 Exp->getType(),
394 S,
395 OMD->getClassInterface(),
396 isCategoryImpl,
397 Receiver,
398 isClassMessage,
399 Args);
400 return;
401}
402
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000403void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000404 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000405 // FIXME: Split it into two separate routines.
406 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
407 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000408 if (isa<ObjCSuperExpr>(E->getBase())) {
409 EmitObjCSuperPropertySet(E, S, Src);
410 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000411 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000412 CallArgList Args;
413 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000414 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
415 EmitScalarExpr(E->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000416 false, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000417 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000418 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000419 Selector S = E->getSetterMethod()->getSelector();
420 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000421 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000422 if (E->getInterfaceDecl()) {
423 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000424 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stumpb3589f42009-07-30 22:28:39 +0000425 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000426 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000427 return;
Mike Stumpb3589f42009-07-30 22:28:39 +0000428 } else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000429 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000430 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000431 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
432 Receiver,
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000433 E->getInterfaceDecl() != 0, Args);
Mike Stumpb3589f42009-07-30 22:28:39 +0000434 } else
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000435 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000436}
437
Chris Lattner74391b42009-03-22 21:03:39 +0000438void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000439 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000440 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000441 llvm::Value *DeclAddress;
442 QualType ElementTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000444 if (!EnumerationMutationFn) {
445 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
446 return;
447 }
448
Anders Carlssonf484c312008-08-31 02:33:12 +0000449 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
450 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000451 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000452 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000453 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000454 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000455 } else {
456 ElementTy = cast<Expr>(S.getElement())->getType();
457 DeclAddress = 0;
458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Anders Carlssonf484c312008-08-31 02:33:12 +0000460 // Fast enumeration state.
461 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar195337d2010-02-09 02:48:28 +0000462 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000463 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Anders Carlssonf484c312008-08-31 02:33:12 +0000465 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000466 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Anders Carlssonf484c312008-08-31 02:33:12 +0000468 // Get selector
Benjamin Kramerad468862010-03-30 11:36:44 +0000469 IdentifierInfo *II[] = {
470 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
471 &CGM.getContext().Idents.get("objects"),
472 &CGM.getContext().Idents.get("count")
473 };
474 Selector FastEnumSel =
475 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlssonf484c312008-08-31 02:33:12 +0000476
477 QualType ItemsTy =
478 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000479 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000480 ArrayType::Normal, 0);
Daniel Dunbar195337d2010-02-09 02:48:28 +0000481 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anders Carlssonf484c312008-08-31 02:33:12 +0000483 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anders Carlssonf484c312008-08-31 02:33:12 +0000485 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000486 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000487 getContext().getPointerType(StateTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000488
489 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000490 getContext().getPointerType(ItemsTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlssonf484c312008-08-31 02:33:12 +0000492 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000493 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump1eb44332009-09-09 15:08:12 +0000494 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000495 getContext().UnsignedLongTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000496
497 RValue CountRV =
498 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000499 getContext().UnsignedLongTy,
500 FastEnumSel,
501 Collection, false, Args);
502
Daniel Dunbar195337d2010-02-09 02:48:28 +0000503 llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy,
504 "limit.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000505 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Daniel Dunbar55e87422008-11-11 02:29:29 +0000507 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
508 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Anders Carlssonf484c312008-08-31 02:33:12 +0000510 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000511 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000512
513 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000514 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000515
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000516 EmitBlock(SetStartMutations);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Daniel Dunbar195337d2010-02-09 02:48:28 +0000518 llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
520 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000521 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000522 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000523 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
525 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000526 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000528 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Daniel Dunbar55e87422008-11-11 02:29:29 +0000530 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000531 EmitBlock(LoopStart);
532
Daniel Dunbar195337d2010-02-09 02:48:28 +0000533 llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy,
534 "counter.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000535 Builder.CreateStore(Zero, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
537 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000538 EmitBlock(LoopBody);
539
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000540 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
541 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
542
Mike Stump1eb44332009-09-09 15:08:12 +0000543 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000544 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000545 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000546 StartMutations,
547 "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000548
549
Daniel Dunbar55e87422008-11-11 02:29:29 +0000550 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
551 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000553 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000555 EmitBlock(WasMutated);
556 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000557 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000558 ConvertType(getContext().getObjCIdType()),
559 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000560 CallArgList Args2;
Mike Stump1eb44332009-09-09 15:08:12 +0000561 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000562 getContext().getObjCIdType()));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000563 // FIXME: We shouldn't need to get the function info here, the runtime already
564 // should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000565 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindola264ba482010-03-30 20:24:48 +0000566 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000567 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000569 EmitBlock(WasNotMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
571 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000572 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
573
574 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
575
576 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
577 "stateitems");
578
Mike Stump1eb44332009-09-09 15:08:12 +0000579 llvm::Value *CurrentItemPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000580 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Anders Carlssonf484c312008-08-31 02:33:12 +0000582 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Anders Carlssonf484c312008-08-31 02:33:12 +0000584 // Cast the item to the right type.
585 CurrentItem = Builder.CreateBitCast(CurrentItem,
586 ConvertType(ElementTy), "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Anders Carlssonf484c312008-08-31 02:33:12 +0000588 if (!DeclAddress) {
589 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlssonf484c312008-08-31 02:33:12 +0000591 // Set the value to null.
592 Builder.CreateStore(CurrentItem, LV.getAddress());
593 } else
594 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Anders Carlssonf484c312008-08-31 02:33:12 +0000596 // Increment the counter.
Mike Stump1eb44332009-09-09 15:08:12 +0000597 Counter = Builder.CreateAdd(Counter,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000598 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000599 Builder.CreateStore(Counter, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Daniel Dunbar55e87422008-11-11 02:29:29 +0000601 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
602 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Anders Carlssone4b6d342009-02-10 05:52:02 +0000604 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000605
606 EmitStmt(S.getBody());
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Anders Carlssonf484c312008-08-31 02:33:12 +0000608 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlssonf484c312008-08-31 02:33:12 +0000610 EmitBlock(AfterBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Daniel Dunbar55e87422008-11-11 02:29:29 +0000612 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000613
614 Counter = Builder.CreateLoad(CounterPtr);
615 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000616 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000617 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000618
619 // Fetch more elements.
620 EmitBlock(FetchMore);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
622 CountRV =
623 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlssonf484c312008-08-31 02:33:12 +0000624 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000625 FastEnumSel,
Anders Carlssonf484c312008-08-31 02:33:12 +0000626 Collection, false, Args);
627 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
628 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Anders Carlssonf484c312008-08-31 02:33:12 +0000630 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
631 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Anders Carlssonf484c312008-08-31 02:33:12 +0000633 // No more elements.
634 EmitBlock(NoElements);
635
636 if (!DeclAddress) {
637 // If the element was not a declaration, set it to be null.
638
639 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Anders Carlssonf484c312008-08-31 02:33:12 +0000641 // Set the value to null.
Owen Andersonc9c88b42009-07-31 20:28:54 +0000642 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlssonf484c312008-08-31 02:33:12 +0000643 LV.getAddress());
644 }
645
646 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000647}
648
Mike Stump1eb44332009-09-09 15:08:12 +0000649void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000650 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000651}
652
Mike Stump1eb44332009-09-09 15:08:12 +0000653void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000654 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
655}
656
Chris Lattner10cac6f2008-11-15 21:26:17 +0000657void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +0000658 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000659 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000660}
661
Ted Kremenek2979ec72008-04-09 15:51:31 +0000662CGObjCRuntime::~CGObjCRuntime() {}