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