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) |
| 25 | : Ctx(Ctx), Size(0), Alignment(8), Packed(false), MaxFieldAlignment(0), |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 26 | NextOffset(0), IsUnion(false), NonVirtualSize(0), NonVirtualAlignment(8) {} |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 27 | |
Mike Stump | 2509480 | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 28 | /// LayoutVtable - Lay out the vtable and set PrimaryBase. |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 29 | void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD, |
| 30 | llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) { |
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 | c266c6d | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 33 | setPrimaryBase(0, false); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 34 | return; |
Mike Stump | 2509480 | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 35 | } |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 36 | |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 37 | SelectPrimaryBase(RD, IndirectPrimary); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 38 | if (PrimaryBase == 0) { |
| 39 | int AS = 0; |
| 40 | UpdateAlignment(Ctx.Target.getPointerAlign(AS)); |
| 41 | Size += Ctx.Target.getPointerWidth(AS); |
| 42 | NextOffset = Size; |
| 43 | } |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | void |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 47 | ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) { |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 48 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 49 | e = RD->bases_end(); i != e; ++i) { |
| 50 | if (!i->isVirtual()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | const CXXRecordDecl *Base = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 52 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 2509480 | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 53 | // Skip the PrimaryBase here, as it is laid down first. |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 54 | if (Base != PrimaryBase || PrimaryBaseWasVirtual) |
| 55 | LayoutBaseNonVirtually(Base, false); |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 60 | // Helper routines related to the abi definition from: |
| 61 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 62 | // |
| 63 | /// IsNearlyEmpty - Indicates when a class has a vtable pointer, but |
| 64 | /// no other data. |
| 65 | bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) { |
| 66 | // FIXME: Audit the corners |
| 67 | if (!RD->isDynamicClass()) |
| 68 | return false; |
| 69 | const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD); |
| 70 | if (BaseInfo.getNonVirtualSize() == Ctx.Target.getPointerWidth(0)) |
| 71 | return true; |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | void ASTRecordLayoutBuilder::SelectPrimaryForBase(const CXXRecordDecl *RD, |
| 76 | llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) { |
Mike Stump | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 77 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD); |
| 78 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 79 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 80 | |
| 81 | if (PrimaryBaseWasVirtual) |
Mike Stump | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 82 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 83 | |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 84 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 85 | e = RD->bases_end(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | const CXXRecordDecl *Base = |
Mike Stump | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 87 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 88 | // Only bases with virtual bases participate in computing the |
| 89 | // indirect primary virtual base classes. |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 90 | if (Base->getNumVBases()) |
| 91 | SelectPrimaryForBase(Base, IndirectPrimary); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 95 | void ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD, |
| 96 | const CXXRecordDecl *&FirstPrimary, |
| 97 | llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) { |
| 98 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 99 | e = RD->bases_end(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | const CXXRecordDecl *Base = |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 101 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 102 | if (!i->isVirtual()) { |
| 103 | SelectPrimaryVBase(Base, FirstPrimary, IndirectPrimary); |
| 104 | if (PrimaryBase) |
| 105 | return; |
| 106 | continue; |
| 107 | } |
| 108 | if (IsNearlyEmpty(Base)) { |
| 109 | if (FirstPrimary==0) |
| 110 | FirstPrimary = Base; |
| 111 | if (!IndirectPrimary.count(Base)) { |
| 112 | setPrimaryBase(Base, true); |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 119 | /// SelectPrimaryBase - Selects the primary base for the given class and |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 120 | /// record that with setPrimaryBase. We also calculate the IndirectPrimaries. |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 121 | void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD, |
| 122 | llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) { |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 123 | // We compute all the primary virtual bases for all of our direct and |
| 124 | // indirect bases, and record all their primary virtual base classes. |
| 125 | const CXXRecordDecl *FirstPrimary = 0; |
| 126 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 127 | e = RD->bases_end(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | const CXXRecordDecl *Base = |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 129 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 130 | SelectPrimaryForBase(Base, IndirectPrimary); |
| 131 | } |
| 132 | |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 133 | // The primary base is the first non-virtual indirect or direct base class, |
| 134 | // if one exists. |
| 135 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 136 | e = RD->bases_end(); i != e; ++i) { |
| 137 | if (!i->isVirtual()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | const CXXRecordDecl *Base = |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 139 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 140 | if (Base->isDynamicClass()) { |
Mike Stump | c266c6d | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 141 | setPrimaryBase(Base, false); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 147 | setPrimaryBase(0, false); |
| 148 | |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 149 | // 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] | 150 | // indirect primary virtual base class, if one exists. |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 151 | |
| 152 | // If we have no virtual bases at this point, bail out as the searching below |
| 153 | // is expensive. |
Mike Stump | 590a7c7 | 2009-08-13 23:26:06 +0000 | [diff] [blame] | 154 | if (RD->getNumVBases() == 0) |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 155 | return; |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 156 | |
| 157 | // Then we can search for the first nearly empty virtual base itself. |
Mike Stump | 6f3793b | 2009-08-12 21:50:08 +0000 | [diff] [blame] | 158 | SelectPrimaryVBase(RD, FirstPrimary, IndirectPrimary); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 159 | |
Mike Stump | c266c6d | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 160 | // Otherwise if is the first nearly empty virtual base, if one exists, |
| 161 | // otherwise there is no primary base class. |
| 162 | setPrimaryBase(FirstPrimary, true); |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 166 | void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) { |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 167 | LayoutBaseNonVirtually(RD, true); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 170 | void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 171 | const CXXRecordDecl *PB, |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 172 | int64_t Offset, |
| 173 | llvm::SmallSet<const CXXRecordDecl*, 32> &mark, |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 174 | llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) { |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 175 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 176 | e = RD->bases_end(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | const CXXRecordDecl *Base = |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 178 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 179 | #if 0 |
| 180 | const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base); |
| 181 | const CXXRecordDecl *PB = L.getPrimaryBase(); |
| 182 | if (PB && L.getPrimaryBaseWasVirtual() |
| 183 | && IndirectPrimary.count(PB)) { |
| 184 | int64_t BaseOffset; |
| 185 | // FIXME: calculate this. |
| 186 | BaseOffset = (1<<63) | (1<<31); |
| 187 | VBases.push_back(PB); |
| 188 | VBaseOffsets.push_back(BaseOffset); |
| 189 | } |
| 190 | #endif |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 191 | int64_t BaseOffset = Offset;; |
| 192 | // FIXME: Calculate BaseOffset. |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 193 | if (i->isVirtual()) { |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 194 | if (Base == PB) { |
| 195 | // Only lay things out once. |
| 196 | if (mark.count(Base)) |
| 197 | continue; |
| 198 | // Mark it so we don't lay it out twice. |
| 199 | mark.insert(Base); |
| 200 | assert (IndirectPrimary.count(Base) && "IndirectPrimary was wrong"); |
Anders Carlsson | d6020c3 | 2009-09-22 00:04:45 +0000 | [diff] [blame^] | 201 | VBases.push_back(std::make_pair(Base, Offset)); |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 202 | } else if (IndirectPrimary.count(Base)) { |
| 203 | // Someone else will eventually lay this out. |
| 204 | ; |
| 205 | } else { |
| 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); |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 211 | LayoutVirtualBase(Base); |
Anders Carlsson | d6020c3 | 2009-09-22 00:04:45 +0000 | [diff] [blame^] | 212 | BaseOffset = VBases.back().second; |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 213 | } |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 214 | } |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 215 | if (Base->getNumVBases()) { |
| 216 | const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base); |
| 217 | const CXXRecordDecl *PB = L.getPrimaryBase(); |
| 218 | LayoutVirtualBases(Base, PB, BaseOffset, mark, IndirectPrimary); |
| 219 | } |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 223 | void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD, |
| 224 | bool IsVirtualBase) { |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 225 | const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD); |
Anders Carlsson | 93b6d5e | 2009-09-17 04:42:56 +0000 | [diff] [blame] | 226 | if (!Bases.empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | assert(BaseInfo.getDataSize() > 0 && |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 228 | "FIXME: Handle empty classes."); |
Anders Carlsson | 93b6d5e | 2009-09-17 04:42:56 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 231 | unsigned BaseAlign = BaseInfo.getNonVirtualAlign(); |
| 232 | uint64_t BaseSize = BaseInfo.getNonVirtualSize(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 234 | // Round up the current record size to the base's alignment boundary. |
| 235 | Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1); |
| 236 | |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 237 | // Add base class offsets. |
Anders Carlsson | d6020c3 | 2009-09-22 00:04:45 +0000 | [diff] [blame^] | 238 | if (IsVirtualBase) |
| 239 | VBases.push_back(std::make_pair(RD, Size)); |
| 240 | else |
| 241 | Bases.push_back(std::make_pair(RD, Size)); |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 242 | |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 243 | #if 0 |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 244 | // And now add offsets for all our primary virtual bases as well, so |
| 245 | // they all have offsets. |
| 246 | const ASTRecordLayout *L = &BaseInfo; |
| 247 | const CXXRecordDecl *PB = L->getPrimaryBase(); |
| 248 | while (PB) { |
| 249 | if (L->getPrimaryBaseWasVirtual()) { |
| 250 | VBases.push_back(PB); |
| 251 | VBaseOffsets.push_back(Size); |
| 252 | } |
| 253 | PB = L->getPrimaryBase(); |
| 254 | if (PB) |
| 255 | L = &Ctx.getASTRecordLayout(PB); |
| 256 | } |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 257 | #endif |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 258 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 259 | // Reserve space for this base. |
| 260 | Size += BaseSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 262 | // Remember the next available offset. |
| 263 | NextOffset = Size; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 265 | // Remember max struct/class alignment. |
| 266 | UpdateAlignment(BaseAlign); |
| 267 | } |
| 268 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 269 | void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 270 | IsUnion = D->isUnion(); |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 271 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 272 | Packed = D->hasAttr<PackedAttr>(); |
| 273 | |
| 274 | // The #pragma pack attribute specifies the maximum field alignment. |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 275 | if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 276 | MaxFieldAlignment = PPA->getAlignment(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 278 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 279 | UpdateAlignment(AA->getAlignment()); |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 280 | |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 281 | llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary; |
| 282 | |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 283 | // 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] | 284 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); |
| 285 | if (RD) { |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 286 | LayoutVtable(RD, IndirectPrimary); |
Mike Stump | 2509480 | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 287 | // PrimaryBase goes first. |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 288 | if (PrimaryBase) { |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 289 | if (PrimaryBaseWasVirtual) |
| 290 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 291 | LayoutBaseNonVirtually(PrimaryBase, PrimaryBaseWasVirtual); |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 292 | } |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 293 | LayoutNonVirtualBases(RD); |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 294 | } |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 295 | |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 296 | LayoutFields(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 298 | NonVirtualSize = Size; |
| 299 | NonVirtualAlignment = Alignment; |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 300 | |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 301 | if (RD) { |
| 302 | llvm::SmallSet<const CXXRecordDecl*, 32> mark; |
Mike Stump | 996576f3 | 2009-08-16 19:04:13 +0000 | [diff] [blame] | 303 | LayoutVirtualBases(RD, PrimaryBase, 0, mark, IndirectPrimary); |
Mike Stump | 22ea1f8 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 304 | } |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 305 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 306 | // Finally, round the size of the total struct up to the alignment of the |
| 307 | // struct itself. |
| 308 | FinishLayout(); |
| 309 | } |
| 310 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 311 | void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D, |
| 312 | const ObjCImplementationDecl *Impl) { |
| 313 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
| 314 | const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD); |
| 315 | |
| 316 | UpdateAlignment(SL.getAlignment()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 318 | // We start laying out ivars not at the end of the superclass |
| 319 | // structure, but at the next byte following the last field. |
Anders Carlsson | 27b5013 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 320 | Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 321 | NextOffset = Size; |
| 322 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 324 | Packed = D->hasAttr<PackedAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 326 | // The #pragma pack attribute specifies the maximum field alignment. |
| 327 | if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) |
| 328 | MaxFieldAlignment = PPA->getAlignment(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 330 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 331 | UpdateAlignment(AA->getAlignment()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 333 | // Layout each ivar sequentially. |
| 334 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 335 | Ctx.ShallowCollectObjCIvars(D, Ivars, Impl); |
| 336 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) |
| 337 | LayoutField(Ivars[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 339 | // Finally, round the size of the total struct up to the alignment of the |
| 340 | // struct itself. |
| 341 | FinishLayout(); |
| 342 | } |
| 343 | |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 344 | void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 345 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 346 | // the future, this will need to be tweakable by targets. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | for (RecordDecl::field_iterator Field = D->field_begin(), |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 348 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) |
| 349 | LayoutField(*Field); |
| 350 | } |
| 351 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 352 | void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 353 | bool FieldPacked = Packed; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 354 | uint64_t FieldOffset = IsUnion ? 0 : Size; |
| 355 | uint64_t FieldSize; |
| 356 | unsigned FieldAlign; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
| 358 | FieldPacked |= D->hasAttr<PackedAttr>(); |
| 359 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 360 | if (const Expr *BitWidthExpr = D->getBitWidth()) { |
| 361 | // TODO: Need to check this algorithm on other targets! |
| 362 | // (tested on Linux-X86) |
| 363 | FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 365 | std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType()); |
| 366 | uint64_t TypeSize = FieldInfo.first; |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 367 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 368 | FieldAlign = FieldInfo.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 370 | if (FieldPacked) |
| 371 | FieldAlign = 1; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 372 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 373 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 374 | // The maximum field alignment overrides the aligned attribute. |
| 375 | if (MaxFieldAlignment) |
| 376 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 377 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 378 | // Check if we need to add padding to give the field the correct |
| 379 | // alignment. |
| 380 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 381 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 383 | // Padding members don't affect overall alignment |
| 384 | if (!D->getIdentifier()) |
| 385 | FieldAlign = 1; |
| 386 | } else { |
| 387 | if (D->getType()->isIncompleteArrayType()) { |
| 388 | // This is a flexible array member; we can't directly |
| 389 | // query getTypeInfo about these, so we figure it out here. |
| 390 | // Flexible array members don't have any size, but they |
| 391 | // have to be aligned appropriately for their element type. |
| 392 | FieldSize = 0; |
| 393 | const ArrayType* ATy = Ctx.getAsArrayType(D->getType()); |
| 394 | FieldAlign = Ctx.getTypeAlign(ATy->getElementType()); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 395 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 396 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
| 397 | FieldSize = Ctx.Target.getPointerWidth(AS); |
| 398 | FieldAlign = Ctx.Target.getPointerAlign(AS); |
| 399 | } else { |
| 400 | std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType()); |
| 401 | FieldSize = FieldInfo.first; |
| 402 | FieldAlign = FieldInfo.second; |
| 403 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 405 | if (FieldPacked) |
| 406 | FieldAlign = 8; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 407 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 408 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 409 | // The maximum field alignment overrides the aligned attribute. |
| 410 | if (MaxFieldAlignment) |
| 411 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 413 | // Round up the current record size to the field's alignment boundary. |
| 414 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 415 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 417 | // Place this field at the current location. |
| 418 | FieldOffsets.push_back(FieldOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 420 | // Reserve space for this field. |
| 421 | if (IsUnion) |
| 422 | Size = std::max(Size, FieldSize); |
| 423 | else |
| 424 | Size = FieldOffset + FieldSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 426 | // Remember the next available offset. |
| 427 | NextOffset = Size; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 429 | // Remember max struct/class alignment. |
| 430 | UpdateAlignment(FieldAlign); |
| 431 | } |
| 432 | |
| 433 | void ASTRecordLayoutBuilder::FinishLayout() { |
| 434 | // In C++, records cannot be of size 0. |
| 435 | if (Ctx.getLangOptions().CPlusPlus && Size == 0) |
| 436 | Size = 8; |
| 437 | // Finally, round the size of the record up to the alignment of the |
| 438 | // record itself. |
| 439 | Size = (Size + (Alignment-1)) & ~(Alignment-1); |
| 440 | } |
| 441 | |
| 442 | void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) { |
| 443 | if (NewAlignment <= Alignment) |
| 444 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 446 | assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2")); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 448 | Alignment = NewAlignment; |
| 449 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 451 | const ASTRecordLayout * |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 453 | const RecordDecl *D) { |
| 454 | ASTRecordLayoutBuilder Builder(Ctx); |
| 455 | |
| 456 | Builder.Layout(D); |
| 457 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 458 | if (!isa<CXXRecordDecl>(D)) |
| 459 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | Builder.FieldOffsets.data(), |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 461 | Builder.FieldOffsets.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 463 | // FIXME: This is not always correct. See the part about bitfields at |
| 464 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 465 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
| 466 | bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD(); |
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 | // FIXME: This should be done in FinalizeLayout. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | uint64_t DataSize = |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 470 | IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | uint64_t NonVirtualSize = |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 472 | IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 474 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | Builder.FieldOffsets.data(), |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 476 | Builder.FieldOffsets.size(), |
| 477 | NonVirtualSize, |
| 478 | Builder.NonVirtualAlignment, |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 479 | Builder.PrimaryBase, |
Mike Stump | c266c6d | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 480 | Builder.PrimaryBaseWasVirtual, |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 481 | Builder.Bases.data(), |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 482 | Builder.Bases.size(), |
| 483 | Builder.VBases.data(), |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 484 | Builder.VBases.size()); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | const ASTRecordLayout * |
| 488 | ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, |
| 489 | const ObjCInterfaceDecl *D, |
| 490 | const ObjCImplementationDecl *Impl) { |
| 491 | ASTRecordLayoutBuilder Builder(Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 493 | Builder.Layout(D, Impl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 495 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, |
| 496 | Builder.NextOffset, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | Builder.FieldOffsets.data(), |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 498 | Builder.FieldOffsets.size()); |
| 499 | } |