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