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