blob: 1157f48d39d1793d1f1cffb52e32751b009dc5ea [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"
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +000021#include "llvm/Target/TargetData.h"
Chris Lattner41110242008-06-17 18:05:57 +000022
Anders Carlsson55085182007-08-21 17:43:55 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattner8fdf3282008-06-24 17:04:18 +000026/// Emits an instance of NSConstantString representing the object.
Daniel Dunbar71fcec92008-11-25 21:53:21 +000027llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
28{
29 std::string String(E->getString()->getStrData(),
30 E->getString()->getByteLength());
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
Daniel Dunbared7c6182008-08-20 00:28:19 +000032 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000033 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-06-24 17:04:18 +000034}
35
36/// Emit a selector.
37llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
38 // Untyped selector.
39 // Note that this implementation allows for non-constant strings to be passed
40 // as arguments to @selector(). Currently, the only thing preventing this
41 // behaviour is the type checking in the front end.
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000042 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000043}
44
Daniel Dunbared7c6182008-08-20 00:28:19 +000045llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
46 // FIXME: This should pass the Decl not the name.
47 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
48}
Chris Lattner8fdf3282008-06-24 17:04:18 +000049
50
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000051RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000052 // Only the lookup mechanism and first two arguments of the method
53 // implementation vary between runtimes. We can get the receiver and
54 // arguments in generic code.
55
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000056 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000057 const Expr *ReceiverExpr = E->getReceiver();
58 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000059 bool isClassMessage = false;
Chris Lattner8fdf3282008-06-24 17:04:18 +000060 // Find the receiver
61 llvm::Value *Receiver;
62 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000063 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
64
65 // Very special case, super send in class method. The receiver is
66 // self (the class object) and the send uses super semantics.
67 if (!OID) {
Chris Lattner92e62b02008-11-20 04:42:34 +000068 assert(E->getClassName()->isStr("super") &&
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000069 "Unexpected missing class interface in message send.");
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000070 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000071 Receiver = LoadObjCSelf();
72 } else {
73 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000074 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +000075
76 isClassMessage = true;
Douglas Gregorcd9b46e2008-11-04 14:56:14 +000077 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000078 isSuperMessage = true;
79 Receiver = LoadObjCSelf();
80 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000081 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000082 }
83
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000084 CallArgList Args;
85 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
86 i != e; ++i)
Daniel Dunbar46f45b92008-09-09 01:06:48 +000087 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000088
Chris Lattner8fdf3282008-06-24 17:04:18 +000089 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000090 // super is only valid in an Objective-C method
91 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000092 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
93 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000094 OMD->getClassInterface(),
95 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000096 isClassMessage,
97 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000098 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000099 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
100 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +0000101}
102
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000103/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
104/// the LLVM function and sets the other context used by
105/// CodeGenFunction.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000106void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000107 FunctionArgList Args;
108 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000109
Daniel Dunbar7c086512008-09-09 23:14:03 +0000110 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000111
Daniel Dunbar7c086512008-09-09 23:14:03 +0000112 Args.push_back(std::make_pair(OMD->getSelfDecl(),
113 OMD->getSelfDecl()->getType()));
114 Args.push_back(std::make_pair(OMD->getCmdDecl(),
115 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000116
Daniel Dunbar7c086512008-09-09 23:14:03 +0000117 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
118 ParmVarDecl *IPD = OMD->getParamDecl(i);
119 Args.push_back(std::make_pair(IPD, IPD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000120 }
Chris Lattner41110242008-06-17 18:05:57 +0000121
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000122 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000123}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000124
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000125/// Generate an Objective-C method. An Objective-C method is a C function with
126/// its pointer, name, and types registered in the class struture.
127void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
128 StartObjCMethod(OMD);
129 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000130 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000131}
132
133// FIXME: I wasn't sure about the synthesis approach. If we end up
134// generating an AST for the whole body we can just fall back to
135// having a GenerateFunction which takes the body Stmt.
136
137/// GenerateObjCGetter - Generate an Objective-C property getter
138/// function. The given Decl must be either an ObjCCategoryImplDecl
139/// or an ObjCImplementationDecl.
140void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000141 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000142 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
143 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
144 assert(OMD && "Invalid call to generate getter (empty method)");
145 // FIXME: This is rather murky, we create this here since they will
146 // not have been created by Sema for us.
147 OMD->createImplicitParams(getContext());
148 StartObjCMethod(OMD);
149
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000150 // Determine if we should use an objc_getProperty call for
151 // this. Non-atomic and properties with assign semantics are
152 // directly evaluated, and in gc-only mode we don't need it at all.
153 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
154 PD->getSetterKind() != ObjCPropertyDecl::Assign &&
155 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
156 llvm::Value *GetPropertyFn =
157 CGM.getObjCRuntime().GetPropertyGetFunction();
158
159 if (!GetPropertyFn) {
160 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
161 FinishFunction();
162 return;
163 }
164
165 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
166 // FIXME: Can't this be simpler? This might even be worse than the
167 // corresponding gcc code.
168 CodeGenTypes &Types = CGM.getTypes();
169 ValueDecl *Cmd = OMD->getCmdDecl();
170 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
171 QualType IdTy = getContext().getObjCIdType();
172 llvm::Value *SelfAsId =
173 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
174 llvm::Value *Offset = EmitIvarOffset(OMD->getClassInterface(), Ivar);
175 llvm::Value *True =
176 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 1);
177 CallArgList Args;
178 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
179 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
180 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
181 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
182 RValue RV = EmitCall(GetPropertyFn, PD->getType(), Args);
183 // We need to fix the type here. Ivars with copy & retain are
184 // always objects so we don't need to worry about complex or
185 // aggregates.
186 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
187 Types.ConvertType(PD->getType())));
188 EmitReturnOfRValue(RV, PD->getType());
189 } else {
190 EmitReturnOfRValue(EmitLoadOfLValue(EmitLValueForIvar(LoadObjCSelf(),
191 Ivar, 0),
192 Ivar->getType()),
193 PD->getType());
194 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000195
196 FinishFunction();
197}
198
199/// GenerateObjCSetter - Generate an Objective-C property setter
200/// function. The given Decl must be either an ObjCCategoryImplDecl
201/// or an ObjCImplementationDecl.
202void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000203 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000204 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
205 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
206 assert(OMD && "Invalid call to generate setter (empty method)");
207 // FIXME: This is rather murky, we create this here since they will
208 // not have been created by Sema for us.
209 OMD->createImplicitParams(getContext());
210 StartObjCMethod(OMD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000211
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000212 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
213 bool IsAtomic =
214 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
215
216 // Determine if we should use an objc_setProperty call for
217 // this. Properties with 'copy' semantics always use it, as do
218 // non-atomic properties with 'release' semantics as long as we are
219 // not in gc-only mode.
220 if (IsCopy ||
221 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
222 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
223 llvm::Value *SetPropertyFn =
224 CGM.getObjCRuntime().GetPropertySetFunction();
225
226 if (!SetPropertyFn) {
227 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
228 FinishFunction();
229 return;
230 }
231
232 // Emit objc_setProperty((id) self, _cmd, offset, arg,
233 // <is-atomic>, <is-copy>).
234 // FIXME: Can't this be simpler? This might even be worse than the
235 // corresponding gcc code.
236 CodeGenTypes &Types = CGM.getTypes();
237 ValueDecl *Cmd = OMD->getCmdDecl();
238 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
239 QualType IdTy = getContext().getObjCIdType();
240 llvm::Value *SelfAsId =
241 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
242 llvm::Value *Offset = EmitIvarOffset(OMD->getClassInterface(), Ivar);
243 llvm::Value *Arg = LocalDeclMap[OMD->getParamDecl(0)];
244 llvm::Value *ArgAsId =
245 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
246 Types.ConvertType(IdTy));
247 llvm::Value *True =
248 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 1);
249 llvm::Value *False =
250 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 0);
251 CallArgList Args;
252 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
253 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
254 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
255 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
256 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
257 getContext().BoolTy));
258 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
259 getContext().BoolTy));
260 EmitCall(SetPropertyFn, PD->getType(), Args);
261 } else {
262 SourceLocation Loc = PD->getLocation();
263 ValueDecl *Self = OMD->getSelfDecl();
264 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
265 DeclRefExpr Base(Self, Self->getType(), Loc);
266 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
267 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
268 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
269 true, true);
270 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
271 Ivar->getType(), Loc);
272 EmitStmt(&Assign);
273 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000274
275 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000276}
277
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000278llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000279 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
280 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000281}
282
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000283RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000284 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000285 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
286 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000287 return CGM.getObjCRuntime().
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000288 GenerateMessageSend(*this, Exp->getType(), S,
289 EmitScalarExpr(E->getBase()),
290 false, CallArgList());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000291 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000292 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
293 Selector S = E->getGetterMethod()->getSelector();
294 return CGM.getObjCRuntime().
295 GenerateMessageSend(*this, Exp->getType(), S,
296 EmitScalarExpr(E->getBase()),
297 false, CallArgList());
298 }
299 else
300 assert (0 && "bad expression node in EmitObjCPropertyGet");
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000301}
302
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000303void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000304 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000305 // FIXME: Split it into two separate routines.
306 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
307 Selector S = E->getProperty()->getSetterName();
308 CallArgList Args;
309 Args.push_back(std::make_pair(Src, E->getType()));
310 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
311 EmitScalarExpr(E->getBase()),
312 false, Args);
313 }
314 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
315 Selector S = E->getSetterMethod()->getSelector();
316 CallArgList Args;
317 Args.push_back(std::make_pair(Src, E->getType()));
318 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
319 EmitScalarExpr(E->getBase()),
320 false, Args);
321 }
322 else
323 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000324}
325
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000326void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
327{
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000328 llvm::Function *EnumerationMutationFn =
329 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000330 llvm::Value *DeclAddress;
331 QualType ElementTy;
332
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000333 if (!EnumerationMutationFn) {
334 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
335 return;
336 }
337
Anders Carlssonf484c312008-08-31 02:33:12 +0000338 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
339 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000340 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Ted Kremenek39741ce2008-10-06 20:59:48 +0000341 const ScopedDecl* D = SD->getSolitaryDecl();
342 ElementTy = cast<ValueDecl>(D)->getType();
343 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000344 } else {
345 ElementTy = cast<Expr>(S.getElement())->getType();
346 DeclAddress = 0;
347 }
348
349 // Fast enumeration state.
350 QualType StateTy = getContext().getObjCFastEnumerationStateType();
351 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
352 "state.ptr");
353 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000354 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000355
356 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000357 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000358
359 // Get selector
360 llvm::SmallVector<IdentifierInfo*, 3> II;
361 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
362 II.push_back(&CGM.getContext().Idents.get("objects"));
363 II.push_back(&CGM.getContext().Idents.get("count"));
364 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
365 &II[0]);
366
367 QualType ItemsTy =
368 getContext().getConstantArrayType(getContext().getObjCIdType(),
369 llvm::APInt(32, NumItems),
370 ArrayType::Normal, 0);
371 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
372
373 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
374
375 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000376 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000377 getContext().getPointerType(StateTy)));
378
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000379 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000380 getContext().getPointerType(ItemsTy)));
381
382 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
383 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000384 Args.push_back(std::make_pair(RValue::get(Count),
385 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000386
387 RValue CountRV =
388 CGM.getObjCRuntime().GenerateMessageSend(*this,
389 getContext().UnsignedLongTy,
390 FastEnumSel,
391 Collection, false, Args);
392
393 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
394 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
395
Daniel Dunbar55e87422008-11-11 02:29:29 +0000396 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
397 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000398
399 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
400 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
401
402 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000403 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000404
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000405 EmitBlock(SetStartMutations);
406
407 llvm::Value *StartMutationsPtr =
408 CreateTempAlloca(UnsignedLongLTy);
409
410 llvm::Value *StateMutationsPtrPtr =
411 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
412 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
413 "mutationsptr");
414
415 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
416 "mutations");
417
418 Builder.CreateStore(StateMutations, StartMutationsPtr);
419
Daniel Dunbar55e87422008-11-11 02:29:29 +0000420 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000421 EmitBlock(LoopStart);
422
Anders Carlssonf484c312008-08-31 02:33:12 +0000423 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
424 Builder.CreateStore(Zero, CounterPtr);
425
Daniel Dunbar55e87422008-11-11 02:29:29 +0000426 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000427 EmitBlock(LoopBody);
428
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000429 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
430 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
431
432 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
433 "mutations");
434 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
435 StartMutations,
436 "tobool");
437
438
Daniel Dunbar55e87422008-11-11 02:29:29 +0000439 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
440 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000441
442 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
443
444 EmitBlock(WasMutated);
445 llvm::Value *V =
446 Builder.CreateBitCast(Collection,
447 ConvertType(getContext().getObjCIdType()),
448 "tmp");
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000449 Builder.CreateCall(EnumerationMutationFn, V);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000450
451 EmitBlock(WasNotMutated);
452
Anders Carlssonf484c312008-08-31 02:33:12 +0000453 llvm::Value *StateItemsPtr =
454 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
455
456 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
457
458 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
459 "stateitems");
460
461 llvm::Value *CurrentItemPtr =
462 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
463
464 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
465
466 // Cast the item to the right type.
467 CurrentItem = Builder.CreateBitCast(CurrentItem,
468 ConvertType(ElementTy), "tmp");
469
470 if (!DeclAddress) {
471 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
472
473 // Set the value to null.
474 Builder.CreateStore(CurrentItem, LV.getAddress());
475 } else
476 Builder.CreateStore(CurrentItem, DeclAddress);
477
478 // Increment the counter.
479 Counter = Builder.CreateAdd(Counter,
480 llvm::ConstantInt::get(UnsignedLongLTy, 1));
481 Builder.CreateStore(Counter, CounterPtr);
482
Daniel Dunbar55e87422008-11-11 02:29:29 +0000483 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
484 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000485
486 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
487
488 EmitStmt(S.getBody());
489
490 BreakContinueStack.pop_back();
491
492 EmitBlock(AfterBody);
493
Daniel Dunbar55e87422008-11-11 02:29:29 +0000494 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Anders Carlssonf484c312008-08-31 02:33:12 +0000495
496 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000497 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000498
499 // Fetch more elements.
500 EmitBlock(FetchMore);
501
502 CountRV =
503 CGM.getObjCRuntime().GenerateMessageSend(*this,
504 getContext().UnsignedLongTy,
505 FastEnumSel,
506 Collection, false, Args);
507 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
508 Limit = Builder.CreateLoad(LimitPtr);
509
510 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
511 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
512
513 // No more elements.
514 EmitBlock(NoElements);
515
516 if (!DeclAddress) {
517 // If the element was not a declaration, set it to be null.
518
519 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
520
521 // Set the value to null.
522 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
523 LV.getAddress());
524 }
525
526 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000527}
528
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000529void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
530{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000531 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000532}
533
534void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
535{
536 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
537}
538
Chris Lattner10cac6f2008-11-15 21:26:17 +0000539void CodeGenFunction::EmitObjCAtSynchronizedStmt(
540 const ObjCAtSynchronizedStmt &S)
541{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000542 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000543}
544
Ted Kremenek2979ec72008-04-09 15:51:31 +0000545CGObjCRuntime::~CGObjCRuntime() {}