Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 1 | //=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==// |
Anders Carlsson | 7947433 | 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 | |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 10 | #include "clang/AST/RecordLayout.h" |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 12 | #include "clang/AST/Attr.h" |
Anders Carlsson | 5adde29 | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 13 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 14 | #include "clang/AST/Decl.h" |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 19 | #include "clang/Sema/SemaDiagnostic.h" |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Ted Kremenek | f75d089 | 2011-03-19 01:00:36 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
| 23 | #include "llvm/Support/MathExtras.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
Benjamin Kramer | c7656cd | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 27 | namespace { |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 28 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 29 | /// BaseSubobjectInfo - Represents a single base subobject in a complete class. |
| 30 | /// For a class hierarchy like |
| 31 | /// |
| 32 | /// class A { }; |
| 33 | /// class B : A { }; |
| 34 | /// class C : A, B { }; |
| 35 | /// |
| 36 | /// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo |
| 37 | /// instances, one for B and two for A. |
| 38 | /// |
| 39 | /// If a base is virtual, it will only have one BaseSubobjectInfo allocated. |
| 40 | struct BaseSubobjectInfo { |
| 41 | /// Class - The class for this base info. |
Anders Carlsson | 056818f | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 42 | const CXXRecordDecl *Class; |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 43 | |
| 44 | /// IsVirtual - Whether the BaseInfo represents a virtual base or not. |
Anders Carlsson | 056818f | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 45 | bool IsVirtual; |
| 46 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 47 | /// Bases - Information about the base subobjects. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 48 | SmallVector<BaseSubobjectInfo*, 4> Bases; |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 49 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 50 | /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base |
| 51 | /// of this base info (if one exists). |
| 52 | BaseSubobjectInfo *PrimaryVirtualBaseInfo; |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 53 | |
| 54 | // FIXME: Document. |
| 55 | const BaseSubobjectInfo *Derived; |
Anders Carlsson | 056818f | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 58 | /// \brief Externally provided layout. Typically used when the AST source, such |
| 59 | /// as DWARF, lacks all the information that was available at compile time, such |
| 60 | /// as alignment attributes on fields and pragmas in effect. |
| 61 | struct ExternalLayout { |
| 62 | ExternalLayout() : Size(0), Align(0) {} |
| 63 | |
| 64 | /// \brief Overall record size in bits. |
| 65 | uint64_t Size; |
| 66 | |
| 67 | /// \brief Overall record alignment in bits. |
| 68 | uint64_t Align; |
| 69 | |
| 70 | /// \brief Record field offsets in bits. |
| 71 | llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsets; |
| 72 | |
| 73 | /// \brief Direct, non-virtual base offsets. |
| 74 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsets; |
| 75 | |
| 76 | /// \brief Virtual base offsets. |
| 77 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> VirtualBaseOffsets; |
| 78 | |
| 79 | /// Get the offset of the given field. The external source must provide |
| 80 | /// entries for all fields in the record. |
| 81 | uint64_t getExternalFieldOffset(const FieldDecl *FD) { |
| 82 | assert(FieldOffsets.count(FD) && |
| 83 | "Field does not have an external offset"); |
| 84 | return FieldOffsets[FD]; |
| 85 | } |
| 86 | |
| 87 | bool getExternalNVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) { |
| 88 | auto Known = BaseOffsets.find(RD); |
| 89 | if (Known == BaseOffsets.end()) |
| 90 | return false; |
| 91 | BaseOffset = Known->second; |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | bool getExternalVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) { |
| 96 | auto Known = VirtualBaseOffsets.find(RD); |
| 97 | if (Known == VirtualBaseOffsets.end()) |
| 98 | return false; |
| 99 | BaseOffset = Known->second; |
| 100 | return true; |
| 101 | } |
| 102 | }; |
| 103 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 104 | /// EmptySubobjectMap - Keeps track of which empty subobjects exist at different |
| 105 | /// offsets while laying out a C++ class. |
| 106 | class EmptySubobjectMap { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 107 | const ASTContext &Context; |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 108 | uint64_t CharWidth; |
| 109 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 110 | /// Class - The class whose empty entries we're keeping track of. |
| 111 | const CXXRecordDecl *Class; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 112 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 113 | /// EmptyClassOffsets - A map from offsets to empty record decls. |
Benjamin Kramer | 834652a | 2014-05-03 18:44:26 +0000 | [diff] [blame] | 114 | typedef llvm::TinyPtrVector<const CXXRecordDecl *> ClassVectorTy; |
Anders Carlsson | f8f756d | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 115 | typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 116 | EmptyClassOffsetsMapTy EmptyClassOffsets; |
| 117 | |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 118 | /// MaxEmptyClassOffset - The highest offset known to contain an empty |
| 119 | /// base subobject. |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 120 | CharUnits MaxEmptyClassOffset; |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 122 | /// ComputeEmptySubobjectSizes - Compute the size of the largest base or |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 123 | /// member subobject that is empty. |
| 124 | void ComputeEmptySubobjectSizes(); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 125 | |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 126 | void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 127 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 128 | void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 129 | CharUnits Offset, bool PlacingEmptyBase); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 130 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 131 | void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
| 132 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 133 | CharUnits Offset); |
| 134 | void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 136 | /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty |
| 137 | /// subobjects beyond the given offset. |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 138 | bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const { |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 139 | return Offset <= MaxEmptyClassOffset; |
| 140 | } |
| 141 | |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 142 | CharUnits |
| 143 | getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const { |
| 144 | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); |
| 145 | assert(FieldOffset % CharWidth == 0 && |
| 146 | "Field offset not at char boundary!"); |
| 147 | |
Ken Dyck | 7c4026b | 2011-01-24 01:28:50 +0000 | [diff] [blame] | 148 | return Context.toCharUnitsFromBits(FieldOffset); |
Anders Carlsson | f8f756d | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 149 | } |
Anders Carlsson | f8f756d | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 150 | |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 151 | protected: |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 152 | bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
| 153 | CharUnits Offset) const; |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 154 | |
| 155 | bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 156 | CharUnits Offset); |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 157 | |
| 158 | bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 159 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 160 | CharUnits Offset) const; |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 161 | bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 162 | CharUnits Offset) const; |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 164 | public: |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 165 | /// This holds the size of the largest empty subobject (either a base |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 166 | /// or a member). Will be zero if the record being built doesn't contain |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 167 | /// any empty classes. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 168 | CharUnits SizeOfLargestEmptySubobject; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 169 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 170 | EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class) |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 171 | : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 172 | ComputeEmptySubobjectSizes(); |
| 173 | } |
| 174 | |
| 175 | /// CanPlaceBaseAtOffset - Return whether the given base class can be placed |
| 176 | /// at the given offset. |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 177 | /// Returns false if placing the record will result in two components |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 178 | /// (direct or indirect) of the same type having the same offset. |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 179 | bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 180 | CharUnits Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 181 | |
| 182 | /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given |
| 183 | /// offset. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 184 | bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 185 | }; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 186 | |
| 187 | void EmptySubobjectMap::ComputeEmptySubobjectSizes() { |
| 188 | // Check the bases. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 189 | for (const CXXBaseSpecifier &Base : Class->bases()) { |
| 190 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 191 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 192 | CharUnits EmptySize; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 193 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 194 | if (BaseDecl->isEmpty()) { |
| 195 | // If the class decl is empty, get its size. |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 196 | EmptySize = Layout.getSize(); |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 197 | } else { |
| 198 | // Otherwise, we get the largest empty subobject for the decl. |
| 199 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 200 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 201 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 202 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 203 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 204 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 205 | |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 206 | // Check the fields. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 207 | for (const FieldDecl *FD : Class->fields()) { |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 208 | const RecordType *RT = |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 209 | Context.getBaseElementType(FD->getType())->getAs<RecordType>(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 210 | |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 211 | // We only care about record types. |
| 212 | if (!RT) |
| 213 | continue; |
| 214 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 215 | CharUnits EmptySize; |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 216 | const CXXRecordDecl *MemberDecl = RT->getAsCXXRecordDecl(); |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 217 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl); |
| 218 | if (MemberDecl->isEmpty()) { |
| 219 | // If the class decl is empty, get its size. |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 220 | EmptySize = Layout.getSize(); |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 221 | } else { |
| 222 | // Otherwise, we get the largest empty subobject for the decl. |
| 223 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 224 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 225 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 226 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 227 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 231 | bool |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 232 | EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 233 | CharUnits Offset) const { |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 234 | // We only need to check empty bases. |
| 235 | if (!RD->isEmpty()) |
| 236 | return true; |
| 237 | |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 238 | EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 239 | if (I == EmptyClassOffsets.end()) |
| 240 | return true; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 241 | |
| 242 | const ClassVectorTy &Classes = I->second; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 243 | if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end()) |
| 244 | return true; |
| 245 | |
| 246 | // There is already an empty class of the same type at this offset. |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 251 | CharUnits Offset) { |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 252 | // We only care about empty bases. |
| 253 | if (!RD->isEmpty()) |
| 254 | return; |
| 255 | |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 256 | // If we have empty structures inside a union, we can assign both |
Rafael Espindola | 7bcde19 | 2010-12-29 23:02:58 +0000 | [diff] [blame] | 257 | // the same offset. Just avoid pushing them twice in the list. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 258 | ClassVectorTy &Classes = EmptyClassOffsets[Offset]; |
Rafael Espindola | 7bcde19 | 2010-12-29 23:02:58 +0000 | [diff] [blame] | 259 | if (std::find(Classes.begin(), Classes.end(), RD) != Classes.end()) |
| 260 | return; |
| 261 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 262 | Classes.push_back(RD); |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 263 | |
| 264 | // Update the empty class offset. |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 265 | if (Offset > MaxEmptyClassOffset) |
| 266 | MaxEmptyClassOffset = Offset; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | bool |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 270 | EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
| 271 | CharUnits Offset) { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 272 | // We don't have to keep looking past the maximum offset that's known to |
| 273 | // contain an empty class. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 274 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 275 | return true; |
| 276 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 277 | if (!CanPlaceSubobjectAtOffset(Info->Class, Offset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 278 | return false; |
| 279 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 280 | // Traverse all non-virtual bases. |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 281 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 282 | for (const BaseSubobjectInfo *Base : Info->Bases) { |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 283 | if (Base->IsVirtual) |
| 284 | continue; |
| 285 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 286 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 287 | |
| 288 | if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset)) |
| 289 | return false; |
| 290 | } |
| 291 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 292 | if (Info->PrimaryVirtualBaseInfo) { |
| 293 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 294 | |
| 295 | if (Info == PrimaryVirtualBaseInfo->Derived) { |
| 296 | if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset)) |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 301 | // Traverse all member variables. |
| 302 | unsigned FieldNo = 0; |
| 303 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
| 304 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 305 | if (I->isBitField()) |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 306 | continue; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 307 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 308 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 309 | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 310 | return false; |
| 311 | } |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 312 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 313 | return true; |
| 314 | } |
| 315 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 316 | void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 317 | CharUnits Offset, |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 318 | bool PlacingEmptyBase) { |
| 319 | if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) { |
| 320 | // We know that the only empty subobjects that can conflict with empty |
| 321 | // subobject of non-empty bases, are empty bases that can be placed at |
| 322 | // offset zero. Because of this, we only need to keep track of empty base |
| 323 | // subobjects with offsets less than the size of the largest empty |
| 324 | // subobject for our class. |
| 325 | return; |
| 326 | } |
| 327 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 328 | AddSubobjectAtOffset(Info->Class, Offset); |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 329 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 330 | // Traverse all non-virtual bases. |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 331 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 332 | for (const BaseSubobjectInfo *Base : Info->Bases) { |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 333 | if (Base->IsVirtual) |
| 334 | continue; |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 335 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 336 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 337 | UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 340 | if (Info->PrimaryVirtualBaseInfo) { |
| 341 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 342 | |
| 343 | if (Info == PrimaryVirtualBaseInfo->Derived) |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 344 | UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset, |
| 345 | PlacingEmptyBase); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 346 | } |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 347 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 348 | // Traverse all member variables. |
| 349 | unsigned FieldNo = 0; |
| 350 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
| 351 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 352 | if (I->isBitField()) |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 353 | continue; |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 354 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 355 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 356 | UpdateEmptyFieldSubobjects(*I, FieldOffset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 357 | } |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Anders Carlsson | a60b86a | 2010-05-29 20:49:49 +0000 | [diff] [blame] | 360 | bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 361 | CharUnits Offset) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 362 | // If we know this class doesn't have any empty subobjects we don't need to |
| 363 | // bother checking. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 364 | if (SizeOfLargestEmptySubobject.isZero()) |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 365 | return true; |
| 366 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 367 | if (!CanPlaceBaseSubobjectAtOffset(Info, Offset)) |
| 368 | return false; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 369 | |
| 370 | // We are able to place the base at this offset. Make sure to update the |
| 371 | // empty base subobject map. |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 372 | UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty()); |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 373 | return true; |
| 374 | } |
| 375 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 376 | bool |
| 377 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 378 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 379 | CharUnits Offset) const { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 380 | // We don't have to keep looking past the maximum offset that's known to |
| 381 | // contain an empty class. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 382 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 383 | return true; |
| 384 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 385 | if (!CanPlaceSubobjectAtOffset(RD, Offset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 386 | return false; |
| 387 | |
| 388 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 389 | |
| 390 | // Traverse all non-virtual bases. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 391 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 392 | if (Base.isVirtual()) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 393 | continue; |
| 394 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 395 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 396 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 397 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 398 | if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset)) |
| 399 | return false; |
| 400 | } |
| 401 | |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 402 | if (RD == Class) { |
| 403 | // This is the most derived class, traverse virtual bases as well. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 404 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
| 405 | const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 406 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 407 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 408 | if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset)) |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 413 | // Traverse all member variables. |
| 414 | unsigned FieldNo = 0; |
| 415 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 416 | I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 417 | if (I->isBitField()) |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 418 | continue; |
| 419 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 420 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 421 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 422 | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 429 | bool |
| 430 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
| 431 | CharUnits Offset) const { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 432 | // We don't have to keep looking past the maximum offset that's known to |
| 433 | // contain an empty class. |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 434 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 435 | return true; |
| 436 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 437 | QualType T = FD->getType(); |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 438 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 439 | return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 440 | |
| 441 | // If we have an array type we need to look at every element. |
| 442 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 443 | QualType ElemTy = Context.getBaseElementType(AT); |
| 444 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 445 | if (!RT) |
| 446 | return true; |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 447 | |
| 448 | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 449 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 450 | |
| 451 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 452 | CharUnits ElementOffset = Offset; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 453 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 454 | // We don't have to keep looking past the maximum offset that's known to |
| 455 | // contain an empty class. |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 456 | if (!AnyEmptySubobjectsBeyondOffset(ElementOffset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 457 | return true; |
| 458 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 459 | if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 460 | return false; |
| 461 | |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 462 | ElementOffset += Layout.getSize(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | bool |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 470 | EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD, |
| 471 | CharUnits Offset) { |
| 472 | if (!CanPlaceFieldSubobjectAtOffset(FD, Offset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 473 | return false; |
| 474 | |
| 475 | // We are able to place the member variable at this offset. |
| 476 | // Make sure to update the empty base subobject map. |
| 477 | UpdateEmptyFieldSubobjects(FD, Offset); |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
| 482 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 483 | CharUnits Offset) { |
Anders Carlsson | ae111dc | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 484 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 485 | // field subobjects are subobjects of empty bases that can be placed at offset |
Anders Carlsson | ae111dc | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 486 | // zero. Because of this, we only need to keep track of empty field |
| 487 | // subobjects with offsets less than the size of the largest empty |
| 488 | // subobject for our class. |
| 489 | if (Offset >= SizeOfLargestEmptySubobject) |
| 490 | return; |
| 491 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 492 | AddSubobjectAtOffset(RD, Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 493 | |
| 494 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 495 | |
| 496 | // Traverse all non-virtual bases. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 497 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 498 | if (Base.isVirtual()) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 499 | continue; |
| 500 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 501 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 502 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 503 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 504 | UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset); |
| 505 | } |
| 506 | |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 507 | if (RD == Class) { |
| 508 | // This is the most derived class, traverse virtual bases as well. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 509 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
| 510 | const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 511 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 512 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 513 | UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset); |
| 514 | } |
| 515 | } |
| 516 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 517 | // Traverse all member variables. |
| 518 | unsigned FieldNo = 0; |
| 519 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 520 | I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 521 | if (I->isBitField()) |
Anders Carlsson | 09814d3 | 2010-11-01 15:14:51 +0000 | [diff] [blame] | 522 | continue; |
| 523 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 524 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 525 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 526 | UpdateEmptyFieldSubobjects(*I, FieldOffset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 531 | CharUnits Offset) { |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 532 | QualType T = FD->getType(); |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 533 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 534 | UpdateEmptyFieldSubobjects(RD, RD, Offset); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | // If we have an array type we need to update every element. |
| 539 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 540 | QualType ElemTy = Context.getBaseElementType(AT); |
| 541 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 542 | if (!RT) |
| 543 | return; |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 544 | |
| 545 | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 546 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 547 | |
| 548 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 549 | CharUnits ElementOffset = Offset; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 550 | |
| 551 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | ae111dc | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 552 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 553 | // field subobjects are subobjects of empty bases that can be placed at |
Anders Carlsson | ae111dc | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 554 | // offset zero. Because of this, we only need to keep track of empty field |
| 555 | // subobjects with offsets less than the size of the largest empty |
| 556 | // subobject for our class. |
| 557 | if (ElementOffset >= SizeOfLargestEmptySubobject) |
| 558 | return; |
| 559 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 560 | UpdateEmptyFieldSubobjects(RD, RD, ElementOffset); |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 561 | ElementOffset += Layout.getSize(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 566 | typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy; |
| 567 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 568 | class RecordLayoutBuilder { |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 569 | protected: |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 570 | // FIXME: Remove this and make the appropriate fields public. |
| 571 | friend class clang::ASTContext; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 572 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 573 | const ASTContext &Context; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 574 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 575 | EmptySubobjectMap *EmptySubobjects; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 576 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 577 | /// Size - The current size of the record layout. |
| 578 | uint64_t Size; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 579 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 580 | /// Alignment - The current alignment of the record layout. |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 581 | CharUnits Alignment; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 582 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 583 | /// \brief The alignment if attribute packed is not used. |
Ken Dyck | 1300b3b | 2011-02-16 02:11:31 +0000 | [diff] [blame] | 584 | CharUnits UnpackedAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 586 | SmallVector<uint64_t, 16> FieldOffsets; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 587 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 588 | /// \brief Whether the external AST source has provided a layout for this |
| 589 | /// record. |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 590 | unsigned UseExternalLayout : 1; |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 591 | |
| 592 | /// \brief Whether we need to infer alignment, even when we have an |
| 593 | /// externally-provided layout. |
| 594 | unsigned InferAlignment : 1; |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 595 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 596 | /// Packed - Whether the record is packed or not. |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 597 | unsigned Packed : 1; |
| 598 | |
| 599 | unsigned IsUnion : 1; |
| 600 | |
| 601 | unsigned IsMac68kAlign : 1; |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 602 | |
| 603 | unsigned IsMsStruct : 1; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 604 | |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 605 | /// UnfilledBitsInLastUnit - If the last field laid out was a bitfield, |
| 606 | /// this contains the number of bits in the last unit that can be used for |
| 607 | /// an adjacent bitfield if necessary. The unit in question is usually |
| 608 | /// a byte, but larger units are used if IsMsStruct. |
| 609 | unsigned char UnfilledBitsInLastUnit; |
| 610 | /// LastBitfieldTypeSize - If IsMsStruct, represents the size of the type |
| 611 | /// of the previous field if it was a bitfield. |
| 612 | unsigned char LastBitfieldTypeSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 614 | /// MaxFieldAlignment - The maximum allowed field alignment. This is set by |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 615 | /// #pragma pack. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 616 | CharUnits MaxFieldAlignment; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 618 | /// DataSize - The data size of the record being laid out. |
| 619 | uint64_t DataSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 620 | |
Ken Dyck | af1c83f | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 621 | CharUnits NonVirtualSize; |
Ken Dyck | a2d3dda | 2011-02-16 01:43:15 +0000 | [diff] [blame] | 622 | CharUnits NonVirtualAlignment; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 623 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 624 | /// PrimaryBase - the primary base class (if one exists) of the class |
| 625 | /// we're laying out. |
| 626 | const CXXRecordDecl *PrimaryBase; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 627 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 628 | /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying |
| 629 | /// out is virtual. |
| 630 | bool PrimaryBaseIsVirtual; |
| 631 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 632 | /// HasOwnVFPtr - Whether the class provides its own vtable/vftbl |
| 633 | /// pointer, as opposed to inheriting one from a primary base class. |
| 634 | bool HasOwnVFPtr; |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 635 | |
Anders Carlsson | 22f5720 | 2010-10-31 21:01:46 +0000 | [diff] [blame] | 636 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 637 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 638 | /// Bases - base classes and their offsets in the record. |
| 639 | BaseOffsetsMapTy Bases; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 640 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 641 | // VBases - virtual base classes and their offsets in the record. |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 642 | ASTRecordLayout::VBaseOffsetsMapTy VBases; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 643 | |
| 644 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 645 | /// primary base classes for some other direct or indirect base class. |
Anders Carlsson | 5adde29 | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 646 | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 647 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 648 | /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in |
| 649 | /// inheritance graph order. Used for determining the primary base class. |
| 650 | const CXXRecordDecl *FirstNearlyEmptyVBase; |
| 651 | |
| 652 | /// VisitedVirtualBases - A set of all the visited virtual bases, used to |
| 653 | /// avoid visiting virtual bases more than once. |
| 654 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 655 | |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 656 | /// Valid if UseExternalLayout is true. |
| 657 | ExternalLayout External; |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 658 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 659 | RecordLayoutBuilder(const ASTContext &Context, |
| 660 | EmptySubobjectMap *EmptySubobjects) |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 661 | : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 662 | Alignment(CharUnits::One()), UnpackedAlignment(CharUnits::One()), |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 663 | UseExternalLayout(false), InferAlignment(false), |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 664 | Packed(false), IsUnion(false), IsMac68kAlign(false), IsMsStruct(false), |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 665 | UnfilledBitsInLastUnit(0), LastBitfieldTypeSize(0), |
| 666 | MaxFieldAlignment(CharUnits::Zero()), |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 667 | DataSize(0), NonVirtualSize(CharUnits::Zero()), |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 668 | NonVirtualAlignment(CharUnits::One()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 669 | PrimaryBase(nullptr), PrimaryBaseIsVirtual(false), |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 670 | HasOwnVFPtr(false), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 671 | FirstNearlyEmptyVBase(nullptr) {} |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 672 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 673 | void Layout(const RecordDecl *D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 674 | void Layout(const CXXRecordDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 675 | void Layout(const ObjCInterfaceDecl *D); |
| 676 | |
| 677 | void LayoutFields(const RecordDecl *D); |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 678 | void LayoutField(const FieldDecl *D, bool InsertExtraPadding); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 679 | void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize, |
| 680 | bool FieldPacked, const FieldDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 681 | void LayoutBitField(const FieldDecl *D); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 682 | |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 683 | TargetCXXABI getCXXABI() const { |
| 684 | return Context.getTargetInfo().getCXXABI(); |
| 685 | } |
| 686 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 687 | /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects. |
| 688 | llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator; |
| 689 | |
| 690 | typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *> |
| 691 | BaseSubobjectInfoMapTy; |
| 692 | |
| 693 | /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases |
| 694 | /// of the class we're laying out to their base subobject info. |
| 695 | BaseSubobjectInfoMapTy VirtualBaseInfo; |
| 696 | |
| 697 | /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the |
| 698 | /// class we're laying out to their base subobject info. |
| 699 | BaseSubobjectInfoMapTy NonVirtualBaseInfo; |
| 700 | |
| 701 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for the |
| 702 | /// bases of the given class. |
| 703 | void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD); |
| 704 | |
| 705 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for a |
| 706 | /// single class and all of its base classes. |
| 707 | BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 708 | bool IsVirtual, |
| 709 | BaseSubobjectInfo *Derived); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 710 | |
| 711 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
| 712 | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
| 713 | |
| 714 | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 715 | |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 716 | void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign); |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 717 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 718 | /// LayoutNonVirtualBases - Determines the primary base class (if any) and |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 719 | /// lays it out. Will then proceed to lay out all non-virtual base clasess. |
| 720 | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
| 721 | |
| 722 | /// LayoutNonVirtualBase - Lays out a single non-virtual base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 723 | void LayoutNonVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 724 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 725 | void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
| 726 | CharUnits Offset); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 727 | |
| 728 | /// LayoutVirtualBases - Lays out all the virtual bases. |
| 729 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 730 | const CXXRecordDecl *MostDerivedClass); |
| 731 | |
| 732 | /// LayoutVirtualBase - Lays out a single virtual base. |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 733 | void LayoutVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 734 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 735 | /// LayoutBase - Will lay out a base and return the offset where it was |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 736 | /// placed, in chars. |
| 737 | CharUnits LayoutBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 738 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 739 | /// InitializeLayout - Initialize record layout for the given record decl. |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 740 | void InitializeLayout(const Decl *D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 741 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 742 | /// FinishLayout - Finalize record layout. Adjust record size based on the |
| 743 | /// alignment. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 744 | void FinishLayout(const NamedDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 745 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 746 | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment); |
| 747 | void UpdateAlignment(CharUnits NewAlignment) { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 748 | UpdateAlignment(NewAlignment, NewAlignment); |
| 749 | } |
| 750 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 751 | /// \brief Retrieve the externally-supplied field offset for the given |
| 752 | /// field. |
| 753 | /// |
| 754 | /// \param Field The field whose offset is being queried. |
| 755 | /// \param ComputedOffset The offset that we've computed for this field. |
| 756 | uint64_t updateExternalFieldOffset(const FieldDecl *Field, |
| 757 | uint64_t ComputedOffset); |
| 758 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 759 | void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset, |
| 760 | uint64_t UnpackedOffset, unsigned UnpackedAlign, |
| 761 | bool isPacked, const FieldDecl *D); |
| 762 | |
| 763 | DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 764 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 765 | CharUnits getSize() const { |
Ken Dyck | 3c215f2 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 766 | assert(Size % Context.getCharWidth() == 0); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 767 | return Context.toCharUnitsFromBits(Size); |
| 768 | } |
| 769 | uint64_t getSizeInBits() const { return Size; } |
| 770 | |
| 771 | void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); } |
| 772 | void setSize(uint64_t NewSize) { Size = NewSize; } |
| 773 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 774 | CharUnits getAligment() const { return Alignment; } |
| 775 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 776 | CharUnits getDataSize() const { |
Ken Dyck | 3c215f2 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 777 | assert(DataSize % Context.getCharWidth() == 0); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 778 | return Context.toCharUnitsFromBits(DataSize); |
| 779 | } |
| 780 | uint64_t getDataSizeInBits() const { return DataSize; } |
| 781 | |
| 782 | void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); } |
| 783 | void setDataSize(uint64_t NewSize) { DataSize = NewSize; } |
| 784 | |
Aaron Ballman | abc1892 | 2015-02-15 22:54:08 +0000 | [diff] [blame] | 785 | RecordLayoutBuilder(const RecordLayoutBuilder &) = delete; |
| 786 | void operator=(const RecordLayoutBuilder &) = delete; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 787 | }; |
Benjamin Kramer | c7656cd | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 788 | } // end anonymous namespace |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 789 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 790 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 791 | RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 792 | for (const auto &I : RD->bases()) { |
| 793 | assert(!I.getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 794 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 795 | |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 796 | const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 797 | |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 798 | // Check if this is a nearly empty virtual base. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 799 | if (I.isVirtual() && Context.isNearlyEmpty(Base)) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 800 | // If it's not an indirect primary base, then we've found our primary |
| 801 | // base. |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 802 | if (!IndirectPrimaryBases.count(Base)) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 803 | PrimaryBase = Base; |
| 804 | PrimaryBaseIsVirtual = true; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 805 | return; |
| 806 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 807 | |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 808 | // Is this the first nearly empty virtual base? |
| 809 | if (!FirstNearlyEmptyVBase) |
| 810 | FirstNearlyEmptyVBase = Base; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 811 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 812 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 813 | SelectPrimaryVBase(Base); |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 814 | if (PrimaryBase) |
Zhongxing Xu | ec345b7 | 2010-02-15 04:28:35 +0000 | [diff] [blame] | 815 | return; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 819 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 820 | void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 821 | // If the class isn't dynamic, it won't have a primary base. |
| 822 | if (!RD->isDynamicClass()) |
| 823 | return; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 824 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 825 | // Compute all the primary virtual bases for all of our direct and |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 826 | // indirect bases, and record all their primary virtual base classes. |
Anders Carlsson | 5adde29 | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 827 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 828 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 829 | // If the record has a dynamic base class, attempt to choose a primary base |
| 830 | // class. It is the first (in direct base class order) non-virtual dynamic |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 831 | // base class, if one exists. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 832 | for (const auto &I : RD->bases()) { |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 833 | // Ignore virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 834 | if (I.isVirtual()) |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 835 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 836 | |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 837 | const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 838 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 839 | if (Base->isDynamicClass()) { |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 840 | // We found it. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 841 | PrimaryBase = Base; |
| 842 | PrimaryBaseIsVirtual = false; |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 843 | return; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 847 | // Under the Itanium ABI, if there is no non-virtual primary base class, |
| 848 | // try to compute the primary virtual base. The primary virtual base is |
| 849 | // the first nearly empty virtual base that is not an indirect primary |
| 850 | // virtual base class, if one exists. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 851 | if (RD->getNumVBases() != 0) { |
| 852 | SelectPrimaryVBase(RD); |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 853 | if (PrimaryBase) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 854 | return; |
| 855 | } |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 856 | |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 857 | // Otherwise, it is the first indirect primary base class, if one exists. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 858 | if (FirstNearlyEmptyVBase) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 859 | PrimaryBase = FirstNearlyEmptyVBase; |
| 860 | PrimaryBaseIsVirtual = true; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 861 | return; |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 862 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 863 | |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 864 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 867 | BaseSubobjectInfo * |
| 868 | RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 869 | bool IsVirtual, |
| 870 | BaseSubobjectInfo *Derived) { |
| 871 | BaseSubobjectInfo *Info; |
| 872 | |
| 873 | if (IsVirtual) { |
| 874 | // Check if we already have info about this virtual base. |
| 875 | BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD]; |
| 876 | if (InfoSlot) { |
| 877 | assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); |
| 878 | return InfoSlot; |
| 879 | } |
| 880 | |
| 881 | // We don't, create it. |
| 882 | InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 883 | Info = InfoSlot; |
| 884 | } else { |
| 885 | Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 886 | } |
| 887 | |
| 888 | Info->Class = RD; |
| 889 | Info->IsVirtual = IsVirtual; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 890 | Info->Derived = nullptr; |
| 891 | Info->PrimaryVirtualBaseInfo = nullptr; |
| 892 | |
| 893 | const CXXRecordDecl *PrimaryVirtualBase = nullptr; |
| 894 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = nullptr; |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 895 | |
| 896 | // Check if this base has a primary virtual base. |
| 897 | if (RD->getNumVBases()) { |
| 898 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 899 | if (Layout.isPrimaryBaseVirtual()) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 900 | // This base does have a primary virtual base. |
| 901 | PrimaryVirtualBase = Layout.getPrimaryBase(); |
| 902 | assert(PrimaryVirtualBase && "Didn't have a primary virtual base!"); |
| 903 | |
| 904 | // Now check if we have base subobject info about this primary base. |
| 905 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 906 | |
| 907 | if (PrimaryVirtualBaseInfo) { |
| 908 | if (PrimaryVirtualBaseInfo->Derived) { |
| 909 | // We did have info about this primary base, and it turns out that it |
| 910 | // has already been claimed as a primary virtual base for another |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 911 | // base. |
| 912 | PrimaryVirtualBase = nullptr; |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 913 | } else { |
| 914 | // We can claim this base as our primary base. |
| 915 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 916 | PrimaryVirtualBaseInfo->Derived = Info; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // Now go through all direct bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 923 | for (const auto &I : RD->bases()) { |
| 924 | bool IsVirtual = I.isVirtual(); |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 925 | |
| 926 | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
| 927 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 928 | Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info)); |
| 929 | } |
| 930 | |
| 931 | if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) { |
| 932 | // Traversing the bases must have created the base info for our primary |
| 933 | // virtual base. |
| 934 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 935 | assert(PrimaryVirtualBaseInfo && |
| 936 | "Did not create a primary virtual base!"); |
| 937 | |
| 938 | // Claim the primary virtual base as our primary virtual base. |
| 939 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 940 | PrimaryVirtualBaseInfo->Derived = Info; |
| 941 | } |
| 942 | |
| 943 | return Info; |
| 944 | } |
| 945 | |
| 946 | void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 947 | for (const auto &I : RD->bases()) { |
| 948 | bool IsVirtual = I.isVirtual(); |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 949 | |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 950 | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
| 951 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 952 | // Compute the base subobject info for this base. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 953 | BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, |
| 954 | nullptr); |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 955 | |
| 956 | if (IsVirtual) { |
| 957 | // ComputeBaseInfo has already added this base for us. |
| 958 | assert(VirtualBaseInfo.count(BaseDecl) && |
| 959 | "Did not add virtual base!"); |
| 960 | } else { |
| 961 | // Add the base info to the map of non-virtual bases. |
| 962 | assert(!NonVirtualBaseInfo.count(BaseDecl) && |
| 963 | "Non-virtual base already exists!"); |
| 964 | NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 969 | void |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 970 | RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) { |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 971 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
| 972 | |
| 973 | // The maximum field alignment overrides base align. |
| 974 | if (!MaxFieldAlignment.isZero()) { |
| 975 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 976 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
| 977 | } |
| 978 | |
| 979 | // Round up the current record size to pointer alignment. |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 980 | setSize(getSize().RoundUpToAlignment(BaseAlign)); |
| 981 | setDataSize(getSize()); |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 982 | |
| 983 | // Update the alignment. |
| 984 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
| 985 | } |
| 986 | |
| 987 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 988 | RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 989 | // Then, determine the primary base class. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 990 | DeterminePrimaryBase(RD); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 991 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 992 | // Compute base subobject info. |
| 993 | ComputeBaseSubobjectInfo(RD); |
| 994 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 995 | // If we have a primary base class, lay it out. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 996 | if (PrimaryBase) { |
| 997 | if (PrimaryBaseIsVirtual) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 998 | // If the primary virtual base was a primary virtual base of some other |
| 999 | // base class we'll have to steal it. |
| 1000 | BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1001 | PrimaryBaseInfo->Derived = nullptr; |
| 1002 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1003 | // We have a virtual primary base, insert it as an indirect primary base. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1004 | IndirectPrimaryBases.insert(PrimaryBase); |
Anders Carlsson | fe90096 | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1005 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1006 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1007 | "vbase already visited!"); |
| 1008 | VisitedVirtualBases.insert(PrimaryBase); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1009 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1010 | LayoutVirtualBase(PrimaryBaseInfo); |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1011 | } else { |
| 1012 | BaseSubobjectInfo *PrimaryBaseInfo = |
| 1013 | NonVirtualBaseInfo.lookup(PrimaryBase); |
| 1014 | assert(PrimaryBaseInfo && |
| 1015 | "Did not find base info for non-virtual primary base!"); |
| 1016 | |
| 1017 | LayoutNonVirtualBase(PrimaryBaseInfo); |
| 1018 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1019 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1020 | // If this class needs a vtable/vf-table and didn't get one from a |
| 1021 | // primary base, add it in now. |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1022 | } else if (RD->isDynamicClass()) { |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1023 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1024 | CharUnits PtrWidth = |
| 1025 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1026 | CharUnits PtrAlign = |
| 1027 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
| 1028 | EnsureVTablePointerAlignment(PtrAlign); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1029 | HasOwnVFPtr = true; |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1030 | setSize(getSize() + PtrWidth); |
| 1031 | setDataSize(getSize()); |
| 1032 | } |
| 1033 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1034 | // Now lay out the non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1035 | for (const auto &I : RD->bases()) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1036 | |
Benjamin Kramer | 273670a | 2013-10-25 07:40:50 +0000 | [diff] [blame] | 1037 | // Ignore virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1038 | if (I.isVirtual()) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1039 | continue; |
| 1040 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1041 | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1042 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1043 | // Skip the primary base, because we've already laid it out. The |
| 1044 | // !PrimaryBaseIsVirtual check is required because we might have a |
| 1045 | // non-virtual base of the same type as a primary virtual base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1046 | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1047 | continue; |
| 1048 | |
| 1049 | // Lay out the base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1050 | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
| 1051 | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
| 1052 | |
| 1053 | LayoutNonVirtualBase(BaseInfo); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1057 | void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1058 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1059 | CharUnits Offset = LayoutBase(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1060 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1061 | // Add its base class offset. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1062 | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1063 | Bases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1064 | |
| 1065 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1066 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1067 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1068 | void |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1069 | RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1070 | CharUnits Offset) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1071 | // This base isn't interesting, it has no virtual bases. |
| 1072 | if (!Info->Class->getNumVBases()) |
| 1073 | return; |
| 1074 | |
| 1075 | // First, check if we have a virtual primary base to add offsets for. |
| 1076 | if (Info->PrimaryVirtualBaseInfo) { |
| 1077 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
| 1078 | "Primary virtual base is not virtual!"); |
| 1079 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
| 1080 | // Add the offset. |
| 1081 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
| 1082 | "primary vbase offset already exists!"); |
| 1083 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1084 | ASTRecordLayout::VBaseInfo(Offset, false))); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1085 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1086 | // Traverse the primary virtual base. |
| 1087 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
| 1088 | } |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1091 | // Now go through all direct non-virtual bases. |
| 1092 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1093 | for (const BaseSubobjectInfo *Base : Info->Bases) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1094 | if (Base->IsVirtual) |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1095 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1096 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 1097 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1098 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1103 | RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1104 | const CXXRecordDecl *MostDerivedClass) { |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1105 | const CXXRecordDecl *PrimaryBase; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1106 | bool PrimaryBaseIsVirtual; |
Anders Carlsson | fe90096 | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1107 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1108 | if (MostDerivedClass == RD) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1109 | PrimaryBase = this->PrimaryBase; |
| 1110 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1111 | } else { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1112 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1113 | PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 1114 | PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1117 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1118 | assert(!Base.getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1119 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1120 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1121 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1122 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1123 | if (Base.isVirtual()) { |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1124 | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) { |
| 1125 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1126 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1127 | // Only lay out the virtual base if it's not an indirect primary base. |
| 1128 | if (!IndirectPrimaryBase) { |
| 1129 | // Only visit virtual bases once. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1130 | if (!VisitedVirtualBases.insert(BaseDecl).second) |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1131 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1132 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1133 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1134 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1135 | LayoutVirtualBase(BaseInfo); |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 1136 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1137 | } |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 1138 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1139 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1140 | if (!BaseDecl->getNumVBases()) { |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1141 | // This base isn't interesting since it doesn't have any virtual bases. |
| 1142 | continue; |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 1143 | } |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1144 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1145 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1149 | void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1150 | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
| 1151 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1152 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1153 | CharUnits Offset = LayoutBase(Base); |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Add its base class offset. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1156 | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1157 | VBases.insert(std::make_pair(Base->Class, |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1158 | ASTRecordLayout::VBaseInfo(Offset, false))); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1159 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1160 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1163 | CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1164 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1166 | |
| 1167 | CharUnits Offset; |
| 1168 | |
| 1169 | // Query the external layout to see if it provides an offset. |
| 1170 | bool HasExternalLayout = false; |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1171 | if (UseExternalLayout) { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1172 | llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known; |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1173 | if (Base->IsVirtual) |
| 1174 | HasExternalLayout = External.getExternalNVBaseOffset(Base->Class, Offset); |
| 1175 | else |
| 1176 | HasExternalLayout = External.getExternalVBaseOffset(Base->Class, Offset); |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 1179 | CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlignment(); |
Eli Friedman | 69d27d2 | 2013-07-16 00:21:28 +0000 | [diff] [blame] | 1180 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
| 1181 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1182 | // If we have an empty base class, try to place it at offset 0. |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1183 | if (Base->Class->isEmpty() && |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1184 | (!HasExternalLayout || Offset == CharUnits::Zero()) && |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1185 | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1186 | setSize(std::max(getSize(), Layout.getSize())); |
Eli Friedman | 69d27d2 | 2013-07-16 00:21:28 +0000 | [diff] [blame] | 1187 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1188 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1189 | return CharUnits::Zero(); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1190 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1191 | |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1192 | // The maximum field alignment overrides base align. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1193 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1194 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 1195 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1196 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1197 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1198 | if (!HasExternalLayout) { |
| 1199 | // Round up the current record size to the base's alignment boundary. |
| 1200 | Offset = getDataSize().RoundUpToAlignment(BaseAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1201 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1202 | // Try to place the base. |
| 1203 | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset)) |
| 1204 | Offset += BaseAlign; |
| 1205 | } else { |
| 1206 | bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset); |
| 1207 | (void)Allowed; |
| 1208 | assert(Allowed && "Base subobject externally placed at overlapping offset"); |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1209 | |
| 1210 | if (InferAlignment && Offset < getDataSize().RoundUpToAlignment(BaseAlign)){ |
| 1211 | // The externally-supplied base offset is before the base offset we |
| 1212 | // computed. Assume that the structure is packed. |
| 1213 | Alignment = CharUnits::One(); |
| 1214 | InferAlignment = false; |
| 1215 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1218 | if (!Base->Class->isEmpty()) { |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1219 | // Update the data size. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1220 | setDataSize(Offset + Layout.getNonVirtualSize()); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1221 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1222 | setSize(std::max(getSize(), getDataSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1223 | } else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1224 | setSize(std::max(getSize(), Offset + Layout.getSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1225 | |
| 1226 | // Remember max struct/class alignment. |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1227 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1228 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1229 | return Offset; |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1232 | void RecordLayoutBuilder::InitializeLayout(const Decl *D) { |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1233 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1234 | IsUnion = RD->isUnion(); |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1235 | IsMsStruct = RD->isMsStruct(Context); |
| 1236 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1237 | |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1238 | Packed = D->hasAttr<PackedAttr>(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1239 | |
Daniel Dunbar | 096ed29 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1240 | // Honor the default struct packing maximum alignment flag. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1241 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) { |
Daniel Dunbar | 096ed29 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1242 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
| 1243 | } |
| 1244 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1245 | // mac68k alignment supersedes maximum field alignment and attribute aligned, |
| 1246 | // and forces all structures to have 2-byte alignment. The IBM docs on it |
| 1247 | // allude to additional (more complicated) semantics, especially with regard |
| 1248 | // to bit-fields, but gcc appears not to follow that. |
| 1249 | if (D->hasAttr<AlignMac68kAttr>()) { |
| 1250 | IsMac68kAlign = true; |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1251 | MaxFieldAlignment = CharUnits::fromQuantity(2); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1252 | Alignment = CharUnits::fromQuantity(2); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1253 | } else { |
| 1254 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1255 | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1256 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1257 | if (unsigned MaxAlign = D->getMaxAlignment()) |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1258 | UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1259 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1260 | |
| 1261 | // If there is an external AST source, ask it for the various offsets. |
| 1262 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1263 | if (ExternalASTSource *Source = Context.getExternalSource()) { |
| 1264 | UseExternalLayout = Source->layoutRecordType( |
| 1265 | RD, External.Size, External.Align, External.FieldOffsets, |
| 1266 | External.BaseOffsets, External.VirtualBaseOffsets); |
NAKAMURA Takumi | 472041f | 2015-02-25 10:32:20 +0000 | [diff] [blame] | 1267 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1268 | // Update based on external alignment. |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1269 | if (UseExternalLayout) { |
| 1270 | if (External.Align > 0) { |
| 1271 | Alignment = Context.toCharUnitsFromBits(External.Align); |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1272 | } else { |
| 1273 | // The external source didn't have alignment information; infer it. |
| 1274 | InferAlignment = true; |
| 1275 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1278 | } |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 1279 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1280 | void RecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 1281 | InitializeLayout(D); |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1282 | LayoutFields(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1284 | // Finally, round the size of the total struct up to the alignment of the |
| 1285 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1286 | FinishLayout(D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
| 1290 | InitializeLayout(RD); |
| 1291 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1292 | // Lay out the vtable and the non-virtual bases. |
| 1293 | LayoutNonVirtualBases(RD); |
| 1294 | |
| 1295 | LayoutFields(RD); |
| 1296 | |
Ken Dyck | e738075 | 2011-03-10 01:53:59 +0000 | [diff] [blame] | 1297 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1298 | llvm::RoundUpToAlignment(getSizeInBits(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1299 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1300 | NonVirtualAlignment = Alignment; |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1301 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1302 | // Lay out the virtual bases and add the primary virtual base offsets. |
| 1303 | LayoutVirtualBases(RD, RD); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1304 | |
| 1305 | // Finally, round the size of the total struct up to the alignment |
Eli Friedman | 83a1258 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 1306 | // of the struct itself. |
| 1307 | FinishLayout(RD); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1308 | |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1309 | #ifndef NDEBUG |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1310 | // Check that we have base offsets for all bases. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1311 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1312 | if (Base.isVirtual()) |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1313 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1314 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1315 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1316 | |
| 1317 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 1318 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1319 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1320 | // And all virtual bases. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1321 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
| 1322 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1323 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1324 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1325 | } |
| 1326 | #endif |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1329 | void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1330 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1331 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1332 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1333 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1335 | // We start laying out ivars not at the end of the superclass |
| 1336 | // structure, but at the next byte following the last field. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1337 | setSize(SL.getDataSize()); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1338 | setDataSize(getSize()); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1339 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1340 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1341 | InitializeLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1342 | // Layout each ivar sequentially. |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1343 | for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD; |
| 1344 | IVD = IVD->getNextIvar()) |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 1345 | LayoutField(IVD, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1347 | // Finally, round the size of the total struct up to the alignment of the |
| 1348 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1349 | FinishLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1352 | void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1353 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 1354 | // the future, this will need to be tweakable by targets. |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 1355 | bool InsertExtraPadding = D->mayInsertExtraPadding(/*EmitRemark=*/true); |
Kostya Serebryany | 68c29da | 2014-10-27 19:34:10 +0000 | [diff] [blame] | 1356 | bool HasFlexibleArrayMember = D->hasFlexibleArrayMember(); |
| 1357 | for (auto I = D->field_begin(), End = D->field_end(); I != End; ++I) { |
| 1358 | auto Next(I); |
| 1359 | ++Next; |
| 1360 | LayoutField(*I, |
| 1361 | InsertExtraPadding && (Next != End || !HasFlexibleArrayMember)); |
| 1362 | } |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Artyom Skrobov | 5e63acc | 2014-10-17 10:22:03 +0000 | [diff] [blame] | 1365 | // Rounds the specified size to have it a multiple of the char size. |
| 1366 | static uint64_t |
| 1367 | roundUpSizeToCharAlignment(uint64_t Size, |
| 1368 | const ASTContext &Context) { |
| 1369 | uint64_t CharAlignment = Context.getTargetInfo().getCharAlign(); |
| 1370 | return llvm::RoundUpToAlignment(Size, CharAlignment); |
| 1371 | } |
| 1372 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1373 | void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1374 | uint64_t TypeSize, |
| 1375 | bool FieldPacked, |
| 1376 | const FieldDecl *D) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1377 | assert(Context.getLangOpts().CPlusPlus && |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1378 | "Can only have wide bit-fields in C++!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1379 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1380 | // Itanium C++ ABI 2.4: |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1381 | // If sizeof(T)*8 < n, let T' be the largest integral POD type with |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1382 | // sizeof(T')*8 <= n. |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1383 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1384 | QualType IntegralPODTypes[] = { |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1385 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1386 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
| 1387 | }; |
| 1388 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1389 | QualType Type; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1390 | for (const QualType &QT : IntegralPODTypes) { |
| 1391 | uint64_t Size = Context.getTypeSize(QT); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (Size > FieldSize) |
| 1394 | break; |
| 1395 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1396 | Type = QT; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1397 | } |
| 1398 | assert(!Type.isNull() && "Did not find a type!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1399 | |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1400 | CharUnits TypeAlign = Context.getTypeAlignInChars(Type); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1401 | |
| 1402 | // We're not going to use any of the unfilled bits in the last byte. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1403 | UnfilledBitsInLastUnit = 0; |
| 1404 | LastBitfieldTypeSize = 0; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1405 | |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1406 | uint64_t FieldOffset; |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1407 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1408 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1409 | if (IsUnion) { |
Artyom Skrobov | 5e63acc | 2014-10-17 10:22:03 +0000 | [diff] [blame] | 1410 | uint64_t RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize, |
| 1411 | Context); |
| 1412 | setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize)); |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1413 | FieldOffset = 0; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1414 | } else { |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1415 | // The bitfield is allocated starting at the next offset aligned |
| 1416 | // appropriately for T', with length n bits. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1417 | FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(), |
| 1418 | Context.toBits(TypeAlign)); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1419 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1420 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1421 | |
Ken Dyck | a1a2e8d | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 1422 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1423 | Context.getTargetInfo().getCharAlign())); |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1424 | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | // Place this field at the current location. |
| 1428 | FieldOffsets.push_back(FieldOffset); |
| 1429 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1430 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1431 | Context.toBits(TypeAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1432 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1433 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1434 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1435 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1436 | // Remember max struct/class alignment. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1437 | UpdateAlignment(TypeAlign); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1440 | void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1441 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1442 | uint64_t FieldSize = D->getBitWidthValue(Context); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1443 | TypeInfo FieldInfo = Context.getTypeInfo(D->getType()); |
| 1444 | uint64_t TypeSize = FieldInfo.Width; |
| 1445 | unsigned FieldAlign = FieldInfo.Align; |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1446 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1447 | // UnfilledBitsInLastUnit is the difference between the end of the |
| 1448 | // last allocated bitfield (i.e. the first bit offset available for |
| 1449 | // bitfields) and the end of the current data size in bits (i.e. the |
| 1450 | // first bit offset available for non-bitfields). The current data |
| 1451 | // size in bits is always a multiple of the char size; additionally, |
| 1452 | // for ms_struct records it's also a multiple of the |
| 1453 | // LastBitfieldTypeSize (if set). |
| 1454 | |
John McCall | 76e1818a | 2014-02-13 00:50:08 +0000 | [diff] [blame] | 1455 | // The struct-layout algorithm is dictated by the platform ABI, |
| 1456 | // which in principle could use almost any rules it likes. In |
| 1457 | // practice, UNIXy targets tend to inherit the algorithm described |
| 1458 | // in the System V generic ABI. The basic bitfield layout rule in |
| 1459 | // System V is to place bitfields at the next available bit offset |
| 1460 | // where the entire bitfield would fit in an aligned storage unit of |
| 1461 | // the declared type; it's okay if an earlier or later non-bitfield |
| 1462 | // is allocated in the same storage unit. However, some targets |
| 1463 | // (those that !useBitFieldTypeAlignment(), e.g. ARM APCS) don't |
| 1464 | // require this storage unit to be aligned, and therefore always put |
| 1465 | // the bitfield at the next available bit offset. |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1466 | |
John McCall | 76e1818a | 2014-02-13 00:50:08 +0000 | [diff] [blame] | 1467 | // ms_struct basically requests a complete replacement of the |
| 1468 | // platform ABI's struct-layout algorithm, with the high-level goal |
| 1469 | // of duplicating MSVC's layout. For non-bitfields, this follows |
| 1470 | // the the standard algorithm. The basic bitfield layout rule is to |
| 1471 | // allocate an entire unit of the bitfield's declared type |
| 1472 | // (e.g. 'unsigned long'), then parcel it up among successive |
| 1473 | // bitfields whose declared types have the same size, making a new |
| 1474 | // unit as soon as the last can no longer store the whole value. |
| 1475 | // Since it completely replaces the platform ABI's algorithm, |
| 1476 | // settings like !useBitFieldTypeAlignment() do not apply. |
| 1477 | |
| 1478 | // A zero-width bitfield forces the use of a new storage unit for |
| 1479 | // later bitfields. In general, this occurs by rounding up the |
| 1480 | // current size of the struct as if the algorithm were about to |
| 1481 | // place a non-bitfield of the field's formal type. Usually this |
| 1482 | // does not change the alignment of the struct itself, but it does |
| 1483 | // on some targets (those that useZeroLengthBitfieldAlignment(), |
| 1484 | // e.g. ARM). In ms_struct layout, zero-width bitfields are |
| 1485 | // ignored unless they follow a non-zero-width bitfield. |
| 1486 | |
| 1487 | // A field alignment restriction (e.g. from #pragma pack) or |
| 1488 | // specification (e.g. from __attribute__((aligned))) changes the |
| 1489 | // formal alignment of the field. For System V, this alters the |
| 1490 | // required alignment of the notional storage unit that must contain |
| 1491 | // the bitfield. For ms_struct, this only affects the placement of |
| 1492 | // new storage units. In both cases, the effect of #pragma pack is |
| 1493 | // ignored on zero-width bitfields. |
| 1494 | |
| 1495 | // On System V, a packed field (e.g. from #pragma pack or |
| 1496 | // __attribute__((packed))) always uses the next available bit |
| 1497 | // offset. |
| 1498 | |
John McCall | 95833f3 | 2014-02-27 20:30:49 +0000 | [diff] [blame] | 1499 | // In an ms_struct struct, the alignment of a fundamental type is |
| 1500 | // always equal to its size. This is necessary in order to mimic |
| 1501 | // the i386 alignment rules on targets which might not fully align |
| 1502 | // all types (e.g. Darwin PPC32, where alignof(long long) == 4). |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1503 | |
| 1504 | // First, some simple bookkeeping to perform for ms_struct structs. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1505 | if (IsMsStruct) { |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1506 | // The field alignment for integer types is always the size. |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1507 | FieldAlign = TypeSize; |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1508 | |
| 1509 | // If the previous field was not a bitfield, or was a bitfield |
| 1510 | // with a different storage unit size, we're done with that |
| 1511 | // storage unit. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1512 | if (LastBitfieldTypeSize != TypeSize) { |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1513 | // Also, ignore zero-length bitfields after non-bitfields. |
| 1514 | if (!LastBitfieldTypeSize && !FieldSize) |
| 1515 | FieldAlign = 1; |
| 1516 | |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1517 | UnfilledBitsInLastUnit = 0; |
| 1518 | LastBitfieldTypeSize = 0; |
| 1519 | } |
| 1520 | } |
| 1521 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1522 | // If the field is wider than its declared type, it follows |
| 1523 | // different rules in all cases. |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1524 | if (FieldSize > TypeSize) { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1525 | LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | } |
| 1528 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1529 | // Compute the next available bit offset. |
| 1530 | uint64_t FieldOffset = |
| 1531 | IsUnion ? 0 : (getDataSizeInBits() - UnfilledBitsInLastUnit); |
| 1532 | |
| 1533 | // Handle targets that don't honor bitfield type alignment. |
John McCall | 76e1818a | 2014-02-13 00:50:08 +0000 | [diff] [blame] | 1534 | if (!IsMsStruct && !Context.getTargetInfo().useBitFieldTypeAlignment()) { |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1535 | // Some such targets do honor it on zero-width bitfields. |
| 1536 | if (FieldSize == 0 && |
| 1537 | Context.getTargetInfo().useZeroLengthBitfieldAlignment()) { |
| 1538 | // The alignment to round up to is the max of the field's natural |
| 1539 | // alignment and a target-specific fixed value (sometimes zero). |
| 1540 | unsigned ZeroLengthBitfieldBoundary = |
| 1541 | Context.getTargetInfo().getZeroLengthBitfieldBoundary(); |
| 1542 | FieldAlign = std::max(FieldAlign, ZeroLengthBitfieldBoundary); |
| 1543 | |
| 1544 | // If that doesn't apply, just ignore the field alignment. |
| 1545 | } else { |
| 1546 | FieldAlign = 1; |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | // Remember the alignment we would have used if the field were not packed. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1551 | unsigned UnpackedFieldAlign = FieldAlign; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1552 | |
Yunzhong Gao | 5fd0c9d | 2014-02-13 02:45:10 +0000 | [diff] [blame] | 1553 | // Ignore the field alignment if the field is packed unless it has zero-size. |
| 1554 | if (!IsMsStruct && FieldPacked && FieldSize != 0) |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1555 | FieldAlign = 1; |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1556 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1557 | // But, if there's an 'aligned' attribute on the field, honor that. |
| 1558 | if (unsigned ExplicitFieldAlign = D->getMaxAlignment()) { |
| 1559 | FieldAlign = std::max(FieldAlign, ExplicitFieldAlign); |
| 1560 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, ExplicitFieldAlign); |
| 1561 | } |
| 1562 | |
| 1563 | // But, if there's a #pragma pack in play, that takes precedent over |
| 1564 | // even the 'aligned' attribute, for non-zero-width bitfields. |
| 1565 | if (!MaxFieldAlignment.isZero() && FieldSize) { |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1566 | unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); |
| 1567 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1568 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1569 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1570 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1571 | // For purposes of diagnostics, we're going to simultaneously |
| 1572 | // compute the field offsets that we would have used if we weren't |
| 1573 | // adding any alignment padding or if the field weren't packed. |
| 1574 | uint64_t UnpaddedFieldOffset = FieldOffset; |
| 1575 | uint64_t UnpackedFieldOffset = FieldOffset; |
| 1576 | |
| 1577 | // Check if we need to add padding to fit the bitfield within an |
| 1578 | // allocation unit with the right size and alignment. The rules are |
| 1579 | // somewhat different here for ms_struct structs. |
| 1580 | if (IsMsStruct) { |
| 1581 | // If it's not a zero-width bitfield, and we can fit the bitfield |
| 1582 | // into the active storage unit (and we haven't already decided to |
| 1583 | // start a new storage unit), just do so, regardless of any other |
| 1584 | // other consideration. Otherwise, round up to the right alignment. |
| 1585 | if (FieldSize == 0 || FieldSize > UnfilledBitsInLastUnit) { |
| 1586 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
| 1587 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1588 | UnpackedFieldAlign); |
| 1589 | UnfilledBitsInLastUnit = 0; |
| 1590 | } |
| 1591 | |
| 1592 | } else { |
| 1593 | // #pragma pack, with any value, suppresses the insertion of padding. |
| 1594 | bool AllowPadding = MaxFieldAlignment.isZero(); |
| 1595 | |
| 1596 | // Compute the real offset. |
| 1597 | if (FieldSize == 0 || |
| 1598 | (AllowPadding && |
| 1599 | (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)) { |
| 1600 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
| 1601 | } |
| 1602 | |
| 1603 | // Repeat the computation for diagnostic purposes. |
| 1604 | if (FieldSize == 0 || |
| 1605 | (AllowPadding && |
| 1606 | (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)) |
| 1607 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1608 | UnpackedFieldAlign); |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1611 | // If we're using external layout, give the external layout a chance |
| 1612 | // to override this information. |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1613 | if (UseExternalLayout) |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1614 | FieldOffset = updateExternalFieldOffset(D, FieldOffset); |
| 1615 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1616 | // Okay, place the bitfield at the calculated offset. |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1617 | FieldOffsets.push_back(FieldOffset); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1618 | |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1619 | // Bookkeeping: |
| 1620 | |
| 1621 | // Anonymous members don't affect the overall record alignment, |
| 1622 | // except on targets where they do. |
| 1623 | if (!IsMsStruct && |
| 1624 | !Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
| 1625 | !D->getIdentifier()) |
| 1626 | FieldAlign = UnpackedFieldAlign = 1; |
| 1627 | |
| 1628 | // Diagnose differences in layout due to padding or packing. |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1629 | if (!UseExternalLayout) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1630 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
| 1631 | UnpackedFieldAlign, FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1632 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1633 | // Update DataSize to include the last byte containing (part of) the bitfield. |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1634 | |
| 1635 | // For unions, this is just a max operation, as usual. |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1636 | if (IsUnion) { |
Artyom Skrobov | 5e63acc | 2014-10-17 10:22:03 +0000 | [diff] [blame] | 1637 | uint64_t RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize, |
| 1638 | Context); |
| 1639 | setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize)); |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1640 | // For non-zero-width bitfields in ms_struct structs, allocate a new |
| 1641 | // storage unit if necessary. |
| 1642 | } else if (IsMsStruct && FieldSize) { |
| 1643 | // We should have cleared UnfilledBitsInLastUnit in every case |
| 1644 | // where we changed storage units. |
| 1645 | if (!UnfilledBitsInLastUnit) { |
| 1646 | setDataSize(FieldOffset + TypeSize); |
| 1647 | UnfilledBitsInLastUnit = TypeSize; |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1648 | } |
John McCall | 30268ca | 2014-01-29 07:53:44 +0000 | [diff] [blame] | 1649 | UnfilledBitsInLastUnit -= FieldSize; |
| 1650 | LastBitfieldTypeSize = TypeSize; |
| 1651 | |
| 1652 | // Otherwise, bump the data size up to include the bitfield, |
| 1653 | // including padding up to char alignment, and then remember how |
| 1654 | // bits we didn't use. |
| 1655 | } else { |
| 1656 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
| 1657 | uint64_t CharAlignment = Context.getTargetInfo().getCharAlign(); |
| 1658 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, CharAlignment)); |
| 1659 | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
| 1660 | |
| 1661 | // The only time we can get here for an ms_struct is if this is a |
| 1662 | // zero-width bitfield, which doesn't count as anything for the |
| 1663 | // purposes of unfilled bits. |
| 1664 | LastBitfieldTypeSize = 0; |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1665 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1666 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1667 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1668 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1669 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1670 | // Remember max struct/class alignment. |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1671 | UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), |
| 1672 | Context.toCharUnitsFromBits(UnpackedFieldAlign)); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 1675 | void RecordLayoutBuilder::LayoutField(const FieldDecl *D, |
| 1676 | bool InsertExtraPadding) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1677 | if (D->isBitField()) { |
| 1678 | LayoutBitField(D); |
| 1679 | return; |
| 1680 | } |
| 1681 | |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1682 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1683 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1684 | // Reset the unfilled bits. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1685 | UnfilledBitsInLastUnit = 0; |
| 1686 | LastBitfieldTypeSize = 0; |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1687 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1688 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1689 | CharUnits FieldOffset = |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1690 | IsUnion ? CharUnits::Zero() : getDataSize(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1691 | CharUnits FieldSize; |
| 1692 | CharUnits FieldAlign; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1693 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1694 | if (D->getType()->isIncompleteArrayType()) { |
| 1695 | // This is a flexible array member; we can't directly |
| 1696 | // query getTypeInfo about these, so we figure it out here. |
| 1697 | // Flexible array members don't have any size, but they |
| 1698 | // have to be aligned appropriately for their element type. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1699 | FieldSize = CharUnits::Zero(); |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1700 | const ArrayType* ATy = Context.getAsArrayType(D->getType()); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1701 | FieldAlign = Context.getTypeAlignInChars(ATy->getElementType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1702 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 1703 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1704 | FieldSize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1705 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS)); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1706 | FieldAlign = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1707 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1708 | } else { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1709 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 1710 | Context.getTypeInfoInChars(D->getType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1711 | FieldSize = FieldInfo.first; |
| 1712 | FieldAlign = FieldInfo.second; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1713 | |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1714 | if (IsMsStruct) { |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1715 | // If MS bitfield layout is required, figure out what type is being |
| 1716 | // laid out and align the field to the width of that type. |
| 1717 | |
| 1718 | // Resolve all typedefs down to their base type and round up the field |
| 1719 | // alignment if necessary. |
| 1720 | QualType T = Context.getBaseElementType(D->getType()); |
| 1721 | if (const BuiltinType *BTy = T->getAs<BuiltinType>()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1722 | CharUnits TypeSize = Context.getTypeSizeInChars(BTy); |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1723 | if (TypeSize > FieldAlign) |
| 1724 | FieldAlign = TypeSize; |
| 1725 | } |
| 1726 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1727 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1729 | // The align if the field is not packed. This is to check if the attribute |
| 1730 | // was unnecessary (-Wpacked). |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1731 | CharUnits UnpackedFieldAlign = FieldAlign; |
| 1732 | CharUnits UnpackedFieldOffset = FieldOffset; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1733 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1734 | if (FieldPacked) |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1735 | FieldAlign = CharUnits::One(); |
| 1736 | CharUnits MaxAlignmentInChars = |
| 1737 | Context.toCharUnitsFromBits(D->getMaxAlignment()); |
| 1738 | FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); |
| 1739 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1740 | |
| 1741 | // The maximum field alignment overrides the aligned attribute. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1742 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1743 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 1744 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1745 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1746 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1747 | // Round up the current record size to the field's alignment boundary. |
| 1748 | FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign); |
| 1749 | UnpackedFieldOffset = |
| 1750 | UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign); |
| 1751 | |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1752 | if (UseExternalLayout) { |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1753 | FieldOffset = Context.toCharUnitsFromBits( |
| 1754 | updateExternalFieldOffset(D, Context.toBits(FieldOffset))); |
| 1755 | |
| 1756 | if (!IsUnion && EmptySubobjects) { |
| 1757 | // Record the fact that we're placing a field at this offset. |
| 1758 | bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset); |
| 1759 | (void)Allowed; |
| 1760 | assert(Allowed && "Externally-placed field cannot be placed here"); |
| 1761 | } |
| 1762 | } else { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1763 | if (!IsUnion && EmptySubobjects) { |
| 1764 | // Check if we can place the field at this offset. |
| 1765 | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { |
| 1766 | // We couldn't place the field at the offset. Try again at a new offset. |
| 1767 | FieldOffset += FieldAlign; |
| 1768 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1769 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1770 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1771 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1772 | // Place this field at the current location. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1773 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1775 | if (!UseExternalLayout) |
| 1776 | CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1777 | Context.toBits(UnpackedFieldOffset), |
| 1778 | Context.toBits(UnpackedFieldAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1779 | |
Kostya Serebryany | 68c29da | 2014-10-27 19:34:10 +0000 | [diff] [blame] | 1780 | if (InsertExtraPadding) { |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 1781 | CharUnits ASanAlignment = CharUnits::fromQuantity(8); |
| 1782 | CharUnits ExtraSizeForAsan = ASanAlignment; |
| 1783 | if (FieldSize % ASanAlignment) |
| 1784 | ExtraSizeForAsan += |
| 1785 | ASanAlignment - CharUnits::fromQuantity(FieldSize % ASanAlignment); |
| 1786 | FieldSize += ExtraSizeForAsan; |
| 1787 | } |
| 1788 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1789 | // Reserve space for this field. |
Eli Friedman | 43f1834 | 2012-01-12 23:27:03 +0000 | [diff] [blame] | 1790 | uint64_t FieldSizeInBits = Context.toBits(FieldSize); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1791 | if (IsUnion) |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1792 | setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1793 | else |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1794 | setDataSize(FieldOffset + FieldSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1796 | // Update the size. |
| 1797 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1799 | // Remember max struct/class alignment. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1800 | UpdateAlignment(FieldAlign, UnpackedFieldAlign); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1803 | void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1804 | // In C++, records cannot be of size 0. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1805 | if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0) { |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1806 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 1807 | // Compatibility with gcc requires a class (pod or non-pod) |
| 1808 | // which is not empty but of size 0; such as having fields of |
| 1809 | // array of zero-length, remains of Size 0 |
| 1810 | if (RD->isEmpty()) |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1811 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1812 | } |
| 1813 | else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1814 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1815 | } |
Eli Friedman | 83a1258 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 1816 | |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1817 | // Finally, round the size of the record up to the alignment of the |
| 1818 | // record itself. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1819 | uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastUnit; |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1820 | uint64_t UnpackedSizeInBits = |
| 1821 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 1822 | Context.toBits(UnpackedAlignment)); |
| 1823 | CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits); |
| 1824 | uint64_t RoundedSize |
| 1825 | = llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)); |
| 1826 | |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1827 | if (UseExternalLayout) { |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1828 | // If we're inferring alignment, and the external size is smaller than |
| 1829 | // our size after we've rounded up to alignment, conservatively set the |
| 1830 | // alignment to 1. |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1831 | if (InferAlignment && External.Size < RoundedSize) { |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1832 | Alignment = CharUnits::One(); |
| 1833 | InferAlignment = false; |
| 1834 | } |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1835 | setSize(External.Size); |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1836 | return; |
| 1837 | } |
| 1838 | |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1839 | // Set the size to the final size. |
| 1840 | setSize(RoundedSize); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1842 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1843 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 1844 | // Warn if padding was introduced to the struct/class/union. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1845 | if (getSizeInBits() > UnpaddedSize) { |
| 1846 | unsigned PadSize = getSizeInBits() - UnpaddedSize; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1847 | bool InBits = true; |
| 1848 | if (PadSize % CharBitNum == 0) { |
| 1849 | PadSize = PadSize / CharBitNum; |
| 1850 | InBits = false; |
| 1851 | } |
| 1852 | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
| 1853 | << Context.getTypeDeclType(RD) |
| 1854 | << PadSize |
| 1855 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 1856 | } |
| 1857 | |
| 1858 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1859 | // bother since there won't be alignment issues. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1860 | if (Packed && UnpackedAlignment > CharUnits::One() && |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1861 | getSize() == UnpackedSize) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1862 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1863 | << Context.getTypeDeclType(RD); |
| 1864 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1867 | void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment, |
| 1868 | CharUnits UnpackedNewAlignment) { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1869 | // The alignment is not modified when using 'mac68k' alignment or when |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1870 | // we have an externally-supplied layout that also provides overall alignment. |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1871 | if (IsMac68kAlign || (UseExternalLayout && !InferAlignment)) |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1872 | return; |
| 1873 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1874 | if (NewAlignment > Alignment) { |
Reid Kleckner | 5a63d70 | 2015-03-24 23:46:25 +0000 | [diff] [blame] | 1875 | assert(llvm::isPowerOf2_64(NewAlignment.getQuantity()) && |
| 1876 | "Alignment not a power of 2"); |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1877 | Alignment = NewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1880 | if (UnpackedNewAlignment > UnpackedAlignment) { |
Reid Kleckner | 5a63d70 | 2015-03-24 23:46:25 +0000 | [diff] [blame] | 1881 | assert(llvm::isPowerOf2_64(UnpackedNewAlignment.getQuantity()) && |
| 1882 | "Alignment not a power of 2"); |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1883 | UnpackedAlignment = UnpackedNewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1887 | uint64_t |
| 1888 | RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field, |
| 1889 | uint64_t ComputedOffset) { |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 1890 | uint64_t ExternalFieldOffset = External.getExternalFieldOffset(Field); |
NAKAMURA Takumi | 472041f | 2015-02-25 10:32:20 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1892 | if (InferAlignment && ExternalFieldOffset < ComputedOffset) { |
| 1893 | // The externally-supplied field offset is before the field offset we |
| 1894 | // computed. Assume that the structure is packed. |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1895 | Alignment = CharUnits::One(); |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1896 | InferAlignment = false; |
| 1897 | } |
| 1898 | |
| 1899 | // Use the externally-supplied field offset. |
Benjamin Kramer | 648e68b | 2012-08-31 22:14:25 +0000 | [diff] [blame] | 1900 | return ExternalFieldOffset; |
| 1901 | } |
| 1902 | |
| 1903 | /// \brief Get diagnostic %select index for tag kind for |
| 1904 | /// field padding diagnostic message. |
| 1905 | /// WARNING: Indexes apply to particular diagnostics only! |
| 1906 | /// |
| 1907 | /// \returns diagnostic %select index. |
| 1908 | static unsigned getPaddingDiagFromTagKind(TagTypeKind Tag) { |
| 1909 | switch (Tag) { |
| 1910 | case TTK_Struct: return 0; |
| 1911 | case TTK_Interface: return 1; |
| 1912 | case TTK_Class: return 2; |
| 1913 | default: llvm_unreachable("Invalid tag kind for field padding diagnostic!"); |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset, |
| 1918 | uint64_t UnpaddedOffset, |
| 1919 | uint64_t UnpackedOffset, |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1920 | unsigned UnpackedAlign, |
| 1921 | bool isPacked, |
| 1922 | const FieldDecl *D) { |
| 1923 | // We let objc ivars without warning, objc interfaces generally are not used |
| 1924 | // for padding tricks. |
| 1925 | if (isa<ObjCIvarDecl>(D)) |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1926 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | |
Ted Kremenek | fed48af | 2011-09-06 19:40:45 +0000 | [diff] [blame] | 1928 | // Don't warn about structs created without a SourceLocation. This can |
| 1929 | // be done by clients of the AST, such as codegen. |
| 1930 | if (D->getLocation().isInvalid()) |
| 1931 | return; |
| 1932 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1933 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1935 | // Warn if padding was introduced to the struct/class. |
| 1936 | if (!IsUnion && Offset > UnpaddedOffset) { |
| 1937 | unsigned PadSize = Offset - UnpaddedOffset; |
| 1938 | bool InBits = true; |
| 1939 | if (PadSize % CharBitNum == 0) { |
| 1940 | PadSize = PadSize / CharBitNum; |
| 1941 | InBits = false; |
Benjamin Kramer | 648e68b | 2012-08-31 22:14:25 +0000 | [diff] [blame] | 1942 | } |
| 1943 | if (D->getIdentifier()) |
| 1944 | Diag(D->getLocation(), diag::warn_padded_struct_field) |
| 1945 | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
| 1946 | << Context.getTypeDeclType(D->getParent()) |
| 1947 | << PadSize |
| 1948 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not |
| 1949 | << D->getIdentifier(); |
| 1950 | else |
| 1951 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
| 1952 | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
| 1953 | << Context.getTypeDeclType(D->getParent()) |
| 1954 | << PadSize |
| 1955 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1959 | // bother since there won't be alignment issues. |
| 1960 | if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset) |
| 1961 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1962 | << D->getIdentifier(); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1963 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1965 | static const CXXMethodDecl *computeKeyFunction(ASTContext &Context, |
| 1966 | const CXXRecordDecl *RD) { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1967 | // If a class isn't polymorphic it doesn't have a key function. |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1968 | if (!RD->isPolymorphic()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1969 | return nullptr; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1970 | |
Eli Friedman | 300f55d | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 1971 | // A class that is not externally visible doesn't have a key function. (Or |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1972 | // at least, there's no point to assigning a key function to such a class; |
| 1973 | // this doesn't affect the ABI.) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1974 | if (!RD->isExternallyVisible()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1975 | return nullptr; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1976 | |
Richard Smith | 750f511 | 2014-03-24 23:54:09 +0000 | [diff] [blame] | 1977 | // Template instantiations don't have key functions per Itanium C++ ABI 5.2.6. |
Argyrios Kyrtzidis | 8c64bbe | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 1978 | // Same behavior as GCC. |
| 1979 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 1980 | if (TSK == TSK_ImplicitInstantiation || |
Richard Smith | 750f511 | 2014-03-24 23:54:09 +0000 | [diff] [blame] | 1981 | TSK == TSK_ExplicitInstantiationDeclaration || |
Argyrios Kyrtzidis | 8c64bbe | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 1982 | TSK == TSK_ExplicitInstantiationDefinition) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1983 | return nullptr; |
Argyrios Kyrtzidis | 8c64bbe | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 1984 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1985 | bool allowInlineFunctions = |
| 1986 | Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline(); |
| 1987 | |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 1988 | for (const CXXMethodDecl *MD : RD->methods()) { |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1989 | if (!MD->isVirtual()) |
| 1990 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1991 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1992 | if (MD->isPure()) |
| 1993 | continue; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1994 | |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1995 | // Ignore implicit member functions, they are always marked as inline, but |
| 1996 | // they don't have a body until they're defined. |
| 1997 | if (MD->isImplicit()) |
| 1998 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1999 | |
Douglas Gregor | a318efd | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 2000 | if (MD->isInlineSpecified()) |
| 2001 | continue; |
Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 2002 | |
| 2003 | if (MD->hasInlineBody()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2004 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2005 | |
Benjamin Kramer | 4a90208 | 2012-08-03 15:43:22 +0000 | [diff] [blame] | 2006 | // Ignore inline deleted or defaulted functions. |
Benjamin Kramer | 73d1be7 | 2012-08-03 08:39:58 +0000 | [diff] [blame] | 2007 | if (!MD->isUserProvided()) |
| 2008 | continue; |
| 2009 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2010 | // In certain ABIs, ignore functions with out-of-line inline definitions. |
| 2011 | if (!allowInlineFunctions) { |
| 2012 | const FunctionDecl *Def; |
| 2013 | if (MD->hasBody(Def) && Def->isInlineSpecified()) |
| 2014 | continue; |
| 2015 | } |
| 2016 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2017 | // We found it. |
| 2018 | return MD; |
| 2019 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2020 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2021 | return nullptr; |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2024 | DiagnosticBuilder |
| 2025 | RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 2026 | return Context.getDiagnostics().Report(Loc, DiagID); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 2029 | /// Does the target C++ ABI require us to skip over the tail-padding |
| 2030 | /// of the given class (considering it as a base class) when allocating |
| 2031 | /// objects? |
| 2032 | static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) { |
| 2033 | switch (ABI.getTailPaddingUseRules()) { |
| 2034 | case TargetCXXABI::AlwaysUseTailPadding: |
| 2035 | return false; |
| 2036 | |
| 2037 | case TargetCXXABI::UseTailPaddingUnlessPOD03: |
| 2038 | // FIXME: To the extent that this is meant to cover the Itanium ABI |
| 2039 | // rules, we should implement the restrictions about over-sized |
| 2040 | // bitfields: |
| 2041 | // |
| 2042 | // http://mentorembedded.github.com/cxx-abi/abi.html#POD : |
| 2043 | // In general, a type is considered a POD for the purposes of |
| 2044 | // layout if it is a POD type (in the sense of ISO C++ |
| 2045 | // [basic.types]). However, a POD-struct or POD-union (in the |
| 2046 | // sense of ISO C++ [class]) with a bitfield member whose |
| 2047 | // declared width is wider than the declared type of the |
| 2048 | // bitfield is not a POD for the purpose of layout. Similarly, |
| 2049 | // an array type is not a POD for the purpose of layout if the |
| 2050 | // element type of the array is not a POD for the purpose of |
| 2051 | // layout. |
| 2052 | // |
| 2053 | // Where references to the ISO C++ are made in this paragraph, |
| 2054 | // the Technical Corrigendum 1 version of the standard is |
| 2055 | // intended. |
| 2056 | return RD->isPOD(); |
| 2057 | |
| 2058 | case TargetCXXABI::UseTailPaddingUnlessPOD11: |
| 2059 | // This is equivalent to RD->getTypeForDecl().isCXX11PODType(), |
| 2060 | // but with a lot of abstraction penalty stripped off. This does |
| 2061 | // assume that these properties are set correctly even in C++98 |
| 2062 | // mode; fortunately, that is true because we want to assign |
| 2063 | // consistently semantics to the type-traits intrinsics (or at |
| 2064 | // least as many of them as possible). |
| 2065 | return RD->isTrivial() && RD->isStandardLayout(); |
| 2066 | } |
| 2067 | |
| 2068 | llvm_unreachable("bad tail-padding use kind"); |
| 2069 | } |
| 2070 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2071 | static bool isMsLayout(const RecordDecl* D) { |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2072 | return D->getASTContext().getTargetInfo().getCXXABI().isMicrosoft(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | // This section contains an implementation of struct layout that is, up to the |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2076 | // included tests, compatible with cl.exe (2013). The layout produced is |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2077 | // significantly different than those produced by the Itanium ABI. Here we note |
| 2078 | // the most important differences. |
| 2079 | // |
| 2080 | // * The alignment of bitfields in unions is ignored when computing the |
| 2081 | // alignment of the union. |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 2082 | // * The existence of zero-width bitfield that occurs after anything other than |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2083 | // a non-zero length bitfield is ignored. |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2084 | // * There is no explicit primary base for the purposes of layout. All bases |
| 2085 | // with vfptrs are laid out first, followed by all bases without vfptrs. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2086 | // * The Itanium equivalent vtable pointers are split into a vfptr (virtual |
| 2087 | // function pointer) and a vbptr (virtual base pointer). They can each be |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2088 | // shared with a, non-virtual bases. These bases need not be the same. vfptrs |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2089 | // always occur at offset 0. vbptrs can occur at an arbitrary offset and are |
| 2090 | // placed after the lexiographically last non-virtual base. This placement |
| 2091 | // is always before fields but can be in the middle of the non-virtual bases |
| 2092 | // due to the two-pass layout scheme for non-virtual-bases. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2093 | // * Virtual bases sometimes require a 'vtordisp' field that is laid out before |
| 2094 | // the virtual base and is used in conjunction with virtual overrides during |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2095 | // construction and destruction. This is always a 4 byte value and is used as |
| 2096 | // an alternative to constructor vtables. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2097 | // * vtordisps are allocated in a block of memory with size and alignment equal |
| 2098 | // to the alignment of the completed structure (before applying __declspec( |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2099 | // align())). The vtordisp always occur at the end of the allocation block, |
| 2100 | // immediately prior to the virtual base. |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2101 | // * vfptrs are injected after all bases and fields have been laid out. In |
| 2102 | // order to guarantee proper alignment of all fields, the vfptr injection |
| 2103 | // pushes all bases and fields back by the alignment imposed by those bases |
| 2104 | // and fields. This can potentially add a significant amount of padding. |
| 2105 | // vfptrs are always injected at offset 0. |
| 2106 | // * vbptrs are injected after all bases and fields have been laid out. In |
| 2107 | // order to guarantee proper alignment of all fields, the vfptr injection |
| 2108 | // pushes all bases and fields back by the alignment imposed by those bases |
| 2109 | // and fields. This can potentially add a significant amount of padding. |
| 2110 | // vbptrs are injected immediately after the last non-virtual base as |
| 2111 | // lexiographically ordered in the code. If this site isn't pointer aligned |
| 2112 | // the vbptr is placed at the next properly aligned location. Enough padding |
| 2113 | // is added to guarantee a fit. |
| 2114 | // * The last zero sized non-virtual base can be placed at the end of the |
| 2115 | // struct (potentially aliasing another object), or may alias with the first |
| 2116 | // field, even if they are of the same type. |
| 2117 | // * The last zero size virtual base may be placed at the end of the struct |
| 2118 | // potentially aliasing another object. |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2119 | // * The ABI attempts to avoid aliasing of zero sized bases by adding padding |
| 2120 | // between bases or vbases with specific properties. The criteria for |
| 2121 | // additional padding between two bases is that the first base is zero sized |
Warren Hunt | 39a907b | 2014-04-09 21:57:24 +0000 | [diff] [blame] | 2122 | // or ends with a zero sized subobject and the second base is zero sized or |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2123 | // trails with a zero sized base or field (sharing of vfptrs can reorder the |
| 2124 | // layout of the so the leading base is not always the first one declared). |
| 2125 | // This rule does take into account fields that are not records, so padding |
| 2126 | // will occur even if the last field is, e.g. an int. The padding added for |
| 2127 | // bases is 1 byte. The padding added between vbases depends on the alignment |
| 2128 | // of the object but is at least 4 bytes (in both 32 and 64 bit modes). |
| 2129 | // * There is no concept of non-virtual alignment, non-virtual alignment and |
| 2130 | // alignment are always identical. |
| 2131 | // * There is a distinction between alignment and required alignment. |
| 2132 | // __declspec(align) changes the required alignment of a struct. This |
| 2133 | // alignment is _always_ obeyed, even in the presence of #pragma pack. A |
Justin Bogner | 2ca9a4a | 2014-10-08 05:45:39 +0000 | [diff] [blame] | 2134 | // record inherits required alignment from all of its fields and bases. |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2135 | // * __declspec(align) on bitfields has the effect of changing the bitfield's |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2136 | // alignment instead of its required alignment. This is the only known way |
| 2137 | // to make the alignment of a struct bigger than 8. Interestingly enough |
| 2138 | // this alignment is also immune to the effects of #pragma pack and can be |
| 2139 | // used to create structures with large alignment under #pragma pack. |
| 2140 | // However, because it does not impact required alignment, such a structure, |
| 2141 | // when used as a field or base, will not be aligned if #pragma pack is |
| 2142 | // still active at the time of use. |
| 2143 | // |
Alp Toker | 08f6e9e | 2014-05-05 19:53:42 +0000 | [diff] [blame] | 2144 | // Known incompatibilities: |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2145 | // * all: #pragma pack between fields in a record |
| 2146 | // * 2010 and back: If the last field in a record is a bitfield, every object |
| 2147 | // laid out after the record will have extra padding inserted before it. The |
| 2148 | // extra padding will have size equal to the size of the storage class of the |
| 2149 | // bitfield. 0 sized bitfields don't exhibit this behavior and the extra |
| 2150 | // padding can be avoided by adding a 0 sized bitfield after the non-zero- |
| 2151 | // sized bitfield. |
| 2152 | // * 2012 and back: In 64-bit mode, if the alignment of a record is 16 or |
| 2153 | // greater due to __declspec(align()) then a second layout phase occurs after |
| 2154 | // The locations of the vf and vb pointers are known. This layout phase |
| 2155 | // suffers from the "last field is a bitfield" bug in 2010 and results in |
| 2156 | // _every_ field getting padding put in front of it, potentially including the |
| 2157 | // vfptr, leaving the vfprt at a non-zero location which results in a fault if |
| 2158 | // anything tries to read the vftbl. The second layout phase also treats |
Alp Toker | 08f6e9e | 2014-05-05 19:53:42 +0000 | [diff] [blame] | 2159 | // bitfields as separate entities and gives them each storage rather than |
Warren Hunt | 917f97f | 2014-04-11 00:54:15 +0000 | [diff] [blame] | 2160 | // packing them. Additionally, because this phase appears to perform a |
| 2161 | // (an unstable) sort on the members before laying them out and because merged |
| 2162 | // bitfields have the same address, the bitfields end up in whatever order |
| 2163 | // the sort left them in, a behavior we could never hope to replicate. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2164 | |
| 2165 | namespace { |
| 2166 | struct MicrosoftRecordLayoutBuilder { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2167 | struct ElementInfo { |
| 2168 | CharUnits Size; |
| 2169 | CharUnits Alignment; |
| 2170 | }; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2171 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
| 2172 | MicrosoftRecordLayoutBuilder(const ASTContext &Context) : Context(Context) {} |
| 2173 | private: |
Aaron Ballman | abc1892 | 2015-02-15 22:54:08 +0000 | [diff] [blame] | 2174 | MicrosoftRecordLayoutBuilder(const MicrosoftRecordLayoutBuilder &) = delete; |
| 2175 | void operator=(const MicrosoftRecordLayoutBuilder &) = delete; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2176 | public: |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2177 | void layout(const RecordDecl *RD); |
| 2178 | void cxxLayout(const CXXRecordDecl *RD); |
| 2179 | /// \brief Initializes size and alignment and honors some flags. |
| 2180 | void initializeLayout(const RecordDecl *RD); |
| 2181 | /// \brief Initialized C++ layout, compute alignment and virtual alignment and |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 2182 | /// existence of vfptrs and vbptrs. Alignment is needed before the vfptr is |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2183 | /// laid out. |
| 2184 | void initializeCXXLayout(const CXXRecordDecl *RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2185 | void layoutNonVirtualBases(const CXXRecordDecl *RD); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2186 | void layoutNonVirtualBase(const CXXRecordDecl *BaseDecl, |
| 2187 | const ASTRecordLayout &BaseLayout, |
| 2188 | const ASTRecordLayout *&PreviousBaseLayout); |
| 2189 | void injectVFPtr(const CXXRecordDecl *RD); |
| 2190 | void injectVBPtr(const CXXRecordDecl *RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2191 | /// \brief Lays out the fields of the record. Also rounds size up to |
| 2192 | /// alignment. |
| 2193 | void layoutFields(const RecordDecl *RD); |
| 2194 | void layoutField(const FieldDecl *FD); |
| 2195 | void layoutBitField(const FieldDecl *FD); |
| 2196 | /// \brief Lays out a single zero-width bit-field in the record and handles |
| 2197 | /// special cases associated with zero-width bit-fields. |
| 2198 | void layoutZeroWidthBitField(const FieldDecl *FD); |
| 2199 | void layoutVirtualBases(const CXXRecordDecl *RD); |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2200 | void finalizeLayout(const RecordDecl *RD); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2201 | /// \brief Gets the size and alignment of a base taking pragma pack and |
| 2202 | /// __declspec(align) into account. |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2203 | ElementInfo getAdjustedElementInfo(const ASTRecordLayout &Layout); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2204 | /// \brief Gets the size and alignment of a field taking pragma pack and |
| 2205 | /// __declspec(align) into account. It also updates RequiredAlignment as a |
| 2206 | /// side effect because it is most convenient to do so here. |
| 2207 | ElementInfo getAdjustedElementInfo(const FieldDecl *FD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2208 | /// \brief Places a field at an offset in CharUnits. |
| 2209 | void placeFieldAtOffset(CharUnits FieldOffset) { |
| 2210 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
| 2211 | } |
| 2212 | /// \brief Places a bitfield at a bit offset. |
| 2213 | void placeFieldAtBitOffset(uint64_t FieldOffset) { |
| 2214 | FieldOffsets.push_back(FieldOffset); |
| 2215 | } |
| 2216 | /// \brief Compute the set of virtual bases for which vtordisps are required. |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2217 | void computeVtorDispSet( |
| 2218 | llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtorDispSet, |
| 2219 | const CXXRecordDecl *RD) const; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2220 | const ASTContext &Context; |
| 2221 | /// \brief The size of the record being laid out. |
| 2222 | CharUnits Size; |
Warren Hunt | f6ec748 | 2014-02-21 01:40:35 +0000 | [diff] [blame] | 2223 | /// \brief The non-virtual size of the record layout. |
| 2224 | CharUnits NonVirtualSize; |
| 2225 | /// \brief The data size of the record layout. |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2226 | CharUnits DataSize; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2227 | /// \brief The current alignment of the record layout. |
| 2228 | CharUnits Alignment; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2229 | /// \brief The maximum allowed field alignment. This is set by #pragma pack. |
| 2230 | CharUnits MaxFieldAlignment; |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2231 | /// \brief The alignment that this record must obey. This is imposed by |
| 2232 | /// __declspec(align()) on the record itself or one of its fields or bases. |
| 2233 | CharUnits RequiredAlignment; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2234 | /// \brief The size of the allocation of the currently active bitfield. |
| 2235 | /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield |
| 2236 | /// is true. |
| 2237 | CharUnits CurrentBitfieldSize; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2238 | /// \brief Offset to the virtual base table pointer (if one exists). |
| 2239 | CharUnits VBPtrOffset; |
David Majnemer | 00a061d | 2014-09-30 06:45:43 +0000 | [diff] [blame] | 2240 | /// \brief Minimum record size possible. |
| 2241 | CharUnits MinEmptyStructSize; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2242 | /// \brief The size and alignment info of a pointer. |
| 2243 | ElementInfo PointerInfo; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2244 | /// \brief The primary base class (if one exists). |
| 2245 | const CXXRecordDecl *PrimaryBase; |
| 2246 | /// \brief The class we share our vb-pointer with. |
| 2247 | const CXXRecordDecl *SharedVBPtrBase; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2248 | /// \brief The collection of field offsets. |
| 2249 | SmallVector<uint64_t, 16> FieldOffsets; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2250 | /// \brief Base classes and their offsets in the record. |
| 2251 | BaseOffsetsMapTy Bases; |
| 2252 | /// \brief virtual base classes and their offsets in the record. |
| 2253 | ASTRecordLayout::VBaseOffsetsMapTy VBases; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2254 | /// \brief The number of remaining bits in our last bitfield allocation. |
| 2255 | /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield is |
| 2256 | /// true. |
| 2257 | unsigned RemainingBitsInField; |
| 2258 | bool IsUnion : 1; |
| 2259 | /// \brief True if the last field laid out was a bitfield and was not 0 |
| 2260 | /// width. |
| 2261 | bool LastFieldIsNonZeroWidthBitfield : 1; |
| 2262 | /// \brief True if the class has its own vftable pointer. |
| 2263 | bool HasOwnVFPtr : 1; |
| 2264 | /// \brief True if the class has a vbtable pointer. |
| 2265 | bool HasVBPtr : 1; |
Warren Hunt | 39a907b | 2014-04-09 21:57:24 +0000 | [diff] [blame] | 2266 | /// \brief True if the last sub-object within the type is zero sized or the |
| 2267 | /// object itself is zero sized. This *does not* count members that are not |
| 2268 | /// records. Only used for MS-ABI. |
| 2269 | bool EndsWithZeroSizedObject : 1; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2270 | /// \brief True if this class is zero sized or first base is zero sized or |
| 2271 | /// has this property. Only used for MS-ABI. |
| 2272 | bool LeadsWithZeroSizedBase : 1; |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2273 | |
| 2274 | /// \brief True if the external AST source provided a layout for this record. |
| 2275 | bool UseExternalLayout : 1; |
| 2276 | |
| 2277 | /// \brief The layout provided by the external AST source. Only active if |
| 2278 | /// UseExternalLayout is true. |
| 2279 | ExternalLayout External; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2280 | }; |
| 2281 | } // namespace |
| 2282 | |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2283 | MicrosoftRecordLayoutBuilder::ElementInfo |
| 2284 | MicrosoftRecordLayoutBuilder::getAdjustedElementInfo( |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2285 | const ASTRecordLayout &Layout) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2286 | ElementInfo Info; |
| 2287 | Info.Alignment = Layout.getAlignment(); |
| 2288 | // Respect pragma pack. |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2289 | if (!MaxFieldAlignment.isZero()) |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2290 | Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment); |
| 2291 | // Track zero-sized subobjects here where it's already available. |
Warren Hunt | 39a907b | 2014-04-09 21:57:24 +0000 | [diff] [blame] | 2292 | EndsWithZeroSizedObject = Layout.hasZeroSizedSubObject(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2293 | // Respect required alignment, this is necessary because we may have adjusted |
Warren Hunt | 9425891 | 2014-01-11 01:16:40 +0000 | [diff] [blame] | 2294 | // the alignment in the case of pragam pack. Note that the required alignment |
| 2295 | // doesn't actually apply to the struct alignment at this point. |
| 2296 | Alignment = std::max(Alignment, Info.Alignment); |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2297 | RequiredAlignment = std::max(RequiredAlignment, Layout.getRequiredAlignment()); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2298 | Info.Alignment = std::max(Info.Alignment, Layout.getRequiredAlignment()); |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2299 | Info.Size = Layout.getNonVirtualSize(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2300 | return Info; |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2303 | MicrosoftRecordLayoutBuilder::ElementInfo |
| 2304 | MicrosoftRecordLayoutBuilder::getAdjustedElementInfo( |
| 2305 | const FieldDecl *FD) { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2306 | // Get the alignment of the field type's natural alignment, ignore any |
| 2307 | // alignment attributes. |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2308 | ElementInfo Info; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 2309 | std::tie(Info.Size, Info.Alignment) = |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2310 | Context.getTypeInfoInChars(FD->getType()->getUnqualifiedDesugaredType()); |
| 2311 | // Respect align attributes on the field. |
| 2312 | CharUnits FieldRequiredAlignment = |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2313 | Context.toCharUnitsFromBits(FD->getMaxAlignment()); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2314 | // Respect align attributes on the type. |
| 2315 | if (Context.isAlignmentRequired(FD->getType())) |
| 2316 | FieldRequiredAlignment = std::max( |
| 2317 | Context.getTypeAlignInChars(FD->getType()), FieldRequiredAlignment); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2318 | // Respect attributes applied to subobjects of the field. |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2319 | if (FD->isBitField()) |
| 2320 | // For some reason __declspec align impacts alignment rather than required |
| 2321 | // alignment when it is applied to bitfields. |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2322 | Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment); |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2323 | else { |
| 2324 | if (auto RT = |
| 2325 | FD->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) { |
| 2326 | auto const &Layout = Context.getASTRecordLayout(RT->getDecl()); |
| 2327 | EndsWithZeroSizedObject = Layout.hasZeroSizedSubObject(); |
| 2328 | FieldRequiredAlignment = std::max(FieldRequiredAlignment, |
| 2329 | Layout.getRequiredAlignment()); |
| 2330 | } |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2331 | // Capture required alignment as a side-effect. |
| 2332 | RequiredAlignment = std::max(RequiredAlignment, FieldRequiredAlignment); |
| 2333 | } |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2334 | // Respect pragma pack, attribute pack and declspec align |
| 2335 | if (!MaxFieldAlignment.isZero()) |
| 2336 | Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment); |
| 2337 | if (FD->hasAttr<PackedAttr>()) |
| 2338 | Info.Alignment = CharUnits::One(); |
| 2339 | Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2340 | return Info; |
| 2341 | } |
| 2342 | |
| 2343 | void MicrosoftRecordLayoutBuilder::layout(const RecordDecl *RD) { |
David Majnemer | 00a061d | 2014-09-30 06:45:43 +0000 | [diff] [blame] | 2344 | // For C record layout, zero-sized records always have size 4. |
| 2345 | MinEmptyStructSize = CharUnits::fromQuantity(4); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2346 | initializeLayout(RD); |
| 2347 | layoutFields(RD); |
| 2348 | DataSize = Size = Size.RoundUpToAlignment(Alignment); |
David Majnemer | 79a1c89 | 2014-02-12 00:43:02 +0000 | [diff] [blame] | 2349 | RequiredAlignment = std::max( |
| 2350 | RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment())); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2351 | finalizeLayout(RD); |
| 2352 | } |
| 2353 | |
| 2354 | void MicrosoftRecordLayoutBuilder::cxxLayout(const CXXRecordDecl *RD) { |
David Majnemer | 00a061d | 2014-09-30 06:45:43 +0000 | [diff] [blame] | 2355 | // The C++ standard says that empty structs have size 1. |
| 2356 | MinEmptyStructSize = CharUnits::One(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2357 | initializeLayout(RD); |
| 2358 | initializeCXXLayout(RD); |
| 2359 | layoutNonVirtualBases(RD); |
| 2360 | layoutFields(RD); |
Warren Hunt | c89450e | 2014-03-24 21:37:27 +0000 | [diff] [blame] | 2361 | injectVBPtr(RD); |
| 2362 | injectVFPtr(RD); |
| 2363 | if (HasOwnVFPtr || (HasVBPtr && !SharedVBPtrBase)) |
| 2364 | Alignment = std::max(Alignment, PointerInfo.Alignment); |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2365 | auto RoundingAlignment = Alignment; |
| 2366 | if (!MaxFieldAlignment.isZero()) |
| 2367 | RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment); |
| 2368 | NonVirtualSize = Size = Size.RoundUpToAlignment(RoundingAlignment); |
David Majnemer | 79a1c89 | 2014-02-12 00:43:02 +0000 | [diff] [blame] | 2369 | RequiredAlignment = std::max( |
| 2370 | RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment())); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2371 | layoutVirtualBases(RD); |
| 2372 | finalizeLayout(RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | void MicrosoftRecordLayoutBuilder::initializeLayout(const RecordDecl *RD) { |
| 2376 | IsUnion = RD->isUnion(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2377 | Size = CharUnits::Zero(); |
| 2378 | Alignment = CharUnits::One(); |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2379 | // In 64-bit mode we always perform an alignment step after laying out vbases. |
| 2380 | // In 32-bit mode we do not. The check to see if we need to perform alignment |
| 2381 | // checks the RequiredAlignment field and performs alignment if it isn't 0. |
David Majnemer | 37ea578 | 2015-04-24 01:24:59 +0000 | [diff] [blame] | 2382 | RequiredAlignment = Context.getTargetInfo().getTriple().isArch64Bit() |
| 2383 | ? CharUnits::One() |
| 2384 | : CharUnits::Zero(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2385 | // Compute the maximum field alignment. |
| 2386 | MaxFieldAlignment = CharUnits::Zero(); |
| 2387 | // Honor the default struct packing maximum alignment flag. |
| 2388 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2389 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
| 2390 | // Honor the packing attribute. The MS-ABI ignores pragma pack if its larger |
| 2391 | // than the pointer size. |
| 2392 | if (const MaxFieldAlignmentAttr *MFAA = RD->getAttr<MaxFieldAlignmentAttr>()){ |
| 2393 | unsigned PackedAlignment = MFAA->getAlignment(); |
| 2394 | if (PackedAlignment <= Context.getTargetInfo().getPointerWidth(0)) |
| 2395 | MaxFieldAlignment = Context.toCharUnitsFromBits(PackedAlignment); |
| 2396 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2397 | // Packed attribute forces max field alignment to be 1. |
| 2398 | if (RD->hasAttr<PackedAttr>()) |
| 2399 | MaxFieldAlignment = CharUnits::One(); |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2400 | |
| 2401 | // Try to respect the external layout if present. |
| 2402 | UseExternalLayout = false; |
| 2403 | if (ExternalASTSource *Source = Context.getExternalSource()) |
| 2404 | UseExternalLayout = Source->layoutRecordType( |
| 2405 | RD, External.Size, External.Align, External.FieldOffsets, |
| 2406 | External.BaseOffsets, External.VirtualBaseOffsets); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2409 | void |
| 2410 | MicrosoftRecordLayoutBuilder::initializeCXXLayout(const CXXRecordDecl *RD) { |
Warren Hunt | 39a907b | 2014-04-09 21:57:24 +0000 | [diff] [blame] | 2411 | EndsWithZeroSizedObject = false; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2412 | LeadsWithZeroSizedBase = false; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2413 | HasOwnVFPtr = false; |
| 2414 | HasVBPtr = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2415 | PrimaryBase = nullptr; |
| 2416 | SharedVBPtrBase = nullptr; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2417 | // Calculate pointer size and alignment. These are used for vfptr and vbprt |
| 2418 | // injection. |
| 2419 | PointerInfo.Size = |
| 2420 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
David Majnemer | 37ea578 | 2015-04-24 01:24:59 +0000 | [diff] [blame] | 2421 | PointerInfo.Alignment = |
| 2422 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2423 | // Respect pragma pack. |
| 2424 | if (!MaxFieldAlignment.isZero()) |
| 2425 | PointerInfo.Alignment = std::min(PointerInfo.Alignment, MaxFieldAlignment); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | void |
| 2429 | MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2430 | // The MS-ABI lays out all bases that contain leading vfptrs before it lays |
| 2431 | // out any bases that do not contain vfptrs. We implement this as two passes |
| 2432 | // over the bases. This approach guarantees that the primary base is laid out |
| 2433 | // first. We use these passes to calculate some additional aggregated |
| 2434 | // information about the bases, such as reqruied alignment and the presence of |
| 2435 | // zero sized members. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2436 | const ASTRecordLayout *PreviousBaseLayout = nullptr; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2437 | // Iterate through the bases and lay out the non-virtual ones. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2438 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 2439 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2440 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2441 | // Mark and skip virtual bases. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2442 | if (Base.isVirtual()) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2443 | HasVBPtr = true; |
| 2444 | continue; |
| 2445 | } |
| 2446 | // Check fo a base to share a VBPtr with. |
| 2447 | if (!SharedVBPtrBase && BaseLayout.hasVBPtr()) { |
| 2448 | SharedVBPtrBase = BaseDecl; |
| 2449 | HasVBPtr = true; |
| 2450 | } |
| 2451 | // Only lay out bases with extendable VFPtrs on the first pass. |
| 2452 | if (!BaseLayout.hasExtendableVFPtr()) |
| 2453 | continue; |
| 2454 | // If we don't have a primary base, this one qualifies. |
Warren Hunt | badf9e0 | 2014-01-13 19:55:52 +0000 | [diff] [blame] | 2455 | if (!PrimaryBase) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2456 | PrimaryBase = BaseDecl; |
Warren Hunt | badf9e0 | 2014-01-13 19:55:52 +0000 | [diff] [blame] | 2457 | LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase(); |
| 2458 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2459 | // Lay out the base. |
| 2460 | layoutNonVirtualBase(BaseDecl, BaseLayout, PreviousBaseLayout); |
| 2461 | } |
| 2462 | // Figure out if we need a fresh VFPtr for this class. |
| 2463 | if (!PrimaryBase && RD->isDynamicClass()) |
| 2464 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 2465 | e = RD->method_end(); |
| 2466 | !HasOwnVFPtr && i != e; ++i) |
| 2467 | HasOwnVFPtr = i->isVirtual() && i->size_overridden_methods() == 0; |
| 2468 | // If we don't have a primary base then we have a leading object that could |
| 2469 | // itself lead with a zero-sized object, something we track. |
| 2470 | bool CheckLeadingLayout = !PrimaryBase; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2471 | // Iterate through the bases and lay out the non-virtual ones. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2472 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 2473 | if (Base.isVirtual()) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2474 | continue; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2475 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2476 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
| 2477 | // Only lay out bases without extendable VFPtrs on the second pass. |
Warren Hunt | bb9c3c3 | 2014-04-10 23:23:34 +0000 | [diff] [blame] | 2478 | if (BaseLayout.hasExtendableVFPtr()) { |
| 2479 | VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize(); |
Warren Hunt | 4431fe6 | 2013-12-12 22:33:37 +0000 | [diff] [blame] | 2480 | continue; |
Warren Hunt | bb9c3c3 | 2014-04-10 23:23:34 +0000 | [diff] [blame] | 2481 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2482 | // If this is the first layout, check to see if it leads with a zero sized |
| 2483 | // object. If it does, so do we. |
| 2484 | if (CheckLeadingLayout) { |
| 2485 | CheckLeadingLayout = false; |
| 2486 | LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase(); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2487 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2488 | // Lay out the base. |
| 2489 | layoutNonVirtualBase(BaseDecl, BaseLayout, PreviousBaseLayout); |
Warren Hunt | bb9c3c3 | 2014-04-10 23:23:34 +0000 | [diff] [blame] | 2490 | VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2491 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2492 | // Set our VBPtroffset if we know it at this point. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2493 | if (!HasVBPtr) |
| 2494 | VBPtrOffset = CharUnits::fromQuantity(-1); |
Warren Hunt | 6eba907 | 2014-01-14 00:31:30 +0000 | [diff] [blame] | 2495 | else if (SharedVBPtrBase) { |
| 2496 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(SharedVBPtrBase); |
| 2497 | VBPtrOffset = Bases[SharedVBPtrBase] + Layout.getVBPtrOffset(); |
| 2498 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | void MicrosoftRecordLayoutBuilder::layoutNonVirtualBase( |
| 2502 | const CXXRecordDecl *BaseDecl, |
| 2503 | const ASTRecordLayout &BaseLayout, |
| 2504 | const ASTRecordLayout *&PreviousBaseLayout) { |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2505 | // Insert padding between two bases if the left first one is zero sized or |
| 2506 | // contains a zero sized subobject and the right is zero sized or one leads |
| 2507 | // with a zero sized base. |
| 2508 | if (PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() && |
| 2509 | BaseLayout.leadsWithZeroSizedBase()) |
| 2510 | Size++; |
| 2511 | ElementInfo Info = getAdjustedElementInfo(BaseLayout); |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2512 | CharUnits BaseOffset; |
| 2513 | |
| 2514 | // Respect the external AST source base offset, if present. |
| 2515 | bool FoundBase = false; |
| 2516 | if (UseExternalLayout) { |
| 2517 | FoundBase = External.getExternalNVBaseOffset(BaseDecl, BaseOffset); |
| 2518 | if (FoundBase) |
| 2519 | assert(BaseOffset >= Size && "base offset already allocated"); |
| 2520 | } |
| 2521 | |
| 2522 | if (!FoundBase) |
| 2523 | BaseOffset = Size.RoundUpToAlignment(Info.Alignment); |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2524 | Bases.insert(std::make_pair(BaseDecl, BaseOffset)); |
Warren Hunt | f6ec748 | 2014-02-21 01:40:35 +0000 | [diff] [blame] | 2525 | Size = BaseOffset + BaseLayout.getNonVirtualSize(); |
Warren Hunt | f4518def | 2014-01-10 01:28:05 +0000 | [diff] [blame] | 2526 | PreviousBaseLayout = &BaseLayout; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | void MicrosoftRecordLayoutBuilder::layoutFields(const RecordDecl *RD) { |
| 2530 | LastFieldIsNonZeroWidthBitfield = false; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2531 | for (const FieldDecl *Field : RD->fields()) |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2532 | layoutField(Field); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) { |
| 2536 | if (FD->isBitField()) { |
| 2537 | layoutBitField(FD); |
| 2538 | return; |
| 2539 | } |
| 2540 | LastFieldIsNonZeroWidthBitfield = false; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2541 | ElementInfo Info = getAdjustedElementInfo(FD); |
David Majnemer | adc45bb | 2014-04-13 08:15:50 +0000 | [diff] [blame] | 2542 | Alignment = std::max(Alignment, Info.Alignment); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2543 | if (IsUnion) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2544 | placeFieldAtOffset(CharUnits::Zero()); |
| 2545 | Size = std::max(Size, Info.Size); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2546 | } else { |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2547 | CharUnits FieldOffset; |
| 2548 | if (UseExternalLayout) { |
| 2549 | FieldOffset = |
| 2550 | Context.toCharUnitsFromBits(External.getExternalFieldOffset(FD)); |
| 2551 | assert(FieldOffset >= Size && "field offset already allocated"); |
| 2552 | } else { |
| 2553 | FieldOffset = Size.RoundUpToAlignment(Info.Alignment); |
| 2554 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2555 | placeFieldAtOffset(FieldOffset); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2556 | Size = FieldOffset + Info.Size; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) { |
| 2561 | unsigned Width = FD->getBitWidthValue(Context); |
| 2562 | if (Width == 0) { |
| 2563 | layoutZeroWidthBitField(FD); |
| 2564 | return; |
| 2565 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2566 | ElementInfo Info = getAdjustedElementInfo(FD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2567 | // Clamp the bitfield to a containable size for the sake of being able |
| 2568 | // to lay them out. Sema will throw an error. |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2569 | if (Width > Context.toBits(Info.Size)) |
| 2570 | Width = Context.toBits(Info.Size); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2571 | // Check to see if this bitfield fits into an existing allocation. Note: |
| 2572 | // MSVC refuses to pack bitfields of formal types with different sizes |
| 2573 | // into the same allocation. |
| 2574 | if (!IsUnion && LastFieldIsNonZeroWidthBitfield && |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2575 | CurrentBitfieldSize == Info.Size && Width <= RemainingBitsInField) { |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2576 | placeFieldAtBitOffset(Context.toBits(Size) - RemainingBitsInField); |
| 2577 | RemainingBitsInField -= Width; |
| 2578 | return; |
| 2579 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2580 | LastFieldIsNonZeroWidthBitfield = true; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2581 | CurrentBitfieldSize = Info.Size; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2582 | if (IsUnion) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2583 | placeFieldAtOffset(CharUnits::Zero()); |
| 2584 | Size = std::max(Size, Info.Size); |
David Majnemer | adc45bb | 2014-04-13 08:15:50 +0000 | [diff] [blame] | 2585 | // TODO: Add a Sema warning that MS ignores bitfield alignment in unions. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2586 | } else { |
| 2587 | // Allocate a new block of memory and place the bitfield in it. |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2588 | CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2589 | placeFieldAtOffset(FieldOffset); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2590 | Size = FieldOffset + Info.Size; |
David Majnemer | adc45bb | 2014-04-13 08:15:50 +0000 | [diff] [blame] | 2591 | Alignment = std::max(Alignment, Info.Alignment); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2592 | RemainingBitsInField = Context.toBits(Info.Size) - Width; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | void |
| 2597 | MicrosoftRecordLayoutBuilder::layoutZeroWidthBitField(const FieldDecl *FD) { |
| 2598 | // Zero-width bitfields are ignored unless they follow a non-zero-width |
| 2599 | // bitfield. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2600 | if (!LastFieldIsNonZeroWidthBitfield) { |
| 2601 | placeFieldAtOffset(IsUnion ? CharUnits::Zero() : Size); |
| 2602 | // TODO: Add a Sema warning that MS ignores alignment for zero |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2603 | // sized bitfields that occur after zero-size bitfields or non-bitfields. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2604 | return; |
| 2605 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2606 | LastFieldIsNonZeroWidthBitfield = false; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2607 | ElementInfo Info = getAdjustedElementInfo(FD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2608 | if (IsUnion) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2609 | placeFieldAtOffset(CharUnits::Zero()); |
| 2610 | Size = std::max(Size, Info.Size); |
David Majnemer | adc45bb | 2014-04-13 08:15:50 +0000 | [diff] [blame] | 2611 | // TODO: Add a Sema warning that MS ignores bitfield alignment in unions. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2612 | } else { |
| 2613 | // Round up the current record size to the field's alignment boundary. |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2614 | CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2615 | placeFieldAtOffset(FieldOffset); |
| 2616 | Size = FieldOffset; |
David Majnemer | adc45bb | 2014-04-13 08:15:50 +0000 | [diff] [blame] | 2617 | Alignment = std::max(Alignment, Info.Alignment); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2618 | } |
| 2619 | } |
| 2620 | |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2621 | void MicrosoftRecordLayoutBuilder::injectVBPtr(const CXXRecordDecl *RD) { |
Warren Hunt | 6eba907 | 2014-01-14 00:31:30 +0000 | [diff] [blame] | 2622 | if (!HasVBPtr || SharedVBPtrBase) |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2623 | return; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2624 | // Inject the VBPointer at the injection site. |
| 2625 | CharUnits InjectionSite = VBPtrOffset; |
| 2626 | // But before we do, make sure it's properly aligned. |
| 2627 | VBPtrOffset = VBPtrOffset.RoundUpToAlignment(PointerInfo.Alignment); |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2628 | // Shift everything after the vbptr down, unless we're using an external |
| 2629 | // layout. |
| 2630 | if (UseExternalLayout) |
| 2631 | return; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2632 | // Determine where the first field should be laid out after the vbptr. |
| 2633 | CharUnits FieldStart = VBPtrOffset + PointerInfo.Size; |
| 2634 | // Make sure that the amount we push the fields back by is a multiple of the |
| 2635 | // alignment. |
David Majnemer | 79a1c89 | 2014-02-12 00:43:02 +0000 | [diff] [blame] | 2636 | CharUnits Offset = (FieldStart - InjectionSite).RoundUpToAlignment( |
| 2637 | std::max(RequiredAlignment, Alignment)); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2638 | Size += Offset; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2639 | for (uint64_t &FieldOffset : FieldOffsets) |
| 2640 | FieldOffset += Context.toBits(Offset); |
| 2641 | for (BaseOffsetsMapTy::value_type &Base : Bases) |
| 2642 | if (Base.second >= InjectionSite) |
| 2643 | Base.second += Offset; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | void MicrosoftRecordLayoutBuilder::injectVFPtr(const CXXRecordDecl *RD) { |
| 2647 | if (!HasOwnVFPtr) |
| 2648 | return; |
| 2649 | // Make sure that the amount we push the struct back by is a multiple of the |
| 2650 | // alignment. |
David Majnemer | 79a1c89 | 2014-02-12 00:43:02 +0000 | [diff] [blame] | 2651 | CharUnits Offset = PointerInfo.Size.RoundUpToAlignment( |
| 2652 | std::max(RequiredAlignment, Alignment)); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2653 | // Increase the size of the object and push back all fields, the vbptr and all |
| 2654 | // bases by the offset amount. |
| 2655 | Size += Offset; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2656 | for (uint64_t &FieldOffset : FieldOffsets) |
| 2657 | FieldOffset += Context.toBits(Offset); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2658 | if (HasVBPtr) |
| 2659 | VBPtrOffset += Offset; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2660 | for (BaseOffsetsMapTy::value_type &Base : Bases) |
| 2661 | Base.second += Offset; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2664 | void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) { |
| 2665 | if (!HasVBPtr) |
| 2666 | return; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2667 | // Vtordisps are always 4 bytes (even in 64-bit mode) |
| 2668 | CharUnits VtorDispSize = CharUnits::fromQuantity(4); |
| 2669 | CharUnits VtorDispAlignment = VtorDispSize; |
| 2670 | // vtordisps respect pragma pack. |
| 2671 | if (!MaxFieldAlignment.isZero()) |
| 2672 | VtorDispAlignment = std::min(VtorDispAlignment, MaxFieldAlignment); |
| 2673 | // The alignment of the vtordisp is at least the required alignment of the |
| 2674 | // entire record. This requirement may be present to support vtordisp |
| 2675 | // injection. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2676 | for (const CXXBaseSpecifier &VBase : RD->vbases()) { |
| 2677 | const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl(); |
David Majnemer | 79a1c89 | 2014-02-12 00:43:02 +0000 | [diff] [blame] | 2678 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
| 2679 | RequiredAlignment = |
| 2680 | std::max(RequiredAlignment, BaseLayout.getRequiredAlignment()); |
| 2681 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2682 | VtorDispAlignment = std::max(VtorDispAlignment, RequiredAlignment); |
| 2683 | // Compute the vtordisp set. |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2684 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtorDispSet; |
| 2685 | computeVtorDispSet(HasVtorDispSet, RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2686 | // Iterate through the virtual bases and lay them out. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2687 | const ASTRecordLayout *PreviousBaseLayout = nullptr; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2688 | for (const CXXBaseSpecifier &VBase : RD->vbases()) { |
| 2689 | const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2690 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2691 | bool HasVtordisp = HasVtorDispSet.count(BaseDecl) > 0; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2692 | // Insert padding between two bases if the left first one is zero sized or |
| 2693 | // contains a zero sized subobject and the right is zero sized or one leads |
| 2694 | // with a zero sized base. The padding between virtual bases is 4 |
| 2695 | // bytes (in both 32 and 64 bits modes) and always involves rounding up to |
| 2696 | // the required alignment, we don't know why. |
Warren Hunt | 4f7efb7 | 2014-04-12 00:20:50 +0000 | [diff] [blame] | 2697 | if ((PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() && |
David Majnemer | bf3d430 | 2014-07-16 07:16:58 +0000 | [diff] [blame] | 2698 | BaseLayout.leadsWithZeroSizedBase()) || HasVtordisp) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2699 | Size = Size.RoundUpToAlignment(VtorDispAlignment) + VtorDispSize; |
David Majnemer | a246468 | 2014-07-17 00:55:19 +0000 | [diff] [blame] | 2700 | Alignment = std::max(VtorDispAlignment, Alignment); |
David Majnemer | bf3d430 | 2014-07-16 07:16:58 +0000 | [diff] [blame] | 2701 | } |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2702 | // Insert the virtual base. |
| 2703 | ElementInfo Info = getAdjustedElementInfo(BaseLayout); |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2704 | CharUnits BaseOffset; |
| 2705 | |
| 2706 | // Respect the external AST source base offset, if present. |
| 2707 | bool FoundBase = false; |
| 2708 | if (UseExternalLayout) { |
| 2709 | FoundBase = External.getExternalVBaseOffset(BaseDecl, BaseOffset); |
| 2710 | if (FoundBase) |
| 2711 | assert(BaseOffset >= Size && "base offset already allocated"); |
| 2712 | } |
| 2713 | if (!FoundBase) |
| 2714 | BaseOffset = Size.RoundUpToAlignment(Info.Alignment); |
| 2715 | |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2716 | VBases.insert(std::make_pair(BaseDecl, |
| 2717 | ASTRecordLayout::VBaseInfo(BaseOffset, HasVtordisp))); |
Warren Hunt | f6ec748 | 2014-02-21 01:40:35 +0000 | [diff] [blame] | 2718 | Size = BaseOffset + BaseLayout.getNonVirtualSize(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2719 | PreviousBaseLayout = &BaseLayout; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2720 | } |
| 2721 | } |
| 2722 | |
Warren Hunt | c338431 | 2013-12-11 22:28:32 +0000 | [diff] [blame] | 2723 | void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) { |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2724 | // Respect required alignment. Note that in 32-bit mode Required alignment |
David Majnemer | 00a061d | 2014-09-30 06:45:43 +0000 | [diff] [blame] | 2725 | // may be 0 and cause size not to be updated. |
Warren Hunt | f6ec748 | 2014-02-21 01:40:35 +0000 | [diff] [blame] | 2726 | DataSize = Size; |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2727 | if (!RequiredAlignment.isZero()) { |
| 2728 | Alignment = std::max(Alignment, RequiredAlignment); |
Warren Hunt | 5d9eebf | 2014-04-10 22:15:18 +0000 | [diff] [blame] | 2729 | auto RoundingAlignment = Alignment; |
| 2730 | if (!MaxFieldAlignment.isZero()) |
| 2731 | RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment); |
| 2732 | RoundingAlignment = std::max(RoundingAlignment, RequiredAlignment); |
| 2733 | Size = Size.RoundUpToAlignment(RoundingAlignment); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2734 | } |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2735 | if (Size.isZero()) { |
Warren Hunt | 39a907b | 2014-04-09 21:57:24 +0000 | [diff] [blame] | 2736 | EndsWithZeroSizedObject = true; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2737 | LeadsWithZeroSizedBase = true; |
David Majnemer | 00a061d | 2014-09-30 06:45:43 +0000 | [diff] [blame] | 2738 | // Zero-sized structures have size equal to their alignment if a |
| 2739 | // __declspec(align) came into play. |
| 2740 | if (RequiredAlignment >= MinEmptyStructSize) |
| 2741 | Size = Alignment; |
| 2742 | else |
| 2743 | Size = MinEmptyStructSize; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2744 | } |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2745 | |
| 2746 | if (UseExternalLayout) { |
| 2747 | Size = Context.toCharUnitsFromBits(External.Size); |
| 2748 | if (External.Align) |
| 2749 | Alignment = Context.toCharUnitsFromBits(External.Align); |
| 2750 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Warren Hunt | 73f4398 | 2014-04-11 22:05:28 +0000 | [diff] [blame] | 2753 | // Recursively walks the non-virtual bases of a class and determines if any of |
| 2754 | // them are in the bases with overridden methods set. |
David Majnemer | 1272764 | 2014-07-16 06:30:31 +0000 | [diff] [blame] | 2755 | static bool |
| 2756 | RequiresVtordisp(const llvm::SmallPtrSetImpl<const CXXRecordDecl *> & |
| 2757 | BasesWithOverriddenMethods, |
| 2758 | const CXXRecordDecl *RD) { |
Warren Hunt | 73f4398 | 2014-04-11 22:05:28 +0000 | [diff] [blame] | 2759 | if (BasesWithOverriddenMethods.count(RD)) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2760 | return true; |
| 2761 | // If any of a virtual bases non-virtual bases (recursively) requires a |
| 2762 | // vtordisp than so does this virtual base. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2763 | for (const CXXBaseSpecifier &Base : RD->bases()) |
| 2764 | if (!Base.isVirtual() && |
Warren Hunt | 73f4398 | 2014-04-11 22:05:28 +0000 | [diff] [blame] | 2765 | RequiresVtordisp(BasesWithOverriddenMethods, |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2766 | Base.getType()->getAsCXXRecordDecl())) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2767 | return true; |
| 2768 | return false; |
| 2769 | } |
| 2770 | |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2771 | void MicrosoftRecordLayoutBuilder::computeVtorDispSet( |
| 2772 | llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtordispSet, |
| 2773 | const CXXRecordDecl *RD) const { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2774 | // /vd2 or #pragma vtordisp(2): Always use vtordisps for virtual bases with |
| 2775 | // vftables. |
| 2776 | if (RD->getMSVtorDispMode() == MSVtorDispAttr::ForVFTable) { |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2777 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
| 2778 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2779 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 2780 | if (Layout.hasExtendableVFPtr()) |
| 2781 | HasVtordispSet.insert(BaseDecl); |
| 2782 | } |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2783 | return; |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2786 | // If any of our bases need a vtordisp for this type, so do we. Check our |
| 2787 | // direct bases for vtordisp requirements. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2788 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 2789 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2790 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 2791 | for (const auto &bi : Layout.getVBaseOffsetsMap()) |
| 2792 | if (bi.second.hasVtorDisp()) |
| 2793 | HasVtordispSet.insert(bi.first); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2794 | } |
David Majnemer | d43388c | 2014-04-13 02:27:32 +0000 | [diff] [blame] | 2795 | // We don't introduce any additional vtordisps if either: |
| 2796 | // * A user declared constructor or destructor aren't declared. |
| 2797 | // * #pragma vtordisp(0) or the /vd0 flag are in use. |
| 2798 | if ((!RD->hasUserDeclaredConstructor() && !RD->hasUserDeclaredDestructor()) || |
| 2799 | RD->getMSVtorDispMode() == MSVtorDispAttr::Never) |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2800 | return; |
David Majnemer | d43388c | 2014-04-13 02:27:32 +0000 | [diff] [blame] | 2801 | // /vd1 or #pragma vtordisp(1): Try to guess based on whether we think it's |
| 2802 | // possible for a partially constructed object with virtual base overrides to |
| 2803 | // escape a non-trivial constructor. |
| 2804 | assert(RD->getMSVtorDispMode() == MSVtorDispAttr::ForVBaseOverride); |
Warren Hunt | 73f4398 | 2014-04-11 22:05:28 +0000 | [diff] [blame] | 2805 | // Compute a set of base classes which define methods we override. A virtual |
| 2806 | // base in this set will require a vtordisp. A virtual base that transitively |
| 2807 | // contains one of these bases as a non-virtual base will also require a |
| 2808 | // vtordisp. |
| 2809 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> Work; |
| 2810 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> BasesWithOverriddenMethods; |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2811 | // Seed the working set with our non-destructor, non-pure virtual methods. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2812 | for (const CXXMethodDecl *MD : RD->methods()) |
David Majnemer | c2e6753 | 2014-09-23 22:58:15 +0000 | [diff] [blame] | 2813 | if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD) && !MD->isPure()) |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2814 | Work.insert(MD); |
Warren Hunt | 73f4398 | 2014-04-11 22:05:28 +0000 | [diff] [blame] | 2815 | while (!Work.empty()) { |
| 2816 | const CXXMethodDecl *MD = *Work.begin(); |
| 2817 | CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(), |
| 2818 | e = MD->end_overridden_methods(); |
| 2819 | // If a virtual method has no-overrides it lives in its parent's vtable. |
| 2820 | if (i == e) |
| 2821 | BasesWithOverriddenMethods.insert(MD->getParent()); |
| 2822 | else |
| 2823 | Work.insert(i, e); |
| 2824 | // We've finished processing this element, remove it from the working set. |
| 2825 | Work.erase(MD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2826 | } |
Warren Hunt | 73f4398 | 2014-04-11 22:05:28 +0000 | [diff] [blame] | 2827 | // For each of our virtual bases, check if it is in the set of overridden |
| 2828 | // bases or if it transitively contains a non-virtual base that is. |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 2829 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
| 2830 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2831 | if (!HasVtordispSet.count(BaseDecl) && |
Warren Hunt | 73f4398 | 2014-04-11 22:05:28 +0000 | [diff] [blame] | 2832 | RequiresVtordisp(BasesWithOverriddenMethods, BaseDecl)) |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2833 | HasVtordispSet.insert(BaseDecl); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2834 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /// \brief Get or compute information about the layout of the specified record |
| 2838 | /// (struct/union/class), which indicates its size and field position |
| 2839 | /// information. |
| 2840 | const ASTRecordLayout * |
| 2841 | ASTContext::BuildMicrosoftASTRecordLayout(const RecordDecl *D) const { |
| 2842 | MicrosoftRecordLayoutBuilder Builder(*this); |
| 2843 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 2844 | Builder.cxxLayout(RD); |
| 2845 | return new (*this) ASTRecordLayout( |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2846 | *this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment, |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2847 | Builder.HasOwnVFPtr, |
| 2848 | Builder.HasOwnVFPtr || Builder.PrimaryBase, |
Warren Hunt | f6ec748 | 2014-02-21 01:40:35 +0000 | [diff] [blame] | 2849 | Builder.VBPtrOffset, Builder.NonVirtualSize, Builder.FieldOffsets.data(), |
| 2850 | Builder.FieldOffsets.size(), Builder.NonVirtualSize, |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 2851 | Builder.Alignment, CharUnits::Zero(), Builder.PrimaryBase, |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2852 | false, Builder.SharedVBPtrBase, |
Warren Hunt | 39a907b | 2014-04-09 21:57:24 +0000 | [diff] [blame] | 2853 | Builder.EndsWithZeroSizedObject, Builder.LeadsWithZeroSizedBase, |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2854 | Builder.Bases, Builder.VBases); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2855 | } else { |
| 2856 | Builder.layout(D); |
| 2857 | return new (*this) ASTRecordLayout( |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2858 | *this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment, |
| 2859 | Builder.Size, Builder.FieldOffsets.data(), Builder.FieldOffsets.size()); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2863 | /// getASTRecordLayout - Get or compute information about the layout of the |
| 2864 | /// specified record (struct/union/class), which indicates its size and field |
| 2865 | /// position information. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2866 | const ASTRecordLayout & |
| 2867 | ASTContext::getASTRecordLayout(const RecordDecl *D) const { |
John McCall | 0710e55 | 2011-10-07 02:39:22 +0000 | [diff] [blame] | 2868 | // These asserts test different things. A record has a definition |
| 2869 | // as soon as we begin to parse the definition. That definition is |
| 2870 | // not a complete definition (which is what isDefinition() tests) |
| 2871 | // until we *finish* parsing the definition. |
Sean Callanan | 56c1989 | 2012-02-08 00:04:52 +0000 | [diff] [blame] | 2872 | |
| 2873 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
| 2874 | getExternalSource()->CompleteType(const_cast<RecordDecl*>(D)); |
| 2875 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2876 | D = D->getDefinition(); |
| 2877 | assert(D && "Cannot get layout of forward declarations!"); |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 2878 | assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!"); |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2879 | assert(D->isCompleteDefinition() && "Cannot layout type before complete!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2880 | |
| 2881 | // Look up this layout, if already laid out, return what we have. |
| 2882 | // Note that we can't save a reference to the entry because this function |
| 2883 | // is recursive. |
| 2884 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
| 2885 | if (Entry) return *Entry; |
| 2886 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2887 | const ASTRecordLayout *NewEntry = nullptr; |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2888 | |
Reid Kleckner | 8b6d034 | 2015-02-25 19:17:45 +0000 | [diff] [blame] | 2889 | if (isMsLayout(D)) { |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2890 | NewEntry = BuildMicrosoftASTRecordLayout(D); |
| 2891 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2892 | EmptySubobjectMap EmptySubobjects(*this, RD); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2893 | RecordLayoutBuilder Builder(*this, &EmptySubobjects); |
| 2894 | Builder.Layout(RD); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 2895 | |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 2896 | // In certain situations, we are allowed to lay out objects in the |
| 2897 | // tail-padding of base classes. This is ABI-dependent. |
| 2898 | // FIXME: this should be stored in the record layout. |
| 2899 | bool skipTailPadding = |
| 2900 | mustSkipTailPadding(getTargetInfo().getCXXABI(), cast<CXXRecordDecl>(D)); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2901 | |
| 2902 | // FIXME: This should be done in FinalizeLayout. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2903 | CharUnits DataSize = |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 2904 | skipTailPadding ? Builder.getSize() : Builder.getDataSize(); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2905 | CharUnits NonVirtualSize = |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 2906 | skipTailPadding ? DataSize : Builder.NonVirtualSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2907 | NewEntry = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2908 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
| 2909 | Builder.Alignment, |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2910 | /*RequiredAlignment : used by MS-ABI)*/ |
| 2911 | Builder.Alignment, |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 2912 | Builder.HasOwnVFPtr, |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2913 | RD->isDynamicClass(), |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2914 | CharUnits::fromQuantity(-1), |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2915 | DataSize, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2916 | Builder.FieldOffsets.data(), |
| 2917 | Builder.FieldOffsets.size(), |
Ken Dyck | af1c83f | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 2918 | NonVirtualSize, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2919 | Builder.NonVirtualAlignment, |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2920 | EmptySubobjects.SizeOfLargestEmptySubobject, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2921 | Builder.PrimaryBase, |
| 2922 | Builder.PrimaryBaseIsVirtual, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2923 | nullptr, false, false, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2924 | Builder.Bases, Builder.VBases); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2925 | } else { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2926 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2927 | Builder.Layout(D); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2928 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2929 | NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2930 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2931 | Builder.Alignment, |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2932 | /*RequiredAlignment : used by MS-ABI)*/ |
| 2933 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2934 | Builder.getSize(), |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2935 | Builder.FieldOffsets.data(), |
| 2936 | Builder.FieldOffsets.size()); |
| 2937 | } |
| 2938 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2939 | ASTRecordLayouts[D] = NewEntry; |
| 2940 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2941 | if (getLangOpts().DumpRecordLayouts) { |
Argyrios Kyrtzidis | 8ade08e | 2013-07-12 22:30:03 +0000 | [diff] [blame] | 2942 | llvm::outs() << "\n*** Dumping AST Record Layout\n"; |
| 2943 | DumpRecordLayout(D, llvm::outs(), getLangOpts().DumpRecordLayoutsSimple); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | return *NewEntry; |
| 2947 | } |
| 2948 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2949 | const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) { |
Reid Kleckner | 5d7f298 | 2013-05-29 16:18:30 +0000 | [diff] [blame] | 2950 | if (!getTargetInfo().getCXXABI().hasKeyFunctions()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2951 | return nullptr; |
Reid Kleckner | 5d7f298 | 2013-05-29 16:18:30 +0000 | [diff] [blame] | 2952 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2953 | assert(RD->getDefinition() && "Cannot get key function for forward decl!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2954 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2955 | |
Richard Smith | a9a1c68 | 2014-07-07 06:38:20 +0000 | [diff] [blame] | 2956 | // Beware: |
| 2957 | // 1) computing the key function might trigger deserialization, which might |
| 2958 | // invalidate iterators into KeyFunctions |
| 2959 | // 2) 'get' on the LazyDeclPtr might also trigger deserialization and |
| 2960 | // invalidate the LazyDeclPtr within the map itself |
| 2961 | LazyDeclPtr Entry = KeyFunctions[RD]; |
| 2962 | const Decl *Result = |
| 2963 | Entry ? Entry.get(getExternalSource()) : computeKeyFunction(*this, RD); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2964 | |
Richard Smith | a9a1c68 | 2014-07-07 06:38:20 +0000 | [diff] [blame] | 2965 | // Store it back if it changed. |
| 2966 | if (Entry.isOffset() || Entry.isValid() != bool(Result)) |
| 2967 | KeyFunctions[RD] = const_cast<Decl*>(Result); |
| 2968 | |
| 2969 | return cast_or_null<CXXMethodDecl>(Result); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2972 | void ASTContext::setNonKeyFunction(const CXXMethodDecl *Method) { |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2973 | assert(Method == Method->getFirstDecl() && |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2974 | "not working with method declaration from class definition"); |
| 2975 | |
| 2976 | // Look up the cache entry. Since we're working with the first |
| 2977 | // declaration, its parent must be the class definition, which is |
| 2978 | // the correct key for the KeyFunctions hash. |
Reid Kleckner | b4a26ed | 2015-05-21 00:12:53 +0000 | [diff] [blame^] | 2979 | const auto &Map = KeyFunctions; |
| 2980 | auto I = Map.find(Method->getParent()); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2981 | |
| 2982 | // If it's not cached, there's nothing to do. |
Reid Kleckner | b4a26ed | 2015-05-21 00:12:53 +0000 | [diff] [blame^] | 2983 | if (I == Map.end()) return; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2984 | |
| 2985 | // If it is cached, check whether it's the target method, and if so, |
Richard Smith | a9a1c68 | 2014-07-07 06:38:20 +0000 | [diff] [blame] | 2986 | // remove it from the cache. Note, the call to 'get' might invalidate |
| 2987 | // the iterator and the LazyDeclPtr object within the map. |
| 2988 | LazyDeclPtr Ptr = I->second; |
| 2989 | if (Ptr.get(getExternalSource()) == Method) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2990 | // FIXME: remember that we did this for module / chained PCH state? |
Richard Smith | a9a1c68 | 2014-07-07 06:38:20 +0000 | [diff] [blame] | 2991 | KeyFunctions.erase(Method->getParent()); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2992 | } |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2995 | static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) { |
| 2996 | const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent()); |
| 2997 | return Layout.getFieldOffset(FD->getFieldIndex()); |
| 2998 | } |
| 2999 | |
| 3000 | uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const { |
| 3001 | uint64_t OffsetInBits; |
| 3002 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 3003 | OffsetInBits = ::getFieldOffset(*this, FD); |
| 3004 | } else { |
| 3005 | const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD); |
| 3006 | |
| 3007 | OffsetInBits = 0; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 3008 | for (const NamedDecl *ND : IFD->chain()) |
| 3009 | OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(ND)); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | return OffsetInBits; |
| 3013 | } |
| 3014 | |
Eric Christopher | 8a39a01 | 2011-10-05 06:00:51 +0000 | [diff] [blame] | 3015 | /// getObjCLayout - Get or compute information about the layout of the |
| 3016 | /// given interface. |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 3017 | /// |
| 3018 | /// \param Impl - If given, also include the layout of the interface's |
| 3019 | /// implementation. This may differ by including synthesized ivars. |
| 3020 | const ASTRecordLayout & |
| 3021 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3022 | const ObjCImplementationDecl *Impl) const { |
Douglas Gregor | 64d9257 | 2011-12-20 15:50:13 +0000 | [diff] [blame] | 3023 | // Retrieve the definition |
Sean Callanan | d9a909c | 2012-03-15 16:33:08 +0000 | [diff] [blame] | 3024 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
| 3025 | getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D)); |
Douglas Gregor | 64d9257 | 2011-12-20 15:50:13 +0000 | [diff] [blame] | 3026 | D = D->getDefinition(); |
| 3027 | assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 3028 | |
| 3029 | // Look up this layout, if already laid out, return what we have. |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 3030 | const ObjCContainerDecl *Key = |
| 3031 | Impl ? (const ObjCContainerDecl*) Impl : (const ObjCContainerDecl*) D; |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 3032 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 3033 | return *Entry; |
| 3034 | |
| 3035 | // Add in synthesized ivar count if laying out an implementation. |
| 3036 | if (Impl) { |
| 3037 | unsigned SynthCount = CountNonClassIvars(D); |
| 3038 | // If there aren't any sythesized ivars then reuse the interface |
| 3039 | // entry. Note we can't cache this because we simply free all |
| 3040 | // entries later; however we shouldn't look up implementations |
| 3041 | // frequently. |
| 3042 | if (SynthCount == 0) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3043 | return getObjCLayout(D, nullptr); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3046 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr); |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 3047 | Builder.Layout(D); |
| 3048 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 3049 | const ASTRecordLayout *NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 3050 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 3051 | Builder.Alignment, |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 3052 | /*RequiredAlignment : used by MS-ABI)*/ |
| 3053 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 3054 | Builder.getDataSize(), |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 3055 | Builder.FieldOffsets.data(), |
| 3056 | Builder.FieldOffsets.size()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 3057 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 3058 | ObjCLayouts[Key] = NewEntry; |
| 3059 | |
| 3060 | return *NewEntry; |
| 3061 | } |
| 3062 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3063 | static void PrintOffset(raw_ostream &OS, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3064 | CharUnits Offset, unsigned IndentLevel) { |
Benjamin Kramer | 96ad717 | 2011-11-05 09:02:52 +0000 | [diff] [blame] | 3065 | OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity()); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3066 | OS.indent(IndentLevel * 2); |
| 3067 | } |
| 3068 | |
Eli Bendersky | f6f93ee | 2012-12-08 00:07:24 +0000 | [diff] [blame] | 3069 | static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) { |
| 3070 | OS << " | "; |
| 3071 | OS.indent(IndentLevel * 2); |
| 3072 | } |
| 3073 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3074 | static void DumpCXXRecordLayout(raw_ostream &OS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3075 | const CXXRecordDecl *RD, const ASTContext &C, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3076 | CharUnits Offset, |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3077 | unsigned IndentLevel, |
| 3078 | const char* Description, |
| 3079 | bool IncludeVirtualBases) { |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3080 | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3081 | |
| 3082 | PrintOffset(OS, Offset, IndentLevel); |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 3083 | OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3084 | if (Description) |
| 3085 | OS << ' ' << Description; |
| 3086 | if (RD->isEmpty()) |
| 3087 | OS << " (empty)"; |
| 3088 | OS << '\n'; |
| 3089 | |
| 3090 | IndentLevel++; |
| 3091 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3092 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3093 | bool HasOwnVFPtr = Layout.hasOwnVFPtr(); |
| 3094 | bool HasOwnVBPtr = Layout.hasOwnVBPtr(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3095 | |
| 3096 | // Vtable pointer. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3097 | if (RD->isDynamicClass() && !PrimaryBase && !isMsLayout(RD)) { |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3098 | PrintOffset(OS, Offset, IndentLevel); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 3099 | OS << '(' << *RD << " vtable pointer)\n"; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3100 | } else if (HasOwnVFPtr) { |
| 3101 | PrintOffset(OS, Offset, IndentLevel); |
| 3102 | // vfptr (for Microsoft C++ ABI) |
| 3103 | OS << '(' << *RD << " vftable pointer)\n"; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3104 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3105 | |
Reid Kleckner | ad59deb | 2014-02-28 01:03:09 +0000 | [diff] [blame] | 3106 | // Collect nvbases. |
| 3107 | SmallVector<const CXXRecordDecl *, 4> Bases; |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 3108 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 3109 | assert(!Base.getType()->isDependentType() && |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3110 | "Cannot layout class with dependent bases."); |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 3111 | if (!Base.isVirtual()) |
| 3112 | Bases.push_back(Base.getType()->getAsCXXRecordDecl()); |
Reid Kleckner | ad59deb | 2014-02-28 01:03:09 +0000 | [diff] [blame] | 3113 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3114 | |
Reid Kleckner | ad59deb | 2014-02-28 01:03:09 +0000 | [diff] [blame] | 3115 | // Sort nvbases by offset. |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 3116 | std::stable_sort(Bases.begin(), Bases.end(), |
| 3117 | [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { |
| 3118 | return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R); |
| 3119 | }); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3120 | |
Reid Kleckner | ad59deb | 2014-02-28 01:03:09 +0000 | [diff] [blame] | 3121 | // Dump (non-virtual) bases |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 3122 | for (const CXXRecordDecl *Base : Bases) { |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3123 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3124 | DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
| 3125 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 3126 | /*IncludeVirtualBases=*/false); |
| 3127 | } |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 3128 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3129 | // vbptr (for Microsoft C++ ABI) |
| 3130 | if (HasOwnVBPtr) { |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 3131 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 3132 | OS << '(' << *RD << " vbtable pointer)\n"; |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 3133 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3134 | |
| 3135 | // Dump fields. |
| 3136 | uint64_t FieldNo = 0; |
| 3137 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 3138 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3139 | const FieldDecl &Field = **I; |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3140 | CharUnits FieldOffset = Offset + |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 3141 | C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo)); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3142 | |
Reid Kleckner | cd612ab | 2014-04-11 16:57:42 +0000 | [diff] [blame] | 3143 | if (const CXXRecordDecl *D = Field.getType()->getAsCXXRecordDecl()) { |
| 3144 | DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel, |
| 3145 | Field.getName().data(), |
| 3146 | /*IncludeVirtualBases=*/true); |
| 3147 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | PrintOffset(OS, FieldOffset, IndentLevel); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3151 | OS << Field.getType().getAsString() << ' ' << Field << '\n'; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | if (!IncludeVirtualBases) |
| 3155 | return; |
| 3156 | |
| 3157 | // Dump virtual bases. |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 3158 | const ASTRecordLayout::VBaseOffsetsMapTy &vtordisps = |
| 3159 | Layout.getVBaseOffsetsMap(); |
David Majnemer | c964b4b | 2014-07-16 06:04:00 +0000 | [diff] [blame] | 3160 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
| 3161 | assert(Base.isVirtual() && "Found non-virtual class!"); |
| 3162 | const CXXRecordDecl *VBase = Base.getType()->getAsCXXRecordDecl(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3163 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3164 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 3165 | |
| 3166 | if (vtordisps.find(VBase)->second.hasVtorDisp()) { |
| 3167 | PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel); |
| 3168 | OS << "(vtordisp for vbase " << *VBase << ")\n"; |
| 3169 | } |
| 3170 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3171 | DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
| 3172 | VBase == PrimaryBase ? |
| 3173 | "(primary virtual base)" : "(virtual base)", |
| 3174 | /*IncludeVirtualBases=*/false); |
| 3175 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3176 | |
Eli Bendersky | f6f93ee | 2012-12-08 00:07:24 +0000 | [diff] [blame] | 3177 | PrintIndentNoOffset(OS, IndentLevel - 1); |
| 3178 | OS << "[sizeof=" << Layout.getSize().getQuantity(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3179 | if (!isMsLayout(RD)) |
| 3180 | OS << ", dsize=" << Layout.getDataSize().getQuantity(); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 3181 | OS << ", align=" << Layout.getAlignment().getQuantity() << '\n'; |
Eli Bendersky | f6f93ee | 2012-12-08 00:07:24 +0000 | [diff] [blame] | 3182 | |
| 3183 | PrintIndentNoOffset(OS, IndentLevel - 1); |
| 3184 | OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity(); |
Warren Hunt | d640d7d | 2014-01-09 00:30:56 +0000 | [diff] [blame] | 3185 | OS << ", nvalign=" << Layout.getNonVirtualAlignment().getQuantity() << "]\n"; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3186 | } |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3187 | |
| 3188 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 3189 | raw_ostream &OS, |
| 3190 | bool Simple) const { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3191 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 3192 | |
| 3193 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 3194 | if (!Simple) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3195 | return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, nullptr, |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 3196 | /*IncludeVirtualBases=*/true); |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3197 | |
| 3198 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 3199 | if (!Simple) { |
| 3200 | OS << "Record: "; |
| 3201 | RD->dump(); |
| 3202 | } |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3203 | OS << "\nLayout: "; |
| 3204 | OS << "<ASTRecordLayout\n"; |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 3205 | OS << " Size:" << toBits(Info.getSize()) << "\n"; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3206 | if (!isMsLayout(RD)) |
| 3207 | OS << " DataSize:" << toBits(Info.getDataSize()) << "\n"; |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 3208 | OS << " Alignment:" << toBits(Info.getAlignment()) << "\n"; |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3209 | OS << " FieldOffsets: ["; |
| 3210 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
| 3211 | if (i) OS << ", "; |
| 3212 | OS << Info.getFieldOffset(i); |
| 3213 | } |
| 3214 | OS << "]>\n"; |
| 3215 | } |