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