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