CGObjC.cpp revision b19c76e857d21b722d0a207cb45b26d7cb20a73f
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) {
1227c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
1234800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel  // Check if we should generate debug info for this method.
1244800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel  if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
1254800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel    DebugInfo = CGM.getDebugInfo();
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
1321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args.push_back(std::make_pair(OMD->getSelfDecl(),
1337c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                OMD->getSelfDecl()->getType()));
1347c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  Args.push_back(std::make_pair(OMD->getCmdDecl(),
1357c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                OMD->getCmdDecl()->getType()));
1364111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
13789951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
13889951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = OMD->param_end(); PI != E; ++PI)
13989951a86b594513c2a013532ed45d197413b1087Chris Lattner    Args.push_back(std::make_pair(*PI, (*PI)->getType()));
140b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
14114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  CurGD = OMD;
14214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
143a92d613a1a3881fb5002ce99e5721056161bcd28Devang Patel  StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart());
144af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
145af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
146af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method.  An Objective-C method is a C function with
1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture.
148af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
149679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  StartObjCMethod(OMD, OMD->getClassInterface());
1506fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  EmitStmt(OMD->getBody());
1516fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  FinishFunction(OMD->getBodyRBrace());
152af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
153af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
154f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump// FIXME: I wasn't sure about the synthesis approach. If we end up generating an
155f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump// AST for the whole body we can just fall back to having a GenerateFunction
156f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump// which takes the body Stmt.
157af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
158af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// GenerateObjCGetter - Generate an Objective-C property getter
159489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
160489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category.
161fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
162fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                         const ObjCPropertyImplDecl *PID) {
163c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
164af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
16515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  bool IsAtomic =
16615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
167af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
168af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  assert(OMD && "Invalid call to generate getter (empty method)");
169679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  StartObjCMethod(OMD, IMP->getClassInterface());
17015bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian
171c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  // Determine if we should use an objc_getProperty call for
172447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian  // this. Non-atomic properties are directly evaluated.
173447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian  // atomic 'copy' and 'retain' properties are also directly
174447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian  // evaluated in gc-only mode.
175c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
17615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian      IsAtomic &&
177447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian      (PD->getSetterKind() == ObjCPropertyDecl::Copy ||
178447d7aeb9499d7ade42be7d63fa03b37b1d2fc09Fariborz Jahanian       PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *GetPropertyFn =
180c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      CGM.getObjCRuntime().GetPropertyGetFunction();
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    if (!GetPropertyFn) {
183c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
184c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      FinishFunction();
185c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      return;
186c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    }
187c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
188c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
189c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // FIXME: Can't this be simpler? This might even be worse than the
190c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // corresponding gcc code.
191c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CodeGenTypes &Types = CGM.getTypes();
192c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    ValueDecl *Cmd = OMD->getCmdDecl();
193c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
194c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    QualType IdTy = getContext().getObjCIdType();
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *SelfAsId =
196c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
197fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian    llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
198c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    llvm::Value *True =
1994a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
200c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CallArgList Args;
201c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
202c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
203c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
204c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
205e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // FIXME: We shouldn't need to get the function info here, the
206e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // runtime already should have computed it to build the function.
20704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
208264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                               FunctionType::ExtInfo()),
209f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                         GetPropertyFn, ReturnValueSlot(), Args);
210c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // We need to fix the type here. Ivars with copy & retain are
211c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // always objects so we don't need to worry about complex or
212c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // aggregates.
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
214c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar                                           Types.ConvertType(PD->getType())));
215c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    EmitReturnOfRValue(RV, PD->getType());
216c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  } else {
2171b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian    if (Ivar->getType()->isAnyComplexType()) {
21897a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
21997a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian                                    Ivar, 0);
2201b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian      ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
2211b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian                                               LV.isVolatileQualified());
2221b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian      StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
2231b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian    }
2241b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian    else if (hasAggregateLLVMType(Ivar->getType())) {
22515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian      bool IsStrong = false;
22615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian      if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType())))
2270b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian          && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
2288fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall          && CGM.getObjCRuntime().GetGetStructFunction()) {
22997a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
23097a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian                                      Ivar, 0);
2310b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        llvm::Value *GetCopyStructFn =
2328fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall          CGM.getObjCRuntime().GetGetStructFunction();
2330b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        CodeGenTypes &Types = CGM.getTypes();
2340b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        // objc_copyStruct (ReturnValue, &structIvar,
2350b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        //                  sizeof (Type of Ivar), isAtomic, false);
2360b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        CallArgList Args;
2370b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue,
2380b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                    Types.ConvertType(getContext().VoidPtrTy)));
2390b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
2400b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
2410b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                    Types.ConvertType(getContext().VoidPtrTy)));
2420b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
2430b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        // sizeof (Type of Ivar)
244fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        CharUnits Size =  getContext().getTypeSizeInChars(Ivar->getType());
2450b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        llvm::Value *SizeVal =
246fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck          llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
247fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                                 Size.getQuantity());
2480b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        Args.push_back(std::make_pair(RValue::get(SizeVal),
2490b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                      getContext().LongTy));
2500b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        llvm::Value *isAtomic =
25108adf327cdcbf838990915bcdaebfdaf553144b7Fariborz Jahanian          llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
25208adf327cdcbf838990915bcdaebfdaf553144b7Fariborz Jahanian                                 IsAtomic ? 1 : 0);
2530b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        Args.push_back(std::make_pair(RValue::get(isAtomic),
2540b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                      getContext().BoolTy));
25515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian        llvm::Value *hasStrong =
25615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian          llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
25715bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian                                 IsStrong ? 1 : 0);
25815bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian        Args.push_back(std::make_pair(RValue::get(hasStrong),
25915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian                                      getContext().BoolTy));
2600b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian        EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
2610b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                       FunctionType::ExtInfo()),
2620b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                 GetCopyStructFn, ReturnValueSlot(), Args);
2630b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian      }
26497a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian      else {
26597a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        if (PID->getGetterCXXConstructor()) {
26697a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian          ReturnStmt *Stmt =
26797a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian            new (getContext()) ReturnStmt(SourceLocation(),
2685077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                          PID->getGetterCXXConstructor(),
2695077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                          0);
27097a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian          EmitReturnStmt(*Stmt);
27197a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        }
27297a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        else {
27397a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian          LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
27497a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian                                        Ivar, 0);
27597a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian          EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
27697a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian        }
27797a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian      }
278b3589f44c5d295cd41de2c83f3475116835eeebdMike Stump    } else {
27997a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
28097a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian                                    Ivar, 0);
281ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian      CodeGenTypes &Types = CGM.getTypes();
282ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian      RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
283ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian      RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       Types.ConvertType(PD->getType())));
285ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian      EmitReturnOfRValue(RV, PD->getType());
286ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian    }
287c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
288af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
289af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  FinishFunction();
290af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
291af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
292af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// GenerateObjCSetter - Generate an Objective-C property setter
293489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
294489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category.
295fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
296fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                         const ObjCPropertyImplDecl *PID) {
29786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
298af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
299af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
300af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  assert(OMD && "Invalid call to generate setter (empty method)");
301679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  StartObjCMethod(OMD, IMP->getClassInterface());
302b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
30386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
3041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool IsAtomic =
30586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
30686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar
30786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // Determine if we should use an objc_setProperty call for
30886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // this. Properties with 'copy' semantics always use it, as do
30986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // non-atomic properties with 'release' semantics as long as we are
31086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  // not in gc-only mode.
31186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  if (IsCopy ||
31286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
31386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar       PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *SetPropertyFn =
31586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      CGM.getObjCRuntime().GetPropertySetFunction();
3161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    if (!SetPropertyFn) {
31886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
31986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      FinishFunction();
32086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      return;
32186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    }
3221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Emit objc_setProperty((id) self, _cmd, offset, arg,
32486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    //                       <is-atomic>, <is-copy>).
32586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    // FIXME: Can't this be simpler? This might even be worse than the
32686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    // corresponding gcc code.
32786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    CodeGenTypes &Types = CGM.getTypes();
32886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    ValueDecl *Cmd = OMD->getCmdDecl();
32986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
33086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    QualType IdTy = getContext().getObjCIdType();
3311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *SelfAsId =
33286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
333fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian    llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
33489951a86b594513c2a013532ed45d197413b1087Chris Lattner    llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
3351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *ArgAsId =
33686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
33786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar                            Types.ConvertType(IdTy));
33886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    llvm::Value *True =
3394a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
34086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    llvm::Value *False =
3414a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
34286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    CallArgList Args;
34386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
34486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
34586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
34686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
34886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar                                  getContext().BoolTy));
3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
35086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar                                  getContext().BoolTy));
351f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: We shouldn't need to get the function info here, the runtime
352f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // already should have computed it to build the function.
35304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
354264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                   FunctionType::ExtInfo()),
355264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola             SetPropertyFn,
356f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson             ReturnValueSlot(), Args);
3570b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) &&
3580b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian             !Ivar->getType()->isAnyComplexType() &&
3590b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian             IndirectObjCSetterArg(*CurFnInfo)
3608fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall             && CGM.getObjCRuntime().GetSetStructFunction()) {
3610b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    // objc_copyStruct (&structIvar, &Arg,
3620b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    //                  sizeof (struct something), true, false);
3630b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    llvm::Value *GetCopyStructFn =
3648fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall      CGM.getObjCRuntime().GetSetStructFunction();
3650b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    CodeGenTypes &Types = CGM.getTypes();
3660b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    CallArgList Args;
3670b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
3680b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(),
3690b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                    Types.ConvertType(getContext().VoidPtrTy)));
3700b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
3710b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
3720b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    llvm::Value *ArgAsPtrTy =
3730b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian      Builder.CreateBitCast(Arg,
3740b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                            Types.ConvertType(getContext().VoidPtrTy));
3750b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    RV = RValue::get(ArgAsPtrTy);
3760b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    Args.push_back(std::make_pair(RV, getContext().VoidPtrTy));
3770b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    // sizeof (Type of Ivar)
378fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck    CharUnits Size =  getContext().getTypeSizeInChars(Ivar->getType());
3790b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    llvm::Value *SizeVal =
380fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck      llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
381fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                             Size.getQuantity());
3820b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    Args.push_back(std::make_pair(RValue::get(SizeVal),
3830b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                  getContext().LongTy));
3840b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    llvm::Value *True =
3850b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
3860b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
3870b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    llvm::Value *False =
3880b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian      llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
3890b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy));
3900b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian    EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
3910b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian                                   FunctionType::ExtInfo()),
3920b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian             GetCopyStructFn, ReturnValueSlot(), Args);
39397a73cd8e2b81f5aed9f59e07e7787e3fd3b8d00Fariborz Jahanian  } else if (PID->getSetterCXXAssignment()) {
3942a41637a995affa1563f4d82a8b026e326a2faa0John McCall    EmitIgnoredExpr(PID->getSetterCXXAssignment());
39586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  } else {
39645e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    // FIXME: Find a clean way to avoid AST node creation.
39786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    SourceLocation Loc = PD->getLocation();
39886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    ValueDecl *Self = OMD->getSelfDecl();
39986957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
400f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc);
40189951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *ArgDecl = *OMD->param_begin();
402f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), VK_LValue, Loc);
40345e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
40445e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar
40545e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    // The property type can differ from the ivar type in some situations with
40645e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    // Objective-C pointer types, we can always bit cast the RHS in these cases.
40745e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    if (getContext().getCanonicalType(Ivar->getType()) !=
40845e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar        getContext().getCanonicalType(ArgDecl->getType())) {
409f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
4102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                 Ivar->getType(), CK_BitCast, &Arg,
4115baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                 VK_RValue);
4122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
413f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                            Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
41445e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar      EmitStmt(&Assign);
41545e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    } else {
4162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
417f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                            Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
41845e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar      EmitStmt(&Assign);
41945e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar    }
42086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  }
421af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
422af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  FinishFunction();
4234111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
4244111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
425109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
426109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 ObjCMethodDecl *MD,
427109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 bool ctor) {
428cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
429109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
430109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  StartObjCMethod(MD, IMP->getClassInterface());
431109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
432109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian       E = IMP->init_end(); B != E; ++B) {
433cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Member = (*B);
434109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    IvarInitializers.push_back(Member);
435109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  }
436109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  if (ctor) {
437109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) {
438cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt      CXXCtorInitializer *IvarInit = IvarInitializers[I];
43900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Field = IvarInit->getAnyMember();
440109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      ObjCIvarDecl  *Ivar = cast<ObjCIvarDecl>(Field);
4419b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
4429b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian                                    LoadObjCSelf(), Ivar, 0);
443558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
444109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    }
445109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    // constructor returns 'self'.
446109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    CodeGenTypes &Types = CGM.getTypes();
447109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    QualType IdTy(CGM.getContext().getObjCIdType());
448109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    llvm::Value *SelfAsId =
449109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
450109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
451bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth  } else {
452109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    // dtor
453109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    for (size_t i = IvarInitializers.size(); i > 0; --i) {
45400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Field = IvarInitializers[i - 1]->getAnyMember();
455109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      QualType FieldType = Field->getType();
4569b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian      const ConstantArrayType *Array =
4579b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian        getContext().getAsConstantArrayType(FieldType);
4589b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian      if (Array)
4599b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian        FieldType = getContext().getBaseElementType(FieldType);
4609b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian
461109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      ObjCIvarDecl  *Ivar = cast<ObjCIvarDecl>(Field);
462109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
463109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                    LoadObjCSelf(), Ivar, 0);
464109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      const RecordType *RT = FieldType->getAs<RecordType>();
465109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
4661d110e05e0ff48c1c7a483d6b7fd094cdf28316aDouglas Gregor      CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor();
467bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth      if (!Dtor->isTrivial()) {
4688b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian        if (Array) {
4698b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian          const llvm::Type *BasePtr = ConvertType(FieldType);
4708b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian          BasePtr = llvm::PointerType::getUnqual(BasePtr);
4718b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian          llvm::Value *BaseAddrPtr =
4728b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian            Builder.CreateBitCast(LV.getAddress(), BasePtr);
4738b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian          EmitCXXAggrDestructorCall(Dtor,
4748b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian                                    Array, BaseAddrPtr);
475bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth        } else {
4768b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian          EmitCXXDestructorCall(Dtor,
4778b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian                                Dtor_Complete, /*ForVirtualBase=*/false,
4788b688ed1d56065458d9e22f813577e0dff5c488eFariborz Jahanian                                LV.getAddress());
479bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth        }
480bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth      }
481bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth    }
482109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  }
483109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  FinishFunction();
484109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian}
485109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
4860b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
4870b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
4880b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  it++; it++;
4890b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  const ABIArgInfo &AI = it->info;
4900b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  // FIXME. Is this sufficient check?
4910b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  return (AI.getKind() == ABIArgInfo::Indirect);
4920b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian}
4930b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian
49415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
49515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
49615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return false;
49715bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
49815bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return FDTTy->getDecl()->hasObjectMember();
49915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  return false;
50015bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian}
50115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian
502c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() {
503b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
504b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
5054111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
5064111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
50745012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() {
50845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
50945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
51014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
51114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    getContext().getCanonicalType(selfDecl->getType()));
51245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  return PTy->getPointeeType();
51345012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian}
51445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian
515e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallLValue
516e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallCodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
517e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // This is a special l-value that just issues sends when we load or
518e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // store through it.
519e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
520e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // For certain base kinds, we need to emit the base immediately.
521e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Base;
522e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  if (E->isSuperReceiver())
523e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = LoadObjCSelf();
524e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  else if (E->isClassReceiver())
525e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
526e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  else
527e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = EmitScalarExpr(E->getBase());
528e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  return LValue::MakePropertyRef(E, Base);
529e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall}
530e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
531e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallstatic RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
532e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       ReturnValueSlot Return,
533e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       QualType ResultType,
534e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       Selector S,
535e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       llvm::Value *Receiver,
536e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       const CallArgList &CallArgs) {
537e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
538f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian  bool isClassMessage = OMD->isClassMethod();
539f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian  bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
540e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  return CGF.CGM.getObjCRuntime()
541e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                .GenerateMessageSendSuper(CGF, Return, ResultType,
542e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          S, OMD->getClassInterface(),
543e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          isCategoryImpl, Receiver,
544e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          isClassMessage, CallArgs);
545f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian}
546f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian
547119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCallRValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
548119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                                    ReturnValueSlot Return) {
549119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
55012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  QualType ResultType;
55112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  Selector S;
55212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isExplicitProperty()) {
55312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    const ObjCPropertyDecl *Property = E->getExplicitProperty();
55412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    S = Property->getGetterName();
55512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    ResultType = E->getType();
556b3589f44c5d295cd41de2c83f3475116835eeebdMike Stump  } else {
55712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter();
55812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    S = Getter->getSelector();
55912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    ResultType = Getter->getResultType(); // with reference!
5605daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian  }
56112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
562e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
563e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
564e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // Accesses to 'super' follow a different code path.
56512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isSuperReceiver())
566e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    return GenerateMessageSendSuper(*this, Return, ResultType,
567e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                    S, Receiver, CallArgList());
56812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
569119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCInterfaceDecl *ReceiverClass
570119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall    = (E->isClassReceiver() ? E->getClassReceiver() : 0);
57112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  return CGM.getObjCRuntime().
57212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall             GenerateMessageSend(*this, Return, ResultType, S,
57312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                 Receiver, CallArgList(), ReceiverClass);
5749c3fc703b29a31d40bcf5027dbb4784dd393804eDaniel Dunbar}
5759c3fc703b29a31d40bcf5027dbb4784dd393804eDaniel Dunbar
576119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCallvoid CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
577119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                                        LValue Dst) {
578119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
57912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  Selector S = E->getSetterSelector();
58012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  QualType ArgType;
58112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isImplicitProperty()) {
58212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    const ObjCMethodDecl *Setter = E->getImplicitPropertySetter();
58312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    ObjCMethodDecl::param_iterator P = Setter->param_begin();
58412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    ArgType = (*P)->getType();
58512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  } else {
58612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    ArgType = E->getType();
58712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  }
588b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  // FIXME. Other than scalars, AST is not adequate for setter and
589b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  // getter type mismatches which require conversion.
590b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  if (Src.isScalar()) {
591b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    llvm::Value *SrcVal = Src.getScalarVal();
592b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    QualType DstType = getContext().getCanonicalType(ArgType);
593b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    const llvm::Type *DstTy = ConvertType(DstType);
594b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    if (SrcVal->getType() != DstTy)
595b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian      Src =
596b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian        RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
597b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  }
598b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian
599e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  CallArgList Args;
600e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  Args.push_back(std::make_pair(Src, ArgType));
601e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
602e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
603e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  QualType ResultType = getContext().VoidTy;
604e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
60512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isSuperReceiver()) {
606e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    GenerateMessageSendSuper(*this, ReturnValueSlot(),
607e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                             ResultType, S, Receiver, Args);
60812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    return;
60912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  }
61012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
611119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCInterfaceDecl *ReceiverClass
612119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall    = (E->isClassReceiver() ? E->getClassReceiver() : 0);
61312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
61412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
615e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                           ResultType, S, Receiver, Args,
616e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                           ReceiverClass);
61785c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar}
61885c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar
61974391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Constant *EnumerationMutationFn =
621c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.getObjCRuntime().EnumerationMutationFunction();
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
623c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  if (!EnumerationMutationFn) {
624c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
625c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    return;
626c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
627c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
628bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  CGDebugInfo *DI = getDebugInfo();
629bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  if (DI) {
630bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->setLocation(S.getSourceRange().getBegin());
631bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->EmitRegionStart(Builder);
632bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  }
633bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
634d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
635d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
637f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Fast enumeration state.
638f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  QualType StateTy = getContext().getObjCFastEnumerationStateType();
639195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
6401884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  EmitNullInitialization(StatePtr, StateTy);
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
642f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Number of elements in the items array.
6432abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  static const unsigned NumItems = 16;
6441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
645d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the countByEnumeratingWithState:objects:count: selector.
646ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  IdentifierInfo *II[] = {
647ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("countByEnumeratingWithState"),
648ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("objects"),
649ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("count")
650ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  };
651ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  Selector FastEnumSel =
652ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
653f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
654f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  QualType ItemsTy =
655f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    getContext().getConstantArrayType(getContext().getObjCIdType(),
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                      llvm::APInt(32, NumItems),
657f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                      ArrayType::Normal, 0);
658195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
660d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Emit the collection pointer.
661f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  llvm::Value *Collection = EmitScalarExpr(S.getCollection());
6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
663d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Send it our message:
664f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  CallArgList Args;
665d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
666d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The first argument is a temporary of the enumeration-state type.
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args.push_back(std::make_pair(RValue::get(StatePtr),
668f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                getContext().getPointerType(StateTy)));
6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
670d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The second argument is a temporary array with space for NumItems
671d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointers.  We'll actually be loading elements from the array
672d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointer written into the control state; this buffer is so that
673d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // collections that *aren't* backed by arrays can still queue up
674d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // batches of elements.
6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args.push_back(std::make_pair(RValue::get(ItemsPtr),
676f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                getContext().getPointerType(ItemsTy)));
6771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
678d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The third argument is the capacity of that temporary array.
679f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
6804a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
6811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args.push_back(std::make_pair(RValue::get(Count),
68246f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar                                getContext().UnsignedLongTy));
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
684d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start the enumeration.
6851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  RValue CountRV =
686ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
687f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
688f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             FastEnumSel,
689c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
690f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
691d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The initial number of objects that were returned in the buffer.
692d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialBufferLimit = CountRV.getScalarVal();
6931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
694d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
695d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
696f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
697d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
698f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
699d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If the limit pointer was zero to begin with, the collection is
700d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // empty; skip all this.
701d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
702d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopInitBB);
7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
704d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, initialize the loop.
705d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopInitBB);
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
707d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Save the initial mutations value.  This is the value at an
708d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // address that was written into the state object by
709d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // countByEnumeratingWithState:objects:count:.
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtrPtr =
7112abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson    Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
7132abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                                                      "mutationsptr");
7141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
715d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialMutations =
716d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
718d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start looping.  This is the point we return to whenever we have a
719d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // fresh, non-empty batch of objects.
720d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
721d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopBodyBB);
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
723d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current index into the buffer.
724d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, "forcoll.index");
725d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, LoopInitBB);
726f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
727d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current buffer size.
728d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, "forcoll.count");
729d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(initialBufferLimit, LoopInitBB);
730f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
731d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether the mutations value has changed from where it was
732d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // at start.  StateMutationsPtr should actually be invariant between
733d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes.
7342abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
735d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *currentMutations
736d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateLoad(StateMutationsPtr, "statemutations");
7371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
738d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
739d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcool.notmutated");
7401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
741d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
742d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       WasNotMutatedBB, WasMutatedBB);
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
744d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If so, call the enumeration-mutation function.
745d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasMutatedBB);
7462abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  llvm::Value *V =
7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Builder.CreateBitCast(Collection,
7482abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                          ConvertType(getContext().getObjCIdType()),
7492abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                          "tmp");
7502b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar  CallArgList Args2;
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Args2.push_back(std::make_pair(RValue::get(V),
7522b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar                                getContext().getObjCIdType()));
753f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We shouldn't need to get the function info here, the runtime already
754f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // should have computed it to build the function.
75504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
756264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                          FunctionType::ExtInfo()),
757f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson           EnumerationMutationFn, ReturnValueSlot(), Args2);
7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
759d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, or if the mutation function returns, just continue.
760d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasNotMutatedBB);
7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
762d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Initialize the element variable.
763d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  RunCleanupsScope elementVariableScope(*this);
764d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  bool elementIsDecl;
765d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  LValue elementLValue;
766d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  QualType elementType;
767d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
768d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    EmitStmt(SD);
769d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
770f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
771d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
772d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                        VK_LValue, SourceLocation());
773d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(&tempDRE);
774d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = D->getType();
775d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementIsDecl = true;
776d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  } else {
777d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = LValue(); // suppress warning
778d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = cast<Expr>(S.getElement())->getType();
779d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementIsDecl = false;
780d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
781d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  const llvm::Type *convertedElementType = ConvertType(elementType);
782f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
783d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the buffer out of the enumeration state.
784d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // TODO: this pointer should actually be invariant between
785d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes, which would help us do certain loop optimizations.
786d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *StateItemsPtr =
787d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
788d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *EnumStateItems =
789d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateItemsPtr, "stateitems");
790f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
791d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the value at the current index from the buffer.
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *CurrentItemPtr =
793d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
794d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
7951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
796d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Cast that value to the right type.
797d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
798d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                                      "currentitem");
7991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
800d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Make sure we have an l-value.  Yes, this gets evaluated every
801d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // time through the loop.
802d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  if (!elementIsDecl)
803d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
805d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType);
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
807d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Perform the loop body, setting up break and continue labels.
808e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
809d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  {
810d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    RunCleanupsScope Scope(*this);
811d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    EmitStmt(S.getBody());
812d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
813f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  BreakContinueStack.pop_back();
8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
815d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Destroy the element variable now.
816d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  elementVariableScope.ForceCleanup();
817d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
818d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether there are more elements.
819ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(AfterBody.getBlock());
8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
821d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
822d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
823d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // First we check in the local buffer.
824d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *indexPlusOne
825d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
826d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
827d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we haven't overrun the buffer yet, we can continue.
828d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
829d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       LoopBodyBB, FetchMoreBB);
830f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian
831d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(indexPlusOne, AfterBody.getBlock());
832d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(count, AfterBody.getBlock());
833f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
834d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, we have to fetch more elements.
835d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(FetchMoreBB);
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CountRV =
838ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
839f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
8401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             FastEnumSel,
841c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
8421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
843d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we got a zero count, we're done.
844d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *refetchCount = CountRV.getScalarVal();
845d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
846d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // (note that the message send might split FetchMoreBB)
847d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, Builder.GetInsertBlock());
848d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(refetchCount, Builder.GetInsertBlock());
849d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
850d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
851d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopBodyBB);
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
853f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // No more elements.
854d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(EmptyBB);
855f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
856d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  if (!elementIsDecl) {
857f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    // If the element was not a declaration, set it to be null.
858f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
859d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
860d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
861d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType);
862f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  }
863f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
864bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  if (DI) {
865bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->setLocation(S.getSourceRange().getEnd());
866bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel    DI->EmitRegionEnd(Builder);
867bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  }
868bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
869ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopEnd.getBlock());
8703d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
8713d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
873f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitTryStmt(*this, S);
87464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
87564d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
87764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  CGM.getObjCRuntime().EmitThrowStmt(*this, S);
87864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
87964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
88010cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt(
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              const ObjCAtSynchronizedStmt &S) {
882f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
88310cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner}
88410cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner
8852979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {}
886