Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 1 | //=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==// |
Anders Carlsson | bda4c10 | 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 | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 10 | #include "clang/AST/Attr.h" |
Anders Carlsson | 245656e | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 11 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 12 | #include "clang/AST/Decl.h" |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 18 | #include "clang/Sema/SemaDiagnostic.h" |
Daniel Dunbar | bf9e48c | 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 | 4d96d9f | 2011-03-19 01:00:36 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CrashRecoveryContext.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
Benjamin Kramer | 7e22028 | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 26 | namespace { |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | ea2f41c | 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 | 4a25799 | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 41 | const CXXRecordDecl *Class; |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 42 | |
| 43 | /// IsVirtual - Whether the BaseInfo represents a virtual base or not. |
Anders Carlsson | 4a25799 | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 44 | bool IsVirtual; |
| 45 | |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 46 | /// Bases - Information about the base subobjects. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 47 | SmallVector<BaseSubobjectInfo*, 4> Bases; |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 48 | |
Anders Carlsson | 6e26454 | 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 | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 52 | |
| 53 | // FIXME: Document. |
| 54 | const BaseSubobjectInfo *Derived; |
Anders Carlsson | 4a25799 | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Anders Carlsson | 6a91c03 | 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 | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 60 | const ASTContext &Context; |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 61 | uint64_t CharWidth; |
| 62 | |
Anders Carlsson | 6a91c03 | 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 | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 65 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 66 | /// EmptyClassOffsets - A map from offsets to empty record decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | typedef SmallVector<const CXXRecordDecl *, 1> ClassVectorTy; |
Anders Carlsson | d8da763 | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 68 | typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 69 | EmptyClassOffsetsMapTy EmptyClassOffsets; |
| 70 | |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 71 | /// MaxEmptyClassOffset - The highest offset known to contain an empty |
| 72 | /// base subobject. |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 73 | CharUnits MaxEmptyClassOffset; |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 75 | /// ComputeEmptySubobjectSizes - Compute the size of the largest base or |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 76 | /// member subobject that is empty. |
| 77 | void ComputeEmptySubobjectSizes(); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 78 | |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 79 | void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 80 | |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 81 | void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 82 | CharUnits Offset, bool PlacingEmptyBase); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 83 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 84 | void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
| 85 | const CXXRecordDecl *Class, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 86 | CharUnits Offset); |
| 87 | void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 88 | |
Anders Carlsson | c8cb462 | 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 | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 91 | bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const { |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 92 | return Offset <= MaxEmptyClassOffset; |
| 93 | } |
| 94 | |
Anders Carlsson | 8c6acc6 | 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 | ff3a517 | 2011-01-24 01:28:50 +0000 | [diff] [blame] | 101 | return Context.toCharUnitsFromBits(FieldOffset); |
Anders Carlsson | d8da763 | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 102 | } |
Anders Carlsson | d8da763 | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 103 | |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 104 | protected: |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 105 | bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
| 106 | CharUnits Offset) const; |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 107 | |
| 108 | bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 109 | CharUnits Offset); |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 110 | |
| 111 | bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 112 | const CXXRecordDecl *Class, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 113 | CharUnits Offset) const; |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 114 | bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 115 | CharUnits Offset) const; |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 116 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 117 | public: |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 118 | /// This holds the size of the largest empty subobject (either a base |
Daniel Dunbar | 0aa7edb | 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 | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 120 | /// any empty classes. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 121 | CharUnits SizeOfLargestEmptySubobject; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 122 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 123 | EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class) |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 124 | : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) { |
Anders Carlsson | 261febd | 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 | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 130 | /// Returns false if placing the record will result in two components |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 131 | /// (direct or indirect) of the same type having the same offset. |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 132 | bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 133 | CharUnits Offset); |
Anders Carlsson | 812a345 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 137 | bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 138 | }; |
Anders Carlsson | 0c54fc9 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 147 | CharUnits EmptySize; |
Anders Carlsson | 0c54fc9 | 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 | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 151 | EmptySize = Layout.getSize(); |
Anders Carlsson | 0c54fc9 | 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 | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 156 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 157 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 158 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 159 | } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 160 | |
Anders Carlsson | 0c54fc9 | 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 | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 165 | |
| 166 | const RecordType *RT = |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 167 | Context.getBaseElementType(FD->getType())->getAs<RecordType>(); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 168 | |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 169 | // We only care about record types. |
| 170 | if (!RT) |
| 171 | continue; |
| 172 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 173 | CharUnits EmptySize; |
Anders Carlsson | 0c54fc9 | 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 | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 178 | EmptySize = Layout.getSize(); |
Anders Carlsson | 0c54fc9 | 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 | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 183 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 184 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 185 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 189 | bool |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 190 | EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 191 | CharUnits Offset) const { |
Anders Carlsson | 812a345 | 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 | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 196 | EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset); |
Anders Carlsson | 812a345 | 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 | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 209 | CharUnits Offset) { |
Anders Carlsson | 812a345 | 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 | 272324b | 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 | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 216 | ClassVectorTy& Classes = EmptyClassOffsets[Offset]; |
Rafael Espindola | 272324b | 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 | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 220 | Classes.push_back(RD); |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 221 | |
| 222 | // Update the empty class offset. |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 223 | if (Offset > MaxEmptyClassOffset) |
| 224 | MaxEmptyClassOffset = Offset; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | bool |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 228 | EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
| 229 | CharUnits Offset) { |
Anders Carlsson | 2177ab7 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 232 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 233 | return true; |
| 234 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 235 | if (!CanPlaceSubobjectAtOffset(Info->Class, Offset)) |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 236 | return false; |
| 237 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 238 | // Traverse all non-virtual bases. |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 239 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 240 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 241 | BaseSubobjectInfo* Base = Info->Bases[I]; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 242 | if (Base->IsVirtual) |
| 243 | continue; |
| 244 | |
Anders Carlsson | 6a35674 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 245 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 246 | |
| 247 | if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset)) |
| 248 | return false; |
| 249 | } |
| 250 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 251 | if (Info->PrimaryVirtualBaseInfo) { |
| 252 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 58b16b6 | 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 | 812a345 | 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 | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 265 | if (FD->isBitField()) |
| 266 | continue; |
| 267 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 268 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 269 | if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset)) |
| 270 | return false; |
| 271 | } |
| 272 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 276 | void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 277 | CharUnits Offset, |
Anders Carlsson | e3362bc | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 288 | AddSubobjectAtOffset(Info->Class, Offset); |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 289 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 290 | // Traverse all non-virtual bases. |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 291 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 292 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 293 | BaseSubobjectInfo* Base = Info->Bases[I]; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 294 | if (Base->IsVirtual) |
| 295 | continue; |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 296 | |
Anders Carlsson | 6a35674 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 297 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 298 | UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 301 | if (Info->PrimaryVirtualBaseInfo) { |
| 302 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 303 | |
| 304 | if (Info == PrimaryVirtualBaseInfo->Derived) |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 305 | UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset, |
| 306 | PlacingEmptyBase); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 307 | } |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 308 | |
Anders Carlsson | 812a345 | 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 | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 314 | if (FD->isBitField()) |
| 315 | continue; |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 316 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 317 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
| 318 | UpdateEmptyFieldSubobjects(FD, FieldOffset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 319 | } |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Anders Carlsson | 5b1319c | 2010-05-29 20:49:49 +0000 | [diff] [blame] | 322 | bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 323 | CharUnits Offset) { |
Anders Carlsson | 261febd | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 326 | if (SizeOfLargestEmptySubobject.isZero()) |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 327 | return true; |
| 328 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 329 | if (!CanPlaceBaseSubobjectAtOffset(Info, Offset)) |
| 330 | return false; |
Anders Carlsson | 812a345 | 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 | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 334 | UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty()); |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 338 | bool |
| 339 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 340 | const CXXRecordDecl *Class, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 341 | CharUnits Offset) const { |
Anders Carlsson | 2177ab7 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 344 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 345 | return true; |
| 346 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 347 | if (!CanPlaceSubobjectAtOffset(RD, Offset)) |
Anders Carlsson | 812a345 | 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 | 6a35674 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 361 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 362 | if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset)) |
| 363 | return false; |
| 364 | } |
| 365 | |
Anders Carlsson | 45f5b54 | 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 | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 373 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 45f5b54 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 374 | if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset)) |
| 375 | return false; |
| 376 | } |
| 377 | } |
| 378 | |
Anders Carlsson | 812a345 | 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 | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 384 | if (FD->isBitField()) |
| 385 | continue; |
| 386 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 387 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | 812a345 | 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 | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 396 | bool |
| 397 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
| 398 | CharUnits Offset) const { |
Anders Carlsson | 2177ab7 | 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 | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 401 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 402 | return true; |
| 403 | |
Anders Carlsson | 812a345 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 407 | return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset); |
Anders Carlsson | 812a345 | 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 | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 421 | CharUnits ElementOffset = Offset; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 422 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | 2177ab7 | 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 | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 425 | if (!AnyEmptySubobjectsBeyondOffset(ElementOffset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 426 | return true; |
| 427 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 428 | if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset)) |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 429 | return false; |
| 430 | |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 431 | ElementOffset += Layout.getSize(); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | bool |
Anders Carlsson | a3d4380 | 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 | 812a345 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 452 | CharUnits Offset) { |
Anders Carlsson | 5ccfdd8 | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 453 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | e3362bc | 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 | 5ccfdd8 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 461 | AddSubobjectAtOffset(RD, Offset); |
Anders Carlsson | 812a345 | 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 | 6a35674 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 474 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 475 | UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset); |
| 476 | } |
| 477 | |
Anders Carlsson | 45f5b54 | 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 | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 485 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 45f5b54 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 486 | UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset); |
| 487 | } |
| 488 | } |
| 489 | |
Anders Carlsson | 812a345 | 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 | fa84fba | 2010-11-01 15:14:51 +0000 | [diff] [blame] | 495 | if (FD->isBitField()) |
| 496 | continue; |
| 497 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 498 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 499 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 500 | UpdateEmptyFieldSubobjects(FD, FieldOffset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 505 | CharUnits Offset) { |
Anders Carlsson | 812a345 | 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 | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 524 | CharUnits ElementOffset = Offset; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 525 | |
| 526 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | 5ccfdd8 | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 527 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 528 | // field subobjects are subobjects of empty bases that can be placed at |
Anders Carlsson | 5ccfdd8 | 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 | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 535 | UpdateEmptyFieldSubobjects(RD, RD, ElementOffset); |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 536 | ElementOffset += Layout.getSize(); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 541 | class RecordLayoutBuilder { |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 542 | protected: |
Anders Carlsson | 9392fa6 | 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 | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 545 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 546 | const ASTContext &Context; |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 547 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 548 | EmptySubobjectMap *EmptySubobjects; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 550 | /// Size - The current size of the record layout. |
| 551 | uint64_t Size; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 552 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 553 | /// Alignment - The current alignment of the record layout. |
Ken Dyck | ea7f6c2 | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 554 | CharUnits Alignment; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 555 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 556 | /// \brief The alignment if attribute packed is not used. |
Ken Dyck | 6feb4bb | 2011-02-16 02:11:31 +0000 | [diff] [blame] | 557 | CharUnits UnpackedAlignment; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 559 | SmallVector<uint64_t, 16> FieldOffsets; |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 560 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 561 | /// \brief Whether the external AST source has provided a layout for this |
| 562 | /// record. |
| 563 | unsigned ExternalLayout : 1; |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 564 | |
| 565 | /// \brief Whether we need to infer alignment, even when we have an |
| 566 | /// externally-provided layout. |
| 567 | unsigned InferAlignment : 1; |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 568 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 569 | /// Packed - Whether the record is packed or not. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 570 | unsigned Packed : 1; |
| 571 | |
| 572 | unsigned IsUnion : 1; |
| 573 | |
| 574 | unsigned IsMac68kAlign : 1; |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 575 | |
| 576 | unsigned IsMsStruct : 1; |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 577 | |
| 578 | /// UnfilledBitsInLastByte - If the last field laid out was a bitfield, |
| 579 | /// this contains the number of bits in the last byte that can be used for |
| 580 | /// an adjacent bitfield if necessary. |
| 581 | unsigned char UnfilledBitsInLastByte; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 582 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 583 | /// MaxFieldAlignment - The maximum allowed field alignment. This is set by |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 584 | /// #pragma pack. |
Ken Dyck | 834945c | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 585 | CharUnits MaxFieldAlignment; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 586 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 587 | /// DataSize - The data size of the record being laid out. |
| 588 | uint64_t DataSize; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 589 | |
Ken Dyck | a1fdb0b | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 590 | CharUnits NonVirtualSize; |
Ken Dyck | df20538 | 2011-02-16 01:43:15 +0000 | [diff] [blame] | 591 | CharUnits NonVirtualAlignment; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 592 | |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 593 | FieldDecl *ZeroLengthBitfield; |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 594 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 595 | /// PrimaryBase - the primary base class (if one exists) of the class |
| 596 | /// we're laying out. |
| 597 | const CXXRecordDecl *PrimaryBase; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 598 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 599 | /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying |
| 600 | /// out is virtual. |
| 601 | bool PrimaryBaseIsVirtual; |
| 602 | |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 603 | /// VFPtrOffset - Virtual function table offset. Only for MS layout. |
| 604 | CharUnits VFPtrOffset; |
| 605 | |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 606 | /// VBPtrOffset - Virtual base table offset. Only for MS layout. |
| 607 | CharUnits VBPtrOffset; |
| 608 | |
Anders Carlsson | 376bda9 | 2010-10-31 21:01:46 +0000 | [diff] [blame] | 609 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 611 | /// Bases - base classes and their offsets in the record. |
| 612 | BaseOffsetsMapTy Bases; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 614 | // VBases - virtual base classes and their offsets in the record. |
| 615 | BaseOffsetsMapTy VBases; |
| 616 | |
| 617 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 618 | /// primary base classes for some other direct or indirect base class. |
Anders Carlsson | 245656e | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 619 | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 620 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 621 | /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in |
| 622 | /// inheritance graph order. Used for determining the primary base class. |
| 623 | const CXXRecordDecl *FirstNearlyEmptyVBase; |
| 624 | |
| 625 | /// VisitedVirtualBases - A set of all the visited virtual bases, used to |
| 626 | /// avoid visiting virtual bases more than once. |
| 627 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 628 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 629 | /// \brief Externally-provided size. |
| 630 | uint64_t ExternalSize; |
| 631 | |
| 632 | /// \brief Externally-provided alignment. |
| 633 | uint64_t ExternalAlign; |
| 634 | |
| 635 | /// \brief Externally-provided field offsets. |
| 636 | llvm::DenseMap<const FieldDecl *, uint64_t> ExternalFieldOffsets; |
| 637 | |
| 638 | /// \brief Externally-provided direct, non-virtual base offsets. |
| 639 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalBaseOffsets; |
| 640 | |
| 641 | /// \brief Externally-provided virtual base offsets. |
| 642 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalVirtualBaseOffsets; |
| 643 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 644 | RecordLayoutBuilder(const ASTContext &Context, |
| 645 | EmptySubobjectMap *EmptySubobjects) |
Ken Dyck | ea7f6c2 | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 646 | : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 647 | Alignment(CharUnits::One()), UnpackedAlignment(CharUnits::One()), |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 648 | ExternalLayout(false), InferAlignment(false), |
| 649 | Packed(false), IsUnion(false), IsMac68kAlign(false), IsMsStruct(false), |
Ken Dyck | 834945c | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 650 | UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()), |
| 651 | DataSize(0), NonVirtualSize(CharUnits::Zero()), |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 652 | NonVirtualAlignment(CharUnits::One()), |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 653 | ZeroLengthBitfield(0), PrimaryBase(0), |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 654 | PrimaryBaseIsVirtual(false), |
| 655 | VFPtrOffset(CharUnits::fromQuantity(-1)), |
| 656 | VBPtrOffset(CharUnits::fromQuantity(-1)), |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 657 | FirstNearlyEmptyVBase(0) { } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 658 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 659 | /// Reset this RecordLayoutBuilder to a fresh state, using the given |
| 660 | /// alignment as the initial alignment. This is used for the |
| 661 | /// correct layout of vb-table pointers in MSVC. |
| 662 | void resetWithTargetAlignment(CharUnits TargetAlignment) { |
| 663 | const ASTContext &Context = this->Context; |
| 664 | EmptySubobjectMap *EmptySubobjects = this->EmptySubobjects; |
| 665 | this->~RecordLayoutBuilder(); |
| 666 | new (this) RecordLayoutBuilder(Context, EmptySubobjects); |
| 667 | Alignment = UnpackedAlignment = TargetAlignment; |
| 668 | } |
| 669 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 670 | void Layout(const RecordDecl *D); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 671 | void Layout(const CXXRecordDecl *D); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 672 | void Layout(const ObjCInterfaceDecl *D); |
| 673 | |
| 674 | void LayoutFields(const RecordDecl *D); |
| 675 | void LayoutField(const FieldDecl *D); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 676 | void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize, |
| 677 | bool FieldPacked, const FieldDecl *D); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 678 | void LayoutBitField(const FieldDecl *D); |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 679 | |
| 680 | bool isMicrosoftCXXABI() const { |
| 681 | return Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft; |
| 682 | } |
| 683 | |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 684 | void MSLayoutVirtualBases(const CXXRecordDecl *RD); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 685 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 686 | /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects. |
| 687 | llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator; |
| 688 | |
| 689 | typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *> |
| 690 | BaseSubobjectInfoMapTy; |
| 691 | |
| 692 | /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases |
| 693 | /// of the class we're laying out to their base subobject info. |
| 694 | BaseSubobjectInfoMapTy VirtualBaseInfo; |
| 695 | |
| 696 | /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the |
| 697 | /// class we're laying out to their base subobject info. |
| 698 | BaseSubobjectInfoMapTy NonVirtualBaseInfo; |
| 699 | |
| 700 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for the |
| 701 | /// bases of the given class. |
| 702 | void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD); |
| 703 | |
| 704 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for a |
| 705 | /// single class and all of its base classes. |
| 706 | BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 707 | bool IsVirtual, |
| 708 | BaseSubobjectInfo *Derived); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 709 | |
| 710 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
| 711 | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
| 712 | |
| 713 | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 714 | |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 715 | void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign); |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 716 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 717 | /// LayoutNonVirtualBases - Determines the primary base class (if any) and |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 718 | /// lays it out. Will then proceed to lay out all non-virtual base clasess. |
| 719 | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
| 720 | |
| 721 | /// LayoutNonVirtualBase - Lays out a single non-virtual base. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 722 | void LayoutNonVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 723 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 724 | void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
| 725 | CharUnits Offset); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 726 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 727 | bool needsVFTable(const CXXRecordDecl *RD) const; |
| 728 | bool hasNewVirtualFunction(const CXXRecordDecl *RD) const; |
| 729 | bool isPossiblePrimaryBase(const CXXRecordDecl *Base) const; |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 731 | /// LayoutVirtualBases - Lays out all the virtual bases. |
| 732 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 733 | const CXXRecordDecl *MostDerivedClass); |
| 734 | |
| 735 | /// LayoutVirtualBase - Lays out a single virtual base. |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 736 | void LayoutVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 737 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 738 | /// LayoutBase - Will lay out a base and return the offset where it was |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 739 | /// placed, in chars. |
| 740 | CharUnits LayoutBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 741 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 742 | /// InitializeLayout - Initialize record layout for the given record decl. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 743 | void InitializeLayout(const Decl *D); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 744 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 745 | /// FinishLayout - Finalize record layout. Adjust record size based on the |
| 746 | /// alignment. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 747 | void FinishLayout(const NamedDecl *D); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 748 | |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 749 | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment); |
| 750 | void UpdateAlignment(CharUnits NewAlignment) { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 751 | UpdateAlignment(NewAlignment, NewAlignment); |
| 752 | } |
| 753 | |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 754 | /// \brief Retrieve the externally-supplied field offset for the given |
| 755 | /// field. |
| 756 | /// |
| 757 | /// \param Field The field whose offset is being queried. |
| 758 | /// \param ComputedOffset The offset that we've computed for this field. |
| 759 | uint64_t updateExternalFieldOffset(const FieldDecl *Field, |
| 760 | uint64_t ComputedOffset); |
| 761 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 762 | void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset, |
| 763 | uint64_t UnpackedOffset, unsigned UnpackedAlign, |
| 764 | bool isPacked, const FieldDecl *D); |
| 765 | |
| 766 | DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 767 | |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 768 | CharUnits getSize() const { |
Ken Dyck | 9911344 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 769 | assert(Size % Context.getCharWidth() == 0); |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 770 | return Context.toCharUnitsFromBits(Size); |
| 771 | } |
| 772 | uint64_t getSizeInBits() const { return Size; } |
| 773 | |
| 774 | void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); } |
| 775 | void setSize(uint64_t NewSize) { Size = NewSize; } |
| 776 | |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 777 | CharUnits getAligment() const { return Alignment; } |
| 778 | |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 779 | CharUnits getDataSize() const { |
Ken Dyck | 9911344 | 2011-02-24 01:33:05 +0000 | [diff] [blame] | 780 | assert(DataSize % Context.getCharWidth() == 0); |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 781 | return Context.toCharUnitsFromBits(DataSize); |
| 782 | } |
| 783 | uint64_t getDataSizeInBits() const { return DataSize; } |
| 784 | |
| 785 | void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); } |
| 786 | void setDataSize(uint64_t NewSize) { DataSize = NewSize; } |
| 787 | |
Argyrios Kyrtzidis | c83d2d7 | 2010-08-15 10:17:39 +0000 | [diff] [blame] | 788 | RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
| 789 | void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 790 | public: |
| 791 | static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD); |
| 792 | }; |
Benjamin Kramer | 7e22028 | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 793 | } // end anonymous namespace |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 794 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 795 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 796 | RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 797 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 798 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 799 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 800 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 801 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | const CXXRecordDecl *Base = |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 803 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 804 | |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 805 | // Check if this is a nearly empty virtual base. |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 806 | if (I->isVirtual() && Context.isNearlyEmpty(Base)) { |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 807 | // If it's not an indirect primary base, then we've found our primary |
| 808 | // base. |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 809 | if (!IndirectPrimaryBases.count(Base)) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 810 | PrimaryBase = Base; |
| 811 | PrimaryBaseIsVirtual = true; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 812 | return; |
| 813 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 814 | |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 815 | // Is this the first nearly empty virtual base? |
| 816 | if (!FirstNearlyEmptyVBase) |
| 817 | FirstNearlyEmptyVBase = Base; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 818 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 819 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 820 | SelectPrimaryVBase(Base); |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 821 | if (PrimaryBase) |
Zhongxing Xu | 94ba380 | 2010-02-15 04:28:35 +0000 | [diff] [blame] | 822 | return; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 826 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 827 | void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 828 | // If the class isn't dynamic, it won't have a primary base. |
| 829 | if (!RD->isDynamicClass()) |
| 830 | return; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 831 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 832 | // Compute all the primary virtual bases for all of our direct and |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 833 | // indirect bases, and record all their primary virtual base classes. |
Anders Carlsson | 245656e | 2010-11-24 22:55:48 +0000 | [diff] [blame] | 834 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 835 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 836 | // If the record has a dynamic base class, attempt to choose a primary base |
| 837 | // class. It is the first (in direct base class order) non-virtual dynamic |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 838 | // base class, if one exists. |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 839 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 840 | e = RD->bases_end(); i != e; ++i) { |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 841 | // Ignore virtual bases. |
| 842 | if (i->isVirtual()) |
| 843 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 844 | |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 845 | const CXXRecordDecl *Base = |
| 846 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 847 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 848 | if (isPossiblePrimaryBase(Base)) { |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 849 | // We found it. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 850 | PrimaryBase = Base; |
| 851 | PrimaryBaseIsVirtual = false; |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 852 | return; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 856 | // The Microsoft ABI doesn't have primary virtual bases. |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 857 | if (isMicrosoftCXXABI()) { |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 858 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | // Under the Itanium ABI, if there is no non-virtual primary base class, |
| 863 | // try to compute the primary virtual base. The primary virtual base is |
| 864 | // the first nearly empty virtual base that is not an indirect primary |
| 865 | // virtual base class, if one exists. |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 866 | if (RD->getNumVBases() != 0) { |
| 867 | SelectPrimaryVBase(RD); |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 868 | if (PrimaryBase) |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 869 | return; |
| 870 | } |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 871 | |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 872 | // Otherwise, it is the first indirect primary base class, if one exists. |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 873 | if (FirstNearlyEmptyVBase) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 874 | PrimaryBase = FirstNearlyEmptyVBase; |
| 875 | PrimaryBaseIsVirtual = true; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 876 | return; |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 877 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 878 | |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 879 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 882 | BaseSubobjectInfo * |
| 883 | RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 884 | bool IsVirtual, |
| 885 | BaseSubobjectInfo *Derived) { |
| 886 | BaseSubobjectInfo *Info; |
| 887 | |
| 888 | if (IsVirtual) { |
| 889 | // Check if we already have info about this virtual base. |
| 890 | BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD]; |
| 891 | if (InfoSlot) { |
| 892 | assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); |
| 893 | return InfoSlot; |
| 894 | } |
| 895 | |
| 896 | // We don't, create it. |
| 897 | InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 898 | Info = InfoSlot; |
| 899 | } else { |
| 900 | Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 901 | } |
| 902 | |
| 903 | Info->Class = RD; |
| 904 | Info->IsVirtual = IsVirtual; |
| 905 | Info->Derived = 0; |
| 906 | Info->PrimaryVirtualBaseInfo = 0; |
| 907 | |
| 908 | const CXXRecordDecl *PrimaryVirtualBase = 0; |
| 909 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0; |
| 910 | |
| 911 | // Check if this base has a primary virtual base. |
| 912 | if (RD->getNumVBases()) { |
| 913 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | c9e814b | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 914 | if (Layout.isPrimaryBaseVirtual()) { |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 915 | // This base does have a primary virtual base. |
| 916 | PrimaryVirtualBase = Layout.getPrimaryBase(); |
| 917 | assert(PrimaryVirtualBase && "Didn't have a primary virtual base!"); |
| 918 | |
| 919 | // Now check if we have base subobject info about this primary base. |
| 920 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 921 | |
| 922 | if (PrimaryVirtualBaseInfo) { |
| 923 | if (PrimaryVirtualBaseInfo->Derived) { |
| 924 | // We did have info about this primary base, and it turns out that it |
| 925 | // has already been claimed as a primary virtual base for another |
| 926 | // base. |
| 927 | PrimaryVirtualBase = 0; |
| 928 | } else { |
| 929 | // We can claim this base as our primary base. |
| 930 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 931 | PrimaryVirtualBaseInfo->Derived = Info; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | // Now go through all direct bases. |
| 938 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 939 | E = RD->bases_end(); I != E; ++I) { |
| 940 | bool IsVirtual = I->isVirtual(); |
| 941 | |
| 942 | const CXXRecordDecl *BaseDecl = |
| 943 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 944 | |
| 945 | Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info)); |
| 946 | } |
| 947 | |
| 948 | if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) { |
| 949 | // Traversing the bases must have created the base info for our primary |
| 950 | // virtual base. |
| 951 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 952 | assert(PrimaryVirtualBaseInfo && |
| 953 | "Did not create a primary virtual base!"); |
| 954 | |
| 955 | // Claim the primary virtual base as our primary virtual base. |
| 956 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 957 | PrimaryVirtualBaseInfo->Derived = Info; |
| 958 | } |
| 959 | |
| 960 | return Info; |
| 961 | } |
| 962 | |
| 963 | void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) { |
| 964 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 965 | E = RD->bases_end(); I != E; ++I) { |
| 966 | bool IsVirtual = I->isVirtual(); |
| 967 | |
| 968 | const CXXRecordDecl *BaseDecl = |
| 969 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 970 | |
| 971 | // Compute the base subobject info for this base. |
| 972 | BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0); |
| 973 | |
| 974 | if (IsVirtual) { |
| 975 | // ComputeBaseInfo has already added this base for us. |
| 976 | assert(VirtualBaseInfo.count(BaseDecl) && |
| 977 | "Did not add virtual base!"); |
| 978 | } else { |
| 979 | // Add the base info to the map of non-virtual bases. |
| 980 | assert(!NonVirtualBaseInfo.count(BaseDecl) && |
| 981 | "Non-virtual base already exists!"); |
| 982 | NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 987 | void |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 988 | RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) { |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 989 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
| 990 | |
| 991 | // The maximum field alignment overrides base align. |
| 992 | if (!MaxFieldAlignment.isZero()) { |
| 993 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 994 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
| 995 | } |
| 996 | |
| 997 | // Round up the current record size to pointer alignment. |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 998 | setSize(getSize().RoundUpToAlignment(BaseAlign)); |
| 999 | setDataSize(getSize()); |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Update the alignment. |
| 1002 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
| 1003 | } |
| 1004 | |
| 1005 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1006 | RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 1007 | // Then, determine the primary base class. |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1008 | DeterminePrimaryBase(RD); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1009 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 1010 | // Compute base subobject info. |
| 1011 | ComputeBaseSubobjectInfo(RD); |
| 1012 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1013 | // If we have a primary base class, lay it out. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1014 | if (PrimaryBase) { |
| 1015 | if (PrimaryBaseIsVirtual) { |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 1016 | // If the primary virtual base was a primary virtual base of some other |
| 1017 | // base class we'll have to steal it. |
| 1018 | BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase); |
| 1019 | PrimaryBaseInfo->Derived = 0; |
| 1020 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1021 | // We have a virtual primary base, insert it as an indirect primary base. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1022 | IndirectPrimaryBases.insert(PrimaryBase); |
Anders Carlsson | 37147ea | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1023 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1024 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1025 | "vbase already visited!"); |
| 1026 | VisitedVirtualBases.insert(PrimaryBase); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1027 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1028 | LayoutVirtualBase(PrimaryBaseInfo); |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | BaseSubobjectInfo *PrimaryBaseInfo = |
| 1031 | NonVirtualBaseInfo.lookup(PrimaryBase); |
| 1032 | assert(PrimaryBaseInfo && |
| 1033 | "Did not find base info for non-virtual primary base!"); |
| 1034 | |
| 1035 | LayoutNonVirtualBase(PrimaryBaseInfo); |
| 1036 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1037 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1038 | // If this class needs a vtable/vf-table and didn't get one from a |
| 1039 | // primary base, add it in now. |
| 1040 | } else if (needsVFTable(RD)) { |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1041 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1042 | CharUnits PtrWidth = |
| 1043 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1044 | CharUnits PtrAlign = |
| 1045 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
| 1046 | EnsureVTablePointerAlignment(PtrAlign); |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1047 | if (isMicrosoftCXXABI()) |
| 1048 | VFPtrOffset = getSize(); |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1049 | setSize(getSize() + PtrWidth); |
| 1050 | setDataSize(getSize()); |
| 1051 | } |
| 1052 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1053 | bool HasDirectVirtualBases = false; |
| 1054 | bool HasNonVirtualBaseWithVBTable = false; |
| 1055 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1056 | // Now lay out the non-virtual bases. |
| 1057 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1058 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1059 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1060 | // Ignore virtual bases, but remember that we saw one. |
| 1061 | if (I->isVirtual()) { |
| 1062 | HasDirectVirtualBases = true; |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1063 | continue; |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1064 | } |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1065 | |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1066 | const CXXRecordDecl *BaseDecl = |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1067 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1068 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1069 | // Remember if this base has virtual bases itself. |
| 1070 | if (BaseDecl->getNumVBases()) |
| 1071 | HasNonVirtualBaseWithVBTable = true; |
| 1072 | |
| 1073 | // Skip the primary base, because we've already laid it out. The |
| 1074 | // !PrimaryBaseIsVirtual check is required because we might have a |
| 1075 | // non-virtual base of the same type as a primary virtual base. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1076 | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual) |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1077 | continue; |
| 1078 | |
| 1079 | // Lay out the base. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1080 | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
| 1081 | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
| 1082 | |
| 1083 | LayoutNonVirtualBase(BaseInfo); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1084 | } |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1085 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1086 | // In the MS ABI, add the vb-table pointer if we need one, which is |
| 1087 | // whenever we have a virtual base and we can't re-use a vb-table |
| 1088 | // pointer from a non-virtual base. |
| 1089 | if (isMicrosoftCXXABI() && |
| 1090 | HasDirectVirtualBases && !HasNonVirtualBaseWithVBTable) { |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1091 | CharUnits PtrWidth = |
| 1092 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1093 | CharUnits PtrAlign = |
| 1094 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1095 | |
| 1096 | // MSVC potentially over-aligns the vb-table pointer by giving it |
| 1097 | // the max alignment of all the non-virtual objects in the class. |
| 1098 | // This is completely unnecessary, but we're not here to pass |
| 1099 | // judgment. |
| 1100 | // |
| 1101 | // Note that we've only laid out the non-virtual bases, so on the |
| 1102 | // first pass Alignment won't be set correctly here, but if the |
| 1103 | // vb-table doesn't end up aligned correctly we'll come through |
| 1104 | // and redo the layout from scratch with the right alignment. |
| 1105 | // |
| 1106 | // TODO: Instead of doing this, just lay out the fields as if the |
| 1107 | // vb-table were at offset zero, then retroactively bump the field |
| 1108 | // offsets up. |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 1109 | PtrAlign = std::max(PtrAlign, Alignment); |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1110 | |
| 1111 | EnsureVTablePointerAlignment(PtrAlign); |
| 1112 | VBPtrOffset = getSize(); |
| 1113 | setSize(getSize() + PtrWidth); |
| 1114 | setDataSize(getSize()); |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1115 | } |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1118 | void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1119 | // Layout the base. |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1120 | CharUnits Offset = LayoutBase(Base); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1121 | |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1122 | // Add its base class offset. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1123 | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1124 | Bases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1125 | |
| 1126 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1127 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1128 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1129 | void |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1130 | RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1131 | CharUnits Offset) { |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1132 | // This base isn't interesting, it has no virtual bases. |
| 1133 | if (!Info->Class->getNumVBases()) |
| 1134 | return; |
| 1135 | |
| 1136 | // First, check if we have a virtual primary base to add offsets for. |
| 1137 | if (Info->PrimaryVirtualBaseInfo) { |
| 1138 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
| 1139 | "Primary virtual base is not virtual!"); |
| 1140 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
| 1141 | // Add the offset. |
| 1142 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
| 1143 | "primary vbase offset already exists!"); |
| 1144 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1145 | Offset)); |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1146 | |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1147 | // Traverse the primary virtual base. |
| 1148 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
| 1149 | } |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1152 | // Now go through all direct non-virtual bases. |
| 1153 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
| 1154 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
| 1155 | const BaseSubobjectInfo *Base = Info->Bases[I]; |
| 1156 | if (Base->IsVirtual) |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1157 | continue; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1158 | |
Anders Carlsson | 6a35674 | 2010-11-01 00:21:58 +0000 | [diff] [blame] | 1159 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1160 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1164 | /// needsVFTable - Return true if this class needs a vtable or vf-table |
| 1165 | /// when laid out as a base class. These are treated the same because |
| 1166 | /// they're both always laid out at offset zero. |
| 1167 | /// |
| 1168 | /// This function assumes that the class has no primary base. |
| 1169 | bool RecordLayoutBuilder::needsVFTable(const CXXRecordDecl *RD) const { |
| 1170 | assert(!PrimaryBase); |
| 1171 | |
| 1172 | // In the Itanium ABI, every dynamic class needs a vtable: even if |
| 1173 | // this class has no virtual functions as a base class (i.e. it's |
| 1174 | // non-polymorphic or only has virtual functions from virtual |
| 1175 | // bases),x it still needs a vtable to locate its virtual bases. |
| 1176 | if (!isMicrosoftCXXABI()) |
| 1177 | return RD->isDynamicClass(); |
| 1178 | |
| 1179 | // In the MS ABI, we need a vfptr if the class has virtual functions |
| 1180 | // other than those declared by its virtual bases. The AST doesn't |
| 1181 | // tell us that directly, and checking manually for virtual |
| 1182 | // functions that aren't overrides is expensive, but there are |
| 1183 | // some important shortcuts: |
| 1184 | |
| 1185 | // - Non-polymorphic classes have no virtual functions at all. |
| 1186 | if (!RD->isPolymorphic()) return false; |
| 1187 | |
| 1188 | // - Polymorphic classes with no virtual bases must either declare |
| 1189 | // virtual functions directly or inherit them, but in the latter |
| 1190 | // case we would have a primary base. |
| 1191 | if (RD->getNumVBases() == 0) return true; |
| 1192 | |
| 1193 | return hasNewVirtualFunction(RD); |
| 1194 | } |
| 1195 | |
| 1196 | /// hasNewVirtualFunction - Does the given polymorphic class declare a |
| 1197 | /// virtual function that does not override a method from any of its |
| 1198 | /// base classes? |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1199 | bool |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1200 | RecordLayoutBuilder::hasNewVirtualFunction(const CXXRecordDecl *RD) const { |
| 1201 | assert(RD->isPolymorphic()); |
| 1202 | if (!RD->getNumBases()) |
| 1203 | return true; |
| 1204 | |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1205 | for (CXXRecordDecl::method_iterator method = RD->method_begin(); |
| 1206 | method != RD->method_end(); |
| 1207 | ++method) { |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1208 | if (method->isVirtual() && !method->size_overridden_methods()) { |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1209 | return true; |
| 1210 | } |
| 1211 | } |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1215 | /// isPossiblePrimaryBase - Is the given base class an acceptable |
| 1216 | /// primary base class? |
Eli Friedman | 97c0aef | 2011-10-18 00:55:28 +0000 | [diff] [blame] | 1217 | bool |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1218 | RecordLayoutBuilder::isPossiblePrimaryBase(const CXXRecordDecl *Base) const { |
| 1219 | // In the Itanium ABI, a class can be a primary base class if it has |
| 1220 | // a vtable for any reason. |
| 1221 | if (!isMicrosoftCXXABI()) |
| 1222 | return Base->isDynamicClass(); |
| 1223 | |
| 1224 | // In the MS ABI, a class can only be a primary base class if it |
| 1225 | // provides a vf-table at a static offset. That means it has to be |
| 1226 | // non-virtual base. The existence of a separate vb-table means |
| 1227 | // that it's possible to get virtual functions only from a virtual |
| 1228 | // base, which we have to guard against. |
| 1229 | |
| 1230 | // First off, it has to have virtual functions. |
| 1231 | if (!Base->isPolymorphic()) return false; |
| 1232 | |
| 1233 | // If it has no virtual bases, then everything is at a static offset. |
| 1234 | if (!Base->getNumVBases()) return true; |
| 1235 | |
| 1236 | // Okay, just ask the base class's layout. |
| 1237 | return (Context.getASTRecordLayout(Base).getVFPtrOffset() |
| 1238 | != CharUnits::fromQuantity(-1)); |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1241 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1242 | RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1243 | const CXXRecordDecl *MostDerivedClass) { |
Anders Carlsson | 88f4296 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1244 | const CXXRecordDecl *PrimaryBase; |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1245 | bool PrimaryBaseIsVirtual; |
Anders Carlsson | 37147ea | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1246 | |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1247 | if (MostDerivedClass == RD) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1248 | PrimaryBase = this->PrimaryBase; |
| 1249 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1250 | } else { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1251 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 88f4296 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1252 | PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | c9e814b | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 1253 | PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1256 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1257 | E = RD->bases_end(); I != E; ++I) { |
| 1258 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1259 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1260 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1261 | const CXXRecordDecl *BaseDecl = |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1262 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1263 | |
| 1264 | if (I->isVirtual()) { |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1265 | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) { |
| 1266 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1267 | |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1268 | // Only lay out the virtual base if it's not an indirect primary base. |
| 1269 | if (!IndirectPrimaryBase) { |
| 1270 | // Only visit virtual bases once. |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1271 | if (!VisitedVirtualBases.insert(BaseDecl)) |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1272 | continue; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1273 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1274 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1275 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1276 | LayoutVirtualBase(BaseInfo); |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 1277 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1278 | } |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 1279 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1280 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1281 | if (!BaseDecl->getNumVBases()) { |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1282 | // This base isn't interesting since it doesn't have any virtual bases. |
| 1283 | continue; |
Mike Stump | fe3010d | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 1284 | } |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1285 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1286 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1290 | void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) { |
| 1291 | |
| 1292 | if (!RD->getNumVBases()) |
| 1293 | return; |
| 1294 | |
| 1295 | // This is substantially simplified because there are no virtual |
| 1296 | // primary bases. |
| 1297 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1298 | E = RD->vbases_end(); I != E; ++I) { |
| 1299 | const CXXRecordDecl *BaseDecl = I->getType()->getAsCXXRecordDecl(); |
| 1300 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1301 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1302 | |
| 1303 | LayoutVirtualBase(BaseInfo); |
| 1304 | } |
| 1305 | } |
| 1306 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1307 | void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1308 | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
| 1309 | |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1310 | // Layout the base. |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1311 | CharUnits Offset = LayoutBase(Base); |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1312 | |
| 1313 | // Add its base class offset. |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1314 | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1315 | VBases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1316 | |
| 1317 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1320 | CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | b1d880b | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1321 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1322 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1323 | |
| 1324 | CharUnits Offset; |
| 1325 | |
| 1326 | // Query the external layout to see if it provides an offset. |
| 1327 | bool HasExternalLayout = false; |
| 1328 | if (ExternalLayout) { |
| 1329 | llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known; |
| 1330 | if (Base->IsVirtual) { |
| 1331 | Known = ExternalVirtualBaseOffsets.find(Base->Class); |
| 1332 | if (Known != ExternalVirtualBaseOffsets.end()) { |
| 1333 | Offset = Known->second; |
| 1334 | HasExternalLayout = true; |
| 1335 | } |
| 1336 | } else { |
| 1337 | Known = ExternalBaseOffsets.find(Base->Class); |
| 1338 | if (Known != ExternalBaseOffsets.end()) { |
| 1339 | Offset = Known->second; |
| 1340 | HasExternalLayout = true; |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1345 | // If we have an empty base class, try to place it at offset 0. |
Anders Carlsson | b1d880b | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1346 | if (Base->Class->isEmpty() && |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1347 | (!HasExternalLayout || Offset == CharUnits::Zero()) && |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1348 | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1349 | setSize(std::max(getSize(), Layout.getSize())); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1350 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1351 | return CharUnits::Zero(); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1352 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1353 | |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1354 | CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign(); |
| 1355 | CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign; |
Argyrios Kyrtzidis | 43ddd9f | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1356 | |
| 1357 | // The maximum field alignment overrides base align. |
Ken Dyck | 834945c | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1358 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1359 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
| 1360 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 43ddd9f | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1361 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1362 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1363 | if (!HasExternalLayout) { |
| 1364 | // Round up the current record size to the base's alignment boundary. |
| 1365 | Offset = getDataSize().RoundUpToAlignment(BaseAlign); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1366 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1367 | // Try to place the base. |
| 1368 | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset)) |
| 1369 | Offset += BaseAlign; |
| 1370 | } else { |
| 1371 | bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset); |
| 1372 | (void)Allowed; |
| 1373 | assert(Allowed && "Base subobject externally placed at overlapping offset"); |
| 1374 | } |
| 1375 | |
Anders Carlsson | b1d880b | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1376 | if (!Base->Class->isEmpty()) { |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1377 | // Update the data size. |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1378 | setDataSize(Offset + Layout.getNonVirtualSize()); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1379 | |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1380 | setSize(std::max(getSize(), getDataSize())); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1381 | } else |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1382 | setSize(std::max(getSize(), Offset + Layout.getSize())); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1383 | |
| 1384 | // Remember max struct/class alignment. |
Argyrios Kyrtzidis | 43ddd9f | 2010-12-09 00:35:20 +0000 | [diff] [blame] | 1385 | UpdateAlignment(BaseAlign, UnpackedBaseAlign); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1386 | |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1387 | return Offset; |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1390 | void RecordLayoutBuilder::InitializeLayout(const Decl *D) { |
| 1391 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 1392 | IsUnion = RD->isUnion(); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1393 | |
Anders Carlsson | a5dd722 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 1394 | Packed = D->hasAttr<PackedAttr>(); |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1395 | |
| 1396 | IsMsStruct = D->hasAttr<MsStructAttr>(); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1397 | |
Daniel Dunbar | 88934e8 | 2011-10-05 21:04:55 +0000 | [diff] [blame] | 1398 | // Honor the default struct packing maximum alignment flag. |
| 1399 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOptions().PackStruct) { |
| 1400 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
| 1401 | } |
| 1402 | |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1403 | // mac68k alignment supersedes maximum field alignment and attribute aligned, |
| 1404 | // and forces all structures to have 2-byte alignment. The IBM docs on it |
| 1405 | // allude to additional (more complicated) semantics, especially with regard |
| 1406 | // to bit-fields, but gcc appears not to follow that. |
| 1407 | if (D->hasAttr<AlignMac68kAttr>()) { |
| 1408 | IsMac68kAlign = true; |
Ken Dyck | 834945c | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1409 | MaxFieldAlignment = CharUnits::fromQuantity(2); |
Ken Dyck | ea7f6c2 | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1410 | Alignment = CharUnits::fromQuantity(2); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1411 | } else { |
| 1412 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
Ken Dyck | 834945c | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1413 | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1414 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1415 | if (unsigned MaxAlign = D->getMaxAlignment()) |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1416 | UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1417 | } |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1418 | |
| 1419 | // If there is an external AST source, ask it for the various offsets. |
| 1420 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 1421 | if (ExternalASTSource *External = Context.getExternalSource()) { |
| 1422 | ExternalLayout = External->layoutRecordType(RD, |
| 1423 | ExternalSize, |
| 1424 | ExternalAlign, |
| 1425 | ExternalFieldOffsets, |
| 1426 | ExternalBaseOffsets, |
| 1427 | ExternalVirtualBaseOffsets); |
| 1428 | |
| 1429 | // Update based on external alignment. |
| 1430 | if (ExternalLayout) { |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 1431 | if (ExternalAlign > 0) { |
| 1432 | Alignment = Context.toCharUnitsFromBits(ExternalAlign); |
| 1433 | UnpackedAlignment = Alignment; |
| 1434 | } else { |
| 1435 | // The external source didn't have alignment information; infer it. |
| 1436 | InferAlignment = true; |
| 1437 | } |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1440 | } |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 1441 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1442 | void RecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 1443 | InitializeLayout(D); |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1444 | LayoutFields(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1446 | // Finally, round the size of the total struct up to the alignment of the |
| 1447 | // struct itself. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1448 | FinishLayout(D); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
| 1452 | InitializeLayout(RD); |
| 1453 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1454 | // Lay out the vtable and the non-virtual bases. |
| 1455 | LayoutNonVirtualBases(RD); |
| 1456 | |
| 1457 | LayoutFields(RD); |
| 1458 | |
Ken Dyck | 90ce2db | 2011-03-10 01:53:59 +0000 | [diff] [blame] | 1459 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1460 | llvm::RoundUpToAlignment(getSizeInBits(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1461 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | ea7f6c2 | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 1462 | NonVirtualAlignment = Alignment; |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1463 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1464 | if (isMicrosoftCXXABI() && |
| 1465 | NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) { |
| 1466 | CharUnits AlignMember = |
| 1467 | NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize; |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1468 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1469 | setSize(getSize() + AlignMember); |
| 1470 | setDataSize(getSize()); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1471 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 1472 | NonVirtualSize = Context.toCharUnitsFromBits( |
| 1473 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 1474 | Context.getTargetInfo().getCharAlign())); |
| 1475 | |
| 1476 | MSLayoutVirtualBases(RD); |
| 1477 | |
| 1478 | } else { |
| 1479 | // Lay out the virtual bases and add the primary virtual base offsets. |
| 1480 | LayoutVirtualBases(RD, RD); |
| 1481 | } |
| 1482 | |
| 1483 | // Finally, round the size of the total struct up to the alignment |
Eli Friedman | 901dd66 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 1484 | // of the struct itself. |
| 1485 | FinishLayout(RD); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1486 | |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1487 | #ifndef NDEBUG |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1488 | // Check that we have base offsets for all bases. |
| 1489 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1490 | E = RD->bases_end(); I != E; ++I) { |
| 1491 | if (I->isVirtual()) |
| 1492 | continue; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1493 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1494 | const CXXRecordDecl *BaseDecl = |
| 1495 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1496 | |
| 1497 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 1498 | } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1499 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1500 | // And all virtual bases. |
| 1501 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1502 | E = RD->vbases_end(); I != E; ++I) { |
| 1503 | const CXXRecordDecl *BaseDecl = |
| 1504 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1505 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1506 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1507 | } |
| 1508 | #endif |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1511 | void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1512 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1513 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1514 | |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1515 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1517 | // We start laying out ivars not at the end of the superclass |
| 1518 | // structure, but at the next byte following the last field. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1519 | setSize(SL.getDataSize()); |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1520 | setDataSize(getSize()); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1523 | InitializeLayout(D); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1524 | // Layout each ivar sequentially. |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1525 | for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD; |
| 1526 | IVD = IVD->getNextIvar()) |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1527 | LayoutField(IVD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1529 | // Finally, round the size of the total struct up to the alignment of the |
| 1530 | // struct itself. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1531 | FinishLayout(D); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1534 | void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1535 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 1536 | // the future, this will need to be tweakable by targets. |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1537 | const FieldDecl *LastFD = 0; |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1538 | ZeroLengthBitfield = 0; |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1539 | unsigned RemainingInAlignment = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1541 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) { |
| 1542 | if (IsMsStruct) { |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1543 | FieldDecl *FD = (*Field); |
| 1544 | if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD)) |
| 1545 | ZeroLengthBitfield = FD; |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1546 | // Zero-length bitfields following non-bitfield members are |
| 1547 | // ignored: |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1548 | else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD)) |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1549 | continue; |
Fariborz Jahanian | 31e7f22 | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1550 | // FIXME. streamline these conditions into a simple one. |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1551 | else if (Context.BitfieldFollowsBitfield(FD, LastFD) || |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1552 | Context.BitfieldFollowsNonBitfield(FD, LastFD) || |
| 1553 | Context.NonBitfieldFollowsBitfield(FD, LastFD)) { |
Fariborz Jahanian | 31e7f22 | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1554 | // 1) Adjacent bit fields are packed into the same 1-, 2-, or |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1555 | // 4-byte allocation unit if the integral types are the same |
| 1556 | // size and if the next bit field fits into the current |
| 1557 | // allocation unit without crossing the boundary imposed by the |
| 1558 | // common alignment requirements of the bit fields. |
Fariborz Jahanian | 31e7f22 | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1559 | // 2) Establish a new alignment for a bitfield following |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1560 | // a non-bitfield if size of their types differ. |
Fariborz Jahanian | 31e7f22 | 2011-05-06 22:42:22 +0000 | [diff] [blame] | 1561 | // 3) Establish a new alignment for a non-bitfield following |
| 1562 | // a bitfield if size of their types differ. |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1563 | std::pair<uint64_t, unsigned> FieldInfo = |
| 1564 | Context.getTypeInfo(FD->getType()); |
| 1565 | uint64_t TypeSize = FieldInfo.first; |
| 1566 | unsigned FieldAlign = FieldInfo.second; |
Fariborz Jahanian | 30364d0 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1567 | // This check is needed for 'long long' in -m32 mode. |
Fariborz Jahanian | 364a59e | 2011-12-12 21:16:36 +0000 | [diff] [blame] | 1568 | if (TypeSize > FieldAlign && |
| 1569 | (Context.hasSameType(FD->getType(), |
| 1570 | Context.UnsignedLongLongTy) |
| 1571 | ||Context.hasSameType(FD->getType(), |
| 1572 | Context.LongLongTy))) |
Fariborz Jahanian | 30364d0 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1573 | FieldAlign = TypeSize; |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1574 | FieldInfo = Context.getTypeInfo(LastFD->getType()); |
| 1575 | uint64_t TypeSizeLastFD = FieldInfo.first; |
| 1576 | unsigned FieldAlignLastFD = FieldInfo.second; |
Fariborz Jahanian | 30364d0 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1577 | // This check is needed for 'long long' in -m32 mode. |
Fariborz Jahanian | 364a59e | 2011-12-12 21:16:36 +0000 | [diff] [blame] | 1578 | if (TypeSizeLastFD > FieldAlignLastFD && |
| 1579 | (Context.hasSameType(LastFD->getType(), |
| 1580 | Context.UnsignedLongLongTy) |
| 1581 | || Context.hasSameType(LastFD->getType(), |
| 1582 | Context.LongLongTy))) |
Fariborz Jahanian | 30364d0 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1583 | FieldAlignLastFD = TypeSizeLastFD; |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1584 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1585 | if (TypeSizeLastFD != TypeSize) { |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1586 | if (RemainingInAlignment && |
| 1587 | LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1588 | LastFD->getBitWidthValue(Context)) { |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1589 | // If previous field was a bitfield with some remaining unfilled |
| 1590 | // bits, pad the field so current field starts on its type boundary. |
| 1591 | uint64_t FieldOffset = |
| 1592 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1593 | uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset; |
| 1594 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1595 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1596 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1597 | RemainingInAlignment = 0; |
| 1598 | } |
| 1599 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1600 | uint64_t UnpaddedFieldOffset = |
| 1601 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1602 | FieldAlign = std::max(FieldAlign, FieldAlignLastFD); |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1603 | |
Fariborz Jahanian | ed63e03 | 2011-05-10 19:00:50 +0000 | [diff] [blame] | 1604 | // The maximum field alignment overrides the aligned attribute. |
| 1605 | if (!MaxFieldAlignment.isZero()) { |
| 1606 | unsigned MaxFieldAlignmentInBits = |
| 1607 | Context.toBits(MaxFieldAlignment); |
| 1608 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1609 | } |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1610 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1611 | uint64_t NewSizeInBits = |
| 1612 | llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign); |
| 1613 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1614 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1615 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
| 1616 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1617 | } |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1618 | if (FD->isBitField()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1619 | uint64_t FieldSize = FD->getBitWidthValue(Context); |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1620 | assert (FieldSize > 0 && "LayoutFields - ms_struct layout"); |
| 1621 | if (RemainingInAlignment < FieldSize) |
| 1622 | RemainingInAlignment = TypeSize - FieldSize; |
| 1623 | else |
| 1624 | RemainingInAlignment -= FieldSize; |
| 1625 | } |
| 1626 | } |
| 1627 | else if (FD->isBitField()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1628 | uint64_t FieldSize = FD->getBitWidthValue(Context); |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1629 | std::pair<uint64_t, unsigned> FieldInfo = |
| 1630 | Context.getTypeInfo(FD->getType()); |
| 1631 | uint64_t TypeSize = FieldInfo.first; |
| 1632 | RemainingInAlignment = TypeSize - FieldSize; |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1633 | } |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1634 | LastFD = FD; |
| 1635 | } |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1636 | else if (!Context.getTargetInfo().useBitFieldTypeAlignment() && |
| 1637 | Context.getTargetInfo().useZeroLengthBitfieldAlignment()) { |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1638 | FieldDecl *FD = (*Field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1639 | if (FD->isBitField() && FD->getBitWidthValue(Context) == 0) |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1640 | ZeroLengthBitfield = FD; |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1641 | } |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1642 | LayoutField(*Field); |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1643 | } |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1644 | if (IsMsStruct && RemainingInAlignment && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1645 | LastFD && LastFD->isBitField() && LastFD->getBitWidthValue(Context)) { |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1646 | // If we ended a bitfield before the full length of the type then |
| 1647 | // pad the struct out to the full length of the last type. |
| 1648 | uint64_t FieldOffset = |
| 1649 | getDataSizeInBits() - UnfilledBitsInLastByte; |
| 1650 | uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset; |
| 1651 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1652 | Context.getTargetInfo().getCharAlign())); |
Fariborz Jahanian | 6ec50ad | 2011-05-11 16:58:31 +0000 | [diff] [blame] | 1653 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
| 1654 | } |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1657 | void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1658 | uint64_t TypeSize, |
| 1659 | bool FieldPacked, |
| 1660 | const FieldDecl *D) { |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1661 | assert(Context.getLangOptions().CPlusPlus && |
| 1662 | "Can only have wide bit-fields in C++!"); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1663 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1664 | // Itanium C++ ABI 2.4: |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1665 | // If sizeof(T)*8 < n, let T' be the largest integral POD type with |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1666 | // sizeof(T')*8 <= n. |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1667 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1668 | QualType IntegralPODTypes[] = { |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1669 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1670 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
| 1671 | }; |
| 1672 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1673 | QualType Type; |
| 1674 | for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes); |
| 1675 | I != E; ++I) { |
| 1676 | uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1677 | |
| 1678 | if (Size > FieldSize) |
| 1679 | break; |
| 1680 | |
| 1681 | Type = IntegralPODTypes[I]; |
| 1682 | } |
| 1683 | assert(!Type.isNull() && "Did not find a type!"); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1684 | |
Ken Dyck | 3b3e1a1 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1685 | CharUnits TypeAlign = Context.getTypeAlignInChars(Type); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1686 | |
| 1687 | // We're not going to use any of the unfilled bits in the last byte. |
| 1688 | UnfilledBitsInLastByte = 0; |
| 1689 | |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1690 | uint64_t FieldOffset; |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1691 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1692 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1693 | if (IsUnion) { |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1694 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1695 | FieldOffset = 0; |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1696 | } else { |
Chad Rosier | b8fca90 | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1697 | // The bitfield is allocated starting at the next offset aligned |
| 1698 | // appropriately for T', with length n bits. |
Ken Dyck | 3b3e1a1 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1699 | FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(), |
| 1700 | Context.toBits(TypeAlign)); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1701 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1702 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1703 | |
Ken Dyck | d5e3ed0 | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 1704 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1705 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1706 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | // Place this field at the current location. |
| 1710 | FieldOffsets.push_back(FieldOffset); |
| 1711 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1712 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
Ken Dyck | 3b3e1a1 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1713 | Context.toBits(TypeAlign), FieldPacked, D); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1714 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1715 | // Update the size. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1716 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1717 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1718 | // Remember max struct/class alignment. |
Ken Dyck | 3b3e1a1 | 2011-03-01 01:36:00 +0000 | [diff] [blame] | 1719 | UpdateAlignment(TypeAlign); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1722 | void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1723 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1724 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1725 | uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1726 | uint64_t FieldSize = D->getBitWidthValue(Context); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1727 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1728 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1729 | uint64_t TypeSize = FieldInfo.first; |
| 1730 | unsigned FieldAlign = FieldInfo.second; |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1731 | |
Fariborz Jahanian | 30364d0 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1732 | // This check is needed for 'long long' in -m32 mode. |
Fariborz Jahanian | 364a59e | 2011-12-12 21:16:36 +0000 | [diff] [blame] | 1733 | if (IsMsStruct && (TypeSize > FieldAlign) && |
| 1734 | (Context.hasSameType(D->getType(), |
| 1735 | Context.UnsignedLongLongTy) |
| 1736 | || Context.hasSameType(D->getType(), Context.LongLongTy))) |
Fariborz Jahanian | 30364d0 | 2011-05-09 22:03:17 +0000 | [diff] [blame] | 1737 | FieldAlign = TypeSize; |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1738 | |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1739 | if (ZeroLengthBitfield) { |
Chad Rosier | b8fca90 | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1740 | std::pair<uint64_t, unsigned> FieldInfo; |
| 1741 | unsigned ZeroLengthBitfieldAlignment; |
| 1742 | if (IsMsStruct) { |
| 1743 | // If a zero-length bitfield is inserted after a bitfield, |
| 1744 | // and the alignment of the zero-length bitfield is |
| 1745 | // greater than the member that follows it, `bar', `bar' |
| 1746 | // will be aligned as the type of the zero-length bitfield. |
| 1747 | if (ZeroLengthBitfield != D) { |
| 1748 | FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType()); |
| 1749 | ZeroLengthBitfieldAlignment = FieldInfo.second; |
| 1750 | // Ignore alignment of subsequent zero-length bitfields. |
| 1751 | if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0)) |
| 1752 | FieldAlign = ZeroLengthBitfieldAlignment; |
| 1753 | if (FieldSize) |
| 1754 | ZeroLengthBitfield = 0; |
| 1755 | } |
| 1756 | } else { |
| 1757 | // The alignment of a zero-length bitfield affects the alignment |
| 1758 | // of the next member. The alignment is the max of the zero |
| 1759 | // length bitfield's alignment and a target specific fixed value. |
| 1760 | unsigned ZeroLengthBitfieldBoundary = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1761 | Context.getTargetInfo().getZeroLengthBitfieldBoundary(); |
Chad Rosier | b8fca90 | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1762 | if (ZeroLengthBitfieldBoundary > FieldAlign) |
| 1763 | FieldAlign = ZeroLengthBitfieldBoundary; |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1764 | } |
| 1765 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1766 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1767 | if (FieldSize > TypeSize) { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1768 | LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1769 | return; |
| 1770 | } |
| 1771 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1772 | // The align if the field is not packed. This is to check if the attribute |
| 1773 | // was unnecessary (-Wpacked). |
| 1774 | unsigned UnpackedFieldAlign = FieldAlign; |
| 1775 | uint64_t UnpackedFieldOffset = FieldOffset; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1776 | if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield) |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1777 | UnpackedFieldAlign = 1; |
| 1778 | |
Chad Rosier | b8fca90 | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1779 | if (FieldPacked || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1780 | (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)) |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1781 | FieldAlign = 1; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1782 | FieldAlign = std::max(FieldAlign, D->getMaxAlignment()); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1783 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1784 | |
| 1785 | // The maximum field alignment overrides the aligned attribute. |
Eli Friedman | bff22ac | 2011-12-02 02:38:48 +0000 | [diff] [blame] | 1786 | if (!MaxFieldAlignment.isZero() && FieldSize != 0) { |
Ken Dyck | 834945c | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1787 | unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); |
| 1788 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
| 1789 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1790 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 1792 | // Check if we need to add padding to give the field the correct alignment. |
| 1793 | if (FieldSize == 0 || |
| 1794 | (MaxFieldAlignment.isZero() && |
| 1795 | (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)) |
| 1796 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1797 | |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 1798 | if (FieldSize == 0 || |
| 1799 | (MaxFieldAlignment.isZero() && |
| 1800 | (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)) |
| 1801 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1802 | UnpackedFieldAlign); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1803 | |
Chad Rosier | b8fca90 | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1804 | // Padding members don't affect overall alignment, unless zero length bitfield |
| 1805 | // alignment is enabled. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1806 | if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment()) |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1807 | FieldAlign = UnpackedFieldAlign = 1; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1808 | |
Chad Rosier | b8fca90 | 2011-08-05 22:38:04 +0000 | [diff] [blame] | 1809 | if (!IsMsStruct) |
| 1810 | ZeroLengthBitfield = 0; |
| 1811 | |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 1812 | if (ExternalLayout) |
| 1813 | FieldOffset = updateExternalFieldOffset(D, FieldOffset); |
| 1814 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1815 | // Place this field at the current location. |
| 1816 | FieldOffsets.push_back(FieldOffset); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1818 | if (!ExternalLayout) |
| 1819 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
| 1820 | UnpackedFieldAlign, FieldPacked, D); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1821 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1822 | // Update DataSize to include the last byte containing (part of) the bitfield. |
| 1823 | if (IsUnion) { |
| 1824 | // FIXME: I think FieldSize should be TypeSize here. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1825 | setDataSize(std::max(getDataSizeInBits(), FieldSize)); |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1826 | } else { |
| 1827 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1828 | |
Ken Dyck | d5e3ed0 | 2011-03-10 02:00:35 +0000 | [diff] [blame] | 1829 | setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1830 | Context.getTargetInfo().getCharAlign())); |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1831 | UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits; |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1832 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1833 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1834 | // Update the size. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1835 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1836 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1837 | // Remember max struct/class alignment. |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 1838 | UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), |
| 1839 | Context.toCharUnitsFromBits(UnpackedFieldAlign)); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1842 | void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1843 | if (D->isBitField()) { |
| 1844 | LayoutBitField(D); |
| 1845 | return; |
| 1846 | } |
| 1847 | |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1848 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1849 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1850 | // Reset the unfilled bits. |
| 1851 | UnfilledBitsInLastByte = 0; |
| 1852 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1853 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1854 | CharUnits FieldOffset = |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1855 | IsUnion ? CharUnits::Zero() : getDataSize(); |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1856 | CharUnits FieldSize; |
| 1857 | CharUnits FieldAlign; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1858 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1859 | if (D->getType()->isIncompleteArrayType()) { |
| 1860 | // This is a flexible array member; we can't directly |
| 1861 | // query getTypeInfo about these, so we figure it out here. |
| 1862 | // Flexible array members don't have any size, but they |
| 1863 | // have to be aligned appropriately for their element type. |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1864 | FieldSize = CharUnits::Zero(); |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1865 | const ArrayType* ATy = Context.getAsArrayType(D->getType()); |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1866 | FieldAlign = Context.getTypeAlignInChars(ATy->getElementType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1867 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 1868 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1869 | FieldSize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1870 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS)); |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1871 | FieldAlign = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1872 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS)); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1873 | } else { |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1874 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 1875 | Context.getTypeInfoInChars(D->getType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1876 | FieldSize = FieldInfo.first; |
| 1877 | FieldAlign = FieldInfo.second; |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1878 | |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1879 | if (ZeroLengthBitfield) { |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1880 | CharUnits ZeroLengthBitfieldBoundary = |
| 1881 | Context.toCharUnitsFromBits( |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1882 | Context.getTargetInfo().getZeroLengthBitfieldBoundary()); |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1883 | if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) { |
| 1884 | // If a zero-length bitfield is inserted after a bitfield, |
| 1885 | // and the alignment of the zero-length bitfield is |
| 1886 | // greater than the member that follows it, `bar', `bar' |
| 1887 | // will be aligned as the type of the zero-length bitfield. |
| 1888 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 1889 | Context.getTypeInfoInChars(ZeroLengthBitfield->getType()); |
| 1890 | CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second; |
| 1891 | if (ZeroLengthBitfieldAlignment > FieldAlign) |
| 1892 | FieldAlign = ZeroLengthBitfieldAlignment; |
Chad Rosier | 0e7bf40 | 2011-08-04 19:25:14 +0000 | [diff] [blame] | 1893 | } else if (ZeroLengthBitfieldBoundary > FieldAlign) { |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1894 | // Align 'bar' based on a fixed alignment specified by the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1895 | assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
Chad Rosier | 6e43f3f | 2011-08-04 17:52:43 +0000 | [diff] [blame] | 1896 | "ZeroLengthBitfieldBoundary should only be used in conjunction" |
| 1897 | " with useZeroLengthBitfieldAlignment."); |
Chad Rosier | 61a6221 | 2011-08-04 01:21:14 +0000 | [diff] [blame] | 1898 | FieldAlign = ZeroLengthBitfieldBoundary; |
| 1899 | } |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 1900 | ZeroLengthBitfield = 0; |
| 1901 | } |
Douglas Gregor | 6f75550 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1902 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1903 | if (Context.getLangOptions().MSBitfields || IsMsStruct) { |
Douglas Gregor | 6f75550 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1904 | // If MS bitfield layout is required, figure out what type is being |
| 1905 | // laid out and align the field to the width of that type. |
| 1906 | |
| 1907 | // Resolve all typedefs down to their base type and round up the field |
| 1908 | // alignment if necessary. |
| 1909 | QualType T = Context.getBaseElementType(D->getType()); |
| 1910 | if (const BuiltinType *BTy = T->getAs<BuiltinType>()) { |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1911 | CharUnits TypeSize = Context.getTypeSizeInChars(BTy); |
Douglas Gregor | 6f75550 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1912 | if (TypeSize > FieldAlign) |
| 1913 | FieldAlign = TypeSize; |
| 1914 | } |
| 1915 | } |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1916 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1918 | // The align if the field is not packed. This is to check if the attribute |
| 1919 | // was unnecessary (-Wpacked). |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1920 | CharUnits UnpackedFieldAlign = FieldAlign; |
| 1921 | CharUnits UnpackedFieldOffset = FieldOffset; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1922 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1923 | if (FieldPacked) |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1924 | FieldAlign = CharUnits::One(); |
| 1925 | CharUnits MaxAlignmentInChars = |
| 1926 | Context.toCharUnitsFromBits(D->getMaxAlignment()); |
| 1927 | FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); |
| 1928 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1929 | |
| 1930 | // The maximum field alignment overrides the aligned attribute. |
Ken Dyck | 834945c | 2011-02-17 01:49:42 +0000 | [diff] [blame] | 1931 | if (!MaxFieldAlignment.isZero()) { |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1932 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 1933 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1934 | } |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1935 | |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 1936 | // Round up the current record size to the field's alignment boundary. |
| 1937 | FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign); |
| 1938 | UnpackedFieldOffset = |
| 1939 | UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign); |
| 1940 | |
| 1941 | if (ExternalLayout) { |
| 1942 | FieldOffset = Context.toCharUnitsFromBits( |
| 1943 | updateExternalFieldOffset(D, Context.toBits(FieldOffset))); |
| 1944 | |
| 1945 | if (!IsUnion && EmptySubobjects) { |
| 1946 | // Record the fact that we're placing a field at this offset. |
| 1947 | bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset); |
| 1948 | (void)Allowed; |
| 1949 | assert(Allowed && "Externally-placed field cannot be placed here"); |
| 1950 | } |
| 1951 | } else { |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1952 | if (!IsUnion && EmptySubobjects) { |
| 1953 | // Check if we can place the field at this offset. |
| 1954 | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { |
| 1955 | // We couldn't place the field at the offset. Try again at a new offset. |
| 1956 | FieldOffset += FieldAlign; |
| 1957 | } |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1958 | } |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1959 | } |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1960 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1961 | // Place this field at the current location. |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1962 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1964 | if (!ExternalLayout) |
| 1965 | CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, |
| 1966 | Context.toBits(UnpackedFieldOffset), |
| 1967 | Context.toBits(UnpackedFieldAlign), FieldPacked, D); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1968 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1969 | // Reserve space for this field. |
Eli Friedman | cd7a21b | 2012-01-12 23:27:03 +0000 | [diff] [blame] | 1970 | uint64_t FieldSizeInBits = Context.toBits(FieldSize); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1971 | if (IsUnion) |
Eli Friedman | 83be12c | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1972 | setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits)); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1973 | else |
Eli Friedman | 83be12c | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1974 | setDataSize(FieldOffset + FieldSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Eli Friedman | 83be12c | 2012-01-12 23:48:56 +0000 | [diff] [blame] | 1976 | // Update the size. |
| 1977 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1979 | // Remember max struct/class alignment. |
Ken Dyck | 9ed9a25 | 2011-02-20 02:06:09 +0000 | [diff] [blame] | 1980 | UpdateAlignment(FieldAlign, UnpackedFieldAlign); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1983 | void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 1984 | if (ExternalLayout) { |
| 1985 | setSize(ExternalSize); |
| 1986 | return; |
| 1987 | } |
| 1988 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1989 | // In C++, records cannot be of size 0. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 1990 | if (Context.getLangOptions().CPlusPlus && getSizeInBits() == 0) { |
Fariborz Jahanian | adf082e | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1991 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 1992 | // Compatibility with gcc requires a class (pod or non-pod) |
| 1993 | // which is not empty but of size 0; such as having fields of |
| 1994 | // array of zero-length, remains of Size 0 |
| 1995 | if (RD->isEmpty()) |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1996 | setSize(CharUnits::One()); |
Fariborz Jahanian | adf082e | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 1997 | } |
| 1998 | else |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 1999 | setSize(CharUnits::One()); |
Fariborz Jahanian | adf082e | 2011-02-02 19:36:18 +0000 | [diff] [blame] | 2000 | } |
Eli Friedman | 901dd66 | 2011-12-01 00:37:01 +0000 | [diff] [blame] | 2001 | |
| 2002 | // MSVC doesn't round up to the alignment of the record with virtual bases. |
| 2003 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 2004 | if (isMicrosoftCXXABI() && RD->getNumVBases()) |
| 2005 | return; |
| 2006 | } |
| 2007 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2008 | // Finally, round the size of the record up to the alignment of the |
| 2009 | // record itself. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2010 | uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte; |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2011 | uint64_t UnpackedSizeInBits = |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2012 | llvm::RoundUpToAlignment(getSizeInBits(), |
| 2013 | Context.toBits(UnpackedAlignment)); |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2014 | CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits); |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2015 | setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment))); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2016 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2017 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2018 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 2019 | // Warn if padding was introduced to the struct/class/union. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2020 | if (getSizeInBits() > UnpaddedSize) { |
| 2021 | unsigned PadSize = getSizeInBits() - UnpaddedSize; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2022 | bool InBits = true; |
| 2023 | if (PadSize % CharBitNum == 0) { |
| 2024 | PadSize = PadSize / CharBitNum; |
| 2025 | InBits = false; |
| 2026 | } |
| 2027 | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
| 2028 | << Context.getTypeDeclType(RD) |
| 2029 | << PadSize |
| 2030 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 2031 | } |
| 2032 | |
| 2033 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 2034 | // bother since there won't be alignment issues. |
Ken Dyck | a0c21c4 | 2011-02-24 01:13:28 +0000 | [diff] [blame] | 2035 | if (Packed && UnpackedAlignment > CharUnits::One() && |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2036 | getSize() == UnpackedSize) |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2037 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 2038 | << Context.getTypeDeclType(RD); |
| 2039 | } |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2042 | void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment, |
| 2043 | CharUnits UnpackedNewAlignment) { |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2044 | // The alignment is not modified when using 'mac68k' alignment or when |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 2045 | // we have an externally-supplied layout that also provides overall alignment. |
| 2046 | if (IsMac68kAlign || (ExternalLayout && !InferAlignment)) |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 2047 | return; |
| 2048 | |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2049 | if (NewAlignment > Alignment) { |
| 2050 | assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() && |
| 2051 | "Alignment not a power of 2")); |
| 2052 | Alignment = NewAlignment; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2055 | if (UnpackedNewAlignment > UnpackedAlignment) { |
| 2056 | assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() && |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2057 | "Alignment not a power of 2")); |
Ken Dyck | 3263e09 | 2011-02-19 18:58:07 +0000 | [diff] [blame] | 2058 | UnpackedAlignment = UnpackedNewAlignment; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | |
Douglas Gregor | 394f7b6 | 2012-01-28 00:53:29 +0000 | [diff] [blame^] | 2062 | uint64_t |
| 2063 | RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field, |
| 2064 | uint64_t ComputedOffset) { |
| 2065 | assert(ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() && |
| 2066 | "Field does not have an external offset"); |
| 2067 | |
| 2068 | uint64_t ExternalFieldOffset = ExternalFieldOffsets[Field]; |
| 2069 | |
| 2070 | if (InferAlignment && ExternalFieldOffset < ComputedOffset) { |
| 2071 | // The externally-supplied field offset is before the field offset we |
| 2072 | // computed. Assume that the structure is packed. |
| 2073 | Alignment = CharUnits::fromQuantity(1); |
| 2074 | InferAlignment = false; |
| 2075 | } |
| 2076 | |
| 2077 | // Use the externally-supplied field offset. |
| 2078 | return ExternalFieldOffset; |
| 2079 | } |
| 2080 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2081 | void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset, |
| 2082 | uint64_t UnpaddedOffset, |
| 2083 | uint64_t UnpackedOffset, |
| 2084 | unsigned UnpackedAlign, |
| 2085 | bool isPacked, |
| 2086 | const FieldDecl *D) { |
| 2087 | // We let objc ivars without warning, objc interfaces generally are not used |
| 2088 | // for padding tricks. |
| 2089 | if (isa<ObjCIvarDecl>(D)) |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2090 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Ted Kremenek | ae5860e | 2011-09-06 19:40:45 +0000 | [diff] [blame] | 2092 | // Don't warn about structs created without a SourceLocation. This can |
| 2093 | // be done by clients of the AST, such as codegen. |
| 2094 | if (D->getLocation().isInvalid()) |
| 2095 | return; |
| 2096 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2097 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2099 | // Warn if padding was introduced to the struct/class. |
| 2100 | if (!IsUnion && Offset > UnpaddedOffset) { |
| 2101 | unsigned PadSize = Offset - UnpaddedOffset; |
| 2102 | bool InBits = true; |
| 2103 | if (PadSize % CharBitNum == 0) { |
| 2104 | PadSize = PadSize / CharBitNum; |
| 2105 | InBits = false; |
| 2106 | } |
| 2107 | if (D->getIdentifier()) |
| 2108 | Diag(D->getLocation(), diag::warn_padded_struct_field) |
| 2109 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 2110 | << Context.getTypeDeclType(D->getParent()) |
| 2111 | << PadSize |
| 2112 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not |
| 2113 | << D->getIdentifier(); |
| 2114 | else |
| 2115 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
| 2116 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 2117 | << Context.getTypeDeclType(D->getParent()) |
| 2118 | << PadSize |
| 2119 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 2120 | } |
| 2121 | |
| 2122 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 2123 | // bother since there won't be alignment issues. |
| 2124 | if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset) |
| 2125 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 2126 | << D->getIdentifier(); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 2127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 2129 | const CXXMethodDecl * |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 2130 | RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2131 | // If a class isn't polymorphic it doesn't have a key function. |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 2132 | if (!RD->isPolymorphic()) |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2133 | return 0; |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 2134 | |
Eli Friedman | cb5d2d0 | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 2135 | // A class that is not externally visible doesn't have a key function. (Or |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 2136 | // at least, there's no point to assigning a key function to such a class; |
| 2137 | // this doesn't affect the ABI.) |
Eli Friedman | cb5d2d0 | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 2138 | if (RD->getLinkage() != ExternalLinkage) |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 2139 | return 0; |
| 2140 | |
Argyrios Kyrtzidis | 3bd5b6c | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 2141 | // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6. |
| 2142 | // Same behavior as GCC. |
| 2143 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 2144 | if (TSK == TSK_ImplicitInstantiation || |
| 2145 | TSK == TSK_ExplicitInstantiationDefinition) |
| 2146 | return 0; |
| 2147 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2148 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 2149 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2150 | const CXXMethodDecl *MD = *I; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2151 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2152 | if (!MD->isVirtual()) |
| 2153 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2154 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2155 | if (MD->isPure()) |
| 2156 | continue; |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 2157 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2158 | // Ignore implicit member functions, they are always marked as inline, but |
| 2159 | // they don't have a body until they're defined. |
| 2160 | if (MD->isImplicit()) |
| 2161 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 2163 | if (MD->isInlineSpecified()) |
| 2164 | continue; |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 2165 | |
| 2166 | if (MD->hasInlineBody()) |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2167 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2168 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2169 | // We found it. |
| 2170 | return MD; |
| 2171 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2172 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2173 | return 0; |
| 2174 | } |
| 2175 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2176 | DiagnosticBuilder |
| 2177 | RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 2178 | return Context.getDiagnostics().Report(Loc, DiagID); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2181 | /// getASTRecordLayout - Get or compute information about the layout of the |
| 2182 | /// specified record (struct/union/class), which indicates its size and field |
| 2183 | /// position information. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2184 | const ASTRecordLayout & |
| 2185 | ASTContext::getASTRecordLayout(const RecordDecl *D) const { |
John McCall | 6595935 | 2011-10-07 02:39:22 +0000 | [diff] [blame] | 2186 | // These asserts test different things. A record has a definition |
| 2187 | // as soon as we begin to parse the definition. That definition is |
| 2188 | // not a complete definition (which is what isDefinition() tests) |
| 2189 | // until we *finish* parsing the definition. |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2190 | D = D->getDefinition(); |
| 2191 | assert(D && "Cannot get layout of forward declarations!"); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2192 | assert(D->isCompleteDefinition() && "Cannot layout type before complete!"); |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2193 | |
| 2194 | // Look up this layout, if already laid out, return what we have. |
| 2195 | // Note that we can't save a reference to the entry because this function |
| 2196 | // is recursive. |
| 2197 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
| 2198 | if (Entry) return *Entry; |
| 2199 | |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2200 | const ASTRecordLayout *NewEntry; |
| 2201 | |
| 2202 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2203 | EmptySubobjectMap EmptySubobjects(*this, RD); |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2204 | RecordLayoutBuilder Builder(*this, &EmptySubobjects); |
| 2205 | Builder.Layout(RD); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 2206 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2207 | // MSVC gives the vb-table pointer an alignment equal to that of |
| 2208 | // the non-virtual part of the structure. That's an inherently |
| 2209 | // multi-pass operation. If our first pass doesn't give us |
| 2210 | // adequate alignment, try again with the specified minimum |
| 2211 | // alignment. This is *much* more maintainable than computing the |
| 2212 | // alignment in advance in a separately-coded pass; it's also |
| 2213 | // significantly more efficient in the common case where the |
| 2214 | // vb-table doesn't need extra padding. |
| 2215 | if (Builder.VBPtrOffset != CharUnits::fromQuantity(-1) && |
| 2216 | (Builder.VBPtrOffset % Builder.NonVirtualAlignment) != 0) { |
| 2217 | Builder.resetWithTargetAlignment(Builder.NonVirtualAlignment); |
| 2218 | Builder.Layout(RD); |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2221 | // FIXME: This is not always correct. See the part about bitfields at |
| 2222 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 2223 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2224 | // This does not affect the calculations of MSVC layouts |
| 2225 | bool IsPODForThePurposeOfLayout = |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2226 | (!Builder.isMicrosoftCXXABI() && cast<CXXRecordDecl>(D)->isPOD()); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2227 | |
| 2228 | // FIXME: This should be done in FinalizeLayout. |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2229 | CharUnits DataSize = |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2230 | IsPODForThePurposeOfLayout ? Builder.getSize() : Builder.getDataSize(); |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2231 | CharUnits NonVirtualSize = |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2232 | IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2233 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2234 | NewEntry = |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2235 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
| 2236 | Builder.Alignment, |
| 2237 | Builder.VFPtrOffset, |
| 2238 | Builder.VBPtrOffset, |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2239 | DataSize, |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2240 | Builder.FieldOffsets.data(), |
| 2241 | Builder.FieldOffsets.size(), |
Ken Dyck | a1fdb0b | 2011-02-16 01:52:01 +0000 | [diff] [blame] | 2242 | NonVirtualSize, |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2243 | Builder.NonVirtualAlignment, |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 2244 | EmptySubobjects.SizeOfLargestEmptySubobject, |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2245 | Builder.PrimaryBase, |
| 2246 | Builder.PrimaryBaseIsVirtual, |
| 2247 | Builder.Bases, Builder.VBases); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2248 | } else { |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2249 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2250 | Builder.Layout(D); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2251 | |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2252 | NewEntry = |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2253 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | ea7f6c2 | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2254 | Builder.Alignment, |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2255 | Builder.getSize(), |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 2256 | Builder.FieldOffsets.data(), |
| 2257 | Builder.FieldOffsets.size()); |
| 2258 | } |
| 2259 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2260 | ASTRecordLayouts[D] = NewEntry; |
| 2261 | |
| 2262 | if (getLangOptions().DumpRecordLayouts) { |
| 2263 | llvm::errs() << "\n*** Dumping AST Record Layout\n"; |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2264 | DumpRecordLayout(D, llvm::errs(), getLangOptions().DumpRecordLayoutsSimple); |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | return *NewEntry; |
| 2268 | } |
| 2269 | |
| 2270 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
| 2271 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
| 2272 | assert(RD && "Cannot get key function for forward declarations!"); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2273 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2274 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2275 | if (!Entry) |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 2276 | Entry = RecordLayoutBuilder::ComputeKeyFunction(RD); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2277 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2278 | return Entry; |
| 2279 | } |
| 2280 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2281 | static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) { |
| 2282 | const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent()); |
| 2283 | return Layout.getFieldOffset(FD->getFieldIndex()); |
| 2284 | } |
| 2285 | |
| 2286 | uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const { |
| 2287 | uint64_t OffsetInBits; |
| 2288 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
| 2289 | OffsetInBits = ::getFieldOffset(*this, FD); |
| 2290 | } else { |
| 2291 | const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD); |
| 2292 | |
| 2293 | OffsetInBits = 0; |
| 2294 | for (IndirectFieldDecl::chain_iterator CI = IFD->chain_begin(), |
| 2295 | CE = IFD->chain_end(); |
| 2296 | CI != CE; ++CI) |
| 2297 | OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(*CI)); |
| 2298 | } |
| 2299 | |
| 2300 | return OffsetInBits; |
| 2301 | } |
| 2302 | |
Eric Christopher | 68395a7 | 2011-10-05 06:00:51 +0000 | [diff] [blame] | 2303 | /// getObjCLayout - Get or compute information about the layout of the |
| 2304 | /// given interface. |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2305 | /// |
| 2306 | /// \param Impl - If given, also include the layout of the interface's |
| 2307 | /// implementation. This may differ by including synthesized ivars. |
| 2308 | const ASTRecordLayout & |
| 2309 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2310 | const ObjCImplementationDecl *Impl) const { |
Douglas Gregor | e7aa27a | 2011-12-20 15:50:13 +0000 | [diff] [blame] | 2311 | // Retrieve the definition |
| 2312 | D = D->getDefinition(); |
| 2313 | assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!"); |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2314 | |
| 2315 | // Look up this layout, if already laid out, return what we have. |
| 2316 | ObjCContainerDecl *Key = |
| 2317 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 2318 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 2319 | return *Entry; |
| 2320 | |
| 2321 | // Add in synthesized ivar count if laying out an implementation. |
| 2322 | if (Impl) { |
| 2323 | unsigned SynthCount = CountNonClassIvars(D); |
| 2324 | // If there aren't any sythesized ivars then reuse the interface |
| 2325 | // entry. Note we can't cache this because we simply free all |
| 2326 | // entries later; however we shouldn't look up implementations |
| 2327 | // frequently. |
| 2328 | if (SynthCount == 0) |
| 2329 | return getObjCLayout(D, 0); |
| 2330 | } |
| 2331 | |
John McCall | 9da2352 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 2332 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 36cdc61 | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2333 | Builder.Layout(D); |
| 2334 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2335 | const ASTRecordLayout *NewEntry = |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2336 | new (*this) ASTRecordLayout(*this, Builder.getSize(), |
Ken Dyck | ea7f6c2 | 2011-02-16 02:05:21 +0000 | [diff] [blame] | 2337 | Builder.Alignment, |
Ken Dyck | f079b73 | 2011-02-28 02:01:38 +0000 | [diff] [blame] | 2338 | Builder.getDataSize(), |
Anders Carlsson | 36cdc61 | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 2339 | Builder.FieldOffsets.data(), |
| 2340 | Builder.FieldOffsets.size()); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 2341 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 2342 | ObjCLayouts[Key] = NewEntry; |
| 2343 | |
| 2344 | return *NewEntry; |
| 2345 | } |
| 2346 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2347 | static void PrintOffset(raw_ostream &OS, |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2348 | CharUnits Offset, unsigned IndentLevel) { |
Benjamin Kramer | e4ebbf7 | 2011-11-05 09:02:52 +0000 | [diff] [blame] | 2349 | OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2350 | OS.indent(IndentLevel * 2); |
| 2351 | } |
| 2352 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2353 | static void DumpCXXRecordLayout(raw_ostream &OS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2354 | const CXXRecordDecl *RD, const ASTContext &C, |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2355 | CharUnits Offset, |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2356 | unsigned IndentLevel, |
| 2357 | const char* Description, |
| 2358 | bool IncludeVirtualBases) { |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2359 | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2360 | |
| 2361 | PrintOffset(OS, Offset, IndentLevel); |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2362 | OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2363 | if (Description) |
| 2364 | OS << ' ' << Description; |
| 2365 | if (RD->isEmpty()) |
| 2366 | OS << " (empty)"; |
| 2367 | OS << '\n'; |
| 2368 | |
| 2369 | IndentLevel++; |
| 2370 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2371 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 2372 | bool HasVfptr = Layout.getVFPtrOffset() != CharUnits::fromQuantity(-1); |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2373 | bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Vtable pointer. |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 2376 | if (RD->isDynamicClass() && !PrimaryBase && |
| 2377 | C.getTargetInfo().getCXXABI() != CXXABI_Microsoft) { |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2378 | PrintOffset(OS, Offset, IndentLevel); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2379 | OS << '(' << *RD << " vtable pointer)\n"; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2380 | } |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2381 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2382 | // Dump (non-virtual) bases |
| 2383 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 2384 | E = RD->bases_end(); I != E; ++I) { |
| 2385 | assert(!I->getType()->isDependentType() && |
| 2386 | "Cannot layout class with dependent bases."); |
| 2387 | if (I->isVirtual()) |
| 2388 | continue; |
| 2389 | |
| 2390 | const CXXRecordDecl *Base = |
| 2391 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2392 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2393 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2394 | |
| 2395 | DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
| 2396 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 2397 | /*IncludeVirtualBases=*/false); |
| 2398 | } |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 2399 | |
| 2400 | // vfptr and vbptr (for Microsoft C++ ABI) |
| 2401 | if (HasVfptr) { |
| 2402 | PrintOffset(OS, Offset + Layout.getVFPtrOffset(), IndentLevel); |
| 2403 | OS << '(' << *RD << " vftable pointer)\n"; |
| 2404 | } |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2405 | if (HasVbptr) { |
| 2406 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2407 | OS << '(' << *RD << " vbtable pointer)\n"; |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 2408 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2409 | |
| 2410 | // Dump fields. |
| 2411 | uint64_t FieldNo = 0; |
| 2412 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 2413 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
| 2414 | const FieldDecl *Field = *I; |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2415 | CharUnits FieldOffset = Offset + |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 2416 | C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo)); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2417 | |
| 2418 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
| 2419 | if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2420 | DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2421 | Field->getName().data(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2422 | /*IncludeVirtualBases=*/true); |
| 2423 | continue; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | PrintOffset(OS, FieldOffset, IndentLevel); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2428 | OS << Field->getType().getAsString() << ' ' << *Field << '\n'; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | if (!IncludeVirtualBases) |
| 2432 | return; |
| 2433 | |
| 2434 | // Dump virtual bases. |
| 2435 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 2436 | E = RD->vbases_end(); I != E; ++I) { |
| 2437 | assert(I->isVirtual() && "Found non-virtual class!"); |
| 2438 | const CXXRecordDecl *VBase = |
| 2439 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2440 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 2441 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2442 | DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
| 2443 | VBase == PrimaryBase ? |
| 2444 | "(primary virtual base)" : "(virtual base)", |
| 2445 | /*IncludeVirtualBases=*/false); |
| 2446 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2447 | |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2448 | OS << " sizeof=" << Layout.getSize().getQuantity(); |
Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 2449 | OS << ", dsize=" << Layout.getDataSize().getQuantity(); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2450 | OS << ", align=" << Layout.getAlignment().getQuantity() << '\n'; |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 2451 | OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity(); |
Ken Dyck | 68cf1a5 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 2452 | OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n'; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 2453 | OS << '\n'; |
| 2454 | } |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2455 | |
| 2456 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2457 | raw_ostream &OS, |
| 2458 | bool Simple) const { |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2459 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 2460 | |
| 2461 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2462 | if (!Simple) |
| 2463 | return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0, |
| 2464 | /*IncludeVirtualBases=*/true); |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2465 | |
| 2466 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
Douglas Gregor | 453dbcb | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 2467 | if (!Simple) { |
| 2468 | OS << "Record: "; |
| 2469 | RD->dump(); |
| 2470 | } |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2471 | OS << "\nLayout: "; |
| 2472 | OS << "<ASTRecordLayout\n"; |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2473 | OS << " Size:" << toBits(Info.getSize()) << "\n"; |
Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 2474 | OS << " DataSize:" << toBits(Info.getDataSize()) << "\n"; |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2475 | OS << " Alignment:" << toBits(Info.getAlignment()) << "\n"; |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 2476 | OS << " FieldOffsets: ["; |
| 2477 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
| 2478 | if (i) OS << ", "; |
| 2479 | OS << Info.getFieldOffset(i); |
| 2480 | } |
| 2481 | OS << "]>\n"; |
| 2482 | } |