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