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