blob: e03d7606c1991ff41d5ffa9c663ab69d6108b6f3 [file] [log] [blame]
Anders Carlsson76f4a902007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Carlsson76f4a902007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek43e06332008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlsson76f4a902007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000019#include "clang/AST/StmtObjC.h"
Daniel Dunbarc5d33042008-09-03 00:27:26 +000020#include "clang/Basic/Diagnostic.h"
Anders Carlsson2e744e82008-08-30 19:51:14 +000021#include "llvm/ADT/STLExtras.h"
Daniel Dunbara08dff12008-09-24 04:04:31 +000022#include "llvm/Target/TargetData.h"
Anders Carlsson76f4a902007-08-21 17:43:55 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattnerb1d329d2008-06-24 17:04:18 +000026/// Emits an instance of NSConstantString representing the object.
Mike Stump11289f42009-09-09 15:08:12 +000027llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
Daniel Dunbar44b58a22008-11-25 21:53:21 +000028{
David Chisnall481e3a82010-01-23 02:40:42 +000029 llvm::Constant *C =
30 CGM.getObjCRuntime().GenerateConstantString(E->getString());
Daniel Dunbar66912a12008-08-20 00:28:19 +000031 // FIXME: This bitcast should just be made an invariant on the Runtime.
Owen Andersonade90fd2009-07-29 18:54:39 +000032 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattnerb1d329d2008-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 Dunbar45858d22010-02-03 20:11:42 +000041 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattnerb1d329d2008-06-24 17:04:18 +000042}
43
Daniel Dunbar66912a12008-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 Lattnerb1d329d2008-06-24 17:04:18 +000048
49
Daniel Dunbar97db84c2008-08-23 03:46:30 +000050RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattnerb1d329d2008-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 Stump11289f42009-09-09 15:08:12 +000054
Daniel Dunbar8d480592008-08-11 18:12:00 +000055 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattnerb1d329d2008-06-24 17:04:18 +000056 const Expr *ReceiverExpr = E->getReceiver();
57 bool isSuperMessage = false;
Daniel Dunbarca8531a2008-08-25 08:19:24 +000058 bool isClassMessage = false;
Chris Lattnerb1d329d2008-06-24 17:04:18 +000059 // Find the receiver
60 llvm::Value *Receiver;
61 if (!ReceiverExpr) {
Daniel Dunbar7c6d3a72008-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 Lattner68e48682008-11-20 04:42:34 +000067 assert(E->getClassName()->isStr("super") &&
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +000068 "Unexpected missing class interface in message send.");
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +000069 isSuperMessage = true;
Daniel Dunbarca8531a2008-08-25 08:19:24 +000070 Receiver = LoadObjCSelf();
71 } else {
72 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattnerb1d329d2008-06-24 17:04:18 +000073 }
Mike Stump11289f42009-09-09 15:08:12 +000074
Daniel Dunbarca8531a2008-08-25 08:19:24 +000075 isClassMessage = true;
Douglas Gregor8ea1f532008-11-04 14:56:14 +000076 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattnerb1d329d2008-06-24 17:04:18 +000077 isSuperMessage = true;
78 Receiver = LoadObjCSelf();
79 } else {
Daniel Dunbar5d715592008-08-12 05:28:47 +000080 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattnerb1d329d2008-06-24 17:04:18 +000081 }
82
Daniel Dunbarc722b852008-08-30 03:02:31 +000083 CallArgList Args;
Anders Carlsson623dcae2009-04-18 20:29:27 +000084 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnerb1d329d2008-06-24 17:04:18 +000086 if (isSuperMessage) {
Chris Lattner6cfec782008-06-26 04:42:20 +000087 // super is only valid in an Objective-C method
88 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000089 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +000090 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
91 E->getSelector(),
Daniel Dunbarca8531a2008-08-25 08:19:24 +000092 OMD->getClassInterface(),
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +000093 isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +000094 Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +000095 isClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +000096 Args,
97 E->getMethodDecl());
Chris Lattnerb1d329d2008-06-24 17:04:18 +000098 }
Daniel Dunbaraff9fca2009-09-17 04:01:22 +000099
Mike Stump11289f42009-09-09 15:08:12 +0000100 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000101 Receiver, isClassMessage, Args,
102 E->getMethodDecl());
Anders Carlsson76f4a902007-08-21 17:43:55 +0000103}
104
Daniel Dunbar89654ee2008-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 Jahanian0196a1c2009-01-10 21:06:09 +0000108void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
109 const ObjCContainerDecl *CD) {
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000110 FunctionArgList Args;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000111 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000112
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000113 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
114 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner5696e7b2008-06-17 18:05:57 +0000115
Mike Stump11289f42009-09-09 15:08:12 +0000116 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000117 OMD->getSelfDecl()->getType()));
118 Args.push_back(std::make_pair(OMD->getCmdDecl(),
119 OMD->getCmdDecl()->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000120
Chris Lattnera4997152009-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 Lattner5696e7b2008-06-17 18:05:57 +0000124
Daniel Dunbar354d2782008-10-18 18:22:23 +0000125 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000126}
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000127
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000128/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump11289f42009-09-09 15:08:12 +0000129/// its pointer, name, and types registered in the class struture.
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000130void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel9d7d17a2009-02-25 01:09:46 +0000131 // Check if we should generate debug info for this method.
Mike Stump3722f582009-08-26 22:31:08 +0000132 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
Devang Patel9d7d17a2009-02-25 01:09:46 +0000133 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000134 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000135 EmitStmt(OMD->getBody());
136 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000137}
138
Mike Stump18bb9282009-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 Dunbar89654ee2008-08-26 08:29:31 +0000142
143/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000144/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
145/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000146void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
147 const ObjCPropertyImplDecl *PID) {
Daniel Dunbara08dff12008-09-24 04:04:31 +0000148 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-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 Stump18bb9282009-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 Jahanian3d8552a2008-12-09 20:23:04 +0000154 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000155 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000156
Daniel Dunbara08dff12008-09-24 04:04:31 +0000157 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian8e0079c2008-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 Dunbara08dff12008-09-24 04:04:31 +0000161 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian8e0079c2008-12-08 23:56:17 +0000162 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
163 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
164 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump11289f42009-09-09 15:08:12 +0000165 llvm::Value *GetPropertyFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000166 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000167
Daniel Dunbara08dff12008-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 Stump11289f42009-09-09 15:08:12 +0000181 llvm::Value *SelfAsId =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000182 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000183 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbara08dff12008-09-24 04:04:31 +0000184 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000185 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbara08dff12008-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 Dunbar1ef73732009-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 Stump11289f42009-09-09 15:08:12 +0000193 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000194 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbara08dff12008-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 Stump11289f42009-09-09 15:08:12 +0000198 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbara08dff12008-09-24 04:04:31 +0000199 Types.ConvertType(PD->getType())));
200 EmitReturnOfRValue(RV, PD->getType());
201 } else {
Daniel Dunbar9ebf9512009-04-21 01:19:28 +0000202 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
Fariborz Jahanian57251782008-11-26 22:36:09 +0000203 if (hasAggregateLLVMType(Ivar->getType())) {
204 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
Mike Stump658fe022009-07-30 22:28:39 +0000205 } else {
Fariborz Jahanianeab5ecd2009-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 Stump11289f42009-09-09 15:08:12 +0000209 Types.ConvertType(PD->getType())));
Fariborz Jahanianeab5ecd2009-03-03 18:49:40 +0000210 EmitReturnOfRValue(RV, PD->getType());
211 }
Daniel Dunbara08dff12008-09-24 04:04:31 +0000212 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000213
214 FinishFunction();
215}
216
217/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff5a7dd782009-01-10 22:55:25 +0000218/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
219/// is illegal within a category.
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000220void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
221 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000222 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar89654ee2008-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 Stump18bb9282009-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 Jahanian3d8552a2008-12-09 20:23:04 +0000228 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000229 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000230
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000231 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump11289f42009-09-09 15:08:12 +0000232 bool IsAtomic =
Daniel Dunbar5449ce52008-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 Stump11289f42009-09-09 15:08:12 +0000242 llvm::Value *SetPropertyFn =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000243 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000244
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000245 if (!SetPropertyFn) {
246 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
247 FinishFunction();
248 return;
249 }
Mike Stump11289f42009-09-09 15:08:12 +0000250
251 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar5449ce52008-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 Stump11289f42009-09-09 15:08:12 +0000259 llvm::Value *SelfAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000260 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000261 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattnera4997152009-02-20 18:43:26 +0000262 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump11289f42009-09-09 15:08:12 +0000263 llvm::Value *ArgAsId =
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000264 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
265 Types.ConvertType(IdTy));
266 llvm::Value *True =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000267 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000268 llvm::Value *False =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000269 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar5449ce52008-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 Stump11289f42009-09-09 15:08:12 +0000275 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000276 getContext().BoolTy));
Mike Stump11289f42009-09-09 15:08:12 +0000277 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000278 getContext().BoolTy));
Mike Stump18bb9282009-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 Carlsson61a401c2009-12-24 19:25:24 +0000281 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args), SetPropertyFn,
282 ReturnValueSlot(), Args);
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000283 } else {
Daniel Dunbarc14753b2009-10-27 19:21:30 +0000284 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar5449ce52008-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 Lattnera4997152009-02-20 18:43:26 +0000289 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar5449ce52008-09-24 06:32:09 +0000290 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbarc14753b2009-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 Dunbar5449ce52008-09-24 06:32:09 +0000307 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000308
309 FinishFunction();
Chris Lattner5696e7b2008-06-17 18:05:57 +0000310}
311
Daniel Dunbara08dff12008-09-24 04:04:31 +0000312llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000313 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump692c6e32009-03-20 21:53:12 +0000314 // See if we need to lazily forward self inside a block literal.
315 BlockForwardSelf();
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000316 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner5696e7b2008-06-17 18:05:57 +0000317}
318
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000319QualType CodeGenFunction::TypeOfSelfObject() {
320 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
321 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000322 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
323 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +0000324 return PTy->getPointeeType();
325}
326
Mike Stump11289f42009-09-09 15:08:12 +0000327RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
Fariborz Jahanian391d4fc2009-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 Stump11289f42009-09-09 15:08:12 +0000333 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000334 Exp->getType(),
335 S,
336 OMD->getClassInterface(),
337 isCategoryImpl,
338 Receiver,
339 isClassMessage,
340 CallArgList());
Mike Stump11289f42009-09-09 15:08:12 +0000341
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000342}
343
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000344RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian1a504772009-09-01 17:02:21 +0000345 Exp = Exp->IgnoreParens();
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000346 // FIXME: Split it into two separate routines.
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000347 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
348 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000349 if (isa<ObjCSuperExpr>(E->getBase()))
350 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000351 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000352 GenerateMessageSend(*this, Exp->getType(), S,
353 EmitScalarExpr(E->getBase()),
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000354 false, CallArgList());
Mike Stump658fe022009-07-30 22:28:39 +0000355 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000356 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000357 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf557d832009-01-16 01:50:29 +0000358 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000359 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000360 if (KE->getInterfaceDecl()) {
361 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000362 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000363 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000364 return EmitObjCSuperPropertyGet(KE, S);
Mike Stump11289f42009-09-09 15:08:12 +0000365 else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000366 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000367 return CGM.getObjCRuntime().
Mike Stump11289f42009-09-09 15:08:12 +0000368 GenerateMessageSend(*this, Exp->getType(), S,
369 Receiver,
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000370 KE->getInterfaceDecl() != 0, CallArgList());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000371 }
Daniel Dunbar55310df2008-08-27 06:57:25 +0000372}
373
Fariborz Jahanian391d4fc2009-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 Stump11289f42009-09-09 15:08:12 +0000383 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
Fariborz Jahanian391d4fc2009-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 Jahanian9ac53512008-11-22 22:30:21 +0000394void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000395 RValue Src) {
Fariborz Jahanian9ac53512008-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 Jahanian391d4fc2009-03-20 19:18:21 +0000399 if (isa<ObjCSuperExpr>(E->getBase())) {
400 EmitObjCSuperPropertySet(E, S, Src);
401 return;
Mike Stump11289f42009-09-09 15:08:12 +0000402 }
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000403 CallArgList Args;
404 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000405 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
406 EmitScalarExpr(E->getBase()),
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000407 false, Args);
Mike Stump11289f42009-09-09 15:08:12 +0000408 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000409 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000410 Selector S = E->getSetterMethod()->getSelector();
411 CallArgList Args;
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000412 llvm::Value *Receiver;
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000413 if (E->getInterfaceDecl()) {
414 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000415 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump658fe022009-07-30 22:28:39 +0000416 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanian391d4fc2009-03-20 19:18:21 +0000417 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian150abf22009-03-20 17:22:23 +0000418 return;
Mike Stump658fe022009-07-30 22:28:39 +0000419 } else
Fariborz Jahanian156506e2009-03-10 18:03:11 +0000420 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000421 Args.push_back(std::make_pair(Src, E->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000422 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
423 Receiver,
Fariborz Jahanian19380c42009-08-18 21:37:33 +0000424 E->getInterfaceDecl() != 0, Args);
Mike Stump658fe022009-07-30 22:28:39 +0000425 } else
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000426 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000427}
428
Chris Lattnerd4808922009-03-22 21:03:39 +0000429void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump11289f42009-09-09 15:08:12 +0000430 llvm::Constant *EnumerationMutationFn =
Daniel Dunbara08dff12008-09-24 04:04:31 +0000431 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlsson75658592008-08-31 02:33:12 +0000432 llvm::Value *DeclAddress;
433 QualType ElementTy;
Mike Stump11289f42009-09-09 15:08:12 +0000434
Daniel Dunbara08dff12008-09-24 04:04:31 +0000435 if (!EnumerationMutationFn) {
436 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
437 return;
438 }
439
Anders Carlsson75658592008-08-31 02:33:12 +0000440 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
441 EmitStmt(SD);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +0000442 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner529efc72009-03-28 06:33:19 +0000443 const Decl* D = SD->getSingleDecl();
Ted Kremenek3fef3572008-10-06 20:59:48 +0000444 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000445 DeclAddress = LocalDeclMap[D];
Anders Carlsson75658592008-08-31 02:33:12 +0000446 } else {
447 ElementTy = cast<Expr>(S.getElement())->getType();
448 DeclAddress = 0;
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Anders Carlsson75658592008-08-31 02:33:12 +0000451 // Fast enumeration state.
452 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar88481752010-02-05 18:56:49 +0000453 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertTypeForMem(
454 StateTy), "state.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000455 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson3f35a262008-08-31 04:05:03 +0000456 EmitMemSetToZero(StatePtr, StateTy);
Mike Stump11289f42009-09-09 15:08:12 +0000457
Anders Carlsson75658592008-08-31 02:33:12 +0000458 // Number of elements in the items array.
Anders Carlsson3f35a262008-08-31 04:05:03 +0000459 static const unsigned NumItems = 16;
Mike Stump11289f42009-09-09 15:08:12 +0000460
Anders Carlsson75658592008-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 Stump11289f42009-09-09 15:08:12 +0000466 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
Anders Carlsson75658592008-08-31 02:33:12 +0000467 &II[0]);
468
469 QualType ItemsTy =
470 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump11289f42009-09-09 15:08:12 +0000471 llvm::APInt(32, NumItems),
Anders Carlsson75658592008-08-31 02:33:12 +0000472 ArrayType::Normal, 0);
Daniel Dunbar88481752010-02-05 18:56:49 +0000473 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertTypeForMem(
474 ItemsTy), "items.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000475
Anders Carlsson75658592008-08-31 02:33:12 +0000476 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump11289f42009-09-09 15:08:12 +0000477
Anders Carlsson75658592008-08-31 02:33:12 +0000478 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000479 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000480 getContext().getPointerType(StateTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000481
482 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson75658592008-08-31 02:33:12 +0000483 getContext().getPointerType(ItemsTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000484
Anders Carlsson75658592008-08-31 02:33:12 +0000485 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000486 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump11289f42009-09-09 15:08:12 +0000487 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000488 getContext().UnsignedLongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000489
490 RValue CountRV =
491 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000492 getContext().UnsignedLongTy,
493 FastEnumSel,
494 Collection, false, Args);
495
496 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
497 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000498
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000499 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
500 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump11289f42009-09-09 15:08:12 +0000501
Anders Carlsson75658592008-08-31 02:33:12 +0000502 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Anderson0b75f232009-07-31 20:28:54 +0000503 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlsson75658592008-08-31 02:33:12 +0000504
505 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson3f35a262008-08-31 04:05:03 +0000506 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson75658592008-08-31 02:33:12 +0000507
Anders Carlsson3f35a262008-08-31 04:05:03 +0000508 EmitBlock(SetStartMutations);
Mike Stump11289f42009-09-09 15:08:12 +0000509
510 llvm::Value *StartMutationsPtr =
Anders Carlsson3f35a262008-08-31 04:05:03 +0000511 CreateTempAlloca(UnsignedLongLTy);
Mike Stump11289f42009-09-09 15:08:12 +0000512
513 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson3f35a262008-08-31 04:05:03 +0000514 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000515 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000516 "mutationsptr");
Mike Stump11289f42009-09-09 15:08:12 +0000517
518 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000519 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000520
Anders Carlsson3f35a262008-08-31 04:05:03 +0000521 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000522
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000523 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlsson75658592008-08-31 02:33:12 +0000524 EmitBlock(LoopStart);
525
Anders Carlsson75658592008-08-31 02:33:12 +0000526 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
527 Builder.CreateStore(Zero, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000528
529 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlsson75658592008-08-31 02:33:12 +0000530 EmitBlock(LoopBody);
531
Anders Carlsson3f35a262008-08-31 04:05:03 +0000532 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
533 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
534
Mike Stump11289f42009-09-09 15:08:12 +0000535 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000536 "mutations");
Mike Stump11289f42009-09-09 15:08:12 +0000537 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000538 StartMutations,
539 "tobool");
Mike Stump11289f42009-09-09 15:08:12 +0000540
541
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000542 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
543 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump11289f42009-09-09 15:08:12 +0000544
Anders Carlsson3f35a262008-08-31 04:05:03 +0000545 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000546
Anders Carlsson3f35a262008-08-31 04:05:03 +0000547 EmitBlock(WasMutated);
548 llvm::Value *V =
Mike Stump11289f42009-09-09 15:08:12 +0000549 Builder.CreateBitCast(Collection,
Anders Carlsson3f35a262008-08-31 04:05:03 +0000550 ConvertType(getContext().getObjCIdType()),
551 "tmp");
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000552 CallArgList Args2;
Mike Stump11289f42009-09-09 15:08:12 +0000553 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar84388bf2009-02-03 23:55:40 +0000554 getContext().getObjCIdType()));
Mike Stump18bb9282009-05-16 07:57:57 +0000555 // FIXME: We shouldn't need to get the function info here, the runtime already
556 // should have computed it to build the function.
Mike Stump11289f42009-09-09 15:08:12 +0000557 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000558 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Anders Carlsson3f35a262008-08-31 04:05:03 +0000560 EmitBlock(WasNotMutated);
Mike Stump11289f42009-09-09 15:08:12 +0000561
562 llvm::Value *StateItemsPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000563 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
564
565 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
566
567 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
568 "stateitems");
569
Mike Stump11289f42009-09-09 15:08:12 +0000570 llvm::Value *CurrentItemPtr =
Anders Carlsson75658592008-08-31 02:33:12 +0000571 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump11289f42009-09-09 15:08:12 +0000572
Anders Carlsson75658592008-08-31 02:33:12 +0000573 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump11289f42009-09-09 15:08:12 +0000574
Anders Carlsson75658592008-08-31 02:33:12 +0000575 // Cast the item to the right type.
576 CurrentItem = Builder.CreateBitCast(CurrentItem,
577 ConvertType(ElementTy), "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000578
Anders Carlsson75658592008-08-31 02:33:12 +0000579 if (!DeclAddress) {
580 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000581
Anders Carlsson75658592008-08-31 02:33:12 +0000582 // Set the value to null.
583 Builder.CreateStore(CurrentItem, LV.getAddress());
584 } else
585 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump11289f42009-09-09 15:08:12 +0000586
Anders Carlsson75658592008-08-31 02:33:12 +0000587 // Increment the counter.
Mike Stump11289f42009-09-09 15:08:12 +0000588 Counter = Builder.CreateAdd(Counter,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000589 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlsson75658592008-08-31 02:33:12 +0000590 Builder.CreateStore(Counter, CounterPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000591
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000592 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
593 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Mike Stump11289f42009-09-09 15:08:12 +0000594
Anders Carlsson33747b62009-02-10 05:52:02 +0000595 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlsson75658592008-08-31 02:33:12 +0000596
597 EmitStmt(S.getBody());
Mike Stump11289f42009-09-09 15:08:12 +0000598
Anders Carlsson75658592008-08-31 02:33:12 +0000599 BreakContinueStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000600
Anders Carlsson75658592008-08-31 02:33:12 +0000601 EmitBlock(AfterBody);
Mike Stump11289f42009-09-09 15:08:12 +0000602
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000603 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanian6e7ecc82009-01-06 18:56:31 +0000604
605 Counter = Builder.CreateLoad(CounterPtr);
606 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlsson75658592008-08-31 02:33:12 +0000607 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar56b936b2008-09-04 21:54:37 +0000608 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson75658592008-08-31 02:33:12 +0000609
610 // Fetch more elements.
611 EmitBlock(FetchMore);
Mike Stump11289f42009-09-09 15:08:12 +0000612
613 CountRV =
614 CGM.getObjCRuntime().GenerateMessageSend(*this,
Anders Carlsson75658592008-08-31 02:33:12 +0000615 getContext().UnsignedLongTy,
Mike Stump11289f42009-09-09 15:08:12 +0000616 FastEnumSel,
Anders Carlsson75658592008-08-31 02:33:12 +0000617 Collection, false, Args);
618 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
619 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Anders Carlsson75658592008-08-31 02:33:12 +0000621 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
622 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump11289f42009-09-09 15:08:12 +0000623
Anders Carlsson75658592008-08-31 02:33:12 +0000624 // No more elements.
625 EmitBlock(NoElements);
626
627 if (!DeclAddress) {
628 // If the element was not a declaration, set it to be null.
629
630 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump11289f42009-09-09 15:08:12 +0000631
Anders Carlsson75658592008-08-31 02:33:12 +0000632 // Set the value to null.
Owen Anderson0b75f232009-07-31 20:28:54 +0000633 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlsson75658592008-08-31 02:33:12 +0000634 LV.getAddress());
635 }
636
637 EmitBlock(LoopEnd);
Anders Carlsson2e744e82008-08-30 19:51:14 +0000638}
639
Mike Stump11289f42009-09-09 15:08:12 +0000640void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000641 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000642}
643
Mike Stump11289f42009-09-09 15:08:12 +0000644void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000645 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
646}
647
Chris Lattnere132e242008-11-15 21:26:17 +0000648void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump11289f42009-09-09 15:08:12 +0000649 const ObjCAtSynchronizedStmt &S) {
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000650 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattnere132e242008-11-15 21:26:17 +0000651}
652
Ted Kremenek43e06332008-04-09 15:51:31 +0000653CGObjCRuntime::~CGObjCRuntime() {}