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