1 //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to emit Expr nodes as LLVM code.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
18 #include "CGDebugInfo.h"
19 #include "CGRecordLayout.h"
20 #include "CGObjCRuntime.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/Frontend/CodeGenOptions.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/Target/TargetData.h"
26 using namespace clang;
27 using namespace CodeGen;
29 //===--------------------------------------------------------------------===//
30 // Miscellaneous Helper Methods
31 //===--------------------------------------------------------------------===//
33 llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
34 unsigned addressSpace =
35 cast<llvm::PointerType>(value->getType())->getAddressSpace();
37 const llvm::PointerType *destType = Int8PtrTy;
39 destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
41 if (value->getType() == destType) return value;
42 return Builder.CreateBitCast(value, destType);
45 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
47 llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
48 const llvm::Twine &Name) {
49 if (!Builder.isNamePreserving())
50 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
51 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
54 void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
56 llvm::StoreInst *Store = new llvm::StoreInst(Init, Var);
57 llvm::BasicBlock *Block = AllocaInsertPt->getParent();
58 Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
61 llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
62 const llvm::Twine &Name) {
63 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
64 // FIXME: Should we prefer the preferred type alignment here?
65 CharUnits Align = getContext().getTypeAlignInChars(Ty);
66 Alloc->setAlignment(Align.getQuantity());
70 llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
71 const llvm::Twine &Name) {
72 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
73 // FIXME: Should we prefer the preferred type alignment here?
74 CharUnits Align = getContext().getTypeAlignInChars(Ty);
75 Alloc->setAlignment(Align.getQuantity());
79 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
80 /// expression and compare the result against zero, returning an Int1Ty value.
81 llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
82 if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
83 llvm::Value *MemPtr = EmitScalarExpr(E);
84 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
87 QualType BoolTy = getContext().BoolTy;
88 if (!E->getType()->isAnyComplexType())
89 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
91 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
94 /// EmitIgnoredExpr - Emit code to compute the specified expression,
95 /// ignoring the result.
96 void CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
98 return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
100 // Just emit it as an l-value and drop the result.
104 /// EmitAnyExpr - Emit code to compute the specified expression which
105 /// can have any type. The result is returned as an RValue struct.
106 /// If this is an aggregate expression, AggSlot indicates where the
107 /// result should be returned.
108 RValue CodeGenFunction::EmitAnyExpr(const Expr *E, AggValueSlot AggSlot,
110 if (!hasAggregateLLVMType(E->getType()))
111 return RValue::get(EmitScalarExpr(E, IgnoreResult));
112 else if (E->getType()->isAnyComplexType())
113 return RValue::getComplex(EmitComplexExpr(E, IgnoreResult, IgnoreResult));
115 EmitAggExpr(E, AggSlot, IgnoreResult);
116 return AggSlot.asRValue();
119 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
120 /// always be accessible even if no aggregate location is provided.
121 RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
122 AggValueSlot AggSlot = AggValueSlot::ignored();
124 if (hasAggregateLLVMType(E->getType()) &&
125 !E->getType()->isAnyComplexType())
126 AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
127 return EmitAnyExpr(E, AggSlot);
130 /// EmitAnyExprToMem - Evaluate an expression into a given memory
132 void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
133 llvm::Value *Location,
136 if (E->getType()->isAnyComplexType())
137 EmitComplexExprIntoAddr(E, Location, Quals.hasVolatile());
138 else if (hasAggregateLLVMType(E->getType()))
139 EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals, IsInit));
141 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
142 LValue LV = MakeAddrLValue(Location, E->getType());
143 EmitStoreThroughLValue(RV, LV);
148 /// \brief An adjustment to be made to the temporary created when emitting a
149 /// reference binding, which accesses a particular subobject of that temporary.
150 struct SubobjectAdjustment {
151 enum { DerivedToBaseAdjustment, FieldAdjustment } Kind;
155 const CastExpr *BasePath;
156 const CXXRecordDecl *DerivedClass;
162 SubobjectAdjustment(const CastExpr *BasePath,
163 const CXXRecordDecl *DerivedClass)
164 : Kind(DerivedToBaseAdjustment) {
165 DerivedToBase.BasePath = BasePath;
166 DerivedToBase.DerivedClass = DerivedClass;
169 SubobjectAdjustment(FieldDecl *Field)
170 : Kind(FieldAdjustment) {
177 CreateReferenceTemporary(CodeGenFunction& CGF, QualType Type,
178 const NamedDecl *InitializedDecl) {
179 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
180 if (VD->hasGlobalStorage()) {
181 llvm::SmallString<256> Name;
182 llvm::raw_svector_ostream Out(Name);
183 CGF.CGM.getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Out);
186 const llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type);
188 // Create the reference temporary.
189 llvm::GlobalValue *RefTemp =
190 new llvm::GlobalVariable(CGF.CGM.getModule(),
191 RefTempTy, /*isConstant=*/false,
192 llvm::GlobalValue::InternalLinkage,
193 llvm::Constant::getNullValue(RefTempTy),
199 return CGF.CreateMemTemp(Type, "ref.tmp");
203 EmitExprForReferenceBinding(CodeGenFunction &CGF, const Expr *E,
204 llvm::Value *&ReferenceTemporary,
205 const CXXDestructorDecl *&ReferenceTemporaryDtor,
206 QualType &ObjCARCReferenceLifetimeType,
207 const NamedDecl *InitializedDecl) {
208 // Look through expressions for materialized temporaries (for now).
209 if (const MaterializeTemporaryExpr *M
210 = dyn_cast<MaterializeTemporaryExpr>(E)) {
211 // Objective-C++ ARC:
212 // If we are binding a reference to a temporary that has ownership, we
213 // need to perform retain/release operations on the temporary.
214 if (CGF.getContext().getLangOptions().ObjCAutoRefCount &&
215 E->getType()->isObjCLifetimeType() &&
216 (E->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
217 E->getType().getObjCLifetime() == Qualifiers::OCL_Weak ||
218 E->getType().getObjCLifetime() == Qualifiers::OCL_Autoreleasing))
219 ObjCARCReferenceLifetimeType = E->getType();
221 E = M->GetTemporaryExpr();
224 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
227 if (const ExprWithCleanups *TE = dyn_cast<ExprWithCleanups>(E)) {
228 CodeGenFunction::RunCleanupsScope Scope(CGF);
230 return EmitExprForReferenceBinding(CGF, TE->getSubExpr(),
232 ReferenceTemporaryDtor,
233 ObjCARCReferenceLifetimeType,
237 if (const ObjCPropertyRefExpr *PRE =
238 dyn_cast<ObjCPropertyRefExpr>(E->IgnoreParenImpCasts()))
239 if (PRE->getGetterResultType()->isReferenceType())
243 if (E->isGLValue()) {
244 // Emit the expression as an lvalue.
245 LValue LV = CGF.EmitLValue(E);
246 if (LV.isPropertyRef()) {
247 RV = CGF.EmitLoadOfPropertyRefLValue(LV);
248 return RV.getScalarVal();
252 return LV.getAddress();
254 // We have to load the lvalue.
255 RV = CGF.EmitLoadOfLValue(LV);
257 if (!ObjCARCReferenceLifetimeType.isNull()) {
258 ReferenceTemporary = CreateReferenceTemporary(CGF,
259 ObjCARCReferenceLifetimeType,
263 LValue RefTempDst = CGF.MakeAddrLValue(ReferenceTemporary,
264 ObjCARCReferenceLifetimeType);
266 CGF.EmitScalarInit(E, dyn_cast_or_null<ValueDecl>(InitializedDecl),
269 bool ExtendsLifeOfTemporary = false;
270 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
271 if (Var->extendsLifetimeOfTemporary())
272 ExtendsLifeOfTemporary = true;
273 } else if (InitializedDecl && isa<FieldDecl>(InitializedDecl)) {
274 ExtendsLifeOfTemporary = true;
277 if (!ExtendsLifeOfTemporary) {
278 // Since the lifetime of this temporary isn't going to be extended,
279 // we need to clean it up ourselves at the end of the full expression.
280 switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
281 case Qualifiers::OCL_None:
282 case Qualifiers::OCL_ExplicitNone:
283 case Qualifiers::OCL_Autoreleasing:
286 case Qualifiers::OCL_Strong: {
287 assert(!ObjCARCReferenceLifetimeType->isArrayType());
288 CleanupKind cleanupKind = CGF.getARCCleanupKind();
289 CGF.pushDestroy(cleanupKind,
291 ObjCARCReferenceLifetimeType,
292 CodeGenFunction::destroyARCStrongImprecise,
293 cleanupKind & EHCleanup);
297 case Qualifiers::OCL_Weak:
298 assert(!ObjCARCReferenceLifetimeType->isArrayType());
299 CGF.pushDestroy(NormalAndEHCleanup,
301 ObjCARCReferenceLifetimeType,
302 CodeGenFunction::destroyARCWeak,
303 /*useEHCleanupForArray*/ true);
307 ObjCARCReferenceLifetimeType = QualType();
310 return ReferenceTemporary;
313 llvm::SmallVector<SubobjectAdjustment, 2> Adjustments;
315 E = E->IgnoreParens();
317 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
318 if ((CE->getCastKind() == CK_DerivedToBase ||
319 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
320 E->getType()->isRecordType()) {
321 E = CE->getSubExpr();
322 CXXRecordDecl *Derived
323 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
324 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
328 if (CE->getCastKind() == CK_NoOp) {
329 E = CE->getSubExpr();
332 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
333 if (!ME->isArrow() && ME->getBase()->isRValue()) {
334 assert(ME->getBase()->getType()->isRecordType());
335 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
337 Adjustments.push_back(SubobjectAdjustment(Field));
343 if (const OpaqueValueExpr *opaque = dyn_cast<OpaqueValueExpr>(E))
344 if (opaque->getType()->isRecordType())
345 return CGF.EmitOpaqueValueLValue(opaque).getAddress();
351 // Create a reference temporary if necessary.
352 AggValueSlot AggSlot = AggValueSlot::ignored();
353 if (CGF.hasAggregateLLVMType(E->getType()) &&
354 !E->getType()->isAnyComplexType()) {
355 ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
357 AggSlot = AggValueSlot::forAddr(ReferenceTemporary, Qualifiers(),
358 InitializedDecl != 0);
361 if (InitializedDecl) {
362 // Get the destructor for the reference temporary.
363 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
364 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
365 if (!ClassDecl->hasTrivialDestructor())
366 ReferenceTemporaryDtor = ClassDecl->getDestructor();
370 RV = CGF.EmitAnyExpr(E, AggSlot);
372 // Check if need to perform derived-to-base casts and/or field accesses, to
373 // get from the temporary object we created (and, potentially, for which we
374 // extended the lifetime) to the subobject we're binding the reference to.
375 if (!Adjustments.empty()) {
376 llvm::Value *Object = RV.getAggregateAddr();
377 for (unsigned I = Adjustments.size(); I != 0; --I) {
378 SubobjectAdjustment &Adjustment = Adjustments[I-1];
379 switch (Adjustment.Kind) {
380 case SubobjectAdjustment::DerivedToBaseAdjustment:
382 CGF.GetAddressOfBaseClass(Object,
383 Adjustment.DerivedToBase.DerivedClass,
384 Adjustment.DerivedToBase.BasePath->path_begin(),
385 Adjustment.DerivedToBase.BasePath->path_end(),
386 /*NullCheckValue=*/false);
389 case SubobjectAdjustment::FieldAdjustment: {
391 CGF.EmitLValueForField(Object, Adjustment.Field, 0);
393 Object = LV.getAddress();
397 // For non-simple lvalues, we actually have to create a copy of
398 // the object we're binding to.
399 QualType T = Adjustment.Field->getType().getNonReferenceType()
400 .getUnqualifiedType();
401 Object = CreateReferenceTemporary(CGF, T, InitializedDecl);
402 LValue TempLV = CGF.MakeAddrLValue(Object,
403 Adjustment.Field->getType());
404 CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV), TempLV);
415 if (RV.isAggregate())
416 return RV.getAggregateAddr();
418 // Create a temporary variable that we can bind the reference to.
419 ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
424 CGF.getContext().getTypeAlignInChars(E->getType()).getQuantity();
426 CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary,
427 /*Volatile=*/false, Alignment, E->getType());
429 CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary,
431 return ReferenceTemporary;
435 CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E,
436 const NamedDecl *InitializedDecl) {
437 llvm::Value *ReferenceTemporary = 0;
438 const CXXDestructorDecl *ReferenceTemporaryDtor = 0;
439 QualType ObjCARCReferenceLifetimeType;
440 llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary,
441 ReferenceTemporaryDtor,
442 ObjCARCReferenceLifetimeType,
444 if (!ReferenceTemporaryDtor && ObjCARCReferenceLifetimeType.isNull())
445 return RValue::get(Value);
447 // Make sure to call the destructor for the reference temporary.
448 const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl);
449 if (VD && VD->hasGlobalStorage()) {
450 if (ReferenceTemporaryDtor) {
451 llvm::Constant *DtorFn =
452 CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete);
453 EmitCXXGlobalDtorRegistration(DtorFn,
454 cast<llvm::Constant>(ReferenceTemporary));
456 assert(!ObjCARCReferenceLifetimeType.isNull());
457 // Note: We intentionally do not register a global "destructor" to
458 // release the object.
461 return RValue::get(Value);
464 if (ReferenceTemporaryDtor)
465 PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary);
467 switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
468 case Qualifiers::OCL_None:
469 assert(0 && "Not a reference temporary that needs to be deallocated");
470 case Qualifiers::OCL_ExplicitNone:
471 case Qualifiers::OCL_Autoreleasing:
475 case Qualifiers::OCL_Strong: {
476 bool precise = VD && VD->hasAttr<ObjCPreciseLifetimeAttr>();
477 CleanupKind cleanupKind = getARCCleanupKind();
478 // This local is a GCC and MSVC compiler workaround.
479 Destroyer *destroyer = precise ? &destroyARCStrongPrecise :
480 &destroyARCStrongImprecise;
481 pushDestroy(cleanupKind, ReferenceTemporary, ObjCARCReferenceLifetimeType,
482 *destroyer, cleanupKind & EHCleanup);
486 case Qualifiers::OCL_Weak: {
487 // This local is a GCC and MSVC compiler workaround.
488 Destroyer *destroyer = &destroyARCWeak;
489 // __weak objects always get EH cleanups; otherwise, exceptions
490 // could cause really nasty crashes instead of mere leaks.
491 pushDestroy(NormalAndEHCleanup, ReferenceTemporary,
492 ObjCARCReferenceLifetimeType, *destroyer, true);
498 return RValue::get(Value);
502 /// getAccessedFieldNo - Given an encoded value and a result number, return the
503 /// input field number being accessed.
504 unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
505 const llvm::Constant *Elts) {
506 if (isa<llvm::ConstantAggregateZero>(Elts))
509 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
512 void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
516 // This needs to be to the standard address space.
517 Address = Builder.CreateBitCast(Address, Int8PtrTy);
519 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy);
521 // In time, people may want to control this and use a 1 here.
522 llvm::Value *Arg = Builder.getFalse();
523 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
524 llvm::BasicBlock *Cont = createBasicBlock();
525 llvm::BasicBlock *Check = createBasicBlock();
526 llvm::Value *NegativeOne = llvm::ConstantInt::get(IntPtrTy, -1ULL);
527 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
530 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
531 llvm::ConstantInt::get(IntPtrTy, Size)),
537 CodeGenFunction::ComplexPairTy CodeGenFunction::
538 EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
539 bool isInc, bool isPre) {
540 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
541 LV.isVolatileQualified());
543 llvm::Value *NextVal;
544 if (isa<llvm::IntegerType>(InVal.first->getType())) {
545 uint64_t AmountVal = isInc ? 1 : -1;
546 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
548 // Add the inc/dec to the real part.
549 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
551 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
552 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
555 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
557 // Add the inc/dec to the real part.
558 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
561 ComplexPairTy IncVal(NextVal, InVal.second);
563 // Store the updated result through the lvalue.
564 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
566 // If this is a postinc, return the value read from memory, otherwise use the
568 return isPre ? IncVal : InVal;
572 //===----------------------------------------------------------------------===//
573 // LValue Expression Emission
574 //===----------------------------------------------------------------------===//
576 RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
577 if (Ty->isVoidType())
578 return RValue::get(0);
580 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
581 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
582 llvm::Value *U = llvm::UndefValue::get(EltTy);
583 return RValue::getComplex(std::make_pair(U, U));
586 // If this is a use of an undefined aggregate type, the aggregate must have an
587 // identifiable address. Just because the contents of the value are undefined
588 // doesn't mean that the address can't be taken and compared.
589 if (hasAggregateLLVMType(Ty)) {
590 llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
591 return RValue::getAggregate(DestPtr);
594 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
597 RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
599 ErrorUnsupported(E, Name);
600 return GetUndefRValue(E->getType());
603 LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
605 ErrorUnsupported(E, Name);
606 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
607 return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
610 LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
611 LValue LV = EmitLValue(E);
612 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
613 EmitCheck(LV.getAddress(),
614 getContext().getTypeSizeInChars(E->getType()).getQuantity());
618 /// EmitLValue - Emit code to compute a designator that specifies the location
619 /// of the expression.
621 /// This can return one of two things: a simple address or a bitfield reference.
622 /// In either case, the LLVM Value* in the LValue structure is guaranteed to be
623 /// an LLVM pointer type.
625 /// If this returns a bitfield reference, nothing about the pointee type of the
626 /// LLVM value is known: For example, it may not be a pointer to an integer.
628 /// If this returns a normal address, and if the lvalue's C type is fixed size,
629 /// this method guarantees that the returned pointer type will point to an LLVM
630 /// type of the same size of the lvalue's type. If the lvalue has a variable
631 /// length type, this is not possible.
633 LValue CodeGenFunction::EmitLValue(const Expr *E) {
634 switch (E->getStmtClass()) {
635 default: return EmitUnsupportedLValue(E, "l-value expression");
637 case Expr::ObjCSelectorExprClass:
638 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
639 case Expr::ObjCIsaExprClass:
640 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
641 case Expr::BinaryOperatorClass:
642 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
643 case Expr::CompoundAssignOperatorClass:
644 if (!E->getType()->isAnyComplexType())
645 return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
646 return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
647 case Expr::CallExprClass:
648 case Expr::CXXMemberCallExprClass:
649 case Expr::CXXOperatorCallExprClass:
650 return EmitCallExprLValue(cast<CallExpr>(E));
651 case Expr::VAArgExprClass:
652 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
653 case Expr::DeclRefExprClass:
654 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
655 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
656 case Expr::GenericSelectionExprClass:
657 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
658 case Expr::PredefinedExprClass:
659 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
660 case Expr::StringLiteralClass:
661 return EmitStringLiteralLValue(cast<StringLiteral>(E));
662 case Expr::ObjCEncodeExprClass:
663 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
665 case Expr::BlockDeclRefExprClass:
666 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
668 case Expr::CXXTemporaryObjectExprClass:
669 case Expr::CXXConstructExprClass:
670 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
671 case Expr::CXXBindTemporaryExprClass:
672 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
673 case Expr::ExprWithCleanupsClass:
674 return EmitExprWithCleanupsLValue(cast<ExprWithCleanups>(E));
675 case Expr::CXXScalarValueInitExprClass:
676 return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E));
677 case Expr::CXXDefaultArgExprClass:
678 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
679 case Expr::CXXTypeidExprClass:
680 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
682 case Expr::ObjCMessageExprClass:
683 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
684 case Expr::ObjCIvarRefExprClass:
685 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
686 case Expr::ObjCPropertyRefExprClass:
687 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
688 case Expr::StmtExprClass:
689 return EmitStmtExprLValue(cast<StmtExpr>(E));
690 case Expr::UnaryOperatorClass:
691 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
692 case Expr::ArraySubscriptExprClass:
693 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
694 case Expr::ExtVectorElementExprClass:
695 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
696 case Expr::MemberExprClass:
697 return EmitMemberExpr(cast<MemberExpr>(E));
698 case Expr::CompoundLiteralExprClass:
699 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
700 case Expr::ConditionalOperatorClass:
701 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
702 case Expr::BinaryConditionalOperatorClass:
703 return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
704 case Expr::ChooseExprClass:
705 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
706 case Expr::OpaqueValueExprClass:
707 return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
708 case Expr::SubstNonTypeTemplateParmExprClass:
709 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
710 case Expr::ImplicitCastExprClass:
711 case Expr::CStyleCastExprClass:
712 case Expr::CXXFunctionalCastExprClass:
713 case Expr::CXXStaticCastExprClass:
714 case Expr::CXXDynamicCastExprClass:
715 case Expr::CXXReinterpretCastExprClass:
716 case Expr::CXXConstCastExprClass:
717 case Expr::ObjCBridgedCastExprClass:
718 return EmitCastLValue(cast<CastExpr>(E));
720 case Expr::MaterializeTemporaryExprClass:
721 return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
725 llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue) {
726 return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
727 lvalue.getAlignment(), lvalue.getType(),
728 lvalue.getTBAAInfo());
731 llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
732 unsigned Alignment, QualType Ty,
733 llvm::MDNode *TBAAInfo) {
734 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
736 Load->setVolatile(true);
738 Load->setAlignment(Alignment);
740 CGM.DecorateInstruction(Load, TBAAInfo);
742 return EmitFromMemory(Load, Ty);
745 static bool isBooleanUnderlyingType(QualType Ty) {
746 if (const EnumType *ET = dyn_cast<EnumType>(Ty))
747 return ET->getDecl()->getIntegerType()->isBooleanType();
751 llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
752 // Bool has a different representation in memory than in registers.
753 if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
754 // This should really always be an i1, but sometimes it's already
755 // an i8, and it's awkward to track those cases down.
756 if (Value->getType()->isIntegerTy(1))
757 return Builder.CreateZExt(Value, Builder.getInt8Ty(), "frombool");
758 assert(Value->getType()->isIntegerTy(8) && "value rep of bool not i1/i8");
764 llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
765 // Bool has a different representation in memory than in registers.
766 if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
767 assert(Value->getType()->isIntegerTy(8) && "memory rep of bool not i8");
768 return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
774 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
775 bool Volatile, unsigned Alignment,
777 llvm::MDNode *TBAAInfo) {
778 Value = EmitToMemory(Value, Ty);
780 llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
782 Store->setAlignment(Alignment);
784 CGM.DecorateInstruction(Store, TBAAInfo);
787 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue) {
788 EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
789 lvalue.getAlignment(), lvalue.getType(),
790 lvalue.getTBAAInfo());
793 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
794 /// method emits the address of the lvalue, then loads the result as an rvalue,
795 /// returning the rvalue.
796 RValue CodeGenFunction::EmitLoadOfLValue(LValue LV) {
797 if (LV.isObjCWeak()) {
798 // load of a __weak object.
799 llvm::Value *AddrWeakObj = LV.getAddress();
800 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
803 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak)
804 return RValue::get(EmitARCLoadWeak(LV.getAddress()));
807 assert(!LV.getType()->isFunctionType());
809 // Everything needs a load.
810 return RValue::get(EmitLoadOfScalar(LV));
813 if (LV.isVectorElt()) {
814 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
815 LV.isVolatileQualified(), "tmp");
816 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
820 // If this is a reference to a subset of the elements of a vector, either
821 // shuffle the input or extract/insert them as appropriate.
822 if (LV.isExtVectorElt())
823 return EmitLoadOfExtVectorElementLValue(LV);
826 return EmitLoadOfBitfieldLValue(LV);
828 assert(LV.isPropertyRef() && "Unknown LValue type!");
829 return EmitLoadOfPropertyRefLValue(LV);
832 RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) {
833 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
835 // Get the output type.
836 const llvm::Type *ResLTy = ConvertType(LV.getType());
837 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
839 // Compute the result as an OR of all of the individual component accesses.
840 llvm::Value *Res = 0;
841 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
842 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
844 // Get the field pointer.
845 llvm::Value *Ptr = LV.getBitFieldBaseAddr();
847 // Only offset by the field index if used, so that incoming values are not
848 // required to be structures.
850 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
852 // Offset by the byte offset, if used.
853 if (!AI.FieldByteOffset.isZero()) {
854 Ptr = EmitCastToVoidPtr(Ptr);
855 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
859 // Cast to the access type.
860 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(getLLVMContext(),
862 CGM.getContext().getTargetAddressSpace(LV.getType()));
863 Ptr = Builder.CreateBitCast(Ptr, PTy);
866 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
867 if (!AI.AccessAlignment.isZero())
868 Load->setAlignment(AI.AccessAlignment.getQuantity());
870 // Shift out unused low bits and mask out unused high bits.
871 llvm::Value *Val = Load;
872 if (AI.FieldBitStart)
873 Val = Builder.CreateLShr(Load, AI.FieldBitStart);
874 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
878 // Extend or truncate to the target size.
879 if (AI.AccessWidth < ResSizeInBits)
880 Val = Builder.CreateZExt(Val, ResLTy);
881 else if (AI.AccessWidth > ResSizeInBits)
882 Val = Builder.CreateTrunc(Val, ResLTy);
884 // Shift into place, and OR into the result.
885 if (AI.TargetBitOffset)
886 Val = Builder.CreateShl(Val, AI.TargetBitOffset);
887 Res = Res ? Builder.CreateOr(Res, Val) : Val;
890 // If the bit-field is signed, perform the sign-extension.
892 // FIXME: This can easily be folded into the load of the high bits, which
893 // could also eliminate the mask of high bits in some situations.
894 if (Info.isSigned()) {
895 unsigned ExtraBits = ResSizeInBits - Info.getSize();
897 Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
898 ExtraBits, "bf.val.sext");
901 return RValue::get(Res);
904 // If this is a reference to a subset of the elements of a vector, create an
905 // appropriate shufflevector.
906 RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
907 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
908 LV.isVolatileQualified(), "tmp");
910 const llvm::Constant *Elts = LV.getExtVectorElts();
912 // If the result of the expression is a non-vector type, we must be extracting
913 // a single element. Just codegen as an extractelement.
914 const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
916 unsigned InIdx = getAccessedFieldNo(0, Elts);
917 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
918 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
921 // Always use shuffle vector to try to retain the original program structure
922 unsigned NumResultElts = ExprVT->getNumElements();
924 llvm::SmallVector<llvm::Constant*, 4> Mask;
925 for (unsigned i = 0; i != NumResultElts; ++i) {
926 unsigned InIdx = getAccessedFieldNo(i, Elts);
927 Mask.push_back(llvm::ConstantInt::get(Int32Ty, InIdx));
930 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
931 Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
933 return RValue::get(Vec);
938 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
939 /// lvalue, where both are guaranteed to the have the same type, and that type
941 void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst) {
942 if (!Dst.isSimple()) {
943 if (Dst.isVectorElt()) {
944 // Read/modify/write the vector, inserting the new element.
945 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
946 Dst.isVolatileQualified(), "tmp");
947 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
948 Dst.getVectorIdx(), "vecins");
949 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
953 // If this is an update of extended vector elements, insert them as
955 if (Dst.isExtVectorElt())
956 return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
958 if (Dst.isBitField())
959 return EmitStoreThroughBitfieldLValue(Src, Dst);
961 assert(Dst.isPropertyRef() && "Unknown LValue type");
962 return EmitStoreThroughPropertyRefLValue(Src, Dst);
965 // There's special magic for assigning into an ARC-qualified l-value.
966 if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
968 case Qualifiers::OCL_None:
969 llvm_unreachable("present but none");
971 case Qualifiers::OCL_ExplicitNone:
975 case Qualifiers::OCL_Strong:
976 EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
979 case Qualifiers::OCL_Weak:
980 EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true);
983 case Qualifiers::OCL_Autoreleasing:
984 Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
985 Src.getScalarVal()));
986 // fall into the normal path
991 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
992 // load of a __weak object.
993 llvm::Value *LvalueDst = Dst.getAddress();
994 llvm::Value *src = Src.getScalarVal();
995 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
999 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
1000 // load of a __strong object.
1001 llvm::Value *LvalueDst = Dst.getAddress();
1002 llvm::Value *src = Src.getScalarVal();
1003 if (Dst.isObjCIvar()) {
1004 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
1005 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
1006 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
1007 llvm::Value *dst = RHS;
1008 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1010 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
1011 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
1012 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
1014 } else if (Dst.isGlobalObjCRef()) {
1015 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
1016 Dst.isThreadLocalRef());
1019 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
1023 assert(Src.isScalar() && "Can't emit an agg store with this method");
1024 EmitStoreOfScalar(Src.getScalarVal(), Dst);
1027 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
1028 llvm::Value **Result) {
1029 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
1031 // Get the output type.
1032 const llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
1033 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
1035 // Get the source value, truncated to the width of the bit-field.
1036 llvm::Value *SrcVal = Src.getScalarVal();
1038 if (Dst.getType()->isBooleanType())
1039 SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
1041 SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
1045 // Return the new value of the bit-field, if requested.
1047 // Cast back to the proper type for result.
1048 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
1049 llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
1052 // Sign extend if necessary.
1053 if (Info.isSigned()) {
1054 unsigned ExtraBits = ResSizeInBits - Info.getSize();
1056 ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
1057 ExtraBits, "bf.reload.sext");
1060 *Result = ReloadVal;
1063 // Iterate over the components, writing each piece to memory.
1064 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1065 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1067 // Get the field pointer.
1068 llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
1069 unsigned addressSpace =
1070 cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
1072 // Only offset by the field index if used, so that incoming values are not
1073 // required to be structures.
1075 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
1077 // Offset by the byte offset, if used.
1078 if (!AI.FieldByteOffset.isZero()) {
1079 Ptr = EmitCastToVoidPtr(Ptr);
1080 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
1084 // Cast to the access type.
1085 const llvm::Type *AccessLTy =
1086 llvm::Type::getIntNTy(getLLVMContext(), AI.AccessWidth);
1088 const llvm::Type *PTy = AccessLTy->getPointerTo(addressSpace);
1089 Ptr = Builder.CreateBitCast(Ptr, PTy);
1091 // Extract the piece of the bit-field value to write in this access, limited
1092 // to the values that are part of this access.
1093 llvm::Value *Val = SrcVal;
1094 if (AI.TargetBitOffset)
1095 Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
1096 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
1097 AI.TargetBitWidth));
1099 // Extend or truncate to the access size.
1100 if (ResSizeInBits < AI.AccessWidth)
1101 Val = Builder.CreateZExt(Val, AccessLTy);
1102 else if (ResSizeInBits > AI.AccessWidth)
1103 Val = Builder.CreateTrunc(Val, AccessLTy);
1105 // Shift into the position in memory.
1106 if (AI.FieldBitStart)
1107 Val = Builder.CreateShl(Val, AI.FieldBitStart);
1109 // If necessary, load and OR in bits that are outside of the bit-field.
1110 if (AI.TargetBitWidth != AI.AccessWidth) {
1111 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
1112 if (!AI.AccessAlignment.isZero())
1113 Load->setAlignment(AI.AccessAlignment.getQuantity());
1115 // Compute the mask for zeroing the bits that are part of the bit-field.
1116 llvm::APInt InvMask =
1117 ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
1118 AI.FieldBitStart + AI.TargetBitWidth);
1120 // Apply the mask and OR in to the value to write.
1121 Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
1125 llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
1126 Dst.isVolatileQualified());
1127 if (!AI.AccessAlignment.isZero())
1128 Store->setAlignment(AI.AccessAlignment.getQuantity());
1132 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
1134 // This access turns into a read/modify/write of the vector. Load the input
1136 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
1137 Dst.isVolatileQualified(), "tmp");
1138 const llvm::Constant *Elts = Dst.getExtVectorElts();
1140 llvm::Value *SrcVal = Src.getScalarVal();
1142 if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
1143 unsigned NumSrcElts = VTy->getNumElements();
1144 unsigned NumDstElts =
1145 cast<llvm::VectorType>(Vec->getType())->getNumElements();
1146 if (NumDstElts == NumSrcElts) {
1147 // Use shuffle vector is the src and destination are the same number of
1148 // elements and restore the vector mask since it is on the side it will be
1150 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
1151 for (unsigned i = 0; i != NumSrcElts; ++i) {
1152 unsigned InIdx = getAccessedFieldNo(i, Elts);
1153 Mask[InIdx] = llvm::ConstantInt::get(Int32Ty, i);
1156 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1157 Vec = Builder.CreateShuffleVector(SrcVal,
1158 llvm::UndefValue::get(Vec->getType()),
1160 } else if (NumDstElts > NumSrcElts) {
1161 // Extended the source vector to the same length and then shuffle it
1162 // into the destination.
1163 // FIXME: since we're shuffling with undef, can we just use the indices
1164 // into that? This could be simpler.
1165 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
1167 for (i = 0; i != NumSrcElts; ++i)
1168 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1169 for (; i != NumDstElts; ++i)
1170 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
1171 llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
1172 llvm::Value *ExtSrcVal =
1173 Builder.CreateShuffleVector(SrcVal,
1174 llvm::UndefValue::get(SrcVal->getType()),
1177 llvm::SmallVector<llvm::Constant*, 4> Mask;
1178 for (unsigned i = 0; i != NumDstElts; ++i)
1179 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1181 // modify when what gets shuffled in
1182 for (unsigned i = 0; i != NumSrcElts; ++i) {
1183 unsigned Idx = getAccessedFieldNo(i, Elts);
1184 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
1186 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1187 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
1189 // We should never shorten the vector
1190 assert(0 && "unexpected shorten vector length");
1193 // If the Src is a scalar (not a vector) it must be updating one element.
1194 unsigned InIdx = getAccessedFieldNo(0, Elts);
1195 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1196 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
1199 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
1202 // setObjCGCLValueClass - sets class of he lvalue for the purpose of
1203 // generating write-barries API. It is currently a global, ivar,
1205 static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1207 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
1210 if (isa<ObjCIvarRefExpr>(E)) {
1211 LV.setObjCIvar(true);
1212 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1213 LV.setBaseIvarExp(Exp->getBase());
1214 LV.setObjCArray(E->getType()->isArrayType());
1218 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1219 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1220 if (VD->hasGlobalStorage()) {
1221 LV.setGlobalObjCRef(true);
1222 LV.setThreadLocalRef(VD->isThreadSpecified());
1225 LV.setObjCArray(E->getType()->isArrayType());
1229 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
1230 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1234 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
1235 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1236 if (LV.isObjCIvar()) {
1237 // If cast is to a structure pointer, follow gcc's behavior and make it
1238 // a non-ivar write-barrier.
1239 QualType ExpTy = E->getType();
1240 if (ExpTy->isPointerType())
1241 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1242 if (ExpTy->isRecordType())
1243 LV.setObjCIvar(false);
1248 if (const GenericSelectionExpr *Exp = dyn_cast<GenericSelectionExpr>(E)) {
1249 setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
1253 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1254 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1258 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
1259 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1263 if (const ObjCBridgedCastExpr *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
1264 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1268 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1269 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1270 if (LV.isObjCIvar() && !LV.isObjCArray())
1271 // Using array syntax to assigning to what an ivar points to is not
1272 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1273 LV.setObjCIvar(false);
1274 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1275 // Using array syntax to assigning to what global points to is not
1276 // same as assigning to the global itself. {id *G;} G[i] = 0;
1277 LV.setGlobalObjCRef(false);
1281 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
1282 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1283 // We don't know if member is an 'ivar', but this flag is looked at
1284 // only in the context of LV.isObjCIvar().
1285 LV.setObjCArray(E->getType()->isArrayType());
1290 static llvm::Value *
1291 EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
1292 llvm::Value *V, llvm::Type *IRType,
1293 llvm::StringRef Name = llvm::StringRef()) {
1294 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1295 return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
1298 static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1299 const Expr *E, const VarDecl *VD) {
1300 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
1301 "Var decl must have external storage or be a file var decl!");
1303 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1304 if (VD->getType()->isReferenceType())
1305 V = CGF.Builder.CreateLoad(V, "tmp");
1307 V = EmitBitCastOfLValueToProperType(CGF, V,
1308 CGF.getTypes().ConvertTypeForMem(E->getType()));
1310 unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
1311 LValue LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
1312 setObjCGCLValueClass(CGF.getContext(), E, LV);
1316 static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1317 const Expr *E, const FunctionDecl *FD) {
1318 llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
1319 if (!FD->hasPrototype()) {
1320 if (const FunctionProtoType *Proto =
1321 FD->getType()->getAs<FunctionProtoType>()) {
1322 // Ugly case: for a K&R-style definition, the type of the definition
1323 // isn't the same as the type of a use. Correct for this with a
1325 QualType NoProtoType =
1326 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1327 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1328 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1331 unsigned Alignment = CGF.getContext().getDeclAlign(FD).getQuantity();
1332 return CGF.MakeAddrLValue(V, E->getType(), Alignment);
1335 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
1336 const NamedDecl *ND = E->getDecl();
1337 unsigned Alignment = getContext().getDeclAlign(ND).getQuantity();
1339 if (ND->hasAttr<WeakRefAttr>()) {
1340 const ValueDecl *VD = cast<ValueDecl>(ND);
1341 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1342 return MakeAddrLValue(Aliasee, E->getType(), Alignment);
1345 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1347 // Check if this is a global variable.
1348 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1349 return EmitGlobalVarDeclLValue(*this, E, VD);
1351 bool NonGCable = VD->hasLocalStorage() &&
1352 !VD->getType()->isReferenceType() &&
1353 !VD->hasAttr<BlocksAttr>();
1355 llvm::Value *V = LocalDeclMap[VD];
1356 if (!V && VD->isStaticLocal())
1357 V = CGM.getStaticLocalDeclAddress(VD);
1358 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1360 if (VD->hasAttr<BlocksAttr>())
1361 V = BuildBlockByrefAddress(V, VD);
1363 if (VD->getType()->isReferenceType())
1364 V = Builder.CreateLoad(V, "tmp");
1366 V = EmitBitCastOfLValueToProperType(*this, V,
1367 getTypes().ConvertTypeForMem(E->getType()));
1369 LValue LV = MakeAddrLValue(V, E->getType(), Alignment);
1371 LV.getQuals().removeObjCGCAttr();
1374 setObjCGCLValueClass(getContext(), E, LV);
1378 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(ND))
1379 return EmitFunctionDeclLValue(*this, E, fn);
1381 assert(false && "Unhandled DeclRefExpr");
1383 // an invalid LValue, but the assert will
1384 // ensure that this point is never reached.
1388 LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
1389 unsigned Alignment =
1390 getContext().getDeclAlign(E->getDecl()).getQuantity();
1391 return MakeAddrLValue(GetAddrOfBlockDecl(E), E->getType(), Alignment);
1394 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1395 // __extension__ doesn't affect lvalue-ness.
1396 if (E->getOpcode() == UO_Extension)
1397 return EmitLValue(E->getSubExpr());
1399 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
1400 switch (E->getOpcode()) {
1401 default: assert(0 && "Unknown unary operator lvalue!");
1403 QualType T = E->getSubExpr()->getType()->getPointeeType();
1404 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
1406 LValue LV = MakeAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
1407 LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
1409 // We should not generate __weak write barrier on indirect reference
1410 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1411 // But, we continue to generate __strong write barrier on indirect write
1412 // into a pointer to object.
1413 if (getContext().getLangOptions().ObjC1 &&
1414 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1416 LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1421 LValue LV = EmitLValue(E->getSubExpr());
1422 assert(LV.isSimple() && "real/imag on non-ordinary l-value");
1423 llvm::Value *Addr = LV.getAddress();
1425 // real and imag are valid on scalars. This is a faster way of
1427 if (!cast<llvm::PointerType>(Addr->getType())
1428 ->getElementType()->isStructTy()) {
1429 assert(E->getSubExpr()->getType()->isArithmeticType());
1433 assert(E->getSubExpr()->getType()->isAnyComplexType());
1435 unsigned Idx = E->getOpcode() == UO_Imag;
1436 return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
1442 LValue LV = EmitLValue(E->getSubExpr());
1443 bool isInc = E->getOpcode() == UO_PreInc;
1445 if (E->getType()->isAnyComplexType())
1446 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1448 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1454 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
1455 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
1459 LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
1460 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1465 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
1466 switch (E->getIdentType()) {
1468 return EmitUnsupportedLValue(E, "predefined expression");
1470 case PredefinedExpr::Func:
1471 case PredefinedExpr::Function:
1472 case PredefinedExpr::PrettyFunction: {
1473 unsigned Type = E->getIdentType();
1474 std::string GlobalVarName;
1477 default: assert(0 && "Invalid type");
1478 case PredefinedExpr::Func:
1479 GlobalVarName = "__func__.";
1481 case PredefinedExpr::Function:
1482 GlobalVarName = "__FUNCTION__.";
1484 case PredefinedExpr::PrettyFunction:
1485 GlobalVarName = "__PRETTY_FUNCTION__.";
1489 llvm::StringRef FnName = CurFn->getName();
1490 if (FnName.startswith("\01"))
1491 FnName = FnName.substr(1);
1492 GlobalVarName += FnName;
1494 const Decl *CurDecl = CurCodeDecl;
1496 CurDecl = getContext().getTranslationUnitDecl();
1498 std::string FunctionName =
1499 (isa<BlockDecl>(CurDecl)
1501 : PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurDecl));
1504 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
1505 return MakeAddrLValue(C, E->getType());
1510 llvm::BasicBlock *CodeGenFunction::getTrapBB() {
1511 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1513 // If we are not optimzing, don't collapse all calls to trap in the function
1514 // to the same call, that way, in the debugger they can see which operation
1515 // did in fact fail. If we are optimizing, we collapse all calls to trap down
1516 // to just one per function to save on codesize.
1517 if (GCO.OptimizationLevel && TrapBB)
1520 llvm::BasicBlock *Cont = 0;
1521 if (HaveInsertPoint()) {
1522 Cont = createBasicBlock("cont");
1525 TrapBB = createBasicBlock("trap");
1528 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap);
1529 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1530 TrapCall->setDoesNotReturn();
1531 TrapCall->setDoesNotThrow();
1532 Builder.CreateUnreachable();
1539 /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
1540 /// array to pointer, return the array subexpression.
1541 static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
1542 // If this isn't just an array->pointer decay, bail out.
1543 const CastExpr *CE = dyn_cast<CastExpr>(E);
1544 if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay)
1547 // If this is a decay from variable width array, bail out.
1548 const Expr *SubExpr = CE->getSubExpr();
1549 if (SubExpr->getType()->isVariableArrayType())
1555 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1556 // The index must always be an integer, which is not an aggregate. Emit it.
1557 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
1558 QualType IdxTy = E->getIdx()->getType();
1559 bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
1561 // If the base is a vector type, then we are forming a vector element lvalue
1562 // with this subscript.
1563 if (E->getBase()->getType()->isVectorType()) {
1564 // Emit the vector as an lvalue to get its address.
1565 LValue LHS = EmitLValue(E->getBase());
1566 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
1567 Idx = Builder.CreateIntCast(Idx, Int32Ty, IdxSigned, "vidx");
1568 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
1569 E->getBase()->getType());
1572 // Extend or truncate the index type to 32 or 64-bits.
1573 if (Idx->getType() != IntPtrTy)
1574 Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
1576 // FIXME: As llvm implements the object size checking, this can come out.
1577 if (CatchUndefined) {
1578 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())){
1579 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1580 if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
1581 if (const ConstantArrayType *CAT
1582 = getContext().getAsConstantArrayType(DRE->getType())) {
1583 llvm::APInt Size = CAT->getSize();
1584 llvm::BasicBlock *Cont = createBasicBlock("cont");
1585 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
1586 llvm::ConstantInt::get(Idx->getType(), Size)),
1595 // We know that the pointer points to a type of the correct size, unless the
1596 // size is a VLA or Objective-C interface.
1597 llvm::Value *Address = 0;
1598 unsigned ArrayAlignment = 0;
1599 if (const VariableArrayType *vla =
1600 getContext().getAsVariableArrayType(E->getType())) {
1601 // The base must be a pointer, which is not an aggregate. Emit
1602 // it. It needs to be emitted first in case it's what captures
1604 Address = EmitScalarExpr(E->getBase());
1606 // The element count here is the total number of non-VLA elements.
1607 llvm::Value *numElements = getVLASize(vla).first;
1609 // Effectively, the multiply by the VLA size is part of the GEP.
1610 // GEP indexes are signed, and scaling an index isn't permitted to
1611 // signed-overflow, so we use the same semantics for our explicit
1612 // multiply. We suppress this if overflow is not undefined behavior.
1613 if (getLangOptions().isSignedOverflowDefined()) {
1614 Idx = Builder.CreateMul(Idx, numElements);
1615 Address = Builder.CreateGEP(Address, Idx, "arrayidx");
1617 Idx = Builder.CreateNSWMul(Idx, numElements);
1618 Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
1620 } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
1621 // Indexing over an interface, as in "NSString *P; P[4];"
1622 llvm::Value *InterfaceSize =
1623 llvm::ConstantInt::get(Idx->getType(),
1624 getContext().getTypeSizeInChars(OIT).getQuantity());
1626 Idx = Builder.CreateMul(Idx, InterfaceSize);
1628 // The base must be a pointer, which is not an aggregate. Emit it.
1629 llvm::Value *Base = EmitScalarExpr(E->getBase());
1630 Address = EmitCastToVoidPtr(Base);
1631 Address = Builder.CreateGEP(Address, Idx, "arrayidx");
1632 Address = Builder.CreateBitCast(Address, Base->getType());
1633 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
1634 // If this is A[i] where A is an array, the frontend will have decayed the
1635 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
1636 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
1637 // "gep x, i" here. Emit one "gep A, 0, i".
1638 assert(Array->getType()->isArrayType() &&
1639 "Array to pointer decay must have array source type!");
1640 LValue ArrayLV = EmitLValue(Array);
1641 llvm::Value *ArrayPtr = ArrayLV.getAddress();
1642 llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
1643 llvm::Value *Args[] = { Zero, Idx };
1645 // Propagate the alignment from the array itself to the result.
1646 ArrayAlignment = ArrayLV.getAlignment();
1648 if (getContext().getLangOptions().isSignedOverflowDefined())
1649 Address = Builder.CreateGEP(ArrayPtr, Args, Args+2, "arrayidx");
1651 Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, Args+2, "arrayidx");
1653 // The base must be a pointer, which is not an aggregate. Emit it.
1654 llvm::Value *Base = EmitScalarExpr(E->getBase());
1655 if (getContext().getLangOptions().isSignedOverflowDefined())
1656 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
1658 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
1661 QualType T = E->getBase()->getType()->getPointeeType();
1662 assert(!T.isNull() &&
1663 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
1665 // Limit the alignment to that of the result type.
1666 if (ArrayAlignment) {
1667 unsigned Align = getContext().getTypeAlignInChars(T).getQuantity();
1668 ArrayAlignment = std::min(Align, ArrayAlignment);
1671 LValue LV = MakeAddrLValue(Address, T, ArrayAlignment);
1672 LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
1674 if (getContext().getLangOptions().ObjC1 &&
1675 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
1676 LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1677 setObjCGCLValueClass(getContext(), E, LV);
1683 llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1684 llvm::SmallVector<unsigned, 4> &Elts) {
1685 llvm::SmallVector<llvm::Constant*, 4> CElts;
1687 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1688 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
1689 CElts.push_back(llvm::ConstantInt::get(Int32Ty, Elts[i]));
1691 return llvm::ConstantVector::get(CElts);
1694 LValue CodeGenFunction::
1695 EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
1696 // Emit the base vector as an l-value.
1699 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
1701 // If it is a pointer to a vector, emit the address and form an lvalue with
1703 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
1704 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
1705 Base = MakeAddrLValue(Ptr, PT->getPointeeType());
1706 Base.getQuals().removeObjCGCAttr();
1707 } else if (E->getBase()->isGLValue()) {
1708 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1709 // emit the base as an lvalue.
1710 assert(E->getBase()->getType()->isVectorType());
1711 Base = EmitLValue(E->getBase());
1713 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
1714 assert(E->getBase()->getType()->isVectorType() &&
1715 "Result must be a vector");
1716 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1718 // Store the vector to memory (because LValue wants an address).
1719 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
1720 Builder.CreateStore(Vec, VecMem);
1721 Base = MakeAddrLValue(VecMem, E->getBase()->getType());
1725 E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
1727 // Encode the element access list into a vector of unsigned indices.
1728 llvm::SmallVector<unsigned, 4> Indices;
1729 E->getEncodedElementAccess(Indices);
1731 if (Base.isSimple()) {
1732 llvm::Constant *CV = GenerateConstantVector(getLLVMContext(), Indices);
1733 return LValue::MakeExtVectorElt(Base.getAddress(), CV, type);
1735 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1737 llvm::Constant *BaseElts = Base.getExtVectorElts();
1738 llvm::SmallVector<llvm::Constant *, 4> CElts;
1740 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1741 if (isa<llvm::ConstantAggregateZero>(BaseElts))
1742 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
1744 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
1746 llvm::Constant *CV = llvm::ConstantVector::get(CElts);
1747 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type);
1750 LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
1751 bool isNonGC = false;
1752 Expr *BaseExpr = E->getBase();
1753 llvm::Value *BaseValue = NULL;
1754 Qualifiers BaseQuals;
1756 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
1758 BaseValue = EmitScalarExpr(BaseExpr);
1759 const PointerType *PTy =
1760 BaseExpr->getType()->getAs<PointerType>();
1761 BaseQuals = PTy->getPointeeType().getQualifiers();
1763 LValue BaseLV = EmitLValue(BaseExpr);
1764 if (BaseLV.isNonGC())
1766 // FIXME: this isn't right for bitfields.
1767 BaseValue = BaseLV.getAddress();
1768 QualType BaseTy = BaseExpr->getType();
1769 BaseQuals = BaseTy.getQualifiers();
1772 NamedDecl *ND = E->getMemberDecl();
1773 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1774 LValue LV = EmitLValueForField(BaseValue, Field,
1775 BaseQuals.getCVRQualifiers());
1776 LV.setNonGC(isNonGC);
1777 setObjCGCLValueClass(getContext(), E, LV);
1781 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1782 return EmitGlobalVarDeclLValue(*this, E, VD);
1784 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1785 return EmitFunctionDeclLValue(*this, E, FD);
1787 assert(false && "Unhandled member declaration!");
1791 LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value *BaseValue,
1792 const FieldDecl *Field,
1793 unsigned CVRQualifiers) {
1794 const CGRecordLayout &RL =
1795 CGM.getTypes().getCGRecordLayout(Field->getParent());
1796 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
1797 return LValue::MakeBitfield(BaseValue, Info,
1798 Field->getType().withCVRQualifiers(CVRQualifiers));
1801 /// EmitLValueForAnonRecordField - Given that the field is a member of
1802 /// an anonymous struct or union buried inside a record, and given
1803 /// that the base value is a pointer to the enclosing record, derive
1804 /// an lvalue for the ultimate field.
1805 LValue CodeGenFunction::EmitLValueForAnonRecordField(llvm::Value *BaseValue,
1806 const IndirectFieldDecl *Field,
1807 unsigned CVRQualifiers) {
1808 IndirectFieldDecl::chain_iterator I = Field->chain_begin(),
1809 IEnd = Field->chain_end();
1811 LValue LV = EmitLValueForField(BaseValue, cast<FieldDecl>(*I),
1813 if (++I == IEnd) return LV;
1815 assert(LV.isSimple());
1816 BaseValue = LV.getAddress();
1817 CVRQualifiers |= LV.getVRQualifiers();
1821 LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr,
1822 const FieldDecl *field,
1824 if (field->isBitField())
1825 return EmitLValueForBitfield(baseAddr, field, cvr);
1827 const RecordDecl *rec = field->getParent();
1828 QualType type = field->getType();
1830 bool mayAlias = rec->hasAttr<MayAliasAttr>();
1832 llvm::Value *addr = baseAddr;
1833 if (rec->isUnion()) {
1834 // For unions, there is no pointer adjustment.
1835 assert(!type->isReferenceType() && "union has reference member");
1837 // For structs, we GEP to the field that the record layout suggests.
1838 unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
1839 addr = Builder.CreateStructGEP(addr, idx, field->getName());
1841 // If this is a reference field, load the reference right now.
1842 if (const ReferenceType *refType = type->getAs<ReferenceType>()) {
1843 llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
1844 if (cvr & Qualifiers::Volatile) load->setVolatile(true);
1846 if (CGM.shouldUseTBAA()) {
1849 tbaa = CGM.getTBAAInfo(getContext().CharTy);
1851 tbaa = CGM.getTBAAInfo(type);
1852 CGM.DecorateInstruction(load, tbaa);
1857 type = refType->getPointeeType();
1858 cvr = 0; // qualifiers don't recursively apply to referencee
1862 // Make sure that the address is pointing to the right type. This is critical
1863 // for both unions and structs. A union needs a bitcast, a struct element
1864 // will need a bitcast if the LLVM type laid out doesn't match the desired
1866 addr = EmitBitCastOfLValueToProperType(*this, addr,
1867 CGM.getTypes().ConvertTypeForMem(type),
1870 unsigned alignment = getContext().getDeclAlign(field).getQuantity();
1871 LValue LV = MakeAddrLValue(addr, type, alignment);
1872 LV.getQuals().addCVRQualifiers(cvr);
1874 // __weak attribute on a field is ignored.
1875 if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
1876 LV.getQuals().removeObjCGCAttr();
1878 // Fields of may_alias structs act like 'char' for TBAA purposes.
1879 // FIXME: this should get propagated down through anonymous structs
1881 if (mayAlias && LV.getTBAAInfo())
1882 LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
1888 CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue,
1889 const FieldDecl *Field,
1890 unsigned CVRQualifiers) {
1891 QualType FieldType = Field->getType();
1893 if (!FieldType->isReferenceType())
1894 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1896 const CGRecordLayout &RL =
1897 CGM.getTypes().getCGRecordLayout(Field->getParent());
1898 unsigned idx = RL.getLLVMFieldNo(Field);
1899 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1900 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1903 // Make sure that the address is pointing to the right type. This is critical
1904 // for both unions and structs. A union needs a bitcast, a struct element
1905 // will need a bitcast if the LLVM type laid out doesn't match the desired
1907 const llvm::Type *llvmType = ConvertTypeForMem(FieldType);
1908 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1909 V = Builder.CreateBitCast(V, llvmType->getPointerTo(AS));
1911 unsigned Alignment = getContext().getDeclAlign(Field).getQuantity();
1912 return MakeAddrLValue(V, FieldType, Alignment);
1915 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
1916 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
1917 const Expr *InitExpr = E->getInitializer();
1918 LValue Result = MakeAddrLValue(DeclPtr, E->getType());
1920 EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
1926 LValue CodeGenFunction::
1927 EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
1928 if (!expr->isGLValue()) {
1929 // ?: here should be an aggregate.
1930 assert((hasAggregateLLVMType(expr->getType()) &&
1931 !expr->getType()->isAnyComplexType()) &&
1932 "Unexpected conditional operator!");
1933 return EmitAggExprToLValue(expr);
1936 const Expr *condExpr = expr->getCond();
1938 if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
1939 const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
1940 if (!CondExprBool) std::swap(live, dead);
1942 if (!ContainsLabel(dead))
1943 return EmitLValue(live);
1946 OpaqueValueMapping binding(*this, expr);
1948 llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
1949 llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
1950 llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
1952 ConditionalEvaluation eval(*this);
1953 EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock);
1955 // Any temporaries created here are conditional.
1956 EmitBlock(lhsBlock);
1958 LValue lhs = EmitLValue(expr->getTrueExpr());
1961 if (!lhs.isSimple())
1962 return EmitUnsupportedLValue(expr, "conditional operator");
1964 lhsBlock = Builder.GetInsertBlock();
1965 Builder.CreateBr(contBlock);
1967 // Any temporaries created here are conditional.
1968 EmitBlock(rhsBlock);
1970 LValue rhs = EmitLValue(expr->getFalseExpr());
1972 if (!rhs.isSimple())
1973 return EmitUnsupportedLValue(expr, "conditional operator");
1974 rhsBlock = Builder.GetInsertBlock();
1976 EmitBlock(contBlock);
1978 llvm::PHINode *phi = Builder.CreatePHI(lhs.getAddress()->getType(), 2,
1980 phi->addIncoming(lhs.getAddress(), lhsBlock);
1981 phi->addIncoming(rhs.getAddress(), rhsBlock);
1982 return MakeAddrLValue(phi, expr->getType());
1985 /// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1986 /// If the cast is a dynamic_cast, we can have the usual lvalue result,
1987 /// otherwise if a cast is needed by the code generator in an lvalue context,
1988 /// then it must mean that we need the address of an aggregate in order to
1989 /// access one of its fields. This can happen for all the reasons that casts
1990 /// are permitted with aggregate result, including noop aggregate casts, and
1991 /// cast from scalar to union.
1992 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1993 switch (E->getCastKind()) {
1995 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1998 llvm_unreachable("dependent cast kind in IR gen!");
2000 case CK_GetObjCProperty: {
2001 LValue LV = EmitLValue(E->getSubExpr());
2002 assert(LV.isPropertyRef());
2003 RValue RV = EmitLoadOfPropertyRefLValue(LV);
2005 // Property is an aggregate r-value.
2006 if (RV.isAggregate()) {
2007 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2010 // Implicit property returns an l-value.
2011 assert(RV.isScalar());
2012 return MakeAddrLValue(RV.getScalarVal(), E->getSubExpr()->getType());
2016 case CK_LValueToRValue:
2017 if (!E->getSubExpr()->Classify(getContext()).isPRValue()
2018 || E->getType()->isRecordType())
2019 return EmitLValue(E->getSubExpr());
2020 // Fall through to synthesize a temporary.
2023 case CK_ArrayToPointerDecay:
2024 case CK_FunctionToPointerDecay:
2025 case CK_NullToMemberPointer:
2026 case CK_NullToPointer:
2027 case CK_IntegralToPointer:
2028 case CK_PointerToIntegral:
2029 case CK_PointerToBoolean:
2030 case CK_VectorSplat:
2031 case CK_IntegralCast:
2032 case CK_IntegralToBoolean:
2033 case CK_IntegralToFloating:
2034 case CK_FloatingToIntegral:
2035 case CK_FloatingToBoolean:
2036 case CK_FloatingCast:
2037 case CK_FloatingRealToComplex:
2038 case CK_FloatingComplexToReal:
2039 case CK_FloatingComplexToBoolean:
2040 case CK_FloatingComplexCast:
2041 case CK_FloatingComplexToIntegralComplex:
2042 case CK_IntegralRealToComplex:
2043 case CK_IntegralComplexToReal:
2044 case CK_IntegralComplexToBoolean:
2045 case CK_IntegralComplexCast:
2046 case CK_IntegralComplexToFloatingComplex:
2047 case CK_DerivedToBaseMemberPointer:
2048 case CK_BaseToDerivedMemberPointer:
2049 case CK_MemberPointerToBoolean:
2050 case CK_AnyPointerToBlockPointerCast:
2051 case CK_ObjCProduceObject:
2052 case CK_ObjCConsumeObject:
2053 case CK_ObjCReclaimReturnedObject: {
2054 // These casts only produce lvalues when we're binding a reference to a
2055 // temporary realized from a (converted) pure rvalue. Emit the expression
2056 // as a value, copy it into a temporary, and return an lvalue referring to
2058 llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp");
2059 EmitAnyExprToMem(E, V, E->getType().getQualifiers(), false);
2060 return MakeAddrLValue(V, E->getType());
2064 LValue LV = EmitLValue(E->getSubExpr());
2065 llvm::Value *V = LV.getAddress();
2066 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
2067 return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
2070 case CK_ConstructorConversion:
2071 case CK_UserDefinedConversion:
2072 case CK_AnyPointerToObjCPointerCast:
2073 return EmitLValue(E->getSubExpr());
2075 case CK_UncheckedDerivedToBase:
2076 case CK_DerivedToBase: {
2077 const RecordType *DerivedClassTy =
2078 E->getSubExpr()->getType()->getAs<RecordType>();
2079 CXXRecordDecl *DerivedClassDecl =
2080 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2082 LValue LV = EmitLValue(E->getSubExpr());
2083 llvm::Value *This = LV.getAddress();
2085 // Perform the derived-to-base conversion
2087 GetAddressOfBaseClass(This, DerivedClassDecl,
2088 E->path_begin(), E->path_end(),
2089 /*NullCheckValue=*/false);
2091 return MakeAddrLValue(Base, E->getType());
2094 return EmitAggExprToLValue(E);
2095 case CK_BaseToDerived: {
2096 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
2097 CXXRecordDecl *DerivedClassDecl =
2098 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2100 LValue LV = EmitLValue(E->getSubExpr());
2102 // Perform the base-to-derived conversion
2103 llvm::Value *Derived =
2104 GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
2105 E->path_begin(), E->path_end(),
2106 /*NullCheckValue=*/false);
2108 return MakeAddrLValue(Derived, E->getType());
2110 case CK_LValueBitCast: {
2111 // This must be a reinterpret_cast (or c-style equivalent).
2112 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
2114 LValue LV = EmitLValue(E->getSubExpr());
2115 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2116 ConvertType(CE->getTypeAsWritten()));
2117 return MakeAddrLValue(V, E->getType());
2119 case CK_ObjCObjectLValueCast: {
2120 LValue LV = EmitLValue(E->getSubExpr());
2121 QualType ToType = getContext().getLValueReferenceType(E->getType());
2122 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2123 ConvertType(ToType));
2124 return MakeAddrLValue(V, E->getType());
2128 llvm_unreachable("Unhandled lvalue cast kind?");
2131 LValue CodeGenFunction::EmitNullInitializationLValue(
2132 const CXXScalarValueInitExpr *E) {
2133 QualType Ty = E->getType();
2134 LValue LV = MakeAddrLValue(CreateMemTemp(Ty), Ty);
2135 EmitNullInitialization(LV.getAddress(), Ty);
2139 LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
2140 assert(e->isGLValue() || e->getType()->isRecordType());
2141 return getOpaqueLValueMapping(e);
2144 LValue CodeGenFunction::EmitMaterializeTemporaryExpr(
2145 const MaterializeTemporaryExpr *E) {
2146 RValue RV = EmitReferenceBindingToExpr(E->GetTemporaryExpr(),
2147 /*InitializedDecl=*/0);
2148 return MakeAddrLValue(RV.getScalarVal(), E->getType());
2152 //===--------------------------------------------------------------------===//
2153 // Expression Emission
2154 //===--------------------------------------------------------------------===//
2156 RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
2157 ReturnValueSlot ReturnValue) {
2158 if (CGDebugInfo *DI = getDebugInfo()) {
2159 DI->setLocation(E->getLocStart());
2160 DI->UpdateLineDirectiveRegion(Builder);
2161 DI->EmitStopPoint(Builder);
2164 // Builtins never have block type.
2165 if (E->getCallee()->getType()->isBlockPointerType())
2166 return EmitBlockCallExpr(E, ReturnValue);
2168 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
2169 return EmitCXXMemberCallExpr(CE, ReturnValue);
2171 const Decl *TargetDecl = 0;
2172 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
2173 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
2174 TargetDecl = DRE->getDecl();
2175 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
2176 if (unsigned builtinID = FD->getBuiltinID())
2177 return EmitBuiltinExpr(FD, builtinID, E);
2181 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
2182 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
2183 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
2185 if (const CXXPseudoDestructorExpr *PseudoDtor
2186 = dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
2187 QualType DestroyedType = PseudoDtor->getDestroyedType();
2188 if (getContext().getLangOptions().ObjCAutoRefCount &&
2189 DestroyedType->isObjCLifetimeType() &&
2190 (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong ||
2191 DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) {
2192 // Automatic Reference Counting:
2193 // If the pseudo-expression names a retainable object with weak or
2194 // strong lifetime, the object shall be released.
2195 Expr *BaseExpr = PseudoDtor->getBase();
2196 llvm::Value *BaseValue = NULL;
2197 Qualifiers BaseQuals;
2199 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
2200 if (PseudoDtor->isArrow()) {
2201 BaseValue = EmitScalarExpr(BaseExpr);
2202 const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>();
2203 BaseQuals = PTy->getPointeeType().getQualifiers();
2205 LValue BaseLV = EmitLValue(BaseExpr);
2206 BaseValue = BaseLV.getAddress();
2207 QualType BaseTy = BaseExpr->getType();
2208 BaseQuals = BaseTy.getQualifiers();
2211 switch (PseudoDtor->getDestroyedType().getObjCLifetime()) {
2212 case Qualifiers::OCL_None:
2213 case Qualifiers::OCL_ExplicitNone:
2214 case Qualifiers::OCL_Autoreleasing:
2217 case Qualifiers::OCL_Strong:
2218 EmitARCRelease(Builder.CreateLoad(BaseValue,
2219 PseudoDtor->getDestroyedType().isVolatileQualified()),
2223 case Qualifiers::OCL_Weak:
2224 EmitARCDestroyWeak(BaseValue);
2228 // C++ [expr.pseudo]p1:
2229 // The result shall only be used as the operand for the function call
2230 // operator (), and the result of such a call has type void. The only
2231 // effect is the evaluation of the postfix-expression before the dot or
2233 EmitScalarExpr(E->getCallee());
2236 return RValue::get(0);
2239 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
2240 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
2241 E->arg_begin(), E->arg_end(), TargetDecl);
2244 LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
2245 // Comma expressions just emit their LHS then their RHS as an l-value.
2246 if (E->getOpcode() == BO_Comma) {
2247 EmitIgnoredExpr(E->getLHS());
2248 EnsureInsertPoint();
2249 return EmitLValue(E->getRHS());
2252 if (E->getOpcode() == BO_PtrMemD ||
2253 E->getOpcode() == BO_PtrMemI)
2254 return EmitPointerToDataMemberBinaryExpr(E);
2256 assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
2258 // Note that in all of these cases, __block variables need the RHS
2259 // evaluated first just in case the variable gets moved by the RHS.
2261 if (!hasAggregateLLVMType(E->getType())) {
2262 switch (E->getLHS()->getType().getObjCLifetime()) {
2263 case Qualifiers::OCL_Strong:
2264 return EmitARCStoreStrong(E, /*ignored*/ false).first;
2266 case Qualifiers::OCL_Autoreleasing:
2267 return EmitARCStoreAutoreleasing(E).first;
2269 // No reason to do any of these differently.
2270 case Qualifiers::OCL_None:
2271 case Qualifiers::OCL_ExplicitNone:
2272 case Qualifiers::OCL_Weak:
2276 RValue RV = EmitAnyExpr(E->getRHS());
2277 LValue LV = EmitLValue(E->getLHS());
2278 EmitStoreThroughLValue(RV, LV);
2282 if (E->getType()->isAnyComplexType())
2283 return EmitComplexAssignmentLValue(E);
2285 return EmitAggExprToLValue(E);
2288 LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
2289 RValue RV = EmitCallExpr(E);
2292 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2294 assert(E->getCallReturnType()->isReferenceType() &&
2295 "Can't have a scalar return unless the return type is a "
2298 return MakeAddrLValue(RV.getScalarVal(), E->getType());
2301 LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
2302 // FIXME: This shouldn't require another copy.
2303 return EmitAggExprToLValue(E);
2306 LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
2307 assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
2308 && "binding l-value to type which needs a temporary");
2309 AggValueSlot Slot = CreateAggTemp(E->getType(), "tmp");
2310 EmitCXXConstructExpr(E, Slot);
2311 return MakeAddrLValue(Slot.getAddr(), E->getType());
2315 CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
2316 return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
2320 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
2321 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
2322 Slot.setLifetimeExternallyManaged();
2323 EmitAggExpr(E->getSubExpr(), Slot);
2324 EmitCXXTemporary(E->getTemporary(), Slot.getAddr());
2325 return MakeAddrLValue(Slot.getAddr(), E->getType());
2328 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
2329 RValue RV = EmitObjCMessageExpr(E);
2332 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2334 assert(E->getMethodDecl()->getResultType()->isReferenceType() &&
2335 "Can't have a scalar return unless the return type is a "
2338 return MakeAddrLValue(RV.getScalarVal(), E->getType());
2341 LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
2343 CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true);
2344 return MakeAddrLValue(V, E->getType());
2347 llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2348 const ObjCIvarDecl *Ivar) {
2349 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
2352 LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
2353 llvm::Value *BaseValue,
2354 const ObjCIvarDecl *Ivar,
2355 unsigned CVRQualifiers) {
2356 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
2357 Ivar, CVRQualifiers);
2360 LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
2361 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
2362 llvm::Value *BaseValue = 0;
2363 const Expr *BaseExpr = E->getBase();
2364 Qualifiers BaseQuals;
2367 BaseValue = EmitScalarExpr(BaseExpr);
2368 ObjectTy = BaseExpr->getType()->getPointeeType();
2369 BaseQuals = ObjectTy.getQualifiers();
2371 LValue BaseLV = EmitLValue(BaseExpr);
2372 // FIXME: this isn't right for bitfields.
2373 BaseValue = BaseLV.getAddress();
2374 ObjectTy = BaseExpr->getType();
2375 BaseQuals = ObjectTy.getQualifiers();
2379 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
2380 BaseQuals.getCVRQualifiers());
2381 setObjCGCLValueClass(getContext(), E, LV);
2385 LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
2386 // Can only get l-value for message expression returning aggregate type
2387 RValue RV = EmitAnyExprToTemp(E);
2388 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2391 RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
2392 ReturnValueSlot ReturnValue,
2393 CallExpr::const_arg_iterator ArgBeg,
2394 CallExpr::const_arg_iterator ArgEnd,
2395 const Decl *TargetDecl) {
2396 // Get the actual function type. The callee type will always be a pointer to
2397 // function type or a block pointer type.
2398 assert(CalleeType->isFunctionPointerType() &&
2399 "Call must have function pointer type!");
2401 CalleeType = getContext().getCanonicalType(CalleeType);
2403 const FunctionType *FnType
2404 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
2407 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
2409 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
2410 Callee, ReturnValue, Args, TargetDecl);
2413 LValue CodeGenFunction::
2414 EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
2416 if (E->getOpcode() == BO_PtrMemI)
2417 BaseV = EmitScalarExpr(E->getLHS());
2419 BaseV = EmitLValue(E->getLHS()).getAddress();
2421 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
2423 const MemberPointerType *MPT
2424 = E->getRHS()->getType()->getAs<MemberPointerType>();
2427 CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT);
2429 return MakeAddrLValue(AddV, MPT->getPointeeType());