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