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