blob: a3da59410d96982a9f793d51869e3bb7080170e7 [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)
Daniel Dunbar46f45b92008-09-09 01:06:48 +000086 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000087
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.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000105void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000106 FunctionArgList Args;
107 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000108
Daniel Dunbar7c086512008-09-09 23:14:03 +0000109 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000110
Daniel Dunbar7c086512008-09-09 23:14:03 +0000111 Args.push_back(std::make_pair(OMD->getSelfDecl(),
112 OMD->getSelfDecl()->getType()));
113 Args.push_back(std::make_pair(OMD->getCmdDecl(),
114 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000115
Daniel Dunbar7c086512008-09-09 23:14:03 +0000116 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
117 ParmVarDecl *IPD = OMD->getParamDecl(i);
118 Args.push_back(std::make_pair(IPD, IPD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000119 }
Chris Lattner41110242008-06-17 18:05:57 +0000120
Daniel Dunbar7c086512008-09-09 23:14:03 +0000121 StartFunction(OMD, OMD->getResultType(), Fn, Args);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000122}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000123
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000124/// Generate an Objective-C method. An Objective-C method is a C function with
125/// its pointer, name, and types registered in the class struture.
126void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
127 StartObjCMethod(OMD);
128 EmitStmt(OMD->getBody());
129
130 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
131 if (S) {
132 FinishFunction(S->getRBracLoc());
133 } else {
134 FinishFunction();
135 }
136}
137
138// FIXME: I wasn't sure about the synthesis approach. If we end up
139// generating an AST for the whole body we can just fall back to
140// having a GenerateFunction which takes the body Stmt.
141
142/// GenerateObjCGetter - Generate an Objective-C property getter
143/// function. The given Decl must be either an ObjCCategoryImplDecl
144/// or an ObjCImplementationDecl.
145void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
146 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
147 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
148 assert(OMD && "Invalid call to generate getter (empty method)");
149 // FIXME: This is rather murky, we create this here since they will
150 // not have been created by Sema for us.
151 OMD->createImplicitParams(getContext());
152 StartObjCMethod(OMD);
153
154 // FIXME: What about nonatomic?
155 SourceLocation Loc = PD->getLocation();
156 ValueDecl *Self = OMD->getSelfDecl();
157 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
158 DeclRefExpr Base(Self, Self->getType(), Loc);
159 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
160 true, true);
161 ReturnStmt Return(Loc, &IvarRef);
162 EmitStmt(&Return);
163
164 FinishFunction();
165}
166
167/// GenerateObjCSetter - Generate an Objective-C property setter
168/// function. The given Decl must be either an ObjCCategoryImplDecl
169/// or an ObjCImplementationDecl.
170void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
171 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
172 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
173 assert(OMD && "Invalid call to generate setter (empty method)");
174 // FIXME: This is rather murky, we create this here since they will
175 // not have been created by Sema for us.
176 OMD->createImplicitParams(getContext());
177 StartObjCMethod(OMD);
178
179 switch (PD->getSetterKind()) {
180 case ObjCPropertyDecl::Assign: break;
181 case ObjCPropertyDecl::Copy:
182 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
183 break;
184 case ObjCPropertyDecl::Retain:
185 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
186 break;
187 }
188
189 // FIXME: What about nonatomic?
190 SourceLocation Loc = PD->getLocation();
191 ValueDecl *Self = OMD->getSelfDecl();
192 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
193 DeclRefExpr Base(Self, Self->getType(), Loc);
194 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
195 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
196 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
197 true, true);
198 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
199 Ivar->getType(), Loc);
200 EmitStmt(&Assign);
201
202 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000203}
204
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000205llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
206 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
207 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000208}
209
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000210RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
211 // Determine getter selector.
212 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000213 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
214 S = E->getGetterMethod()->getSelector();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000215 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000216 S = E->getProperty()->getGetterName();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000217 }
218
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000219 return CGM.getObjCRuntime().
220 GenerateMessageSend(*this, E->getType(), S,
221 EmitScalarExpr(E->getBase()),
222 false, CallArgList());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000223}
224
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000225void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
226 RValue Src) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000227 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000228 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
229 ObjCMethodDecl *Setter = E->getSetterMethod();
230
231 if (Setter) {
232 S = Setter->getSelector();
233 } else {
234 // FIXME: This should be diagnosed by sema.
235 SourceRange Range = E->getSourceRange();
236 CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()),
237 diag::err_typecheck_assign_const, 0, 0,
238 &Range, 1);
239 return;
240 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000241 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000242 S = E->getProperty()->getSetterName();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000243 }
244
245 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000246 Args.push_back(std::make_pair(Src, E->getType()));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000247 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
248 EmitScalarExpr(E->getBase()),
249 false, Args);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000250}
251
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000252void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
253{
Anders Carlssonf484c312008-08-31 02:33:12 +0000254 llvm::Value *DeclAddress;
255 QualType ElementTy;
256
257 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
258 EmitStmt(SD);
259
260 ElementTy = cast<ValueDecl>(SD->getDecl())->getType();
261 DeclAddress = LocalDeclMap[SD->getDecl()];
262 } else {
263 ElementTy = cast<Expr>(S.getElement())->getType();
264 DeclAddress = 0;
265 }
266
267 // Fast enumeration state.
268 QualType StateTy = getContext().getObjCFastEnumerationStateType();
269 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
270 "state.ptr");
271 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000272 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000273
274 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000275 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000276
277 // Get selector
278 llvm::SmallVector<IdentifierInfo*, 3> II;
279 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
280 II.push_back(&CGM.getContext().Idents.get("objects"));
281 II.push_back(&CGM.getContext().Idents.get("count"));
282 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
283 &II[0]);
284
285 QualType ItemsTy =
286 getContext().getConstantArrayType(getContext().getObjCIdType(),
287 llvm::APInt(32, NumItems),
288 ArrayType::Normal, 0);
289 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
290
291 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
292
293 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000294 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000295 getContext().getPointerType(StateTy)));
296
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000297 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000298 getContext().getPointerType(ItemsTy)));
299
300 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
301 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000302 Args.push_back(std::make_pair(RValue::get(Count),
303 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000304
305 RValue CountRV =
306 CGM.getObjCRuntime().GenerateMessageSend(*this,
307 getContext().UnsignedLongTy,
308 FastEnumSel,
309 Collection, false, Args);
310
311 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
312 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
313
314 llvm::BasicBlock *NoElements = llvm::BasicBlock::Create("noelements");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000315 llvm::BasicBlock *SetStartMutations =
316 llvm::BasicBlock::Create("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000317
318 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
319 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
320
321 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000322 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000323
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000324 EmitBlock(SetStartMutations);
325
326 llvm::Value *StartMutationsPtr =
327 CreateTempAlloca(UnsignedLongLTy);
328
329 llvm::Value *StateMutationsPtrPtr =
330 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
331 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
332 "mutationsptr");
333
334 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
335 "mutations");
336
337 Builder.CreateStore(StateMutations, StartMutationsPtr);
338
339 llvm::BasicBlock *LoopStart = llvm::BasicBlock::Create("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000340 EmitBlock(LoopStart);
341
Anders Carlssonf484c312008-08-31 02:33:12 +0000342 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
343 Builder.CreateStore(Zero, CounterPtr);
344
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000345 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000346 EmitBlock(LoopBody);
347
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000348 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
349 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
350
351 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
352 "mutations");
353 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
354 StartMutations,
355 "tobool");
356
357
358 llvm::BasicBlock *WasMutated = llvm::BasicBlock::Create("wasmutated");
359 llvm::BasicBlock *WasNotMutated = llvm::BasicBlock::Create("wasnotmutated");
360
361 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
362
363 EmitBlock(WasMutated);
364 llvm::Value *V =
365 Builder.CreateBitCast(Collection,
366 ConvertType(getContext().getObjCIdType()),
367 "tmp");
368 Builder.CreateCall(CGM.getObjCRuntime().EnumerationMutationFunction(),
369 V);
370
371 EmitBlock(WasNotMutated);
372
Anders Carlssonf484c312008-08-31 02:33:12 +0000373 llvm::Value *StateItemsPtr =
374 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
375
376 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
377
378 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
379 "stateitems");
380
381 llvm::Value *CurrentItemPtr =
382 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
383
384 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
385
386 // Cast the item to the right type.
387 CurrentItem = Builder.CreateBitCast(CurrentItem,
388 ConvertType(ElementTy), "tmp");
389
390 if (!DeclAddress) {
391 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
392
393 // Set the value to null.
394 Builder.CreateStore(CurrentItem, LV.getAddress());
395 } else
396 Builder.CreateStore(CurrentItem, DeclAddress);
397
398 // Increment the counter.
399 Counter = Builder.CreateAdd(Counter,
400 llvm::ConstantInt::get(UnsignedLongLTy, 1));
401 Builder.CreateStore(Counter, CounterPtr);
402
403 llvm::BasicBlock *LoopEnd = llvm::BasicBlock::Create("loopend");
404 llvm::BasicBlock *AfterBody = llvm::BasicBlock::Create("afterbody");
405
406 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
407
408 EmitStmt(S.getBody());
409
410 BreakContinueStack.pop_back();
411
412 EmitBlock(AfterBody);
413
414 llvm::BasicBlock *FetchMore = llvm::BasicBlock::Create("fetchmore");
415
416 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000417 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000418
419 // Fetch more elements.
420 EmitBlock(FetchMore);
421
422 CountRV =
423 CGM.getObjCRuntime().GenerateMessageSend(*this,
424 getContext().UnsignedLongTy,
425 FastEnumSel,
426 Collection, false, Args);
427 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
428 Limit = Builder.CreateLoad(LimitPtr);
429
430 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
431 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
432
433 // No more elements.
434 EmitBlock(NoElements);
435
436 if (!DeclAddress) {
437 // If the element was not a declaration, set it to be null.
438
439 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
440
441 // Set the value to null.
442 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
443 LV.getAddress());
444 }
445
446 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000447}
448
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000449void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
450{
451 CGM.getObjCRuntime().EmitTryStmt(*this, S);
452}
453
454void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
455{
456 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
457}
458
Ted Kremenek2979ec72008-04-09 15:51:31 +0000459CGObjCRuntime::~CGObjCRuntime() {}