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