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