Anders Carlsson | bda4c10 | 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 | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | bda4c10 | 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 | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 19 | #include <llvm/ADT/SmallSet.h> |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 20 | #include <llvm/Support/MathExtras.h> |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 24 | ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx) |
Anders Carlsson | e4fc0d9 | 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 | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 28 | |
Mike Stump | 2effeca | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 29 | /// LayoutVtable - Lay out the vtable and set PrimaryBase. |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 30 | void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD) { |
Mike Stump | 02b1623 | 2009-08-12 22:06:55 +0000 | [diff] [blame] | 31 | if (!RD->isDynamicClass()) { |
Mike Stump | 2effeca | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 32 | // There is no primary base in this case. |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 33 | return; |
Mike Stump | 2effeca | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 34 | } |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 36 | SelectPrimaryBase(RD); |
Mike Stump | 6f37633 | 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 | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 41 | DataSize = Size; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 42 | } |
Mike Stump | 3dee6ef | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | void |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 46 | ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | 74cbe22 | 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 | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 50 | assert(!i->getType()->isDependentType() && |
| 51 | "Cannot layout class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | const CXXRecordDecl *Base = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 53 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 2effeca | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 54 | // Skip the PrimaryBase here, as it is laid down first. |
Mike Stump | 2153891 | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 55 | if (Base != PrimaryBase || PrimaryBaseWasVirtual) |
| 56 | LayoutBaseNonVirtually(Base, false); |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
Mike Stump | 6f37633 | 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 | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 66 | bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const { |
Mike Stump | 6f37633 | 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 | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 76 | void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) { |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 77 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD); |
Anders Carlsson | 3f06652 | 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 | 6f37633 | 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 | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 87 | assert(!i->getType()->isDependentType() && |
| 88 | "Cannot layout class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | const CXXRecordDecl *Base = |
Mike Stump | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 90 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 91 | |
Mike Stump | 4952094 | 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 | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 94 | if (Base->getNumVBases()) |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 95 | IdentifyPrimaryBases(Base); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 99 | void |
| 100 | ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD, |
| 101 | const CXXRecordDecl *&FirstPrimary) { |
Mike Stump | d76264e | 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 | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 104 | assert(!i->getType()->isDependentType() && |
| 105 | "Cannot layout class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | const CXXRecordDecl *Base = |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 107 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 108 | if (!i->isVirtual()) { |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 109 | SelectPrimaryVBase(Base, FirstPrimary); |
Mike Stump | d76264e | 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 | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 117 | if (!IndirectPrimaryBases.count(Base)) { |
Mike Stump | d76264e | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 118 | setPrimaryBase(Base, true); |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 125 | /// SelectPrimaryBase - Selects the primary base for the given class and |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 126 | /// record that with setPrimaryBase. We also calculate the IndirectPrimaries. |
Anders Carlsson | 3f06652 | 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 | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 129 | // indirect bases, and record all their primary virtual base classes. |
Mike Stump | 0880e75 | 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 | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 132 | assert(!i->getType()->isDependentType() && |
| 133 | "Cannot layout class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | const CXXRecordDecl *Base = |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 135 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 136 | IdentifyPrimaryBases(Base); |
Mike Stump | 0880e75 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Anders Carlsson | 3f06652 | 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 | 6f37633 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | const CXXRecordDecl *Base = |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 146 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 147 | if (Base->isDynamicClass()) { |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 148 | // We found it. |
Mike Stump | 928f150 | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 149 | setPrimaryBase(Base, false); |
Mike Stump | 6f37633 | 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 | 4952094 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 156 | // indirect primary virtual base class, if one exists. |
Mike Stump | 6f37633 | 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 | 0cd40e4 | 2009-09-22 18:21:58 +0000 | [diff] [blame] | 160 | if (RD->getNumVBases() == 0) |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 161 | return; |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 162 | |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 163 | // Then we can search for the first nearly empty virtual base itself. |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 164 | const CXXRecordDecl *FirstPrimary = 0; |
| 165 | SelectPrimaryVBase(RD, FirstPrimary); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 166 | |
Mike Stump | 928f150 | 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 | 56cfdc1 | 2009-09-22 19:16:59 +0000 | [diff] [blame] | 169 | if (!PrimaryBase) |
| 170 | setPrimaryBase(FirstPrimary, true); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 173 | void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) { |
Mike Stump | 2153891 | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 174 | LayoutBaseNonVirtually(RD, true); |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Mike Stump | 968db33 | 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 | fe3010d | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 193 | const CXXRecordDecl *PB, |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 194 | uint64_t Offset, |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 195 | llvm::SmallSet<const CXXRecordDecl*, 32> &mark, |
Mike Stump | d53cef1 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 196 | llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) { |
Mike Stump | 4ef9809 | 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 | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 199 | assert(!i->getType()->isDependentType() && |
| 200 | "Cannot layout class with dependent bases."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | const CXXRecordDecl *Base = |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 202 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 203 | uint64_t BaseOffset = Offset; |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 204 | if (i->isVirtual()) { |
Mike Stump | fe3010d | 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 | e4feb83 | 2009-09-22 00:04:45 +0000 | [diff] [blame] | 212 | VBases.push_back(std::make_pair(Base, Offset)); |
Mike Stump | fe3010d | 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 | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 222 | LayoutVirtualBase(Base); |
Anders Carlsson | e4feb83 | 2009-09-22 00:04:45 +0000 | [diff] [blame] | 223 | BaseOffset = VBases.back().second; |
Mike Stump | fe3010d | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 224 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 225 | } else { |
| 226 | if (RD == Class) |
| 227 | BaseOffset = getBaseOffset(Base); |
| 228 | else { |
Mike Stump | 37acf5a | 2009-11-11 02:49:00 +0000 | [diff] [blame] | 229 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD); |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 230 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 231 | } |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 232 | } |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 233 | |
Mike Stump | fe3010d | 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 | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 237 | LayoutVirtualBases(Class, Base, PB, BaseOffset, mark, IndirectPrimary); |
Mike Stump | fe3010d | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 238 | } |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 242 | bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD, |
| 243 | uint64_t Offset) const { |
Anders Carlsson | 1345bd2 | 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 | ffbdefc | 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 | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 258 | assert(!I->getType()->isDependentType() && |
| 259 | "Cannot layout class with dependent bases."); |
Anders Carlsson | ffbdefc | 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 | 1eca99b | 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 | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 284 | // FIXME: virtual bases. |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | |
Anders Carlsson | 6026504 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 288 | bool ASTRecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, |
| 289 | uint64_t Offset) const { |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 290 | QualType T = FD->getType(); |
| 291 | if (const RecordType *RT = T->getAs<RecordType>()) { |
Anders Carlsson | 6026504 | 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 | fbbce49 | 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 | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 308 | uint64_t ElementOffset = Offset; |
Anders Carlsson | fbbce49 | 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 | 6026504 | 2009-09-25 00:02:51 +0000 | [diff] [blame] | 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 320 | void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD, |
| 321 | uint64_t Offset) { |
Anders Carlsson | 1345bd2 | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 322 | if (RD->isEmpty()) |
| 323 | EmptyClassOffsets.insert(std::make_pair(Offset, RD)); |
Anders Carlsson | ffbdefc | 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 | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 330 | assert(!I->getType()->isDependentType() && |
| 331 | "Cannot layout class with dependent bases."); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 332 | if (I->isVirtual()) |
| 333 | continue; |
Anders Carlsson | 1345bd2 | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 334 | |
Anders Carlsson | ffbdefc | 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 | 1eca99b | 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 | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Anders Carlsson | a4c6081 | 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 | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 379 | uint64_t ElementOffset = Offset; |
Anders Carlsson | a4c6081 | 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 | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 388 | uint64_t ASTRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *RD) { |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 389 | const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD); |
Anders Carlsson | 1345bd2 | 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 | 1345bd2 | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 394 | UpdateEmptyClassOffsets(RD, 0); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 395 | |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 396 | Size = std::max(Size, BaseInfo.getSize()); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 397 | |
Anders Carlsson | 1345bd2 | 2009-09-24 03:22:10 +0000 | [diff] [blame] | 398 | return 0; |
Anders Carlsson | b237fd6 | 2009-09-17 04:42:56 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Anders Carlsson | b2fafd4 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 401 | unsigned BaseAlign = BaseInfo.getNonVirtualAlign(); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 402 | |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 403 | // Round up the current record size to the base's alignment boundary. |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 404 | uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign); |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 405 | |
Anders Carlsson | ffbdefc | 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 | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 414 | if (!RD->isEmpty()) { |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 415 | // Update the data size. |
| 416 | DataSize = Offset + BaseInfo.getNonVirtualSize(); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 417 | |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 418 | Size = std::max(Size, DataSize); |
Anders Carlsson | 1eca99b | 2009-09-25 15:39:00 +0000 | [diff] [blame] | 419 | } else |
| 420 | Size = std::max(Size, Offset + BaseInfo.getSize()); |
| 421 | |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 422 | // Remember max struct/class alignment. |
| 423 | UpdateAlignment(BaseAlign); |
Anders Carlsson | ffbdefc | 2009-09-24 05:21:31 +0000 | [diff] [blame] | 424 | |
| 425 | UpdateEmptyClassOffsets(RD, Offset); |
Anders Carlsson | 9606149 | 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 | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 432 | uint64_t Offset = LayoutBase(RD); |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 433 | |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 434 | // Add base class offsets. |
Anders Carlsson | e4feb83 | 2009-09-22 00:04:45 +0000 | [diff] [blame] | 435 | if (IsVirtualBase) |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 436 | VBases.push_back(std::make_pair(RD, Offset)); |
Anders Carlsson | e4feb83 | 2009-09-22 00:04:45 +0000 | [diff] [blame] | 437 | else |
Anders Carlsson | 9606149 | 2009-09-24 03:13:30 +0000 | [diff] [blame] | 438 | Bases.push_back(std::make_pair(RD, Offset)); |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 441 | void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 442 | IsUnion = D->isUnion(); |
Anders Carlsson | a860e75 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 443 | |
Anders Carlsson | a5dd722 | 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 | a860e75 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 447 | if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) |
Anders Carlsson | a5dd722 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 448 | MaxFieldAlignment = PPA->getAlignment(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 450 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 451 | UpdateAlignment(AA->getMaxAlignment()); |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 452 | |
Mike Stump | 276b9f1 | 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 | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 454 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); |
| 455 | if (RD) { |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 456 | LayoutVtable(RD); |
Mike Stump | 2effeca | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 457 | // PrimaryBase goes first. |
Mike Stump | d53cef1 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 458 | if (PrimaryBase) { |
Mike Stump | d53cef1 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 459 | if (PrimaryBaseWasVirtual) |
Anders Carlsson | 3f06652 | 2009-09-22 03:02:06 +0000 | [diff] [blame] | 460 | IndirectPrimaryBases.insert(PrimaryBase); |
Mike Stump | 2153891 | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 461 | LayoutBaseNonVirtually(PrimaryBase, PrimaryBaseWasVirtual); |
Mike Stump | d53cef1 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 462 | } |
Mike Stump | 3dee6ef | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 463 | LayoutNonVirtualBases(RD); |
Mike Stump | 3dee6ef | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 464 | } |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 465 | |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 466 | LayoutFields(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Anders Carlsson | b2fafd4 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 468 | NonVirtualSize = Size; |
| 469 | NonVirtualAlignment = Alignment; |
Mike Stump | 3dee6ef | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 470 | |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 471 | if (RD) { |
| 472 | llvm::SmallSet<const CXXRecordDecl*, 32> mark; |
Mike Stump | 968db33 | 2009-11-05 04:02:15 +0000 | [diff] [blame] | 473 | LayoutVirtualBases(RD, RD, PrimaryBase, 0, mark, IndirectPrimaryBases); |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 474 | } |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 475 | |
Anders Carlsson | bda4c10 | 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 | 93fab9d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Anders Carlsson | 93fab9d | 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 | 243a685 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 490 | Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8); |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 491 | DataSize = Size; |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Anders Carlsson | a5dd722 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 494 | Packed = D->hasAttr<PackedAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Anders Carlsson | a5dd722 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 500 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 501 | UpdateAlignment(AA->getMaxAlignment()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Anders Carlsson | 93fab9d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Anders Carlsson | 93fab9d | 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 | a2df41c | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Anders Carlsson | a2df41c | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 518 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) |
| 519 | LayoutField(*Field); |
| 520 | } |
| 521 | |
Anders Carlsson | 42dbcc4 | 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 | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame^] | 524 | uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte); |
Anders Carlsson | 42dbcc4 | 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 | e4fc0d9 | 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 | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 562 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame^] | 563 | // Update the size. |
| 564 | Size = std::max(Size, DataSize); |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 565 | |
| 566 | // Remember max struct/class alignment. |
| 567 | UpdateAlignment(FieldAlign); |
| 568 | } |
| 569 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 570 | void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 571 | if (D->isBitField()) { |
| 572 | LayoutBitField(D); |
| 573 | return; |
| 574 | } |
| 575 | |
Anders Carlsson | e4fc0d9 | 2009-11-22 19:13:51 +0000 | [diff] [blame^] | 576 | // Reset the unfilled bits. |
| 577 | UnfilledBitsInLastByte = 0; |
| 578 | |
Anders Carlsson | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 579 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 580 | uint64_t FieldOffset = IsUnion ? 0 : DataSize; |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 581 | uint64_t FieldSize; |
| 582 | unsigned FieldAlign; |
Anders Carlsson | 42dbcc4 | 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 | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 596 | } else { |
Anders Carlsson | 42dbcc4 | 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 | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Anders Carlsson | 42dbcc4 | 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 | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 627 | // Place this field at the current location. |
| 628 | FieldOffsets.push_back(FieldOffset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Anders Carlsson | bda4c10 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 636 | // Update the data size. |
| 637 | DataSize = Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Anders Carlsson | bda4c10 | 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 | 42dbcc4 | 2009-11-22 17:37:31 +0000 | [diff] [blame] | 649 | Size = llvm::RoundUpToAlignment(Size, Alignment); |
Anders Carlsson | bda4c10 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 656 | assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 658 | Alignment = NewAlignment; |
| 659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 661 | const ASTRecordLayout * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, |
Anders Carlsson | bda4c10 | 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 | b2fafd4 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | Builder.FieldOffsets.data(), |
Anders Carlsson | b2fafd4 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 671 | Builder.FieldOffsets.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
Anders Carlsson | b2fafd4 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | |
Anders Carlsson | b2fafd4 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 678 | // FIXME: This should be done in FinalizeLayout. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | uint64_t DataSize = |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 680 | IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | uint64_t NonVirtualSize = |
Anders Carlsson | b2fafd4 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 682 | IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Anders Carlsson | 74cbe22 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 684 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | Builder.FieldOffsets.data(), |
Anders Carlsson | b2fafd4 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 686 | Builder.FieldOffsets.size(), |
| 687 | NonVirtualSize, |
| 688 | Builder.NonVirtualAlignment, |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 689 | Builder.PrimaryBase, |
Mike Stump | 928f150 | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 690 | Builder.PrimaryBaseWasVirtual, |
Anders Carlsson | b2fafd4 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 691 | Builder.Bases.data(), |
Mike Stump | 2153891 | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 692 | Builder.Bases.size(), |
| 693 | Builder.VBases.data(), |
Mike Stump | 2153891 | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 694 | Builder.VBases.size()); |
Anders Carlsson | 93fab9d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 703 | Builder.Layout(D, Impl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
Anders Carlsson | 93fab9d | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 705 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, |
Anders Carlsson | a223935 | 2009-09-26 01:34:51 +0000 | [diff] [blame] | 706 | Builder.DataSize, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | Builder.FieldOffsets.data(), |
Anders Carlsson | bda4c10 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 708 | Builder.FieldOffsets.size()); |
| 709 | } |