Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 1 | //=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==// |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 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 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 10 | #include "clang/AST/Attr.h" |
| 11 | #include "clang/AST/Decl.h" |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 12 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 14 | #include "clang/AST/Expr.h" |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 15 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Format.h" |
| 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | #include "llvm/Support/MathExtras.h" |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 20 | #include <map> |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Benjamin Kramer | 7e22028 | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 24 | namespace { |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 25 | |
| 26 | /// EmptySubobjectMap - Keeps track of which empty subobjects exist at different |
| 27 | /// offsets while laying out a C++ class. |
| 28 | class EmptySubobjectMap { |
| 29 | ASTContext &Context; |
| 30 | |
| 31 | /// Class - The class whose empty entries we're keeping track of. |
| 32 | const CXXRecordDecl *Class; |
| 33 | |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 34 | /// ComputeEmptySubobjectSizes - Compute the size of the largest base or |
| 35 | /// member subobject that is empty. |
| 36 | void ComputeEmptySubobjectSizes(); |
| 37 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 38 | public: |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 39 | /// This holds the size of the largest empty subobject (either a base |
| 40 | /// or a member). Will be zero if the record being built doesn't contain |
| 41 | /// any empty classes. |
| 42 | uint64_t SizeOfLargestEmptySubobject; |
| 43 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 44 | EmptySubobjectMap(ASTContext &Context, const CXXRecordDecl *Class) |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 45 | : Context(Context), Class(Class), SizeOfLargestEmptySubobject(0) { |
| 46 | ComputeEmptySubobjectSizes(); |
| 47 | } |
| 48 | |
| 49 | /// CanPlaceBaseAtOffset - Return whether the given base class can be placed |
| 50 | /// at the given offset. |
| 51 | /// Returns false if placing the record will result in two components |
| 52 | /// (direct or indirect) of the same type having the same offset. |
| 53 | bool CanPlaceBaseAtOffset(const CXXRecordDecl *RD, bool BaseIsVirtual, |
| 54 | uint64_t Offset); |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 55 | }; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 56 | |
| 57 | void EmptySubobjectMap::ComputeEmptySubobjectSizes() { |
| 58 | // Check the bases. |
| 59 | for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), |
| 60 | E = Class->bases_end(); I != E; ++I) { |
| 61 | const CXXRecordDecl *BaseDecl = |
| 62 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 63 | |
| 64 | uint64_t EmptySize = 0; |
| 65 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 66 | if (BaseDecl->isEmpty()) { |
| 67 | // If the class decl is empty, get its size. |
| 68 | EmptySize = Layout.getSize(); |
| 69 | } else { |
| 70 | // Otherwise, we get the largest empty subobject for the decl. |
| 71 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 72 | } |
| 73 | |
| 74 | SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject, |
| 75 | EmptySize); |
| 76 | } |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 77 | |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 78 | // Check the fields. |
| 79 | for (CXXRecordDecl::field_iterator I = Class->field_begin(), |
| 80 | E = Class->field_end(); I != E; ++I) { |
| 81 | const FieldDecl *FD = *I; |
| 82 | |
| 83 | const RecordType *RT = |
| 84 | Context.getBaseElementType(FD->getType())->getAs<RecordType>(); |
| 85 | |
| 86 | // We only care about record types. |
| 87 | if (!RT) |
| 88 | continue; |
| 89 | |
| 90 | uint64_t EmptySize = 0; |
| 91 | const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 92 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl); |
| 93 | if (MemberDecl->isEmpty()) { |
| 94 | // If the class decl is empty, get its size. |
| 95 | EmptySize = Layout.getSize(); |
| 96 | } else { |
| 97 | // Otherwise, we get the largest empty subobject for the decl. |
| 98 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 99 | } |
| 100 | |
| 101 | SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject, |
| 102 | EmptySize); |
| 103 | } |
| 104 | } |
| 105 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 106 | bool |
| 107 | EmptySubobjectMap::CanPlaceBaseAtOffset(const CXXRecordDecl *RD, |
| 108 | bool BaseIsVirtual, |
| 109 | uint64_t Offset) { |
| 110 | // If we know this class doesn't have any empty subobjects we don't need to |
| 111 | // bother checking. |
| 112 | if (!SizeOfLargestEmptySubobject) |
| 113 | return true; |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 118 | class RecordLayoutBuilder { |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 119 | // FIXME: Remove this and make the appropriate fields public. |
| 120 | friend class clang::ASTContext; |
| 121 | |
| 122 | ASTContext &Context; |
| 123 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 124 | EmptySubobjectMap *EmptySubobjects; |
| 125 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 126 | /// Size - The current size of the record layout. |
| 127 | uint64_t Size; |
| 128 | |
| 129 | /// Alignment - The current alignment of the record layout. |
| 130 | unsigned Alignment; |
| 131 | |
| 132 | llvm::SmallVector<uint64_t, 16> FieldOffsets; |
| 133 | |
| 134 | /// Packed - Whether the record is packed or not. |
| 135 | bool Packed; |
| 136 | |
| 137 | /// UnfilledBitsInLastByte - If the last field laid out was a bitfield, |
| 138 | /// this contains the number of bits in the last byte that can be used for |
| 139 | /// an adjacent bitfield if necessary. |
| 140 | unsigned char UnfilledBitsInLastByte; |
| 141 | |
| 142 | /// MaxFieldAlignment - The maximum allowed field alignment. This is set by |
| 143 | /// #pragma pack. |
| 144 | unsigned MaxFieldAlignment; |
| 145 | |
| 146 | /// DataSize - The data size of the record being laid out. |
| 147 | uint64_t DataSize; |
| 148 | |
| 149 | bool IsUnion; |
| 150 | |
| 151 | uint64_t NonVirtualSize; |
| 152 | unsigned NonVirtualAlignment; |
| 153 | |
| 154 | /// PrimaryBase - the primary base class (if one exists) of the class |
| 155 | /// we're laying out. |
| 156 | const CXXRecordDecl *PrimaryBase; |
| 157 | |
| 158 | /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying |
| 159 | /// out is virtual. |
| 160 | bool PrimaryBaseIsVirtual; |
| 161 | |
| 162 | typedef llvm::DenseMap<const CXXRecordDecl *, uint64_t> BaseOffsetsMapTy; |
| 163 | |
| 164 | /// Bases - base classes and their offsets in the record. |
| 165 | BaseOffsetsMapTy Bases; |
| 166 | |
| 167 | // VBases - virtual base classes and their offsets in the record. |
| 168 | BaseOffsetsMapTy VBases; |
| 169 | |
| 170 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 171 | /// primary base classes for some other direct or indirect base class. |
| 172 | llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases; |
| 173 | |
| 174 | /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in |
| 175 | /// inheritance graph order. Used for determining the primary base class. |
| 176 | const CXXRecordDecl *FirstNearlyEmptyVBase; |
| 177 | |
| 178 | /// VisitedVirtualBases - A set of all the visited virtual bases, used to |
| 179 | /// avoid visiting virtual bases more than once. |
| 180 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 181 | |
| 182 | /// EmptyClassOffsets - A map from offsets to empty record decls. |
| 183 | typedef std::multimap<uint64_t, const CXXRecordDecl *> EmptyClassOffsetsTy; |
| 184 | EmptyClassOffsetsTy EmptyClassOffsets; |
| 185 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 186 | RecordLayoutBuilder(ASTContext &Context, EmptySubobjectMap *EmptySubobjects) |
| 187 | : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), Alignment(8), |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 188 | Packed(false), UnfilledBitsInLastByte(0), MaxFieldAlignment(0), DataSize(0), |
Anders Carlsson | 0efac25 | 2010-05-26 15:20:08 +0000 | [diff] [blame] | 189 | IsUnion(false), NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0), |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 190 | PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { } |
Anders Carlsson | 0efac25 | 2010-05-26 15:20:08 +0000 | [diff] [blame] | 191 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 192 | void Layout(const RecordDecl *D); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 193 | void Layout(const CXXRecordDecl *D); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 194 | void Layout(const ObjCInterfaceDecl *D); |
| 195 | |
| 196 | void LayoutFields(const RecordDecl *D); |
| 197 | void LayoutField(const FieldDecl *D); |
| 198 | void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize); |
| 199 | void LayoutBitField(const FieldDecl *D); |
| 200 | |
| 201 | /// ComputeEmptySubobjectSizes - Compute the size of the largest base or |
| 202 | /// member subobject that is empty. |
| 203 | void ComputeEmptySubobjectSizes(const CXXRecordDecl *RD); |
| 204 | |
| 205 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
| 206 | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
| 207 | |
| 208 | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
| 209 | |
| 210 | /// IdentifyPrimaryBases - Identify all virtual base classes, direct or |
| 211 | /// indirect, that are primary base classes for some other direct or indirect |
| 212 | /// base class. |
| 213 | void IdentifyPrimaryBases(const CXXRecordDecl *RD); |
| 214 | |
| 215 | bool IsNearlyEmpty(const CXXRecordDecl *RD) const; |
| 216 | |
| 217 | /// LayoutNonVirtualBases - Determines the primary base class (if any) and |
| 218 | /// lays it out. Will then proceed to lay out all non-virtual base clasess. |
| 219 | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
| 220 | |
| 221 | /// LayoutNonVirtualBase - Lays out a single non-virtual base. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 222 | void LayoutNonVirtualBase(const CXXRecordDecl *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 223 | |
| 224 | void AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, |
| 225 | const CXXRecordDecl *MostDerivedClass); |
| 226 | |
| 227 | /// LayoutVirtualBases - Lays out all the virtual bases. |
| 228 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 229 | const CXXRecordDecl *MostDerivedClass); |
| 230 | |
| 231 | /// LayoutVirtualBase - Lays out a single virtual base. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 232 | void LayoutVirtualBase(const CXXRecordDecl *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 233 | |
| 234 | /// LayoutBase - Will lay out a base and return the offset where it was |
| 235 | /// placed, in bits. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 236 | uint64_t LayoutBase(const CXXRecordDecl *Base, bool BaseIsVirtual); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 237 | |
| 238 | /// canPlaceRecordAtOffset - Return whether a record (either a base class |
| 239 | /// or a field) can be placed at the given offset. |
| 240 | /// Returns false if placing the record will result in two components |
| 241 | /// (direct or indirect) of the same type having the same offset. |
| 242 | bool canPlaceRecordAtOffset(const CXXRecordDecl *RD, uint64_t Offset, |
| 243 | bool CheckVBases) const; |
| 244 | |
| 245 | /// canPlaceFieldAtOffset - Return whether a field can be placed at the given |
| 246 | /// offset. |
| 247 | bool canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const; |
| 248 | |
| 249 | /// UpdateEmptyClassOffsets - Called after a record (either a base class |
| 250 | /// or a field) has been placed at the given offset. Will update the |
| 251 | /// EmptyClassOffsets map if the class is empty or has any empty bases or |
| 252 | /// fields. |
| 253 | void UpdateEmptyClassOffsets(const CXXRecordDecl *RD, uint64_t Offset, |
| 254 | bool UpdateVBases); |
| 255 | |
| 256 | /// UpdateEmptyClassOffsets - Called after a field has been placed at the |
| 257 | /// given offset. |
| 258 | void UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t Offset); |
| 259 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 260 | /// InitializeLayout - Initialize record layout for the given record decl. |
| 261 | void InitializeLayout(const RecordDecl *D); |
| 262 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 263 | /// FinishLayout - Finalize record layout. Adjust record size based on the |
| 264 | /// alignment. |
| 265 | void FinishLayout(); |
| 266 | |
| 267 | void UpdateAlignment(unsigned NewAlignment); |
| 268 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 269 | RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
| 270 | void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 271 | public: |
| 272 | static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD); |
| 273 | }; |
Benjamin Kramer | 7e22028 | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 274 | } // end anonymous namespace |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 275 | |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 276 | /// IsNearlyEmpty - Indicates when a class has a vtable pointer, but |
| 277 | /// no other data. |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 278 | bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 279 | // FIXME: Audit the corners |
| 280 | if (!RD->isDynamicClass()) |
| 281 | return false; |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 282 | const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD); |
| 283 | if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0)) |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 284 | return true; |
| 285 | return false; |
| 286 | } |
| 287 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 288 | void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) { |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 289 | const ASTRecordLayout::PrimaryBaseInfo &BaseInfo = |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 290 | Context.getASTRecordLayout(RD).getPrimaryBaseInfo(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 291 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 292 | // If the record has a primary base class that is virtual, add it to the set |
| 293 | // of primary bases. |
Anders Carlsson | 261fba6 | 2009-11-27 22:14:40 +0000 | [diff] [blame] | 294 | if (BaseInfo.isVirtual()) |
| 295 | IndirectPrimaryBases.insert(BaseInfo.getBase()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 296 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 297 | // Now traverse all bases and find primary bases for them. |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 298 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 299 | e = RD->bases_end(); i != e; ++i) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 300 | assert(!i->getType()->isDependentType() && |
| 301 | "Cannot layout class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | const CXXRecordDecl *Base = |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 303 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 304 | |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 305 | // Only bases with virtual bases participate in computing the |
| 306 | // indirect primary virtual base classes. |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 307 | if (Base->getNumVBases()) |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 308 | IdentifyPrimaryBases(Base); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 312 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 313 | RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 314 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 315 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 316 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 317 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 318 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | const CXXRecordDecl *Base = |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 320 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 321 | |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 322 | // Check if this is a nearly empty virtual base. |
| 323 | if (I->isVirtual() && IsNearlyEmpty(Base)) { |
| 324 | // If it's not an indirect primary base, then we've found our primary |
| 325 | // base. |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 326 | if (!IndirectPrimaryBases.count(Base)) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 327 | PrimaryBase = Base; |
| 328 | PrimaryBaseIsVirtual = true; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 329 | return; |
| 330 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 331 | |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 332 | // Is this the first nearly empty virtual base? |
| 333 | if (!FirstNearlyEmptyVBase) |
| 334 | FirstNearlyEmptyVBase = Base; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 335 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 336 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 337 | SelectPrimaryVBase(Base); |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 338 | if (PrimaryBase) |
Zhongxing Xu | 94ba380 | 2010-02-15 04:28:35 +0000 | [diff] [blame] | 339 | return; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 343 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 344 | void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 345 | // If the class isn't dynamic, it won't have a primary base. |
| 346 | if (!RD->isDynamicClass()) |
| 347 | return; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 348 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 349 | // Compute all the primary virtual bases for all of our direct and |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 350 | // indirect bases, and record all their primary virtual base classes. |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 351 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 352 | e = RD->bases_end(); i != e; ++i) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 353 | assert(!i->getType()->isDependentType() && |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 354 | "Cannot lay out class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | const CXXRecordDecl *Base = |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 356 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 357 | IdentifyPrimaryBases(Base); |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 360 | // If the record has a dynamic base class, attempt to choose a primary base |
| 361 | // class. It is the first (in direct base class order) non-virtual dynamic |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 362 | // base class, if one exists. |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 363 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 364 | e = RD->bases_end(); i != e; ++i) { |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 365 | // Ignore virtual bases. |
| 366 | if (i->isVirtual()) |
| 367 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 368 | |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 369 | const CXXRecordDecl *Base = |
| 370 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 371 | |
| 372 | if (Base->isDynamicClass()) { |
| 373 | // We found it. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 374 | PrimaryBase = Base; |
| 375 | PrimaryBaseIsVirtual = false; |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 376 | return; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | // Otherwise, it is the first nearly empty virtual base that is not an |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 381 | // indirect primary virtual base class, if one exists. |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 382 | if (RD->getNumVBases() != 0) { |
| 383 | SelectPrimaryVBase(RD); |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 384 | if (PrimaryBase) |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 385 | return; |
| 386 | } |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 387 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 388 | // Otherwise, it is the first nearly empty virtual base that is not an |
| 389 | // indirect primary virtual base class, if one exists. |
| 390 | if (FirstNearlyEmptyVBase) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 391 | PrimaryBase = FirstNearlyEmptyVBase; |
| 392 | PrimaryBaseIsVirtual = true; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 393 | return; |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 394 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 395 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 396 | // Otherwise there is no primary base class. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 397 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 398 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 399 | // Allocate the virtual table pointer at offset zero. |
| 400 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 401 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 402 | // Update the size. |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 403 | Size += Context.Target.getPointerWidth(0); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 404 | DataSize = Size; |
| 405 | |
| 406 | // Update the alignment. |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 407 | UpdateAlignment(Context.Target.getPointerAlign(0)); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 410 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 411 | RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 412 | // First, determine the primary base class. |
| 413 | DeterminePrimaryBase(RD); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 414 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 415 | // If we have a primary base class, lay it out. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 416 | if (PrimaryBase) { |
| 417 | if (PrimaryBaseIsVirtual) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 418 | // We have a virtual primary base, insert it as an indirect primary base. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 419 | IndirectPrimaryBases.insert(PrimaryBase); |
Anders Carlsson | 37147ea | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 420 | |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 421 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
| 422 | "vbase already visited!"); |
| 423 | VisitedVirtualBases.insert(PrimaryBase); |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 424 | |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 425 | LayoutVirtualBase(PrimaryBase); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 426 | } else |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 427 | LayoutNonVirtualBase(PrimaryBase); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 428 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 429 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 430 | // Now lay out the non-virtual bases. |
| 431 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 432 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 433 | |
| 434 | // Ignore virtual bases. |
| 435 | if (I->isVirtual()) |
| 436 | continue; |
| 437 | |
| 438 | const CXXRecordDecl *Base = |
| 439 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 440 | |
| 441 | // Skip the primary base. |
Anders Carlsson | aa230b7 | 2010-05-26 05:31:23 +0000 | [diff] [blame] | 442 | if (Base == PrimaryBase && !PrimaryBaseIsVirtual) |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 443 | continue; |
| 444 | |
| 445 | // Lay out the base. |
| 446 | LayoutNonVirtualBase(Base); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 450 | void RecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *Base) { |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 451 | // Layout the base. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 452 | uint64_t Offset = LayoutBase(Base, /*BaseIsVirtual=*/false); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 453 | |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 454 | // Add its base class offset. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 455 | if (!Bases.insert(std::make_pair(Base, Offset)).second) |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 456 | assert(false && "Added same base offset more than once!"); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 457 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 458 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 459 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 460 | RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 461 | uint64_t Offset, |
| 462 | const CXXRecordDecl *MostDerivedClass) { |
| 463 | // We already have the offset for the primary base of the most derived class. |
| 464 | if (RD != MostDerivedClass) { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 465 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 466 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 467 | |
| 468 | // If this is a primary virtual base and we haven't seen it before, add it. |
| 469 | if (PrimaryBase && Layout.getPrimaryBaseWasVirtual() && |
| 470 | !VBases.count(PrimaryBase)) |
| 471 | VBases.insert(std::make_pair(PrimaryBase, Offset)); |
| 472 | } |
| 473 | |
| 474 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 475 | E = RD->bases_end(); I != E; ++I) { |
| 476 | assert(!I->getType()->isDependentType() && |
| 477 | "Cannot layout class with dependent bases."); |
| 478 | |
| 479 | const CXXRecordDecl *BaseDecl = |
| 480 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 481 | |
| 482 | if (!BaseDecl->getNumVBases()) { |
| 483 | // This base isn't interesting since it doesn't have any virtual bases. |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | // Compute the offset of this base. |
| 488 | uint64_t BaseOffset; |
| 489 | |
| 490 | if (I->isVirtual()) { |
| 491 | // If we don't know this vbase yet, don't visit it. It will be visited |
| 492 | // later. |
| 493 | if (!VBases.count(BaseDecl)) { |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | // Check if we've already visited this base. |
| 498 | if (!VisitedVirtualBases.insert(BaseDecl)) |
| 499 | continue; |
| 500 | |
| 501 | // We want the vbase offset from the class we're currently laying out. |
| 502 | BaseOffset = VBases[BaseDecl]; |
| 503 | } else if (RD == MostDerivedClass) { |
| 504 | // We want the base offset from the class we're currently laying out. |
| 505 | assert(Bases.count(BaseDecl) && "Did not find base!"); |
| 506 | BaseOffset = Bases[BaseDecl]; |
| 507 | } else { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 508 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 509 | BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
| 510 | } |
| 511 | |
| 512 | AddPrimaryVirtualBaseOffsets(BaseDecl, BaseOffset, MostDerivedClass); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 517 | RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 518 | const CXXRecordDecl *MostDerivedClass) { |
Anders Carlsson | 88f4296 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 519 | const CXXRecordDecl *PrimaryBase; |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 520 | bool PrimaryBaseIsVirtual; |
Anders Carlsson | 37147ea | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 521 | |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 522 | if (MostDerivedClass == RD) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 523 | PrimaryBase = this->PrimaryBase; |
| 524 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 525 | } else { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 526 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 88f4296 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 527 | PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 528 | PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 529 | } |
| 530 | |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 531 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 532 | E = RD->bases_end(); I != E; ++I) { |
| 533 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 534 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 535 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | const CXXRecordDecl *Base = |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 537 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 538 | |
| 539 | if (I->isVirtual()) { |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 540 | if (PrimaryBase != Base || !PrimaryBaseIsVirtual) { |
| 541 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(Base); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 542 | |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 543 | // Only lay out the virtual base if it's not an indirect primary base. |
| 544 | if (!IndirectPrimaryBase) { |
| 545 | // Only visit virtual bases once. |
| 546 | if (!VisitedVirtualBases.insert(Base)) |
| 547 | continue; |
| 548 | |
Anders Carlsson | 37147ea | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 549 | LayoutVirtualBase(Base); |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 550 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 551 | } |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 552 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 553 | |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 554 | if (!Base->getNumVBases()) { |
| 555 | // This base isn't interesting since it doesn't have any virtual bases. |
| 556 | continue; |
Mike Stump | fe3010d | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 557 | } |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 558 | |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 559 | LayoutVirtualBases(Base, MostDerivedClass); |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 563 | void RecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *Base) { |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 564 | // Layout the base. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 565 | uint64_t Offset = LayoutBase(Base, /*BaseIsVirtual=*/true); |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 566 | |
| 567 | // Add its base class offset. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 568 | if (!VBases.insert(std::make_pair(Base, Offset)).second) |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 569 | assert(false && "Added same vbase offset more than once!"); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 572 | uint64_t RecordLayoutBuilder::LayoutBase(const CXXRecordDecl *Base, |
| 573 | bool BaseIsVirtual) { |
| 574 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 575 | |
| 576 | // If we have an empty base class, try to place it at offset 0. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 577 | if (Base->isEmpty() && |
| 578 | EmptySubobjects->CanPlaceBaseAtOffset(Base, BaseIsVirtual, 0) && |
| 579 | canPlaceRecordAtOffset(Base, 0, /*CheckVBases=*/false)) { |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 580 | // We were able to place the class at offset 0. |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 581 | UpdateEmptyClassOffsets(Base, 0, /*UpdateVBases=*/false); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 582 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 583 | Size = std::max(Size, Layout.getSize()); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 584 | |
| 585 | return 0; |
| 586 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 587 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 588 | unsigned BaseAlign = Layout.getNonVirtualAlign(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 589 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 590 | // Round up the current record size to the base's alignment boundary. |
| 591 | uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 592 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 593 | // Try to place the base. |
| 594 | while (true) { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 595 | if (EmptySubobjects->CanPlaceBaseAtOffset(Base, BaseIsVirtual, Offset) && |
| 596 | canPlaceRecordAtOffset(Base, Offset, /*CheckVBases=*/false)) |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 597 | break; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 598 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 599 | Offset += BaseAlign; |
| 600 | } |
| 601 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 602 | if (!Base->isEmpty()) { |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 603 | // Update the data size. |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 604 | DataSize = Offset + Layout.getNonVirtualSize(); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 605 | |
| 606 | Size = std::max(Size, DataSize); |
| 607 | } else |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 608 | Size = std::max(Size, Offset + Layout.getSize()); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 609 | |
| 610 | // Remember max struct/class alignment. |
| 611 | UpdateAlignment(BaseAlign); |
| 612 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 613 | UpdateEmptyClassOffsets(Base, Offset, /*UpdateVBases=*/false); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 614 | return Offset; |
| 615 | } |
| 616 | |
Anders Carlsson | 3159ffe | 2010-05-10 15:26:14 +0000 | [diff] [blame] | 617 | bool |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 618 | RecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | 3159ffe | 2010-05-10 15:26:14 +0000 | [diff] [blame] | 619 | uint64_t Offset, |
| 620 | bool CheckVBases) const { |
Anders Carlsson | 1345bd2 | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 621 | // Look for an empty class with the same type at the same offset. |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 622 | for (EmptyClassOffsetsTy::const_iterator I = |
| 623 | EmptyClassOffsets.lower_bound(Offset), |
| 624 | E = EmptyClassOffsets.upper_bound(Offset); I != E; ++I) { |
| 625 | |
Anders Carlsson | 1345bd2 | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 626 | if (I->second == RD) |
| 627 | return false; |
| 628 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 629 | |
Anders Carlsson | ddae877 | 2010-05-09 05:03:38 +0000 | [diff] [blame] | 630 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 631 | |
| 632 | // Check bases. |
| 633 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 634 | E = RD->bases_end(); I != E; ++I) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 635 | assert(!I->getType()->isDependentType() && |
| 636 | "Cannot layout class with dependent bases."); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 637 | if (I->isVirtual()) |
| 638 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 639 | |
Anders Carlsson | ddae877 | 2010-05-09 05:03:38 +0000 | [diff] [blame] | 640 | const CXXRecordDecl *BaseDecl = |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 641 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 642 | |
Anders Carlsson | ddae877 | 2010-05-09 05:03:38 +0000 | [diff] [blame] | 643 | uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 644 | |
Anders Carlsson | 3159ffe | 2010-05-10 15:26:14 +0000 | [diff] [blame] | 645 | if (!canPlaceRecordAtOffset(BaseDecl, Offset + BaseOffset, |
| 646 | /*CheckVBases=*/false)) |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 647 | return false; |
| 648 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 649 | |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 650 | // Check fields. |
| 651 | unsigned FieldNo = 0; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 652 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 653 | I != E; ++I, ++FieldNo) { |
| 654 | const FieldDecl *FD = *I; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 655 | |
Anders Carlsson | ddae877 | 2010-05-09 05:03:38 +0000 | [diff] [blame] | 656 | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 657 | |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 658 | if (!canPlaceFieldAtOffset(FD, Offset + FieldOffset)) |
| 659 | return false; |
| 660 | } |
| 661 | |
Anders Carlsson | 3159ffe | 2010-05-10 15:26:14 +0000 | [diff] [blame] | 662 | if (CheckVBases) { |
| 663 | // FIXME: virtual bases. |
| 664 | } |
| 665 | |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 666 | return true; |
| 667 | } |
| 668 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 669 | bool RecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, |
Anders Carlsson | 6026504 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 670 | uint64_t Offset) const { |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 671 | QualType T = FD->getType(); |
| 672 | if (const RecordType *RT = T->getAs<RecordType>()) { |
Anders Carlsson | 6026504 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 673 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
Anders Carlsson | 3159ffe | 2010-05-10 15:26:14 +0000 | [diff] [blame] | 674 | return canPlaceRecordAtOffset(RD, Offset, /*CheckVBases=*/true); |
Anders Carlsson | 6026504 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 675 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 676 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 677 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 678 | QualType ElemTy = Context.getBaseElementType(AT); |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 679 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 680 | if (!RT) |
| 681 | return true; |
| 682 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 683 | if (!RD) |
| 684 | return true; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 685 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 686 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 687 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 688 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 689 | uint64_t ElementOffset = Offset; |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 690 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | 3159ffe | 2010-05-10 15:26:14 +0000 | [diff] [blame] | 691 | if (!canPlaceRecordAtOffset(RD, ElementOffset, /*CheckVBases=*/true)) |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 692 | return false; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 693 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 694 | ElementOffset += Layout.getSize(); |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 697 | |
Anders Carlsson | 6026504 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 698 | return true; |
| 699 | } |
| 700 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 701 | void RecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD, |
Anders Carlsson | ecafebe | 2010-05-10 15:28:59 +0000 | [diff] [blame] | 702 | uint64_t Offset, |
| 703 | bool UpdateVBases) { |
Anders Carlsson | 1345bd2 | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 704 | if (RD->isEmpty()) |
| 705 | EmptyClassOffsets.insert(std::make_pair(Offset, RD)); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 706 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 707 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 708 | |
| 709 | // Update bases. |
| 710 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 711 | E = RD->bases_end(); I != E; ++I) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 712 | assert(!I->getType()->isDependentType() && |
| 713 | "Cannot layout class with dependent bases."); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 714 | if (I->isVirtual()) |
| 715 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 716 | |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 717 | const CXXRecordDecl *Base = |
| 718 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 719 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 720 | uint64_t BaseClassOffset = Layout.getBaseClassOffset(Base); |
Anders Carlsson | ecafebe | 2010-05-10 15:28:59 +0000 | [diff] [blame] | 721 | UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset, |
| 722 | /*UpdateVBases=*/false); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 723 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 724 | |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 725 | // Update fields. |
| 726 | unsigned FieldNo = 0; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 727 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 728 | I != E; ++I, ++FieldNo) { |
| 729 | const FieldDecl *FD = *I; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 731 | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 732 | UpdateEmptyClassOffsets(FD, Offset + FieldOffset); |
| 733 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 734 | |
Anders Carlsson | bfcdc40 | 2010-05-23 18:14:24 +0000 | [diff] [blame] | 735 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 736 | |
Anders Carlsson | ecafebe | 2010-05-10 15:28:59 +0000 | [diff] [blame] | 737 | if (UpdateVBases) { |
| 738 | // FIXME: Update virtual bases. |
Anders Carlsson | bfcdc40 | 2010-05-23 18:14:24 +0000 | [diff] [blame] | 739 | } else if (PrimaryBase && Layout.getPrimaryBaseWasVirtual()) { |
| 740 | // We always want to update the offsets of a primary virtual base. |
| 741 | assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 && |
| 742 | "primary base class offset must always be 0!"); |
| 743 | UpdateEmptyClassOffsets(PrimaryBase, Offset, /*UpdateVBases=*/false); |
Anders Carlsson | ecafebe | 2010-05-10 15:28:59 +0000 | [diff] [blame] | 744 | } |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 747 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 748 | RecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD, |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 749 | uint64_t Offset) { |
| 750 | QualType T = FD->getType(); |
| 751 | |
| 752 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 753 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Anders Carlsson | ecafebe | 2010-05-10 15:28:59 +0000 | [diff] [blame] | 754 | UpdateEmptyClassOffsets(RD, Offset, /*UpdateVBases=*/true); |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 758 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 759 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 760 | QualType ElemTy = Context.getBaseElementType(AT); |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 761 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 762 | if (!RT) |
| 763 | return; |
| 764 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 765 | if (!RD) |
| 766 | return; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 767 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 768 | const ASTRecordLayout &Info = Context.getASTRecordLayout(RD); |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 769 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 770 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 771 | uint64_t ElementOffset = Offset; |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 772 | |
| 773 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | ecafebe | 2010-05-10 15:28:59 +0000 | [diff] [blame] | 774 | UpdateEmptyClassOffsets(RD, ElementOffset, /*UpdateVBases=*/true); |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 775 | ElementOffset += Info.getSize(); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 780 | void RecordLayoutBuilder::InitializeLayout(const RecordDecl *D) { |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 781 | IsUnion = D->isUnion(); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 782 | |
Anders Carlsson | a5dd722 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 783 | Packed = D->hasAttr<PackedAttr>(); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 784 | |
Daniel Dunbar | 8a2c92c | 2010-05-27 01:12:46 +0000 | [diff] [blame^] | 785 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
| 786 | MaxFieldAlignment = MFAA->getAlignment(); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 787 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 788 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 789 | UpdateAlignment(AA->getMaxAlignment()); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 790 | } |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 791 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 792 | void RecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 793 | InitializeLayout(D); |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 794 | LayoutFields(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 796 | // Finally, round the size of the total struct up to the alignment of the |
| 797 | // struct itself. |
| 798 | FinishLayout(); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 802 | // Create our empty subobject offset map. |
| 803 | EmptySubobjectMap EmptySubobjectMap(Context, RD); |
| 804 | EmptySubobjects = &EmptySubobjectMap; |
| 805 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 806 | InitializeLayout(RD); |
| 807 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 808 | // Lay out the vtable and the non-virtual bases. |
| 809 | LayoutNonVirtualBases(RD); |
| 810 | |
| 811 | LayoutFields(RD); |
| 812 | |
| 813 | NonVirtualSize = Size; |
| 814 | NonVirtualAlignment = Alignment; |
| 815 | |
| 816 | // Lay out the virtual bases and add the primary virtual base offsets. |
| 817 | LayoutVirtualBases(RD, RD); |
| 818 | |
| 819 | VisitedVirtualBases.clear(); |
| 820 | AddPrimaryVirtualBaseOffsets(RD, 0, RD); |
| 821 | |
| 822 | // Finally, round the size of the total struct up to the alignment of the |
| 823 | // struct itself. |
| 824 | FinishLayout(); |
| 825 | |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 826 | #ifndef NDEBUG |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 827 | // Check that we have base offsets for all bases. |
| 828 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 829 | E = RD->bases_end(); I != E; ++I) { |
| 830 | if (I->isVirtual()) |
| 831 | continue; |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 832 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 833 | const CXXRecordDecl *BaseDecl = |
| 834 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 835 | |
| 836 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 837 | } |
| 838 | |
| 839 | // And all virtual bases. |
| 840 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 841 | E = RD->vbases_end(); I != E; ++I) { |
| 842 | const CXXRecordDecl *BaseDecl = |
| 843 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 844 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 845 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 846 | } |
| 847 | #endif |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 850 | void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 851 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 852 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 853 | |
| 854 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 856 | // We start laying out ivars not at the end of the superclass |
| 857 | // structure, but at the next byte following the last field. |
Anders Carlsson | 243a685 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 858 | Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8); |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 859 | DataSize = Size; |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Anders Carlsson | a5dd722 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 862 | Packed = D->hasAttr<PackedAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | 8a2c92c | 2010-05-27 01:12:46 +0000 | [diff] [blame^] | 864 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
| 865 | MaxFieldAlignment = MFAA->getAlignment(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 867 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 868 | UpdateAlignment(AA->getMaxAlignment()); |
Daniel Dunbar | 8a2c92c | 2010-05-27 01:12:46 +0000 | [diff] [blame^] | 869 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 870 | // Layout each ivar sequentially. |
| 871 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 872 | Context.ShallowCollectObjCIvars(D, Ivars); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 873 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) |
| 874 | LayoutField(Ivars[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 876 | // Finally, round the size of the total struct up to the alignment of the |
| 877 | // struct itself. |
| 878 | FinishLayout(); |
| 879 | } |
| 880 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 881 | void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 882 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 883 | // the future, this will need to be tweakable by targets. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 885 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 886 | LayoutField(*Field); |
| 887 | } |
| 888 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 889 | void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 890 | uint64_t TypeSize) { |
| 891 | assert(Context.getLangOptions().CPlusPlus && |
| 892 | "Can only have wide bit-fields in C++!"); |
| 893 | |
| 894 | // Itanium C++ ABI 2.4: |
| 895 | // If sizeof(T)*8 < n, let T' be the largest integral POD type with |
| 896 | // sizeof(T')*8 <= n. |
| 897 | |
| 898 | QualType IntegralPODTypes[] = { |
| 899 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
| 900 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
| 901 | }; |
| 902 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 903 | QualType Type; |
| 904 | for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes); |
| 905 | I != E; ++I) { |
| 906 | uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 907 | |
| 908 | if (Size > FieldSize) |
| 909 | break; |
| 910 | |
| 911 | Type = IntegralPODTypes[I]; |
| 912 | } |
| 913 | assert(!Type.isNull() && "Did not find a type!"); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 914 | |
| 915 | unsigned TypeAlign = Context.getTypeAlign(Type); |
| 916 | |
| 917 | // We're not going to use any of the unfilled bits in the last byte. |
| 918 | UnfilledBitsInLastByte = 0; |
| 919 | |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 920 | uint64_t FieldOffset; |
| 921 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 922 | if (IsUnion) { |
| 923 | DataSize = std::max(DataSize, FieldSize); |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 924 | FieldOffset = 0; |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 925 | } else { |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 926 | // The bitfield is allocated starting at the next offset aligned appropriately |
| 927 | // for T', with length n bits. |
| 928 | FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign); |
| 929 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 930 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
| 931 | |
| 932 | DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); |
| 933 | UnfilledBitsInLastByte = DataSize - NewSizeInBits; |
| 934 | } |
| 935 | |
| 936 | // Place this field at the current location. |
| 937 | FieldOffsets.push_back(FieldOffset); |
| 938 | |
| 939 | // Update the size. |
| 940 | Size = std::max(Size, DataSize); |
| 941 | |
| 942 | // Remember max struct/class alignment. |
| 943 | UpdateAlignment(TypeAlign); |
| 944 | } |
| 945 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 946 | void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 947 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 948 | uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte); |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 949 | uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 950 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 951 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 952 | uint64_t TypeSize = FieldInfo.first; |
| 953 | unsigned FieldAlign = FieldInfo.second; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 954 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 955 | if (FieldSize > TypeSize) { |
| 956 | LayoutWideBitField(FieldSize, TypeSize); |
| 957 | return; |
| 958 | } |
| 959 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 960 | if (FieldPacked || !Context.Target.useBitFieldTypeAlignment()) |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 961 | FieldAlign = 1; |
| 962 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 963 | FieldAlign = std::max(FieldAlign, AA->getMaxAlignment()); |
| 964 | |
| 965 | // The maximum field alignment overrides the aligned attribute. |
| 966 | if (MaxFieldAlignment) |
| 967 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 968 | |
Daniel Dunbar | b6a1693 | 2010-04-15 06:18:39 +0000 | [diff] [blame] | 969 | // Check if we need to add padding to give the field the correct alignment. |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 970 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 971 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 972 | |
Daniel Dunbar | b6a1693 | 2010-04-15 06:18:39 +0000 | [diff] [blame] | 973 | // Padding members don't affect overall alignment. |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 974 | if (!D->getIdentifier()) |
| 975 | FieldAlign = 1; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 976 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 977 | // Place this field at the current location. |
| 978 | FieldOffsets.push_back(FieldOffset); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 979 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 980 | // Update DataSize to include the last byte containing (part of) the bitfield. |
| 981 | if (IsUnion) { |
| 982 | // FIXME: I think FieldSize should be TypeSize here. |
| 983 | DataSize = std::max(DataSize, FieldSize); |
| 984 | } else { |
| 985 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 986 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 987 | DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); |
| 988 | UnfilledBitsInLastByte = DataSize - NewSizeInBits; |
| 989 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 990 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 991 | // Update the size. |
| 992 | Size = std::max(Size, DataSize); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 993 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 994 | // Remember max struct/class alignment. |
| 995 | UpdateAlignment(FieldAlign); |
| 996 | } |
| 997 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 998 | void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 999 | if (D->isBitField()) { |
| 1000 | LayoutBitField(D); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1004 | // Reset the unfilled bits. |
| 1005 | UnfilledBitsInLastByte = 0; |
| 1006 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1007 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 1008 | uint64_t FieldOffset = IsUnion ? 0 : DataSize; |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1009 | uint64_t FieldSize; |
| 1010 | unsigned FieldAlign; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1011 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1012 | if (D->getType()->isIncompleteArrayType()) { |
| 1013 | // This is a flexible array member; we can't directly |
| 1014 | // query getTypeInfo about these, so we figure it out here. |
| 1015 | // Flexible array members don't have any size, but they |
| 1016 | // have to be aligned appropriately for their element type. |
| 1017 | FieldSize = 0; |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1018 | const ArrayType* ATy = Context.getAsArrayType(D->getType()); |
| 1019 | FieldAlign = Context.getTypeAlign(ATy->getElementType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1020 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 1021 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1022 | FieldSize = Context.Target.getPointerWidth(AS); |
| 1023 | FieldAlign = Context.Target.getPointerAlign(AS); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1024 | } else { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1025 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1026 | FieldSize = FieldInfo.first; |
| 1027 | FieldAlign = FieldInfo.second; |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1028 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1030 | if (FieldPacked) |
| 1031 | FieldAlign = 8; |
| 1032 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 1033 | FieldAlign = std::max(FieldAlign, AA->getMaxAlignment()); |
| 1034 | |
| 1035 | // The maximum field alignment overrides the aligned attribute. |
| 1036 | if (MaxFieldAlignment) |
| 1037 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 1038 | |
| 1039 | // Round up the current record size to the field's alignment boundary. |
| 1040 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1041 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1042 | if (!IsUnion) { |
| 1043 | while (true) { |
| 1044 | // Check if we can place the field at this offset. |
| 1045 | if (canPlaceFieldAtOffset(D, FieldOffset)) |
| 1046 | break; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1047 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1048 | // We couldn't place the field at the offset. Try again at a new offset. |
| 1049 | FieldOffset += FieldAlign; |
| 1050 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1051 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1052 | UpdateEmptyClassOffsets(D, FieldOffset); |
| 1053 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1054 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1055 | // Place this field at the current location. |
| 1056 | FieldOffsets.push_back(FieldOffset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1058 | // Reserve space for this field. |
| 1059 | if (IsUnion) |
| 1060 | Size = std::max(Size, FieldSize); |
| 1061 | else |
| 1062 | Size = FieldOffset + FieldSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 1064 | // Update the data size. |
| 1065 | DataSize = Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1067 | // Remember max struct/class alignment. |
| 1068 | UpdateAlignment(FieldAlign); |
| 1069 | } |
| 1070 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1071 | void RecordLayoutBuilder::FinishLayout() { |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1072 | // In C++, records cannot be of size 0. |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1073 | if (Context.getLangOptions().CPlusPlus && Size == 0) |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1074 | Size = 8; |
| 1075 | // Finally, round the size of the record up to the alignment of the |
| 1076 | // record itself. |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1077 | Size = llvm::RoundUpToAlignment(Size, Alignment); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1080 | void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) { |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1081 | if (NewAlignment <= Alignment) |
| 1082 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1084 | assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1086 | Alignment = NewAlignment; |
| 1087 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1089 | const CXXMethodDecl * |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1090 | RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1091 | assert(RD->isDynamicClass() && "Class does not have any virtual methods!"); |
| 1092 | |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1093 | // If a class isn't polymorphic it doesn't have a key function. |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1094 | if (!RD->isPolymorphic()) |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1095 | return 0; |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1096 | |
| 1097 | // A class inside an anonymous namespace doesn't have a key function. (Or |
| 1098 | // at least, there's no point to assigning a key function to such a class; |
| 1099 | // this doesn't affect the ABI.) |
| 1100 | if (RD->isInAnonymousNamespace()) |
| 1101 | return 0; |
| 1102 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1103 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 1104 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1105 | const CXXMethodDecl *MD = *I; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1106 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1107 | if (!MD->isVirtual()) |
| 1108 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1109 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1110 | if (MD->isPure()) |
| 1111 | continue; |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1112 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1113 | // Ignore implicit member functions, they are always marked as inline, but |
| 1114 | // they don't have a body until they're defined. |
| 1115 | if (MD->isImplicit()) |
| 1116 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 1118 | if (MD->isInlineSpecified()) |
| 1119 | continue; |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1120 | |
| 1121 | if (MD->hasInlineBody()) |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1122 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1123 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1124 | // We found it. |
| 1125 | return MD; |
| 1126 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1127 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1128 | return 0; |
| 1129 | } |
| 1130 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1131 | /// getASTRecordLayout - Get or compute information about the layout of the |
| 1132 | /// specified record (struct/union/class), which indicates its size and field |
| 1133 | /// position information. |
| 1134 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
| 1135 | D = D->getDefinition(); |
| 1136 | assert(D && "Cannot get layout of forward declarations!"); |
| 1137 | |
| 1138 | // Look up this layout, if already laid out, return what we have. |
| 1139 | // Note that we can't save a reference to the entry because this function |
| 1140 | // is recursive. |
| 1141 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
| 1142 | if (Entry) return *Entry; |
| 1143 | |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1144 | const ASTRecordLayout *NewEntry; |
| 1145 | |
| 1146 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1147 | EmptySubobjectMap EmptySubobjects(*this, RD); |
| 1148 | RecordLayoutBuilder Builder(*this, &EmptySubobjects); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1149 | Builder.Layout(RD); |
| 1150 | |
| 1151 | // FIXME: This is not always correct. See the part about bitfields at |
| 1152 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 1153 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
| 1154 | bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD(); |
| 1155 | |
| 1156 | // FIXME: This should be done in FinalizeLayout. |
| 1157 | uint64_t DataSize = |
| 1158 | IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize; |
| 1159 | uint64_t NonVirtualSize = |
| 1160 | IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; |
| 1161 | |
| 1162 | NewEntry = |
| 1163 | new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, |
| 1164 | DataSize, Builder.FieldOffsets.data(), |
| 1165 | Builder.FieldOffsets.size(), |
| 1166 | NonVirtualSize, |
| 1167 | Builder.NonVirtualAlignment, |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1168 | EmptySubobjects.SizeOfLargestEmptySubobject, |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1169 | Builder.PrimaryBase, |
Anders Carlsson | 4831710 | 2010-05-26 05:25:15 +0000 | [diff] [blame] | 1170 | Builder.PrimaryBaseIsVirtual, |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1171 | Builder.Bases, Builder.VBases); |
| 1172 | } else { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1173 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1174 | Builder.Layout(D); |
| 1175 | |
| 1176 | NewEntry = |
| 1177 | new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, |
| 1178 | Builder.Size, |
| 1179 | Builder.FieldOffsets.data(), |
| 1180 | Builder.FieldOffsets.size()); |
| 1181 | } |
| 1182 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1183 | ASTRecordLayouts[D] = NewEntry; |
| 1184 | |
| 1185 | if (getLangOptions().DumpRecordLayouts) { |
| 1186 | llvm::errs() << "\n*** Dumping AST Record Layout\n"; |
| 1187 | DumpRecordLayout(D, llvm::errs()); |
| 1188 | } |
| 1189 | |
| 1190 | return *NewEntry; |
| 1191 | } |
| 1192 | |
| 1193 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
| 1194 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
| 1195 | assert(RD && "Cannot get key function for forward declarations!"); |
| 1196 | |
| 1197 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
| 1198 | if (!Entry) |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1199 | Entry = RecordLayoutBuilder::ComputeKeyFunction(RD); |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1200 | else |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1201 | assert(Entry == RecordLayoutBuilder::ComputeKeyFunction(RD) && |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1202 | "Key function changed!"); |
| 1203 | |
| 1204 | return Entry; |
| 1205 | } |
| 1206 | |
| 1207 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 1208 | /// layout of the given interface. |
| 1209 | /// |
| 1210 | /// \param Impl - If given, also include the layout of the interface's |
| 1211 | /// implementation. This may differ by including synthesized ivars. |
| 1212 | const ASTRecordLayout & |
| 1213 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 1214 | const ObjCImplementationDecl *Impl) { |
| 1215 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 1216 | |
| 1217 | // Look up this layout, if already laid out, return what we have. |
| 1218 | ObjCContainerDecl *Key = |
| 1219 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 1220 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 1221 | return *Entry; |
| 1222 | |
| 1223 | // Add in synthesized ivar count if laying out an implementation. |
| 1224 | if (Impl) { |
| 1225 | unsigned SynthCount = CountNonClassIvars(D); |
| 1226 | // If there aren't any sythesized ivars then reuse the interface |
| 1227 | // entry. Note we can't cache this because we simply free all |
| 1228 | // entries later; however we shouldn't look up implementations |
| 1229 | // frequently. |
| 1230 | if (SynthCount == 0) |
| 1231 | return getObjCLayout(D, 0); |
| 1232 | } |
| 1233 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1234 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 36cdc61 | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 1235 | Builder.Layout(D); |
| 1236 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1237 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 36cdc61 | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 1238 | new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, |
| 1239 | Builder.DataSize, |
| 1240 | Builder.FieldOffsets.data(), |
| 1241 | Builder.FieldOffsets.size()); |
| 1242 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1243 | ObjCLayouts[Key] = NewEntry; |
| 1244 | |
| 1245 | return *NewEntry; |
| 1246 | } |
| 1247 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1248 | static void PrintOffset(llvm::raw_ostream &OS, |
| 1249 | uint64_t Offset, unsigned IndentLevel) { |
| 1250 | OS << llvm::format("%4d | ", Offset); |
| 1251 | OS.indent(IndentLevel * 2); |
| 1252 | } |
| 1253 | |
| 1254 | static void DumpCXXRecordLayout(llvm::raw_ostream &OS, |
| 1255 | const CXXRecordDecl *RD, ASTContext &C, |
| 1256 | uint64_t Offset, |
| 1257 | unsigned IndentLevel, |
| 1258 | const char* Description, |
| 1259 | bool IncludeVirtualBases) { |
| 1260 | const ASTRecordLayout &Info = C.getASTRecordLayout(RD); |
| 1261 | |
| 1262 | PrintOffset(OS, Offset, IndentLevel); |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 1263 | OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1264 | if (Description) |
| 1265 | OS << ' ' << Description; |
| 1266 | if (RD->isEmpty()) |
| 1267 | OS << " (empty)"; |
| 1268 | OS << '\n'; |
| 1269 | |
| 1270 | IndentLevel++; |
| 1271 | |
| 1272 | const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase(); |
| 1273 | |
| 1274 | // Vtable pointer. |
| 1275 | if (RD->isDynamicClass() && !PrimaryBase) { |
| 1276 | PrintOffset(OS, Offset, IndentLevel); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1277 | OS << '(' << RD << " vtable pointer)\n"; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1278 | } |
| 1279 | // Dump (non-virtual) bases |
| 1280 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1281 | E = RD->bases_end(); I != E; ++I) { |
| 1282 | assert(!I->getType()->isDependentType() && |
| 1283 | "Cannot layout class with dependent bases."); |
| 1284 | if (I->isVirtual()) |
| 1285 | continue; |
| 1286 | |
| 1287 | const CXXRecordDecl *Base = |
| 1288 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1289 | |
| 1290 | uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8; |
| 1291 | |
| 1292 | DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
| 1293 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 1294 | /*IncludeVirtualBases=*/false); |
| 1295 | } |
| 1296 | |
| 1297 | // Dump fields. |
| 1298 | uint64_t FieldNo = 0; |
| 1299 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1300 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
| 1301 | const FieldDecl *Field = *I; |
| 1302 | uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8; |
| 1303 | |
| 1304 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
| 1305 | if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 1306 | DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel, |
| 1307 | Field->getNameAsCString(), |
| 1308 | /*IncludeVirtualBases=*/true); |
| 1309 | continue; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | PrintOffset(OS, FieldOffset, IndentLevel); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1314 | OS << Field->getType().getAsString() << ' ' << Field << '\n'; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | if (!IncludeVirtualBases) |
| 1318 | return; |
| 1319 | |
| 1320 | // Dump virtual bases. |
| 1321 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1322 | E = RD->vbases_end(); I != E; ++I) { |
| 1323 | assert(I->isVirtual() && "Found non-virtual class!"); |
| 1324 | const CXXRecordDecl *VBase = |
| 1325 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1326 | |
| 1327 | uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8; |
| 1328 | DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
| 1329 | VBase == PrimaryBase ? |
| 1330 | "(primary virtual base)" : "(virtual base)", |
| 1331 | /*IncludeVirtualBases=*/false); |
| 1332 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1333 | |
| 1334 | OS << " sizeof=" << Info.getSize() / 8; |
| 1335 | OS << ", dsize=" << Info.getDataSize() / 8; |
| 1336 | OS << ", align=" << Info.getAlignment() / 8 << '\n'; |
| 1337 | OS << " nvsize=" << Info.getNonVirtualSize() / 8; |
| 1338 | OS << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n'; |
| 1339 | OS << '\n'; |
| 1340 | } |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1341 | |
| 1342 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, |
| 1343 | llvm::raw_ostream &OS) { |
| 1344 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 1345 | |
| 1346 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1347 | return DumpCXXRecordLayout(OS, CXXRD, *this, 0, 0, 0, |
| 1348 | /*IncludeVirtualBases=*/true); |
| 1349 | |
| 1350 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
| 1351 | OS << "Record: "; |
| 1352 | RD->dump(); |
| 1353 | OS << "\nLayout: "; |
| 1354 | OS << "<ASTRecordLayout\n"; |
| 1355 | OS << " Size:" << Info.getSize() << "\n"; |
| 1356 | OS << " DataSize:" << Info.getDataSize() << "\n"; |
| 1357 | OS << " Alignment:" << Info.getAlignment() << "\n"; |
| 1358 | OS << " FieldOffsets: ["; |
| 1359 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
| 1360 | if (i) OS << ", "; |
| 1361 | OS << Info.getFieldOffset(i); |
| 1362 | } |
| 1363 | OS << "]>\n"; |
| 1364 | } |