blob: dd0292ea4db0330f76f3676853865371d0528fa2 [file] [log] [blame]
Anders Carlssona66cad42007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Carlssona66cad42007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekfa4ebab2008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlssona66cad42007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbare6c31752008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Daniel Dunbarcc37ac52008-09-03 00:27:26 +000019#include "clang/Basic/Diagnostic.h"
Anders Carlsson82b0d0c2008-08-30 19:51:14 +000020#include "llvm/ADT/STLExtras.h"
Daniel Dunbard82223f2008-09-24 04:04:31 +000021#include "llvm/Target/TargetData.h"
Chris Lattner8c7c6a12008-06-17 18:05:57 +000022
Anders Carlssona66cad42007-08-21 17:43:55 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattner6ee20e32008-06-24 17:04:18 +000026/// Emits an instance of NSConstantString representing the object.
Daniel Dunbard76c2ff2008-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 Dunbardaf4ad42008-08-12 00:12:39 +000031 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000032 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000033 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner6ee20e32008-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 Dunbarfc69bde2008-08-11 18:12:00 +000042 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner6ee20e32008-06-24 17:04:18 +000043}
44
Daniel Dunbarf1f7f192008-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 Lattner6ee20e32008-06-24 17:04:18 +000049
50
Daniel Dunbara04840b2008-08-23 03:46:30 +000051RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner6ee20e32008-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 Dunbarfc69bde2008-08-11 18:12:00 +000056 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner6ee20e32008-06-24 17:04:18 +000057 const Expr *ReceiverExpr = E->getReceiver();
58 bool isSuperMessage = false;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000059 bool isClassMessage = false;
Chris Lattner6ee20e32008-06-24 17:04:18 +000060 // Find the receiver
61 llvm::Value *Receiver;
62 if (!ReceiverExpr) {
Daniel Dunbar434627a2008-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 Lattner05fb7c82008-11-20 04:42:34 +000068 assert(E->getClassName()->isStr("super") &&
Daniel Dunbar434627a2008-08-16 00:25:02 +000069 "Unexpected missing class interface in message send.");
Daniel Dunbar434627a2008-08-16 00:25:02 +000070 isSuperMessage = true;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000071 Receiver = LoadObjCSelf();
72 } else {
73 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner6ee20e32008-06-24 17:04:18 +000074 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000075
76 isClassMessage = true;
Douglas Gregord8606632008-11-04 14:56:14 +000077 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner6ee20e32008-06-24 17:04:18 +000078 isSuperMessage = true;
79 Receiver = LoadObjCSelf();
80 } else {
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000081 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner6ee20e32008-06-24 17:04:18 +000082 }
83
Daniel Dunbar0ed60b02008-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 Dunbar0a2da0f2008-09-09 01:06:48 +000087 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000088
Chris Lattner6ee20e32008-06-24 17:04:18 +000089 if (isSuperMessage) {
Chris Lattner8384c142008-06-26 04:42:20 +000090 // super is only valid in an Objective-C method
91 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbardd851282008-08-30 05:35:15 +000092 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
93 E->getSelector(),
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000094 OMD->getClassInterface(),
95 Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000096 isClassMessage,
97 Args);
Chris Lattner6ee20e32008-06-24 17:04:18 +000098 }
Daniel Dunbardd851282008-08-30 05:35:15 +000099 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
100 Receiver, isClassMessage, Args);
Anders Carlssona66cad42007-08-21 17:43:55 +0000101}
102
Daniel Dunbar6b57d432008-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 Jahanian0adaa8a2009-01-10 21:06:09 +0000106void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
107 const ObjCContainerDecl *CD) {
Daniel Dunbar96816832008-09-09 23:14:03 +0000108 FunctionArgList Args;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000109 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000110
Daniel Dunbar96816832008-09-09 23:14:03 +0000111 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000112
Daniel Dunbar96816832008-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 Lattner8c7c6a12008-06-17 18:05:57 +0000117
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000118 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
119 E = OMD->param_end(); PI != E; ++PI)
120 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000121
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000122 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000123}
Daniel Dunbarace33292008-08-16 03:19:19 +0000124
Daniel Dunbar6b57d432008-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) {
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000128 StartObjCMethod(OMD, OMD->getClassInterface());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000129 EmitStmt(OMD->getBody());
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000130 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbar6b57d432008-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
Steve Naroff9336dbd2009-01-10 22:55:25 +0000138/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
139/// is illegal within a category.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000140void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
141 const ObjCPropertyImplDecl *PID) {
Daniel Dunbard82223f2008-09-24 04:04:31 +0000142 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000143 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
144 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
145 assert(OMD && "Invalid call to generate getter (empty method)");
146 // FIXME: This is rather murky, we create this here since they will
147 // not have been created by Sema for us.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000148 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000149 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000150
Daniel Dunbard82223f2008-09-24 04:04:31 +0000151 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian32849782008-12-08 23:56:17 +0000152 // this. Non-atomic properties are directly evaluated.
153 // atomic 'copy' and 'retain' properties are also directly
154 // evaluated in gc-only mode.
Daniel Dunbard82223f2008-09-24 04:04:31 +0000155 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian32849782008-12-08 23:56:17 +0000156 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
157 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
158 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbard82223f2008-09-24 04:04:31 +0000159 llvm::Value *GetPropertyFn =
160 CGM.getObjCRuntime().GetPropertyGetFunction();
161
162 if (!GetPropertyFn) {
163 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
164 FinishFunction();
165 return;
166 }
167
168 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
169 // FIXME: Can't this be simpler? This might even be worse than the
170 // corresponding gcc code.
171 CodeGenTypes &Types = CGM.getTypes();
172 ValueDecl *Cmd = OMD->getCmdDecl();
173 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
174 QualType IdTy = getContext().getObjCIdType();
175 llvm::Value *SelfAsId =
176 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000177 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbard82223f2008-09-24 04:04:31 +0000178 llvm::Value *True =
Daniel Dunbard83141af2009-02-04 00:55:44 +0000179 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbard82223f2008-09-24 04:04:31 +0000180 CallArgList Args;
181 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
182 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
183 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
184 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbar93ba3732009-02-03 23:43:59 +0000185 // FIXME: We shouldn't need to get the function info here, the
186 // runtime already should have computed it to build the function.
Daniel Dunbar34bda882009-02-02 23:23:47 +0000187 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000188 GetPropertyFn, Args);
Daniel Dunbard82223f2008-09-24 04:04:31 +0000189 // We need to fix the type here. Ivars with copy & retain are
190 // always objects so we don't need to worry about complex or
191 // aggregates.
192 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
193 Types.ConvertType(PD->getType())));
194 EmitReturnOfRValue(RV, PD->getType());
195 } else {
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000196 FieldDecl *Field =
197 IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
Fariborz Jahanian55343922009-02-03 00:09:52 +0000198 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
199 LoadObjCSelf(), Ivar, Field, 0);
Fariborz Jahaniand70b09c2008-11-26 22:36:09 +0000200 if (hasAggregateLLVMType(Ivar->getType())) {
201 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
202 }
203 else
204 EmitReturnOfRValue(EmitLoadOfLValue(LV, Ivar->getType()),
205 PD->getType());
Daniel Dunbard82223f2008-09-24 04:04:31 +0000206 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000207
208 FinishFunction();
209}
210
211/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff9336dbd2009-01-10 22:55:25 +0000212/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
213/// is illegal within a category.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000214void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
215 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000216 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000217 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
218 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
219 assert(OMD && "Invalid call to generate setter (empty method)");
220 // FIXME: This is rather murky, we create this here since they will
221 // not have been created by Sema for us.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000222 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000223 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000224
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000225 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
226 bool IsAtomic =
227 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
228
229 // Determine if we should use an objc_setProperty call for
230 // this. Properties with 'copy' semantics always use it, as do
231 // non-atomic properties with 'release' semantics as long as we are
232 // not in gc-only mode.
233 if (IsCopy ||
234 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
235 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
236 llvm::Value *SetPropertyFn =
237 CGM.getObjCRuntime().GetPropertySetFunction();
238
239 if (!SetPropertyFn) {
240 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
241 FinishFunction();
242 return;
243 }
244
245 // Emit objc_setProperty((id) self, _cmd, offset, arg,
246 // <is-atomic>, <is-copy>).
247 // FIXME: Can't this be simpler? This might even be worse than the
248 // corresponding gcc code.
249 CodeGenTypes &Types = CGM.getTypes();
250 ValueDecl *Cmd = OMD->getCmdDecl();
251 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
252 QualType IdTy = getContext().getObjCIdType();
253 llvm::Value *SelfAsId =
254 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000255 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000256 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000257 llvm::Value *ArgAsId =
258 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
259 Types.ConvertType(IdTy));
260 llvm::Value *True =
Daniel Dunbard83141af2009-02-04 00:55:44 +0000261 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000262 llvm::Value *False =
Daniel Dunbard83141af2009-02-04 00:55:44 +0000263 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000264 CallArgList Args;
265 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
266 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
267 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
268 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
269 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
270 getContext().BoolTy));
271 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
272 getContext().BoolTy));
Daniel Dunbar93ba3732009-02-03 23:43:59 +0000273 // FIXME: We shouldn't need to get the function info here, the
274 // runtime already should have computed it to build the function.
275 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar34bda882009-02-02 23:23:47 +0000276 SetPropertyFn, Args);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000277 } else {
278 SourceLocation Loc = PD->getLocation();
279 ValueDecl *Self = OMD->getSelfDecl();
280 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
281 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000282 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000283 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000284 ObjCInterfaceDecl *OI = IMP->getClassInterface();
Fariborz Jahanianea944842008-12-18 17:29:46 +0000285 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000286 true, true);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000287 getContext().setFieldDecl(OI, Ivar, &IvarRef);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000288 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
289 Ivar->getType(), Loc);
290 EmitStmt(&Assign);
291 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000292
293 FinishFunction();
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000294}
295
Daniel Dunbard82223f2008-09-24 04:04:31 +0000296llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarace33292008-08-16 03:19:19 +0000297 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
298 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000299}
300
Fariborz Jahanian55343922009-02-03 00:09:52 +0000301QualType CodeGenFunction::TypeOfSelfObject() {
302 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
303 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
304 const PointerType *PTy =
305 cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
306 return PTy->getPointeeType();
307}
308
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000309RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000310 // FIXME: Split it into two separate routines.
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000311 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
312 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000313 return CGM.getObjCRuntime().
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000314 GenerateMessageSend(*this, Exp->getType(), S,
315 EmitScalarExpr(E->getBase()),
316 false, CallArgList());
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000317 }
Daniel Dunbarddebeca2009-01-15 18:32:35 +0000318 else {
Daniel Dunbarb5b06b22009-01-16 01:50:29 +0000319 const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp);
320 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000321 return CGM.getObjCRuntime().
322 GenerateMessageSend(*this, Exp->getType(), S,
Daniel Dunbarb5b06b22009-01-16 01:50:29 +0000323 EmitScalarExpr(KE->getBase()),
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000324 false, CallArgList());
325 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000326}
327
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000328void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbare6c31752008-08-29 08:11:39 +0000329 RValue Src) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000330 // FIXME: Split it into two separate routines.
331 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
332 Selector S = E->getProperty()->getSetterName();
333 CallArgList Args;
334 Args.push_back(std::make_pair(Src, E->getType()));
335 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
336 EmitScalarExpr(E->getBase()),
337 false, Args);
338 }
339 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
340 Selector S = E->getSetterMethod()->getSelector();
341 CallArgList Args;
342 Args.push_back(std::make_pair(Src, E->getType()));
343 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
344 EmitScalarExpr(E->getBase()),
345 false, Args);
346 }
347 else
348 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbare6c31752008-08-29 08:11:39 +0000349}
350
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000351void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
352{
Daniel Dunbard82223f2008-09-24 04:04:31 +0000353 llvm::Function *EnumerationMutationFn =
354 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000355 llvm::Value *DeclAddress;
356 QualType ElementTy;
357
Daniel Dunbard82223f2008-09-24 04:04:31 +0000358 if (!EnumerationMutationFn) {
359 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
360 return;
361 }
362
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000363 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
364 EmitStmt(SD);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +0000365 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000366 const Decl* D = SD->getSolitaryDecl();
Ted Kremenek2ac10d02008-10-06 20:59:48 +0000367 ElementTy = cast<ValueDecl>(D)->getType();
368 DeclAddress = LocalDeclMap[D];
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000369 } else {
370 ElementTy = cast<Expr>(S.getElement())->getType();
371 DeclAddress = 0;
372 }
373
374 // Fast enumeration state.
375 QualType StateTy = getContext().getObjCFastEnumerationStateType();
376 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
377 "state.ptr");
378 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson58d16242008-08-31 04:05:03 +0000379 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000380
381 // Number of elements in the items array.
Anders Carlsson58d16242008-08-31 04:05:03 +0000382 static const unsigned NumItems = 16;
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000383
384 // Get selector
385 llvm::SmallVector<IdentifierInfo*, 3> II;
386 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
387 II.push_back(&CGM.getContext().Idents.get("objects"));
388 II.push_back(&CGM.getContext().Idents.get("count"));
389 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
390 &II[0]);
391
392 QualType ItemsTy =
393 getContext().getConstantArrayType(getContext().getObjCIdType(),
394 llvm::APInt(32, NumItems),
395 ArrayType::Normal, 0);
396 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
397
398 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
399
400 CallArgList Args;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000401 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000402 getContext().getPointerType(StateTy)));
403
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000404 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000405 getContext().getPointerType(ItemsTy)));
406
407 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
408 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000409 Args.push_back(std::make_pair(RValue::get(Count),
410 getContext().UnsignedLongTy));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000411
412 RValue CountRV =
413 CGM.getObjCRuntime().GenerateMessageSend(*this,
414 getContext().UnsignedLongTy,
415 FastEnumSel,
416 Collection, false, Args);
417
418 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
419 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
420
Daniel Dunbar72f96552008-11-11 02:29:29 +0000421 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
422 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000423
424 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
425 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
426
427 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson58d16242008-08-31 04:05:03 +0000428 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000429
Anders Carlsson58d16242008-08-31 04:05:03 +0000430 EmitBlock(SetStartMutations);
431
432 llvm::Value *StartMutationsPtr =
433 CreateTempAlloca(UnsignedLongLTy);
434
435 llvm::Value *StateMutationsPtrPtr =
436 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
437 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
438 "mutationsptr");
439
440 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
441 "mutations");
442
443 Builder.CreateStore(StateMutations, StartMutationsPtr);
444
Daniel Dunbar72f96552008-11-11 02:29:29 +0000445 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000446 EmitBlock(LoopStart);
447
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000448 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
449 Builder.CreateStore(Zero, CounterPtr);
450
Daniel Dunbar72f96552008-11-11 02:29:29 +0000451 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000452 EmitBlock(LoopBody);
453
Anders Carlsson58d16242008-08-31 04:05:03 +0000454 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
455 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
456
457 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
458 "mutations");
459 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
460 StartMutations,
461 "tobool");
462
463
Daniel Dunbar72f96552008-11-11 02:29:29 +0000464 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
465 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson58d16242008-08-31 04:05:03 +0000466
467 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
468
469 EmitBlock(WasMutated);
470 llvm::Value *V =
471 Builder.CreateBitCast(Collection,
472 ConvertType(getContext().getObjCIdType()),
473 "tmp");
Daniel Dunbar903041b2009-02-03 23:55:40 +0000474 CallArgList Args2;
475 Args2.push_back(std::make_pair(RValue::get(V),
476 getContext().getObjCIdType()));
477 // FIXME: We shouldn't need to get the function info here, the
478 // runtime already should have computed it to build the function.
Daniel Dunbar87f3edc2009-02-04 22:00:33 +0000479 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar903041b2009-02-03 23:55:40 +0000480 EnumerationMutationFn, Args2);
Anders Carlsson58d16242008-08-31 04:05:03 +0000481
482 EmitBlock(WasNotMutated);
483
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000484 llvm::Value *StateItemsPtr =
485 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
486
487 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
488
489 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
490 "stateitems");
491
492 llvm::Value *CurrentItemPtr =
493 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
494
495 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
496
497 // Cast the item to the right type.
498 CurrentItem = Builder.CreateBitCast(CurrentItem,
499 ConvertType(ElementTy), "tmp");
500
501 if (!DeclAddress) {
502 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
503
504 // Set the value to null.
505 Builder.CreateStore(CurrentItem, LV.getAddress());
506 } else
507 Builder.CreateStore(CurrentItem, DeclAddress);
508
509 // Increment the counter.
510 Counter = Builder.CreateAdd(Counter,
511 llvm::ConstantInt::get(UnsignedLongLTy, 1));
512 Builder.CreateStore(Counter, CounterPtr);
513
Daniel Dunbar72f96552008-11-11 02:29:29 +0000514 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
515 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000516
Anders Carlsson7c314902009-02-10 05:52:02 +0000517 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000518
519 EmitStmt(S.getBody());
520
521 BreakContinueStack.pop_back();
522
523 EmitBlock(AfterBody);
524
Daniel Dunbar72f96552008-11-11 02:29:29 +0000525 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanian6296cda2009-01-06 18:56:31 +0000526
527 Counter = Builder.CreateLoad(CounterPtr);
528 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000529 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar35bd6192008-09-04 21:54:37 +0000530 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000531
532 // Fetch more elements.
533 EmitBlock(FetchMore);
534
535 CountRV =
536 CGM.getObjCRuntime().GenerateMessageSend(*this,
537 getContext().UnsignedLongTy,
538 FastEnumSel,
539 Collection, false, Args);
540 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
541 Limit = Builder.CreateLoad(LimitPtr);
542
543 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
544 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
545
546 // No more elements.
547 EmitBlock(NoElements);
548
549 if (!DeclAddress) {
550 // If the element was not a declaration, set it to be null.
551
552 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
553
554 // Set the value to null.
555 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
556 LV.getAddress());
557 }
558
559 EmitBlock(LoopEnd);
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000560}
561
Anders Carlssonb01a2112008-09-09 10:04:29 +0000562void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
563{
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000564 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000565}
566
567void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
568{
569 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
570}
571
Chris Lattnerdd978702008-11-15 21:26:17 +0000572void CodeGenFunction::EmitObjCAtSynchronizedStmt(
573 const ObjCAtSynchronizedStmt &S)
574{
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000575 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattnerdd978702008-11-15 21:26:17 +0000576}
577
Ted Kremenekfa4ebab2008-04-09 15:51:31 +0000578CGObjCRuntime::~CGObjCRuntime() {}