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