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