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