blob: de63f51b330bc06677ce802ebe7466e418d74af1 [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);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000092 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000093 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
94 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000095 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000096 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000097 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000098 isClassMessage,
99 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000100 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000101 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
102 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +0000103}
104
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000105/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
106/// the LLVM function and sets the other context used by
107/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000108void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
109 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000110 FunctionArgList Args;
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000111 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000112
Daniel Dunbar7c086512008-09-09 23:14:03 +0000113 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000114
Daniel Dunbar7c086512008-09-09 23:14:03 +0000115 Args.push_back(std::make_pair(OMD->getSelfDecl(),
116 OMD->getSelfDecl()->getType()));
117 Args.push_back(std::make_pair(OMD->getCmdDecl(),
118 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000119
Chris Lattner89951a82009-02-20 18:43:26 +0000120 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
121 E = OMD->param_end(); PI != E; ++PI)
122 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000123
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000124 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000125}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000126
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000127/// Generate an Objective-C method. An Objective-C method is a C function with
128/// its pointer, name, and types registered in the class struture.
129void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel1d6a4512009-02-25 01:09:46 +0000130 // Check if we should generate debug info for this method.
131 if (CGM.getDebugInfo() && !OMD->getAttr<NodebugAttr>())
132 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000133 StartObjCMethod(OMD, OMD->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000134 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000135 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000136}
137
138// FIXME: I wasn't sure about the synthesis approach. If we end up
139// generating an AST for the whole body we can just fall back to
140// having a GenerateFunction which takes the body Stmt.
141
142/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000143/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
144/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000145void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
146 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000147 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000148 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
149 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
150 assert(OMD && "Invalid call to generate getter (empty method)");
151 // FIXME: This is rather murky, we create this here since they will
152 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000153 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000154 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000155
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000156 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000157 // this. Non-atomic properties are directly evaluated.
158 // atomic 'copy' and 'retain' properties are also directly
159 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000160 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000161 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
162 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
163 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000164 llvm::Value *GetPropertyFn =
165 CGM.getObjCRuntime().GetPropertyGetFunction();
166
167 if (!GetPropertyFn) {
168 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
169 FinishFunction();
170 return;
171 }
172
173 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
174 // FIXME: Can't this be simpler? This might even be worse than the
175 // corresponding gcc code.
176 CodeGenTypes &Types = CGM.getTypes();
177 ValueDecl *Cmd = OMD->getCmdDecl();
178 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
179 QualType IdTy = getContext().getObjCIdType();
180 llvm::Value *SelfAsId =
181 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000182 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000183 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000184 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000185 CallArgList Args;
186 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
187 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
188 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
189 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000190 // FIXME: We shouldn't need to get the function info here, the
191 // runtime already should have computed it to build the function.
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000192 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar88b53962009-02-02 22:03:45 +0000193 GetPropertyFn, Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000194 // We need to fix the type here. Ivars with copy & retain are
195 // always objects so we don't need to worry about complex or
196 // aggregates.
197 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
198 Types.ConvertType(PD->getType())));
199 EmitReturnOfRValue(RV, PD->getType());
200 } else {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000201 FieldDecl *Field =
202 IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000203 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
204 LoadObjCSelf(), Ivar, Field, 0);
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000205 if (hasAggregateLLVMType(Ivar->getType())) {
206 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
207 }
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000208 else {
209 CodeGenTypes &Types = CGM.getTypes();
210 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
211 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
212 Types.ConvertType(PD->getType())));
213 EmitReturnOfRValue(RV, PD->getType());
214 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000215 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000216
217 FinishFunction();
218}
219
220/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000221/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
222/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000223void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
224 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000225 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000226 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
227 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
228 assert(OMD && "Invalid call to generate setter (empty method)");
229 // FIXME: This is rather murky, we create this here since they will
230 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000231 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000232 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000233
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000234 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
235 bool IsAtomic =
236 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
237
238 // Determine if we should use an objc_setProperty call for
239 // this. Properties with 'copy' semantics always use it, as do
240 // non-atomic properties with 'release' semantics as long as we are
241 // not in gc-only mode.
242 if (IsCopy ||
243 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
244 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
245 llvm::Value *SetPropertyFn =
246 CGM.getObjCRuntime().GetPropertySetFunction();
247
248 if (!SetPropertyFn) {
249 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
250 FinishFunction();
251 return;
252 }
253
254 // Emit objc_setProperty((id) self, _cmd, offset, arg,
255 // <is-atomic>, <is-copy>).
256 // FIXME: Can't this be simpler? This might even be worse than the
257 // corresponding gcc code.
258 CodeGenTypes &Types = CGM.getTypes();
259 ValueDecl *Cmd = OMD->getCmdDecl();
260 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
261 QualType IdTy = getContext().getObjCIdType();
262 llvm::Value *SelfAsId =
263 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000264 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000265 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000266 llvm::Value *ArgAsId =
267 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
268 Types.ConvertType(IdTy));
269 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000270 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000271 llvm::Value *False =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000272 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000273 CallArgList Args;
274 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
275 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
276 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
277 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
278 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
279 getContext().BoolTy));
280 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
281 getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000282 // FIXME: We shouldn't need to get the function info here, the
283 // runtime already should have computed it to build the function.
284 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000285 SetPropertyFn, Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000286 } else {
287 SourceLocation Loc = PD->getLocation();
288 ValueDecl *Self = OMD->getSelfDecl();
289 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
290 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000291 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000292 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanianaaa63a72008-12-13 22:20:28 +0000293 ObjCInterfaceDecl *OI = IMP->getClassInterface();
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000294 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000295 true, true);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000296 getContext().setFieldDecl(OI, Ivar, &IvarRef);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000297 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
298 Ivar->getType(), Loc);
299 EmitStmt(&Assign);
300 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000301
302 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000303}
304
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000305llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000306 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000307 // See if we need to lazily forward self inside a block literal.
308 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000309 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000310}
311
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000312QualType CodeGenFunction::TypeOfSelfObject() {
313 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
314 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
315 const PointerType *PTy =
316 cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
317 return PTy->getPointeeType();
318}
319
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000320RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
321 const Selector &S) {
322 llvm::Value *Receiver = LoadObjCSelf();
323 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
324 bool isClassMessage = OMD->isClassMethod();
325 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
326 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
327 Exp->getType(),
328 S,
329 OMD->getClassInterface(),
330 isCategoryImpl,
331 Receiver,
332 isClassMessage,
333 CallArgList());
334
335}
336
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000337RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000338 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000339 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
340 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000341 if (isa<ObjCSuperExpr>(E->getBase()))
342 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000343 return CGM.getObjCRuntime().
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000344 GenerateMessageSend(*this, Exp->getType(), S,
345 EmitScalarExpr(E->getBase()),
346 false, CallArgList());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000347 }
Daniel Dunbarf1853192009-01-15 18:32:35 +0000348 else {
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000349 const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp);
350 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000351 llvm::Value *Receiver;
352 if (KE->getClassProp()) {
353 const ObjCInterfaceDecl *OID = KE->getClassProp();
354 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
355 }
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000356 else if (isa<ObjCSuperExpr>(KE->getBase()))
357 return EmitObjCSuperPropertyGet(KE, S);
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000358 else
359 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000360 return CGM.getObjCRuntime().
361 GenerateMessageSend(*this, Exp->getType(), S,
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000362 Receiver,
363 KE->getClassProp() != 0, CallArgList());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000364 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000365}
366
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000367void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
368 const Selector &S,
369 RValue Src) {
370 CallArgList Args;
371 llvm::Value *Receiver = LoadObjCSelf();
372 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
373 bool isClassMessage = OMD->isClassMethod();
374 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
375 Args.push_back(std::make_pair(Src, Exp->getType()));
376 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
377 Exp->getType(),
378 S,
379 OMD->getClassInterface(),
380 isCategoryImpl,
381 Receiver,
382 isClassMessage,
383 Args);
384 return;
385}
386
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000387void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000388 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000389 // FIXME: Split it into two separate routines.
390 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
391 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000392 if (isa<ObjCSuperExpr>(E->getBase())) {
393 EmitObjCSuperPropertySet(E, S, Src);
394 return;
395 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000396 CallArgList Args;
397 Args.push_back(std::make_pair(Src, E->getType()));
398 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
399 EmitScalarExpr(E->getBase()),
400 false, Args);
401 }
402 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
403 Selector S = E->getSetterMethod()->getSelector();
404 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000405 llvm::Value *Receiver;
406 if (E->getClassProp()) {
407 const ObjCInterfaceDecl *OID = E->getClassProp();
408 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
409 }
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000410 else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000411 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000412 return;
413 }
414 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000415 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000416 Args.push_back(std::make_pair(Src, E->getType()));
417 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000418 Receiver,
419 E->getClassProp() != 0, Args);
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000420 }
421 else
422 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000423}
424
Chris Lattner74391b42009-03-22 21:03:39 +0000425void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
426 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000427 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000428 llvm::Value *DeclAddress;
429 QualType ElementTy;
430
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000431 if (!EnumerationMutationFn) {
432 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
433 return;
434 }
435
Anders Carlssonf484c312008-08-31 02:33:12 +0000436 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
437 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000438 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000439 const Decl* D = SD->getSolitaryDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000440 ElementTy = cast<ValueDecl>(D)->getType();
441 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000442 } else {
443 ElementTy = cast<Expr>(S.getElement())->getType();
444 DeclAddress = 0;
445 }
446
447 // Fast enumeration state.
448 QualType StateTy = getContext().getObjCFastEnumerationStateType();
449 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
450 "state.ptr");
451 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000452 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000453
454 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000455 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000456
457 // Get selector
458 llvm::SmallVector<IdentifierInfo*, 3> II;
459 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
460 II.push_back(&CGM.getContext().Idents.get("objects"));
461 II.push_back(&CGM.getContext().Idents.get("count"));
462 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
463 &II[0]);
464
465 QualType ItemsTy =
466 getContext().getConstantArrayType(getContext().getObjCIdType(),
467 llvm::APInt(32, NumItems),
468 ArrayType::Normal, 0);
469 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
470
471 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
472
473 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000474 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000475 getContext().getPointerType(StateTy)));
476
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000477 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000478 getContext().getPointerType(ItemsTy)));
479
480 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
481 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000482 Args.push_back(std::make_pair(RValue::get(Count),
483 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000484
485 RValue CountRV =
486 CGM.getObjCRuntime().GenerateMessageSend(*this,
487 getContext().UnsignedLongTy,
488 FastEnumSel,
489 Collection, false, Args);
490
491 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
492 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
493
Daniel Dunbar55e87422008-11-11 02:29:29 +0000494 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
495 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000496
497 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
498 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
499
500 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000501 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000502
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000503 EmitBlock(SetStartMutations);
504
505 llvm::Value *StartMutationsPtr =
506 CreateTempAlloca(UnsignedLongLTy);
507
508 llvm::Value *StateMutationsPtrPtr =
509 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
510 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
511 "mutationsptr");
512
513 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
514 "mutations");
515
516 Builder.CreateStore(StateMutations, StartMutationsPtr);
517
Daniel Dunbar55e87422008-11-11 02:29:29 +0000518 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000519 EmitBlock(LoopStart);
520
Anders Carlssonf484c312008-08-31 02:33:12 +0000521 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
522 Builder.CreateStore(Zero, CounterPtr);
523
Daniel Dunbar55e87422008-11-11 02:29:29 +0000524 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000525 EmitBlock(LoopBody);
526
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000527 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
528 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
529
530 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
531 "mutations");
532 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
533 StartMutations,
534 "tobool");
535
536
Daniel Dunbar55e87422008-11-11 02:29:29 +0000537 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
538 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000539
540 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
541
542 EmitBlock(WasMutated);
543 llvm::Value *V =
544 Builder.CreateBitCast(Collection,
545 ConvertType(getContext().getObjCIdType()),
546 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000547 CallArgList Args2;
548 Args2.push_back(std::make_pair(RValue::get(V),
549 getContext().getObjCIdType()));
550 // FIXME: We shouldn't need to get the function info here, the
551 // runtime already should have computed it to build the function.
Daniel Dunbar90350b62009-02-04 22:00:33 +0000552 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000553 EnumerationMutationFn, Args2);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000554
555 EmitBlock(WasNotMutated);
556
Anders Carlssonf484c312008-08-31 02:33:12 +0000557 llvm::Value *StateItemsPtr =
558 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
559
560 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
561
562 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
563 "stateitems");
564
565 llvm::Value *CurrentItemPtr =
566 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
567
568 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
569
570 // Cast the item to the right type.
571 CurrentItem = Builder.CreateBitCast(CurrentItem,
572 ConvertType(ElementTy), "tmp");
573
574 if (!DeclAddress) {
575 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
576
577 // Set the value to null.
578 Builder.CreateStore(CurrentItem, LV.getAddress());
579 } else
580 Builder.CreateStore(CurrentItem, DeclAddress);
581
582 // Increment the counter.
583 Counter = Builder.CreateAdd(Counter,
584 llvm::ConstantInt::get(UnsignedLongLTy, 1));
585 Builder.CreateStore(Counter, CounterPtr);
586
Daniel Dunbar55e87422008-11-11 02:29:29 +0000587 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
588 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000589
Anders Carlssone4b6d342009-02-10 05:52:02 +0000590 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000591
592 EmitStmt(S.getBody());
593
594 BreakContinueStack.pop_back();
595
596 EmitBlock(AfterBody);
597
Daniel Dunbar55e87422008-11-11 02:29:29 +0000598 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000599
600 Counter = Builder.CreateLoad(CounterPtr);
601 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000602 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000603 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000604
605 // Fetch more elements.
606 EmitBlock(FetchMore);
607
608 CountRV =
609 CGM.getObjCRuntime().GenerateMessageSend(*this,
610 getContext().UnsignedLongTy,
611 FastEnumSel,
612 Collection, false, Args);
613 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
614 Limit = Builder.CreateLoad(LimitPtr);
615
616 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
617 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
618
619 // No more elements.
620 EmitBlock(NoElements);
621
622 if (!DeclAddress) {
623 // If the element was not a declaration, set it to be null.
624
625 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
626
627 // Set the value to null.
628 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
629 LV.getAddress());
630 }
631
632 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000633}
634
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000635void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
636{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000637 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000638}
639
640void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
641{
642 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
643}
644
Chris Lattner10cac6f2008-11-15 21:26:17 +0000645void CodeGenFunction::EmitObjCAtSynchronizedStmt(
646 const ObjCAtSynchronizedStmt &S)
647{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000648 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000649}
650
Ted Kremenek2979ec72008-04-09 15:51:31 +0000651CGObjCRuntime::~CGObjCRuntime() {}