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