Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===// |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 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 | // |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 10 | // Builder implementation for CGRecordLayout objects. |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 14 | #include "CGRecordLayout.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
| 16 | #include "CodeGenTypes.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Attr.h" |
Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | d3f3d93 | 2011-06-21 18:54:46 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
John McCall | bcd3821 | 2010-11-30 23:17:27 +0000 | [diff] [blame] | 32 | namespace { |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 33 | |
| 34 | class CGRecordLayoutBuilder { |
| 35 | public: |
| 36 | /// FieldTypes - Holds the LLVM types that the struct is created from. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 37 | /// |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 38 | SmallVector<llvm::Type *, 16> FieldTypes; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 39 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 40 | /// BaseSubobjectType - Holds the LLVM type for the non-virtual part |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 41 | /// of the struct. For example, consider: |
| 42 | /// |
| 43 | /// struct A { int i; }; |
| 44 | /// struct B { void *v; }; |
| 45 | /// struct C : virtual A, B { }; |
| 46 | /// |
| 47 | /// The LLVM type of C will be |
| 48 | /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B } |
| 49 | /// |
| 50 | /// And the LLVM type of the non-virtual base struct will be |
| 51 | /// %struct.C.base = type { i32 (...)**, %struct.A, i32 } |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 52 | /// |
| 53 | /// This only gets initialized if the base subobject type is |
| 54 | /// different from the complete-object type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 55 | llvm::StructType *BaseSubobjectType; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 56 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 57 | /// FieldInfo - Holds a field and its corresponding LLVM field number. |
| 58 | llvm::DenseMap<const FieldDecl *, unsigned> Fields; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 59 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 60 | /// BitFieldInfo - Holds location and size information about a bit field. |
| 61 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 62 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 63 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
| 64 | llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; |
Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 65 | |
| 66 | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
| 67 | /// primary base classes for some other direct or indirect base class. |
| 68 | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
| 69 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 70 | /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid |
| 71 | /// avoid laying out virtual bases more than once. |
| 72 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases; |
| 73 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 74 | /// IsZeroInitializable - Whether this struct can be C++ |
| 75 | /// zero-initialized with an LLVM zeroinitializer. |
| 76 | bool IsZeroInitializable; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 77 | bool IsZeroInitializableAsBase; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 78 | |
| 79 | /// Packed - Whether the resulting LLVM struct will be packed or not. |
| 80 | bool Packed; |
| 81 | |
| 82 | private: |
| 83 | CodeGenTypes &Types; |
| 84 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 85 | /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the |
| 86 | /// last base laid out. Used so that we can replace the last laid out base |
| 87 | /// type with an i8 array if needed. |
| 88 | struct LastLaidOutBaseInfo { |
| 89 | CharUnits Offset; |
| 90 | CharUnits NonVirtualSize; |
| 91 | |
| 92 | bool isValid() const { return !NonVirtualSize.isZero(); } |
| 93 | void invalidate() { NonVirtualSize = CharUnits::Zero(); } |
| 94 | |
| 95 | } LastLaidOutBase; |
| 96 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 97 | /// Alignment - Contains the alignment of the RecordDecl. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 98 | CharUnits Alignment; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 99 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 100 | /// NextFieldOffset - Holds the next field offset. |
| 101 | CharUnits NextFieldOffset; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 102 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 103 | /// LayoutUnionField - Will layout a field in an union and return the type |
| 104 | /// that the field will have. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 105 | llvm::Type *LayoutUnionField(const FieldDecl *Field, |
| 106 | const ASTRecordLayout &Layout); |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 107 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 108 | /// LayoutUnion - Will layout a union RecordDecl. |
| 109 | void LayoutUnion(const RecordDecl *D); |
| 110 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 111 | /// Lay out a sequence of contiguous bitfields. |
| 112 | bool LayoutBitfields(const ASTRecordLayout &Layout, |
| 113 | unsigned &FirstFieldNo, |
| 114 | RecordDecl::field_iterator &FI, |
| 115 | RecordDecl::field_iterator FE); |
| 116 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 117 | /// LayoutField - try to layout all fields in the record decl. |
| 118 | /// Returns false if the operation failed because the struct is not packed. |
| 119 | bool LayoutFields(const RecordDecl *D); |
| 120 | |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 121 | /// Layout a single base, virtual or non-virtual |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 122 | bool LayoutBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 123 | const CGRecordLayout &baseLayout, |
| 124 | CharUnits baseOffset); |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 125 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 126 | /// LayoutVirtualBase - layout a single virtual base. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 127 | bool LayoutVirtualBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 128 | CharUnits baseOffset); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 129 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 130 | /// LayoutVirtualBases - layout the virtual bases of a record decl. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 131 | bool LayoutVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 132 | const ASTRecordLayout &Layout); |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 133 | |
| 134 | /// MSLayoutVirtualBases - layout the virtual bases of a record decl, |
| 135 | /// like MSVC. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 136 | bool MSLayoutVirtualBases(const CXXRecordDecl *RD, |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 137 | const ASTRecordLayout &Layout); |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 139 | /// LayoutNonVirtualBase - layout a single non-virtual base. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 140 | bool LayoutNonVirtualBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 141 | CharUnits baseOffset); |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 142 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 143 | /// LayoutNonVirtualBases - layout the virtual bases of a record decl. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 144 | bool LayoutNonVirtualBases(const CXXRecordDecl *RD, |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 145 | const ASTRecordLayout &Layout); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 146 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 147 | /// ComputeNonVirtualBaseType - Compute the non-virtual base field types. |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 148 | bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 150 | /// LayoutField - layout a single field. Returns false if the operation failed |
| 151 | /// because the current struct is not packed. |
| 152 | bool LayoutField(const FieldDecl *D, uint64_t FieldOffset); |
| 153 | |
| 154 | /// LayoutBitField - layout a single bit field. |
| 155 | void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset); |
| 156 | |
| 157 | /// AppendField - Appends a field with the given offset and type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 158 | void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 159 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 160 | /// AppendPadding - Appends enough padding bytes so that the total |
| 161 | /// struct size is a multiple of the field alignment. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 162 | void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 164 | /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the |
| 165 | /// tail padding of a previous base. If this happens, the type of the previous |
| 166 | /// base needs to be changed to an array of i8. Returns true if the last |
| 167 | /// laid out base was resized. |
| 168 | bool ResizeLastBaseFieldIfNecessary(CharUnits offset); |
| 169 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 170 | /// getByteArrayType - Returns a byte array type with the given number of |
| 171 | /// elements. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 172 | llvm::Type *getByteArrayType(CharUnits NumBytes); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 173 | |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 174 | /// AppendBytes - Append a given number of bytes to the record. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 175 | void AppendBytes(CharUnits numBytes); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 176 | |
| 177 | /// AppendTailPadding - Append enough tail padding so that the type will have |
| 178 | /// the passed size. |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 179 | void AppendTailPadding(CharUnits RecordSize); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 181 | CharUnits getTypeAlignment(llvm::Type *Ty) const; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 182 | |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 183 | /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the |
| 184 | /// LLVM element types. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 185 | CharUnits getAlignmentAsLLVMStruct() const; |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 186 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 187 | /// CheckZeroInitializable - Check if the given type contains a pointer |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 188 | /// to data member. |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 189 | void CheckZeroInitializable(QualType T); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 190 | |
| 191 | public: |
| 192 | CGRecordLayoutBuilder(CodeGenTypes &Types) |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 193 | : BaseSubobjectType(0), |
| 194 | IsZeroInitializable(true), IsZeroInitializableAsBase(true), |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 195 | Packed(false), Types(Types) { } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 196 | |
| 197 | /// Layout - Will layout a RecordDecl. |
| 198 | void Layout(const RecordDecl *D); |
| 199 | }; |
| 200 | |
| 201 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 202 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 203 | void CGRecordLayoutBuilder::Layout(const RecordDecl *D) { |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 204 | Alignment = Types.getContext().getASTRecordLayout(D).getAlignment(); |
Anders Carlsson | 09a3774 | 2009-09-02 17:51:33 +0000 | [diff] [blame] | 205 | Packed = D->hasAttr<PackedAttr>(); |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 206 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 207 | if (D->isUnion()) { |
| 208 | LayoutUnion(D); |
| 209 | return; |
| 210 | } |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 212 | if (LayoutFields(D)) |
| 213 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 215 | // We weren't able to layout the struct. Try again with a packed struct |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 216 | Packed = true; |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 217 | LastLaidOutBase.invalidate(); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 218 | NextFieldOffset = CharUnits::Zero(); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 219 | FieldTypes.clear(); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 220 | Fields.clear(); |
| 221 | BitFields.clear(); |
| 222 | NonVirtualBases.clear(); |
| 223 | VirtualBases.clear(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 225 | LayoutFields(D); |
| 226 | } |
| 227 | |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 228 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 229 | const FieldDecl *FD, |
| 230 | uint64_t Offset, uint64_t Size, |
| 231 | uint64_t StorageSize, |
| 232 | uint64_t StorageAlignment) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 233 | llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType()); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 234 | CharUnits TypeSizeInBytes = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 235 | CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty)); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 236 | uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 238 | bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 239 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 240 | if (Size > TypeSizeInBits) { |
Anders Carlsson | d5f27b0 | 2010-04-17 22:54:57 +0000 | [diff] [blame] | 241 | // We have a wide bit-field. The extra bits are only used for padding, so |
| 242 | // if we have a bitfield of type T, with size N: |
| 243 | // |
| 244 | // T t : N; |
| 245 | // |
| 246 | // We can just assume that it's: |
| 247 | // |
| 248 | // T t : sizeof(T); |
| 249 | // |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 250 | Size = TypeSizeInBits; |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chandler Carruth | fd8eca2 | 2012-12-09 07:26:04 +0000 | [diff] [blame] | 253 | // Reverse the bit offsets for big endian machines. Because we represent |
| 254 | // a bitfield as a single large integer load, we can imagine the bits |
| 255 | // counting from the most-significant-bit instead of the |
| 256 | // least-significant-bit. |
| 257 | if (Types.getDataLayout().isBigEndian()) { |
| 258 | Offset = StorageSize - (Offset + Size); |
| 259 | } |
Chris Lattner | fb59c7c | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 260 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 261 | return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 264 | /// \brief Layout the range of bitfields from BFI to BFE as contiguous storage. |
| 265 | bool CGRecordLayoutBuilder::LayoutBitfields(const ASTRecordLayout &Layout, |
| 266 | unsigned &FirstFieldNo, |
| 267 | RecordDecl::field_iterator &FI, |
| 268 | RecordDecl::field_iterator FE) { |
| 269 | assert(FI != FE); |
| 270 | uint64_t FirstFieldOffset = Layout.getFieldOffset(FirstFieldNo); |
| 271 | uint64_t NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset); |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 272 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 273 | unsigned CharAlign = Types.getTarget().getCharAlign(); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 274 | assert(FirstFieldOffset % CharAlign == 0 && |
| 275 | "First field offset is misaligned"); |
| 276 | CharUnits FirstFieldOffsetInBytes |
| 277 | = Types.getContext().toCharUnitsFromBits(FirstFieldOffset); |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 278 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 279 | unsigned StorageAlignment |
| 280 | = llvm::MinAlign(Alignment.getQuantity(), |
| 281 | FirstFieldOffsetInBytes.getQuantity()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 283 | if (FirstFieldOffset < NextFieldOffsetInBits) { |
| 284 | CharUnits FieldOffsetInCharUnits = |
| 285 | Types.getContext().toCharUnitsFromBits(FirstFieldOffset); |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 286 | |
| 287 | // Try to resize the last base field. |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 288 | if (!ResizeLastBaseFieldIfNecessary(FieldOffsetInCharUnits)) |
| 289 | llvm_unreachable("We must be able to resize the last base if we need to " |
| 290 | "pack bits into it."); |
| 291 | |
| 292 | NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset); |
| 293 | assert(FirstFieldOffset >= NextFieldOffsetInBits); |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 296 | // Append padding if necessary. |
| 297 | AppendPadding(Types.getContext().toCharUnitsFromBits(FirstFieldOffset), |
| 298 | CharUnits::One()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 300 | // Find the last bitfield in a contiguous run of bitfields. |
| 301 | RecordDecl::field_iterator BFI = FI; |
| 302 | unsigned LastFieldNo = FirstFieldNo; |
| 303 | uint64_t NextContiguousFieldOffset = FirstFieldOffset; |
| 304 | for (RecordDecl::field_iterator FJ = FI; |
| 305 | (FJ != FE && (*FJ)->isBitField() && |
| 306 | NextContiguousFieldOffset == Layout.getFieldOffset(LastFieldNo) && |
| 307 | (*FJ)->getBitWidthValue(Types.getContext()) != 0); FI = FJ++) { |
| 308 | NextContiguousFieldOffset += (*FJ)->getBitWidthValue(Types.getContext()); |
| 309 | ++LastFieldNo; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 310 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 311 | // We must use packed structs for packed fields, and also unnamed bit |
| 312 | // fields since they don't affect the struct alignment. |
| 313 | if (!Packed && ((*FJ)->hasAttr<PackedAttr>() || !(*FJ)->getDeclName())) |
| 314 | return false; |
| 315 | } |
| 316 | RecordDecl::field_iterator BFE = llvm::next(FI); |
| 317 | --LastFieldNo; |
| 318 | assert(LastFieldNo >= FirstFieldNo && "Empty run of contiguous bitfields"); |
| 319 | FieldDecl *LastFD = *FI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 321 | // Find the last bitfield's offset, add its size, and round it up to the |
| 322 | // character alignment to compute the storage required. |
| 323 | uint64_t LastFieldOffset = Layout.getFieldOffset(LastFieldNo); |
| 324 | uint64_t LastFieldSize = LastFD->getBitWidthValue(Types.getContext()); |
| 325 | uint64_t TotalBits = (LastFieldOffset + LastFieldSize) - FirstFieldOffset; |
| 326 | CharUnits StorageBytes = Types.getContext().toCharUnitsFromBits( |
| 327 | llvm::RoundUpToAlignment(TotalBits, CharAlign)); |
| 328 | uint64_t StorageBits = Types.getContext().toBits(StorageBytes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 330 | // Grow the storage to encompass any known padding in the layout when doing |
| 331 | // so will make the storage a power-of-two. There are two cases when we can |
| 332 | // do this. The first is when we have a subsequent field and can widen up to |
| 333 | // its offset. The second is when the data size of the AST record layout is |
| 334 | // past the end of the current storage. The latter is true when there is tail |
| 335 | // padding on a struct and no members of a super class can be packed into it. |
| 336 | // |
| 337 | // Note that we widen the storage as much as possible here to express the |
| 338 | // maximum latitude the language provides, and rely on the backend to lower |
| 339 | // these in conjunction with shifts and masks to narrower operations where |
| 340 | // beneficial. |
| 341 | uint64_t EndOffset = Types.getContext().toBits(Layout.getDataSize()); |
| 342 | if (BFE != FE) |
| 343 | // If there are more fields to be laid out, the offset at the end of the |
| 344 | // bitfield is the offset of the next field in the record. |
| 345 | EndOffset = Layout.getFieldOffset(LastFieldNo + 1); |
| 346 | assert(EndOffset >= (FirstFieldOffset + TotalBits) && |
| 347 | "End offset is not past the end of the known storage bits."); |
| 348 | uint64_t SpaceBits = EndOffset - FirstFieldOffset; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 349 | uint64_t LongBits = Types.getTarget().getLongWidth(); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 350 | uint64_t WidenedBits = (StorageBits / LongBits) * LongBits + |
| 351 | llvm::NextPowerOf2(StorageBits % LongBits - 1); |
| 352 | assert(WidenedBits >= StorageBits && "Widening shrunk the bits!"); |
| 353 | if (WidenedBits <= SpaceBits) { |
| 354 | StorageBits = WidenedBits; |
| 355 | StorageBytes = Types.getContext().toCharUnitsFromBits(StorageBits); |
| 356 | assert(StorageBits == (uint64_t)Types.getContext().toBits(StorageBytes)); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 359 | unsigned FieldIndex = FieldTypes.size(); |
| 360 | AppendBytes(StorageBytes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 362 | // Now walk the bitfields associating them with this field of storage and |
| 363 | // building up the bitfield specific info. |
| 364 | unsigned FieldNo = FirstFieldNo; |
| 365 | for (; BFI != BFE; ++BFI, ++FieldNo) { |
| 366 | FieldDecl *FD = *BFI; |
| 367 | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo) - FirstFieldOffset; |
| 368 | uint64_t FieldSize = FD->getBitWidthValue(Types.getContext()); |
| 369 | Fields[FD] = FieldIndex; |
| 370 | BitFields[FD] = CGBitFieldInfo::MakeInfo(Types, FD, FieldOffset, FieldSize, |
| 371 | StorageBits, StorageAlignment); |
| 372 | } |
| 373 | FirstFieldNo = LastFieldNo; |
| 374 | return true; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 378 | uint64_t fieldOffset) { |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 379 | // If the field is packed, then we need a packed struct. |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 380 | if (!Packed && D->hasAttr<PackedAttr>()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 381 | return false; |
| 382 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 383 | assert(!D->isBitField() && "Bitfields should be laid out seperately."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 385 | CheckZeroInitializable(D->getType()); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 386 | |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 387 | assert(fieldOffset % Types.getTarget().getCharWidth() == 0 |
| 388 | && "field offset is not on a byte boundary!"); |
| 389 | CharUnits fieldOffsetInBytes |
| 390 | = Types.getContext().toCharUnitsFromBits(fieldOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 392 | llvm::Type *Ty = Types.ConvertTypeForMem(D->getType()); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 393 | CharUnits typeAlignment = getTypeAlignment(Ty); |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 394 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 395 | // If the type alignment is larger then the struct alignment, we must use |
| 396 | // a packed struct. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 397 | if (typeAlignment > Alignment) { |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 398 | assert(!Packed && "Alignment is wrong even with packed struct!"); |
| 399 | return false; |
| 400 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 402 | if (!Packed) { |
| 403 | if (const RecordType *RT = D->getType()->getAs<RecordType>()) { |
| 404 | const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); |
| 405 | if (const MaxFieldAlignmentAttr *MFAA = |
| 406 | RD->getAttr<MaxFieldAlignmentAttr>()) { |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 407 | if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment)) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 408 | return false; |
| 409 | } |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 413 | // Round up the field offset to the alignment of the field type. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 414 | CharUnits alignedNextFieldOffsetInBytes = |
| 415 | NextFieldOffset.RoundUpToAlignment(typeAlignment); |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 416 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 417 | if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) { |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 418 | // Try to resize the last base field. |
| 419 | if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) { |
| 420 | alignedNextFieldOffsetInBytes = |
| 421 | NextFieldOffset.RoundUpToAlignment(typeAlignment); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) { |
Anders Carlsson | 19702bb | 2009-08-04 16:29:15 +0000 | [diff] [blame] | 426 | assert(!Packed && "Could not place field even with packed struct!"); |
| 427 | return false; |
| 428 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 430 | AppendPadding(fieldOffsetInBytes, typeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 432 | // Now append the field. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 433 | Fields[D] = FieldTypes.size(); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 434 | AppendField(fieldOffsetInBytes, Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 436 | LastLaidOutBase.invalidate(); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 440 | llvm::Type * |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 441 | CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field, |
| 442 | const ASTRecordLayout &Layout) { |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 443 | Fields[Field] = 0; |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 444 | if (Field->isBitField()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 445 | uint64_t FieldSize = Field->getBitWidthValue(Types.getContext()); |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 446 | |
| 447 | // Ignore zero sized bit fields. |
| 448 | if (FieldSize == 0) |
| 449 | return 0; |
| 450 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 451 | unsigned StorageBits = llvm::RoundUpToAlignment( |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 452 | FieldSize, Types.getTarget().getCharAlign()); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 453 | CharUnits NumBytesToAppend |
| 454 | = Types.getContext().toCharUnitsFromBits(StorageBits); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 455 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 456 | llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
Ken Dyck | 7a0b19f | 2011-04-24 16:47:33 +0000 | [diff] [blame] | 457 | if (NumBytesToAppend > CharUnits::One()) |
| 458 | FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity()); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 459 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 460 | // Add the bit field info. |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 461 | BitFields[Field] = CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize, |
| 462 | StorageBits, |
| 463 | Alignment.getQuantity()); |
Anders Carlsson | 2295f13 | 2010-04-17 21:04:52 +0000 | [diff] [blame] | 464 | return FieldTy; |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 465 | } |
Daniel Dunbar | 20b551a | 2010-04-20 17:52:30 +0000 | [diff] [blame] | 466 | |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 467 | // This is a regular union field. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 468 | return Types.ConvertTypeForMem(Field->getType()); |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 471 | void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) { |
| 472 | assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 474 | const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 476 | llvm::Type *unionType = 0; |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 477 | CharUnits unionSize = CharUnits::Zero(); |
| 478 | CharUnits unionAlign = CharUnits::Zero(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 480 | bool hasOnlyZeroSizedBitFields = true; |
Eli Friedman | dae858a | 2011-12-07 01:30:11 +0000 | [diff] [blame] | 481 | bool checkedFirstFieldZeroInit = false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 482 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 483 | unsigned fieldNo = 0; |
| 484 | for (RecordDecl::field_iterator field = D->field_begin(), |
| 485 | fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) { |
| 486 | assert(layout.getFieldOffset(fieldNo) == 0 && |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 487 | "Union field offset did not start at the beginning of record!"); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 488 | llvm::Type *fieldType = LayoutUnionField(*field, layout); |
Anders Carlsson | f814ee6 | 2009-07-23 04:00:39 +0000 | [diff] [blame] | 489 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 490 | if (!fieldType) |
Anders Carlsson | 1de2f57 | 2010-04-17 20:49:27 +0000 | [diff] [blame] | 491 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Eli Friedman | dae858a | 2011-12-07 01:30:11 +0000 | [diff] [blame] | 493 | if (field->getDeclName() && !checkedFirstFieldZeroInit) { |
| 494 | CheckZeroInitializable(field->getType()); |
| 495 | checkedFirstFieldZeroInit = true; |
| 496 | } |
| 497 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 498 | hasOnlyZeroSizedBitFields = false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 499 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 500 | CharUnits fieldAlign = CharUnits::fromQuantity( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 501 | Types.getDataLayout().getABITypeAlignment(fieldType)); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 502 | CharUnits fieldSize = CharUnits::fromQuantity( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 503 | Types.getDataLayout().getTypeAllocSize(fieldType)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 505 | if (fieldAlign < unionAlign) |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 506 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 508 | if (fieldAlign > unionAlign || fieldSize > unionSize) { |
| 509 | unionType = fieldType; |
| 510 | unionAlign = fieldAlign; |
| 511 | unionSize = fieldSize; |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 515 | // Now add our field. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 516 | if (unionType) { |
| 517 | AppendField(CharUnits::Zero(), unionType); |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 518 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 519 | if (getTypeAlignment(unionType) > layout.getAlignment()) { |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 520 | // We need a packed struct. |
| 521 | Packed = true; |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 522 | unionAlign = CharUnits::One(); |
Anders Carlsson | 0e91275 | 2009-09-03 22:56:02 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 525 | if (unionAlign.isZero()) { |
Chandler Carruth | 52b6ac2 | 2012-03-04 12:16:40 +0000 | [diff] [blame] | 526 | (void)hasOnlyZeroSizedBitFields; |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 527 | assert(hasOnlyZeroSizedBitFields && |
Anders Carlsson | b1ef991 | 2010-01-28 18:22:03 +0000 | [diff] [blame] | 528 | "0-align record did not have all zero-sized bit-fields!"); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 529 | unionAlign = CharUnits::One(); |
Fariborz Jahanian | e8e631c | 2009-11-06 20:47:40 +0000 | [diff] [blame] | 530 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 531 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 532 | // Append tail padding. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 533 | CharUnits recordSize = layout.getSize(); |
| 534 | if (recordSize > unionSize) |
| 535 | AppendPadding(recordSize, unionAlign); |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 538 | bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 539 | const CGRecordLayout &baseLayout, |
| 540 | CharUnits baseOffset) { |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 541 | ResizeLastBaseFieldIfNecessary(baseOffset); |
| 542 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 543 | AppendPadding(baseOffset, CharUnits::One()); |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 544 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 545 | const ASTRecordLayout &baseASTLayout |
| 546 | = Types.getContext().getASTRecordLayout(base); |
| 547 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 548 | LastLaidOutBase.Offset = NextFieldOffset; |
| 549 | LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize(); |
| 550 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 551 | llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType(); |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 552 | if (getTypeAlignment(subobjectType) > Alignment) |
| 553 | return false; |
| 554 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 555 | AppendField(baseOffset, subobjectType); |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 556 | return true; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 559 | bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 560 | CharUnits baseOffset) { |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 561 | // Ignore empty bases. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 562 | if (base->isEmpty()) return true; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 563 | |
| 564 | const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); |
| 565 | if (IsZeroInitializableAsBase) { |
| 566 | assert(IsZeroInitializable && |
| 567 | "class zero-initializable as base but not as complete object"); |
| 568 | |
| 569 | IsZeroInitializable = IsZeroInitializableAsBase = |
| 570 | baseLayout.isZeroInitializableAsBase(); |
| 571 | } |
| 572 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 573 | if (!LayoutBase(base, baseLayout, baseOffset)) |
| 574 | return false; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 575 | NonVirtualBases[base] = (FieldTypes.size() - 1); |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 576 | return true; |
Anders Carlsson | a518b2a | 2010-12-04 23:59:48 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 579 | bool |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 580 | CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base, |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 581 | CharUnits baseOffset) { |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 582 | // Ignore empty bases. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 583 | if (base->isEmpty()) return true; |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 584 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 585 | const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base); |
| 586 | if (IsZeroInitializable) |
| 587 | IsZeroInitializable = baseLayout.isZeroInitializableAsBase(); |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 588 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 589 | if (!LayoutBase(base, baseLayout, baseOffset)) |
| 590 | return false; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 591 | VirtualBases[base] = (FieldTypes.size() - 1); |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 592 | return true; |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 595 | bool |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 596 | CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD, |
| 597 | const ASTRecordLayout &Layout) { |
| 598 | if (!RD->getNumVBases()) |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 599 | return true; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 600 | |
| 601 | // The vbases list is uniqued and ordered by a depth-first |
| 602 | // traversal, which is what we need here. |
| 603 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 604 | E = RD->vbases_end(); I != E; ++I) { |
| 605 | |
| 606 | const CXXRecordDecl *BaseDecl = |
| 607 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
| 608 | |
| 609 | CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 610 | if (!LayoutVirtualBase(BaseDecl, vbaseOffset)) |
| 611 | return false; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 612 | } |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 613 | return true; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 616 | /// LayoutVirtualBases - layout the non-virtual bases of a record decl. |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 617 | bool |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 618 | CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD, |
| 619 | const ASTRecordLayout &Layout) { |
| 620 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 621 | E = RD->bases_end(); I != E; ++I) { |
| 622 | const CXXRecordDecl *BaseDecl = |
| 623 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 624 | |
| 625 | // We only want to lay out virtual bases that aren't indirect primary bases |
| 626 | // of some other base. |
| 627 | if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) { |
| 628 | // Only lay out the base once. |
| 629 | if (!LaidOutVirtualBases.insert(BaseDecl)) |
| 630 | continue; |
| 631 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 632 | CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 633 | if (!LayoutVirtualBase(BaseDecl, vbaseOffset)) |
| 634 | return false; |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (!BaseDecl->getNumVBases()) { |
| 638 | // This base isn't interesting since it doesn't have any virtual bases. |
| 639 | continue; |
| 640 | } |
| 641 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 642 | if (!LayoutVirtualBases(BaseDecl, Layout)) |
| 643 | return false; |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 644 | } |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 645 | return true; |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 648 | bool |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 649 | CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD, |
| 650 | const ASTRecordLayout &Layout) { |
| 651 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 652 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 653 | // If we have a primary base, lay it out first. |
| 654 | if (PrimaryBase) { |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 655 | if (!Layout.isPrimaryBaseVirtual()) { |
| 656 | if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero())) |
| 657 | return false; |
| 658 | } else { |
| 659 | if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero())) |
| 660 | return false; |
| 661 | } |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 662 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 663 | // Otherwise, add a vtable / vf-table if the layout says to do so. |
John McCall | e42a336 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 664 | } else if (Layout.hasOwnVFPtr()) { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 665 | llvm::Type *FunctionType = |
| 666 | llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()), |
| 667 | /*isVarArg=*/true); |
| 668 | llvm::Type *VTableTy = FunctionType->getPointerTo(); |
Eli Friedman | f927b8b | 2012-04-27 02:34:46 +0000 | [diff] [blame] | 669 | |
| 670 | if (getTypeAlignment(VTableTy) > Alignment) { |
| 671 | // FIXME: Should we allow this to happen in Sema? |
| 672 | assert(!Packed && "Alignment is wrong even with packed struct!"); |
| 673 | return false; |
| 674 | } |
| 675 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 676 | assert(NextFieldOffset.isZero() && |
| 677 | "VTable pointer must come first!"); |
| 678 | AppendField(CharUnits::Zero(), VTableTy->getPointerTo()); |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // Layout the non-virtual bases. |
| 682 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 683 | E = RD->bases_end(); I != E; ++I) { |
| 684 | if (I->isVirtual()) |
| 685 | continue; |
| 686 | |
| 687 | const CXXRecordDecl *BaseDecl = |
| 688 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 689 | |
| 690 | // We've already laid out the primary base. |
Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 691 | if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual()) |
Anders Carlsson | af9e5af | 2010-05-18 05:12:20 +0000 | [diff] [blame] | 692 | continue; |
| 693 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 694 | if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl))) |
| 695 | return false; |
Anders Carlsson | d681a29 | 2009-12-16 17:27:20 +0000 | [diff] [blame] | 696 | } |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 697 | |
| 698 | // Add a vb-table pointer if the layout insists. |
| 699 | if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) { |
| 700 | CharUnits VBPtrOffset = Layout.getVBPtrOffset(); |
| 701 | llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext()); |
| 702 | AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr)); |
| 703 | AppendField(VBPtrOffset, Vbptr); |
| 704 | } |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 705 | |
| 706 | return true; |
Anders Carlsson | d681a29 | 2009-12-16 17:27:20 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 709 | bool |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 710 | CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) { |
| 711 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD); |
| 712 | |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 713 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
| 714 | CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 715 | CharUnits AlignedNonVirtualTypeSize = |
| 716 | NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 717 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 718 | // First check if we can use the same fields as for the complete class. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 719 | CharUnits RecordSize = Layout.getSize(); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 720 | if (AlignedNonVirtualTypeSize == RecordSize) |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 721 | return true; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 722 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 723 | // Check if we need padding. |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 724 | CharUnits AlignedNextFieldOffset = |
| 725 | NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct()); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 726 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 727 | if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) { |
| 728 | assert(!Packed && "cannot layout even as packed struct"); |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 729 | return false; // Needs packing. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 730 | } |
| 731 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 732 | bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset); |
| 733 | if (needsPadding) { |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 734 | CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 735 | FieldTypes.push_back(getByteArrayType(NumBytes)); |
| 736 | } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 738 | BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(), |
| 739 | FieldTypes, "", Packed); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 740 | Types.addRecordTypeName(RD, BaseSubobjectType, ".base"); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 741 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 742 | // Pull the padding back off. |
| 743 | if (needsPadding) |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 744 | FieldTypes.pop_back(); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 745 | |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 746 | return true; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 749 | bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 750 | assert(!D->isUnion() && "Can't call LayoutFields on a union!"); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 751 | assert(!Alignment.isZero() && "Did not set alignment!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 753 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 755 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D); |
| 756 | if (RD) |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 757 | if (!LayoutNonVirtualBases(RD, Layout)) |
| 758 | return false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 759 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 760 | unsigned FieldNo = 0; |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 761 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 762 | for (RecordDecl::field_iterator FI = D->field_begin(), FE = D->field_end(); |
| 763 | FI != FE; ++FI, ++FieldNo) { |
| 764 | FieldDecl *FD = *FI; |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 765 | |
| 766 | // If this field is a bitfield, layout all of the consecutive |
| 767 | // non-zero-length bitfields and the last zero-length bitfield; these will |
| 768 | // all share storage. |
| 769 | if (FD->isBitField()) { |
| 770 | // If all we have is a zero-width bitfield, skip it. |
| 771 | if (FD->getBitWidthValue(Types.getContext()) == 0) |
| 772 | continue; |
| 773 | |
| 774 | // Layout this range of bitfields. |
| 775 | if (!LayoutBitfields(Layout, FieldNo, FI, FE)) { |
| 776 | assert(!Packed && |
| 777 | "Could not layout bitfields even with a packed LLVM struct!"); |
| 778 | return false; |
| 779 | } |
| 780 | assert(FI != FE && "Advanced past the last bitfield"); |
| 781 | continue; |
| 782 | } |
| 783 | |
| 784 | if (!LayoutField(FD, Layout.getFieldOffset(FieldNo))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | assert(!Packed && |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 786 | "Could not layout fields even with a packed LLVM struct!"); |
| 787 | return false; |
| 788 | } |
| 789 | } |
| 790 | |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 791 | if (RD) { |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 792 | // We've laid out the non-virtual bases and the fields, now compute the |
| 793 | // non-virtual base field types. |
Argyrios Kyrtzidis | 648fcbe | 2010-12-10 00:11:00 +0000 | [diff] [blame] | 794 | if (!ComputeNonVirtualBaseType(RD)) { |
| 795 | assert(!Packed && "Could not layout even with a packed LLVM struct!"); |
| 796 | return false; |
| 797 | } |
Anders Carlsson | 1f95ee3 | 2010-11-25 01:59:35 +0000 | [diff] [blame] | 798 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 799 | // Lay out the virtual bases. The MS ABI uses a different |
| 800 | // algorithm here due to the lack of primary virtual bases. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 801 | if (Types.getTarget().getCXXABI().hasPrimaryVBases()) { |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 802 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
| 803 | if (Layout.isPrimaryBaseVirtual()) |
| 804 | IndirectPrimaryBases.insert(Layout.getPrimaryBase()); |
| 805 | |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 806 | if (!LayoutVirtualBases(RD, Layout)) |
| 807 | return false; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 808 | } else { |
Eli Friedman | 3c840aa | 2011-12-12 23:13:20 +0000 | [diff] [blame] | 809 | if (!MSLayoutVirtualBases(RD, Layout)) |
| 810 | return false; |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 811 | } |
Anders Carlsson | a459adb | 2010-11-28 19:18:44 +0000 | [diff] [blame] | 812 | } |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 813 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 814 | // Append tail padding if necessary. |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 815 | AppendTailPadding(Layout.getSize()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 817 | return true; |
| 818 | } |
| 819 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 820 | void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) { |
| 821 | ResizeLastBaseFieldIfNecessary(RecordSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 823 | assert(NextFieldOffset <= RecordSize && "Size mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 825 | CharUnits AlignedNextFieldOffset = |
| 826 | NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct()); |
Anders Carlsson | 220bf4f | 2009-12-08 01:24:23 +0000 | [diff] [blame] | 827 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 828 | if (AlignedNextFieldOffset == RecordSize) { |
Anders Carlsson | 220bf4f | 2009-12-08 01:24:23 +0000 | [diff] [blame] | 829 | // We don't need any padding. |
| 830 | return; |
| 831 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 832 | |
Ken Dyck | 272b6fa | 2011-04-24 16:53:44 +0000 | [diff] [blame] | 833 | CharUnits NumPadBytes = RecordSize - NextFieldOffset; |
Anders Carlsson | b97a3ec | 2009-07-27 14:55:54 +0000 | [diff] [blame] | 834 | AppendBytes(NumPadBytes); |
| 835 | } |
| 836 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 837 | void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 838 | llvm::Type *fieldType) { |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 839 | CharUnits fieldSize = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 840 | CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(fieldType)); |
Anders Carlsson | 6e853bf | 2009-07-24 02:45:50 +0000 | [diff] [blame] | 841 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 842 | FieldTypes.push_back(fieldType); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 843 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 844 | NextFieldOffset = fieldOffset + fieldSize; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 845 | } |
| 846 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 847 | void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset, |
| 848 | CharUnits fieldAlignment) { |
| 849 | assert(NextFieldOffset <= fieldOffset && |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 850 | "Incorrect field layout!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 852 | // Do nothing if we're already at the right offset. |
| 853 | if (fieldOffset == NextFieldOffset) return; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 854 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 855 | // If we're not emitting a packed LLVM type, try to avoid adding |
| 856 | // unnecessary padding fields. |
| 857 | if (!Packed) { |
| 858 | // Round up the field offset to the alignment of the field type. |
| 859 | CharUnits alignedNextFieldOffset = |
| 860 | NextFieldOffset.RoundUpToAlignment(fieldAlignment); |
| 861 | assert(alignedNextFieldOffset <= fieldOffset); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 862 | |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 863 | // If that's the right offset, we're done. |
| 864 | if (alignedNextFieldOffset == fieldOffset) return; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 865 | } |
John McCall | 0153cd3 | 2011-11-08 04:01:03 +0000 | [diff] [blame] | 866 | |
| 867 | // Otherwise we need explicit padding. |
| 868 | CharUnits padding = fieldOffset - NextFieldOffset; |
| 869 | AppendBytes(padding); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Anders Carlsson | fcaaa69 | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 872 | bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) { |
| 873 | // Check if we have a base to resize. |
| 874 | if (!LastLaidOutBase.isValid()) |
| 875 | return false; |
| 876 | |
| 877 | // This offset does not overlap with the tail padding. |
| 878 | if (offset >= NextFieldOffset) |
| 879 | return false; |
| 880 | |
| 881 | // Restore the field offset and append an i8 array instead. |
| 882 | FieldTypes.pop_back(); |
| 883 | NextFieldOffset = LastLaidOutBase.Offset; |
| 884 | AppendBytes(LastLaidOutBase.NonVirtualSize); |
| 885 | LastLaidOutBase.invalidate(); |
| 886 | |
| 887 | return true; |
| 888 | } |
| 889 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 890 | llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) { |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 891 | assert(!numBytes.isZero() && "Empty byte arrays aren't allowed."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 893 | llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 894 | if (numBytes > CharUnits::One()) |
| 895 | Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 897 | return Ty; |
| 898 | } |
| 899 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 900 | void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) { |
| 901 | if (numBytes.isZero()) |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 902 | return; |
| 903 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 904 | // Append the padding field |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 905 | AppendField(NextFieldOffset, getByteArrayType(numBytes)); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 908 | CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const { |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 909 | if (Packed) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 910 | return CharUnits::One(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 912 | return CharUnits::fromQuantity(Types.getDataLayout().getABITypeAlignment(Ty)); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 913 | } |
| 914 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 915 | CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const { |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 916 | if (Packed) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 917 | return CharUnits::One(); |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 918 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 919 | CharUnits maxAlignment = CharUnits::One(); |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 920 | for (size_t i = 0; i != FieldTypes.size(); ++i) |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 921 | maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i])); |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 922 | |
John McCall | 4d9f142 | 2011-02-15 22:21:29 +0000 | [diff] [blame] | 923 | return maxAlignment; |
Anders Carlsson | acf877b | 2010-11-28 23:06:23 +0000 | [diff] [blame] | 924 | } |
| 925 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 926 | /// Merge in whether a field of the given type is zero-initializable. |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 927 | void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) { |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 928 | // This record already contains a member pointer. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 929 | if (!IsZeroInitializableAsBase) |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 930 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 932 | // Can only have member pointers if we're compiling C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 933 | if (!Types.getContext().getLangOpts().CPlusPlus) |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 934 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 936 | const Type *elementType = T->getBaseElementTypeUnsafe(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 938 | if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) { |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 939 | if (!Types.getCXXABI().isZeroInitializable(MPT)) |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 940 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 941 | } else if (const RecordType *RT = elementType->getAs<RecordType>()) { |
Anders Carlsson | e8bfe41 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 942 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 943 | const CGRecordLayout &Layout = Types.getCGRecordLayout(RD); |
| 944 | if (!Layout.isZeroInitializable()) |
| 945 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 946 | } |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 949 | CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, |
| 950 | llvm::StructType *Ty) { |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 951 | CGRecordLayoutBuilder Builder(*this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 953 | Builder.Layout(D); |
Anders Carlsson | e1d5ca5 | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 954 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 955 | Ty->setBody(Builder.FieldTypes, Builder.Packed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 957 | // If we're in C++, compute the base subobject type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 958 | llvm::StructType *BaseTy = 0; |
Eli Friedman | 09d272d | 2012-01-13 03:58:31 +0000 | [diff] [blame] | 959 | if (isa<CXXRecordDecl>(D) && !D->isUnion()) { |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 960 | BaseTy = Builder.BaseSubobjectType; |
| 961 | if (!BaseTy) BaseTy = Ty; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 964 | CGRecordLayout *RL = |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 965 | new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, |
| 966 | Builder.IsZeroInitializableAsBase); |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 967 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 968 | RL->NonVirtualBases.swap(Builder.NonVirtualBases); |
| 969 | RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases); |
Anders Carlsson | 061ca52 | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 970 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 971 | // Add all the field numbers. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 972 | RL->FieldInfo.swap(Builder.Fields); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 973 | |
| 974 | // Add bitfield info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 975 | RL->BitFields.swap(Builder.BitFields); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 977 | // Dump the layout, if requested. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 978 | if (getContext().getLangOpts().DumpRecordLayouts) { |
Argyrios Kyrtzidis | 8ade08e | 2013-07-12 22:30:03 +0000 | [diff] [blame^] | 979 | llvm::outs() << "\n*** Dumping IRgen Record Layout\n"; |
| 980 | llvm::outs() << "Record: "; |
| 981 | D->dump(llvm::outs()); |
| 982 | llvm::outs() << "\nLayout: "; |
| 983 | RL->print(llvm::outs()); |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 984 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 985 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 986 | #ifndef NDEBUG |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 987 | // Verify that the computed LLVM struct size matches the AST layout size. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 988 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); |
| 989 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 990 | uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 991 | assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) && |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 992 | "Type size mismatch!"); |
| 993 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 994 | if (BaseTy) { |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 995 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
| 996 | CharUnits NonVirtualAlign = Layout.getNonVirtualAlign(); |
| 997 | CharUnits AlignedNonVirtualTypeSize = |
| 998 | NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); |
| 999 | |
| 1000 | uint64_t AlignedNonVirtualTypeSizeInBits = |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1001 | getContext().toBits(AlignedNonVirtualTypeSize); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 1002 | |
| 1003 | assert(AlignedNonVirtualTypeSizeInBits == |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1004 | getDataLayout().getTypeAllocSizeInBits(BaseTy) && |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 1005 | "Type size mismatch!"); |
| 1006 | } |
| 1007 | |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 1008 | // Verify that the LLVM and AST field offsets agree. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1009 | llvm::StructType *ST = |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 1010 | dyn_cast<llvm::StructType>(RL->getLLVMType()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1011 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 1012 | |
| 1013 | const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); |
| 1014 | RecordDecl::field_iterator it = D->field_begin(); |
| 1015 | for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1016 | const FieldDecl *FD = *it; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 1017 | |
| 1018 | // For non-bit-fields, just check that the LLVM struct offset matches the |
| 1019 | // AST offset. |
| 1020 | if (!FD->isBitField()) { |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 1021 | unsigned FieldNo = RL->getLLVMFieldNo(FD); |
| 1022 | assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && |
| 1023 | "Invalid field offset!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 1024 | continue; |
| 1025 | } |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 1026 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 1027 | // Ignore unnamed bit-fields. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 1028 | if (!FD->getDeclName()) |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 1029 | continue; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 1030 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1031 | // Don't inspect zero-length bitfields. |
| 1032 | if (FD->getBitWidthValue(getContext()) == 0) |
| 1033 | continue; |
| 1034 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1035 | const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 1036 | llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD)); |
| 1037 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1038 | // Unions have overlapping elements dictating their layout, but for |
| 1039 | // non-unions we can verify that this section of the layout is the exact |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 1040 | // expected size. |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1041 | if (D->isUnion()) { |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 1042 | // For unions we verify that the start is zero and the size |
| 1043 | // is in-bounds. However, on BE systems, the offset may be non-zero, but |
| 1044 | // the size + offset should match the storage size in that case as it |
| 1045 | // "starts" at the back. |
| 1046 | if (getDataLayout().isBigEndian()) |
David Greene | 464d219 | 2013-01-15 23:13:49 +0000 | [diff] [blame] | 1047 | assert(static_cast<unsigned>(Info.Offset + Info.Size) == |
| 1048 | Info.StorageSize && |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 1049 | "Big endian union bitfield does not end at the back"); |
| 1050 | else |
| 1051 | assert(Info.Offset == 0 && |
| 1052 | "Little endian union bitfield with a non-zero offset"); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1053 | assert(Info.StorageSize <= SL->getSizeInBits() && |
| 1054 | "Union not large enough for bitfield storage"); |
| 1055 | } else { |
| 1056 | assert(Info.StorageSize == |
| 1057 | getDataLayout().getTypeAllocSizeInBits(ElementTy) && |
| 1058 | "Storage size does not match the element type size"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 1059 | } |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1060 | assert(Info.Size > 0 && "Empty bitfield!"); |
Eli Bendersky | 76bd3d8 | 2012-12-18 18:53:14 +0000 | [diff] [blame] | 1061 | assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize && |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1062 | "Bitfield outside of its allocated storage"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 1063 | } |
| 1064 | #endif |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 1065 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 1066 | return RL; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 1067 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1068 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1069 | void CGRecordLayout::print(raw_ostream &OS) const { |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1070 | OS << "<CGRecordLayout\n"; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1071 | OS << " LLVMType:" << *CompleteObjectType << "\n"; |
| 1072 | if (BaseSubobjectType) |
| 1073 | OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1074 | OS << " IsZeroInitializable:" << IsZeroInitializable << "\n"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1075 | OS << " BitFields:[\n"; |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Print bit-field infos in declaration order. |
| 1078 | std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1079 | for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator |
| 1080 | it = BitFields.begin(), ie = BitFields.end(); |
| 1081 | it != ie; ++it) { |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1082 | const RecordDecl *RD = it->first->getParent(); |
| 1083 | unsigned Index = 0; |
| 1084 | for (RecordDecl::field_iterator |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1085 | it2 = RD->field_begin(); *it2 != it->first; ++it2) |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1086 | ++Index; |
| 1087 | BFIs.push_back(std::make_pair(Index, &it->second)); |
| 1088 | } |
| 1089 | llvm::array_pod_sort(BFIs.begin(), BFIs.end()); |
| 1090 | for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 1091 | OS.indent(4); |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1092 | BFIs[i].second->print(OS); |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1093 | OS << "\n"; |
| 1094 | } |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 1095 | |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1096 | OS << "]>\n"; |
| 1097 | } |
| 1098 | |
| 1099 | void CGRecordLayout::dump() const { |
| 1100 | print(llvm::errs()); |
| 1101 | } |
| 1102 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1103 | void CGBitFieldInfo::print(raw_ostream &OS) const { |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1104 | OS << "<CGBitFieldInfo" |
| 1105 | << " Offset:" << Offset |
| 1106 | << " Size:" << Size |
| 1107 | << " IsSigned:" << IsSigned |
| 1108 | << " StorageSize:" << StorageSize |
| 1109 | << " StorageAlignment:" << StorageAlignment << ">"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | void CGBitFieldInfo::dump() const { |
| 1113 | print(llvm::errs()); |
| 1114 | } |