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