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