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