CGExprConstant.cpp revision 24fe798fffc1748d8bce1321af42981c3719cb85
1//===--- CGExprConstant.cpp - Emit LLVM Code from Constant 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 Constant Expr nodes as LLVM code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CodeGenFunction.h" 15#include "CodeGenModule.h" 16#include "CGCXXABI.h" 17#include "CGObjCRuntime.h" 18#include "CGRecordLayout.h" 19#include "clang/AST/APValue.h" 20#include "clang/AST/ASTContext.h" 21#include "clang/AST/RecordLayout.h" 22#include "clang/AST/StmtVisitor.h" 23#include "clang/Basic/Builtins.h" 24#include "llvm/Constants.h" 25#include "llvm/Function.h" 26#include "llvm/GlobalVariable.h" 27#include "llvm/Target/TargetData.h" 28using namespace clang; 29using namespace CodeGen; 30 31//===----------------------------------------------------------------------===// 32// ConstStructBuilder 33//===----------------------------------------------------------------------===// 34 35namespace { 36class ConstStructBuilder { 37 CodeGenModule &CGM; 38 CodeGenFunction *CGF; 39 40 bool Packed; 41 CharUnits NextFieldOffsetInChars; 42 CharUnits LLVMStructAlignment; 43 SmallVector<llvm::Constant *, 32> Elements; 44public: 45 static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, 46 InitListExpr *ILE); 47 static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, 48 const APValue &Value, QualType ValTy); 49 50private: 51 ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF) 52 : CGM(CGM), CGF(CGF), Packed(false), 53 NextFieldOffsetInChars(CharUnits::Zero()), 54 LLVMStructAlignment(CharUnits::One()) { } 55 56 void AppendField(const FieldDecl *Field, uint64_t FieldOffset, 57 llvm::Constant *InitExpr); 58 59 void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset, 60 llvm::ConstantInt *InitExpr); 61 62 void AppendPadding(CharUnits PadSize); 63 64 void AppendTailPadding(CharUnits RecordSize); 65 66 void ConvertStructToPacked(); 67 68 bool Build(InitListExpr *ILE); 69 void Build(const APValue &Val, QualType ValTy); 70 llvm::Constant *Finalize(QualType Ty); 71 72 CharUnits getAlignment(const llvm::Constant *C) const { 73 if (Packed) return CharUnits::One(); 74 return CharUnits::fromQuantity( 75 CGM.getTargetData().getABITypeAlignment(C->getType())); 76 } 77 78 CharUnits getSizeInChars(const llvm::Constant *C) const { 79 return CharUnits::fromQuantity( 80 CGM.getTargetData().getTypeAllocSize(C->getType())); 81 } 82}; 83 84void ConstStructBuilder:: 85AppendField(const FieldDecl *Field, uint64_t FieldOffset, 86 llvm::Constant *InitCst) { 87 88 const ASTContext &Context = CGM.getContext(); 89 90 CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset); 91 92 assert(NextFieldOffsetInChars <= FieldOffsetInChars 93 && "Field offset mismatch!"); 94 95 CharUnits FieldAlignment = getAlignment(InitCst); 96 97 // Round up the field offset to the alignment of the field type. 98 CharUnits AlignedNextFieldOffsetInChars = 99 NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment); 100 101 if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) { 102 assert(!Packed && "Alignment is wrong even with a packed struct!"); 103 104 // Convert the struct to a packed struct. 105 ConvertStructToPacked(); 106 107 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; 108 } 109 110 if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) { 111 // We need to append padding. 112 AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars); 113 114 assert(NextFieldOffsetInChars == FieldOffsetInChars && 115 "Did not add enough padding!"); 116 117 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; 118 } 119 120 // Add the field. 121 Elements.push_back(InitCst); 122 NextFieldOffsetInChars = AlignedNextFieldOffsetInChars + 123 getSizeInChars(InitCst); 124 125 if (Packed) 126 assert(LLVMStructAlignment == CharUnits::One() && 127 "Packed struct not byte-aligned!"); 128 else 129 LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment); 130} 131 132void ConstStructBuilder::AppendBitField(const FieldDecl *Field, 133 uint64_t FieldOffset, 134 llvm::ConstantInt *CI) { 135 const ASTContext &Context = CGM.getContext(); 136 const uint64_t CharWidth = Context.getCharWidth(); 137 uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); 138 if (FieldOffset > NextFieldOffsetInBits) { 139 // We need to add padding. 140 CharUnits PadSize = Context.toCharUnitsFromBits( 141 llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits, 142 Context.getTargetInfo().getCharAlign())); 143 144 AppendPadding(PadSize); 145 } 146 147 uint64_t FieldSize = Field->getBitWidthValue(Context); 148 149 llvm::APInt FieldValue = CI->getValue(); 150 151 // Promote the size of FieldValue if necessary 152 // FIXME: This should never occur, but currently it can because initializer 153 // constants are cast to bool, and because clang is not enforcing bitfield 154 // width limits. 155 if (FieldSize > FieldValue.getBitWidth()) 156 FieldValue = FieldValue.zext(FieldSize); 157 158 // Truncate the size of FieldValue to the bit field size. 159 if (FieldSize < FieldValue.getBitWidth()) 160 FieldValue = FieldValue.trunc(FieldSize); 161 162 NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); 163 if (FieldOffset < NextFieldOffsetInBits) { 164 // Either part of the field or the entire field can go into the previous 165 // byte. 166 assert(!Elements.empty() && "Elements can't be empty!"); 167 168 unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset; 169 170 bool FitsCompletelyInPreviousByte = 171 BitsInPreviousByte >= FieldValue.getBitWidth(); 172 173 llvm::APInt Tmp = FieldValue; 174 175 if (!FitsCompletelyInPreviousByte) { 176 unsigned NewFieldWidth = FieldSize - BitsInPreviousByte; 177 178 if (CGM.getTargetData().isBigEndian()) { 179 Tmp = Tmp.lshr(NewFieldWidth); 180 Tmp = Tmp.trunc(BitsInPreviousByte); 181 182 // We want the remaining high bits. 183 FieldValue = FieldValue.trunc(NewFieldWidth); 184 } else { 185 Tmp = Tmp.trunc(BitsInPreviousByte); 186 187 // We want the remaining low bits. 188 FieldValue = FieldValue.lshr(BitsInPreviousByte); 189 FieldValue = FieldValue.trunc(NewFieldWidth); 190 } 191 } 192 193 Tmp = Tmp.zext(CharWidth); 194 if (CGM.getTargetData().isBigEndian()) { 195 if (FitsCompletelyInPreviousByte) 196 Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth()); 197 } else { 198 Tmp = Tmp.shl(CharWidth - BitsInPreviousByte); 199 } 200 201 // 'or' in the bits that go into the previous byte. 202 llvm::Value *LastElt = Elements.back(); 203 if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt)) 204 Tmp |= Val->getValue(); 205 else { 206 assert(isa<llvm::UndefValue>(LastElt)); 207 // If there is an undef field that we're adding to, it can either be a 208 // scalar undef (in which case, we just replace it with our field) or it 209 // is an array. If it is an array, we have to pull one byte off the 210 // array so that the other undef bytes stay around. 211 if (!isa<llvm::IntegerType>(LastElt->getType())) { 212 // The undef padding will be a multibyte array, create a new smaller 213 // padding and then an hole for our i8 to get plopped into. 214 assert(isa<llvm::ArrayType>(LastElt->getType()) && 215 "Expected array padding of undefs"); 216 llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType()); 217 assert(AT->getElementType()->isIntegerTy(CharWidth) && 218 AT->getNumElements() != 0 && 219 "Expected non-empty array padding of undefs"); 220 221 // Remove the padding array. 222 NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements()); 223 Elements.pop_back(); 224 225 // Add the padding back in two chunks. 226 AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1)); 227 AppendPadding(CharUnits::One()); 228 assert(isa<llvm::UndefValue>(Elements.back()) && 229 Elements.back()->getType()->isIntegerTy(CharWidth) && 230 "Padding addition didn't work right"); 231 } 232 } 233 234 Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp); 235 236 if (FitsCompletelyInPreviousByte) 237 return; 238 } 239 240 while (FieldValue.getBitWidth() > CharWidth) { 241 llvm::APInt Tmp; 242 243 if (CGM.getTargetData().isBigEndian()) { 244 // We want the high bits. 245 Tmp = 246 FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth); 247 } else { 248 // We want the low bits. 249 Tmp = FieldValue.trunc(CharWidth); 250 251 FieldValue = FieldValue.lshr(CharWidth); 252 } 253 254 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp)); 255 ++NextFieldOffsetInChars; 256 257 FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth); 258 } 259 260 assert(FieldValue.getBitWidth() > 0 && 261 "Should have at least one bit left!"); 262 assert(FieldValue.getBitWidth() <= CharWidth && 263 "Should not have more than a byte left!"); 264 265 if (FieldValue.getBitWidth() < CharWidth) { 266 if (CGM.getTargetData().isBigEndian()) { 267 unsigned BitWidth = FieldValue.getBitWidth(); 268 269 FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth); 270 } else 271 FieldValue = FieldValue.zext(CharWidth); 272 } 273 274 // Append the last element. 275 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), 276 FieldValue)); 277 ++NextFieldOffsetInChars; 278} 279 280void ConstStructBuilder::AppendPadding(CharUnits PadSize) { 281 if (PadSize.isZero()) 282 return; 283 284 llvm::Type *Ty = CGM.Int8Ty; 285 if (PadSize > CharUnits::One()) 286 Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity()); 287 288 llvm::Constant *C = llvm::UndefValue::get(Ty); 289 Elements.push_back(C); 290 assert(getAlignment(C) == CharUnits::One() && 291 "Padding must have 1 byte alignment!"); 292 293 NextFieldOffsetInChars += getSizeInChars(C); 294} 295 296void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) { 297 assert(NextFieldOffsetInChars <= RecordSize && 298 "Size mismatch!"); 299 300 AppendPadding(RecordSize - NextFieldOffsetInChars); 301} 302 303void ConstStructBuilder::ConvertStructToPacked() { 304 SmallVector<llvm::Constant *, 16> PackedElements; 305 CharUnits ElementOffsetInChars = CharUnits::Zero(); 306 307 for (unsigned i = 0, e = Elements.size(); i != e; ++i) { 308 llvm::Constant *C = Elements[i]; 309 310 CharUnits ElementAlign = CharUnits::fromQuantity( 311 CGM.getTargetData().getABITypeAlignment(C->getType())); 312 CharUnits AlignedElementOffsetInChars = 313 ElementOffsetInChars.RoundUpToAlignment(ElementAlign); 314 315 if (AlignedElementOffsetInChars > ElementOffsetInChars) { 316 // We need some padding. 317 CharUnits NumChars = 318 AlignedElementOffsetInChars - ElementOffsetInChars; 319 320 llvm::Type *Ty = CGM.Int8Ty; 321 if (NumChars > CharUnits::One()) 322 Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity()); 323 324 llvm::Constant *Padding = llvm::UndefValue::get(Ty); 325 PackedElements.push_back(Padding); 326 ElementOffsetInChars += getSizeInChars(Padding); 327 } 328 329 PackedElements.push_back(C); 330 ElementOffsetInChars += getSizeInChars(C); 331 } 332 333 assert(ElementOffsetInChars == NextFieldOffsetInChars && 334 "Packing the struct changed its size!"); 335 336 Elements.swap(PackedElements); 337 LLVMStructAlignment = CharUnits::One(); 338 Packed = true; 339} 340 341bool ConstStructBuilder::Build(InitListExpr *ILE) { 342 if (ILE->initializesStdInitializerList()) { 343 CGM.ErrorUnsupported(ILE, "global std::initializer_list"); 344 return false; 345 } 346 347 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl(); 348 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 349 350 unsigned FieldNo = 0; 351 unsigned ElementNo = 0; 352 const FieldDecl *LastFD = 0; 353 bool IsMsStruct = RD->hasAttr<MsStructAttr>(); 354 355 for (RecordDecl::field_iterator Field = RD->field_begin(), 356 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 357 if (IsMsStruct) { 358 // Zero-length bitfields following non-bitfield members are 359 // ignored: 360 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) { 361 --FieldNo; 362 continue; 363 } 364 LastFD = (*Field); 365 } 366 367 // If this is a union, skip all the fields that aren't being initialized. 368 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field) 369 continue; 370 371 // Don't emit anonymous bitfields, they just affect layout. 372 if (Field->isUnnamedBitfield()) { 373 LastFD = (*Field); 374 continue; 375 } 376 377 // Get the initializer. A struct can include fields without initializers, 378 // we just use explicit null values for them. 379 llvm::Constant *EltInit; 380 if (ElementNo < ILE->getNumInits()) 381 EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++), 382 Field->getType(), CGF); 383 else 384 EltInit = CGM.EmitNullConstant(Field->getType()); 385 386 if (!EltInit) 387 return false; 388 389 if (!Field->isBitField()) { 390 // Handle non-bitfield members. 391 AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit); 392 } else { 393 // Otherwise we have a bitfield. 394 AppendBitField(*Field, Layout.getFieldOffset(FieldNo), 395 cast<llvm::ConstantInt>(EltInit)); 396 } 397 } 398 399 return true; 400} 401 402void ConstStructBuilder::Build(const APValue &Val, QualType ValTy) { 403 RecordDecl *RD = ValTy->getAs<RecordType>()->getDecl(); 404 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 405 406 if (CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { 407 unsigned BaseNo = 0; 408 for (CXXRecordDecl::base_class_iterator Base = CD->bases_begin(), 409 BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) { 410 // Build the base class subobject at the appropriately-offset location 411 // within this object. 412 const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl(); 413 CharUnits BaseOffset = Layout.getBaseClassOffset(BD); 414 NextFieldOffsetInChars -= BaseOffset; 415 416 Build(Val.getStructBase(BaseNo), Base->getType()); 417 418 NextFieldOffsetInChars += BaseOffset; 419 } 420 } 421 422 unsigned FieldNo = 0; 423 const FieldDecl *LastFD = 0; 424 bool IsMsStruct = RD->hasAttr<MsStructAttr>(); 425 426 for (RecordDecl::field_iterator Field = RD->field_begin(), 427 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 428 if (IsMsStruct) { 429 // Zero-length bitfields following non-bitfield members are 430 // ignored: 431 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) { 432 --FieldNo; 433 continue; 434 } 435 LastFD = (*Field); 436 } 437 438 // If this is a union, skip all the fields that aren't being initialized. 439 if (RD->isUnion() && Val.getUnionField() != *Field) 440 continue; 441 442 // Don't emit anonymous bitfields, they just affect layout. 443 if (Field->isUnnamedBitfield()) { 444 LastFD = (*Field); 445 continue; 446 } 447 448 // Emit the value of the initializer. 449 const APValue &FieldValue = 450 RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo); 451 llvm::Constant *EltInit = 452 CGM.EmitConstantValue(FieldValue, Field->getType(), CGF); 453 assert(EltInit && "EmitConstantValue can't fail"); 454 455 if (!Field->isBitField()) { 456 // Handle non-bitfield members. 457 AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit); 458 } else { 459 // Otherwise we have a bitfield. 460 AppendBitField(*Field, Layout.getFieldOffset(FieldNo), 461 cast<llvm::ConstantInt>(EltInit)); 462 } 463 } 464} 465 466llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) { 467 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); 468 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 469 470 CharUnits LayoutSizeInChars = Layout.getSize(); 471 472 if (NextFieldOffsetInChars > LayoutSizeInChars) { 473 // If the struct is bigger than the size of the record type, 474 // we must have a flexible array member at the end. 475 assert(RD->hasFlexibleArrayMember() && 476 "Must have flexible array member if struct is bigger than type!"); 477 478 // No tail padding is necessary. 479 } else { 480 // Append tail padding if necessary. 481 AppendTailPadding(LayoutSizeInChars); 482 483 CharUnits LLVMSizeInChars = 484 NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment); 485 486 // Check if we need to convert the struct to a packed struct. 487 if (NextFieldOffsetInChars <= LayoutSizeInChars && 488 LLVMSizeInChars > LayoutSizeInChars) { 489 assert(!Packed && "Size mismatch!"); 490 491 ConvertStructToPacked(); 492 assert(NextFieldOffsetInChars <= LayoutSizeInChars && 493 "Converting to packed did not help!"); 494 } 495 496 assert(LayoutSizeInChars == NextFieldOffsetInChars && 497 "Tail padding mismatch!"); 498 } 499 500 // Pick the type to use. If the type is layout identical to the ConvertType 501 // type then use it, otherwise use whatever the builder produced for us. 502 llvm::StructType *STy = 503 llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(), 504 Elements, Packed); 505 llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty); 506 if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) { 507 if (ValSTy->isLayoutIdentical(STy)) 508 STy = ValSTy; 509 } 510 511 llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements); 512 513 assert(NextFieldOffsetInChars.RoundUpToAlignment(getAlignment(Result)) == 514 getSizeInChars(Result) && "Size mismatch!"); 515 516 return Result; 517} 518 519llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM, 520 CodeGenFunction *CGF, 521 InitListExpr *ILE) { 522 ConstStructBuilder Builder(CGM, CGF); 523 524 if (!Builder.Build(ILE)) 525 return 0; 526 527 return Builder.Finalize(ILE->getType()); 528} 529 530llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM, 531 CodeGenFunction *CGF, 532 const APValue &Val, 533 QualType ValTy) { 534 ConstStructBuilder Builder(CGM, CGF); 535 Builder.Build(Val, ValTy); 536 return Builder.Finalize(ValTy); 537} 538 539 540//===----------------------------------------------------------------------===// 541// ConstExprEmitter 542//===----------------------------------------------------------------------===// 543 544/// This class only needs to handle two cases: 545/// 1) Literals (this is used by APValue emission to emit literals). 546/// 2) Arrays, structs and unions (outside C++11 mode, we don't currently 547/// constant fold these types). 548class ConstExprEmitter : 549 public StmtVisitor<ConstExprEmitter, llvm::Constant*> { 550 CodeGenModule &CGM; 551 CodeGenFunction *CGF; 552 llvm::LLVMContext &VMContext; 553public: 554 ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) 555 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) { 556 } 557 558 //===--------------------------------------------------------------------===// 559 // Visitor Methods 560 //===--------------------------------------------------------------------===// 561 562 llvm::Constant *VisitStmt(Stmt *S) { 563 return 0; 564 } 565 566 llvm::Constant *VisitParenExpr(ParenExpr *PE) { 567 return Visit(PE->getSubExpr()); 568 } 569 570 llvm::Constant * 571 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) { 572 return Visit(PE->getReplacement()); 573 } 574 575 llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 576 return Visit(GE->getResultExpr()); 577 } 578 579 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 580 return Visit(E->getInitializer()); 581 } 582 583 llvm::Constant *VisitCastExpr(CastExpr* E) { 584 Expr *subExpr = E->getSubExpr(); 585 llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF); 586 if (!C) return 0; 587 588 llvm::Type *destType = ConvertType(E->getType()); 589 590 switch (E->getCastKind()) { 591 case CK_ToUnion: { 592 // GCC cast to union extension 593 assert(E->getType()->isUnionType() && 594 "Destination type is not union type!"); 595 596 // Build a struct with the union sub-element as the first member, 597 // and padded to the appropriate size 598 SmallVector<llvm::Constant*, 2> Elts; 599 SmallVector<llvm::Type*, 2> Types; 600 Elts.push_back(C); 601 Types.push_back(C->getType()); 602 unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType()); 603 unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(destType); 604 605 assert(CurSize <= TotalSize && "Union size mismatch!"); 606 if (unsigned NumPadBytes = TotalSize - CurSize) { 607 llvm::Type *Ty = CGM.Int8Ty; 608 if (NumPadBytes > 1) 609 Ty = llvm::ArrayType::get(Ty, NumPadBytes); 610 611 Elts.push_back(llvm::UndefValue::get(Ty)); 612 Types.push_back(Ty); 613 } 614 615 llvm::StructType* STy = 616 llvm::StructType::get(C->getType()->getContext(), Types, false); 617 return llvm::ConstantStruct::get(STy, Elts); 618 } 619 620 case CK_LValueToRValue: 621 case CK_AtomicToNonAtomic: 622 case CK_NonAtomicToAtomic: 623 case CK_NoOp: 624 return C; 625 626 case CK_Dependent: llvm_unreachable("saw dependent cast!"); 627 628 case CK_ReinterpretMemberPointer: 629 case CK_DerivedToBaseMemberPointer: 630 case CK_BaseToDerivedMemberPointer: 631 return CGM.getCXXABI().EmitMemberPointerConversion(E, C); 632 633 // These will never be supported. 634 case CK_ObjCObjectLValueCast: 635 case CK_ARCProduceObject: 636 case CK_ARCConsumeObject: 637 case CK_ARCReclaimReturnedObject: 638 case CK_ARCExtendBlockObject: 639 return 0; 640 641 // These don't need to be handled here because Evaluate knows how to 642 // evaluate them in the cases where they can be folded. 643 case CK_BitCast: 644 case CK_ToVoid: 645 case CK_Dynamic: 646 case CK_LValueBitCast: 647 case CK_NullToMemberPointer: 648 case CK_UserDefinedConversion: 649 case CK_ConstructorConversion: 650 case CK_CPointerToObjCPointerCast: 651 case CK_BlockPointerToObjCPointerCast: 652 case CK_AnyPointerToBlockPointerCast: 653 case CK_ArrayToPointerDecay: 654 case CK_FunctionToPointerDecay: 655 case CK_BaseToDerived: 656 case CK_DerivedToBase: 657 case CK_UncheckedDerivedToBase: 658 case CK_MemberPointerToBoolean: 659 case CK_VectorSplat: 660 case CK_FloatingRealToComplex: 661 case CK_FloatingComplexToReal: 662 case CK_FloatingComplexToBoolean: 663 case CK_FloatingComplexCast: 664 case CK_FloatingComplexToIntegralComplex: 665 case CK_IntegralRealToComplex: 666 case CK_IntegralComplexToReal: 667 case CK_IntegralComplexToBoolean: 668 case CK_IntegralComplexCast: 669 case CK_IntegralComplexToFloatingComplex: 670 case CK_PointerToIntegral: 671 case CK_PointerToBoolean: 672 case CK_NullToPointer: 673 case CK_IntegralCast: 674 case CK_IntegralToPointer: 675 case CK_IntegralToBoolean: 676 case CK_IntegralToFloating: 677 case CK_FloatingToIntegral: 678 case CK_FloatingToBoolean: 679 case CK_FloatingCast: 680 return 0; 681 } 682 llvm_unreachable("Invalid CastKind"); 683 } 684 685 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 686 return Visit(DAE->getExpr()); 687 } 688 689 llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { 690 return Visit(E->GetTemporaryExpr()); 691 } 692 693 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { 694 unsigned NumInitElements = ILE->getNumInits(); 695 if (NumInitElements == 1 && ILE->getType() == ILE->getInit(0)->getType() && 696 (isa<StringLiteral>(ILE->getInit(0)) || 697 isa<ObjCEncodeExpr>(ILE->getInit(0)))) 698 return Visit(ILE->getInit(0)); 699 700 llvm::ArrayType *AType = 701 cast<llvm::ArrayType>(ConvertType(ILE->getType())); 702 llvm::Type *ElemTy = AType->getElementType(); 703 unsigned NumElements = AType->getNumElements(); 704 705 // Initialising an array requires us to automatically 706 // initialise any elements that have not been initialised explicitly 707 unsigned NumInitableElts = std::min(NumInitElements, NumElements); 708 709 // Copy initializer elements. 710 std::vector<llvm::Constant*> Elts; 711 Elts.reserve(NumInitableElts + NumElements); 712 713 bool RewriteType = false; 714 for (unsigned i = 0; i < NumInitableElts; ++i) { 715 Expr *Init = ILE->getInit(i); 716 llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); 717 if (!C) 718 return 0; 719 RewriteType |= (C->getType() != ElemTy); 720 Elts.push_back(C); 721 } 722 723 // Initialize remaining array elements. 724 // FIXME: This doesn't handle member pointers correctly! 725 llvm::Constant *fillC; 726 if (Expr *filler = ILE->getArrayFiller()) 727 fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF); 728 else 729 fillC = llvm::Constant::getNullValue(ElemTy); 730 if (!fillC) 731 return 0; 732 RewriteType |= (fillC->getType() != ElemTy); 733 Elts.resize(NumElements, fillC); 734 735 if (RewriteType) { 736 // FIXME: Try to avoid packing the array 737 std::vector<llvm::Type*> Types; 738 Types.reserve(NumInitableElts + NumElements); 739 for (unsigned i = 0, e = Elts.size(); i < e; ++i) 740 Types.push_back(Elts[i]->getType()); 741 llvm::StructType *SType = llvm::StructType::get(AType->getContext(), 742 Types, true); 743 return llvm::ConstantStruct::get(SType, Elts); 744 } 745 746 return llvm::ConstantArray::get(AType, Elts); 747 } 748 749 llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { 750 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); 751 } 752 753 llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { 754 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); 755 } 756 757 llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) { 758 return CGM.EmitNullConstant(E->getType()); 759 } 760 761 llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { 762 if (ILE->getType()->isArrayType()) 763 return EmitArrayInitialization(ILE); 764 765 if (ILE->getType()->isRecordType()) 766 return EmitStructInitialization(ILE); 767 768 if (ILE->getType()->isUnionType()) 769 return EmitUnionInitialization(ILE); 770 771 return 0; 772 } 773 774 llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) { 775 if (!E->getConstructor()->isTrivial()) 776 return 0; 777 778 QualType Ty = E->getType(); 779 780 // FIXME: We should not have to call getBaseElementType here. 781 const RecordType *RT = 782 CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>(); 783 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 784 785 // If the class doesn't have a trivial destructor, we can't emit it as a 786 // constant expr. 787 if (!RD->hasTrivialDestructor()) 788 return 0; 789 790 // Only copy and default constructors can be trivial. 791 792 793 if (E->getNumArgs()) { 794 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); 795 assert(E->getConstructor()->isCopyOrMoveConstructor() && 796 "trivial ctor has argument but isn't a copy/move ctor"); 797 798 Expr *Arg = E->getArg(0); 799 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && 800 "argument to copy ctor is of wrong type"); 801 802 return Visit(Arg); 803 } 804 805 return CGM.EmitNullConstant(Ty); 806 } 807 808 llvm::Constant *VisitStringLiteral(StringLiteral *E) { 809 return CGM.GetConstantArrayFromStringLiteral(E); 810 } 811 812 llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { 813 // This must be an @encode initializing an array in a static initializer. 814 // Don't emit it as the address of the string, emit the string data itself 815 // as an inline array. 816 std::string Str; 817 CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); 818 const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); 819 820 // Resize the string to the right size, adding zeros at the end, or 821 // truncating as needed. 822 Str.resize(CAT->getSize().getZExtValue(), '\0'); 823 return llvm::ConstantDataArray::getString(VMContext, Str, false); 824 } 825 826 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { 827 return Visit(E->getSubExpr()); 828 } 829 830 // Utility methods 831 llvm::Type *ConvertType(QualType T) { 832 return CGM.getTypes().ConvertType(T); 833 } 834 835public: 836 llvm::Constant *EmitLValue(APValue::LValueBase LVBase) { 837 if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) { 838 if (Decl->hasAttr<WeakRefAttr>()) 839 return CGM.GetWeakRefReference(Decl); 840 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) 841 return CGM.GetAddrOfFunction(FD); 842 if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { 843 // We can never refer to a variable with local storage. 844 if (!VD->hasLocalStorage()) { 845 if (VD->isFileVarDecl() || VD->hasExternalStorage()) 846 return CGM.GetAddrOfGlobalVar(VD); 847 else if (VD->isLocalVarDecl()) { 848 assert(CGF && "Can't access static local vars without CGF"); 849 return CGF->GetAddrOfStaticLocalVar(VD); 850 } 851 } 852 } 853 return 0; 854 } 855 856 Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>()); 857 switch (E->getStmtClass()) { 858 default: break; 859 case Expr::CompoundLiteralExprClass: { 860 // Note that due to the nature of compound literals, this is guaranteed 861 // to be the only use of the variable, so we just generate it here. 862 CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); 863 llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(), 864 CLE->getType(), CGF); 865 // FIXME: "Leaked" on failure. 866 if (C) 867 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), 868 E->getType().isConstant(CGM.getContext()), 869 llvm::GlobalValue::InternalLinkage, 870 C, ".compoundliteral", 0, false, 871 CGM.getContext().getTargetAddressSpace(E->getType())); 872 return C; 873 } 874 case Expr::StringLiteralClass: 875 return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); 876 case Expr::ObjCEncodeExprClass: 877 return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); 878 case Expr::ObjCStringLiteralClass: { 879 ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); 880 llvm::Constant *C = 881 CGM.getObjCRuntime().GenerateConstantString(SL->getString()); 882 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 883 } 884 case Expr::PredefinedExprClass: { 885 unsigned Type = cast<PredefinedExpr>(E)->getIdentType(); 886 if (CGF) { 887 LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E)); 888 return cast<llvm::Constant>(Res.getAddress()); 889 } else if (Type == PredefinedExpr::PrettyFunction) { 890 return CGM.GetAddrOfConstantCString("top level", ".tmp"); 891 } 892 893 return CGM.GetAddrOfConstantCString("", ".tmp"); 894 } 895 case Expr::AddrLabelExprClass: { 896 assert(CGF && "Invalid address of label expression outside function."); 897 llvm::Constant *Ptr = 898 CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel()); 899 return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType())); 900 } 901 case Expr::CallExprClass: { 902 CallExpr* CE = cast<CallExpr>(E); 903 unsigned builtin = CE->isBuiltinCall(); 904 if (builtin != 905 Builtin::BI__builtin___CFStringMakeConstantString && 906 builtin != 907 Builtin::BI__builtin___NSStringMakeConstantString) 908 break; 909 const Expr *Arg = CE->getArg(0)->IgnoreParenCasts(); 910 const StringLiteral *Literal = cast<StringLiteral>(Arg); 911 if (builtin == 912 Builtin::BI__builtin___NSStringMakeConstantString) { 913 return CGM.getObjCRuntime().GenerateConstantString(Literal); 914 } 915 // FIXME: need to deal with UCN conversion issues. 916 return CGM.GetAddrOfConstantCFString(Literal); 917 } 918 case Expr::BlockExprClass: { 919 std::string FunctionName; 920 if (CGF) 921 FunctionName = CGF->CurFn->getName(); 922 else 923 FunctionName = "global"; 924 925 return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str()); 926 } 927 case Expr::CXXTypeidExprClass: { 928 CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E); 929 QualType T; 930 if (Typeid->isTypeOperand()) 931 T = Typeid->getTypeOperand(); 932 else 933 T = Typeid->getExprOperand()->getType(); 934 return CGM.GetAddrOfRTTIDescriptor(T); 935 } 936 } 937 938 return 0; 939 } 940}; 941 942} // end anonymous namespace. 943 944llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D, 945 CodeGenFunction *CGF) { 946 if (const APValue *Value = D.evaluateValue()) 947 return EmitConstantValue(*Value, D.getType(), CGF); 948 949 // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a 950 // reference is a constant expression, and the reference binds to a temporary, 951 // then constant initialization is performed. ConstExprEmitter will 952 // incorrectly emit a prvalue constant in this case, and the calling code 953 // interprets that as the (pointer) value of the reference, rather than the 954 // desired value of the referee. 955 if (D.getType()->isReferenceType()) 956 return 0; 957 958 const Expr *E = D.getInit(); 959 assert(E && "No initializer to emit"); 960 961 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); 962 if (C && C->getType()->isIntegerTy(1)) { 963 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); 964 C = llvm::ConstantExpr::getZExt(C, BoolTy); 965 } 966 return C; 967} 968 969llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, 970 QualType DestType, 971 CodeGenFunction *CGF) { 972 Expr::EvalResult Result; 973 974 bool Success = false; 975 976 if (DestType->isReferenceType()) 977 Success = E->EvaluateAsLValue(Result, Context); 978 else 979 Success = E->EvaluateAsRValue(Result, Context); 980 981 if (Success && !Result.HasSideEffects) 982 return EmitConstantValue(Result.Val, DestType, CGF); 983 984 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); 985 if (C && C->getType()->isIntegerTy(1)) { 986 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); 987 C = llvm::ConstantExpr::getZExt(C, BoolTy); 988 } 989 return C; 990} 991 992llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value, 993 QualType DestType, 994 CodeGenFunction *CGF) { 995 switch (Value.getKind()) { 996 case APValue::Uninitialized: 997 llvm_unreachable("Constant expressions should be initialized."); 998 case APValue::LValue: { 999 llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType); 1000 llvm::Constant *Offset = 1001 llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity()); 1002 1003 llvm::Constant *C; 1004 if (APValue::LValueBase LVBase = Value.getLValueBase()) { 1005 // An array can be represented as an lvalue referring to the base. 1006 if (isa<llvm::ArrayType>(DestTy)) { 1007 assert(Offset->isNullValue() && "offset on array initializer"); 1008 return ConstExprEmitter(*this, CGF).Visit( 1009 const_cast<Expr*>(LVBase.get<const Expr*>())); 1010 } 1011 1012 C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase); 1013 1014 // Apply offset if necessary. 1015 if (!Offset->isNullValue()) { 1016 llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Int8PtrTy); 1017 Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset); 1018 C = llvm::ConstantExpr::getBitCast(Casted, C->getType()); 1019 } 1020 1021 // Convert to the appropriate type; this could be an lvalue for 1022 // an integer. 1023 if (isa<llvm::PointerType>(DestTy)) 1024 return llvm::ConstantExpr::getBitCast(C, DestTy); 1025 1026 return llvm::ConstantExpr::getPtrToInt(C, DestTy); 1027 } else { 1028 C = Offset; 1029 1030 // Convert to the appropriate type; this could be an lvalue for 1031 // an integer. 1032 if (isa<llvm::PointerType>(DestTy)) 1033 return llvm::ConstantExpr::getIntToPtr(C, DestTy); 1034 1035 // If the types don't match this should only be a truncate. 1036 if (C->getType() != DestTy) 1037 return llvm::ConstantExpr::getTrunc(C, DestTy); 1038 1039 return C; 1040 } 1041 } 1042 case APValue::Int: { 1043 llvm::Constant *C = llvm::ConstantInt::get(VMContext, 1044 Value.getInt()); 1045 1046 if (C->getType()->isIntegerTy(1)) { 1047 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType); 1048 C = llvm::ConstantExpr::getZExt(C, BoolTy); 1049 } 1050 return C; 1051 } 1052 case APValue::ComplexInt: { 1053 llvm::Constant *Complex[2]; 1054 1055 Complex[0] = llvm::ConstantInt::get(VMContext, 1056 Value.getComplexIntReal()); 1057 Complex[1] = llvm::ConstantInt::get(VMContext, 1058 Value.getComplexIntImag()); 1059 1060 // FIXME: the target may want to specify that this is packed. 1061 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), 1062 Complex[1]->getType(), 1063 NULL); 1064 return llvm::ConstantStruct::get(STy, Complex); 1065 } 1066 case APValue::Float: { 1067 const llvm::APFloat &Init = Value.getFloat(); 1068 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf) 1069 return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt()); 1070 else 1071 return llvm::ConstantFP::get(VMContext, Init); 1072 } 1073 case APValue::ComplexFloat: { 1074 llvm::Constant *Complex[2]; 1075 1076 Complex[0] = llvm::ConstantFP::get(VMContext, 1077 Value.getComplexFloatReal()); 1078 Complex[1] = llvm::ConstantFP::get(VMContext, 1079 Value.getComplexFloatImag()); 1080 1081 // FIXME: the target may want to specify that this is packed. 1082 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), 1083 Complex[1]->getType(), 1084 NULL); 1085 return llvm::ConstantStruct::get(STy, Complex); 1086 } 1087 case APValue::Vector: { 1088 SmallVector<llvm::Constant *, 4> Inits; 1089 unsigned NumElts = Value.getVectorLength(); 1090 1091 for (unsigned i = 0; i != NumElts; ++i) { 1092 const APValue &Elt = Value.getVectorElt(i); 1093 if (Elt.isInt()) 1094 Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt())); 1095 else 1096 Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat())); 1097 } 1098 return llvm::ConstantVector::get(Inits); 1099 } 1100 case APValue::AddrLabelDiff: { 1101 const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS(); 1102 const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS(); 1103 llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF); 1104 llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF); 1105 1106 // Compute difference 1107 llvm::Type *ResultType = getTypes().ConvertType(DestType); 1108 LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy); 1109 RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy); 1110 llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS); 1111 1112 // LLVM is a bit sensitive about the exact format of the 1113 // address-of-label difference; make sure to truncate after 1114 // the subtraction. 1115 return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType); 1116 } 1117 case APValue::Struct: 1118 case APValue::Union: 1119 return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType); 1120 case APValue::Array: { 1121 const ArrayType *CAT = Context.getAsArrayType(DestType); 1122 unsigned NumElements = Value.getArraySize(); 1123 unsigned NumInitElts = Value.getArrayInitializedElts(); 1124 1125 std::vector<llvm::Constant*> Elts; 1126 Elts.reserve(NumElements); 1127 1128 // Emit array filler, if there is one. 1129 llvm::Constant *Filler = 0; 1130 if (Value.hasArrayFiller()) 1131 Filler = EmitConstantValue(Value.getArrayFiller(), 1132 CAT->getElementType(), CGF); 1133 1134 // Emit initializer elements. 1135 llvm::Type *CommonElementType = 0; 1136 for (unsigned I = 0; I < NumElements; ++I) { 1137 llvm::Constant *C = Filler; 1138 if (I < NumInitElts) 1139 C = EmitConstantValue(Value.getArrayInitializedElt(I), 1140 CAT->getElementType(), CGF); 1141 if (I == 0) 1142 CommonElementType = C->getType(); 1143 else if (C->getType() != CommonElementType) 1144 CommonElementType = 0; 1145 Elts.push_back(C); 1146 } 1147 1148 if (!CommonElementType) { 1149 // FIXME: Try to avoid packing the array 1150 std::vector<llvm::Type*> Types; 1151 Types.reserve(NumElements); 1152 for (unsigned i = 0, e = Elts.size(); i < e; ++i) 1153 Types.push_back(Elts[i]->getType()); 1154 llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true); 1155 return llvm::ConstantStruct::get(SType, Elts); 1156 } 1157 1158 llvm::ArrayType *AType = 1159 llvm::ArrayType::get(CommonElementType, NumElements); 1160 return llvm::ConstantArray::get(AType, Elts); 1161 } 1162 case APValue::MemberPointer: 1163 return getCXXABI().EmitMemberPointer(Value, DestType); 1164 } 1165 llvm_unreachable("Unknown APValue kind"); 1166} 1167 1168llvm::Constant * 1169CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) { 1170 assert(E->isFileScope() && "not a file-scope compound literal expr"); 1171 return ConstExprEmitter(*this, 0).EmitLValue(E); 1172} 1173 1174llvm::Constant * 1175CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) { 1176 // Member pointer constants always have a very particular form. 1177 const MemberPointerType *type = cast<MemberPointerType>(uo->getType()); 1178 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl(); 1179 1180 // A member function pointer. 1181 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl)) 1182 return getCXXABI().EmitMemberPointer(method); 1183 1184 // Otherwise, a member data pointer. 1185 uint64_t fieldOffset = getContext().getFieldOffset(decl); 1186 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset); 1187 return getCXXABI().EmitMemberDataPointer(type, chars); 1188} 1189 1190static void 1191FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T, 1192 SmallVectorImpl<llvm::Constant *> &Elements, 1193 uint64_t StartOffset) { 1194 assert(StartOffset % CGM.getContext().getCharWidth() == 0 && 1195 "StartOffset not byte aligned!"); 1196 1197 if (CGM.getTypes().isZeroInitializable(T)) 1198 return; 1199 1200 if (const ConstantArrayType *CAT = 1201 CGM.getContext().getAsConstantArrayType(T)) { 1202 QualType ElementTy = CAT->getElementType(); 1203 uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy); 1204 1205 for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) { 1206 FillInNullDataMemberPointers(CGM, ElementTy, Elements, 1207 StartOffset + I * ElementSize); 1208 } 1209 } else if (const RecordType *RT = T->getAs<RecordType>()) { 1210 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1211 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 1212 1213 // Go through all bases and fill in any null pointer to data members. 1214 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 1215 E = RD->bases_end(); I != E; ++I) { 1216 if (I->isVirtual()) { 1217 // Ignore virtual bases. 1218 continue; 1219 } 1220 1221 const CXXRecordDecl *BaseDecl = 1222 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 1223 1224 // Ignore empty bases. 1225 if (BaseDecl->isEmpty()) 1226 continue; 1227 1228 // Ignore bases that don't have any pointer to data members. 1229 if (CGM.getTypes().isZeroInitializable(BaseDecl)) 1230 continue; 1231 1232 uint64_t BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl); 1233 FillInNullDataMemberPointers(CGM, I->getType(), 1234 Elements, StartOffset + BaseOffset); 1235 } 1236 1237 // Visit all fields. 1238 unsigned FieldNo = 0; 1239 for (RecordDecl::field_iterator I = RD->field_begin(), 1240 E = RD->field_end(); I != E; ++I, ++FieldNo) { 1241 QualType FieldType = I->getType(); 1242 1243 if (CGM.getTypes().isZeroInitializable(FieldType)) 1244 continue; 1245 1246 uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo); 1247 FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset); 1248 } 1249 } else { 1250 assert(T->isMemberPointerType() && "Should only see member pointers here!"); 1251 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && 1252 "Should only see pointers to data members here!"); 1253 1254 CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset); 1255 CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T); 1256 1257 // FIXME: hardcodes Itanium member pointer representation! 1258 llvm::Constant *NegativeOne = 1259 llvm::ConstantInt::get(CGM.Int8Ty, -1ULL, /*isSigned*/true); 1260 1261 // Fill in the null data member pointer. 1262 for (CharUnits I = StartIndex; I != EndIndex; ++I) 1263 Elements[I.getQuantity()] = NegativeOne; 1264 } 1265} 1266 1267static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, 1268 llvm::Type *baseType, 1269 const CXXRecordDecl *base); 1270 1271static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, 1272 const CXXRecordDecl *record, 1273 bool asCompleteObject) { 1274 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record); 1275 llvm::StructType *structure = 1276 (asCompleteObject ? layout.getLLVMType() 1277 : layout.getBaseSubobjectLLVMType()); 1278 1279 unsigned numElements = structure->getNumElements(); 1280 std::vector<llvm::Constant *> elements(numElements); 1281 1282 // Fill in all the bases. 1283 for (CXXRecordDecl::base_class_const_iterator 1284 I = record->bases_begin(), E = record->bases_end(); I != E; ++I) { 1285 if (I->isVirtual()) { 1286 // Ignore virtual bases; if we're laying out for a complete 1287 // object, we'll lay these out later. 1288 continue; 1289 } 1290 1291 const CXXRecordDecl *base = 1292 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); 1293 1294 // Ignore empty bases. 1295 if (base->isEmpty()) 1296 continue; 1297 1298 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base); 1299 llvm::Type *baseType = structure->getElementType(fieldIndex); 1300 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); 1301 } 1302 1303 // Fill in all the fields. 1304 for (RecordDecl::field_iterator I = record->field_begin(), 1305 E = record->field_end(); I != E; ++I) { 1306 const FieldDecl *field = *I; 1307 1308 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we 1309 // will fill in later.) 1310 if (!field->isBitField()) { 1311 unsigned fieldIndex = layout.getLLVMFieldNo(field); 1312 elements[fieldIndex] = CGM.EmitNullConstant(field->getType()); 1313 } 1314 1315 // For unions, stop after the first named field. 1316 if (record->isUnion() && field->getDeclName()) 1317 break; 1318 } 1319 1320 // Fill in the virtual bases, if we're working with the complete object. 1321 if (asCompleteObject) { 1322 for (CXXRecordDecl::base_class_const_iterator 1323 I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) { 1324 const CXXRecordDecl *base = 1325 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); 1326 1327 // Ignore empty bases. 1328 if (base->isEmpty()) 1329 continue; 1330 1331 unsigned fieldIndex = layout.getVirtualBaseIndex(base); 1332 1333 // We might have already laid this field out. 1334 if (elements[fieldIndex]) continue; 1335 1336 llvm::Type *baseType = structure->getElementType(fieldIndex); 1337 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); 1338 } 1339 } 1340 1341 // Now go through all other fields and zero them out. 1342 for (unsigned i = 0; i != numElements; ++i) { 1343 if (!elements[i]) 1344 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i)); 1345 } 1346 1347 return llvm::ConstantStruct::get(structure, elements); 1348} 1349 1350/// Emit the null constant for a base subobject. 1351static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, 1352 llvm::Type *baseType, 1353 const CXXRecordDecl *base) { 1354 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base); 1355 1356 // Just zero out bases that don't have any pointer to data members. 1357 if (baseLayout.isZeroInitializableAsBase()) 1358 return llvm::Constant::getNullValue(baseType); 1359 1360 // If the base type is a struct, we can just use its null constant. 1361 if (isa<llvm::StructType>(baseType)) { 1362 return EmitNullConstant(CGM, base, /*complete*/ false); 1363 } 1364 1365 // Otherwise, some bases are represented as arrays of i8 if the size 1366 // of the base is smaller than its corresponding LLVM type. Figure 1367 // out how many elements this base array has. 1368 llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType); 1369 unsigned numBaseElements = baseArrayType->getNumElements(); 1370 1371 // Fill in null data member pointers. 1372 SmallVector<llvm::Constant *, 16> baseElements(numBaseElements); 1373 FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base), 1374 baseElements, 0); 1375 1376 // Now go through all other elements and zero them out. 1377 if (numBaseElements) { 1378 llvm::Constant *i8_zero = llvm::Constant::getNullValue(CGM.Int8Ty); 1379 for (unsigned i = 0; i != numBaseElements; ++i) { 1380 if (!baseElements[i]) 1381 baseElements[i] = i8_zero; 1382 } 1383 } 1384 1385 return llvm::ConstantArray::get(baseArrayType, baseElements); 1386} 1387 1388llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { 1389 if (getTypes().isZeroInitializable(T)) 1390 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); 1391 1392 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { 1393 llvm::ArrayType *ATy = 1394 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); 1395 1396 QualType ElementTy = CAT->getElementType(); 1397 1398 llvm::Constant *Element = EmitNullConstant(ElementTy); 1399 unsigned NumElements = CAT->getSize().getZExtValue(); 1400 1401 if (Element->isNullValue()) 1402 return llvm::ConstantAggregateZero::get(ATy); 1403 1404 SmallVector<llvm::Constant *, 8> Array(NumElements, Element); 1405 return llvm::ConstantArray::get(ATy, Array); 1406 } 1407 1408 if (const RecordType *RT = T->getAs<RecordType>()) { 1409 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1410 return ::EmitNullConstant(*this, RD, /*complete object*/ true); 1411 } 1412 1413 assert(T->isMemberPointerType() && "Should only see member pointers here!"); 1414 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && 1415 "Should only see pointers to data members here!"); 1416 1417 // Itanium C++ ABI 2.3: 1418 // A NULL pointer is represented as -1. 1419 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>()); 1420} 1421 1422llvm::Constant * 1423CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) { 1424 return ::EmitNullConstant(*this, Record, false); 1425} 1426