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