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