blob: 70c199388778817f2223f4aa37cf539520a04466 [file] [log] [blame]
Anders Carlssona66cad42007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Carlssona66cad42007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekfa4ebab2008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlssona66cad42007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbare6c31752008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Daniel Dunbarcc37ac52008-09-03 00:27:26 +000019#include "clang/Basic/Diagnostic.h"
Anders Carlsson82b0d0c2008-08-30 19:51:14 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner8c7c6a12008-06-17 18:05:57 +000021
Anders Carlssona66cad42007-08-21 17:43:55 +000022using namespace clang;
23using namespace CodeGen;
24
Chris Lattner6ee20e32008-06-24 17:04:18 +000025/// Emits an instance of NSConstantString representing the object.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000026llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) {
27 std::string String(E->getString()->getStrData(), E->getString()->getByteLength());
28 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000029 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000030 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner6ee20e32008-06-24 17:04:18 +000031}
32
33/// Emit a selector.
34llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
35 // Untyped selector.
36 // Note that this implementation allows for non-constant strings to be passed
37 // as arguments to @selector(). Currently, the only thing preventing this
38 // behaviour is the type checking in the front end.
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000039 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner6ee20e32008-06-24 17:04:18 +000040}
41
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000042llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
43 // FIXME: This should pass the Decl not the name.
44 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
45}
Chris Lattner6ee20e32008-06-24 17:04:18 +000046
47
Daniel Dunbara04840b2008-08-23 03:46:30 +000048RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner6ee20e32008-06-24 17:04:18 +000049 // Only the lookup mechanism and first two arguments of the method
50 // implementation vary between runtimes. We can get the receiver and
51 // arguments in generic code.
52
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000053 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner6ee20e32008-06-24 17:04:18 +000054 const Expr *ReceiverExpr = E->getReceiver();
55 bool isSuperMessage = false;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000056 bool isClassMessage = false;
Chris Lattner6ee20e32008-06-24 17:04:18 +000057 // Find the receiver
58 llvm::Value *Receiver;
59 if (!ReceiverExpr) {
Daniel Dunbar434627a2008-08-16 00:25:02 +000060 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
61
62 // Very special case, super send in class method. The receiver is
63 // self (the class object) and the send uses super semantics.
64 if (!OID) {
65 assert(!strcmp(E->getClassName()->getName(), "super") &&
66 "Unexpected missing class interface in message send.");
Daniel Dunbar434627a2008-08-16 00:25:02 +000067 isSuperMessage = true;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000068 Receiver = LoadObjCSelf();
69 } else {
70 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner6ee20e32008-06-24 17:04:18 +000071 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000072
73 isClassMessage = true;
Chris Lattner69909292008-08-10 01:53:14 +000074 } else if (const PredefinedExpr *PDE =
75 dyn_cast<PredefinedExpr>(E->getReceiver())) {
76 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
Chris Lattner6ee20e32008-06-24 17:04:18 +000077 isSuperMessage = true;
78 Receiver = LoadObjCSelf();
79 } else {
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000080 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner6ee20e32008-06-24 17:04:18 +000081 }
82
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000083 CallArgList Args;
84 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
85 i != e; ++i)
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000086 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000087
Chris Lattner6ee20e32008-06-24 17:04:18 +000088 if (isSuperMessage) {
Chris Lattner8384c142008-06-26 04:42:20 +000089 // super is only valid in an Objective-C method
90 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbardd851282008-08-30 05:35:15 +000091 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
92 E->getSelector(),
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000093 OMD->getClassInterface(),
94 Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000095 isClassMessage,
96 Args);
Chris Lattner6ee20e32008-06-24 17:04:18 +000097 }
Daniel Dunbardd851282008-08-30 05:35:15 +000098 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
99 Receiver, isClassMessage, Args);
Anders Carlssona66cad42007-08-21 17:43:55 +0000100}
101
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000102/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
103/// the LLVM function and sets the other context used by
104/// CodeGenFunction.
105
106// FIXME: This should really be merged with GenerateCode.
107void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000108 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000109
110 CGM.SetMethodAttributes(OMD, CurFn);
111
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000112 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
113
114 // Create a marker to make it easy to insert allocas into the entryblock
115 // later. Don't create this with the builder, because we don't want it
116 // folded.
117 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
118 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
119 EntryBB);
120
121 FnRetTy = OMD->getResultType();
122 CurFuncDecl = OMD;
123
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000124 ReturnBlock = llvm::BasicBlock::Create("return", CurFn);
125 ReturnValue = 0;
126 if (!FnRetTy->isVoidType())
127 ReturnValue = CreateTempAlloca(ConvertType(FnRetTy), "retval");
128
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000129 Builder.SetInsertPoint(EntryBB);
130
131 // Emit allocs for param decls. Give the LLVM Argument nodes names.
132 llvm::Function::arg_iterator AI = CurFn->arg_begin();
133
Daniel Dunbarace33292008-08-16 03:19:19 +0000134 // Name the struct return argument.
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000135 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarace33292008-08-16 03:19:19 +0000136 AI->setName("agg.result");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000137 ++AI;
138 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000139
Daniel Dunbarace33292008-08-16 03:19:19 +0000140 // Add implicit parameters to the decl map.
141 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000142 ++AI;
Daniel Dunbarace33292008-08-16 03:19:19 +0000143
144 EmitParmDecl(*OMD->getCmdDecl(), AI);
145 ++AI;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000146
147 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
148 assert(AI != CurFn->arg_end() && "Argument mismatch!");
149 EmitParmDecl(*OMD->getParamDecl(i), AI);
150 }
Daniel Dunbarace33292008-08-16 03:19:19 +0000151 assert(AI == CurFn->arg_end() && "Argument mismatch");
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000152}
Daniel Dunbarace33292008-08-16 03:19:19 +0000153
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000154/// Generate an Objective-C method. An Objective-C method is a C function with
155/// its pointer, name, and types registered in the class struture.
156void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
157 StartObjCMethod(OMD);
158 EmitStmt(OMD->getBody());
159
160 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
161 if (S) {
162 FinishFunction(S->getRBracLoc());
163 } else {
164 FinishFunction();
165 }
166}
167
168// FIXME: I wasn't sure about the synthesis approach. If we end up
169// generating an AST for the whole body we can just fall back to
170// having a GenerateFunction which takes the body Stmt.
171
172/// GenerateObjCGetter - Generate an Objective-C property getter
173/// function. The given Decl must be either an ObjCCategoryImplDecl
174/// or an ObjCImplementationDecl.
175void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
176 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
177 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
178 assert(OMD && "Invalid call to generate getter (empty method)");
179 // FIXME: This is rather murky, we create this here since they will
180 // not have been created by Sema for us.
181 OMD->createImplicitParams(getContext());
182 StartObjCMethod(OMD);
183
184 // FIXME: What about nonatomic?
185 SourceLocation Loc = PD->getLocation();
186 ValueDecl *Self = OMD->getSelfDecl();
187 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
188 DeclRefExpr Base(Self, Self->getType(), Loc);
189 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
190 true, true);
191 ReturnStmt Return(Loc, &IvarRef);
192 EmitStmt(&Return);
193
194 FinishFunction();
195}
196
197/// GenerateObjCSetter - Generate an Objective-C property setter
198/// function. The given Decl must be either an ObjCCategoryImplDecl
199/// or an ObjCImplementationDecl.
200void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
201 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
202 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
203 assert(OMD && "Invalid call to generate setter (empty method)");
204 // FIXME: This is rather murky, we create this here since they will
205 // not have been created by Sema for us.
206 OMD->createImplicitParams(getContext());
207 StartObjCMethod(OMD);
208
209 switch (PD->getSetterKind()) {
210 case ObjCPropertyDecl::Assign: break;
211 case ObjCPropertyDecl::Copy:
212 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
213 break;
214 case ObjCPropertyDecl::Retain:
215 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
216 break;
217 }
218
219 // FIXME: What about nonatomic?
220 SourceLocation Loc = PD->getLocation();
221 ValueDecl *Self = OMD->getSelfDecl();
222 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
223 DeclRefExpr Base(Self, Self->getType(), Loc);
224 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
225 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
226 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
227 true, true);
228 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
229 Ivar->getType(), Loc);
230 EmitStmt(&Assign);
231
232 FinishFunction();
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000233}
234
Daniel Dunbarace33292008-08-16 03:19:19 +0000235llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
236 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
237 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000238}
239
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000240RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
241 // Determine getter selector.
242 Selector S;
Daniel Dunbarcc37ac52008-09-03 00:27:26 +0000243 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
244 S = E->getGetterMethod()->getSelector();
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000245 } else {
Daniel Dunbarcc37ac52008-09-03 00:27:26 +0000246 S = E->getProperty()->getGetterName();
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000247 }
248
Daniel Dunbardd851282008-08-30 05:35:15 +0000249 return CGM.getObjCRuntime().
250 GenerateMessageSend(*this, E->getType(), S,
251 EmitScalarExpr(E->getBase()),
252 false, CallArgList());
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000253}
254
Daniel Dunbare6c31752008-08-29 08:11:39 +0000255void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
256 RValue Src) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000257 Selector S;
Daniel Dunbarcc37ac52008-09-03 00:27:26 +0000258 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
259 ObjCMethodDecl *Setter = E->getSetterMethod();
260
261 if (Setter) {
262 S = Setter->getSelector();
263 } else {
264 // FIXME: This should be diagnosed by sema.
265 SourceRange Range = E->getSourceRange();
266 CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()),
267 diag::err_typecheck_assign_const, 0, 0,
268 &Range, 1);
269 return;
270 }
Daniel Dunbardd851282008-08-30 05:35:15 +0000271 } else {
Daniel Dunbarcc37ac52008-09-03 00:27:26 +0000272 S = E->getProperty()->getSetterName();
Daniel Dunbardd851282008-08-30 05:35:15 +0000273 }
274
275 CallArgList Args;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000276 Args.push_back(std::make_pair(Src, E->getType()));
Daniel Dunbardd851282008-08-30 05:35:15 +0000277 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
278 EmitScalarExpr(E->getBase()),
279 false, Args);
Daniel Dunbare6c31752008-08-29 08:11:39 +0000280}
281
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000282void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
283{
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000284 llvm::Value *DeclAddress;
285 QualType ElementTy;
286
287 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
288 EmitStmt(SD);
289
290 ElementTy = cast<ValueDecl>(SD->getDecl())->getType();
291 DeclAddress = LocalDeclMap[SD->getDecl()];
292 } else {
293 ElementTy = cast<Expr>(S.getElement())->getType();
294 DeclAddress = 0;
295 }
296
297 // Fast enumeration state.
298 QualType StateTy = getContext().getObjCFastEnumerationStateType();
299 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
300 "state.ptr");
301 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson58d16242008-08-31 04:05:03 +0000302 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000303
304 // Number of elements in the items array.
Anders Carlsson58d16242008-08-31 04:05:03 +0000305 static const unsigned NumItems = 16;
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000306
307 // Get selector
308 llvm::SmallVector<IdentifierInfo*, 3> II;
309 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
310 II.push_back(&CGM.getContext().Idents.get("objects"));
311 II.push_back(&CGM.getContext().Idents.get("count"));
312 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
313 &II[0]);
314
315 QualType ItemsTy =
316 getContext().getConstantArrayType(getContext().getObjCIdType(),
317 llvm::APInt(32, NumItems),
318 ArrayType::Normal, 0);
319 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
320
321 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
322
323 CallArgList Args;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000324 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000325 getContext().getPointerType(StateTy)));
326
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000327 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000328 getContext().getPointerType(ItemsTy)));
329
330 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
331 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000332 Args.push_back(std::make_pair(RValue::get(Count),
333 getContext().UnsignedLongTy));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000334
335 RValue CountRV =
336 CGM.getObjCRuntime().GenerateMessageSend(*this,
337 getContext().UnsignedLongTy,
338 FastEnumSel,
339 Collection, false, Args);
340
341 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
342 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
343
344 llvm::BasicBlock *NoElements = llvm::BasicBlock::Create("noelements");
Anders Carlsson58d16242008-08-31 04:05:03 +0000345 llvm::BasicBlock *SetStartMutations =
346 llvm::BasicBlock::Create("setstartmutations");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000347
348 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
349 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
350
351 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson58d16242008-08-31 04:05:03 +0000352 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000353
Anders Carlsson58d16242008-08-31 04:05:03 +0000354 EmitBlock(SetStartMutations);
355
356 llvm::Value *StartMutationsPtr =
357 CreateTempAlloca(UnsignedLongLTy);
358
359 llvm::Value *StateMutationsPtrPtr =
360 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
361 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
362 "mutationsptr");
363
364 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
365 "mutations");
366
367 Builder.CreateStore(StateMutations, StartMutationsPtr);
368
369 llvm::BasicBlock *LoopStart = llvm::BasicBlock::Create("loopstart");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000370 EmitBlock(LoopStart);
371
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000372 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
373 Builder.CreateStore(Zero, CounterPtr);
374
Anders Carlsson58d16242008-08-31 04:05:03 +0000375 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("loopbody");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000376 EmitBlock(LoopBody);
377
Anders Carlsson58d16242008-08-31 04:05:03 +0000378 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
379 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
380
381 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
382 "mutations");
383 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
384 StartMutations,
385 "tobool");
386
387
388 llvm::BasicBlock *WasMutated = llvm::BasicBlock::Create("wasmutated");
389 llvm::BasicBlock *WasNotMutated = llvm::BasicBlock::Create("wasnotmutated");
390
391 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
392
393 EmitBlock(WasMutated);
394 llvm::Value *V =
395 Builder.CreateBitCast(Collection,
396 ConvertType(getContext().getObjCIdType()),
397 "tmp");
398 Builder.CreateCall(CGM.getObjCRuntime().EnumerationMutationFunction(),
399 V);
400
401 EmitBlock(WasNotMutated);
402
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000403 llvm::Value *StateItemsPtr =
404 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
405
406 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
407
408 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
409 "stateitems");
410
411 llvm::Value *CurrentItemPtr =
412 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
413
414 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
415
416 // Cast the item to the right type.
417 CurrentItem = Builder.CreateBitCast(CurrentItem,
418 ConvertType(ElementTy), "tmp");
419
420 if (!DeclAddress) {
421 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
422
423 // Set the value to null.
424 Builder.CreateStore(CurrentItem, LV.getAddress());
425 } else
426 Builder.CreateStore(CurrentItem, DeclAddress);
427
428 // Increment the counter.
429 Counter = Builder.CreateAdd(Counter,
430 llvm::ConstantInt::get(UnsignedLongLTy, 1));
431 Builder.CreateStore(Counter, CounterPtr);
432
433 llvm::BasicBlock *LoopEnd = llvm::BasicBlock::Create("loopend");
434 llvm::BasicBlock *AfterBody = llvm::BasicBlock::Create("afterbody");
435
436 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
437
438 EmitStmt(S.getBody());
439
440 BreakContinueStack.pop_back();
441
442 EmitBlock(AfterBody);
443
444 llvm::BasicBlock *FetchMore = llvm::BasicBlock::Create("fetchmore");
445
446 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar35bd6192008-09-04 21:54:37 +0000447 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000448
449 // Fetch more elements.
450 EmitBlock(FetchMore);
451
452 CountRV =
453 CGM.getObjCRuntime().GenerateMessageSend(*this,
454 getContext().UnsignedLongTy,
455 FastEnumSel,
456 Collection, false, Args);
457 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
458 Limit = Builder.CreateLoad(LimitPtr);
459
460 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
461 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
462
463 // No more elements.
464 EmitBlock(NoElements);
465
466 if (!DeclAddress) {
467 // If the element was not a declaration, set it to be null.
468
469 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
470
471 // Set the value to null.
472 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
473 LV.getAddress());
474 }
475
476 EmitBlock(LoopEnd);
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000477}
478
Anders Carlssonb01a2112008-09-09 10:04:29 +0000479void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
480{
481 CGM.getObjCRuntime().EmitTryStmt(*this, S);
482}
483
484void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
485{
486 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
487}
488
Ted Kremenekfa4ebab2008-04-09 15:51:31 +0000489CGObjCRuntime::~CGObjCRuntime() {}