blob: 4db29ce05d08df6ce63e274d549a490e48ba7038 [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{
Steve Naroff33fdb732009-03-31 16:53:37 +000029 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(E);
Daniel Dunbared7c6182008-08-20 00:28:19 +000030 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-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 Dunbar208ff5e2008-08-11 18:12:00 +000040 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000041}
42
Daniel Dunbared7c6182008-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 Lattner8fdf3282008-06-24 17:04:18 +000047
48
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000049RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-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 Dunbar208ff5e2008-08-11 18:12:00 +000054 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000055 const Expr *ReceiverExpr = E->getReceiver();
56 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000057 bool isClassMessage = false;
Chris Lattner8fdf3282008-06-24 17:04:18 +000058 // Find the receiver
59 llvm::Value *Receiver;
60 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-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 Lattner92e62b02008-11-20 04:42:34 +000066 assert(E->getClassName()->isStr("super") &&
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000067 "Unexpected missing class interface in message send.");
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000068 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000069 Receiver = LoadObjCSelf();
70 } else {
71 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000072 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +000073
74 isClassMessage = true;
Douglas Gregorcd9b46e2008-11-04 14:56:14 +000075 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000076 isSuperMessage = true;
77 Receiver = LoadObjCSelf();
78 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000079 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000080 }
81
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000082 CallArgList Args;
83 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
84 i != e; ++i)
Daniel Dunbar46f45b92008-09-09 01:06:48 +000085 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000086
Chris Lattner8fdf3282008-06-24 17:04:18 +000087 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000088 // super is only valid in an Objective-C method
89 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000090 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000091 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
92 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000093 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000094 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000095 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000096 isClassMessage,
97 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000098 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000099 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
100 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +0000101}
102
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000103/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
104/// the LLVM function and sets the other context used by
105/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000106void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
107 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000108 FunctionArgList Args;
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000109 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000110
Daniel Dunbar7c086512008-09-09 23:14:03 +0000111 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000112
Daniel Dunbar7c086512008-09-09 23:14:03 +0000113 Args.push_back(std::make_pair(OMD->getSelfDecl(),
114 OMD->getSelfDecl()->getType()));
115 Args.push_back(std::make_pair(OMD->getCmdDecl(),
116 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000117
Chris Lattner89951a82009-02-20 18:43:26 +0000118 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
119 E = OMD->param_end(); PI != E; ++PI)
120 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000121
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000122 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000123}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000124
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000125/// Generate an Objective-C method. An Objective-C method is a C function with
126/// its pointer, name, and types registered in the class struture.
127void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel1d6a4512009-02-25 01:09:46 +0000128 // Check if we should generate debug info for this method.
129 if (CGM.getDebugInfo() && !OMD->getAttr<NodebugAttr>())
130 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000131 StartObjCMethod(OMD, OMD->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000132 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000133 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000134}
135
136// FIXME: I wasn't sure about the synthesis approach. If we end up
137// generating an AST for the whole body we can just fall back to
138// having a GenerateFunction which takes the body Stmt.
139
140/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000141/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
142/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000143void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
144 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000145 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000146 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
147 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
148 assert(OMD && "Invalid call to generate getter (empty method)");
149 // FIXME: This is rather murky, we create this here since they will
150 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000151 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000152 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000153
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000154 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000155 // this. Non-atomic properties are directly evaluated.
156 // atomic 'copy' and 'retain' properties are also directly
157 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000158 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000159 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
160 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
161 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000162 llvm::Value *GetPropertyFn =
163 CGM.getObjCRuntime().GetPropertyGetFunction();
164
165 if (!GetPropertyFn) {
166 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
167 FinishFunction();
168 return;
169 }
170
171 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
172 // FIXME: Can't this be simpler? This might even be worse than the
173 // corresponding gcc code.
174 CodeGenTypes &Types = CGM.getTypes();
175 ValueDecl *Cmd = OMD->getCmdDecl();
176 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
177 QualType IdTy = getContext().getObjCIdType();
178 llvm::Value *SelfAsId =
179 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000180 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000181 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000182 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000183 CallArgList Args;
184 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
185 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
186 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
187 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000188 // FIXME: We shouldn't need to get the function info here, the
189 // runtime already should have computed it to build the function.
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000190 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar88b53962009-02-02 22:03:45 +0000191 GetPropertyFn, Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000192 // We need to fix the type here. Ivars with copy & retain are
193 // always objects so we don't need to worry about complex or
194 // aggregates.
195 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
196 Types.ConvertType(PD->getType())));
197 EmitReturnOfRValue(RV, PD->getType());
198 } else {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000199 FieldDecl *Field =
200 IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000201 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
202 LoadObjCSelf(), Ivar, Field, 0);
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000203 if (hasAggregateLLVMType(Ivar->getType())) {
204 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
205 }
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000206 else {
207 CodeGenTypes &Types = CGM.getTypes();
208 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
209 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
210 Types.ConvertType(PD->getType())));
211 EmitReturnOfRValue(RV, PD->getType());
212 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000213 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000214
215 FinishFunction();
216}
217
218/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000219/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
220/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000221void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
222 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000223 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000224 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
225 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
226 assert(OMD && "Invalid call to generate setter (empty method)");
227 // FIXME: This is rather murky, we create this here since they will
228 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000229 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000230 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000231
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000232 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
233 bool IsAtomic =
234 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
235
236 // Determine if we should use an objc_setProperty call for
237 // this. Properties with 'copy' semantics always use it, as do
238 // non-atomic properties with 'release' semantics as long as we are
239 // not in gc-only mode.
240 if (IsCopy ||
241 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
242 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
243 llvm::Value *SetPropertyFn =
244 CGM.getObjCRuntime().GetPropertySetFunction();
245
246 if (!SetPropertyFn) {
247 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
248 FinishFunction();
249 return;
250 }
251
252 // Emit objc_setProperty((id) self, _cmd, offset, arg,
253 // <is-atomic>, <is-copy>).
254 // FIXME: Can't this be simpler? This might even be worse than the
255 // corresponding gcc code.
256 CodeGenTypes &Types = CGM.getTypes();
257 ValueDecl *Cmd = OMD->getCmdDecl();
258 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
259 QualType IdTy = getContext().getObjCIdType();
260 llvm::Value *SelfAsId =
261 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000262 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000263 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000264 llvm::Value *ArgAsId =
265 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
266 Types.ConvertType(IdTy));
267 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000268 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000269 llvm::Value *False =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000270 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000271 CallArgList Args;
272 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
273 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
274 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
275 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
276 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
277 getContext().BoolTy));
278 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
279 getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000280 // FIXME: We shouldn't need to get the function info here, the
281 // runtime already should have computed it to build the function.
282 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000283 SetPropertyFn, Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000284 } else {
285 SourceLocation Loc = PD->getLocation();
286 ValueDecl *Self = OMD->getSelfDecl();
287 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
288 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000289 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000290 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanianaaa63a72008-12-13 22:20:28 +0000291 ObjCInterfaceDecl *OI = IMP->getClassInterface();
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000292 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000293 true, true);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000294 getContext().setFieldDecl(OI, Ivar, &IvarRef);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000295 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
296 Ivar->getType(), Loc);
297 EmitStmt(&Assign);
298 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000299
300 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000301}
302
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000303llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000304 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000305 // See if we need to lazily forward self inside a block literal.
306 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000307 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000308}
309
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000310QualType CodeGenFunction::TypeOfSelfObject() {
311 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
312 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
313 const PointerType *PTy =
314 cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
315 return PTy->getPointeeType();
316}
317
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000318RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
319 const Selector &S) {
320 llvm::Value *Receiver = LoadObjCSelf();
321 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
322 bool isClassMessage = OMD->isClassMethod();
323 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
324 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
325 Exp->getType(),
326 S,
327 OMD->getClassInterface(),
328 isCategoryImpl,
329 Receiver,
330 isClassMessage,
331 CallArgList());
332
333}
334
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000335RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000336 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000337 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
338 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000339 if (isa<ObjCSuperExpr>(E->getBase()))
340 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000341 return CGM.getObjCRuntime().
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000342 GenerateMessageSend(*this, Exp->getType(), S,
343 EmitScalarExpr(E->getBase()),
344 false, CallArgList());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000345 }
Daniel Dunbarf1853192009-01-15 18:32:35 +0000346 else {
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000347 const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp);
348 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000349 llvm::Value *Receiver;
350 if (KE->getClassProp()) {
351 const ObjCInterfaceDecl *OID = KE->getClassProp();
352 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
353 }
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000354 else if (isa<ObjCSuperExpr>(KE->getBase()))
355 return EmitObjCSuperPropertyGet(KE, S);
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000356 else
357 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000358 return CGM.getObjCRuntime().
359 GenerateMessageSend(*this, Exp->getType(), S,
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000360 Receiver,
361 KE->getClassProp() != 0, CallArgList());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000362 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000363}
364
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000365void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
366 const Selector &S,
367 RValue Src) {
368 CallArgList Args;
369 llvm::Value *Receiver = LoadObjCSelf();
370 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
371 bool isClassMessage = OMD->isClassMethod();
372 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
373 Args.push_back(std::make_pair(Src, Exp->getType()));
374 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
375 Exp->getType(),
376 S,
377 OMD->getClassInterface(),
378 isCategoryImpl,
379 Receiver,
380 isClassMessage,
381 Args);
382 return;
383}
384
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000385void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000386 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000387 // FIXME: Split it into two separate routines.
388 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
389 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000390 if (isa<ObjCSuperExpr>(E->getBase())) {
391 EmitObjCSuperPropertySet(E, S, Src);
392 return;
393 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000394 CallArgList Args;
395 Args.push_back(std::make_pair(Src, E->getType()));
396 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
397 EmitScalarExpr(E->getBase()),
398 false, Args);
399 }
400 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
401 Selector S = E->getSetterMethod()->getSelector();
402 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000403 llvm::Value *Receiver;
404 if (E->getClassProp()) {
405 const ObjCInterfaceDecl *OID = E->getClassProp();
406 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
407 }
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000408 else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000409 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000410 return;
411 }
412 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000413 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000414 Args.push_back(std::make_pair(Src, E->getType()));
415 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000416 Receiver,
417 E->getClassProp() != 0, Args);
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000418 }
419 else
420 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000421}
422
Chris Lattner74391b42009-03-22 21:03:39 +0000423void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
424 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000425 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000426 llvm::Value *DeclAddress;
427 QualType ElementTy;
428
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000429 if (!EnumerationMutationFn) {
430 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
431 return;
432 }
433
Anders Carlssonf484c312008-08-31 02:33:12 +0000434 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
435 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000436 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000437 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000438 ElementTy = cast<ValueDecl>(D)->getType();
439 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000440 } else {
441 ElementTy = cast<Expr>(S.getElement())->getType();
442 DeclAddress = 0;
443 }
444
445 // Fast enumeration state.
446 QualType StateTy = getContext().getObjCFastEnumerationStateType();
447 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
448 "state.ptr");
449 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000450 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000451
452 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000453 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000454
455 // Get selector
456 llvm::SmallVector<IdentifierInfo*, 3> II;
457 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
458 II.push_back(&CGM.getContext().Idents.get("objects"));
459 II.push_back(&CGM.getContext().Idents.get("count"));
460 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
461 &II[0]);
462
463 QualType ItemsTy =
464 getContext().getConstantArrayType(getContext().getObjCIdType(),
465 llvm::APInt(32, NumItems),
466 ArrayType::Normal, 0);
467 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
468
469 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
470
471 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000472 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000473 getContext().getPointerType(StateTy)));
474
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000475 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000476 getContext().getPointerType(ItemsTy)));
477
478 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
479 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000480 Args.push_back(std::make_pair(RValue::get(Count),
481 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000482
483 RValue CountRV =
484 CGM.getObjCRuntime().GenerateMessageSend(*this,
485 getContext().UnsignedLongTy,
486 FastEnumSel,
487 Collection, false, Args);
488
489 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
490 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
491
Daniel Dunbar55e87422008-11-11 02:29:29 +0000492 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
493 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000494
495 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
496 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
497
498 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000499 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000500
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000501 EmitBlock(SetStartMutations);
502
503 llvm::Value *StartMutationsPtr =
504 CreateTempAlloca(UnsignedLongLTy);
505
506 llvm::Value *StateMutationsPtrPtr =
507 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
508 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
509 "mutationsptr");
510
511 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
512 "mutations");
513
514 Builder.CreateStore(StateMutations, StartMutationsPtr);
515
Daniel Dunbar55e87422008-11-11 02:29:29 +0000516 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000517 EmitBlock(LoopStart);
518
Anders Carlssonf484c312008-08-31 02:33:12 +0000519 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
520 Builder.CreateStore(Zero, CounterPtr);
521
Daniel Dunbar55e87422008-11-11 02:29:29 +0000522 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000523 EmitBlock(LoopBody);
524
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000525 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
526 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
527
528 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
529 "mutations");
530 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
531 StartMutations,
532 "tobool");
533
534
Daniel Dunbar55e87422008-11-11 02:29:29 +0000535 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
536 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000537
538 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
539
540 EmitBlock(WasMutated);
541 llvm::Value *V =
542 Builder.CreateBitCast(Collection,
543 ConvertType(getContext().getObjCIdType()),
544 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000545 CallArgList Args2;
546 Args2.push_back(std::make_pair(RValue::get(V),
547 getContext().getObjCIdType()));
548 // FIXME: We shouldn't need to get the function info here, the
549 // runtime already should have computed it to build the function.
Daniel Dunbar90350b62009-02-04 22:00:33 +0000550 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000551 EnumerationMutationFn, Args2);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000552
553 EmitBlock(WasNotMutated);
554
Anders Carlssonf484c312008-08-31 02:33:12 +0000555 llvm::Value *StateItemsPtr =
556 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
557
558 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
559
560 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
561 "stateitems");
562
563 llvm::Value *CurrentItemPtr =
564 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
565
566 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
567
568 // Cast the item to the right type.
569 CurrentItem = Builder.CreateBitCast(CurrentItem,
570 ConvertType(ElementTy), "tmp");
571
572 if (!DeclAddress) {
573 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
574
575 // Set the value to null.
576 Builder.CreateStore(CurrentItem, LV.getAddress());
577 } else
578 Builder.CreateStore(CurrentItem, DeclAddress);
579
580 // Increment the counter.
581 Counter = Builder.CreateAdd(Counter,
582 llvm::ConstantInt::get(UnsignedLongLTy, 1));
583 Builder.CreateStore(Counter, CounterPtr);
584
Daniel Dunbar55e87422008-11-11 02:29:29 +0000585 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
586 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000587
Anders Carlssone4b6d342009-02-10 05:52:02 +0000588 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000589
590 EmitStmt(S.getBody());
591
592 BreakContinueStack.pop_back();
593
594 EmitBlock(AfterBody);
595
Daniel Dunbar55e87422008-11-11 02:29:29 +0000596 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000597
598 Counter = Builder.CreateLoad(CounterPtr);
599 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000600 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000601 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000602
603 // Fetch more elements.
604 EmitBlock(FetchMore);
605
606 CountRV =
607 CGM.getObjCRuntime().GenerateMessageSend(*this,
608 getContext().UnsignedLongTy,
609 FastEnumSel,
610 Collection, false, Args);
611 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
612 Limit = Builder.CreateLoad(LimitPtr);
613
614 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
615 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
616
617 // No more elements.
618 EmitBlock(NoElements);
619
620 if (!DeclAddress) {
621 // If the element was not a declaration, set it to be null.
622
623 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
624
625 // Set the value to null.
626 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
627 LV.getAddress());
628 }
629
630 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000631}
632
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000633void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
634{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000635 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000636}
637
638void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
639{
640 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
641}
642
Chris Lattner10cac6f2008-11-15 21:26:17 +0000643void CodeGenFunction::EmitObjCAtSynchronizedStmt(
644 const ObjCAtSynchronizedStmt &S)
645{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000646 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000647}
648
Ted Kremenek2979ec72008-04-09 15:51:31 +0000649CGObjCRuntime::~CGObjCRuntime() {}