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