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