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