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