CGExpr.cpp revision 48620bafe4ba879f96c2d17caefeb79f3fae2eea
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 "CGObjCRuntime.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
20#include "llvm/Target/TargetData.h"
21using namespace clang;
22using namespace CodeGen;
23
24//===--------------------------------------------------------------------===//
25//                        Miscellaneous Helper Methods
26//===--------------------------------------------------------------------===//
27
28/// CreateTempAlloca - This creates a alloca and inserts it into the entry
29/// block.
30llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
31                                                    const llvm::Twine &Name) {
32  if (!Builder.isNamePreserving())
33    return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
34  return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
35}
36
37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38/// expression and compare the result against zero, returning an Int1Ty value.
39llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
40  QualType BoolTy = getContext().BoolTy;
41  if (!E->getType()->isAnyComplexType())
42    return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
43
44  return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
45}
46
47/// EmitAnyExpr - Emit code to compute the specified expression which can have
48/// any type.  The result is returned as an RValue struct.  If this is an
49/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
50/// result should be returned.
51RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
52                                    bool IsAggLocVolatile, bool IgnoreResult,
53                                    bool IsInitializer) {
54  if (!hasAggregateLLVMType(E->getType()))
55    return RValue::get(EmitScalarExpr(E, IgnoreResult));
56  else if (E->getType()->isAnyComplexType())
57    return RValue::getComplex(EmitComplexExpr(E, false, false,
58                                              IgnoreResult, IgnoreResult));
59
60  EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
61  return RValue::getAggregate(AggLoc, IsAggLocVolatile);
62}
63
64/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
65/// always be accessible even if no aggregate location is provided.
66RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
67                                          bool IsAggLocVolatile,
68                                          bool IsInitializer) {
69  llvm::Value *AggLoc = 0;
70
71  if (hasAggregateLLVMType(E->getType()) &&
72      !E->getType()->isAnyComplexType())
73    AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
74  return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
75                     IsInitializer);
76}
77
78RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
79                                                   QualType DestType,
80                                                   bool IsInitializer) {
81  bool ShouldDestroyTemporaries = false;
82  unsigned OldNumLiveTemporaries = 0;
83
84  if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
85    ShouldDestroyTemporaries = TE->shouldDestroyTemporaries();
86
87    if (ShouldDestroyTemporaries) {
88      // Keep track of the current cleanup stack depth.
89      OldNumLiveTemporaries = LiveTemporaries.size();
90    }
91
92    E = TE->getSubExpr();
93  }
94
95  RValue Val;
96  if (E->isLvalue(getContext()) == Expr::LV_Valid) {
97    // Emit the expr as an lvalue.
98    LValue LV = EmitLValue(E);
99    if (LV.isSimple())
100      return RValue::get(LV.getAddress());
101    Val = EmitLoadOfLValue(LV, E->getType());
102
103    if (ShouldDestroyTemporaries) {
104      // Pop temporaries.
105      while (LiveTemporaries.size() > OldNumLiveTemporaries)
106        PopCXXTemporary();
107    }
108  } else {
109    const CXXRecordDecl *BaseClassDecl = 0;
110    const CXXRecordDecl *DerivedClassDecl = 0;
111
112    if (const CastExpr *CE =
113          dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
114      if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
115        E = CE->getSubExpr();
116
117        BaseClassDecl =
118          cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
119        DerivedClassDecl =
120          cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
121      }
122    }
123
124    Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
125                            IsInitializer);
126
127    if (ShouldDestroyTemporaries) {
128      // Pop temporaries.
129      while (LiveTemporaries.size() > OldNumLiveTemporaries)
130        PopCXXTemporary();
131    }
132
133    if (IsInitializer) {
134      // We might have to destroy the temporary variable.
135      if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
136        if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
137          if (!ClassDecl->hasTrivialDestructor()) {
138            const CXXDestructorDecl *Dtor =
139              ClassDecl->getDestructor(getContext());
140
141            CleanupScope scope(*this);
142            EmitCXXDestructorCall(Dtor, Dtor_Complete, Val.getAggregateAddr());
143          }
144        }
145      }
146    }
147
148    // Check if need to perform the derived-to-base cast.
149    if (BaseClassDecl) {
150      llvm::Value *Derived = Val.getAggregateAddr();
151
152      llvm::Value *Base =
153        GetAddressCXXOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
154                                 /*NullCheckValue=*/false);
155      return RValue::get(Base);
156    }
157  }
158
159  if (Val.isAggregate()) {
160    Val = RValue::get(Val.getAggregateAddr());
161  } else {
162    // Create a temporary variable that we can bind the reference to.
163    llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
164                                         "reftmp");
165    if (Val.isScalar())
166      EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
167    else
168      StoreComplexToAddr(Val.getComplexVal(), Temp, false);
169    Val = RValue::get(Temp);
170  }
171
172  return Val;
173}
174
175
176/// getAccessedFieldNo - Given an encoded value and a result number, return the
177/// input field number being accessed.
178unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
179                                             const llvm::Constant *Elts) {
180  if (isa<llvm::ConstantAggregateZero>(Elts))
181    return 0;
182
183  return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
184}
185
186
187//===----------------------------------------------------------------------===//
188//                         LValue Expression Emission
189//===----------------------------------------------------------------------===//
190
191RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
192  if (Ty->isVoidType()) {
193    return RValue::get(0);
194  } else if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
195    const llvm::Type *EltTy = ConvertType(CTy->getElementType());
196    llvm::Value *U = llvm::UndefValue::get(EltTy);
197    return RValue::getComplex(std::make_pair(U, U));
198  } else if (hasAggregateLLVMType(Ty)) {
199    const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
200    return RValue::getAggregate(llvm::UndefValue::get(LTy));
201  } else {
202    return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
203  }
204}
205
206RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
207                                              const char *Name) {
208  ErrorUnsupported(E, Name);
209  return GetUndefRValue(E->getType());
210}
211
212LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
213                                              const char *Name) {
214  ErrorUnsupported(E, Name);
215  llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
216  return LValue::MakeAddr(llvm::UndefValue::get(Ty),
217                          MakeQualifiers(E->getType()));
218}
219
220/// EmitLValue - Emit code to compute a designator that specifies the location
221/// of the expression.
222///
223/// This can return one of two things: a simple address or a bitfield reference.
224/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
225/// an LLVM pointer type.
226///
227/// If this returns a bitfield reference, nothing about the pointee type of the
228/// LLVM value is known: For example, it may not be a pointer to an integer.
229///
230/// If this returns a normal address, and if the lvalue's C type is fixed size,
231/// this method guarantees that the returned pointer type will point to an LLVM
232/// type of the same size of the lvalue's type.  If the lvalue has a variable
233/// length type, this is not possible.
234///
235LValue CodeGenFunction::EmitLValue(const Expr *E) {
236  switch (E->getStmtClass()) {
237  default: return EmitUnsupportedLValue(E, "l-value expression");
238
239  case Expr::BinaryOperatorClass:
240    return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
241  case Expr::CallExprClass:
242  case Expr::CXXMemberCallExprClass:
243  case Expr::CXXOperatorCallExprClass:
244    return EmitCallExprLValue(cast<CallExpr>(E));
245  case Expr::VAArgExprClass:
246    return EmitVAArgExprLValue(cast<VAArgExpr>(E));
247  case Expr::DeclRefExprClass:
248  case Expr::QualifiedDeclRefExprClass:
249    return EmitDeclRefLValue(cast<DeclRefExpr>(E));
250  case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
251  case Expr::PredefinedExprClass:
252    return EmitPredefinedLValue(cast<PredefinedExpr>(E));
253  case Expr::StringLiteralClass:
254    return EmitStringLiteralLValue(cast<StringLiteral>(E));
255  case Expr::ObjCEncodeExprClass:
256    return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
257
258  case Expr::BlockDeclRefExprClass:
259    return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
260
261  case Expr::CXXConditionDeclExprClass:
262    return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
263  case Expr::CXXTemporaryObjectExprClass:
264  case Expr::CXXConstructExprClass:
265    return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
266  case Expr::CXXBindTemporaryExprClass:
267    return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
268  case Expr::CXXExprWithTemporariesClass:
269    return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
270
271  case Expr::ObjCMessageExprClass:
272    return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
273  case Expr::ObjCIvarRefExprClass:
274    return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
275  case Expr::ObjCPropertyRefExprClass:
276    return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
277  case Expr::ObjCImplicitSetterGetterRefExprClass:
278    return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
279  case Expr::ObjCSuperExprClass:
280    return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
281
282  case Expr::StmtExprClass:
283    return EmitStmtExprLValue(cast<StmtExpr>(E));
284  case Expr::UnaryOperatorClass:
285    return EmitUnaryOpLValue(cast<UnaryOperator>(E));
286  case Expr::ArraySubscriptExprClass:
287    return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
288  case Expr::ExtVectorElementExprClass:
289    return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
290  case Expr::MemberExprClass:
291    return EmitMemberExpr(cast<MemberExpr>(E));
292  case Expr::CompoundLiteralExprClass:
293    return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
294  case Expr::ConditionalOperatorClass:
295    return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
296  case Expr::ChooseExprClass:
297    return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
298  case Expr::ImplicitCastExprClass:
299  case Expr::CStyleCastExprClass:
300  case Expr::CXXFunctionalCastExprClass:
301  case Expr::CXXStaticCastExprClass:
302  case Expr::CXXDynamicCastExprClass:
303  case Expr::CXXReinterpretCastExprClass:
304  case Expr::CXXConstCastExprClass:
305    return EmitCastLValue(cast<CastExpr>(E));
306  case Expr::CXXZeroInitValueExprClass:
307    return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
308  }
309}
310
311llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
312                                               QualType Ty) {
313  llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
314
315  // Bool can have different representation in memory than in registers.
316  if (Ty->isBooleanType())
317    if (V->getType() != llvm::Type::getInt1Ty(VMContext))
318      V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
319
320  return V;
321}
322
323void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
324                                        bool Volatile, QualType Ty) {
325
326  if (Ty->isBooleanType()) {
327    // Bool can have different representation in memory than in registers.
328    const llvm::Type *SrcTy = Value->getType();
329    const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
330    if (DstPtr->getElementType() != SrcTy) {
331      const llvm::Type *MemTy =
332        llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
333      Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
334    }
335  }
336  Builder.CreateStore(Value, Addr, Volatile);
337}
338
339/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
340/// method emits the address of the lvalue, then loads the result as an rvalue,
341/// returning the rvalue.
342RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
343  if (LV.isObjCWeak()) {
344    // load of a __weak object.
345    llvm::Value *AddrWeakObj = LV.getAddress();
346    llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
347                                                                   AddrWeakObj);
348    return RValue::get(read_weak);
349  }
350
351  if (LV.isSimple()) {
352    llvm::Value *Ptr = LV.getAddress();
353    const llvm::Type *EltTy =
354      cast<llvm::PointerType>(Ptr->getType())->getElementType();
355
356    // Simple scalar l-value.
357    if (EltTy->isSingleValueType())
358      return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
359                                          ExprType));
360
361    assert(ExprType->isFunctionType() && "Unknown scalar value");
362    return RValue::get(Ptr);
363  }
364
365  if (LV.isVectorElt()) {
366    llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
367                                          LV.isVolatileQualified(), "tmp");
368    return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
369                                                    "vecext"));
370  }
371
372  // If this is a reference to a subset of the elements of a vector, either
373  // shuffle the input or extract/insert them as appropriate.
374  if (LV.isExtVectorElt())
375    return EmitLoadOfExtVectorElementLValue(LV, ExprType);
376
377  if (LV.isBitfield())
378    return EmitLoadOfBitfieldLValue(LV, ExprType);
379
380  if (LV.isPropertyRef())
381    return EmitLoadOfPropertyRefLValue(LV, ExprType);
382
383  assert(LV.isKVCRef() && "Unknown LValue type!");
384  return EmitLoadOfKVCRefLValue(LV, ExprType);
385}
386
387RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
388                                                 QualType ExprType) {
389  unsigned StartBit = LV.getBitfieldStartBit();
390  unsigned BitfieldSize = LV.getBitfieldSize();
391  llvm::Value *Ptr = LV.getBitfieldAddr();
392
393  const llvm::Type *EltTy =
394    cast<llvm::PointerType>(Ptr->getType())->getElementType();
395  unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
396
397  // In some cases the bitfield may straddle two memory locations.  Currently we
398  // load the entire bitfield, then do the magic to sign-extend it if
399  // necessary. This results in somewhat more code than necessary for the common
400  // case (one load), since two shifts accomplish both the masking and sign
401  // extension.
402  unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
403  llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
404
405  // Shift to proper location.
406  if (StartBit)
407    Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
408                             "bf.lo");
409
410  // Mask off unused bits.
411  llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
412                                llvm::APInt::getLowBitsSet(EltTySize, LowBits));
413  Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
414
415  // Fetch the high bits if necessary.
416  if (LowBits < BitfieldSize) {
417    unsigned HighBits = BitfieldSize - LowBits;
418    llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
419                            llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
420    llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
421                                              LV.isVolatileQualified(),
422                                              "tmp");
423
424    // Mask off unused bits.
425    llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
426                               llvm::APInt::getLowBitsSet(EltTySize, HighBits));
427    HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
428
429    // Shift to proper location and or in to bitfield value.
430    HighVal = Builder.CreateShl(HighVal,
431                                llvm::ConstantInt::get(EltTy, LowBits));
432    Val = Builder.CreateOr(Val, HighVal, "bf.val");
433  }
434
435  // Sign extend if necessary.
436  if (LV.isBitfieldSigned()) {
437    llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
438                                                    EltTySize - BitfieldSize);
439    Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
440                             ExtraBits, "bf.val.sext");
441  }
442
443  // The bitfield type and the normal type differ when the storage sizes differ
444  // (currently just _Bool).
445  Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
446
447  return RValue::get(Val);
448}
449
450RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
451                                                    QualType ExprType) {
452  return EmitObjCPropertyGet(LV.getPropertyRefExpr());
453}
454
455RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
456                                               QualType ExprType) {
457  return EmitObjCPropertyGet(LV.getKVCRefExpr());
458}
459
460// If this is a reference to a subset of the elements of a vector, create an
461// appropriate shufflevector.
462RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
463                                                         QualType ExprType) {
464  llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
465                                        LV.isVolatileQualified(), "tmp");
466
467  const llvm::Constant *Elts = LV.getExtVectorElts();
468
469  // If the result of the expression is a non-vector type, we must be extracting
470  // a single element.  Just codegen as an extractelement.
471  const VectorType *ExprVT = ExprType->getAs<VectorType>();
472  if (!ExprVT) {
473    unsigned InIdx = getAccessedFieldNo(0, Elts);
474    llvm::Value *Elt = llvm::ConstantInt::get(
475                                      llvm::Type::getInt32Ty(VMContext), InIdx);
476    return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
477  }
478
479  // Always use shuffle vector to try to retain the original program structure
480  unsigned NumResultElts = ExprVT->getNumElements();
481
482  llvm::SmallVector<llvm::Constant*, 4> Mask;
483  for (unsigned i = 0; i != NumResultElts; ++i) {
484    unsigned InIdx = getAccessedFieldNo(i, Elts);
485    Mask.push_back(llvm::ConstantInt::get(
486                                     llvm::Type::getInt32Ty(VMContext), InIdx));
487  }
488
489  llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
490  Vec = Builder.CreateShuffleVector(Vec,
491                                    llvm::UndefValue::get(Vec->getType()),
492                                    MaskV, "tmp");
493  return RValue::get(Vec);
494}
495
496
497
498/// EmitStoreThroughLValue - Store the specified rvalue into the specified
499/// lvalue, where both are guaranteed to the have the same type, and that type
500/// is 'Ty'.
501void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
502                                             QualType Ty) {
503  if (!Dst.isSimple()) {
504    if (Dst.isVectorElt()) {
505      // Read/modify/write the vector, inserting the new element.
506      llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
507                                            Dst.isVolatileQualified(), "tmp");
508      Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
509                                        Dst.getVectorIdx(), "vecins");
510      Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
511      return;
512    }
513
514    // If this is an update of extended vector elements, insert them as
515    // appropriate.
516    if (Dst.isExtVectorElt())
517      return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
518
519    if (Dst.isBitfield())
520      return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
521
522    if (Dst.isPropertyRef())
523      return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
524
525    if (Dst.isKVCRef())
526      return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
527
528    assert(0 && "Unknown LValue type");
529  }
530
531  if (Dst.isObjCWeak() && !Dst.isNonGC()) {
532    // load of a __weak object.
533    llvm::Value *LvalueDst = Dst.getAddress();
534    llvm::Value *src = Src.getScalarVal();
535     CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
536    return;
537  }
538
539  if (Dst.isObjCStrong() && !Dst.isNonGC()) {
540    // load of a __strong object.
541    llvm::Value *LvalueDst = Dst.getAddress();
542    llvm::Value *src = Src.getScalarVal();
543    if (Dst.isObjCIvar()) {
544      assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
545      const llvm::Type *ResultType = ConvertType(getContext().LongTy);
546      llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
547      llvm::Value *dst = RHS;
548      RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
549      llvm::Value *LHS =
550        Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
551      llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
552      CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
553                                              BytesBetween);
554    }
555    else if (Dst.isGlobalObjCRef())
556      CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
557    else
558      CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
559    return;
560  }
561
562  assert(Src.isScalar() && "Can't emit an agg store with this method");
563  EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
564                    Dst.isVolatileQualified(), Ty);
565}
566
567void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
568                                                     QualType Ty,
569                                                     llvm::Value **Result) {
570  unsigned StartBit = Dst.getBitfieldStartBit();
571  unsigned BitfieldSize = Dst.getBitfieldSize();
572  llvm::Value *Ptr = Dst.getBitfieldAddr();
573
574  const llvm::Type *EltTy =
575    cast<llvm::PointerType>(Ptr->getType())->getElementType();
576  unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
577
578  // Get the new value, cast to the appropriate type and masked to exactly the
579  // size of the bit-field.
580  llvm::Value *SrcVal = Src.getScalarVal();
581  llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
582  llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
583                           llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
584  NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
585
586  // Return the new value of the bit-field, if requested.
587  if (Result) {
588    // Cast back to the proper type for result.
589    const llvm::Type *SrcTy = SrcVal->getType();
590    llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
591                                                  "bf.reload.val");
592
593    // Sign extend if necessary.
594    if (Dst.isBitfieldSigned()) {
595      unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
596      llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
597                                                      SrcTySize - BitfieldSize);
598      SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
599                                    ExtraBits, "bf.reload.sext");
600    }
601
602    *Result = SrcTrunc;
603  }
604
605  // In some cases the bitfield may straddle two memory locations.  Emit the low
606  // part first and check to see if the high needs to be done.
607  unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
608  llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
609                                           "bf.prev.low");
610
611  // Compute the mask for zero-ing the low part of this bitfield.
612  llvm::Constant *InvMask =
613    llvm::ConstantInt::get(VMContext,
614             ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
615
616  // Compute the new low part as
617  //   LowVal = (LowVal & InvMask) | (NewVal << StartBit),
618  // with the shift of NewVal implicitly stripping the high bits.
619  llvm::Value *NewLowVal =
620    Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
621                      "bf.value.lo");
622  LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
623  LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
624
625  // Write back.
626  Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
627
628  // If the low part doesn't cover the bitfield emit a high part.
629  if (LowBits < BitfieldSize) {
630    unsigned HighBits = BitfieldSize - LowBits;
631    llvm::Value *HighPtr =  Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
632                            llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
633    llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
634                                              Dst.isVolatileQualified(),
635                                              "bf.prev.hi");
636
637    // Compute the mask for zero-ing the high part of this bitfield.
638    llvm::Constant *InvMask =
639      llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
640                               HighBits));
641
642    // Compute the new high part as
643    //   HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
644    // where the high bits of NewVal have already been cleared and the
645    // shift stripping the low bits.
646    llvm::Value *NewHighVal =
647      Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
648                        "bf.value.high");
649    HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
650    HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
651
652    // Write back.
653    Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
654  }
655}
656
657void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
658                                                        LValue Dst,
659                                                        QualType Ty) {
660  EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
661}
662
663void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
664                                                   LValue Dst,
665                                                   QualType Ty) {
666  EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
667}
668
669void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
670                                                               LValue Dst,
671                                                               QualType Ty) {
672  // This access turns into a read/modify/write of the vector.  Load the input
673  // value now.
674  llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
675                                        Dst.isVolatileQualified(), "tmp");
676  const llvm::Constant *Elts = Dst.getExtVectorElts();
677
678  llvm::Value *SrcVal = Src.getScalarVal();
679
680  if (const VectorType *VTy = Ty->getAs<VectorType>()) {
681    unsigned NumSrcElts = VTy->getNumElements();
682    unsigned NumDstElts =
683       cast<llvm::VectorType>(Vec->getType())->getNumElements();
684    if (NumDstElts == NumSrcElts) {
685      // Use shuffle vector is the src and destination are the same number of
686      // elements and restore the vector mask since it is on the side it will be
687      // stored.
688      llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
689      for (unsigned i = 0; i != NumSrcElts; ++i) {
690        unsigned InIdx = getAccessedFieldNo(i, Elts);
691        Mask[InIdx] = llvm::ConstantInt::get(
692                                          llvm::Type::getInt32Ty(VMContext), i);
693      }
694
695      llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
696      Vec = Builder.CreateShuffleVector(SrcVal,
697                                        llvm::UndefValue::get(Vec->getType()),
698                                        MaskV, "tmp");
699    } else if (NumDstElts > NumSrcElts) {
700      // Extended the source vector to the same length and then shuffle it
701      // into the destination.
702      // FIXME: since we're shuffling with undef, can we just use the indices
703      //        into that?  This could be simpler.
704      llvm::SmallVector<llvm::Constant*, 4> ExtMask;
705      unsigned i;
706      for (i = 0; i != NumSrcElts; ++i)
707        ExtMask.push_back(llvm::ConstantInt::get(
708                                         llvm::Type::getInt32Ty(VMContext), i));
709      for (; i != NumDstElts; ++i)
710        ExtMask.push_back(llvm::UndefValue::get(
711                                            llvm::Type::getInt32Ty(VMContext)));
712      llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
713                                                        ExtMask.size());
714      llvm::Value *ExtSrcVal =
715        Builder.CreateShuffleVector(SrcVal,
716                                    llvm::UndefValue::get(SrcVal->getType()),
717                                    ExtMaskV, "tmp");
718      // build identity
719      llvm::SmallVector<llvm::Constant*, 4> Mask;
720      for (unsigned i = 0; i != NumDstElts; ++i) {
721        Mask.push_back(llvm::ConstantInt::get(
722                                        llvm::Type::getInt32Ty(VMContext), i));
723      }
724      // modify when what gets shuffled in
725      for (unsigned i = 0; i != NumSrcElts; ++i) {
726        unsigned Idx = getAccessedFieldNo(i, Elts);
727        Mask[Idx] = llvm::ConstantInt::get(
728                               llvm::Type::getInt32Ty(VMContext), i+NumDstElts);
729      }
730      llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
731      Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
732    } else {
733      // We should never shorten the vector
734      assert(0 && "unexpected shorten vector length");
735    }
736  } else {
737    // If the Src is a scalar (not a vector) it must be updating one element.
738    unsigned InIdx = getAccessedFieldNo(0, Elts);
739    llvm::Value *Elt = llvm::ConstantInt::get(
740                                      llvm::Type::getInt32Ty(VMContext), InIdx);
741    Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
742  }
743
744  Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
745}
746
747// setObjCGCLValueClass - sets class of he lvalue for the purpose of
748// generating write-barries API. It is currently a global, ivar,
749// or neither.
750static
751void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, LValue &LV) {
752  if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
753    return;
754
755  if (isa<ObjCIvarRefExpr>(E)) {
756    LV.SetObjCIvar(LV, true);
757    ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
758    LV.setBaseIvarExp(Exp->getBase());
759    LV.SetObjCArray(LV, E->getType()->isArrayType());
760    return;
761  }
762  if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
763    if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
764      if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
765          VD->isFileVarDecl())
766        LV.SetGlobalObjCRef(LV, true);
767    }
768    LV.SetObjCArray(LV, E->getType()->isArrayType());
769  }
770  else if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E))
771    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
772  else if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
773    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
774    if (LV.isObjCIvar()) {
775      // If cast is to a structure pointer, follow gcc's behavior and make it
776      // a non-ivar write-barrier.
777      QualType ExpTy = E->getType();
778      if (ExpTy->isPointerType())
779        ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
780      if (ExpTy->isRecordType())
781        LV.SetObjCIvar(LV, false);
782    }
783  }
784  else if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E))
785    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
786  else if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E))
787    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
788  else if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
789    setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
790    if (LV.isObjCIvar() && !LV.isObjCArray())
791      // Using array syntax to assigning to what an ivar points to is not
792      // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
793      LV.SetObjCIvar(LV, false);
794    else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
795      // Using array syntax to assigning to what global points to is not
796      // same as assigning to the global itself. {id *G;} G[i] = 0;
797      LV.SetGlobalObjCRef(LV, false);
798  }
799  else if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
800    setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
801    // We don't know if member is an 'ivar', but this flag is looked at
802    // only in the context of LV.isObjCIvar().
803    LV.SetObjCArray(LV, E->getType()->isArrayType());
804  }
805}
806
807LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
808  const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
809
810  if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
811        isa<ImplicitParamDecl>(VD))) {
812    LValue LV;
813    bool NonGCable = VD->hasLocalStorage() &&
814      !VD->hasAttr<BlocksAttr>();
815    if (VD->hasExternalStorage()) {
816      llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
817      if (VD->getType()->isReferenceType())
818        V = Builder.CreateLoad(V, "tmp");
819      LV = LValue::MakeAddr(V, MakeQualifiers(E->getType()));
820    } else {
821      llvm::Value *V = LocalDeclMap[VD];
822      assert(V && "DeclRefExpr not entered in LocalDeclMap?");
823
824      Qualifiers Quals = MakeQualifiers(E->getType());
825      // local variables do not get their gc attribute set.
826      // local static?
827      if (NonGCable) Quals.removeObjCGCAttr();
828
829      if (VD->hasAttr<BlocksAttr>()) {
830        V = Builder.CreateStructGEP(V, 1, "forwarding");
831        V = Builder.CreateLoad(V, false);
832        V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
833                                    VD->getNameAsString());
834      }
835      if (VD->getType()->isReferenceType())
836        V = Builder.CreateLoad(V, "tmp");
837      LV = LValue::MakeAddr(V, Quals);
838    }
839    LValue::SetObjCNonGC(LV, NonGCable);
840    setObjCGCLValueClass(getContext(), E, LV);
841    return LV;
842  } else if (VD && VD->isFileVarDecl()) {
843    llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
844    if (VD->getType()->isReferenceType())
845      V = Builder.CreateLoad(V, "tmp");
846    LValue LV = LValue::MakeAddr(V, MakeQualifiers(E->getType()));
847    setObjCGCLValueClass(getContext(), E, LV);
848    return LV;
849  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
850    llvm::Value* V = CGM.GetAddrOfFunction(FD);
851    if (!FD->hasPrototype()) {
852      if (const FunctionProtoType *Proto =
853              FD->getType()->getAs<FunctionProtoType>()) {
854        // Ugly case: for a K&R-style definition, the type of the definition
855        // isn't the same as the type of a use.  Correct for this with a
856        // bitcast.
857        QualType NoProtoType =
858            getContext().getFunctionNoProtoType(Proto->getResultType());
859        NoProtoType = getContext().getPointerType(NoProtoType);
860        V = Builder.CreateBitCast(V, ConvertType(NoProtoType), "tmp");
861      }
862    }
863    return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
864  } else if (const ImplicitParamDecl *IPD =
865      dyn_cast<ImplicitParamDecl>(E->getDecl())) {
866    llvm::Value *V = LocalDeclMap[IPD];
867    assert(V && "BlockVarDecl not entered in LocalDeclMap?");
868    return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
869  }
870  assert(0 && "Unimp declref");
871  //an invalid LValue, but the assert will
872  //ensure that this point is never reached.
873  return LValue();
874}
875
876LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
877  return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
878}
879
880LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
881  // __extension__ doesn't affect lvalue-ness.
882  if (E->getOpcode() == UnaryOperator::Extension)
883    return EmitLValue(E->getSubExpr());
884
885  QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
886  switch (E->getOpcode()) {
887  default: assert(0 && "Unknown unary operator lvalue!");
888  case UnaryOperator::Deref:
889    {
890      QualType T = E->getSubExpr()->getType()->getPointeeType();
891      assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
892
893      Qualifiers Quals = MakeQualifiers(T);
894      Quals.setAddressSpace(ExprTy.getAddressSpace());
895
896      LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
897     // We should not generate __weak write barrier on indirect reference
898     // of a pointer to object; as in void foo (__weak id *param); *param = 0;
899     // But, we continue to generate __strong write barrier on indirect write
900     // into a pointer to object.
901     if (getContext().getLangOptions().ObjC1 &&
902         getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
903         LV.isObjCWeak())
904       LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
905     return LV;
906    }
907  case UnaryOperator::Real:
908  case UnaryOperator::Imag:
909    LValue LV = EmitLValue(E->getSubExpr());
910    unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
911    return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
912                                                    Idx, "idx"),
913                            MakeQualifiers(ExprTy));
914  }
915}
916
917LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
918  return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
919                          Qualifiers());
920}
921
922LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
923  return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
924                          Qualifiers());
925}
926
927
928LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
929  std::string GlobalVarName;
930
931  switch (Type) {
932  default:
933    assert(0 && "Invalid type");
934  case PredefinedExpr::Func:
935    GlobalVarName = "__func__.";
936    break;
937  case PredefinedExpr::Function:
938    GlobalVarName = "__FUNCTION__.";
939    break;
940  case PredefinedExpr::PrettyFunction:
941    GlobalVarName = "__PRETTY_FUNCTION__.";
942    break;
943  }
944
945  llvm::StringRef FnName = CurFn->getName();
946  if (FnName.startswith("\01"))
947    FnName = FnName.substr(1);
948  GlobalVarName += FnName;
949
950  std::string FunctionName =
951    PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
952                                CurCodeDecl);
953
954  llvm::Constant *C =
955    CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
956  return LValue::MakeAddr(C, Qualifiers());
957}
958
959LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
960  switch (E->getIdentType()) {
961  default:
962    return EmitUnsupportedLValue(E, "predefined expression");
963  case PredefinedExpr::Func:
964  case PredefinedExpr::Function:
965  case PredefinedExpr::PrettyFunction:
966    return EmitPredefinedFunctionName(E->getIdentType());
967  }
968}
969
970LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
971  // The index must always be an integer, which is not an aggregate.  Emit it.
972  llvm::Value *Idx = EmitScalarExpr(E->getIdx());
973  QualType IdxTy  = E->getIdx()->getType();
974  bool IdxSigned = IdxTy->isSignedIntegerType();
975
976  // If the base is a vector type, then we are forming a vector element lvalue
977  // with this subscript.
978  if (E->getBase()->getType()->isVectorType()) {
979    // Emit the vector as an lvalue to get its address.
980    LValue LHS = EmitLValue(E->getBase());
981    assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
982    Idx = Builder.CreateIntCast(Idx,
983                          llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
984    return LValue::MakeVectorElt(LHS.getAddress(), Idx,
985                                 E->getBase()->getType().getCVRQualifiers());
986  }
987
988  // The base must be a pointer, which is not an aggregate.  Emit it.
989  llvm::Value *Base = EmitScalarExpr(E->getBase());
990
991  // Extend or truncate the index type to 32 or 64-bits.
992  unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
993  if (IdxBitwidth != LLVMPointerWidth)
994    Idx = Builder.CreateIntCast(Idx,
995                            llvm::IntegerType::get(VMContext, LLVMPointerWidth),
996                                IdxSigned, "idxprom");
997
998  // We know that the pointer points to a type of the correct size, unless the
999  // size is a VLA or Objective-C interface.
1000  llvm::Value *Address = 0;
1001  if (const VariableArrayType *VAT =
1002        getContext().getAsVariableArrayType(E->getType())) {
1003    llvm::Value *VLASize = GetVLASize(VAT);
1004
1005    Idx = Builder.CreateMul(Idx, VLASize);
1006
1007    QualType BaseType = getContext().getBaseElementType(VAT);
1008
1009    uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
1010    Idx = Builder.CreateUDiv(Idx,
1011                             llvm::ConstantInt::get(Idx->getType(),
1012                                                    BaseTypeSize));
1013    Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
1014  } else if (const ObjCInterfaceType *OIT =
1015             dyn_cast<ObjCInterfaceType>(E->getType())) {
1016    llvm::Value *InterfaceSize =
1017      llvm::ConstantInt::get(Idx->getType(),
1018                             getContext().getTypeSize(OIT) / 8);
1019
1020    Idx = Builder.CreateMul(Idx, InterfaceSize);
1021
1022    const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
1023    Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
1024                                Idx, "arrayidx");
1025    Address = Builder.CreateBitCast(Address, Base->getType());
1026  } else {
1027    Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
1028  }
1029
1030  QualType T = E->getBase()->getType()->getPointeeType();
1031  assert(!T.isNull() &&
1032         "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
1033
1034  Qualifiers Quals = MakeQualifiers(T);
1035  Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1036
1037  LValue LV = LValue::MakeAddr(Address, Quals);
1038  if (getContext().getLangOptions().ObjC1 &&
1039      getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
1040    LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1041    setObjCGCLValueClass(getContext(), E, LV);
1042  }
1043  return LV;
1044}
1045
1046static
1047llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1048                                       llvm::SmallVector<unsigned, 4> &Elts) {
1049  llvm::SmallVector<llvm::Constant *, 4> CElts;
1050
1051  for (unsigned i = 0, e = Elts.size(); i != e; ++i)
1052    CElts.push_back(llvm::ConstantInt::get(
1053                                   llvm::Type::getInt32Ty(VMContext), Elts[i]));
1054
1055  return llvm::ConstantVector::get(&CElts[0], CElts.size());
1056}
1057
1058LValue CodeGenFunction::
1059EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
1060  // Emit the base vector as an l-value.
1061  LValue Base;
1062
1063  // ExtVectorElementExpr's base can either be a vector or pointer to vector.
1064  if (!E->isArrow()) {
1065    assert(E->getBase()->getType()->isVectorType());
1066    Base = EmitLValue(E->getBase());
1067  } else {
1068    const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
1069    llvm::Value *Ptr = EmitScalarExpr(E->getBase());
1070    Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1071    Quals.removeObjCGCAttr();
1072    Base = LValue::MakeAddr(Ptr, Quals);
1073  }
1074
1075  // Encode the element access list into a vector of unsigned indices.
1076  llvm::SmallVector<unsigned, 4> Indices;
1077  E->getEncodedElementAccess(Indices);
1078
1079  if (Base.isSimple()) {
1080    llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
1081    return LValue::MakeExtVectorElt(Base.getAddress(), CV,
1082                                    Base.getVRQualifiers());
1083  }
1084  assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1085
1086  llvm::Constant *BaseElts = Base.getExtVectorElts();
1087  llvm::SmallVector<llvm::Constant *, 4> CElts;
1088
1089  for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1090    if (isa<llvm::ConstantAggregateZero>(BaseElts))
1091      CElts.push_back(llvm::ConstantInt::get(
1092                                         llvm::Type::getInt32Ty(VMContext), 0));
1093    else
1094      CElts.push_back(BaseElts->getOperand(Indices[i]));
1095  }
1096  llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
1097  return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
1098                                  Base.getVRQualifiers());
1099}
1100
1101LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
1102  bool isUnion = false;
1103  bool isNonGC = false;
1104  Expr *BaseExpr = E->getBase();
1105  llvm::Value *BaseValue = NULL;
1106  Qualifiers BaseQuals;
1107
1108  // If this is s.x, emit s as an lvalue.  If it is s->x, emit s as a scalar.
1109  if (E->isArrow()) {
1110    BaseValue = EmitScalarExpr(BaseExpr);
1111    const PointerType *PTy =
1112      BaseExpr->getType()->getAs<PointerType>();
1113    if (PTy->getPointeeType()->isUnionType())
1114      isUnion = true;
1115    BaseQuals = PTy->getPointeeType().getQualifiers();
1116  } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1117             isa<ObjCImplicitSetterGetterRefExpr>(
1118               BaseExpr->IgnoreParens())) {
1119    RValue RV = EmitObjCPropertyGet(BaseExpr);
1120    BaseValue = RV.getAggregateAddr();
1121    if (BaseExpr->getType()->isUnionType())
1122      isUnion = true;
1123    BaseQuals = BaseExpr->getType().getQualifiers();
1124  } else {
1125    LValue BaseLV = EmitLValue(BaseExpr);
1126    if (BaseLV.isNonGC())
1127      isNonGC = true;
1128    // FIXME: this isn't right for bitfields.
1129    BaseValue = BaseLV.getAddress();
1130    QualType BaseTy = BaseExpr->getType();
1131    if (BaseTy->isUnionType())
1132      isUnion = true;
1133    BaseQuals = BaseTy.getQualifiers();
1134  }
1135
1136  FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
1137  // FIXME: Handle non-field member expressions
1138  assert(Field && "No code generation for non-field member references");
1139  LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
1140                                       BaseQuals.getCVRQualifiers());
1141  LValue::SetObjCNonGC(MemExpLV, isNonGC);
1142  setObjCGCLValueClass(getContext(), E, MemExpLV);
1143  return MemExpLV;
1144}
1145
1146LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
1147                                              FieldDecl* Field,
1148                                              unsigned CVRQualifiers) {
1149  CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1150
1151  // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1152  // FieldTy (the appropriate type is ABI-dependent).
1153  const llvm::Type *FieldTy =
1154    CGM.getTypes().ConvertTypeForMem(Field->getType());
1155  const llvm::PointerType *BaseTy =
1156  cast<llvm::PointerType>(BaseValue->getType());
1157  unsigned AS = BaseTy->getAddressSpace();
1158  BaseValue = Builder.CreateBitCast(BaseValue,
1159                                    llvm::PointerType::get(FieldTy, AS),
1160                                    "tmp");
1161
1162  llvm::Value *Idx =
1163    llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
1164  llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
1165
1166  return LValue::MakeBitfield(V, Info.Start, Info.Size,
1167                              Field->getType()->isSignedIntegerType(),
1168                            Field->getType().getCVRQualifiers()|CVRQualifiers);
1169}
1170
1171LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
1172                                           FieldDecl* Field,
1173                                           bool isUnion,
1174                                           unsigned CVRQualifiers) {
1175  if (Field->isBitField())
1176    return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
1177
1178  unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1179  llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1180
1181  // Match union field type.
1182  if (isUnion) {
1183    const llvm::Type *FieldTy =
1184      CGM.getTypes().ConvertTypeForMem(Field->getType());
1185    const llvm::PointerType * BaseTy =
1186      cast<llvm::PointerType>(BaseValue->getType());
1187    unsigned AS = BaseTy->getAddressSpace();
1188    V = Builder.CreateBitCast(V,
1189                              llvm::PointerType::get(FieldTy, AS),
1190                              "tmp");
1191  }
1192  if (Field->getType()->isReferenceType())
1193    V = Builder.CreateLoad(V, "tmp");
1194
1195  Qualifiers Quals = MakeQualifiers(Field->getType());
1196  Quals.addCVRQualifiers(CVRQualifiers);
1197  // __weak attribute on a field is ignored.
1198  if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1199    Quals.removeObjCGCAttr();
1200
1201  return LValue::MakeAddr(V, Quals);
1202}
1203
1204LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
1205  const llvm::Type *LTy = ConvertType(E->getType());
1206  llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1207
1208  const Expr* InitExpr = E->getInitializer();
1209  LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
1210
1211  if (E->getType()->isComplexType()) {
1212    EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
1213  } else if (hasAggregateLLVMType(E->getType())) {
1214    EmitAnyExpr(InitExpr, DeclPtr, false);
1215  } else {
1216    EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
1217  }
1218
1219  return Result;
1220}
1221
1222LValue
1223CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1224  if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1225    llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1226    llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1227    llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1228
1229    llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
1230    Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
1231
1232    EmitBlock(LHSBlock);
1233
1234    LValue LHS = EmitLValue(E->getLHS());
1235    if (!LHS.isSimple())
1236      return EmitUnsupportedLValue(E, "conditional operator");
1237
1238    llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),
1239                                         "condtmp");
1240
1241    Builder.CreateStore(LHS.getAddress(), Temp);
1242    EmitBranch(ContBlock);
1243
1244    EmitBlock(RHSBlock);
1245    LValue RHS = EmitLValue(E->getRHS());
1246    if (!RHS.isSimple())
1247      return EmitUnsupportedLValue(E, "conditional operator");
1248
1249    Builder.CreateStore(RHS.getAddress(), Temp);
1250    EmitBranch(ContBlock);
1251
1252    EmitBlock(ContBlock);
1253
1254    Temp = Builder.CreateLoad(Temp, "lv");
1255    return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1256  }
1257
1258  // ?: here should be an aggregate.
1259  assert((hasAggregateLLVMType(E->getType()) &&
1260          !E->getType()->isAnyComplexType()) &&
1261         "Unexpected conditional operator!");
1262
1263  llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1264  EmitAggExpr(E, Temp, false);
1265
1266  return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1267}
1268
1269/// EmitCastLValue - Casts are never lvalues.  If a cast is needed by the code
1270/// generator in an lvalue context, then it must mean that we need the address
1271/// of an aggregate in order to access one of its fields.  This can happen for
1272/// all the reasons that casts are permitted with aggregate result, including
1273/// noop aggregate casts, and cast from scalar to union.
1274LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1275  switch (E->getCastKind()) {
1276  default:
1277    // If this is an lvalue cast, treat it as a no-op.
1278    // FIXME: We shouldn't need to check for this explicitly!
1279    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1280      if (ICE->isLvalueCast())
1281        return EmitLValue(E->getSubExpr());
1282
1283    assert(0 && "Unhandled cast!");
1284
1285  case CastExpr::CK_NoOp:
1286  case CastExpr::CK_ConstructorConversion:
1287  case CastExpr::CK_UserDefinedConversion:
1288    return EmitLValue(E->getSubExpr());
1289
1290  case CastExpr::CK_DerivedToBase: {
1291    const RecordType *DerivedClassTy =
1292      E->getSubExpr()->getType()->getAs<RecordType>();
1293    CXXRecordDecl *DerivedClassDecl =
1294      cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1295
1296    const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1297    CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1298
1299    LValue LV = EmitLValue(E->getSubExpr());
1300
1301    // Perform the derived-to-base conversion
1302    llvm::Value *Base =
1303      GetAddressCXXOfBaseClass(LV.getAddress(), DerivedClassDecl,
1304                               BaseClassDecl, /*NullCheckValue=*/false);
1305
1306    return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
1307  }
1308
1309  case CastExpr::CK_ToUnion: {
1310    llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1311    EmitAnyExpr(E->getSubExpr(), Temp, false);
1312
1313    return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1314    }
1315  }
1316}
1317
1318LValue CodeGenFunction::EmitNullInitializationLValue(
1319                                              const CXXZeroInitValueExpr *E) {
1320  QualType Ty = E->getType();
1321  const llvm::Type *LTy = ConvertTypeForMem(Ty);
1322  llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
1323  unsigned Align = getContext().getTypeAlign(Ty)/8;
1324  Alloc->setAlignment(Align);
1325  LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1326  EmitMemSetToZero(lvalue.getAddress(), Ty);
1327  return lvalue;
1328}
1329
1330//===--------------------------------------------------------------------===//
1331//                             Expression Emission
1332//===--------------------------------------------------------------------===//
1333
1334
1335RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
1336  // Builtins never have block type.
1337  if (E->getCallee()->getType()->isBlockPointerType())
1338    return EmitBlockCallExpr(E);
1339
1340  if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1341    return EmitCXXMemberCallExpr(CE);
1342
1343  const Decl *TargetDecl = 0;
1344  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1345    if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1346      TargetDecl = DRE->getDecl();
1347      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
1348        if (unsigned builtinID = FD->getBuiltinID())
1349          return EmitBuiltinExpr(FD, builtinID, E);
1350    }
1351  }
1352
1353  if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
1354    if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1355      return EmitCXXOperatorMemberCallExpr(CE, MD);
1356
1357  if (isa<CXXPseudoDestructorExpr>(E->getCallee())) {
1358    // C++ [expr.pseudo]p1:
1359    //   The result shall only be used as the operand for the function call
1360    //   operator (), and the result of such a call has type void. The only
1361    //   effect is the evaluation of the postfix-expression before the dot or
1362    //   arrow.
1363    EmitScalarExpr(E->getCallee());
1364    return RValue::get(0);
1365  }
1366
1367  llvm::Value *Callee = EmitScalarExpr(E->getCallee());
1368  return EmitCall(Callee, E->getCallee()->getType(),
1369                  E->arg_begin(), E->arg_end(), TargetDecl);
1370}
1371
1372LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
1373  // Comma expressions just emit their LHS then their RHS as an l-value.
1374  if (E->getOpcode() == BinaryOperator::Comma) {
1375    EmitAnyExpr(E->getLHS());
1376    return EmitLValue(E->getRHS());
1377  }
1378
1379  // Can only get l-value for binary operator expressions which are a
1380  // simple assignment of aggregate type.
1381  if (E->getOpcode() != BinaryOperator::Assign)
1382    return EmitUnsupportedLValue(E, "binary l-value expression");
1383
1384  if (!hasAggregateLLVMType(E->getType())) {
1385    // Emit the LHS as an l-value.
1386    LValue LV = EmitLValue(E->getLHS());
1387
1388    llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1389    EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1390                      E->getType());
1391    return LV;
1392  }
1393
1394  llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1395  EmitAggExpr(E, Temp, false);
1396  // FIXME: Are these qualifiers correct?
1397  return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1398}
1399
1400LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
1401  RValue RV = EmitCallExpr(E);
1402
1403  if (RV.isScalar()) {
1404    assert(E->getCallReturnType()->isReferenceType() &&
1405           "Can't have a scalar return unless the return type is a "
1406           "reference type!");
1407
1408    return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
1409  }
1410
1411  return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
1412}
1413
1414LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1415  // FIXME: This shouldn't require another copy.
1416  llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1417  EmitAggExpr(E, Temp, false);
1418  return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1419}
1420
1421LValue
1422CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1423  EmitLocalBlockVarDecl(*E->getVarDecl());
1424  return EmitDeclRefLValue(E);
1425}
1426
1427LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1428  llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1429  EmitCXXConstructExpr(Temp, E);
1430  return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1431}
1432
1433LValue
1434CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1435  LValue LV = EmitLValue(E->getSubExpr());
1436
1437  PushCXXTemporary(E->getTemporary(), LV.getAddress());
1438
1439  return LV;
1440}
1441
1442LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1443  // Can only get l-value for message expression returning aggregate type
1444  RValue RV = EmitObjCMessageExpr(E);
1445  // FIXME: can this be volatile?
1446  return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
1447}
1448
1449llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
1450                                             const ObjCIvarDecl *Ivar) {
1451  return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
1452}
1453
1454LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1455                                          llvm::Value *BaseValue,
1456                                          const ObjCIvarDecl *Ivar,
1457                                          unsigned CVRQualifiers) {
1458  return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
1459                                                   Ivar, CVRQualifiers);
1460}
1461
1462LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
1463  // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1464  llvm::Value *BaseValue = 0;
1465  const Expr *BaseExpr = E->getBase();
1466  Qualifiers BaseQuals;
1467  QualType ObjectTy;
1468  if (E->isArrow()) {
1469    BaseValue = EmitScalarExpr(BaseExpr);
1470    ObjectTy = BaseExpr->getType()->getPointeeType();
1471    BaseQuals = ObjectTy.getQualifiers();
1472  } else {
1473    LValue BaseLV = EmitLValue(BaseExpr);
1474    // FIXME: this isn't right for bitfields.
1475    BaseValue = BaseLV.getAddress();
1476    ObjectTy = BaseExpr->getType();
1477    BaseQuals = ObjectTy.getQualifiers();
1478  }
1479
1480  LValue LV =
1481    EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1482                      BaseQuals.getCVRQualifiers());
1483  setObjCGCLValueClass(getContext(), E, LV);
1484  return LV;
1485}
1486
1487LValue
1488CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1489  // This is a special l-value that just issues sends when we load or store
1490  // through it.
1491  return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1492}
1493
1494LValue
1495CodeGenFunction::EmitObjCKVCRefLValue(
1496                                const ObjCImplicitSetterGetterRefExpr *E) {
1497  // This is a special l-value that just issues sends when we load or store
1498  // through it.
1499  return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1500}
1501
1502LValue
1503CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
1504  return EmitUnsupportedLValue(E, "use of super");
1505}
1506
1507LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
1508
1509  // Can only get l-value for message expression returning aggregate type
1510  RValue RV = EmitAnyExprToTemp(E);
1511  // FIXME: can this be volatile?
1512  return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
1513}
1514
1515
1516RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
1517                                 CallExpr::const_arg_iterator ArgBeg,
1518                                 CallExpr::const_arg_iterator ArgEnd,
1519                                 const Decl *TargetDecl) {
1520  // Get the actual function type. The callee type will always be a pointer to
1521  // function type or a block pointer type.
1522  assert(CalleeType->isFunctionPointerType() &&
1523         "Call must have function pointer type!");
1524
1525  QualType FnType = CalleeType->getAs<PointerType>()->getPointeeType();
1526  QualType ResultType = FnType->getAs<FunctionType>()->getResultType();
1527
1528  CallArgList Args;
1529  EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), ArgBeg, ArgEnd);
1530
1531  // FIXME: We should not need to do this, it should be part of the function
1532  // type.
1533  unsigned CallingConvention = 0;
1534  if (const llvm::Function *F =
1535      dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1536    CallingConvention = F->getCallingConv();
1537  return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1538                                                 CallingConvention),
1539                  Callee, Args, TargetDecl);
1540}
1541