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" |
| 11 | #include "clang/AST/Decl.h" |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 12 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 14 | #include "clang/AST/Expr.h" |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 15 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaDiagnostic.h" |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
| 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/Support/MathExtras.h" |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 21 | #include <map> |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Benjamin Kramer | 7e22028 | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 25 | namespace { |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 26 | |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 27 | /// BaseSubobjectInfo - Represents a single base subobject in a complete class. |
| 28 | /// For a class hierarchy like |
| 29 | /// |
| 30 | /// class A { }; |
| 31 | /// class B : A { }; |
| 32 | /// class C : A, B { }; |
| 33 | /// |
| 34 | /// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo |
| 35 | /// instances, one for B and two for A. |
| 36 | /// |
| 37 | /// If a base is virtual, it will only have one BaseSubobjectInfo allocated. |
| 38 | struct BaseSubobjectInfo { |
| 39 | /// Class - The class for this base info. |
Anders Carlsson | 4a25799 | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 40 | const CXXRecordDecl *Class; |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 41 | |
| 42 | /// IsVirtual - Whether the BaseInfo represents a virtual base or not. |
Anders Carlsson | 4a25799 | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 43 | bool IsVirtual; |
| 44 | |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 45 | /// Bases - Information about the base subobjects. |
| 46 | llvm::SmallVector<BaseSubobjectInfo*, 4> Bases; |
| 47 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 48 | /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base |
| 49 | /// of this base info (if one exists). |
| 50 | BaseSubobjectInfo *PrimaryVirtualBaseInfo; |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 51 | |
| 52 | // FIXME: Document. |
| 53 | const BaseSubobjectInfo *Derived; |
Anders Carlsson | 4a25799 | 2010-05-28 21:13:31 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 56 | /// EmptySubobjectMap - Keeps track of which empty subobjects exist at different |
| 57 | /// offsets while laying out a C++ class. |
| 58 | class EmptySubobjectMap { |
| 59 | ASTContext &Context; |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 60 | uint64_t CharWidth; |
| 61 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 62 | /// Class - The class whose empty entries we're keeping track of. |
| 63 | const CXXRecordDecl *Class; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 64 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 65 | /// EmptyClassOffsets - A map from offsets to empty record decls. |
| 66 | typedef llvm::SmallVector<const CXXRecordDecl *, 1> ClassVectorTy; |
Anders Carlsson | d8da763 | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 67 | typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 68 | EmptyClassOffsetsMapTy EmptyClassOffsets; |
| 69 | |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 70 | /// MaxEmptyClassOffset - The highest offset known to contain an empty |
| 71 | /// base subobject. |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 72 | CharUnits MaxEmptyClassOffset; |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 73 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 74 | /// ComputeEmptySubobjectSizes - Compute the size of the largest base or |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 75 | /// member subobject that is empty. |
| 76 | void ComputeEmptySubobjectSizes(); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 77 | |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 78 | void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 79 | |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 80 | void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 81 | CharUnits Offset, bool PlacingEmptyBase); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 82 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 83 | void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
| 84 | const CXXRecordDecl *Class, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 85 | CharUnits Offset); |
| 86 | void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 87 | |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 88 | /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty |
| 89 | /// subobjects beyond the given offset. |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 90 | bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const { |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 91 | return Offset <= MaxEmptyClassOffset; |
| 92 | } |
| 93 | |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 94 | CharUnits |
| 95 | getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const { |
| 96 | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); |
| 97 | assert(FieldOffset % CharWidth == 0 && |
| 98 | "Field offset not at char boundary!"); |
| 99 | |
| 100 | return CharUnits::fromQuantity(FieldOffset / CharWidth); |
| 101 | } |
| 102 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 103 | // FIXME: Remove this. |
Anders Carlsson | d8da763 | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 104 | CharUnits toCharUnits(uint64_t Offset) const { |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 105 | return CharUnits::fromQuantity(Offset / CharWidth); |
Anders Carlsson | d8da763 | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 106 | } |
Anders Carlsson | d8da763 | 2010-10-31 21:22:43 +0000 | [diff] [blame] | 107 | |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 108 | protected: |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 109 | bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
| 110 | CharUnits Offset) const; |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 111 | |
| 112 | bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 113 | CharUnits Offset); |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 114 | |
| 115 | bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 116 | const CXXRecordDecl *Class, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 117 | CharUnits Offset) const; |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 118 | bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 119 | CharUnits Offset) const; |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 120 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 121 | public: |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 122 | /// This holds the size of the largest empty subobject (either a base |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 123 | /// 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] | 124 | /// any empty classes. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 125 | CharUnits SizeOfLargestEmptySubobject; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 126 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 127 | EmptySubobjectMap(ASTContext &Context, const CXXRecordDecl *Class) |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 128 | : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 129 | ComputeEmptySubobjectSizes(); |
| 130 | } |
| 131 | |
| 132 | /// CanPlaceBaseAtOffset - Return whether the given base class can be placed |
| 133 | /// at the given offset. |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 134 | /// Returns false if placing the record will result in two components |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 135 | /// (direct or indirect) of the same type having the same offset. |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 136 | bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 137 | CharUnits Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 138 | |
| 139 | /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given |
| 140 | /// offset. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 141 | bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset); |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 142 | }; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 143 | |
| 144 | void EmptySubobjectMap::ComputeEmptySubobjectSizes() { |
| 145 | // Check the bases. |
| 146 | for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), |
| 147 | E = Class->bases_end(); I != E; ++I) { |
| 148 | const CXXRecordDecl *BaseDecl = |
| 149 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 150 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 151 | CharUnits EmptySize; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 152 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
| 153 | if (BaseDecl->isEmpty()) { |
| 154 | // If the class decl is empty, get its size. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 155 | EmptySize = toCharUnits(Layout.getSize()); |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 156 | } else { |
| 157 | // Otherwise, we get the largest empty subobject for the decl. |
| 158 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 159 | } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 160 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 161 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 162 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 163 | } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 164 | |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 165 | // Check the fields. |
| 166 | for (CXXRecordDecl::field_iterator I = Class->field_begin(), |
| 167 | E = Class->field_end(); I != E; ++I) { |
| 168 | const FieldDecl *FD = *I; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 169 | |
| 170 | const RecordType *RT = |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 171 | Context.getBaseElementType(FD->getType())->getAs<RecordType>(); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 172 | |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 173 | // We only care about record types. |
| 174 | if (!RT) |
| 175 | continue; |
| 176 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 177 | CharUnits EmptySize; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 178 | const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 179 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl); |
| 180 | if (MemberDecl->isEmpty()) { |
| 181 | // If the class decl is empty, get its size. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 182 | EmptySize = toCharUnits(Layout.getSize()); |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 183 | } else { |
| 184 | // Otherwise, we get the largest empty subobject for the decl. |
| 185 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
| 186 | } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 187 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 188 | if (EmptySize > SizeOfLargestEmptySubobject) |
| 189 | SizeOfLargestEmptySubobject = EmptySize; |
Anders Carlsson | 0c54fc9 | 2010-05-26 15:54:25 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 193 | bool |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 194 | EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 195 | CharUnits Offset) const { |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 196 | // We only need to check empty bases. |
| 197 | if (!RD->isEmpty()) |
| 198 | return true; |
| 199 | |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 200 | EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 201 | if (I == EmptyClassOffsets.end()) |
| 202 | return true; |
| 203 | |
| 204 | const ClassVectorTy& Classes = I->second; |
| 205 | if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end()) |
| 206 | return true; |
| 207 | |
| 208 | // There is already an empty class of the same type at this offset. |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD, |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 213 | CharUnits Offset) { |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 214 | // We only care about empty bases. |
| 215 | if (!RD->isEmpty()) |
| 216 | return; |
| 217 | |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 218 | ClassVectorTy& Classes = EmptyClassOffsets[Offset]; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 219 | assert(std::find(Classes.begin(), Classes.end(), RD) == Classes.end() && |
| 220 | "Duplicate empty class detected!"); |
Anders Carlsson | 45f5b54 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 221 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 222 | Classes.push_back(RD); |
Anders Carlsson | c8cb462 | 2010-06-08 15:56:03 +0000 | [diff] [blame] | 223 | |
| 224 | // Update the empty class offset. |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 225 | if (Offset > MaxEmptyClassOffset) |
| 226 | MaxEmptyClassOffset = Offset; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | bool |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 230 | EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
| 231 | CharUnits Offset) { |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 232 | // We don't have to keep looking past the maximum offset that's known to |
| 233 | // contain an empty class. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 234 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 235 | return true; |
| 236 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 237 | if (!CanPlaceSubobjectAtOffset(Info->Class, Offset)) |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 238 | return false; |
| 239 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 240 | // Traverse all non-virtual bases. |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 241 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 242 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 243 | BaseSubobjectInfo* Base = Info->Bases[I]; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 244 | if (Base->IsVirtual) |
| 245 | continue; |
| 246 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 247 | CharUnits BaseOffset = |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 248 | Offset + toCharUnits(Layout.getBaseClassOffsetInBits(Base->Class)); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 249 | |
| 250 | if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset)) |
| 251 | return false; |
| 252 | } |
| 253 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 254 | if (Info->PrimaryVirtualBaseInfo) { |
| 255 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 256 | |
| 257 | if (Info == PrimaryVirtualBaseInfo->Derived) { |
| 258 | if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset)) |
| 259 | return false; |
| 260 | } |
| 261 | } |
| 262 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 263 | // Traverse all member variables. |
| 264 | unsigned FieldNo = 0; |
| 265 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
| 266 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
| 267 | const FieldDecl *FD = *I; |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 268 | if (FD->isBitField()) |
| 269 | continue; |
| 270 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 271 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 272 | if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset)) |
| 273 | return false; |
| 274 | } |
| 275 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 279 | void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 280 | CharUnits Offset, |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 281 | bool PlacingEmptyBase) { |
| 282 | if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) { |
| 283 | // We know that the only empty subobjects that can conflict with empty |
| 284 | // subobject of non-empty bases, are empty bases that can be placed at |
| 285 | // offset zero. Because of this, we only need to keep track of empty base |
| 286 | // subobjects with offsets less than the size of the largest empty |
| 287 | // subobject for our class. |
| 288 | return; |
| 289 | } |
| 290 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 291 | AddSubobjectAtOffset(Info->Class, Offset); |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 292 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 293 | // Traverse all non-virtual bases. |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 294 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 295 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
Anders Carlsson | ea2f41c | 2010-05-28 21:24:37 +0000 | [diff] [blame] | 296 | BaseSubobjectInfo* Base = Info->Bases[I]; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 297 | if (Base->IsVirtual) |
| 298 | continue; |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 299 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 300 | CharUnits BaseOffset = |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 301 | Offset + toCharUnits(Layout.getBaseClassOffsetInBits(Base->Class)); |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 302 | UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 305 | if (Info->PrimaryVirtualBaseInfo) { |
| 306 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 307 | |
| 308 | if (Info == PrimaryVirtualBaseInfo->Derived) |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 309 | UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset, |
| 310 | PlacingEmptyBase); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 311 | } |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 312 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 313 | // Traverse all member variables. |
| 314 | unsigned FieldNo = 0; |
| 315 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
| 316 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
| 317 | const FieldDecl *FD = *I; |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 318 | if (FD->isBitField()) |
| 319 | continue; |
Anders Carlsson | 4137d51 | 2010-05-29 21:10:24 +0000 | [diff] [blame] | 320 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 321 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
| 322 | UpdateEmptyFieldSubobjects(FD, FieldOffset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 323 | } |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Anders Carlsson | 5b1319c | 2010-05-29 20:49:49 +0000 | [diff] [blame] | 326 | bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 327 | CharUnits Offset) { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 328 | // If we know this class doesn't have any empty subobjects we don't need to |
| 329 | // bother checking. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 330 | if (SizeOfLargestEmptySubobject.isZero()) |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 331 | return true; |
| 332 | |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 333 | if (!CanPlaceBaseSubobjectAtOffset(Info, Offset)) |
| 334 | return false; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 335 | |
| 336 | // We are able to place the base at this offset. Make sure to update the |
| 337 | // empty base subobject map. |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 338 | UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty()); |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 342 | bool |
| 343 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
| 344 | const CXXRecordDecl *Class, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 345 | CharUnits Offset) const { |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 346 | // We don't have to keep looking past the maximum offset that's known to |
| 347 | // contain an empty class. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 348 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 349 | return true; |
| 350 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 351 | if (!CanPlaceSubobjectAtOffset(RD, Offset)) |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 352 | return false; |
| 353 | |
| 354 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 355 | |
| 356 | // Traverse all non-virtual bases. |
| 357 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 358 | E = RD->bases_end(); I != E; ++I) { |
| 359 | if (I->isVirtual()) |
| 360 | continue; |
| 361 | |
| 362 | const CXXRecordDecl *BaseDecl = |
| 363 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 364 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 365 | CharUnits BaseOffset = |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 366 | Offset + toCharUnits(Layout.getBaseClassOffsetInBits(BaseDecl)); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 367 | if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset)) |
| 368 | return false; |
| 369 | } |
| 370 | |
Anders Carlsson | 45f5b54 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 371 | if (RD == Class) { |
| 372 | // This is the most derived class, traverse virtual bases as well. |
| 373 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 374 | E = RD->vbases_end(); I != E; ++I) { |
| 375 | const CXXRecordDecl *VBaseDecl = |
| 376 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 377 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 378 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 45f5b54 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 379 | if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset)) |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 384 | // Traverse all member variables. |
| 385 | unsigned FieldNo = 0; |
| 386 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 387 | I != E; ++I, ++FieldNo) { |
| 388 | const FieldDecl *FD = *I; |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 389 | if (FD->isBitField()) |
| 390 | continue; |
| 391 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 392 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 393 | |
| 394 | if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset)) |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 401 | bool |
| 402 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
| 403 | CharUnits Offset) const { |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 404 | // We don't have to keep looking past the maximum offset that's known to |
| 405 | // contain an empty class. |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 406 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 407 | return true; |
| 408 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 409 | QualType T = FD->getType(); |
| 410 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 411 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 412 | return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // If we have an array type we need to look at every element. |
| 416 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 417 | QualType ElemTy = Context.getBaseElementType(AT); |
| 418 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 419 | if (!RT) |
| 420 | return true; |
| 421 | |
| 422 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 423 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 424 | |
| 425 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 426 | CharUnits ElementOffset = Offset; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 427 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 428 | // We don't have to keep looking past the maximum offset that's known to |
| 429 | // contain an empty class. |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 430 | if (!AnyEmptySubobjectsBeyondOffset(ElementOffset)) |
Anders Carlsson | 2177ab7 | 2010-06-08 16:20:35 +0000 | [diff] [blame] | 431 | return true; |
| 432 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 433 | if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset)) |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 434 | return false; |
| 435 | |
Anders Carlsson | 8c6acc6 | 2010-10-31 21:54:55 +0000 | [diff] [blame] | 436 | ElementOffset += toCharUnits(Layout.getSize()); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | bool |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 444 | EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD, |
| 445 | CharUnits Offset) { |
| 446 | if (!CanPlaceFieldSubobjectAtOffset(FD, Offset)) |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 447 | return false; |
| 448 | |
| 449 | // We are able to place the member variable at this offset. |
| 450 | // Make sure to update the empty base subobject map. |
| 451 | UpdateEmptyFieldSubobjects(FD, Offset); |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
| 456 | const CXXRecordDecl *Class, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 457 | CharUnits Offset) { |
Anders Carlsson | 5ccfdd8 | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 458 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 459 | // 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] | 460 | // zero. Because of this, we only need to keep track of empty field |
| 461 | // subobjects with offsets less than the size of the largest empty |
| 462 | // subobject for our class. |
| 463 | if (Offset >= SizeOfLargestEmptySubobject) |
| 464 | return; |
| 465 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 466 | AddSubobjectAtOffset(RD, Offset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 467 | |
| 468 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 469 | |
| 470 | // Traverse all non-virtual bases. |
| 471 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 472 | E = RD->bases_end(); I != E; ++I) { |
| 473 | if (I->isVirtual()) |
| 474 | continue; |
| 475 | |
| 476 | const CXXRecordDecl *BaseDecl = |
| 477 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 478 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 479 | CharUnits BaseOffset = |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 480 | Offset + toCharUnits(Layout.getBaseClassOffsetInBits(BaseDecl)); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 481 | UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset); |
| 482 | } |
| 483 | |
Anders Carlsson | 45f5b54 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 484 | if (RD == Class) { |
| 485 | // This is the most derived class, traverse virtual bases as well. |
| 486 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 487 | E = RD->vbases_end(); I != E; ++I) { |
| 488 | const CXXRecordDecl *VBaseDecl = |
| 489 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 490 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 491 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
Anders Carlsson | 45f5b54 | 2010-06-08 19:09:24 +0000 | [diff] [blame] | 492 | UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset); |
| 493 | } |
| 494 | } |
| 495 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 496 | // Traverse all member variables. |
| 497 | unsigned FieldNo = 0; |
| 498 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 499 | I != E; ++I, ++FieldNo) { |
| 500 | const FieldDecl *FD = *I; |
| 501 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 502 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 503 | |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 504 | UpdateEmptyFieldSubobjects(FD, FieldOffset); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 509 | CharUnits Offset) { |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 510 | QualType T = FD->getType(); |
| 511 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 512 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 513 | UpdateEmptyFieldSubobjects(RD, RD, Offset); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | // If we have an array type we need to update every element. |
| 518 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
| 519 | QualType ElemTy = Context.getBaseElementType(AT); |
| 520 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 521 | if (!RT) |
| 522 | return; |
| 523 | |
| 524 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 525 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 526 | |
| 527 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 528 | CharUnits ElementOffset = Offset; |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 529 | |
| 530 | for (uint64_t I = 0; I != NumElements; ++I) { |
Anders Carlsson | 5ccfdd8 | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 531 | // We know that the only empty subobjects that can conflict with empty |
Anders Carlsson | e3362bc | 2010-06-13 18:00:18 +0000 | [diff] [blame] | 532 | // field subobjects are subobjects of empty bases that can be placed at |
Anders Carlsson | 5ccfdd8 | 2010-06-13 17:49:16 +0000 | [diff] [blame] | 533 | // offset zero. Because of this, we only need to keep track of empty field |
| 534 | // subobjects with offsets less than the size of the largest empty |
| 535 | // subobject for our class. |
| 536 | if (ElementOffset >= SizeOfLargestEmptySubobject) |
| 537 | return; |
| 538 | |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 539 | UpdateEmptyFieldSubobjects(RD, RD, ElementOffset); |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 540 | ElementOffset += toCharUnits(Layout.getSize()); |
Anders Carlsson | 812a345 | 2010-05-27 18:20:57 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 545 | class RecordLayoutBuilder { |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 546 | protected: |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 547 | // FIXME: Remove this and make the appropriate fields public. |
| 548 | friend class clang::ASTContext; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 550 | ASTContext &Context; |
| 551 | |
Anders Carlsson | 6a91c03 | 2010-05-26 15:32:58 +0000 | [diff] [blame] | 552 | EmptySubobjectMap *EmptySubobjects; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 553 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 554 | /// Size - The current size of the record layout. |
| 555 | uint64_t Size; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 556 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 557 | /// Alignment - The current alignment of the record layout. |
| 558 | unsigned Alignment; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 559 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 560 | /// \brief The alignment if attribute packed is not used. |
| 561 | unsigned UnpackedAlignment; |
| 562 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 563 | llvm::SmallVector<uint64_t, 16> FieldOffsets; |
| 564 | |
| 565 | /// Packed - Whether the record is packed or not. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 566 | unsigned Packed : 1; |
| 567 | |
| 568 | unsigned IsUnion : 1; |
| 569 | |
| 570 | unsigned IsMac68kAlign : 1; |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 571 | |
| 572 | /// UnfilledBitsInLastByte - If the last field laid out was a bitfield, |
| 573 | /// this contains the number of bits in the last byte that can be used for |
| 574 | /// an adjacent bitfield if necessary. |
| 575 | unsigned char UnfilledBitsInLastByte; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 576 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 577 | /// MaxFieldAlignment - The maximum allowed field alignment. This is set by |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 578 | /// #pragma pack. |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 579 | unsigned MaxFieldAlignment; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 580 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 581 | /// DataSize - The data size of the record being laid out. |
| 582 | uint64_t DataSize; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 583 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 584 | uint64_t NonVirtualSize; |
| 585 | unsigned NonVirtualAlignment; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 586 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 587 | /// PrimaryBase - the primary base class (if one exists) of the class |
| 588 | /// we're laying out. |
| 589 | const CXXRecordDecl *PrimaryBase; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 590 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 591 | /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying |
| 592 | /// out is virtual. |
| 593 | bool PrimaryBaseIsVirtual; |
| 594 | |
Anders Carlsson | 376bda9 | 2010-10-31 21:01:46 +0000 | [diff] [blame] | 595 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 596 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 597 | /// Bases - base classes and their offsets in the record. |
| 598 | BaseOffsetsMapTy Bases; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 599 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 600 | // VBases - virtual base classes and their offsets in the record. |
| 601 | BaseOffsetsMapTy VBases; |
| 602 | |
| 603 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 604 | /// primary base classes for some other direct or indirect base class. |
| 605 | llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 606 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 607 | /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in |
| 608 | /// inheritance graph order. Used for determining the primary base class. |
| 609 | const CXXRecordDecl *FirstNearlyEmptyVBase; |
| 610 | |
| 611 | /// VisitedVirtualBases - A set of all the visited virtual bases, used to |
| 612 | /// avoid visiting virtual bases more than once. |
| 613 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 614 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 615 | RecordLayoutBuilder(ASTContext &Context, EmptySubobjectMap *EmptySubobjects) |
| 616 | : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), Alignment(8), |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 617 | UnpackedAlignment(Alignment), Packed(false), IsUnion(false), |
| 618 | IsMac68kAlign(false), UnfilledBitsInLastByte(0), MaxFieldAlignment(0), |
| 619 | DataSize(0), NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0), |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 620 | PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 621 | |
Anders Carlsson | fe5ef73 | 2010-10-31 21:39:24 +0000 | [diff] [blame] | 622 | // FIXME: Remove these. |
| 623 | CharUnits toCharUnits(uint64_t Offset) const { |
| 624 | return CharUnits::fromQuantity(Offset / Context.getCharWidth()); |
| 625 | } |
| 626 | uint64_t toOffset(CharUnits Offset) const { |
| 627 | return Offset.getQuantity() * Context.getCharWidth(); |
| 628 | } |
| 629 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 630 | void Layout(const RecordDecl *D); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 631 | void Layout(const CXXRecordDecl *D); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 632 | void Layout(const ObjCInterfaceDecl *D); |
| 633 | |
| 634 | void LayoutFields(const RecordDecl *D); |
| 635 | void LayoutField(const FieldDecl *D); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 636 | void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize, |
| 637 | bool FieldPacked, const FieldDecl *D); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 638 | void LayoutBitField(const FieldDecl *D); |
| 639 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 640 | /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects. |
| 641 | llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator; |
| 642 | |
| 643 | typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *> |
| 644 | BaseSubobjectInfoMapTy; |
| 645 | |
| 646 | /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases |
| 647 | /// of the class we're laying out to their base subobject info. |
| 648 | BaseSubobjectInfoMapTy VirtualBaseInfo; |
| 649 | |
| 650 | /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the |
| 651 | /// class we're laying out to their base subobject info. |
| 652 | BaseSubobjectInfoMapTy NonVirtualBaseInfo; |
| 653 | |
| 654 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for the |
| 655 | /// bases of the given class. |
| 656 | void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD); |
| 657 | |
| 658 | /// ComputeBaseSubobjectInfo - Compute the base subobject information for a |
| 659 | /// single class and all of its base classes. |
| 660 | BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 661 | bool IsVirtual, |
| 662 | BaseSubobjectInfo *Derived); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 663 | |
| 664 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
| 665 | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
| 666 | |
| 667 | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 668 | |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 669 | virtual uint64_t GetVirtualPointersSize(const CXXRecordDecl *RD) const; |
| 670 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 671 | /// IdentifyPrimaryBases - Identify all virtual base classes, direct or |
| 672 | /// indirect, that are primary base classes for some other direct or indirect |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 673 | /// base class. |
| 674 | void IdentifyPrimaryBases(const CXXRecordDecl *RD); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 675 | |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 676 | virtual bool IsNearlyEmpty(const CXXRecordDecl *RD) const; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 677 | |
| 678 | /// LayoutNonVirtualBases - Determines the primary base class (if any) and |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 679 | /// lays it out. Will then proceed to lay out all non-virtual base clasess. |
| 680 | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
| 681 | |
| 682 | /// LayoutNonVirtualBase - Lays out a single non-virtual base. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 683 | void LayoutNonVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 684 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 685 | void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
| 686 | CharUnits Offset); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 687 | |
| 688 | /// LayoutVirtualBases - Lays out all the virtual bases. |
| 689 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
| 690 | const CXXRecordDecl *MostDerivedClass); |
| 691 | |
| 692 | /// LayoutVirtualBase - Lays out a single virtual base. |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 693 | void LayoutVirtualBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 694 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 695 | /// 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] | 696 | /// placed, in chars. |
| 697 | CharUnits LayoutBase(const BaseSubobjectInfo *Base); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 698 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 699 | /// InitializeLayout - Initialize record layout for the given record decl. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 700 | void InitializeLayout(const Decl *D); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 701 | |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 702 | /// FinishLayout - Finalize record layout. Adjust record size based on the |
| 703 | /// alignment. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 704 | void FinishLayout(const NamedDecl *D); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 705 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 706 | void UpdateAlignment(unsigned NewAlignment, unsigned UnpackedNewAlignment); |
| 707 | void UpdateAlignment(unsigned NewAlignment) { |
| 708 | UpdateAlignment(NewAlignment, NewAlignment); |
| 709 | } |
| 710 | |
| 711 | void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset, |
| 712 | uint64_t UnpackedOffset, unsigned UnpackedAlign, |
| 713 | bool isPacked, const FieldDecl *D); |
| 714 | |
| 715 | DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 716 | |
Argyrios Kyrtzidis | c83d2d7 | 2010-08-15 10:17:39 +0000 | [diff] [blame] | 717 | RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
| 718 | void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 719 | public: |
| 720 | static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD); |
Argyrios Kyrtzidis | cfe17e5 | 2010-08-25 00:32:14 +0000 | [diff] [blame] | 721 | |
| 722 | virtual ~RecordLayoutBuilder() { } |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 723 | }; |
Benjamin Kramer | 7e22028 | 2010-05-26 09:58:31 +0000 | [diff] [blame] | 724 | } // end anonymous namespace |
Anders Carlsson | 9392fa6 | 2010-05-26 05:41:04 +0000 | [diff] [blame] | 725 | |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 726 | /// IsNearlyEmpty - Indicates when a class has a vtable pointer, but |
| 727 | /// no other data. |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 728 | bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 729 | // FIXME: Audit the corners |
| 730 | if (!RD->isDynamicClass()) |
| 731 | return false; |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 732 | const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD); |
| 733 | if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0)) |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 734 | return true; |
| 735 | return false; |
| 736 | } |
| 737 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 738 | void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) { |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 739 | const ASTRecordLayout::PrimaryBaseInfo &BaseInfo = |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 740 | Context.getASTRecordLayout(RD).getPrimaryBaseInfo(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 741 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 742 | // If the record has a primary base class that is virtual, add it to the set |
| 743 | // of primary bases. |
Anders Carlsson | 261fba6 | 2009-11-27 22:14:40 +0000 | [diff] [blame] | 744 | if (BaseInfo.isVirtual()) |
| 745 | IndirectPrimaryBases.insert(BaseInfo.getBase()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 746 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 747 | // Now traverse all bases and find primary bases for them. |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 748 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 749 | e = RD->bases_end(); i != e; ++i) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 750 | assert(!i->getType()->isDependentType() && |
| 751 | "Cannot layout class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | const CXXRecordDecl *Base = |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 753 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 754 | |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 755 | // Only bases with virtual bases participate in computing the |
| 756 | // indirect primary virtual base classes. |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 757 | if (Base->getNumVBases()) |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 758 | IdentifyPrimaryBases(Base); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 762 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 763 | RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 764 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 765 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 766 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 767 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 768 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | const CXXRecordDecl *Base = |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 770 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 771 | |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 772 | // Check if this is a nearly empty virtual base. |
| 773 | if (I->isVirtual() && IsNearlyEmpty(Base)) { |
| 774 | // If it's not an indirect primary base, then we've found our primary |
| 775 | // base. |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 776 | if (!IndirectPrimaryBases.count(Base)) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 777 | PrimaryBase = Base; |
| 778 | PrimaryBaseIsVirtual = true; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 779 | return; |
| 780 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 781 | |
Anders Carlsson | 584e1df | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 782 | // Is this the first nearly empty virtual base? |
| 783 | if (!FirstNearlyEmptyVBase) |
| 784 | FirstNearlyEmptyVBase = Base; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 785 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 786 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 787 | SelectPrimaryVBase(Base); |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 788 | if (PrimaryBase) |
Zhongxing Xu | 94ba380 | 2010-02-15 04:28:35 +0000 | [diff] [blame] | 789 | return; |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 793 | uint64_t |
| 794 | RecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const { |
| 795 | return Context.Target.getPointerWidth(0); |
| 796 | } |
| 797 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 798 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 799 | void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 800 | // If the class isn't dynamic, it won't have a primary base. |
| 801 | if (!RD->isDynamicClass()) |
| 802 | return; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 803 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 804 | // Compute all the primary virtual bases for all of our direct and |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 805 | // indirect bases, and record all their primary virtual base classes. |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 806 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 807 | e = RD->bases_end(); i != e; ++i) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 808 | assert(!i->getType()->isDependentType() && |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 809 | "Cannot lay out class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | const CXXRecordDecl *Base = |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 811 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 812 | IdentifyPrimaryBases(Base); |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 815 | // If the record has a dynamic base class, attempt to choose a primary base |
| 816 | // 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] | 817 | // base class, if one exists. |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 818 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 819 | e = RD->bases_end(); i != e; ++i) { |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 820 | // Ignore virtual bases. |
| 821 | if (i->isVirtual()) |
| 822 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 823 | |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 824 | const CXXRecordDecl *Base = |
| 825 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 826 | |
| 827 | if (Base->isDynamicClass()) { |
| 828 | // We found it. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 829 | PrimaryBase = Base; |
| 830 | PrimaryBaseIsVirtual = false; |
Anders Carlsson | ce2009a | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 831 | return; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
| 835 | // Otherwise, it is the first nearly empty virtual base that is not an |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 836 | // indirect primary virtual base class, if one exists. |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 837 | if (RD->getNumVBases() != 0) { |
| 838 | SelectPrimaryVBase(RD); |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 839 | if (PrimaryBase) |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 840 | return; |
| 841 | } |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 842 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 843 | // Otherwise, it is the first nearly empty virtual base that is not an |
| 844 | // indirect primary virtual base class, if one exists. |
| 845 | if (FirstNearlyEmptyVBase) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 846 | PrimaryBase = FirstNearlyEmptyVBase; |
| 847 | PrimaryBaseIsVirtual = true; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 848 | return; |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 849 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 850 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 851 | // Otherwise there is no primary base class. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 852 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 853 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 854 | // Allocate the virtual table pointer at offset zero. |
| 855 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 856 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 857 | // Update the size. |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 858 | Size += GetVirtualPointersSize(RD); |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 859 | DataSize = Size; |
| 860 | |
| 861 | // Update the alignment. |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 862 | UpdateAlignment(Context.Target.getPointerAlign(0)); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 865 | BaseSubobjectInfo * |
| 866 | RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
| 867 | bool IsVirtual, |
| 868 | BaseSubobjectInfo *Derived) { |
| 869 | BaseSubobjectInfo *Info; |
| 870 | |
| 871 | if (IsVirtual) { |
| 872 | // Check if we already have info about this virtual base. |
| 873 | BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD]; |
| 874 | if (InfoSlot) { |
| 875 | assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); |
| 876 | return InfoSlot; |
| 877 | } |
| 878 | |
| 879 | // We don't, create it. |
| 880 | InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 881 | Info = InfoSlot; |
| 882 | } else { |
| 883 | Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
| 884 | } |
| 885 | |
| 886 | Info->Class = RD; |
| 887 | Info->IsVirtual = IsVirtual; |
| 888 | Info->Derived = 0; |
| 889 | Info->PrimaryVirtualBaseInfo = 0; |
| 890 | |
| 891 | const CXXRecordDecl *PrimaryVirtualBase = 0; |
| 892 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0; |
| 893 | |
| 894 | // Check if this base has a primary virtual base. |
| 895 | if (RD->getNumVBases()) { |
| 896 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 897 | if (Layout.getPrimaryBaseWasVirtual()) { |
| 898 | // This base does have a primary virtual base. |
| 899 | PrimaryVirtualBase = Layout.getPrimaryBase(); |
| 900 | assert(PrimaryVirtualBase && "Didn't have a primary virtual base!"); |
| 901 | |
| 902 | // Now check if we have base subobject info about this primary base. |
| 903 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 904 | |
| 905 | if (PrimaryVirtualBaseInfo) { |
| 906 | if (PrimaryVirtualBaseInfo->Derived) { |
| 907 | // We did have info about this primary base, and it turns out that it |
| 908 | // has already been claimed as a primary virtual base for another |
| 909 | // base. |
| 910 | PrimaryVirtualBase = 0; |
| 911 | } else { |
| 912 | // We can claim this base as our primary base. |
| 913 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 914 | PrimaryVirtualBaseInfo->Derived = Info; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | // Now go through all direct bases. |
| 921 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 922 | E = RD->bases_end(); I != E; ++I) { |
| 923 | bool IsVirtual = I->isVirtual(); |
| 924 | |
| 925 | const CXXRecordDecl *BaseDecl = |
| 926 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 927 | |
| 928 | Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info)); |
| 929 | } |
| 930 | |
| 931 | if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) { |
| 932 | // Traversing the bases must have created the base info for our primary |
| 933 | // virtual base. |
| 934 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
| 935 | assert(PrimaryVirtualBaseInfo && |
| 936 | "Did not create a primary virtual base!"); |
| 937 | |
| 938 | // Claim the primary virtual base as our primary virtual base. |
| 939 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
| 940 | PrimaryVirtualBaseInfo->Derived = Info; |
| 941 | } |
| 942 | |
| 943 | return Info; |
| 944 | } |
| 945 | |
| 946 | void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) { |
| 947 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 948 | E = RD->bases_end(); I != E; ++I) { |
| 949 | bool IsVirtual = I->isVirtual(); |
| 950 | |
| 951 | const CXXRecordDecl *BaseDecl = |
| 952 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 953 | |
| 954 | // Compute the base subobject info for this base. |
| 955 | BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0); |
| 956 | |
| 957 | if (IsVirtual) { |
| 958 | // ComputeBaseInfo has already added this base for us. |
| 959 | assert(VirtualBaseInfo.count(BaseDecl) && |
| 960 | "Did not add virtual base!"); |
| 961 | } else { |
| 962 | // Add the base info to the map of non-virtual bases. |
| 963 | assert(!NonVirtualBaseInfo.count(BaseDecl) && |
| 964 | "Non-virtual base already exists!"); |
| 965 | NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 970 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 971 | RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 972 | // Then, determine the primary base class. |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 973 | DeterminePrimaryBase(RD); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 974 | |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 975 | // Compute base subobject info. |
| 976 | ComputeBaseSubobjectInfo(RD); |
| 977 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 978 | // If we have a primary base class, lay it out. |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 979 | if (PrimaryBase) { |
| 980 | if (PrimaryBaseIsVirtual) { |
Anders Carlsson | 6e26454 | 2010-05-29 17:35:14 +0000 | [diff] [blame] | 981 | // If the primary virtual base was a primary virtual base of some other |
| 982 | // base class we'll have to steal it. |
| 983 | BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase); |
| 984 | PrimaryBaseInfo->Derived = 0; |
| 985 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 986 | // 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] | 987 | IndirectPrimaryBases.insert(PrimaryBase); |
Anders Carlsson | 37147ea | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 988 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 989 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 990 | "vbase already visited!"); |
| 991 | VisitedVirtualBases.insert(PrimaryBase); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 992 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 993 | LayoutVirtualBase(PrimaryBaseInfo); |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 994 | } else { |
| 995 | BaseSubobjectInfo *PrimaryBaseInfo = |
| 996 | NonVirtualBaseInfo.lookup(PrimaryBase); |
| 997 | assert(PrimaryBaseInfo && |
| 998 | "Did not find base info for non-virtual primary base!"); |
| 999 | |
| 1000 | LayoutNonVirtualBase(PrimaryBaseInfo); |
| 1001 | } |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1002 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1003 | |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1004 | // Now lay out the non-virtual bases. |
| 1005 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1006 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1007 | |
| 1008 | // Ignore virtual bases. |
| 1009 | if (I->isVirtual()) |
| 1010 | continue; |
| 1011 | |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1012 | const CXXRecordDecl *BaseDecl = |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1013 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1014 | |
| 1015 | // Skip the primary base. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1016 | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual) |
Anders Carlsson | 200c5c2 | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 1017 | continue; |
| 1018 | |
| 1019 | // Lay out the base. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1020 | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
| 1021 | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
| 1022 | |
| 1023 | LayoutNonVirtualBase(BaseInfo); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1027 | void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1028 | // Layout the base. |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1029 | CharUnits Offset = LayoutBase(Base); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1030 | |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1031 | // Add its base class offset. |
Anders Carlsson | 07cebc5 | 2010-05-29 17:42:25 +0000 | [diff] [blame] | 1032 | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1033 | Bases.insert(std::make_pair(Base->Class, Offset)); |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1034 | |
| 1035 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1036 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1038 | void |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1039 | RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1040 | CharUnits Offset) { |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1041 | // This base isn't interesting, it has no virtual bases. |
| 1042 | if (!Info->Class->getNumVBases()) |
| 1043 | return; |
| 1044 | |
| 1045 | // First, check if we have a virtual primary base to add offsets for. |
| 1046 | if (Info->PrimaryVirtualBaseInfo) { |
| 1047 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
| 1048 | "Primary virtual base is not virtual!"); |
| 1049 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
| 1050 | // Add the offset. |
| 1051 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
| 1052 | "primary vbase offset already exists!"); |
| 1053 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1054 | Offset)); |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1055 | |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1056 | // Traverse the primary virtual base. |
| 1057 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
| 1058 | } |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1061 | // Now go through all direct non-virtual bases. |
| 1062 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
| 1063 | for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) { |
| 1064 | const BaseSubobjectInfo *Base = Info->Bases[I]; |
| 1065 | if (Base->IsVirtual) |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1066 | continue; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1067 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1068 | CharUnits BaseOffset = |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1069 | Offset + toCharUnits(Layout.getBaseClassOffsetInBits(Base->Class)); |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1070 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | void |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1075 | RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | 9791357 | 2010-04-15 16:12:58 +0000 | [diff] [blame] | 1076 | const CXXRecordDecl *MostDerivedClass) { |
Anders Carlsson | 88f4296 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1077 | const CXXRecordDecl *PrimaryBase; |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1078 | bool PrimaryBaseIsVirtual; |
Anders Carlsson | 37147ea | 2010-03-11 05:42:17 +0000 | [diff] [blame] | 1079 | |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1080 | if (MostDerivedClass == RD) { |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 1081 | PrimaryBase = this->PrimaryBase; |
| 1082 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1083 | } else { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1084 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | 88f4296 | 2010-03-11 04:33:54 +0000 | [diff] [blame] | 1085 | PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1086 | PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1087 | } |
| 1088 | |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1089 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1090 | E = RD->bases_end(); I != E; ++I) { |
| 1091 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1092 | "Cannot layout class with dependent bases."); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1093 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1094 | const CXXRecordDecl *BaseDecl = |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1095 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1096 | |
| 1097 | if (I->isVirtual()) { |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1098 | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) { |
| 1099 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1100 | |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1101 | // Only lay out the virtual base if it's not an indirect primary base. |
| 1102 | if (!IndirectPrimaryBase) { |
| 1103 | // Only visit virtual bases once. |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1104 | if (!VisitedVirtualBases.insert(BaseDecl)) |
Anders Carlsson | bdda6c1 | 2010-04-10 18:42:27 +0000 | [diff] [blame] | 1105 | continue; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1106 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1107 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
| 1108 | assert(BaseInfo && "Did not find virtual base info!"); |
| 1109 | LayoutVirtualBase(BaseInfo); |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 1110 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 1111 | } |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 1112 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1113 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1114 | if (!BaseDecl->getNumVBases()) { |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1115 | // This base isn't interesting since it doesn't have any virtual bases. |
| 1116 | continue; |
Mike Stump | fe3010d | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 1117 | } |
Anders Carlsson | 622e247 | 2010-03-11 04:24:02 +0000 | [diff] [blame] | 1118 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1119 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1123 | void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | 3cd09cc | 2010-05-29 19:44:50 +0000 | [diff] [blame] | 1124 | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
| 1125 | |
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); |
Anders Carlsson | e3bdbee | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Add its base class offset. |
Anders Carlsson | 276b491 | 2010-05-29 17:48:36 +0000 | [diff] [blame] | 1130 | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1131 | VBases.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 | } |
| 1135 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1136 | CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
Anders Carlsson | b1d880b | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1137 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1138 | |
| 1139 | // 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] | 1140 | if (Base->Class->isEmpty() && |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1141 | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 1142 | Size = std::max(Size, Layout.getSize()); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1143 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1144 | return CharUnits::Zero(); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1145 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1146 | |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 1147 | unsigned BaseAlign = Layout.getNonVirtualAlign(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1148 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1149 | // Round up the current record size to the base's alignment boundary. |
| 1150 | uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1151 | |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1152 | // Try to place the base. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1153 | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, toCharUnits(Offset))) |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1154 | Offset += BaseAlign; |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1155 | |
Anders Carlsson | b1d880b | 2010-05-29 20:47:33 +0000 | [diff] [blame] | 1156 | if (!Base->Class->isEmpty()) { |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1157 | // Update the data size. |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 1158 | DataSize = Offset + Layout.getNonVirtualSize(); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1159 | |
| 1160 | Size = std::max(Size, DataSize); |
| 1161 | } else |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 1162 | Size = std::max(Size, Offset + Layout.getSize()); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Remember max struct/class alignment. |
| 1165 | UpdateAlignment(BaseAlign); |
| 1166 | |
Anders Carlsson | a231151 | 2010-10-31 22:20:42 +0000 | [diff] [blame] | 1167 | return toCharUnits(Offset); |
Anders Carlsson | e239b9d | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1170 | void RecordLayoutBuilder::InitializeLayout(const Decl *D) { |
| 1171 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 1172 | IsUnion = RD->isUnion(); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1173 | |
Anders Carlsson | a5dd722 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 1174 | Packed = D->hasAttr<PackedAttr>(); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1175 | |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1176 | // mac68k alignment supersedes maximum field alignment and attribute aligned, |
| 1177 | // and forces all structures to have 2-byte alignment. The IBM docs on it |
| 1178 | // allude to additional (more complicated) semantics, especially with regard |
| 1179 | // to bit-fields, but gcc appears not to follow that. |
| 1180 | if (D->hasAttr<AlignMac68kAttr>()) { |
| 1181 | IsMac68kAlign = true; |
| 1182 | MaxFieldAlignment = 2 * 8; |
| 1183 | Alignment = 2 * 8; |
| 1184 | } else { |
| 1185 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
| 1186 | MaxFieldAlignment = MFAA->getAlignment(); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1187 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1188 | if (unsigned MaxAlign = D->getMaxAlignment()) |
| 1189 | UpdateAlignment(MaxAlign); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1190 | } |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1191 | } |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 1192 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1193 | void RecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 1194 | InitializeLayout(D); |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1195 | LayoutFields(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1197 | // Finally, round the size of the total struct up to the alignment of the |
| 1198 | // struct itself. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1199 | FinishLayout(D); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
| 1203 | InitializeLayout(RD); |
| 1204 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1205 | // Lay out the vtable and the non-virtual bases. |
| 1206 | LayoutNonVirtualBases(RD); |
| 1207 | |
| 1208 | LayoutFields(RD); |
| 1209 | |
| 1210 | NonVirtualSize = Size; |
| 1211 | NonVirtualAlignment = Alignment; |
| 1212 | |
| 1213 | // Lay out the virtual bases and add the primary virtual base offsets. |
| 1214 | LayoutVirtualBases(RD, RD); |
| 1215 | |
| 1216 | VisitedVirtualBases.clear(); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Finally, round the size of the total struct up to the alignment of the |
| 1219 | // struct itself. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1220 | FinishLayout(RD); |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1221 | |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1222 | #ifndef NDEBUG |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1223 | // Check that we have base offsets for all bases. |
| 1224 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1225 | E = RD->bases_end(); I != E; ++I) { |
| 1226 | if (I->isVirtual()) |
| 1227 | continue; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1228 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1229 | const CXXRecordDecl *BaseDecl = |
| 1230 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1231 | |
| 1232 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
| 1233 | } |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1234 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1235 | // And all virtual bases. |
| 1236 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1237 | E = RD->vbases_end(); I != E; ++I) { |
| 1238 | const CXXRecordDecl *BaseDecl = |
| 1239 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1240 | |
Anders Carlsson | c6cab68 | 2010-05-26 15:10:00 +0000 | [diff] [blame] | 1241 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
Anders Carlsson | a1e8716 | 2010-04-10 21:24:48 +0000 | [diff] [blame] | 1242 | } |
| 1243 | #endif |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1246 | void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1247 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1248 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1249 | |
| 1250 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1252 | // We start laying out ivars not at the end of the superclass |
| 1253 | // structure, but at the next byte following the last field. |
Anders Carlsson | 243a685 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 1254 | Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8); |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 1255 | DataSize = Size; |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1256 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1258 | InitializeLayout(D); |
Daniel Dunbar | 8a2c92c | 2010-05-27 01:12:46 +0000 | [diff] [blame] | 1259 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1260 | // Layout each ivar sequentially. |
| 1261 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1262 | Context.ShallowCollectObjCIvars(D, Ivars); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1263 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) |
| 1264 | LayoutField(Ivars[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1266 | // Finally, round the size of the total struct up to the alignment of the |
| 1267 | // struct itself. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1268 | FinishLayout(D); |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1271 | void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1272 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 1273 | // the future, this will need to be tweakable by targets. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1275 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 1276 | LayoutField(*Field); |
| 1277 | } |
| 1278 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1279 | void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1280 | uint64_t TypeSize, |
| 1281 | bool FieldPacked, |
| 1282 | const FieldDecl *D) { |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1283 | assert(Context.getLangOptions().CPlusPlus && |
| 1284 | "Can only have wide bit-fields in C++!"); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1285 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1286 | // Itanium C++ ABI 2.4: |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1287 | // 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] | 1288 | // sizeof(T')*8 <= n. |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1289 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1290 | QualType IntegralPODTypes[] = { |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1291 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1292 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
| 1293 | }; |
| 1294 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1295 | QualType Type; |
| 1296 | for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes); |
| 1297 | I != E; ++I) { |
| 1298 | uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1299 | |
| 1300 | if (Size > FieldSize) |
| 1301 | break; |
| 1302 | |
| 1303 | Type = IntegralPODTypes[I]; |
| 1304 | } |
| 1305 | assert(!Type.isNull() && "Did not find a type!"); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1306 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1307 | unsigned TypeAlign = Context.getTypeAlign(Type); |
| 1308 | |
| 1309 | // We're not going to use any of the unfilled bits in the last byte. |
| 1310 | UnfilledBitsInLastByte = 0; |
| 1311 | |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1312 | uint64_t FieldOffset; |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1313 | uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1314 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1315 | if (IsUnion) { |
| 1316 | DataSize = std::max(DataSize, FieldSize); |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1317 | FieldOffset = 0; |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1318 | } else { |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1319 | // The bitfield is allocated starting at the next offset aligned appropriately |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1320 | // for T', with length n bits. |
Anders Carlsson | de9f153 | 2010-04-17 20:21:41 +0000 | [diff] [blame] | 1321 | FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1322 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1323 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1324 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1325 | DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); |
| 1326 | UnfilledBitsInLastByte = DataSize - NewSizeInBits; |
| 1327 | } |
| 1328 | |
| 1329 | // Place this field at the current location. |
| 1330 | FieldOffsets.push_back(FieldOffset); |
| 1331 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1332 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
| 1333 | TypeAlign, FieldPacked, D); |
| 1334 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1335 | // Update the size. |
| 1336 | Size = std::max(Size, DataSize); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1337 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1338 | // Remember max struct/class alignment. |
| 1339 | UpdateAlignment(TypeAlign); |
| 1340 | } |
| 1341 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1342 | void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1343 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1344 | uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte; |
| 1345 | uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset; |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1346 | uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1347 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1348 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1349 | uint64_t TypeSize = FieldInfo.first; |
| 1350 | unsigned FieldAlign = FieldInfo.second; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1351 | |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1352 | if (FieldSize > TypeSize) { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1353 | LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D); |
Anders Carlsson | 4cf6f5f | 2010-04-16 15:57:11 +0000 | [diff] [blame] | 1354 | return; |
| 1355 | } |
| 1356 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1357 | // The align if the field is not packed. This is to check if the attribute |
| 1358 | // was unnecessary (-Wpacked). |
| 1359 | unsigned UnpackedFieldAlign = FieldAlign; |
| 1360 | uint64_t UnpackedFieldOffset = FieldOffset; |
| 1361 | if (!Context.Target.useBitFieldTypeAlignment()) |
| 1362 | UnpackedFieldAlign = 1; |
| 1363 | |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1364 | if (FieldPacked || !Context.Target.useBitFieldTypeAlignment()) |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1365 | FieldAlign = 1; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1366 | FieldAlign = std::max(FieldAlign, D->getMaxAlignment()); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1367 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1368 | |
| 1369 | // The maximum field alignment overrides the aligned attribute. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1370 | if (MaxFieldAlignment) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1371 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1372 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
| 1373 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1374 | |
Daniel Dunbar | b6a1693 | 2010-04-15 06:18:39 +0000 | [diff] [blame] | 1375 | // Check if we need to add padding to give the field the correct alignment. |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1376 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
Daniel Dunbar | 84b0316 | 2010-06-29 18:34:35 +0000 | [diff] [blame] | 1377 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1378 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1379 | if (FieldSize == 0 || |
| 1380 | (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize) |
| 1381 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1382 | UnpackedFieldAlign); |
| 1383 | |
Daniel Dunbar | b6a1693 | 2010-04-15 06:18:39 +0000 | [diff] [blame] | 1384 | // Padding members don't affect overall alignment. |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1385 | if (!D->getIdentifier()) |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1386 | FieldAlign = UnpackedFieldAlign = 1; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1387 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1388 | // Place this field at the current location. |
| 1389 | FieldOffsets.push_back(FieldOffset); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1390 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1391 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
| 1392 | UnpackedFieldAlign, FieldPacked, D); |
| 1393 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1394 | // Update DataSize to include the last byte containing (part of) the bitfield. |
| 1395 | if (IsUnion) { |
| 1396 | // FIXME: I think FieldSize should be TypeSize here. |
| 1397 | DataSize = std::max(DataSize, FieldSize); |
| 1398 | } else { |
| 1399 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1400 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1401 | DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); |
| 1402 | UnfilledBitsInLastByte = DataSize - NewSizeInBits; |
| 1403 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1404 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1405 | // Update the size. |
| 1406 | Size = std::max(Size, DataSize); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1407 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1408 | // Remember max struct/class alignment. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1409 | UpdateAlignment(FieldAlign, UnpackedFieldAlign); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1412 | void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1413 | if (D->isBitField()) { |
| 1414 | LayoutBitField(D); |
| 1415 | return; |
| 1416 | } |
| 1417 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1418 | uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte; |
| 1419 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 1420 | // Reset the unfilled bits. |
| 1421 | UnfilledBitsInLastByte = 0; |
| 1422 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1423 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 1424 | uint64_t FieldOffset = IsUnion ? 0 : DataSize; |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1425 | uint64_t FieldSize; |
| 1426 | unsigned FieldAlign; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1427 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1428 | if (D->getType()->isIncompleteArrayType()) { |
| 1429 | // This is a flexible array member; we can't directly |
| 1430 | // query getTypeInfo about these, so we figure it out here. |
| 1431 | // Flexible array members don't have any size, but they |
| 1432 | // have to be aligned appropriately for their element type. |
| 1433 | FieldSize = 0; |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1434 | const ArrayType* ATy = Context.getAsArrayType(D->getType()); |
| 1435 | FieldAlign = Context.getTypeAlign(ATy->getElementType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1436 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 1437 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1438 | FieldSize = Context.Target.getPointerWidth(AS); |
| 1439 | FieldAlign = Context.Target.getPointerAlign(AS); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1440 | } else { |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1441 | std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1442 | FieldSize = FieldInfo.first; |
| 1443 | FieldAlign = FieldInfo.second; |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1444 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1446 | // The align if the field is not packed. This is to check if the attribute |
| 1447 | // was unnecessary (-Wpacked). |
| 1448 | unsigned UnpackedFieldAlign = FieldAlign; |
| 1449 | uint64_t UnpackedFieldOffset = FieldOffset; |
| 1450 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1451 | if (FieldPacked) |
| 1452 | FieldAlign = 8; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1453 | FieldAlign = std::max(FieldAlign, D->getMaxAlignment()); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1454 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment()); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1455 | |
| 1456 | // The maximum field alignment overrides the aligned attribute. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1457 | if (MaxFieldAlignment) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1458 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1459 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
| 1460 | } |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Round up the current record size to the field's alignment boundary. |
| 1463 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1464 | UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, |
| 1465 | UnpackedFieldAlign); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1466 | |
Anders Carlsson | 83a45e7 | 2010-05-30 06:52:33 +0000 | [diff] [blame] | 1467 | if (!IsUnion && EmptySubobjects) { |
| 1468 | // Check if we can place the field at this offset. |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 1469 | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, |
| 1470 | toCharUnits(FieldOffset))) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1471 | // We couldn't place the field at the offset. Try again at a new offset. |
| 1472 | FieldOffset += FieldAlign; |
| 1473 | } |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1474 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1475 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1476 | // Place this field at the current location. |
| 1477 | FieldOffsets.push_back(FieldOffset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1479 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
| 1480 | UnpackedFieldAlign, FieldPacked, D); |
| 1481 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1482 | // Reserve space for this field. |
| 1483 | if (IsUnion) |
| 1484 | Size = std::max(Size, FieldSize); |
| 1485 | else |
| 1486 | Size = FieldOffset + FieldSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 1488 | // Update the data size. |
| 1489 | DataSize = Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1491 | // Remember max struct/class alignment. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1492 | UpdateAlignment(FieldAlign, UnpackedFieldAlign); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1495 | void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1496 | // In C++, records cannot be of size 0. |
Anders Carlsson | 0f0e9b0 | 2010-04-16 15:07:51 +0000 | [diff] [blame] | 1497 | if (Context.getLangOptions().CPlusPlus && Size == 0) |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1498 | Size = 8; |
| 1499 | // Finally, round the size of the record up to the alignment of the |
| 1500 | // record itself. |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1501 | uint64_t UnpaddedSize = Size - UnfilledBitsInLastByte; |
| 1502 | uint64_t UnpackedSize = llvm::RoundUpToAlignment(Size, UnpackedAlignment); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 1503 | Size = llvm::RoundUpToAlignment(Size, Alignment); |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1504 | |
| 1505 | unsigned CharBitNum = Context.Target.getCharWidth(); |
| 1506 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 1507 | // Warn if padding was introduced to the struct/class/union. |
| 1508 | if (Size > UnpaddedSize) { |
| 1509 | unsigned PadSize = Size - UnpaddedSize; |
| 1510 | bool InBits = true; |
| 1511 | if (PadSize % CharBitNum == 0) { |
| 1512 | PadSize = PadSize / CharBitNum; |
| 1513 | InBits = false; |
| 1514 | } |
| 1515 | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
| 1516 | << Context.getTypeDeclType(RD) |
| 1517 | << PadSize |
| 1518 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 1519 | } |
| 1520 | |
| 1521 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1522 | // bother since there won't be alignment issues. |
| 1523 | if (Packed && UnpackedAlignment > CharBitNum && Size == UnpackedSize) |
| 1524 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1525 | << Context.getTypeDeclType(RD); |
| 1526 | } |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1529 | void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment, |
| 1530 | unsigned UnpackedNewAlignment) { |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 1531 | // The alignment is not modified when using 'mac68k' alignment. |
| 1532 | if (IsMac68kAlign) |
| 1533 | return; |
| 1534 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1535 | if (NewAlignment > Alignment) { |
| 1536 | assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2")); |
| 1537 | Alignment = NewAlignment; |
| 1538 | } |
| 1539 | |
| 1540 | if (UnpackedNewAlignment > UnpackedAlignment) { |
| 1541 | assert(llvm::isPowerOf2_32(UnpackedNewAlignment && |
| 1542 | "Alignment not a power of 2")); |
| 1543 | UnpackedAlignment = UnpackedNewAlignment; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset, |
| 1548 | uint64_t UnpaddedOffset, |
| 1549 | uint64_t UnpackedOffset, |
| 1550 | unsigned UnpackedAlign, |
| 1551 | bool isPacked, |
| 1552 | const FieldDecl *D) { |
| 1553 | // We let objc ivars without warning, objc interfaces generally are not used |
| 1554 | // for padding tricks. |
| 1555 | if (isa<ObjCIvarDecl>(D)) |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1556 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1558 | unsigned CharBitNum = Context.Target.getCharWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1560 | // Warn if padding was introduced to the struct/class. |
| 1561 | if (!IsUnion && Offset > UnpaddedOffset) { |
| 1562 | unsigned PadSize = Offset - UnpaddedOffset; |
| 1563 | bool InBits = true; |
| 1564 | if (PadSize % CharBitNum == 0) { |
| 1565 | PadSize = PadSize / CharBitNum; |
| 1566 | InBits = false; |
| 1567 | } |
| 1568 | if (D->getIdentifier()) |
| 1569 | Diag(D->getLocation(), diag::warn_padded_struct_field) |
| 1570 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 1571 | << Context.getTypeDeclType(D->getParent()) |
| 1572 | << PadSize |
| 1573 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not |
| 1574 | << D->getIdentifier(); |
| 1575 | else |
| 1576 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
| 1577 | << (D->getParent()->isStruct() ? 0 : 1) // struct|class |
| 1578 | << Context.getTypeDeclType(D->getParent()) |
| 1579 | << PadSize |
| 1580 | << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not |
| 1581 | } |
| 1582 | |
| 1583 | // Warn if we packed it unnecessarily. If the alignment is 1 byte don't |
| 1584 | // bother since there won't be alignment issues. |
| 1585 | if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset) |
| 1586 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
| 1587 | << D->getIdentifier(); |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1590 | const CXXMethodDecl * |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1591 | RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1592 | // 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] | 1593 | if (!RD->isPolymorphic()) |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1594 | return 0; |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1595 | |
| 1596 | // A class inside an anonymous namespace doesn't have a key function. (Or |
| 1597 | // at least, there's no point to assigning a key function to such a class; |
| 1598 | // this doesn't affect the ABI.) |
| 1599 | if (RD->isInAnonymousNamespace()) |
| 1600 | return 0; |
| 1601 | |
Argyrios Kyrtzidis | 3bd5b6c | 2010-10-13 02:39:41 +0000 | [diff] [blame] | 1602 | // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6. |
| 1603 | // Same behavior as GCC. |
| 1604 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 1605 | if (TSK == TSK_ImplicitInstantiation || |
| 1606 | TSK == TSK_ExplicitInstantiationDefinition) |
| 1607 | return 0; |
| 1608 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1609 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 1610 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1611 | const CXXMethodDecl *MD = *I; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1612 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1613 | if (!MD->isVirtual()) |
| 1614 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1615 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1616 | if (MD->isPure()) |
| 1617 | continue; |
Eli Friedman | 61eab88 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 1618 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1619 | // Ignore implicit member functions, they are always marked as inline, but |
| 1620 | // they don't have a body until they're defined. |
| 1621 | if (MD->isImplicit()) |
| 1622 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1623 | |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 1624 | if (MD->isInlineSpecified()) |
| 1625 | continue; |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1626 | |
| 1627 | if (MD->hasInlineBody()) |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1628 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1629 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1630 | // We found it. |
| 1631 | return MD; |
| 1632 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1633 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1637 | DiagnosticBuilder |
| 1638 | RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) { |
| 1639 | return Context.getDiagnostics().Report( |
| 1640 | FullSourceLoc(Loc, Context.getSourceManager()), DiagID); |
| 1641 | } |
| 1642 | |
Benjamin Kramer | cb9c074 | 2010-10-22 16:33:16 +0000 | [diff] [blame] | 1643 | namespace { |
| 1644 | // This class implements layout specific to the Microsoft ABI. |
| 1645 | class MSRecordLayoutBuilder : public RecordLayoutBuilder { |
| 1646 | public: |
| 1647 | MSRecordLayoutBuilder(ASTContext& Ctx, EmptySubobjectMap *EmptySubobjects) : |
| 1648 | RecordLayoutBuilder(Ctx, EmptySubobjects) {} |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1649 | |
Benjamin Kramer | cb9c074 | 2010-10-22 16:33:16 +0000 | [diff] [blame] | 1650 | virtual bool IsNearlyEmpty(const CXXRecordDecl *RD) const; |
| 1651 | virtual uint64_t GetVirtualPointersSize(const CXXRecordDecl *RD) const; |
| 1652 | }; |
| 1653 | } |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1654 | |
| 1655 | bool MSRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { |
| 1656 | // FIXME: Audit the corners |
| 1657 | if (!RD->isDynamicClass()) |
| 1658 | return false; |
| 1659 | const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD); |
| 1660 | // In the Microsoft ABI, classes can have one or two vtable pointers. |
| 1661 | if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0) || |
| 1662 | BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0) * 2) |
| 1663 | return true; |
| 1664 | return false; |
| 1665 | } |
| 1666 | |
| 1667 | uint64_t |
| 1668 | MSRecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const { |
| 1669 | // We should reserve space for two pointers if the class has both |
| 1670 | // virtual functions and virtual bases. |
| 1671 | if (RD->isPolymorphic() && RD->getNumVBases() > 0) |
| 1672 | return 2 * Context.Target.getPointerWidth(0); |
| 1673 | return Context.Target.getPointerWidth(0); |
| 1674 | } |
| 1675 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1676 | /// getASTRecordLayout - Get or compute information about the layout of the |
| 1677 | /// specified record (struct/union/class), which indicates its size and field |
| 1678 | /// position information. |
| 1679 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
| 1680 | D = D->getDefinition(); |
| 1681 | assert(D && "Cannot get layout of forward declarations!"); |
| 1682 | |
| 1683 | // Look up this layout, if already laid out, return what we have. |
| 1684 | // Note that we can't save a reference to the entry because this function |
| 1685 | // is recursive. |
| 1686 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
| 1687 | if (Entry) return *Entry; |
| 1688 | |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1689 | const ASTRecordLayout *NewEntry; |
| 1690 | |
| 1691 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1692 | EmptySubobjectMap EmptySubobjects(*this, RD); |
Anders Carlsson | 58b16b6 | 2010-05-27 05:41:06 +0000 | [diff] [blame] | 1693 | |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1694 | // When compiling for Microsoft, use the special MS builder. |
Argyrios Kyrtzidis | cfe17e5 | 2010-08-25 00:32:14 +0000 | [diff] [blame] | 1695 | llvm::OwningPtr<RecordLayoutBuilder> Builder; |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 1696 | switch (Target.getCXXABI()) { |
| 1697 | default: |
Argyrios Kyrtzidis | cfe17e5 | 2010-08-25 00:32:14 +0000 | [diff] [blame] | 1698 | Builder.reset(new RecordLayoutBuilder(*this, &EmptySubobjects)); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | case CXXABI_Microsoft: |
Argyrios Kyrtzidis | cfe17e5 | 2010-08-25 00:32:14 +0000 | [diff] [blame] | 1701 | Builder.reset(new MSRecordLayoutBuilder(*this, &EmptySubobjects)); |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 1702 | } |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1703 | Builder->Layout(RD); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1704 | |
| 1705 | // FIXME: This is not always correct. See the part about bitfields at |
| 1706 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 1707 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
| 1708 | bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD(); |
| 1709 | |
| 1710 | // FIXME: This should be done in FinalizeLayout. |
| 1711 | uint64_t DataSize = |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1712 | IsPODForThePurposeOfLayout ? Builder->Size : Builder->DataSize; |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1713 | uint64_t NonVirtualSize = |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1714 | IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize; |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1715 | |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1716 | NewEntry = |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1717 | new (*this) ASTRecordLayout(*this, Builder->Size, Builder->Alignment, |
| 1718 | DataSize, Builder->FieldOffsets.data(), |
| 1719 | Builder->FieldOffsets.size(), |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1720 | NonVirtualSize, |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1721 | Builder->NonVirtualAlignment, |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1722 | EmptySubobjects.SizeOfLargestEmptySubobject, |
Charles Davis | c9f8aec | 2010-08-19 00:55:19 +0000 | [diff] [blame] | 1723 | Builder->PrimaryBase, |
| 1724 | Builder->PrimaryBaseIsVirtual, |
| 1725 | Builder->Bases, Builder->VBases); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1726 | } else { |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1727 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1728 | Builder.Layout(D); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1729 | |
Anders Carlsson | 2f64e37 | 2010-05-26 05:10:47 +0000 | [diff] [blame] | 1730 | NewEntry = |
| 1731 | new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, |
| 1732 | Builder.Size, |
| 1733 | Builder.FieldOffsets.data(), |
| 1734 | Builder.FieldOffsets.size()); |
| 1735 | } |
| 1736 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1737 | ASTRecordLayouts[D] = NewEntry; |
| 1738 | |
| 1739 | if (getLangOptions().DumpRecordLayouts) { |
| 1740 | llvm::errs() << "\n*** Dumping AST Record Layout\n"; |
| 1741 | DumpRecordLayout(D, llvm::errs()); |
| 1742 | } |
| 1743 | |
| 1744 | return *NewEntry; |
| 1745 | } |
| 1746 | |
| 1747 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
| 1748 | RD = cast<CXXRecordDecl>(RD->getDefinition()); |
| 1749 | assert(RD && "Cannot get key function for forward declarations!"); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1750 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1751 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1752 | if (!Entry) |
Anders Carlsson | 7d0918a | 2010-05-26 05:58:59 +0000 | [diff] [blame] | 1753 | Entry = RecordLayoutBuilder::ComputeKeyFunction(RD); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1754 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1755 | return Entry; |
| 1756 | } |
| 1757 | |
| 1758 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 1759 | /// layout of the given interface. |
| 1760 | /// |
| 1761 | /// \param Impl - If given, also include the layout of the interface's |
| 1762 | /// implementation. This may differ by including synthesized ivars. |
| 1763 | const ASTRecordLayout & |
| 1764 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 1765 | const ObjCImplementationDecl *Impl) { |
| 1766 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 1767 | |
| 1768 | // Look up this layout, if already laid out, return what we have. |
| 1769 | ObjCContainerDecl *Key = |
| 1770 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 1771 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 1772 | return *Entry; |
| 1773 | |
| 1774 | // Add in synthesized ivar count if laying out an implementation. |
| 1775 | if (Impl) { |
| 1776 | unsigned SynthCount = CountNonClassIvars(D); |
| 1777 | // If there aren't any sythesized ivars then reuse the interface |
| 1778 | // entry. Note we can't cache this because we simply free all |
| 1779 | // entries later; however we shouldn't look up implementations |
| 1780 | // frequently. |
| 1781 | if (SynthCount == 0) |
| 1782 | return getObjCLayout(D, 0); |
| 1783 | } |
| 1784 | |
Anders Carlsson | 261febd | 2010-05-27 00:07:01 +0000 | [diff] [blame] | 1785 | RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0); |
Anders Carlsson | 36cdc61 | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 1786 | Builder.Layout(D); |
| 1787 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1788 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 36cdc61 | 2010-05-26 05:04:25 +0000 | [diff] [blame] | 1789 | new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment, |
| 1790 | Builder.DataSize, |
| 1791 | Builder.FieldOffsets.data(), |
| 1792 | Builder.FieldOffsets.size()); |
Daniel Dunbar | 0aa7edb | 2010-05-27 02:25:46 +0000 | [diff] [blame] | 1793 | |
Anders Carlsson | 1e641ce | 2010-05-26 04:56:53 +0000 | [diff] [blame] | 1794 | ObjCLayouts[Key] = NewEntry; |
| 1795 | |
| 1796 | return *NewEntry; |
| 1797 | } |
| 1798 | |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1799 | static void PrintOffset(llvm::raw_ostream &OS, |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1800 | CharUnits Offset, unsigned IndentLevel) { |
| 1801 | OS << llvm::format("%4d | ", Offset.getQuantity()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1802 | OS.indent(IndentLevel * 2); |
| 1803 | } |
| 1804 | |
| 1805 | static void DumpCXXRecordLayout(llvm::raw_ostream &OS, |
| 1806 | const CXXRecordDecl *RD, ASTContext &C, |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1807 | CharUnits Offset, |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1808 | unsigned IndentLevel, |
| 1809 | const char* Description, |
| 1810 | bool IncludeVirtualBases) { |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1811 | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1812 | |
| 1813 | PrintOffset(OS, Offset, IndentLevel); |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 1814 | OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1815 | if (Description) |
| 1816 | OS << ' ' << Description; |
| 1817 | if (RD->isEmpty()) |
| 1818 | OS << " (empty)"; |
| 1819 | OS << '\n'; |
| 1820 | |
| 1821 | IndentLevel++; |
| 1822 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1823 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1824 | |
| 1825 | // Vtable pointer. |
| 1826 | if (RD->isDynamicClass() && !PrimaryBase) { |
| 1827 | PrintOffset(OS, Offset, IndentLevel); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1828 | OS << '(' << RD << " vtable pointer)\n"; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1829 | } |
| 1830 | // Dump (non-virtual) bases |
| 1831 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1832 | E = RD->bases_end(); I != E; ++I) { |
| 1833 | assert(!I->getType()->isDependentType() && |
| 1834 | "Cannot layout class with dependent bases."); |
| 1835 | if (I->isVirtual()) |
| 1836 | continue; |
| 1837 | |
| 1838 | const CXXRecordDecl *Base = |
| 1839 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1840 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1841 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1842 | |
| 1843 | DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
| 1844 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 1845 | /*IncludeVirtualBases=*/false); |
| 1846 | } |
| 1847 | |
| 1848 | // Dump fields. |
| 1849 | uint64_t FieldNo = 0; |
| 1850 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1851 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
| 1852 | const FieldDecl *Field = *I; |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1853 | CharUnits FieldOffset = Offset + |
| 1854 | CharUnits::fromQuantity(Layout.getFieldOffset(FieldNo) / |
| 1855 | C.getCharWidth()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1856 | |
| 1857 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
| 1858 | if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 1859 | DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel, |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1860 | Field->getName().data(), |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1861 | /*IncludeVirtualBases=*/true); |
| 1862 | continue; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | PrintOffset(OS, FieldOffset, IndentLevel); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1867 | OS << Field->getType().getAsString() << ' ' << Field << '\n'; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | if (!IncludeVirtualBases) |
| 1871 | return; |
| 1872 | |
| 1873 | // Dump virtual bases. |
| 1874 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 1875 | E = RD->vbases_end(); I != E; ++I) { |
| 1876 | assert(I->isVirtual() && "Found non-virtual class!"); |
| 1877 | const CXXRecordDecl *VBase = |
| 1878 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1879 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1880 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1881 | DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
| 1882 | VBase == PrimaryBase ? |
| 1883 | "(primary virtual base)" : "(virtual base)", |
| 1884 | /*IncludeVirtualBases=*/false); |
| 1885 | } |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1886 | |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1887 | OS << " sizeof=" << Layout.getSize() / 8; |
| 1888 | OS << ", dsize=" << Layout.getDataSize() / 8; |
| 1889 | OS << ", align=" << Layout.getAlignment() / 8 << '\n'; |
| 1890 | OS << " nvsize=" << Layout.getNonVirtualSize() / 8; |
| 1891 | OS << ", nvalign=" << Layout.getNonVirtualAlign() / 8 << '\n'; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame] | 1892 | OS << '\n'; |
| 1893 | } |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1894 | |
| 1895 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, |
| 1896 | llvm::raw_ostream &OS) { |
| 1897 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 1898 | |
| 1899 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame^] | 1900 | return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0, |
Daniel Dunbar | 8d8ab74 | 2010-04-19 20:44:53 +0000 | [diff] [blame] | 1901 | /*IncludeVirtualBases=*/true); |
| 1902 | |
| 1903 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
| 1904 | OS << "Record: "; |
| 1905 | RD->dump(); |
| 1906 | OS << "\nLayout: "; |
| 1907 | OS << "<ASTRecordLayout\n"; |
| 1908 | OS << " Size:" << Info.getSize() << "\n"; |
| 1909 | OS << " DataSize:" << Info.getDataSize() << "\n"; |
| 1910 | OS << " Alignment:" << Info.getAlignment() << "\n"; |
| 1911 | OS << " FieldOffsets: ["; |
| 1912 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
| 1913 | if (i) OS << ", "; |
| 1914 | OS << Info.getFieldOffset(i); |
| 1915 | } |
| 1916 | OS << "]>\n"; |
| 1917 | } |