blob: e22efa162adf05394823937351341b5c312059dc [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{
Steve Naroff2c8a08e2009-03-31 16:53:37 +000029 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(E);
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000030 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000031 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner6ee20e32008-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 Dunbarfc69bde2008-08-11 18:12:00 +000040 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner6ee20e32008-06-24 17:04:18 +000041}
42
Daniel Dunbarf1f7f192008-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 Lattner6ee20e32008-06-24 17:04:18 +000047
48
Daniel Dunbara04840b2008-08-23 03:46:30 +000049RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner6ee20e32008-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 Dunbarfc69bde2008-08-11 18:12:00 +000054 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner6ee20e32008-06-24 17:04:18 +000055 const Expr *ReceiverExpr = E->getReceiver();
56 bool isSuperMessage = false;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000057 bool isClassMessage = false;
Chris Lattner6ee20e32008-06-24 17:04:18 +000058 // Find the receiver
59 llvm::Value *Receiver;
60 if (!ReceiverExpr) {
Daniel Dunbar434627a2008-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 Lattner05fb7c82008-11-20 04:42:34 +000066 assert(E->getClassName()->isStr("super") &&
Daniel Dunbar434627a2008-08-16 00:25:02 +000067 "Unexpected missing class interface in message send.");
Daniel Dunbar434627a2008-08-16 00:25:02 +000068 isSuperMessage = true;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000069 Receiver = LoadObjCSelf();
70 } else {
71 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner6ee20e32008-06-24 17:04:18 +000072 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000073
74 isClassMessage = true;
Douglas Gregord8606632008-11-04 14:56:14 +000075 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner6ee20e32008-06-24 17:04:18 +000076 isSuperMessage = true;
77 Receiver = LoadObjCSelf();
78 } else {
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000079 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner6ee20e32008-06-24 17:04:18 +000080 }
81
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000082 CallArgList Args;
Anders Carlsson804de012009-04-18 20:29:27 +000083 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000084
Chris Lattner6ee20e32008-06-24 17:04:18 +000085 if (isSuperMessage) {
Chris Lattner8384c142008-06-26 04:42:20 +000086 // super is only valid in an Objective-C method
87 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian17636fa2009-02-28 20:07:56 +000088 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbardd851282008-08-30 05:35:15 +000089 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
90 E->getSelector(),
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000091 OMD->getClassInterface(),
Fariborz Jahanian17636fa2009-02-28 20:07:56 +000092 isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000093 Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000094 isClassMessage,
95 Args);
Chris Lattner6ee20e32008-06-24 17:04:18 +000096 }
Daniel Dunbardd851282008-08-30 05:35:15 +000097 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
98 Receiver, isClassMessage, Args);
Anders Carlssona66cad42007-08-21 17:43:55 +000099}
100
Daniel Dunbar6b57d432008-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.
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000104void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
105 const ObjCContainerDecl *CD) {
Daniel Dunbar96816832008-09-09 23:14:03 +0000106 FunctionArgList Args;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000107 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000108
Daniel Dunbar5b90ae12009-04-17 00:48:04 +0000109 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
110 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000111
Daniel Dunbar96816832008-09-09 23:14:03 +0000112 Args.push_back(std::make_pair(OMD->getSelfDecl(),
113 OMD->getSelfDecl()->getType()));
114 Args.push_back(std::make_pair(OMD->getCmdDecl(),
115 OMD->getCmdDecl()->getType()));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000116
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000117 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
118 E = OMD->param_end(); PI != E; ++PI)
119 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000120
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000121 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000122}
Daniel Dunbarace33292008-08-16 03:19:19 +0000123
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000124/// Generate an Objective-C method. An Objective-C method is a C function with
125/// its pointer, name, and types registered in the class struture.
126void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel9c9dd7f2009-02-25 01:09:46 +0000127 // Check if we should generate debug info for this method.
Daniel Dunbar78582862009-04-13 21:08:27 +0000128 if (CGM.getDebugInfo() && !OMD->hasAttr<NodebugAttr>())
Devang Patel9c9dd7f2009-02-25 01:09:46 +0000129 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000130 StartObjCMethod(OMD, OMD->getClassInterface());
Douglas Gregore3241e92009-04-18 00:02:19 +0000131 EmitStmt(OMD->getBody(getContext()));
132 FinishFunction(cast<CompoundStmt>(OMD->getBody(getContext()))->getRBracLoc());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000133}
134
135// FIXME: I wasn't sure about the synthesis approach. If we end up
136// generating an AST for the whole body we can just fall back to
137// having a GenerateFunction which takes the body Stmt.
138
139/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff9336dbd2009-01-10 22:55:25 +0000140/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
141/// is illegal within a category.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000142void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
143 const ObjCPropertyImplDecl *PID) {
Daniel Dunbard82223f2008-09-24 04:04:31 +0000144 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000145 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
146 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
147 assert(OMD && "Invalid call to generate getter (empty method)");
148 // FIXME: This is rather murky, we create this here since they will
149 // not have been created by Sema for us.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000150 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000151 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000152
Daniel Dunbard82223f2008-09-24 04:04:31 +0000153 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian32849782008-12-08 23:56:17 +0000154 // this. Non-atomic properties are directly evaluated.
155 // atomic 'copy' and 'retain' properties are also directly
156 // evaluated in gc-only mode.
Daniel Dunbard82223f2008-09-24 04:04:31 +0000157 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian32849782008-12-08 23:56:17 +0000158 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
159 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
160 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbard82223f2008-09-24 04:04:31 +0000161 llvm::Value *GetPropertyFn =
162 CGM.getObjCRuntime().GetPropertyGetFunction();
163
164 if (!GetPropertyFn) {
165 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
166 FinishFunction();
167 return;
168 }
169
170 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
171 // FIXME: Can't this be simpler? This might even be worse than the
172 // corresponding gcc code.
173 CodeGenTypes &Types = CGM.getTypes();
174 ValueDecl *Cmd = OMD->getCmdDecl();
175 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
176 QualType IdTy = getContext().getObjCIdType();
177 llvm::Value *SelfAsId =
178 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000179 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbard82223f2008-09-24 04:04:31 +0000180 llvm::Value *True =
Daniel Dunbard83141af2009-02-04 00:55:44 +0000181 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbard82223f2008-09-24 04:04:31 +0000182 CallArgList Args;
183 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
184 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
185 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
186 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbar93ba3732009-02-03 23:43:59 +0000187 // FIXME: We shouldn't need to get the function info here, the
188 // runtime already should have computed it to build the function.
Daniel Dunbar34bda882009-02-02 23:23:47 +0000189 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000190 GetPropertyFn, Args);
Daniel Dunbard82223f2008-09-24 04:04:31 +0000191 // We need to fix the type here. Ivars with copy & retain are
192 // always objects so we don't need to worry about complex or
193 // aggregates.
194 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
195 Types.ConvertType(PD->getType())));
196 EmitReturnOfRValue(RV, PD->getType());
197 } else {
Daniel Dunbarf0587c62009-04-20 00:37:55 +0000198 const FieldDecl *Field =
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000199 IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
Fariborz Jahanian55343922009-02-03 00:09:52 +0000200 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
201 LoadObjCSelf(), Ivar, Field, 0);
Fariborz Jahaniand70b09c2008-11-26 22:36:09 +0000202 if (hasAggregateLLVMType(Ivar->getType())) {
203 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
204 }
Fariborz Jahanianb66b3fa2009-03-03 18:49:40 +0000205 else {
206 CodeGenTypes &Types = CGM.getTypes();
207 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
208 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
209 Types.ConvertType(PD->getType())));
210 EmitReturnOfRValue(RV, PD->getType());
211 }
Daniel Dunbard82223f2008-09-24 04:04:31 +0000212 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000213
214 FinishFunction();
215}
216
217/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff9336dbd2009-01-10 22:55:25 +0000218/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
219/// is illegal within a category.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000220void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
221 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000222 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000223 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
224 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
225 assert(OMD && "Invalid call to generate setter (empty method)");
226 // FIXME: This is rather murky, we create this here since they will
227 // not have been created by Sema for us.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000228 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000229 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000230
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000231 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
232 bool IsAtomic =
233 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
234
235 // Determine if we should use an objc_setProperty call for
236 // this. Properties with 'copy' semantics always use it, as do
237 // non-atomic properties with 'release' semantics as long as we are
238 // not in gc-only mode.
239 if (IsCopy ||
240 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
241 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
242 llvm::Value *SetPropertyFn =
243 CGM.getObjCRuntime().GetPropertySetFunction();
244
245 if (!SetPropertyFn) {
246 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
247 FinishFunction();
248 return;
249 }
250
251 // Emit objc_setProperty((id) self, _cmd, offset, arg,
252 // <is-atomic>, <is-copy>).
253 // FIXME: Can't this be simpler? This might even be worse than the
254 // corresponding gcc code.
255 CodeGenTypes &Types = CGM.getTypes();
256 ValueDecl *Cmd = OMD->getCmdDecl();
257 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
258 QualType IdTy = getContext().getObjCIdType();
259 llvm::Value *SelfAsId =
260 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000261 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000262 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000263 llvm::Value *ArgAsId =
264 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
265 Types.ConvertType(IdTy));
266 llvm::Value *True =
Daniel Dunbard83141af2009-02-04 00:55:44 +0000267 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000268 llvm::Value *False =
Daniel Dunbard83141af2009-02-04 00:55:44 +0000269 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000270 CallArgList Args;
271 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
272 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
273 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
274 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
275 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
276 getContext().BoolTy));
277 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
278 getContext().BoolTy));
Daniel Dunbar93ba3732009-02-03 23:43:59 +0000279 // FIXME: We shouldn't need to get the function info here, the
280 // runtime already should have computed it to build the function.
281 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar34bda882009-02-02 23:23:47 +0000282 SetPropertyFn, Args);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000283 } else {
284 SourceLocation Loc = PD->getLocation();
285 ValueDecl *Self = OMD->getSelfDecl();
286 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
287 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000288 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000289 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000290 ObjCInterfaceDecl *OI = IMP->getClassInterface();
Fariborz Jahanianea944842008-12-18 17:29:46 +0000291 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000292 true, true);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000293 getContext().setFieldDecl(OI, Ivar, &IvarRef);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000294 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
295 Ivar->getType(), Loc);
296 EmitStmt(&Assign);
297 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000298
299 FinishFunction();
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000300}
301
Daniel Dunbard82223f2008-09-24 04:04:31 +0000302llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarace33292008-08-16 03:19:19 +0000303 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stumpfc5e6de2009-03-20 21:53:12 +0000304 // See if we need to lazily forward self inside a block literal.
305 BlockForwardSelf();
Daniel Dunbarace33292008-08-16 03:19:19 +0000306 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000307}
308
Fariborz Jahanian55343922009-02-03 00:09:52 +0000309QualType CodeGenFunction::TypeOfSelfObject() {
310 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
311 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
312 const PointerType *PTy =
313 cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
314 return PTy->getPointeeType();
315}
316
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000317RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
318 const Selector &S) {
319 llvm::Value *Receiver = LoadObjCSelf();
320 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
321 bool isClassMessage = OMD->isClassMethod();
322 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
323 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
324 Exp->getType(),
325 S,
326 OMD->getClassInterface(),
327 isCategoryImpl,
328 Receiver,
329 isClassMessage,
330 CallArgList());
331
332}
333
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000334RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000335 // FIXME: Split it into two separate routines.
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000336 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
337 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000338 if (isa<ObjCSuperExpr>(E->getBase()))
339 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000340 return CGM.getObjCRuntime().
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000341 GenerateMessageSend(*this, Exp->getType(), S,
342 EmitScalarExpr(E->getBase()),
343 false, CallArgList());
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000344 }
Daniel Dunbarddebeca2009-01-15 18:32:35 +0000345 else {
Daniel Dunbarb5b06b22009-01-16 01:50:29 +0000346 const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp);
347 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000348 llvm::Value *Receiver;
349 if (KE->getClassProp()) {
350 const ObjCInterfaceDecl *OID = KE->getClassProp();
351 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
352 }
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000353 else if (isa<ObjCSuperExpr>(KE->getBase()))
354 return EmitObjCSuperPropertyGet(KE, S);
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000355 else
356 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000357 return CGM.getObjCRuntime().
358 GenerateMessageSend(*this, Exp->getType(), S,
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000359 Receiver,
360 KE->getClassProp() != 0, CallArgList());
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000361 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000362}
363
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000364void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
365 const Selector &S,
366 RValue Src) {
367 CallArgList Args;
368 llvm::Value *Receiver = LoadObjCSelf();
369 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
370 bool isClassMessage = OMD->isClassMethod();
371 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
372 Args.push_back(std::make_pair(Src, Exp->getType()));
373 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
374 Exp->getType(),
375 S,
376 OMD->getClassInterface(),
377 isCategoryImpl,
378 Receiver,
379 isClassMessage,
380 Args);
381 return;
382}
383
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000384void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbare6c31752008-08-29 08:11:39 +0000385 RValue Src) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000386 // FIXME: Split it into two separate routines.
387 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
388 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000389 if (isa<ObjCSuperExpr>(E->getBase())) {
390 EmitObjCSuperPropertySet(E, S, Src);
391 return;
392 }
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000393 CallArgList Args;
394 Args.push_back(std::make_pair(Src, E->getType()));
395 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
396 EmitScalarExpr(E->getBase()),
397 false, Args);
398 }
399 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
400 Selector S = E->getSetterMethod()->getSelector();
401 CallArgList Args;
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000402 llvm::Value *Receiver;
403 if (E->getClassProp()) {
404 const ObjCInterfaceDecl *OID = E->getClassProp();
405 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
406 }
Fariborz Jahanianbafa3c22009-03-20 17:22:23 +0000407 else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000408 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanianbafa3c22009-03-20 17:22:23 +0000409 return;
410 }
411 else
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000412 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000413 Args.push_back(std::make_pair(Src, E->getType()));
414 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000415 Receiver,
416 E->getClassProp() != 0, Args);
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000417 }
418 else
419 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbare6c31752008-08-29 08:11:39 +0000420}
421
Chris Lattneraea1aee2009-03-22 21:03:39 +0000422void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
423 llvm::Constant *EnumerationMutationFn =
Daniel Dunbard82223f2008-09-24 04:04:31 +0000424 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000425 llvm::Value *DeclAddress;
426 QualType ElementTy;
427
Daniel Dunbard82223f2008-09-24 04:04:31 +0000428 if (!EnumerationMutationFn) {
429 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
430 return;
431 }
432
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000433 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
434 EmitStmt(SD);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +0000435 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000436 const Decl* D = SD->getSingleDecl();
Ted Kremenek2ac10d02008-10-06 20:59:48 +0000437 ElementTy = cast<ValueDecl>(D)->getType();
438 DeclAddress = LocalDeclMap[D];
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000439 } else {
440 ElementTy = cast<Expr>(S.getElement())->getType();
441 DeclAddress = 0;
442 }
443
444 // Fast enumeration state.
445 QualType StateTy = getContext().getObjCFastEnumerationStateType();
446 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
447 "state.ptr");
448 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson58d16242008-08-31 04:05:03 +0000449 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000450
451 // Number of elements in the items array.
Anders Carlsson58d16242008-08-31 04:05:03 +0000452 static const unsigned NumItems = 16;
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000453
454 // Get selector
455 llvm::SmallVector<IdentifierInfo*, 3> II;
456 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
457 II.push_back(&CGM.getContext().Idents.get("objects"));
458 II.push_back(&CGM.getContext().Idents.get("count"));
459 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
460 &II[0]);
461
462 QualType ItemsTy =
463 getContext().getConstantArrayType(getContext().getObjCIdType(),
464 llvm::APInt(32, NumItems),
465 ArrayType::Normal, 0);
466 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
467
468 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
469
470 CallArgList Args;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000471 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000472 getContext().getPointerType(StateTy)));
473
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000474 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000475 getContext().getPointerType(ItemsTy)));
476
477 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
478 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000479 Args.push_back(std::make_pair(RValue::get(Count),
480 getContext().UnsignedLongTy));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000481
482 RValue CountRV =
483 CGM.getObjCRuntime().GenerateMessageSend(*this,
484 getContext().UnsignedLongTy,
485 FastEnumSel,
486 Collection, false, Args);
487
488 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
489 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
490
Daniel Dunbar72f96552008-11-11 02:29:29 +0000491 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
492 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000493
494 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
495 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
496
497 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson58d16242008-08-31 04:05:03 +0000498 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000499
Anders Carlsson58d16242008-08-31 04:05:03 +0000500 EmitBlock(SetStartMutations);
501
502 llvm::Value *StartMutationsPtr =
503 CreateTempAlloca(UnsignedLongLTy);
504
505 llvm::Value *StateMutationsPtrPtr =
506 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
507 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
508 "mutationsptr");
509
510 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
511 "mutations");
512
513 Builder.CreateStore(StateMutations, StartMutationsPtr);
514
Daniel Dunbar72f96552008-11-11 02:29:29 +0000515 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000516 EmitBlock(LoopStart);
517
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000518 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
519 Builder.CreateStore(Zero, CounterPtr);
520
Daniel Dunbar72f96552008-11-11 02:29:29 +0000521 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000522 EmitBlock(LoopBody);
523
Anders Carlsson58d16242008-08-31 04:05:03 +0000524 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
525 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
526
527 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
528 "mutations");
529 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
530 StartMutations,
531 "tobool");
532
533
Daniel Dunbar72f96552008-11-11 02:29:29 +0000534 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
535 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson58d16242008-08-31 04:05:03 +0000536
537 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
538
539 EmitBlock(WasMutated);
540 llvm::Value *V =
541 Builder.CreateBitCast(Collection,
542 ConvertType(getContext().getObjCIdType()),
543 "tmp");
Daniel Dunbar903041b2009-02-03 23:55:40 +0000544 CallArgList Args2;
545 Args2.push_back(std::make_pair(RValue::get(V),
546 getContext().getObjCIdType()));
547 // FIXME: We shouldn't need to get the function info here, the
548 // runtime already should have computed it to build the function.
Daniel Dunbar87f3edc2009-02-04 22:00:33 +0000549 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar903041b2009-02-03 23:55:40 +0000550 EnumerationMutationFn, Args2);
Anders Carlsson58d16242008-08-31 04:05:03 +0000551
552 EmitBlock(WasNotMutated);
553
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000554 llvm::Value *StateItemsPtr =
555 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
556
557 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
558
559 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
560 "stateitems");
561
562 llvm::Value *CurrentItemPtr =
563 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
564
565 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
566
567 // Cast the item to the right type.
568 CurrentItem = Builder.CreateBitCast(CurrentItem,
569 ConvertType(ElementTy), "tmp");
570
571 if (!DeclAddress) {
572 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
573
574 // Set the value to null.
575 Builder.CreateStore(CurrentItem, LV.getAddress());
576 } else
577 Builder.CreateStore(CurrentItem, DeclAddress);
578
579 // Increment the counter.
580 Counter = Builder.CreateAdd(Counter,
581 llvm::ConstantInt::get(UnsignedLongLTy, 1));
582 Builder.CreateStore(Counter, CounterPtr);
583
Daniel Dunbar72f96552008-11-11 02:29:29 +0000584 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
585 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000586
Anders Carlsson7c314902009-02-10 05:52:02 +0000587 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000588
589 EmitStmt(S.getBody());
590
591 BreakContinueStack.pop_back();
592
593 EmitBlock(AfterBody);
594
Daniel Dunbar72f96552008-11-11 02:29:29 +0000595 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanian6296cda2009-01-06 18:56:31 +0000596
597 Counter = Builder.CreateLoad(CounterPtr);
598 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000599 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar35bd6192008-09-04 21:54:37 +0000600 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000601
602 // Fetch more elements.
603 EmitBlock(FetchMore);
604
605 CountRV =
606 CGM.getObjCRuntime().GenerateMessageSend(*this,
607 getContext().UnsignedLongTy,
608 FastEnumSel,
609 Collection, false, Args);
610 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
611 Limit = Builder.CreateLoad(LimitPtr);
612
613 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
614 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
615
616 // No more elements.
617 EmitBlock(NoElements);
618
619 if (!DeclAddress) {
620 // If the element was not a declaration, set it to be null.
621
622 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
623
624 // Set the value to null.
625 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
626 LV.getAddress());
627 }
628
629 EmitBlock(LoopEnd);
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000630}
631
Anders Carlssonb01a2112008-09-09 10:04:29 +0000632void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
633{
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000634 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000635}
636
637void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
638{
639 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
640}
641
Chris Lattnerdd978702008-11-15 21:26:17 +0000642void CodeGenFunction::EmitObjCAtSynchronizedStmt(
643 const ObjCAtSynchronizedStmt &S)
644{
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000645 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattnerdd978702008-11-15 21:26:17 +0000646}
647
Ted Kremenekfa4ebab2008-04-09 15:51:31 +0000648CGObjCRuntime::~CGObjCRuntime() {}