blob: 58999129813bb7ea83fa7d779c9e6e3d05aaada8 [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"
Chris Lattner4a9e9272009-04-26 01:32:48 +000019#include "clang/AST/StmtObjC.h"
Daniel Dunbarcc37ac52008-09-03 00:27:26 +000020#include "clang/Basic/Diagnostic.h"
Anders Carlsson82b0d0c2008-08-30 19:51:14 +000021#include "llvm/ADT/STLExtras.h"
Daniel Dunbard82223f2008-09-24 04:04:31 +000022#include "llvm/Target/TargetData.h"
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.
Owen Anderson557515b2009-07-29 18:54: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(),
Fariborz Jahanianc8007152009-05-05 21:36:57 +000098 Receiver, isClassMessage, Args,
99 E->getMethodDecl());
Anders Carlssona66cad42007-08-21 17:43:55 +0000100}
101
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000102/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
103/// the LLVM function and sets the other context used by
104/// CodeGenFunction.
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000105void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
106 const ObjCContainerDecl *CD) {
Daniel Dunbar96816832008-09-09 23:14:03 +0000107 FunctionArgList Args;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000108 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000109
Daniel Dunbar5b90ae12009-04-17 00:48:04 +0000110 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
111 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
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) {
Devang Patel9c9dd7f2009-02-25 01:09:46 +0000128 // Check if we should generate debug info for this method.
Mike Stump9c71db12009-08-26 22:31:08 +0000129 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
Devang Patel9c9dd7f2009-02-25 01:09:46 +0000130 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000131 StartObjCMethod(OMD, OMD->getClassInterface());
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000132 EmitStmt(OMD->getBody());
133 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000134}
135
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000136// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
137// AST for the whole body we can just fall back to having a GenerateFunction
138// which takes the body Stmt.
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000139
140/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff9336dbd2009-01-10 22:55:25 +0000141/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
142/// is illegal within a category.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000143void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
144 const ObjCPropertyImplDecl *PID) {
Daniel Dunbard82223f2008-09-24 04:04:31 +0000145 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar6b57d432008-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)");
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000149 // FIXME: This is rather murky, we create this here since they will not have
150 // been created by Sema for us.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000151 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000152 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000153
Daniel Dunbard82223f2008-09-24 04:04:31 +0000154 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian32849782008-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 Dunbard82223f2008-09-24 04:04:31 +0000158 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian32849782008-12-08 23:56:17 +0000159 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
160 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
161 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbard82223f2008-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 Jahanian91dd9d32008-12-09 20:23:04 +0000180 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbard82223f2008-09-24 04:04:31 +0000181 llvm::Value *True =
Owen Andersonb17ec712009-07-24 23:12:58 +0000182 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbard82223f2008-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 Dunbar93ba3732009-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 Dunbar34bda882009-02-02 23:23:47 +0000190 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000191 GetPropertyFn, Args);
Daniel Dunbard82223f2008-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 {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +0000199 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
Fariborz Jahaniand70b09c2008-11-26 22:36:09 +0000200 if (hasAggregateLLVMType(Ivar->getType())) {
201 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
Mike Stump487ce382009-07-30 22:28:39 +0000202 } else {
Fariborz Jahanianb66b3fa2009-03-03 18:49:40 +0000203 CodeGenTypes &Types = CGM.getTypes();
204 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
205 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
206 Types.ConvertType(PD->getType())));
207 EmitReturnOfRValue(RV, PD->getType());
208 }
Daniel Dunbard82223f2008-09-24 04:04:31 +0000209 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000210
211 FinishFunction();
212}
213
214/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff9336dbd2009-01-10 22:55:25 +0000215/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
216/// is illegal within a category.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000217void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
218 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000219 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbar6b57d432008-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)");
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000223 // FIXME: This is rather murky, we create this here since they will not have
224 // been created by Sema for us.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000225 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000226 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000227
Daniel Dunbarf7f6c7b2008-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 Jahanian91dd9d32008-12-09 20:23:04 +0000258 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000259 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000260 llvm::Value *ArgAsId =
261 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
262 Types.ConvertType(IdTy));
263 llvm::Value *True =
Owen Andersonb17ec712009-07-24 23:12:58 +0000264 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000265 llvm::Value *False =
Owen Andersonb17ec712009-07-24 23:12:58 +0000266 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbarf7f6c7b2008-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));
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000276 // FIXME: We shouldn't need to get the function info here, the runtime
277 // already should have computed it to build the function.
Daniel Dunbar93ba3732009-02-03 23:43:59 +0000278 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar34bda882009-02-02 23:23:47 +0000279 SetPropertyFn, Args);
Daniel Dunbarf7f6c7b2008-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 Lattner5c6b2c62009-02-20 18:43:26 +0000285 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000286 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000287 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbarf7f6c7b2008-09-24 06:32:09 +0000288 true, true);
289 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
290 Ivar->getType(), Loc);
291 EmitStmt(&Assign);
292 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000293
294 FinishFunction();
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000295}
296
Daniel Dunbard82223f2008-09-24 04:04:31 +0000297llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarace33292008-08-16 03:19:19 +0000298 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stumpfc5e6de2009-03-20 21:53:12 +0000299 // See if we need to lazily forward self inside a block literal.
300 BlockForwardSelf();
Daniel Dunbarace33292008-08-16 03:19:19 +0000301 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000302}
303
Fariborz Jahanian55343922009-02-03 00:09:52 +0000304QualType CodeGenFunction::TypeOfSelfObject() {
305 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
306 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff329ec222009-07-10 23:34:53 +0000307 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
308 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian55343922009-02-03 00:09:52 +0000309 return PTy->getPointeeType();
310}
311
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000312RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
313 const Selector &S) {
314 llvm::Value *Receiver = LoadObjCSelf();
315 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
316 bool isClassMessage = OMD->isClassMethod();
317 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
318 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
319 Exp->getType(),
320 S,
321 OMD->getClassInterface(),
322 isCategoryImpl,
323 Receiver,
324 isClassMessage,
325 CallArgList());
326
327}
328
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000329RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian293280c2009-09-01 17:02:21 +0000330 Exp = Exp->IgnoreParens();
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000331 // FIXME: Split it into two separate routines.
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000332 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
333 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000334 if (isa<ObjCSuperExpr>(E->getBase()))
335 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000336 return CGM.getObjCRuntime().
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000337 GenerateMessageSend(*this, Exp->getType(), S,
338 EmitScalarExpr(E->getBase()),
339 false, CallArgList());
Mike Stump487ce382009-07-30 22:28:39 +0000340 } else {
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000341 const ObjCImplicitSetterGetterRefExpr *KE =
342 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarb5b06b22009-01-16 01:50:29 +0000343 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000344 llvm::Value *Receiver;
Fariborz Jahanian5bb99722009-08-18 21:37:33 +0000345 if (KE->getInterfaceDecl()) {
346 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000347 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump487ce382009-07-30 22:28:39 +0000348 } else if (isa<ObjCSuperExpr>(KE->getBase()))
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000349 return EmitObjCSuperPropertyGet(KE, S);
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000350 else
351 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000352 return CGM.getObjCRuntime().
353 GenerateMessageSend(*this, Exp->getType(), S,
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000354 Receiver,
Fariborz Jahanian5bb99722009-08-18 21:37:33 +0000355 KE->getInterfaceDecl() != 0, CallArgList());
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000356 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000357}
358
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000359void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
360 const Selector &S,
361 RValue Src) {
362 CallArgList Args;
363 llvm::Value *Receiver = LoadObjCSelf();
364 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
365 bool isClassMessage = OMD->isClassMethod();
366 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
367 Args.push_back(std::make_pair(Src, Exp->getType()));
368 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
369 Exp->getType(),
370 S,
371 OMD->getClassInterface(),
372 isCategoryImpl,
373 Receiver,
374 isClassMessage,
375 Args);
376 return;
377}
378
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000379void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbare6c31752008-08-29 08:11:39 +0000380 RValue Src) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000381 // FIXME: Split it into two separate routines.
382 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
383 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000384 if (isa<ObjCSuperExpr>(E->getBase())) {
385 EmitObjCSuperPropertySet(E, S, Src);
386 return;
387 }
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000388 CallArgList Args;
389 Args.push_back(std::make_pair(Src, E->getType()));
390 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
391 EmitScalarExpr(E->getBase()),
392 false, Args);
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000393 } else if (const ObjCImplicitSetterGetterRefExpr *E =
394 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000395 Selector S = E->getSetterMethod()->getSelector();
396 CallArgList Args;
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000397 llvm::Value *Receiver;
Fariborz Jahanian5bb99722009-08-18 21:37:33 +0000398 if (E->getInterfaceDecl()) {
399 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000400 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Mike Stump487ce382009-07-30 22:28:39 +0000401 } else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanian0be55c12009-03-20 19:18:21 +0000402 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanianbafa3c22009-03-20 17:22:23 +0000403 return;
Mike Stump487ce382009-07-30 22:28:39 +0000404 } else
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000405 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000406 Args.push_back(std::make_pair(Src, E->getType()));
407 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
Fariborz Jahaniana5dfc182009-03-10 18:03:11 +0000408 Receiver,
Fariborz Jahanian5bb99722009-08-18 21:37:33 +0000409 E->getInterfaceDecl() != 0, Args);
Mike Stump487ce382009-07-30 22:28:39 +0000410 } else
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000411 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbare6c31752008-08-29 08:11:39 +0000412}
413
Chris Lattneraea1aee2009-03-22 21:03:39 +0000414void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
415 llvm::Constant *EnumerationMutationFn =
Daniel Dunbard82223f2008-09-24 04:04:31 +0000416 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000417 llvm::Value *DeclAddress;
418 QualType ElementTy;
419
Daniel Dunbard82223f2008-09-24 04:04:31 +0000420 if (!EnumerationMutationFn) {
421 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
422 return;
423 }
424
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000425 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
426 EmitStmt(SD);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +0000427 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000428 const Decl* D = SD->getSingleDecl();
Ted Kremenek2ac10d02008-10-06 20:59:48 +0000429 ElementTy = cast<ValueDecl>(D)->getType();
430 DeclAddress = LocalDeclMap[D];
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000431 } else {
432 ElementTy = cast<Expr>(S.getElement())->getType();
433 DeclAddress = 0;
434 }
435
436 // Fast enumeration state.
437 QualType StateTy = getContext().getObjCFastEnumerationStateType();
438 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
439 "state.ptr");
440 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson58d16242008-08-31 04:05:03 +0000441 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000442
443 // Number of elements in the items array.
Anders Carlsson58d16242008-08-31 04:05:03 +0000444 static const unsigned NumItems = 16;
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000445
446 // Get selector
447 llvm::SmallVector<IdentifierInfo*, 3> II;
448 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
449 II.push_back(&CGM.getContext().Idents.get("objects"));
450 II.push_back(&CGM.getContext().Idents.get("count"));
451 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
452 &II[0]);
453
454 QualType ItemsTy =
455 getContext().getConstantArrayType(getContext().getObjCIdType(),
456 llvm::APInt(32, NumItems),
457 ArrayType::Normal, 0);
458 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
459
460 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
461
462 CallArgList Args;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000463 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000464 getContext().getPointerType(StateTy)));
465
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000466 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000467 getContext().getPointerType(ItemsTy)));
468
469 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Andersonb17ec712009-07-24 23:12:58 +0000470 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000471 Args.push_back(std::make_pair(RValue::get(Count),
472 getContext().UnsignedLongTy));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000473
474 RValue CountRV =
475 CGM.getObjCRuntime().GenerateMessageSend(*this,
476 getContext().UnsignedLongTy,
477 FastEnumSel,
478 Collection, false, Args);
479
480 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
481 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
482
Daniel Dunbar72f96552008-11-11 02:29:29 +0000483 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
484 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000485
486 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Andersonf37b84b2009-07-31 20:28:54 +0000487 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000488
489 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson58d16242008-08-31 04:05:03 +0000490 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000491
Anders Carlsson58d16242008-08-31 04:05:03 +0000492 EmitBlock(SetStartMutations);
493
494 llvm::Value *StartMutationsPtr =
495 CreateTempAlloca(UnsignedLongLTy);
496
497 llvm::Value *StateMutationsPtrPtr =
498 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
499 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
500 "mutationsptr");
501
502 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
503 "mutations");
504
505 Builder.CreateStore(StateMutations, StartMutationsPtr);
506
Daniel Dunbar72f96552008-11-11 02:29:29 +0000507 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000508 EmitBlock(LoopStart);
509
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000510 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
511 Builder.CreateStore(Zero, CounterPtr);
512
Daniel Dunbar72f96552008-11-11 02:29:29 +0000513 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000514 EmitBlock(LoopBody);
515
Anders Carlsson58d16242008-08-31 04:05:03 +0000516 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
517 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
518
519 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
520 "mutations");
521 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
522 StartMutations,
523 "tobool");
524
525
Daniel Dunbar72f96552008-11-11 02:29:29 +0000526 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
527 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson58d16242008-08-31 04:05:03 +0000528
529 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
530
531 EmitBlock(WasMutated);
532 llvm::Value *V =
533 Builder.CreateBitCast(Collection,
534 ConvertType(getContext().getObjCIdType()),
535 "tmp");
Daniel Dunbar903041b2009-02-03 23:55:40 +0000536 CallArgList Args2;
537 Args2.push_back(std::make_pair(RValue::get(V),
538 getContext().getObjCIdType()));
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000539 // FIXME: We shouldn't need to get the function info here, the runtime already
540 // should have computed it to build the function.
Daniel Dunbar87f3edc2009-02-04 22:00:33 +0000541 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar903041b2009-02-03 23:55:40 +0000542 EnumerationMutationFn, Args2);
Anders Carlsson58d16242008-08-31 04:05:03 +0000543
544 EmitBlock(WasNotMutated);
545
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000546 llvm::Value *StateItemsPtr =
547 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
548
549 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
550
551 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
552 "stateitems");
553
554 llvm::Value *CurrentItemPtr =
555 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
556
557 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
558
559 // Cast the item to the right type.
560 CurrentItem = Builder.CreateBitCast(CurrentItem,
561 ConvertType(ElementTy), "tmp");
562
563 if (!DeclAddress) {
564 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
565
566 // Set the value to null.
567 Builder.CreateStore(CurrentItem, LV.getAddress());
568 } else
569 Builder.CreateStore(CurrentItem, DeclAddress);
570
571 // Increment the counter.
572 Counter = Builder.CreateAdd(Counter,
Owen Andersonb17ec712009-07-24 23:12:58 +0000573 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000574 Builder.CreateStore(Counter, CounterPtr);
575
Daniel Dunbar72f96552008-11-11 02:29:29 +0000576 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
577 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000578
Anders Carlsson7c314902009-02-10 05:52:02 +0000579 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000580
581 EmitStmt(S.getBody());
582
583 BreakContinueStack.pop_back();
584
585 EmitBlock(AfterBody);
586
Daniel Dunbar72f96552008-11-11 02:29:29 +0000587 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanian6296cda2009-01-06 18:56:31 +0000588
589 Counter = Builder.CreateLoad(CounterPtr);
590 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000591 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbar35bd6192008-09-04 21:54:37 +0000592 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000593
594 // Fetch more elements.
595 EmitBlock(FetchMore);
596
597 CountRV =
598 CGM.getObjCRuntime().GenerateMessageSend(*this,
599 getContext().UnsignedLongTy,
600 FastEnumSel,
601 Collection, false, Args);
602 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
603 Limit = Builder.CreateLoad(LimitPtr);
604
605 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
606 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
607
608 // No more elements.
609 EmitBlock(NoElements);
610
611 if (!DeclAddress) {
612 // If the element was not a declaration, set it to be null.
613
614 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
615
616 // Set the value to null.
Owen Andersonf37b84b2009-07-31 20:28:54 +0000617 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlsson6bdf32d2008-08-31 02:33:12 +0000618 LV.getAddress());
619 }
620
621 EmitBlock(LoopEnd);
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000622}
623
Anders Carlssonb01a2112008-09-09 10:04:29 +0000624void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
625{
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000626 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000627}
628
629void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
630{
631 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
632}
633
Chris Lattnerdd978702008-11-15 21:26:17 +0000634void CodeGenFunction::EmitObjCAtSynchronizedStmt(
635 const ObjCAtSynchronizedStmt &S)
636{
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000637 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattnerdd978702008-11-15 21:26:17 +0000638}
639
Ted Kremenekfa4ebab2008-04-09 15:51:31 +0000640CGObjCRuntime::~CGObjCRuntime() {}