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