Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 1 | //=== ASTRecordLayoutBuilder.cpp - Helper class for building record layouts ==// |
| 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 | |
| 10 | #include "RecordLayoutBuilder.h" |
| 11 | |
| 12 | #include "clang/AST/Attr.h" |
| 13 | #include "clang/AST/Decl.h" |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 18 | #include <llvm/ADT/SmallSet.h> |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 19 | #include <llvm/Support/MathExtras.h> |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 23 | ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx) |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 24 | : Ctx(Ctx), Size(0), Alignment(8), Packed(false), UnfilledBitsInLastByte(0), |
| 25 | MaxFieldAlignment(0), DataSize(0), IsUnion(false), NonVirtualSize(0), |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 26 | NonVirtualAlignment(8), FirstNearlyEmptyVBase(0) { } |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 27 | |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 28 | /// IsNearlyEmpty - Indicates when a class has a vtable pointer, but |
| 29 | /// no other data. |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 30 | bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 31 | // FIXME: Audit the corners |
| 32 | if (!RD->isDynamicClass()) |
| 33 | return false; |
| 34 | const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD); |
| 35 | if (BaseInfo.getNonVirtualSize() == Ctx.Target.getPointerWidth(0)) |
| 36 | return true; |
| 37 | return false; |
| 38 | } |
| 39 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 40 | void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) { |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 41 | const ASTRecordLayout::PrimaryBaseInfo &BaseInfo = |
| 42 | Ctx.getASTRecordLayout(RD).getPrimaryBaseInfo(); |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 43 | |
| 44 | // If the record has a primary base class that is virtual, add it to the set |
| 45 | // of primary bases. |
Anders Carlsson | a30c0d3 | 2009-11-27 22:14:40 +0000 | [diff] [blame] | 46 | if (BaseInfo.isVirtual()) |
| 47 | IndirectPrimaryBases.insert(BaseInfo.getBase()); |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 48 | |
| 49 | // Now traverse all bases and find primary bases for them. |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 50 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 51 | e = RD->bases_end(); i != e; ++i) { |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 52 | assert(!i->getType()->isDependentType() && |
| 53 | "Cannot layout class with dependent bases."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | const CXXRecordDecl *Base = |
Mike Stump | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 55 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 56 | |
Mike Stump | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 57 | // Only bases with virtual bases participate in computing the |
| 58 | // indirect primary virtual base classes. |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 59 | if (Base->getNumVBases()) |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 60 | IdentifyPrimaryBases(Base); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 64 | void |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 65 | ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 66 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 67 | E = RD->bases_end(); I != E; ++I) { |
| 68 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 69 | "Cannot layout class with dependent bases."); |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 70 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | const CXXRecordDecl *Base = |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 72 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 73 | |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 74 | // Check if this is a nearly empty virtual base. |
| 75 | if (I->isVirtual() && IsNearlyEmpty(Base)) { |
| 76 | // If it's not an indirect primary base, then we've found our primary |
| 77 | // base. |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 78 | if (!IndirectPrimaryBases.count(Base)) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 79 | PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(Base, |
| 80 | /*IsVirtual=*/true); |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 81 | return; |
| 82 | } |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 83 | |
| 84 | // Is this the first nearly empty virtual base? |
| 85 | if (!FirstNearlyEmptyVBase) |
| 86 | FirstNearlyEmptyVBase = Base; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 87 | } |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 88 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 89 | SelectPrimaryVBase(Base); |
Zhongxing Xu | ec345b7 | 2010-02-15 04:28:35 +0000 | [diff] [blame] | 90 | if (PrimaryBase.getBase()) |
| 91 | return; |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 95 | /// DeterminePrimaryBase - Determine the primary base of the given class. |
| 96 | void ASTRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
| 97 | // If the class isn't dynamic, it won't have a primary base. |
| 98 | if (!RD->isDynamicClass()) |
| 99 | return; |
| 100 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 101 | // Compute all the primary virtual bases for all of our direct and |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 102 | // indirect bases, and record all their primary virtual base classes. |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 103 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 104 | e = RD->bases_end(); i != e; ++i) { |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 105 | assert(!i->getType()->isDependentType() && |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 106 | "Cannot lay out class with dependent bases."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | const CXXRecordDecl *Base = |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 108 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 109 | IdentifyPrimaryBases(Base); |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 112 | // If the record has a dynamic base class, attempt to choose a primary base |
| 113 | // class. It is the first (in direct base class order) non-virtual dynamic |
| 114 | // base class, if one exists. |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 115 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 116 | e = RD->bases_end(); i != e; ++i) { |
Anders Carlsson | 03ff379 | 2009-11-27 22:05:05 +0000 | [diff] [blame] | 117 | // Ignore virtual bases. |
| 118 | if (i->isVirtual()) |
| 119 | continue; |
| 120 | |
| 121 | const CXXRecordDecl *Base = |
| 122 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 123 | |
| 124 | if (Base->isDynamicClass()) { |
| 125 | // We found it. |
| 126 | PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(Base, /*IsVirtual=*/false); |
| 127 | return; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | // Otherwise, it is the first nearly empty virtual base that is not an |
Mike Stump | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 132 | // indirect primary virtual base class, if one exists. |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 133 | if (RD->getNumVBases() != 0) { |
| 134 | SelectPrimaryVBase(RD); |
| 135 | if (PrimaryBase.getBase()) |
| 136 | return; |
| 137 | } |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 139 | // Otherwise, it is the first nearly empty virtual base that is not an |
| 140 | // indirect primary virtual base class, if one exists. |
| 141 | if (FirstNearlyEmptyVBase) { |
Anders Carlsson | f2fa75b | 2010-03-11 03:39:12 +0000 | [diff] [blame] | 142 | PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(FirstNearlyEmptyVBase, |
| 143 | /*IsVirtual=*/true); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 144 | return; |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 145 | } |
Anders Carlsson | 8143069 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 146 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 147 | // Otherwise there is no primary base class. |
| 148 | assert(!PrimaryBase.getBase() && "Should not get here with a primary base!"); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 149 | |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 150 | // Allocate the virtual table pointer at offset zero. |
| 151 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
| 152 | |
| 153 | // Update the size. |
| 154 | Size += Ctx.Target.getPointerWidth(0); |
| 155 | DataSize = Size; |
| 156 | |
| 157 | // Update the alignment. |
| 158 | UpdateAlignment(Ctx.Target.getPointerAlign(0)); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 161 | uint64_t ASTRecordLayoutBuilder::getBaseOffset(const CXXRecordDecl *Base) { |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 162 | ASTRecordLayout::BaseOffsetsMapTy::iterator I = Bases.find(Base); |
| 163 | if (I != Bases.end()) |
| 164 | return I->second; |
| 165 | |
| 166 | I = VBases.find(Base); |
| 167 | if (I != VBases.end()) |
| 168 | return I->second; |
| 169 | |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 170 | assert(0 && "missing base"); |
| 171 | return 0; |
| 172 | } |
| 173 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 174 | void |
| 175 | ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 176 | // First, determine the primary base class. |
| 177 | DeterminePrimaryBase(RD); |
| 178 | |
| 179 | // If we have a primary base class, lay it out. |
| 180 | if (const CXXRecordDecl *Base = PrimaryBase.getBase()) { |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 181 | if (PrimaryBase.isVirtual()) { |
| 182 | // We have a virtual primary base, insert it as an indirect primary base. |
| 183 | IndirectPrimaryBases.insert(Base); |
| 184 | |
| 185 | LayoutVirtualBase(Base); |
| 186 | } else |
| 187 | LayoutNonVirtualBase(Base); |
| 188 | } |
| 189 | |
| 190 | // Now lay out the non-virtual bases. |
| 191 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 192 | E = RD->bases_end(); I != E; ++I) { |
| 193 | |
| 194 | // Ignore virtual bases. |
| 195 | if (I->isVirtual()) |
| 196 | continue; |
| 197 | |
| 198 | const CXXRecordDecl *Base = |
| 199 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 200 | |
| 201 | // Skip the primary base. |
| 202 | if (Base == PrimaryBase.getBase() && !PrimaryBase.isVirtual()) |
| 203 | continue; |
| 204 | |
| 205 | // Lay out the base. |
| 206 | LayoutNonVirtualBase(Base); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | void ASTRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 211 | // Layout the base. |
| 212 | uint64_t Offset = LayoutBase(RD); |
| 213 | |
| 214 | // Add its base class offset. |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 215 | if (!Bases.insert(std::make_pair(RD, Offset)).second) |
| 216 | assert(false && "Added same base offset more than once!"); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 217 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 218 | |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 219 | void |
| 220 | ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *Class, |
| 221 | const CXXRecordDecl *RD, |
| 222 | const CXXRecordDecl *PB, |
| 223 | uint64_t Offset) { |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame^] | 224 | |
| 225 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 226 | E = RD->bases_end(); I != E; ++I) { |
| 227 | assert(!I->getType()->isDependentType() && |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 228 | "Cannot layout class with dependent bases."); |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame^] | 229 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | const CXXRecordDecl *Base = |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame^] | 231 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 232 | |
| 233 | if (I->isVirtual()) { |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 234 | if (Base == PB) { |
| 235 | // Only lay things out once. |
Anders Carlsson | c4c00ec | 2010-03-11 02:41:30 +0000 | [diff] [blame] | 236 | if (VisitedVirtualBases.count(Base)) |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 237 | continue; |
| 238 | // Mark it so we don't lay it out twice. |
Anders Carlsson | c4c00ec | 2010-03-11 02:41:30 +0000 | [diff] [blame] | 239 | VisitedVirtualBases.insert(Base); |
Anders Carlsson | aa87b4e | 2010-03-11 00:21:21 +0000 | [diff] [blame] | 240 | assert (IndirectPrimaryBases.count(Base) && "IndirectPrimary was wrong"); |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 241 | |
| 242 | if (!VBases.insert(std::make_pair(Base, Offset)).second) { |
| 243 | // FIXME: Enable this assertion. |
| 244 | // assert(false && "Added same vbase offset more than once!"); |
| 245 | } |
Anders Carlsson | aa87b4e | 2010-03-11 00:21:21 +0000 | [diff] [blame] | 246 | } else if (IndirectPrimaryBases.count(Base)) { |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 247 | // Someone else will eventually lay this out. |
| 248 | ; |
| 249 | } else { |
| 250 | // Only lay things out once. |
Anders Carlsson | c4c00ec | 2010-03-11 02:41:30 +0000 | [diff] [blame] | 251 | if (VisitedVirtualBases.count(Base)) |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 252 | continue; |
| 253 | // Mark it so we don't lay it out twice. |
Anders Carlsson | c4c00ec | 2010-03-11 02:41:30 +0000 | [diff] [blame] | 254 | VisitedVirtualBases.insert(Base); |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 255 | LayoutVirtualBase(Base); |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 256 | } |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 257 | } |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 258 | |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame^] | 259 | if (!Base->getNumVBases()) { |
| 260 | // This base isn't interesting since it doesn't have any virtual bases. |
| 261 | continue; |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 262 | } |
Anders Carlsson | f7b7a1e | 2010-03-11 04:24:02 +0000 | [diff] [blame^] | 263 | |
| 264 | // Compute the offset of this base. |
| 265 | uint64_t BaseOffset; |
| 266 | |
| 267 | if (I->isVirtual()) { |
| 268 | // We want the vbase offset from the class we're currently laying out. |
| 269 | assert(VBases.count(Base) && "Did not find virtual base!"); |
| 270 | BaseOffset = VBases[Base]; |
| 271 | } else if (RD == Class) { |
| 272 | // We want the base offset from the class we're currently laying out. |
| 273 | assert(Bases.count(Base) && "Did not find base!"); |
| 274 | BaseOffset = Bases[Base]; |
| 275 | } else { |
| 276 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD); |
| 277 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 278 | } |
| 279 | |
| 280 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Base); |
| 281 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBaseInfo().getBase(); |
| 282 | LayoutVirtualBases(Class, Base, PrimaryBase, BaseOffset); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 286 | void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 0d0b588 | 2010-03-10 22:26:24 +0000 | [diff] [blame] | 287 | // Layout the base. |
| 288 | uint64_t Offset = LayoutBase(RD); |
| 289 | |
| 290 | // Add its base class offset. |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 291 | if (!VBases.insert(std::make_pair(RD, Offset)).second) |
| 292 | assert(false && "Added same vbase offset more than once!"); |
Anders Carlsson | 09ffa32 | 2010-03-10 22:21:28 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | uint64_t ASTRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *RD) { |
| 296 | const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD); |
| 297 | |
| 298 | // If we have an empty base class, try to place it at offset 0. |
| 299 | if (RD->isEmpty() && canPlaceRecordAtOffset(RD, 0)) { |
| 300 | // We were able to place the class at offset 0. |
| 301 | UpdateEmptyClassOffsets(RD, 0); |
| 302 | |
| 303 | Size = std::max(Size, BaseInfo.getSize()); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | unsigned BaseAlign = BaseInfo.getNonVirtualAlign(); |
| 309 | |
| 310 | // Round up the current record size to the base's alignment boundary. |
| 311 | uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign); |
| 312 | |
| 313 | // Try to place the base. |
| 314 | while (true) { |
| 315 | if (canPlaceRecordAtOffset(RD, Offset)) |
| 316 | break; |
| 317 | |
| 318 | Offset += BaseAlign; |
| 319 | } |
| 320 | |
| 321 | if (!RD->isEmpty()) { |
| 322 | // Update the data size. |
| 323 | DataSize = Offset + BaseInfo.getNonVirtualSize(); |
| 324 | |
| 325 | Size = std::max(Size, DataSize); |
| 326 | } else |
| 327 | Size = std::max(Size, Offset + BaseInfo.getSize()); |
| 328 | |
| 329 | // Remember max struct/class alignment. |
| 330 | UpdateAlignment(BaseAlign); |
| 331 | |
| 332 | UpdateEmptyClassOffsets(RD, Offset); |
| 333 | return Offset; |
| 334 | } |
| 335 | |
Anders Carlsson | 6522b05 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 336 | bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, |
| 337 | uint64_t Offset) const { |
Anders Carlsson | f24b18f | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 338 | // Look for an empty class with the same type at the same offset. |
| 339 | for (EmptyClassOffsetsTy::const_iterator I = |
| 340 | EmptyClassOffsets.lower_bound(Offset), |
| 341 | E = EmptyClassOffsets.upper_bound(Offset); I != E; ++I) { |
| 342 | |
| 343 | if (I->second == RD) |
| 344 | return false; |
| 345 | } |
| 346 | |
Anders Carlsson | bb66bc8 | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 347 | const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD); |
| 348 | |
| 349 | // Check bases. |
| 350 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 351 | E = RD->bases_end(); I != E; ++I) { |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 352 | assert(!I->getType()->isDependentType() && |
| 353 | "Cannot layout class with dependent bases."); |
Anders Carlsson | bb66bc8 | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 354 | if (I->isVirtual()) |
| 355 | continue; |
| 356 | |
| 357 | const CXXRecordDecl *Base = |
| 358 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 359 | |
| 360 | uint64_t BaseClassOffset = Info.getBaseClassOffset(Base); |
| 361 | |
| 362 | if (!canPlaceRecordAtOffset(Base, Offset + BaseClassOffset)) |
| 363 | return false; |
| 364 | } |
| 365 | |
Anders Carlsson | d7d358a | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 366 | // Check fields. |
| 367 | unsigned FieldNo = 0; |
| 368 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 369 | I != E; ++I, ++FieldNo) { |
| 370 | const FieldDecl *FD = *I; |
| 371 | |
| 372 | uint64_t FieldOffset = Info.getFieldOffset(FieldNo); |
| 373 | |
| 374 | if (!canPlaceFieldAtOffset(FD, Offset + FieldOffset)) |
| 375 | return false; |
| 376 | } |
| 377 | |
Anders Carlsson | bb66bc8 | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 378 | // FIXME: virtual bases. |
Anders Carlsson | 6522b05 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | |
Anders Carlsson | 6f95c70 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 382 | bool ASTRecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, |
| 383 | uint64_t Offset) const { |
Anders Carlsson | 4bf8214 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 384 | QualType T = FD->getType(); |
| 385 | if (const RecordType *RT = T->getAs<RecordType>()) { |
Anders Carlsson | 6f95c70 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 386 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 387 | return canPlaceRecordAtOffset(RD, Offset); |
| 388 | } |
| 389 | |
Anders Carlsson | 4bf8214 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 390 | if (const ConstantArrayType *AT = Ctx.getAsConstantArrayType(T)) { |
| 391 | QualType ElemTy = Ctx.getBaseElementType(AT); |
| 392 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 393 | if (!RT) |
| 394 | return true; |
| 395 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 396 | if (!RD) |
| 397 | return true; |
| 398 | |
| 399 | const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD); |
| 400 | |
| 401 | uint64_t NumElements = Ctx.getConstantArrayElementCount(AT); |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 402 | uint64_t ElementOffset = Offset; |
Anders Carlsson | 4bf8214 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 403 | for (uint64_t I = 0; I != NumElements; ++I) { |
| 404 | if (!canPlaceRecordAtOffset(RD, ElementOffset)) |
| 405 | return false; |
| 406 | |
| 407 | ElementOffset += Info.getSize(); |
| 408 | } |
| 409 | } |
Anders Carlsson | 6f95c70 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 410 | |
| 411 | return true; |
| 412 | } |
| 413 | |
Anders Carlsson | 6522b05 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 414 | void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD, |
| 415 | uint64_t Offset) { |
Anders Carlsson | f24b18f | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 416 | if (RD->isEmpty()) |
| 417 | EmptyClassOffsets.insert(std::make_pair(Offset, RD)); |
Anders Carlsson | bb66bc8 | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 418 | |
| 419 | const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD); |
| 420 | |
| 421 | // Update bases. |
| 422 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 423 | E = RD->bases_end(); I != E; ++I) { |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 424 | assert(!I->getType()->isDependentType() && |
| 425 | "Cannot layout class with dependent bases."); |
Anders Carlsson | bb66bc8 | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 426 | if (I->isVirtual()) |
| 427 | continue; |
Anders Carlsson | f24b18f | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 428 | |
Anders Carlsson | bb66bc8 | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 429 | const CXXRecordDecl *Base = |
| 430 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 431 | |
| 432 | uint64_t BaseClassOffset = Info.getBaseClassOffset(Base); |
| 433 | UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset); |
| 434 | } |
| 435 | |
Anders Carlsson | d7d358a | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 436 | // Update fields. |
| 437 | unsigned FieldNo = 0; |
| 438 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 439 | I != E; ++I, ++FieldNo) { |
| 440 | const FieldDecl *FD = *I; |
| 441 | |
| 442 | uint64_t FieldOffset = Info.getFieldOffset(FieldNo); |
| 443 | UpdateEmptyClassOffsets(FD, Offset + FieldOffset); |
| 444 | } |
| 445 | |
| 446 | // FIXME: Update virtual bases. |
Anders Carlsson | 6522b05 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Anders Carlsson | e188310 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 449 | void |
| 450 | ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD, |
| 451 | uint64_t Offset) { |
| 452 | QualType T = FD->getType(); |
| 453 | |
| 454 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 455 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 456 | UpdateEmptyClassOffsets(RD, Offset); |
| 457 | return; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (const ConstantArrayType *AT = Ctx.getAsConstantArrayType(T)) { |
| 462 | QualType ElemTy = Ctx.getBaseElementType(AT); |
| 463 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
| 464 | if (!RT) |
| 465 | return; |
| 466 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 467 | if (!RD) |
| 468 | return; |
| 469 | |
| 470 | const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD); |
| 471 | |
| 472 | uint64_t NumElements = Ctx.getConstantArrayElementCount(AT); |
Mike Stump | 2b84dd3 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 473 | uint64_t ElementOffset = Offset; |
Anders Carlsson | e188310 | 2009-09-25 01:54:38 +0000 | [diff] [blame] | 474 | |
| 475 | for (uint64_t I = 0; I != NumElements; ++I) { |
| 476 | UpdateEmptyClassOffsets(RD, ElementOffset); |
| 477 | ElementOffset += Info.getSize(); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 482 | void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 483 | IsUnion = D->isUnion(); |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 484 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 485 | Packed = D->hasAttr<PackedAttr>(); |
| 486 | |
| 487 | // The #pragma pack attribute specifies the maximum field alignment. |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 488 | if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 489 | MaxFieldAlignment = PPA->getAlignment(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 491 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 492 | UpdateAlignment(AA->getMaxAlignment()); |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 493 | |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 494 | // If this is a C++ class, lay out the vtable and the non-virtual bases. |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 495 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); |
Anders Carlsson | 8630b5b | 2010-03-11 00:15:35 +0000 | [diff] [blame] | 496 | if (RD) |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 497 | LayoutNonVirtualBases(RD); |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 498 | |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 499 | LayoutFields(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 501 | NonVirtualSize = Size; |
| 502 | NonVirtualAlignment = Alignment; |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 503 | |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 504 | if (RD) { |
Anders Carlsson | c4c00ec | 2010-03-11 02:41:30 +0000 | [diff] [blame] | 505 | LayoutVirtualBases(RD, RD, PrimaryBase.getBase(), 0); |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 506 | } |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 507 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 508 | // Finally, round the size of the total struct up to the alignment of the |
| 509 | // struct itself. |
| 510 | FinishLayout(); |
| 511 | } |
| 512 | |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 513 | // FIXME. Impl is no longer needed. |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 514 | void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D, |
| 515 | const ObjCImplementationDecl *Impl) { |
| 516 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
| 517 | const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD); |
| 518 | |
| 519 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 521 | // We start laying out ivars not at the end of the superclass |
| 522 | // structure, but at the next byte following the last field. |
Anders Carlsson | 27b5013 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 523 | Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8); |
Anders Carlsson | 47680d8 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 524 | DataSize = Size; |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 525 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 527 | Packed = D->hasAttr<PackedAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 529 | // The #pragma pack attribute specifies the maximum field alignment. |
| 530 | if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) |
| 531 | MaxFieldAlignment = PPA->getAlignment(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 533 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 534 | UpdateAlignment(AA->getMaxAlignment()); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 535 | // Layout each ivar sequentially. |
| 536 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 537 | Ctx.ShallowCollectObjCIvars(D, Ivars); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 538 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) |
| 539 | LayoutField(Ivars[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 541 | // Finally, round the size of the total struct up to the alignment of the |
| 542 | // struct itself. |
| 543 | FinishLayout(); |
| 544 | } |
| 545 | |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 546 | void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 547 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 548 | // the future, this will need to be tweakable by targets. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 550 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) |
| 551 | LayoutField(*Field); |
| 552 | } |
| 553 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 554 | void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
| 555 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 556 | uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 557 | uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Ctx).getZExtValue(); |
| 558 | |
| 559 | std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType()); |
| 560 | uint64_t TypeSize = FieldInfo.first; |
| 561 | unsigned FieldAlign = FieldInfo.second; |
| 562 | |
| 563 | if (FieldPacked) |
| 564 | FieldAlign = 1; |
| 565 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 566 | FieldAlign = std::max(FieldAlign, AA->getMaxAlignment()); |
| 567 | |
| 568 | // The maximum field alignment overrides the aligned attribute. |
| 569 | if (MaxFieldAlignment) |
| 570 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 571 | |
| 572 | // Check if we need to add padding to give the field the correct |
| 573 | // alignment. |
| 574 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 575 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 576 | |
| 577 | // Padding members don't affect overall alignment |
| 578 | if (!D->getIdentifier()) |
| 579 | FieldAlign = 1; |
| 580 | |
| 581 | // Place this field at the current location. |
| 582 | FieldOffsets.push_back(FieldOffset); |
| 583 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 584 | // Update DataSize to include the last byte containing (part of) the bitfield. |
| 585 | if (IsUnion) { |
| 586 | // FIXME: I think FieldSize should be TypeSize here. |
| 587 | DataSize = std::max(DataSize, FieldSize); |
| 588 | } else { |
| 589 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
| 590 | |
| 591 | DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8); |
| 592 | UnfilledBitsInLastByte = DataSize - NewSizeInBits; |
| 593 | } |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 594 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 595 | // Update the size. |
| 596 | Size = std::max(Size, DataSize); |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 597 | |
| 598 | // Remember max struct/class alignment. |
| 599 | UpdateAlignment(FieldAlign); |
| 600 | } |
| 601 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 602 | void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 603 | if (D->isBitField()) { |
| 604 | LayoutBitField(D); |
| 605 | return; |
| 606 | } |
| 607 | |
Anders Carlsson | ba95840 | 2009-11-22 19:13:51 +0000 | [diff] [blame] | 608 | // Reset the unfilled bits. |
| 609 | UnfilledBitsInLastByte = 0; |
| 610 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 611 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Anders Carlsson | 47680d8 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 612 | uint64_t FieldOffset = IsUnion ? 0 : DataSize; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 613 | uint64_t FieldSize; |
| 614 | unsigned FieldAlign; |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 615 | |
| 616 | if (D->getType()->isIncompleteArrayType()) { |
| 617 | // This is a flexible array member; we can't directly |
| 618 | // query getTypeInfo about these, so we figure it out here. |
| 619 | // Flexible array members don't have any size, but they |
| 620 | // have to be aligned appropriately for their element type. |
| 621 | FieldSize = 0; |
| 622 | const ArrayType* ATy = Ctx.getAsArrayType(D->getType()); |
| 623 | FieldAlign = Ctx.getTypeAlign(ATy->getElementType()); |
| 624 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
| 625 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
| 626 | FieldSize = Ctx.Target.getPointerWidth(AS); |
| 627 | FieldAlign = Ctx.Target.getPointerAlign(AS); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 628 | } else { |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 629 | std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType()); |
| 630 | FieldSize = FieldInfo.first; |
| 631 | FieldAlign = FieldInfo.second; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 632 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 634 | if (FieldPacked) |
| 635 | FieldAlign = 8; |
| 636 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 637 | FieldAlign = std::max(FieldAlign, AA->getMaxAlignment()); |
| 638 | |
| 639 | // The maximum field alignment overrides the aligned attribute. |
| 640 | if (MaxFieldAlignment) |
| 641 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 642 | |
| 643 | // Round up the current record size to the field's alignment boundary. |
| 644 | FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); |
| 645 | |
| 646 | if (!IsUnion) { |
| 647 | while (true) { |
| 648 | // Check if we can place the field at this offset. |
| 649 | if (canPlaceFieldAtOffset(D, FieldOffset)) |
| 650 | break; |
| 651 | |
| 652 | // We couldn't place the field at the offset. Try again at a new offset. |
| 653 | FieldOffset += FieldAlign; |
| 654 | } |
| 655 | |
| 656 | UpdateEmptyClassOffsets(D, FieldOffset); |
| 657 | } |
| 658 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 659 | // Place this field at the current location. |
| 660 | FieldOffsets.push_back(FieldOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 662 | // Reserve space for this field. |
| 663 | if (IsUnion) |
| 664 | Size = std::max(Size, FieldSize); |
| 665 | else |
| 666 | Size = FieldOffset + FieldSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Anders Carlsson | 47680d8 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 668 | // Update the data size. |
| 669 | DataSize = Size; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 671 | // Remember max struct/class alignment. |
| 672 | UpdateAlignment(FieldAlign); |
| 673 | } |
| 674 | |
| 675 | void ASTRecordLayoutBuilder::FinishLayout() { |
| 676 | // In C++, records cannot be of size 0. |
| 677 | if (Ctx.getLangOptions().CPlusPlus && Size == 0) |
| 678 | Size = 8; |
| 679 | // Finally, round the size of the record up to the alignment of the |
| 680 | // record itself. |
Anders Carlsson | 0720944 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 681 | Size = llvm::RoundUpToAlignment(Size, Alignment); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) { |
| 685 | if (NewAlignment <= Alignment) |
| 686 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 688 | assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2")); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 690 | Alignment = NewAlignment; |
| 691 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 693 | const ASTRecordLayout * |
| 694 | ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, |
| 695 | const RecordDecl *D) { |
| 696 | ASTRecordLayoutBuilder Builder(Ctx); |
| 697 | |
| 698 | Builder.Layout(D); |
| 699 | |
| 700 | if (!isa<CXXRecordDecl>(D)) |
Ted Kremenek | c3015a9 | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 701 | return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment, |
| 702 | Builder.Size, |
| 703 | Builder.FieldOffsets.data(), |
| 704 | Builder.FieldOffsets.size()); |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 705 | |
| 706 | // FIXME: This is not always correct. See the part about bitfields at |
| 707 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 708 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
| 709 | bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD(); |
| 710 | |
| 711 | // FIXME: This should be done in FinalizeLayout. |
| 712 | uint64_t DataSize = |
| 713 | IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize; |
| 714 | uint64_t NonVirtualSize = |
| 715 | IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; |
| 716 | |
Ted Kremenek | c3015a9 | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 717 | return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment, |
| 718 | DataSize, Builder.FieldOffsets.data(), |
| 719 | Builder.FieldOffsets.size(), |
| 720 | NonVirtualSize, |
| 721 | Builder.NonVirtualAlignment, |
| 722 | Builder.PrimaryBase, |
Anders Carlsson | 6a84889 | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 723 | Builder.Bases, Builder.VBases); |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | const ASTRecordLayout * |
| 727 | ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, |
| 728 | const ObjCInterfaceDecl *D, |
| 729 | const ObjCImplementationDecl *Impl) { |
| 730 | ASTRecordLayoutBuilder Builder(Ctx); |
| 731 | |
| 732 | Builder.Layout(D, Impl); |
| 733 | |
Ted Kremenek | c3015a9 | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 734 | return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment, |
| 735 | Builder.DataSize, |
| 736 | Builder.FieldOffsets.data(), |
| 737 | Builder.FieldOffsets.size()); |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | const CXXMethodDecl * |
| 741 | ASTRecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) { |
| 742 | assert(RD->isDynamicClass() && "Class does not have any virtual methods!"); |
| 743 | |
| 744 | // If a class isnt' polymorphic it doesn't have a key function. |
| 745 | if (!RD->isPolymorphic()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 746 | return 0; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 747 | |
| 748 | // A class inside an anonymous namespace doesn't have a key function. (Or |
| 749 | // at least, there's no point to assigning a key function to such a class; |
| 750 | // this doesn't affect the ABI.) |
| 751 | if (RD->isInAnonymousNamespace()) |
| 752 | return 0; |
| 753 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 754 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 755 | E = RD->method_end(); I != E; ++I) { |
| 756 | const CXXMethodDecl *MD = *I; |
| 757 | |
| 758 | if (!MD->isVirtual()) |
| 759 | continue; |
| 760 | |
| 761 | if (MD->isPure()) |
| 762 | continue; |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 763 | |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 764 | // Ignore implicit member functions, they are always marked as inline, but |
| 765 | // they don't have a body until they're defined. |
| 766 | if (MD->isImplicit()) |
| 767 | continue; |
Douglas Gregor | a318efd | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 768 | |
| 769 | if (MD->isInlineSpecified()) |
| 770 | continue; |
Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 771 | |
| 772 | if (MD->hasInlineBody()) |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 773 | continue; |
| 774 | |
| 775 | // We found it. |
| 776 | return MD; |
| 777 | } |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |