clang API Documentation
00001 //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Builder implementation for CGRecordLayout objects. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "CGRecordLayout.h" 00015 #include "clang/AST/ASTContext.h" 00016 #include "clang/AST/Attr.h" 00017 #include "clang/AST/CXXInheritance.h" 00018 #include "clang/AST/DeclCXX.h" 00019 #include "clang/AST/Expr.h" 00020 #include "clang/AST/RecordLayout.h" 00021 #include "clang/Frontend/CodeGenOptions.h" 00022 #include "CodeGenTypes.h" 00023 #include "CGCXXABI.h" 00024 #include "llvm/DerivedTypes.h" 00025 #include "llvm/Type.h" 00026 #include "llvm/Support/Debug.h" 00027 #include "llvm/Support/raw_ostream.h" 00028 #include "llvm/Target/TargetData.h" 00029 using namespace clang; 00030 using namespace CodeGen; 00031 00032 namespace { 00033 00034 class CGRecordLayoutBuilder { 00035 public: 00036 /// FieldTypes - Holds the LLVM types that the struct is created from. 00037 /// 00038 SmallVector<llvm::Type *, 16> FieldTypes; 00039 00040 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part 00041 /// of the struct. For example, consider: 00042 /// 00043 /// struct A { int i; }; 00044 /// struct B { void *v; }; 00045 /// struct C : virtual A, B { }; 00046 /// 00047 /// The LLVM type of C will be 00048 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B } 00049 /// 00050 /// And the LLVM type of the non-virtual base struct will be 00051 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 } 00052 /// 00053 /// This only gets initialized if the base subobject type is 00054 /// different from the complete-object type. 00055 llvm::StructType *BaseSubobjectType; 00056 00057 /// FieldInfo - Holds a field and its corresponding LLVM field number. 00058 llvm::DenseMap<const FieldDecl *, unsigned> Fields; 00059 00060 /// BitFieldInfo - Holds location and size information about a bit field. 00061 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; 00062 00063 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; 00064 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; 00065 00066 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are 00067 /// primary base classes for some other direct or indirect base class. 00068 CXXIndirectPrimaryBaseSet IndirectPrimaryBases; 00069 00070 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid 00071 /// avoid laying out virtual bases more than once. 00072 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases; 00073 00074 /// IsZeroInitializable - Whether this struct can be C++ 00075 /// zero-initialized with an LLVM zeroinitializer. 00076 bool IsZeroInitializable; 00077 bool IsZeroInitializableAsBase; 00078 00079 /// Packed - Whether the resulting LLVM struct will be packed or not. 00080 bool Packed; 00081 00082 /// IsMsStruct - Whether ms_struct is in effect or not 00083 bool IsMsStruct; 00084 00085 private: 00086 CodeGenTypes &Types; 00087 00088 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the 00089 /// last base laid out. Used so that we can replace the last laid out base 00090 /// type with an i8 array if needed. 00091 struct LastLaidOutBaseInfo { 00092 CharUnits Offset; 00093 CharUnits NonVirtualSize; 00094 00095 bool isValid() const { return !NonVirtualSize.isZero(); } 00096 void invalidate() { NonVirtualSize = CharUnits::Zero(); } 00097 00098 } LastLaidOutBase; 00099 00100 /// Alignment - Contains the alignment of the RecordDecl. 00101 CharUnits Alignment; 00102 00103 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field, 00104 /// this will have the number of bits still available in the field. 00105 char BitsAvailableInLastField; 00106 00107 /// NextFieldOffset - Holds the next field offset. 00108 CharUnits NextFieldOffset; 00109 00110 /// LayoutUnionField - Will layout a field in an union and return the type 00111 /// that the field will have. 00112 llvm::Type *LayoutUnionField(const FieldDecl *Field, 00113 const ASTRecordLayout &Layout); 00114 00115 /// LayoutUnion - Will layout a union RecordDecl. 00116 void LayoutUnion(const RecordDecl *D); 00117 00118 /// LayoutField - try to layout all fields in the record decl. 00119 /// Returns false if the operation failed because the struct is not packed. 00120 bool LayoutFields(const RecordDecl *D); 00121 00122 /// Layout a single base, virtual or non-virtual 00123 bool LayoutBase(const CXXRecordDecl *base, 00124 const CGRecordLayout &baseLayout, 00125 CharUnits baseOffset); 00126 00127 /// LayoutVirtualBase - layout a single virtual base. 00128 bool LayoutVirtualBase(const CXXRecordDecl *base, 00129 CharUnits baseOffset); 00130 00131 /// LayoutVirtualBases - layout the virtual bases of a record decl. 00132 bool LayoutVirtualBases(const CXXRecordDecl *RD, 00133 const ASTRecordLayout &Layout); 00134 00135 /// MSLayoutVirtualBases - layout the virtual bases of a record decl, 00136 /// like MSVC. 00137 bool MSLayoutVirtualBases(const CXXRecordDecl *RD, 00138 const ASTRecordLayout &Layout); 00139 00140 /// LayoutNonVirtualBase - layout a single non-virtual base. 00141 bool LayoutNonVirtualBase(const CXXRecordDecl *base, 00142 CharUnits baseOffset); 00143 00144 /// LayoutNonVirtualBases - layout the virtual bases of a record decl. 00145 bool LayoutNonVirtualBases(const CXXRecordDecl *RD, 00146 const ASTRecordLayout &Layout); 00147 00148 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types. 00149 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD); 00150 00151 /// LayoutField - layout a single field. Returns false if the operation failed 00152 /// because the current struct is not packed. 00153 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset); 00154 00155 /// LayoutBitField - layout a single bit field. 00156 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset); 00157 00158 /// AppendField - Appends a field with the given offset and type. 00159 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy); 00160 00161 /// AppendPadding - Appends enough padding bytes so that the total 00162 /// struct size is a multiple of the field alignment. 00163 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment); 00164 00165 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the 00166 /// tail padding of a previous base. If this happens, the type of the previous 00167 /// base needs to be changed to an array of i8. Returns true if the last 00168 /// laid out base was resized. 00169 bool ResizeLastBaseFieldIfNecessary(CharUnits offset); 00170 00171 /// getByteArrayType - Returns a byte array type with the given number of 00172 /// elements. 00173 llvm::Type *getByteArrayType(CharUnits NumBytes); 00174 00175 /// AppendBytes - Append a given number of bytes to the record. 00176 void AppendBytes(CharUnits numBytes); 00177 00178 /// AppendTailPadding - Append enough tail padding so that the type will have 00179 /// the passed size. 00180 void AppendTailPadding(CharUnits RecordSize); 00181 00182 CharUnits getTypeAlignment(llvm::Type *Ty) const; 00183 00184 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the 00185 /// LLVM element types. 00186 CharUnits getAlignmentAsLLVMStruct() const; 00187 00188 /// CheckZeroInitializable - Check if the given type contains a pointer 00189 /// to data member. 00190 void CheckZeroInitializable(QualType T); 00191 00192 public: 00193 CGRecordLayoutBuilder(CodeGenTypes &Types) 00194 : BaseSubobjectType(0), 00195 IsZeroInitializable(true), IsZeroInitializableAsBase(true), 00196 Packed(false), IsMsStruct(false), 00197 Types(Types), BitsAvailableInLastField(0) { } 00198 00199 /// Layout - Will layout a RecordDecl. 00200 void Layout(const RecordDecl *D); 00201 }; 00202 00203 } 00204 00205 void CGRecordLayoutBuilder::Layout(const RecordDecl *D) { 00206 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment(); 00207 Packed = D->hasAttr<PackedAttr>(); 00208 00209 IsMsStruct = D->hasAttr<MsStructAttr>(); 00210 00211 if (D->isUnion()) { 00212 LayoutUnion(D); 00213 return; 00214 } 00215 00216 if (LayoutFields(D)) 00217 return; 00218 00219 // We weren't able to layout the struct. Try again with a packed struct 00220 Packed = true; 00221 LastLaidOutBase.invalidate(); 00222 NextFieldOffset = CharUnits::Zero(); 00223 FieldTypes.clear(); 00224 Fields.clear(); 00225 BitFields.clear(); 00226 NonVirtualBases.clear(); 00227 VirtualBases.clear(); 00228 00229 LayoutFields(D); 00230 } 00231 00232 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, 00233 const FieldDecl *FD, 00234 uint64_t FieldOffset, 00235 uint64_t FieldSize, 00236 uint64_t ContainingTypeSizeInBits, 00237 unsigned ContainingTypeAlign) { 00238 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType()); 00239 CharUnits TypeSizeInBytes = 00240 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty)); 00241 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes); 00242 00243 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); 00244 00245 if (FieldSize > TypeSizeInBits) { 00246 // We have a wide bit-field. The extra bits are only used for padding, so 00247 // if we have a bitfield of type T, with size N: 00248 // 00249 // T t : N; 00250 // 00251 // We can just assume that it's: 00252 // 00253 // T t : sizeof(T); 00254 // 00255 FieldSize = TypeSizeInBits; 00256 } 00257 00258 // in big-endian machines the first fields are in higher bit positions, 00259 // so revert the offset. The byte offsets are reversed(back) later. 00260 if (Types.getTargetData().isBigEndian()) { 00261 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize); 00262 } 00263 00264 // Compute the access components. The policy we use is to start by attempting 00265 // to access using the width of the bit-field type itself and to always access 00266 // at aligned indices of that type. If such an access would fail because it 00267 // extends past the bound of the type, then we reduce size to the next smaller 00268 // power of two and retry. The current algorithm assumes pow2 sized types, 00269 // although this is easy to fix. 00270 // 00271 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!"); 00272 CGBitFieldInfo::AccessInfo Components[3]; 00273 unsigned NumComponents = 0; 00274 unsigned AccessedTargetBits = 0; // The number of target bits accessed. 00275 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt. 00276 00277 // If requested, widen the initial bit-field access to be register sized. The 00278 // theory is that this is most likely to allow multiple accesses into the same 00279 // structure to be coalesced, and that the backend should be smart enough to 00280 // narrow the store if no coalescing is ever done. 00281 // 00282 // The subsequent code will handle align these access to common boundaries and 00283 // guaranteeing that we do not access past the end of the structure. 00284 if (Types.getCodeGenOpts().UseRegisterSizedBitfieldAccess) { 00285 if (AccessWidth < Types.getTarget().getRegisterWidth()) 00286 AccessWidth = Types.getTarget().getRegisterWidth(); 00287 } 00288 00289 // Round down from the field offset to find the first access position that is 00290 // at an aligned offset of the initial access type. 00291 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth); 00292 00293 // Adjust initial access size to fit within record. 00294 while (AccessWidth > Types.getTarget().getCharWidth() && 00295 AccessStart + AccessWidth > ContainingTypeSizeInBits) { 00296 AccessWidth >>= 1; 00297 AccessStart = FieldOffset - (FieldOffset % AccessWidth); 00298 } 00299 00300 while (AccessedTargetBits < FieldSize) { 00301 // Check that we can access using a type of this size, without reading off 00302 // the end of the structure. This can occur with packed structures and 00303 // -fno-bitfield-type-align, for example. 00304 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) { 00305 // If so, reduce access size to the next smaller power-of-two and retry. 00306 AccessWidth >>= 1; 00307 assert(AccessWidth >= Types.getTarget().getCharWidth() 00308 && "Cannot access under byte size!"); 00309 continue; 00310 } 00311 00312 // Otherwise, add an access component. 00313 00314 // First, compute the bits inside this access which are part of the 00315 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the 00316 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits 00317 // in the target that we are reading. 00318 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!"); 00319 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!"); 00320 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset); 00321 uint64_t AccessBitsInFieldSize = 00322 std::min(AccessWidth + AccessStart, 00323 FieldOffset + FieldSize) - AccessBitsInFieldStart; 00324 00325 assert(NumComponents < 3 && "Unexpected number of components!"); 00326 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++]; 00327 AI.FieldIndex = 0; 00328 // FIXME: We still follow the old access pattern of only using the field 00329 // byte offset. We should switch this once we fix the struct layout to be 00330 // pretty. 00331 00332 // on big-endian machines we reverted the bit offset because first fields are 00333 // in higher bits. But this also reverts the bytes, so fix this here by reverting 00334 // the byte offset on big-endian machines. 00335 if (Types.getTargetData().isBigEndian()) { 00336 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits( 00337 ContainingTypeSizeInBits - AccessStart - AccessWidth); 00338 } else { 00339 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart); 00340 } 00341 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart; 00342 AI.AccessWidth = AccessWidth; 00343 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits( 00344 llvm::MinAlign(ContainingTypeAlign, AccessStart)); 00345 AI.TargetBitOffset = AccessedTargetBits; 00346 AI.TargetBitWidth = AccessBitsInFieldSize; 00347 00348 AccessStart += AccessWidth; 00349 AccessedTargetBits += AI.TargetBitWidth; 00350 } 00351 00352 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!"); 00353 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned); 00354 } 00355 00356 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, 00357 const FieldDecl *FD, 00358 uint64_t FieldOffset, 00359 uint64_t FieldSize) { 00360 const RecordDecl *RD = FD->getParent(); 00361 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD); 00362 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize()); 00363 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment()); 00364 00365 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits, 00366 ContainingTypeAlign); 00367 } 00368 00369 void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D, 00370 uint64_t fieldOffset) { 00371 uint64_t fieldSize = D->getBitWidthValue(Types.getContext()); 00372 00373 if (fieldSize == 0) 00374 return; 00375 00376 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset); 00377 CharUnits numBytesToAppend; 00378 unsigned charAlign = Types.getContext().getTargetInfo().getCharAlign(); 00379 00380 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) { 00381 assert(fieldOffset % charAlign == 0 && 00382 "Field offset not aligned correctly"); 00383 00384 CharUnits fieldOffsetInCharUnits = 00385 Types.getContext().toCharUnitsFromBits(fieldOffset); 00386 00387 // Try to resize the last base field. 00388 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits)) 00389 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset); 00390 } 00391 00392 if (fieldOffset < nextFieldOffsetInBits) { 00393 assert(BitsAvailableInLastField && "Bitfield size mismatch!"); 00394 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte"); 00395 00396 // The bitfield begins in the previous bit-field. 00397 numBytesToAppend = Types.getContext().toCharUnitsFromBits( 00398 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField, 00399 charAlign)); 00400 } else { 00401 assert(fieldOffset % charAlign == 0 && 00402 "Field offset not aligned correctly"); 00403 00404 // Append padding if necessary. 00405 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset), 00406 CharUnits::One()); 00407 00408 numBytesToAppend = Types.getContext().toCharUnitsFromBits( 00409 llvm::RoundUpToAlignment(fieldSize, charAlign)); 00410 00411 assert(!numBytesToAppend.isZero() && "No bytes to append!"); 00412 } 00413 00414 // Add the bit field info. 00415 BitFields.insert(std::make_pair(D, 00416 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize))); 00417 00418 AppendBytes(numBytesToAppend); 00419 00420 BitsAvailableInLastField = 00421 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize); 00422 } 00423 00424 bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D, 00425 uint64_t fieldOffset) { 00426 // If the field is packed, then we need a packed struct. 00427 if (!Packed && D->hasAttr<PackedAttr>()) 00428 return false; 00429 00430 if (D->isBitField()) { 00431 // We must use packed structs for unnamed bit fields since they 00432 // don't affect the struct alignment. 00433 if (!Packed && !D->getDeclName()) 00434 return false; 00435 00436 LayoutBitField(D, fieldOffset); 00437 return true; 00438 } 00439 00440 CheckZeroInitializable(D->getType()); 00441 00442 assert(fieldOffset % Types.getTarget().getCharWidth() == 0 00443 && "field offset is not on a byte boundary!"); 00444 CharUnits fieldOffsetInBytes 00445 = Types.getContext().toCharUnitsFromBits(fieldOffset); 00446 00447 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType()); 00448 CharUnits typeAlignment = getTypeAlignment(Ty); 00449 00450 // If the type alignment is larger then the struct alignment, we must use 00451 // a packed struct. 00452 if (typeAlignment > Alignment) { 00453 assert(!Packed && "Alignment is wrong even with packed struct!"); 00454 return false; 00455 } 00456 00457 if (!Packed) { 00458 if (const RecordType *RT = D->getType()->getAs<RecordType>()) { 00459 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); 00460 if (const MaxFieldAlignmentAttr *MFAA = 00461 RD->getAttr<MaxFieldAlignmentAttr>()) { 00462 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment)) 00463 return false; 00464 } 00465 } 00466 } 00467 00468 // Round up the field offset to the alignment of the field type. 00469 CharUnits alignedNextFieldOffsetInBytes = 00470 NextFieldOffset.RoundUpToAlignment(typeAlignment); 00471 00472 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) { 00473 // Try to resize the last base field. 00474 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) { 00475 alignedNextFieldOffsetInBytes = 00476 NextFieldOffset.RoundUpToAlignment(typeAlignment); 00477 } 00478 } 00479 00480 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) { 00481 assert(!Packed && "Could not place field even with packed struct!"); 00482 return false; 00483 } 00484 00485 AppendPadding(fieldOffsetInBytes, typeAlignment); 00486 00487 // Now append the field. 00488 Fields[D] = FieldTypes.size(); 00489 AppendField(fieldOffsetInBytes, Ty); 00490 00491 LastLaidOutBase.invalidate(); 00492 return true; 00493 } 00494 00495 llvm::Type * 00496 CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field, 00497 const ASTRecordLayout &Layout) { 00498 if (Field->isBitField()) { 00499 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext()); 00500 00501 // Ignore zero sized bit fields. 00502 if (FieldSize == 0) 00503 return 0; 00504 00505 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext()); 00506 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits( 00507 llvm::RoundUpToAlignment(FieldSize, 00508 Types.getContext().getTargetInfo().getCharAlign())); 00509 00510 if (NumBytesToAppend > CharUnits::One()) 00511 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity()); 00512 00513 // Add the bit field info. 00514 BitFields.insert(std::make_pair(Field, 00515 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize))); 00516 return FieldTy; 00517 } 00518 00519 // This is a regular union field. 00520 Fields[Field] = 0; 00521 return Types.ConvertTypeForMem(Field->getType()); 00522 } 00523 00524 void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) { 00525 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!"); 00526 00527 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D); 00528 00529 llvm::Type *unionType = 0; 00530 CharUnits unionSize = CharUnits::Zero(); 00531 CharUnits unionAlign = CharUnits::Zero(); 00532 00533 bool hasOnlyZeroSizedBitFields = true; 00534 bool checkedFirstFieldZeroInit = false; 00535 00536 unsigned fieldNo = 0; 00537 for (RecordDecl::field_iterator field = D->field_begin(), 00538 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) { 00539 assert(layout.getFieldOffset(fieldNo) == 0 && 00540 "Union field offset did not start at the beginning of record!"); 00541 llvm::Type *fieldType = LayoutUnionField(&*field, layout); 00542 00543 if (!fieldType) 00544 continue; 00545 00546 if (field->getDeclName() && !checkedFirstFieldZeroInit) { 00547 CheckZeroInitializable(field->getType()); 00548 checkedFirstFieldZeroInit = true; 00549 } 00550 00551 hasOnlyZeroSizedBitFields = false; 00552 00553 CharUnits fieldAlign = CharUnits::fromQuantity( 00554 Types.getTargetData().getABITypeAlignment(fieldType)); 00555 CharUnits fieldSize = CharUnits::fromQuantity( 00556 Types.getTargetData().getTypeAllocSize(fieldType)); 00557 00558 if (fieldAlign < unionAlign) 00559 continue; 00560 00561 if (fieldAlign > unionAlign || fieldSize > unionSize) { 00562 unionType = fieldType; 00563 unionAlign = fieldAlign; 00564 unionSize = fieldSize; 00565 } 00566 } 00567 00568 // Now add our field. 00569 if (unionType) { 00570 AppendField(CharUnits::Zero(), unionType); 00571 00572 if (getTypeAlignment(unionType) > layout.getAlignment()) { 00573 // We need a packed struct. 00574 Packed = true; 00575 unionAlign = CharUnits::One(); 00576 } 00577 } 00578 if (unionAlign.isZero()) { 00579 (void)hasOnlyZeroSizedBitFields; 00580 assert(hasOnlyZeroSizedBitFields && 00581 "0-align record did not have all zero-sized bit-fields!"); 00582 unionAlign = CharUnits::One(); 00583 } 00584 00585 // Append tail padding. 00586 CharUnits recordSize = layout.getSize(); 00587 if (recordSize > unionSize) 00588 AppendPadding(recordSize, unionAlign); 00589 } 00590 00591 bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base, 00592 const CGRecordLayout &baseLayout, 00593 CharUnits baseOffset) { 00594 ResizeLastBaseFieldIfNecessary(baseOffset); 00595 00596 AppendPadding(baseOffset, CharUnits::One()); 00597 00598 const ASTRecordLayout &baseASTLayout 00599 = Types.getContext().getASTRecordLayout(base); 00600 00601 LastLaidOutBase.Offset = NextFieldOffset; 00602 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize(); 00603 00604 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType(); 00605 if (getTypeAlignment(subobjectType) > Alignment) 00606 return false; 00607 00608 AppendField(baseOffset, subobjectType); 00609 return true; 00610 } 00611 00612 bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base, 00613 CharUnits baseOffset) { 00614 // Ignore empty bases. 00615 if (base->isEmpty()) return true; 00616 00617 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); 00618 if (IsZeroInitializableAsBase) { 00619 assert(IsZeroInitializable && 00620 "class zero-initializable as base but not as complete object"); 00621 00622 IsZeroInitializable = IsZeroInitializableAsBase = 00623 baseLayout.isZeroInitializableAsBase(); 00624 } 00625 00626 if (!LayoutBase(base, baseLayout, baseOffset)) 00627 return false; 00628 NonVirtualBases[base] = (FieldTypes.size() - 1); 00629 return true; 00630 } 00631 00632 bool 00633 CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base, 00634 CharUnits baseOffset) { 00635 // Ignore empty bases. 00636 if (base->isEmpty()) return true; 00637 00638 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); 00639 if (IsZeroInitializable) 00640 IsZeroInitializable = baseLayout.isZeroInitializableAsBase(); 00641 00642 if (!LayoutBase(base, baseLayout, baseOffset)) 00643 return false; 00644 VirtualBases[base] = (FieldTypes.size() - 1); 00645 return true; 00646 } 00647 00648 bool 00649 CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD, 00650 const ASTRecordLayout &Layout) { 00651 if (!RD->getNumVBases()) 00652 return true; 00653 00654 // The vbases list is uniqued and ordered by a depth-first 00655 // traversal, which is what we need here. 00656 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), 00657 E = RD->vbases_end(); I != E; ++I) { 00658 00659 const CXXRecordDecl *BaseDecl = 00660 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); 00661 00662 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl); 00663 if (!LayoutVirtualBase(BaseDecl, vbaseOffset)) 00664 return false; 00665 } 00666 return true; 00667 } 00668 00669 /// LayoutVirtualBases - layout the non-virtual bases of a record decl. 00670 bool 00671 CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, 00672 const ASTRecordLayout &Layout) { 00673 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 00674 E = RD->bases_end(); I != E; ++I) { 00675 const CXXRecordDecl *BaseDecl = 00676 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 00677 00678 // We only want to lay out virtual bases that aren't indirect primary bases 00679 // of some other base. 00680 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) { 00681 // Only lay out the base once. 00682 if (!LaidOutVirtualBases.insert(BaseDecl)) 00683 continue; 00684 00685 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl); 00686 if (!LayoutVirtualBase(BaseDecl, vbaseOffset)) 00687 return false; 00688 } 00689 00690 if (!BaseDecl->getNumVBases()) { 00691 // This base isn't interesting since it doesn't have any virtual bases. 00692 continue; 00693 } 00694 00695 if (!LayoutVirtualBases(BaseDecl, Layout)) 00696 return false; 00697 } 00698 return true; 00699 } 00700 00701 bool 00702 CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD, 00703 const ASTRecordLayout &Layout) { 00704 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 00705 00706 // If we have a primary base, lay it out first. 00707 if (PrimaryBase) { 00708 if (!Layout.isPrimaryBaseVirtual()) { 00709 if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero())) 00710 return false; 00711 } else { 00712 if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero())) 00713 return false; 00714 } 00715 00716 // Otherwise, add a vtable / vf-table if the layout says to do so. 00717 } else if (Layout.hasOwnVFPtr()) { 00718 llvm::Type *FunctionType = 00719 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()), 00720 /*isVarArg=*/true); 00721 llvm::Type *VTableTy = FunctionType->getPointerTo(); 00722 00723 if (getTypeAlignment(VTableTy) > Alignment) { 00724 // FIXME: Should we allow this to happen in Sema? 00725 assert(!Packed && "Alignment is wrong even with packed struct!"); 00726 return false; 00727 } 00728 00729 assert(NextFieldOffset.isZero() && 00730 "VTable pointer must come first!"); 00731 AppendField(CharUnits::Zero(), VTableTy->getPointerTo()); 00732 } 00733 00734 // Layout the non-virtual bases. 00735 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 00736 E = RD->bases_end(); I != E; ++I) { 00737 if (I->isVirtual()) 00738 continue; 00739 00740 const CXXRecordDecl *BaseDecl = 00741 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 00742 00743 // We've already laid out the primary base. 00744 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual()) 00745 continue; 00746 00747 if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl))) 00748 return false; 00749 } 00750 00751 // Add a vb-table pointer if the layout insists. 00752 if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) { 00753 CharUnits VBPtrOffset = Layout.getVBPtrOffset(); 00754 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext()); 00755 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr)); 00756 AppendField(VBPtrOffset, Vbptr); 00757 } 00758 00759 return true; 00760 } 00761 00762 bool 00763 CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) { 00764 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD); 00765 00766 CharUnits NonVirtualSize = Layout.getNonVirtualSize(); 00767 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); 00768 CharUnits AlignedNonVirtualTypeSize = 00769 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); 00770 00771 // First check if we can use the same fields as for the complete class. 00772 CharUnits RecordSize = Layout.getSize(); 00773 if (AlignedNonVirtualTypeSize == RecordSize) 00774 return true; 00775 00776 // Check if we need padding. 00777 CharUnits AlignedNextFieldOffset = 00778 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct()); 00779 00780 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) { 00781 assert(!Packed && "cannot layout even as packed struct"); 00782 return false; // Needs packing. 00783 } 00784 00785 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset); 00786 if (needsPadding) { 00787 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset; 00788 FieldTypes.push_back(getByteArrayType(NumBytes)); 00789 } 00790 00791 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(), 00792 FieldTypes, "", Packed); 00793 Types.addRecordTypeName(RD, BaseSubobjectType, ".base"); 00794 00795 // Pull the padding back off. 00796 if (needsPadding) 00797 FieldTypes.pop_back(); 00798 00799 return true; 00800 } 00801 00802 bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { 00803 assert(!D->isUnion() && "Can't call LayoutFields on a union!"); 00804 assert(!Alignment.isZero() && "Did not set alignment!"); 00805 00806 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); 00807 00808 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); 00809 if (RD) 00810 if (!LayoutNonVirtualBases(RD, Layout)) 00811 return false; 00812 00813 unsigned FieldNo = 0; 00814 const FieldDecl *LastFD = 0; 00815 00816 for (RecordDecl::field_iterator Field = D->field_begin(), 00817 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 00818 if (IsMsStruct) { 00819 // Zero-length bitfields following non-bitfield members are 00820 // ignored: 00821 const FieldDecl *FD = &*Field; 00822 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) { 00823 --FieldNo; 00824 continue; 00825 } 00826 LastFD = FD; 00827 } 00828 00829 if (!LayoutField(&*Field, Layout.getFieldOffset(FieldNo))) { 00830 assert(!Packed && 00831 "Could not layout fields even with a packed LLVM struct!"); 00832 return false; 00833 } 00834 } 00835 00836 if (RD) { 00837 // We've laid out the non-virtual bases and the fields, now compute the 00838 // non-virtual base field types. 00839 if (!ComputeNonVirtualBaseType(RD)) { 00840 assert(!Packed && "Could not layout even with a packed LLVM struct!"); 00841 return false; 00842 } 00843 00844 // Lay out the virtual bases. The MS ABI uses a different 00845 // algorithm here due to the lack of primary virtual bases. 00846 if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) { 00847 RD->getIndirectPrimaryBases(IndirectPrimaryBases); 00848 if (Layout.isPrimaryBaseVirtual()) 00849 IndirectPrimaryBases.insert(Layout.getPrimaryBase()); 00850 00851 if (!LayoutVirtualBases(RD, Layout)) 00852 return false; 00853 } else { 00854 if (!MSLayoutVirtualBases(RD, Layout)) 00855 return false; 00856 } 00857 } 00858 00859 // Append tail padding if necessary. 00860 AppendTailPadding(Layout.getSize()); 00861 00862 return true; 00863 } 00864 00865 void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) { 00866 ResizeLastBaseFieldIfNecessary(RecordSize); 00867 00868 assert(NextFieldOffset <= RecordSize && "Size mismatch!"); 00869 00870 CharUnits AlignedNextFieldOffset = 00871 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct()); 00872 00873 if (AlignedNextFieldOffset == RecordSize) { 00874 // We don't need any padding. 00875 return; 00876 } 00877 00878 CharUnits NumPadBytes = RecordSize - NextFieldOffset; 00879 AppendBytes(NumPadBytes); 00880 } 00881 00882 void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset, 00883 llvm::Type *fieldType) { 00884 CharUnits fieldSize = 00885 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType)); 00886 00887 FieldTypes.push_back(fieldType); 00888 00889 NextFieldOffset = fieldOffset + fieldSize; 00890 BitsAvailableInLastField = 0; 00891 } 00892 00893 void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset, 00894 CharUnits fieldAlignment) { 00895 assert(NextFieldOffset <= fieldOffset && 00896 "Incorrect field layout!"); 00897 00898 // Do nothing if we're already at the right offset. 00899 if (fieldOffset == NextFieldOffset) return; 00900 00901 // If we're not emitting a packed LLVM type, try to avoid adding 00902 // unnecessary padding fields. 00903 if (!Packed) { 00904 // Round up the field offset to the alignment of the field type. 00905 CharUnits alignedNextFieldOffset = 00906 NextFieldOffset.RoundUpToAlignment(fieldAlignment); 00907 assert(alignedNextFieldOffset <= fieldOffset); 00908 00909 // If that's the right offset, we're done. 00910 if (alignedNextFieldOffset == fieldOffset) return; 00911 } 00912 00913 // Otherwise we need explicit padding. 00914 CharUnits padding = fieldOffset - NextFieldOffset; 00915 AppendBytes(padding); 00916 } 00917 00918 bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) { 00919 // Check if we have a base to resize. 00920 if (!LastLaidOutBase.isValid()) 00921 return false; 00922 00923 // This offset does not overlap with the tail padding. 00924 if (offset >= NextFieldOffset) 00925 return false; 00926 00927 // Restore the field offset and append an i8 array instead. 00928 FieldTypes.pop_back(); 00929 NextFieldOffset = LastLaidOutBase.Offset; 00930 AppendBytes(LastLaidOutBase.NonVirtualSize); 00931 LastLaidOutBase.invalidate(); 00932 00933 return true; 00934 } 00935 00936 llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) { 00937 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed."); 00938 00939 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext()); 00940 if (numBytes > CharUnits::One()) 00941 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity()); 00942 00943 return Ty; 00944 } 00945 00946 void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) { 00947 if (numBytes.isZero()) 00948 return; 00949 00950 // Append the padding field 00951 AppendField(NextFieldOffset, getByteArrayType(numBytes)); 00952 } 00953 00954 CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const { 00955 if (Packed) 00956 return CharUnits::One(); 00957 00958 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty)); 00959 } 00960 00961 CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const { 00962 if (Packed) 00963 return CharUnits::One(); 00964 00965 CharUnits maxAlignment = CharUnits::One(); 00966 for (size_t i = 0; i != FieldTypes.size(); ++i) 00967 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i])); 00968 00969 return maxAlignment; 00970 } 00971 00972 /// Merge in whether a field of the given type is zero-initializable. 00973 void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) { 00974 // This record already contains a member pointer. 00975 if (!IsZeroInitializableAsBase) 00976 return; 00977 00978 // Can only have member pointers if we're compiling C++. 00979 if (!Types.getContext().getLangOpts().CPlusPlus) 00980 return; 00981 00982 const Type *elementType = T->getBaseElementTypeUnsafe(); 00983 00984 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) { 00985 if (!Types.getCXXABI().isZeroInitializable(MPT)) 00986 IsZeroInitializable = IsZeroInitializableAsBase = false; 00987 } else if (const RecordType *RT = elementType->getAs<RecordType>()) { 00988 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 00989 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD); 00990 if (!Layout.isZeroInitializable()) 00991 IsZeroInitializable = IsZeroInitializableAsBase = false; 00992 } 00993 } 00994 00995 CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, 00996 llvm::StructType *Ty) { 00997 CGRecordLayoutBuilder Builder(*this); 00998 00999 Builder.Layout(D); 01000 01001 Ty->setBody(Builder.FieldTypes, Builder.Packed); 01002 01003 // If we're in C++, compute the base subobject type. 01004 llvm::StructType *BaseTy = 0; 01005 if (isa<CXXRecordDecl>(D) && !D->isUnion()) { 01006 BaseTy = Builder.BaseSubobjectType; 01007 if (!BaseTy) BaseTy = Ty; 01008 } 01009 01010 CGRecordLayout *RL = 01011 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, 01012 Builder.IsZeroInitializableAsBase); 01013 01014 RL->NonVirtualBases.swap(Builder.NonVirtualBases); 01015 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases); 01016 01017 // Add all the field numbers. 01018 RL->FieldInfo.swap(Builder.Fields); 01019 01020 // Add bitfield info. 01021 RL->BitFields.swap(Builder.BitFields); 01022 01023 // Dump the layout, if requested. 01024 if (getContext().getLangOpts().DumpRecordLayouts) { 01025 llvm::errs() << "\n*** Dumping IRgen Record Layout\n"; 01026 llvm::errs() << "Record: "; 01027 D->dump(); 01028 llvm::errs() << "\nLayout: "; 01029 RL->dump(); 01030 } 01031 01032 #ifndef NDEBUG 01033 // Verify that the computed LLVM struct size matches the AST layout size. 01034 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); 01035 01036 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); 01037 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) && 01038 "Type size mismatch!"); 01039 01040 if (BaseTy) { 01041 CharUnits NonVirtualSize = Layout.getNonVirtualSize(); 01042 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); 01043 CharUnits AlignedNonVirtualTypeSize = 01044 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); 01045 01046 uint64_t AlignedNonVirtualTypeSizeInBits = 01047 getContext().toBits(AlignedNonVirtualTypeSize); 01048 01049 assert(AlignedNonVirtualTypeSizeInBits == 01050 getTargetData().getTypeAllocSizeInBits(BaseTy) && 01051 "Type size mismatch!"); 01052 } 01053 01054 // Verify that the LLVM and AST field offsets agree. 01055 llvm::StructType *ST = 01056 dyn_cast<llvm::StructType>(RL->getLLVMType()); 01057 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST); 01058 01059 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); 01060 RecordDecl::field_iterator it = D->field_begin(); 01061 const FieldDecl *LastFD = 0; 01062 bool IsMsStruct = D->hasAttr<MsStructAttr>(); 01063 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { 01064 const FieldDecl *FD = &*it; 01065 01066 // For non-bit-fields, just check that the LLVM struct offset matches the 01067 // AST offset. 01068 if (!FD->isBitField()) { 01069 unsigned FieldNo = RL->getLLVMFieldNo(FD); 01070 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && 01071 "Invalid field offset!"); 01072 LastFD = FD; 01073 continue; 01074 } 01075 01076 if (IsMsStruct) { 01077 // Zero-length bitfields following non-bitfield members are 01078 // ignored: 01079 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) { 01080 --i; 01081 continue; 01082 } 01083 LastFD = FD; 01084 } 01085 01086 // Ignore unnamed bit-fields. 01087 if (!FD->getDeclName()) { 01088 LastFD = FD; 01089 continue; 01090 } 01091 01092 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); 01093 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { 01094 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); 01095 01096 // Verify that every component access is within the structure. 01097 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex); 01098 uint64_t AccessBitOffset = FieldOffset + 01099 getContext().toBits(AI.FieldByteOffset); 01100 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits && 01101 "Invalid bit-field access (out of range)!"); 01102 } 01103 } 01104 #endif 01105 01106 return RL; 01107 } 01108 01109 void CGRecordLayout::print(raw_ostream &OS) const { 01110 OS << "<CGRecordLayout\n"; 01111 OS << " LLVMType:" << *CompleteObjectType << "\n"; 01112 if (BaseSubobjectType) 01113 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; 01114 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n"; 01115 OS << " BitFields:[\n"; 01116 01117 // Print bit-field infos in declaration order. 01118 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; 01119 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator 01120 it = BitFields.begin(), ie = BitFields.end(); 01121 it != ie; ++it) { 01122 const RecordDecl *RD = it->first->getParent(); 01123 unsigned Index = 0; 01124 for (RecordDecl::field_iterator 01125 it2 = RD->field_begin(); &*it2 != it->first; ++it2) 01126 ++Index; 01127 BFIs.push_back(std::make_pair(Index, &it->second)); 01128 } 01129 llvm::array_pod_sort(BFIs.begin(), BFIs.end()); 01130 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { 01131 OS.indent(4); 01132 BFIs[i].second->print(OS); 01133 OS << "\n"; 01134 } 01135 01136 OS << "]>\n"; 01137 } 01138 01139 void CGRecordLayout::dump() const { 01140 print(llvm::errs()); 01141 } 01142 01143 void CGBitFieldInfo::print(raw_ostream &OS) const { 01144 OS << "<CGBitFieldInfo"; 01145 OS << " Size:" << Size; 01146 OS << " IsSigned:" << IsSigned << "\n"; 01147 01148 OS.indent(4 + strlen("<CGBitFieldInfo")); 01149 OS << " NumComponents:" << getNumComponents(); 01150 OS << " Components: ["; 01151 if (getNumComponents()) { 01152 OS << "\n"; 01153 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) { 01154 const AccessInfo &AI = getComponent(i); 01155 OS.indent(8); 01156 OS << "<AccessInfo" 01157 << " FieldIndex:" << AI.FieldIndex 01158 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity() 01159 << " FieldBitStart:" << AI.FieldBitStart 01160 << " AccessWidth:" << AI.AccessWidth << "\n"; 01161 OS.indent(8 + strlen("<AccessInfo")); 01162 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity() 01163 << " TargetBitOffset:" << AI.TargetBitOffset 01164 << " TargetBitWidth:" << AI.TargetBitWidth 01165 << ">\n"; 01166 } 01167 OS.indent(4); 01168 } 01169 OS << "]>"; 01170 } 01171 01172 void CGBitFieldInfo::dump() const { 01173 print(llvm::errs()); 01174 }