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 | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 595 | /// VBPtrOffset - Virtual base table offset. Only for MS layout. |
| 596 | CharUnits VBPtrOffset; |
| 597 | |
Anders Carlsson | 22f5720 | 2010-10-31 21:01:46 +0000 | [diff] [blame] | 598 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 599 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 600 | /// Bases - base classes and their offsets in the record. |
| 601 | BaseOffsetsMapTy Bases; |
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 | // VBases - virtual base classes and their offsets in the record. |
| 604 | BaseOffsetsMapTy VBases; |
| 605 | |
| 606 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 607 | /// primary base classes for some other direct or indirect base class. |
Anders Carlsson | 5adde29 | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 608 | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 609 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 610 | /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in |
| 611 | /// inheritance graph order. Used for determining the primary base class. |
| 612 | const CXXRecordDecl *FirstNearlyEmptyVBase; |
| 613 | |
| 614 | /// VisitedVirtualBases - A set of all the visited virtual bases, used to |
| 615 | /// avoid visiting virtual bases more than once. |
| 616 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 617 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 618 | RecordLayoutBuilder(const ASTContext &Context, EmptySubobjectMap |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 619 | *EmptySubobjects, CharUnits Alignment) |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 620 | : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 621 | Alignment(Alignment), UnpackedAlignment(Alignment), |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 622 | Packed(false), IsUnion(false), |
| 623 | IsMac68kAlign(false), IsMsStruct(false), |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 624 | UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()), |
| 625 | DataSize(0), NonVirtualSize(CharUnits::Zero()), |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 626 | NonVirtualAlignment(CharUnits::One()), |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 627 | ZeroLengthBitfield(0), PrimaryBase(0), |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 628 | PrimaryBaseIsVirtual(false), VBPtrOffset(CharUnits::fromQuantity(-1)), |
| 629 | FirstNearlyEmptyVBase(0) { } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 630 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 631 | void Layout(const RecordDecl *D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 632 | void Layout(const CXXRecordDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 633 | void Layout(const ObjCInterfaceDecl *D); |
| 634 | |
| 635 | void LayoutFields(const RecordDecl *D); |
| 636 | void LayoutField(const FieldDecl *D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 637 | void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize, |
| 638 | bool FieldPacked, const FieldDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 639 | void LayoutBitField(const FieldDecl *D); |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 640 | void MSLayoutVirtualBases(const CXXRecordDecl *RD); |
| 641 | void MSLayout(const CXXRecordDecl *RD); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 642 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 643 | /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects. |
| 644 | llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator; |
| 645 | |
| 646 | typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *> |
| 647 | BaseSubobjectInfoMapTy; |
| 648 | |
| 649 | /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases |
| 650 | /// of the class we're laying out to their base subobject info. |
| 651 | BaseSubobjectInfoMapTy VirtualBaseInfo; |
| 652 | |
| 653 | /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the |
| 654 | /// class we're laying out to their base subobject info. |
| 655 | BaseSubobjectInfoMapTy NonVirtualBaseInfo; |
| 656 | |
| 657 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for the |
| 658 | /// bases of the given class. |
| 659 | void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD); |
| 660 | |
| 661 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for a |
| 662 | /// single class and all of its base classes. |
| 663 | BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 664 | bool IsVirtual, |
| 665 | BaseSubobjectInfo *Derived); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 666 | |
| 667 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
| 668 | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
| 669 | |
| 670 | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 671 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 672 | CharUnits GetVirtualPointersSize(const CXXRecordDecl *RD) const; |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 673 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 674 | /// LayoutNonVirtualBases - Determines the primary base class (if any) and |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 675 | /// lays it out. Will then proceed to lay out all non-virtual base clasess. |
| 676 | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
| 677 | |
| 678 | /// LayoutNonVirtualBase - Lays out a single non-virtual base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 679 | void LayoutNonVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 680 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 681 | void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
| 682 | CharUnits Offset); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 683 | |
| 684 | /// LayoutVirtualBases - Lays out all the virtual bases. |
| 685 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 686 | const CXXRecordDecl *MostDerivedClass); |
| 687 | |
| 688 | /// LayoutVirtualBase - Lays out a single virtual base. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 689 | void LayoutVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 690 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 691 | /// 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] | 692 | /// placed, in chars. |
| 693 | CharUnits LayoutBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 694 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 695 | /// InitializeLayout - Initialize record layout for the given record decl. |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 696 | void InitializeLayout(const Decl *D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 697 | |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 698 | /// FinishLayout - Finalize record layout. Adjust record size based on the |
| 699 | /// alignment. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 700 | void FinishLayout(const NamedDecl *D); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 701 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 702 | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment); |
| 703 | void UpdateAlignment(CharUnits NewAlignment) { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 704 | UpdateAlignment(NewAlignment, NewAlignment); |
| 705 | } |
| 706 | |
| 707 | void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset, |
| 708 | uint64_t UnpackedOffset, unsigned UnpackedAlign, |
| 709 | bool isPacked, const FieldDecl *D); |
| 710 | |
| 711 | DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 712 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 713 | CharUnits getSize() const { |
Ken Dyck | 3c215f2 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 714 | assert(Size % Context.getCharWidth() == 0); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 715 | return Context.toCharUnitsFromBits(Size); |
| 716 | } |
| 717 | uint64_t getSizeInBits() const { return Size; } |
| 718 | |
| 719 | void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); } |
| 720 | void setSize(uint64_t NewSize) { Size = NewSize; } |
| 721 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 722 | CharUnits getAligment() const { return Alignment; } |
| 723 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 724 | CharUnits getDataSize() const { |
Ken Dyck | 3c215f2 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 725 | assert(DataSize % Context.getCharWidth() == 0); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 726 | return Context.toCharUnitsFromBits(DataSize); |
| 727 | } |
| 728 | uint64_t getDataSizeInBits() const { return DataSize; } |
| 729 | |
| 730 | void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); } |
| 731 | void setDataSize(uint64_t NewSize) { DataSize = NewSize; } |
| 732 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 733 | bool HasVBPtr(const CXXRecordDecl *RD) const; |
| 734 | bool HasNewVirtualFunction(const CXXRecordDecl *RD) const; |
| 735 | |
| 736 | /// Add vbptr or vfptr to layout. |
| 737 | void AddVPointer(); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 738 | |
Argyrios Kyrtzidis | 499e6e4 | 2010-08-15 10:17:39 +0000 | [diff] [blame] | 739 | RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
| 740 | void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 741 | public: |
| 742 | static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD); |
Argyrios Kyrtzidis | 3c6cd17 | 2010-08-25 00:32:14 +0000 | [diff] [blame] | 743 | |
| 744 | virtual ~RecordLayoutBuilder() { } |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 745 | |
| 746 | CharUnits GetVBPtrOffset() const { return VBPtrOffset; } |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 747 | }; |
Benjamin Kramer | c7656cd | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 748 | } // end anonymous namespace |
Anders Carlsson | 35a36eb | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 749 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 750 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 751 | RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 752 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 753 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 754 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 755 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 756 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | const CXXRecordDecl *Base = |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 758 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 759 | |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 760 | // Check if this is a nearly empty virtual base. |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 761 | if (I->isVirtual() && Context.isNearlyEmpty(Base)) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 762 | // If it's not an indirect primary base, then we've found our primary |
| 763 | // base. |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 764 | if (!IndirectPrimaryBases.count(Base)) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 765 | PrimaryBase = Base; |
| 766 | PrimaryBaseIsVirtual = true; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 767 | return; |
| 768 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 769 | |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 770 | // Is this the first nearly empty virtual base? |
| 771 | if (!FirstNearlyEmptyVBase) |
| 772 | FirstNearlyEmptyVBase = Base; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 773 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 774 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 775 | SelectPrimaryVBase(Base); |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 776 | if (PrimaryBase) |
Zhongxing Xu | ec345b7 | 2010-02-15 04:28:35 +0000 | [diff] [blame] | 777 | return; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Ken Dyck | aab635e | 2011-03-01 01:22:45 +0000 | [diff] [blame] | 781 | CharUnits |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 782 | RecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 783 | return Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 786 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 787 | void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 788 | // If the class isn't dynamic, it won't have a primary base. |
| 789 | if (!RD->isDynamicClass()) |
| 790 | return; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 791 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 792 | // Compute all the primary virtual bases for all of our direct and |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 793 | // indirect bases, and record all their primary virtual base classes. |
Anders Carlsson | 5adde29 | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 794 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 795 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 796 | // If the record has a dynamic base class, attempt to choose a primary base |
| 797 | // 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] | 798 | // base class, if one exists. |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 799 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 800 | e = RD->bases_end(); i != e; ++i) { |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 801 | // Ignore virtual bases. |
| 802 | if (i->isVirtual()) |
| 803 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 804 | |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 805 | const CXXRecordDecl *Base = |
| 806 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 807 | |
| 808 | if (Base->isDynamicClass()) { |
| 809 | // We found it. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 810 | PrimaryBase = Base; |
| 811 | PrimaryBaseIsVirtual = false; |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 812 | return; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | // Otherwise, it is the first nearly empty virtual base that is not an |
Mike Stump | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 817 | // indirect primary virtual base class, if one exists. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 818 | if (RD->getNumVBases() != 0) { |
| 819 | SelectPrimaryVBase(RD); |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 820 | if (PrimaryBase) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 821 | return; |
| 822 | } |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 823 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 824 | // Otherwise, it is the first nearly empty virtual base that is not an |
| 825 | // indirect primary virtual base class, if one exists. |
| 826 | if (FirstNearlyEmptyVBase) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 827 | PrimaryBase = FirstNearlyEmptyVBase; |
| 828 | PrimaryBaseIsVirtual = true; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 829 | return; |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 830 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 831 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 832 | // Otherwise there is no primary base class. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 833 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 834 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 835 | // Allocate the virtual table pointer at offset zero. |
| 836 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 837 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 838 | // Update the size. |
Ken Dyck | aab635e | 2011-03-01 01:22:45 +0000 | [diff] [blame] | 839 | setSize(getSize() + GetVirtualPointersSize(RD)); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 840 | setDataSize(getSize()); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 841 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 842 | CharUnits UnpackedBaseAlign = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 843 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 844 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
Argyrios Kyrtzidis | d62c9be | 2010-12-09 02:47:58 +0000 | [diff] [blame] | 845 | |
| 846 | // The maximum field alignment overrides base align. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 847 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 848 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 849 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | d62c9be | 2010-12-09 02:47:58 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 852 | // Update the alignment. |
Argyrios Kyrtzidis | d62c9be | 2010-12-09 02:47:58 +0000 | [diff] [blame] | 853 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 856 | BaseSubobjectInfo * |
| 857 | RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 858 | bool IsVirtual, |
| 859 | BaseSubobjectInfo *Derived) { |
| 860 | BaseSubobjectInfo *Info; |
| 861 | |
| 862 | if (IsVirtual) { |
| 863 | // Check if we already have info about this virtual base. |
| 864 | BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD]; |
| 865 | if (InfoSlot) { |
| 866 | assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); |
| 867 | return InfoSlot; |
| 868 | } |
| 869 | |
| 870 | // We don't, create it. |
| 871 | InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 872 | Info = InfoSlot; |
| 873 | } else { |
| 874 | Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 875 | } |
| 876 | |
| 877 | Info->Class = RD; |
| 878 | Info->IsVirtual = IsVirtual; |
| 879 | Info->Derived = 0; |
| 880 | Info->PrimaryVirtualBaseInfo = 0; |
| 881 | |
| 882 | const CXXRecordDecl *PrimaryVirtualBase = 0; |
| 883 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0; |
| 884 | |
| 885 | // Check if this base has a primary virtual base. |
| 886 | if (RD->getNumVBases()) { |
| 887 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 888 | if (Layout.isPrimaryBaseVirtual()) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 889 | // This base does have a primary virtual base. |
| 890 | PrimaryVirtualBase = Layout.getPrimaryBase(); |
| 891 | assert(PrimaryVirtualBase && "Didn't have a primary virtual base!"); |
| 892 | |
| 893 | // Now check if we have base subobject info about this primary base. |
| 894 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 895 | |
| 896 | if (PrimaryVirtualBaseInfo) { |
| 897 | if (PrimaryVirtualBaseInfo->Derived) { |
| 898 | // We did have info about this primary base, and it turns out that it |
| 899 | // has already been claimed as a primary virtual base for another |
| 900 | // base. |
| 901 | PrimaryVirtualBase = 0; |
| 902 | } else { |
| 903 | // We can claim this base as our primary base. |
| 904 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 905 | PrimaryVirtualBaseInfo->Derived = Info; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // Now go through all direct bases. |
| 912 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 913 | E = RD->bases_end(); I != E; ++I) { |
| 914 | bool IsVirtual = I->isVirtual(); |
| 915 | |
| 916 | const CXXRecordDecl *BaseDecl = |
| 917 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 918 | |
| 919 | Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info)); |
| 920 | } |
| 921 | |
| 922 | if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) { |
| 923 | // Traversing the bases must have created the base info for our primary |
| 924 | // virtual base. |
| 925 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 926 | assert(PrimaryVirtualBaseInfo && |
| 927 | "Did not create a primary virtual base!"); |
| 928 | |
| 929 | // Claim the primary virtual base as our primary virtual base. |
| 930 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 931 | PrimaryVirtualBaseInfo->Derived = Info; |
| 932 | } |
| 933 | |
| 934 | return Info; |
| 935 | } |
| 936 | |
| 937 | void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) { |
| 938 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 939 | E = RD->bases_end(); I != E; ++I) { |
| 940 | bool IsVirtual = I->isVirtual(); |
| 941 | |
| 942 | const CXXRecordDecl *BaseDecl = |
| 943 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 944 | |
| 945 | // Compute the base subobject info for this base. |
| 946 | BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0); |
| 947 | |
| 948 | if (IsVirtual) { |
| 949 | // ComputeBaseInfo has already added this base for us. |
| 950 | assert(VirtualBaseInfo.count(BaseDecl) && |
| 951 | "Did not add virtual base!"); |
| 952 | } else { |
| 953 | // Add the base info to the map of non-virtual bases. |
| 954 | assert(!NonVirtualBaseInfo.count(BaseDecl) && |
| 955 | "Non-virtual base already exists!"); |
| 956 | NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 961 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 962 | RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 963 | // Then, determine the primary base class. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 964 | DeterminePrimaryBase(RD); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 965 | |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 966 | // Compute base subobject info. |
| 967 | ComputeBaseSubobjectInfo(RD); |
| 968 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 969 | // If we have a primary base class, lay it out. |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 970 | if (PrimaryBase) { |
| 971 | if (PrimaryBaseIsVirtual) { |
Anders Carlsson | e3c24c7 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 972 | // If the primary virtual base was a primary virtual base of some other |
| 973 | // base class we'll have to steal it. |
| 974 | BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase); |
| 975 | PrimaryBaseInfo->Derived = 0; |
| 976 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 977 | // 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] | 978 | IndirectPrimaryBases.insert(PrimaryBase); |
Anders Carlsson | fe90096 | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 979 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 980 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 981 | "vbase already visited!"); |
| 982 | VisitedVirtualBases.insert(PrimaryBase); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 983 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 984 | LayoutVirtualBase(PrimaryBaseInfo); |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 985 | } else { |
| 986 | BaseSubobjectInfo *PrimaryBaseInfo = |
| 987 | NonVirtualBaseInfo.lookup(PrimaryBase); |
| 988 | assert(PrimaryBaseInfo && |
| 989 | "Did not find base info for non-virtual primary base!"); |
| 990 | |
| 991 | LayoutNonVirtualBase(PrimaryBaseInfo); |
| 992 | } |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 993 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 994 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 995 | // Now lay out the non-virtual bases. |
| 996 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 997 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 998 | |
| 999 | // Ignore virtual bases. |
| 1000 | if (I->isVirtual()) |
| 1001 | continue; |
| 1002 | |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1003 | const CXXRecordDecl *BaseDecl = |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1004 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1005 | |
| 1006 | // Skip the primary base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1007 | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual) |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1008 | continue; |
| 1009 | |
| 1010 | // Lay out the base. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1011 | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
| 1012 | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
| 1013 | |
| 1014 | LayoutNonVirtualBase(BaseInfo); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1018 | void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1019 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1020 | CharUnits Offset = LayoutBase(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1021 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1022 | // Add its base class offset. |
Anders Carlsson | bb0e678 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1023 | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1024 | Bases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1025 | |
| 1026 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1027 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1028 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1029 | void |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1030 | RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1031 | CharUnits Offset) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1032 | // This base isn't interesting, it has no virtual bases. |
| 1033 | if (!Info->Class->getNumVBases()) |
| 1034 | return; |
| 1035 | |
| 1036 | // First, check if we have a virtual primary base to add offsets for. |
| 1037 | if (Info->PrimaryVirtualBaseInfo) { |
| 1038 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
| 1039 | "Primary virtual base is not virtual!"); |
| 1040 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
| 1041 | // Add the offset. |
| 1042 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
| 1043 | "primary vbase offset already exists!"); |
| 1044 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1045 | Offset)); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1046 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1047 | // Traverse the primary virtual base. |
| 1048 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
| 1049 | } |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1052 | // Now go through all direct non-virtual bases. |
| 1053 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
| 1054 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
| 1055 | const BaseSubobjectInfo *Base = Info->Bases[I]; |
| 1056 | if (Base->IsVirtual) |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1057 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1058 | |
Anders Carlsson | 0a14ee9 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 1059 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1060 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1064 | void RecordLayoutBuilder::AddVPointer() { |
| 1065 | CharUnits PtrWidth = |
| 1066 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
| 1067 | setSize(getSize() + PtrWidth); |
| 1068 | setDataSize(getSize()); |
| 1069 | |
| 1070 | if (Alignment > PtrWidth) { |
| 1071 | setSize(getSize() + (Alignment - PtrWidth)); |
| 1072 | setDataSize(getSize()); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | bool |
| 1077 | RecordLayoutBuilder::HasNewVirtualFunction(const CXXRecordDecl *RD) const { |
| 1078 | for (CXXRecordDecl::method_iterator method = RD->method_begin(); |
| 1079 | method != RD->method_end(); |
| 1080 | ++method) { |
| 1081 | if (method->isVirtual() && |
| 1082 | !method->size_overridden_methods()) { |
| 1083 | return true; |
| 1084 | } |
| 1085 | } |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | bool |
| 1090 | RecordLayoutBuilder::HasVBPtr(const CXXRecordDecl *RD) const { |
| 1091 | if (!RD->getNumBases()) |
| 1092 | return false; |
| 1093 | |
| 1094 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1095 | E = RD->bases_end(); I != E; ++I) { |
| 1096 | if (!I->isVirtual()) { |
| 1097 | return false; |
| 1098 | } |
| 1099 | } |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1103 | void |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1104 | RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | ea7b182 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1105 | const CXXRecordDecl *MostDerivedClass) { |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1106 | const CXXRecordDecl *PrimaryBase; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1107 | bool PrimaryBaseIsVirtual; |
Anders Carlsson | fe90096 | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1108 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1109 | if (MostDerivedClass == RD) { |
Anders Carlsson | d20e7cd | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1110 | PrimaryBase = this->PrimaryBase; |
| 1111 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1112 | } else { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1113 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | de710c9 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1114 | PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 1115 | PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1118 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1119 | E = RD->bases_end(); I != E; ++I) { |
| 1120 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1121 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1122 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1123 | const CXXRecordDecl *BaseDecl = |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1124 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1125 | |
| 1126 | if (I->isVirtual()) { |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1127 | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) { |
| 1128 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1129 | |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1130 | // Only lay out the virtual base if it's not an indirect primary base. |
| 1131 | if (!IndirectPrimaryBase) { |
| 1132 | // Only visit virtual bases once. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1133 | if (!VisitedVirtualBases.insert(BaseDecl)) |
Anders Carlsson | 291279e | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1134 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1135 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1136 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1137 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1138 | LayoutVirtualBase(BaseInfo); |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 1139 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1140 | } |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 1141 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1142 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1143 | if (!BaseDecl->getNumVBases()) { |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1144 | // This base isn't interesting since it doesn't have any virtual bases. |
| 1145 | continue; |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 1146 | } |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1147 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1148 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1152 | void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1153 | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
| 1154 | |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1155 | // Layout the base. |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1156 | CharUnits Offset = LayoutBase(Base); |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1157 | |
| 1158 | // Add its base class offset. |
Anders Carlsson | d6ff5d7 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1159 | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1160 | VBases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 6b0d914 | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1161 | |
| 1162 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1165 | CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1166 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1167 | |
| 1168 | // 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] | 1169 | if (Base->Class->isEmpty() && |
Anders Carlsson | 28466ab | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1170 | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1171 | setSize(std::max(getSize(), Layout.getSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1172 | |
Anders Carlsson | a2f8e41 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1173 | return CharUnits::Zero(); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1174 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1175 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1176 | CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign(); |
| 1177 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1178 | |
| 1179 | // The maximum field alignment overrides base align. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1180 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1181 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 1182 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1183 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1184 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1185 | // Round up the current record size to the base's alignment boundary. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1186 | CharUnits Offset = getDataSize().RoundUpToAlignment(BaseAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1187 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1188 | // Try to place the base. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1189 | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset)) |
| 1190 | Offset += BaseAlign; |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1191 | |
Anders Carlsson | d7f3fcf | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1192 | if (!Base->Class->isEmpty()) { |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1193 | // Update the data size. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1194 | setDataSize(Offset + Layout.getNonVirtualSize()); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1195 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1196 | setSize(std::max(getSize(), getDataSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1197 | } else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1198 | setSize(std::max(getSize(), Offset + Layout.getSize())); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Remember max struct/class alignment. |
Argyrios Kyrtzidis | 8b54274 | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1201 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1202 | |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1203 | return Offset; |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1206 | void RecordLayoutBuilder::InitializeLayout(const Decl *D) { |
| 1207 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 1208 | IsUnion = RD->isUnion(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1209 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 1210 | Packed = D->hasAttr<PackedAttr>(); |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1211 | |
| 1212 | IsMsStruct = D->hasAttr<MsStructAttr>(); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1213 | |
Daniel Dunbar | 096ed29 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1214 | // Honor the default struct packing maximum alignment flag. |
| 1215 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOptions().PackStruct) { |
| 1216 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
| 1217 | } |
| 1218 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1219 | // mac68k alignment supersedes maximum field alignment and attribute aligned, |
| 1220 | // and forces all structures to have 2-byte alignment. The IBM docs on it |
| 1221 | // allude to additional (more complicated) semantics, especially with regard |
| 1222 | // to bit-fields, but gcc appears not to follow that. |
| 1223 | if (D->hasAttr<AlignMac68kAttr>()) { |
| 1224 | IsMac68kAlign = true; |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1225 | MaxFieldAlignment = CharUnits::fromQuantity(2); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1226 | Alignment = CharUnits::fromQuantity(2); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1227 | } else { |
| 1228 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1229 | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1230 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1231 | if (unsigned MaxAlign = D->getMaxAlignment()) |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1232 | UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1233 | } |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1234 | } |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 1235 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1236 | void RecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 1237 | InitializeLayout(D); |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1238 | LayoutFields(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1240 | // Finally, round the size of the total struct up to the alignment of the |
| 1241 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1242 | FinishLayout(D); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1246 | if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft) { |
| 1247 | MSLayout(RD); |
| 1248 | return ; |
| 1249 | } |
| 1250 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1251 | InitializeLayout(RD); |
| 1252 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1253 | // Lay out the vtable and the non-virtual bases. |
| 1254 | LayoutNonVirtualBases(RD); |
| 1255 | |
| 1256 | LayoutFields(RD); |
| 1257 | |
Ken Dyck | e738075 | 2011-03-10 01:53:59 +0000 | [diff] [blame] | 1258 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1259 | llvm::RoundUpToAlignment(getSizeInBits(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1260 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1261 | NonVirtualAlignment = Alignment; |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1262 | |
| 1263 | // Lay out the virtual bases and add the primary virtual base offsets. |
| 1264 | LayoutVirtualBases(RD, RD); |
| 1265 | |
| 1266 | VisitedVirtualBases.clear(); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1267 | |
| 1268 | // Finally, round the size of the total struct up to the alignment of the |
| 1269 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1270 | FinishLayout(RD); |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1271 | |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1272 | #ifndef NDEBUG |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1273 | // Check that we have base offsets for all bases. |
| 1274 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1275 | E = RD->bases_end(); I != E; ++I) { |
| 1276 | if (I->isVirtual()) |
| 1277 | continue; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1278 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1279 | const CXXRecordDecl *BaseDecl = |
| 1280 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1281 | |
| 1282 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 1283 | } |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1284 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1285 | // And all virtual bases. |
| 1286 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1287 | E = RD->vbases_end(); I != E; ++I) { |
| 1288 | const CXXRecordDecl *BaseDecl = |
| 1289 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1290 | |
Anders Carlsson | c28a6c9 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1291 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
Anders Carlsson | 5b441d7 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1292 | } |
| 1293 | #endif |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1296 | void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1297 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1298 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1299 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1300 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1302 | // We start laying out ivars not at the end of the superclass |
| 1303 | // structure, but at the next byte following the last field. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1304 | setSize(SL.getDataSize()); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1305 | setDataSize(getSize()); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1306 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1308 | InitializeLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1309 | // Layout each ivar sequentially. |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1310 | for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD; |
| 1311 | IVD = IVD->getNextIvar()) |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1312 | LayoutField(IVD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1314 | // Finally, round the size of the total struct up to the alignment of the |
| 1315 | // struct itself. |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1316 | FinishLayout(D); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1319 | void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1320 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 1321 | // the future, this will need to be tweakable by targets. |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1322 | const FieldDecl *LastFD = 0; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1323 | ZeroLengthBitfield = 0; |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1324 | unsigned RemainingInAlignment = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1326 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) { |
| 1327 | if (IsMsStruct) { |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1328 | FieldDecl *FD = (*Field); |
| 1329 | if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD)) |
| 1330 | ZeroLengthBitfield = FD; |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1331 | // Zero-length bitfields following non-bitfield members are |
| 1332 | // ignored: |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1333 | else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD)) |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1334 | continue; |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1335 | // FIXME. streamline these conditions into a simple one. |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1336 | else if (Context.BitfieldFollowsBitfield(FD, LastFD) || |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1337 | Context.BitfieldFollowsNonBitfield(FD, LastFD) || |
| 1338 | Context.NonBitfieldFollowsBitfield(FD, LastFD)) { |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1339 | // 1) Adjacent bit fields are packed into the same 1-, 2-, or |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1340 | // 4-byte allocation unit if the integral types are the same |
| 1341 | // size and if the next bit field fits into the current |
| 1342 | // allocation unit without crossing the boundary imposed by the |
| 1343 | // common alignment requirements of the bit fields. |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1344 | // 2) Establish a new alignment for a bitfield following |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1345 | // a non-bitfield if size of their types differ. |
Fariborz Jahanian | 307eace | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1346 | // 3) Establish a new alignment for a non-bitfield following |
| 1347 | // a bitfield if size of their types differ. |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1348 | std::pair<uint64_t, unsigned> FieldInfo = |
| 1349 | Context.getTypeInfo(FD->getType()); |
| 1350 | uint64_t TypeSize = FieldInfo.first; |
| 1351 | unsigned FieldAlign = FieldInfo.second; |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1352 | // This check is needed for 'long long' in -m32 mode. |
| 1353 | if (TypeSize > FieldAlign) |
| 1354 | FieldAlign = TypeSize; |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1355 | FieldInfo = Context.getTypeInfo(LastFD->getType()); |
| 1356 | uint64_t TypeSizeLastFD = FieldInfo.first; |
| 1357 | unsigned FieldAlignLastFD = FieldInfo.second; |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1358 | // This check is needed for 'long long' in -m32 mode. |
| 1359 | if (TypeSizeLastFD > FieldAlignLastFD) |
| 1360 | FieldAlignLastFD = TypeSizeLastFD; |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1361 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1362 | if (TypeSizeLastFD != TypeSize) { |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1363 | if (RemainingInAlignment && |
| 1364 | LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame^] | 1365 | LastFD->getBitWidthValue(Context)) { |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1366 | // If previous field was a bitfield with some remaining unfilled |
| 1367 | // bits, pad the field so current field starts on its type boundary. |
| 1368 | uint64_t FieldOffset = |
| 1369 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1370 | uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset; |
| 1371 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1372 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1373 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1374 | RemainingInAlignment = 0; |
| 1375 | } |
| 1376 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1377 | uint64_t UnpaddedFieldOffset = |
| 1378 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1379 | FieldAlign = std::max(FieldAlign, FieldAlignLastFD); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1380 | |
Fariborz Jahanian | 07ceb0a | 2011-05-10 19:00:50 +0000 | [diff] [blame] | 1381 | // The maximum field alignment overrides the aligned attribute. |
| 1382 | if (!MaxFieldAlignment.isZero()) { |
| 1383 | unsigned MaxFieldAlignmentInBits = |
| 1384 | Context.toBits(MaxFieldAlignment); |
| 1385 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1386 | } |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1387 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1388 | uint64_t NewSizeInBits = |
| 1389 | llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign); |
| 1390 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1391 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1392 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
| 1393 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1394 | } |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1395 | if (FD->isBitField()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame^] | 1396 | uint64_t FieldSize = FD->getBitWidthValue(Context); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1397 | assert (FieldSize > 0 && "LayoutFields - ms_struct layout"); |
| 1398 | if (RemainingInAlignment < FieldSize) |
| 1399 | RemainingInAlignment = TypeSize - FieldSize; |
| 1400 | else |
| 1401 | RemainingInAlignment -= FieldSize; |
| 1402 | } |
| 1403 | } |
| 1404 | else if (FD->isBitField()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame^] | 1405 | uint64_t FieldSize = FD->getBitWidthValue(Context); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1406 | std::pair<uint64_t, unsigned> FieldInfo = |
| 1407 | Context.getTypeInfo(FD->getType()); |
| 1408 | uint64_t TypeSize = FieldInfo.first; |
| 1409 | RemainingInAlignment = TypeSize - FieldSize; |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1410 | } |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1411 | LastFD = FD; |
| 1412 | } |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1413 | else if (!Context.getTargetInfo().useBitFieldTypeAlignment() && |
| 1414 | Context.getTargetInfo().useZeroLengthBitfieldAlignment()) { |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1415 | FieldDecl *FD = (*Field); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame^] | 1416 | if (FD->isBitField() && FD->getBitWidthValue(Context) == 0) |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1417 | ZeroLengthBitfield = FD; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1418 | } |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1419 | LayoutField(*Field); |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1420 | } |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1421 | if (IsMsStruct && RemainingInAlignment && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame^] | 1422 | LastFD && LastFD->isBitField() && LastFD->getBitWidthValue(Context)) { |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1423 | // If we ended a bitfield before the full length of the type then |
| 1424 | // pad the struct out to the full length of the last type. |
| 1425 | uint64_t FieldOffset = |
| 1426 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1427 | uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset; |
| 1428 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1429 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 783243b | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1430 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1431 | } |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1434 | void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1435 | uint64_t TypeSize, |
| 1436 | bool FieldPacked, |
| 1437 | const FieldDecl *D) { |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1438 | assert(Context.getLangOptions().CPlusPlus && |
| 1439 | "Can only have wide bit-fields in C++!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1440 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1441 | // Itanium C++ ABI 2.4: |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1442 | // 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] | 1443 | // sizeof(T')*8 <= n. |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1444 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1445 | QualType IntegralPODTypes[] = { |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1446 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1447 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
| 1448 | }; |
| 1449 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1450 | QualType Type; |
| 1451 | for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes); |
| 1452 | I != E; ++I) { |
| 1453 | uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (Size > FieldSize) |
| 1456 | break; |
| 1457 | |
| 1458 | Type = IntegralPODTypes[I]; |
| 1459 | } |
| 1460 | assert(!Type.isNull() && "Did not find a type!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1461 | |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1462 | CharUnits TypeAlign = Context.getTypeAlignInChars(Type); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1463 | |
| 1464 | // We're not going to use any of the unfilled bits in the last byte. |
| 1465 | UnfilledBitsInLastByte = 0; |
| 1466 | |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1467 | uint64_t FieldOffset; |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1468 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1469 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1470 | if (IsUnion) { |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1471 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | aad5fa8 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1472 | FieldOffset = 0; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1473 | } else { |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1474 | // The bitfield is allocated starting at the next offset aligned |
| 1475 | // appropriately for T', with length n bits. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1476 | FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(), |
| 1477 | Context.toBits(TypeAlign)); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1478 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1479 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1480 | |
Ken Dyck | a1a2e8d | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 1481 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1482 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1483 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | // Place this field at the current location. |
| 1487 | FieldOffsets.push_back(FieldOffset); |
| 1488 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1489 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1490 | Context.toBits(TypeAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1491 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1492 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1493 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1494 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1495 | // Remember max struct/class alignment. |
Ken Dyck | dbe37f3 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1496 | UpdateAlignment(TypeAlign); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1499 | void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1500 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1501 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1502 | uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame^] | 1503 | uint64_t FieldSize = D->getBitWidthValue(Context); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1504 | |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1505 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1506 | uint64_t TypeSize = FieldInfo.first; |
| 1507 | unsigned FieldAlign = FieldInfo.second; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1508 | |
Fariborz Jahanian | 7adbed6 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1509 | // This check is needed for 'long long' in -m32 mode. |
| 1510 | if (IsMsStruct && (TypeSize > FieldAlign)) |
| 1511 | FieldAlign = TypeSize; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1512 | |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1513 | if (ZeroLengthBitfield) { |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1514 | std::pair<uint64_t, unsigned> FieldInfo; |
| 1515 | unsigned ZeroLengthBitfieldAlignment; |
| 1516 | if (IsMsStruct) { |
| 1517 | // If a zero-length bitfield is inserted after a bitfield, |
| 1518 | // and the alignment of the zero-length bitfield is |
| 1519 | // greater than the member that follows it, `bar', `bar' |
| 1520 | // will be aligned as the type of the zero-length bitfield. |
| 1521 | if (ZeroLengthBitfield != D) { |
| 1522 | FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType()); |
| 1523 | ZeroLengthBitfieldAlignment = FieldInfo.second; |
| 1524 | // Ignore alignment of subsequent zero-length bitfields. |
| 1525 | if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0)) |
| 1526 | FieldAlign = ZeroLengthBitfieldAlignment; |
| 1527 | if (FieldSize) |
| 1528 | ZeroLengthBitfield = 0; |
| 1529 | } |
| 1530 | } else { |
| 1531 | // The alignment of a zero-length bitfield affects the alignment |
| 1532 | // of the next member. The alignment is the max of the zero |
| 1533 | // length bitfield's alignment and a target specific fixed value. |
| 1534 | unsigned ZeroLengthBitfieldBoundary = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1535 | Context.getTargetInfo().getZeroLengthBitfieldBoundary(); |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1536 | if (ZeroLengthBitfieldBoundary > FieldAlign) |
| 1537 | FieldAlign = ZeroLengthBitfieldBoundary; |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1538 | } |
| 1539 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1540 | |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1541 | if (FieldSize > TypeSize) { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1542 | LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D); |
Anders Carlsson | 5723516 | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1543 | return; |
| 1544 | } |
| 1545 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1546 | // The align if the field is not packed. This is to check if the attribute |
| 1547 | // was unnecessary (-Wpacked). |
| 1548 | unsigned UnpackedFieldAlign = FieldAlign; |
| 1549 | uint64_t UnpackedFieldOffset = FieldOffset; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1550 | if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1551 | UnpackedFieldAlign = 1; |
| 1552 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1553 | if (FieldPacked || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1554 | (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)) |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1555 | FieldAlign = 1; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1556 | FieldAlign = std::max(FieldAlign, D->getMaxAlignment()); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1557 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1558 | |
| 1559 | // The maximum field alignment overrides the aligned attribute. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1560 | if (!MaxFieldAlignment.isZero()) { |
| 1561 | unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); |
| 1562 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1563 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1564 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1565 | |
Daniel Dunbar | 3d9289c | 2010-04-15 06:18:39 +0000 | [diff] [blame] | 1566 | // Check if we need to add padding to give the field the correct alignment. |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1567 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
Daniel Dunbar | f35e765 | 2010-06-29 18:34:35 +0000 | [diff] [blame] | 1568 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1569 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1570 | if (FieldSize == 0 || |
| 1571 | (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize) |
| 1572 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1573 | UnpackedFieldAlign); |
| 1574 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1575 | // Padding members don't affect overall alignment, unless zero length bitfield |
| 1576 | // alignment is enabled. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1577 | if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment()) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1578 | FieldAlign = UnpackedFieldAlign = 1; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1579 | |
Chad Rosier | e1a6a0e | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1580 | if (!IsMsStruct) |
| 1581 | ZeroLengthBitfield = 0; |
| 1582 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1583 | // Place this field at the current location. |
| 1584 | FieldOffsets.push_back(FieldOffset); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1585 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1586 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
| 1587 | UnpackedFieldAlign, FieldPacked, D); |
| 1588 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1589 | // Update DataSize to include the last byte containing (part of) the bitfield. |
| 1590 | if (IsUnion) { |
| 1591 | // FIXME: I think FieldSize should be TypeSize here. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1592 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1593 | } else { |
| 1594 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1595 | |
Ken Dyck | a1a2e8d | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 1596 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1597 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1598 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1599 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1600 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1601 | // Update the size. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1602 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1603 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1604 | // Remember max struct/class alignment. |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1605 | UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), |
| 1606 | Context.toCharUnitsFromBits(UnpackedFieldAlign)); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1609 | void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1610 | if (D->isBitField()) { |
| 1611 | LayoutBitField(D); |
| 1612 | return; |
| 1613 | } |
| 1614 | |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1615 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1616 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1617 | // Reset the unfilled bits. |
| 1618 | UnfilledBitsInLastByte = 0; |
| 1619 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1620 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1621 | CharUnits FieldOffset = |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1622 | IsUnion ? CharUnits::Zero() : getDataSize(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1623 | CharUnits FieldSize; |
| 1624 | CharUnits FieldAlign; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1625 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1626 | if (D->getType()->isIncompleteArrayType()) { |
| 1627 | // This is a flexible array member; we can't directly |
| 1628 | // query getTypeInfo about these, so we figure it out here. |
| 1629 | // Flexible array members don't have any size, but they |
| 1630 | // have to be aligned appropriately for their element type. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1631 | FieldSize = CharUnits::Zero(); |
Anders Carlsson | 5efc56e | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1632 | const ArrayType* ATy = Context.getAsArrayType(D->getType()); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1633 | FieldAlign = Context.getTypeAlignInChars(ATy->getElementType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1634 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 1635 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1636 | FieldSize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1637 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS)); |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1638 | FieldAlign = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1639 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1640 | } else { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1641 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 1642 | Context.getTypeInfoInChars(D->getType()); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1643 | FieldSize = FieldInfo.first; |
| 1644 | FieldAlign = FieldInfo.second; |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1645 | |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1646 | if (ZeroLengthBitfield) { |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1647 | CharUnits ZeroLengthBitfieldBoundary = |
| 1648 | Context.toCharUnitsFromBits( |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1649 | Context.getTargetInfo().getZeroLengthBitfieldBoundary()); |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1650 | if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) { |
| 1651 | // If a zero-length bitfield is inserted after a bitfield, |
| 1652 | // and the alignment of the zero-length bitfield is |
| 1653 | // greater than the member that follows it, `bar', `bar' |
| 1654 | // will be aligned as the type of the zero-length bitfield. |
| 1655 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 1656 | Context.getTypeInfoInChars(ZeroLengthBitfield->getType()); |
| 1657 | CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second; |
| 1658 | if (ZeroLengthBitfieldAlignment > FieldAlign) |
| 1659 | FieldAlign = ZeroLengthBitfieldAlignment; |
Chad Rosier | 64b18ee | 2011-08-04 19:25:14 +0000 | [diff] [blame] | 1660 | } else if (ZeroLengthBitfieldBoundary > FieldAlign) { |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1661 | // Align 'bar' based on a fixed alignment specified by the target. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1662 | assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
Chad Rosier | a336c6f | 2011-08-04 17:52:43 +0000 | [diff] [blame] | 1663 | "ZeroLengthBitfieldBoundary should only be used in conjunction" |
| 1664 | " with useZeroLengthBitfieldAlignment."); |
Chad Rosier | 18903ee | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1665 | FieldAlign = ZeroLengthBitfieldBoundary; |
| 1666 | } |
Fariborz Jahanian | fc0fe6e | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1667 | ZeroLengthBitfield = 0; |
| 1668 | } |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1669 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1670 | if (Context.getLangOptions().MSBitfields || IsMsStruct) { |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1671 | // If MS bitfield layout is required, figure out what type is being |
| 1672 | // laid out and align the field to the width of that type. |
| 1673 | |
| 1674 | // Resolve all typedefs down to their base type and round up the field |
| 1675 | // alignment if necessary. |
| 1676 | QualType T = Context.getBaseElementType(D->getType()); |
| 1677 | if (const BuiltinType *BTy = T->getAs<BuiltinType>()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1678 | CharUnits TypeSize = Context.getTypeSizeInChars(BTy); |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1679 | if (TypeSize > FieldAlign) |
| 1680 | FieldAlign = TypeSize; |
| 1681 | } |
| 1682 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1683 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1685 | // The align if the field is not packed. This is to check if the attribute |
| 1686 | // was unnecessary (-Wpacked). |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1687 | CharUnits UnpackedFieldAlign = FieldAlign; |
| 1688 | CharUnits UnpackedFieldOffset = FieldOffset; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1689 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1690 | if (FieldPacked) |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1691 | FieldAlign = CharUnits::One(); |
| 1692 | CharUnits MaxAlignmentInChars = |
| 1693 | Context.toCharUnitsFromBits(D->getMaxAlignment()); |
| 1694 | FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); |
| 1695 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1696 | |
| 1697 | // The maximum field alignment overrides the aligned attribute. |
Ken Dyck | 02ced6f | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1698 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1699 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 1700 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1701 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1702 | |
| 1703 | // Round up the current record size to the field's alignment boundary. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1704 | FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign); |
| 1705 | UnpackedFieldOffset = |
| 1706 | UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1707 | |
Anders Carlsson | b1fcdd0 | 2010-05-30 06:52:33 +0000 | [diff] [blame] | 1708 | if (!IsUnion && EmptySubobjects) { |
| 1709 | // Check if we can place the field at this offset. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1710 | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1711 | // We couldn't place the field at the offset. Try again at a new offset. |
| 1712 | FieldOffset += FieldAlign; |
| 1713 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1714 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1715 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1716 | // Place this field at the current location. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1717 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1719 | CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, |
| 1720 | Context.toBits(UnpackedFieldOffset), |
| 1721 | Context.toBits(UnpackedFieldAlign), FieldPacked, D); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1722 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1723 | // Reserve space for this field. |
Daniel Dunbar | 30cdc70 | 2011-02-24 16:40:53 +0000 | [diff] [blame] | 1724 | uint64_t FieldSizeInBits = Context.toBits(FieldSize); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1725 | if (IsUnion) |
Daniel Dunbar | 30cdc70 | 2011-02-24 16:40:53 +0000 | [diff] [blame] | 1726 | setSize(std::max(getSizeInBits(), FieldSizeInBits)); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1727 | else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1728 | setSize(FieldOffset + FieldSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Anders Carlsson | 47680d8 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 1730 | // Update the data size. |
Daniel Dunbar | 30cdc70 | 2011-02-24 16:40:53 +0000 | [diff] [blame] | 1731 | setDataSize(getSizeInBits()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1733 | // Remember max struct/class alignment. |
Ken Dyck | 6d90e89 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1734 | UpdateAlignment(FieldAlign, UnpackedFieldAlign); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1737 | void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) { |
| 1738 | |
| 1739 | if (!RD->getNumVBases()) |
| 1740 | return; |
| 1741 | |
| 1742 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1743 | E = RD->vbases_end(); I != E; ++I) { |
| 1744 | |
| 1745 | const CXXRecordDecl* BaseDecl = I->getType()->getAsCXXRecordDecl(); |
| 1746 | const BaseSubobjectInfo* BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1747 | |
| 1748 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1749 | |
| 1750 | LayoutVirtualBase(BaseInfo); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | void RecordLayoutBuilder::MSLayout(const CXXRecordDecl *RD) { |
| 1755 | |
| 1756 | bool IsVBPtrAddedToLayout = false; |
| 1757 | |
| 1758 | InitializeLayout(RD); |
| 1759 | |
| 1760 | if (HasVBPtr(RD)) { |
| 1761 | // If all bases are virtual and the class declares a new virtual function, |
| 1762 | // MSVC builds a vfptr. |
| 1763 | if (HasNewVirtualFunction(RD)) { |
| 1764 | AddVPointer(); |
| 1765 | } |
| 1766 | |
| 1767 | VBPtrOffset = getSize(); |
| 1768 | AddVPointer(); |
| 1769 | IsVBPtrAddedToLayout = true; |
| 1770 | |
| 1771 | ComputeBaseSubobjectInfo(RD); |
| 1772 | } else { |
| 1773 | LayoutNonVirtualBases(RD); |
| 1774 | } |
| 1775 | |
| 1776 | if (RD->getNumVBases() && |
| 1777 | !IsVBPtrAddedToLayout) { |
| 1778 | // Add vbptr. |
| 1779 | VBPtrOffset = getSize(); |
| 1780 | AddVPointer(); |
| 1781 | } |
| 1782 | |
| 1783 | LayoutFields(RD); |
| 1784 | |
| 1785 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1786 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 1787 | Context.getTargetInfo().getCharAlign())); |
| 1788 | NonVirtualAlignment = Alignment; |
| 1789 | |
| 1790 | if (NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) { |
| 1791 | CharUnits AlignMember = |
| 1792 | NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize; |
| 1793 | |
| 1794 | setSize(getSize() + AlignMember); |
| 1795 | setDataSize(getSize()); |
| 1796 | |
| 1797 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1798 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 1799 | Context.getTargetInfo().getCharAlign())); |
| 1800 | } |
| 1801 | |
| 1802 | MSLayoutVirtualBases(RD); |
| 1803 | |
| 1804 | VisitedVirtualBases.clear(); |
| 1805 | |
| 1806 | // Finally, round the size of the total struct up to the alignment of the |
| 1807 | // struct itself. |
| 1808 | if (!RD->getNumVBases()) |
| 1809 | FinishLayout(RD); |
| 1810 | |
| 1811 | #ifndef NDEBUG |
| 1812 | // Check that we have base offsets for all bases. |
| 1813 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1814 | E = RD->bases_end(); I != E; ++I) { |
| 1815 | if (I->isVirtual()) |
| 1816 | continue; |
| 1817 | |
| 1818 | const CXXRecordDecl *BaseDecl = |
| 1819 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1820 | |
| 1821 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 1822 | } |
| 1823 | |
| 1824 | // And all virtual bases. |
| 1825 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1826 | E = RD->vbases_end(); I != E; ++I) { |
| 1827 | const CXXRecordDecl *BaseDecl = |
| 1828 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1829 | |
| 1830 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
| 1831 | } |
| 1832 | #endif |
| 1833 | } |
| 1834 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1835 | void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1836 | // In C++, records cannot be of size 0. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1837 | if (Context.getLangOptions().CPlusPlus && getSizeInBits() == 0) { |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1838 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 1839 | // Compatibility with gcc requires a class (pod or non-pod) |
| 1840 | // which is not empty but of size 0; such as having fields of |
| 1841 | // array of zero-length, remains of Size 0 |
| 1842 | if (RD->isEmpty()) |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1843 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1844 | } |
| 1845 | else |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1846 | setSize(CharUnits::One()); |
Fariborz Jahanian | 09b2331 | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1847 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1848 | // Finally, round the size of the record up to the alignment of the |
| 1849 | // record itself. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1850 | uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte; |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1851 | uint64_t UnpackedSizeInBits = |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1852 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 1853 | Context.toBits(UnpackedAlignment)); |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1854 | CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits); |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1855 | setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment))); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1857 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1858 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 1859 | // Warn if padding was introduced to the struct/class/union. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1860 | if (getSizeInBits() > UnpaddedSize) { |
| 1861 | unsigned PadSize = getSizeInBits() - UnpaddedSize; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1862 | bool InBits = true; |
| 1863 | if (PadSize % CharBitNum == 0) { |
| 1864 | PadSize = PadSize / CharBitNum; |
| 1865 | InBits = false; |
| 1866 | } |
| 1867 | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
| 1868 | << Context.getTypeDeclType(RD) |
| 1869 | << PadSize |
| 1870 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 1871 | } |
| 1872 | |
| 1873 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1874 | // bother since there won't be alignment issues. |
Ken Dyck | ecfc755 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1875 | if (Packed && UnpackedAlignment > CharUnits::One() && |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1876 | getSize() == UnpackedSize) |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1877 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1878 | << Context.getTypeDeclType(RD); |
| 1879 | } |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1882 | void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment, |
| 1883 | CharUnits UnpackedNewAlignment) { |
Daniel Dunbar | 6da1098 | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1884 | // The alignment is not modified when using 'mac68k' alignment. |
| 1885 | if (IsMac68kAlign) |
| 1886 | return; |
| 1887 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1888 | if (NewAlignment > Alignment) { |
| 1889 | assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() && |
| 1890 | "Alignment not a power of 2")); |
| 1891 | Alignment = NewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1894 | if (UnpackedNewAlignment > UnpackedAlignment) { |
| 1895 | assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() && |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1896 | "Alignment not a power of 2")); |
Ken Dyck | 85ef043 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1897 | UnpackedAlignment = UnpackedNewAlignment; |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset, |
| 1902 | uint64_t UnpaddedOffset, |
| 1903 | uint64_t UnpackedOffset, |
| 1904 | unsigned UnpackedAlign, |
| 1905 | bool isPacked, |
| 1906 | const FieldDecl *D) { |
| 1907 | // We let objc ivars without warning, objc interfaces generally are not used |
| 1908 | // for padding tricks. |
| 1909 | if (isa<ObjCIvarDecl>(D)) |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1910 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Ted Kremenek | fed48af | 2011-09-06 19:40:45 +0000 | [diff] [blame] | 1912 | // Don't warn about structs created without a SourceLocation. This can |
| 1913 | // be done by clients of the AST, such as codegen. |
| 1914 | if (D->getLocation().isInvalid()) |
| 1915 | return; |
| 1916 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1917 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1919 | // Warn if padding was introduced to the struct/class. |
| 1920 | if (!IsUnion && Offset > UnpaddedOffset) { |
| 1921 | unsigned PadSize = Offset - UnpaddedOffset; |
| 1922 | bool InBits = true; |
| 1923 | if (PadSize % CharBitNum == 0) { |
| 1924 | PadSize = PadSize / CharBitNum; |
| 1925 | InBits = false; |
| 1926 | } |
| 1927 | if (D->getIdentifier()) |
| 1928 | Diag(D->getLocation(), diag::warn_padded_struct_field) |
| 1929 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 1930 | << Context.getTypeDeclType(D->getParent()) |
| 1931 | << PadSize |
| 1932 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not |
| 1933 | << D->getIdentifier(); |
| 1934 | else |
| 1935 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
| 1936 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 1937 | << Context.getTypeDeclType(D->getParent()) |
| 1938 | << PadSize |
| 1939 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 1940 | } |
| 1941 | |
| 1942 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1943 | // bother since there won't be alignment issues. |
| 1944 | if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset) |
| 1945 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1946 | << D->getIdentifier(); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1947 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1949 | const CXXMethodDecl * |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1950 | RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1951 | // 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] | 1952 | if (!RD->isPolymorphic()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1953 | return 0; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1954 | |
Eli Friedman | 300f55d | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 1955 | // 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] | 1956 | // at least, there's no point to assigning a key function to such a class; |
| 1957 | // this doesn't affect the ABI.) |
Eli Friedman | 300f55d | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 1958 | if (RD->getLinkage() != ExternalLinkage) |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1959 | return 0; |
| 1960 | |
Argyrios Kyrtzidis | 8c64bbe | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 1961 | // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6. |
| 1962 | // Same behavior as GCC. |
| 1963 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 1964 | if (TSK == TSK_ImplicitInstantiation || |
| 1965 | TSK == TSK_ExplicitInstantiationDefinition) |
| 1966 | return 0; |
| 1967 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1968 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 1969 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1970 | const CXXMethodDecl *MD = *I; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1971 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1972 | if (!MD->isVirtual()) |
| 1973 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1974 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1975 | if (MD->isPure()) |
| 1976 | continue; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1977 | |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1978 | // Ignore implicit member functions, they are always marked as inline, but |
| 1979 | // they don't have a body until they're defined. |
| 1980 | if (MD->isImplicit()) |
| 1981 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | a318efd | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 1983 | if (MD->isInlineSpecified()) |
| 1984 | continue; |
Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1985 | |
| 1986 | if (MD->hasInlineBody()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1987 | continue; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1988 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1989 | // We found it. |
| 1990 | return MD; |
| 1991 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1992 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1993 | return 0; |
| 1994 | } |
| 1995 | |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1996 | DiagnosticBuilder |
| 1997 | RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1998 | return Context.getDiagnostics().Report(Loc, DiagID); |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2001 | /// getASTRecordLayout - Get or compute information about the layout of the |
| 2002 | /// specified record (struct/union/class), which indicates its size and field |
| 2003 | /// position information. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2004 | const ASTRecordLayout & |
| 2005 | ASTContext::getASTRecordLayout(const RecordDecl *D) const { |
John McCall | 0710e55 | 2011-10-07 02:39:22 +0000 | [diff] [blame] | 2006 | // These asserts test different things. A record has a definition |
| 2007 | // as soon as we begin to parse the definition. That definition is |
| 2008 | // not a complete definition (which is what isDefinition() tests) |
| 2009 | // until we *finish* parsing the definition. |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2010 | D = D->getDefinition(); |
| 2011 | assert(D && "Cannot get layout of forward declarations!"); |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2012 | assert(D->isCompleteDefinition() && "Cannot layout type before complete!"); |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2013 | |
| 2014 | // Look up this layout, if already laid out, return what we have. |
| 2015 | // Note that we can't save a reference to the entry because this function |
| 2016 | // is recursive. |
| 2017 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
| 2018 | if (Entry) return *Entry; |
| 2019 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2020 | const ASTRecordLayout *NewEntry; |
| 2021 | |
| 2022 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2023 | EmptySubobjectMap EmptySubobjects(*this, RD); |
Anders Carlsson | 439edd1 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 2024 | |
Argyrios Kyrtzidis | 3c6cd17 | 2010-08-25 00:32:14 +0000 | [diff] [blame] | 2025 | llvm::OwningPtr<RecordLayoutBuilder> Builder; |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2026 | CharUnits TargetAlign = CharUnits::One(); |
| 2027 | |
| 2028 | Builder.reset(new RecordLayoutBuilder(*this, |
| 2029 | &EmptySubobjects, |
| 2030 | TargetAlign)); |
| 2031 | |
Ted Kremenek | f75d089 | 2011-03-19 01:00:36 +0000 | [diff] [blame] | 2032 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022dbc1 | 2011-03-22 01:15:19 +0000 | [diff] [blame] | 2033 | llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder> |
| 2034 | RecordBuilderCleanup(Builder.get()); |
Ted Kremenek | f75d089 | 2011-03-19 01:00:36 +0000 | [diff] [blame] | 2035 | |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 2036 | Builder->Layout(RD); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2037 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2038 | TargetAlign = Builder->getAligment(); |
| 2039 | |
| 2040 | if (getTargetInfo().getCXXABI() == CXXABI_Microsoft && |
| 2041 | TargetAlign.getQuantity() > 4) { |
| 2042 | // MSVC rounds the vtable pointer to the struct alignment in what must |
| 2043 | // be a multi-pass operation. For now, let the builder figure out the |
| 2044 | // alignment and recalculate the layout once its known. |
| 2045 | Builder.reset(new RecordLayoutBuilder(*this, |
| 2046 | &EmptySubobjects, |
| 2047 | TargetAlign)); |
| 2048 | |
| 2049 | Builder->Layout(RD); |
| 2050 | |
| 2051 | // Recover resources if we crash before exiting this method. |
| 2052 | llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder> |
| 2053 | RecordBuilderCleanup(Builder.get()); |
| 2054 | } |
| 2055 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2056 | // FIXME: This is not always correct. See the part about bitfields at |
| 2057 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 2058 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2059 | // This does not affect the calculations of MSVC layouts |
| 2060 | bool IsPODForThePurposeOfLayout = |
| 2061 | (getTargetInfo().getCXXABI() == CXXABI_Microsoft) || |
| 2062 | cast<CXXRecordDecl>(D)->isPOD(); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2063 | |
| 2064 | // FIXME: This should be done in FinalizeLayout. |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2065 | CharUnits DataSize = |
| 2066 | IsPODForThePurposeOfLayout ? Builder->getSize() : Builder->getDataSize(); |
| 2067 | CharUnits NonVirtualSize = |
| 2068 | IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize; |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2069 | |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2070 | NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2071 | new (*this) ASTRecordLayout(*this, Builder->getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2072 | Builder->Alignment, |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2073 | Builder->GetVBPtrOffset(), |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2074 | DataSize, |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 2075 | Builder->FieldOffsets.data(), |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 2076 | Builder->FieldOffsets.size(), |
Ken Dyck | af1c83f | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 2077 | NonVirtualSize, |
Ken Dyck | a2d3dda | 2011-02-16 01:43:15 +0000 | [diff] [blame] | 2078 | Builder->NonVirtualAlignment, |
Anders Carlsson | c121b4e | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2079 | EmptySubobjects.SizeOfLargestEmptySubobject, |
Charles Davis | c2c576a | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 2080 | Builder->PrimaryBase, |
| 2081 | Builder->PrimaryBaseIsVirtual, |
| 2082 | Builder->Bases, Builder->VBases); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2083 | } else { |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2084 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0, CharUnits::One()); |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2085 | Builder.Layout(D); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2086 | |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2087 | NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2088 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2089 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2090 | Builder.getSize(), |
Anders Carlsson | d295486 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2091 | Builder.FieldOffsets.data(), |
| 2092 | Builder.FieldOffsets.size()); |
| 2093 | } |
| 2094 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2095 | ASTRecordLayouts[D] = NewEntry; |
| 2096 | |
| 2097 | if (getLangOptions().DumpRecordLayouts) { |
| 2098 | llvm::errs() << "\n*** Dumping AST Record Layout\n"; |
| 2099 | DumpRecordLayout(D, llvm::errs()); |
| 2100 | } |
| 2101 | |
| 2102 | return *NewEntry; |
| 2103 | } |
| 2104 | |
| 2105 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
| 2106 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
| 2107 | assert(RD && "Cannot get key function for forward declarations!"); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2108 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2109 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2110 | if (!Entry) |
Anders Carlsson | c222620 | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 2111 | Entry = RecordLayoutBuilder::ComputeKeyFunction(RD); |
Daniel Dunbar | 592a85c | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2112 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2113 | return Entry; |
| 2114 | } |
| 2115 | |
Eric Christopher | 8a39a01 | 2011-10-05 06:00:51 +0000 | [diff] [blame] | 2116 | /// getObjCLayout - Get or compute information about the layout of the |
| 2117 | /// given interface. |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2118 | /// |
| 2119 | /// \param Impl - If given, also include the layout of the interface's |
| 2120 | /// implementation. This may differ by including synthesized ivars. |
| 2121 | const ASTRecordLayout & |
| 2122 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2123 | const ObjCImplementationDecl *Impl) const { |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2124 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 2125 | |
| 2126 | // Look up this layout, if already laid out, return what we have. |
| 2127 | ObjCContainerDecl *Key = |
| 2128 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 2129 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 2130 | return *Entry; |
| 2131 | |
| 2132 | // Add in synthesized ivar count if laying out an implementation. |
| 2133 | if (Impl) { |
| 2134 | unsigned SynthCount = CountNonClassIvars(D); |
| 2135 | // If there aren't any sythesized ivars then reuse the interface |
| 2136 | // entry. Note we can't cache this because we simply free all |
| 2137 | // entries later; however we shouldn't look up implementations |
| 2138 | // frequently. |
| 2139 | if (SynthCount == 0) |
| 2140 | return getObjCLayout(D, 0); |
| 2141 | } |
| 2142 | |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2143 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0, CharUnits::One()); |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2144 | Builder.Layout(D); |
| 2145 | |
Anders Carlsson | df291d8 | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2146 | const ASTRecordLayout *NewEntry = |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2147 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | 4731d5b | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2148 | Builder.Alignment, |
Ken Dyck | 1b4420e | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2149 | Builder.getDataSize(), |
Anders Carlsson | 6ed3a9a | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2150 | Builder.FieldOffsets.data(), |
| 2151 | Builder.FieldOffsets.size()); |
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 | ObjCLayouts[Key] = NewEntry; |
| 2154 | |
| 2155 | return *NewEntry; |
| 2156 | } |
| 2157 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2158 | static void PrintOffset(raw_ostream &OS, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2159 | CharUnits Offset, unsigned IndentLevel) { |
| 2160 | OS << llvm::format("%4d | ", Offset.getQuantity()); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2161 | OS.indent(IndentLevel * 2); |
| 2162 | } |
| 2163 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2164 | static void DumpCXXRecordLayout(raw_ostream &OS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2165 | const CXXRecordDecl *RD, const ASTContext &C, |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2166 | CharUnits Offset, |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2167 | unsigned IndentLevel, |
| 2168 | const char* Description, |
| 2169 | bool IncludeVirtualBases) { |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2170 | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2171 | |
| 2172 | PrintOffset(OS, Offset, IndentLevel); |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2173 | OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString(); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2174 | if (Description) |
| 2175 | OS << ' ' << Description; |
| 2176 | if (RD->isEmpty()) |
| 2177 | OS << " (empty)"; |
| 2178 | OS << '\n'; |
| 2179 | |
| 2180 | IndentLevel++; |
| 2181 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2182 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2183 | bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2184 | |
| 2185 | // Vtable pointer. |
| 2186 | if (RD->isDynamicClass() && !PrimaryBase) { |
| 2187 | PrintOffset(OS, Offset, IndentLevel); |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 2188 | OS << '(' << RD << " vtable pointer)\n"; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2189 | } |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2190 | |
| 2191 | if (HasVbptr && !PrimaryBase) { |
| 2192 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
| 2193 | OS << '(' << RD << " vbtable pointer)\n"; |
| 2194 | |
| 2195 | // one vbtable per class |
| 2196 | HasVbptr = false; |
| 2197 | } |
| 2198 | |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2199 | // Dump (non-virtual) bases |
| 2200 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 2201 | E = RD->bases_end(); I != E; ++I) { |
| 2202 | assert(!I->getType()->isDependentType() && |
| 2203 | "Cannot layout class with dependent bases."); |
| 2204 | if (I->isVirtual()) |
| 2205 | continue; |
| 2206 | |
| 2207 | const CXXRecordDecl *Base = |
| 2208 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2209 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2210 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2211 | |
| 2212 | DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
| 2213 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 2214 | /*IncludeVirtualBases=*/false); |
| 2215 | } |
Eli Friedman | 84d2d3a | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2216 | // vbptr |
| 2217 | if (HasVbptr) { |
| 2218 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
| 2219 | OS << '(' << RD << " vbtable pointer)\n"; |
| 2220 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2221 | |
| 2222 | // Dump fields. |
| 2223 | uint64_t FieldNo = 0; |
| 2224 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 2225 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
| 2226 | const FieldDecl *Field = *I; |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2227 | CharUnits FieldOffset = Offset + |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 2228 | C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo)); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2229 | |
| 2230 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
| 2231 | if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2232 | DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel, |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2233 | Field->getName().data(), |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2234 | /*IncludeVirtualBases=*/true); |
| 2235 | continue; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | PrintOffset(OS, FieldOffset, IndentLevel); |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 2240 | OS << Field->getType().getAsString() << ' ' << Field << '\n'; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | if (!IncludeVirtualBases) |
| 2244 | return; |
| 2245 | |
| 2246 | // Dump virtual bases. |
| 2247 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 2248 | E = RD->vbases_end(); I != E; ++I) { |
| 2249 | assert(I->isVirtual() && "Found non-virtual class!"); |
| 2250 | const CXXRecordDecl *VBase = |
| 2251 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2252 | |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2253 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2254 | DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
| 2255 | VBase == PrimaryBase ? |
| 2256 | "(primary virtual base)" : "(virtual base)", |
| 2257 | /*IncludeVirtualBases=*/false); |
| 2258 | } |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2259 | |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2260 | OS << " sizeof=" << Layout.getSize().getQuantity(); |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 2261 | OS << ", dsize=" << Layout.getDataSize().getQuantity(); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2262 | OS << ", align=" << Layout.getAlignment().getQuantity() << '\n'; |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 2263 | OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity(); |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 2264 | OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n'; |
Daniel Dunbar | aa423af | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2265 | OS << '\n'; |
| 2266 | } |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2267 | |
| 2268 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2269 | raw_ostream &OS) const { |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2270 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 2271 | |
| 2272 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Anders Carlsson | 3f01871 | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2273 | return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0, |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2274 | /*IncludeVirtualBases=*/true); |
| 2275 | |
| 2276 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
| 2277 | OS << "Record: "; |
| 2278 | RD->dump(); |
| 2279 | OS << "\nLayout: "; |
| 2280 | OS << "<ASTRecordLayout\n"; |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2281 | OS << " Size:" << toBits(Info.getSize()) << "\n"; |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 2282 | OS << " DataSize:" << toBits(Info.getDataSize()) << "\n"; |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2283 | OS << " Alignment:" << toBits(Info.getAlignment()) << "\n"; |
Daniel Dunbar | ccabe48 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2284 | OS << " FieldOffsets: ["; |
| 2285 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
| 2286 | if (i) OS << ", "; |
| 2287 | OS << Info.getFieldOffset(i); |
| 2288 | } |
| 2289 | OS << "]>\n"; |
| 2290 | } |