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 | |
| 24 | ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx) |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 25 | : Ctx(Ctx), Size(0), Alignment(8), Packed(false), MaxFieldAlignment(0), |
| 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 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 46 | void |
| 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()) { |
| 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 | 78696a7 | 2009-08-11 04:03:59 +0000 | [diff] [blame] | 86 | const CXXRecordDecl *Base = |
| 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) { |
| 100 | const CXXRecordDecl *Base = |
| 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) { |
| 128 | const CXXRecordDecl *Base = |
| 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()) { |
| 138 | const CXXRecordDecl *Base = |
| 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, |
| 171 | llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) { |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 172 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 173 | e = RD->bases_end(); i != e; ++i) { |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 174 | const CXXRecordDecl *Base = |
| 175 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 176 | if (i->isVirtual() && !IndirectPrimary.count(Base)) { |
| 177 | // Mark it so we don't output it twice. |
| 178 | IndirectPrimary.insert(Base); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 179 | LayoutVirtualBase(Base); |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 180 | } |
| 181 | if (Base->getNumVBases()) |
| 182 | LayoutVirtualBases(Base, IndirectPrimary); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame^] | 186 | void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD, |
| 187 | bool IsVirtualBase) { |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 188 | const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD); |
| 189 | assert(BaseInfo.getDataSize() > 0 && |
| 190 | "FIXME: Handle empty classes."); |
| 191 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 192 | unsigned BaseAlign = BaseInfo.getNonVirtualAlign(); |
| 193 | uint64_t BaseSize = BaseInfo.getNonVirtualSize(); |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 194 | |
| 195 | // Round up the current record size to the base's alignment boundary. |
| 196 | Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1); |
| 197 | |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame^] | 198 | // Add base class offsets. |
| 199 | if (IsVirtualBase) { |
| 200 | VBases.push_back(RD); |
| 201 | VBaseOffsets.push_back(Size); |
| 202 | } else { |
| 203 | Bases.push_back(RD); |
| 204 | BaseOffsets.push_back(Size); |
| 205 | } |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 206 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 207 | // Reserve space for this base. |
| 208 | Size += BaseSize; |
| 209 | |
| 210 | // Remember the next available offset. |
| 211 | NextOffset = Size; |
| 212 | |
| 213 | // Remember max struct/class alignment. |
| 214 | UpdateAlignment(BaseAlign); |
| 215 | } |
| 216 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 217 | void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) { |
| 218 | IsUnion = D->isUnion(); |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 219 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 220 | Packed = D->hasAttr<PackedAttr>(); |
| 221 | |
| 222 | // The #pragma pack attribute specifies the maximum field alignment. |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 223 | if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 224 | MaxFieldAlignment = PPA->getAlignment(); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 225 | |
| 226 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 227 | UpdateAlignment(AA->getAlignment()); |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 228 | |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 229 | llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary; |
| 230 | |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 231 | // If this is a C++ class, lay out the nonvirtual bases. |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 232 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); |
| 233 | if (RD) { |
Mike Stump | c2f591b | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 234 | LayoutVtable(RD, IndirectPrimary); |
Mike Stump | 2509480 | 2009-08-06 00:38:46 +0000 | [diff] [blame] | 235 | // PrimaryBase goes first. |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 236 | if (PrimaryBase) { |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 237 | if (PrimaryBaseWasVirtual) |
| 238 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame^] | 239 | LayoutBaseNonVirtually(PrimaryBase, PrimaryBaseWasVirtual); |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 240 | } |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 241 | LayoutNonVirtualBases(RD); |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 242 | } |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 243 | |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 244 | LayoutFields(D); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 245 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 246 | NonVirtualSize = Size; |
| 247 | NonVirtualAlignment = Alignment; |
Mike Stump | 3dc7eb9 | 2009-07-30 00:22:38 +0000 | [diff] [blame] | 248 | |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 249 | if (RD) |
Mike Stump | 1300754 | 2009-08-13 02:02:14 +0000 | [diff] [blame] | 250 | LayoutVirtualBases(RD, IndirectPrimary); |
Mike Stump | 6b2556f | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 251 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 252 | // Finally, round the size of the total struct up to the alignment of the |
| 253 | // struct itself. |
| 254 | FinishLayout(); |
| 255 | } |
| 256 | |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 257 | void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D, |
| 258 | const ObjCImplementationDecl *Impl) { |
| 259 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
| 260 | const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD); |
| 261 | |
| 262 | UpdateAlignment(SL.getAlignment()); |
| 263 | |
| 264 | // We start laying out ivars not at the end of the superclass |
| 265 | // structure, but at the next byte following the last field. |
Anders Carlsson | 27b5013 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 266 | Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 267 | NextOffset = Size; |
| 268 | } |
| 269 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 270 | Packed = D->hasAttr<PackedAttr>(); |
| 271 | |
| 272 | // The #pragma pack attribute specifies the maximum field alignment. |
| 273 | if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>()) |
| 274 | MaxFieldAlignment = PPA->getAlignment(); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 275 | |
| 276 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 277 | UpdateAlignment(AA->getAlignment()); |
| 278 | |
| 279 | // Layout each ivar sequentially. |
| 280 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 281 | Ctx.ShallowCollectObjCIvars(D, Ivars, Impl); |
| 282 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) |
| 283 | LayoutField(Ivars[i]); |
| 284 | |
| 285 | // Finally, round the size of the total struct up to the alignment of the |
| 286 | // struct itself. |
| 287 | FinishLayout(); |
| 288 | } |
| 289 | |
Anders Carlsson | 118ce16 | 2009-07-18 21:48:39 +0000 | [diff] [blame] | 290 | void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 291 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 292 | // the future, this will need to be tweakable by targets. |
| 293 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 294 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field) |
| 295 | LayoutField(*Field); |
| 296 | } |
| 297 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 298 | void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) { |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 299 | bool FieldPacked = Packed; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 300 | uint64_t FieldOffset = IsUnion ? 0 : Size; |
| 301 | uint64_t FieldSize; |
| 302 | unsigned FieldAlign; |
| 303 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 304 | FieldPacked |= D->hasAttr<PackedAttr>(); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 305 | |
| 306 | if (const Expr *BitWidthExpr = D->getBitWidth()) { |
| 307 | // TODO: Need to check this algorithm on other targets! |
| 308 | // (tested on Linux-X86) |
| 309 | FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue(); |
| 310 | |
| 311 | std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType()); |
| 312 | uint64_t TypeSize = FieldInfo.first; |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 313 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 314 | FieldAlign = FieldInfo.second; |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 315 | |
| 316 | if (FieldPacked) |
| 317 | FieldAlign = 1; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 318 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 319 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 320 | // The maximum field alignment overrides the aligned attribute. |
| 321 | if (MaxFieldAlignment) |
| 322 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
| 323 | |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 324 | // Check if we need to add padding to give the field the correct |
| 325 | // alignment. |
| 326 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 327 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 328 | |
| 329 | // Padding members don't affect overall alignment |
| 330 | if (!D->getIdentifier()) |
| 331 | FieldAlign = 1; |
| 332 | } else { |
| 333 | if (D->getType()->isIncompleteArrayType()) { |
| 334 | // This is a flexible array member; we can't directly |
| 335 | // query getTypeInfo about these, so we figure it out here. |
| 336 | // Flexible array members don't have any size, but they |
| 337 | // have to be aligned appropriately for their element type. |
| 338 | FieldSize = 0; |
| 339 | const ArrayType* ATy = Ctx.getAsArrayType(D->getType()); |
| 340 | FieldAlign = Ctx.getTypeAlign(ATy->getElementType()); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 341 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 342 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
| 343 | FieldSize = Ctx.Target.getPointerWidth(AS); |
| 344 | FieldAlign = Ctx.Target.getPointerAlign(AS); |
| 345 | } else { |
| 346 | std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType()); |
| 347 | FieldSize = FieldInfo.first; |
| 348 | FieldAlign = FieldInfo.second; |
| 349 | } |
| 350 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 351 | if (FieldPacked) |
| 352 | FieldAlign = 8; |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 353 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 354 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 355 | // The maximum field alignment overrides the aligned attribute. |
| 356 | if (MaxFieldAlignment) |
| 357 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 358 | |
| 359 | // Round up the current record size to the field's alignment boundary. |
| 360 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 361 | } |
| 362 | |
| 363 | // Place this field at the current location. |
| 364 | FieldOffsets.push_back(FieldOffset); |
| 365 | |
| 366 | // Reserve space for this field. |
| 367 | if (IsUnion) |
| 368 | Size = std::max(Size, FieldSize); |
| 369 | else |
| 370 | Size = FieldOffset + FieldSize; |
| 371 | |
| 372 | // Remember the next available offset. |
| 373 | NextOffset = Size; |
| 374 | |
| 375 | // Remember max struct/class alignment. |
| 376 | UpdateAlignment(FieldAlign); |
| 377 | } |
| 378 | |
| 379 | void ASTRecordLayoutBuilder::FinishLayout() { |
| 380 | // In C++, records cannot be of size 0. |
| 381 | if (Ctx.getLangOptions().CPlusPlus && Size == 0) |
| 382 | Size = 8; |
| 383 | // Finally, round the size of the record up to the alignment of the |
| 384 | // record itself. |
| 385 | Size = (Size + (Alignment-1)) & ~(Alignment-1); |
| 386 | } |
| 387 | |
| 388 | void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) { |
| 389 | if (NewAlignment <= Alignment) |
| 390 | return; |
| 391 | |
| 392 | assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2")); |
| 393 | |
| 394 | Alignment = NewAlignment; |
| 395 | } |
| 396 | |
| 397 | const ASTRecordLayout * |
| 398 | ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, |
| 399 | const RecordDecl *D) { |
| 400 | ASTRecordLayoutBuilder Builder(Ctx); |
| 401 | |
| 402 | Builder.Layout(D); |
| 403 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 404 | if (!isa<CXXRecordDecl>(D)) |
| 405 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size, |
| 406 | Builder.FieldOffsets.data(), |
| 407 | Builder.FieldOffsets.size()); |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 408 | |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 409 | // FIXME: This is not always correct. See the part about bitfields at |
| 410 | // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info. |
| 411 | // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout. |
| 412 | bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD(); |
| 413 | |
| 414 | assert(Builder.Bases.size() == Builder.BaseOffsets.size() && |
| 415 | "Base offsets vector must be same size as bases vector!"); |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame^] | 416 | assert(Builder.VBases.size() == Builder.VBaseOffsets.size() && |
| 417 | "Base offsets vector must be same size as bases vector!"); |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 418 | |
| 419 | // FIXME: This should be done in FinalizeLayout. |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 420 | uint64_t DataSize = |
| 421 | IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset; |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 422 | uint64_t NonVirtualSize = |
| 423 | IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize; |
Anders Carlsson | 6d9f6f3 | 2009-07-19 00:18:47 +0000 | [diff] [blame] | 424 | |
| 425 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize, |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 426 | Builder.FieldOffsets.data(), |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 427 | Builder.FieldOffsets.size(), |
| 428 | NonVirtualSize, |
| 429 | Builder.NonVirtualAlignment, |
Mike Stump | d8fe7b2 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 430 | Builder.PrimaryBase, |
Mike Stump | c266c6d | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 431 | Builder.PrimaryBaseWasVirtual, |
Anders Carlsson | fc8cfa8 | 2009-07-28 19:24:15 +0000 | [diff] [blame] | 432 | Builder.Bases.data(), |
| 433 | Builder.BaseOffsets.data(), |
Mike Stump | bcf756c | 2009-08-14 01:44:03 +0000 | [diff] [blame^] | 434 | Builder.Bases.size(), |
| 435 | Builder.VBases.data(), |
| 436 | Builder.VBaseOffsets.data(), |
| 437 | Builder.VBases.size()); |
Anders Carlsson | 4f51628 | 2009-07-18 20:50:59 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | const ASTRecordLayout * |
| 441 | ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx, |
| 442 | const ObjCInterfaceDecl *D, |
| 443 | const ObjCImplementationDecl *Impl) { |
| 444 | ASTRecordLayoutBuilder Builder(Ctx); |
| 445 | |
| 446 | Builder.Layout(D, Impl); |
| 447 | |
| 448 | return new ASTRecordLayout(Builder.Size, Builder.Alignment, |
| 449 | Builder.NextOffset, |
Anders Carlsson | 7947433 | 2009-07-18 20:20:21 +0000 | [diff] [blame] | 450 | Builder.FieldOffsets.data(), |
| 451 | Builder.FieldOffsets.size()); |
| 452 | } |