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 | |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1043 | const CXXRecordDecl *BaseDecl = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1044 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1045 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1046 | // Skip the primary base, because we've already laid it out. The |
| 1047 | // !PrimaryBaseIsVirtual check is required because we might have a |
| 1048 | // non-virtual base of the same type as a primary virtual base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1049 | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1050 | continue; |
| 1051 | |
| 1052 | // Lay out the base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1053 | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
| 1054 | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
| 1055 | |
| 1056 | LayoutNonVirtualBase(BaseInfo); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1060 | void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1061 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1062 | CharUnits Offset = LayoutBase(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1063 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1064 | // Add its base class offset. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1065 | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1066 | Bases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1067 | |
| 1068 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1069 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1070 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1071 | void |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1072 | RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1073 | CharUnits Offset) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1074 | // This base isn't interesting, it has no virtual bases. |
| 1075 | if (!Info->Class->getNumVBases()) |
| 1076 | return; |
| 1077 | |
| 1078 | // First, check if we have a virtual primary base to add offsets for. |
| 1079 | if (Info->PrimaryVirtualBaseInfo) { |
| 1080 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
| 1081 | "Primary virtual base is not virtual!"); |
| 1082 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
| 1083 | // Add the offset. |
| 1084 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
| 1085 | "primary vbase offset already exists!"); |
| 1086 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1087 | ASTRecordLayout::VBaseInfo(Offset, false))); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1088 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1089 | // Traverse the primary virtual base. |
| 1090 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
| 1091 | } |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1094 | // Now go through all direct non-virtual bases. |
| 1095 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
| 1096 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
| 1097 | const BaseSubobjectInfo *Base = Info->Bases[I]; |
| 1098 | if (Base->IsVirtual) |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1099 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1100 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 1101 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1102 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1107 | RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1108 | const CXXRecordDecl *MostDerivedClass) { |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1109 | const CXXRecordDecl *PrimaryBase; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1110 | bool PrimaryBaseIsVirtual; |
Anders Carlsson | fe90096 | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1111 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1112 | if (MostDerivedClass == RD) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1113 | PrimaryBase = this->PrimaryBase; |
| 1114 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1115 | } else { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1116 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1117 | PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 1118 | PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1121 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1122 | E = RD->bases_end(); I != E; ++I) { |
| 1123 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1124 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1125 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1126 | const CXXRecordDecl *BaseDecl = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1127 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1128 | |
| 1129 | if (I->isVirtual()) { |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1130 | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) { |
| 1131 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1132 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1133 | // Only lay out the virtual base if it's not an indirect primary base. |
| 1134 | if (!IndirectPrimaryBase) { |
| 1135 | // Only visit virtual bases once. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1136 | if (!VisitedVirtualBases.insert(BaseDecl)) |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1137 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1138 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1139 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1140 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1141 | LayoutVirtualBase(BaseInfo); |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 1142 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1143 | } |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 1144 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1145 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1146 | if (!BaseDecl->getNumVBases()) { |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1147 | // This base isn't interesting since it doesn't have any virtual bases. |
| 1148 | continue; |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 1149 | } |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1150 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1151 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1155 | void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1156 | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
| 1157 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1158 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1159 | CharUnits Offset = LayoutBase(Base); |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Add its base class offset. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1162 | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1163 | VBases.insert(std::make_pair(Base->Class, |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1164 | ASTRecordLayout::VBaseInfo(Offset, false))); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1165 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1166 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1169 | CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1170 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1172 | |
| 1173 | CharUnits Offset; |
| 1174 | |
| 1175 | // Query the external layout to see if it provides an offset. |
| 1176 | bool HasExternalLayout = false; |
| 1177 | if (ExternalLayout) { |
| 1178 | llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known; |
| 1179 | if (Base->IsVirtual) { |
| 1180 | Known = ExternalVirtualBaseOffsets.find(Base->Class); |
| 1181 | if (Known != ExternalVirtualBaseOffsets.end()) { |
| 1182 | Offset = Known->second; |
| 1183 | HasExternalLayout = true; |
| 1184 | } |
| 1185 | } else { |
| 1186 | Known = ExternalBaseOffsets.find(Base->Class); |
| 1187 | if (Known != ExternalBaseOffsets.end()) { |
| 1188 | Offset = Known->second; |
| 1189 | HasExternalLayout = true; |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
Eli Friedman | 69d27d2 | 2013-07-16 00:21:28 +0000 | [diff] [blame] | 1194 | CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign(); |
| 1195 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
| 1196 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1197 | // 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] | 1198 | if (Base->Class->isEmpty() && |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1199 | (!HasExternalLayout || Offset == CharUnits::Zero()) && |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1200 | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1201 | setSize(std::max(getSize(), Layout.getSize())); |
Eli Friedman | 69d27d2 | 2013-07-16 00:21:28 +0000 | [diff] [blame] | 1202 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1203 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1204 | return CharUnits::Zero(); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1205 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1206 | |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1207 | // The maximum field alignment overrides base align. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1208 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1209 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 1210 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1211 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1212 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1213 | if (!HasExternalLayout) { |
| 1214 | // Round up the current record size to the base's alignment boundary. |
| 1215 | Offset = getDataSize().RoundUpToAlignment(BaseAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1216 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1217 | // Try to place the base. |
| 1218 | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset)) |
| 1219 | Offset += BaseAlign; |
| 1220 | } else { |
| 1221 | bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset); |
| 1222 | (void)Allowed; |
| 1223 | assert(Allowed && "Base subobject externally placed at overlapping offset"); |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1224 | |
| 1225 | if (InferAlignment && Offset < getDataSize().RoundUpToAlignment(BaseAlign)){ |
| 1226 | // The externally-supplied base offset is before the base offset we |
| 1227 | // computed. Assume that the structure is packed. |
| 1228 | Alignment = CharUnits::One(); |
| 1229 | InferAlignment = false; |
| 1230 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1233 | if (!Base->Class->isEmpty()) { |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1234 | // Update the data size. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1235 | setDataSize(Offset + Layout.getNonVirtualSize()); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1236 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1237 | setSize(std::max(getSize(), getDataSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1238 | } else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1239 | setSize(std::max(getSize(), Offset + Layout.getSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Remember max struct/class alignment. |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1242 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1243 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1244 | return Offset; |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1247 | void RecordLayoutBuilder::InitializeLayout(const Decl *D) { |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1248 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1249 | IsUnion = RD->isUnion(); |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1250 | IsMsStruct = RD->isMsStruct(Context); |
| 1251 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1252 | |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1253 | Packed = D->hasAttr<PackedAttr>(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1254 | |
Daniel Dunbar | 096ed29 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1255 | // Honor the default struct packing maximum alignment flag. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1256 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) { |
Daniel Dunbar | 096ed29 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1257 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
| 1258 | } |
| 1259 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1260 | // mac68k alignment supersedes maximum field alignment and attribute aligned, |
| 1261 | // and forces all structures to have 2-byte alignment. The IBM docs on it |
| 1262 | // allude to additional (more complicated) semantics, especially with regard |
| 1263 | // to bit-fields, but gcc appears not to follow that. |
| 1264 | if (D->hasAttr<AlignMac68kAttr>()) { |
| 1265 | IsMac68kAlign = true; |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1266 | MaxFieldAlignment = CharUnits::fromQuantity(2); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1267 | Alignment = CharUnits::fromQuantity(2); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1268 | } else { |
| 1269 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1270 | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1271 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1272 | if (unsigned MaxAlign = D->getMaxAlignment()) |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1273 | UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1274 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1275 | |
| 1276 | // If there is an external AST source, ask it for the various offsets. |
| 1277 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 1278 | if (ExternalASTSource *External = Context.getExternalSource()) { |
| 1279 | ExternalLayout = External->layoutRecordType(RD, |
| 1280 | ExternalSize, |
| 1281 | ExternalAlign, |
| 1282 | ExternalFieldOffsets, |
| 1283 | ExternalBaseOffsets, |
| 1284 | ExternalVirtualBaseOffsets); |
| 1285 | |
| 1286 | // Update based on external alignment. |
| 1287 | if (ExternalLayout) { |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1288 | if (ExternalAlign > 0) { |
| 1289 | Alignment = Context.toCharUnitsFromBits(ExternalAlign); |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1290 | } else { |
| 1291 | // The external source didn't have alignment information; infer it. |
| 1292 | InferAlignment = true; |
| 1293 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1294 | } |
| 1295 | } |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1296 | } |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 1297 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1298 | void RecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 1299 | InitializeLayout(D); |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1300 | LayoutFields(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1302 | // Finally, round the size of the total struct up to the alignment of the |
| 1303 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1304 | FinishLayout(D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
| 1308 | InitializeLayout(RD); |
| 1309 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1310 | // Lay out the vtable and the non-virtual bases. |
| 1311 | LayoutNonVirtualBases(RD); |
| 1312 | |
| 1313 | LayoutFields(RD); |
| 1314 | |
Ken Dyck | e738075 | 2011-03-10 01:53:59 +0000 | [diff] [blame] | 1315 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1316 | llvm::RoundUpToAlignment(getSizeInBits(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1317 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1318 | NonVirtualAlignment = Alignment; |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1319 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1320 | // Lay out the virtual bases and add the primary virtual base offsets. |
| 1321 | LayoutVirtualBases(RD, RD); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1322 | |
| 1323 | // Finally, round the size of the total struct up to the alignment |
Eli Friedman | 83a1258 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 1324 | // of the struct itself. |
| 1325 | FinishLayout(RD); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1326 | |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1327 | #ifndef NDEBUG |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1328 | // Check that we have base offsets for all bases. |
| 1329 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1330 | E = RD->bases_end(); I != E; ++I) { |
| 1331 | if (I->isVirtual()) |
| 1332 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1333 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1334 | const CXXRecordDecl *BaseDecl = |
| 1335 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1336 | |
| 1337 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 1338 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1339 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1340 | // And all virtual bases. |
| 1341 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1342 | E = RD->vbases_end(); I != E; ++I) { |
| 1343 | const CXXRecordDecl *BaseDecl = |
| 1344 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1345 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1346 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1347 | } |
| 1348 | #endif |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1351 | void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1352 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1353 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1354 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1355 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1357 | // We start laying out ivars not at the end of the superclass |
| 1358 | // structure, but at the next byte following the last field. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1359 | setSize(SL.getDataSize()); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1360 | setDataSize(getSize()); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1361 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1363 | InitializeLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1364 | // Layout each ivar sequentially. |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1365 | for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD; |
| 1366 | IVD = IVD->getNextIvar()) |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1367 | LayoutField(IVD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1369 | // Finally, round the size of the total struct up to the alignment of the |
| 1370 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1371 | FinishLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1374 | void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1375 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 1376 | // the future, this will need to be tweakable by targets. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Eli Friedman | 481673f | 2013-06-26 23:47:39 +0000 | [diff] [blame] | 1378 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1379 | LayoutField(*Field); |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1382 | void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1383 | uint64_t TypeSize, |
| 1384 | bool FieldPacked, |
| 1385 | const FieldDecl *D) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1386 | assert(Context.getLangOpts().CPlusPlus && |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1387 | "Can only have wide bit-fields in C++!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1388 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1389 | // Itanium C++ ABI 2.4: |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1390 | // If sizeof(T)*8 < n, let T' be the largest integral POD type with |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1391 | // sizeof(T')*8 <= n. |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1392 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1393 | QualType IntegralPODTypes[] = { |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1394 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1395 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
| 1396 | }; |
| 1397 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1398 | QualType Type; |
| 1399 | for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes); |
| 1400 | I != E; ++I) { |
| 1401 | uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (Size > FieldSize) |
| 1404 | break; |
| 1405 | |
| 1406 | Type = IntegralPODTypes[I]; |
| 1407 | } |
| 1408 | assert(!Type.isNull() && "Did not find a type!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1409 | |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1410 | CharUnits TypeAlign = Context.getTypeAlignInChars(Type); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1411 | |
| 1412 | // 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] | 1413 | UnfilledBitsInLastUnit = 0; |
| 1414 | LastBitfieldTypeSize = 0; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1415 | |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1416 | uint64_t FieldOffset; |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1417 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1418 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1419 | if (IsUnion) { |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1420 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1421 | FieldOffset = 0; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1422 | } else { |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1423 | // The bitfield is allocated starting at the next offset aligned |
| 1424 | // appropriately for T', with length n bits. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1425 | FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(), |
| 1426 | Context.toBits(TypeAlign)); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1427 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1428 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1429 | |
Ken Dyck | a1a2e8d | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 1430 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1431 | Context.getTargetInfo().getCharAlign())); |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1432 | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | // Place this field at the current location. |
| 1436 | FieldOffsets.push_back(FieldOffset); |
| 1437 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1438 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1439 | Context.toBits(TypeAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1440 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1441 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1442 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1443 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1444 | // Remember max struct/class alignment. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1445 | UpdateAlignment(TypeAlign); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1448 | void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1449 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1450 | uint64_t FieldSize = D->getBitWidthValue(Context); |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1451 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1452 | uint64_t TypeSize = FieldInfo.first; |
| 1453 | unsigned FieldAlign = FieldInfo.second; |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (IsMsStruct) { |
| 1456 | // The field alignment for integer types in ms_struct structs is |
| 1457 | // always the size. |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1458 | FieldAlign = TypeSize; |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1459 | // Ignore zero-length bitfields after non-bitfields in ms_struct structs. |
| 1460 | if (!FieldSize && !LastBitfieldTypeSize) |
| 1461 | FieldAlign = 1; |
| 1462 | // If a bitfield is followed by a bitfield of a different size, don't |
| 1463 | // pack the bits together in ms_struct structs. |
| 1464 | if (LastBitfieldTypeSize != TypeSize) { |
| 1465 | UnfilledBitsInLastUnit = 0; |
| 1466 | LastBitfieldTypeSize = 0; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
| 1471 | uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1472 | |
Eli Friedman | 481673f | 2013-06-26 23:47:39 +0000 | [diff] [blame] | 1473 | bool ZeroLengthBitfield = false; |
| 1474 | if (!Context.getTargetInfo().useBitFieldTypeAlignment() && |
| 1475 | Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
| 1476 | FieldSize == 0) { |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1477 | // The alignment of a zero-length bitfield affects the alignment |
| 1478 | // of the next member. The alignment is the max of the zero |
| 1479 | // length bitfield's alignment and a target specific fixed value. |
Eli Friedman | 481673f | 2013-06-26 23:47:39 +0000 | [diff] [blame] | 1480 | ZeroLengthBitfield = true; |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1481 | unsigned ZeroLengthBitfieldBoundary = |
| 1482 | Context.getTargetInfo().getZeroLengthBitfieldBoundary(); |
| 1483 | if (ZeroLengthBitfieldBoundary > FieldAlign) |
| 1484 | FieldAlign = ZeroLengthBitfieldBoundary; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1485 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1486 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1487 | if (FieldSize > TypeSize) { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1488 | LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1489 | return; |
| 1490 | } |
| 1491 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1492 | // The align if the field is not packed. This is to check if the attribute |
| 1493 | // was unnecessary (-Wpacked). |
| 1494 | unsigned UnpackedFieldAlign = FieldAlign; |
| 1495 | uint64_t UnpackedFieldOffset = FieldOffset; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1496 | if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1497 | UnpackedFieldAlign = 1; |
| 1498 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1499 | if (FieldPacked || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1500 | (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)) |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1501 | FieldAlign = 1; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1502 | FieldAlign = std::max(FieldAlign, D->getMaxAlignment()); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1503 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1504 | |
| 1505 | // The maximum field alignment overrides the aligned attribute. |
Eli Friedman | a91d38a | 2011-12-02 02:38:48 +0000 | [diff] [blame] | 1506 | if (!MaxFieldAlignment.isZero() && FieldSize != 0) { |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1507 | unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); |
| 1508 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1509 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1510 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1511 | |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1512 | // ms_struct bitfields always have to start at a round alignment. |
| 1513 | if (IsMsStruct && !LastBitfieldTypeSize) { |
| 1514 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
| 1515 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1516 | UnpackedFieldAlign); |
| 1517 | } |
| 1518 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1519 | // Check if we need to add padding to give the field the correct alignment. |
| 1520 | if (FieldSize == 0 || |
| 1521 | (MaxFieldAlignment.isZero() && |
| 1522 | (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)) |
| 1523 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1525 | if (FieldSize == 0 || |
| 1526 | (MaxFieldAlignment.isZero() && |
| 1527 | (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)) |
| 1528 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1529 | UnpackedFieldAlign); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1530 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1531 | // Padding members don't affect overall alignment, unless zero length bitfield |
| 1532 | // alignment is enabled. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1533 | if (!D->getIdentifier() && |
| 1534 | !Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
| 1535 | !IsMsStruct) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1536 | FieldAlign = UnpackedFieldAlign = 1; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1537 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1538 | if (ExternalLayout) |
| 1539 | FieldOffset = updateExternalFieldOffset(D, FieldOffset); |
| 1540 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1541 | // Place this field at the current location. |
| 1542 | FieldOffsets.push_back(FieldOffset); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1543 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1544 | if (!ExternalLayout) |
| 1545 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
| 1546 | UnpackedFieldAlign, FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1547 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1548 | // Update DataSize to include the last byte containing (part of) the bitfield. |
| 1549 | if (IsUnion) { |
| 1550 | // FIXME: I think FieldSize should be TypeSize here. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1551 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1552 | } else { |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1553 | if (IsMsStruct && FieldSize) { |
| 1554 | // Under ms_struct, a bitfield always takes up space equal to the size |
| 1555 | // of the type. We can't just change the alignment computation on the |
| 1556 | // other codepath because of the way this interacts with #pragma pack: |
| 1557 | // in a packed struct, we need to allocate misaligned space in the |
| 1558 | // struct to hold the bitfield. |
| 1559 | if (!UnfilledBitsInLastUnit) { |
| 1560 | setDataSize(FieldOffset + TypeSize); |
| 1561 | UnfilledBitsInLastUnit = TypeSize - FieldSize; |
| 1562 | } else if (UnfilledBitsInLastUnit < FieldSize) { |
| 1563 | setDataSize(getDataSizeInBits() + TypeSize); |
| 1564 | UnfilledBitsInLastUnit = TypeSize - FieldSize; |
| 1565 | } else { |
| 1566 | UnfilledBitsInLastUnit -= FieldSize; |
| 1567 | } |
| 1568 | LastBitfieldTypeSize = TypeSize; |
| 1569 | } else { |
| 1570 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
| 1571 | uint64_t BitfieldAlignment = Context.getTargetInfo().getCharAlign(); |
| 1572 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, BitfieldAlignment)); |
| 1573 | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
| 1574 | LastBitfieldTypeSize = 0; |
| 1575 | } |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1576 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1577 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1578 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1579 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1580 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1581 | // Remember max struct/class alignment. |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1582 | UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), |
| 1583 | Context.toCharUnitsFromBits(UnpackedFieldAlign)); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1586 | void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1587 | if (D->isBitField()) { |
| 1588 | LayoutBitField(D); |
| 1589 | return; |
| 1590 | } |
| 1591 | |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1592 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1593 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1594 | // Reset the unfilled bits. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1595 | UnfilledBitsInLastUnit = 0; |
| 1596 | LastBitfieldTypeSize = 0; |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1597 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1598 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1599 | CharUnits FieldOffset = |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1600 | IsUnion ? CharUnits::Zero() : getDataSize(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1601 | CharUnits FieldSize; |
| 1602 | CharUnits FieldAlign; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1603 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1604 | if (D->getType()->isIncompleteArrayType()) { |
| 1605 | // This is a flexible array member; we can't directly |
| 1606 | // query getTypeInfo about these, so we figure it out here. |
| 1607 | // Flexible array members don't have any size, but they |
| 1608 | // have to be aligned appropriately for their element type. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1609 | FieldSize = CharUnits::Zero(); |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1610 | const ArrayType* ATy = Context.getAsArrayType(D->getType()); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1611 | FieldAlign = Context.getTypeAlignInChars(ATy->getElementType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1612 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 1613 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1614 | FieldSize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1615 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS)); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1616 | FieldAlign = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1617 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1618 | } else { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1619 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 1620 | Context.getTypeInfoInChars(D->getType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1621 | FieldSize = FieldInfo.first; |
| 1622 | FieldAlign = FieldInfo.second; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1623 | |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1624 | if (IsMsStruct) { |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1625 | // If MS bitfield layout is required, figure out what type is being |
| 1626 | // laid out and align the field to the width of that type. |
| 1627 | |
| 1628 | // Resolve all typedefs down to their base type and round up the field |
| 1629 | // alignment if necessary. |
| 1630 | QualType T = Context.getBaseElementType(D->getType()); |
| 1631 | if (const BuiltinType *BTy = T->getAs<BuiltinType>()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1632 | CharUnits TypeSize = Context.getTypeSizeInChars(BTy); |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1633 | if (TypeSize > FieldAlign) |
| 1634 | FieldAlign = TypeSize; |
| 1635 | } |
| 1636 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1637 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1639 | // The align if the field is not packed. This is to check if the attribute |
| 1640 | // was unnecessary (-Wpacked). |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1641 | CharUnits UnpackedFieldAlign = FieldAlign; |
| 1642 | CharUnits UnpackedFieldOffset = FieldOffset; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1643 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1644 | if (FieldPacked) |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1645 | FieldAlign = CharUnits::One(); |
| 1646 | CharUnits MaxAlignmentInChars = |
| 1647 | Context.toCharUnitsFromBits(D->getMaxAlignment()); |
| 1648 | FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); |
| 1649 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1650 | |
| 1651 | // The maximum field alignment overrides the aligned attribute. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1652 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1653 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 1654 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1655 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1657 | // Round up the current record size to the field's alignment boundary. |
| 1658 | FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign); |
| 1659 | UnpackedFieldOffset = |
| 1660 | UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign); |
| 1661 | |
| 1662 | if (ExternalLayout) { |
| 1663 | FieldOffset = Context.toCharUnitsFromBits( |
| 1664 | updateExternalFieldOffset(D, Context.toBits(FieldOffset))); |
| 1665 | |
| 1666 | if (!IsUnion && EmptySubobjects) { |
| 1667 | // Record the fact that we're placing a field at this offset. |
| 1668 | bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset); |
| 1669 | (void)Allowed; |
| 1670 | assert(Allowed && "Externally-placed field cannot be placed here"); |
| 1671 | } |
| 1672 | } else { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1673 | if (!IsUnion && EmptySubobjects) { |
| 1674 | // Check if we can place the field at this offset. |
| 1675 | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { |
| 1676 | // We couldn't place the field at the offset. Try again at a new offset. |
| 1677 | FieldOffset += FieldAlign; |
| 1678 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1679 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1680 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1681 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1682 | // Place this field at the current location. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1683 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1685 | if (!ExternalLayout) |
| 1686 | CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, |
| 1687 | Context.toBits(UnpackedFieldOffset), |
| 1688 | Context.toBits(UnpackedFieldAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1689 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1690 | // Reserve space for this field. |
Eli Friedman | 43f1834 | 2012-01-12 23:27:03 +0000 | [diff] [blame] | 1691 | uint64_t FieldSizeInBits = Context.toBits(FieldSize); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1692 | if (IsUnion) |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1693 | setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1694 | else |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1695 | setDataSize(FieldOffset + FieldSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1697 | // Update the size. |
| 1698 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1700 | // Remember max struct/class alignment. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1701 | UpdateAlignment(FieldAlign, UnpackedFieldAlign); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1704 | void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1705 | // In C++, records cannot be of size 0. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1706 | if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0) { |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1707 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 1708 | // Compatibility with gcc requires a class (pod or non-pod) |
| 1709 | // which is not empty but of size 0; such as having fields of |
| 1710 | // array of zero-length, remains of Size 0 |
| 1711 | if (RD->isEmpty()) |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1712 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1713 | } |
| 1714 | else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1715 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1716 | } |
Eli Friedman | 83a1258 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1718 | // Finally, round the size of the record up to the alignment of the |
| 1719 | // record itself. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1720 | uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastUnit; |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1721 | uint64_t UnpackedSizeInBits = |
| 1722 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 1723 | Context.toBits(UnpackedAlignment)); |
| 1724 | CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits); |
| 1725 | uint64_t RoundedSize |
| 1726 | = llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)); |
| 1727 | |
| 1728 | if (ExternalLayout) { |
| 1729 | // If we're inferring alignment, and the external size is smaller than |
| 1730 | // our size after we've rounded up to alignment, conservatively set the |
| 1731 | // alignment to 1. |
| 1732 | if (InferAlignment && ExternalSize < RoundedSize) { |
| 1733 | Alignment = CharUnits::One(); |
| 1734 | InferAlignment = false; |
| 1735 | } |
| 1736 | setSize(ExternalSize); |
| 1737 | return; |
| 1738 | } |
| 1739 | |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1740 | // Set the size to the final size. |
| 1741 | setSize(RoundedSize); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1742 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1743 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1744 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 1745 | // Warn if padding was introduced to the struct/class/union. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1746 | if (getSizeInBits() > UnpaddedSize) { |
| 1747 | unsigned PadSize = getSizeInBits() - UnpaddedSize; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1748 | bool InBits = true; |
| 1749 | if (PadSize % CharBitNum == 0) { |
| 1750 | PadSize = PadSize / CharBitNum; |
| 1751 | InBits = false; |
| 1752 | } |
| 1753 | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
| 1754 | << Context.getTypeDeclType(RD) |
| 1755 | << PadSize |
| 1756 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 1757 | } |
| 1758 | |
| 1759 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1760 | // bother since there won't be alignment issues. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1761 | if (Packed && UnpackedAlignment > CharUnits::One() && |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1762 | getSize() == UnpackedSize) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1763 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1764 | << Context.getTypeDeclType(RD); |
| 1765 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1768 | void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment, |
| 1769 | CharUnits UnpackedNewAlignment) { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1770 | // The alignment is not modified when using 'mac68k' alignment or when |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1771 | // we have an externally-supplied layout that also provides overall alignment. |
| 1772 | if (IsMac68kAlign || (ExternalLayout && !InferAlignment)) |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1773 | return; |
| 1774 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1775 | if (NewAlignment > Alignment) { |
| 1776 | assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() && |
| 1777 | "Alignment not a power of 2")); |
| 1778 | Alignment = NewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1781 | if (UnpackedNewAlignment > UnpackedAlignment) { |
| 1782 | assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() && |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1783 | "Alignment not a power of 2")); |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1784 | UnpackedAlignment = UnpackedNewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1788 | uint64_t |
| 1789 | RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field, |
| 1790 | uint64_t ComputedOffset) { |
| 1791 | assert(ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() && |
| 1792 | "Field does not have an external offset"); |
| 1793 | |
| 1794 | uint64_t ExternalFieldOffset = ExternalFieldOffsets[Field]; |
| 1795 | |
| 1796 | if (InferAlignment && ExternalFieldOffset < ComputedOffset) { |
| 1797 | // The externally-supplied field offset is before the field offset we |
| 1798 | // computed. Assume that the structure is packed. |
Douglas Gregor | 1423a5c | 2012-10-26 22:31:14 +0000 | [diff] [blame] | 1799 | Alignment = CharUnits::One(); |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1800 | InferAlignment = false; |
| 1801 | } |
| 1802 | |
| 1803 | // Use the externally-supplied field offset. |
Benjamin Kramer | 648e68b | 2012-08-31 22:14:25 +0000 | [diff] [blame] | 1804 | return ExternalFieldOffset; |
| 1805 | } |
| 1806 | |
| 1807 | /// \brief Get diagnostic %select index for tag kind for |
| 1808 | /// field padding diagnostic message. |
| 1809 | /// WARNING: Indexes apply to particular diagnostics only! |
| 1810 | /// |
| 1811 | /// \returns diagnostic %select index. |
| 1812 | static unsigned getPaddingDiagFromTagKind(TagTypeKind Tag) { |
| 1813 | switch (Tag) { |
| 1814 | case TTK_Struct: return 0; |
| 1815 | case TTK_Interface: return 1; |
| 1816 | case TTK_Class: return 2; |
| 1817 | default: llvm_unreachable("Invalid tag kind for field padding diagnostic!"); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset, |
| 1822 | uint64_t UnpaddedOffset, |
| 1823 | uint64_t UnpackedOffset, |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1824 | unsigned UnpackedAlign, |
| 1825 | bool isPacked, |
| 1826 | const FieldDecl *D) { |
| 1827 | // We let objc ivars without warning, objc interfaces generally are not used |
| 1828 | // for padding tricks. |
| 1829 | if (isa<ObjCIvarDecl>(D)) |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1830 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Ted Kremenek | fed48af | 2011-09-06 19:40:45 +0000 | [diff] [blame] | 1832 | // Don't warn about structs created without a SourceLocation. This can |
| 1833 | // be done by clients of the AST, such as codegen. |
| 1834 | if (D->getLocation().isInvalid()) |
| 1835 | return; |
| 1836 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1837 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1839 | // Warn if padding was introduced to the struct/class. |
| 1840 | if (!IsUnion && Offset > UnpaddedOffset) { |
| 1841 | unsigned PadSize = Offset - UnpaddedOffset; |
| 1842 | bool InBits = true; |
| 1843 | if (PadSize % CharBitNum == 0) { |
| 1844 | PadSize = PadSize / CharBitNum; |
| 1845 | InBits = false; |
Benjamin Kramer | 648e68b | 2012-08-31 22:14:25 +0000 | [diff] [blame] | 1846 | } |
| 1847 | if (D->getIdentifier()) |
| 1848 | Diag(D->getLocation(), diag::warn_padded_struct_field) |
| 1849 | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
| 1850 | << Context.getTypeDeclType(D->getParent()) |
| 1851 | << PadSize |
| 1852 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not |
| 1853 | << D->getIdentifier(); |
| 1854 | else |
| 1855 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
| 1856 | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
| 1857 | << Context.getTypeDeclType(D->getParent()) |
| 1858 | << PadSize |
| 1859 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1863 | // bother since there won't be alignment issues. |
| 1864 | if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset) |
| 1865 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1866 | << D->getIdentifier(); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1869 | static const CXXMethodDecl *computeKeyFunction(ASTContext &Context, |
| 1870 | const CXXRecordDecl *RD) { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1871 | // 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] | 1872 | if (!RD->isPolymorphic()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1873 | return 0; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1874 | |
Eli Friedman | 300f55d | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 1875 | // 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] | 1876 | // at least, there's no point to assigning a key function to such a class; |
| 1877 | // this doesn't affect the ABI.) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1878 | if (!RD->isExternallyVisible()) |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1879 | return 0; |
| 1880 | |
Argyrios Kyrtzidis | 8c64bbe | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 1881 | // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6. |
| 1882 | // Same behavior as GCC. |
| 1883 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 1884 | if (TSK == TSK_ImplicitInstantiation || |
| 1885 | TSK == TSK_ExplicitInstantiationDefinition) |
| 1886 | return 0; |
| 1887 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1888 | bool allowInlineFunctions = |
| 1889 | Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline(); |
| 1890 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1891 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 1892 | E = RD->method_end(); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1893 | const CXXMethodDecl *MD = *I; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1894 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1895 | if (!MD->isVirtual()) |
| 1896 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1897 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1898 | if (MD->isPure()) |
| 1899 | continue; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1900 | |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1901 | // Ignore implicit member functions, they are always marked as inline, but |
| 1902 | // they don't have a body until they're defined. |
| 1903 | if (MD->isImplicit()) |
| 1904 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | a318efd | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 1906 | if (MD->isInlineSpecified()) |
| 1907 | continue; |
Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1908 | |
| 1909 | if (MD->hasInlineBody()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1910 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1911 | |
Benjamin Kramer | 4a90208 | 2012-08-03 15:43:22 +0000 | [diff] [blame] | 1912 | // Ignore inline deleted or defaulted functions. |
Benjamin Kramer | 73d1be7 | 2012-08-03 08:39:58 +0000 | [diff] [blame] | 1913 | if (!MD->isUserProvided()) |
| 1914 | continue; |
| 1915 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1916 | // In certain ABIs, ignore functions with out-of-line inline definitions. |
| 1917 | if (!allowInlineFunctions) { |
| 1918 | const FunctionDecl *Def; |
| 1919 | if (MD->hasBody(Def) && Def->isInlineSpecified()) |
| 1920 | continue; |
| 1921 | } |
| 1922 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1923 | // We found it. |
| 1924 | return MD; |
| 1925 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1926 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1927 | return 0; |
| 1928 | } |
| 1929 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1930 | DiagnosticBuilder |
| 1931 | RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1932 | return Context.getDiagnostics().Report(Loc, DiagID); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 1935 | /// Does the target C++ ABI require us to skip over the tail-padding |
| 1936 | /// of the given class (considering it as a base class) when allocating |
| 1937 | /// objects? |
| 1938 | static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) { |
| 1939 | switch (ABI.getTailPaddingUseRules()) { |
| 1940 | case TargetCXXABI::AlwaysUseTailPadding: |
| 1941 | return false; |
| 1942 | |
| 1943 | case TargetCXXABI::UseTailPaddingUnlessPOD03: |
| 1944 | // FIXME: To the extent that this is meant to cover the Itanium ABI |
| 1945 | // rules, we should implement the restrictions about over-sized |
| 1946 | // bitfields: |
| 1947 | // |
| 1948 | // http://mentorembedded.github.com/cxx-abi/abi.html#POD : |
| 1949 | // In general, a type is considered a POD for the purposes of |
| 1950 | // layout if it is a POD type (in the sense of ISO C++ |
| 1951 | // [basic.types]). However, a POD-struct or POD-union (in the |
| 1952 | // sense of ISO C++ [class]) with a bitfield member whose |
| 1953 | // declared width is wider than the declared type of the |
| 1954 | // bitfield is not a POD for the purpose of layout. Similarly, |
| 1955 | // an array type is not a POD for the purpose of layout if the |
| 1956 | // element type of the array is not a POD for the purpose of |
| 1957 | // layout. |
| 1958 | // |
| 1959 | // Where references to the ISO C++ are made in this paragraph, |
| 1960 | // the Technical Corrigendum 1 version of the standard is |
| 1961 | // intended. |
| 1962 | return RD->isPOD(); |
| 1963 | |
| 1964 | case TargetCXXABI::UseTailPaddingUnlessPOD11: |
| 1965 | // This is equivalent to RD->getTypeForDecl().isCXX11PODType(), |
| 1966 | // but with a lot of abstraction penalty stripped off. This does |
| 1967 | // assume that these properties are set correctly even in C++98 |
| 1968 | // mode; fortunately, that is true because we want to assign |
| 1969 | // consistently semantics to the type-traits intrinsics (or at |
| 1970 | // least as many of them as possible). |
| 1971 | return RD->isTrivial() && RD->isStandardLayout(); |
| 1972 | } |
| 1973 | |
| 1974 | llvm_unreachable("bad tail-padding use kind"); |
| 1975 | } |
| 1976 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 1977 | static bool isMsLayout(const RecordDecl* D) { |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1978 | return D->getASTContext().getTargetInfo().getCXXABI().isMicrosoft(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | // This section contains an implementation of struct layout that is, up to the |
| 1982 | // included tests, compatible with cl.exe (2012). The layout produced is |
| 1983 | // significantly different than those produced by the Itanium ABI. Here we note |
| 1984 | // the most important differences. |
| 1985 | // |
| 1986 | // * The alignment of bitfields in unions is ignored when computing the |
| 1987 | // alignment of the union. |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 1988 | // * The existence of zero-width bitfield that occurs after anything other than |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 1989 | // a non-zero length bitfield is ignored. |
| 1990 | // * The Itanium equivalent vtable pointers are split into a vfptr (virtual |
| 1991 | // function pointer) and a vbptr (virtual base pointer). They can each be |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 1992 | // shared with a, non-virtual bases. These bases need not be the same. vfptrs |
| 1993 | // always occur at offset 0. vbptrs can occur at an |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 1994 | // arbitrary offset and are placed after non-virtual bases but before fields. |
| 1995 | // * Virtual bases sometimes require a 'vtordisp' field that is laid out before |
| 1996 | // the virtual base and is used in conjunction with virtual overrides during |
| 1997 | // construction and destruction. |
| 1998 | // * 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] | 1999 | // fields and non-virtual bases at offset 0 in 32 bit mode and in a pointer |
| 2000 | // sized block of memory in 64 bit mode. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2001 | // * 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] | 2002 | // fields and non-virtual bases. This block is at a potentially unaligned |
| 2003 | // offset. If the allocation slot is unaligned and the alignment is less than |
| 2004 | // or equal to the pointer size, additional space is allocated so that the |
| 2005 | // pointer can be aligned properly. This causes very strange effects on the |
| 2006 | // placement of objects after the allocated block. (see the code). |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2007 | // * vtordisps are allocated in a block of memory with size and alignment equal |
| 2008 | // to the alignment of the completed structure (before applying __declspec( |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2009 | // align())). The vtordisp always occur at the end of the allocation block, |
| 2010 | // immediately prior to the virtual base. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2011 | // * The last zero sized non-virtual base is allocated after the placement of |
| 2012 | // vbptr if one exists and can be placed at the end of the struct, potentially |
| 2013 | // aliasing either the first member or another struct allocated after this |
| 2014 | // one. |
| 2015 | // * The last zero size virtual base may be placed at the end of the struct. |
| 2016 | // and can potentially alias a zero sized type in the next struct. |
Warren Hunt | 71140d6 | 2013-12-06 20:16:49 +0000 | [diff] [blame] | 2017 | // * If the last field is a non-zero length bitfield, all virtual bases will |
| 2018 | // have extra padding added before them for no obvious reason. The padding |
| 2019 | // has the same number of bits as the type of the bitfield. |
Warren Hunt | c0df47d | 2013-11-19 22:11:09 +0000 | [diff] [blame] | 2020 | // * When laying out empty non-virtual bases, an extra byte of padding is added |
| 2021 | // 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] | 2022 | // * The ABI attempts to avoid aliasing of zero sized bases by adding padding |
| 2023 | // between bases or vbases with specific properties. The criteria for |
| 2024 | // additional padding between two bases is that the first base is zero sized |
| 2025 | // or has a zero sized subobject and the second base is zero sized or leads |
| 2026 | // with a zero sized base (sharing of vfptrs can reorder the layout of the |
| 2027 | // so the leading base is not always the first one declared). The padding |
| 2028 | // added for bases is 1 byte. The padding added for vbases depends on the |
| 2029 | // alignment of the object but is at least 4 bytes (in both 32 and 64 bit |
| 2030 | // modes). |
Warren Hunt | 6b7048d | 2013-11-06 00:54:59 +0000 | [diff] [blame] | 2031 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2032 | |
| 2033 | namespace { |
| 2034 | struct MicrosoftRecordLayoutBuilder { |
| 2035 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
| 2036 | MicrosoftRecordLayoutBuilder(const ASTContext &Context) : Context(Context) {} |
| 2037 | private: |
| 2038 | MicrosoftRecordLayoutBuilder(const MicrosoftRecordLayoutBuilder &) |
| 2039 | LLVM_DELETED_FUNCTION; |
| 2040 | void operator=(const MicrosoftRecordLayoutBuilder &) LLVM_DELETED_FUNCTION; |
| 2041 | public: |
| 2042 | |
| 2043 | void layout(const RecordDecl *RD); |
| 2044 | void cxxLayout(const CXXRecordDecl *RD); |
| 2045 | /// \brief Initializes size and alignment and honors some flags. |
| 2046 | void initializeLayout(const RecordDecl *RD); |
| 2047 | /// \brief Initialized C++ layout, compute alignment and virtual alignment and |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 2048 | /// existence of vfptrs and vbptrs. Alignment is needed before the vfptr is |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2049 | /// laid out. |
| 2050 | void initializeCXXLayout(const CXXRecordDecl *RD); |
| 2051 | void layoutVFPtr(const CXXRecordDecl *RD); |
| 2052 | void layoutNonVirtualBases(const CXXRecordDecl *RD); |
| 2053 | void layoutNonVirtualBase(const CXXRecordDecl *RD); |
| 2054 | void layoutVBPtr(const CXXRecordDecl *RD); |
| 2055 | /// \brief Lays out the fields of the record. Also rounds size up to |
| 2056 | /// alignment. |
| 2057 | void layoutFields(const RecordDecl *RD); |
| 2058 | void layoutField(const FieldDecl *FD); |
| 2059 | void layoutBitField(const FieldDecl *FD); |
| 2060 | /// \brief Lays out a single zero-width bit-field in the record and handles |
| 2061 | /// special cases associated with zero-width bit-fields. |
| 2062 | void layoutZeroWidthBitField(const FieldDecl *FD); |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2063 | void fixSizeAndAlignment(const RecordDecl *FD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2064 | void layoutVirtualBases(const CXXRecordDecl *RD); |
| 2065 | void layoutVirtualBase(const CXXRecordDecl *RD, bool HasVtordisp); |
| 2066 | /// \brief Flushes the lazy virtual base and conditionally rounds up to |
| 2067 | /// alignment. |
| 2068 | void finalizeCXXLayout(const CXXRecordDecl *RD); |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2069 | void finalizeLayout(const RecordDecl *RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2070 | |
| 2071 | /// \brief Updates the alignment of the type. This function doesn't take any |
| 2072 | /// properties (such as packedness) into account. getAdjustedFieldInfo() |
| 2073 | /// adjustes for packedness. |
| 2074 | void updateAlignment(CharUnits NewAlignment) { |
| 2075 | Alignment = std::max(Alignment, NewAlignment); |
| 2076 | } |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2077 | CharUnits getBaseAlignment(const ASTRecordLayout& Layout); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2078 | /// \brief Gets the size and alignment taking attributes into account. |
| 2079 | std::pair<CharUnits, CharUnits> getAdjustedFieldInfo(const FieldDecl *FD); |
| 2080 | /// \brief Places a field at offset 0. |
| 2081 | void placeFieldAtZero() { FieldOffsets.push_back(0); } |
| 2082 | /// \brief Places a field at an offset in CharUnits. |
| 2083 | void placeFieldAtOffset(CharUnits FieldOffset) { |
| 2084 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
| 2085 | } |
| 2086 | /// \brief Places a bitfield at a bit offset. |
| 2087 | void placeFieldAtBitOffset(uint64_t FieldOffset) { |
| 2088 | FieldOffsets.push_back(FieldOffset); |
| 2089 | } |
| 2090 | /// \brief Compute the set of virtual bases for which vtordisps are required. |
| 2091 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> |
| 2092 | computeVtorDispSet(const CXXRecordDecl *RD); |
| 2093 | |
| 2094 | const ASTContext &Context; |
| 2095 | /// \brief The size of the record being laid out. |
| 2096 | CharUnits Size; |
| 2097 | /// \brief The current alignment of the record layout. |
| 2098 | CharUnits Alignment; |
| 2099 | /// \brief The collection of field offsets. |
| 2100 | SmallVector<uint64_t, 16> FieldOffsets; |
| 2101 | /// \brief The maximum allowed field alignment. This is set by #pragma pack. |
| 2102 | CharUnits MaxFieldAlignment; |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2103 | /// \brief The alignment that this record must obey. This is imposed by |
| 2104 | /// __declspec(align()) on the record itself or one of its fields or bases. |
| 2105 | CharUnits RequiredAlignment; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2106 | bool IsUnion : 1; |
| 2107 | /// \brief True if the last field laid out was a bitfield and was not 0 |
| 2108 | /// width. |
| 2109 | bool LastFieldIsNonZeroWidthBitfield : 1; |
| 2110 | /// \brief The size of the allocation of the currently active bitfield. |
| 2111 | /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield |
| 2112 | /// is true. |
| 2113 | CharUnits CurrentBitfieldSize; |
| 2114 | /// \brief The number of remaining bits in our last bitfield allocation. |
| 2115 | /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield is |
| 2116 | /// true. |
| 2117 | unsigned RemainingBitsInField; |
| 2118 | |
| 2119 | /// \brief The data alignment of the record layout. |
| 2120 | CharUnits DataSize; |
| 2121 | /// \brief The alignment of the non-virtual portion of the record layout |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2122 | /// without the impact of the virtual pointers. |
| 2123 | /// Only used for C++ layouts. |
| 2124 | CharUnits BasesAndFieldsAlignment; |
| 2125 | /// \brief The alignment of the non-virtual portion of the record layout |
| 2126 | /// Only used for C++ layouts. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2127 | CharUnits NonVirtualAlignment; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2128 | /// \brief The primary base class (if one exists). |
| 2129 | const CXXRecordDecl *PrimaryBase; |
| 2130 | /// \brief The class we share our vb-pointer with. |
| 2131 | const CXXRecordDecl *SharedVBPtrBase; |
Warren Hunt | 486e714 | 2013-11-13 22:16:13 +0000 | [diff] [blame] | 2132 | /// \brief True if the class has a vftable pointer that can be extended |
| 2133 | /// by this class or classes derived from it. Such a vfptr will always occur |
| 2134 | /// at offset 0. |
| 2135 | bool HasExtendableVFPtr : 1; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2136 | /// \brief True if the class has a (not necessarily its own) vbtable pointer. |
| 2137 | bool HasVBPtr : 1; |
| 2138 | /// \brief Offset to the virtual base table pointer (if one exists). |
| 2139 | CharUnits VBPtrOffset; |
| 2140 | /// \brief Base classes and their offsets in the record. |
| 2141 | BaseOffsetsMapTy Bases; |
| 2142 | /// \brief virtual base classes and their offsets in the record. |
| 2143 | ASTRecordLayout::VBaseOffsetsMapTy VBases; |
| 2144 | /// \brief The size of a pointer. |
| 2145 | CharUnits PointerSize; |
| 2146 | /// \brief The alignment of a pointer. |
| 2147 | CharUnits PointerAlignment; |
| 2148 | /// \brief Holds an empty base we haven't yet laid out. |
| 2149 | const CXXRecordDecl *LazyEmptyBase; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2150 | /// \brief A pointer to the Layout for the most recent non-virtual base that |
| 2151 | /// has been laid out. |
| 2152 | const ASTRecordLayout *PreviousBaseLayout; |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2153 | /// \brief Lets us know if we're in 64-bit mode |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2154 | bool Is64BitMode : 1; |
| 2155 | /// \brief True if this class contains a zero sized member or base or a base |
| 2156 | /// with a zero sized member or base. Only used for MS-ABI. |
| 2157 | bool HasZeroSizedSubObject : 1; |
| 2158 | /// \brief True if this class is zero sized or first base is zero sized or |
| 2159 | /// has this property. Only used for MS-ABI. |
| 2160 | bool LeadsWithZeroSizedBase : 1; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2161 | }; |
| 2162 | } // namespace |
| 2163 | |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2164 | CharUnits |
| 2165 | MicrosoftRecordLayoutBuilder::getBaseAlignment(const ASTRecordLayout& Layout) { |
| 2166 | CharUnits BaseAlignment = Layout.getAlignment(); |
| 2167 | if (!MaxFieldAlignment.isZero()) |
| 2168 | BaseAlignment = std::min(BaseAlignment, |
| 2169 | std::max(MaxFieldAlignment, |
| 2170 | Layout.getRequiredAlignment())); |
| 2171 | return BaseAlignment; |
| 2172 | } |
| 2173 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2174 | std::pair<CharUnits, CharUnits> |
| 2175 | MicrosoftRecordLayoutBuilder::getAdjustedFieldInfo(const FieldDecl *FD) { |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 2176 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 2177 | Context.getTypeInfoInChars(FD->getType()); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2178 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2179 | // Respect packed attribute. |
| 2180 | if (FD->hasAttr<PackedAttr>()) |
| 2181 | FieldInfo.second = CharUnits::One(); |
| 2182 | // Respect pack pragma. |
| 2183 | else if (!MaxFieldAlignment.isZero()) |
| 2184 | FieldInfo.second = std::min(FieldInfo.second, MaxFieldAlignment); |
| 2185 | // Respect alignment attributes. |
| 2186 | if (unsigned fieldAlign = FD->getMaxAlignment()) { |
| 2187 | CharUnits FieldAlign = Context.toCharUnitsFromBits(fieldAlign); |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2188 | RequiredAlignment = std::max(RequiredAlignment, FieldAlign); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2189 | FieldInfo.second = std::max(FieldInfo.second, FieldAlign); |
| 2190 | } |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2191 | // Respect attributes applied to subobjects of the field. |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2192 | if (const RecordType *RT = |
| 2193 | FD->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) { |
| 2194 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RT->getDecl()); |
| 2195 | RequiredAlignment = std::max(RequiredAlignment, |
| 2196 | Layout.getRequiredAlignment()); |
| 2197 | FieldInfo.second = std::max(FieldInfo.second, |
| 2198 | Layout.getRequiredAlignment()); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2199 | // Track zero-sized subobjects here where it's already avaliable. |
| 2200 | if (Layout.hasZeroSizedSubObject()) |
| 2201 | HasZeroSizedSubObject = true; |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2202 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2203 | return FieldInfo; |
| 2204 | } |
| 2205 | |
| 2206 | void MicrosoftRecordLayoutBuilder::initializeLayout(const RecordDecl *RD) { |
| 2207 | IsUnion = RD->isUnion(); |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 2208 | Is64BitMode = Context.getTargetInfo().getPointerWidth(0) == 64; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2209 | |
| 2210 | Size = CharUnits::Zero(); |
| 2211 | Alignment = CharUnits::One(); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2212 | |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2213 | // In 64-bit mode we always perform an alignment step after laying out vbases. |
| 2214 | // In 32-bit mode we do not. The check to see if we need to perform alignment |
| 2215 | // checks the RequiredAlignment field and performs alignment if it isn't 0. |
| 2216 | RequiredAlignment = Is64BitMode ? CharUnits::One() : CharUnits::Zero(); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2217 | HasZeroSizedSubObject = false; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2218 | |
| 2219 | // Compute the maximum field alignment. |
| 2220 | MaxFieldAlignment = CharUnits::Zero(); |
| 2221 | // Honor the default struct packing maximum alignment flag. |
| 2222 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) |
| 2223 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
| 2224 | // Honor the packing attribute. |
| 2225 | if (const MaxFieldAlignmentAttr *MFAA = RD->getAttr<MaxFieldAlignmentAttr>()) |
| 2226 | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
| 2227 | // Packed attribute forces max field alignment to be 1. |
| 2228 | if (RD->hasAttr<PackedAttr>()) |
| 2229 | MaxFieldAlignment = CharUnits::One(); |
| 2230 | } |
| 2231 | |
| 2232 | void MicrosoftRecordLayoutBuilder::layout(const RecordDecl *RD) { |
| 2233 | initializeLayout(RD); |
| 2234 | layoutFields(RD); |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2235 | fixSizeAndAlignment(RD); |
| 2236 | finalizeLayout(RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | void MicrosoftRecordLayoutBuilder::cxxLayout(const CXXRecordDecl *RD) { |
| 2240 | initializeLayout(RD); |
| 2241 | initializeCXXLayout(RD); |
| 2242 | layoutVFPtr(RD); |
| 2243 | layoutNonVirtualBases(RD); |
| 2244 | layoutVBPtr(RD); |
| 2245 | layoutFields(RD); |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2246 | fixSizeAndAlignment(RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2247 | layoutVirtualBases(RD); |
| 2248 | finalizeCXXLayout(RD); |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2249 | finalizeLayout(RD); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | void |
| 2253 | MicrosoftRecordLayoutBuilder::initializeCXXLayout(const CXXRecordDecl *RD) { |
| 2254 | // Calculate pointer size and alignment. |
| 2255 | PointerSize = |
| 2256 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
| 2257 | PointerAlignment = PointerSize; |
| 2258 | if (!MaxFieldAlignment.isZero()) |
| 2259 | PointerAlignment = std::min(PointerAlignment, MaxFieldAlignment); |
| 2260 | |
| 2261 | // Initialize information about the bases. |
| 2262 | HasVBPtr = false; |
Warren Hunt | 486e714 | 2013-11-13 22:16:13 +0000 | [diff] [blame] | 2263 | HasExtendableVFPtr = false; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2264 | SharedVBPtrBase = 0; |
| 2265 | PrimaryBase = 0; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2266 | LeadsWithZeroSizedBase = false; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2267 | |
| 2268 | // If the record has a dynamic base class, attempt to choose a primary base |
| 2269 | // class. It is the first (in direct base class order) non-virtual dynamic |
| 2270 | // base class, if one exists. |
| 2271 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 2272 | e = RD->bases_end(); |
| 2273 | i != e; ++i) { |
| 2274 | const CXXRecordDecl *BaseDecl = |
| 2275 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 2276 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2277 | // Handle required alignment. |
| 2278 | RequiredAlignment = std::max(RequiredAlignment, |
| 2279 | Layout.getRequiredAlignment()); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2280 | // Check for zero sized subobjects |
| 2281 | if (Layout.hasZeroSizedSubObject()) |
| 2282 | HasZeroSizedSubObject = true; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2283 | // Handle virtual bases. |
| 2284 | if (i->isVirtual()) { |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2285 | HasVBPtr = true; |
| 2286 | continue; |
| 2287 | } |
| 2288 | // We located a primary base class! |
Warren Hunt | 486e714 | 2013-11-13 22:16:13 +0000 | [diff] [blame] | 2289 | if (!PrimaryBase && Layout.hasExtendableVFPtr()) { |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2290 | PrimaryBase = BaseDecl; |
Warren Hunt | 486e714 | 2013-11-13 22:16:13 +0000 | [diff] [blame] | 2291 | HasExtendableVFPtr = true; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2292 | } |
| 2293 | // We located a base to share a VBPtr with! |
| 2294 | if (!SharedVBPtrBase && Layout.hasVBPtr()) { |
| 2295 | SharedVBPtrBase = BaseDecl; |
| 2296 | HasVBPtr = true; |
| 2297 | } |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2298 | updateAlignment(getBaseAlignment(Layout)); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | // Use LayoutFields to compute the alignment of the fields. The layout |
| 2302 | // is discarded. This is the simplest way to get all of the bit-field |
| 2303 | // behavior correct and is not actually very expensive. |
| 2304 | layoutFields(RD); |
| 2305 | Size = CharUnits::Zero(); |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2306 | BasesAndFieldsAlignment = Alignment; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2307 | FieldOffsets.clear(); |
| 2308 | } |
| 2309 | |
| 2310 | void MicrosoftRecordLayoutBuilder::layoutVFPtr(const CXXRecordDecl *RD) { |
| 2311 | // If we have a primary base then our VFPtr was already laid out |
| 2312 | if (PrimaryBase) |
| 2313 | return; |
| 2314 | |
| 2315 | // Look at all of our methods to determine if we need a VFPtr. We need a |
| 2316 | // vfptr if we define a new virtual function. |
Warren Hunt | 486e714 | 2013-11-13 22:16:13 +0000 | [diff] [blame] | 2317 | if (!HasExtendableVFPtr && RD->isDynamicClass()) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2318 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 2319 | e = RD->method_end(); |
Warren Hunt | 486e714 | 2013-11-13 22:16:13 +0000 | [diff] [blame] | 2320 | !HasExtendableVFPtr && i != e; ++i) |
| 2321 | HasExtendableVFPtr = i->isVirtual() && i->size_overridden_methods() == 0; |
| 2322 | if (!HasExtendableVFPtr) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2323 | return; |
| 2324 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2325 | // MSVC 32 (but not 64) potentially over-aligns the vf-table pointer by giving |
| 2326 | // it the max alignment of all the non-virtual data in the class. The |
| 2327 | // resulting layout is essentially { vftbl, { nvdata } }. This is completely |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2328 | // unnecessary, but we're not here to pass judgment. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2329 | updateAlignment(PointerAlignment); |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2330 | if (Is64BitMode) |
| 2331 | Size = Size.RoundUpToAlignment(PointerAlignment) + PointerSize; |
| 2332 | else |
| 2333 | Size = Size.RoundUpToAlignment(PointerAlignment) + Alignment; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | void |
| 2337 | MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) { |
| 2338 | LazyEmptyBase = 0; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2339 | PreviousBaseLayout = 0; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2340 | |
| 2341 | // Lay out the primary base first. |
| 2342 | if (PrimaryBase) |
| 2343 | layoutNonVirtualBase(PrimaryBase); |
| 2344 | |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2345 | const CXXRecordDecl *LeadingBase = PrimaryBase; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2346 | // Iterate through the bases and lay out the non-virtual ones. |
| 2347 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 2348 | e = RD->bases_end(); |
| 2349 | i != e; ++i) { |
| 2350 | if (i->isVirtual()) |
| 2351 | continue; |
| 2352 | const CXXRecordDecl *BaseDecl = |
| 2353 | cast<CXXRecordDecl>(i->getType()->castAs<RecordType>()->getDecl()); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2354 | if (!LeadingBase) { |
| 2355 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 2356 | LeadsWithZeroSizedBase = Layout.leadsWithZeroSizedBase(); |
| 2357 | LeadingBase = BaseDecl; |
| 2358 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2359 | if (BaseDecl != PrimaryBase) |
| 2360 | layoutNonVirtualBase(BaseDecl); |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | void |
| 2365 | MicrosoftRecordLayoutBuilder::layoutNonVirtualBase(const CXXRecordDecl *RD) { |
| 2366 | const ASTRecordLayout *Layout = RD ? &Context.getASTRecordLayout(RD) : 0; |
| 2367 | |
| 2368 | // If we have a lazy empty base we haven't laid out yet, do that now. |
| 2369 | if (LazyEmptyBase) { |
| 2370 | const ASTRecordLayout &LazyLayout = |
| 2371 | Context.getASTRecordLayout(LazyEmptyBase); |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2372 | Size = Size.RoundUpToAlignment(getBaseAlignment(LazyLayout)); |
Warren Hunt | c0df47d | 2013-11-19 22:11:09 +0000 | [diff] [blame] | 2373 | // If the last non-virtual base has a vbptr we add a byte of padding for no |
| 2374 | // obvious reason. |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2375 | if (PreviousBaseLayout && PreviousBaseLayout->hasVBPtr()) |
Warren Hunt | c0df47d | 2013-11-19 22:11:09 +0000 | [diff] [blame] | 2376 | Size++; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2377 | Bases.insert(std::make_pair(LazyEmptyBase, Size)); |
| 2378 | // Empty bases only consume space when followed by another empty base. |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2379 | if (RD && Layout->getNonVirtualSize().isZero()) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2380 | Size++; |
| 2381 | LazyEmptyBase = 0; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2382 | PreviousBaseLayout = &LazyLayout; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | // RD is null when flushing the final lazy base. |
| 2386 | if (!RD) |
| 2387 | return; |
| 2388 | |
| 2389 | if (Layout->getNonVirtualSize().isZero()) { |
| 2390 | LazyEmptyBase = RD; |
| 2391 | return; |
| 2392 | } |
| 2393 | |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2394 | // Insert padding between two bases if the left first one is zero sized or |
| 2395 | // contains a zero sized subobject and the right is zero sized or one leads |
| 2396 | // with a zero sized base. |
| 2397 | if (PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() && |
| 2398 | Layout->leadsWithZeroSizedBase()) |
| 2399 | Size++; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2400 | // Insert the base here. |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2401 | CharUnits BaseOffset = Size.RoundUpToAlignment(getBaseAlignment(*Layout)); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2402 | Bases.insert(std::make_pair(RD, BaseOffset)); |
| 2403 | Size = BaseOffset + Layout->getDataSize(); |
| 2404 | // Note: we don't update alignment here because it was accounted |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2405 | // for during initalization. |
| 2406 | PreviousBaseLayout = Layout; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | void MicrosoftRecordLayoutBuilder::layoutVBPtr(const CXXRecordDecl *RD) { |
| 2410 | if (!HasVBPtr) |
| 2411 | VBPtrOffset = CharUnits::fromQuantity(-1); |
| 2412 | else if (SharedVBPtrBase) { |
| 2413 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(SharedVBPtrBase); |
| 2414 | VBPtrOffset = Bases[SharedVBPtrBase] + Layout.getVBPtrOffset(); |
| 2415 | } else { |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2416 | VBPtrOffset = Size.RoundUpToAlignment(PointerAlignment); |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2417 | CharUnits OldSize = Size; |
| 2418 | Size = VBPtrOffset + PointerSize; |
| 2419 | if (BasesAndFieldsAlignment <= PointerAlignment) { |
| 2420 | // Handle strange padding rules for the lazily placed base. I have no |
| 2421 | // explanation for why the last virtual base is padded in such an odd way. |
| 2422 | // Two things to note about this padding are that the rules are different |
| 2423 | // if the alignment of the bases+fields is <= to the alignemnt of a |
| 2424 | // pointer and that the rule in 64-bit mode behaves differently depending |
| 2425 | // on if the second to last base was also zero sized. |
| 2426 | Size += OldSize % BasesAndFieldsAlignment.getQuantity(); |
| 2427 | } else { |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2428 | if (!Is64BitMode) |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2429 | Size = OldSize + BasesAndFieldsAlignment; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2430 | else if (PreviousBaseLayout && |
| 2431 | PreviousBaseLayout->getNonVirtualSize().isZero()) |
| 2432 | Size += CharUnits::One(); |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2433 | } |
| 2434 | updateAlignment(PointerAlignment); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | // Flush the lazy empty base. |
| 2438 | layoutNonVirtualBase(0); |
| 2439 | } |
| 2440 | |
| 2441 | void MicrosoftRecordLayoutBuilder::layoutFields(const RecordDecl *RD) { |
| 2442 | LastFieldIsNonZeroWidthBitfield = false; |
| 2443 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 2444 | FieldEnd = RD->field_end(); |
| 2445 | Field != FieldEnd; ++Field) |
| 2446 | layoutField(*Field); |
| 2447 | Size = Size.RoundUpToAlignment(Alignment); |
| 2448 | } |
| 2449 | |
| 2450 | void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) { |
| 2451 | if (FD->isBitField()) { |
| 2452 | layoutBitField(FD); |
| 2453 | return; |
| 2454 | } |
| 2455 | LastFieldIsNonZeroWidthBitfield = false; |
| 2456 | |
| 2457 | std::pair<CharUnits, CharUnits> FieldInfo = getAdjustedFieldInfo(FD); |
| 2458 | CharUnits FieldSize = FieldInfo.first; |
| 2459 | CharUnits FieldAlign = FieldInfo.second; |
| 2460 | |
| 2461 | updateAlignment(FieldAlign); |
| 2462 | if (IsUnion) { |
| 2463 | placeFieldAtZero(); |
| 2464 | Size = std::max(Size, FieldSize); |
| 2465 | } else { |
| 2466 | // Round up the current record size to the field's alignment boundary. |
| 2467 | CharUnits FieldOffset = Size.RoundUpToAlignment(FieldAlign); |
| 2468 | placeFieldAtOffset(FieldOffset); |
| 2469 | Size = FieldOffset + FieldSize; |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) { |
| 2474 | unsigned Width = FD->getBitWidthValue(Context); |
| 2475 | if (Width == 0) { |
| 2476 | layoutZeroWidthBitField(FD); |
| 2477 | return; |
| 2478 | } |
| 2479 | |
| 2480 | std::pair<CharUnits, CharUnits> FieldInfo = getAdjustedFieldInfo(FD); |
| 2481 | CharUnits FieldSize = FieldInfo.first; |
| 2482 | CharUnits FieldAlign = FieldInfo.second; |
| 2483 | |
| 2484 | // Clamp the bitfield to a containable size for the sake of being able |
| 2485 | // to lay them out. Sema will throw an error. |
| 2486 | if (Width > Context.toBits(FieldSize)) |
| 2487 | Width = Context.toBits(FieldSize); |
| 2488 | |
| 2489 | // Check to see if this bitfield fits into an existing allocation. Note: |
| 2490 | // MSVC refuses to pack bitfields of formal types with different sizes |
| 2491 | // into the same allocation. |
| 2492 | if (!IsUnion && LastFieldIsNonZeroWidthBitfield && |
| 2493 | CurrentBitfieldSize == FieldSize && Width <= RemainingBitsInField) { |
| 2494 | placeFieldAtBitOffset(Context.toBits(Size) - RemainingBitsInField); |
| 2495 | RemainingBitsInField -= Width; |
| 2496 | return; |
| 2497 | } |
| 2498 | |
| 2499 | LastFieldIsNonZeroWidthBitfield = true; |
| 2500 | CurrentBitfieldSize = FieldSize; |
| 2501 | if (IsUnion) { |
| 2502 | placeFieldAtZero(); |
| 2503 | Size = std::max(Size, FieldSize); |
| 2504 | // TODO: Add a Sema warning that MS ignores bitfield alignment in unions. |
| 2505 | } else { |
| 2506 | // Allocate a new block of memory and place the bitfield in it. |
| 2507 | CharUnits FieldOffset = Size.RoundUpToAlignment(FieldAlign); |
| 2508 | placeFieldAtOffset(FieldOffset); |
| 2509 | Size = FieldOffset + FieldSize; |
| 2510 | updateAlignment(FieldAlign); |
| 2511 | RemainingBitsInField = Context.toBits(FieldSize) - Width; |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | void |
| 2516 | MicrosoftRecordLayoutBuilder::layoutZeroWidthBitField(const FieldDecl *FD) { |
| 2517 | // Zero-width bitfields are ignored unless they follow a non-zero-width |
| 2518 | // bitfield. |
| 2519 | std::pair<CharUnits, CharUnits> FieldInfo = getAdjustedFieldInfo(FD); |
| 2520 | CharUnits FieldSize = FieldInfo.first; |
| 2521 | CharUnits FieldAlign = FieldInfo.second; |
| 2522 | |
| 2523 | if (!LastFieldIsNonZeroWidthBitfield) { |
| 2524 | placeFieldAtOffset(IsUnion ? CharUnits::Zero() : Size); |
| 2525 | // TODO: Add a Sema warning that MS ignores alignment for zero |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2526 | // sized bitfields that occur after zero-size bitfields or non-bitfields. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2527 | return; |
| 2528 | } |
| 2529 | |
| 2530 | LastFieldIsNonZeroWidthBitfield = false; |
| 2531 | if (IsUnion) { |
| 2532 | placeFieldAtZero(); |
| 2533 | Size = std::max(Size, FieldSize); |
| 2534 | } else { |
| 2535 | // Round up the current record size to the field's alignment boundary. |
| 2536 | CharUnits FieldOffset = Size.RoundUpToAlignment(FieldAlign); |
| 2537 | placeFieldAtOffset(FieldOffset); |
| 2538 | Size = FieldOffset; |
| 2539 | updateAlignment(FieldAlign); |
| 2540 | } |
| 2541 | } |
| 2542 | |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2543 | void MicrosoftRecordLayoutBuilder::fixSizeAndAlignment(const RecordDecl *RD) { |
| 2544 | DataSize = Size; |
| 2545 | NonVirtualAlignment = Alignment; |
| 2546 | RequiredAlignment = std::max(RequiredAlignment, |
| 2547 | Context.toCharUnitsFromBits(RD->getMaxAlignment())); |
| 2548 | } |
| 2549 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2550 | void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) { |
| 2551 | if (!HasVBPtr) |
| 2552 | return; |
| 2553 | |
Warren Hunt | 1b51843 | 2013-12-11 02:21:03 +0000 | [diff] [blame] | 2554 | for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(), |
| 2555 | e = RD->vbases_end(); |
| 2556 | i != e; ++i) { |
| 2557 | const CXXRecordDecl *BaseDecl = |
| 2558 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 2559 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 2560 | updateAlignment(getBaseAlignment(Layout)); |
| 2561 | } |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2562 | PreviousBaseLayout = 0; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2563 | |
| 2564 | // Zero-sized v-bases obey the alignment attribute so apply it here. The |
| 2565 | // alignment attribute is normally accounted for in FinalizeLayout. |
| 2566 | if (unsigned MaxAlign = RD->getMaxAlignment()) |
| 2567 | updateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
| 2568 | |
| 2569 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtordisp = |
| 2570 | computeVtorDispSet(RD); |
| 2571 | |
| 2572 | // Iterate through the virtual bases and lay them out. |
| 2573 | for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(), |
| 2574 | e = RD->vbases_end(); |
| 2575 | i != e; ++i) { |
| 2576 | const CXXRecordDecl *BaseDecl = |
| 2577 | cast<CXXRecordDecl>(i->getType()->castAs<RecordType>()->getDecl()); |
Warren Hunt | 71140d6 | 2013-12-06 20:16:49 +0000 | [diff] [blame] | 2578 | // If the last field we laid out was a non-zero length bitfield then add |
| 2579 | // some extra padding for no obvious reason. |
| 2580 | if (LastFieldIsNonZeroWidthBitfield) |
| 2581 | Size += CurrentBitfieldSize; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2582 | layoutVirtualBase(BaseDecl, HasVtordisp.count(BaseDecl)); |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | void MicrosoftRecordLayoutBuilder::layoutVirtualBase(const CXXRecordDecl *RD, |
| 2587 | bool HasVtordisp) { |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2588 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2589 | // Insert padding between two bases if the left first one is zero sized or |
| 2590 | // contains a zero sized subobject and the right is zero sized or one leads |
| 2591 | // with a zero sized base. The minimal padding between virtual bases is 4 |
| 2592 | // bytes (in both 32 and 64 bits modes), we don't know why. |
| 2593 | if (PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() && |
| 2594 | Layout.leadsWithZeroSizedBase()) |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2595 | Size = Size.RoundUpToAlignment(std::max(CharUnits::fromQuantity(4), |
| 2596 | RequiredAlignment)) + |
| 2597 | CharUnits::fromQuantity(4); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2598 | |
| 2599 | CharUnits BaseNVSize = Layout.getNonVirtualSize(); |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2600 | CharUnits BaseAlign = getBaseAlignment(Layout); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2601 | |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2602 | // vtordisps are always 4 bytes (even in 64-bit mode) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2603 | if (HasVtordisp) |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2604 | Size = Size.RoundUpToAlignment(Alignment) + CharUnits::fromQuantity(4); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2605 | |
| 2606 | // Insert the base here. |
Warren Hunt | 4aafaee | 2013-12-07 00:15:04 +0000 | [diff] [blame] | 2607 | Size = Size.RoundUpToAlignment(BaseAlign); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2608 | VBases.insert( |
Warren Hunt | 4aafaee | 2013-12-07 00:15:04 +0000 | [diff] [blame] | 2609 | std::make_pair(RD, ASTRecordLayout::VBaseInfo(Size, HasVtordisp))); |
| 2610 | Size += BaseNVSize; |
| 2611 | // Alignment was upadated in InitializeCXXLayout. |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2612 | |
| 2613 | PreviousBaseLayout = &Layout; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | void MicrosoftRecordLayoutBuilder::finalizeCXXLayout(const CXXRecordDecl *RD) { |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2617 | if (RD->vbases_begin() == RD->vbases_end() || !RequiredAlignment.isZero()) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2618 | Size = Size.RoundUpToAlignment(Alignment); |
| 2619 | |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2620 | if (Size.isZero()) { |
| 2621 | HasZeroSizedSubObject = true; |
| 2622 | LeadsWithZeroSizedBase = true; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2623 | Size = Alignment; |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2624 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Warren Hunt | 1603e52 | 2013-12-10 01:44:39 +0000 | [diff] [blame] | 2627 | void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) { |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2628 | if (!RequiredAlignment.isZero()) { |
| 2629 | updateAlignment(RequiredAlignment); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2630 | Size = Size.RoundUpToAlignment(Alignment); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | static bool |
| 2635 | RequiresVtordisp(const llvm::SmallPtrSet<const CXXRecordDecl *, 2> &HasVtordisp, |
| 2636 | const CXXRecordDecl *RD) { |
| 2637 | if (HasVtordisp.count(RD)) |
| 2638 | return true; |
| 2639 | // If any of a virtual bases non-virtual bases (recursively) requires a |
| 2640 | // vtordisp than so does this virtual base. |
| 2641 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 2642 | e = RD->bases_end(); |
| 2643 | i != e; ++i) |
| 2644 | if (!i->isVirtual() && |
| 2645 | RequiresVtordisp( |
| 2646 | HasVtordisp, |
| 2647 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()))) |
| 2648 | return true; |
| 2649 | return false; |
| 2650 | } |
| 2651 | |
| 2652 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> |
| 2653 | MicrosoftRecordLayoutBuilder::computeVtorDispSet(const CXXRecordDecl *RD) { |
| 2654 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtordisp; |
| 2655 | |
| 2656 | // If any of our bases need a vtordisp for this type, so do we. Check our |
| 2657 | // direct bases for vtordisp requirements. |
| 2658 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 2659 | e = RD->bases_end(); |
| 2660 | i != e; ++i) { |
| 2661 | const CXXRecordDecl *BaseDecl = |
| 2662 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 2663 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 2664 | for (ASTRecordLayout::VBaseOffsetsMapTy::const_iterator |
| 2665 | bi = Layout.getVBaseOffsetsMap().begin(), |
| 2666 | be = Layout.getVBaseOffsetsMap().end(); |
| 2667 | bi != be; ++bi) |
| 2668 | if (bi->second.hasVtorDisp()) |
| 2669 | HasVtordisp.insert(bi->first); |
| 2670 | } |
| 2671 | |
| 2672 | // If we define a constructor or destructor and override a function that is |
| 2673 | // defined in a virtual base's vtable, that virtual bases need a vtordisp. |
| 2674 | // Here we collect a list of classes with vtables for which our virtual bases |
| 2675 | // actually live. The virtual bases with this property will require |
| 2676 | // vtordisps. In addition, virtual bases that contain non-virtual bases that |
| 2677 | // define functions we override also require vtordisps, this case is checked |
| 2678 | // explicitly below. |
| 2679 | if (RD->hasUserDeclaredConstructor() || RD->hasUserDeclaredDestructor()) { |
| 2680 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> Work; |
| 2681 | // Seed the working set with our non-destructor virtual methods. |
| 2682 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 2683 | e = RD->method_end(); |
| 2684 | i != e; ++i) |
Warren Hunt | 42be776 | 2013-10-14 20:14:09 +0000 | [diff] [blame] | 2685 | if ((*i)->isVirtual() && !isa<CXXDestructorDecl>(*i)) |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2686 | Work.insert(*i); |
| 2687 | while (!Work.empty()) { |
| 2688 | const CXXMethodDecl *MD = *Work.begin(); |
| 2689 | CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(), |
| 2690 | e = MD->end_overridden_methods(); |
| 2691 | if (i == e) |
| 2692 | // If a virtual method has no-overrides it lives in its parent's vtable. |
| 2693 | HasVtordisp.insert(MD->getParent()); |
| 2694 | else |
| 2695 | Work.insert(i, e); |
| 2696 | // We've finished processing this element, remove it from the working set. |
| 2697 | Work.erase(MD); |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | // Re-check all of our vbases for vtordisp requirements (in case their |
| 2702 | // non-virtual bases have vtordisp requirements). |
| 2703 | for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(), |
| 2704 | e = RD->vbases_end(); |
| 2705 | i != e; ++i) { |
| 2706 | const CXXRecordDecl *BaseDecl = i->getType()->getAsCXXRecordDecl(); |
| 2707 | if (!HasVtordisp.count(BaseDecl) && RequiresVtordisp(HasVtordisp, BaseDecl)) |
| 2708 | HasVtordisp.insert(BaseDecl); |
| 2709 | } |
| 2710 | |
| 2711 | return HasVtordisp; |
| 2712 | } |
| 2713 | |
| 2714 | /// \brief Get or compute information about the layout of the specified record |
| 2715 | /// (struct/union/class), which indicates its size and field position |
| 2716 | /// information. |
| 2717 | const ASTRecordLayout * |
| 2718 | ASTContext::BuildMicrosoftASTRecordLayout(const RecordDecl *D) const { |
| 2719 | MicrosoftRecordLayoutBuilder Builder(*this); |
| 2720 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 2721 | Builder.cxxLayout(RD); |
| 2722 | return new (*this) ASTRecordLayout( |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2723 | *this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment, |
Warren Hunt | 486e714 | 2013-11-13 22:16:13 +0000 | [diff] [blame] | 2724 | Builder.HasExtendableVFPtr && !Builder.PrimaryBase, |
| 2725 | Builder.HasExtendableVFPtr, |
Timur Iskhodzhanov | 2c9341f | 2013-11-08 11:45:35 +0000 | [diff] [blame] | 2726 | Builder.VBPtrOffset, Builder.DataSize, Builder.FieldOffsets.data(), |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2727 | Builder.FieldOffsets.size(), Builder.DataSize, |
| 2728 | Builder.NonVirtualAlignment, CharUnits::Zero(), Builder.PrimaryBase, |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2729 | false, Builder.SharedVBPtrBase, |
| 2730 | Builder.HasZeroSizedSubObject, Builder.LeadsWithZeroSizedBase, |
| 2731 | Builder.Bases, Builder.VBases); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2732 | } else { |
| 2733 | Builder.layout(D); |
| 2734 | return new (*this) ASTRecordLayout( |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2735 | *this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment, |
| 2736 | Builder.Size, Builder.FieldOffsets.data(), Builder.FieldOffsets.size()); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2740 | /// getASTRecordLayout - Get or compute information about the layout of the |
| 2741 | /// specified record (struct/union/class), which indicates its size and field |
| 2742 | /// position information. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2743 | const ASTRecordLayout & |
| 2744 | ASTContext::getASTRecordLayout(const RecordDecl *D) const { |
John McCall | 0710e55 | 2011-10-07 02:39:22 +0000 | [diff] [blame] | 2745 | // These asserts test different things. A record has a definition |
| 2746 | // as soon as we begin to parse the definition. That definition is |
| 2747 | // not a complete definition (which is what isDefinition() tests) |
| 2748 | // until we *finish* parsing the definition. |
Sean Callanan | 56c1989 | 2012-02-08 00:04:52 +0000 | [diff] [blame] | 2749 | |
| 2750 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
| 2751 | getExternalSource()->CompleteType(const_cast<RecordDecl*>(D)); |
| 2752 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2753 | D = D->getDefinition(); |
| 2754 | assert(D && "Cannot get layout of forward declarations!"); |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 2755 | assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!"); |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2756 | assert(D->isCompleteDefinition() && "Cannot layout type before complete!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2757 | |
| 2758 | // Look up this layout, if already laid out, return what we have. |
| 2759 | // Note that we can't save a reference to the entry because this function |
| 2760 | // is recursive. |
| 2761 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
| 2762 | if (Entry) return *Entry; |
| 2763 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2764 | const ASTRecordLayout *NewEntry = 0; |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2765 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2766 | if (isMsLayout(D) && !D->getASTContext().getExternalSource()) { |
| 2767 | NewEntry = BuildMicrosoftASTRecordLayout(D); |
| 2768 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2769 | EmptySubobjectMap EmptySubobjects(*this, RD); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2770 | RecordLayoutBuilder Builder(*this, &EmptySubobjects); |
| 2771 | Builder.Layout(RD); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 2772 | |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 2773 | // In certain situations, we are allowed to lay out objects in the |
| 2774 | // tail-padding of base classes. This is ABI-dependent. |
| 2775 | // FIXME: this should be stored in the record layout. |
| 2776 | bool skipTailPadding = |
| 2777 | mustSkipTailPadding(getTargetInfo().getCXXABI(), cast<CXXRecordDecl>(D)); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2778 | |
| 2779 | // FIXME: This should be done in FinalizeLayout. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2780 | CharUnits DataSize = |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 2781 | skipTailPadding ? Builder.getSize() : Builder.getDataSize(); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2782 | CharUnits NonVirtualSize = |
John McCall | 5c1f1d0 | 2013-01-29 01:14:22 +0000 | [diff] [blame] | 2783 | skipTailPadding ? DataSize : Builder.NonVirtualSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2784 | NewEntry = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2785 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
| 2786 | Builder.Alignment, |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2787 | /*RequiredAlignment : used by MS-ABI)*/ |
| 2788 | Builder.Alignment, |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 2789 | Builder.HasOwnVFPtr, |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2790 | RD->isDynamicClass(), |
Warren Hunt | 55d8e82 | 2013-10-23 23:53:07 +0000 | [diff] [blame] | 2791 | CharUnits::fromQuantity(-1), |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2792 | DataSize, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2793 | Builder.FieldOffsets.data(), |
| 2794 | Builder.FieldOffsets.size(), |
Ken Dyck | af1c83f | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 2795 | NonVirtualSize, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2796 | Builder.NonVirtualAlignment, |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2797 | EmptySubobjects.SizeOfLargestEmptySubobject, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2798 | Builder.PrimaryBase, |
| 2799 | Builder.PrimaryBaseIsVirtual, |
Warren Hunt | 049f673 | 2013-12-06 19:54:25 +0000 | [diff] [blame] | 2800 | 0, false, false, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2801 | Builder.Bases, Builder.VBases); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2802 | } else { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2803 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2804 | Builder.Layout(D); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2805 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2806 | NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2807 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2808 | Builder.Alignment, |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2809 | /*RequiredAlignment : used by MS-ABI)*/ |
| 2810 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2811 | Builder.getSize(), |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2812 | Builder.FieldOffsets.data(), |
| 2813 | Builder.FieldOffsets.size()); |
| 2814 | } |
| 2815 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2816 | ASTRecordLayouts[D] = NewEntry; |
| 2817 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2818 | if (getLangOpts().DumpRecordLayouts) { |
Argyrios Kyrtzidis | 8ade08e | 2013-07-12 22:30:03 +0000 | [diff] [blame] | 2819 | llvm::outs() << "\n*** Dumping AST Record Layout\n"; |
| 2820 | DumpRecordLayout(D, llvm::outs(), getLangOpts().DumpRecordLayoutsSimple); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | return *NewEntry; |
| 2824 | } |
| 2825 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2826 | const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) { |
Reid Kleckner | 5d7f298 | 2013-05-29 16:18:30 +0000 | [diff] [blame] | 2827 | if (!getTargetInfo().getCXXABI().hasKeyFunctions()) |
| 2828 | return 0; |
| 2829 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2830 | assert(RD->getDefinition() && "Cannot get key function for forward decl!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2831 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2832 | |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2833 | LazyDeclPtr &Entry = KeyFunctions[RD]; |
| 2834 | if (!Entry) |
| 2835 | Entry = const_cast<CXXMethodDecl*>(computeKeyFunction(*this, RD)); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2836 | |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2837 | return cast_or_null<CXXMethodDecl>(Entry.get(getExternalSource())); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2840 | void ASTContext::setNonKeyFunction(const CXXMethodDecl *Method) { |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2841 | assert(Method == Method->getFirstDecl() && |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2842 | "not working with method declaration from class definition"); |
| 2843 | |
| 2844 | // Look up the cache entry. Since we're working with the first |
| 2845 | // declaration, its parent must be the class definition, which is |
| 2846 | // the correct key for the KeyFunctions hash. |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2847 | llvm::DenseMap<const CXXRecordDecl*, LazyDeclPtr>::iterator |
| 2848 | I = KeyFunctions.find(Method->getParent()); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2849 | |
| 2850 | // If it's not cached, there's nothing to do. |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2851 | if (I == KeyFunctions.end()) return; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2852 | |
| 2853 | // If it is cached, check whether it's the target method, and if so, |
| 2854 | // remove it from the cache. |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2855 | if (I->second.get(getExternalSource()) == Method) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2856 | // FIXME: remember that we did this for module / chained PCH state? |
Richard Smith | 676c404 | 2013-08-29 23:59:27 +0000 | [diff] [blame] | 2857 | KeyFunctions.erase(I); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2858 | } |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2861 | static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) { |
| 2862 | const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent()); |
| 2863 | return Layout.getFieldOffset(FD->getFieldIndex()); |
| 2864 | } |
| 2865 | |
| 2866 | uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const { |
| 2867 | uint64_t OffsetInBits; |
| 2868 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 2869 | OffsetInBits = ::getFieldOffset(*this, FD); |
| 2870 | } else { |
| 2871 | const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD); |
| 2872 | |
| 2873 | OffsetInBits = 0; |
| 2874 | for (IndirectFieldDecl::chain_iterator CI = IFD->chain_begin(), |
| 2875 | CE = IFD->chain_end(); |
| 2876 | CI != CE; ++CI) |
| 2877 | OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(*CI)); |
| 2878 | } |
| 2879 | |
| 2880 | return OffsetInBits; |
| 2881 | } |
| 2882 | |
Eric Christopher | 8a39a01 | 2011-10-05 06:00:51 +0000 | [diff] [blame] | 2883 | /// getObjCLayout - Get or compute information about the layout of the |
| 2884 | /// given interface. |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2885 | /// |
| 2886 | /// \param Impl - If given, also include the layout of the interface's |
| 2887 | /// implementation. This may differ by including synthesized ivars. |
| 2888 | const ASTRecordLayout & |
| 2889 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2890 | const ObjCImplementationDecl *Impl) const { |
Douglas Gregor | 64d9257 | 2011-12-20 15:50:13 +0000 | [diff] [blame] | 2891 | // Retrieve the definition |
Sean Callanan | d9a909c | 2012-03-15 16:33:08 +0000 | [diff] [blame] | 2892 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
| 2893 | getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D)); |
Douglas Gregor | 64d9257 | 2011-12-20 15:50:13 +0000 | [diff] [blame] | 2894 | D = D->getDefinition(); |
| 2895 | assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2896 | |
| 2897 | // Look up this layout, if already laid out, return what we have. |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 2898 | const ObjCContainerDecl *Key = |
| 2899 | Impl ? (const ObjCContainerDecl*) Impl : (const ObjCContainerDecl*) D; |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2900 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 2901 | return *Entry; |
| 2902 | |
| 2903 | // Add in synthesized ivar count if laying out an implementation. |
| 2904 | if (Impl) { |
| 2905 | unsigned SynthCount = CountNonClassIvars(D); |
| 2906 | // If there aren't any sythesized ivars then reuse the interface |
| 2907 | // entry. Note we can't cache this because we simply free all |
| 2908 | // entries later; however we shouldn't look up implementations |
| 2909 | // frequently. |
| 2910 | if (SynthCount == 0) |
| 2911 | return getObjCLayout(D, 0); |
| 2912 | } |
| 2913 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2914 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2915 | Builder.Layout(D); |
| 2916 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2917 | const ASTRecordLayout *NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2918 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2919 | Builder.Alignment, |
Warren Hunt | 7b252d2 | 2013-12-06 00:01:17 +0000 | [diff] [blame] | 2920 | /*RequiredAlignment : used by MS-ABI)*/ |
| 2921 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2922 | Builder.getDataSize(), |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2923 | Builder.FieldOffsets.data(), |
| 2924 | Builder.FieldOffsets.size()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2925 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2926 | ObjCLayouts[Key] = NewEntry; |
| 2927 | |
| 2928 | return *NewEntry; |
| 2929 | } |
| 2930 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2931 | static void PrintOffset(raw_ostream &OS, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2932 | CharUnits Offset, unsigned IndentLevel) { |
Benjamin Kramer | 96ad717 | 2011-11-05 09:02:52 +0000 | [diff] [blame] | 2933 | OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity()); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2934 | OS.indent(IndentLevel * 2); |
| 2935 | } |
| 2936 | |
Eli Bendersky | f6f93ee | 2012-12-08 00:07:24 +0000 | [diff] [blame] | 2937 | static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) { |
| 2938 | OS << " | "; |
| 2939 | OS.indent(IndentLevel * 2); |
| 2940 | } |
| 2941 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2942 | static void DumpCXXRecordLayout(raw_ostream &OS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2943 | const CXXRecordDecl *RD, const ASTContext &C, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2944 | CharUnits Offset, |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2945 | unsigned IndentLevel, |
| 2946 | const char* Description, |
| 2947 | bool IncludeVirtualBases) { |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2948 | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2949 | |
| 2950 | PrintOffset(OS, Offset, IndentLevel); |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2951 | OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2952 | if (Description) |
| 2953 | OS << ' ' << Description; |
| 2954 | if (RD->isEmpty()) |
| 2955 | OS << " (empty)"; |
| 2956 | OS << '\n'; |
| 2957 | |
| 2958 | IndentLevel++; |
| 2959 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2960 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2961 | bool HasOwnVFPtr = Layout.hasOwnVFPtr(); |
| 2962 | bool HasOwnVBPtr = Layout.hasOwnVBPtr(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2963 | |
| 2964 | // Vtable pointer. |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2965 | if (RD->isDynamicClass() && !PrimaryBase && !isMsLayout(RD)) { |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2966 | PrintOffset(OS, Offset, IndentLevel); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2967 | OS << '(' << *RD << " vtable pointer)\n"; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2968 | } else if (HasOwnVFPtr) { |
| 2969 | PrintOffset(OS, Offset, IndentLevel); |
| 2970 | // vfptr (for Microsoft C++ ABI) |
| 2971 | OS << '(' << *RD << " vftable pointer)\n"; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2972 | } |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2973 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2974 | // Dump (non-virtual) bases |
| 2975 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 2976 | E = RD->bases_end(); I != E; ++I) { |
| 2977 | assert(!I->getType()->isDependentType() && |
| 2978 | "Cannot layout class with dependent bases."); |
| 2979 | if (I->isVirtual()) |
| 2980 | continue; |
| 2981 | |
| 2982 | const CXXRecordDecl *Base = |
| 2983 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2984 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2985 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2986 | |
| 2987 | DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
| 2988 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 2989 | /*IncludeVirtualBases=*/false); |
| 2990 | } |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 2991 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 2992 | // vbptr (for Microsoft C++ ABI) |
| 2993 | if (HasOwnVBPtr) { |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2994 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2995 | OS << '(' << *RD << " vbtable pointer)\n"; |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2996 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2997 | |
| 2998 | // Dump fields. |
| 2999 | uint64_t FieldNo = 0; |
| 3000 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 3001 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3002 | const FieldDecl &Field = **I; |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3003 | CharUnits FieldOffset = Offset + |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 3004 | C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo)); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3005 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3006 | if (const RecordType *RT = Field.getType()->getAs<RecordType>()) { |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3007 | if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3008 | DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel, |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3009 | Field.getName().data(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3010 | /*IncludeVirtualBases=*/true); |
| 3011 | continue; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | PrintOffset(OS, FieldOffset, IndentLevel); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3016 | OS << Field.getType().getAsString() << ' ' << Field << '\n'; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | if (!IncludeVirtualBases) |
| 3020 | return; |
| 3021 | |
| 3022 | // Dump virtual bases. |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 3023 | const ASTRecordLayout::VBaseOffsetsMapTy &vtordisps = |
| 3024 | Layout.getVBaseOffsetsMap(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3025 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 3026 | E = RD->vbases_end(); I != E; ++I) { |
| 3027 | assert(I->isVirtual() && "Found non-virtual class!"); |
| 3028 | const CXXRecordDecl *VBase = |
| 3029 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 3030 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 3031 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 3032 | |
| 3033 | if (vtordisps.find(VBase)->second.hasVtorDisp()) { |
| 3034 | PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel); |
| 3035 | OS << "(vtordisp for vbase " << *VBase << ")\n"; |
| 3036 | } |
| 3037 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3038 | DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
| 3039 | VBase == PrimaryBase ? |
| 3040 | "(primary virtual base)" : "(virtual base)", |
| 3041 | /*IncludeVirtualBases=*/false); |
| 3042 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3043 | |
Eli Bendersky | f6f93ee | 2012-12-08 00:07:24 +0000 | [diff] [blame] | 3044 | PrintIndentNoOffset(OS, IndentLevel - 1); |
| 3045 | OS << "[sizeof=" << Layout.getSize().getQuantity(); |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3046 | if (!isMsLayout(RD)) |
| 3047 | OS << ", dsize=" << Layout.getDataSize().getQuantity(); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 3048 | OS << ", align=" << Layout.getAlignment().getQuantity() << '\n'; |
Eli Bendersky | f6f93ee | 2012-12-08 00:07:24 +0000 | [diff] [blame] | 3049 | |
| 3050 | PrintIndentNoOffset(OS, IndentLevel - 1); |
| 3051 | OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity(); |
| 3052 | OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << "]\n"; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 3053 | OS << '\n'; |
| 3054 | } |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3055 | |
| 3056 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 3057 | raw_ostream &OS, |
| 3058 | bool Simple) const { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3059 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 3060 | |
| 3061 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 3062 | if (!Simple) |
| 3063 | return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0, |
| 3064 | /*IncludeVirtualBases=*/true); |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3065 | |
| 3066 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 3067 | if (!Simple) { |
| 3068 | OS << "Record: "; |
| 3069 | RD->dump(); |
| 3070 | } |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3071 | OS << "\nLayout: "; |
| 3072 | OS << "<ASTRecordLayout\n"; |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 3073 | OS << " Size:" << toBits(Info.getSize()) << "\n"; |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 3074 | if (!isMsLayout(RD)) |
| 3075 | OS << " DataSize:" << toBits(Info.getDataSize()) << "\n"; |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 3076 | OS << " Alignment:" << toBits(Info.getAlignment()) << "\n"; |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 3077 | OS << " FieldOffsets: ["; |
| 3078 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
| 3079 | if (i) OS << ", "; |
| 3080 | OS << Info.getFieldOffset(i); |
| 3081 | } |
| 3082 | OS << "]>\n"; |
| 3083 | } |