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