blob: c2e42d598176284298528c456544984590d9d8b5 [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
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000281RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
282 // Determine getter selector.
283 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000284 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
285 S = E->getGetterMethod()->getSelector();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000286 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000287 S = E->getProperty()->getGetterName();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000288 }
289
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000290 return CGM.getObjCRuntime().
291 GenerateMessageSend(*this, E->getType(), S,
292 EmitScalarExpr(E->getBase()),
293 false, CallArgList());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000294}
295
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000296void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
297 RValue Src) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000298 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000299 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
300 ObjCMethodDecl *Setter = E->getSetterMethod();
301
302 if (Setter) {
303 S = Setter->getSelector();
304 } else {
305 // FIXME: This should be diagnosed by sema.
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000306 CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()),
Chris Lattner0a14eee2008-11-18 07:04:44 +0000307 diag::err_typecheck_assign_const)
308 << E->getSourceRange();
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000309 return;
310 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000311 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000312 S = E->getProperty()->getSetterName();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000313 }
314
315 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000316 Args.push_back(std::make_pair(Src, E->getType()));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000317 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
318 EmitScalarExpr(E->getBase()),
319 false, Args);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000320}
321
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000322void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
323{
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000324 llvm::Function *EnumerationMutationFn =
325 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000326 llvm::Value *DeclAddress;
327 QualType ElementTy;
328
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000329 if (!EnumerationMutationFn) {
330 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
331 return;
332 }
333
Anders Carlssonf484c312008-08-31 02:33:12 +0000334 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
335 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000336 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Ted Kremenek39741ce2008-10-06 20:59:48 +0000337 const ScopedDecl* D = SD->getSolitaryDecl();
338 ElementTy = cast<ValueDecl>(D)->getType();
339 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000340 } else {
341 ElementTy = cast<Expr>(S.getElement())->getType();
342 DeclAddress = 0;
343 }
344
345 // Fast enumeration state.
346 QualType StateTy = getContext().getObjCFastEnumerationStateType();
347 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
348 "state.ptr");
349 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000350 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000351
352 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000353 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000354
355 // Get selector
356 llvm::SmallVector<IdentifierInfo*, 3> II;
357 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
358 II.push_back(&CGM.getContext().Idents.get("objects"));
359 II.push_back(&CGM.getContext().Idents.get("count"));
360 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
361 &II[0]);
362
363 QualType ItemsTy =
364 getContext().getConstantArrayType(getContext().getObjCIdType(),
365 llvm::APInt(32, NumItems),
366 ArrayType::Normal, 0);
367 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
368
369 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
370
371 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000372 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000373 getContext().getPointerType(StateTy)));
374
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000375 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000376 getContext().getPointerType(ItemsTy)));
377
378 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
379 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000380 Args.push_back(std::make_pair(RValue::get(Count),
381 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000382
383 RValue CountRV =
384 CGM.getObjCRuntime().GenerateMessageSend(*this,
385 getContext().UnsignedLongTy,
386 FastEnumSel,
387 Collection, false, Args);
388
389 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
390 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
391
Daniel Dunbar55e87422008-11-11 02:29:29 +0000392 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
393 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000394
395 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
396 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
397
398 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000399 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000400
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000401 EmitBlock(SetStartMutations);
402
403 llvm::Value *StartMutationsPtr =
404 CreateTempAlloca(UnsignedLongLTy);
405
406 llvm::Value *StateMutationsPtrPtr =
407 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
408 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
409 "mutationsptr");
410
411 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
412 "mutations");
413
414 Builder.CreateStore(StateMutations, StartMutationsPtr);
415
Daniel Dunbar55e87422008-11-11 02:29:29 +0000416 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000417 EmitBlock(LoopStart);
418
Anders Carlssonf484c312008-08-31 02:33:12 +0000419 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
420 Builder.CreateStore(Zero, CounterPtr);
421
Daniel Dunbar55e87422008-11-11 02:29:29 +0000422 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000423 EmitBlock(LoopBody);
424
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000425 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
426 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
427
428 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
429 "mutations");
430 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
431 StartMutations,
432 "tobool");
433
434
Daniel Dunbar55e87422008-11-11 02:29:29 +0000435 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
436 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000437
438 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
439
440 EmitBlock(WasMutated);
441 llvm::Value *V =
442 Builder.CreateBitCast(Collection,
443 ConvertType(getContext().getObjCIdType()),
444 "tmp");
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000445 Builder.CreateCall(EnumerationMutationFn, V);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000446
447 EmitBlock(WasNotMutated);
448
Anders Carlssonf484c312008-08-31 02:33:12 +0000449 llvm::Value *StateItemsPtr =
450 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
451
452 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
453
454 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
455 "stateitems");
456
457 llvm::Value *CurrentItemPtr =
458 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
459
460 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
461
462 // Cast the item to the right type.
463 CurrentItem = Builder.CreateBitCast(CurrentItem,
464 ConvertType(ElementTy), "tmp");
465
466 if (!DeclAddress) {
467 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
468
469 // Set the value to null.
470 Builder.CreateStore(CurrentItem, LV.getAddress());
471 } else
472 Builder.CreateStore(CurrentItem, DeclAddress);
473
474 // Increment the counter.
475 Counter = Builder.CreateAdd(Counter,
476 llvm::ConstantInt::get(UnsignedLongLTy, 1));
477 Builder.CreateStore(Counter, CounterPtr);
478
Daniel Dunbar55e87422008-11-11 02:29:29 +0000479 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
480 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000481
482 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
483
484 EmitStmt(S.getBody());
485
486 BreakContinueStack.pop_back();
487
488 EmitBlock(AfterBody);
489
Daniel Dunbar55e87422008-11-11 02:29:29 +0000490 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Anders Carlssonf484c312008-08-31 02:33:12 +0000491
492 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000493 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000494
495 // Fetch more elements.
496 EmitBlock(FetchMore);
497
498 CountRV =
499 CGM.getObjCRuntime().GenerateMessageSend(*this,
500 getContext().UnsignedLongTy,
501 FastEnumSel,
502 Collection, false, Args);
503 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
504 Limit = Builder.CreateLoad(LimitPtr);
505
506 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
507 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
508
509 // No more elements.
510 EmitBlock(NoElements);
511
512 if (!DeclAddress) {
513 // If the element was not a declaration, set it to be null.
514
515 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
516
517 // Set the value to null.
518 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
519 LV.getAddress());
520 }
521
522 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000523}
524
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000525void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
526{
527 CGM.getObjCRuntime().EmitTryStmt(*this, S);
528}
529
530void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
531{
532 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
533}
534
Chris Lattner10cac6f2008-11-15 21:26:17 +0000535void CodeGenFunction::EmitObjCAtSynchronizedStmt(
536 const ObjCAtSynchronizedStmt &S)
537{
538 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
539}
540
Ted Kremenek2979ec72008-04-09 15:51:31 +0000541CGObjCRuntime::~CGObjCRuntime() {}