blob: af79b6610635698df8ef5172621635f60517e743 [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"
Daniel Dunbare66f4e32008-09-03 00:27:26 +000019#include "clang/Basic/Diagnostic.h"
Anders Carlsson3d8400d2008-08-30 19:51:14 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner41110242008-06-17 18:05:57 +000021
Anders Carlsson55085182007-08-21 17:43:55 +000022using namespace clang;
23using namespace CodeGen;
24
Chris Lattner8fdf3282008-06-24 17:04:18 +000025/// Emits an instance of NSConstantString representing the object.
Daniel Dunbarbbce49b2008-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 Dunbared7c6182008-08-20 00:28:19 +000029 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000030 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-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 Dunbar208ff5e2008-08-11 18:12:00 +000039 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000040}
41
Daniel Dunbared7c6182008-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 Lattner8fdf3282008-06-24 17:04:18 +000046
47
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000048RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-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 Dunbar208ff5e2008-08-11 18:12:00 +000053 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000054 const Expr *ReceiverExpr = E->getReceiver();
55 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000056 bool isClassMessage = false;
Chris Lattner8fdf3282008-06-24 17:04:18 +000057 // Find the receiver
58 llvm::Value *Receiver;
59 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-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 Dunbarddb2a3d2008-08-16 00:25:02 +000067 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000068 Receiver = LoadObjCSelf();
69 } else {
70 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000071 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +000072
73 isClassMessage = true;
Chris Lattnerd9f69102008-08-10 01:53:14 +000074 } else if (const PredefinedExpr *PDE =
75 dyn_cast<PredefinedExpr>(E->getReceiver())) {
76 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
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;
84 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
85 i != e; ++i)
86 EmitCallArg(*i, Args);
87
Chris Lattner8fdf3282008-06-24 17:04:18 +000088 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000089 // super is only valid in an Objective-C method
90 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000091 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
92 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000093 OMD->getClassInterface(),
94 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000095 isClassMessage,
96 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000097 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000098 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
99 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +0000100}
101
Daniel Dunbaraf05bb92008-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 Dunbar7ded7f42008-08-15 22:20:32 +0000108 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Chris Lattner41110242008-06-17 18:05:57 +0000109 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
110
111 // Create a marker to make it easy to insert allocas into the entryblock
112 // later. Don't create this with the builder, because we don't want it
113 // folded.
114 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
115 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
116 EntryBB);
117
118 FnRetTy = OMD->getResultType();
119 CurFuncDecl = OMD;
120
121 Builder.SetInsertPoint(EntryBB);
122
123 // Emit allocs for param decls. Give the LLVM Argument nodes names.
124 llvm::Function::arg_iterator AI = CurFn->arg_begin();
125
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000126 // Name the struct return argument.
Chris Lattner41110242008-06-17 18:05:57 +0000127 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000128 AI->setName("agg.result");
Chris Lattner41110242008-06-17 18:05:57 +0000129 ++AI;
130 }
Chris Lattner41110242008-06-17 18:05:57 +0000131
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000132 // Add implicit parameters to the decl map.
133 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner41110242008-06-17 18:05:57 +0000134 ++AI;
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000135
136 EmitParmDecl(*OMD->getCmdDecl(), AI);
137 ++AI;
Chris Lattner41110242008-06-17 18:05:57 +0000138
139 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
140 assert(AI != CurFn->arg_end() && "Argument mismatch!");
141 EmitParmDecl(*OMD->getParamDecl(i), AI);
142 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000143 assert(AI == CurFn->arg_end() && "Argument mismatch");
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000144}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000145
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000146/// Generate an Objective-C method. An Objective-C method is a C function with
147/// its pointer, name, and types registered in the class struture.
148void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
149 StartObjCMethod(OMD);
150 EmitStmt(OMD->getBody());
151
152 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
153 if (S) {
154 FinishFunction(S->getRBracLoc());
155 } else {
156 FinishFunction();
157 }
158}
159
160// FIXME: I wasn't sure about the synthesis approach. If we end up
161// generating an AST for the whole body we can just fall back to
162// having a GenerateFunction which takes the body Stmt.
163
164/// GenerateObjCGetter - Generate an Objective-C property getter
165/// function. The given Decl must be either an ObjCCategoryImplDecl
166/// or an ObjCImplementationDecl.
167void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
168 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
169 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
170 assert(OMD && "Invalid call to generate getter (empty method)");
171 // FIXME: This is rather murky, we create this here since they will
172 // not have been created by Sema for us.
173 OMD->createImplicitParams(getContext());
174 StartObjCMethod(OMD);
175
176 // FIXME: What about nonatomic?
177 SourceLocation Loc = PD->getLocation();
178 ValueDecl *Self = OMD->getSelfDecl();
179 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
180 DeclRefExpr Base(Self, Self->getType(), Loc);
181 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
182 true, true);
183 ReturnStmt Return(Loc, &IvarRef);
184 EmitStmt(&Return);
185
186 FinishFunction();
187}
188
189/// GenerateObjCSetter - Generate an Objective-C property setter
190/// function. The given Decl must be either an ObjCCategoryImplDecl
191/// or an ObjCImplementationDecl.
192void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
193 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
194 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
195 assert(OMD && "Invalid call to generate setter (empty method)");
196 // FIXME: This is rather murky, we create this here since they will
197 // not have been created by Sema for us.
198 OMD->createImplicitParams(getContext());
199 StartObjCMethod(OMD);
200
201 switch (PD->getSetterKind()) {
202 case ObjCPropertyDecl::Assign: break;
203 case ObjCPropertyDecl::Copy:
204 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
205 break;
206 case ObjCPropertyDecl::Retain:
207 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
208 break;
209 }
210
211 // FIXME: What about nonatomic?
212 SourceLocation Loc = PD->getLocation();
213 ValueDecl *Self = OMD->getSelfDecl();
214 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
215 DeclRefExpr Base(Self, Self->getType(), Loc);
216 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
217 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
218 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
219 true, true);
220 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
221 Ivar->getType(), Loc);
222 EmitStmt(&Assign);
223
224 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000225}
226
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000227llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
228 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
229 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000230}
231
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000232RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
233 // Determine getter selector.
234 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000235 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
236 S = E->getGetterMethod()->getSelector();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000237 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000238 S = E->getProperty()->getGetterName();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000239 }
240
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000241 return CGM.getObjCRuntime().
242 GenerateMessageSend(*this, E->getType(), S,
243 EmitScalarExpr(E->getBase()),
244 false, CallArgList());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000245}
246
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000247void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
248 RValue Src) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000249 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000250 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
251 ObjCMethodDecl *Setter = E->getSetterMethod();
252
253 if (Setter) {
254 S = Setter->getSelector();
255 } else {
256 // FIXME: This should be diagnosed by sema.
257 SourceRange Range = E->getSourceRange();
258 CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()),
259 diag::err_typecheck_assign_const, 0, 0,
260 &Range, 1);
261 return;
262 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000263 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000264 S = E->getProperty()->getSetterName();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000265 }
266
267 CallArgList Args;
268 EmitCallArg(Src, E->getType(), Args);
269 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
270 EmitScalarExpr(E->getBase()),
271 false, Args);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000272}
273
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000274void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
275{
Anders Carlssonf484c312008-08-31 02:33:12 +0000276 llvm::Value *DeclAddress;
277 QualType ElementTy;
278
279 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
280 EmitStmt(SD);
281
282 ElementTy = cast<ValueDecl>(SD->getDecl())->getType();
283 DeclAddress = LocalDeclMap[SD->getDecl()];
284 } else {
285 ElementTy = cast<Expr>(S.getElement())->getType();
286 DeclAddress = 0;
287 }
288
289 // Fast enumeration state.
290 QualType StateTy = getContext().getObjCFastEnumerationStateType();
291 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
292 "state.ptr");
293 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000294 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000295
296 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000297 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000298
299 // Get selector
300 llvm::SmallVector<IdentifierInfo*, 3> II;
301 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
302 II.push_back(&CGM.getContext().Idents.get("objects"));
303 II.push_back(&CGM.getContext().Idents.get("count"));
304 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
305 &II[0]);
306
307 QualType ItemsTy =
308 getContext().getConstantArrayType(getContext().getObjCIdType(),
309 llvm::APInt(32, NumItems),
310 ArrayType::Normal, 0);
311 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
312
313 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
314
315 CallArgList Args;
316 Args.push_back(std::make_pair(StatePtr,
317 getContext().getPointerType(StateTy)));
318
319 Args.push_back(std::make_pair(ItemsPtr,
320 getContext().getPointerType(ItemsTy)));
321
322 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
323 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
324 Args.push_back(std::make_pair(Count, getContext().UnsignedLongTy));
325
326 RValue CountRV =
327 CGM.getObjCRuntime().GenerateMessageSend(*this,
328 getContext().UnsignedLongTy,
329 FastEnumSel,
330 Collection, false, Args);
331
332 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
333 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
334
335 llvm::BasicBlock *NoElements = llvm::BasicBlock::Create("noelements");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000336 llvm::BasicBlock *SetStartMutations =
337 llvm::BasicBlock::Create("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000338
339 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
340 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
341
342 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000343 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000344
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000345 EmitBlock(SetStartMutations);
346
347 llvm::Value *StartMutationsPtr =
348 CreateTempAlloca(UnsignedLongLTy);
349
350 llvm::Value *StateMutationsPtrPtr =
351 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
352 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
353 "mutationsptr");
354
355 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
356 "mutations");
357
358 Builder.CreateStore(StateMutations, StartMutationsPtr);
359
360 llvm::BasicBlock *LoopStart = llvm::BasicBlock::Create("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000361 EmitBlock(LoopStart);
362
Anders Carlssonf484c312008-08-31 02:33:12 +0000363 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
364 Builder.CreateStore(Zero, CounterPtr);
365
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000366 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000367 EmitBlock(LoopBody);
368
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000369 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
370 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
371
372 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
373 "mutations");
374 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
375 StartMutations,
376 "tobool");
377
378
379 llvm::BasicBlock *WasMutated = llvm::BasicBlock::Create("wasmutated");
380 llvm::BasicBlock *WasNotMutated = llvm::BasicBlock::Create("wasnotmutated");
381
382 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
383
384 EmitBlock(WasMutated);
385 llvm::Value *V =
386 Builder.CreateBitCast(Collection,
387 ConvertType(getContext().getObjCIdType()),
388 "tmp");
389 Builder.CreateCall(CGM.getObjCRuntime().EnumerationMutationFunction(),
390 V);
391
392 EmitBlock(WasNotMutated);
393
Anders Carlssonf484c312008-08-31 02:33:12 +0000394 llvm::Value *StateItemsPtr =
395 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
396
397 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
398
399 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
400 "stateitems");
401
402 llvm::Value *CurrentItemPtr =
403 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
404
405 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
406
407 // Cast the item to the right type.
408 CurrentItem = Builder.CreateBitCast(CurrentItem,
409 ConvertType(ElementTy), "tmp");
410
411 if (!DeclAddress) {
412 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
413
414 // Set the value to null.
415 Builder.CreateStore(CurrentItem, LV.getAddress());
416 } else
417 Builder.CreateStore(CurrentItem, DeclAddress);
418
419 // Increment the counter.
420 Counter = Builder.CreateAdd(Counter,
421 llvm::ConstantInt::get(UnsignedLongLTy, 1));
422 Builder.CreateStore(Counter, CounterPtr);
423
424 llvm::BasicBlock *LoopEnd = llvm::BasicBlock::Create("loopend");
425 llvm::BasicBlock *AfterBody = llvm::BasicBlock::Create("afterbody");
426
427 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
428
429 EmitStmt(S.getBody());
430
431 BreakContinueStack.pop_back();
432
433 EmitBlock(AfterBody);
434
435 llvm::BasicBlock *FetchMore = llvm::BasicBlock::Create("fetchmore");
436
437 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000438 Builder.CreateCondBr(IsLess, LoopStart, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000439
440 // Fetch more elements.
441 EmitBlock(FetchMore);
442
443 CountRV =
444 CGM.getObjCRuntime().GenerateMessageSend(*this,
445 getContext().UnsignedLongTy,
446 FastEnumSel,
447 Collection, false, Args);
448 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
449 Limit = Builder.CreateLoad(LimitPtr);
450
451 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
452 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
453
454 // No more elements.
455 EmitBlock(NoElements);
456
457 if (!DeclAddress) {
458 // If the element was not a declaration, set it to be null.
459
460 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
461
462 // Set the value to null.
463 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
464 LV.getAddress());
465 }
466
467 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000468}
469
Ted Kremenek2979ec72008-04-09 15:51:31 +0000470CGObjCRuntime::~CGObjCRuntime() {}