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