CGExpr.cpp revision 8ac2d449820fd0df00fcbde5bf82165c1f49854d
1//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "CGCall.h"
17#include "CGCXXABI.h"
18#include "CGRecordLayout.h"
19#include "CGObjCRuntime.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclObjC.h"
22#include "llvm/Intrinsics.h"
23#include "clang/Frontend/CodeGenOptions.h"
24#include "llvm/Target/TargetData.h"
25using namespace clang;
26using namespace CodeGen;
27
28//===--------------------------------------------------------------------===//
29//                        Miscellaneous Helper Methods
30//===--------------------------------------------------------------------===//
31
32/// CreateTempAlloca - This creates a alloca and inserts it into the entry
33/// block.
34llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
35                                                    const llvm::Twine &Name) {
36  if (!Builder.isNamePreserving())
37    return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
38  return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
39}
40
41void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
42                                     llvm::Value *Init) {
43  llvm::StoreInst *Store = new llvm::StoreInst(Init, Var);
44  llvm::BasicBlock *Block = AllocaInsertPt->getParent();
45  Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
46}
47
48llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
49                                                const llvm::Twine &Name) {
50  llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
51  // FIXME: Should we prefer the preferred type alignment here?
52  CharUnits Align = getContext().getTypeAlignInChars(Ty);
53  Alloc->setAlignment(Align.getQuantity());
54  return Alloc;
55}
56
57llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
58                                                 const llvm::Twine &Name) {
59  llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
60  // FIXME: Should we prefer the preferred type alignment here?
61  CharUnits Align = getContext().getTypeAlignInChars(Ty);
62  Alloc->setAlignment(Align.getQuantity());
63  return Alloc;
64}
65
66/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
67/// expression and compare the result against zero, returning an Int1Ty value.
68llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
69  if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
70    llvm::Value *MemPtr = EmitScalarExpr(E);
71    return CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT);
72  }
73
74  QualType BoolTy = getContext().BoolTy;
75  if (!E->getType()->isAnyComplexType())
76    return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
77
78  return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
79}
80
81/// EmitAnyExpr - Emit code to compute the specified expression which
82/// can have any type.  The result is returned as an RValue struct.
83/// If this is an aggregate expression, AggSlot indicates where the
84/// result should be returned.
85RValue CodeGenFunction::EmitAnyExpr(const Expr *E, AggValueSlot AggSlot,
86                                    bool IgnoreResult) {
87  if (!hasAggregateLLVMType(E->getType()))
88    return RValue::get(EmitScalarExpr(E, IgnoreResult));
89  else if (E->getType()->isAnyComplexType())
90    return RValue::getComplex(EmitComplexExpr(E, false, false,
91                                              IgnoreResult, IgnoreResult));
92
93  EmitAggExpr(E, AggSlot, IgnoreResult);
94  return AggSlot.asRValue();
95}
96
97/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
98/// always be accessible even if no aggregate location is provided.
99RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
100  AggValueSlot AggSlot = AggValueSlot::ignored();
101
102  if (hasAggregateLLVMType(E->getType()) &&
103      !E->getType()->isAnyComplexType())
104    AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
105  return EmitAnyExpr(E, AggSlot);
106}
107
108/// EmitAnyExprToMem - Evaluate an expression into a given memory
109/// location.
110void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
111                                       llvm::Value *Location,
112                                       bool IsLocationVolatile,
113                                       bool IsInit) {
114  if (E->getType()->isComplexType())
115    EmitComplexExprIntoAddr(E, Location, IsLocationVolatile);
116  else if (hasAggregateLLVMType(E->getType()))
117    EmitAggExpr(E, AggValueSlot::forAddr(Location, IsLocationVolatile, IsInit));
118  else {
119    RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
120    LValue LV = MakeAddrLValue(Location, E->getType());
121    EmitStoreThroughLValue(RV, LV, E->getType());
122  }
123}
124
125/// \brief An adjustment to be made to the temporary created when emitting a
126/// reference binding, which accesses a particular subobject of that temporary.
127struct SubobjectAdjustment {
128  enum { DerivedToBaseAdjustment, FieldAdjustment } Kind;
129
130  union {
131    struct {
132      const CastExpr *BasePath;
133      const CXXRecordDecl *DerivedClass;
134    } DerivedToBase;
135
136    FieldDecl *Field;
137  };
138
139  SubobjectAdjustment(const CastExpr *BasePath,
140                      const CXXRecordDecl *DerivedClass)
141    : Kind(DerivedToBaseAdjustment)
142  {
143    DerivedToBase.BasePath = BasePath;
144    DerivedToBase.DerivedClass = DerivedClass;
145  }
146
147  SubobjectAdjustment(FieldDecl *Field)
148    : Kind(FieldAdjustment)
149  {
150    this->Field = Field;
151  }
152};
153
154static llvm::Value *
155CreateReferenceTemporary(CodeGenFunction& CGF, QualType Type,
156                         const NamedDecl *InitializedDecl) {
157  if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
158    if (VD->hasGlobalStorage()) {
159      llvm::SmallString<256> Name;
160      CGF.CGM.getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Name);
161
162      const llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type);
163
164      // Create the reference temporary.
165      llvm::GlobalValue *RefTemp =
166        new llvm::GlobalVariable(CGF.CGM.getModule(),
167                                 RefTempTy, /*isConstant=*/false,
168                                 llvm::GlobalValue::InternalLinkage,
169                                 llvm::Constant::getNullValue(RefTempTy),
170                                 Name.str());
171      return RefTemp;
172    }
173  }
174
175  return CGF.CreateMemTemp(Type, "ref.tmp");
176}
177
178static llvm::Value *
179EmitExprForReferenceBinding(CodeGenFunction &CGF, const Expr *E,
180                            llvm::Value *&ReferenceTemporary,
181                            const CXXDestructorDecl *&ReferenceTemporaryDtor,
182                            const NamedDecl *InitializedDecl) {
183  if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
184    E = DAE->getExpr();
185
186  if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
187    CodeGenFunction::RunCleanupsScope Scope(CGF);
188
189    return EmitExprForReferenceBinding(CGF, TE->getSubExpr(),
190                                       ReferenceTemporary,
191                                       ReferenceTemporaryDtor,
192                                       InitializedDecl);
193  }
194
195  RValue RV;
196  if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) {
197    // Emit the expression as an lvalue.
198    LValue LV = CGF.EmitLValue(E);
199    if (LV.isPropertyRef() || LV.isKVCRef()) {
200      QualType QT = E->getType();
201      RValue RV =
202        LV.isPropertyRef() ? CGF.EmitLoadOfPropertyRefLValue(LV, QT)
203                           : CGF.EmitLoadOfKVCRefLValue(LV, QT);
204      assert(RV.isScalar() && "EmitExprForReferenceBinding");
205      return RV.getScalarVal();
206    }
207
208    if (LV.isSimple())
209      return LV.getAddress();
210
211    // We have to load the lvalue.
212    RV = CGF.EmitLoadOfLValue(LV, E->getType());
213  } else {
214    QualType ResultTy = E->getType();
215
216    llvm::SmallVector<SubobjectAdjustment, 2> Adjustments;
217    while (true) {
218      if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
219        E = PE->getSubExpr();
220        continue;
221      }
222
223      if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
224        if ((CE->getCastKind() == CK_DerivedToBase ||
225             CE->getCastKind() == CK_UncheckedDerivedToBase) &&
226            E->getType()->isRecordType()) {
227          E = CE->getSubExpr();
228          CXXRecordDecl *Derived
229            = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
230          Adjustments.push_back(SubobjectAdjustment(CE, Derived));
231          continue;
232        }
233
234        if (CE->getCastKind() == CK_NoOp) {
235          E = CE->getSubExpr();
236          continue;
237        }
238      } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
239        if (ME->getBase()->isLvalue(CGF.getContext()) != Expr::LV_Valid &&
240            ME->getBase()->getType()->isRecordType()) {
241          if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
242            E = ME->getBase();
243            Adjustments.push_back(SubobjectAdjustment(Field));
244            continue;
245          }
246        }
247      }
248
249      // Nothing changed.
250      break;
251    }
252
253    // Create a reference temporary if necessary.
254    AggValueSlot AggSlot = AggValueSlot::ignored();
255    if (CGF.hasAggregateLLVMType(E->getType()) &&
256        !E->getType()->isAnyComplexType()) {
257      ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
258                                                    InitializedDecl);
259      AggSlot = AggValueSlot::forAddr(ReferenceTemporary, false,
260                                      InitializedDecl != 0);
261    }
262
263    RV = CGF.EmitAnyExpr(E, AggSlot);
264
265    if (InitializedDecl) {
266      // Get the destructor for the reference temporary.
267      if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
268        CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
269        if (!ClassDecl->hasTrivialDestructor())
270          ReferenceTemporaryDtor = ClassDecl->getDestructor();
271      }
272    }
273
274    // Check if need to perform derived-to-base casts and/or field accesses, to
275    // get from the temporary object we created (and, potentially, for which we
276    // extended the lifetime) to the subobject we're binding the reference to.
277    if (!Adjustments.empty()) {
278      llvm::Value *Object = RV.getAggregateAddr();
279      for (unsigned I = Adjustments.size(); I != 0; --I) {
280        SubobjectAdjustment &Adjustment = Adjustments[I-1];
281        switch (Adjustment.Kind) {
282        case SubobjectAdjustment::DerivedToBaseAdjustment:
283          Object =
284              CGF.GetAddressOfBaseClass(Object,
285                                        Adjustment.DerivedToBase.DerivedClass,
286                              Adjustment.DerivedToBase.BasePath->path_begin(),
287                              Adjustment.DerivedToBase.BasePath->path_end(),
288                                        /*NullCheckValue=*/false);
289          break;
290
291        case SubobjectAdjustment::FieldAdjustment: {
292          LValue LV =
293            CGF.EmitLValueForField(Object, Adjustment.Field, 0);
294          if (LV.isSimple()) {
295            Object = LV.getAddress();
296            break;
297          }
298
299          // For non-simple lvalues, we actually have to create a copy of
300          // the object we're binding to.
301          QualType T = Adjustment.Field->getType().getNonReferenceType()
302                                                  .getUnqualifiedType();
303          Object = CreateReferenceTemporary(CGF, T, InitializedDecl);
304          LValue TempLV = CGF.MakeAddrLValue(Object,
305                                             Adjustment.Field->getType());
306          CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV, T), TempLV, T);
307          break;
308        }
309
310        }
311      }
312
313      const llvm::Type *ResultPtrTy = CGF.ConvertType(ResultTy)->getPointerTo();
314      return CGF.Builder.CreateBitCast(Object, ResultPtrTy, "temp");
315    }
316  }
317
318  if (RV.isAggregate())
319    return RV.getAggregateAddr();
320
321  // Create a temporary variable that we can bind the reference to.
322  ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
323                                                InitializedDecl);
324
325
326  unsigned Alignment =
327    CGF.getContext().getTypeAlignInChars(E->getType()).getQuantity();
328  if (RV.isScalar())
329    CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary,
330                          /*Volatile=*/false, Alignment, E->getType());
331  else
332    CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary,
333                           /*Volatile=*/false);
334  return ReferenceTemporary;
335}
336
337RValue
338CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E,
339                                            const NamedDecl *InitializedDecl) {
340  llvm::Value *ReferenceTemporary = 0;
341  const CXXDestructorDecl *ReferenceTemporaryDtor = 0;
342  llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary,
343                                                   ReferenceTemporaryDtor,
344                                                   InitializedDecl);
345  if (!ReferenceTemporaryDtor)
346    return RValue::get(Value);
347
348  // Make sure to call the destructor for the reference temporary.
349  if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
350    if (VD->hasGlobalStorage()) {
351      llvm::Constant *DtorFn =
352        CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete);
353      CGF.EmitCXXGlobalDtorRegistration(DtorFn,
354                                      cast<llvm::Constant>(ReferenceTemporary));
355
356      return RValue::get(Value);
357    }
358  }
359
360  PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary);
361
362  return RValue::get(Value);
363}
364
365
366/// getAccessedFieldNo - Given an encoded value and a result number, return the
367/// input field number being accessed.
368unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
369                                             const llvm::Constant *Elts) {
370  if (isa<llvm::ConstantAggregateZero>(Elts))
371    return 0;
372
373  return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
374}
375
376void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
377  if (!CatchUndefined)
378    return;
379
380  Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
381
382  const llvm::Type *IntPtrT = IntPtrTy;
383  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, &IntPtrT, 1);
384  const llvm::IntegerType *Int1Ty = llvm::Type::getInt1Ty(VMContext);
385
386  // In time, people may want to control this and use a 1 here.
387  llvm::Value *Arg = llvm::ConstantInt::get(Int1Ty, 0);
388  llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
389  llvm::BasicBlock *Cont = createBasicBlock();
390  llvm::BasicBlock *Check = createBasicBlock();
391  llvm::Value *NegativeOne = llvm::ConstantInt::get(IntPtrTy, -1ULL);
392  Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
393
394  EmitBlock(Check);
395  Builder.CreateCondBr(Builder.CreateICmpUGE(C,
396                                        llvm::ConstantInt::get(IntPtrTy, Size)),
397                       Cont, getTrapBB());
398  EmitBlock(Cont);
399}
400
401
402CodeGenFunction::ComplexPairTy CodeGenFunction::
403EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
404                         bool isInc, bool isPre) {
405  ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
406                                            LV.isVolatileQualified());
407
408  llvm::Value *NextVal;
409  if (isa<llvm::IntegerType>(InVal.first->getType())) {
410    uint64_t AmountVal = isInc ? 1 : -1;
411    NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
412
413    // Add the inc/dec to the real part.
414    NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
415  } else {
416    QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
417    llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
418    if (!isInc)
419      FVal.changeSign();
420    NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
421
422    // Add the inc/dec to the real part.
423    NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
424  }
425
426  ComplexPairTy IncVal(NextVal, InVal.second);
427
428  // Store the updated result through the lvalue.
429  StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
430
431  // If this is a postinc, return the value read from memory, otherwise use the
432  // updated value.
433  return isPre ? IncVal : InVal;
434}
435
436
437//===----------------------------------------------------------------------===//
438//                         LValue Expression Emission
439//===----------------------------------------------------------------------===//
440
441RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
442  if (Ty->isVoidType())
443    return RValue::get(0);
444
445  if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
446    const llvm::Type *EltTy = ConvertType(CTy->getElementType());
447    llvm::Value *U = llvm::UndefValue::get(EltTy);
448    return RValue::getComplex(std::make_pair(U, U));
449  }
450
451  // If this is a use of an undefined aggregate type, the aggregate must have an
452  // identifiable address.  Just because the contents of the value are undefined
453  // doesn't mean that the address can't be taken and compared.
454  if (hasAggregateLLVMType(Ty)) {
455    llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
456    return RValue::getAggregate(DestPtr);
457  }
458
459  return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
460}
461
462RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
463                                              const char *Name) {
464  ErrorUnsupported(E, Name);
465  return GetUndefRValue(E->getType());
466}
467
468LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
469                                              const char *Name) {
470  ErrorUnsupported(E, Name);
471  llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
472  return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
473}
474
475LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
476  LValue LV = EmitLValue(E);
477  if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
478    EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
479  return LV;
480}
481
482/// EmitLValue - Emit code to compute a designator that specifies the location
483/// of the expression.
484///
485/// This can return one of two things: a simple address or a bitfield reference.
486/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
487/// an LLVM pointer type.
488///
489/// If this returns a bitfield reference, nothing about the pointee type of the
490/// LLVM value is known: For example, it may not be a pointer to an integer.
491///
492/// If this returns a normal address, and if the lvalue's C type is fixed size,
493/// this method guarantees that the returned pointer type will point to an LLVM
494/// type of the same size of the lvalue's type.  If the lvalue has a variable
495/// length type, this is not possible.
496///
497LValue CodeGenFunction::EmitLValue(const Expr *E) {
498  llvm::DenseMap<const Expr *, LValue>::iterator I =
499                                      CGF.ConditionalSaveLValueExprs.find(E);
500  if (I != CGF.ConditionalSaveLValueExprs.end())
501    return I->second;
502
503  switch (E->getStmtClass()) {
504  default: return EmitUnsupportedLValue(E, "l-value expression");
505
506  case Expr::ObjCSelectorExprClass:
507  return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
508  case Expr::ObjCIsaExprClass:
509    return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
510  case Expr::BinaryOperatorClass:
511    return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
512  case Expr::CompoundAssignOperatorClass:
513    return EmitCompoundAssignOperatorLValue(cast<CompoundAssignOperator>(E));
514  case Expr::CallExprClass:
515  case Expr::CXXMemberCallExprClass:
516  case Expr::CXXOperatorCallExprClass:
517    return EmitCallExprLValue(cast<CallExpr>(E));
518  case Expr::VAArgExprClass:
519    return EmitVAArgExprLValue(cast<VAArgExpr>(E));
520  case Expr::DeclRefExprClass:
521    return EmitDeclRefLValue(cast<DeclRefExpr>(E));
522  case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
523  case Expr::PredefinedExprClass:
524    return EmitPredefinedLValue(cast<PredefinedExpr>(E));
525  case Expr::StringLiteralClass:
526    return EmitStringLiteralLValue(cast<StringLiteral>(E));
527  case Expr::ObjCEncodeExprClass:
528    return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
529
530  case Expr::BlockDeclRefExprClass:
531    return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
532
533  case Expr::CXXTemporaryObjectExprClass:
534  case Expr::CXXConstructExprClass:
535    return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
536  case Expr::CXXBindTemporaryExprClass:
537    return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
538  case Expr::CXXExprWithTemporariesClass:
539    return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
540  case Expr::CXXScalarValueInitExprClass:
541    return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E));
542  case Expr::CXXDefaultArgExprClass:
543    return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
544  case Expr::CXXTypeidExprClass:
545    return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
546
547  case Expr::ObjCMessageExprClass:
548    return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
549  case Expr::ObjCIvarRefExprClass:
550    return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
551  case Expr::ObjCPropertyRefExprClass:
552    return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
553  case Expr::ObjCImplicitSetterGetterRefExprClass:
554    return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
555  case Expr::StmtExprClass:
556    return EmitStmtExprLValue(cast<StmtExpr>(E));
557  case Expr::UnaryOperatorClass:
558    return EmitUnaryOpLValue(cast<UnaryOperator>(E));
559  case Expr::ArraySubscriptExprClass:
560    return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
561  case Expr::ExtVectorElementExprClass:
562    return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
563  case Expr::MemberExprClass:
564    return EmitMemberExpr(cast<MemberExpr>(E));
565  case Expr::CompoundLiteralExprClass:
566    return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
567  case Expr::ConditionalOperatorClass:
568    return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
569  case Expr::ChooseExprClass:
570    return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
571  case Expr::ImplicitCastExprClass:
572  case Expr::CStyleCastExprClass:
573  case Expr::CXXFunctionalCastExprClass:
574  case Expr::CXXStaticCastExprClass:
575  case Expr::CXXDynamicCastExprClass:
576  case Expr::CXXReinterpretCastExprClass:
577  case Expr::CXXConstCastExprClass:
578    return EmitCastLValue(cast<CastExpr>(E));
579  }
580}
581
582llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
583                                              unsigned Alignment, QualType Ty) {
584  llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
585  if (Volatile)
586    Load->setVolatile(true);
587  if (Alignment)
588    Load->setAlignment(Alignment);
589
590  // Bool can have different representation in memory than in registers.
591  llvm::Value *V = Load;
592  if (Ty->isBooleanType())
593    if (V->getType() != llvm::Type::getInt1Ty(VMContext))
594      V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
595
596  return V;
597}
598
599static bool isBooleanUnderlyingType(QualType Ty) {
600  if (const EnumType *ET = dyn_cast<EnumType>(Ty))
601    return ET->getDecl()->getIntegerType()->isBooleanType();
602  return false;
603}
604
605void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
606                                        bool Volatile, unsigned Alignment,
607                                        QualType Ty) {
608
609  if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
610    // Bool can have different representation in memory than in registers.
611    const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
612    Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
613  }
614
615  llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
616  if (Alignment)
617    Store->setAlignment(Alignment);
618}
619
620/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
621/// method emits the address of the lvalue, then loads the result as an rvalue,
622/// returning the rvalue.
623RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
624  if (LV.isObjCWeak()) {
625    // load of a __weak object.
626    llvm::Value *AddrWeakObj = LV.getAddress();
627    return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
628                                                             AddrWeakObj));
629  }
630
631  if (LV.isSimple()) {
632    llvm::Value *Ptr = LV.getAddress();
633
634    // Functions are l-values that don't require loading.
635    if (ExprType->isFunctionType())
636      return RValue::get(Ptr);
637
638    // Everything needs a load.
639    return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
640                                        LV.getAlignment(), ExprType));
641
642  }
643
644  if (LV.isVectorElt()) {
645    llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
646                                          LV.isVolatileQualified(), "tmp");
647    return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
648                                                    "vecext"));
649  }
650
651  // If this is a reference to a subset of the elements of a vector, either
652  // shuffle the input or extract/insert them as appropriate.
653  if (LV.isExtVectorElt())
654    return EmitLoadOfExtVectorElementLValue(LV, ExprType);
655
656  if (LV.isBitField())
657    return EmitLoadOfBitfieldLValue(LV, ExprType);
658
659  if (LV.isPropertyRef())
660    return EmitLoadOfPropertyRefLValue(LV, ExprType);
661
662  assert(LV.isKVCRef() && "Unknown LValue type!");
663  return EmitLoadOfKVCRefLValue(LV, ExprType);
664}
665
666RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
667                                                 QualType ExprType) {
668  const CGBitFieldInfo &Info = LV.getBitFieldInfo();
669
670  // Get the output type.
671  const llvm::Type *ResLTy = ConvertType(ExprType);
672  unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
673
674  // Compute the result as an OR of all of the individual component accesses.
675  llvm::Value *Res = 0;
676  for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
677    const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
678
679    // Get the field pointer.
680    llvm::Value *Ptr = LV.getBitFieldBaseAddr();
681
682    // Only offset by the field index if used, so that incoming values are not
683    // required to be structures.
684    if (AI.FieldIndex)
685      Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
686
687    // Offset by the byte offset, if used.
688    if (AI.FieldByteOffset) {
689      const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
690      Ptr = Builder.CreateBitCast(Ptr, i8PTy);
691      Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
692    }
693
694    // Cast to the access type.
695    const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
696                                                    ExprType.getAddressSpace());
697    Ptr = Builder.CreateBitCast(Ptr, PTy);
698
699    // Perform the load.
700    llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
701    if (AI.AccessAlignment)
702      Load->setAlignment(AI.AccessAlignment);
703
704    // Shift out unused low bits and mask out unused high bits.
705    llvm::Value *Val = Load;
706    if (AI.FieldBitStart)
707      Val = Builder.CreateLShr(Load, AI.FieldBitStart);
708    Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
709                                                            AI.TargetBitWidth),
710                            "bf.clear");
711
712    // Extend or truncate to the target size.
713    if (AI.AccessWidth < ResSizeInBits)
714      Val = Builder.CreateZExt(Val, ResLTy);
715    else if (AI.AccessWidth > ResSizeInBits)
716      Val = Builder.CreateTrunc(Val, ResLTy);
717
718    // Shift into place, and OR into the result.
719    if (AI.TargetBitOffset)
720      Val = Builder.CreateShl(Val, AI.TargetBitOffset);
721    Res = Res ? Builder.CreateOr(Res, Val) : Val;
722  }
723
724  // If the bit-field is signed, perform the sign-extension.
725  //
726  // FIXME: This can easily be folded into the load of the high bits, which
727  // could also eliminate the mask of high bits in some situations.
728  if (Info.isSigned()) {
729    unsigned ExtraBits = ResSizeInBits - Info.getSize();
730    if (ExtraBits)
731      Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
732                               ExtraBits, "bf.val.sext");
733  }
734
735  return RValue::get(Res);
736}
737
738RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
739                                                    QualType ExprType) {
740  return EmitObjCPropertyGet(LV.getPropertyRefExpr());
741}
742
743RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
744                                               QualType ExprType) {
745  return EmitObjCPropertyGet(LV.getKVCRefExpr());
746}
747
748// If this is a reference to a subset of the elements of a vector, create an
749// appropriate shufflevector.
750RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
751                                                         QualType ExprType) {
752  llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
753                                        LV.isVolatileQualified(), "tmp");
754
755  const llvm::Constant *Elts = LV.getExtVectorElts();
756
757  // If the result of the expression is a non-vector type, we must be extracting
758  // a single element.  Just codegen as an extractelement.
759  const VectorType *ExprVT = ExprType->getAs<VectorType>();
760  if (!ExprVT) {
761    unsigned InIdx = getAccessedFieldNo(0, Elts);
762    llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
763    return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
764  }
765
766  // Always use shuffle vector to try to retain the original program structure
767  unsigned NumResultElts = ExprVT->getNumElements();
768
769  llvm::SmallVector<llvm::Constant*, 4> Mask;
770  for (unsigned i = 0; i != NumResultElts; ++i) {
771    unsigned InIdx = getAccessedFieldNo(i, Elts);
772    Mask.push_back(llvm::ConstantInt::get(Int32Ty, InIdx));
773  }
774
775  llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
776  Vec = Builder.CreateShuffleVector(Vec,
777                                    llvm::UndefValue::get(Vec->getType()),
778                                    MaskV, "tmp");
779  return RValue::get(Vec);
780}
781
782
783
784/// EmitStoreThroughLValue - Store the specified rvalue into the specified
785/// lvalue, where both are guaranteed to the have the same type, and that type
786/// is 'Ty'.
787void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
788                                             QualType Ty) {
789  if (!Dst.isSimple()) {
790    if (Dst.isVectorElt()) {
791      // Read/modify/write the vector, inserting the new element.
792      llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
793                                            Dst.isVolatileQualified(), "tmp");
794      Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
795                                        Dst.getVectorIdx(), "vecins");
796      Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
797      return;
798    }
799
800    // If this is an update of extended vector elements, insert them as
801    // appropriate.
802    if (Dst.isExtVectorElt())
803      return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
804
805    if (Dst.isBitField())
806      return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
807
808    if (Dst.isPropertyRef())
809      return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
810
811    assert(Dst.isKVCRef() && "Unknown LValue type");
812    return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
813  }
814
815  if (Dst.isObjCWeak() && !Dst.isNonGC()) {
816    // load of a __weak object.
817    llvm::Value *LvalueDst = Dst.getAddress();
818    llvm::Value *src = Src.getScalarVal();
819     CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
820    return;
821  }
822
823  if (Dst.isObjCStrong() && !Dst.isNonGC()) {
824    // load of a __strong object.
825    llvm::Value *LvalueDst = Dst.getAddress();
826    llvm::Value *src = Src.getScalarVal();
827    if (Dst.isObjCIvar()) {
828      assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
829      const llvm::Type *ResultType = ConvertType(getContext().LongTy);
830      llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
831      llvm::Value *dst = RHS;
832      RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
833      llvm::Value *LHS =
834        Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
835      llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
836      CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
837                                              BytesBetween);
838    } else if (Dst.isGlobalObjCRef()) {
839      CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
840                                                Dst.isThreadLocalRef());
841    }
842    else
843      CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
844    return;
845  }
846
847  assert(Src.isScalar() && "Can't emit an agg store with this method");
848  EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
849                    Dst.isVolatileQualified(), Dst.getAlignment(), Ty);
850}
851
852void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
853                                                     QualType Ty,
854                                                     llvm::Value **Result) {
855  const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
856
857  // Get the output type.
858  const llvm::Type *ResLTy = ConvertTypeForMem(Ty);
859  unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
860
861  // Get the source value, truncated to the width of the bit-field.
862  llvm::Value *SrcVal = Src.getScalarVal();
863
864  if (Ty->isBooleanType())
865    SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
866
867  SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
868                                                                Info.getSize()),
869                             "bf.value");
870
871  // Return the new value of the bit-field, if requested.
872  if (Result) {
873    // Cast back to the proper type for result.
874    const llvm::Type *SrcTy = Src.getScalarVal()->getType();
875    llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
876                                                   "bf.reload.val");
877
878    // Sign extend if necessary.
879    if (Info.isSigned()) {
880      unsigned ExtraBits = ResSizeInBits - Info.getSize();
881      if (ExtraBits)
882        ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
883                                       ExtraBits, "bf.reload.sext");
884    }
885
886    *Result = ReloadVal;
887  }
888
889  // Iterate over the components, writing each piece to memory.
890  for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
891    const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
892
893    // Get the field pointer.
894    llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
895
896    // Only offset by the field index if used, so that incoming values are not
897    // required to be structures.
898    if (AI.FieldIndex)
899      Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
900
901    // Offset by the byte offset, if used.
902    if (AI.FieldByteOffset) {
903      const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
904      Ptr = Builder.CreateBitCast(Ptr, i8PTy);
905      Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
906    }
907
908    // Cast to the access type.
909    const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
910                                                     Ty.getAddressSpace());
911    Ptr = Builder.CreateBitCast(Ptr, PTy);
912
913    // Extract the piece of the bit-field value to write in this access, limited
914    // to the values that are part of this access.
915    llvm::Value *Val = SrcVal;
916    if (AI.TargetBitOffset)
917      Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
918    Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
919                                                            AI.TargetBitWidth));
920
921    // Extend or truncate to the access size.
922    const llvm::Type *AccessLTy =
923      llvm::Type::getIntNTy(VMContext, AI.AccessWidth);
924    if (ResSizeInBits < AI.AccessWidth)
925      Val = Builder.CreateZExt(Val, AccessLTy);
926    else if (ResSizeInBits > AI.AccessWidth)
927      Val = Builder.CreateTrunc(Val, AccessLTy);
928
929    // Shift into the position in memory.
930    if (AI.FieldBitStart)
931      Val = Builder.CreateShl(Val, AI.FieldBitStart);
932
933    // If necessary, load and OR in bits that are outside of the bit-field.
934    if (AI.TargetBitWidth != AI.AccessWidth) {
935      llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
936      if (AI.AccessAlignment)
937        Load->setAlignment(AI.AccessAlignment);
938
939      // Compute the mask for zeroing the bits that are part of the bit-field.
940      llvm::APInt InvMask =
941        ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
942                                 AI.FieldBitStart + AI.TargetBitWidth);
943
944      // Apply the mask and OR in to the value to write.
945      Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
946    }
947
948    // Write the value.
949    llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
950                                                 Dst.isVolatileQualified());
951    if (AI.AccessAlignment)
952      Store->setAlignment(AI.AccessAlignment);
953  }
954}
955
956void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
957                                                        LValue Dst,
958                                                        QualType Ty) {
959  EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
960}
961
962void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
963                                                   LValue Dst,
964                                                   QualType Ty) {
965  EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
966}
967
968void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
969                                                               LValue Dst,
970                                                               QualType Ty) {
971  // This access turns into a read/modify/write of the vector.  Load the input
972  // value now.
973  llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
974                                        Dst.isVolatileQualified(), "tmp");
975  const llvm::Constant *Elts = Dst.getExtVectorElts();
976
977  llvm::Value *SrcVal = Src.getScalarVal();
978
979  if (const VectorType *VTy = Ty->getAs<VectorType>()) {
980    unsigned NumSrcElts = VTy->getNumElements();
981    unsigned NumDstElts =
982       cast<llvm::VectorType>(Vec->getType())->getNumElements();
983    if (NumDstElts == NumSrcElts) {
984      // Use shuffle vector is the src and destination are the same number of
985      // elements and restore the vector mask since it is on the side it will be
986      // stored.
987      llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
988      for (unsigned i = 0; i != NumSrcElts; ++i) {
989        unsigned InIdx = getAccessedFieldNo(i, Elts);
990        Mask[InIdx] = llvm::ConstantInt::get(Int32Ty, i);
991      }
992
993      llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
994      Vec = Builder.CreateShuffleVector(SrcVal,
995                                        llvm::UndefValue::get(Vec->getType()),
996                                        MaskV, "tmp");
997    } else if (NumDstElts > NumSrcElts) {
998      // Extended the source vector to the same length and then shuffle it
999      // into the destination.
1000      // FIXME: since we're shuffling with undef, can we just use the indices
1001      //        into that?  This could be simpler.
1002      llvm::SmallVector<llvm::Constant*, 4> ExtMask;
1003      unsigned i;
1004      for (i = 0; i != NumSrcElts; ++i)
1005        ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1006      for (; i != NumDstElts; ++i)
1007        ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
1008      llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
1009                                                        ExtMask.size());
1010      llvm::Value *ExtSrcVal =
1011        Builder.CreateShuffleVector(SrcVal,
1012                                    llvm::UndefValue::get(SrcVal->getType()),
1013                                    ExtMaskV, "tmp");
1014      // build identity
1015      llvm::SmallVector<llvm::Constant*, 4> Mask;
1016      for (unsigned i = 0; i != NumDstElts; ++i)
1017        Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1018
1019      // modify when what gets shuffled in
1020      for (unsigned i = 0; i != NumSrcElts; ++i) {
1021        unsigned Idx = getAccessedFieldNo(i, Elts);
1022        Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
1023      }
1024      llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
1025      Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
1026    } else {
1027      // We should never shorten the vector
1028      assert(0 && "unexpected shorten vector length");
1029    }
1030  } else {
1031    // If the Src is a scalar (not a vector) it must be updating one element.
1032    unsigned InIdx = getAccessedFieldNo(0, Elts);
1033    llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1034    Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
1035  }
1036
1037  Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
1038}
1039
1040// setObjCGCLValueClass - sets class of he lvalue for the purpose of
1041// generating write-barries API. It is currently a global, ivar,
1042// or neither.
1043static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1044                                 LValue &LV) {
1045  if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
1046    return;
1047
1048  if (isa<ObjCIvarRefExpr>(E)) {
1049    LV.setObjCIvar(true);
1050    ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1051    LV.setBaseIvarExp(Exp->getBase());
1052    LV.setObjCArray(E->getType()->isArrayType());
1053    return;
1054  }
1055
1056  if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1057    if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1058      if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
1059          VD->isFileVarDecl()) {
1060        LV.setGlobalObjCRef(true);
1061        LV.setThreadLocalRef(VD->isThreadSpecified());
1062      }
1063    }
1064    LV.setObjCArray(E->getType()->isArrayType());
1065    return;
1066  }
1067
1068  if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
1069    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1070    return;
1071  }
1072
1073  if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
1074    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1075    if (LV.isObjCIvar()) {
1076      // If cast is to a structure pointer, follow gcc's behavior and make it
1077      // a non-ivar write-barrier.
1078      QualType ExpTy = E->getType();
1079      if (ExpTy->isPointerType())
1080        ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1081      if (ExpTy->isRecordType())
1082        LV.setObjCIvar(false);
1083    }
1084    return;
1085  }
1086  if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1087    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1088    return;
1089  }
1090
1091  if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
1092    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1093    return;
1094  }
1095
1096  if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1097    setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1098    if (LV.isObjCIvar() && !LV.isObjCArray())
1099      // Using array syntax to assigning to what an ivar points to is not
1100      // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1101      LV.setObjCIvar(false);
1102    else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1103      // Using array syntax to assigning to what global points to is not
1104      // same as assigning to the global itself. {id *G;} G[i] = 0;
1105      LV.setGlobalObjCRef(false);
1106    return;
1107  }
1108
1109  if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
1110    setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1111    // We don't know if member is an 'ivar', but this flag is looked at
1112    // only in the context of LV.isObjCIvar().
1113    LV.setObjCArray(E->getType()->isArrayType());
1114    return;
1115  }
1116}
1117
1118static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1119                                      const Expr *E, const VarDecl *VD) {
1120  assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
1121         "Var decl must have external storage or be a file var decl!");
1122
1123  llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1124  if (VD->getType()->isReferenceType())
1125    V = CGF.Builder.CreateLoad(V, "tmp");
1126  unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
1127  LValue LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
1128  setObjCGCLValueClass(CGF.getContext(), E, LV);
1129  return LV;
1130}
1131
1132static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1133                                      const Expr *E, const FunctionDecl *FD) {
1134  llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
1135  if (!FD->hasPrototype()) {
1136    if (const FunctionProtoType *Proto =
1137            FD->getType()->getAs<FunctionProtoType>()) {
1138      // Ugly case: for a K&R-style definition, the type of the definition
1139      // isn't the same as the type of a use.  Correct for this with a
1140      // bitcast.
1141      QualType NoProtoType =
1142          CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1143      NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1144      V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1145    }
1146  }
1147  unsigned Alignment = CGF.getContext().getDeclAlign(FD).getQuantity();
1148  return CGF.MakeAddrLValue(V, E->getType(), Alignment);
1149}
1150
1151LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
1152  const NamedDecl *ND = E->getDecl();
1153  unsigned Alignment = CGF.getContext().getDeclAlign(ND).getQuantity();
1154
1155  if (ND->hasAttr<WeakRefAttr>()) {
1156    const ValueDecl *VD = cast<ValueDecl>(ND);
1157    llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1158    return MakeAddrLValue(Aliasee, E->getType(), Alignment);
1159  }
1160
1161  if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1162
1163    // Check if this is a global variable.
1164    if (VD->hasExternalStorage() || VD->isFileVarDecl())
1165      return EmitGlobalVarDeclLValue(*this, E, VD);
1166
1167    bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1168
1169    llvm::Value *V = LocalDeclMap[VD];
1170    if (!V && VD->isStaticLocal())
1171      V = CGM.getStaticLocalDeclAddress(VD);
1172    assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1173
1174    if (VD->hasAttr<BlocksAttr>()) {
1175      V = Builder.CreateStructGEP(V, 1, "forwarding");
1176      V = Builder.CreateLoad(V);
1177      V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1178                                  VD->getNameAsString());
1179    }
1180    if (VD->getType()->isReferenceType())
1181      V = Builder.CreateLoad(V, "tmp");
1182
1183    LValue LV = MakeAddrLValue(V, E->getType(), Alignment);
1184    if (NonGCable) {
1185      LV.getQuals().removeObjCGCAttr();
1186      LV.setNonGC(true);
1187    }
1188    setObjCGCLValueClass(getContext(), E, LV);
1189    return LV;
1190  }
1191
1192  // If we're emitting an instance method as an independent lvalue,
1193  // we're actually emitting a member pointer.
1194  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1195    if (MD->isInstance()) {
1196      llvm::Value *V = CGM.getCXXABI().EmitMemberPointer(MD);
1197      return MakeAddrLValue(V, MD->getType(), Alignment);
1198    }
1199  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1200    return EmitFunctionDeclLValue(*this, E, FD);
1201
1202  // If we're emitting a field as an independent lvalue, we're
1203  // actually emitting a member pointer.
1204  if (const FieldDecl *FD = dyn_cast<FieldDecl>(ND)) {
1205    llvm::Value *V = CGM.getCXXABI().EmitMemberPointer(FD);
1206    return MakeAddrLValue(V, FD->getType(), Alignment);
1207  }
1208
1209  assert(false && "Unhandled DeclRefExpr");
1210
1211  // an invalid LValue, but the assert will
1212  // ensure that this point is never reached.
1213  return LValue();
1214}
1215
1216LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
1217  unsigned Alignment =
1218    CGF.getContext().getDeclAlign(E->getDecl()).getQuantity();
1219  return MakeAddrLValue(GetAddrOfBlockDecl(E), E->getType(), Alignment);
1220}
1221
1222LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1223  // __extension__ doesn't affect lvalue-ness.
1224  if (E->getOpcode() == UO_Extension)
1225    return EmitLValue(E->getSubExpr());
1226
1227  QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
1228  switch (E->getOpcode()) {
1229  default: assert(0 && "Unknown unary operator lvalue!");
1230  case UO_Deref: {
1231    QualType T = E->getSubExpr()->getType()->getPointeeType();
1232    assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
1233
1234    LValue LV = MakeAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
1235    LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
1236
1237    // We should not generate __weak write barrier on indirect reference
1238    // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1239    // But, we continue to generate __strong write barrier on indirect write
1240    // into a pointer to object.
1241    if (getContext().getLangOptions().ObjC1 &&
1242        getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1243        LV.isObjCWeak())
1244      LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1245    return LV;
1246  }
1247  case UO_Real:
1248  case UO_Imag: {
1249    LValue LV = EmitLValue(E->getSubExpr());
1250    unsigned Idx = E->getOpcode() == UO_Imag;
1251    return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
1252                                                    Idx, "idx"),
1253                          ExprTy);
1254  }
1255  case UO_PreInc:
1256  case UO_PreDec: {
1257    LValue LV = EmitLValue(E->getSubExpr());
1258    bool isInc = E->getOpcode() == UO_PreInc;
1259
1260    if (E->getType()->isAnyComplexType())
1261      EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1262    else
1263      EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1264    return LV;
1265  }
1266  }
1267}
1268
1269LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
1270  return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
1271                        E->getType());
1272}
1273
1274LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
1275  return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1276                        E->getType());
1277}
1278
1279
1280LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
1281  switch (E->getIdentType()) {
1282  default:
1283    return EmitUnsupportedLValue(E, "predefined expression");
1284
1285  case PredefinedExpr::Func:
1286  case PredefinedExpr::Function:
1287  case PredefinedExpr::PrettyFunction: {
1288    unsigned Type = E->getIdentType();
1289    std::string GlobalVarName;
1290
1291    switch (Type) {
1292    default: assert(0 && "Invalid type");
1293    case PredefinedExpr::Func:
1294      GlobalVarName = "__func__.";
1295      break;
1296    case PredefinedExpr::Function:
1297      GlobalVarName = "__FUNCTION__.";
1298      break;
1299    case PredefinedExpr::PrettyFunction:
1300      GlobalVarName = "__PRETTY_FUNCTION__.";
1301      break;
1302    }
1303
1304    llvm::StringRef FnName = CurFn->getName();
1305    if (FnName.startswith("\01"))
1306      FnName = FnName.substr(1);
1307    GlobalVarName += FnName;
1308
1309    const Decl *CurDecl = CurCodeDecl;
1310    if (CurDecl == 0)
1311      CurDecl = getContext().getTranslationUnitDecl();
1312
1313    std::string FunctionName =
1314      PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurDecl);
1315
1316    llvm::Constant *C =
1317      CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
1318    return MakeAddrLValue(C, E->getType());
1319  }
1320  }
1321}
1322
1323llvm::BasicBlock *CodeGenFunction::getTrapBB() {
1324  const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1325
1326  // If we are not optimzing, don't collapse all calls to trap in the function
1327  // to the same call, that way, in the debugger they can see which operation
1328  // did in fact fail.  If we are optimizing, we collapse all calls to trap down
1329  // to just one per function to save on codesize.
1330  if (GCO.OptimizationLevel && TrapBB)
1331    return TrapBB;
1332
1333  llvm::BasicBlock *Cont = 0;
1334  if (HaveInsertPoint()) {
1335    Cont = createBasicBlock("cont");
1336    EmitBranch(Cont);
1337  }
1338  TrapBB = createBasicBlock("trap");
1339  EmitBlock(TrapBB);
1340
1341  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1342  llvm::CallInst *TrapCall = Builder.CreateCall(F);
1343  TrapCall->setDoesNotReturn();
1344  TrapCall->setDoesNotThrow();
1345  Builder.CreateUnreachable();
1346
1347  if (Cont)
1348    EmitBlock(Cont);
1349  return TrapBB;
1350}
1351
1352/// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
1353/// array to pointer, return the array subexpression.
1354static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
1355  // If this isn't just an array->pointer decay, bail out.
1356  const CastExpr *CE = dyn_cast<CastExpr>(E);
1357  if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay)
1358    return 0;
1359
1360  // If this is a decay from variable width array, bail out.
1361  const Expr *SubExpr = CE->getSubExpr();
1362  if (SubExpr->getType()->isVariableArrayType())
1363    return 0;
1364
1365  return SubExpr;
1366}
1367
1368LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1369  // The index must always be an integer, which is not an aggregate.  Emit it.
1370  llvm::Value *Idx = EmitScalarExpr(E->getIdx());
1371  QualType IdxTy  = E->getIdx()->getType();
1372  bool IdxSigned = IdxTy->isSignedIntegerType();
1373
1374  // If the base is a vector type, then we are forming a vector element lvalue
1375  // with this subscript.
1376  if (E->getBase()->getType()->isVectorType()) {
1377    // Emit the vector as an lvalue to get its address.
1378    LValue LHS = EmitLValue(E->getBase());
1379    assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
1380    Idx = Builder.CreateIntCast(Idx, CGF.Int32Ty, IdxSigned, "vidx");
1381    return LValue::MakeVectorElt(LHS.getAddress(), Idx,
1382                                 E->getBase()->getType().getCVRQualifiers());
1383  }
1384
1385  // Extend or truncate the index type to 32 or 64-bits.
1386  if (!Idx->getType()->isIntegerTy(LLVMPointerWidth))
1387    Idx = Builder.CreateIntCast(Idx, IntPtrTy,
1388                                IdxSigned, "idxprom");
1389
1390  // FIXME: As llvm implements the object size checking, this can come out.
1391  if (CatchUndefined) {
1392    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())){
1393      if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1394        if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
1395          if (const ConstantArrayType *CAT
1396              = getContext().getAsConstantArrayType(DRE->getType())) {
1397            llvm::APInt Size = CAT->getSize();
1398            llvm::BasicBlock *Cont = createBasicBlock("cont");
1399            Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
1400                                  llvm::ConstantInt::get(Idx->getType(), Size)),
1401                                 Cont, getTrapBB());
1402            EmitBlock(Cont);
1403          }
1404        }
1405      }
1406    }
1407  }
1408
1409  // We know that the pointer points to a type of the correct size, unless the
1410  // size is a VLA or Objective-C interface.
1411  llvm::Value *Address = 0;
1412  if (const VariableArrayType *VAT =
1413        getContext().getAsVariableArrayType(E->getType())) {
1414    llvm::Value *VLASize = GetVLASize(VAT);
1415
1416    Idx = Builder.CreateMul(Idx, VLASize);
1417
1418    QualType BaseType = getContext().getBaseElementType(VAT);
1419
1420    CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
1421    Idx = Builder.CreateUDiv(Idx,
1422                             llvm::ConstantInt::get(Idx->getType(),
1423                                 BaseTypeSize.getQuantity()));
1424
1425    // The base must be a pointer, which is not an aggregate.  Emit it.
1426    llvm::Value *Base = EmitScalarExpr(E->getBase());
1427
1428    Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
1429  } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
1430    // Indexing over an interface, as in "NSString *P; P[4];"
1431    llvm::Value *InterfaceSize =
1432      llvm::ConstantInt::get(Idx->getType(),
1433          getContext().getTypeSizeInChars(OIT).getQuantity());
1434
1435    Idx = Builder.CreateMul(Idx, InterfaceSize);
1436
1437    const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
1438
1439    // The base must be a pointer, which is not an aggregate.  Emit it.
1440    llvm::Value *Base = EmitScalarExpr(E->getBase());
1441    Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
1442                                Idx, "arrayidx");
1443    Address = Builder.CreateBitCast(Address, Base->getType());
1444  } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
1445    // If this is A[i] where A is an array, the frontend will have decayed the
1446    // base to be a ArrayToPointerDecay implicit cast.  While correct, it is
1447    // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
1448    // "gep x, i" here.  Emit one "gep A, 0, i".
1449    assert(Array->getType()->isArrayType() &&
1450           "Array to pointer decay must have array source type!");
1451    llvm::Value *ArrayPtr = EmitLValue(Array).getAddress();
1452    llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
1453    llvm::Value *Args[] = { Zero, Idx };
1454
1455    Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, Args+2, "arrayidx");
1456  } else {
1457    // The base must be a pointer, which is not an aggregate.  Emit it.
1458    llvm::Value *Base = EmitScalarExpr(E->getBase());
1459    Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
1460  }
1461
1462  QualType T = E->getBase()->getType()->getPointeeType();
1463  assert(!T.isNull() &&
1464         "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
1465
1466  LValue LV = MakeAddrLValue(Address, T);
1467  LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
1468
1469  if (getContext().getLangOptions().ObjC1 &&
1470      getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
1471    LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1472    setObjCGCLValueClass(getContext(), E, LV);
1473  }
1474  return LV;
1475}
1476
1477static
1478llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1479                                       llvm::SmallVector<unsigned, 4> &Elts) {
1480  llvm::SmallVector<llvm::Constant*, 4> CElts;
1481
1482  const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1483  for (unsigned i = 0, e = Elts.size(); i != e; ++i)
1484    CElts.push_back(llvm::ConstantInt::get(Int32Ty, Elts[i]));
1485
1486  return llvm::ConstantVector::get(&CElts[0], CElts.size());
1487}
1488
1489LValue CodeGenFunction::
1490EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
1491  // Emit the base vector as an l-value.
1492  LValue Base;
1493
1494  // ExtVectorElementExpr's base can either be a vector or pointer to vector.
1495  if (E->isArrow()) {
1496    // If it is a pointer to a vector, emit the address and form an lvalue with
1497    // it.
1498    llvm::Value *Ptr = EmitScalarExpr(E->getBase());
1499    const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
1500    Base = MakeAddrLValue(Ptr, PT->getPointeeType());
1501    Base.getQuals().removeObjCGCAttr();
1502  } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1503    // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1504    // emit the base as an lvalue.
1505    assert(E->getBase()->getType()->isVectorType());
1506    Base = EmitLValue(E->getBase());
1507  } else {
1508    // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
1509    assert(E->getBase()->getType()->getAs<VectorType>() &&
1510           "Result must be a vector");
1511    llvm::Value *Vec = EmitScalarExpr(E->getBase());
1512
1513    // Store the vector to memory (because LValue wants an address).
1514    llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
1515    Builder.CreateStore(Vec, VecMem);
1516    Base = MakeAddrLValue(VecMem, E->getBase()->getType());
1517  }
1518
1519  // Encode the element access list into a vector of unsigned indices.
1520  llvm::SmallVector<unsigned, 4> Indices;
1521  E->getEncodedElementAccess(Indices);
1522
1523  if (Base.isSimple()) {
1524    llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
1525    return LValue::MakeExtVectorElt(Base.getAddress(), CV,
1526                                    Base.getVRQualifiers());
1527  }
1528  assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1529
1530  llvm::Constant *BaseElts = Base.getExtVectorElts();
1531  llvm::SmallVector<llvm::Constant *, 4> CElts;
1532
1533  for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1534    if (isa<llvm::ConstantAggregateZero>(BaseElts))
1535      CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
1536    else
1537      CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
1538  }
1539  llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
1540  return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
1541                                  Base.getVRQualifiers());
1542}
1543
1544LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
1545  bool isNonGC = false;
1546  Expr *BaseExpr = E->getBase();
1547  llvm::Value *BaseValue = NULL;
1548  Qualifiers BaseQuals;
1549
1550  // If this is s.x, emit s as an lvalue.  If it is s->x, emit s as a scalar.
1551  if (E->isArrow()) {
1552    BaseValue = EmitScalarExpr(BaseExpr);
1553    const PointerType *PTy =
1554      BaseExpr->getType()->getAs<PointerType>();
1555    BaseQuals = PTy->getPointeeType().getQualifiers();
1556  } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1557             isa<ObjCImplicitSetterGetterRefExpr>(
1558               BaseExpr->IgnoreParens())) {
1559    RValue RV = EmitObjCPropertyGet(BaseExpr);
1560    BaseValue = RV.getAggregateAddr();
1561    BaseQuals = BaseExpr->getType().getQualifiers();
1562  } else {
1563    LValue BaseLV = EmitLValue(BaseExpr);
1564    if (BaseLV.isNonGC())
1565      isNonGC = true;
1566    // FIXME: this isn't right for bitfields.
1567    BaseValue = BaseLV.getAddress();
1568    QualType BaseTy = BaseExpr->getType();
1569    BaseQuals = BaseTy.getQualifiers();
1570  }
1571
1572  NamedDecl *ND = E->getMemberDecl();
1573  if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1574    LValue LV = EmitLValueForField(BaseValue, Field,
1575                                   BaseQuals.getCVRQualifiers());
1576    LV.setNonGC(isNonGC);
1577    setObjCGCLValueClass(getContext(), E, LV);
1578    return LV;
1579  }
1580
1581  if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1582    return EmitGlobalVarDeclLValue(*this, E, VD);
1583
1584  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1585    return EmitFunctionDeclLValue(*this, E, FD);
1586
1587  assert(false && "Unhandled member declaration!");
1588  return LValue();
1589}
1590
1591LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value *BaseValue,
1592                                              const FieldDecl *Field,
1593                                              unsigned CVRQualifiers) {
1594  const CGRecordLayout &RL =
1595    CGM.getTypes().getCGRecordLayout(Field->getParent());
1596  const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
1597  return LValue::MakeBitfield(BaseValue, Info,
1598                             Field->getType().getCVRQualifiers()|CVRQualifiers);
1599}
1600
1601/// EmitLValueForAnonRecordField - Given that the field is a member of
1602/// an anonymous struct or union buried inside a record, and given
1603/// that the base value is a pointer to the enclosing record, derive
1604/// an lvalue for the ultimate field.
1605LValue CodeGenFunction::EmitLValueForAnonRecordField(llvm::Value *BaseValue,
1606                                                     const FieldDecl *Field,
1607                                                     unsigned CVRQualifiers) {
1608  llvm::SmallVector<const FieldDecl *, 8> Path;
1609  Path.push_back(Field);
1610
1611  while (Field->getParent()->isAnonymousStructOrUnion()) {
1612    const ValueDecl *VD = Field->getParent()->getAnonymousStructOrUnionObject();
1613    if (!isa<FieldDecl>(VD)) break;
1614    Field = cast<FieldDecl>(VD);
1615    Path.push_back(Field);
1616  }
1617
1618  llvm::SmallVectorImpl<const FieldDecl*>::reverse_iterator
1619    I = Path.rbegin(), E = Path.rend();
1620  while (true) {
1621    LValue LV = EmitLValueForField(BaseValue, *I, CVRQualifiers);
1622    if (++I == E) return LV;
1623
1624    assert(LV.isSimple());
1625    BaseValue = LV.getAddress();
1626    CVRQualifiers |= LV.getVRQualifiers();
1627  }
1628}
1629
1630LValue CodeGenFunction::EmitLValueForField(llvm::Value *BaseValue,
1631                                           const FieldDecl *Field,
1632                                           unsigned CVRQualifiers) {
1633  if (Field->isBitField())
1634    return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
1635
1636  const CGRecordLayout &RL =
1637    CGM.getTypes().getCGRecordLayout(Field->getParent());
1638  unsigned idx = RL.getLLVMFieldNo(Field);
1639  llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1640
1641  // Match union field type.
1642  if (Field->getParent()->isUnion()) {
1643    const llvm::Type *FieldTy =
1644      CGM.getTypes().ConvertTypeForMem(Field->getType());
1645    const llvm::PointerType *BaseTy =
1646      cast<llvm::PointerType>(BaseValue->getType());
1647    unsigned AS = BaseTy->getAddressSpace();
1648    V = Builder.CreateBitCast(V,
1649                              llvm::PointerType::get(FieldTy, AS),
1650                              "tmp");
1651  }
1652  if (Field->getType()->isReferenceType())
1653    V = Builder.CreateLoad(V, "tmp");
1654
1655  unsigned Alignment = getContext().getDeclAlign(Field).getQuantity();
1656  LValue LV = MakeAddrLValue(V, Field->getType(), Alignment);
1657  LV.getQuals().addCVRQualifiers(CVRQualifiers);
1658
1659  // __weak attribute on a field is ignored.
1660  if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
1661    LV.getQuals().removeObjCGCAttr();
1662
1663  return LV;
1664}
1665
1666LValue
1667CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue,
1668                                                  const FieldDecl *Field,
1669                                                  unsigned CVRQualifiers) {
1670  QualType FieldType = Field->getType();
1671
1672  if (!FieldType->isReferenceType())
1673    return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1674
1675  const CGRecordLayout &RL =
1676    CGM.getTypes().getCGRecordLayout(Field->getParent());
1677  unsigned idx = RL.getLLVMFieldNo(Field);
1678  llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1679
1680  assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1681
1682  unsigned Alignment = getContext().getDeclAlign(Field).getQuantity();
1683  return MakeAddrLValue(V, FieldType, Alignment);
1684}
1685
1686LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
1687  llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
1688  const Expr *InitExpr = E->getInitializer();
1689  LValue Result = MakeAddrLValue(DeclPtr, E->getType());
1690
1691  EmitAnyExprToMem(InitExpr, DeclPtr, /*Volatile*/ false, /*Init*/ true);
1692
1693  return Result;
1694}
1695
1696LValue
1697CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator *E) {
1698  if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1699    if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1700      Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1701      if (Live)
1702        return EmitLValue(Live);
1703    }
1704
1705    llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1706    llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1707    llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1708
1709    if (E->getLHS())
1710      EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1711    else {
1712      Expr *save = E->getSAVE();
1713      assert(save && "VisitConditionalOperator - save is null");
1714      // Intentianlly not doing direct assignment to ConditionalSaveExprs[save]
1715      LValue SaveVal = EmitLValue(save);
1716      ConditionalSaveLValueExprs[save] = SaveVal;
1717      EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1718    }
1719
1720    // Any temporaries created here are conditional.
1721    BeginConditionalBranch();
1722    EmitBlock(LHSBlock);
1723    LValue LHS = EmitLValue(E->getTrueExpr());
1724
1725    EndConditionalBranch();
1726
1727    if (!LHS.isSimple())
1728      return EmitUnsupportedLValue(E, "conditional operator");
1729
1730    // FIXME: We shouldn't need an alloca for this.
1731    llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
1732    Builder.CreateStore(LHS.getAddress(), Temp);
1733    EmitBranch(ContBlock);
1734
1735    // Any temporaries created here are conditional.
1736    BeginConditionalBranch();
1737    EmitBlock(RHSBlock);
1738    LValue RHS = EmitLValue(E->getRHS());
1739    EndConditionalBranch();
1740    if (!RHS.isSimple())
1741      return EmitUnsupportedLValue(E, "conditional operator");
1742
1743    Builder.CreateStore(RHS.getAddress(), Temp);
1744    EmitBranch(ContBlock);
1745
1746    EmitBlock(ContBlock);
1747
1748    Temp = Builder.CreateLoad(Temp, "lv");
1749    return MakeAddrLValue(Temp, E->getType());
1750  }
1751
1752  // ?: here should be an aggregate.
1753  assert((hasAggregateLLVMType(E->getType()) &&
1754          !E->getType()->isAnyComplexType()) &&
1755         "Unexpected conditional operator!");
1756
1757  return EmitAggExprToLValue(E);
1758}
1759
1760/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1761/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1762/// otherwise if a cast is needed by the code generator in an lvalue context,
1763/// then it must mean that we need the address of an aggregate in order to
1764/// access one of its fields.  This can happen for all the reasons that casts
1765/// are permitted with aggregate result, including noop aggregate casts, and
1766/// cast from scalar to union.
1767LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1768  switch (E->getCastKind()) {
1769  case CK_ToVoid:
1770    return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1771
1772  case CK_NoOp:
1773    if (E->getSubExpr()->Classify(getContext()).getKind()
1774                                          != Expr::Classification::CL_PRValue) {
1775      LValue LV = EmitLValue(E->getSubExpr());
1776      if (LV.isPropertyRef() || LV.isKVCRef()) {
1777        QualType QT = E->getSubExpr()->getType();
1778        RValue RV =
1779          LV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(LV, QT)
1780                             : EmitLoadOfKVCRefLValue(LV, QT);
1781        assert(!RV.isScalar() && "EmitCastLValue-scalar cast of property ref");
1782        llvm::Value *V = RV.getAggregateAddr();
1783        return MakeAddrLValue(V, QT);
1784      }
1785      return LV;
1786    }
1787    // Fall through to synthesize a temporary.
1788
1789  case CK_Unknown:
1790  case CK_BitCast:
1791  case CK_ArrayToPointerDecay:
1792  case CK_FunctionToPointerDecay:
1793  case CK_NullToMemberPointer:
1794  case CK_IntegralToPointer:
1795  case CK_PointerToIntegral:
1796  case CK_VectorSplat:
1797  case CK_IntegralCast:
1798  case CK_IntegralToFloating:
1799  case CK_FloatingToIntegral:
1800  case CK_FloatingCast:
1801  case CK_DerivedToBaseMemberPointer:
1802  case CK_BaseToDerivedMemberPointer:
1803  case CK_MemberPointerToBoolean:
1804  case CK_AnyPointerToBlockPointerCast: {
1805    // These casts only produce lvalues when we're binding a reference to a
1806    // temporary realized from a (converted) pure rvalue. Emit the expression
1807    // as a value, copy it into a temporary, and return an lvalue referring to
1808    // that temporary.
1809    llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp");
1810    EmitAnyExprToMem(E, V, false, false);
1811    return MakeAddrLValue(V, E->getType());
1812  }
1813
1814  case CK_Dynamic: {
1815    LValue LV = EmitLValue(E->getSubExpr());
1816    llvm::Value *V = LV.getAddress();
1817    const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1818    return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
1819  }
1820
1821  case CK_ConstructorConversion:
1822  case CK_UserDefinedConversion:
1823  case CK_AnyPointerToObjCPointerCast:
1824    return EmitLValue(E->getSubExpr());
1825
1826  case CK_UncheckedDerivedToBase:
1827  case CK_DerivedToBase: {
1828    const RecordType *DerivedClassTy =
1829      E->getSubExpr()->getType()->getAs<RecordType>();
1830    CXXRecordDecl *DerivedClassDecl =
1831      cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1832
1833    LValue LV = EmitLValue(E->getSubExpr());
1834    llvm::Value *This;
1835    if (LV.isPropertyRef() || LV.isKVCRef()) {
1836      QualType QT = E->getSubExpr()->getType();
1837      RValue RV =
1838        LV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(LV, QT)
1839                           : EmitLoadOfKVCRefLValue(LV, QT);
1840      assert (!RV.isScalar() && "EmitCastLValue");
1841      This = RV.getAggregateAddr();
1842    }
1843    else
1844      This = LV.getAddress();
1845
1846    // Perform the derived-to-base conversion
1847    llvm::Value *Base =
1848      GetAddressOfBaseClass(This, DerivedClassDecl,
1849                            E->path_begin(), E->path_end(),
1850                            /*NullCheckValue=*/false);
1851
1852    return MakeAddrLValue(Base, E->getType());
1853  }
1854  case CK_ToUnion:
1855    return EmitAggExprToLValue(E);
1856  case CK_BaseToDerived: {
1857    const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1858    CXXRecordDecl *DerivedClassDecl =
1859      cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1860
1861    LValue LV = EmitLValue(E->getSubExpr());
1862
1863    // Perform the base-to-derived conversion
1864    llvm::Value *Derived =
1865      GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
1866                               E->path_begin(), E->path_end(),
1867                               /*NullCheckValue=*/false);
1868
1869    return MakeAddrLValue(Derived, E->getType());
1870  }
1871  case CK_LValueBitCast: {
1872    // This must be a reinterpret_cast (or c-style equivalent).
1873    const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
1874
1875    LValue LV = EmitLValue(E->getSubExpr());
1876    llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1877                                           ConvertType(CE->getTypeAsWritten()));
1878    return MakeAddrLValue(V, E->getType());
1879  }
1880  case CK_ObjCObjectLValueCast: {
1881    LValue LV = EmitLValue(E->getSubExpr());
1882    QualType ToType = getContext().getLValueReferenceType(E->getType());
1883    llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1884                                           ConvertType(ToType));
1885    return MakeAddrLValue(V, E->getType());
1886  }
1887  }
1888
1889  llvm_unreachable("Unhandled lvalue cast kind?");
1890}
1891
1892LValue CodeGenFunction::EmitNullInitializationLValue(
1893                                              const CXXScalarValueInitExpr *E) {
1894  QualType Ty = E->getType();
1895  LValue LV = MakeAddrLValue(CreateMemTemp(Ty), Ty);
1896  EmitNullInitialization(LV.getAddress(), Ty);
1897  return LV;
1898}
1899
1900//===--------------------------------------------------------------------===//
1901//                             Expression Emission
1902//===--------------------------------------------------------------------===//
1903
1904
1905RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1906                                     ReturnValueSlot ReturnValue) {
1907  // Builtins never have block type.
1908  if (E->getCallee()->getType()->isBlockPointerType())
1909    return EmitBlockCallExpr(E, ReturnValue);
1910
1911  if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1912    return EmitCXXMemberCallExpr(CE, ReturnValue);
1913
1914  const Decl *TargetDecl = 0;
1915  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1916    if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1917      TargetDecl = DRE->getDecl();
1918      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
1919        if (unsigned builtinID = FD->getBuiltinID())
1920          return EmitBuiltinExpr(FD, builtinID, E);
1921    }
1922  }
1923
1924  if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
1925    if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1926      return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
1927
1928  if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
1929    // C++ [expr.pseudo]p1:
1930    //   The result shall only be used as the operand for the function call
1931    //   operator (), and the result of such a call has type void. The only
1932    //   effect is the evaluation of the postfix-expression before the dot or
1933    //   arrow.
1934    EmitScalarExpr(E->getCallee());
1935    return RValue::get(0);
1936  }
1937
1938  llvm::Value *Callee = EmitScalarExpr(E->getCallee());
1939  return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
1940                  E->arg_begin(), E->arg_end(), TargetDecl);
1941}
1942
1943LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
1944  // Comma expressions just emit their LHS then their RHS as an l-value.
1945  if (E->getOpcode() == BO_Comma) {
1946    EmitAnyExpr(E->getLHS());
1947    EnsureInsertPoint();
1948    return EmitLValue(E->getRHS());
1949  }
1950
1951  if (E->getOpcode() == BO_PtrMemD ||
1952      E->getOpcode() == BO_PtrMemI)
1953    return EmitPointerToDataMemberBinaryExpr(E);
1954
1955  // Can only get l-value for binary operator expressions which are a
1956  // simple assignment of aggregate type.
1957  if (E->getOpcode() != BO_Assign)
1958    return EmitUnsupportedLValue(E, "binary l-value expression");
1959
1960  if (!hasAggregateLLVMType(E->getType())) {
1961    // Emit the LHS as an l-value.
1962    LValue LV = EmitLValue(E->getLHS());
1963    // Store the value through the l-value.
1964    EmitStoreThroughLValue(EmitAnyExpr(E->getRHS()), LV, E->getType());
1965    return LV;
1966  }
1967
1968  return EmitAggExprToLValue(E);
1969}
1970
1971LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
1972  RValue RV = EmitCallExpr(E);
1973
1974  if (!RV.isScalar())
1975    return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
1976
1977  assert(E->getCallReturnType()->isReferenceType() &&
1978         "Can't have a scalar return unless the return type is a "
1979         "reference type!");
1980
1981  return MakeAddrLValue(RV.getScalarVal(), E->getType());
1982}
1983
1984LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1985  // FIXME: This shouldn't require another copy.
1986  return EmitAggExprToLValue(E);
1987}
1988
1989LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1990  assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
1991         && "binding l-value to type which needs a temporary");
1992  AggValueSlot Slot = CreateAggTemp(E->getType(), "tmp");
1993  EmitCXXConstructExpr(E, Slot);
1994  return MakeAddrLValue(Slot.getAddr(), E->getType());
1995}
1996
1997LValue
1998CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1999  return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
2000}
2001
2002LValue
2003CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
2004  AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
2005  Slot.setLifetimeExternallyManaged();
2006  EmitAggExpr(E->getSubExpr(), Slot);
2007  EmitCXXTemporary(E->getTemporary(), Slot.getAddr());
2008  return MakeAddrLValue(Slot.getAddr(), E->getType());
2009}
2010
2011LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
2012  RValue RV = EmitObjCMessageExpr(E);
2013
2014  if (!RV.isScalar())
2015    return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2016
2017  assert(E->getMethodDecl()->getResultType()->isReferenceType() &&
2018         "Can't have a scalar return unless the return type is a "
2019         "reference type!");
2020
2021  return MakeAddrLValue(RV.getScalarVal(), E->getType());
2022}
2023
2024LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
2025  llvm::Value *V =
2026    CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true);
2027  return MakeAddrLValue(V, E->getType());
2028}
2029
2030llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2031                                             const ObjCIvarDecl *Ivar) {
2032  return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
2033}
2034
2035LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
2036                                          llvm::Value *BaseValue,
2037                                          const ObjCIvarDecl *Ivar,
2038                                          unsigned CVRQualifiers) {
2039  return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
2040                                                   Ivar, CVRQualifiers);
2041}
2042
2043LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
2044  // FIXME: A lot of the code below could be shared with EmitMemberExpr.
2045  llvm::Value *BaseValue = 0;
2046  const Expr *BaseExpr = E->getBase();
2047  Qualifiers BaseQuals;
2048  QualType ObjectTy;
2049  if (E->isArrow()) {
2050    BaseValue = EmitScalarExpr(BaseExpr);
2051    ObjectTy = BaseExpr->getType()->getPointeeType();
2052    BaseQuals = ObjectTy.getQualifiers();
2053  } else {
2054    LValue BaseLV = EmitLValue(BaseExpr);
2055    // FIXME: this isn't right for bitfields.
2056    BaseValue = BaseLV.getAddress();
2057    ObjectTy = BaseExpr->getType();
2058    BaseQuals = ObjectTy.getQualifiers();
2059  }
2060
2061  LValue LV =
2062    EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
2063                      BaseQuals.getCVRQualifiers());
2064  setObjCGCLValueClass(getContext(), E, LV);
2065  return LV;
2066}
2067
2068LValue
2069CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
2070  // This is a special l-value that just issues sends when we load or store
2071  // through it.
2072  return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
2073}
2074
2075LValue CodeGenFunction::EmitObjCKVCRefLValue(
2076                                const ObjCImplicitSetterGetterRefExpr *E) {
2077  // This is a special l-value that just issues sends when we load or store
2078  // through it.
2079  return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
2080}
2081
2082LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
2083  // Can only get l-value for message expression returning aggregate type
2084  RValue RV = EmitAnyExprToTemp(E);
2085  return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2086}
2087
2088RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
2089                                 ReturnValueSlot ReturnValue,
2090                                 CallExpr::const_arg_iterator ArgBeg,
2091                                 CallExpr::const_arg_iterator ArgEnd,
2092                                 const Decl *TargetDecl) {
2093  // Get the actual function type. The callee type will always be a pointer to
2094  // function type or a block pointer type.
2095  assert(CalleeType->isFunctionPointerType() &&
2096         "Call must have function pointer type!");
2097
2098  CalleeType = getContext().getCanonicalType(CalleeType);
2099
2100  const FunctionType *FnType
2101    = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
2102  QualType ResultType = FnType->getResultType();
2103
2104  CallArgList Args;
2105  EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
2106
2107  return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
2108                  Callee, ReturnValue, Args, TargetDecl);
2109}
2110
2111LValue CodeGenFunction::
2112EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
2113  llvm::Value *BaseV;
2114  if (E->getOpcode() == BO_PtrMemI)
2115    BaseV = EmitScalarExpr(E->getLHS());
2116  else
2117    BaseV = EmitLValue(E->getLHS()).getAddress();
2118
2119  llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
2120
2121  const MemberPointerType *MPT
2122    = E->getRHS()->getType()->getAs<MemberPointerType>();
2123
2124  llvm::Value *AddV =
2125    CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT);
2126
2127  return MakeAddrLValue(AddV, MPT->getPointeeType());
2128}
2129
2130