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 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 10 | #include "clang/AST/Attr.h" |
Anders Carlsson | 5adde29 | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 11 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 12 | #include "clang/AST/Decl.h" |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 18 | #include "clang/Sema/SemaDiagnostic.h" |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
| 20 | #include "llvm/ADT/SmallSet.h" |
| 21 | #include "llvm/Support/MathExtras.h" |
Ted Kremenek | f75d089 | 2011-03-19 01:00:36 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CrashRecoveryContext.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
Benjamin Kramer | c7656cd | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 26 | namespace { |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 28 | /// BaseSubobjectInfo - Represents a single base subobject in a complete class. |
| 29 | /// For a class hierarchy like |
| 30 | /// |
| 31 | /// class A { }; |
| 32 | /// class B : A { }; |
| 33 | /// class C : A, B { }; |
| 34 | /// |
| 35 | /// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo |
| 36 | /// instances, one for B and two for A. |
| 37 | /// |
| 38 | /// If a base is virtual, it will only have one BaseSubobjectInfo allocated. |
| 39 | struct BaseSubobjectInfo { |
| 40 | /// Class - The class for this base info. |
Anders Carlsson | 056818f | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 41 | const CXXRecordDecl *Class; |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 42 | |
| 43 | /// IsVirtual - Whether the BaseInfo represents a virtual base or not. |
Anders Carlsson | 056818f | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 44 | bool IsVirtual; |
| 45 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 46 | /// Bases - Information about the base subobjects. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 47 | SmallVector<BaseSubobjectInfo*, 4> Bases; |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 48 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 49 | /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base |
| 50 | /// of this base info (if one exists). |
| 51 | BaseSubobjectInfo *PrimaryVirtualBaseInfo; |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 52 | |
| 53 | // FIXME: Document. |
| 54 | const BaseSubobjectInfo *Derived; |
Anders Carlsson | 056818f | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 57 | /// EmptySubobjectMap - Keeps track of which empty subobjects exist at different |
| 58 | /// offsets while laying out a C++ class. |
| 59 | class EmptySubobjectMap { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 60 | const ASTContext &Context; |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 61 | uint64_t CharWidth; |
| 62 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 63 | /// Class - The class whose empty entries we're keeping track of. |
| 64 | const CXXRecordDecl *Class; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 65 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 66 | /// EmptyClassOffsets - A map from offsets to empty record decls. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | typedef SmallVector<const CXXRecordDecl *, 1> ClassVectorTy; |
Anders Carlsson | f8f756d | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 68 | typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 69 | EmptyClassOffsetsMapTy EmptyClassOffsets; |
| 70 | |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 71 | /// MaxEmptyClassOffset - The highest offset known to contain an empty |
| 72 | /// base subobject. |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 73 | CharUnits MaxEmptyClassOffset; |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 75 | /// ComputeEmptySubobjectSizes - Compute the size of the largest base or |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 76 | /// member subobject that is empty. |
| 77 | void ComputeEmptySubobjectSizes(); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 78 | |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 79 | void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 80 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 81 | void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 82 | CharUnits Offset, bool PlacingEmptyBase); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 83 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 84 | void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
| 85 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 86 | CharUnits Offset); |
| 87 | void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 88 | |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 89 | /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty |
| 90 | /// subobjects beyond the given offset. |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 91 | bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const { |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 92 | return Offset <= MaxEmptyClassOffset; |
| 93 | } |
| 94 | |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 95 | CharUnits |
| 96 | getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const { |
| 97 | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); |
| 98 | assert(FieldOffset % CharWidth == 0 && |
| 99 | "Field offset not at char boundary!"); |
| 100 | |
Ken Dyck | 7c4026b | 2011-01-24 01:28:50 +0000 | [diff] [blame] | 101 | return Context.toCharUnitsFromBits(FieldOffset); |
Anders Carlsson | f8f756d | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 102 | } |
Anders Carlsson | f8f756d | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 103 | |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 104 | protected: |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 105 | bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
| 106 | CharUnits Offset) const; |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 107 | |
| 108 | bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 109 | CharUnits Offset); |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 110 | |
| 111 | bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 112 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 113 | CharUnits Offset) const; |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 114 | bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 115 | CharUnits Offset) const; |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 116 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 117 | public: |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 118 | /// This holds the size of the largest empty subobject (either a base |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 119 | /// 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] | 120 | /// any empty classes. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 121 | CharUnits SizeOfLargestEmptySubobject; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 122 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 123 | EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class) |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 124 | : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 125 | ComputeEmptySubobjectSizes(); |
| 126 | } |
| 127 | |
| 128 | /// CanPlaceBaseAtOffset - Return whether the given base class can be placed |
| 129 | /// at the given offset. |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 130 | /// Returns false if placing the record will result in two components |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 131 | /// (direct or indirect) of the same type having the same offset. |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 132 | bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 133 | CharUnits Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 134 | |
| 135 | /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given |
| 136 | /// offset. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 137 | bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 138 | }; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 139 | |
| 140 | void EmptySubobjectMap::ComputeEmptySubobjectSizes() { |
| 141 | // Check the bases. |
| 142 | for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), |
| 143 | E = Class->bases_end(); I != E; ++I) { |
| 144 | const CXXRecordDecl *BaseDecl = |
| 145 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 146 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 147 | CharUnits EmptySize; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 148 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 149 | if (BaseDecl->isEmpty()) { |
| 150 | // If the class decl is empty, get its size. |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 151 | EmptySize = Layout.getSize(); |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 152 | } else { |
| 153 | // Otherwise, we get the largest empty subobject for the decl. |
| 154 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 155 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 156 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 157 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 158 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 159 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 160 | |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 161 | // Check the fields. |
| 162 | for (CXXRecordDecl::field_iterator I = Class->field_begin(), |
| 163 | E = Class->field_end(); I != E; ++I) { |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 164 | |
| 165 | const RecordType *RT = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 166 | Context.getBaseElementType(I->getType())->getAs<RecordType>(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 167 | |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 168 | // We only care about record types. |
| 169 | if (!RT) |
| 170 | continue; |
| 171 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 172 | CharUnits EmptySize; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 173 | const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 174 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl); |
| 175 | if (MemberDecl->isEmpty()) { |
| 176 | // If the class decl is empty, get its size. |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 177 | EmptySize = Layout.getSize(); |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 178 | } else { |
| 179 | // Otherwise, we get the largest empty subobject for the decl. |
| 180 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 181 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 182 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 183 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 184 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | c5ca1f7 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 188 | bool |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 189 | EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 190 | CharUnits Offset) const { |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 191 | // We only need to check empty bases. |
| 192 | if (!RD->isEmpty()) |
| 193 | return true; |
| 194 | |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 195 | EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 196 | if (I == EmptyClassOffsets.end()) |
| 197 | return true; |
| 198 | |
| 199 | const ClassVectorTy& Classes = I->second; |
| 200 | if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end()) |
| 201 | return true; |
| 202 | |
| 203 | // There is already an empty class of the same type at this offset. |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 208 | CharUnits Offset) { |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 209 | // We only care about empty bases. |
| 210 | if (!RD->isEmpty()) |
| 211 | return; |
| 212 | |
Rafael Espindola | 7bcde19 | 2010-12-29 23:02:58 +0000 | [diff] [blame] | 213 | // If we have empty structures inside an union, we can assign both |
| 214 | // the same offset. Just avoid pushing them twice in the list. |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 215 | ClassVectorTy& Classes = EmptyClassOffsets[Offset]; |
Rafael Espindola | 7bcde19 | 2010-12-29 23:02:58 +0000 | [diff] [blame] | 216 | if (std::find(Classes.begin(), Classes.end(), RD) != Classes.end()) |
| 217 | return; |
| 218 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 219 | Classes.push_back(RD); |
Anders Carlsson | cc5de09 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 220 | |
| 221 | // Update the empty class offset. |
Anders Carlsson | 725190f | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 222 | if (Offset > MaxEmptyClassOffset) |
| 223 | MaxEmptyClassOffset = Offset; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | bool |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 227 | EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
| 228 | CharUnits Offset) { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 229 | // We don't have to keep looking past the maximum offset that's known to |
| 230 | // contain an empty class. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 231 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 232 | return true; |
| 233 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 234 | if (!CanPlaceSubobjectAtOffset(Info->Class, Offset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 235 | return false; |
| 236 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 237 | // Traverse all non-virtual bases. |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 238 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 239 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 240 | BaseSubobjectInfo* Base = Info->Bases[I]; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 241 | if (Base->IsVirtual) |
| 242 | continue; |
| 243 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 244 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 245 | |
| 246 | if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset)) |
| 247 | return false; |
| 248 | } |
| 249 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 250 | if (Info->PrimaryVirtualBaseInfo) { |
| 251 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 252 | |
| 253 | if (Info == PrimaryVirtualBaseInfo->Derived) { |
| 254 | if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset)) |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 259 | // Traverse all member variables. |
| 260 | unsigned FieldNo = 0; |
| 261 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
| 262 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 263 | if (I->isBitField()) |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 264 | continue; |
| 265 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 266 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 267 | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 271 | return true; |
| 272 | } |
| 273 | |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 274 | void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 275 | CharUnits Offset, |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 276 | bool PlacingEmptyBase) { |
| 277 | if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) { |
| 278 | // We know that the only empty subobjects that can conflict with empty |
| 279 | // subobject of non-empty bases, are empty bases that can be placed at |
| 280 | // offset zero. Because of this, we only need to keep track of empty base |
| 281 | // subobjects with offsets less than the size of the largest empty |
| 282 | // subobject for our class. |
| 283 | return; |
| 284 | } |
| 285 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 286 | AddSubobjectAtOffset(Info->Class, Offset); |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 287 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 288 | // Traverse all non-virtual bases. |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 289 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 290 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
Anders Carlsson | a7f3cdb | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 291 | BaseSubobjectInfo* Base = Info->Bases[I]; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 292 | if (Base->IsVirtual) |
| 293 | continue; |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 294 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 295 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 296 | UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 299 | if (Info->PrimaryVirtualBaseInfo) { |
| 300 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 301 | |
| 302 | if (Info == PrimaryVirtualBaseInfo->Derived) |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 303 | UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset, |
| 304 | PlacingEmptyBase); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 305 | } |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 306 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 307 | // Traverse all member variables. |
| 308 | unsigned FieldNo = 0; |
| 309 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
| 310 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 311 | if (I->isBitField()) |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 312 | continue; |
Anders Carlsson | a7774a6 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 313 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 314 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 315 | UpdateEmptyFieldSubobjects(*I, FieldOffset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 316 | } |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Anders Carlsson | a60b86a | 2010-05-29 20:49:49 +0000 | [diff] [blame] | 319 | bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 320 | CharUnits Offset) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 321 | // If we know this class doesn't have any empty subobjects we don't need to |
| 322 | // bother checking. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 323 | if (SizeOfLargestEmptySubobject.isZero()) |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 324 | return true; |
| 325 | |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 326 | if (!CanPlaceBaseSubobjectAtOffset(Info, Offset)) |
| 327 | return false; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 328 | |
| 329 | // We are able to place the base at this offset. Make sure to update the |
| 330 | // empty base subobject map. |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 331 | UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty()); |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 335 | bool |
| 336 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 337 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 338 | CharUnits Offset) const { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 339 | // We don't have to keep looking past the maximum offset that's known to |
| 340 | // contain an empty class. |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 341 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 342 | return true; |
| 343 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 344 | if (!CanPlaceSubobjectAtOffset(RD, Offset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 345 | return false; |
| 346 | |
| 347 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 348 | |
| 349 | // Traverse all non-virtual bases. |
| 350 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 351 | E = RD->bases_end(); I != E; ++I) { |
| 352 | if (I->isVirtual()) |
| 353 | continue; |
| 354 | |
| 355 | const CXXRecordDecl *BaseDecl = |
| 356 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 357 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 358 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 359 | if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset)) |
| 360 | return false; |
| 361 | } |
| 362 | |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 363 | if (RD == Class) { |
| 364 | // This is the most derived class, traverse virtual bases as well. |
| 365 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 366 | E = RD->vbases_end(); I != E; ++I) { |
| 367 | const CXXRecordDecl *VBaseDecl = |
| 368 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 369 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 370 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 371 | if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset)) |
| 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 376 | // Traverse all member variables. |
| 377 | unsigned FieldNo = 0; |
| 378 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 379 | I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 380 | if (I->isBitField()) |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 381 | continue; |
| 382 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 383 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 384 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 385 | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 392 | bool |
| 393 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
| 394 | CharUnits Offset) const { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 395 | // We don't have to keep looking past the maximum offset that's known to |
| 396 | // contain an empty class. |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 397 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 398 | return true; |
| 399 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 400 | QualType T = FD->getType(); |
| 401 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 402 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 403 | return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // If we have an array type we need to look at every element. |
| 407 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 408 | QualType ElemTy = Context.getBaseElementType(AT); |
| 409 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 410 | if (!RT) |
| 411 | return true; |
| 412 | |
| 413 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 414 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 415 | |
| 416 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 417 | CharUnits ElementOffset = Offset; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 418 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 419 | // We don't have to keep looking past the maximum offset that's known to |
| 420 | // contain an empty class. |
Anders Carlsson | 233e272 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 421 | if (!AnyEmptySubobjectsBeyondOffset(ElementOffset)) |
Anders Carlsson | 45c1d28 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 422 | return true; |
| 423 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 424 | if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 425 | return false; |
| 426 | |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 427 | ElementOffset += Layout.getSize(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | bool |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 435 | EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD, |
| 436 | CharUnits Offset) { |
| 437 | if (!CanPlaceFieldSubobjectAtOffset(FD, Offset)) |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 438 | return false; |
| 439 | |
| 440 | // We are able to place the member variable at this offset. |
| 441 | // Make sure to update the empty base subobject map. |
| 442 | UpdateEmptyFieldSubobjects(FD, Offset); |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
| 447 | const CXXRecordDecl *Class, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 448 | CharUnits Offset) { |
Anders Carlsson | ae111dc | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 449 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 450 | // 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] | 451 | // zero. Because of this, we only need to keep track of empty field |
| 452 | // subobjects with offsets less than the size of the largest empty |
| 453 | // subobject for our class. |
| 454 | if (Offset >= SizeOfLargestEmptySubobject) |
| 455 | return; |
| 456 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 457 | AddSubobjectAtOffset(RD, Offset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 458 | |
| 459 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 460 | |
| 461 | // Traverse all non-virtual bases. |
| 462 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 463 | E = RD->bases_end(); I != E; ++I) { |
| 464 | if (I->isVirtual()) |
| 465 | continue; |
| 466 | |
| 467 | const CXXRecordDecl *BaseDecl = |
| 468 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 469 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 470 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 471 | UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset); |
| 472 | } |
| 473 | |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 474 | if (RD == Class) { |
| 475 | // This is the most derived class, traverse virtual bases as well. |
| 476 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 477 | E = RD->vbases_end(); I != E; ++I) { |
| 478 | const CXXRecordDecl *VBaseDecl = |
| 479 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 480 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 481 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 4468720 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 482 | UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset); |
| 483 | } |
| 484 | } |
| 485 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 486 | // Traverse all member variables. |
| 487 | unsigned FieldNo = 0; |
| 488 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 489 | I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 490 | if (I->isBitField()) |
Anders Carlsson | 09814d3 | 2010-11-01 15:14:51 +0000 | [diff] [blame] | 491 | continue; |
| 492 | |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 493 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 494 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 495 | UpdateEmptyFieldSubobjects(*I, FieldOffset); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD, |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 500 | CharUnits Offset) { |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 501 | QualType T = FD->getType(); |
| 502 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 503 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 504 | UpdateEmptyFieldSubobjects(RD, RD, Offset); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | // If we have an array type we need to update every element. |
| 509 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 510 | QualType ElemTy = Context.getBaseElementType(AT); |
| 511 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 512 | if (!RT) |
| 513 | return; |
| 514 | |
| 515 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 516 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 517 | |
| 518 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 519 | CharUnits ElementOffset = Offset; |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 520 | |
| 521 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | ae111dc | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 522 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | cc59cc5 | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 523 | // field subobjects are subobjects of empty bases that can be placed at |
Anders Carlsson | ae111dc | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 524 | // offset zero. Because of this, we only need to keep track of empty field |
| 525 | // subobjects with offsets less than the size of the largest empty |
| 526 | // subobject for our class. |
| 527 | if (ElementOffset >= SizeOfLargestEmptySubobject) |
| 528 | return; |
| 529 | |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 530 | UpdateEmptyFieldSubobjects(RD, RD, ElementOffset); |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 531 | ElementOffset += Layout.getSize(); |
Anders Carlsson | db31976 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 536 | typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy; |
| 537 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 538 | class RecordLayoutBuilder { |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 539 | protected: |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 540 | // FIXME: Remove this and make the appropriate fields public. |
| 541 | friend class clang::ASTContext; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 542 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 543 | const ASTContext &Context; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 544 | |
Anders Carlsson | f58de11 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 545 | EmptySubobjectMap *EmptySubobjects; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 547 | /// Size - The current size of the record layout. |
| 548 | uint64_t Size; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 550 | /// Alignment - The current alignment of the record layout. |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 551 | CharUnits Alignment; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 552 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 553 | /// \brief The alignment if attribute packed is not used. |
Ken Dyck | 1300b3b | 2011-02-16 02:11:31 +0000 | [diff] [blame] | 554 | CharUnits UnpackedAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 556 | SmallVector<uint64_t, 16> FieldOffsets; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 558 | /// \brief Whether the external AST source has provided a layout for this |
| 559 | /// record. |
| 560 | unsigned ExternalLayout : 1; |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 561 | |
| 562 | /// \brief Whether we need to infer alignment, even when we have an |
| 563 | /// externally-provided layout. |
| 564 | unsigned InferAlignment : 1; |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 565 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 566 | /// Packed - Whether the record is packed or not. |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 567 | unsigned Packed : 1; |
| 568 | |
| 569 | unsigned IsUnion : 1; |
| 570 | |
| 571 | unsigned IsMac68kAlign : 1; |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 572 | |
| 573 | unsigned IsMsStruct : 1; |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 574 | |
| 575 | /// UnfilledBitsInLastByte - If the last field laid out was a bitfield, |
| 576 | /// this contains the number of bits in the last byte that can be used for |
| 577 | /// an adjacent bitfield if necessary. |
| 578 | unsigned char UnfilledBitsInLastByte; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 579 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 580 | /// MaxFieldAlignment - The maximum allowed field alignment. This is set by |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 581 | /// #pragma pack. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 582 | CharUnits MaxFieldAlignment; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 583 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 584 | /// DataSize - The data size of the record being laid out. |
| 585 | uint64_t DataSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 586 | |
Ken Dyck | af1c83f | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 587 | CharUnits NonVirtualSize; |
Ken Dyck | a2d3dda | 2011-02-16 01:43:15 +0000 | [diff] [blame] | 588 | CharUnits NonVirtualAlignment; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 589 | |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 590 | FieldDecl *ZeroLengthBitfield; |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 591 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 592 | /// PrimaryBase - the primary base class (if one exists) of the class |
| 593 | /// we're laying out. |
| 594 | const CXXRecordDecl *PrimaryBase; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 595 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 596 | /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying |
| 597 | /// out is virtual. |
| 598 | bool PrimaryBaseIsVirtual; |
| 599 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 600 | /// HasOwnVFPtr - Whether the class provides its own vtable/vftbl |
| 601 | /// pointer, as opposed to inheriting one from a primary base class. |
| 602 | bool HasOwnVFPtr; |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 603 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 604 | /// VBPtrOffset - Virtual base table offset. Only for MS layout. |
| 605 | CharUnits VBPtrOffset; |
| 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), |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 648 | UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()), |
| 649 | DataSize(0), NonVirtualSize(CharUnits::Zero()), |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 650 | NonVirtualAlignment(CharUnits::One()), |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 651 | ZeroLengthBitfield(0), PrimaryBase(0), |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 652 | PrimaryBaseIsVirtual(false), |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 653 | HasOwnVFPtr(false), |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 654 | VBPtrOffset(CharUnits::fromQuantity(-1)), |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 655 | FirstNearlyEmptyVBase(0) { } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 656 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 657 | /// Reset this RecordLayoutBuilder to a fresh state, using the given |
| 658 | /// alignment as the initial alignment. This is used for the |
| 659 | /// correct layout of vb-table pointers in MSVC. |
| 660 | void resetWithTargetAlignment(CharUnits TargetAlignment) { |
| 661 | const ASTContext &Context = this->Context; |
| 662 | EmptySubobjectMap *EmptySubobjects = this->EmptySubobjects; |
| 663 | this->~RecordLayoutBuilder(); |
| 664 | new (this) RecordLayoutBuilder(Context, EmptySubobjects); |
| 665 | Alignment = UnpackedAlignment = TargetAlignment; |
| 666 | } |
| 667 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 668 | void Layout(const RecordDecl *D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 669 | void Layout(const CXXRecordDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 670 | void Layout(const ObjCInterfaceDecl *D); |
| 671 | |
| 672 | void LayoutFields(const RecordDecl *D); |
| 673 | void LayoutField(const FieldDecl *D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 674 | void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize, |
| 675 | bool FieldPacked, const FieldDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 676 | void LayoutBitField(const FieldDecl *D); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 677 | |
| 678 | bool isMicrosoftCXXABI() const { |
| 679 | return Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft; |
| 680 | } |
| 681 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 682 | void MSLayoutVirtualBases(const CXXRecordDecl *RD); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 683 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 684 | /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects. |
| 685 | llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator; |
| 686 | |
| 687 | typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *> |
| 688 | BaseSubobjectInfoMapTy; |
| 689 | |
| 690 | /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases |
| 691 | /// of the class we're laying out to their base subobject info. |
| 692 | BaseSubobjectInfoMapTy VirtualBaseInfo; |
| 693 | |
| 694 | /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the |
| 695 | /// class we're laying out to their base subobject info. |
| 696 | BaseSubobjectInfoMapTy NonVirtualBaseInfo; |
| 697 | |
| 698 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for the |
| 699 | /// bases of the given class. |
| 700 | void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD); |
| 701 | |
| 702 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for a |
| 703 | /// single class and all of its base classes. |
| 704 | BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 705 | bool IsVirtual, |
| 706 | BaseSubobjectInfo *Derived); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 707 | |
| 708 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
| 709 | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
| 710 | |
| 711 | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 712 | |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 713 | void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign); |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 714 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 715 | /// LayoutNonVirtualBases - Determines the primary base class (if any) and |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 716 | /// lays it out. Will then proceed to lay out all non-virtual base clasess. |
| 717 | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
| 718 | |
| 719 | /// LayoutNonVirtualBase - Lays out a single non-virtual base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 720 | void LayoutNonVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 721 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 722 | void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
| 723 | CharUnits Offset); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 724 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 725 | bool needsVFTable(const CXXRecordDecl *RD) const; |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 726 | bool hasNewVirtualFunction(const CXXRecordDecl *RD, |
| 727 | bool IgnoreDestructor = false) const; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 728 | bool isPossiblePrimaryBase(const CXXRecordDecl *Base) const; |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 729 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 730 | void computeVtordisps(const CXXRecordDecl *RD, |
| 731 | ClassSetTy &VtordispVBases); |
| 732 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 733 | /// LayoutVirtualBases - Lays out all the virtual bases. |
| 734 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 735 | const CXXRecordDecl *MostDerivedClass); |
| 736 | |
| 737 | /// LayoutVirtualBase - Lays out a single virtual base. |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 738 | void LayoutVirtualBase(const BaseSubobjectInfo *Base, |
| 739 | bool IsVtordispNeed = false); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 740 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 741 | /// 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] | 742 | /// placed, in chars. |
| 743 | CharUnits LayoutBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 744 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 745 | /// InitializeLayout - Initialize record layout for the given record decl. |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 746 | void InitializeLayout(const Decl *D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 747 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 748 | /// FinishLayout - Finalize record layout. Adjust record size based on the |
| 749 | /// alignment. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 750 | void FinishLayout(const NamedDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 751 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 752 | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment); |
| 753 | void UpdateAlignment(CharUnits NewAlignment) { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 754 | UpdateAlignment(NewAlignment, NewAlignment); |
| 755 | } |
| 756 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 757 | /// \brief Retrieve the externally-supplied field offset for the given |
| 758 | /// field. |
| 759 | /// |
| 760 | /// \param Field The field whose offset is being queried. |
| 761 | /// \param ComputedOffset The offset that we've computed for this field. |
| 762 | uint64_t updateExternalFieldOffset(const FieldDecl *Field, |
| 763 | uint64_t ComputedOffset); |
| 764 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 765 | void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset, |
| 766 | uint64_t UnpackedOffset, unsigned UnpackedAlign, |
| 767 | bool isPacked, const FieldDecl *D); |
| 768 | |
| 769 | DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 770 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 771 | CharUnits getSize() const { |
Ken Dyck | 3c215f2 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 772 | assert(Size % Context.getCharWidth() == 0); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 773 | return Context.toCharUnitsFromBits(Size); |
| 774 | } |
| 775 | uint64_t getSizeInBits() const { return Size; } |
| 776 | |
| 777 | void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); } |
| 778 | void setSize(uint64_t NewSize) { Size = NewSize; } |
| 779 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 780 | CharUnits getAligment() const { return Alignment; } |
| 781 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 782 | CharUnits getDataSize() const { |
Ken Dyck | 3c215f2 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 783 | assert(DataSize % Context.getCharWidth() == 0); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 784 | return Context.toCharUnitsFromBits(DataSize); |
| 785 | } |
| 786 | uint64_t getDataSizeInBits() const { return DataSize; } |
| 787 | |
| 788 | void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); } |
| 789 | void setDataSize(uint64_t NewSize) { DataSize = NewSize; } |
| 790 | |
Argyrios Kyrtzidis | 499e6e4 | 2010-08-15 10:17:39 +0000 | [diff] [blame] | 791 | RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
| 792 | void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 793 | public: |
| 794 | static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD); |
| 795 | }; |
Benjamin Kramer | c7656cd | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 796 | } // end anonymous namespace |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 797 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 798 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 799 | RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 800 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 801 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 802 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 803 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 804 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | const CXXRecordDecl *Base = |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 806 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 807 | |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 808 | // Check if this is a nearly empty virtual base. |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 809 | if (I->isVirtual() && Context.isNearlyEmpty(Base)) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 810 | // If it's not an indirect primary base, then we've found our primary |
| 811 | // base. |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 812 | if (!IndirectPrimaryBases.count(Base)) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 813 | PrimaryBase = Base; |
| 814 | PrimaryBaseIsVirtual = true; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 815 | return; |
| 816 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 817 | |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 818 | // Is this the first nearly empty virtual base? |
| 819 | if (!FirstNearlyEmptyVBase) |
| 820 | FirstNearlyEmptyVBase = Base; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 821 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 822 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 823 | SelectPrimaryVBase(Base); |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 824 | if (PrimaryBase) |
Zhongxing Xu | ec345b7 | 2010-02-15 04:28:35 +0000 | [diff] [blame] | 825 | return; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 829 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 830 | void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 831 | // If the class isn't dynamic, it won't have a primary base. |
| 832 | if (!RD->isDynamicClass()) |
| 833 | return; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 834 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 835 | // Compute all the primary virtual bases for all of our direct and |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 836 | // indirect bases, and record all their primary virtual base classes. |
Anders Carlsson | 5adde29 | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 837 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 838 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 839 | // If the record has a dynamic base class, attempt to choose a primary base |
| 840 | // 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] | 841 | // base class, if one exists. |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 842 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 843 | e = RD->bases_end(); i != e; ++i) { |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 844 | // Ignore virtual bases. |
| 845 | if (i->isVirtual()) |
| 846 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 847 | |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 848 | const CXXRecordDecl *Base = |
| 849 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 850 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 851 | if (isPossiblePrimaryBase(Base)) { |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 852 | // We found it. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 853 | PrimaryBase = Base; |
| 854 | PrimaryBaseIsVirtual = false; |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 855 | return; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 859 | // The Microsoft ABI doesn't have primary virtual bases. |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 860 | if (isMicrosoftCXXABI()) { |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 861 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
| 862 | return; |
| 863 | } |
| 864 | |
| 865 | // Under the Itanium ABI, if there is no non-virtual primary base class, |
| 866 | // try to compute the primary virtual base. The primary virtual base is |
| 867 | // the first nearly empty virtual base that is not an indirect primary |
| 868 | // virtual base class, if one exists. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 869 | if (RD->getNumVBases() != 0) { |
| 870 | SelectPrimaryVBase(RD); |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 871 | if (PrimaryBase) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 872 | return; |
| 873 | } |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 874 | |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 875 | // Otherwise, it is the first indirect primary base class, if one exists. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 876 | if (FirstNearlyEmptyVBase) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 877 | PrimaryBase = FirstNearlyEmptyVBase; |
| 878 | PrimaryBaseIsVirtual = true; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 879 | return; |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 880 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 881 | |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 882 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 885 | BaseSubobjectInfo * |
| 886 | RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 887 | bool IsVirtual, |
| 888 | BaseSubobjectInfo *Derived) { |
| 889 | BaseSubobjectInfo *Info; |
| 890 | |
| 891 | if (IsVirtual) { |
| 892 | // Check if we already have info about this virtual base. |
| 893 | BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD]; |
| 894 | if (InfoSlot) { |
| 895 | assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); |
| 896 | return InfoSlot; |
| 897 | } |
| 898 | |
| 899 | // We don't, create it. |
| 900 | InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 901 | Info = InfoSlot; |
| 902 | } else { |
| 903 | Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 904 | } |
| 905 | |
| 906 | Info->Class = RD; |
| 907 | Info->IsVirtual = IsVirtual; |
| 908 | Info->Derived = 0; |
| 909 | Info->PrimaryVirtualBaseInfo = 0; |
| 910 | |
| 911 | const CXXRecordDecl *PrimaryVirtualBase = 0; |
| 912 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0; |
| 913 | |
| 914 | // Check if this base has a primary virtual base. |
| 915 | if (RD->getNumVBases()) { |
| 916 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 917 | if (Layout.isPrimaryBaseVirtual()) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 918 | // This base does have a primary virtual base. |
| 919 | PrimaryVirtualBase = Layout.getPrimaryBase(); |
| 920 | assert(PrimaryVirtualBase && "Didn't have a primary virtual base!"); |
| 921 | |
| 922 | // Now check if we have base subobject info about this primary base. |
| 923 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 924 | |
| 925 | if (PrimaryVirtualBaseInfo) { |
| 926 | if (PrimaryVirtualBaseInfo->Derived) { |
| 927 | // We did have info about this primary base, and it turns out that it |
| 928 | // has already been claimed as a primary virtual base for another |
| 929 | // base. |
| 930 | PrimaryVirtualBase = 0; |
| 931 | } else { |
| 932 | // We can claim this base as our primary base. |
| 933 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 934 | PrimaryVirtualBaseInfo->Derived = Info; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | // Now go through all direct bases. |
| 941 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 942 | E = RD->bases_end(); I != E; ++I) { |
| 943 | bool IsVirtual = I->isVirtual(); |
| 944 | |
| 945 | const CXXRecordDecl *BaseDecl = |
| 946 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 947 | |
| 948 | Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info)); |
| 949 | } |
| 950 | |
| 951 | if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) { |
| 952 | // Traversing the bases must have created the base info for our primary |
| 953 | // virtual base. |
| 954 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 955 | assert(PrimaryVirtualBaseInfo && |
| 956 | "Did not create a primary virtual base!"); |
| 957 | |
| 958 | // Claim the primary virtual base as our primary virtual base. |
| 959 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 960 | PrimaryVirtualBaseInfo->Derived = Info; |
| 961 | } |
| 962 | |
| 963 | return Info; |
| 964 | } |
| 965 | |
| 966 | void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) { |
| 967 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 968 | E = RD->bases_end(); I != E; ++I) { |
| 969 | bool IsVirtual = I->isVirtual(); |
| 970 | |
| 971 | const CXXRecordDecl *BaseDecl = |
| 972 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 973 | |
| 974 | // Compute the base subobject info for this base. |
| 975 | BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0); |
| 976 | |
| 977 | if (IsVirtual) { |
| 978 | // ComputeBaseInfo has already added this base for us. |
| 979 | assert(VirtualBaseInfo.count(BaseDecl) && |
| 980 | "Did not add virtual base!"); |
| 981 | } else { |
| 982 | // Add the base info to the map of non-virtual bases. |
| 983 | assert(!NonVirtualBaseInfo.count(BaseDecl) && |
| 984 | "Non-virtual base already exists!"); |
| 985 | NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 990 | void |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 991 | RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) { |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 992 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
| 993 | |
| 994 | // The maximum field alignment overrides base align. |
| 995 | if (!MaxFieldAlignment.isZero()) { |
| 996 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 997 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
| 998 | } |
| 999 | |
| 1000 | // Round up the current record size to pointer alignment. |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1001 | setSize(getSize().RoundUpToAlignment(BaseAlign)); |
| 1002 | setDataSize(getSize()); |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Update the alignment. |
| 1005 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
| 1006 | } |
| 1007 | |
| 1008 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1009 | RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 1010 | // Then, determine the primary base class. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1011 | DeterminePrimaryBase(RD); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1012 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 1013 | // Compute base subobject info. |
| 1014 | ComputeBaseSubobjectInfo(RD); |
| 1015 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1016 | // If we have a primary base class, lay it out. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1017 | if (PrimaryBase) { |
| 1018 | if (PrimaryBaseIsVirtual) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 1019 | // If the primary virtual base was a primary virtual base of some other |
| 1020 | // base class we'll have to steal it. |
| 1021 | BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase); |
| 1022 | PrimaryBaseInfo->Derived = 0; |
| 1023 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1024 | // 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] | 1025 | IndirectPrimaryBases.insert(PrimaryBase); |
Anders Carlsson | fe90096 | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1026 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1027 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1028 | "vbase already visited!"); |
| 1029 | VisitedVirtualBases.insert(PrimaryBase); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1030 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1031 | LayoutVirtualBase(PrimaryBaseInfo); |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1032 | } else { |
| 1033 | BaseSubobjectInfo *PrimaryBaseInfo = |
| 1034 | NonVirtualBaseInfo.lookup(PrimaryBase); |
| 1035 | assert(PrimaryBaseInfo && |
| 1036 | "Did not find base info for non-virtual primary base!"); |
| 1037 | |
| 1038 | LayoutNonVirtualBase(PrimaryBaseInfo); |
| 1039 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1040 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1041 | // If this class needs a vtable/vf-table and didn't get one from a |
| 1042 | // primary base, add it in now. |
| 1043 | } else if (needsVFTable(RD)) { |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1044 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1045 | CharUnits PtrWidth = |
| 1046 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1047 | CharUnits PtrAlign = |
| 1048 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
| 1049 | EnsureVTablePointerAlignment(PtrAlign); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1050 | HasOwnVFPtr = true; |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1051 | setSize(getSize() + PtrWidth); |
| 1052 | setDataSize(getSize()); |
| 1053 | } |
| 1054 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1055 | bool HasDirectVirtualBases = false; |
| 1056 | bool HasNonVirtualBaseWithVBTable = false; |
| 1057 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1058 | // Now lay out the non-virtual bases. |
| 1059 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1060 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1061 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1062 | // Ignore virtual bases, but remember that we saw one. |
| 1063 | if (I->isVirtual()) { |
| 1064 | HasDirectVirtualBases = true; |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1065 | continue; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1066 | } |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1067 | |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1068 | const CXXRecordDecl *BaseDecl = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1069 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1070 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1071 | // Remember if this base has virtual bases itself. |
| 1072 | if (BaseDecl->getNumVBases()) |
| 1073 | HasNonVirtualBaseWithVBTable = true; |
| 1074 | |
| 1075 | // Skip the primary base, because we've already laid it out. The |
| 1076 | // !PrimaryBaseIsVirtual check is required because we might have a |
| 1077 | // non-virtual base of the same type as a primary virtual base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1078 | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1079 | continue; |
| 1080 | |
| 1081 | // Lay out the base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1082 | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
| 1083 | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
| 1084 | |
| 1085 | LayoutNonVirtualBase(BaseInfo); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1086 | } |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1087 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1088 | // In the MS ABI, add the vb-table pointer if we need one, which is |
| 1089 | // whenever we have a virtual base and we can't re-use a vb-table |
| 1090 | // pointer from a non-virtual base. |
| 1091 | if (isMicrosoftCXXABI() && |
| 1092 | HasDirectVirtualBases && !HasNonVirtualBaseWithVBTable) { |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1093 | CharUnits PtrWidth = |
| 1094 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1095 | CharUnits PtrAlign = |
| 1096 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1097 | |
| 1098 | // MSVC potentially over-aligns the vb-table pointer by giving it |
| 1099 | // the max alignment of all the non-virtual objects in the class. |
| 1100 | // This is completely unnecessary, but we're not here to pass |
| 1101 | // judgment. |
| 1102 | // |
| 1103 | // Note that we've only laid out the non-virtual bases, so on the |
| 1104 | // first pass Alignment won't be set correctly here, but if the |
| 1105 | // vb-table doesn't end up aligned correctly we'll come through |
| 1106 | // and redo the layout from scratch with the right alignment. |
| 1107 | // |
| 1108 | // TODO: Instead of doing this, just lay out the fields as if the |
| 1109 | // vb-table were at offset zero, then retroactively bump the field |
| 1110 | // offsets up. |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1111 | PtrAlign = std::max(PtrAlign, Alignment); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1112 | |
| 1113 | EnsureVTablePointerAlignment(PtrAlign); |
| 1114 | VBPtrOffset = getSize(); |
| 1115 | setSize(getSize() + PtrWidth); |
| 1116 | setDataSize(getSize()); |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1117 | } |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1120 | void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1121 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1122 | CharUnits Offset = LayoutBase(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1123 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1124 | // Add its base class offset. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1125 | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1126 | Bases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1127 | |
| 1128 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1129 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1130 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1131 | void |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1132 | RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1133 | CharUnits Offset) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1134 | // This base isn't interesting, it has no virtual bases. |
| 1135 | if (!Info->Class->getNumVBases()) |
| 1136 | return; |
| 1137 | |
| 1138 | // First, check if we have a virtual primary base to add offsets for. |
| 1139 | if (Info->PrimaryVirtualBaseInfo) { |
| 1140 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
| 1141 | "Primary virtual base is not virtual!"); |
| 1142 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
| 1143 | // Add the offset. |
| 1144 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
| 1145 | "primary vbase offset already exists!"); |
| 1146 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1147 | ASTRecordLayout::VBaseInfo(Offset, false))); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1148 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1149 | // Traverse the primary virtual base. |
| 1150 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
| 1151 | } |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1154 | // Now go through all direct non-virtual bases. |
| 1155 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
| 1156 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
| 1157 | const BaseSubobjectInfo *Base = Info->Bases[I]; |
| 1158 | if (Base->IsVirtual) |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1159 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1160 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 1161 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1162 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1166 | /// needsVFTable - Return true if this class needs a vtable or vf-table |
| 1167 | /// when laid out as a base class. These are treated the same because |
| 1168 | /// they're both always laid out at offset zero. |
| 1169 | /// |
| 1170 | /// This function assumes that the class has no primary base. |
| 1171 | bool RecordLayoutBuilder::needsVFTable(const CXXRecordDecl *RD) const { |
| 1172 | assert(!PrimaryBase); |
| 1173 | |
| 1174 | // In the Itanium ABI, every dynamic class needs a vtable: even if |
| 1175 | // this class has no virtual functions as a base class (i.e. it's |
| 1176 | // non-polymorphic or only has virtual functions from virtual |
| 1177 | // bases),x it still needs a vtable to locate its virtual bases. |
| 1178 | if (!isMicrosoftCXXABI()) |
| 1179 | return RD->isDynamicClass(); |
| 1180 | |
| 1181 | // In the MS ABI, we need a vfptr if the class has virtual functions |
| 1182 | // other than those declared by its virtual bases. The AST doesn't |
| 1183 | // tell us that directly, and checking manually for virtual |
| 1184 | // functions that aren't overrides is expensive, but there are |
| 1185 | // some important shortcuts: |
| 1186 | |
| 1187 | // - Non-polymorphic classes have no virtual functions at all. |
| 1188 | if (!RD->isPolymorphic()) return false; |
| 1189 | |
| 1190 | // - Polymorphic classes with no virtual bases must either declare |
| 1191 | // virtual functions directly or inherit them, but in the latter |
| 1192 | // case we would have a primary base. |
| 1193 | if (RD->getNumVBases() == 0) return true; |
| 1194 | |
| 1195 | return hasNewVirtualFunction(RD); |
| 1196 | } |
| 1197 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1198 | /// Does the given class inherit non-virtually from any of the classes |
| 1199 | /// in the given set? |
| 1200 | static bool hasNonVirtualBaseInSet(const CXXRecordDecl *RD, |
| 1201 | const ClassSetTy &set) { |
| 1202 | for (CXXRecordDecl::base_class_const_iterator |
| 1203 | I = RD->bases_begin(), E = RD->bases_end(); I != E; ++I) { |
| 1204 | // Ignore virtual links. |
| 1205 | if (I->isVirtual()) continue; |
| 1206 | |
| 1207 | // Check whether the set contains the base. |
| 1208 | const CXXRecordDecl *base = I->getType()->getAsCXXRecordDecl(); |
| 1209 | if (set.count(base)) |
| 1210 | return true; |
| 1211 | |
| 1212 | // Otherwise, recurse and propagate. |
| 1213 | if (hasNonVirtualBaseInSet(base, set)) |
| 1214 | return true; |
| 1215 | } |
| 1216 | |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
| 1220 | /// Does the given method (B::foo()) already override a method (A::foo()) |
| 1221 | /// such that A requires a vtordisp in B? If so, we don't need to add a |
| 1222 | /// new vtordisp for B in a yet-more-derived class C providing C::foo(). |
| 1223 | static bool overridesMethodRequiringVtorDisp(const ASTContext &Context, |
| 1224 | const CXXMethodDecl *M) { |
| 1225 | CXXMethodDecl::method_iterator |
| 1226 | I = M->begin_overridden_methods(), E = M->end_overridden_methods(); |
| 1227 | if (I == E) return false; |
| 1228 | |
| 1229 | const ASTRecordLayout::VBaseOffsetsMapTy &offsets = |
| 1230 | Context.getASTRecordLayout(M->getParent()).getVBaseOffsetsMap(); |
| 1231 | do { |
| 1232 | const CXXMethodDecl *overridden = *I; |
| 1233 | |
| 1234 | // If the overridden method's class isn't recognized as a virtual |
| 1235 | // base in the derived class, ignore it. |
| 1236 | ASTRecordLayout::VBaseOffsetsMapTy::const_iterator |
| 1237 | it = offsets.find(overridden->getParent()); |
| 1238 | if (it == offsets.end()) continue; |
| 1239 | |
| 1240 | // Otherwise, check if the overridden method's class needs a vtordisp. |
| 1241 | if (it->second.hasVtorDisp()) return true; |
| 1242 | |
| 1243 | } while (++I != E); |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | /// In the Microsoft ABI, decide which of the virtual bases require a |
| 1248 | /// vtordisp field. |
| 1249 | void RecordLayoutBuilder::computeVtordisps(const CXXRecordDecl *RD, |
| 1250 | ClassSetTy &vtordispVBases) { |
| 1251 | // Bail out if we have no virtual bases. |
| 1252 | assert(RD->getNumVBases()); |
| 1253 | |
| 1254 | // Build up the set of virtual bases that we haven't decided yet. |
| 1255 | ClassSetTy undecidedVBases; |
| 1256 | for (CXXRecordDecl::base_class_const_iterator |
| 1257 | I = RD->vbases_begin(), E = RD->vbases_end(); I != E; ++I) { |
| 1258 | const CXXRecordDecl *vbase = I->getType()->getAsCXXRecordDecl(); |
| 1259 | undecidedVBases.insert(vbase); |
| 1260 | } |
| 1261 | assert(!undecidedVBases.empty()); |
| 1262 | |
| 1263 | // A virtual base requires a vtordisp field in a derived class if it |
| 1264 | // requires a vtordisp field in a base class. Walk all the direct |
| 1265 | // bases and collect this information. |
| 1266 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1267 | E = RD->bases_end(); I != E; ++I) { |
| 1268 | const CXXRecordDecl *base = I->getType()->getAsCXXRecordDecl(); |
| 1269 | const ASTRecordLayout &baseLayout = Context.getASTRecordLayout(base); |
| 1270 | |
| 1271 | // Iterate over the set of virtual bases provided by this class. |
| 1272 | for (ASTRecordLayout::VBaseOffsetsMapTy::const_iterator |
| 1273 | VI = baseLayout.getVBaseOffsetsMap().begin(), |
| 1274 | VE = baseLayout.getVBaseOffsetsMap().end(); VI != VE; ++VI) { |
| 1275 | // If it doesn't need a vtordisp in this base, ignore it. |
| 1276 | if (!VI->second.hasVtorDisp()) continue; |
| 1277 | |
| 1278 | // If we've already seen it and decided it needs a vtordisp, ignore it. |
| 1279 | if (!undecidedVBases.erase(VI->first)) |
| 1280 | continue; |
| 1281 | |
| 1282 | // Add it. |
| 1283 | vtordispVBases.insert(VI->first); |
| 1284 | |
| 1285 | // Quit as soon as we've decided everything. |
| 1286 | if (undecidedVBases.empty()) |
| 1287 | return; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | // Okay, we have virtual bases that we haven't yet decided about. A |
| 1292 | // virtual base requires a vtordisp if any the non-destructor |
| 1293 | // virtual methods declared in this class directly override a method |
| 1294 | // provided by that virtual base. (If so, we need to emit a thunk |
| 1295 | // for that method, to be used in the construction vftable, which |
| 1296 | // applies an additional 'vtordisp' this-adjustment.) |
| 1297 | |
| 1298 | // Collect the set of bases directly overridden by any method in this class. |
| 1299 | // It's possible that some of these classes won't be virtual bases, or won't be |
| 1300 | // provided by virtual bases, or won't be virtual bases in the overridden |
| 1301 | // instance but are virtual bases elsewhere. Only the last matters for what |
| 1302 | // we're doing, and we can ignore those: if we don't directly override |
| 1303 | // a method provided by a virtual copy of a base class, but we do directly |
| 1304 | // override a method provided by a non-virtual copy of that base class, |
| 1305 | // then we must indirectly override the method provided by the virtual base, |
| 1306 | // and so we should already have collected it in the loop above. |
| 1307 | ClassSetTy overriddenBases; |
| 1308 | for (CXXRecordDecl::method_iterator |
| 1309 | M = RD->method_begin(), E = RD->method_end(); M != E; ++M) { |
| 1310 | // Ignore non-virtual methods and destructors. |
| 1311 | if (isa<CXXDestructorDecl>(*M) || !M->isVirtual()) |
| 1312 | continue; |
| 1313 | |
| 1314 | for (CXXMethodDecl::method_iterator I = M->begin_overridden_methods(), |
| 1315 | E = M->end_overridden_methods(); I != E; ++I) { |
| 1316 | const CXXMethodDecl *overriddenMethod = (*I); |
| 1317 | |
| 1318 | // Ignore methods that override methods from vbases that require |
| 1319 | // require vtordisps. |
| 1320 | if (overridesMethodRequiringVtorDisp(Context, overriddenMethod)) |
| 1321 | continue; |
| 1322 | |
| 1323 | // As an optimization, check immediately whether we're overriding |
| 1324 | // something from the undecided set. |
| 1325 | const CXXRecordDecl *overriddenBase = overriddenMethod->getParent(); |
| 1326 | if (undecidedVBases.erase(overriddenBase)) { |
| 1327 | vtordispVBases.insert(overriddenBase); |
| 1328 | if (undecidedVBases.empty()) return; |
| 1329 | |
| 1330 | // We can't 'continue;' here because one of our undecided |
| 1331 | // vbases might non-virtually inherit from this base. |
| 1332 | // Consider: |
| 1333 | // struct A { virtual void foo(); }; |
| 1334 | // struct B : A {}; |
| 1335 | // struct C : virtual A, virtual B { virtual void foo(); }; |
| 1336 | // We need a vtordisp for B here. |
| 1337 | } |
| 1338 | |
| 1339 | // Otherwise, just collect it. |
| 1340 | overriddenBases.insert(overriddenBase); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | // Walk the undecided v-bases and check whether they (non-virtually) |
| 1345 | // provide any of the overridden bases. We don't need to consider |
| 1346 | // virtual links because the vtordisp inheres to the layout |
| 1347 | // subobject containing the base. |
| 1348 | for (ClassSetTy::const_iterator |
| 1349 | I = undecidedVBases.begin(), E = undecidedVBases.end(); I != E; ++I) { |
| 1350 | if (hasNonVirtualBaseInSet(*I, overriddenBases)) |
| 1351 | vtordispVBases.insert(*I); |
| 1352 | } |
| 1353 | } |
| 1354 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1355 | /// hasNewVirtualFunction - Does the given polymorphic class declare a |
| 1356 | /// virtual function that does not override a method from any of its |
| 1357 | /// base classes? |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1358 | bool |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1359 | RecordLayoutBuilder::hasNewVirtualFunction(const CXXRecordDecl *RD, |
| 1360 | bool IgnoreDestructor) const { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1361 | if (!RD->getNumBases()) |
| 1362 | return true; |
| 1363 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1364 | for (CXXRecordDecl::method_iterator method = RD->method_begin(); |
| 1365 | method != RD->method_end(); |
| 1366 | ++method) { |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1367 | if (method->isVirtual() && !method->size_overridden_methods() && |
| 1368 | !(IgnoreDestructor && method->getKind() == Decl::CXXDestructor)) { |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1369 | return true; |
| 1370 | } |
| 1371 | } |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1375 | /// isPossiblePrimaryBase - Is the given base class an acceptable |
| 1376 | /// primary base class? |
Eli Friedman | 5e9534b | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1377 | bool |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1378 | RecordLayoutBuilder::isPossiblePrimaryBase(const CXXRecordDecl *base) const { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1379 | // In the Itanium ABI, a class can be a primary base class if it has |
| 1380 | // a vtable for any reason. |
| 1381 | if (!isMicrosoftCXXABI()) |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1382 | return base->isDynamicClass(); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1383 | |
| 1384 | // In the MS ABI, a class can only be a primary base class if it |
| 1385 | // provides a vf-table at a static offset. That means it has to be |
| 1386 | // non-virtual base. The existence of a separate vb-table means |
| 1387 | // that it's possible to get virtual functions only from a virtual |
| 1388 | // base, which we have to guard against. |
| 1389 | |
| 1390 | // First off, it has to have virtual functions. |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1391 | if (!base->isPolymorphic()) return false; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1392 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1393 | // If it has no virtual bases, then the vfptr must be at a static offset. |
| 1394 | if (!base->getNumVBases()) return true; |
| 1395 | |
| 1396 | // Otherwise, the necessary information is cached in the layout. |
| 1397 | const ASTRecordLayout &layout = Context.getASTRecordLayout(base); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1398 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1399 | // If the base has its own vfptr, it can be a primary base. |
| 1400 | if (layout.hasOwnVFPtr()) return true; |
| 1401 | |
| 1402 | // If the base has a primary base class, then it can be a primary base. |
| 1403 | if (layout.getPrimaryBase()) return true; |
| 1404 | |
| 1405 | // Otherwise it can't. |
| 1406 | return false; |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1409 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1410 | RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1411 | const CXXRecordDecl *MostDerivedClass) { |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1412 | const CXXRecordDecl *PrimaryBase; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1413 | bool PrimaryBaseIsVirtual; |
Anders Carlsson | fe90096 | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1414 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1415 | if (MostDerivedClass == RD) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1416 | PrimaryBase = this->PrimaryBase; |
| 1417 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1418 | } else { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1419 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1420 | PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 1421 | PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1424 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1425 | E = RD->bases_end(); I != E; ++I) { |
| 1426 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1427 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1428 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1429 | const CXXRecordDecl *BaseDecl = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1430 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1431 | |
| 1432 | if (I->isVirtual()) { |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1433 | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) { |
| 1434 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1435 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1436 | // Only lay out the virtual base if it's not an indirect primary base. |
| 1437 | if (!IndirectPrimaryBase) { |
| 1438 | // Only visit virtual bases once. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1439 | if (!VisitedVirtualBases.insert(BaseDecl)) |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1440 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1441 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1442 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1443 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1444 | LayoutVirtualBase(BaseInfo); |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 1445 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1446 | } |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 1447 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1448 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1449 | if (!BaseDecl->getNumVBases()) { |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1450 | // This base isn't interesting since it doesn't have any virtual bases. |
| 1451 | continue; |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 1452 | } |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1453 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1454 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1458 | void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1459 | if (!RD->getNumVBases()) |
| 1460 | return; |
| 1461 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1462 | ClassSetTy VtordispVBases; |
| 1463 | computeVtordisps(RD, VtordispVBases); |
| 1464 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1465 | // This is substantially simplified because there are no virtual |
| 1466 | // primary bases. |
| 1467 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1468 | E = RD->vbases_end(); I != E; ++I) { |
| 1469 | const CXXRecordDecl *BaseDecl = I->getType()->getAsCXXRecordDecl(); |
| 1470 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1471 | assert(BaseInfo && "Did not find virtual base info!"); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1472 | |
| 1473 | // If this base requires a vtordisp, add enough space for an int field. |
| 1474 | // This is apparently always 32-bits, even on x64. |
| 1475 | bool vtordispNeeded = false; |
| 1476 | if (VtordispVBases.count(BaseDecl)) { |
| 1477 | CharUnits IntSize = |
| 1478 | CharUnits::fromQuantity(Context.getTargetInfo().getIntWidth() / 8); |
| 1479 | |
| 1480 | setSize(getSize() + IntSize); |
| 1481 | setDataSize(getSize()); |
| 1482 | vtordispNeeded = true; |
| 1483 | } |
| 1484 | |
| 1485 | LayoutVirtualBase(BaseInfo, vtordispNeeded); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1489 | void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base, |
| 1490 | bool IsVtordispNeed) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1491 | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
| 1492 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1493 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1494 | CharUnits Offset = LayoutBase(Base); |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1495 | |
| 1496 | // Add its base class offset. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1497 | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1498 | VBases.insert(std::make_pair(Base->Class, |
| 1499 | ASTRecordLayout::VBaseInfo(Offset, IsVtordispNeed))); |
| 1500 | |
| 1501 | if (!isMicrosoftCXXABI()) |
| 1502 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1505 | CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1506 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1507 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1508 | |
| 1509 | CharUnits Offset; |
| 1510 | |
| 1511 | // Query the external layout to see if it provides an offset. |
| 1512 | bool HasExternalLayout = false; |
| 1513 | if (ExternalLayout) { |
| 1514 | llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known; |
| 1515 | if (Base->IsVirtual) { |
| 1516 | Known = ExternalVirtualBaseOffsets.find(Base->Class); |
| 1517 | if (Known != ExternalVirtualBaseOffsets.end()) { |
| 1518 | Offset = Known->second; |
| 1519 | HasExternalLayout = true; |
| 1520 | } |
| 1521 | } else { |
| 1522 | Known = ExternalBaseOffsets.find(Base->Class); |
| 1523 | if (Known != ExternalBaseOffsets.end()) { |
| 1524 | Offset = Known->second; |
| 1525 | HasExternalLayout = true; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1530 | // 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] | 1531 | if (Base->Class->isEmpty() && |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1532 | (!HasExternalLayout || Offset == CharUnits::Zero()) && |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1533 | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1534 | setSize(std::max(getSize(), Layout.getSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1535 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1536 | return CharUnits::Zero(); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1537 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1538 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1539 | CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign(); |
| 1540 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1541 | |
| 1542 | // The maximum field alignment overrides base align. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1543 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1544 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 1545 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1546 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1548 | if (!HasExternalLayout) { |
| 1549 | // Round up the current record size to the base's alignment boundary. |
| 1550 | Offset = getDataSize().RoundUpToAlignment(BaseAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1551 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1552 | // Try to place the base. |
| 1553 | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset)) |
| 1554 | Offset += BaseAlign; |
| 1555 | } else { |
| 1556 | bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset); |
| 1557 | (void)Allowed; |
| 1558 | assert(Allowed && "Base subobject externally placed at overlapping offset"); |
| 1559 | } |
| 1560 | |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1561 | if (!Base->Class->isEmpty()) { |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1562 | // Update the data size. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1563 | setDataSize(Offset + Layout.getNonVirtualSize()); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1564 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1565 | setSize(std::max(getSize(), getDataSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1566 | } else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1567 | setSize(std::max(getSize(), Offset + Layout.getSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Remember max struct/class alignment. |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1570 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1571 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1572 | return Offset; |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1575 | void RecordLayoutBuilder::InitializeLayout(const Decl *D) { |
| 1576 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 1577 | IsUnion = RD->isUnion(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1578 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 1579 | Packed = D->hasAttr<PackedAttr>(); |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1580 | |
| 1581 | IsMsStruct = D->hasAttr<MsStructAttr>(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1582 | |
Daniel Dunbar | 096ed29 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1583 | // Honor the default struct packing maximum alignment flag. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1584 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) { |
Daniel Dunbar | 096ed29 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1585 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
| 1586 | } |
| 1587 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1588 | // mac68k alignment supersedes maximum field alignment and attribute aligned, |
| 1589 | // and forces all structures to have 2-byte alignment. The IBM docs on it |
| 1590 | // allude to additional (more complicated) semantics, especially with regard |
| 1591 | // to bit-fields, but gcc appears not to follow that. |
| 1592 | if (D->hasAttr<AlignMac68kAttr>()) { |
| 1593 | IsMac68kAlign = true; |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1594 | MaxFieldAlignment = CharUnits::fromQuantity(2); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1595 | Alignment = CharUnits::fromQuantity(2); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1596 | } else { |
| 1597 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1598 | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1599 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1600 | if (unsigned MaxAlign = D->getMaxAlignment()) |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1601 | UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1602 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1603 | |
| 1604 | // If there is an external AST source, ask it for the various offsets. |
| 1605 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 1606 | if (ExternalASTSource *External = Context.getExternalSource()) { |
| 1607 | ExternalLayout = External->layoutRecordType(RD, |
| 1608 | ExternalSize, |
| 1609 | ExternalAlign, |
| 1610 | ExternalFieldOffsets, |
| 1611 | ExternalBaseOffsets, |
| 1612 | ExternalVirtualBaseOffsets); |
| 1613 | |
| 1614 | // Update based on external alignment. |
| 1615 | if (ExternalLayout) { |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1616 | if (ExternalAlign > 0) { |
| 1617 | Alignment = Context.toCharUnitsFromBits(ExternalAlign); |
| 1618 | UnpackedAlignment = Alignment; |
| 1619 | } else { |
| 1620 | // The external source didn't have alignment information; infer it. |
| 1621 | InferAlignment = true; |
| 1622 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1625 | } |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 1626 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1627 | void RecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 1628 | InitializeLayout(D); |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1629 | LayoutFields(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1631 | // Finally, round the size of the total struct up to the alignment of the |
| 1632 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1633 | FinishLayout(D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
| 1637 | InitializeLayout(RD); |
| 1638 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1639 | // Lay out the vtable and the non-virtual bases. |
| 1640 | LayoutNonVirtualBases(RD); |
| 1641 | |
| 1642 | LayoutFields(RD); |
| 1643 | |
Ken Dyck | e738075 | 2011-03-10 01:53:59 +0000 | [diff] [blame] | 1644 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1645 | llvm::RoundUpToAlignment(getSizeInBits(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1646 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1647 | NonVirtualAlignment = Alignment; |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1648 | |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1649 | if (isMicrosoftCXXABI()) { |
| 1650 | if (NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1651 | CharUnits AlignMember = |
| 1652 | NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize; |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1653 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1654 | setSize(getSize() + AlignMember); |
| 1655 | setDataSize(getSize()); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1656 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1657 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1658 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 1659 | Context.getTargetInfo().getCharAlign())); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 1660 | } |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1661 | |
| 1662 | MSLayoutVirtualBases(RD); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1663 | } else { |
| 1664 | // Lay out the virtual bases and add the primary virtual base offsets. |
| 1665 | LayoutVirtualBases(RD, RD); |
| 1666 | } |
| 1667 | |
| 1668 | // Finally, round the size of the total struct up to the alignment |
Eli Friedman | 83a1258 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 1669 | // of the struct itself. |
| 1670 | FinishLayout(RD); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1671 | |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1672 | #ifndef NDEBUG |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1673 | // Check that we have base offsets for all bases. |
| 1674 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1675 | E = RD->bases_end(); I != E; ++I) { |
| 1676 | if (I->isVirtual()) |
| 1677 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1678 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1679 | const CXXRecordDecl *BaseDecl = |
| 1680 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1681 | |
| 1682 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 1683 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1684 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1685 | // And all virtual bases. |
| 1686 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1687 | E = RD->vbases_end(); I != E; ++I) { |
| 1688 | const CXXRecordDecl *BaseDecl = |
| 1689 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1690 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1691 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1692 | } |
| 1693 | #endif |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1696 | void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1697 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1698 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1699 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1700 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1702 | // We start laying out ivars not at the end of the superclass |
| 1703 | // structure, but at the next byte following the last field. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1704 | setSize(SL.getDataSize()); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1705 | setDataSize(getSize()); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1706 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1708 | InitializeLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1709 | // Layout each ivar sequentially. |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1710 | for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD; |
| 1711 | IVD = IVD->getNextIvar()) |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1712 | LayoutField(IVD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1714 | // Finally, round the size of the total struct up to the alignment of the |
| 1715 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1716 | FinishLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1719 | void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1720 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 1721 | // the future, this will need to be tweakable by targets. |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1722 | const FieldDecl *LastFD = 0; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1723 | ZeroLengthBitfield = 0; |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1724 | unsigned RemainingInAlignment = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1726 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) { |
| 1727 | if (IsMsStruct) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 1728 | FieldDecl *FD = *Field; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1729 | if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD)) |
| 1730 | ZeroLengthBitfield = FD; |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1731 | // Zero-length bitfields following non-bitfield members are |
| 1732 | // ignored: |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1733 | else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD)) |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1734 | continue; |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1735 | // FIXME. streamline these conditions into a simple one. |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1736 | else if (Context.BitfieldFollowsBitfield(FD, LastFD) || |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1737 | Context.BitfieldFollowsNonBitfield(FD, LastFD) || |
| 1738 | Context.NonBitfieldFollowsBitfield(FD, LastFD)) { |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1739 | // 1) Adjacent bit fields are packed into the same 1-, 2-, or |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1740 | // 4-byte allocation unit if the integral types are the same |
| 1741 | // size and if the next bit field fits into the current |
| 1742 | // allocation unit without crossing the boundary imposed by the |
| 1743 | // common alignment requirements of the bit fields. |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1744 | // 2) Establish a new alignment for a bitfield following |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1745 | // a non-bitfield if size of their types differ. |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1746 | // 3) Establish a new alignment for a non-bitfield following |
| 1747 | // a bitfield if size of their types differ. |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1748 | std::pair<uint64_t, unsigned> FieldInfo = |
| 1749 | Context.getTypeInfo(FD->getType()); |
| 1750 | uint64_t TypeSize = FieldInfo.first; |
| 1751 | unsigned FieldAlign = FieldInfo.second; |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1752 | // This check is needed for 'long long' in -m32 mode. |
Fariborz Jahanian | 0586df4 | 2011-12-12 21:16:36 +0000 | [diff] [blame] | 1753 | if (TypeSize > FieldAlign && |
| 1754 | (Context.hasSameType(FD->getType(), |
| 1755 | Context.UnsignedLongLongTy) |
| 1756 | ||Context.hasSameType(FD->getType(), |
| 1757 | Context.LongLongTy))) |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1758 | FieldAlign = TypeSize; |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1759 | FieldInfo = Context.getTypeInfo(LastFD->getType()); |
| 1760 | uint64_t TypeSizeLastFD = FieldInfo.first; |
| 1761 | unsigned FieldAlignLastFD = FieldInfo.second; |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1762 | // This check is needed for 'long long' in -m32 mode. |
Fariborz Jahanian | 0586df4 | 2011-12-12 21:16:36 +0000 | [diff] [blame] | 1763 | if (TypeSizeLastFD > FieldAlignLastFD && |
| 1764 | (Context.hasSameType(LastFD->getType(), |
| 1765 | Context.UnsignedLongLongTy) |
| 1766 | || Context.hasSameType(LastFD->getType(), |
| 1767 | Context.LongLongTy))) |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1768 | FieldAlignLastFD = TypeSizeLastFD; |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1769 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1770 | if (TypeSizeLastFD != TypeSize) { |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1771 | if (RemainingInAlignment && |
| 1772 | LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1773 | LastFD->getBitWidthValue(Context)) { |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1774 | // If previous field was a bitfield with some remaining unfilled |
| 1775 | // bits, pad the field so current field starts on its type boundary. |
| 1776 | uint64_t FieldOffset = |
| 1777 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1778 | uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset; |
| 1779 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1780 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1781 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1782 | RemainingInAlignment = 0; |
| 1783 | } |
| 1784 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1785 | uint64_t UnpaddedFieldOffset = |
| 1786 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1787 | FieldAlign = std::max(FieldAlign, FieldAlignLastFD); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1788 | |
Fariborz Jahanian | 07ceb0a | 2011-05-10 19:00:50 +0000 | [diff] [blame] | 1789 | // The maximum field alignment overrides the aligned attribute. |
| 1790 | if (!MaxFieldAlignment.isZero()) { |
| 1791 | unsigned MaxFieldAlignmentInBits = |
| 1792 | Context.toBits(MaxFieldAlignment); |
| 1793 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1794 | } |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1795 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1796 | uint64_t NewSizeInBits = |
| 1797 | llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign); |
| 1798 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1799 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1800 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
| 1801 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1802 | } |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1803 | if (FD->isBitField()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1804 | uint64_t FieldSize = FD->getBitWidthValue(Context); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1805 | assert (FieldSize > 0 && "LayoutFields - ms_struct layout"); |
| 1806 | if (RemainingInAlignment < FieldSize) |
| 1807 | RemainingInAlignment = TypeSize - FieldSize; |
| 1808 | else |
| 1809 | RemainingInAlignment -= FieldSize; |
| 1810 | } |
| 1811 | } |
| 1812 | else if (FD->isBitField()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1813 | uint64_t FieldSize = FD->getBitWidthValue(Context); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1814 | std::pair<uint64_t, unsigned> FieldInfo = |
| 1815 | Context.getTypeInfo(FD->getType()); |
| 1816 | uint64_t TypeSize = FieldInfo.first; |
| 1817 | RemainingInAlignment = TypeSize - FieldSize; |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1818 | } |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1819 | LastFD = FD; |
| 1820 | } |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1821 | else if (!Context.getTargetInfo().useBitFieldTypeAlignment() && |
| 1822 | Context.getTargetInfo().useZeroLengthBitfieldAlignment()) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 1823 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 1824 | ZeroLengthBitfield = *Field; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1825 | } |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 1826 | LayoutField(*Field); |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1827 | } |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1828 | if (IsMsStruct && RemainingInAlignment && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1829 | LastFD && LastFD->isBitField() && LastFD->getBitWidthValue(Context)) { |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1830 | // If we ended a bitfield before the full length of the type then |
| 1831 | // pad the struct out to the full length of the last type. |
| 1832 | uint64_t FieldOffset = |
| 1833 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1834 | uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset; |
| 1835 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1836 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1837 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1838 | } |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1841 | void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1842 | uint64_t TypeSize, |
| 1843 | bool FieldPacked, |
| 1844 | const FieldDecl *D) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1845 | assert(Context.getLangOpts().CPlusPlus && |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1846 | "Can only have wide bit-fields in C++!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1847 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1848 | // Itanium C++ ABI 2.4: |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1849 | // 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] | 1850 | // sizeof(T')*8 <= n. |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1851 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1852 | QualType IntegralPODTypes[] = { |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1853 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1854 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
| 1855 | }; |
| 1856 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1857 | QualType Type; |
| 1858 | for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes); |
| 1859 | I != E; ++I) { |
| 1860 | uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1861 | |
| 1862 | if (Size > FieldSize) |
| 1863 | break; |
| 1864 | |
| 1865 | Type = IntegralPODTypes[I]; |
| 1866 | } |
| 1867 | assert(!Type.isNull() && "Did not find a type!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1868 | |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1869 | CharUnits TypeAlign = Context.getTypeAlignInChars(Type); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1870 | |
| 1871 | // We're not going to use any of the unfilled bits in the last byte. |
| 1872 | UnfilledBitsInLastByte = 0; |
| 1873 | |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1874 | uint64_t FieldOffset; |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1875 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1876 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1877 | if (IsUnion) { |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1878 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1879 | FieldOffset = 0; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1880 | } else { |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1881 | // The bitfield is allocated starting at the next offset aligned |
| 1882 | // appropriately for T', with length n bits. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1883 | FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(), |
| 1884 | Context.toBits(TypeAlign)); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1885 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1886 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1887 | |
Ken Dyck | a1a2e8d | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 1888 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1889 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1890 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | // Place this field at the current location. |
| 1894 | FieldOffsets.push_back(FieldOffset); |
| 1895 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1896 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1897 | Context.toBits(TypeAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1898 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1899 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1900 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1901 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1902 | // Remember max struct/class alignment. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1903 | UpdateAlignment(TypeAlign); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1906 | void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1907 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1908 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1909 | uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1910 | uint64_t FieldSize = D->getBitWidthValue(Context); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1911 | |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1912 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1913 | uint64_t TypeSize = FieldInfo.first; |
| 1914 | unsigned FieldAlign = FieldInfo.second; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1915 | |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1916 | // This check is needed for 'long long' in -m32 mode. |
Fariborz Jahanian | 0586df4 | 2011-12-12 21:16:36 +0000 | [diff] [blame] | 1917 | if (IsMsStruct && (TypeSize > FieldAlign) && |
| 1918 | (Context.hasSameType(D->getType(), |
| 1919 | Context.UnsignedLongLongTy) |
| 1920 | || Context.hasSameType(D->getType(), Context.LongLongTy))) |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1921 | FieldAlign = TypeSize; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1922 | |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1923 | if (ZeroLengthBitfield) { |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1924 | std::pair<uint64_t, unsigned> FieldInfo; |
| 1925 | unsigned ZeroLengthBitfieldAlignment; |
| 1926 | if (IsMsStruct) { |
| 1927 | // If a zero-length bitfield is inserted after a bitfield, |
| 1928 | // and the alignment of the zero-length bitfield is |
| 1929 | // greater than the member that follows it, `bar', `bar' |
| 1930 | // will be aligned as the type of the zero-length bitfield. |
| 1931 | if (ZeroLengthBitfield != D) { |
| 1932 | FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType()); |
| 1933 | ZeroLengthBitfieldAlignment = FieldInfo.second; |
| 1934 | // Ignore alignment of subsequent zero-length bitfields. |
| 1935 | if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0)) |
| 1936 | FieldAlign = ZeroLengthBitfieldAlignment; |
| 1937 | if (FieldSize) |
| 1938 | ZeroLengthBitfield = 0; |
| 1939 | } |
| 1940 | } else { |
| 1941 | // The alignment of a zero-length bitfield affects the alignment |
| 1942 | // of the next member. The alignment is the max of the zero |
| 1943 | // length bitfield's alignment and a target specific fixed value. |
| 1944 | unsigned ZeroLengthBitfieldBoundary = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1945 | Context.getTargetInfo().getZeroLengthBitfieldBoundary(); |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1946 | if (ZeroLengthBitfieldBoundary > FieldAlign) |
| 1947 | FieldAlign = ZeroLengthBitfieldBoundary; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1948 | } |
| 1949 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1950 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1951 | if (FieldSize > TypeSize) { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1952 | LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1953 | return; |
| 1954 | } |
| 1955 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1956 | // The align if the field is not packed. This is to check if the attribute |
| 1957 | // was unnecessary (-Wpacked). |
| 1958 | unsigned UnpackedFieldAlign = FieldAlign; |
| 1959 | uint64_t UnpackedFieldOffset = FieldOffset; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1960 | if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1961 | UnpackedFieldAlign = 1; |
| 1962 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1963 | if (FieldPacked || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1964 | (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)) |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1965 | FieldAlign = 1; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1966 | FieldAlign = std::max(FieldAlign, D->getMaxAlignment()); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1967 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1968 | |
| 1969 | // The maximum field alignment overrides the aligned attribute. |
Eli Friedman | a91d38a | 2011-12-02 02:38:48 +0000 | [diff] [blame] | 1970 | if (!MaxFieldAlignment.isZero() && FieldSize != 0) { |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1971 | unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); |
| 1972 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1973 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1974 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1976 | // Check if we need to add padding to give the field the correct alignment. |
| 1977 | if (FieldSize == 0 || |
| 1978 | (MaxFieldAlignment.isZero() && |
| 1979 | (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)) |
| 1980 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1982 | if (FieldSize == 0 || |
| 1983 | (MaxFieldAlignment.isZero() && |
| 1984 | (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)) |
| 1985 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1986 | UnpackedFieldAlign); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1987 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1988 | // Padding members don't affect overall alignment, unless zero length bitfield |
| 1989 | // alignment is enabled. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1990 | if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment()) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1991 | FieldAlign = UnpackedFieldAlign = 1; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1992 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1993 | if (!IsMsStruct) |
| 1994 | ZeroLengthBitfield = 0; |
| 1995 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 1996 | if (ExternalLayout) |
| 1997 | FieldOffset = updateExternalFieldOffset(D, FieldOffset); |
| 1998 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1999 | // Place this field at the current location. |
| 2000 | FieldOffsets.push_back(FieldOffset); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2002 | if (!ExternalLayout) |
| 2003 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
| 2004 | UnpackedFieldAlign, FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2005 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 2006 | // Update DataSize to include the last byte containing (part of) the bitfield. |
| 2007 | if (IsUnion) { |
| 2008 | // FIXME: I think FieldSize should be TypeSize here. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2009 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 2010 | } else { |
| 2011 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2012 | |
Ken Dyck | a1a2e8d | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 2013 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2014 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2015 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 2016 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2017 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 2018 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2019 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2020 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2021 | // Remember max struct/class alignment. |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2022 | UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), |
| 2023 | Context.toCharUnitsFromBits(UnpackedFieldAlign)); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2026 | void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2027 | if (D->isBitField()) { |
| 2028 | LayoutBitField(D); |
| 2029 | return; |
| 2030 | } |
| 2031 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2032 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2033 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 2034 | // Reset the unfilled bits. |
| 2035 | UnfilledBitsInLastByte = 0; |
| 2036 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2037 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2038 | CharUnits FieldOffset = |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2039 | IsUnion ? CharUnits::Zero() : getDataSize(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2040 | CharUnits FieldSize; |
| 2041 | CharUnits FieldAlign; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2042 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2043 | if (D->getType()->isIncompleteArrayType()) { |
| 2044 | // This is a flexible array member; we can't directly |
| 2045 | // query getTypeInfo about these, so we figure it out here. |
| 2046 | // Flexible array members don't have any size, but they |
| 2047 | // have to be aligned appropriately for their element type. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2048 | FieldSize = CharUnits::Zero(); |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 2049 | const ArrayType* ATy = Context.getAsArrayType(D->getType()); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2050 | FieldAlign = Context.getTypeAlignInChars(ATy->getElementType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2051 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 2052 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2053 | FieldSize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2054 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS)); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2055 | FieldAlign = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2056 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2057 | } else { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2058 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 2059 | Context.getTypeInfoInChars(D->getType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2060 | FieldSize = FieldInfo.first; |
| 2061 | FieldAlign = FieldInfo.second; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 2062 | |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 2063 | if (ZeroLengthBitfield) { |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 2064 | CharUnits ZeroLengthBitfieldBoundary = |
| 2065 | Context.toCharUnitsFromBits( |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2066 | Context.getTargetInfo().getZeroLengthBitfieldBoundary()); |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 2067 | if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) { |
| 2068 | // If a zero-length bitfield is inserted after a bitfield, |
| 2069 | // and the alignment of the zero-length bitfield is |
| 2070 | // greater than the member that follows it, `bar', `bar' |
| 2071 | // will be aligned as the type of the zero-length bitfield. |
| 2072 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 2073 | Context.getTypeInfoInChars(ZeroLengthBitfield->getType()); |
| 2074 | CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second; |
| 2075 | if (ZeroLengthBitfieldAlignment > FieldAlign) |
| 2076 | FieldAlign = ZeroLengthBitfieldAlignment; |
Chad Rosier | 64b18ee | 2011-08-04 19:25:14 +0000 | [diff] [blame] | 2077 | } else if (ZeroLengthBitfieldBoundary > FieldAlign) { |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 2078 | // Align 'bar' based on a fixed alignment specified by the target. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2079 | assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
Chad Rosier | a336c6f | 2011-08-04 17:52:43 +0000 | [diff] [blame] | 2080 | "ZeroLengthBitfieldBoundary should only be used in conjunction" |
| 2081 | " with useZeroLengthBitfieldAlignment."); |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 2082 | FieldAlign = ZeroLengthBitfieldBoundary; |
| 2083 | } |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 2084 | ZeroLengthBitfield = 0; |
| 2085 | } |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 2086 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2087 | if (Context.getLangOpts().MSBitfields || IsMsStruct) { |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 2088 | // If MS bitfield layout is required, figure out what type is being |
| 2089 | // laid out and align the field to the width of that type. |
| 2090 | |
| 2091 | // Resolve all typedefs down to their base type and round up the field |
| 2092 | // alignment if necessary. |
| 2093 | QualType T = Context.getBaseElementType(D->getType()); |
| 2094 | if (const BuiltinType *BTy = T->getAs<BuiltinType>()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2095 | CharUnits TypeSize = Context.getTypeSizeInChars(BTy); |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 2096 | if (TypeSize > FieldAlign) |
| 2097 | FieldAlign = TypeSize; |
| 2098 | } |
| 2099 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2102 | // The align if the field is not packed. This is to check if the attribute |
| 2103 | // was unnecessary (-Wpacked). |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2104 | CharUnits UnpackedFieldAlign = FieldAlign; |
| 2105 | CharUnits UnpackedFieldOffset = FieldOffset; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2106 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2107 | if (FieldPacked) |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2108 | FieldAlign = CharUnits::One(); |
| 2109 | CharUnits MaxAlignmentInChars = |
| 2110 | Context.toCharUnitsFromBits(D->getMaxAlignment()); |
| 2111 | FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); |
| 2112 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2113 | |
| 2114 | // The maximum field alignment overrides the aligned attribute. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 2115 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2116 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 2117 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2118 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 2120 | // Round up the current record size to the field's alignment boundary. |
| 2121 | FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign); |
| 2122 | UnpackedFieldOffset = |
| 2123 | UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign); |
| 2124 | |
| 2125 | if (ExternalLayout) { |
| 2126 | FieldOffset = Context.toCharUnitsFromBits( |
| 2127 | updateExternalFieldOffset(D, Context.toBits(FieldOffset))); |
| 2128 | |
| 2129 | if (!IsUnion && EmptySubobjects) { |
| 2130 | // Record the fact that we're placing a field at this offset. |
| 2131 | bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset); |
| 2132 | (void)Allowed; |
| 2133 | assert(Allowed && "Externally-placed field cannot be placed here"); |
| 2134 | } |
| 2135 | } else { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2136 | if (!IsUnion && EmptySubobjects) { |
| 2137 | // Check if we can place the field at this offset. |
| 2138 | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { |
| 2139 | // We couldn't place the field at the offset. Try again at a new offset. |
| 2140 | FieldOffset += FieldAlign; |
| 2141 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2142 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 2143 | } |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2144 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2145 | // Place this field at the current location. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2146 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2148 | if (!ExternalLayout) |
| 2149 | CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, |
| 2150 | Context.toBits(UnpackedFieldOffset), |
| 2151 | Context.toBits(UnpackedFieldAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2152 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2153 | // Reserve space for this field. |
Eli Friedman | 43f1834 | 2012-01-12 23:27:03 +0000 | [diff] [blame] | 2154 | uint64_t FieldSizeInBits = Context.toBits(FieldSize); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2155 | if (IsUnion) |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 2156 | setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2157 | else |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 2158 | setDataSize(FieldOffset + FieldSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Eli Friedman | 2e10837 | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 2160 | // Update the size. |
| 2161 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2163 | // Remember max struct/class alignment. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 2164 | UpdateAlignment(FieldAlign, UnpackedFieldAlign); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2167 | void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 2168 | if (ExternalLayout) { |
| 2169 | setSize(ExternalSize); |
| 2170 | return; |
| 2171 | } |
| 2172 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2173 | // In C++, records cannot be of size 0. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2174 | if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0) { |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 2175 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 2176 | // Compatibility with gcc requires a class (pod or non-pod) |
| 2177 | // which is not empty but of size 0; such as having fields of |
| 2178 | // array of zero-length, remains of Size 0 |
| 2179 | if (RD->isEmpty()) |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2180 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 2181 | } |
| 2182 | else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2183 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 2184 | } |
Eli Friedman | 83a1258 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 2185 | |
| 2186 | // MSVC doesn't round up to the alignment of the record with virtual bases. |
| 2187 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 2188 | if (isMicrosoftCXXABI() && RD->getNumVBases()) |
| 2189 | return; |
| 2190 | } |
| 2191 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2192 | // Finally, round the size of the record up to the alignment of the |
| 2193 | // record itself. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2194 | uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte; |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2195 | uint64_t UnpackedSizeInBits = |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2196 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 2197 | Context.toBits(UnpackedAlignment)); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2198 | CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2199 | setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment))); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2200 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2201 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2202 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 2203 | // Warn if padding was introduced to the struct/class/union. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2204 | if (getSizeInBits() > UnpaddedSize) { |
| 2205 | unsigned PadSize = getSizeInBits() - UnpaddedSize; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2206 | bool InBits = true; |
| 2207 | if (PadSize % CharBitNum == 0) { |
| 2208 | PadSize = PadSize / CharBitNum; |
| 2209 | InBits = false; |
| 2210 | } |
| 2211 | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
| 2212 | << Context.getTypeDeclType(RD) |
| 2213 | << PadSize |
| 2214 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 2215 | } |
| 2216 | |
| 2217 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 2218 | // bother since there won't be alignment issues. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2219 | if (Packed && UnpackedAlignment > CharUnits::One() && |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2220 | getSize() == UnpackedSize) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2221 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 2222 | << Context.getTypeDeclType(RD); |
| 2223 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2226 | void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment, |
| 2227 | CharUnits UnpackedNewAlignment) { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2228 | // The alignment is not modified when using 'mac68k' alignment or when |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 2229 | // we have an externally-supplied layout that also provides overall alignment. |
| 2230 | if (IsMac68kAlign || (ExternalLayout && !InferAlignment)) |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 2231 | return; |
| 2232 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2233 | if (NewAlignment > Alignment) { |
| 2234 | assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() && |
| 2235 | "Alignment not a power of 2")); |
| 2236 | Alignment = NewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2239 | if (UnpackedNewAlignment > UnpackedAlignment) { |
| 2240 | assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() && |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2241 | "Alignment not a power of 2")); |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2242 | UnpackedAlignment = UnpackedNewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
Douglas Gregor | 44ba789 | 2012-01-28 00:53:29 +0000 | [diff] [blame] | 2246 | uint64_t |
| 2247 | RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field, |
| 2248 | uint64_t ComputedOffset) { |
| 2249 | assert(ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() && |
| 2250 | "Field does not have an external offset"); |
| 2251 | |
| 2252 | uint64_t ExternalFieldOffset = ExternalFieldOffsets[Field]; |
| 2253 | |
| 2254 | if (InferAlignment && ExternalFieldOffset < ComputedOffset) { |
| 2255 | // The externally-supplied field offset is before the field offset we |
| 2256 | // computed. Assume that the structure is packed. |
| 2257 | Alignment = CharUnits::fromQuantity(1); |
| 2258 | InferAlignment = false; |
| 2259 | } |
| 2260 | |
| 2261 | // Use the externally-supplied field offset. |
| 2262 | return ExternalFieldOffset; |
| 2263 | } |
| 2264 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2265 | void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset, |
| 2266 | uint64_t UnpaddedOffset, |
| 2267 | uint64_t UnpackedOffset, |
| 2268 | unsigned UnpackedAlign, |
| 2269 | bool isPacked, |
| 2270 | const FieldDecl *D) { |
| 2271 | // We let objc ivars without warning, objc interfaces generally are not used |
| 2272 | // for padding tricks. |
| 2273 | if (isa<ObjCIvarDecl>(D)) |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2274 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | |
Ted Kremenek | fed48af | 2011-09-06 19:40:45 +0000 | [diff] [blame] | 2276 | // Don't warn about structs created without a SourceLocation. This can |
| 2277 | // be done by clients of the AST, such as codegen. |
| 2278 | if (D->getLocation().isInvalid()) |
| 2279 | return; |
| 2280 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2281 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2283 | // Warn if padding was introduced to the struct/class. |
| 2284 | if (!IsUnion && Offset > UnpaddedOffset) { |
| 2285 | unsigned PadSize = Offset - UnpaddedOffset; |
| 2286 | bool InBits = true; |
| 2287 | if (PadSize % CharBitNum == 0) { |
| 2288 | PadSize = PadSize / CharBitNum; |
| 2289 | InBits = false; |
| 2290 | } |
| 2291 | if (D->getIdentifier()) |
| 2292 | Diag(D->getLocation(), diag::warn_padded_struct_field) |
| 2293 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 2294 | << Context.getTypeDeclType(D->getParent()) |
| 2295 | << PadSize |
| 2296 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not |
| 2297 | << D->getIdentifier(); |
| 2298 | else |
| 2299 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
| 2300 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 2301 | << Context.getTypeDeclType(D->getParent()) |
| 2302 | << PadSize |
| 2303 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 2304 | } |
| 2305 | |
| 2306 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 2307 | // bother since there won't be alignment issues. |
| 2308 | if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset) |
| 2309 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 2310 | << D->getIdentifier(); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2311 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 2313 | const CXXMethodDecl * |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 2314 | RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2315 | // 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] | 2316 | if (!RD->isPolymorphic()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2317 | return 0; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 2318 | |
Eli Friedman | 300f55d | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 2319 | // 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] | 2320 | // at least, there's no point to assigning a key function to such a class; |
| 2321 | // this doesn't affect the ABI.) |
Eli Friedman | 300f55d | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 2322 | if (RD->getLinkage() != ExternalLinkage) |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 2323 | return 0; |
| 2324 | |
Argyrios Kyrtzidis | 8c64bbe | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 2325 | // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6. |
| 2326 | // Same behavior as GCC. |
| 2327 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 2328 | if (TSK == TSK_ImplicitInstantiation || |
| 2329 | TSK == TSK_ExplicitInstantiationDefinition) |
| 2330 | return 0; |
| 2331 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2332 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 2333 | E = RD->method_end(); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 2334 | const CXXMethodDecl *MD = *I; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2335 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2336 | if (!MD->isVirtual()) |
| 2337 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2338 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2339 | if (MD->isPure()) |
| 2340 | continue; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 2341 | |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2342 | // Ignore implicit member functions, they are always marked as inline, but |
| 2343 | // they don't have a body until they're defined. |
| 2344 | if (MD->isImplicit()) |
| 2345 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | a318efd | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 2347 | if (MD->isInlineSpecified()) |
| 2348 | continue; |
Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 2349 | |
| 2350 | if (MD->hasInlineBody()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2351 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2352 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2353 | // We found it. |
| 2354 | return MD; |
| 2355 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2356 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2357 | return 0; |
| 2358 | } |
| 2359 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2360 | DiagnosticBuilder |
| 2361 | RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 2362 | return Context.getDiagnostics().Report(Loc, DiagID); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2365 | /// getASTRecordLayout - Get or compute information about the layout of the |
| 2366 | /// specified record (struct/union/class), which indicates its size and field |
| 2367 | /// position information. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2368 | const ASTRecordLayout & |
| 2369 | ASTContext::getASTRecordLayout(const RecordDecl *D) const { |
John McCall | 0710e55 | 2011-10-07 02:39:22 +0000 | [diff] [blame] | 2370 | // These asserts test different things. A record has a definition |
| 2371 | // as soon as we begin to parse the definition. That definition is |
| 2372 | // not a complete definition (which is what isDefinition() tests) |
| 2373 | // until we *finish* parsing the definition. |
Sean Callanan | 56c1989 | 2012-02-08 00:04:52 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
| 2376 | getExternalSource()->CompleteType(const_cast<RecordDecl*>(D)); |
| 2377 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2378 | D = D->getDefinition(); |
| 2379 | assert(D && "Cannot get layout of forward declarations!"); |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2380 | assert(D->isCompleteDefinition() && "Cannot layout type before complete!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2381 | |
| 2382 | // Look up this layout, if already laid out, return what we have. |
| 2383 | // Note that we can't save a reference to the entry because this function |
| 2384 | // is recursive. |
| 2385 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
| 2386 | if (Entry) return *Entry; |
| 2387 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2388 | const ASTRecordLayout *NewEntry; |
| 2389 | |
| 2390 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2391 | EmptySubobjectMap EmptySubobjects(*this, RD); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2392 | RecordLayoutBuilder Builder(*this, &EmptySubobjects); |
| 2393 | Builder.Layout(RD); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 2394 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2395 | // MSVC gives the vb-table pointer an alignment equal to that of |
| 2396 | // the non-virtual part of the structure. That's an inherently |
| 2397 | // multi-pass operation. If our first pass doesn't give us |
| 2398 | // adequate alignment, try again with the specified minimum |
| 2399 | // alignment. This is *much* more maintainable than computing the |
| 2400 | // alignment in advance in a separately-coded pass; it's also |
| 2401 | // significantly more efficient in the common case where the |
| 2402 | // vb-table doesn't need extra padding. |
| 2403 | if (Builder.VBPtrOffset != CharUnits::fromQuantity(-1) && |
| 2404 | (Builder.VBPtrOffset % Builder.NonVirtualAlignment) != 0) { |
| 2405 | Builder.resetWithTargetAlignment(Builder.NonVirtualAlignment); |
| 2406 | Builder.Layout(RD); |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2409 | // FIXME: This is not always correct. See the part about bitfields at |
| 2410 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 2411 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2412 | // This does not affect the calculations of MSVC layouts |
| 2413 | bool IsPODForThePurposeOfLayout = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2414 | (!Builder.isMicrosoftCXXABI() && cast<CXXRecordDecl>(D)->isPOD()); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2415 | |
| 2416 | // FIXME: This should be done in FinalizeLayout. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2417 | CharUnits DataSize = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2418 | IsPODForThePurposeOfLayout ? Builder.getSize() : Builder.getDataSize(); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2419 | CharUnits NonVirtualSize = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2420 | IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2421 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2422 | NewEntry = |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2423 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
| 2424 | Builder.Alignment, |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 2425 | Builder.HasOwnVFPtr, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2426 | Builder.VBPtrOffset, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2427 | DataSize, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2428 | Builder.FieldOffsets.data(), |
| 2429 | Builder.FieldOffsets.size(), |
Ken Dyck | af1c83f | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 2430 | NonVirtualSize, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2431 | Builder.NonVirtualAlignment, |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2432 | EmptySubobjects.SizeOfLargestEmptySubobject, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2433 | Builder.PrimaryBase, |
| 2434 | Builder.PrimaryBaseIsVirtual, |
| 2435 | Builder.Bases, Builder.VBases); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2436 | } else { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2437 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2438 | Builder.Layout(D); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2439 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2440 | NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2441 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2442 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2443 | Builder.getSize(), |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2444 | Builder.FieldOffsets.data(), |
| 2445 | Builder.FieldOffsets.size()); |
| 2446 | } |
| 2447 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2448 | ASTRecordLayouts[D] = NewEntry; |
| 2449 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2450 | if (getLangOpts().DumpRecordLayouts) { |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2451 | llvm::errs() << "\n*** Dumping AST Record Layout\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2452 | DumpRecordLayout(D, llvm::errs(), getLangOpts().DumpRecordLayoutsSimple); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | return *NewEntry; |
| 2456 | } |
| 2457 | |
| 2458 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
| 2459 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
| 2460 | assert(RD && "Cannot get key function for forward declarations!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2461 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2462 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2463 | if (!Entry) |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 2464 | Entry = RecordLayoutBuilder::ComputeKeyFunction(RD); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2465 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2466 | return Entry; |
| 2467 | } |
| 2468 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2469 | static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) { |
| 2470 | const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent()); |
| 2471 | return Layout.getFieldOffset(FD->getFieldIndex()); |
| 2472 | } |
| 2473 | |
| 2474 | uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const { |
| 2475 | uint64_t OffsetInBits; |
| 2476 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 2477 | OffsetInBits = ::getFieldOffset(*this, FD); |
| 2478 | } else { |
| 2479 | const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD); |
| 2480 | |
| 2481 | OffsetInBits = 0; |
| 2482 | for (IndirectFieldDecl::chain_iterator CI = IFD->chain_begin(), |
| 2483 | CE = IFD->chain_end(); |
| 2484 | CI != CE; ++CI) |
| 2485 | OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(*CI)); |
| 2486 | } |
| 2487 | |
| 2488 | return OffsetInBits; |
| 2489 | } |
| 2490 | |
Eric Christopher | 8a39a01 | 2011-10-05 06:00:51 +0000 | [diff] [blame] | 2491 | /// getObjCLayout - Get or compute information about the layout of the |
| 2492 | /// given interface. |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2493 | /// |
| 2494 | /// \param Impl - If given, also include the layout of the interface's |
| 2495 | /// implementation. This may differ by including synthesized ivars. |
| 2496 | const ASTRecordLayout & |
| 2497 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2498 | const ObjCImplementationDecl *Impl) const { |
Douglas Gregor | 64d9257 | 2011-12-20 15:50:13 +0000 | [diff] [blame] | 2499 | // Retrieve the definition |
Sean Callanan | d9a909c | 2012-03-15 16:33:08 +0000 | [diff] [blame] | 2500 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
| 2501 | getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D)); |
Douglas Gregor | 64d9257 | 2011-12-20 15:50:13 +0000 | [diff] [blame] | 2502 | D = D->getDefinition(); |
| 2503 | assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2504 | |
| 2505 | // Look up this layout, if already laid out, return what we have. |
| 2506 | ObjCContainerDecl *Key = |
| 2507 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 2508 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 2509 | return *Entry; |
| 2510 | |
| 2511 | // Add in synthesized ivar count if laying out an implementation. |
| 2512 | if (Impl) { |
| 2513 | unsigned SynthCount = CountNonClassIvars(D); |
| 2514 | // If there aren't any sythesized ivars then reuse the interface |
| 2515 | // entry. Note we can't cache this because we simply free all |
| 2516 | // entries later; however we shouldn't look up implementations |
| 2517 | // frequently. |
| 2518 | if (SynthCount == 0) |
| 2519 | return getObjCLayout(D, 0); |
| 2520 | } |
| 2521 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2522 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2523 | Builder.Layout(D); |
| 2524 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2525 | const ASTRecordLayout *NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2526 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2527 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2528 | Builder.getDataSize(), |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2529 | Builder.FieldOffsets.data(), |
| 2530 | Builder.FieldOffsets.size()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2531 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2532 | ObjCLayouts[Key] = NewEntry; |
| 2533 | |
| 2534 | return *NewEntry; |
| 2535 | } |
| 2536 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2537 | static void PrintOffset(raw_ostream &OS, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2538 | CharUnits Offset, unsigned IndentLevel) { |
Benjamin Kramer | 96ad717 | 2011-11-05 09:02:52 +0000 | [diff] [blame] | 2539 | OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity()); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2540 | OS.indent(IndentLevel * 2); |
| 2541 | } |
| 2542 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2543 | static void DumpCXXRecordLayout(raw_ostream &OS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2544 | const CXXRecordDecl *RD, const ASTContext &C, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2545 | CharUnits Offset, |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2546 | unsigned IndentLevel, |
| 2547 | const char* Description, |
| 2548 | bool IncludeVirtualBases) { |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2549 | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2550 | |
| 2551 | PrintOffset(OS, Offset, IndentLevel); |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2552 | OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2553 | if (Description) |
| 2554 | OS << ' ' << Description; |
| 2555 | if (RD->isEmpty()) |
| 2556 | OS << " (empty)"; |
| 2557 | OS << '\n'; |
| 2558 | |
| 2559 | IndentLevel++; |
| 2560 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2561 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 2562 | bool HasVfptr = Layout.hasOwnVFPtr(); |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2563 | bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2564 | |
| 2565 | // Vtable pointer. |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 2566 | if (RD->isDynamicClass() && !PrimaryBase && |
| 2567 | C.getTargetInfo().getCXXABI() != CXXABI_Microsoft) { |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2568 | PrintOffset(OS, Offset, IndentLevel); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2569 | OS << '(' << *RD << " vtable pointer)\n"; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2570 | } |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2571 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2572 | // Dump (non-virtual) bases |
| 2573 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 2574 | E = RD->bases_end(); I != E; ++I) { |
| 2575 | assert(!I->getType()->isDependentType() && |
| 2576 | "Cannot layout class with dependent bases."); |
| 2577 | if (I->isVirtual()) |
| 2578 | continue; |
| 2579 | |
| 2580 | const CXXRecordDecl *Base = |
| 2581 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2582 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2583 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2584 | |
| 2585 | DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
| 2586 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 2587 | /*IncludeVirtualBases=*/false); |
| 2588 | } |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 2589 | |
| 2590 | // vfptr and vbptr (for Microsoft C++ ABI) |
| 2591 | if (HasVfptr) { |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 2592 | PrintOffset(OS, Offset, IndentLevel); |
Eli Friedman | 43114f9 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 2593 | OS << '(' << *RD << " vftable pointer)\n"; |
| 2594 | } |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2595 | if (HasVbptr) { |
| 2596 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2597 | OS << '(' << *RD << " vbtable pointer)\n"; |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2598 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2599 | |
| 2600 | // Dump fields. |
| 2601 | uint64_t FieldNo = 0; |
| 2602 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 2603 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame^] | 2604 | const FieldDecl &Field = **I; |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2605 | CharUnits FieldOffset = Offset + |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 2606 | C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo)); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2607 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2608 | if (const RecordType *RT = Field.getType()->getAs<RecordType>()) { |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2609 | if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2610 | DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel, |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2611 | Field.getName().data(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2612 | /*IncludeVirtualBases=*/true); |
| 2613 | continue; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | PrintOffset(OS, FieldOffset, IndentLevel); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2618 | OS << Field.getType().getAsString() << ' ' << Field << '\n'; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | if (!IncludeVirtualBases) |
| 2622 | return; |
| 2623 | |
| 2624 | // Dump virtual bases. |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 2625 | const ASTRecordLayout::VBaseOffsetsMapTy &vtordisps = |
| 2626 | Layout.getVBaseOffsetsMap(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2627 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 2628 | E = RD->vbases_end(); I != E; ++I) { |
| 2629 | assert(I->isVirtual() && "Found non-virtual class!"); |
| 2630 | const CXXRecordDecl *VBase = |
| 2631 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2632 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2633 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 2634 | |
| 2635 | if (vtordisps.find(VBase)->second.hasVtorDisp()) { |
| 2636 | PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel); |
| 2637 | OS << "(vtordisp for vbase " << *VBase << ")\n"; |
| 2638 | } |
| 2639 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2640 | DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
| 2641 | VBase == PrimaryBase ? |
| 2642 | "(primary virtual base)" : "(virtual base)", |
| 2643 | /*IncludeVirtualBases=*/false); |
| 2644 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2645 | |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2646 | OS << " sizeof=" << Layout.getSize().getQuantity(); |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 2647 | OS << ", dsize=" << Layout.getDataSize().getQuantity(); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2648 | OS << ", align=" << Layout.getAlignment().getQuantity() << '\n'; |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 2649 | OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity(); |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 2650 | OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n'; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2651 | OS << '\n'; |
| 2652 | } |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2653 | |
| 2654 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2655 | raw_ostream &OS, |
| 2656 | bool Simple) const { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2657 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 2658 | |
| 2659 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2660 | if (!Simple) |
| 2661 | return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0, |
| 2662 | /*IncludeVirtualBases=*/true); |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2663 | |
| 2664 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2665 | if (!Simple) { |
| 2666 | OS << "Record: "; |
| 2667 | RD->dump(); |
| 2668 | } |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2669 | OS << "\nLayout: "; |
| 2670 | OS << "<ASTRecordLayout\n"; |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2671 | OS << " Size:" << toBits(Info.getSize()) << "\n"; |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 2672 | OS << " DataSize:" << toBits(Info.getDataSize()) << "\n"; |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2673 | OS << " Alignment:" << toBits(Info.getAlignment()) << "\n"; |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2674 | OS << " FieldOffsets: ["; |
| 2675 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
| 2676 | if (i) OS << ", "; |
| 2677 | OS << Info.getFieldOffset(i); |
| 2678 | } |
| 2679 | OS << "]>\n"; |
| 2680 | } |