CGObjC.cpp revision 1d3a61a5ab0952ebf1f829bd2e48bc09bb332189
15508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
25508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//
35508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//                     The LLVM Compiler Infrastructure
45508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//
85508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===//
95508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//
105508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// This contains code to emit Objective-C code as LLVM code.
115508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//
125508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===//
135508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
14bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel#include "CGDebugInfo.h"
152979ec73b4f974d85f2ce84167712177a44c6f09Ted Kremenek#include "CGObjCRuntime.h"
165508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenFunction.h"
175508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenModule.h"
1885c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar#include "clang/AST/ASTContext.h"
19c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
2016f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
21e66f4e3e3ae9d7d11b0c302211066fad69228abaDaniel Dunbar#include "clang/Basic/Diagnostic.h"
223d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson#include "llvm/ADT/STLExtras.h"
23c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar#include "llvm/Target/TargetData.h"
245508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace clang;
255508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace CodeGen;
265508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
278fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emits an instance of NSConstantString representing the object.
281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
2971fcec9abf2ce66d5e17a24bd021680e94e42f0dDaniel Dunbar{
300d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  llvm::Constant *C =
310d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall      CGM.getObjCRuntime().GenerateConstantString(E->getString());
32ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  // FIXME: This bitcast should just be made an invariant on the Runtime.
333c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
348fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner}
358fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
368fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emit a selector.
378fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattnerllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
388fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Untyped selector.
398fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Note that this implementation allows for non-constant strings to be passed
408fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // as arguments to @selector().  Currently, the only thing preventing this
418fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // behaviour is the type checking in the front end.
426d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar  return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
438fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner}
448fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
45ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbarllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
46ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  // FIXME: This should pass the Decl not the name.
47ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
48ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar}
498fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
508fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
51ef072fd2f3347cfd857d6eb787b245b950771430John McCallRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
52ef072fd2f3347cfd857d6eb787b245b950771430John McCall                                            ReturnValueSlot Return) {
538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Only the lookup mechanism and first two arguments of the method
548fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // implementation vary between runtimes.  We can get the receiver and
558fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // arguments in generic code.
561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
588fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  bool isSuperMessage = false;
59f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar  bool isClassMessage = false;
60c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall  ObjCInterfaceDecl *OID = 0;
618fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Find the receiver
620b647a6ea18151149d624ab373e6fe0e819e4a9aDaniel Dunbar  llvm::Value *Receiver = 0;
6304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (E->getReceiverKind()) {
6404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::Instance:
6504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    Receiver = EmitScalarExpr(E->getInstanceReceiver());
6604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::Class: {
693031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    const ObjCObjectType *ObjTy
703031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall      = E->getClassReceiver()->getAs<ObjCObjectType>();
713031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    assert(ObjTy && "Invalid Objective-C class message send");
723031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    OID = ObjTy->getInterface();
733031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    assert(OID && "Invalid Objective-C class message send");
74c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall    Receiver = Runtime.GetClass(Builder, OID);
75f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar    isClassMessage = true;
7604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
7704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
7804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
7904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::SuperInstance:
8004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    Receiver = LoadObjCSelf();
818fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner    isSuperMessage = true;
8204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
8304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
8404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::SuperClass:
858fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner    Receiver = LoadObjCSelf();
8604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    isSuperMessage = true;
8704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    isClassMessage = true;
8804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
898fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  }
908fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
9119cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar  CallArgList Args;
92131038e2125d98f1be0d9cb62561a7cede41bb44Anders Carlsson  EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end());
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
947e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson  QualType ResultType =
957e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson    E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType();
967e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson
978fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  if (isSuperMessage) {
989384c768e93f270118a30ce96546083a666da284Chris Lattner    // super is only valid in an Objective-C method
999384c768e93f270118a30ce96546083a666da284Chris Lattner    const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1007ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian    bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
1017e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson    return Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
1027f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                                            E->getSelector(),
103f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                            OMD->getClassInterface(),
1047ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian                                            isCategoryImpl,
105f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                            Receiver,
10619cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar                                            isClassMessage,
107d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                            Args,
108d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                            E->getMethodDecl());
1098fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  }
110d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar
1117e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson  return Runtime.GenerateMessageSend(*this, Return, ResultType,
112ef072fd2f3347cfd857d6eb787b245b950771430John McCall                                     E->getSelector(),
113c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                     Receiver, Args, OID,
114df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                                     E->getMethodDecl());
1155508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1165508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
117af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
118af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// the LLVM function and sets the other context used by
119af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// CodeGenFunction.
120679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
121679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian                                      const ObjCContainerDecl *CD) {
122d26bc76c98006609002d9930f8840490e88ac5b5John McCall  FunctionArgList args;
1234800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel  // Check if we should generate debug info for this method.
124aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
125aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    DebugInfo = CGM.getModuleDebugInfo();
1264800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel
127679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
128f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
1290e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
1300e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
1314111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
132d26bc76c98006609002d9930f8840490e88ac5b5John McCall  args.push_back(OMD->getSelfDecl());
133d26bc76c98006609002d9930f8840490e88ac5b5John McCall  args.push_back(OMD->getCmdDecl());
1344111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
13589951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
13689951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = OMD->param_end(); PI != E; ++PI)
137d26bc76c98006609002d9930f8840490e88ac5b5John McCall    args.push_back(*PI);
138b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
13914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  CurGD = OMD;
14014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
141d26bc76c98006609002d9930f8840490e88ac5b5John McCall  StartFunction(OMD, OMD->getResultType(), Fn, FI, args, OMD->getLocStart());
142af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
143af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1442846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanianvoid CodeGenFunction::GenerateObjCGetterBody(ObjCIvarDecl *Ivar,
1452846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                             bool IsAtomic, bool IsStrong) {
1462846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
1472846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                Ivar, 0);
1482846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *GetCopyStructFn =
1492846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CGM.getObjCRuntime().GetGetStructFunction();
1502846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CodeGenTypes &Types = CGM.getTypes();
1512846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  // objc_copyStruct (ReturnValue, &structIvar,
1522846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  //                  sizeof (Type of Ivar), isAtomic, false);
1532846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CallArgList Args;
1542846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
1552846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                                Types.ConvertType(getContext().VoidPtrTy)));
1562846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
1572846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
1582846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                         Types.ConvertType(getContext().VoidPtrTy)));
1592846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
1602846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  // sizeof (Type of Ivar)
1612846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CharUnits Size =  getContext().getTypeSizeInChars(Ivar->getType());
1622846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *SizeVal =
1632846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
1642846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                         Size.getQuantity());
1652846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RValue::get(SizeVal),
1662846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                getContext().LongTy));
1672846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *isAtomic =
1682846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
1692846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                         IsAtomic ? 1 : 0);
1702846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RValue::get(isAtomic),
1712846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                getContext().BoolTy));
1722846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *hasStrong =
1732846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
1742846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                         IsStrong ? 1 : 0);
1752846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RValue::get(hasStrong),
1762846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                getContext().BoolTy));
1772846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
1782846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                 FunctionType::ExtInfo()),
1792846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian           GetCopyStructFn, ReturnValueSlot(), Args);
1802846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian}
1812846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian
182af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method.  An Objective-C method is a C function with
1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture.
184af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
185679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  StartObjCMethod(OMD, OMD->getClassInterface());
1866fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  EmitStmt(OMD->getBody());
1876fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  FinishFunction(OMD->getBodyRBrace());
188af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
189af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
190f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
191f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump// AST for the whole body we can just fall back to having a GenerateFunction
192f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump// which takes the body Stmt.
193af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
194af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// GenerateObjCGetter - Generate an Objective-C property getter
195489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
196489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category.
197fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
198fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                         const ObjCPropertyImplDecl *PID) {
199c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
200af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
20115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  bool IsAtomic =
20215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
203af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
204af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  assert(OMD && "Invalid call to generate getter (empty method)");
205679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  StartObjCMethod(OMD, IMP->getClassInterface());
20615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian
207c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  // Determine if we should use an objc_getProperty call for
208447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian  // this. Non-atomic properties are directly evaluated.
209447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian  // atomic 'copy' and 'retain' properties are also directly
210447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian  // evaluated in gc-only mode.
211c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
21215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian      IsAtomic &&
213447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian      (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
214447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian       PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *GetPropertyFn =
216c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      CGM.getObjCRuntime().GetPropertyGetFunction();
2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
218c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    if (!GetPropertyFn) {
219c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
220c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      FinishFunction();
221c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      return;
222c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    }
223c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
224c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
225c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // FIXME: Can't this be simpler? This might even be worse than the
226c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // corresponding gcc code.
227c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CodeGenTypes &Types = CGM.getTypes();
228c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    ValueDecl *Cmd = OMD->getCmdDecl();
229c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
230c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    QualType IdTy = getContext().getObjCIdType();
2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *SelfAsId =
232c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
233fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian    llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
234c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    llvm::Value *True =
2354a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
236c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CallArgList Args;
237c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
238c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
239ab5824e97666ca7a91ed3013524cee18420866f1David Chisnall    Args.push_back(std::make_pair(RValue::get(Offset),
240ab5824e97666ca7a91ed3013524cee18420866f1David Chisnall                getContext().getPointerDiffType()));
241c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
242e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // FIXME: We shouldn't need to get the function info here, the
243e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // runtime already should have computed it to build the function.
24404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
245264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                               FunctionType::ExtInfo()),
246f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                         GetPropertyFn, ReturnValueSlot(), Args);
247c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // We need to fix the type here. Ivars with copy & retain are
248c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // always objects so we don't need to worry about complex or
249c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // aggregates.
2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
251c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar                                           Types.ConvertType(PD->getType())));
252c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    EmitReturnOfRValue(RV, PD->getType());
253c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  } else {
2542846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    const llvm::Triple &Triple = getContext().Target.getTriple();
2552846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    QualType IVART = Ivar->getType();
2562846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    if (IsAtomic &&
2572846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        IVART->isScalarType() &&
2582846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        (Triple.getArch() == llvm::Triple::arm ||
2592846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian         Triple.getArch() == llvm::Triple::thumb) &&
2602846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        (getContext().getTypeSizeInChars(IVART)
2612846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian         > CharUnits::fromQuantity(4)) &&
2622846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        CGM.getObjCRuntime().GetGetStructFunction()) {
2632846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      GenerateObjCGetterBody(Ivar, true, false);
2642846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    }
2651d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    else if (IsAtomic &&
2661d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
2671d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             Triple.getArch() == llvm::Triple::x86 &&
2681d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (getContext().getTypeSizeInChars(IVART)
2691d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian              > CharUnits::fromQuantity(4)) &&
2701d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             CGM.getObjCRuntime().GetGetStructFunction()) {
2711d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian      GenerateObjCGetterBody(Ivar, true, false);
2721d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    }
2731d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    else if (IsAtomic &&
2741d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
2751d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             Triple.getArch() == llvm::Triple::x86_64 &&
2761d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (getContext().getTypeSizeInChars(IVART)
2771d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian              > CharUnits::fromQuantity(8)) &&
2781d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             CGM.getObjCRuntime().GetGetStructFunction()) {
2791d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian      GenerateObjCGetterBody(Ivar, true, false);
2801d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    }
2812846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    else if (IVART->isAnyComplexType()) {
28297a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
28397a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian                                    Ivar, 0);
2841b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian      ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
2851b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian                                               LV.isVolatileQualified());
2861b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian      StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
2871b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian    }
2882846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    else if (hasAggregateLLVMType(IVART)) {
28915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian      bool IsStrong = false;
2902846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(IVART)))
2910b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian          && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
2928fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall          && CGM.getObjCRuntime().GetGetStructFunction()) {
2932846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong);
2940b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian      }
29597a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian      else {
29697a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        if (PID->getGetterCXXConstructor()) {
29797a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian          ReturnStmt *Stmt =
29897a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian            new (getContext()) ReturnStmt(SourceLocation(),
2995077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                          PID->getGetterCXXConstructor(),
3005077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                          0);
30197a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian          EmitReturnStmt(*Stmt);
3021d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian        } else if (IsAtomic &&
3031d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                   !IVART->isAnyComplexType() &&
3041d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                   Triple.getArch() == llvm::Triple::x86 &&
3051d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                   (getContext().getTypeSizeInChars(IVART)
3061d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                    > CharUnits::fromQuantity(4)) &&
3071d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                   CGM.getObjCRuntime().GetGetStructFunction()) {
3081d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian          GenerateObjCGetterBody(Ivar, true, false);
3091d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian        }
3101d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian        else if (IsAtomic &&
3111d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                 !IVART->isAnyComplexType() &&
3121d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                 Triple.getArch() == llvm::Triple::x86_64 &&
3131d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                 (getContext().getTypeSizeInChars(IVART)
3141d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                  > CharUnits::fromQuantity(8)) &&
3151d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian                 CGM.getObjCRuntime().GetGetStructFunction()) {
3161d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian          GenerateObjCGetterBody(Ivar, true, false);
31797a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        }
31897a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        else {
31997a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian          LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
32097a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian                                        Ivar, 0);
3212846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian          EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART);
32297a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        }
32397a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian      }
3242846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    }
3252846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    else {
3262846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
32797a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian                                    Ivar, 0);
32814086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian        if (PD->getType()->isReferenceType()) {
32914086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian          RValue RV = RValue::get(LV.getAddress());
33014086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian          EmitReturnOfRValue(RV, PD->getType());
33114086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian        }
33214086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian        else {
33314086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian          CodeGenTypes &Types = CGM.getTypes();
33414086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian          RValue RV = EmitLoadOfLValue(LV, IVART);
33514086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian          RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
3362846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                               Types.ConvertType(PD->getType())));
33714086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian          EmitReturnOfRValue(RV, PD->getType());
33814086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian        }
339ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian    }
340c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
341af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
342af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  FinishFunction();
343af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
344af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
3452846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanianvoid CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
3462846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                                   ObjCIvarDecl *Ivar) {
3472846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  // objc_copyStruct (&structIvar, &Arg,
3482846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  //                  sizeof (struct something), true, false);
3492846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *GetCopyStructFn =
3502846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CGM.getObjCRuntime().GetSetStructFunction();
3512846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CodeGenTypes &Types = CGM.getTypes();
3522846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CallArgList Args;
3532846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
3542846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  RValue RV =
3552846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    RValue::get(Builder.CreateBitCast(LV.getAddress(),
3562846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                Types.ConvertType(getContext().VoidPtrTy)));
3572846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
3582846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
3592846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *ArgAsPtrTy =
3602846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Builder.CreateBitCast(Arg,
3612846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                      Types.ConvertType(getContext().VoidPtrTy));
3622846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  RV = RValue::get(ArgAsPtrTy);
3632846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
3642846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  // sizeof (Type of Ivar)
3652846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  CharUnits Size =  getContext().getTypeSizeInChars(Ivar->getType());
3662846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *SizeVal =
3672846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
3682846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                         Size.getQuantity());
3692846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RValue::get(SizeVal),
3702846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                getContext().LongTy));
3712846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *True =
3722846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
3732846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
3742846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::Value *False =
3752846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
3762846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
3772846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
3782846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                 FunctionType::ExtInfo()),
3792846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian           GetCopyStructFn, ReturnValueSlot(), Args);
3802846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian}
3812846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian
382af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// GenerateObjCSetter - Generate an Objective-C property setter
383489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
384489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category.
385fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
386fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                         const ObjCPropertyImplDecl *PID) {
38786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
388af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
389af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
390af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  assert(OMD && "Invalid call to generate setter (empty method)");
391679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  StartObjCMethod(OMD, IMP->getClassInterface());
3921d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian  const llvm::Triple &Triple = getContext().Target.getTriple();
3931d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian  QualType IVART = Ivar->getType();
39486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
3951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool IsAtomic =
39686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
39786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar
39886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // Determine if we should use an objc_setProperty call for
39986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // this. Properties with 'copy' semantics always use it, as do
40086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // non-atomic properties with 'release' semantics as long as we are
40186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // not in gc-only mode.
40286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  if (IsCopy ||
40386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
40486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar       PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *SetPropertyFn =
40686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      CGM.getObjCRuntime().GetPropertySetFunction();
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    if (!SetPropertyFn) {
40986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
41086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      FinishFunction();
41186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      return;
41286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    }
4131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Emit objc_setProperty((id) self, _cmd, offset, arg,
41586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    //                       <is-atomic>, <is-copy>).
41686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    // FIXME: Can't this be simpler? This might even be worse than the
41786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    // corresponding gcc code.
41886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    CodeGenTypes &Types = CGM.getTypes();
41986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    ValueDecl *Cmd = OMD->getCmdDecl();
42086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
42186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    QualType IdTy = getContext().getObjCIdType();
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *SelfAsId =
42386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
424fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian    llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
42589951a86b594513c2a013532ed45d197413b1087Chris Lattner    llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *ArgAsId =
42786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
42886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar                            Types.ConvertType(IdTy));
42986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    llvm::Value *True =
4304a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
43186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    llvm::Value *False =
4324a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
43386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    CallArgList Args;
43486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
43586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
436ab5824e97666ca7a91ed3013524cee18420866f1David Chisnall    Args.push_back(std::make_pair(RValue::get(Offset),
437ab5824e97666ca7a91ed3013524cee18420866f1David Chisnall                getContext().getPointerDiffType()));
43886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
44086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar                                  getContext().BoolTy));
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
44286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar                                  getContext().BoolTy));
443f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: We shouldn't need to get the function info here, the runtime
444f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // already should have computed it to build the function.
44504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
446264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                   FunctionType::ExtInfo()),
447264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola             SetPropertyFn,
448f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson             ReturnValueSlot(), Args);
4491d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian  } else if (IsAtomic && hasAggregateLLVMType(IVART) &&
4501d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             !IVART->isAnyComplexType() &&
4511d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             ((Triple.getArch() == llvm::Triple::x86 &&
4521d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian              (getContext().getTypeSizeInChars(IVART)
4531d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian               > CharUnits::fromQuantity(4))) ||
4541d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian              (Triple.getArch() == llvm::Triple::x86_64 &&
4551d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian              (getContext().getTypeSizeInChars(IVART)
4561d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian               > CharUnits::fromQuantity(8))))
4578fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall             && CGM.getObjCRuntime().GetSetStructFunction()) {
4581d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian          // objc_copyStruct (&structIvar, &Arg,
4591d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian          //                  sizeof (struct something), true, false);
4602846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    GenerateObjCAtomicSetterBody(OMD, Ivar);
46197a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian  } else if (PID->getSetterCXXAssignment()) {
4622a41637a995affa1563f4d82a8b026e326a2faa0John McCall    EmitIgnoredExpr(PID->getSetterCXXAssignment());
46386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  } else {
4642846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    if (IsAtomic &&
4652846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        IVART->isScalarType() &&
4662846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        (Triple.getArch() == llvm::Triple::arm ||
4672846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian         Triple.getArch() == llvm::Triple::thumb) &&
4682846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        (getContext().getTypeSizeInChars(IVART)
4692846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian          > CharUnits::fromQuantity(4)) &&
4702846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        CGM.getObjCRuntime().GetGetStructFunction()) {
4712846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      GenerateObjCAtomicSetterBody(OMD, Ivar);
4721d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    }
4731d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    else if (IsAtomic &&
4741d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
4751d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             Triple.getArch() == llvm::Triple::x86 &&
4761d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (getContext().getTypeSizeInChars(IVART)
4771d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian              > CharUnits::fromQuantity(4)) &&
4781d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             CGM.getObjCRuntime().GetGetStructFunction()) {
4791d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian      GenerateObjCAtomicSetterBody(OMD, Ivar);
4801d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    }
4811d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian    else if (IsAtomic &&
4821d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (IVART->isScalarType() && !IVART->isRealFloatingType()) &&
4831d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             Triple.getArch() == llvm::Triple::x86_64 &&
4841d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             (getContext().getTypeSizeInChars(IVART)
4851d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian              > CharUnits::fromQuantity(8)) &&
4861d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian             CGM.getObjCRuntime().GetGetStructFunction()) {
4871d3a61a5ab0952ebf1f829bd2e48bc09bb332189Fariborz Jahanian      GenerateObjCAtomicSetterBody(OMD, Ivar);
4882846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    }
4892846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian    else {
4902846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      // FIXME: Find a clean way to avoid AST node creation.
4912846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      SourceLocation Loc = PD->getLocation();
4922846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      ValueDecl *Self = OMD->getSelfDecl();
4932846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
4942846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc);
4952846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      ParmVarDecl *ArgDecl = *OMD->param_begin();
49614086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian      QualType T = ArgDecl->getType();
49714086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian      if (T->isReferenceType())
49814086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian        T = cast<ReferenceType>(T)->getPointeeType();
49914086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian      DeclRefExpr Arg(ArgDecl, T, VK_LValue, Loc);
5002846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
50145e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar
5022846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      // The property type can differ from the ivar type in some situations with
5032846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      // Objective-C pointer types, we can always bit cast the RHS in these cases.
5042846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      if (getContext().getCanonicalType(Ivar->getType()) !=
5052846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian          getContext().getCanonicalType(ArgDecl->getType())) {
5062846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
5072846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                   Ivar->getType(), CK_BitCast, &Arg,
5082846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                   VK_RValue);
5092846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
5102846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                              Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
5112846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        EmitStmt(&Assign);
5122846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      } else {
5132846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
5142846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                              Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
5152846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian        EmitStmt(&Assign);
5162846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian      }
51745e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    }
51886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  }
519af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
520af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  FinishFunction();
5214111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
5224111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
523e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall// FIXME: these are stolen from CGClass.cpp, which is lame.
524e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace {
525e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  struct CallArrayIvarDtor : EHScopeStack::Cleanup {
526e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    const ObjCIvarDecl *ivar;
527e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    llvm::Value *self;
528e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    CallArrayIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
529e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      : ivar(ivar), self(self) {}
530e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
531e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    void Emit(CodeGenFunction &CGF, bool IsForEH) {
532e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      LValue lvalue =
533e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall        CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
534e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
535e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      QualType type = ivar->getType();
536e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      const ConstantArrayType *arrayType
537e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall        = CGF.getContext().getAsConstantArrayType(type);
538e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      QualType baseType = CGF.getContext().getBaseElementType(arrayType);
539e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      const CXXRecordDecl *classDecl = baseType->getAsCXXRecordDecl();
540e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
541e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      llvm::Value *base
542e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall        = CGF.Builder.CreateBitCast(lvalue.getAddress(),
543e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                    CGF.ConvertType(baseType)->getPointerTo());
544e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      CGF.EmitCXXAggrDestructorCall(classDecl->getDestructor(),
545e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                    arrayType, base);
546e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    }
547e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  };
548e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
549e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  struct CallIvarDtor : EHScopeStack::Cleanup {
550e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    const ObjCIvarDecl *ivar;
551e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    llvm::Value *self;
552e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    CallIvarDtor(const ObjCIvarDecl *ivar, llvm::Value *self)
553e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      : ivar(ivar), self(self) {}
554e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
555e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    void Emit(CodeGenFunction &CGF, bool IsForEH) {
556e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      LValue lvalue =
557e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall        CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), self, ivar, 0);
558e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
559e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      QualType type = ivar->getType();
560e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      const CXXRecordDecl *classDecl = type->getAsCXXRecordDecl();
561e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
562e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      CGF.EmitCXXDestructorCall(classDecl->getDestructor(),
563e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                Dtor_Complete, /*ForVirtualBase=*/false,
564e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                lvalue.getAddress());
565e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    }
566e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  };
567e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall}
568e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
569e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF,
570e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                  ObjCImplementationDecl *impl) {
571e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  CodeGenFunction::RunCleanupsScope scope(CGF);
572e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
573e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  llvm::Value *self = CGF.LoadObjCSelf();
574e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
575e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  ObjCInterfaceDecl *iface
576e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    = const_cast<ObjCInterfaceDecl*>(impl->getClassInterface());
577e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  for (ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
578e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall       ivar; ivar = ivar->getNextIvar()) {
579e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    QualType type = ivar->getType();
580e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
581e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    // Drill down to the base element type.
582e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    QualType baseType = type;
583e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    const ConstantArrayType *arrayType =
584e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      CGF.getContext().getAsConstantArrayType(baseType);
585e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    if (arrayType) baseType = CGF.getContext().getBaseElementType(arrayType);
586e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
587e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    // Check whether the ivar is a destructible type.
588e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    QualType::DestructionKind destructKind = baseType.isDestructedType();
589e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    assert(destructKind == type.isDestructedType());
590e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
591e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    switch (destructKind) {
592e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    case QualType::DK_none:
593e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      continue;
594e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
595e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    case QualType::DK_cxx_destructor:
596e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      if (arrayType)
597e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall        CGF.EHStack.pushCleanup<CallArrayIvarDtor>(NormalAndEHCleanup,
598e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                                   ivar, self);
599e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      else
600e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall        CGF.EHStack.pushCleanup<CallIvarDtor>(NormalAndEHCleanup,
601e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                              ivar, self);
602e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      break;
603e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    }
604e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  }
605e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
606e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
607e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall}
608e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
609109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
610109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 ObjCMethodDecl *MD,
611109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 bool ctor) {
612109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
613109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  StartObjCMethod(MD, IMP->getClassInterface());
614e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
615e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  // Emit .cxx_construct.
616109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  if (ctor) {
617e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
618e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
619e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall           E = IMP->init_end(); B != E; ++B) {
620e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      CXXCtorInitializer *IvarInit = (*B);
62100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Field = IvarInit->getAnyMember();
622109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      ObjCIvarDecl  *Ivar = cast<ObjCIvarDecl>(Field);
6239b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
6249b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian                                    LoadObjCSelf(), Ivar, 0);
625558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
626109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    }
627109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    // constructor returns 'self'.
628109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    CodeGenTypes &Types = CGM.getTypes();
629109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    QualType IdTy(CGM.getContext().getObjCIdType());
630109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    llvm::Value *SelfAsId =
631109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
632109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
633e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
634e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  // Emit .cxx_destruct.
635bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth  } else {
636e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    emitCXXDestructMethod(*this, IMP);
637109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  }
638109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  FinishFunction();
639109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian}
640109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
6410b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
6420b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
6430b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  it++; it++;
6440b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  const ABIArgInfo &AI = it->info;
6450b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  // FIXME. Is this sufficient check?
6460b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  return (AI.getKind() == ABIArgInfo::Indirect);
6470b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian}
6480b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian
64915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
65015bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
65115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return false;
65215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
65315bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return FDTTy->getDecl()->hasObjectMember();
65415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  return false;
65515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian}
65615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian
657c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() {
658b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
659b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
6604111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
6614111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
66245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() {
66345012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
66445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
66514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
66614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    getContext().getCanonicalType(selfDecl->getType()));
66745012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  return PTy->getPointeeType();
66845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian}
66945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian
670e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallLValue
671e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallCodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
672e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // This is a special l-value that just issues sends when we load or
673e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // store through it.
674e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
675e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // For certain base kinds, we need to emit the base immediately.
676e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Base;
677e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  if (E->isSuperReceiver())
678e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = LoadObjCSelf();
679e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  else if (E->isClassReceiver())
680e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
681e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  else
682e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = EmitScalarExpr(E->getBase());
683e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  return LValue::MakePropertyRef(E, Base);
684e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall}
685e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
686e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallstatic RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
687e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       ReturnValueSlot Return,
688e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       QualType ResultType,
689e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       Selector S,
690e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       llvm::Value *Receiver,
691e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       const CallArgList &CallArgs) {
692e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
693f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian  bool isClassMessage = OMD->isClassMethod();
694f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian  bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
695e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  return CGF.CGM.getObjCRuntime()
696e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                .GenerateMessageSendSuper(CGF, Return, ResultType,
697e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          S, OMD->getClassInterface(),
698e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          isCategoryImpl, Receiver,
699e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          isClassMessage, CallArgs);
700f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian}
701f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian
702119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCallRValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
703119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                                    ReturnValueSlot Return) {
704119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
70568af13f3ca39947e3f285f864fe3b76640fddf69Fariborz Jahanian  QualType ResultType = E->getGetterResultType();
70612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  Selector S;
70712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isExplicitProperty()) {
70812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    const ObjCPropertyDecl *Property = E->getExplicitProperty();
70912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    S = Property->getGetterName();
710b3589f44c5d295cd41de2c83f3475116835eeebdMike Stump  } else {
71112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter();
71212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    S = Getter->getSelector();
7135daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian  }
71412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
715e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
716e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
717e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // Accesses to 'super' follow a different code path.
71812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isSuperReceiver())
719e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    return GenerateMessageSendSuper(*this, Return, ResultType,
720e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                    S, Receiver, CallArgList());
72112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
722119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCInterfaceDecl *ReceiverClass
723119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall    = (E->isClassReceiver() ? E->getClassReceiver() : 0);
72412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  return CGM.getObjCRuntime().
72512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall             GenerateMessageSend(*this, Return, ResultType, S,
72612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                 Receiver, CallArgList(), ReceiverClass);
7279c3fc703b29a31d40bcf5027dbb4784dd393804eDaniel Dunbar}
7289c3fc703b29a31d40bcf5027dbb4784dd393804eDaniel Dunbar
729119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCallvoid CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
730119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                                        LValue Dst) {
731119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
73212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  Selector S = E->getSetterSelector();
73368af13f3ca39947e3f285f864fe3b76640fddf69Fariborz Jahanian  QualType ArgType = E->getSetterArgType();
73468af13f3ca39947e3f285f864fe3b76640fddf69Fariborz Jahanian
735b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  // FIXME. Other than scalars, AST is not adequate for setter and
736b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  // getter type mismatches which require conversion.
737b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  if (Src.isScalar()) {
738b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    llvm::Value *SrcVal = Src.getScalarVal();
739b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    QualType DstType = getContext().getCanonicalType(ArgType);
740b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    const llvm::Type *DstTy = ConvertType(DstType);
741b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    if (SrcVal->getType() != DstTy)
742b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian      Src =
743b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian        RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
744b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  }
745b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian
746e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  CallArgList Args;
747e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  Args.push_back(std::make_pair(Src, ArgType));
748e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
749e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
750e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  QualType ResultType = getContext().VoidTy;
751e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
75212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isSuperReceiver()) {
753e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    GenerateMessageSendSuper(*this, ReturnValueSlot(),
754e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                             ResultType, S, Receiver, Args);
75512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    return;
75612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  }
75712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
758119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCInterfaceDecl *ReceiverClass
759119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall    = (E->isClassReceiver() ? E->getClassReceiver() : 0);
76012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
76112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
762e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                           ResultType, S, Receiver, Args,
763e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                           ReceiverClass);
76485c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar}
76585c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar
76674391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Constant *EnumerationMutationFn =
768c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.getObjCRuntime().EnumerationMutationFunction();
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
770c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  if (!EnumerationMutationFn) {
771c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
772c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    return;
773c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
774c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
77557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  // The local variable comes into scope immediately.
77657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  AutoVarEmission variable = AutoVarEmission::invalid();
77757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
77857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
77957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
780bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  CGDebugInfo *DI = getDebugInfo();
781bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  if (DI) {
782bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->setLocation(S.getSourceRange().getBegin());
783bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->EmitRegionStart(Builder);
784bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  }
785bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
786d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
787d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
789f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Fast enumeration state.
790f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  QualType StateTy = getContext().getObjCFastEnumerationStateType();
791195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
7921884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  EmitNullInitialization(StatePtr, StateTy);
7931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
794f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Number of elements in the items array.
7952abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  static const unsigned NumItems = 16;
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
797d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the countByEnumeratingWithState:objects:count: selector.
798ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  IdentifierInfo *II[] = {
799ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("countByEnumeratingWithState"),
800ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("objects"),
801ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("count")
802ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  };
803ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  Selector FastEnumSel =
804ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
805f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
806f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  QualType ItemsTy =
807f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    getContext().getConstantArrayType(getContext().getObjCIdType(),
8081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                      llvm::APInt(32, NumItems),
809f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                      ArrayType::Normal, 0);
810195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
812d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Emit the collection pointer.
813f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  llvm::Value *Collection = EmitScalarExpr(S.getCollection());
8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
815d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Send it our message:
816f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  CallArgList Args;
817d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
818d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The first argument is a temporary of the enumeration-state type.
8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args.push_back(std::make_pair(RValue::get(StatePtr),
820f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                getContext().getPointerType(StateTy)));
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
822d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The second argument is a temporary array with space for NumItems
823d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointers.  We'll actually be loading elements from the array
824d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointer written into the control state; this buffer is so that
825d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // collections that *aren't* backed by arrays can still queue up
826d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // batches of elements.
8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args.push_back(std::make_pair(RValue::get(ItemsPtr),
828f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                getContext().getPointerType(ItemsTy)));
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
830d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The third argument is the capacity of that temporary array.
831f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
8324a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args.push_back(std::make_pair(RValue::get(Count),
83446f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar                                getContext().UnsignedLongTy));
8351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
836d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start the enumeration.
8371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  RValue CountRV =
838ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
839f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
840f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             FastEnumSel,
841c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
842f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
843d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The initial number of objects that were returned in the buffer.
844d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialBufferLimit = CountRV.getScalarVal();
8451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
846d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
847d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
848f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
849d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
850f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
851d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If the limit pointer was zero to begin with, the collection is
852d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // empty; skip all this.
853d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
854d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopInitBB);
8551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
856d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, initialize the loop.
857d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopInitBB);
8581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
859d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Save the initial mutations value.  This is the value at an
860d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // address that was written into the state object by
861d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // countByEnumeratingWithState:objects:count:.
8621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtrPtr =
8632abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson    Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
8652abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                                                      "mutationsptr");
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
867d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialMutations =
868d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
870d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start looping.  This is the point we return to whenever we have a
871d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // fresh, non-empty batch of objects.
872d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
873d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopBodyBB);
8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
875d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current index into the buffer.
876bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
877d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, LoopInitBB);
878f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
879d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current buffer size.
880bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
881d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(initialBufferLimit, LoopInitBB);
882f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
883d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether the mutations value has changed from where it was
884d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // at start.  StateMutationsPtr should actually be invariant between
885d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes.
8862abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
887d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *currentMutations
888d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateLoad(StateMutationsPtr, "statemutations");
8891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
890d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
891361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman  llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
893d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
894d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       WasNotMutatedBB, WasMutatedBB);
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
896d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If so, call the enumeration-mutation function.
897d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasMutatedBB);
8982abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  llvm::Value *V =
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Builder.CreateBitCast(Collection,
9002abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                          ConvertType(getContext().getObjCIdType()),
9012abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                          "tmp");
9022b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar  CallArgList Args2;
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args2.push_back(std::make_pair(RValue::get(V),
9042b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar                                getContext().getObjCIdType()));
905f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We shouldn't need to get the function info here, the runtime already
906f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // should have computed it to build the function.
90704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
908264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                          FunctionType::ExtInfo()),
909f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson           EnumerationMutationFn, ReturnValueSlot(), Args2);
9101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
911d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, or if the mutation function returns, just continue.
912d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasNotMutatedBB);
9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
914d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Initialize the element variable.
915d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  RunCleanupsScope elementVariableScope(*this);
91657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  bool elementIsVariable;
917d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  LValue elementLValue;
918d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  QualType elementType;
919d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
92057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    // Initialize the variable, in case it's a __block variable or something.
92157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    EmitAutoVarInit(variable);
922f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
92357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
924d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
925d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                        VK_LValue, SourceLocation());
926d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(&tempDRE);
927d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = D->getType();
92857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    elementIsVariable = true;
929d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  } else {
930d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = LValue(); // suppress warning
931d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = cast<Expr>(S.getElement())->getType();
93257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    elementIsVariable = false;
933d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
934d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  const llvm::Type *convertedElementType = ConvertType(elementType);
935f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
936d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the buffer out of the enumeration state.
937d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // TODO: this pointer should actually be invariant between
938d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes, which would help us do certain loop optimizations.
939d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *StateItemsPtr =
940d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
941d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *EnumStateItems =
942d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateItemsPtr, "stateitems");
943f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
944d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the value at the current index from the buffer.
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *CurrentItemPtr =
946d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
947d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
9481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
949d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Cast that value to the right type.
950d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
951d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                                      "currentitem");
9521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
953d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Make sure we have an l-value.  Yes, this gets evaluated every
954d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // time through the loop.
95557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (!elementIsVariable)
956d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
9571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
958d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType);
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  // If we do have an element variable, this assignment is the end of
96157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  // its initialization.
96257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (elementIsVariable)
96357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    EmitAutoVarCleanups(variable);
96457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
965d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Perform the loop body, setting up break and continue labels.
966e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
967d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  {
968d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    RunCleanupsScope Scope(*this);
969d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    EmitStmt(S.getBody());
970d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
971f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  BreakContinueStack.pop_back();
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
973d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Destroy the element variable now.
974d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  elementVariableScope.ForceCleanup();
975d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
976d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether there are more elements.
977ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(AfterBody.getBlock());
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
979d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
980d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
981d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // First we check in the local buffer.
982d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *indexPlusOne
983d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
984d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
985d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we haven't overrun the buffer yet, we can continue.
986d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
987d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       LoopBodyBB, FetchMoreBB);
988f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian
989d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(indexPlusOne, AfterBody.getBlock());
990d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(count, AfterBody.getBlock());
991f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
992d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, we have to fetch more elements.
993d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(FetchMoreBB);
9941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CountRV =
996ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
997f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             FastEnumSel,
999c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1001d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we got a zero count, we're done.
1002d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *refetchCount = CountRV.getScalarVal();
1003d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1004d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // (note that the message send might split FetchMoreBB)
1005d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, Builder.GetInsertBlock());
1006d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(refetchCount, Builder.GetInsertBlock());
1007d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1008d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1009d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopBodyBB);
10101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1011f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // No more elements.
1012d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(EmptyBB);
1013f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
101457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (!elementIsVariable) {
1015f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    // If the element was not a declaration, set it to be null.
1016f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1017d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1018d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1019d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType);
1020f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  }
1021f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1022bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  if (DI) {
1023bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->setLocation(S.getSourceRange().getEnd());
1024bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->EmitRegionEnd(Builder);
1025bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  }
1026bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
1027ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopEnd.getBlock());
10283d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
10293d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
10301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
1031f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitTryStmt(*this, S);
103264d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
103364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
10341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
103564d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  CGM.getObjCRuntime().EmitThrowStmt(*this, S);
103664d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
103764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
103810cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt(
10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              const ObjCAtSynchronizedStmt &S) {
1040f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
104110cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner}
104210cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner
10432979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {}
1044