blob: efdb0db129fa668e7fbfe0dac4c2aacb79068b71 [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 Dunbarbbce49b2008-08-12 00:12:39 +000027llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) {
28 std::string String(E->getString()->getStrData(), E->getString()->getByteLength());
29 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
Daniel Dunbared7c6182008-08-20 00:28:19 +000030 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-06-24 17:04:18 +000032}
33
34/// Emit a selector.
35llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
36 // Untyped selector.
37 // Note that this implementation allows for non-constant strings to be passed
38 // as arguments to @selector(). Currently, the only thing preventing this
39 // behaviour is the type checking in the front end.
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000040 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000041}
42
Daniel Dunbared7c6182008-08-20 00:28:19 +000043llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
44 // FIXME: This should pass the Decl not the name.
45 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
46}
Chris Lattner8fdf3282008-06-24 17:04:18 +000047
48
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000049RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000050 // Only the lookup mechanism and first two arguments of the method
51 // implementation vary between runtimes. We can get the receiver and
52 // arguments in generic code.
53
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000054 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000055 const Expr *ReceiverExpr = E->getReceiver();
56 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000057 bool isClassMessage = false;
Chris Lattner8fdf3282008-06-24 17:04:18 +000058 // Find the receiver
59 llvm::Value *Receiver;
60 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000061 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
62
63 // Very special case, super send in class method. The receiver is
64 // self (the class object) and the send uses super semantics.
65 if (!OID) {
Chris Lattner92e62b02008-11-20 04:42:34 +000066 assert(E->getClassName()->isStr("super") &&
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000067 "Unexpected missing class interface in message send.");
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000068 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000069 Receiver = LoadObjCSelf();
70 } else {
71 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000072 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +000073
74 isClassMessage = true;
Douglas Gregorcd9b46e2008-11-04 14:56:14 +000075 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000076 isSuperMessage = true;
77 Receiver = LoadObjCSelf();
78 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000079 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000080 }
81
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000082 CallArgList Args;
83 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
84 i != e; ++i)
Daniel Dunbar46f45b92008-09-09 01:06:48 +000085 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000086
Chris Lattner8fdf3282008-06-24 17:04:18 +000087 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000088 // super is only valid in an Objective-C method
89 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000090 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
91 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000092 OMD->getClassInterface(),
93 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000094 isClassMessage,
95 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000096 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000097 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
98 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +000099}
100
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000101/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
102/// the LLVM function and sets the other context used by
103/// CodeGenFunction.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000104void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000105 FunctionArgList Args;
106 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000107
Daniel Dunbar7c086512008-09-09 23:14:03 +0000108 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000109
Daniel Dunbar7c086512008-09-09 23:14:03 +0000110 Args.push_back(std::make_pair(OMD->getSelfDecl(),
111 OMD->getSelfDecl()->getType()));
112 Args.push_back(std::make_pair(OMD->getCmdDecl(),
113 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000114
Daniel Dunbar7c086512008-09-09 23:14:03 +0000115 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
116 ParmVarDecl *IPD = OMD->getParamDecl(i);
117 Args.push_back(std::make_pair(IPD, IPD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000118 }
Chris Lattner41110242008-06-17 18:05:57 +0000119
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000120 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000121}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000122
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000123/// Generate an Objective-C method. An Objective-C method is a C function with
124/// its pointer, name, and types registered in the class struture.
125void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
126 StartObjCMethod(OMD);
127 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000128 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000129}
130
131// FIXME: I wasn't sure about the synthesis approach. If we end up
132// generating an AST for the whole body we can just fall back to
133// having a GenerateFunction which takes the body Stmt.
134
135/// GenerateObjCGetter - Generate an Objective-C property getter
136/// function. The given Decl must be either an ObjCCategoryImplDecl
137/// or an ObjCImplementationDecl.
138void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000139 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000140 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
141 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
142 assert(OMD && "Invalid call to generate getter (empty method)");
143 // FIXME: This is rather murky, we create this here since they will
144 // not have been created by Sema for us.
145 OMD->createImplicitParams(getContext());
146 StartObjCMethod(OMD);
147
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000148 // Determine if we should use an objc_getProperty call for
149 // this. Non-atomic and properties with assign semantics are
150 // directly evaluated, and in gc-only mode we don't need it at all.
151 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
152 PD->getSetterKind() != ObjCPropertyDecl::Assign &&
153 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
154 llvm::Value *GetPropertyFn =
155 CGM.getObjCRuntime().GetPropertyGetFunction();
156
157 if (!GetPropertyFn) {
158 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
159 FinishFunction();
160 return;
161 }
162
163 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
164 // FIXME: Can't this be simpler? This might even be worse than the
165 // corresponding gcc code.
166 CodeGenTypes &Types = CGM.getTypes();
167 ValueDecl *Cmd = OMD->getCmdDecl();
168 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
169 QualType IdTy = getContext().getObjCIdType();
170 llvm::Value *SelfAsId =
171 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
172 llvm::Value *Offset = EmitIvarOffset(OMD->getClassInterface(), Ivar);
173 llvm::Value *True =
174 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 1);
175 CallArgList Args;
176 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
177 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
178 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
179 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
180 RValue RV = EmitCall(GetPropertyFn, PD->getType(), Args);
181 // We need to fix the type here. Ivars with copy & retain are
182 // always objects so we don't need to worry about complex or
183 // aggregates.
184 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
185 Types.ConvertType(PD->getType())));
186 EmitReturnOfRValue(RV, PD->getType());
187 } else {
188 EmitReturnOfRValue(EmitLoadOfLValue(EmitLValueForIvar(LoadObjCSelf(),
189 Ivar, 0),
190 Ivar->getType()),
191 PD->getType());
192 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000193
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) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000201 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000202 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
203 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
204 assert(OMD && "Invalid call to generate setter (empty method)");
205 // FIXME: This is rather murky, we create this here since they will
206 // not have been created by Sema for us.
207 OMD->createImplicitParams(getContext());
208 StartObjCMethod(OMD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000209
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000210 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
211 bool IsAtomic =
212 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
213
214 // Determine if we should use an objc_setProperty call for
215 // this. Properties with 'copy' semantics always use it, as do
216 // non-atomic properties with 'release' semantics as long as we are
217 // not in gc-only mode.
218 if (IsCopy ||
219 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
220 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
221 llvm::Value *SetPropertyFn =
222 CGM.getObjCRuntime().GetPropertySetFunction();
223
224 if (!SetPropertyFn) {
225 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
226 FinishFunction();
227 return;
228 }
229
230 // Emit objc_setProperty((id) self, _cmd, offset, arg,
231 // <is-atomic>, <is-copy>).
232 // FIXME: Can't this be simpler? This might even be worse than the
233 // corresponding gcc code.
234 CodeGenTypes &Types = CGM.getTypes();
235 ValueDecl *Cmd = OMD->getCmdDecl();
236 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
237 QualType IdTy = getContext().getObjCIdType();
238 llvm::Value *SelfAsId =
239 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
240 llvm::Value *Offset = EmitIvarOffset(OMD->getClassInterface(), Ivar);
241 llvm::Value *Arg = LocalDeclMap[OMD->getParamDecl(0)];
242 llvm::Value *ArgAsId =
243 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
244 Types.ConvertType(IdTy));
245 llvm::Value *True =
246 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 1);
247 llvm::Value *False =
248 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 0);
249 CallArgList Args;
250 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
251 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
252 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
253 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
254 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
255 getContext().BoolTy));
256 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
257 getContext().BoolTy));
258 EmitCall(SetPropertyFn, PD->getType(), Args);
259 } else {
260 SourceLocation Loc = PD->getLocation();
261 ValueDecl *Self = OMD->getSelfDecl();
262 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
263 DeclRefExpr Base(Self, Self->getType(), Loc);
264 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
265 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
266 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
267 true, true);
268 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
269 Ivar->getType(), Loc);
270 EmitStmt(&Assign);
271 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000272
273 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000274}
275
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000276llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000277 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
278 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000279}
280
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000281RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000282 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000283 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
284 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000285 return CGM.getObjCRuntime().
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000286 GenerateMessageSend(*this, Exp->getType(), S,
287 EmitScalarExpr(E->getBase()),
288 false, CallArgList());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000289 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000290 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
291 Selector S = E->getGetterMethod()->getSelector();
292 return CGM.getObjCRuntime().
293 GenerateMessageSend(*this, Exp->getType(), S,
294 EmitScalarExpr(E->getBase()),
295 false, CallArgList());
296 }
297 else
298 assert (0 && "bad expression node in EmitObjCPropertyGet");
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000299}
300
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000301void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000302 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000303 // FIXME: Split it into two separate routines.
304 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
305 Selector S = E->getProperty()->getSetterName();
306 CallArgList Args;
307 Args.push_back(std::make_pair(Src, E->getType()));
308 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
309 EmitScalarExpr(E->getBase()),
310 false, Args);
311 }
312 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
313 Selector S = E->getSetterMethod()->getSelector();
314 CallArgList Args;
315 Args.push_back(std::make_pair(Src, E->getType()));
316 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
317 EmitScalarExpr(E->getBase()),
318 false, Args);
319 }
320 else
321 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000322}
323
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000324void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
325{
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000326 llvm::Function *EnumerationMutationFn =
327 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000328 llvm::Value *DeclAddress;
329 QualType ElementTy;
330
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000331 if (!EnumerationMutationFn) {
332 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
333 return;
334 }
335
Anders Carlssonf484c312008-08-31 02:33:12 +0000336 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
337 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000338 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Ted Kremenek39741ce2008-10-06 20:59:48 +0000339 const ScopedDecl* D = SD->getSolitaryDecl();
340 ElementTy = cast<ValueDecl>(D)->getType();
341 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000342 } else {
343 ElementTy = cast<Expr>(S.getElement())->getType();
344 DeclAddress = 0;
345 }
346
347 // Fast enumeration state.
348 QualType StateTy = getContext().getObjCFastEnumerationStateType();
349 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
350 "state.ptr");
351 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000352 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000353
354 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000355 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000356
357 // Get selector
358 llvm::SmallVector<IdentifierInfo*, 3> II;
359 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
360 II.push_back(&CGM.getContext().Idents.get("objects"));
361 II.push_back(&CGM.getContext().Idents.get("count"));
362 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
363 &II[0]);
364
365 QualType ItemsTy =
366 getContext().getConstantArrayType(getContext().getObjCIdType(),
367 llvm::APInt(32, NumItems),
368 ArrayType::Normal, 0);
369 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
370
371 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
372
373 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000374 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000375 getContext().getPointerType(StateTy)));
376
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000377 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000378 getContext().getPointerType(ItemsTy)));
379
380 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
381 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000382 Args.push_back(std::make_pair(RValue::get(Count),
383 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000384
385 RValue CountRV =
386 CGM.getObjCRuntime().GenerateMessageSend(*this,
387 getContext().UnsignedLongTy,
388 FastEnumSel,
389 Collection, false, Args);
390
391 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
392 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
393
Daniel Dunbar55e87422008-11-11 02:29:29 +0000394 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
395 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000396
397 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
398 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
399
400 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000401 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000402
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000403 EmitBlock(SetStartMutations);
404
405 llvm::Value *StartMutationsPtr =
406 CreateTempAlloca(UnsignedLongLTy);
407
408 llvm::Value *StateMutationsPtrPtr =
409 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
410 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
411 "mutationsptr");
412
413 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
414 "mutations");
415
416 Builder.CreateStore(StateMutations, StartMutationsPtr);
417
Daniel Dunbar55e87422008-11-11 02:29:29 +0000418 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000419 EmitBlock(LoopStart);
420
Anders Carlssonf484c312008-08-31 02:33:12 +0000421 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
422 Builder.CreateStore(Zero, CounterPtr);
423
Daniel Dunbar55e87422008-11-11 02:29:29 +0000424 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000425 EmitBlock(LoopBody);
426
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000427 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
428 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
429
430 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
431 "mutations");
432 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
433 StartMutations,
434 "tobool");
435
436
Daniel Dunbar55e87422008-11-11 02:29:29 +0000437 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
438 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000439
440 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
441
442 EmitBlock(WasMutated);
443 llvm::Value *V =
444 Builder.CreateBitCast(Collection,
445 ConvertType(getContext().getObjCIdType()),
446 "tmp");
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000447 Builder.CreateCall(EnumerationMutationFn, V);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000448
449 EmitBlock(WasNotMutated);
450
Anders Carlssonf484c312008-08-31 02:33:12 +0000451 llvm::Value *StateItemsPtr =
452 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
453
454 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
455
456 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
457 "stateitems");
458
459 llvm::Value *CurrentItemPtr =
460 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
461
462 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
463
464 // Cast the item to the right type.
465 CurrentItem = Builder.CreateBitCast(CurrentItem,
466 ConvertType(ElementTy), "tmp");
467
468 if (!DeclAddress) {
469 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
470
471 // Set the value to null.
472 Builder.CreateStore(CurrentItem, LV.getAddress());
473 } else
474 Builder.CreateStore(CurrentItem, DeclAddress);
475
476 // Increment the counter.
477 Counter = Builder.CreateAdd(Counter,
478 llvm::ConstantInt::get(UnsignedLongLTy, 1));
479 Builder.CreateStore(Counter, CounterPtr);
480
Daniel Dunbar55e87422008-11-11 02:29:29 +0000481 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
482 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000483
484 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
485
486 EmitStmt(S.getBody());
487
488 BreakContinueStack.pop_back();
489
490 EmitBlock(AfterBody);
491
Daniel Dunbar55e87422008-11-11 02:29:29 +0000492 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Anders Carlssonf484c312008-08-31 02:33:12 +0000493
494 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000495 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000496
497 // Fetch more elements.
498 EmitBlock(FetchMore);
499
500 CountRV =
501 CGM.getObjCRuntime().GenerateMessageSend(*this,
502 getContext().UnsignedLongTy,
503 FastEnumSel,
504 Collection, false, Args);
505 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
506 Limit = Builder.CreateLoad(LimitPtr);
507
508 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
509 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
510
511 // No more elements.
512 EmitBlock(NoElements);
513
514 if (!DeclAddress) {
515 // If the element was not a declaration, set it to be null.
516
517 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
518
519 // Set the value to null.
520 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
521 LV.getAddress());
522 }
523
524 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000525}
526
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000527void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
528{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000529 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000530}
531
532void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
533{
534 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
535}
536
Chris Lattner10cac6f2008-11-15 21:26:17 +0000537void CodeGenFunction::EmitObjCAtSynchronizedStmt(
538 const ObjCAtSynchronizedStmt &S)
539{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000540 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000541}
542
Ted Kremenek2979ec72008-04-09 15:51:31 +0000543CGObjCRuntime::~CGObjCRuntime() {}