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