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