blob: 7c2dfe8b23b535dabd655b6c5cbd472d8f788529 [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"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/StmtObjC.h"
Daniel Dunbare66f4e32008-09-03 00:27:26 +000020#include "clang/Basic/Diagnostic.h"
Anders Carlsson3d8400d2008-08-30 19:51:14 +000021#include "llvm/ADT/STLExtras.h"
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +000022#include "llvm/Target/TargetData.h"
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.
Mike Stump1eb44332009-09-09 15:08:12 +000027llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
Daniel Dunbar71fcec92008-11-25 21:53:21 +000028{
David Chisnall0d13f6f2010-01-23 02:40:42 +000029 llvm::Constant *C =
30 CGM.getObjCRuntime().GenerateConstantString(E->getString());
Daniel Dunbared7c6182008-08-20 00:28:19 +000031 // FIXME: This bitcast should just be made an invariant on the Runtime.
Owen Anderson3c4972d2009-07-29 18:54:39 +000032 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-06-24 17:04:18 +000033}
34
35/// Emit a selector.
36llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
37 // Untyped selector.
38 // Note that this implementation allows for non-constant strings to be passed
39 // as arguments to @selector(). Currently, the only thing preventing this
40 // behaviour is the type checking in the front end.
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +000041 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000042}
43
Daniel Dunbared7c6182008-08-20 00:28:19 +000044llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
45 // FIXME: This should pass the Decl not the name.
46 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
47}
Chris Lattner8fdf3282008-06-24 17:04:18 +000048
49
John McCallef072fd2010-05-22 01:48:05 +000050RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
51 ReturnValueSlot Return) {
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.
Mike Stump1eb44332009-09-09 15:08:12 +000055
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000056 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000057 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000058 bool isClassMessage = false;
David Chisnallc6cd5fd2010-04-28 19:33:36 +000059 ObjCInterfaceDecl *OID = 0;
Chris Lattner8fdf3282008-06-24 17:04:18 +000060 // Find the receiver
Daniel Dunbar0b647a62010-04-22 03:17:06 +000061 llvm::Value *Receiver = 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +000062 switch (E->getReceiverKind()) {
63 case ObjCMessageExpr::Instance:
64 Receiver = EmitScalarExpr(E->getInstanceReceiver());
65 break;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000066
Douglas Gregor04badcf2010-04-21 00:45:42 +000067 case ObjCMessageExpr::Class: {
John McCall3031c632010-05-17 20:12:43 +000068 const ObjCObjectType *ObjTy
69 = E->getClassReceiver()->getAs<ObjCObjectType>();
70 assert(ObjTy && "Invalid Objective-C class message send");
71 OID = ObjTy->getInterface();
72 assert(OID && "Invalid Objective-C class message send");
David Chisnallc6cd5fd2010-04-28 19:33:36 +000073 Receiver = Runtime.GetClass(Builder, OID);
Daniel Dunbarf56f1912008-08-25 08:19:24 +000074 isClassMessage = true;
Douglas Gregor04badcf2010-04-21 00:45:42 +000075 break;
76 }
77
78 case ObjCMessageExpr::SuperInstance:
Chris Lattner8fdf3282008-06-24 17:04:18 +000079 Receiver = LoadObjCSelf();
Douglas Gregor04badcf2010-04-21 00:45:42 +000080 isSuperMessage = true;
81 break;
82
83 case ObjCMessageExpr::SuperClass:
84 Receiver = LoadObjCSelf();
85 isSuperMessage = true;
86 isClassMessage = true;
87 break;
Chris Lattner8fdf3282008-06-24 17:04:18 +000088 }
89
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000090 CallArgList Args;
Anders Carlsson131038e2009-04-18 20:29:27 +000091 EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +000092
Anders Carlsson7e70fb22010-06-21 20:59:55 +000093 QualType ResultType =
94 E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType();
95
Chris Lattner8fdf3282008-06-24 17:04:18 +000096 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000097 // super is only valid in an Objective-C method
98 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +000099 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Anders Carlsson7e70fb22010-06-21 20:59:55 +0000100 return Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000101 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000102 OMD->getClassInterface(),
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000103 isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000104 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000105 isClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000106 Args,
107 E->getMethodDecl());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000108 }
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000109
Anders Carlsson7e70fb22010-06-21 20:59:55 +0000110 return Runtime.GenerateMessageSend(*this, Return, ResultType,
John McCallef072fd2010-05-22 01:48:05 +0000111 E->getSelector(),
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000112 Receiver, Args, OID,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000113 E->getMethodDecl());
Anders Carlsson55085182007-08-21 17:43:55 +0000114}
115
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000116/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
117/// the LLVM function and sets the other context used by
118/// CodeGenFunction.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000119void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
120 const ObjCContainerDecl *CD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000121 FunctionArgList Args;
Devang Patel4800ea62010-04-05 21:09:15 +0000122 // Check if we should generate debug info for this method.
123 if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
124 DebugInfo = CGM.getDebugInfo();
125
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000126 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000127
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000128 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
129 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
Chris Lattner41110242008-06-17 18:05:57 +0000130
Mike Stump1eb44332009-09-09 15:08:12 +0000131 Args.push_back(std::make_pair(OMD->getSelfDecl(),
Daniel Dunbar7c086512008-09-09 23:14:03 +0000132 OMD->getSelfDecl()->getType()));
133 Args.push_back(std::make_pair(OMD->getCmdDecl(),
134 OMD->getCmdDecl()->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000135
Chris Lattner89951a82009-02-20 18:43:26 +0000136 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
137 E = OMD->param_end(); PI != E; ++PI)
138 Args.push_back(std::make_pair(*PI, (*PI)->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000139
Devang Patela92d6132010-02-15 18:08:38 +0000140 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000141}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000142
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000143/// Generate an Objective-C method. An Objective-C method is a C function with
Mike Stump1eb44332009-09-09 15:08:12 +0000144/// its pointer, name, and types registered in the class struture.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000145void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000146 StartObjCMethod(OMD, OMD->getClassInterface());
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000147 EmitStmt(OMD->getBody());
148 FinishFunction(OMD->getBodyRBrace());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000149}
150
Mike Stumpf5408fe2009-05-16 07:57:57 +0000151// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
152// AST for the whole body we can just fall back to having a GenerateFunction
153// which takes the body Stmt.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000154
155/// GenerateObjCGetter - Generate an Objective-C property getter
Steve Naroff489034c2009-01-10 22:55:25 +0000156/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
157/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000158void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
159 const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000160 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000161 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000162 bool IsAtomic =
163 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000164 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
165 assert(OMD && "Invalid call to generate getter (empty method)");
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000166 StartObjCMethod(OMD, IMP->getClassInterface());
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000167
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000168 // Determine if we should use an objc_getProperty call for
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000169 // this. Non-atomic properties are directly evaluated.
170 // atomic 'copy' and 'retain' properties are also directly
171 // evaluated in gc-only mode.
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000172 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000173 IsAtomic &&
Fariborz Jahanian447d7ae2008-12-08 23:56:17 +0000174 (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
175 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000176 llvm::Value *GetPropertyFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000177 CGM.getObjCRuntime().GetPropertyGetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000179 if (!GetPropertyFn) {
180 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
181 FinishFunction();
182 return;
183 }
184
185 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
186 // FIXME: Can't this be simpler? This might even be worse than the
187 // corresponding gcc code.
188 CodeGenTypes &Types = CGM.getTypes();
189 ValueDecl *Cmd = OMD->getCmdDecl();
190 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
191 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000192 llvm::Value *SelfAsId =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000193 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000194 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000195 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000196 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000197 CallArgList Args;
198 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
199 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
200 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
201 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
Daniel Dunbare4be5a62009-02-03 23:43:59 +0000202 // FIXME: We shouldn't need to get the function info here, the
203 // runtime already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000204 RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000205 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000206 GetPropertyFn, ReturnValueSlot(), Args);
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000207 // We need to fix the type here. Ivars with copy & retain are
208 // always objects so we don't need to worry about complex or
209 // aggregates.
Mike Stump1eb44332009-09-09 15:08:12 +0000210 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000211 Types.ConvertType(PD->getType())));
212 EmitReturnOfRValue(RV, PD->getType());
213 } else {
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000214 if (Ivar->getType()->isAnyComplexType()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000215 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
216 Ivar, 0);
Fariborz Jahanian1b23fe62010-03-25 21:56:43 +0000217 ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
218 LV.isVolatileQualified());
219 StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
220 }
221 else if (hasAggregateLLVMType(Ivar->getType())) {
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000222 bool IsStrong = false;
223 if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType())))
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000224 && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
225 && CGM.getObjCRuntime().GetCopyStructFunction()) {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000226 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
227 Ivar, 0);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000228 llvm::Value *GetCopyStructFn =
229 CGM.getObjCRuntime().GetCopyStructFunction();
230 CodeGenTypes &Types = CGM.getTypes();
231 // objc_copyStruct (ReturnValue, &structIvar,
232 // sizeof (Type of Ivar), isAtomic, false);
233 CallArgList Args;
234 RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
235 Types.ConvertType(getContext().VoidPtrTy)));
236 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
237 RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
238 Types.ConvertType(getContext().VoidPtrTy)));
239 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
240 // sizeof (Type of Ivar)
241 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
242 llvm::Value *SizeVal =
243 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
244 Args.push_back(std::make_pair(RValue::get(SizeVal),
245 getContext().LongTy));
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000246 llvm::Value *isAtomic =
Fariborz Jahanian08adf322010-04-13 18:43:37 +0000247 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
248 IsAtomic ? 1 : 0);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000249 Args.push_back(std::make_pair(RValue::get(isAtomic),
250 getContext().BoolTy));
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000251 llvm::Value *hasStrong =
252 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
253 IsStrong ? 1 : 0);
254 Args.push_back(std::make_pair(RValue::get(hasStrong),
255 getContext().BoolTy));
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000256 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
257 FunctionType::ExtInfo()),
258 GetCopyStructFn, ReturnValueSlot(), Args);
259 }
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000260 else {
261 if (PID->getGetterCXXConstructor()) {
262 ReturnStmt *Stmt =
263 new (getContext()) ReturnStmt(SourceLocation(),
Douglas Gregor5077c382010-05-15 06:01:05 +0000264 PID->getGetterCXXConstructor(),
265 0);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000266 EmitReturnStmt(*Stmt);
267 }
268 else {
269 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
270 Ivar, 0);
271 EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
272 }
273 }
Mike Stumpb3589f42009-07-30 22:28:39 +0000274 } else {
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000275 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
276 Ivar, 0);
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000277 CodeGenTypes &Types = CGM.getTypes();
278 RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
279 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
Mike Stump1eb44332009-09-09 15:08:12 +0000280 Types.ConvertType(PD->getType())));
Fariborz Jahanianed1d29d2009-03-03 18:49:40 +0000281 EmitReturnOfRValue(RV, PD->getType());
282 }
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000283 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000284
285 FinishFunction();
286}
287
288/// GenerateObjCSetter - Generate an Objective-C property setter
Steve Naroff489034c2009-01-10 22:55:25 +0000289/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
290/// is illegal within a category.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000291void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
292 const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000293 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000294 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
295 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
296 assert(OMD && "Invalid call to generate setter (empty method)");
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000297 StartObjCMethod(OMD, IMP->getClassInterface());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000298
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000299 bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
Mike Stump1eb44332009-09-09 15:08:12 +0000300 bool IsAtomic =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000301 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
302
303 // Determine if we should use an objc_setProperty call for
304 // this. Properties with 'copy' semantics always use it, as do
305 // non-atomic properties with 'release' semantics as long as we are
306 // not in gc-only mode.
307 if (IsCopy ||
308 (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
309 PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000310 llvm::Value *SetPropertyFn =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000311 CGM.getObjCRuntime().GetPropertySetFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000313 if (!SetPropertyFn) {
314 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
315 FinishFunction();
316 return;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
319 // Emit objc_setProperty((id) self, _cmd, offset, arg,
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000320 // <is-atomic>, <is-copy>).
321 // FIXME: Can't this be simpler? This might even be worse than the
322 // corresponding gcc code.
323 CodeGenTypes &Types = CGM.getTypes();
324 ValueDecl *Cmd = OMD->getCmdDecl();
325 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
326 QualType IdTy = getContext().getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +0000327 llvm::Value *SelfAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000328 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000329 llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
Chris Lattner89951a82009-02-20 18:43:26 +0000330 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
Mike Stump1eb44332009-09-09 15:08:12 +0000331 llvm::Value *ArgAsId =
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000332 Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
333 Types.ConvertType(IdTy));
334 llvm::Value *True =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000335 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000336 llvm::Value *False =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000337 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000338 CallArgList Args;
339 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
340 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
341 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
342 Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000343 Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000344 getContext().BoolTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000345 Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000346 getContext().BoolTy));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000347 // FIXME: We shouldn't need to get the function info here, the runtime
348 // already should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000349 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000350 FunctionType::ExtInfo()),
351 SetPropertyFn,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000352 ReturnValueSlot(), Args);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000353 } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) &&
354 !Ivar->getType()->isAnyComplexType() &&
355 IndirectObjCSetterArg(*CurFnInfo)
356 && CGM.getObjCRuntime().GetCopyStructFunction()) {
357 // objc_copyStruct (&structIvar, &Arg,
358 // sizeof (struct something), true, false);
359 llvm::Value *GetCopyStructFn =
360 CGM.getObjCRuntime().GetCopyStructFunction();
361 CodeGenTypes &Types = CGM.getTypes();
362 CallArgList Args;
363 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
364 RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
365 Types.ConvertType(getContext().VoidPtrTy)));
366 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
367 llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
368 llvm::Value *ArgAsPtrTy =
369 Builder.CreateBitCast(Arg,
370 Types.ConvertType(getContext().VoidPtrTy));
371 RV = RValue::get(ArgAsPtrTy);
372 Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
373 // sizeof (Type of Ivar)
374 uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8;
375 llvm::Value *SizeVal =
376 llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size);
377 Args.push_back(std::make_pair(RValue::get(SizeVal),
378 getContext().LongTy));
379 llvm::Value *True =
380 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
381 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
382 llvm::Value *False =
383 llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
384 Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
385 EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
386 FunctionType::ExtInfo()),
387 GetCopyStructFn, ReturnValueSlot(), Args);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000388 } else if (PID->getSetterCXXAssignment()) {
John McCall558d2ab2010-09-15 10:14:12 +0000389 EmitAnyExpr(PID->getSetterCXXAssignment(), AggValueSlot::ignored(), true);
Fariborz Jahanian97a73cd2010-05-06 15:45:36 +0000390
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000391 } else {
Daniel Dunbar45e84232009-10-27 19:21:30 +0000392 // FIXME: Find a clean way to avoid AST node creation.
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000393 SourceLocation Loc = PD->getLocation();
394 ValueDecl *Self = OMD->getSelfDecl();
395 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
396 DeclRefExpr Base(Self, Self->getType(), Loc);
Chris Lattner89951a82009-02-20 18:43:26 +0000397 ParmVarDecl *ArgDecl = *OMD->param_begin();
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000398 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
Daniel Dunbar45e84232009-10-27 19:21:30 +0000399 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
400
401 // The property type can differ from the ivar type in some situations with
402 // Objective-C pointer types, we can always bit cast the RHS in these cases.
403 if (getContext().getCanonicalType(Ivar->getType()) !=
404 getContext().getCanonicalType(ArgDecl->getType())) {
John McCallf871d0c2010-08-07 06:22:56 +0000405 ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
John McCall2de56d12010-08-25 11:45:40 +0000406 Ivar->getType(), CK_BitCast, &Arg,
John McCall5baba9d2010-08-25 10:28:54 +0000407 VK_RValue);
John McCall2de56d12010-08-25 11:45:40 +0000408 BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
Daniel Dunbar45e84232009-10-27 19:21:30 +0000409 Ivar->getType(), Loc);
410 EmitStmt(&Assign);
411 } else {
John McCall2de56d12010-08-25 11:45:40 +0000412 BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
Daniel Dunbar45e84232009-10-27 19:21:30 +0000413 Ivar->getType(), Loc);
414 EmitStmt(&Assign);
415 }
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000416 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000417
418 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000419}
420
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000421void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
422 ObjCMethodDecl *MD,
423 bool ctor) {
424 llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> IvarInitializers;
425 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
426 StartObjCMethod(MD, IMP->getClassInterface());
427 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
428 E = IMP->init_end(); B != E; ++B) {
429 CXXBaseOrMemberInitializer *Member = (*B);
430 IvarInitializers.push_back(Member);
431 }
432 if (ctor) {
433 for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) {
434 CXXBaseOrMemberInitializer *IvarInit = IvarInitializers[I];
435 FieldDecl *Field = IvarInit->getMember();
436 QualType FieldType = Field->getType();
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000437 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Fariborz Jahanian9b4d4fc2010-04-28 22:30:33 +0000438 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
439 LoadObjCSelf(), Ivar, 0);
John McCall558d2ab2010-09-15 10:14:12 +0000440 EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000441 }
442 // constructor returns 'self'.
443 CodeGenTypes &Types = CGM.getTypes();
444 QualType IdTy(CGM.getContext().getObjCIdType());
445 llvm::Value *SelfAsId =
446 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
447 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000448 } else {
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000449 // dtor
450 for (size_t i = IvarInitializers.size(); i > 0; --i) {
451 FieldDecl *Field = IvarInitializers[i - 1]->getMember();
452 QualType FieldType = Field->getType();
Fariborz Jahanian9b4d4fc2010-04-28 22:30:33 +0000453 const ConstantArrayType *Array =
454 getContext().getAsConstantArrayType(FieldType);
455 if (Array)
456 FieldType = getContext().getBaseElementType(FieldType);
457
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000458 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
459 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
460 LoadObjCSelf(), Ivar, 0);
461 const RecordType *RT = FieldType->getAs<RecordType>();
462 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Douglas Gregor1d110e02010-07-01 14:13:13 +0000463 CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor();
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000464 if (!Dtor->isTrivial()) {
Fariborz Jahanian8b688ed2010-05-04 19:29:32 +0000465 if (Array) {
466 const llvm::Type *BasePtr = ConvertType(FieldType);
467 BasePtr = llvm::PointerType::getUnqual(BasePtr);
468 llvm::Value *BaseAddrPtr =
469 Builder.CreateBitCast(LV.getAddress(), BasePtr);
470 EmitCXXAggrDestructorCall(Dtor,
471 Array, BaseAddrPtr);
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000472 } else {
Fariborz Jahanian8b688ed2010-05-04 19:29:32 +0000473 EmitCXXDestructorCall(Dtor,
474 Dtor_Complete, /*ForVirtualBase=*/false,
475 LV.getAddress());
Chandler Carruthbc397cf2010-05-06 00:20:39 +0000476 }
477 }
478 }
Fariborz Jahanian109dfc62010-04-28 21:28:56 +0000479 }
480 FinishFunction();
481}
482
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +0000483bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
484 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
485 it++; it++;
486 const ABIArgInfo &AI = it->info;
487 // FIXME. Is this sufficient check?
488 return (AI.getKind() == ABIArgInfo::Indirect);
489}
490
Fariborz Jahanian15bd5882010-04-13 18:32:24 +0000491bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
492 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
493 return false;
494 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
495 return FDTTy->getDecl()->hasObjectMember();
496 return false;
497}
498
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000499llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000500 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
501 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000502}
503
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000504QualType CodeGenFunction::TypeOfSelfObject() {
505 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
506 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
Steve Naroff14108da2009-07-10 23:34:53 +0000507 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
508 getContext().getCanonicalType(selfDecl->getType()));
Fariborz Jahanian45012a72009-02-03 00:09:52 +0000509 return PTy->getPointeeType();
510}
511
Mike Stump1eb44332009-09-09 15:08:12 +0000512RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
John McCallef072fd2010-05-22 01:48:05 +0000513 const Selector &S,
514 ReturnValueSlot Return) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000515 llvm::Value *Receiver = LoadObjCSelf();
516 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
517 bool isClassMessage = OMD->isClassMethod();
518 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000519 return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
John McCallef072fd2010-05-22 01:48:05 +0000520 Return,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000521 Exp->getType(),
522 S,
523 OMD->getClassInterface(),
524 isCategoryImpl,
525 Receiver,
526 isClassMessage,
527 CallArgList());
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000529}
530
John McCallef072fd2010-05-22 01:48:05 +0000531RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp,
532 ReturnValueSlot Return) {
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +0000533 Exp = Exp->IgnoreParens();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000534 // FIXME: Split it into two separate routines.
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000535 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
536 Selector S = E->getProperty()->getGetterName();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000537 if (E->isSuperReceiver())
John McCallef072fd2010-05-22 01:48:05 +0000538 return EmitObjCSuperPropertyGet(E, S, Return);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000539 return CGM.getObjCRuntime().
John McCallef072fd2010-05-22 01:48:05 +0000540 GenerateMessageSend(*this, Return, Exp->getType(), S,
Mike Stump1eb44332009-09-09 15:08:12 +0000541 EmitScalarExpr(E->getBase()),
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000542 CallArgList());
Mike Stumpb3589f42009-07-30 22:28:39 +0000543 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000544 const ObjCImplicitSetterGetterRefExpr *KE =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000545 cast<ObjCImplicitSetterGetterRefExpr>(Exp);
Daniel Dunbarf479cea2009-01-16 01:50:29 +0000546 Selector S = KE->getGetterMethod()->getSelector();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000547 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000548 if (KE->getInterfaceDecl()) {
549 const ObjCInterfaceDecl *OID = KE->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000550 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000551 } else if (KE->isSuperReceiver())
John McCallef072fd2010-05-22 01:48:05 +0000552 return EmitObjCSuperPropertyGet(KE, S, Return);
Mike Stump1eb44332009-09-09 15:08:12 +0000553 else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000554 Receiver = EmitScalarExpr(KE->getBase());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000555 return CGM.getObjCRuntime().
Fariborz Jahanian0339d722010-09-10 18:56:35 +0000556 GenerateMessageSend(*this, Return,
557 KE->getGetterMethod()->getResultType(), S,
Mike Stump1eb44332009-09-09 15:08:12 +0000558 Receiver,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000559 CallArgList(), KE->getInterfaceDecl());
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000560 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000561}
562
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000563void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
564 const Selector &S,
565 RValue Src) {
566 CallArgList Args;
567 llvm::Value *Receiver = LoadObjCSelf();
568 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
569 bool isClassMessage = OMD->isClassMethod();
570 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
571 Args.push_back(std::make_pair(Src, Exp->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000572 CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
John McCallef072fd2010-05-22 01:48:05 +0000573 ReturnValueSlot(),
Fariborz Jahanian3d4aeda2010-07-24 00:34:08 +0000574 getContext().VoidTy,
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000575 S,
576 OMD->getClassInterface(),
577 isCategoryImpl,
578 Receiver,
579 isClassMessage,
580 Args);
581 return;
582}
583
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000584void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000585 RValue Src) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000586 // FIXME: Split it into two separate routines.
587 if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
588 Selector S = E->getProperty()->getSetterName();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000589 if (E->isSuperReceiver()) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000590 EmitObjCSuperPropertySet(E, S, Src);
591 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000592 }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000593 CallArgList Args;
594 Args.push_back(std::make_pair(Src, E->getType()));
John McCallef072fd2010-05-22 01:48:05 +0000595 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
596 getContext().VoidTy, S,
Mike Stump1eb44332009-09-09 15:08:12 +0000597 EmitScalarExpr(E->getBase()),
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000598 Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000599 } else if (const ObjCImplicitSetterGetterRefExpr *E =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000600 dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) {
Fariborz Jahaniane2a901a2010-06-07 22:02:01 +0000601 const ObjCMethodDecl *SetterMD = E->getSetterMethod();
602 Selector S = SetterMD->getSelector();
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000603 CallArgList Args;
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000604 llvm::Value *Receiver;
Fariborz Jahaniand2ae5aa2009-08-18 21:37:33 +0000605 if (E->getInterfaceDecl()) {
606 const ObjCInterfaceDecl *OID = E->getInterfaceDecl();
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000607 Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000608 } else if (E->isSuperReceiver()) {
Fariborz Jahanianf4695572009-03-20 19:18:21 +0000609 EmitObjCSuperPropertySet(E, S, Src);
Fariborz Jahanian8e5d2322009-03-20 17:22:23 +0000610 return;
Mike Stumpb3589f42009-07-30 22:28:39 +0000611 } else
Fariborz Jahanian3523d4f2009-03-10 18:03:11 +0000612 Receiver = EmitScalarExpr(E->getBase());
Fariborz Jahaniane2a901a2010-06-07 22:02:01 +0000613 ObjCMethodDecl::param_iterator P = SetterMD->param_begin();
614 Args.push_back(std::make_pair(Src, (*P)->getType()));
John McCallef072fd2010-05-22 01:48:05 +0000615 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
616 getContext().VoidTy, S,
Mike Stump1eb44332009-09-09 15:08:12 +0000617 Receiver,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000618 Args, E->getInterfaceDecl());
Mike Stumpb3589f42009-07-30 22:28:39 +0000619 } else
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000620 assert (0 && "bad expression node in EmitObjCPropertySet");
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000621}
622
Chris Lattner74391b42009-03-22 21:03:39 +0000623void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Mike Stump1eb44332009-09-09 15:08:12 +0000624 llvm::Constant *EnumerationMutationFn =
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000625 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000626 llvm::Value *DeclAddress;
627 QualType ElementTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000629 if (!EnumerationMutationFn) {
630 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
631 return;
632 }
633
Anders Carlssonf484c312008-08-31 02:33:12 +0000634 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
635 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000636 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Chris Lattner7e24e822009-03-28 06:33:19 +0000637 const Decl* D = SD->getSingleDecl();
Ted Kremenek39741ce2008-10-06 20:59:48 +0000638 ElementTy = cast<ValueDecl>(D)->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000639 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000640 } else {
641 ElementTy = cast<Expr>(S.getElement())->getType();
642 DeclAddress = 0;
643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Anders Carlssonf484c312008-08-31 02:33:12 +0000645 // Fast enumeration state.
646 QualType StateTy = getContext().getObjCFastEnumerationStateType();
Daniel Dunbar195337d2010-02-09 02:48:28 +0000647 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
Anders Carlsson1884eb02010-05-22 17:35:42 +0000648 EmitNullInitialization(StatePtr, StateTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Anders Carlssonf484c312008-08-31 02:33:12 +0000650 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000651 static const unsigned NumItems = 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Anders Carlssonf484c312008-08-31 02:33:12 +0000653 // Get selector
Benjamin Kramerad468862010-03-30 11:36:44 +0000654 IdentifierInfo *II[] = {
655 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
656 &CGM.getContext().Idents.get("objects"),
657 &CGM.getContext().Idents.get("count")
658 };
659 Selector FastEnumSel =
660 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
Anders Carlssonf484c312008-08-31 02:33:12 +0000661
662 QualType ItemsTy =
663 getContext().getConstantArrayType(getContext().getObjCIdType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000664 llvm::APInt(32, NumItems),
Anders Carlssonf484c312008-08-31 02:33:12 +0000665 ArrayType::Normal, 0);
Daniel Dunbar195337d2010-02-09 02:48:28 +0000666 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Anders Carlssonf484c312008-08-31 02:33:12 +0000668 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Anders Carlssonf484c312008-08-31 02:33:12 +0000670 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000671 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000672 getContext().getPointerType(StateTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000673
674 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000675 getContext().getPointerType(ItemsTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Anders Carlssonf484c312008-08-31 02:33:12 +0000677 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000678 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Mike Stump1eb44332009-09-09 15:08:12 +0000679 Args.push_back(std::make_pair(RValue::get(Count),
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000680 getContext().UnsignedLongTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000681
682 RValue CountRV =
John McCallef072fd2010-05-22 01:48:05 +0000683 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000684 getContext().UnsignedLongTy,
685 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000686 Collection, Args);
Anders Carlssonf484c312008-08-31 02:33:12 +0000687
Daniel Dunbar195337d2010-02-09 02:48:28 +0000688 llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy,
689 "limit.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000690 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Daniel Dunbar55e87422008-11-11 02:29:29 +0000692 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
693 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Anders Carlssonf484c312008-08-31 02:33:12 +0000695 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000696 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000697
698 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000699 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000700
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000701 EmitBlock(SetStartMutations);
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Daniel Dunbar195337d2010-02-09 02:48:28 +0000703 llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
705 llvm::Value *StateMutationsPtrPtr =
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000706 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000707 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000708 "mutationsptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000709
710 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000711 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000713 Builder.CreateStore(StateMutations, StartMutationsPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Daniel Dunbar55e87422008-11-11 02:29:29 +0000715 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000716 EmitBlock(LoopStart);
717
Daniel Dunbar195337d2010-02-09 02:48:28 +0000718 llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy,
719 "counter.ptr");
Anders Carlssonf484c312008-08-31 02:33:12 +0000720 Builder.CreateStore(Zero, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
722 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000723 EmitBlock(LoopBody);
724
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000725 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
726 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
727
Mike Stump1eb44332009-09-09 15:08:12 +0000728 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000729 "mutations");
Mike Stump1eb44332009-09-09 15:08:12 +0000730 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000731 StartMutations,
732 "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000733
734
Daniel Dunbar55e87422008-11-11 02:29:29 +0000735 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
736 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000738 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000740 EmitBlock(WasMutated);
741 llvm::Value *V =
Mike Stump1eb44332009-09-09 15:08:12 +0000742 Builder.CreateBitCast(Collection,
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000743 ConvertType(getContext().getObjCIdType()),
744 "tmp");
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000745 CallArgList Args2;
Mike Stump1eb44332009-09-09 15:08:12 +0000746 Args2.push_back(std::make_pair(RValue::get(V),
Daniel Dunbar2b2105e2009-02-03 23:55:40 +0000747 getContext().getObjCIdType()));
Mike Stumpf5408fe2009-05-16 07:57:57 +0000748 // FIXME: We shouldn't need to get the function info here, the runtime already
749 // should have computed it to build the function.
John McCall04a67a62010-02-05 21:31:56 +0000750 EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
Rafael Espindola264ba482010-03-30 20:24:48 +0000751 FunctionType::ExtInfo()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000752 EnumerationMutationFn, ReturnValueSlot(), Args2);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000754 EmitBlock(WasNotMutated);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
756 llvm::Value *StateItemsPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000757 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
758
759 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
760
761 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
762 "stateitems");
763
Mike Stump1eb44332009-09-09 15:08:12 +0000764 llvm::Value *CurrentItemPtr =
Anders Carlssonf484c312008-08-31 02:33:12 +0000765 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Anders Carlssonf484c312008-08-31 02:33:12 +0000767 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Anders Carlssonf484c312008-08-31 02:33:12 +0000769 // Cast the item to the right type.
770 CurrentItem = Builder.CreateBitCast(CurrentItem,
771 ConvertType(ElementTy), "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Anders Carlssonf484c312008-08-31 02:33:12 +0000773 if (!DeclAddress) {
774 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Anders Carlssonf484c312008-08-31 02:33:12 +0000776 // Set the value to null.
777 Builder.CreateStore(CurrentItem, LV.getAddress());
778 } else
779 Builder.CreateStore(CurrentItem, DeclAddress);
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Anders Carlssonf484c312008-08-31 02:33:12 +0000781 // Increment the counter.
Mike Stump1eb44332009-09-09 15:08:12 +0000782 Counter = Builder.CreateAdd(Counter,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000783 llvm::ConstantInt::get(UnsignedLongLTy, 1));
Anders Carlssonf484c312008-08-31 02:33:12 +0000784 Builder.CreateStore(Counter, CounterPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
John McCallf1549f62010-07-06 01:34:17 +0000786 JumpDest LoopEnd = getJumpDestInCurrentScope("loopend");
787 JumpDest AfterBody = getJumpDestInCurrentScope("afterbody");
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Anders Carlssone4b6d342009-02-10 05:52:02 +0000789 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
Anders Carlssonf484c312008-08-31 02:33:12 +0000790
791 EmitStmt(S.getBody());
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Anders Carlssonf484c312008-08-31 02:33:12 +0000793 BreakContinueStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000794
John McCallff8e1152010-07-23 21:56:41 +0000795 EmitBlock(AfterBody.getBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Daniel Dunbar55e87422008-11-11 02:29:29 +0000797 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Fariborz Jahanianf0906c42009-01-06 18:56:31 +0000798
799 Counter = Builder.CreateLoad(CounterPtr);
800 Limit = Builder.CreateLoad(LimitPtr);
Anders Carlssonf484c312008-08-31 02:33:12 +0000801 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000802 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000803
804 // Fetch more elements.
805 EmitBlock(FetchMore);
Mike Stump1eb44332009-09-09 15:08:12 +0000806
807 CountRV =
John McCallef072fd2010-05-22 01:48:05 +0000808 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
Anders Carlssonf484c312008-08-31 02:33:12 +0000809 getContext().UnsignedLongTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000810 FastEnumSel,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000811 Collection, Args);
Anders Carlssonf484c312008-08-31 02:33:12 +0000812 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
813 Limit = Builder.CreateLoad(LimitPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Anders Carlssonf484c312008-08-31 02:33:12 +0000815 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
816 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Anders Carlssonf484c312008-08-31 02:33:12 +0000818 // No more elements.
819 EmitBlock(NoElements);
820
821 if (!DeclAddress) {
822 // If the element was not a declaration, set it to be null.
823
824 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Anders Carlssonf484c312008-08-31 02:33:12 +0000826 // Set the value to null.
Owen Andersonc9c88b42009-07-31 20:28:54 +0000827 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
Anders Carlssonf484c312008-08-31 02:33:12 +0000828 LV.getAddress());
829 }
830
John McCallff8e1152010-07-23 21:56:41 +0000831 EmitBlock(LoopEnd.getBlock());
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000832}
833
Mike Stump1eb44332009-09-09 15:08:12 +0000834void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +0000835 CGM.getObjCRuntime().EmitTryStmt(*this, S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000836}
837
Mike Stump1eb44332009-09-09 15:08:12 +0000838void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000839 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
840}
841
Chris Lattner10cac6f2008-11-15 21:26:17 +0000842void CodeGenFunction::EmitObjCAtSynchronizedStmt(
Mike Stump1eb44332009-09-09 15:08:12 +0000843 const ObjCAtSynchronizedStmt &S) {
John McCallf1549f62010-07-06 01:34:17 +0000844 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000845}
846
Ted Kremenek2979ec72008-04-09 15:51:31 +0000847CGObjCRuntime::~CGObjCRuntime() {}
John McCallf1549f62010-07-06 01:34:17 +0000848
849