blob: 4423217c7dd2553530bba8e2b32b4bee46a19c17 [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
Anders Carlsson782f3972009-04-08 23:13:16 +000082 // FIXME: This should use EmitCallArgs.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000083 CallArgList Args;
84 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
85 i != e; ++i)
Daniel Dunbar46f45b92008-09-09 01:06:48 +000086 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000087
Chris Lattner8fdf3282008-06-24 17:04:18 +000088 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000089 // super is only valid in an Objective-C method
90 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000091 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000092 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
93 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000094 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000095 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000096 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000097 isClassMessage,
98 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000099 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000100 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
101 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +0000102}
103
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000104/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
105/// the LLVM function and sets the other context used by
106/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000107void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
108 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000109 FunctionArgList Args;
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000110 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000111
Daniel Dunbar7c086512008-09-09 23:14:03 +0000112 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000113
Daniel Dunbar7c086512008-09-09 23:14:03 +0000114 Args.push_back(std::make_pair(OMD->getSelfDecl(),
115 OMD->getSelfDecl()->getType()));
116 Args.push_back(std::make_pair(OMD->getCmdDecl(),
117 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000118
Chris Lattner89951a82009-02-20 18:43:26 +0000119 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
120 E = OMD->param_end(); PI != E; ++PI)
121 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000122
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000123 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000124}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000125
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000126/// Generate an Objective-C method. An Objective-C method is a C function with
127/// its pointer, name, and types registered in the class struture.
128void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Devang Patel1d6a4512009-02-25 01:09:46 +0000129 // Check if we should generate debug info for this method.
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +0000130 if (CGM.getDebugInfo() && !OMD->hasAttr<NodebugAttr>())
Devang Patel1d6a4512009-02-25 01:09:46 +0000131 DebugInfo = CGM.getDebugInfo();
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000132 StartObjCMethod(OMD, OMD->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000133 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000134 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000135}
136
137// FIXME: I wasn't sure about the synthesis approach. If we end up
138// generating an AST for the whole body we can just fall back to
139// having a GenerateFunction which takes the body Stmt.
140
141/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000142/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
143/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000144void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
145 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000146 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000147 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
148 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
149 assert(OMD && "Invalid call to generate getter (empty method)");
150 // FIXME: This is rather murky, we create this here since they will
151 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000152 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000153 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000154
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000155 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000156 // this. Non-atomic properties are directly evaluated.
157 // atomic 'copy' and 'retain' properties are also directly
158 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000159 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000160 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
161 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
162 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000163 llvm::Value *GetPropertyFn =
164 CGM.getObjCRuntime().GetPropertyGetFunction();
165
166 if (!GetPropertyFn) {
167 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
168 FinishFunction();
169 return;
170 }
171
172 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
173 // FIXME: Can't this be simpler? This might even be worse than the
174 // corresponding gcc code.
175 CodeGenTypes &Types = CGM.getTypes();
176 ValueDecl *Cmd = OMD->getCmdDecl();
177 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
178 QualType IdTy = getContext().getObjCIdType();
179 llvm::Value *SelfAsId =
180 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000181 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000182 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000183 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000184 CallArgList Args;
185 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
186 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
187 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
188 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000189 // FIXME: We shouldn't need to get the function info here, the
190 // runtime already should have computed it to build the function.
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000191 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
Daniel Dunbar88b53962009-02-02 22:03:45 +0000192 GetPropertyFn, Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000193 // We need to fix the type here. Ivars with copy & retain are
194 // always objects so we don't need to worry about complex or
195 // aggregates.
196 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
197 Types.ConvertType(PD->getType())));
198 EmitReturnOfRValue(RV, PD->getType());
199 } else {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000200 FieldDecl *Field =
201 IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000202 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
203 LoadObjCSelf(), Ivar, Field, 0);
Fariborz Jahanian6010bca2008-11-26 22:36:09 +0000204 if (hasAggregateLLVMType(Ivar->getType())) {
205 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
206 }
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000207 else {
208 CodeGenTypes &Types = CGM.getTypes();
209 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
210 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
211 Types.ConvertType(PD->getType())));
212 EmitReturnOfRValue(RV, PD->getType());
213 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000214 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000215
216 FinishFunction();
217}
218
219/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000220/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
221/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000222void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
223 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000224 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000225 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
226 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
227 assert(OMD && "Invalid call to generate setter (empty method)");
228 // FIXME: This is rather murky, we create this here since they will
229 // not have been created by Sema for us.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000230 OMD->createImplicitParams(getContext(), IMP->getClassInterface());
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000231 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000232
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000233 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
234 bool IsAtomic =
235 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
236
237 // Determine if we should use an objc_setProperty call for
238 // this. Properties with 'copy' semantics always use it, as do
239 // non-atomic properties with 'release' semantics as long as we are
240 // not in gc-only mode.
241 if (IsCopy ||
242 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
243 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
244 llvm::Value *SetPropertyFn =
245 CGM.getObjCRuntime().GetPropertySetFunction();
246
247 if (!SetPropertyFn) {
248 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
249 FinishFunction();
250 return;
251 }
252
253 // Emit objc_setProperty((id) self, _cmd, offset, arg,
254 // <is-atomic>, <is-copy>).
255 // FIXME: Can't this be simpler? This might even be worse than the
256 // corresponding gcc code.
257 CodeGenTypes &Types = CGM.getTypes();
258 ValueDecl *Cmd = OMD->getCmdDecl();
259 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
260 QualType IdTy = getContext().getObjCIdType();
261 llvm::Value *SelfAsId =
262 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000263 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000264 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000265 llvm::Value *ArgAsId =
266 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
267 Types.ConvertType(IdTy));
268 llvm::Value *True =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000269 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000270 llvm::Value *False =
Daniel Dunbarbe395f62009-02-04 00:55:44 +0000271 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000272 CallArgList Args;
273 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
274 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
275 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
276 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
277 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
278 getContext().BoolTy));
279 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
280 getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000281 // FIXME: We shouldn't need to get the function info here, the
282 // runtime already should have computed it to build the function.
283 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000284 SetPropertyFn, Args);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000285 } else {
286 SourceLocation Loc = PD->getLocation();
287 ValueDecl *Self = OMD->getSelfDecl();
288 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
289 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000290 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000291 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Fariborz Jahanianaaa63a72008-12-13 22:20:28 +0000292 ObjCInterfaceDecl *OI = IMP->getClassInterface();
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000293 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000294 true, true);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000295 getContext().setFieldDecl(OI, Ivar, &IvarRef);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000296 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
297 Ivar->getType(), Loc);
298 EmitStmt(&Assign);
299 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000300
301 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000302}
303
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000304llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000305 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Mike Stump6cc88f72009-03-20 21:53:12 +0000306 // See if we need to lazily forward self inside a block literal.
307 BlockForwardSelf();
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000308 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000309}
310
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000311QualType CodeGenFunction::TypeOfSelfObject() {
312 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
313 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
314 const PointerType *PTy =
315 cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
316 return PTy->getPointeeType();
317}
318
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000319RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
320 const Selector &S) {
321 llvm::Value *Receiver = LoadObjCSelf();
322 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
323 bool isClassMessage = OMD->isClassMethod();
324 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
325 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
326 Exp->getType(),
327 S,
328 OMD->getClassInterface(),
329 isCategoryImpl,
330 Receiver,
331 isClassMessage,
332 CallArgList());
333
334}
335
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000336RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000337 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000338 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
339 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000340 if (isa<ObjCSuperExpr>(E->getBase()))
341 return EmitObjCSuperPropertyGet(E, S);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000342 return CGM.getObjCRuntime().
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000343 GenerateMessageSend(*this, Exp->getType(), S,
344 EmitScalarExpr(E->getBase()),
345 false, CallArgList());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000346 }
Daniel Dunbarf1853192009-01-15 18:32:35 +0000347 else {
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000348 const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp);
349 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000350 llvm::Value *Receiver;
351 if (KE->getClassProp()) {
352 const ObjCInterfaceDecl *OID = KE->getClassProp();
353 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
354 }
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000355 else if (isa<ObjCSuperExpr>(KE->getBase()))
356 return EmitObjCSuperPropertyGet(KE, S);
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000357 else
358 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000359 return CGM.getObjCRuntime().
360 GenerateMessageSend(*this, Exp->getType(), S,
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000361 Receiver,
362 KE->getClassProp() != 0, CallArgList());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000363 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000364}
365
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000366void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
367 const Selector &S,
368 RValue Src) {
369 CallArgList Args;
370 llvm::Value *Receiver = LoadObjCSelf();
371 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
372 bool isClassMessage = OMD->isClassMethod();
373 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
374 Args.push_back(std::make_pair(Src, Exp->getType()));
375 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
376 Exp->getType(),
377 S,
378 OMD->getClassInterface(),
379 isCategoryImpl,
380 Receiver,
381 isClassMessage,
382 Args);
383 return;
384}
385
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000386void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000387 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000388 // FIXME: Split it into two separate routines.
389 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
390 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000391 if (isa<ObjCSuperExpr>(E->getBase())) {
392 EmitObjCSuperPropertySet(E, S, Src);
393 return;
394 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000395 CallArgList Args;
396 Args.push_back(std::make_pair(Src, E->getType()));
397 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
398 EmitScalarExpr(E->getBase()),
399 false, Args);
400 }
401 else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
402 Selector S = E->getSetterMethod()->getSelector();
403 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000404 llvm::Value *Receiver;
405 if (E->getClassProp()) {
406 const ObjCInterfaceDecl *OID = E->getClassProp();
407 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
408 }
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000409 else if (isa<ObjCSuperExpr>(E->getBase())) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000410 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000411 return;
412 }
413 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000414 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000415 Args.push_back(std::make_pair(Src, E->getType()));
416 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000417 Receiver,
418 E->getClassProp() != 0, Args);
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000419 }
420 else
421 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000422}
423
Chris Lattner74391b42009-03-22 21:03:39 +0000424void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
425 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000426 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000427 llvm::Value *DeclAddress;
428 QualType ElementTy;
429
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000430 if (!EnumerationMutationFn) {
431 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
432 return;
433 }
434
Anders Carlssonf484c312008-08-31 02:33:12 +0000435 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
436 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000437 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000438 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000439 ElementTy = cast<ValueDecl>(D)->getType();
440 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000441 } else {
442 ElementTy = cast<Expr>(S.getElement())->getType();
443 DeclAddress = 0;
444 }
445
446 // Fast enumeration state.
447 QualType StateTy = getContext().getObjCFastEnumerationStateType();
448 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
449 "state.ptr");
450 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000451 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000452
453 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000454 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000455
456 // Get selector
457 llvm::SmallVector<IdentifierInfo*, 3> II;
458 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
459 II.push_back(&CGM.getContext().Idents.get("objects"));
460 II.push_back(&CGM.getContext().Idents.get("count"));
461 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
462 &II[0]);
463
464 QualType ItemsTy =
465 getContext().getConstantArrayType(getContext().getObjCIdType(),
466 llvm::APInt(32, NumItems),
467 ArrayType::Normal, 0);
468 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
469
470 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
471
472 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000473 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000474 getContext().getPointerType(StateTy)));
475
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000476 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000477 getContext().getPointerType(ItemsTy)));
478
479 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
480 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000481 Args.push_back(std::make_pair(RValue::get(Count),
482 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000483
484 RValue CountRV =
485 CGM.getObjCRuntime().GenerateMessageSend(*this,
486 getContext().UnsignedLongTy,
487 FastEnumSel,
488 Collection, false, Args);
489
490 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
491 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
492
Daniel Dunbar55e87422008-11-11 02:29:29 +0000493 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
494 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000495
496 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
497 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
498
499 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000500 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000501
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000502 EmitBlock(SetStartMutations);
503
504 llvm::Value *StartMutationsPtr =
505 CreateTempAlloca(UnsignedLongLTy);
506
507 llvm::Value *StateMutationsPtrPtr =
508 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
509 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
510 "mutationsptr");
511
512 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
513 "mutations");
514
515 Builder.CreateStore(StateMutations, StartMutationsPtr);
516
Daniel Dunbar55e87422008-11-11 02:29:29 +0000517 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000518 EmitBlock(LoopStart);
519
Anders Carlssonf484c312008-08-31 02:33:12 +0000520 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
521 Builder.CreateStore(Zero, CounterPtr);
522
Daniel Dunbar55e87422008-11-11 02:29:29 +0000523 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000524 EmitBlock(LoopBody);
525
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000526 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
527 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
528
529 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
530 "mutations");
531 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
532 StartMutations,
533 "tobool");
534
535
Daniel Dunbar55e87422008-11-11 02:29:29 +0000536 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
537 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000538
539 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
540
541 EmitBlock(WasMutated);
542 llvm::Value *V =
543 Builder.CreateBitCast(Collection,
544 ConvertType(getContext().getObjCIdType()),
545 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000546 CallArgList Args2;
547 Args2.push_back(std::make_pair(RValue::get(V),
548 getContext().getObjCIdType()));
549 // FIXME: We shouldn't need to get the function info here, the
550 // runtime already should have computed it to build the function.
Daniel Dunbar90350b62009-02-04 22:00:33 +0000551 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000552 EnumerationMutationFn, Args2);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000553
554 EmitBlock(WasNotMutated);
555
Anders Carlssonf484c312008-08-31 02:33:12 +0000556 llvm::Value *StateItemsPtr =
557 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
558
559 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
560
561 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
562 "stateitems");
563
564 llvm::Value *CurrentItemPtr =
565 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
566
567 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
568
569 // Cast the item to the right type.
570 CurrentItem = Builder.CreateBitCast(CurrentItem,
571 ConvertType(ElementTy), "tmp");
572
573 if (!DeclAddress) {
574 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
575
576 // Set the value to null.
577 Builder.CreateStore(CurrentItem, LV.getAddress());
578 } else
579 Builder.CreateStore(CurrentItem, DeclAddress);
580
581 // Increment the counter.
582 Counter = Builder.CreateAdd(Counter,
583 llvm::ConstantInt::get(UnsignedLongLTy, 1));
584 Builder.CreateStore(Counter, CounterPtr);
585
Daniel Dunbar55e87422008-11-11 02:29:29 +0000586 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
587 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000588
Anders Carlssone4b6d342009-02-10 05:52:02 +0000589 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000590
591 EmitStmt(S.getBody());
592
593 BreakContinueStack.pop_back();
594
595 EmitBlock(AfterBody);
596
Daniel Dunbar55e87422008-11-11 02:29:29 +0000597 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000598
599 Counter = Builder.CreateLoad(CounterPtr);
600 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000601 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000602 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000603
604 // Fetch more elements.
605 EmitBlock(FetchMore);
606
607 CountRV =
608 CGM.getObjCRuntime().GenerateMessageSend(*this,
609 getContext().UnsignedLongTy,
610 FastEnumSel,
611 Collection, false, Args);
612 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
613 Limit = Builder.CreateLoad(LimitPtr);
614
615 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
616 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
617
618 // No more elements.
619 EmitBlock(NoElements);
620
621 if (!DeclAddress) {
622 // If the element was not a declaration, set it to be null.
623
624 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
625
626 // Set the value to null.
627 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
628 LV.getAddress());
629 }
630
631 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000632}
633
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000634void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
635{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000636 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000637}
638
639void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
640{
641 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
642}
643
Chris Lattner10cac6f2008-11-15 21:26:17 +0000644void CodeGenFunction::EmitObjCAtSynchronizedStmt(
645 const ObjCAtSynchronizedStmt &S)
646{
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000647 CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000648}
649
Ted Kremenek2979ec72008-04-09 15:51:31 +0000650CGObjCRuntime::~CGObjCRuntime() {}