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