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