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