blob: b5834e68e534a8d119321424da9764cc2b18317e [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.
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);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000109
110 CGM.SetMethodAttributes(OMD, CurFn);
111
Chris Lattner41110242008-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
124 Builder.SetInsertPoint(EntryBB);
125
126 // Emit allocs for param decls. Give the LLVM Argument nodes names.
127 llvm::Function::arg_iterator AI = CurFn->arg_begin();
128
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000129 // Name the struct return argument.
Chris Lattner41110242008-06-17 18:05:57 +0000130 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000131 AI->setName("agg.result");
Chris Lattner41110242008-06-17 18:05:57 +0000132 ++AI;
133 }
Chris Lattner41110242008-06-17 18:05:57 +0000134
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000135 // Add implicit parameters to the decl map.
136 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner41110242008-06-17 18:05:57 +0000137 ++AI;
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000138
139 EmitParmDecl(*OMD->getCmdDecl(), AI);
140 ++AI;
Chris Lattner41110242008-06-17 18:05:57 +0000141
142 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
143 assert(AI != CurFn->arg_end() && "Argument mismatch!");
144 EmitParmDecl(*OMD->getParamDecl(i), AI);
145 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000146 assert(AI == CurFn->arg_end() && "Argument mismatch");
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000147}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000148
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000149/// Generate an Objective-C method. An Objective-C method is a C function with
150/// its pointer, name, and types registered in the class struture.
151void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
152 StartObjCMethod(OMD);
153 EmitStmt(OMD->getBody());
154
155 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
156 if (S) {
157 FinishFunction(S->getRBracLoc());
158 } else {
159 FinishFunction();
160 }
161}
162
163// FIXME: I wasn't sure about the synthesis approach. If we end up
164// generating an AST for the whole body we can just fall back to
165// having a GenerateFunction which takes the body Stmt.
166
167/// GenerateObjCGetter - Generate an Objective-C property getter
168/// function. The given Decl must be either an ObjCCategoryImplDecl
169/// or an ObjCImplementationDecl.
170void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
171 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
172 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
173 assert(OMD && "Invalid call to generate getter (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 // FIXME: What about nonatomic?
180 SourceLocation Loc = PD->getLocation();
181 ValueDecl *Self = OMD->getSelfDecl();
182 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
183 DeclRefExpr Base(Self, Self->getType(), Loc);
184 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
185 true, true);
186 ReturnStmt Return(Loc, &IvarRef);
187 EmitStmt(&Return);
188
189 FinishFunction();
190}
191
192/// GenerateObjCSetter - Generate an Objective-C property setter
193/// function. The given Decl must be either an ObjCCategoryImplDecl
194/// or an ObjCImplementationDecl.
195void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
196 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
197 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
198 assert(OMD && "Invalid call to generate setter (empty method)");
199 // FIXME: This is rather murky, we create this here since they will
200 // not have been created by Sema for us.
201 OMD->createImplicitParams(getContext());
202 StartObjCMethod(OMD);
203
204 switch (PD->getSetterKind()) {
205 case ObjCPropertyDecl::Assign: break;
206 case ObjCPropertyDecl::Copy:
207 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
208 break;
209 case ObjCPropertyDecl::Retain:
210 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
211 break;
212 }
213
214 // FIXME: What about nonatomic?
215 SourceLocation Loc = PD->getLocation();
216 ValueDecl *Self = OMD->getSelfDecl();
217 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
218 DeclRefExpr Base(Self, Self->getType(), Loc);
219 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
220 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
221 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
222 true, true);
223 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
224 Ivar->getType(), Loc);
225 EmitStmt(&Assign);
226
227 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000228}
229
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000230llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
231 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
232 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000233}
234
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000235RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
236 // Determine getter selector.
237 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000238 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
239 S = E->getGetterMethod()->getSelector();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000240 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000241 S = E->getProperty()->getGetterName();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000242 }
243
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000244 return CGM.getObjCRuntime().
245 GenerateMessageSend(*this, E->getType(), S,
246 EmitScalarExpr(E->getBase()),
247 false, CallArgList());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000248}
249
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000250void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
251 RValue Src) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000252 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000253 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
254 ObjCMethodDecl *Setter = E->getSetterMethod();
255
256 if (Setter) {
257 S = Setter->getSelector();
258 } else {
259 // FIXME: This should be diagnosed by sema.
260 SourceRange Range = E->getSourceRange();
261 CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()),
262 diag::err_typecheck_assign_const, 0, 0,
263 &Range, 1);
264 return;
265 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000266 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000267 S = E->getProperty()->getSetterName();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000268 }
269
270 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000271 Args.push_back(std::make_pair(Src, E->getType()));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000272 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
273 EmitScalarExpr(E->getBase()),
274 false, Args);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000275}
276
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000277void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
278{
Anders Carlssonf484c312008-08-31 02:33:12 +0000279 llvm::Value *DeclAddress;
280 QualType ElementTy;
281
282 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
283 EmitStmt(SD);
284
285 ElementTy = cast<ValueDecl>(SD->getDecl())->getType();
286 DeclAddress = LocalDeclMap[SD->getDecl()];
287 } else {
288 ElementTy = cast<Expr>(S.getElement())->getType();
289 DeclAddress = 0;
290 }
291
292 // Fast enumeration state.
293 QualType StateTy = getContext().getObjCFastEnumerationStateType();
294 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
295 "state.ptr");
296 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000297 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000298
299 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000300 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000301
302 // Get selector
303 llvm::SmallVector<IdentifierInfo*, 3> II;
304 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
305 II.push_back(&CGM.getContext().Idents.get("objects"));
306 II.push_back(&CGM.getContext().Idents.get("count"));
307 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
308 &II[0]);
309
310 QualType ItemsTy =
311 getContext().getConstantArrayType(getContext().getObjCIdType(),
312 llvm::APInt(32, NumItems),
313 ArrayType::Normal, 0);
314 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
315
316 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
317
318 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000319 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000320 getContext().getPointerType(StateTy)));
321
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000322 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000323 getContext().getPointerType(ItemsTy)));
324
325 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
326 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000327 Args.push_back(std::make_pair(RValue::get(Count),
328 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000329
330 RValue CountRV =
331 CGM.getObjCRuntime().GenerateMessageSend(*this,
332 getContext().UnsignedLongTy,
333 FastEnumSel,
334 Collection, false, Args);
335
336 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
337 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
338
339 llvm::BasicBlock *NoElements = llvm::BasicBlock::Create("noelements");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000340 llvm::BasicBlock *SetStartMutations =
341 llvm::BasicBlock::Create("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000342
343 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
344 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
345
346 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000347 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000348
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000349 EmitBlock(SetStartMutations);
350
351 llvm::Value *StartMutationsPtr =
352 CreateTempAlloca(UnsignedLongLTy);
353
354 llvm::Value *StateMutationsPtrPtr =
355 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
356 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
357 "mutationsptr");
358
359 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
360 "mutations");
361
362 Builder.CreateStore(StateMutations, StartMutationsPtr);
363
364 llvm::BasicBlock *LoopStart = llvm::BasicBlock::Create("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000365 EmitBlock(LoopStart);
366
Anders Carlssonf484c312008-08-31 02:33:12 +0000367 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
368 Builder.CreateStore(Zero, CounterPtr);
369
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000370 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000371 EmitBlock(LoopBody);
372
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000373 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
374 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
375
376 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
377 "mutations");
378 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
379 StartMutations,
380 "tobool");
381
382
383 llvm::BasicBlock *WasMutated = llvm::BasicBlock::Create("wasmutated");
384 llvm::BasicBlock *WasNotMutated = llvm::BasicBlock::Create("wasnotmutated");
385
386 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
387
388 EmitBlock(WasMutated);
389 llvm::Value *V =
390 Builder.CreateBitCast(Collection,
391 ConvertType(getContext().getObjCIdType()),
392 "tmp");
393 Builder.CreateCall(CGM.getObjCRuntime().EnumerationMutationFunction(),
394 V);
395
396 EmitBlock(WasNotMutated);
397
Anders Carlssonf484c312008-08-31 02:33:12 +0000398 llvm::Value *StateItemsPtr =
399 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
400
401 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
402
403 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
404 "stateitems");
405
406 llvm::Value *CurrentItemPtr =
407 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
408
409 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
410
411 // Cast the item to the right type.
412 CurrentItem = Builder.CreateBitCast(CurrentItem,
413 ConvertType(ElementTy), "tmp");
414
415 if (!DeclAddress) {
416 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
417
418 // Set the value to null.
419 Builder.CreateStore(CurrentItem, LV.getAddress());
420 } else
421 Builder.CreateStore(CurrentItem, DeclAddress);
422
423 // Increment the counter.
424 Counter = Builder.CreateAdd(Counter,
425 llvm::ConstantInt::get(UnsignedLongLTy, 1));
426 Builder.CreateStore(Counter, CounterPtr);
427
428 llvm::BasicBlock *LoopEnd = llvm::BasicBlock::Create("loopend");
429 llvm::BasicBlock *AfterBody = llvm::BasicBlock::Create("afterbody");
430
431 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
432
433 EmitStmt(S.getBody());
434
435 BreakContinueStack.pop_back();
436
437 EmitBlock(AfterBody);
438
439 llvm::BasicBlock *FetchMore = llvm::BasicBlock::Create("fetchmore");
440
441 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000442 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000443
444 // Fetch more elements.
445 EmitBlock(FetchMore);
446
447 CountRV =
448 CGM.getObjCRuntime().GenerateMessageSend(*this,
449 getContext().UnsignedLongTy,
450 FastEnumSel,
451 Collection, false, Args);
452 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
453 Limit = Builder.CreateLoad(LimitPtr);
454
455 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
456 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
457
458 // No more elements.
459 EmitBlock(NoElements);
460
461 if (!DeclAddress) {
462 // If the element was not a declaration, set it to be null.
463
464 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
465
466 // Set the value to null.
467 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
468 LV.getAddress());
469 }
470
471 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000472}
473
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000474void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
475{
476 CGM.getObjCRuntime().EmitTryStmt(*this, S);
477}
478
479void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
480{
481 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
482}
483
Ted Kremenek2979ec72008-04-09 15:51:31 +0000484CGObjCRuntime::~CGObjCRuntime() {}