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