blob: ec11edb3492c8c14a789b2bc5a78c041c3ecd383 [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
Chris Lattner89951a82009-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 Lattner41110242008-06-17 18:05:57 +0000121
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000122 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000123}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000124
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000125/// Generate an Objective-C method. An Objective-C method is a C function with
126/// its pointer, name, and types registered in the class struture.
127void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel1d6a4512009-02-25 01:09:46 +0000128 // Check if we should generate debug info for this method.
129 if (CGM.getDebugInfo() && !OMD->getAttr<NodebugAttr>())
130 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000131 StartObjCMethod(OMD, OMD->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000132 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000133 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000134}
135
136// FIXME: I wasn't sure about the synthesis approach. If we end up
137// generating an AST for the whole body we can just fall back to
138// having a GenerateFunction which takes the body Stmt.
139
140/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000141/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
142/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000143void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
144 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000145 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000146 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
147 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
148 assert(OMD && "Invalid call to generate getter (empty method)");
149 // FIXME: This is rather murky, we create this here since they will
150 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000151 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000152 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000153
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000154 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000155 // this. Non-atomic properties are directly evaluated.
156 // atomic 'copy' and 'retain' properties are also directly
157 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000158 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000159 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
160 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
161 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000162 llvm::Value *GetPropertyFn =
163 CGM.getObjCRuntime().GetPropertyGetFunction();
164
165 if (!GetPropertyFn) {
166 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
167 FinishFunction();
168 return;
169 }
170
171 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
172 // FIXME: Can't this be simpler? This might even be worse than the
173 // corresponding gcc code.
174 CodeGenTypes &Types = CGM.getTypes();
175 ValueDecl *Cmd = OMD->getCmdDecl();
176 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
177 QualType IdTy = getContext().getObjCIdType();
178 llvm::Value *SelfAsId =
179 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000180 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000181 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000182 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000183 CallArgList Args;
184 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
185 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
186 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
187 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000188 // FIXME: We shouldn't need to get the function info here, the
189 // runtime already should have computed it to build the function.
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000190 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar88b53962009-02-02 22:03:45 +0000191 GetPropertyFn, Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000192 // We need to fix the type here. Ivars with copy & retain are
193 // always objects so we don't need to worry about complex or
194 // aggregates.
195 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
196 Types.ConvertType(PD->getType())));
197 EmitReturnOfRValue(RV, PD->getType());
198 } else {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000199 FieldDecl *Field =
200 IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000201 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
202 LoadObjCSelf(), Ivar, Field, 0);
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000203 if (hasAggregateLLVMType(Ivar->getType())) {
204 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
205 }
206 else
207 EmitReturnOfRValue(EmitLoadOfLValue(LV, Ivar->getType()),
208 PD->getType());
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000209 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000210
211 FinishFunction();
212}
213
214/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000215/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
216/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000217void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
218 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000219 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000220 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
221 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
222 assert(OMD && "Invalid call to generate setter (empty method)");
223 // FIXME: This is rather murky, we create this here since they will
224 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000225 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000226 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000227
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000228 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
229 bool IsAtomic =
230 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
231
232 // Determine if we should use an objc_setProperty call for
233 // this. Properties with 'copy' semantics always use it, as do
234 // non-atomic properties with 'release' semantics as long as we are
235 // not in gc-only mode.
236 if (IsCopy ||
237 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
238 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
239 llvm::Value *SetPropertyFn =
240 CGM.getObjCRuntime().GetPropertySetFunction();
241
242 if (!SetPropertyFn) {
243 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
244 FinishFunction();
245 return;
246 }
247
248 // Emit objc_setProperty((id) self, _cmd, offset, arg,
249 // <is-atomic>, <is-copy>).
250 // FIXME: Can't this be simpler? This might even be worse than the
251 // corresponding gcc code.
252 CodeGenTypes &Types = CGM.getTypes();
253 ValueDecl *Cmd = OMD->getCmdDecl();
254 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
255 QualType IdTy = getContext().getObjCIdType();
256 llvm::Value *SelfAsId =
257 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000258 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000259 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000260 llvm::Value *ArgAsId =
261 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
262 Types.ConvertType(IdTy));
263 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000264 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000265 llvm::Value *False =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000266 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000267 CallArgList Args;
268 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
269 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
270 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
271 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
272 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
273 getContext().BoolTy));
274 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
275 getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000276 // FIXME: We shouldn't need to get the function info here, the
277 // runtime already should have computed it to build the function.
278 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000279 SetPropertyFn, Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000280 } else {
281 SourceLocation Loc = PD->getLocation();
282 ValueDecl *Self = OMD->getSelfDecl();
283 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
284 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000285 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000286 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanianaaa63a72008-12-13 22:20:28 +0000287 ObjCInterfaceDecl *OI = IMP->getClassInterface();
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000288 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000289 true, true);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000290 getContext().setFieldDecl(OI, Ivar, &IvarRef);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000291 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
292 Ivar->getType(), Loc);
293 EmitStmt(&Assign);
294 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000295
296 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000297}
298
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000299llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000300 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
301 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000302}
303
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000304QualType CodeGenFunction::TypeOfSelfObject() {
305 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
306 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
307 const PointerType *PTy =
308 cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
309 return PTy->getPointeeType();
310}
311
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000312RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000313 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000314 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
315 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000316 return CGM.getObjCRuntime().
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000317 GenerateMessageSend(*this, Exp->getType(), S,
318 EmitScalarExpr(E->getBase()),
319 false, CallArgList());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000320 }
Daniel Dunbarf1853192009-01-15 18:32:35 +0000321 else {
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000322 const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp);
323 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000324 return CGM.getObjCRuntime().
325 GenerateMessageSend(*this, Exp->getType(), S,
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000326 EmitScalarExpr(KE->getBase()),
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000327 false, CallArgList());
328 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000329}
330
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000331void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000332 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000333 // FIXME: Split it into two separate routines.
334 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
335 Selector S = E->getProperty()->getSetterName();
336 CallArgList Args;
337 Args.push_back(std::make_pair(Src, E->getType()));
338 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
339 EmitScalarExpr(E->getBase()),
340 false, Args);
341 }
342 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
343 Selector S = E->getSetterMethod()->getSelector();
344 CallArgList Args;
345 Args.push_back(std::make_pair(Src, E->getType()));
346 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
347 EmitScalarExpr(E->getBase()),
348 false, Args);
349 }
350 else
351 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000352}
353
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000354void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
355{
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000356 llvm::Function *EnumerationMutationFn =
357 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000358 llvm::Value *DeclAddress;
359 QualType ElementTy;
360
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000361 if (!EnumerationMutationFn) {
362 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
363 return;
364 }
365
Anders Carlssonf484c312008-08-31 02:33:12 +0000366 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
367 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000368 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000369 const Decl* D = SD->getSolitaryDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000370 ElementTy = cast<ValueDecl>(D)->getType();
371 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000372 } else {
373 ElementTy = cast<Expr>(S.getElement())->getType();
374 DeclAddress = 0;
375 }
376
377 // Fast enumeration state.
378 QualType StateTy = getContext().getObjCFastEnumerationStateType();
379 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
380 "state.ptr");
381 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000382 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000383
384 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000385 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000386
387 // Get selector
388 llvm::SmallVector<IdentifierInfo*, 3> II;
389 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
390 II.push_back(&CGM.getContext().Idents.get("objects"));
391 II.push_back(&CGM.getContext().Idents.get("count"));
392 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
393 &II[0]);
394
395 QualType ItemsTy =
396 getContext().getConstantArrayType(getContext().getObjCIdType(),
397 llvm::APInt(32, NumItems),
398 ArrayType::Normal, 0);
399 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
400
401 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
402
403 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000404 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000405 getContext().getPointerType(StateTy)));
406
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000407 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000408 getContext().getPointerType(ItemsTy)));
409
410 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
411 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000412 Args.push_back(std::make_pair(RValue::get(Count),
413 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000414
415 RValue CountRV =
416 CGM.getObjCRuntime().GenerateMessageSend(*this,
417 getContext().UnsignedLongTy,
418 FastEnumSel,
419 Collection, false, Args);
420
421 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
422 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
423
Daniel Dunbar55e87422008-11-11 02:29:29 +0000424 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
425 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000426
427 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
428 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
429
430 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000431 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000432
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000433 EmitBlock(SetStartMutations);
434
435 llvm::Value *StartMutationsPtr =
436 CreateTempAlloca(UnsignedLongLTy);
437
438 llvm::Value *StateMutationsPtrPtr =
439 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
440 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
441 "mutationsptr");
442
443 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
444 "mutations");
445
446 Builder.CreateStore(StateMutations, StartMutationsPtr);
447
Daniel Dunbar55e87422008-11-11 02:29:29 +0000448 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000449 EmitBlock(LoopStart);
450
Anders Carlssonf484c312008-08-31 02:33:12 +0000451 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
452 Builder.CreateStore(Zero, CounterPtr);
453
Daniel Dunbar55e87422008-11-11 02:29:29 +0000454 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000455 EmitBlock(LoopBody);
456
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000457 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
458 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
459
460 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
461 "mutations");
462 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
463 StartMutations,
464 "tobool");
465
466
Daniel Dunbar55e87422008-11-11 02:29:29 +0000467 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
468 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000469
470 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
471
472 EmitBlock(WasMutated);
473 llvm::Value *V =
474 Builder.CreateBitCast(Collection,
475 ConvertType(getContext().getObjCIdType()),
476 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000477 CallArgList Args2;
478 Args2.push_back(std::make_pair(RValue::get(V),
479 getContext().getObjCIdType()));
480 // FIXME: We shouldn't need to get the function info here, the
481 // runtime already should have computed it to build the function.
Daniel Dunbar90350b62009-02-04 22:00:33 +0000482 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000483 EnumerationMutationFn, Args2);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000484
485 EmitBlock(WasNotMutated);
486
Anders Carlssonf484c312008-08-31 02:33:12 +0000487 llvm::Value *StateItemsPtr =
488 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
489
490 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
491
492 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
493 "stateitems");
494
495 llvm::Value *CurrentItemPtr =
496 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
497
498 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
499
500 // Cast the item to the right type.
501 CurrentItem = Builder.CreateBitCast(CurrentItem,
502 ConvertType(ElementTy), "tmp");
503
504 if (!DeclAddress) {
505 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
506
507 // Set the value to null.
508 Builder.CreateStore(CurrentItem, LV.getAddress());
509 } else
510 Builder.CreateStore(CurrentItem, DeclAddress);
511
512 // Increment the counter.
513 Counter = Builder.CreateAdd(Counter,
514 llvm::ConstantInt::get(UnsignedLongLTy, 1));
515 Builder.CreateStore(Counter, CounterPtr);
516
Daniel Dunbar55e87422008-11-11 02:29:29 +0000517 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
518 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000519
Anders Carlssone4b6d342009-02-10 05:52:02 +0000520 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000521
522 EmitStmt(S.getBody());
523
524 BreakContinueStack.pop_back();
525
526 EmitBlock(AfterBody);
527
Daniel Dunbar55e87422008-11-11 02:29:29 +0000528 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000529
530 Counter = Builder.CreateLoad(CounterPtr);
531 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000532 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000533 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000534
535 // Fetch more elements.
536 EmitBlock(FetchMore);
537
538 CountRV =
539 CGM.getObjCRuntime().GenerateMessageSend(*this,
540 getContext().UnsignedLongTy,
541 FastEnumSel,
542 Collection, false, Args);
543 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
544 Limit = Builder.CreateLoad(LimitPtr);
545
546 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
547 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
548
549 // No more elements.
550 EmitBlock(NoElements);
551
552 if (!DeclAddress) {
553 // If the element was not a declaration, set it to be null.
554
555 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
556
557 // Set the value to null.
558 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
559 LV.getAddress());
560 }
561
562 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000563}
564
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000565void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
566{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000567 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000568}
569
570void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
571{
572 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
573}
574
Chris Lattner10cac6f2008-11-15 21:26:17 +0000575void CodeGenFunction::EmitObjCAtSynchronizedStmt(
576 const ObjCAtSynchronizedStmt &S)
577{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000578 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000579}
580
Ted Kremenek2979ec72008-04-09 15:51:31 +0000581CGObjCRuntime::~CGObjCRuntime() {}