blob: a285d7533a99bdbd553b3e8e26c9a291ae522701 [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.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000106void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
107 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000108 FunctionArgList Args;
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000109 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000110
Daniel Dunbar7c086512008-09-09 23:14:03 +0000111 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000112
Daniel Dunbar7c086512008-09-09 23:14:03 +0000113 Args.push_back(std::make_pair(OMD->getSelfDecl(),
114 OMD->getSelfDecl()->getType()));
115 Args.push_back(std::make_pair(OMD->getCmdDecl(),
116 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000117
Daniel Dunbar7c086512008-09-09 23:14:03 +0000118 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
119 ParmVarDecl *IPD = OMD->getParamDecl(i);
120 Args.push_back(std::make_pair(IPD, IPD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000121 }
Chris Lattner41110242008-06-17 18:05:57 +0000122
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000123 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000124}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000125
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000126/// Generate an Objective-C method. An Objective-C method is a C function with
127/// its pointer, name, and types registered in the class struture.
128void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000129 StartObjCMethod(OMD, OMD->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000130 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000131 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000132}
133
134// FIXME: I wasn't sure about the synthesis approach. If we end up
135// generating an AST for the whole body we can just fall back to
136// having a GenerateFunction which takes the body Stmt.
137
138/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000139/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
140/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000141void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
142 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000143 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000144 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
145 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
146 assert(OMD && "Invalid call to generate getter (empty method)");
147 // FIXME: This is rather murky, we create this here since they will
148 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000149 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000150 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000151
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000152 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000153 // this. Non-atomic properties are directly evaluated.
154 // atomic 'copy' and 'retain' properties are also directly
155 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000156 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000157 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
158 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
159 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000160 llvm::Value *GetPropertyFn =
161 CGM.getObjCRuntime().GetPropertyGetFunction();
162
163 if (!GetPropertyFn) {
164 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
165 FinishFunction();
166 return;
167 }
168
169 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
170 // FIXME: Can't this be simpler? This might even be worse than the
171 // corresponding gcc code.
172 CodeGenTypes &Types = CGM.getTypes();
173 ValueDecl *Cmd = OMD->getCmdDecl();
174 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
175 QualType IdTy = getContext().getObjCIdType();
176 llvm::Value *SelfAsId =
177 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000178 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000179 llvm::Value *True =
180 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 1);
181 CallArgList Args;
182 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
183 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
184 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
185 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
186 RValue RV = EmitCall(GetPropertyFn, PD->getType(), Args);
187 // We need to fix the type here. Ivars with copy & retain are
188 // always objects so we don't need to worry about complex or
189 // aggregates.
190 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
191 Types.ConvertType(PD->getType())));
192 EmitReturnOfRValue(RV, PD->getType());
193 } else {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000194 FieldDecl *Field =
195 IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
196 LValue LV = EmitLValueForIvar(LoadObjCSelf(), Ivar, Field, 0);
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000197 if (hasAggregateLLVMType(Ivar->getType())) {
198 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
199 }
200 else
201 EmitReturnOfRValue(EmitLoadOfLValue(LV, Ivar->getType()),
202 PD->getType());
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000203 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000204
205 FinishFunction();
206}
207
208/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000209/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
210/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000211void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
212 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000213 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000214 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
215 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
216 assert(OMD && "Invalid call to generate setter (empty method)");
217 // FIXME: This is rather murky, we create this here since they will
218 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000219 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000220 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000221
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000222 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
223 bool IsAtomic =
224 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
225
226 // Determine if we should use an objc_setProperty call for
227 // this. Properties with 'copy' semantics always use it, as do
228 // non-atomic properties with 'release' semantics as long as we are
229 // not in gc-only mode.
230 if (IsCopy ||
231 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
232 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
233 llvm::Value *SetPropertyFn =
234 CGM.getObjCRuntime().GetPropertySetFunction();
235
236 if (!SetPropertyFn) {
237 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
238 FinishFunction();
239 return;
240 }
241
242 // Emit objc_setProperty((id) self, _cmd, offset, arg,
243 // <is-atomic>, <is-copy>).
244 // FIXME: Can't this be simpler? This might even be worse than the
245 // corresponding gcc code.
246 CodeGenTypes &Types = CGM.getTypes();
247 ValueDecl *Cmd = OMD->getCmdDecl();
248 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
249 QualType IdTy = getContext().getObjCIdType();
250 llvm::Value *SelfAsId =
251 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000252 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000253 llvm::Value *Arg = LocalDeclMap[OMD->getParamDecl(0)];
254 llvm::Value *ArgAsId =
255 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
256 Types.ConvertType(IdTy));
257 llvm::Value *True =
258 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 1);
259 llvm::Value *False =
260 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 0);
261 CallArgList Args;
262 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
263 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
264 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
265 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
266 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
267 getContext().BoolTy));
268 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
269 getContext().BoolTy));
270 EmitCall(SetPropertyFn, PD->getType(), Args);
271 } else {
272 SourceLocation Loc = PD->getLocation();
273 ValueDecl *Self = OMD->getSelfDecl();
274 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
275 DeclRefExpr Base(Self, Self->getType(), Loc);
276 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
277 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanianaaa63a72008-12-13 22:20:28 +0000278 ObjCInterfaceDecl *OI = IMP->getClassInterface();
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000279 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000280 true, true);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000281 getContext().setFieldDecl(OI, Ivar, &IvarRef);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000282 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
283 Ivar->getType(), Loc);
284 EmitStmt(&Assign);
285 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000286
287 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000288}
289
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000290llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000291 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
292 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000293}
294
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000295RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000296 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000297 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
298 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000299 return CGM.getObjCRuntime().
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000300 GenerateMessageSend(*this, Exp->getType(), S,
301 EmitScalarExpr(E->getBase()),
302 false, CallArgList());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000303 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000304 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
305 Selector S = E->getGetterMethod()->getSelector();
306 return CGM.getObjCRuntime().
307 GenerateMessageSend(*this, Exp->getType(), S,
308 EmitScalarExpr(E->getBase()),
309 false, CallArgList());
310 }
311 else
312 assert (0 && "bad expression node in EmitObjCPropertyGet");
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000313}
314
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000315void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000316 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000317 // FIXME: Split it into two separate routines.
318 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
319 Selector S = E->getProperty()->getSetterName();
320 CallArgList Args;
321 Args.push_back(std::make_pair(Src, E->getType()));
322 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
323 EmitScalarExpr(E->getBase()),
324 false, Args);
325 }
326 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
327 Selector S = E->getSetterMethod()->getSelector();
328 CallArgList Args;
329 Args.push_back(std::make_pair(Src, E->getType()));
330 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
331 EmitScalarExpr(E->getBase()),
332 false, Args);
333 }
334 else
335 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000336}
337
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000338void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
339{
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000340 llvm::Function *EnumerationMutationFn =
341 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000342 llvm::Value *DeclAddress;
343 QualType ElementTy;
344
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000345 if (!EnumerationMutationFn) {
346 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
347 return;
348 }
349
Anders Carlssonf484c312008-08-31 02:33:12 +0000350 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
351 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000352 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Ted Kremenek39741ce2008-10-06 20:59:48 +0000353 const ScopedDecl* D = SD->getSolitaryDecl();
354 ElementTy = cast<ValueDecl>(D)->getType();
355 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000356 } else {
357 ElementTy = cast<Expr>(S.getElement())->getType();
358 DeclAddress = 0;
359 }
360
361 // Fast enumeration state.
362 QualType StateTy = getContext().getObjCFastEnumerationStateType();
363 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
364 "state.ptr");
365 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000366 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000367
368 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000369 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000370
371 // Get selector
372 llvm::SmallVector<IdentifierInfo*, 3> II;
373 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
374 II.push_back(&CGM.getContext().Idents.get("objects"));
375 II.push_back(&CGM.getContext().Idents.get("count"));
376 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
377 &II[0]);
378
379 QualType ItemsTy =
380 getContext().getConstantArrayType(getContext().getObjCIdType(),
381 llvm::APInt(32, NumItems),
382 ArrayType::Normal, 0);
383 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
384
385 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
386
387 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000388 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000389 getContext().getPointerType(StateTy)));
390
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000391 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000392 getContext().getPointerType(ItemsTy)));
393
394 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
395 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000396 Args.push_back(std::make_pair(RValue::get(Count),
397 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000398
399 RValue CountRV =
400 CGM.getObjCRuntime().GenerateMessageSend(*this,
401 getContext().UnsignedLongTy,
402 FastEnumSel,
403 Collection, false, Args);
404
405 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
406 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
407
Daniel Dunbar55e87422008-11-11 02:29:29 +0000408 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
409 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000410
411 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
412 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
413
414 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000415 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000416
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000417 EmitBlock(SetStartMutations);
418
419 llvm::Value *StartMutationsPtr =
420 CreateTempAlloca(UnsignedLongLTy);
421
422 llvm::Value *StateMutationsPtrPtr =
423 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
424 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
425 "mutationsptr");
426
427 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
428 "mutations");
429
430 Builder.CreateStore(StateMutations, StartMutationsPtr);
431
Daniel Dunbar55e87422008-11-11 02:29:29 +0000432 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000433 EmitBlock(LoopStart);
434
Anders Carlssonf484c312008-08-31 02:33:12 +0000435 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
436 Builder.CreateStore(Zero, CounterPtr);
437
Daniel Dunbar55e87422008-11-11 02:29:29 +0000438 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000439 EmitBlock(LoopBody);
440
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000441 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
442 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
443
444 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
445 "mutations");
446 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
447 StartMutations,
448 "tobool");
449
450
Daniel Dunbar55e87422008-11-11 02:29:29 +0000451 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
452 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000453
454 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
455
456 EmitBlock(WasMutated);
457 llvm::Value *V =
458 Builder.CreateBitCast(Collection,
459 ConvertType(getContext().getObjCIdType()),
460 "tmp");
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000461 Builder.CreateCall(EnumerationMutationFn, V);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000462
463 EmitBlock(WasNotMutated);
464
Anders Carlssonf484c312008-08-31 02:33:12 +0000465 llvm::Value *StateItemsPtr =
466 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
467
468 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
469
470 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
471 "stateitems");
472
473 llvm::Value *CurrentItemPtr =
474 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
475
476 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
477
478 // Cast the item to the right type.
479 CurrentItem = Builder.CreateBitCast(CurrentItem,
480 ConvertType(ElementTy), "tmp");
481
482 if (!DeclAddress) {
483 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
484
485 // Set the value to null.
486 Builder.CreateStore(CurrentItem, LV.getAddress());
487 } else
488 Builder.CreateStore(CurrentItem, DeclAddress);
489
490 // Increment the counter.
491 Counter = Builder.CreateAdd(Counter,
492 llvm::ConstantInt::get(UnsignedLongLTy, 1));
493 Builder.CreateStore(Counter, CounterPtr);
494
Daniel Dunbar55e87422008-11-11 02:29:29 +0000495 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
496 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000497
Anders Carlssone21269b2008-12-13 22:52:24 +0000498 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody,
499 ObjCEHStack.size()));
Anders Carlssonf484c312008-08-31 02:33:12 +0000500
501 EmitStmt(S.getBody());
502
503 BreakContinueStack.pop_back();
504
505 EmitBlock(AfterBody);
506
Daniel Dunbar55e87422008-11-11 02:29:29 +0000507 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000508
509 Counter = Builder.CreateLoad(CounterPtr);
510 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000511 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000512 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000513
514 // Fetch more elements.
515 EmitBlock(FetchMore);
516
517 CountRV =
518 CGM.getObjCRuntime().GenerateMessageSend(*this,
519 getContext().UnsignedLongTy,
520 FastEnumSel,
521 Collection, false, Args);
522 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
523 Limit = Builder.CreateLoad(LimitPtr);
524
525 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
526 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
527
528 // No more elements.
529 EmitBlock(NoElements);
530
531 if (!DeclAddress) {
532 // If the element was not a declaration, set it to be null.
533
534 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
535
536 // Set the value to null.
537 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
538 LV.getAddress());
539 }
540
541 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000542}
543
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000544void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
545{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000546 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000547}
548
549void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
550{
551 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
552}
553
Chris Lattner10cac6f2008-11-15 21:26:17 +0000554void CodeGenFunction::EmitObjCAtSynchronizedStmt(
555 const ObjCAtSynchronizedStmt &S)
556{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000557 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000558}
559
Ted Kremenek2979ec72008-04-09 15:51:31 +0000560CGObjCRuntime::~CGObjCRuntime() {}