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