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