/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/RecordLayoutBuilder.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "clang/AST/ASTContext.h" |
10 | | #include "clang/AST/ASTDiagnostic.h" |
11 | | #include "clang/AST/Attr.h" |
12 | | #include "clang/AST/CXXInheritance.h" |
13 | | #include "clang/AST/Decl.h" |
14 | | #include "clang/AST/DeclCXX.h" |
15 | | #include "clang/AST/DeclObjC.h" |
16 | | #include "clang/AST/Expr.h" |
17 | | #include "clang/AST/VTableBuilder.h" |
18 | | #include "clang/AST/RecordLayout.h" |
19 | | #include "clang/Basic/TargetInfo.h" |
20 | | #include "llvm/ADT/SmallSet.h" |
21 | | #include "llvm/Support/Format.h" |
22 | | #include "llvm/Support/MathExtras.h" |
23 | | |
24 | | using namespace clang; |
25 | | |
26 | | namespace { |
27 | | |
28 | | /// BaseSubobjectInfo - Represents a single base subobject in a complete class. |
29 | | /// For a class hierarchy like |
30 | | /// |
31 | | /// class A { }; |
32 | | /// class B : A { }; |
33 | | /// class C : A, B { }; |
34 | | /// |
35 | | /// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo |
36 | | /// instances, one for B and two for A. |
37 | | /// |
38 | | /// If a base is virtual, it will only have one BaseSubobjectInfo allocated. |
39 | | struct BaseSubobjectInfo { |
40 | | /// Class - The class for this base info. |
41 | | const CXXRecordDecl *Class; |
42 | | |
43 | | /// IsVirtual - Whether the BaseInfo represents a virtual base or not. |
44 | | bool IsVirtual; |
45 | | |
46 | | /// Bases - Information about the base subobjects. |
47 | | SmallVector<BaseSubobjectInfo*, 4> Bases; |
48 | | |
49 | | /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base |
50 | | /// of this base info (if one exists). |
51 | | BaseSubobjectInfo *PrimaryVirtualBaseInfo; |
52 | | |
53 | | // FIXME: Document. |
54 | | const BaseSubobjectInfo *Derived; |
55 | | }; |
56 | | |
57 | | /// Externally provided layout. Typically used when the AST source, such |
58 | | /// as DWARF, lacks all the information that was available at compile time, such |
59 | | /// as alignment attributes on fields and pragmas in effect. |
60 | | struct ExternalLayout { |
61 | 421k | ExternalLayout() : Size(0), Align(0) {} |
62 | | |
63 | | /// Overall record size in bits. |
64 | | uint64_t Size; |
65 | | |
66 | | /// Overall record alignment in bits. |
67 | | uint64_t Align; |
68 | | |
69 | | /// Record field offsets in bits. |
70 | | llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsets; |
71 | | |
72 | | /// Direct, non-virtual base offsets. |
73 | | llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsets; |
74 | | |
75 | | /// Virtual base offsets. |
76 | | llvm::DenseMap<const CXXRecordDecl *, CharUnits> VirtualBaseOffsets; |
77 | | |
78 | | /// Get the offset of the given field. The external source must provide |
79 | | /// entries for all fields in the record. |
80 | 33.8k | uint64_t getExternalFieldOffset(const FieldDecl *FD) { |
81 | 33.8k | assert(FieldOffsets.count(FD) && |
82 | 33.8k | "Field does not have an external offset"); |
83 | 0 | return FieldOffsets[FD]; |
84 | 33.8k | } |
85 | | |
86 | 14.5k | bool getExternalNVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) { |
87 | 14.5k | auto Known = BaseOffsets.find(RD); |
88 | 14.5k | if (Known == BaseOffsets.end()) |
89 | 10 | return false; |
90 | 14.5k | BaseOffset = Known->second; |
91 | 14.5k | return true; |
92 | 14.5k | } |
93 | | |
94 | 25 | bool getExternalVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) { |
95 | 25 | auto Known = VirtualBaseOffsets.find(RD); |
96 | 25 | if (Known == VirtualBaseOffsets.end()) |
97 | 9 | return false; |
98 | 16 | BaseOffset = Known->second; |
99 | 16 | return true; |
100 | 25 | } |
101 | | }; |
102 | | |
103 | | /// EmptySubobjectMap - Keeps track of which empty subobjects exist at different |
104 | | /// offsets while laying out a C++ class. |
105 | | class EmptySubobjectMap { |
106 | | const ASTContext &Context; |
107 | | uint64_t CharWidth; |
108 | | |
109 | | /// Class - The class whose empty entries we're keeping track of. |
110 | | const CXXRecordDecl *Class; |
111 | | |
112 | | /// EmptyClassOffsets - A map from offsets to empty record decls. |
113 | | typedef llvm::TinyPtrVector<const CXXRecordDecl *> ClassVectorTy; |
114 | | typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy; |
115 | | EmptyClassOffsetsMapTy EmptyClassOffsets; |
116 | | |
117 | | /// MaxEmptyClassOffset - The highest offset known to contain an empty |
118 | | /// base subobject. |
119 | | CharUnits MaxEmptyClassOffset; |
120 | | |
121 | | /// ComputeEmptySubobjectSizes - Compute the size of the largest base or |
122 | | /// member subobject that is empty. |
123 | | void ComputeEmptySubobjectSizes(); |
124 | | |
125 | | void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset); |
126 | | |
127 | | void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
128 | | CharUnits Offset, bool PlacingEmptyBase); |
129 | | |
130 | | void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD, |
131 | | const CXXRecordDecl *Class, CharUnits Offset, |
132 | | bool PlacingOverlappingField); |
133 | | void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset, |
134 | | bool PlacingOverlappingField); |
135 | | |
136 | | /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty |
137 | | /// subobjects beyond the given offset. |
138 | 1.71M | bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const { |
139 | 1.71M | return Offset <= MaxEmptyClassOffset; |
140 | 1.71M | } |
141 | | |
142 | | CharUnits |
143 | 211k | getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const { |
144 | 211k | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); |
145 | 211k | assert(FieldOffset % CharWidth == 0 && |
146 | 211k | "Field offset not at char boundary!"); |
147 | | |
148 | 0 | return Context.toCharUnitsFromBits(FieldOffset); |
149 | 211k | } |
150 | | |
151 | | protected: |
152 | | bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
153 | | CharUnits Offset) const; |
154 | | |
155 | | bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
156 | | CharUnits Offset); |
157 | | |
158 | | bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
159 | | const CXXRecordDecl *Class, |
160 | | CharUnits Offset) const; |
161 | | bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
162 | | CharUnits Offset) const; |
163 | | |
164 | | public: |
165 | | /// This holds the size of the largest empty subobject (either a base |
166 | | /// or a member). Will be zero if the record being built doesn't contain |
167 | | /// any empty classes. |
168 | | CharUnits SizeOfLargestEmptySubobject; |
169 | | |
170 | | EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class) |
171 | 294k | : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) { |
172 | 294k | ComputeEmptySubobjectSizes(); |
173 | 294k | } |
174 | | |
175 | | /// CanPlaceBaseAtOffset - Return whether the given base class can be placed |
176 | | /// at the given offset. |
177 | | /// Returns false if placing the record will result in two components |
178 | | /// (direct or indirect) of the same type having the same offset. |
179 | | bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
180 | | CharUnits Offset); |
181 | | |
182 | | /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given |
183 | | /// offset. |
184 | | bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset); |
185 | | }; |
186 | | |
187 | 294k | void EmptySubobjectMap::ComputeEmptySubobjectSizes() { |
188 | | // Check the bases. |
189 | 294k | for (const CXXBaseSpecifier &Base : Class->bases()) { |
190 | 69.9k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
191 | | |
192 | 69.9k | CharUnits EmptySize; |
193 | 69.9k | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
194 | 69.9k | if (BaseDecl->isEmpty()) { |
195 | | // If the class decl is empty, get its size. |
196 | 41.6k | EmptySize = Layout.getSize(); |
197 | 41.6k | } else { |
198 | | // Otherwise, we get the largest empty subobject for the decl. |
199 | 28.3k | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
200 | 28.3k | } |
201 | | |
202 | 69.9k | if (EmptySize > SizeOfLargestEmptySubobject) |
203 | 44.8k | SizeOfLargestEmptySubobject = EmptySize; |
204 | 69.9k | } |
205 | | |
206 | | // Check the fields. |
207 | 428k | for (const FieldDecl *FD : Class->fields()) { |
208 | 428k | const RecordType *RT = |
209 | 428k | Context.getBaseElementType(FD->getType())->getAs<RecordType>(); |
210 | | |
211 | | // We only care about record types. |
212 | 428k | if (!RT) |
213 | 352k | continue; |
214 | | |
215 | 76.0k | CharUnits EmptySize; |
216 | 76.0k | const CXXRecordDecl *MemberDecl = RT->getAsCXXRecordDecl(); |
217 | 76.0k | const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl); |
218 | 76.0k | if (MemberDecl->isEmpty()) { |
219 | | // If the class decl is empty, get its size. |
220 | 1.93k | EmptySize = Layout.getSize(); |
221 | 74.1k | } else { |
222 | | // Otherwise, we get the largest empty subobject for the decl. |
223 | 74.1k | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
224 | 74.1k | } |
225 | | |
226 | 76.0k | if (EmptySize > SizeOfLargestEmptySubobject) |
227 | 9.91k | SizeOfLargestEmptySubobject = EmptySize; |
228 | 76.0k | } |
229 | 294k | } |
230 | | |
231 | | bool |
232 | | EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
233 | 1.17M | CharUnits Offset) const { |
234 | | // We only need to check empty bases. |
235 | 1.17M | if (!RD->isEmpty()) |
236 | 601k | return true; |
237 | | |
238 | 573k | EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset); |
239 | 573k | if (I == EmptyClassOffsets.end()) |
240 | 572k | return true; |
241 | | |
242 | 1.11k | const ClassVectorTy &Classes = I->second; |
243 | 1.11k | if (!llvm::is_contained(Classes, RD)) |
244 | 867 | return true; |
245 | | |
246 | | // There is already an empty class of the same type at this offset. |
247 | 249 | return false; |
248 | 1.11k | } |
249 | | |
250 | | void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD, |
251 | 1.14M | CharUnits Offset) { |
252 | | // We only care about empty bases. |
253 | 1.14M | if (!RD->isEmpty()) |
254 | 567k | return; |
255 | | |
256 | | // If we have empty structures inside a union, we can assign both |
257 | | // the same offset. Just avoid pushing them twice in the list. |
258 | 573k | ClassVectorTy &Classes = EmptyClassOffsets[Offset]; |
259 | 573k | if (llvm::is_contained(Classes, RD)) |
260 | 1 | return; |
261 | | |
262 | 573k | Classes.push_back(RD); |
263 | | |
264 | | // Update the empty class offset. |
265 | 573k | if (Offset > MaxEmptyClassOffset) |
266 | 228 | MaxEmptyClassOffset = Offset; |
267 | 573k | } |
268 | | |
269 | | bool |
270 | | EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
271 | 1.09M | CharUnits Offset) { |
272 | | // We don't have to keep looking past the maximum offset that's known to |
273 | | // contain an empty class. |
274 | 1.09M | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
275 | 693 | return true; |
276 | | |
277 | 1.09M | if (!CanPlaceSubobjectAtOffset(Info->Class, Offset)) |
278 | 172 | return false; |
279 | | |
280 | | // Traverse all non-virtual bases. |
281 | 1.09M | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
282 | 1.09M | for (const BaseSubobjectInfo *Base : Info->Bases) { |
283 | 1.04M | if (Base->IsVirtual) |
284 | 263 | continue; |
285 | | |
286 | 1.04M | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
287 | | |
288 | 1.04M | if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset)) |
289 | 115 | return false; |
290 | 1.04M | } |
291 | | |
292 | 1.09M | if (Info->PrimaryVirtualBaseInfo) { |
293 | 40 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
294 | | |
295 | 40 | if (Info == PrimaryVirtualBaseInfo->Derived) { |
296 | 40 | if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset)) |
297 | 9 | return false; |
298 | 40 | } |
299 | 40 | } |
300 | | |
301 | | // Traverse all member variables. |
302 | 1.09M | unsigned FieldNo = 0; |
303 | 1.09M | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
304 | 1.11M | E = Info->Class->field_end(); I != E; ++I, ++FieldNo14.5k ) { |
305 | 14.5k | if (I->isBitField()) |
306 | 4 | continue; |
307 | | |
308 | 14.5k | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
309 | 14.5k | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
310 | 2 | return false; |
311 | 14.5k | } |
312 | | |
313 | 1.09M | return true; |
314 | 1.09M | } |
315 | | |
316 | | void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
317 | | CharUnits Offset, |
318 | 1.09M | bool PlacingEmptyBase) { |
319 | 1.09M | if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject536k ) { |
320 | | // We know that the only empty subobjects that can conflict with empty |
321 | | // subobject of non-empty bases, are empty bases that can be placed at |
322 | | // offset zero. Because of this, we only need to keep track of empty base |
323 | | // subobjects with offsets less than the size of the largest empty |
324 | | // subobject for our class. |
325 | 581 | return; |
326 | 581 | } |
327 | | |
328 | 1.09M | AddSubobjectAtOffset(Info->Class, Offset); |
329 | | |
330 | | // Traverse all non-virtual bases. |
331 | 1.09M | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
332 | 1.09M | for (const BaseSubobjectInfo *Base : Info->Bases) { |
333 | 1.04M | if (Base->IsVirtual) |
334 | 254 | continue; |
335 | | |
336 | 1.04M | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
337 | 1.04M | UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase); |
338 | 1.04M | } |
339 | | |
340 | 1.09M | if (Info->PrimaryVirtualBaseInfo) { |
341 | 31 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
342 | | |
343 | 31 | if (Info == PrimaryVirtualBaseInfo->Derived) |
344 | 31 | UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset, |
345 | 31 | PlacingEmptyBase); |
346 | 31 | } |
347 | | |
348 | | // Traverse all member variables. |
349 | 1.09M | unsigned FieldNo = 0; |
350 | 1.09M | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
351 | 1.11M | E = Info->Class->field_end(); I != E; ++I, ++FieldNo14.5k ) { |
352 | 14.5k | if (I->isBitField()) |
353 | 4 | continue; |
354 | | |
355 | 14.5k | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
356 | 14.5k | UpdateEmptyFieldSubobjects(*I, FieldOffset, PlacingEmptyBase); |
357 | 14.5k | } |
358 | 1.09M | } |
359 | | |
360 | | bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
361 | 70.5k | CharUnits Offset) { |
362 | | // If we know this class doesn't have any empty subobjects we don't need to |
363 | | // bother checking. |
364 | 70.5k | if (SizeOfLargestEmptySubobject.isZero()) |
365 | 16.7k | return true; |
366 | | |
367 | 53.7k | if (!CanPlaceBaseSubobjectAtOffset(Info, Offset)) |
368 | 174 | return false; |
369 | | |
370 | | // We are able to place the base at this offset. Make sure to update the |
371 | | // empty base subobject map. |
372 | 53.5k | UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty()); |
373 | 53.5k | return true; |
374 | 53.7k | } |
375 | | |
376 | | bool |
377 | | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
378 | | const CXXRecordDecl *Class, |
379 | 78.3k | CharUnits Offset) const { |
380 | | // We don't have to keep looking past the maximum offset that's known to |
381 | | // contain an empty class. |
382 | 78.3k | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
383 | 1.41k | return true; |
384 | | |
385 | 76.8k | if (!CanPlaceSubobjectAtOffset(RD, Offset)) |
386 | 77 | return false; |
387 | | |
388 | 76.8k | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
389 | | |
390 | | // Traverse all non-virtual bases. |
391 | 76.8k | for (const CXXBaseSpecifier &Base : RD->bases()) { |
392 | 17.4k | if (Base.isVirtual()) |
393 | 46 | continue; |
394 | | |
395 | 17.4k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
396 | | |
397 | 17.4k | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
398 | 17.4k | if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset)) |
399 | 34 | return false; |
400 | 17.4k | } |
401 | | |
402 | 76.7k | if (RD == Class) { |
403 | | // This is the most derived class, traverse virtual bases as well. |
404 | 60.7k | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
405 | 46 | const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl(); |
406 | | |
407 | 46 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
408 | 46 | if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset)) |
409 | 1 | return false; |
410 | 46 | } |
411 | 60.7k | } |
412 | | |
413 | | // Traverse all member variables. |
414 | 76.7k | unsigned FieldNo = 0; |
415 | 76.7k | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
416 | 231k | I != E; ++I, ++FieldNo154k ) { |
417 | 154k | if (I->isBitField()) |
418 | 20.9k | continue; |
419 | | |
420 | 133k | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
421 | | |
422 | 133k | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
423 | 10 | return false; |
424 | 133k | } |
425 | | |
426 | 76.7k | return true; |
427 | 76.7k | } |
428 | | |
429 | | bool |
430 | | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
431 | 539k | CharUnits Offset) const { |
432 | | // We don't have to keep looking past the maximum offset that's known to |
433 | | // contain an empty class. |
434 | 539k | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
435 | 337k | return true; |
436 | | |
437 | 201k | QualType T = FD->getType(); |
438 | 201k | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
439 | 60.4k | return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset); |
440 | | |
441 | | // If we have an array type we need to look at every element. |
442 | 141k | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
443 | 15.0k | QualType ElemTy = Context.getBaseElementType(AT); |
444 | 15.0k | const RecordType *RT = ElemTy->getAs<RecordType>(); |
445 | 15.0k | if (!RT) |
446 | 13.6k | return true; |
447 | | |
448 | 1.35k | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
449 | 1.35k | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
450 | | |
451 | 1.35k | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
452 | 1.35k | CharUnits ElementOffset = Offset; |
453 | 1.67k | for (uint64_t I = 0; I != NumElements; ++I321 ) { |
454 | | // We don't have to keep looking past the maximum offset that's known to |
455 | | // contain an empty class. |
456 | 601 | if (!AnyEmptySubobjectsBeyondOffset(ElementOffset)) |
457 | 274 | return true; |
458 | | |
459 | 327 | if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset)) |
460 | 6 | return false; |
461 | | |
462 | 321 | ElementOffset += Layout.getSize(); |
463 | 321 | } |
464 | 1.35k | } |
465 | | |
466 | 127k | return true; |
467 | 141k | } |
468 | | |
469 | | bool |
470 | | EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD, |
471 | 390k | CharUnits Offset) { |
472 | 390k | if (!CanPlaceFieldSubobjectAtOffset(FD, Offset)) |
473 | 75 | return false; |
474 | | |
475 | | // We are able to place the member variable at this offset. |
476 | | // Make sure to update the empty field subobject map. |
477 | 390k | UpdateEmptyFieldSubobjects(FD, Offset, FD->hasAttr<NoUniqueAddressAttr>()); |
478 | 390k | return true; |
479 | 390k | } |
480 | | |
481 | | void EmptySubobjectMap::UpdateEmptyFieldSubobjects( |
482 | | const CXXRecordDecl *RD, const CXXRecordDecl *Class, CharUnits Offset, |
483 | 103k | bool PlacingOverlappingField) { |
484 | | // We know that the only empty subobjects that can conflict with empty |
485 | | // field subobjects are subobjects of empty bases and potentially-overlapping |
486 | | // fields that can be placed at offset zero. Because of this, we only need to |
487 | | // keep track of empty field subobjects with offsets less than the size of |
488 | | // the largest empty subobject for our class. |
489 | | // |
490 | | // (Proof: we will only consider placing a subobject at offset zero or at |
491 | | // >= the current dsize. The only cases where the earlier subobject can be |
492 | | // placed beyond the end of dsize is if it's an empty base or a |
493 | | // potentially-overlapping field.) |
494 | 103k | if (!PlacingOverlappingField && Offset >= SizeOfLargestEmptySubobject102k ) |
495 | 59.9k | return; |
496 | | |
497 | 43.0k | AddSubobjectAtOffset(RD, Offset); |
498 | | |
499 | 43.0k | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
500 | | |
501 | | // Traverse all non-virtual bases. |
502 | 43.0k | for (const CXXBaseSpecifier &Base : RD->bases()) { |
503 | 14.5k | if (Base.isVirtual()) |
504 | 44 | continue; |
505 | | |
506 | 14.5k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
507 | | |
508 | 14.5k | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
509 | 14.5k | UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset, |
510 | 14.5k | PlacingOverlappingField); |
511 | 14.5k | } |
512 | | |
513 | 43.0k | if (RD == Class) { |
514 | | // This is the most derived class, traverse virtual bases as well. |
515 | 28.9k | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
516 | 44 | const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl(); |
517 | | |
518 | 44 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
519 | 44 | UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset, |
520 | 44 | PlacingOverlappingField); |
521 | 44 | } |
522 | 28.9k | } |
523 | | |
524 | | // Traverse all member variables. |
525 | 43.0k | unsigned FieldNo = 0; |
526 | 43.0k | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
527 | 100k | I != E; ++I, ++FieldNo57.4k ) { |
528 | 57.4k | if (I->isBitField()) |
529 | 9.08k | continue; |
530 | | |
531 | 48.4k | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
532 | | |
533 | 48.4k | UpdateEmptyFieldSubobjects(*I, FieldOffset, PlacingOverlappingField); |
534 | 48.4k | } |
535 | 43.0k | } |
536 | | |
537 | | void EmptySubobjectMap::UpdateEmptyFieldSubobjects( |
538 | 453k | const FieldDecl *FD, CharUnits Offset, bool PlacingOverlappingField) { |
539 | 453k | QualType T = FD->getType(); |
540 | 453k | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
541 | 88.3k | UpdateEmptyFieldSubobjects(RD, RD, Offset, PlacingOverlappingField); |
542 | 88.3k | return; |
543 | 88.3k | } |
544 | | |
545 | | // If we have an array type we need to update every element. |
546 | 365k | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
547 | 37.6k | QualType ElemTy = Context.getBaseElementType(AT); |
548 | 37.6k | const RecordType *RT = ElemTy->getAs<RecordType>(); |
549 | 37.6k | if (!RT) |
550 | 35.3k | return; |
551 | | |
552 | 2.28k | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
553 | 2.28k | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
554 | | |
555 | 2.28k | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
556 | 2.28k | CharUnits ElementOffset = Offset; |
557 | | |
558 | 2.41k | for (uint64_t I = 0; I != NumElements; ++I122 ) { |
559 | | // We know that the only empty subobjects that can conflict with empty |
560 | | // field subobjects are subobjects of empty bases that can be placed at |
561 | | // offset zero. Because of this, we only need to keep track of empty field |
562 | | // subobjects with offsets less than the size of the largest empty |
563 | | // subobject for our class. |
564 | 1.34k | if (!PlacingOverlappingField && |
565 | 1.34k | ElementOffset >= SizeOfLargestEmptySubobject1.32k ) |
566 | 1.22k | return; |
567 | | |
568 | 122 | UpdateEmptyFieldSubobjects(RD, RD, ElementOffset, |
569 | 122 | PlacingOverlappingField); |
570 | 122 | ElementOffset += Layout.getSize(); |
571 | 122 | } |
572 | 2.28k | } |
573 | 365k | } |
574 | | |
575 | | typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy; |
576 | | |
577 | | class ItaniumRecordLayoutBuilder { |
578 | | protected: |
579 | | // FIXME: Remove this and make the appropriate fields public. |
580 | | friend class clang::ASTContext; |
581 | | |
582 | | const ASTContext &Context; |
583 | | |
584 | | EmptySubobjectMap *EmptySubobjects; |
585 | | |
586 | | /// Size - The current size of the record layout. |
587 | | uint64_t Size; |
588 | | |
589 | | /// Alignment - The current alignment of the record layout. |
590 | | CharUnits Alignment; |
591 | | |
592 | | /// PreferredAlignment - The preferred alignment of the record layout. |
593 | | CharUnits PreferredAlignment; |
594 | | |
595 | | /// The alignment if attribute packed is not used. |
596 | | CharUnits UnpackedAlignment; |
597 | | |
598 | | /// \brief The maximum of the alignments of top-level members. |
599 | | CharUnits UnadjustedAlignment; |
600 | | |
601 | | SmallVector<uint64_t, 16> FieldOffsets; |
602 | | |
603 | | /// Whether the external AST source has provided a layout for this |
604 | | /// record. |
605 | | unsigned UseExternalLayout : 1; |
606 | | |
607 | | /// Whether we need to infer alignment, even when we have an |
608 | | /// externally-provided layout. |
609 | | unsigned InferAlignment : 1; |
610 | | |
611 | | /// Packed - Whether the record is packed or not. |
612 | | unsigned Packed : 1; |
613 | | |
614 | | unsigned IsUnion : 1; |
615 | | |
616 | | unsigned IsMac68kAlign : 1; |
617 | | |
618 | | unsigned IsNaturalAlign : 1; |
619 | | |
620 | | unsigned IsMsStruct : 1; |
621 | | |
622 | | /// UnfilledBitsInLastUnit - If the last field laid out was a bitfield, |
623 | | /// this contains the number of bits in the last unit that can be used for |
624 | | /// an adjacent bitfield if necessary. The unit in question is usually |
625 | | /// a byte, but larger units are used if IsMsStruct. |
626 | | unsigned char UnfilledBitsInLastUnit; |
627 | | |
628 | | /// LastBitfieldStorageUnitSize - If IsMsStruct, represents the size of the |
629 | | /// storage unit of the previous field if it was a bitfield. |
630 | | unsigned char LastBitfieldStorageUnitSize; |
631 | | |
632 | | /// MaxFieldAlignment - The maximum allowed field alignment. This is set by |
633 | | /// #pragma pack. |
634 | | CharUnits MaxFieldAlignment; |
635 | | |
636 | | /// DataSize - The data size of the record being laid out. |
637 | | uint64_t DataSize; |
638 | | |
639 | | CharUnits NonVirtualSize; |
640 | | CharUnits NonVirtualAlignment; |
641 | | CharUnits PreferredNVAlignment; |
642 | | |
643 | | /// If we've laid out a field but not included its tail padding in Size yet, |
644 | | /// this is the size up to the end of that field. |
645 | | CharUnits PaddedFieldSize; |
646 | | |
647 | | /// PrimaryBase - the primary base class (if one exists) of the class |
648 | | /// we're laying out. |
649 | | const CXXRecordDecl *PrimaryBase; |
650 | | |
651 | | /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying |
652 | | /// out is virtual. |
653 | | bool PrimaryBaseIsVirtual; |
654 | | |
655 | | /// HasOwnVFPtr - Whether the class provides its own vtable/vftbl |
656 | | /// pointer, as opposed to inheriting one from a primary base class. |
657 | | bool HasOwnVFPtr; |
658 | | |
659 | | /// the flag of field offset changing due to packed attribute. |
660 | | bool HasPackedField; |
661 | | |
662 | | /// HandledFirstNonOverlappingEmptyField - An auxiliary field used for AIX. |
663 | | /// When there are OverlappingEmptyFields existing in the aggregate, the |
664 | | /// flag shows if the following first non-empty or empty-but-non-overlapping |
665 | | /// field has been handled, if any. |
666 | | bool HandledFirstNonOverlappingEmptyField; |
667 | | |
668 | | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
669 | | |
670 | | /// Bases - base classes and their offsets in the record. |
671 | | BaseOffsetsMapTy Bases; |
672 | | |
673 | | // VBases - virtual base classes and their offsets in the record. |
674 | | ASTRecordLayout::VBaseOffsetsMapTy VBases; |
675 | | |
676 | | /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are |
677 | | /// primary base classes for some other direct or indirect base class. |
678 | | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
679 | | |
680 | | /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in |
681 | | /// inheritance graph order. Used for determining the primary base class. |
682 | | const CXXRecordDecl *FirstNearlyEmptyVBase; |
683 | | |
684 | | /// VisitedVirtualBases - A set of all the visited virtual bases, used to |
685 | | /// avoid visiting virtual bases more than once. |
686 | | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
687 | | |
688 | | /// Valid if UseExternalLayout is true. |
689 | | ExternalLayout External; |
690 | | |
691 | | ItaniumRecordLayoutBuilder(const ASTContext &Context, |
692 | | EmptySubobjectMap *EmptySubobjects) |
693 | | : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), |
694 | | Alignment(CharUnits::One()), PreferredAlignment(CharUnits::One()), |
695 | | UnpackedAlignment(CharUnits::One()), |
696 | | UnadjustedAlignment(CharUnits::One()), UseExternalLayout(false), |
697 | | InferAlignment(false), Packed(false), IsUnion(false), |
698 | | IsMac68kAlign(false), |
699 | | IsNaturalAlign(!Context.getTargetInfo().getTriple().isOSAIX()), |
700 | | IsMsStruct(false), UnfilledBitsInLastUnit(0), |
701 | | LastBitfieldStorageUnitSize(0), MaxFieldAlignment(CharUnits::Zero()), |
702 | | DataSize(0), NonVirtualSize(CharUnits::Zero()), |
703 | | NonVirtualAlignment(CharUnits::One()), |
704 | | PreferredNVAlignment(CharUnits::One()), |
705 | | PaddedFieldSize(CharUnits::Zero()), PrimaryBase(nullptr), |
706 | | PrimaryBaseIsVirtual(false), HasOwnVFPtr(false), HasPackedField(false), |
707 | | HandledFirstNonOverlappingEmptyField(false), |
708 | 413k | FirstNearlyEmptyVBase(nullptr) {} |
709 | | |
710 | | void Layout(const RecordDecl *D); |
711 | | void Layout(const CXXRecordDecl *D); |
712 | | void Layout(const ObjCInterfaceDecl *D); |
713 | | |
714 | | void LayoutFields(const RecordDecl *D); |
715 | | void LayoutField(const FieldDecl *D, bool InsertExtraPadding); |
716 | | void LayoutWideBitField(uint64_t FieldSize, uint64_t StorageUnitSize, |
717 | | bool FieldPacked, const FieldDecl *D); |
718 | | void LayoutBitField(const FieldDecl *D); |
719 | | |
720 | 0 | TargetCXXABI getCXXABI() const { |
721 | 0 | return Context.getTargetInfo().getCXXABI(); |
722 | 0 | } |
723 | | |
724 | | /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects. |
725 | | llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator; |
726 | | |
727 | | typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *> |
728 | | BaseSubobjectInfoMapTy; |
729 | | |
730 | | /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases |
731 | | /// of the class we're laying out to their base subobject info. |
732 | | BaseSubobjectInfoMapTy VirtualBaseInfo; |
733 | | |
734 | | /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the |
735 | | /// class we're laying out to their base subobject info. |
736 | | BaseSubobjectInfoMapTy NonVirtualBaseInfo; |
737 | | |
738 | | /// ComputeBaseSubobjectInfo - Compute the base subobject information for the |
739 | | /// bases of the given class. |
740 | | void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD); |
741 | | |
742 | | /// ComputeBaseSubobjectInfo - Compute the base subobject information for a |
743 | | /// single class and all of its base classes. |
744 | | BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
745 | | bool IsVirtual, |
746 | | BaseSubobjectInfo *Derived); |
747 | | |
748 | | /// DeterminePrimaryBase - Determine the primary base of the given class. |
749 | | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
750 | | |
751 | | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
752 | | |
753 | | void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign); |
754 | | |
755 | | /// LayoutNonVirtualBases - Determines the primary base class (if any) and |
756 | | /// lays it out. Will then proceed to lay out all non-virtual base clasess. |
757 | | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
758 | | |
759 | | /// LayoutNonVirtualBase - Lays out a single non-virtual base. |
760 | | void LayoutNonVirtualBase(const BaseSubobjectInfo *Base); |
761 | | |
762 | | void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
763 | | CharUnits Offset); |
764 | | |
765 | | /// LayoutVirtualBases - Lays out all the virtual bases. |
766 | | void LayoutVirtualBases(const CXXRecordDecl *RD, |
767 | | const CXXRecordDecl *MostDerivedClass); |
768 | | |
769 | | /// LayoutVirtualBase - Lays out a single virtual base. |
770 | | void LayoutVirtualBase(const BaseSubobjectInfo *Base); |
771 | | |
772 | | /// LayoutBase - Will lay out a base and return the offset where it was |
773 | | /// placed, in chars. |
774 | | CharUnits LayoutBase(const BaseSubobjectInfo *Base); |
775 | | |
776 | | /// InitializeLayout - Initialize record layout for the given record decl. |
777 | | void InitializeLayout(const Decl *D); |
778 | | |
779 | | /// FinishLayout - Finalize record layout. Adjust record size based on the |
780 | | /// alignment. |
781 | | void FinishLayout(const NamedDecl *D); |
782 | | |
783 | | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment, |
784 | | CharUnits PreferredAlignment); |
785 | 24.0k | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment) { |
786 | 24.0k | UpdateAlignment(NewAlignment, UnpackedNewAlignment, NewAlignment); |
787 | 24.0k | } |
788 | 7.38k | void UpdateAlignment(CharUnits NewAlignment) { |
789 | 7.38k | UpdateAlignment(NewAlignment, NewAlignment, NewAlignment); |
790 | 7.38k | } |
791 | | |
792 | | /// Retrieve the externally-supplied field offset for the given |
793 | | /// field. |
794 | | /// |
795 | | /// \param Field The field whose offset is being queried. |
796 | | /// \param ComputedOffset The offset that we've computed for this field. |
797 | | uint64_t updateExternalFieldOffset(const FieldDecl *Field, |
798 | | uint64_t ComputedOffset); |
799 | | |
800 | | void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset, |
801 | | uint64_t UnpackedOffset, unsigned UnpackedAlign, |
802 | | bool isPacked, const FieldDecl *D); |
803 | | |
804 | | DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); |
805 | | |
806 | 762k | CharUnits getSize() const { |
807 | 762k | assert(Size % Context.getCharWidth() == 0); |
808 | 0 | return Context.toCharUnitsFromBits(Size); |
809 | 762k | } |
810 | 3.53M | uint64_t getSizeInBits() const { return Size; } |
811 | | |
812 | 169k | void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); } |
813 | 1.69M | void setSize(uint64_t NewSize) { Size = NewSize; } |
814 | | |
815 | 0 | CharUnits getAligment() const { return Alignment; } |
816 | | |
817 | 1.02M | CharUnits getDataSize() const { |
818 | 1.02M | assert(DataSize % Context.getCharWidth() == 0); |
819 | 0 | return Context.toCharUnitsFromBits(DataSize); |
820 | 1.02M | } |
821 | 1.80M | uint64_t getDataSizeInBits() const { return DataSize; } |
822 | | |
823 | 835k | void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); } |
824 | 71.3k | void setDataSize(uint64_t NewSize) { DataSize = NewSize; } |
825 | | |
826 | | ItaniumRecordLayoutBuilder(const ItaniumRecordLayoutBuilder &) = delete; |
827 | | void operator=(const ItaniumRecordLayoutBuilder &) = delete; |
828 | | }; |
829 | | } // end anonymous namespace |
830 | | |
831 | 1.89k | void ItaniumRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
832 | 1.89k | for (const auto &I : RD->bases()) { |
833 | 1.27k | assert(!I.getType()->isDependentType() && |
834 | 1.27k | "Cannot layout class with dependent bases."); |
835 | | |
836 | 0 | const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
837 | | |
838 | | // Check if this is a nearly empty virtual base. |
839 | 1.27k | if (I.isVirtual() && Context.isNearlyEmpty(Base)904 ) { |
840 | | // If it's not an indirect primary base, then we've found our primary |
841 | | // base. |
842 | 233 | if (!IndirectPrimaryBases.count(Base)) { |
843 | 230 | PrimaryBase = Base; |
844 | 230 | PrimaryBaseIsVirtual = true; |
845 | 230 | return; |
846 | 230 | } |
847 | | |
848 | | // Is this the first nearly empty virtual base? |
849 | 3 | if (!FirstNearlyEmptyVBase) |
850 | 3 | FirstNearlyEmptyVBase = Base; |
851 | 3 | } |
852 | | |
853 | 1.04k | SelectPrimaryVBase(Base); |
854 | 1.04k | if (PrimaryBase) |
855 | 12 | return; |
856 | 1.04k | } |
857 | 1.89k | } |
858 | | |
859 | | /// DeterminePrimaryBase - Determine the primary base of the given class. |
860 | 294k | void ItaniumRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
861 | | // If the class isn't dynamic, it won't have a primary base. |
862 | 294k | if (!RD->isDynamicClass()) |
863 | 277k | return; |
864 | | |
865 | | // Compute all the primary virtual bases for all of our direct and |
866 | | // indirect bases, and record all their primary virtual base classes. |
867 | 17.3k | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
868 | | |
869 | | // If the record has a dynamic base class, attempt to choose a primary base |
870 | | // class. It is the first (in direct base class order) non-virtual dynamic |
871 | | // base class, if one exists. |
872 | 17.3k | for (const auto &I : RD->bases()) { |
873 | | // Ignore virtual bases. |
874 | 12.2k | if (I.isVirtual()) |
875 | 931 | continue; |
876 | | |
877 | 11.3k | const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
878 | | |
879 | 11.3k | if (Base->isDynamicClass()) { |
880 | | // We found it. |
881 | 11.1k | PrimaryBase = Base; |
882 | 11.1k | PrimaryBaseIsVirtual = false; |
883 | 11.1k | return; |
884 | 11.1k | } |
885 | 11.3k | } |
886 | | |
887 | | // Under the Itanium ABI, if there is no non-virtual primary base class, |
888 | | // try to compute the primary virtual base. The primary virtual base is |
889 | | // the first nearly empty virtual base that is not an indirect primary |
890 | | // virtual base class, if one exists. |
891 | 6.22k | if (RD->getNumVBases() != 0) { |
892 | 846 | SelectPrimaryVBase(RD); |
893 | 846 | if (PrimaryBase) |
894 | 230 | return; |
895 | 846 | } |
896 | | |
897 | | // Otherwise, it is the first indirect primary base class, if one exists. |
898 | 5.99k | if (FirstNearlyEmptyVBase) { |
899 | 2 | PrimaryBase = FirstNearlyEmptyVBase; |
900 | 2 | PrimaryBaseIsVirtual = true; |
901 | 2 | return; |
902 | 2 | } |
903 | | |
904 | 5.99k | assert(!PrimaryBase && "Should not get here with a primary base!"); |
905 | 5.99k | } |
906 | | |
907 | | BaseSubobjectInfo *ItaniumRecordLayoutBuilder::ComputeBaseSubobjectInfo( |
908 | 1.12M | const CXXRecordDecl *RD, bool IsVirtual, BaseSubobjectInfo *Derived) { |
909 | 1.12M | BaseSubobjectInfo *Info; |
910 | | |
911 | 1.12M | if (IsVirtual) { |
912 | | // Check if we already have info about this virtual base. |
913 | 1.87k | BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD]; |
914 | 1.87k | if (InfoSlot) { |
915 | 293 | assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); |
916 | 0 | return InfoSlot; |
917 | 293 | } |
918 | | |
919 | | // We don't, create it. |
920 | 1.58k | InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
921 | 1.58k | Info = InfoSlot; |
922 | 1.12M | } else { |
923 | 1.12M | Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
924 | 1.12M | } |
925 | | |
926 | 1.12M | Info->Class = RD; |
927 | 1.12M | Info->IsVirtual = IsVirtual; |
928 | 1.12M | Info->Derived = nullptr; |
929 | 1.12M | Info->PrimaryVirtualBaseInfo = nullptr; |
930 | | |
931 | 1.12M | const CXXRecordDecl *PrimaryVirtualBase = nullptr; |
932 | 1.12M | BaseSubobjectInfo *PrimaryVirtualBaseInfo = nullptr; |
933 | | |
934 | | // Check if this base has a primary virtual base. |
935 | 1.12M | if (RD->getNumVBases()) { |
936 | 881 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
937 | 881 | if (Layout.isPrimaryBaseVirtual()) { |
938 | | // This base does have a primary virtual base. |
939 | 203 | PrimaryVirtualBase = Layout.getPrimaryBase(); |
940 | 203 | assert(PrimaryVirtualBase && "Didn't have a primary virtual base!"); |
941 | | |
942 | | // Now check if we have base subobject info about this primary base. |
943 | 0 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
944 | | |
945 | 203 | if (PrimaryVirtualBaseInfo) { |
946 | 68 | if (PrimaryVirtualBaseInfo->Derived) { |
947 | | // We did have info about this primary base, and it turns out that it |
948 | | // has already been claimed as a primary virtual base for another |
949 | | // base. |
950 | 34 | PrimaryVirtualBase = nullptr; |
951 | 34 | } else { |
952 | | // We can claim this base as our primary base. |
953 | 34 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
954 | 34 | PrimaryVirtualBaseInfo->Derived = Info; |
955 | 34 | } |
956 | 68 | } |
957 | 203 | } |
958 | 881 | } |
959 | | |
960 | | // Now go through all direct bases. |
961 | 1.05M | for (const auto &I : RD->bases()) { |
962 | 1.05M | bool IsVirtual = I.isVirtual(); |
963 | | |
964 | 1.05M | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
965 | | |
966 | 1.05M | Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info)); |
967 | 1.05M | } |
968 | | |
969 | 1.12M | if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo169 ) { |
970 | | // Traversing the bases must have created the base info for our primary |
971 | | // virtual base. |
972 | 135 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
973 | 135 | assert(PrimaryVirtualBaseInfo && |
974 | 135 | "Did not create a primary virtual base!"); |
975 | | |
976 | | // Claim the primary virtual base as our primary virtual base. |
977 | 0 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
978 | 135 | PrimaryVirtualBaseInfo->Derived = Info; |
979 | 135 | } |
980 | | |
981 | 0 | return Info; |
982 | 1.12M | } |
983 | | |
984 | | void ItaniumRecordLayoutBuilder::ComputeBaseSubobjectInfo( |
985 | 294k | const CXXRecordDecl *RD) { |
986 | 294k | for (const auto &I : RD->bases()) { |
987 | 69.9k | bool IsVirtual = I.isVirtual(); |
988 | | |
989 | 69.9k | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
990 | | |
991 | | // Compute the base subobject info for this base. |
992 | 69.9k | BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, |
993 | 69.9k | nullptr); |
994 | | |
995 | 69.9k | if (IsVirtual) { |
996 | | // ComputeBaseInfo has already added this base for us. |
997 | 997 | assert(VirtualBaseInfo.count(BaseDecl) && |
998 | 997 | "Did not add virtual base!"); |
999 | 68.9k | } else { |
1000 | | // Add the base info to the map of non-virtual bases. |
1001 | 68.9k | assert(!NonVirtualBaseInfo.count(BaseDecl) && |
1002 | 68.9k | "Non-virtual base already exists!"); |
1003 | 0 | NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); |
1004 | 68.9k | } |
1005 | 69.9k | } |
1006 | 294k | } |
1007 | | |
1008 | | void ItaniumRecordLayoutBuilder::EnsureVTablePointerAlignment( |
1009 | 5.99k | CharUnits UnpackedBaseAlign) { |
1010 | 5.99k | CharUnits BaseAlign = Packed ? CharUnits::One()2 : UnpackedBaseAlign5.98k ; |
1011 | | |
1012 | | // The maximum field alignment overrides base align. |
1013 | 5.99k | if (!MaxFieldAlignment.isZero()) { |
1014 | 5 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
1015 | 5 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
1016 | 5 | } |
1017 | | |
1018 | | // Round up the current record size to pointer alignment. |
1019 | 5.99k | setSize(getSize().alignTo(BaseAlign)); |
1020 | | |
1021 | | // Update the alignment. |
1022 | 5.99k | UpdateAlignment(BaseAlign, UnpackedBaseAlign, BaseAlign); |
1023 | 5.99k | } |
1024 | | |
1025 | | void ItaniumRecordLayoutBuilder::LayoutNonVirtualBases( |
1026 | 294k | const CXXRecordDecl *RD) { |
1027 | | // Then, determine the primary base class. |
1028 | 294k | DeterminePrimaryBase(RD); |
1029 | | |
1030 | | // Compute base subobject info. |
1031 | 294k | ComputeBaseSubobjectInfo(RD); |
1032 | | |
1033 | | // If we have a primary base class, lay it out. |
1034 | 294k | if (PrimaryBase) { |
1035 | 11.3k | if (PrimaryBaseIsVirtual) { |
1036 | | // If the primary virtual base was a primary virtual base of some other |
1037 | | // base class we'll have to steal it. |
1038 | 232 | BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase); |
1039 | 232 | PrimaryBaseInfo->Derived = nullptr; |
1040 | | |
1041 | | // We have a virtual primary base, insert it as an indirect primary base. |
1042 | 232 | IndirectPrimaryBases.insert(PrimaryBase); |
1043 | | |
1044 | 232 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
1045 | 232 | "vbase already visited!"); |
1046 | 0 | VisitedVirtualBases.insert(PrimaryBase); |
1047 | | |
1048 | 232 | LayoutVirtualBase(PrimaryBaseInfo); |
1049 | 11.1k | } else { |
1050 | 11.1k | BaseSubobjectInfo *PrimaryBaseInfo = |
1051 | 11.1k | NonVirtualBaseInfo.lookup(PrimaryBase); |
1052 | 11.1k | assert(PrimaryBaseInfo && |
1053 | 11.1k | "Did not find base info for non-virtual primary base!"); |
1054 | | |
1055 | 0 | LayoutNonVirtualBase(PrimaryBaseInfo); |
1056 | 11.1k | } |
1057 | | |
1058 | | // If this class needs a vtable/vf-table and didn't get one from a |
1059 | | // primary base, add it in now. |
1060 | 283k | } else if (RD->isDynamicClass()) { |
1061 | 5.99k | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
1062 | 0 | CharUnits PtrWidth = |
1063 | 5.99k | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
1064 | 5.99k | CharUnits PtrAlign = |
1065 | 5.99k | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
1066 | 5.99k | EnsureVTablePointerAlignment(PtrAlign); |
1067 | 5.99k | HasOwnVFPtr = true; |
1068 | | |
1069 | 5.99k | assert(!IsUnion && "Unions cannot be dynamic classes."); |
1070 | 0 | HandledFirstNonOverlappingEmptyField = true; |
1071 | | |
1072 | 5.99k | setSize(getSize() + PtrWidth); |
1073 | 5.99k | setDataSize(getSize()); |
1074 | 5.99k | } |
1075 | | |
1076 | | // Now lay out the non-virtual bases. |
1077 | 69.9k | for (const auto &I : RD->bases()) { |
1078 | | |
1079 | | // Ignore virtual bases. |
1080 | 69.9k | if (I.isVirtual()) |
1081 | 997 | continue; |
1082 | | |
1083 | 68.9k | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
1084 | | |
1085 | | // Skip the primary base, because we've already laid it out. The |
1086 | | // !PrimaryBaseIsVirtual check is required because we might have a |
1087 | | // non-virtual base of the same type as a primary virtual base. |
1088 | 68.9k | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual11.1k ) |
1089 | 11.1k | continue; |
1090 | | |
1091 | | // Lay out the base. |
1092 | 57.8k | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
1093 | 57.8k | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
1094 | | |
1095 | 0 | LayoutNonVirtualBase(BaseInfo); |
1096 | 57.8k | } |
1097 | 294k | } |
1098 | | |
1099 | | void ItaniumRecordLayoutBuilder::LayoutNonVirtualBase( |
1100 | 68.9k | const BaseSubobjectInfo *Base) { |
1101 | | // Layout the base. |
1102 | 68.9k | CharUnits Offset = LayoutBase(Base); |
1103 | | |
1104 | | // Add its base class offset. |
1105 | 68.9k | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
1106 | 0 | Bases.insert(std::make_pair(Base->Class, Offset)); |
1107 | | |
1108 | 68.9k | AddPrimaryVirtualBaseOffsets(Base, Offset); |
1109 | 68.9k | } |
1110 | | |
1111 | | void ItaniumRecordLayoutBuilder::AddPrimaryVirtualBaseOffsets( |
1112 | 70.7k | const BaseSubobjectInfo *Info, CharUnits Offset) { |
1113 | | // This base isn't interesting, it has no virtual bases. |
1114 | 70.7k | if (!Info->Class->getNumVBases()) |
1115 | 69.8k | return; |
1116 | | |
1117 | | // First, check if we have a virtual primary base to add offsets for. |
1118 | 881 | if (Info->PrimaryVirtualBaseInfo) { |
1119 | 169 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
1120 | 169 | "Primary virtual base is not virtual!"); |
1121 | 169 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
1122 | | // Add the offset. |
1123 | 166 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
1124 | 166 | "primary vbase offset already exists!"); |
1125 | 0 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
1126 | 166 | ASTRecordLayout::VBaseInfo(Offset, false))); |
1127 | | |
1128 | | // Traverse the primary virtual base. |
1129 | 166 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
1130 | 166 | } |
1131 | 169 | } |
1132 | | |
1133 | | // Now go through all direct non-virtual bases. |
1134 | 0 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
1135 | 1.15k | for (const BaseSubobjectInfo *Base : Info->Bases) { |
1136 | 1.15k | if (Base->IsVirtual) |
1137 | 880 | continue; |
1138 | | |
1139 | 278 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
1140 | 278 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
1141 | 278 | } |
1142 | 881 | } |
1143 | | |
1144 | | void ItaniumRecordLayoutBuilder::LayoutVirtualBases( |
1145 | 295k | const CXXRecordDecl *RD, const CXXRecordDecl *MostDerivedClass) { |
1146 | 295k | const CXXRecordDecl *PrimaryBase; |
1147 | 295k | bool PrimaryBaseIsVirtual; |
1148 | | |
1149 | 295k | if (MostDerivedClass == RD) { |
1150 | 294k | PrimaryBase = this->PrimaryBase; |
1151 | 294k | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
1152 | 294k | } else { |
1153 | 885 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
1154 | 885 | PrimaryBase = Layout.getPrimaryBase(); |
1155 | 885 | PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
1156 | 885 | } |
1157 | | |
1158 | 295k | for (const CXXBaseSpecifier &Base : RD->bases()) { |
1159 | 71.0k | assert(!Base.getType()->isDependentType() && |
1160 | 71.0k | "Cannot layout class with dependent bases."); |
1161 | | |
1162 | 0 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
1163 | | |
1164 | 71.0k | if (Base.isVirtual()) { |
1165 | 1.88k | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual413 ) { |
1166 | 1.46k | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
1167 | | |
1168 | | // Only lay out the virtual base if it's not an indirect primary base. |
1169 | 1.46k | if (!IndirectPrimaryBase) { |
1170 | | // Only visit virtual bases once. |
1171 | 1.41k | if (!VisitedVirtualBases.insert(BaseDecl).second) |
1172 | 231 | continue; |
1173 | | |
1174 | 1.18k | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
1175 | 1.18k | assert(BaseInfo && "Did not find virtual base info!"); |
1176 | 0 | LayoutVirtualBase(BaseInfo); |
1177 | 1.18k | } |
1178 | 1.46k | } |
1179 | 1.88k | } |
1180 | | |
1181 | 70.8k | if (!BaseDecl->getNumVBases()) { |
1182 | | // This base isn't interesting since it doesn't have any virtual bases. |
1183 | 69.9k | continue; |
1184 | 69.9k | } |
1185 | | |
1186 | 885 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
1187 | 885 | } |
1188 | 295k | } |
1189 | | |
1190 | | void ItaniumRecordLayoutBuilder::LayoutVirtualBase( |
1191 | 1.41k | const BaseSubobjectInfo *Base) { |
1192 | 1.41k | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
1193 | | |
1194 | | // Layout the base. |
1195 | 0 | CharUnits Offset = LayoutBase(Base); |
1196 | | |
1197 | | // Add its base class offset. |
1198 | 1.41k | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
1199 | 0 | VBases.insert(std::make_pair(Base->Class, |
1200 | 1.41k | ASTRecordLayout::VBaseInfo(Offset, false))); |
1201 | | |
1202 | 1.41k | AddPrimaryVirtualBaseOffsets(Base, Offset); |
1203 | 1.41k | } |
1204 | | |
1205 | | CharUnits |
1206 | 70.3k | ItaniumRecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
1207 | 70.3k | assert(!IsUnion && "Unions cannot have base classes."); |
1208 | | |
1209 | 0 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
1210 | 70.3k | CharUnits Offset; |
1211 | | |
1212 | | // Query the external layout to see if it provides an offset. |
1213 | 70.3k | bool HasExternalLayout = false; |
1214 | 70.3k | if (UseExternalLayout) { |
1215 | 14.5k | if (Base->IsVirtual) |
1216 | 22 | HasExternalLayout = External.getExternalVBaseOffset(Base->Class, Offset); |
1217 | 14.5k | else |
1218 | 14.5k | HasExternalLayout = External.getExternalNVBaseOffset(Base->Class, Offset); |
1219 | 14.5k | } |
1220 | | |
1221 | 140k | auto getBaseOrPreferredBaseAlignFromUnpacked = [&](CharUnits UnpackedAlign) { |
1222 | | // Clang <= 6 incorrectly applied the 'packed' attribute to base classes. |
1223 | | // Per GCC's documentation, it only applies to non-static data members. |
1224 | 140k | return (Packed && (134 (Context.getLangOpts().getClangABICompat() <= |
1225 | 134 | LangOptions::ClangABI::Ver6) || |
1226 | 134 | Context.getTargetInfo().getTriple().isPS()120 || |
1227 | 134 | Context.getTargetInfo().getTriple().isOSAIX()92 )) |
1228 | 140k | ? CharUnits::One()58 |
1229 | 140k | : UnpackedAlign140k ; |
1230 | 140k | }; |
1231 | | |
1232 | 70.3k | CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlignment(); |
1233 | 70.3k | CharUnits UnpackedPreferredBaseAlign = Layout.getPreferredNVAlignment(); |
1234 | 70.3k | CharUnits BaseAlign = |
1235 | 70.3k | getBaseOrPreferredBaseAlignFromUnpacked(UnpackedBaseAlign); |
1236 | 70.3k | CharUnits PreferredBaseAlign = |
1237 | 70.3k | getBaseOrPreferredBaseAlignFromUnpacked(UnpackedPreferredBaseAlign); |
1238 | | |
1239 | 70.3k | const bool DefaultsToAIXPowerAlignment = |
1240 | 70.3k | Context.getTargetInfo().defaultsToAIXPowerAlignment(); |
1241 | 70.3k | if (DefaultsToAIXPowerAlignment) { |
1242 | | // AIX `power` alignment does not apply the preferred alignment for |
1243 | | // non-union classes if the source of the alignment (the current base in |
1244 | | // this context) follows introduction of the first subobject with |
1245 | | // exclusively allocated space or zero-extent array. |
1246 | 46 | if (!Base->Class->isEmpty() && !HandledFirstNonOverlappingEmptyField40 ) { |
1247 | | // By handling a base class that is not empty, we're handling the |
1248 | | // "first (inherited) member". |
1249 | 24 | HandledFirstNonOverlappingEmptyField = true; |
1250 | 24 | } else if (22 !IsNaturalAlign22 ) { |
1251 | 20 | UnpackedPreferredBaseAlign = UnpackedBaseAlign; |
1252 | 20 | PreferredBaseAlign = BaseAlign; |
1253 | 20 | } |
1254 | 46 | } |
1255 | | |
1256 | 70.3k | CharUnits UnpackedAlignTo = !DefaultsToAIXPowerAlignment |
1257 | 70.3k | ? UnpackedBaseAlign70.2k |
1258 | 70.3k | : UnpackedPreferredBaseAlign46 ; |
1259 | | // If we have an empty base class, try to place it at offset 0. |
1260 | 70.3k | if (Base->Class->isEmpty() && |
1261 | 70.3k | (41.7k !HasExternalLayout41.7k || Offset == CharUnits::Zero()9.48k ) && |
1262 | 70.3k | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())41.7k ) { |
1263 | 41.6k | setSize(std::max(getSize(), Layout.getSize())); |
1264 | | // On PS4/PS5, don't update the alignment, to preserve compatibility. |
1265 | 41.6k | if (!Context.getTargetInfo().getTriple().isPS()) |
1266 | 41.6k | UpdateAlignment(BaseAlign, UnpackedAlignTo, PreferredBaseAlign); |
1267 | | |
1268 | 41.6k | return CharUnits::Zero(); |
1269 | 41.6k | } |
1270 | | |
1271 | | // The maximum field alignment overrides the base align/(AIX-only) preferred |
1272 | | // base align. |
1273 | 28.6k | if (!MaxFieldAlignment.isZero()) { |
1274 | 41 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
1275 | 41 | PreferredBaseAlign = std::min(PreferredBaseAlign, MaxFieldAlignment); |
1276 | 41 | UnpackedAlignTo = std::min(UnpackedAlignTo, MaxFieldAlignment); |
1277 | 41 | } |
1278 | | |
1279 | 28.6k | CharUnits AlignTo = |
1280 | 28.6k | !DefaultsToAIXPowerAlignment ? BaseAlign28.6k : PreferredBaseAlign40 ; |
1281 | 28.6k | if (!HasExternalLayout) { |
1282 | | // Round up the current record size to the base's alignment boundary. |
1283 | 23.5k | Offset = getDataSize().alignTo(AlignTo); |
1284 | | |
1285 | | // Try to place the base. |
1286 | 23.6k | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset)) |
1287 | 61 | Offset += AlignTo; |
1288 | 23.5k | } else { |
1289 | 5.09k | bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset); |
1290 | 5.09k | (void)Allowed; |
1291 | 5.09k | assert(Allowed && "Base subobject externally placed at overlapping offset"); |
1292 | | |
1293 | 5.09k | if (InferAlignment && Offset < getDataSize().alignTo(AlignTo)682 ) { |
1294 | | // The externally-supplied base offset is before the base offset we |
1295 | | // computed. Assume that the structure is packed. |
1296 | 0 | Alignment = CharUnits::One(); |
1297 | 0 | InferAlignment = false; |
1298 | 0 | } |
1299 | 5.09k | } |
1300 | | |
1301 | 28.6k | if (!Base->Class->isEmpty()) { |
1302 | | // Update the data size. |
1303 | 28.5k | setDataSize(Offset + Layout.getNonVirtualSize()); |
1304 | | |
1305 | 28.5k | setSize(std::max(getSize(), getDataSize())); |
1306 | 28.5k | } else |
1307 | 113 | setSize(std::max(getSize(), Offset + Layout.getSize())); |
1308 | | |
1309 | | // Remember max struct/class alignment. |
1310 | 28.6k | UpdateAlignment(BaseAlign, UnpackedAlignTo, PreferredBaseAlign); |
1311 | | |
1312 | 28.6k | return Offset; |
1313 | 70.3k | } |
1314 | | |
1315 | 413k | void ItaniumRecordLayoutBuilder::InitializeLayout(const Decl *D) { |
1316 | 413k | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
1317 | 408k | IsUnion = RD->isUnion(); |
1318 | 408k | IsMsStruct = RD->isMsStruct(Context); |
1319 | 408k | } |
1320 | | |
1321 | 413k | Packed = D->hasAttr<PackedAttr>(); |
1322 | | |
1323 | | // Honor the default struct packing maximum alignment flag. |
1324 | 413k | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) { |
1325 | 2 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
1326 | 2 | } |
1327 | | |
1328 | | // mac68k alignment supersedes maximum field alignment and attribute aligned, |
1329 | | // and forces all structures to have 2-byte alignment. The IBM docs on it |
1330 | | // allude to additional (more complicated) semantics, especially with regard |
1331 | | // to bit-fields, but gcc appears not to follow that. |
1332 | 413k | if (D->hasAttr<AlignMac68kAttr>()) { |
1333 | 12 | assert( |
1334 | 12 | !D->hasAttr<AlignNaturalAttr>() && |
1335 | 12 | "Having both mac68k and natural alignment on a decl is not allowed."); |
1336 | 0 | IsMac68kAlign = true; |
1337 | 12 | MaxFieldAlignment = CharUnits::fromQuantity(2); |
1338 | 12 | Alignment = CharUnits::fromQuantity(2); |
1339 | 12 | PreferredAlignment = CharUnits::fromQuantity(2); |
1340 | 413k | } else { |
1341 | 413k | if (D->hasAttr<AlignNaturalAttr>()) |
1342 | 26 | IsNaturalAlign = true; |
1343 | | |
1344 | 413k | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
1345 | 44.8k | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
1346 | | |
1347 | 413k | if (unsigned MaxAlign = D->getMaxAlignment()) |
1348 | 3.50k | UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
1349 | 413k | } |
1350 | | |
1351 | 0 | HandledFirstNonOverlappingEmptyField = |
1352 | 413k | !Context.getTargetInfo().defaultsToAIXPowerAlignment() || IsNaturalAlign397 ; |
1353 | | |
1354 | | // If there is an external AST source, ask it for the various offsets. |
1355 | 413k | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
1356 | 408k | if (ExternalASTSource *Source = Context.getExternalSource()) { |
1357 | 147k | UseExternalLayout = Source->layoutRecordType( |
1358 | 147k | RD, External.Size, External.Align, External.FieldOffsets, |
1359 | 147k | External.BaseOffsets, External.VirtualBaseOffsets); |
1360 | | |
1361 | | // Update based on external alignment. |
1362 | 147k | if (UseExternalLayout) { |
1363 | 30.2k | if (External.Align > 0) { |
1364 | 25.5k | Alignment = Context.toCharUnitsFromBits(External.Align); |
1365 | 25.5k | PreferredAlignment = Context.toCharUnitsFromBits(External.Align); |
1366 | 25.5k | } else { |
1367 | | // The external source didn't have alignment information; infer it. |
1368 | 4.69k | InferAlignment = true; |
1369 | 4.69k | } |
1370 | 30.2k | } |
1371 | 147k | } |
1372 | 413k | } |
1373 | | |
1374 | 113k | void ItaniumRecordLayoutBuilder::Layout(const RecordDecl *D) { |
1375 | 113k | InitializeLayout(D); |
1376 | 113k | LayoutFields(D); |
1377 | | |
1378 | | // Finally, round the size of the total struct up to the alignment of the |
1379 | | // struct itself. |
1380 | 113k | FinishLayout(D); |
1381 | 113k | } |
1382 | | |
1383 | 294k | void ItaniumRecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
1384 | 294k | InitializeLayout(RD); |
1385 | | |
1386 | | // Lay out the vtable and the non-virtual bases. |
1387 | 294k | LayoutNonVirtualBases(RD); |
1388 | | |
1389 | 294k | LayoutFields(RD); |
1390 | | |
1391 | 294k | NonVirtualSize = Context.toCharUnitsFromBits( |
1392 | 294k | llvm::alignTo(getSizeInBits(), Context.getTargetInfo().getCharAlign())); |
1393 | 294k | NonVirtualAlignment = Alignment; |
1394 | 294k | PreferredNVAlignment = PreferredAlignment; |
1395 | | |
1396 | | // Lay out the virtual bases and add the primary virtual base offsets. |
1397 | 294k | LayoutVirtualBases(RD, RD); |
1398 | | |
1399 | | // Finally, round the size of the total struct up to the alignment |
1400 | | // of the struct itself. |
1401 | 294k | FinishLayout(RD); |
1402 | | |
1403 | 294k | #ifndef NDEBUG |
1404 | | // Check that we have base offsets for all bases. |
1405 | 294k | for (const CXXBaseSpecifier &Base : RD->bases()) { |
1406 | 69.9k | if (Base.isVirtual()) |
1407 | 997 | continue; |
1408 | | |
1409 | 68.9k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
1410 | | |
1411 | 68.9k | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
1412 | 68.9k | } |
1413 | | |
1414 | | // And all virtual bases. |
1415 | 294k | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
1416 | 1.58k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
1417 | | |
1418 | 1.58k | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
1419 | 1.58k | } |
1420 | 294k | #endif |
1421 | 294k | } |
1422 | | |
1423 | 5.09k | void ItaniumRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
1424 | 5.09k | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
1425 | 3.83k | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
1426 | | |
1427 | 3.83k | UpdateAlignment(SL.getAlignment()); |
1428 | | |
1429 | | // We start laying out ivars not at the end of the superclass |
1430 | | // structure, but at the next byte following the last field. |
1431 | 3.83k | setDataSize(SL.getDataSize()); |
1432 | 3.83k | setSize(getDataSize()); |
1433 | 3.83k | } |
1434 | | |
1435 | 5.09k | InitializeLayout(D); |
1436 | | // Layout each ivar sequentially. |
1437 | 11.7k | for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD; |
1438 | 6.60k | IVD = IVD->getNextIvar()) |
1439 | 6.60k | LayoutField(IVD, false); |
1440 | | |
1441 | | // Finally, round the size of the total struct up to the alignment of the |
1442 | | // struct itself. |
1443 | 5.09k | FinishLayout(D); |
1444 | 5.09k | } |
1445 | | |
1446 | 408k | void ItaniumRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
1447 | | // Layout each field, for now, just sequentially, respecting alignment. In |
1448 | | // the future, this will need to be tweakable by targets. |
1449 | 408k | bool InsertExtraPadding = D->mayInsertExtraPadding(/*EmitRemark=*/true); |
1450 | 408k | bool HasFlexibleArrayMember = D->hasFlexibleArrayMember(); |
1451 | 1.26M | for (auto I = D->field_begin(), End = D->field_end(); I != End; ++I861k ) { |
1452 | 861k | auto Next(I); |
1453 | 861k | ++Next; |
1454 | 861k | LayoutField(*I, |
1455 | 861k | InsertExtraPadding && (42 Next != End42 || !HasFlexibleArrayMember16 )); |
1456 | 861k | } |
1457 | 408k | } |
1458 | | |
1459 | | // Rounds the specified size to have it a multiple of the char size. |
1460 | | static uint64_t |
1461 | | roundUpSizeToCharAlignment(uint64_t Size, |
1462 | 141 | const ASTContext &Context) { |
1463 | 141 | uint64_t CharAlignment = Context.getTargetInfo().getCharAlign(); |
1464 | 141 | return llvm::alignTo(Size, CharAlignment); |
1465 | 141 | } |
1466 | | |
1467 | | void ItaniumRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
1468 | | uint64_t StorageUnitSize, |
1469 | | bool FieldPacked, |
1470 | 40 | const FieldDecl *D) { |
1471 | 40 | assert(Context.getLangOpts().CPlusPlus && |
1472 | 40 | "Can only have wide bit-fields in C++!"); |
1473 | | |
1474 | | // Itanium C++ ABI 2.4: |
1475 | | // If sizeof(T)*8 < n, let T' be the largest integral POD type with |
1476 | | // sizeof(T')*8 <= n. |
1477 | | |
1478 | 0 | QualType IntegralPODTypes[] = { |
1479 | 40 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
1480 | 40 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
1481 | 40 | }; |
1482 | | |
1483 | 40 | QualType Type; |
1484 | 166 | for (const QualType &QT : IntegralPODTypes) { |
1485 | 166 | uint64_t Size = Context.getTypeSize(QT); |
1486 | | |
1487 | 166 | if (Size > FieldSize) |
1488 | 20 | break; |
1489 | | |
1490 | 146 | Type = QT; |
1491 | 146 | } |
1492 | 40 | assert(!Type.isNull() && "Did not find a type!"); |
1493 | | |
1494 | 0 | CharUnits TypeAlign = Context.getTypeAlignInChars(Type); |
1495 | | |
1496 | | // We're not going to use any of the unfilled bits in the last byte. |
1497 | 40 | UnfilledBitsInLastUnit = 0; |
1498 | 40 | LastBitfieldStorageUnitSize = 0; |
1499 | | |
1500 | 40 | uint64_t FieldOffset; |
1501 | 40 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
1502 | | |
1503 | 40 | if (IsUnion) { |
1504 | 11 | uint64_t RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize, |
1505 | 11 | Context); |
1506 | 11 | setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize)); |
1507 | 11 | FieldOffset = 0; |
1508 | 29 | } else { |
1509 | | // The bitfield is allocated starting at the next offset aligned |
1510 | | // appropriately for T', with length n bits. |
1511 | 29 | FieldOffset = llvm::alignTo(getDataSizeInBits(), Context.toBits(TypeAlign)); |
1512 | | |
1513 | 29 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
1514 | | |
1515 | 29 | setDataSize( |
1516 | 29 | llvm::alignTo(NewSizeInBits, Context.getTargetInfo().getCharAlign())); |
1517 | 29 | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
1518 | 29 | } |
1519 | | |
1520 | | // Place this field at the current location. |
1521 | 40 | FieldOffsets.push_back(FieldOffset); |
1522 | | |
1523 | 40 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
1524 | 40 | Context.toBits(TypeAlign), FieldPacked, D); |
1525 | | |
1526 | | // Update the size. |
1527 | 40 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
1528 | | |
1529 | | // Remember max struct/class alignment. |
1530 | 40 | UpdateAlignment(TypeAlign); |
1531 | 40 | } |
1532 | | |
1533 | 48.0k | static bool isAIXLayout(const ASTContext &Context) { |
1534 | 48.0k | return Context.getTargetInfo().getTriple().getOS() == llvm::Triple::AIX; |
1535 | 48.0k | } |
1536 | | |
1537 | 24.0k | void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
1538 | 24.0k | bool FieldPacked = Packed || D->hasAttr<PackedAttr>()20.2k ; |
1539 | 24.0k | uint64_t FieldSize = D->getBitWidthValue(Context); |
1540 | 24.0k | TypeInfo FieldInfo = Context.getTypeInfo(D->getType()); |
1541 | 24.0k | uint64_t StorageUnitSize = FieldInfo.Width; |
1542 | 24.0k | unsigned FieldAlign = FieldInfo.Align; |
1543 | 24.0k | bool AlignIsRequired = FieldInfo.isAlignRequired(); |
1544 | | |
1545 | | // UnfilledBitsInLastUnit is the difference between the end of the |
1546 | | // last allocated bitfield (i.e. the first bit offset available for |
1547 | | // bitfields) and the end of the current data size in bits (i.e. the |
1548 | | // first bit offset available for non-bitfields). The current data |
1549 | | // size in bits is always a multiple of the char size; additionally, |
1550 | | // for ms_struct records it's also a multiple of the |
1551 | | // LastBitfieldStorageUnitSize (if set). |
1552 | | |
1553 | | // The struct-layout algorithm is dictated by the platform ABI, |
1554 | | // which in principle could use almost any rules it likes. In |
1555 | | // practice, UNIXy targets tend to inherit the algorithm described |
1556 | | // in the System V generic ABI. The basic bitfield layout rule in |
1557 | | // System V is to place bitfields at the next available bit offset |
1558 | | // where the entire bitfield would fit in an aligned storage unit of |
1559 | | // the declared type; it's okay if an earlier or later non-bitfield |
1560 | | // is allocated in the same storage unit. However, some targets |
1561 | | // (those that !useBitFieldTypeAlignment(), e.g. ARM APCS) don't |
1562 | | // require this storage unit to be aligned, and therefore always put |
1563 | | // the bitfield at the next available bit offset. |
1564 | | |
1565 | | // ms_struct basically requests a complete replacement of the |
1566 | | // platform ABI's struct-layout algorithm, with the high-level goal |
1567 | | // of duplicating MSVC's layout. For non-bitfields, this follows |
1568 | | // the standard algorithm. The basic bitfield layout rule is to |
1569 | | // allocate an entire unit of the bitfield's declared type |
1570 | | // (e.g. 'unsigned long'), then parcel it up among successive |
1571 | | // bitfields whose declared types have the same size, making a new |
1572 | | // unit as soon as the last can no longer store the whole value. |
1573 | | // Since it completely replaces the platform ABI's algorithm, |
1574 | | // settings like !useBitFieldTypeAlignment() do not apply. |
1575 | | |
1576 | | // A zero-width bitfield forces the use of a new storage unit for |
1577 | | // later bitfields. In general, this occurs by rounding up the |
1578 | | // current size of the struct as if the algorithm were about to |
1579 | | // place a non-bitfield of the field's formal type. Usually this |
1580 | | // does not change the alignment of the struct itself, but it does |
1581 | | // on some targets (those that useZeroLengthBitfieldAlignment(), |
1582 | | // e.g. ARM). In ms_struct layout, zero-width bitfields are |
1583 | | // ignored unless they follow a non-zero-width bitfield. |
1584 | | |
1585 | | // A field alignment restriction (e.g. from #pragma pack) or |
1586 | | // specification (e.g. from __attribute__((aligned))) changes the |
1587 | | // formal alignment of the field. For System V, this alters the |
1588 | | // required alignment of the notional storage unit that must contain |
1589 | | // the bitfield. For ms_struct, this only affects the placement of |
1590 | | // new storage units. In both cases, the effect of #pragma pack is |
1591 | | // ignored on zero-width bitfields. |
1592 | | |
1593 | | // On System V, a packed field (e.g. from #pragma pack or |
1594 | | // __attribute__((packed))) always uses the next available bit |
1595 | | // offset. |
1596 | | |
1597 | | // In an ms_struct struct, the alignment of a fundamental type is |
1598 | | // always equal to its size. This is necessary in order to mimic |
1599 | | // the i386 alignment rules on targets which might not fully align |
1600 | | // all types (e.g. Darwin PPC32, where alignof(long long) == 4). |
1601 | | |
1602 | | // First, some simple bookkeeping to perform for ms_struct structs. |
1603 | 24.0k | if (IsMsStruct) { |
1604 | | // The field alignment for integer types is always the size. |
1605 | 307 | FieldAlign = StorageUnitSize; |
1606 | | |
1607 | | // If the previous field was not a bitfield, or was a bitfield |
1608 | | // with a different storage unit size, or if this field doesn't fit into |
1609 | | // the current storage unit, we're done with that storage unit. |
1610 | 307 | if (LastBitfieldStorageUnitSize != StorageUnitSize || |
1611 | 307 | UnfilledBitsInLastUnit < FieldSize55 ) { |
1612 | | // Also, ignore zero-length bitfields after non-bitfields. |
1613 | 272 | if (!LastBitfieldStorageUnitSize && !FieldSize173 ) |
1614 | 93 | FieldAlign = 1; |
1615 | | |
1616 | 272 | UnfilledBitsInLastUnit = 0; |
1617 | 272 | LastBitfieldStorageUnitSize = 0; |
1618 | 272 | } |
1619 | 307 | } |
1620 | | |
1621 | 24.0k | if (isAIXLayout(Context)) { |
1622 | 228 | if (StorageUnitSize < Context.getTypeSize(Context.UnsignedIntTy)) { |
1623 | | // On AIX, [bool, char, short] bitfields have the same alignment |
1624 | | // as [unsigned]. |
1625 | 64 | StorageUnitSize = Context.getTypeSize(Context.UnsignedIntTy); |
1626 | 164 | } else if (StorageUnitSize > Context.getTypeSize(Context.UnsignedIntTy) && |
1627 | 164 | Context.getTargetInfo().getTriple().isArch32Bit()56 && |
1628 | 164 | FieldSize <= 3220 ) { |
1629 | | // Under 32-bit compile mode, the bitcontainer is 32 bits if a single |
1630 | | // long long bitfield has length no greater than 32 bits. |
1631 | 18 | StorageUnitSize = 32; |
1632 | | |
1633 | 18 | if (!AlignIsRequired) |
1634 | 12 | FieldAlign = 32; |
1635 | 18 | } |
1636 | | |
1637 | 228 | if (FieldAlign < StorageUnitSize) { |
1638 | | // The bitfield alignment should always be greater than or equal to |
1639 | | // bitcontainer size. |
1640 | 68 | FieldAlign = StorageUnitSize; |
1641 | 68 | } |
1642 | 228 | } |
1643 | | |
1644 | | // If the field is wider than its declared type, it follows |
1645 | | // different rules in all cases, except on AIX. |
1646 | | // On AIX, wide bitfield follows the same rules as normal bitfield. |
1647 | 24.0k | if (FieldSize > StorageUnitSize && !isAIXLayout(Context)40 ) { |
1648 | 40 | LayoutWideBitField(FieldSize, StorageUnitSize, FieldPacked, D); |
1649 | 40 | return; |
1650 | 40 | } |
1651 | | |
1652 | | // Compute the next available bit offset. |
1653 | 24.0k | uint64_t FieldOffset = |
1654 | 24.0k | IsUnion ? 0138 : (getDataSizeInBits() - UnfilledBitsInLastUnit)23.8k ; |
1655 | | |
1656 | | // Handle targets that don't honor bitfield type alignment. |
1657 | 24.0k | if (!IsMsStruct && !Context.getTargetInfo().useBitFieldTypeAlignment()23.7k ) { |
1658 | | // Some such targets do honor it on zero-width bitfields. |
1659 | 157 | if (FieldSize == 0 && |
1660 | 157 | Context.getTargetInfo().useZeroLengthBitfieldAlignment()79 ) { |
1661 | | // Some targets don't honor leading zero-width bitfield. |
1662 | 77 | if (!IsUnion && FieldOffset == 069 && |
1663 | 77 | !Context.getTargetInfo().useLeadingZeroLengthBitfield()22 ) |
1664 | 12 | FieldAlign = 1; |
1665 | 65 | else { |
1666 | | // The alignment to round up to is the max of the field's natural |
1667 | | // alignment and a target-specific fixed value (sometimes zero). |
1668 | 65 | unsigned ZeroLengthBitfieldBoundary = |
1669 | 65 | Context.getTargetInfo().getZeroLengthBitfieldBoundary(); |
1670 | 65 | FieldAlign = std::max(FieldAlign, ZeroLengthBitfieldBoundary); |
1671 | 65 | } |
1672 | | // If that doesn't apply, just ignore the field alignment. |
1673 | 80 | } else { |
1674 | 80 | FieldAlign = 1; |
1675 | 80 | } |
1676 | 157 | } |
1677 | | |
1678 | | // Remember the alignment we would have used if the field were not packed. |
1679 | 24.0k | unsigned UnpackedFieldAlign = FieldAlign; |
1680 | | |
1681 | | // Ignore the field alignment if the field is packed unless it has zero-size. |
1682 | 24.0k | if (!IsMsStruct && FieldPacked23.7k && FieldSize != 03.84k ) |
1683 | 3.82k | FieldAlign = 1; |
1684 | | |
1685 | | // But, if there's an 'aligned' attribute on the field, honor that. |
1686 | 24.0k | unsigned ExplicitFieldAlign = D->getMaxAlignment(); |
1687 | 24.0k | if (ExplicitFieldAlign) { |
1688 | 164 | FieldAlign = std::max(FieldAlign, ExplicitFieldAlign); |
1689 | 164 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, ExplicitFieldAlign); |
1690 | 164 | } |
1691 | | |
1692 | | // But, if there's a #pragma pack in play, that takes precedent over |
1693 | | // even the 'aligned' attribute, for non-zero-width bitfields. |
1694 | 24.0k | unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); |
1695 | 24.0k | if (!MaxFieldAlignment.isZero() && FieldSize4.13k ) { |
1696 | 4.11k | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
1697 | 4.11k | if (FieldPacked) |
1698 | 34 | FieldAlign = UnpackedFieldAlign; |
1699 | 4.07k | else |
1700 | 4.07k | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
1701 | 4.11k | } |
1702 | | |
1703 | | // But, ms_struct just ignores all of that in unions, even explicit |
1704 | | // alignment attributes. |
1705 | 24.0k | if (IsMsStruct && IsUnion307 ) { |
1706 | 8 | FieldAlign = UnpackedFieldAlign = 1; |
1707 | 8 | } |
1708 | | |
1709 | | // For purposes of diagnostics, we're going to simultaneously |
1710 | | // compute the field offsets that we would have used if we weren't |
1711 | | // adding any alignment padding or if the field weren't packed. |
1712 | 24.0k | uint64_t UnpaddedFieldOffset = FieldOffset; |
1713 | 24.0k | uint64_t UnpackedFieldOffset = FieldOffset; |
1714 | | |
1715 | | // Check if we need to add padding to fit the bitfield within an |
1716 | | // allocation unit with the right size and alignment. The rules are |
1717 | | // somewhat different here for ms_struct structs. |
1718 | 24.0k | if (IsMsStruct) { |
1719 | | // If it's not a zero-width bitfield, and we can fit the bitfield |
1720 | | // into the active storage unit (and we haven't already decided to |
1721 | | // start a new storage unit), just do so, regardless of any other |
1722 | | // other consideration. Otherwise, round up to the right alignment. |
1723 | 307 | if (FieldSize == 0 || FieldSize > UnfilledBitsInLastUnit199 ) { |
1724 | 275 | FieldOffset = llvm::alignTo(FieldOffset, FieldAlign); |
1725 | 275 | UnpackedFieldOffset = |
1726 | 275 | llvm::alignTo(UnpackedFieldOffset, UnpackedFieldAlign); |
1727 | 275 | UnfilledBitsInLastUnit = 0; |
1728 | 275 | } |
1729 | | |
1730 | 23.7k | } else { |
1731 | | // #pragma pack, with any value, suppresses the insertion of padding. |
1732 | 23.7k | bool AllowPadding = MaxFieldAlignment.isZero(); |
1733 | | |
1734 | | // Compute the real offset. |
1735 | 23.7k | if (FieldSize == 0 || |
1736 | 23.7k | (23.1k AllowPadding23.1k && |
1737 | 23.1k | (FieldOffset & (FieldAlign - 1)) + FieldSize > StorageUnitSize19.1k )) { |
1738 | 639 | FieldOffset = llvm::alignTo(FieldOffset, FieldAlign); |
1739 | 23.0k | } else if (ExplicitFieldAlign && |
1740 | 23.0k | (133 MaxFieldAlignmentInBits == 0133 || |
1741 | 133 | ExplicitFieldAlign <= MaxFieldAlignmentInBits24 ) && |
1742 | 23.0k | Context.getTargetInfo().useExplicitBitFieldAlignment()125 ) { |
1743 | | // TODO: figure it out what needs to be done on targets that don't honor |
1744 | | // bit-field type alignment like ARM APCS ABI. |
1745 | 105 | FieldOffset = llvm::alignTo(FieldOffset, ExplicitFieldAlign); |
1746 | 105 | } |
1747 | | |
1748 | | // Repeat the computation for diagnostic purposes. |
1749 | 23.7k | if (FieldSize == 0 || |
1750 | 23.7k | (23.1k AllowPadding23.1k && |
1751 | 23.1k | (UnpackedFieldOffset & (UnpackedFieldAlign - 1)) + FieldSize > |
1752 | 19.1k | StorageUnitSize)) |
1753 | 697 | UnpackedFieldOffset = |
1754 | 697 | llvm::alignTo(UnpackedFieldOffset, UnpackedFieldAlign); |
1755 | 23.0k | else if (ExplicitFieldAlign && |
1756 | 23.0k | (128 MaxFieldAlignmentInBits == 0128 || |
1757 | 128 | ExplicitFieldAlign <= MaxFieldAlignmentInBits24 ) && |
1758 | 23.0k | Context.getTargetInfo().useExplicitBitFieldAlignment()120 ) |
1759 | 101 | UnpackedFieldOffset = |
1760 | 101 | llvm::alignTo(UnpackedFieldOffset, ExplicitFieldAlign); |
1761 | 23.7k | } |
1762 | | |
1763 | | // If we're using external layout, give the external layout a chance |
1764 | | // to override this information. |
1765 | 24.0k | if (UseExternalLayout) |
1766 | 1.52k | FieldOffset = updateExternalFieldOffset(D, FieldOffset); |
1767 | | |
1768 | | // Okay, place the bitfield at the calculated offset. |
1769 | 24.0k | FieldOffsets.push_back(FieldOffset); |
1770 | | |
1771 | | // Bookkeeping: |
1772 | | |
1773 | | // Anonymous members don't affect the overall record alignment, |
1774 | | // except on targets where they do. |
1775 | 24.0k | if (!IsMsStruct && |
1776 | 24.0k | !Context.getTargetInfo().useZeroLengthBitfieldAlignment()23.7k && |
1777 | 24.0k | !D->getIdentifier()22.3k ) |
1778 | 1.41k | FieldAlign = UnpackedFieldAlign = 1; |
1779 | | |
1780 | | // On AIX, zero-width bitfields pad out to the natural alignment boundary, |
1781 | | // but do not increase the alignment greater than the MaxFieldAlignment, or 1 |
1782 | | // if packed. |
1783 | 24.0k | if (isAIXLayout(Context) && !FieldSize228 ) { |
1784 | 44 | if (FieldPacked) |
1785 | 8 | FieldAlign = 1; |
1786 | 44 | if (!MaxFieldAlignment.isZero()) { |
1787 | 12 | UnpackedFieldAlign = |
1788 | 12 | std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
1789 | 12 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
1790 | 12 | } |
1791 | 44 | } |
1792 | | |
1793 | | // Diagnose differences in layout due to padding or packing. |
1794 | 24.0k | if (!UseExternalLayout) |
1795 | 22.4k | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
1796 | 22.4k | UnpackedFieldAlign, FieldPacked, D); |
1797 | | |
1798 | | // Update DataSize to include the last byte containing (part of) the bitfield. |
1799 | | |
1800 | | // For unions, this is just a max operation, as usual. |
1801 | 24.0k | if (IsUnion) { |
1802 | | // For ms_struct, allocate the entire storage unit --- unless this |
1803 | | // is a zero-width bitfield, in which case just use a size of 1. |
1804 | 138 | uint64_t RoundedFieldSize; |
1805 | 138 | if (IsMsStruct) { |
1806 | 8 | RoundedFieldSize = (FieldSize ? StorageUnitSize5 |
1807 | 8 | : Context.getTargetInfo().getCharWidth()3 ); |
1808 | | |
1809 | | // Otherwise, allocate just the number of bytes required to store |
1810 | | // the bitfield. |
1811 | 130 | } else { |
1812 | 130 | RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize, Context); |
1813 | 130 | } |
1814 | 138 | setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize)); |
1815 | | |
1816 | | // For non-zero-width bitfields in ms_struct structs, allocate a new |
1817 | | // storage unit if necessary. |
1818 | 23.8k | } else if (IsMsStruct && FieldSize299 ) { |
1819 | | // We should have cleared UnfilledBitsInLastUnit in every case |
1820 | | // where we changed storage units. |
1821 | 194 | if (!UnfilledBitsInLastUnit) { |
1822 | 162 | setDataSize(FieldOffset + StorageUnitSize); |
1823 | 162 | UnfilledBitsInLastUnit = StorageUnitSize; |
1824 | 162 | } |
1825 | 194 | UnfilledBitsInLastUnit -= FieldSize; |
1826 | 194 | LastBitfieldStorageUnitSize = StorageUnitSize; |
1827 | | |
1828 | | // Otherwise, bump the data size up to include the bitfield, |
1829 | | // including padding up to char alignment, and then remember how |
1830 | | // bits we didn't use. |
1831 | 23.6k | } else { |
1832 | 23.6k | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
1833 | 23.6k | uint64_t CharAlignment = Context.getTargetInfo().getCharAlign(); |
1834 | 23.6k | setDataSize(llvm::alignTo(NewSizeInBits, CharAlignment)); |
1835 | 23.6k | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
1836 | | |
1837 | | // The only time we can get here for an ms_struct is if this is a |
1838 | | // zero-width bitfield, which doesn't count as anything for the |
1839 | | // purposes of unfilled bits. |
1840 | 23.6k | LastBitfieldStorageUnitSize = 0; |
1841 | 23.6k | } |
1842 | | |
1843 | | // Update the size. |
1844 | 24.0k | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
1845 | | |
1846 | | // Remember max struct/class alignment. |
1847 | 24.0k | UnadjustedAlignment = |
1848 | 24.0k | std::max(UnadjustedAlignment, Context.toCharUnitsFromBits(FieldAlign)); |
1849 | 24.0k | UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), |
1850 | 24.0k | Context.toCharUnitsFromBits(UnpackedFieldAlign)); |
1851 | 24.0k | } |
1852 | | |
1853 | | void ItaniumRecordLayoutBuilder::LayoutField(const FieldDecl *D, |
1854 | 868k | bool InsertExtraPadding) { |
1855 | 868k | auto *FieldClass = D->getType()->getAsCXXRecordDecl(); |
1856 | 868k | bool PotentiallyOverlapping = D->hasAttr<NoUniqueAddressAttr>() && FieldClass139 ; |
1857 | 868k | bool IsOverlappingEmptyField = |
1858 | 868k | PotentiallyOverlapping && FieldClass->isEmpty()129 ; |
1859 | | |
1860 | 868k | CharUnits FieldOffset = |
1861 | 868k | (IsUnion || IsOverlappingEmptyField820k ) ? CharUnits::Zero()47.5k : getDataSize()820k ; |
1862 | | |
1863 | 868k | const bool DefaultsToAIXPowerAlignment = |
1864 | 868k | Context.getTargetInfo().defaultsToAIXPowerAlignment(); |
1865 | 868k | bool FoundFirstNonOverlappingEmptyFieldForAIX = false; |
1866 | 868k | if (DefaultsToAIXPowerAlignment && !HandledFirstNonOverlappingEmptyField578 ) { |
1867 | 269 | assert(FieldOffset == CharUnits::Zero() && |
1868 | 269 | "The first non-overlapping empty field should have been handled."); |
1869 | | |
1870 | 269 | if (!IsOverlappingEmptyField) { |
1871 | 261 | FoundFirstNonOverlappingEmptyFieldForAIX = true; |
1872 | | |
1873 | | // We're going to handle the "first member" based on |
1874 | | // `FoundFirstNonOverlappingEmptyFieldForAIX` during the current |
1875 | | // invocation of this function; record it as handled for future |
1876 | | // invocations (except for unions, because the current field does not |
1877 | | // represent all "firsts"). |
1878 | 261 | HandledFirstNonOverlappingEmptyField = !IsUnion; |
1879 | 261 | } |
1880 | 269 | } |
1881 | | |
1882 | 868k | if (D->isBitField()) { |
1883 | 24.0k | LayoutBitField(D); |
1884 | 24.0k | return; |
1885 | 24.0k | } |
1886 | | |
1887 | 844k | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
1888 | | // Reset the unfilled bits. |
1889 | 844k | UnfilledBitsInLastUnit = 0; |
1890 | 844k | LastBitfieldStorageUnitSize = 0; |
1891 | | |
1892 | 844k | llvm::Triple Target = Context.getTargetInfo().getTriple(); |
1893 | 844k | bool FieldPacked = (Packed && (17.1k !FieldClass17.1k || FieldClass->isPOD()351 || |
1894 | 17.1k | Context.getLangOpts().getClangABICompat() <= |
1895 | 18 | LangOptions::ClangABI::Ver14 || |
1896 | 17.1k | Target.isPS()14 || Target.isOSDarwin()10 )) || |
1897 | 844k | D->hasAttr<PackedAttr>()827k ; |
1898 | | |
1899 | 844k | AlignRequirementKind AlignRequirement = AlignRequirementKind::None; |
1900 | 844k | CharUnits FieldSize; |
1901 | 844k | CharUnits FieldAlign; |
1902 | | // The amount of this class's dsize occupied by the field. |
1903 | | // This is equal to FieldSize unless we're permitted to pack |
1904 | | // into the field's tail padding. |
1905 | 844k | CharUnits EffectiveFieldSize; |
1906 | | |
1907 | 844k | auto setDeclInfo = [&](bool IsIncompleteArrayType) { |
1908 | 838k | auto TI = Context.getTypeInfoInChars(D->getType()); |
1909 | 838k | FieldAlign = TI.Align; |
1910 | | // Flexible array members don't have any size, but they have to be |
1911 | | // aligned appropriately for their element type. |
1912 | 838k | EffectiveFieldSize = FieldSize = |
1913 | 838k | IsIncompleteArrayType ? CharUnits::Zero()339 : TI.Width837k ; |
1914 | 838k | AlignRequirement = TI.AlignRequirement; |
1915 | 838k | }; |
1916 | | |
1917 | 844k | if (D->getType()->isIncompleteArrayType()) { |
1918 | 339 | setDeclInfo(true /* IsIncompleteArrayType */); |
1919 | 844k | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
1920 | 6.28k | unsigned AS = Context.getTargetAddressSpace(RT->getPointeeType()); |
1921 | 6.28k | EffectiveFieldSize = FieldSize = Context.toCharUnitsFromBits( |
1922 | 6.28k | Context.getTargetInfo().getPointerWidth(AS)); |
1923 | 6.28k | FieldAlign = Context.toCharUnitsFromBits( |
1924 | 6.28k | Context.getTargetInfo().getPointerAlign(AS)); |
1925 | 837k | } else { |
1926 | 837k | setDeclInfo(false /* IsIncompleteArrayType */); |
1927 | | |
1928 | | // A potentially-overlapping field occupies its dsize or nvsize, whichever |
1929 | | // is larger. |
1930 | 837k | if (PotentiallyOverlapping) { |
1931 | 129 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(FieldClass); |
1932 | 129 | EffectiveFieldSize = |
1933 | 129 | std::max(Layout.getNonVirtualSize(), Layout.getDataSize()); |
1934 | 129 | } |
1935 | | |
1936 | 837k | if (IsMsStruct) { |
1937 | | // If MS bitfield layout is required, figure out what type is being |
1938 | | // laid out and align the field to the width of that type. |
1939 | | |
1940 | | // Resolve all typedefs down to their base type and round up the field |
1941 | | // alignment if necessary. |
1942 | 173 | QualType T = Context.getBaseElementType(D->getType()); |
1943 | 173 | if (const BuiltinType *BTy = T->getAs<BuiltinType>()) { |
1944 | 166 | CharUnits TypeSize = Context.getTypeSizeInChars(BTy); |
1945 | | |
1946 | 166 | if (!llvm::isPowerOf2_64(TypeSize.getQuantity())) { |
1947 | 4 | assert( |
1948 | 4 | !Context.getTargetInfo().getTriple().isWindowsMSVCEnvironment() && |
1949 | 4 | "Non PowerOf2 size in MSVC mode"); |
1950 | | // Base types with sizes that aren't a power of two don't work |
1951 | | // with the layout rules for MS structs. This isn't an issue in |
1952 | | // MSVC itself since there are no such base data types there. |
1953 | | // On e.g. x86_32 mingw and linux, long double is 12 bytes though. |
1954 | | // Any structs involving that data type obviously can't be ABI |
1955 | | // compatible with MSVC regardless of how it is laid out. |
1956 | | |
1957 | | // Since ms_struct can be mass enabled (via a pragma or via the |
1958 | | // -mms-bitfields command line parameter), this can trigger for |
1959 | | // structs that don't actually need MSVC compatibility, so we |
1960 | | // need to be able to sidestep the ms_struct layout for these types. |
1961 | | |
1962 | | // Since the combination of -mms-bitfields together with structs |
1963 | | // like max_align_t (which contains a long double) for mingw is |
1964 | | // quite common (and GCC handles it silently), just handle it |
1965 | | // silently there. For other targets that have ms_struct enabled |
1966 | | // (most probably via a pragma or attribute), trigger a diagnostic |
1967 | | // that defaults to an error. |
1968 | 4 | if (!Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) |
1969 | 2 | Diag(D->getLocation(), diag::warn_npot_ms_struct); |
1970 | 4 | } |
1971 | 166 | if (TypeSize > FieldAlign && |
1972 | 166 | llvm::isPowerOf2_64(TypeSize.getQuantity())13 ) |
1973 | 9 | FieldAlign = TypeSize; |
1974 | 166 | } |
1975 | 173 | } |
1976 | 837k | } |
1977 | | |
1978 | | // When used as part of a typedef, or together with a 'packed' attribute, the |
1979 | | // 'aligned' attribute can be used to decrease alignment. In that case, it |
1980 | | // overrides any computed alignment we have, and there is no need to upgrade |
1981 | | // the alignment. |
1982 | 350 | auto alignedAttrCanDecreaseAIXAlignment = [AlignRequirement, FieldPacked] { |
1983 | | // Enum alignment sources can be safely ignored here, because this only |
1984 | | // helps decide whether we need the AIX alignment upgrade, which only |
1985 | | // applies to floating-point types. |
1986 | 350 | return AlignRequirement == AlignRequirementKind::RequiredByTypedef || |
1987 | 350 | (342 AlignRequirement == AlignRequirementKind::RequiredByRecord342 && |
1988 | 342 | FieldPacked4 ); |
1989 | 350 | }; |
1990 | | |
1991 | | // The AIX `power` alignment rules apply the natural alignment of the |
1992 | | // "first member" if it is of a floating-point data type (or is an aggregate |
1993 | | // whose recursively "first" member or element is such a type). The alignment |
1994 | | // associated with these types for subsequent members use an alignment value |
1995 | | // where the floating-point data type is considered to have 4-byte alignment. |
1996 | | // |
1997 | | // For the purposes of the foregoing: vtable pointers, non-empty base classes, |
1998 | | // and zero-width bit-fields count as prior members; members of empty class |
1999 | | // types marked `no_unique_address` are not considered to be prior members. |
2000 | 844k | CharUnits PreferredAlign = FieldAlign; |
2001 | 844k | if (DefaultsToAIXPowerAlignment && !alignedAttrCanDecreaseAIXAlignment()350 && |
2002 | 844k | (342 FoundFirstNonOverlappingEmptyFieldForAIX342 || IsNaturalAlign165 )) { |
2003 | 221 | auto performBuiltinTypeAlignmentUpgrade = [&](const BuiltinType *BTy) { |
2004 | 181 | if (BTy->getKind() == BuiltinType::Double || |
2005 | 181 | BTy->getKind() == BuiltinType::LongDouble115 ) { |
2006 | 68 | assert(PreferredAlign == CharUnits::fromQuantity(4) && |
2007 | 68 | "No need to upgrade the alignment value."); |
2008 | 0 | PreferredAlign = CharUnits::fromQuantity(8); |
2009 | 68 | } |
2010 | 181 | }; |
2011 | | |
2012 | 221 | const Type *BaseTy = D->getType()->getBaseElementTypeUnsafe(); |
2013 | 221 | if (const ComplexType *CTy = BaseTy->getAs<ComplexType>()) { |
2014 | 2 | performBuiltinTypeAlignmentUpgrade( |
2015 | 2 | CTy->getElementType()->castAs<BuiltinType>()); |
2016 | 219 | } else if (const BuiltinType *BTy = BaseTy->getAs<BuiltinType>()) { |
2017 | 179 | performBuiltinTypeAlignmentUpgrade(BTy); |
2018 | 179 | } else if (const RecordType *40 RT40 = BaseTy->getAs<RecordType>()) { |
2019 | 36 | const RecordDecl *RD = RT->getDecl(); |
2020 | 36 | assert(RD && "Expected non-null RecordDecl."); |
2021 | 0 | const ASTRecordLayout &FieldRecord = Context.getASTRecordLayout(RD); |
2022 | 36 | PreferredAlign = FieldRecord.getPreferredAlignment(); |
2023 | 36 | } |
2024 | 221 | } |
2025 | | |
2026 | | // The align if the field is not packed. This is to check if the attribute |
2027 | | // was unnecessary (-Wpacked). |
2028 | 0 | CharUnits UnpackedFieldAlign = |
2029 | 844k | !DefaultsToAIXPowerAlignment ? FieldAlign844k : PreferredAlign350 ; |
2030 | 844k | CharUnits UnpackedFieldOffset = FieldOffset; |
2031 | 844k | CharUnits OriginalFieldAlign = UnpackedFieldAlign; |
2032 | | |
2033 | 844k | if (FieldPacked) { |
2034 | 18.0k | FieldAlign = CharUnits::One(); |
2035 | 18.0k | PreferredAlign = CharUnits::One(); |
2036 | 18.0k | } |
2037 | 844k | CharUnits MaxAlignmentInChars = |
2038 | 844k | Context.toCharUnitsFromBits(D->getMaxAlignment()); |
2039 | 844k | FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); |
2040 | 844k | PreferredAlign = std::max(PreferredAlign, MaxAlignmentInChars); |
2041 | 844k | UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); |
2042 | | |
2043 | | // The maximum field alignment overrides the aligned attribute. |
2044 | 844k | if (!MaxFieldAlignment.isZero()) { |
2045 | 179k | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
2046 | 179k | PreferredAlign = std::min(PreferredAlign, MaxFieldAlignment); |
2047 | 179k | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
2048 | 179k | } |
2049 | | |
2050 | 844k | CharUnits AlignTo = |
2051 | 844k | !DefaultsToAIXPowerAlignment ? FieldAlign844k : PreferredAlign350 ; |
2052 | | // Round up the current record size to the field's alignment boundary. |
2053 | 844k | FieldOffset = FieldOffset.alignTo(AlignTo); |
2054 | 844k | UnpackedFieldOffset = UnpackedFieldOffset.alignTo(UnpackedFieldAlign); |
2055 | | |
2056 | 844k | if (UseExternalLayout) { |
2057 | 32.3k | FieldOffset = Context.toCharUnitsFromBits( |
2058 | 32.3k | updateExternalFieldOffset(D, Context.toBits(FieldOffset))); |
2059 | | |
2060 | 32.3k | if (!IsUnion && EmptySubobjects29.8k ) { |
2061 | | // Record the fact that we're placing a field at this offset. |
2062 | 29.7k | bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset); |
2063 | 29.7k | (void)Allowed; |
2064 | 29.7k | assert(Allowed && "Externally-placed field cannot be placed here"); |
2065 | 29.7k | } |
2066 | 812k | } else { |
2067 | 812k | if (!IsUnion && EmptySubobjects767k ) { |
2068 | | // Check if we can place the field at this offset. |
2069 | 360k | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { |
2070 | | // We couldn't place the field at the offset. Try again at a new offset. |
2071 | | // We try offset 0 (for an empty field) and then dsize(C) onwards. |
2072 | 75 | if (FieldOffset == CharUnits::Zero() && |
2073 | 75 | getDataSize() != CharUnits::Zero()68 ) |
2074 | 11 | FieldOffset = getDataSize().alignTo(AlignTo); |
2075 | 64 | else |
2076 | 64 | FieldOffset += AlignTo; |
2077 | 75 | } |
2078 | 360k | } |
2079 | 812k | } |
2080 | | |
2081 | | // Place this field at the current location. |
2082 | 0 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
2083 | | |
2084 | 844k | if (!UseExternalLayout) |
2085 | 812k | CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, |
2086 | 812k | Context.toBits(UnpackedFieldOffset), |
2087 | 812k | Context.toBits(UnpackedFieldAlign), FieldPacked, D); |
2088 | | |
2089 | 844k | if (InsertExtraPadding) { |
2090 | 36 | CharUnits ASanAlignment = CharUnits::fromQuantity(8); |
2091 | 36 | CharUnits ExtraSizeForAsan = ASanAlignment; |
2092 | 36 | if (FieldSize % ASanAlignment) |
2093 | 30 | ExtraSizeForAsan += |
2094 | 30 | ASanAlignment - CharUnits::fromQuantity(FieldSize % ASanAlignment); |
2095 | 36 | EffectiveFieldSize = FieldSize = FieldSize + ExtraSizeForAsan; |
2096 | 36 | } |
2097 | | |
2098 | | // Reserve space for this field. |
2099 | 844k | if (!IsOverlappingEmptyField) { |
2100 | 844k | uint64_t EffectiveFieldSizeInBits = Context.toBits(EffectiveFieldSize); |
2101 | 844k | if (IsUnion) |
2102 | 47.3k | setDataSize(std::max(getDataSizeInBits(), EffectiveFieldSizeInBits)); |
2103 | 796k | else |
2104 | 796k | setDataSize(FieldOffset + EffectiveFieldSize); |
2105 | | |
2106 | 844k | PaddedFieldSize = std::max(PaddedFieldSize, FieldOffset + FieldSize); |
2107 | 844k | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
2108 | 844k | } else { |
2109 | 106 | setSize(std::max(getSizeInBits(), |
2110 | 106 | (uint64_t)Context.toBits(FieldOffset + FieldSize))); |
2111 | 106 | } |
2112 | | |
2113 | | // Remember max struct/class ABI-specified alignment. |
2114 | 844k | UnadjustedAlignment = std::max(UnadjustedAlignment, FieldAlign); |
2115 | 844k | UpdateAlignment(FieldAlign, UnpackedFieldAlign, PreferredAlign); |
2116 | | |
2117 | | // For checking the alignment of inner fields against |
2118 | | // the alignment of its parent record. |
2119 | 844k | if (const RecordDecl *RD = D->getParent()) { |
2120 | | // Check if packed attribute or pragma pack is present. |
2121 | 838k | if (RD->hasAttr<PackedAttr>() || !MaxFieldAlignment.isZero()821k ) |
2122 | 196k | if (FieldAlign < OriginalFieldAlign) |
2123 | 67.7k | if (D->getType()->isRecordType()) { |
2124 | | // If the offset is a multiple of the alignment of |
2125 | | // the type, raise the warning. |
2126 | | // TODO: Takes no account the alignment of the outer struct |
2127 | 1.10k | if (FieldOffset % OriginalFieldAlign != 0) |
2128 | 177 | Diag(D->getLocation(), diag::warn_unaligned_access) |
2129 | 177 | << Context.getTypeDeclType(RD) << D->getName() << D->getType(); |
2130 | 1.10k | } |
2131 | 838k | } |
2132 | 844k | } |
2133 | | |
2134 | 413k | void ItaniumRecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
2135 | | // In C++, records cannot be of size 0. |
2136 | 413k | if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0326k ) { |
2137 | 84.0k | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
2138 | | // Compatibility with gcc requires a class (pod or non-pod) |
2139 | | // which is not empty but of size 0; such as having fields of |
2140 | | // array of zero-length, remains of Size 0 |
2141 | 83.9k | if (RD->isEmpty()) |
2142 | 82.9k | setSize(CharUnits::One()); |
2143 | 83.9k | } |
2144 | 64 | else |
2145 | 64 | setSize(CharUnits::One()); |
2146 | 84.0k | } |
2147 | | |
2148 | | // If we have any remaining field tail padding, include that in the overall |
2149 | | // size. |
2150 | 413k | setSize(std::max(getSizeInBits(), (uint64_t)Context.toBits(PaddedFieldSize))); |
2151 | | |
2152 | | // Finally, round the size of the record up to the alignment of the |
2153 | | // record itself. |
2154 | 413k | uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastUnit; |
2155 | 413k | uint64_t UnpackedSizeInBits = |
2156 | 413k | llvm::alignTo(getSizeInBits(), Context.toBits(UnpackedAlignment)); |
2157 | | |
2158 | 413k | uint64_t RoundedSize = llvm::alignTo( |
2159 | 413k | getSizeInBits(), |
2160 | 413k | Context.toBits(!Context.getTargetInfo().defaultsToAIXPowerAlignment() |
2161 | 413k | ? Alignment412k |
2162 | 413k | : PreferredAlignment397 )); |
2163 | | |
2164 | 413k | if (UseExternalLayout) { |
2165 | | // If we're inferring alignment, and the external size is smaller than |
2166 | | // our size after we've rounded up to alignment, conservatively set the |
2167 | | // alignment to 1. |
2168 | 30.2k | if (InferAlignment && External.Size < RoundedSize4.68k ) { |
2169 | 3 | Alignment = CharUnits::One(); |
2170 | 3 | PreferredAlignment = CharUnits::One(); |
2171 | 3 | InferAlignment = false; |
2172 | 3 | } |
2173 | 30.2k | setSize(External.Size); |
2174 | 30.2k | return; |
2175 | 30.2k | } |
2176 | | |
2177 | | // Set the size to the final size. |
2178 | 383k | setSize(RoundedSize); |
2179 | | |
2180 | 383k | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
2181 | 383k | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
2182 | | // Warn if padding was introduced to the struct/class/union. |
2183 | 377k | if (getSizeInBits() > UnpaddedSize) { |
2184 | 16.0k | unsigned PadSize = getSizeInBits() - UnpaddedSize; |
2185 | 16.0k | bool InBits = true; |
2186 | 16.0k | if (PadSize % CharBitNum == 0) { |
2187 | 15.2k | PadSize = PadSize / CharBitNum; |
2188 | 15.2k | InBits = false; |
2189 | 15.2k | } |
2190 | 16.0k | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
2191 | 16.0k | << Context.getTypeDeclType(RD) |
2192 | 16.0k | << PadSize |
2193 | 16.0k | << (InBits ? 1793 : 015.2k ); // (byte|bit) |
2194 | 16.0k | } |
2195 | | |
2196 | | // Warn if we packed it unnecessarily, when the unpacked alignment is not |
2197 | | // greater than the one after packing, the size in bits doesn't change and |
2198 | | // the offset of each field is identical. |
2199 | 377k | if (Packed && UnpackedAlignment <= Alignment7.62k && |
2200 | 377k | UnpackedSizeInBits == getSizeInBits()1.83k && !HasPackedField1.83k ) |
2201 | 1.81k | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
2202 | 1.81k | << Context.getTypeDeclType(RD); |
2203 | 377k | } |
2204 | 383k | } |
2205 | | |
2206 | | void ItaniumRecordLayoutBuilder::UpdateAlignment( |
2207 | | CharUnits NewAlignment, CharUnits UnpackedNewAlignment, |
2208 | 952k | CharUnits PreferredNewAlignment) { |
2209 | | // The alignment is not modified when using 'mac68k' alignment or when |
2210 | | // we have an externally-supplied layout that also provides overall alignment. |
2211 | 952k | if (IsMac68kAlign || (952k UseExternalLayout952k && !InferAlignment48.7k )) |
2212 | 40.5k | return; |
2213 | | |
2214 | 911k | if (NewAlignment > Alignment) { |
2215 | 283k | assert(llvm::isPowerOf2_64(NewAlignment.getQuantity()) && |
2216 | 283k | "Alignment not a power of 2"); |
2217 | 0 | Alignment = NewAlignment; |
2218 | 283k | } |
2219 | | |
2220 | 911k | if (UnpackedNewAlignment > UnpackedAlignment) { |
2221 | 290k | assert(llvm::isPowerOf2_64(UnpackedNewAlignment.getQuantity()) && |
2222 | 290k | "Alignment not a power of 2"); |
2223 | 0 | UnpackedAlignment = UnpackedNewAlignment; |
2224 | 290k | } |
2225 | | |
2226 | 911k | if (PreferredNewAlignment > PreferredAlignment) { |
2227 | 283k | assert(llvm::isPowerOf2_64(PreferredNewAlignment.getQuantity()) && |
2228 | 283k | "Alignment not a power of 2"); |
2229 | 0 | PreferredAlignment = PreferredNewAlignment; |
2230 | 283k | } |
2231 | 911k | } |
2232 | | |
2233 | | uint64_t |
2234 | | ItaniumRecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field, |
2235 | 33.8k | uint64_t ComputedOffset) { |
2236 | 33.8k | uint64_t ExternalFieldOffset = External.getExternalFieldOffset(Field); |
2237 | | |
2238 | 33.8k | if (InferAlignment && ExternalFieldOffset < ComputedOffset6.44k ) { |
2239 | | // The externally-supplied field offset is before the field offset we |
2240 | | // computed. Assume that the structure is packed. |
2241 | 9 | Alignment = CharUnits::One(); |
2242 | 9 | PreferredAlignment = CharUnits::One(); |
2243 | 9 | InferAlignment = false; |
2244 | 9 | } |
2245 | | |
2246 | | // Use the externally-supplied field offset. |
2247 | 33.8k | return ExternalFieldOffset; |
2248 | 33.8k | } |
2249 | | |
2250 | | /// Get diagnostic %select index for tag kind for |
2251 | | /// field padding diagnostic message. |
2252 | | /// WARNING: Indexes apply to particular diagnostics only! |
2253 | | /// |
2254 | | /// \returns diagnostic %select index. |
2255 | 19.3k | static unsigned getPaddingDiagFromTagKind(TagTypeKind Tag) { |
2256 | 19.3k | switch (Tag) { |
2257 | 18.0k | case TTK_Struct: return 0; |
2258 | 0 | case TTK_Interface: return 1; |
2259 | 1.36k | case TTK_Class: return 2; |
2260 | 0 | default: llvm_unreachable("Invalid tag kind for field padding diagnostic!"); |
2261 | 19.3k | } |
2262 | 19.3k | } |
2263 | | |
2264 | | void ItaniumRecordLayoutBuilder::CheckFieldPadding( |
2265 | | uint64_t Offset, uint64_t UnpaddedOffset, uint64_t UnpackedOffset, |
2266 | 834k | unsigned UnpackedAlign, bool isPacked, const FieldDecl *D) { |
2267 | | // We let objc ivars without warning, objc interfaces generally are not used |
2268 | | // for padding tricks. |
2269 | 834k | if (isa<ObjCIvarDecl>(D)) |
2270 | 6.60k | return; |
2271 | | |
2272 | | // Don't warn about structs created without a SourceLocation. This can |
2273 | | // be done by clients of the AST, such as codegen. |
2274 | 827k | if (D->getLocation().isInvalid()) |
2275 | 84.7k | return; |
2276 | | |
2277 | 743k | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
2278 | | |
2279 | | // Warn if padding was introduced to the struct/class. |
2280 | 743k | if (!IsUnion && Offset > UnpaddedOffset699k ) { |
2281 | 19.3k | unsigned PadSize = Offset - UnpaddedOffset; |
2282 | 19.3k | bool InBits = true; |
2283 | 19.3k | if (PadSize % CharBitNum == 0) { |
2284 | 18.7k | PadSize = PadSize / CharBitNum; |
2285 | 18.7k | InBits = false; |
2286 | 18.7k | } |
2287 | 19.3k | if (D->getIdentifier()) |
2288 | 19.0k | Diag(D->getLocation(), diag::warn_padded_struct_field) |
2289 | 19.0k | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
2290 | 19.0k | << Context.getTypeDeclType(D->getParent()) |
2291 | 19.0k | << PadSize |
2292 | 19.0k | << (InBits ? 1484 : 018.5k ) // (byte|bit) |
2293 | 19.0k | << D->getIdentifier(); |
2294 | 358 | else |
2295 | 358 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
2296 | 358 | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
2297 | 358 | << Context.getTypeDeclType(D->getParent()) |
2298 | 358 | << PadSize |
2299 | 358 | << (InBits ? 190 : 0268 ); // (byte|bit) |
2300 | 19.3k | } |
2301 | 743k | if (isPacked && Offset != UnpackedOffset21.8k ) { |
2302 | 2.11k | HasPackedField = true; |
2303 | 2.11k | } |
2304 | 743k | } |
2305 | | |
2306 | | static const CXXMethodDecl *computeKeyFunction(ASTContext &Context, |
2307 | 145k | const CXXRecordDecl *RD) { |
2308 | | // If a class isn't polymorphic it doesn't have a key function. |
2309 | 145k | if (!RD->isPolymorphic()) |
2310 | 120k | return nullptr; |
2311 | | |
2312 | | // A class that is not externally visible doesn't have a key function. (Or |
2313 | | // at least, there's no point to assigning a key function to such a class; |
2314 | | // this doesn't affect the ABI.) |
2315 | 25.0k | if (!RD->isExternallyVisible()) |
2316 | 548 | return nullptr; |
2317 | | |
2318 | | // Template instantiations don't have key functions per Itanium C++ ABI 5.2.6. |
2319 | | // Same behavior as GCC. |
2320 | 24.4k | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
2321 | 24.4k | if (TSK == TSK_ImplicitInstantiation || |
2322 | 24.4k | TSK == TSK_ExplicitInstantiationDeclaration21.9k || |
2323 | 24.4k | TSK == TSK_ExplicitInstantiationDefinition20.3k ) |
2324 | 4.44k | return nullptr; |
2325 | | |
2326 | 20.0k | bool allowInlineFunctions = |
2327 | 20.0k | Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline(); |
2328 | | |
2329 | 82.9k | for (const CXXMethodDecl *MD : RD->methods()) { |
2330 | 82.9k | if (!MD->isVirtual()) |
2331 | 60.6k | continue; |
2332 | | |
2333 | 22.2k | if (MD->isPure()) |
2334 | 703 | continue; |
2335 | | |
2336 | | // Ignore implicit member functions, they are always marked as inline, but |
2337 | | // they don't have a body until they're defined. |
2338 | 21.5k | if (MD->isImplicit()) |
2339 | 860 | continue; |
2340 | | |
2341 | 20.7k | if (MD->isInlineSpecified() || MD->isConstexpr()20.6k ) |
2342 | 137 | continue; |
2343 | | |
2344 | 20.5k | if (MD->hasInlineBody()) |
2345 | 5.70k | continue; |
2346 | | |
2347 | | // Ignore inline deleted or defaulted functions. |
2348 | 14.8k | if (!MD->isUserProvided()) |
2349 | 71 | continue; |
2350 | | |
2351 | | // In certain ABIs, ignore functions with out-of-line inline definitions. |
2352 | 14.8k | if (!allowInlineFunctions) { |
2353 | 225 | const FunctionDecl *Def; |
2354 | 225 | if (MD->hasBody(Def) && Def->isInlineSpecified()159 ) |
2355 | 88 | continue; |
2356 | 225 | } |
2357 | | |
2358 | 14.7k | if (Context.getLangOpts().CUDA) { |
2359 | | // While compiler may see key method in this TU, during CUDA |
2360 | | // compilation we should ignore methods that are not accessible |
2361 | | // on this side of compilation. |
2362 | 0 | if (Context.getLangOpts().CUDAIsDevice) { |
2363 | | // In device mode ignore methods without __device__ attribute. |
2364 | 0 | if (!MD->hasAttr<CUDADeviceAttr>()) |
2365 | 0 | continue; |
2366 | 0 | } else { |
2367 | | // In host mode ignore __device__-only methods. |
2368 | 0 | if (!MD->hasAttr<CUDAHostAttr>() && MD->hasAttr<CUDADeviceAttr>()) |
2369 | 0 | continue; |
2370 | 0 | } |
2371 | 0 | } |
2372 | | |
2373 | | // If the key function is dllimport but the class isn't, then the class has |
2374 | | // no key function. The DLL that exports the key function won't export the |
2375 | | // vtable in this case. |
2376 | 14.7k | if (MD->hasAttr<DLLImportAttr>() && !RD->hasAttr<DLLImportAttr>()104 && |
2377 | 14.7k | !Context.getTargetInfo().hasPS4DLLImportExport()93 ) |
2378 | 78 | return nullptr; |
2379 | | |
2380 | | // We found it. |
2381 | 14.6k | return MD; |
2382 | 14.7k | } |
2383 | | |
2384 | 5.31k | return nullptr; |
2385 | 20.0k | } |
2386 | | |
2387 | | DiagnosticBuilder ItaniumRecordLayoutBuilder::Diag(SourceLocation Loc, |
2388 | 37.3k | unsigned DiagID) { |
2389 | 37.3k | return Context.getDiagnostics().Report(Loc, DiagID); |
2390 | 37.3k | } |
2391 | | |
2392 | | /// Does the target C++ ABI require us to skip over the tail-padding |
2393 | | /// of the given class (considering it as a base class) when allocating |
2394 | | /// objects? |
2395 | 294k | static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) { |
2396 | 294k | switch (ABI.getTailPaddingUseRules()) { |
2397 | 0 | case TargetCXXABI::AlwaysUseTailPadding: |
2398 | 0 | return false; |
2399 | | |
2400 | 294k | case TargetCXXABI::UseTailPaddingUnlessPOD03: |
2401 | | // FIXME: To the extent that this is meant to cover the Itanium ABI |
2402 | | // rules, we should implement the restrictions about over-sized |
2403 | | // bitfields: |
2404 | | // |
2405 | | // http://itanium-cxx-abi.github.io/cxx-abi/abi.html#POD : |
2406 | | // In general, a type is considered a POD for the purposes of |
2407 | | // layout if it is a POD type (in the sense of ISO C++ |
2408 | | // [basic.types]). However, a POD-struct or POD-union (in the |
2409 | | // sense of ISO C++ [class]) with a bitfield member whose |
2410 | | // declared width is wider than the declared type of the |
2411 | | // bitfield is not a POD for the purpose of layout. Similarly, |
2412 | | // an array type is not a POD for the purpose of layout if the |
2413 | | // element type of the array is not a POD for the purpose of |
2414 | | // layout. |
2415 | | // |
2416 | | // Where references to the ISO C++ are made in this paragraph, |
2417 | | // the Technical Corrigendum 1 version of the standard is |
2418 | | // intended. |
2419 | 294k | return RD->isPOD(); |
2420 | | |
2421 | 297 | case TargetCXXABI::UseTailPaddingUnlessPOD11: |
2422 | | // This is equivalent to RD->getTypeForDecl().isCXX11PODType(), |
2423 | | // but with a lot of abstraction penalty stripped off. This does |
2424 | | // assume that these properties are set correctly even in C++98 |
2425 | | // mode; fortunately, that is true because we want to assign |
2426 | | // consistently semantics to the type-traits intrinsics (or at |
2427 | | // least as many of them as possible). |
2428 | 297 | return RD->isTrivial() && RD->isCXX11StandardLayout()94 ; |
2429 | 294k | } |
2430 | | |
2431 | 0 | llvm_unreachable("bad tail-padding use kind"); |
2432 | 0 | } |
2433 | | |
2434 | 417k | static bool isMsLayout(const ASTContext &Context) { |
2435 | 417k | return Context.getTargetInfo().getCXXABI().isMicrosoft(); |
2436 | 417k | } |
2437 | | |
2438 | | // This section contains an implementation of struct layout that is, up to the |
2439 | | // included tests, compatible with cl.exe (2013). The layout produced is |
2440 | | // significantly different than those produced by the Itanium ABI. Here we note |
2441 | | // the most important differences. |
2442 | | // |
2443 | | // * The alignment of bitfields in unions is ignored when computing the |
2444 | | // alignment of the union. |
2445 | | // * The existence of zero-width bitfield that occurs after anything other than |
2446 | | // a non-zero length bitfield is ignored. |
2447 | | // * There is no explicit primary base for the purposes of layout. All bases |
2448 | | // with vfptrs are laid out first, followed by all bases without vfptrs. |
2449 | | // * The Itanium equivalent vtable pointers are split into a vfptr (virtual |
2450 | | // function pointer) and a vbptr (virtual base pointer). They can each be |
2451 | | // shared with a, non-virtual bases. These bases need not be the same. vfptrs |
2452 | | // always occur at offset 0. vbptrs can occur at an arbitrary offset and are |
2453 | | // placed after the lexicographically last non-virtual base. This placement |
2454 | | // is always before fields but can be in the middle of the non-virtual bases |
2455 | | // due to the two-pass layout scheme for non-virtual-bases. |
2456 | | // * Virtual bases sometimes require a 'vtordisp' field that is laid out before |
2457 | | // the virtual base and is used in conjunction with virtual overrides during |
2458 | | // construction and destruction. This is always a 4 byte value and is used as |
2459 | | // an alternative to constructor vtables. |
2460 | | // * vtordisps are allocated in a block of memory with size and alignment equal |
2461 | | // to the alignment of the completed structure (before applying __declspec( |
2462 | | // align())). The vtordisp always occur at the end of the allocation block, |
2463 | | // immediately prior to the virtual base. |
2464 | | // * vfptrs are injected after all bases and fields have been laid out. In |
2465 | | // order to guarantee proper alignment of all fields, the vfptr injection |
2466 | | // pushes all bases and fields back by the alignment imposed by those bases |
2467 | | // and fields. This can potentially add a significant amount of padding. |
2468 | | // vfptrs are always injected at offset 0. |
2469 | | // * vbptrs are injected after all bases and fields have been laid out. In |
2470 | | // order to guarantee proper alignment of all fields, the vfptr injection |
2471 | | // pushes all bases and fields back by the alignment imposed by those bases |
2472 | | // and fields. This can potentially add a significant amount of padding. |
2473 | | // vbptrs are injected immediately after the last non-virtual base as |
2474 | | // lexicographically ordered in the code. If this site isn't pointer aligned |
2475 | | // the vbptr is placed at the next properly aligned location. Enough padding |
2476 | | // is added to guarantee a fit. |
2477 | | // * The last zero sized non-virtual base can be placed at the end of the |
2478 | | // struct (potentially aliasing another object), or may alias with the first |
2479 | | // field, even if they are of the same type. |
2480 | | // * The last zero size virtual base may be placed at the end of the struct |
2481 | | // potentially aliasing another object. |
2482 | | // * The ABI attempts to avoid aliasing of zero sized bases by adding padding |
2483 | | // between bases or vbases with specific properties. The criteria for |
2484 | | // additional padding between two bases is that the first base is zero sized |
2485 | | // or ends with a zero sized subobject and the second base is zero sized or |
2486 | | // trails with a zero sized base or field (sharing of vfptrs can reorder the |
2487 | | // layout of the so the leading base is not always the first one declared). |
2488 | | // This rule does take into account fields that are not records, so padding |
2489 | | // will occur even if the last field is, e.g. an int. The padding added for |
2490 | | // bases is 1 byte. The padding added between vbases depends on the alignment |
2491 | | // of the object but is at least 4 bytes (in both 32 and 64 bit modes). |
2492 | | // * There is no concept of non-virtual alignment, non-virtual alignment and |
2493 | | // alignment are always identical. |
2494 | | // * There is a distinction between alignment and required alignment. |
2495 | | // __declspec(align) changes the required alignment of a struct. This |
2496 | | // alignment is _always_ obeyed, even in the presence of #pragma pack. A |
2497 | | // record inherits required alignment from all of its fields and bases. |
2498 | | // * __declspec(align) on bitfields has the effect of changing the bitfield's |
2499 | | // alignment instead of its required alignment. This is the only known way |
2500 | | // to make the alignment of a struct bigger than 8. Interestingly enough |
2501 | | // this alignment is also immune to the effects of #pragma pack and can be |
2502 | | // used to create structures with large alignment under #pragma pack. |
2503 | | // However, because it does not impact required alignment, such a structure, |
2504 | | // when used as a field or base, will not be aligned if #pragma pack is |
2505 | | // still active at the time of use. |
2506 | | // |
2507 | | // Known incompatibilities: |
2508 | | // * all: #pragma pack between fields in a record |
2509 | | // * 2010 and back: If the last field in a record is a bitfield, every object |
2510 | | // laid out after the record will have extra padding inserted before it. The |
2511 | | // extra padding will have size equal to the size of the storage class of the |
2512 | | // bitfield. 0 sized bitfields don't exhibit this behavior and the extra |
2513 | | // padding can be avoided by adding a 0 sized bitfield after the non-zero- |
2514 | | // sized bitfield. |
2515 | | // * 2012 and back: In 64-bit mode, if the alignment of a record is 16 or |
2516 | | // greater due to __declspec(align()) then a second layout phase occurs after |
2517 | | // The locations of the vf and vb pointers are known. This layout phase |
2518 | | // suffers from the "last field is a bitfield" bug in 2010 and results in |
2519 | | // _every_ field getting padding put in front of it, potentially including the |
2520 | | // vfptr, leaving the vfprt at a non-zero location which results in a fault if |
2521 | | // anything tries to read the vftbl. The second layout phase also treats |
2522 | | // bitfields as separate entities and gives them each storage rather than |
2523 | | // packing them. Additionally, because this phase appears to perform a |
2524 | | // (an unstable) sort on the members before laying them out and because merged |
2525 | | // bitfields have the same address, the bitfields end up in whatever order |
2526 | | // the sort left them in, a behavior we could never hope to replicate. |
2527 | | |
2528 | | namespace { |
2529 | | struct MicrosoftRecordLayoutBuilder { |
2530 | | struct ElementInfo { |
2531 | | CharUnits Size; |
2532 | | CharUnits Alignment; |
2533 | | }; |
2534 | | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
2535 | 7.87k | MicrosoftRecordLayoutBuilder(const ASTContext &Context) : Context(Context) {} |
2536 | | private: |
2537 | | MicrosoftRecordLayoutBuilder(const MicrosoftRecordLayoutBuilder &) = delete; |
2538 | | void operator=(const MicrosoftRecordLayoutBuilder &) = delete; |
2539 | | public: |
2540 | | void layout(const RecordDecl *RD); |
2541 | | void cxxLayout(const CXXRecordDecl *RD); |
2542 | | /// Initializes size and alignment and honors some flags. |
2543 | | void initializeLayout(const RecordDecl *RD); |
2544 | | /// Initialized C++ layout, compute alignment and virtual alignment and |
2545 | | /// existence of vfptrs and vbptrs. Alignment is needed before the vfptr is |
2546 | | /// laid out. |
2547 | | void initializeCXXLayout(const CXXRecordDecl *RD); |
2548 | | void layoutNonVirtualBases(const CXXRecordDecl *RD); |
2549 | | void layoutNonVirtualBase(const CXXRecordDecl *RD, |
2550 | | const CXXRecordDecl *BaseDecl, |
2551 | | const ASTRecordLayout &BaseLayout, |
2552 | | const ASTRecordLayout *&PreviousBaseLayout); |
2553 | | void injectVFPtr(const CXXRecordDecl *RD); |
2554 | | void injectVBPtr(const CXXRecordDecl *RD); |
2555 | | /// Lays out the fields of the record. Also rounds size up to |
2556 | | /// alignment. |
2557 | | void layoutFields(const RecordDecl *RD); |
2558 | | void layoutField(const FieldDecl *FD); |
2559 | | void layoutBitField(const FieldDecl *FD); |
2560 | | /// Lays out a single zero-width bit-field in the record and handles |
2561 | | /// special cases associated with zero-width bit-fields. |
2562 | | void layoutZeroWidthBitField(const FieldDecl *FD); |
2563 | | void layoutVirtualBases(const CXXRecordDecl *RD); |
2564 | | void finalizeLayout(const RecordDecl *RD); |
2565 | | /// Gets the size and alignment of a base taking pragma pack and |
2566 | | /// __declspec(align) into account. |
2567 | | ElementInfo getAdjustedElementInfo(const ASTRecordLayout &Layout); |
2568 | | /// Gets the size and alignment of a field taking pragma pack and |
2569 | | /// __declspec(align) into account. It also updates RequiredAlignment as a |
2570 | | /// side effect because it is most convenient to do so here. |
2571 | | ElementInfo getAdjustedElementInfo(const FieldDecl *FD); |
2572 | | /// Places a field at an offset in CharUnits. |
2573 | 3.27k | void placeFieldAtOffset(CharUnits FieldOffset) { |
2574 | 3.27k | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
2575 | 3.27k | } |
2576 | | /// Places a bitfield at a bit offset. |
2577 | 66 | void placeFieldAtBitOffset(uint64_t FieldOffset) { |
2578 | 66 | FieldOffsets.push_back(FieldOffset); |
2579 | 66 | } |
2580 | | /// Compute the set of virtual bases for which vtordisps are required. |
2581 | | void computeVtorDispSet( |
2582 | | llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtorDispSet, |
2583 | | const CXXRecordDecl *RD) const; |
2584 | | const ASTContext &Context; |
2585 | | /// The size of the record being laid out. |
2586 | | CharUnits Size; |
2587 | | /// The non-virtual size of the record layout. |
2588 | | CharUnits NonVirtualSize; |
2589 | | /// The data size of the record layout. |
2590 | | CharUnits DataSize; |
2591 | | /// The current alignment of the record layout. |
2592 | | CharUnits Alignment; |
2593 | | /// The maximum allowed field alignment. This is set by #pragma pack. |
2594 | | CharUnits MaxFieldAlignment; |
2595 | | /// The alignment that this record must obey. This is imposed by |
2596 | | /// __declspec(align()) on the record itself or one of its fields or bases. |
2597 | | CharUnits RequiredAlignment; |
2598 | | /// The size of the allocation of the currently active bitfield. |
2599 | | /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield |
2600 | | /// is true. |
2601 | | CharUnits CurrentBitfieldSize; |
2602 | | /// Offset to the virtual base table pointer (if one exists). |
2603 | | CharUnits VBPtrOffset; |
2604 | | /// Minimum record size possible. |
2605 | | CharUnits MinEmptyStructSize; |
2606 | | /// The size and alignment info of a pointer. |
2607 | | ElementInfo PointerInfo; |
2608 | | /// The primary base class (if one exists). |
2609 | | const CXXRecordDecl *PrimaryBase; |
2610 | | /// The class we share our vb-pointer with. |
2611 | | const CXXRecordDecl *SharedVBPtrBase; |
2612 | | /// The collection of field offsets. |
2613 | | SmallVector<uint64_t, 16> FieldOffsets; |
2614 | | /// Base classes and their offsets in the record. |
2615 | | BaseOffsetsMapTy Bases; |
2616 | | /// virtual base classes and their offsets in the record. |
2617 | | ASTRecordLayout::VBaseOffsetsMapTy VBases; |
2618 | | /// The number of remaining bits in our last bitfield allocation. |
2619 | | /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield is |
2620 | | /// true. |
2621 | | unsigned RemainingBitsInField; |
2622 | | bool IsUnion : 1; |
2623 | | /// True if the last field laid out was a bitfield and was not 0 |
2624 | | /// width. |
2625 | | bool LastFieldIsNonZeroWidthBitfield : 1; |
2626 | | /// True if the class has its own vftable pointer. |
2627 | | bool HasOwnVFPtr : 1; |
2628 | | /// True if the class has a vbtable pointer. |
2629 | | bool HasVBPtr : 1; |
2630 | | /// True if the last sub-object within the type is zero sized or the |
2631 | | /// object itself is zero sized. This *does not* count members that are not |
2632 | | /// records. Only used for MS-ABI. |
2633 | | bool EndsWithZeroSizedObject : 1; |
2634 | | /// True if this class is zero sized or first base is zero sized or |
2635 | | /// has this property. Only used for MS-ABI. |
2636 | | bool LeadsWithZeroSizedBase : 1; |
2637 | | |
2638 | | /// True if the external AST source provided a layout for this record. |
2639 | | bool UseExternalLayout : 1; |
2640 | | |
2641 | | /// The layout provided by the external AST source. Only active if |
2642 | | /// UseExternalLayout is true. |
2643 | | ExternalLayout External; |
2644 | | }; |
2645 | | } // namespace |
2646 | | |
2647 | | MicrosoftRecordLayoutBuilder::ElementInfo |
2648 | | MicrosoftRecordLayoutBuilder::getAdjustedElementInfo( |
2649 | 3.14k | const ASTRecordLayout &Layout) { |
2650 | 3.14k | ElementInfo Info; |
2651 | 3.14k | Info.Alignment = Layout.getAlignment(); |
2652 | | // Respect pragma pack. |
2653 | 3.14k | if (!MaxFieldAlignment.isZero()) |
2654 | 34 | Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment); |
2655 | | // Track zero-sized subobjects here where it's already available. |
2656 | 3.14k | EndsWithZeroSizedObject = Layout.endsWithZeroSizedObject(); |
2657 | | // Respect required alignment, this is necessary because we may have adjusted |
2658 | | // the alignment in the case of pragma pack. Note that the required alignment |
2659 | | // doesn't actually apply to the struct alignment at this point. |
2660 | 3.14k | Alignment = std::max(Alignment, Info.Alignment); |
2661 | 3.14k | RequiredAlignment = std::max(RequiredAlignment, Layout.getRequiredAlignment()); |
2662 | 3.14k | Info.Alignment = std::max(Info.Alignment, Layout.getRequiredAlignment()); |
2663 | 3.14k | Info.Size = Layout.getNonVirtualSize(); |
2664 | 3.14k | return Info; |
2665 | 3.14k | } |
2666 | | |
2667 | | MicrosoftRecordLayoutBuilder::ElementInfo |
2668 | | MicrosoftRecordLayoutBuilder::getAdjustedElementInfo( |
2669 | 3.31k | const FieldDecl *FD) { |
2670 | | // Get the alignment of the field type's natural alignment, ignore any |
2671 | | // alignment attributes. |
2672 | 3.31k | auto TInfo = |
2673 | 3.31k | Context.getTypeInfoInChars(FD->getType()->getUnqualifiedDesugaredType()); |
2674 | 3.31k | ElementInfo Info{TInfo.Width, TInfo.Align}; |
2675 | | // Respect align attributes on the field. |
2676 | 3.31k | CharUnits FieldRequiredAlignment = |
2677 | 3.31k | Context.toCharUnitsFromBits(FD->getMaxAlignment()); |
2678 | | // Respect align attributes on the type. |
2679 | 3.31k | if (Context.isAlignmentRequired(FD->getType())) |
2680 | 84 | FieldRequiredAlignment = std::max( |
2681 | 84 | Context.getTypeAlignInChars(FD->getType()), FieldRequiredAlignment); |
2682 | | // Respect attributes applied to subobjects of the field. |
2683 | 3.31k | if (FD->isBitField()) |
2684 | | // For some reason __declspec align impacts alignment rather than required |
2685 | | // alignment when it is applied to bitfields. |
2686 | 230 | Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment); |
2687 | 3.08k | else { |
2688 | 3.08k | if (auto RT = |
2689 | 3.08k | FD->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) { |
2690 | 256 | auto const &Layout = Context.getASTRecordLayout(RT->getDecl()); |
2691 | 256 | EndsWithZeroSizedObject = Layout.endsWithZeroSizedObject(); |
2692 | 256 | FieldRequiredAlignment = std::max(FieldRequiredAlignment, |
2693 | 256 | Layout.getRequiredAlignment()); |
2694 | 256 | } |
2695 | | // Capture required alignment as a side-effect. |
2696 | 3.08k | RequiredAlignment = std::max(RequiredAlignment, FieldRequiredAlignment); |
2697 | 3.08k | } |
2698 | | // Respect pragma pack, attribute pack and declspec align |
2699 | 3.31k | if (!MaxFieldAlignment.isZero()) |
2700 | 257 | Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment); |
2701 | 3.31k | if (FD->hasAttr<PackedAttr>()) |
2702 | 7 | Info.Alignment = CharUnits::One(); |
2703 | 3.31k | Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment); |
2704 | 3.31k | return Info; |
2705 | 3.31k | } |
2706 | | |
2707 | 348 | void MicrosoftRecordLayoutBuilder::layout(const RecordDecl *RD) { |
2708 | | // For C record layout, zero-sized records always have size 4. |
2709 | 348 | MinEmptyStructSize = CharUnits::fromQuantity(4); |
2710 | 348 | initializeLayout(RD); |
2711 | 348 | layoutFields(RD); |
2712 | 348 | DataSize = Size = Size.alignTo(Alignment); |
2713 | 348 | RequiredAlignment = std::max( |
2714 | 348 | RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment())); |
2715 | 348 | finalizeLayout(RD); |
2716 | 348 | } |
2717 | | |
2718 | 7.52k | void MicrosoftRecordLayoutBuilder::cxxLayout(const CXXRecordDecl *RD) { |
2719 | | // The C++ standard says that empty structs have size 1. |
2720 | 7.52k | MinEmptyStructSize = CharUnits::One(); |
2721 | 7.52k | initializeLayout(RD); |
2722 | 7.52k | initializeCXXLayout(RD); |
2723 | 7.52k | layoutNonVirtualBases(RD); |
2724 | 7.52k | layoutFields(RD); |
2725 | 7.52k | injectVBPtr(RD); |
2726 | 7.52k | injectVFPtr(RD); |
2727 | 7.52k | if (HasOwnVFPtr || (6.68k HasVBPtr6.68k && !SharedVBPtrBase748 )) |
2728 | 1.38k | Alignment = std::max(Alignment, PointerInfo.Alignment); |
2729 | 7.52k | auto RoundingAlignment = Alignment; |
2730 | 7.52k | if (!MaxFieldAlignment.isZero()) |
2731 | 56 | RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment); |
2732 | 7.52k | if (!UseExternalLayout) |
2733 | 7.51k | Size = Size.alignTo(RoundingAlignment); |
2734 | 7.52k | NonVirtualSize = Size; |
2735 | 7.52k | RequiredAlignment = std::max( |
2736 | 7.52k | RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment())); |
2737 | 7.52k | layoutVirtualBases(RD); |
2738 | 7.52k | finalizeLayout(RD); |
2739 | 7.52k | } |
2740 | | |
2741 | 7.87k | void MicrosoftRecordLayoutBuilder::initializeLayout(const RecordDecl *RD) { |
2742 | 7.87k | IsUnion = RD->isUnion(); |
2743 | 7.87k | Size = CharUnits::Zero(); |
2744 | 7.87k | Alignment = CharUnits::One(); |
2745 | | // In 64-bit mode we always perform an alignment step after laying out vbases. |
2746 | | // In 32-bit mode we do not. The check to see if we need to perform alignment |
2747 | | // checks the RequiredAlignment field and performs alignment if it isn't 0. |
2748 | 7.87k | RequiredAlignment = Context.getTargetInfo().getTriple().isArch64Bit() |
2749 | 7.87k | ? CharUnits::One()3.83k |
2750 | 7.87k | : CharUnits::Zero()4.03k ; |
2751 | | // Compute the maximum field alignment. |
2752 | 7.87k | MaxFieldAlignment = CharUnits::Zero(); |
2753 | | // Honor the default struct packing maximum alignment flag. |
2754 | 7.87k | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) |
2755 | 0 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
2756 | | // Honor the packing attribute. The MS-ABI ignores pragma pack if its larger |
2757 | | // than the pointer size. |
2758 | 7.87k | if (const MaxFieldAlignmentAttr *MFAA = RD->getAttr<MaxFieldAlignmentAttr>()){ |
2759 | 125 | unsigned PackedAlignment = MFAA->getAlignment(); |
2760 | 125 | if (PackedAlignment <= Context.getTargetInfo().getPointerWidth(0)) |
2761 | 92 | MaxFieldAlignment = Context.toCharUnitsFromBits(PackedAlignment); |
2762 | 125 | } |
2763 | | // Packed attribute forces max field alignment to be 1. |
2764 | 7.87k | if (RD->hasAttr<PackedAttr>()) |
2765 | 61 | MaxFieldAlignment = CharUnits::One(); |
2766 | | |
2767 | | // Try to respect the external layout if present. |
2768 | 7.87k | UseExternalLayout = false; |
2769 | 7.87k | if (ExternalASTSource *Source = Context.getExternalSource()) |
2770 | 655 | UseExternalLayout = Source->layoutRecordType( |
2771 | 655 | RD, External.Size, External.Align, External.FieldOffsets, |
2772 | 655 | External.BaseOffsets, External.VirtualBaseOffsets); |
2773 | 7.87k | } |
2774 | | |
2775 | | void |
2776 | 7.52k | MicrosoftRecordLayoutBuilder::initializeCXXLayout(const CXXRecordDecl *RD) { |
2777 | 7.52k | EndsWithZeroSizedObject = false; |
2778 | 7.52k | LeadsWithZeroSizedBase = false; |
2779 | 7.52k | HasOwnVFPtr = false; |
2780 | 7.52k | HasVBPtr = false; |
2781 | 7.52k | PrimaryBase = nullptr; |
2782 | 7.52k | SharedVBPtrBase = nullptr; |
2783 | | // Calculate pointer size and alignment. These are used for vfptr and vbprt |
2784 | | // injection. |
2785 | 7.52k | PointerInfo.Size = |
2786 | 7.52k | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
2787 | 7.52k | PointerInfo.Alignment = |
2788 | 7.52k | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
2789 | | // Respect pragma pack. |
2790 | 7.52k | if (!MaxFieldAlignment.isZero()) |
2791 | 56 | PointerInfo.Alignment = std::min(PointerInfo.Alignment, MaxFieldAlignment); |
2792 | 7.52k | } |
2793 | | |
2794 | | void |
2795 | 7.52k | MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) { |
2796 | | // The MS-ABI lays out all bases that contain leading vfptrs before it lays |
2797 | | // out any bases that do not contain vfptrs. We implement this as two passes |
2798 | | // over the bases. This approach guarantees that the primary base is laid out |
2799 | | // first. We use these passes to calculate some additional aggregated |
2800 | | // information about the bases, such as required alignment and the presence of |
2801 | | // zero sized members. |
2802 | 7.52k | const ASTRecordLayout *PreviousBaseLayout = nullptr; |
2803 | 7.52k | bool HasPolymorphicBaseClass = false; |
2804 | | // Iterate through the bases and lay out the non-virtual ones. |
2805 | 7.52k | for (const CXXBaseSpecifier &Base : RD->bases()) { |
2806 | 2.78k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
2807 | 2.78k | HasPolymorphicBaseClass |= BaseDecl->isPolymorphic(); |
2808 | 2.78k | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
2809 | | // Mark and skip virtual bases. |
2810 | 2.78k | if (Base.isVirtual()) { |
2811 | 916 | HasVBPtr = true; |
2812 | 916 | continue; |
2813 | 916 | } |
2814 | | // Check for a base to share a VBPtr with. |
2815 | 1.86k | if (!SharedVBPtrBase && BaseLayout.hasVBPtr()1.74k ) { |
2816 | 228 | SharedVBPtrBase = BaseDecl; |
2817 | 228 | HasVBPtr = true; |
2818 | 228 | } |
2819 | | // Only lay out bases with extendable VFPtrs on the first pass. |
2820 | 1.86k | if (!BaseLayout.hasExtendableVFPtr()) |
2821 | 1.33k | continue; |
2822 | | // If we don't have a primary base, this one qualifies. |
2823 | 534 | if (!PrimaryBase) { |
2824 | 391 | PrimaryBase = BaseDecl; |
2825 | 391 | LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase(); |
2826 | 391 | } |
2827 | | // Lay out the base. |
2828 | 534 | layoutNonVirtualBase(RD, BaseDecl, BaseLayout, PreviousBaseLayout); |
2829 | 534 | } |
2830 | | // Figure out if we need a fresh VFPtr for this class. |
2831 | 7.52k | if (RD->isPolymorphic()) { |
2832 | 1.50k | if (!HasPolymorphicBaseClass) |
2833 | | // This class introduces polymorphism, so we need a vftable to store the |
2834 | | // RTTI information. |
2835 | 755 | HasOwnVFPtr = true; |
2836 | 754 | else if (!PrimaryBase) { |
2837 | | // We have a polymorphic base class but can't extend its vftable. Add a |
2838 | | // new vfptr if we would use any vftable slots. |
2839 | 1.63k | for (CXXMethodDecl *M : RD->methods()) { |
2840 | 1.63k | if (MicrosoftVTableContext::hasVtableSlot(M) && |
2841 | 1.63k | M->size_overridden_methods() == 0325 ) { |
2842 | 85 | HasOwnVFPtr = true; |
2843 | 85 | break; |
2844 | 85 | } |
2845 | 1.63k | } |
2846 | 363 | } |
2847 | 1.50k | } |
2848 | | // If we don't have a primary base then we have a leading object that could |
2849 | | // itself lead with a zero-sized object, something we track. |
2850 | 7.52k | bool CheckLeadingLayout = !PrimaryBase; |
2851 | | // Iterate through the bases and lay out the non-virtual ones. |
2852 | 7.52k | for (const CXXBaseSpecifier &Base : RD->bases()) { |
2853 | 2.78k | if (Base.isVirtual()) |
2854 | 916 | continue; |
2855 | 1.86k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
2856 | 1.86k | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
2857 | | // Only lay out bases without extendable VFPtrs on the second pass. |
2858 | 1.86k | if (BaseLayout.hasExtendableVFPtr()) { |
2859 | 534 | VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize(); |
2860 | 534 | continue; |
2861 | 534 | } |
2862 | | // If this is the first layout, check to see if it leads with a zero sized |
2863 | | // object. If it does, so do we. |
2864 | 1.33k | if (CheckLeadingLayout) { |
2865 | 908 | CheckLeadingLayout = false; |
2866 | 908 | LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase(); |
2867 | 908 | } |
2868 | | // Lay out the base. |
2869 | 1.33k | layoutNonVirtualBase(RD, BaseDecl, BaseLayout, PreviousBaseLayout); |
2870 | 1.33k | VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize(); |
2871 | 1.33k | } |
2872 | | // Set our VBPtroffset if we know it at this point. |
2873 | 7.52k | if (!HasVBPtr) |
2874 | 6.66k | VBPtrOffset = CharUnits::fromQuantity(-1); |
2875 | 861 | else if (SharedVBPtrBase) { |
2876 | 228 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(SharedVBPtrBase); |
2877 | 228 | VBPtrOffset = Bases[SharedVBPtrBase] + Layout.getVBPtrOffset(); |
2878 | 228 | } |
2879 | 7.52k | } |
2880 | | |
2881 | 6.13k | static bool recordUsesEBO(const RecordDecl *RD) { |
2882 | 6.13k | if (!isa<CXXRecordDecl>(RD)) |
2883 | 30 | return false; |
2884 | 6.10k | if (RD->hasAttr<EmptyBasesAttr>()) |
2885 | 16 | return true; |
2886 | 6.09k | if (auto *LVA = RD->getAttr<LayoutVersionAttr>()) |
2887 | | // TODO: Double check with the next version of MSVC. |
2888 | 0 | if (LVA->getVersion() <= LangOptions::MSVC2015) |
2889 | 0 | return false; |
2890 | | // TODO: Some later version of MSVC will change the default behavior of the |
2891 | | // compiler to enable EBO by default. When this happens, we will need an |
2892 | | // additional isCompatibleWithMSVC check. |
2893 | 6.09k | return false; |
2894 | 6.09k | } |
2895 | | |
2896 | | void MicrosoftRecordLayoutBuilder::layoutNonVirtualBase( |
2897 | | const CXXRecordDecl *RD, |
2898 | | const CXXRecordDecl *BaseDecl, |
2899 | | const ASTRecordLayout &BaseLayout, |
2900 | 1.86k | const ASTRecordLayout *&PreviousBaseLayout) { |
2901 | | // Insert padding between two bases if the left first one is zero sized or |
2902 | | // contains a zero sized subobject and the right is zero sized or one leads |
2903 | | // with a zero sized base. |
2904 | 1.86k | bool MDCUsesEBO = recordUsesEBO(RD); |
2905 | 1.86k | if (PreviousBaseLayout && PreviousBaseLayout->endsWithZeroSizedObject()565 && |
2906 | 1.86k | BaseLayout.leadsWithZeroSizedBase()178 && !MDCUsesEBO142 ) |
2907 | 140 | Size++; |
2908 | 1.86k | ElementInfo Info = getAdjustedElementInfo(BaseLayout); |
2909 | 1.86k | CharUnits BaseOffset; |
2910 | | |
2911 | | // Respect the external AST source base offset, if present. |
2912 | 1.86k | bool FoundBase = false; |
2913 | 1.86k | if (UseExternalLayout) { |
2914 | 2 | FoundBase = External.getExternalNVBaseOffset(BaseDecl, BaseOffset); |
2915 | 2 | if (FoundBase) { |
2916 | 0 | assert(BaseOffset >= Size && "base offset already allocated"); |
2917 | 0 | Size = BaseOffset; |
2918 | 0 | } |
2919 | 2 | } |
2920 | | |
2921 | 1.86k | if (!FoundBase) { |
2922 | 1.86k | if (MDCUsesEBO && BaseDecl->isEmpty()12 ) { |
2923 | 4 | assert(BaseLayout.getNonVirtualSize() == CharUnits::Zero()); |
2924 | 0 | BaseOffset = CharUnits::Zero(); |
2925 | 1.86k | } else { |
2926 | | // Otherwise, lay the base out at the end of the MDC. |
2927 | 1.86k | BaseOffset = Size = Size.alignTo(Info.Alignment); |
2928 | 1.86k | } |
2929 | 1.86k | } |
2930 | 0 | Bases.insert(std::make_pair(BaseDecl, BaseOffset)); |
2931 | 1.86k | Size += BaseLayout.getNonVirtualSize(); |
2932 | 1.86k | PreviousBaseLayout = &BaseLayout; |
2933 | 1.86k | } |
2934 | | |
2935 | 7.87k | void MicrosoftRecordLayoutBuilder::layoutFields(const RecordDecl *RD) { |
2936 | 7.87k | LastFieldIsNonZeroWidthBitfield = false; |
2937 | 7.87k | for (const FieldDecl *Field : RD->fields()) |
2938 | 3.33k | layoutField(Field); |
2939 | 7.87k | } |
2940 | | |
2941 | 3.33k | void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) { |
2942 | 3.33k | if (FD->isBitField()) { |
2943 | 251 | layoutBitField(FD); |
2944 | 251 | return; |
2945 | 251 | } |
2946 | 3.08k | LastFieldIsNonZeroWidthBitfield = false; |
2947 | 3.08k | ElementInfo Info = getAdjustedElementInfo(FD); |
2948 | 3.08k | Alignment = std::max(Alignment, Info.Alignment); |
2949 | 3.08k | CharUnits FieldOffset; |
2950 | 3.08k | if (UseExternalLayout) |
2951 | 6 | FieldOffset = |
2952 | 6 | Context.toCharUnitsFromBits(External.getExternalFieldOffset(FD)); |
2953 | 3.08k | else if (IsUnion) |
2954 | 81 | FieldOffset = CharUnits::Zero(); |
2955 | 3.00k | else |
2956 | 3.00k | FieldOffset = Size.alignTo(Info.Alignment); |
2957 | 3.08k | placeFieldAtOffset(FieldOffset); |
2958 | 3.08k | Size = std::max(Size, FieldOffset + Info.Size); |
2959 | 3.08k | } |
2960 | | |
2961 | 251 | void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) { |
2962 | 251 | unsigned Width = FD->getBitWidthValue(Context); |
2963 | 251 | if (Width == 0) { |
2964 | 43 | layoutZeroWidthBitField(FD); |
2965 | 43 | return; |
2966 | 43 | } |
2967 | 208 | ElementInfo Info = getAdjustedElementInfo(FD); |
2968 | | // Clamp the bitfield to a containable size for the sake of being able |
2969 | | // to lay them out. Sema will throw an error. |
2970 | 208 | if (Width > Context.toBits(Info.Size)) |
2971 | 0 | Width = Context.toBits(Info.Size); |
2972 | | // Check to see if this bitfield fits into an existing allocation. Note: |
2973 | | // MSVC refuses to pack bitfields of formal types with different sizes |
2974 | | // into the same allocation. |
2975 | 208 | if (!UseExternalLayout && !IsUnion203 && LastFieldIsNonZeroWidthBitfield185 && |
2976 | 208 | CurrentBitfieldSize == Info.Size100 && Width <= RemainingBitsInField91 ) { |
2977 | 61 | placeFieldAtBitOffset(Context.toBits(Size) - RemainingBitsInField); |
2978 | 61 | RemainingBitsInField -= Width; |
2979 | 61 | return; |
2980 | 61 | } |
2981 | 147 | LastFieldIsNonZeroWidthBitfield = true; |
2982 | 147 | CurrentBitfieldSize = Info.Size; |
2983 | 147 | if (UseExternalLayout) { |
2984 | 5 | auto FieldBitOffset = External.getExternalFieldOffset(FD); |
2985 | 5 | placeFieldAtBitOffset(FieldBitOffset); |
2986 | 5 | auto NewSize = Context.toCharUnitsFromBits( |
2987 | 5 | llvm::alignDown(FieldBitOffset, Context.toBits(Info.Alignment)) + |
2988 | 5 | Context.toBits(Info.Size)); |
2989 | 5 | Size = std::max(Size, NewSize); |
2990 | 5 | Alignment = std::max(Alignment, Info.Alignment); |
2991 | 142 | } else if (IsUnion) { |
2992 | 18 | placeFieldAtOffset(CharUnits::Zero()); |
2993 | 18 | Size = std::max(Size, Info.Size); |
2994 | | // TODO: Add a Sema warning that MS ignores bitfield alignment in unions. |
2995 | 124 | } else { |
2996 | | // Allocate a new block of memory and place the bitfield in it. |
2997 | 124 | CharUnits FieldOffset = Size.alignTo(Info.Alignment); |
2998 | 124 | placeFieldAtOffset(FieldOffset); |
2999 | 124 | Size = FieldOffset + Info.Size; |
3000 | 124 | Alignment = std::max(Alignment, Info.Alignment); |
3001 | 124 | RemainingBitsInField = Context.toBits(Info.Size) - Width; |
3002 | 124 | } |
3003 | 147 | } |
3004 | | |
3005 | | void |
3006 | 43 | MicrosoftRecordLayoutBuilder::layoutZeroWidthBitField(const FieldDecl *FD) { |
3007 | | // Zero-width bitfields are ignored unless they follow a non-zero-width |
3008 | | // bitfield. |
3009 | 43 | if (!LastFieldIsNonZeroWidthBitfield) { |
3010 | 21 | placeFieldAtOffset(IsUnion ? CharUnits::Zero()4 : Size17 ); |
3011 | | // TODO: Add a Sema warning that MS ignores alignment for zero |
3012 | | // sized bitfields that occur after zero-size bitfields or non-bitfields. |
3013 | 21 | return; |
3014 | 21 | } |
3015 | 22 | LastFieldIsNonZeroWidthBitfield = false; |
3016 | 22 | ElementInfo Info = getAdjustedElementInfo(FD); |
3017 | 22 | if (IsUnion) { |
3018 | 8 | placeFieldAtOffset(CharUnits::Zero()); |
3019 | 8 | Size = std::max(Size, Info.Size); |
3020 | | // TODO: Add a Sema warning that MS ignores bitfield alignment in unions. |
3021 | 14 | } else { |
3022 | | // Round up the current record size to the field's alignment boundary. |
3023 | 14 | CharUnits FieldOffset = Size.alignTo(Info.Alignment); |
3024 | 14 | placeFieldAtOffset(FieldOffset); |
3025 | 14 | Size = FieldOffset; |
3026 | 14 | Alignment = std::max(Alignment, Info.Alignment); |
3027 | 14 | } |
3028 | 22 | } |
3029 | | |
3030 | 7.52k | void MicrosoftRecordLayoutBuilder::injectVBPtr(const CXXRecordDecl *RD) { |
3031 | 7.52k | if (!HasVBPtr || SharedVBPtrBase861 ) |
3032 | 6.88k | return; |
3033 | | // Inject the VBPointer at the injection site. |
3034 | 633 | CharUnits InjectionSite = VBPtrOffset; |
3035 | | // But before we do, make sure it's properly aligned. |
3036 | 633 | VBPtrOffset = VBPtrOffset.alignTo(PointerInfo.Alignment); |
3037 | | // Determine where the first field should be laid out after the vbptr. |
3038 | 633 | CharUnits FieldStart = VBPtrOffset + PointerInfo.Size; |
3039 | | // Shift everything after the vbptr down, unless we're using an external |
3040 | | // layout. |
3041 | 633 | if (UseExternalLayout) { |
3042 | | // It is possible that there were no fields or bases located after vbptr, |
3043 | | // so the size was not adjusted before. |
3044 | 2 | if (Size < FieldStart) |
3045 | 1 | Size = FieldStart; |
3046 | 2 | return; |
3047 | 2 | } |
3048 | | // Make sure that the amount we push the fields back by is a multiple of the |
3049 | | // alignment. |
3050 | 631 | CharUnits Offset = (FieldStart - InjectionSite) |
3051 | 631 | .alignTo(std::max(RequiredAlignment, Alignment)); |
3052 | 631 | Size += Offset; |
3053 | 631 | for (uint64_t &FieldOffset : FieldOffsets) |
3054 | 360 | FieldOffset += Context.toBits(Offset); |
3055 | 631 | for (BaseOffsetsMapTy::value_type &Base : Bases) |
3056 | 256 | if (Base.second >= InjectionSite) |
3057 | 63 | Base.second += Offset; |
3058 | 631 | } |
3059 | | |
3060 | 7.52k | void MicrosoftRecordLayoutBuilder::injectVFPtr(const CXXRecordDecl *RD) { |
3061 | 7.52k | if (!HasOwnVFPtr) |
3062 | 6.68k | return; |
3063 | | // Make sure that the amount we push the struct back by is a multiple of the |
3064 | | // alignment. |
3065 | 840 | CharUnits Offset = |
3066 | 840 | PointerInfo.Size.alignTo(std::max(RequiredAlignment, Alignment)); |
3067 | | // Push back the vbptr, but increase the size of the object and push back |
3068 | | // regular fields by the offset only if not using external record layout. |
3069 | 840 | if (HasVBPtr) |
3070 | 113 | VBPtrOffset += Offset; |
3071 | | |
3072 | 840 | if (UseExternalLayout) { |
3073 | | // The class may have no bases or fields, but still have a vfptr |
3074 | | // (e.g. it's an interface class). The size was not correctly set before |
3075 | | // in this case. |
3076 | 2 | if (FieldOffsets.empty() && Bases.empty()1 ) |
3077 | 1 | Size += Offset; |
3078 | 2 | return; |
3079 | 2 | } |
3080 | | |
3081 | 838 | Size += Offset; |
3082 | | |
3083 | | // If we're using an external layout, the fields offsets have already |
3084 | | // accounted for this adjustment. |
3085 | 838 | for (uint64_t &FieldOffset : FieldOffsets) |
3086 | 161 | FieldOffset += Context.toBits(Offset); |
3087 | 838 | for (BaseOffsetsMapTy::value_type &Base : Bases) |
3088 | 87 | Base.second += Offset; |
3089 | 838 | } |
3090 | | |
3091 | 7.52k | void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) { |
3092 | 7.52k | if (!HasVBPtr) |
3093 | 6.66k | return; |
3094 | | // Vtordisps are always 4 bytes (even in 64-bit mode) |
3095 | 861 | CharUnits VtorDispSize = CharUnits::fromQuantity(4); |
3096 | 861 | CharUnits VtorDispAlignment = VtorDispSize; |
3097 | | // vtordisps respect pragma pack. |
3098 | 861 | if (!MaxFieldAlignment.isZero()) |
3099 | 16 | VtorDispAlignment = std::min(VtorDispAlignment, MaxFieldAlignment); |
3100 | | // The alignment of the vtordisp is at least the required alignment of the |
3101 | | // entire record. This requirement may be present to support vtordisp |
3102 | | // injection. |
3103 | 1.28k | for (const CXXBaseSpecifier &VBase : RD->vbases()) { |
3104 | 1.28k | const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl(); |
3105 | 1.28k | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
3106 | 1.28k | RequiredAlignment = |
3107 | 1.28k | std::max(RequiredAlignment, BaseLayout.getRequiredAlignment()); |
3108 | 1.28k | } |
3109 | 861 | VtorDispAlignment = std::max(VtorDispAlignment, RequiredAlignment); |
3110 | | // Compute the vtordisp set. |
3111 | 861 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtorDispSet; |
3112 | 861 | computeVtorDispSet(HasVtorDispSet, RD); |
3113 | | // Iterate through the virtual bases and lay them out. |
3114 | 861 | const ASTRecordLayout *PreviousBaseLayout = nullptr; |
3115 | 1.28k | for (const CXXBaseSpecifier &VBase : RD->vbases()) { |
3116 | 1.28k | const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl(); |
3117 | 1.28k | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
3118 | 1.28k | bool HasVtordisp = HasVtorDispSet.contains(BaseDecl); |
3119 | | // Insert padding between two bases if the left first one is zero sized or |
3120 | | // contains a zero sized subobject and the right is zero sized or one leads |
3121 | | // with a zero sized base. The padding between virtual bases is 4 |
3122 | | // bytes (in both 32 and 64 bits modes) and always involves rounding up to |
3123 | | // the required alignment, we don't know why. |
3124 | 1.28k | if ((PreviousBaseLayout && PreviousBaseLayout->endsWithZeroSizedObject()420 && |
3125 | 1.28k | BaseLayout.leadsWithZeroSizedBase()168 && !recordUsesEBO(RD)118 ) || |
3126 | 1.28k | HasVtordisp1.16k ) { |
3127 | 276 | Size = Size.alignTo(VtorDispAlignment) + VtorDispSize; |
3128 | 276 | Alignment = std::max(VtorDispAlignment, Alignment); |
3129 | 276 | } |
3130 | | // Insert the virtual base. |
3131 | 1.28k | ElementInfo Info = getAdjustedElementInfo(BaseLayout); |
3132 | 1.28k | CharUnits BaseOffset; |
3133 | | |
3134 | | // Respect the external AST source base offset, if present. |
3135 | 1.28k | if (UseExternalLayout) { |
3136 | 3 | if (!External.getExternalVBaseOffset(BaseDecl, BaseOffset)) |
3137 | 3 | BaseOffset = Size; |
3138 | 3 | } else |
3139 | 1.27k | BaseOffset = Size.alignTo(Info.Alignment); |
3140 | | |
3141 | 1.28k | assert(BaseOffset >= Size && "base offset already allocated"); |
3142 | | |
3143 | 0 | VBases.insert(std::make_pair(BaseDecl, |
3144 | 1.28k | ASTRecordLayout::VBaseInfo(BaseOffset, HasVtordisp))); |
3145 | 1.28k | Size = BaseOffset + BaseLayout.getNonVirtualSize(); |
3146 | 1.28k | PreviousBaseLayout = &BaseLayout; |
3147 | 1.28k | } |
3148 | 861 | } |
3149 | | |
3150 | 7.87k | void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) { |
3151 | | // Respect required alignment. Note that in 32-bit mode Required alignment |
3152 | | // may be 0 and cause size not to be updated. |
3153 | 7.87k | DataSize = Size; |
3154 | 7.87k | if (!RequiredAlignment.isZero()) { |
3155 | 4.01k | Alignment = std::max(Alignment, RequiredAlignment); |
3156 | 4.01k | auto RoundingAlignment = Alignment; |
3157 | 4.01k | if (!MaxFieldAlignment.isZero()) |
3158 | 132 | RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment); |
3159 | 4.01k | RoundingAlignment = std::max(RoundingAlignment, RequiredAlignment); |
3160 | 4.01k | Size = Size.alignTo(RoundingAlignment); |
3161 | 4.01k | } |
3162 | 7.87k | if (Size.isZero()) { |
3163 | 4.15k | if (!recordUsesEBO(RD) || !cast<CXXRecordDecl>(RD)->isEmpty()4 ) { |
3164 | 4.15k | EndsWithZeroSizedObject = true; |
3165 | 4.15k | LeadsWithZeroSizedBase = true; |
3166 | 4.15k | } |
3167 | | // Zero-sized structures have size equal to their alignment if a |
3168 | | // __declspec(align) came into play. |
3169 | 4.15k | if (RequiredAlignment >= MinEmptyStructSize) |
3170 | 2.06k | Size = Alignment; |
3171 | 2.09k | else |
3172 | 2.09k | Size = MinEmptyStructSize; |
3173 | 4.15k | } |
3174 | | |
3175 | 7.87k | if (UseExternalLayout) { |
3176 | 8 | Size = Context.toCharUnitsFromBits(External.Size); |
3177 | 8 | if (External.Align) |
3178 | 4 | Alignment = Context.toCharUnitsFromBits(External.Align); |
3179 | 8 | } |
3180 | 7.87k | } |
3181 | | |
3182 | | // Recursively walks the non-virtual bases of a class and determines if any of |
3183 | | // them are in the bases with overridden methods set. |
3184 | | static bool |
3185 | | RequiresVtordisp(const llvm::SmallPtrSetImpl<const CXXRecordDecl *> & |
3186 | | BasesWithOverriddenMethods, |
3187 | 681 | const CXXRecordDecl *RD) { |
3188 | 681 | if (BasesWithOverriddenMethods.count(RD)) |
3189 | 94 | return true; |
3190 | | // If any of a virtual bases non-virtual bases (recursively) requires a |
3191 | | // vtordisp than so does this virtual base. |
3192 | 587 | for (const CXXBaseSpecifier &Base : RD->bases()) |
3193 | 92 | if (!Base.isVirtual() && |
3194 | 92 | RequiresVtordisp(BasesWithOverriddenMethods, |
3195 | 53 | Base.getType()->getAsCXXRecordDecl())) |
3196 | 19 | return true; |
3197 | 568 | return false; |
3198 | 587 | } |
3199 | | |
3200 | | void MicrosoftRecordLayoutBuilder::computeVtorDispSet( |
3201 | | llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtordispSet, |
3202 | 861 | const CXXRecordDecl *RD) const { |
3203 | | // /vd2 or #pragma vtordisp(2): Always use vtordisps for virtual bases with |
3204 | | // vftables. |
3205 | 861 | if (RD->getMSVtorDispMode() == MSVtorDispMode::ForVFTable) { |
3206 | 25 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
3207 | 25 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
3208 | 25 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
3209 | 25 | if (Layout.hasExtendableVFPtr()) |
3210 | 23 | HasVtordispSet.insert(BaseDecl); |
3211 | 25 | } |
3212 | 20 | return; |
3213 | 20 | } |
3214 | | |
3215 | | // If any of our bases need a vtordisp for this type, so do we. Check our |
3216 | | // direct bases for vtordisp requirements. |
3217 | 1.53k | for (const CXXBaseSpecifier &Base : RD->bases())841 { |
3218 | 1.53k | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
3219 | 1.53k | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
3220 | 1.53k | for (const auto &bi : Layout.getVBaseOffsetsMap()) |
3221 | 486 | if (bi.second.hasVtorDisp()) |
3222 | 43 | HasVtordispSet.insert(bi.first); |
3223 | 1.53k | } |
3224 | | // We don't introduce any additional vtordisps if either: |
3225 | | // * A user declared constructor or destructor aren't declared. |
3226 | | // * #pragma vtordisp(0) or the /vd0 flag are in use. |
3227 | 841 | if ((!RD->hasUserDeclaredConstructor() && !RD->hasUserDeclaredDestructor()483 ) || |
3228 | 841 | RD->getMSVtorDispMode() == MSVtorDispMode::Never427 ) |
3229 | 419 | return; |
3230 | | // /vd1 or #pragma vtordisp(1): Try to guess based on whether we think it's |
3231 | | // possible for a partially constructed object with virtual base overrides to |
3232 | | // escape a non-trivial constructor. |
3233 | 422 | assert(RD->getMSVtorDispMode() == MSVtorDispMode::ForVBaseOverride); |
3234 | | // Compute a set of base classes which define methods we override. A virtual |
3235 | | // base in this set will require a vtordisp. A virtual base that transitively |
3236 | | // contains one of these bases as a non-virtual base will also require a |
3237 | | // vtordisp. |
3238 | 0 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> Work; |
3239 | 422 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> BasesWithOverriddenMethods; |
3240 | | // Seed the working set with our non-destructor, non-pure virtual methods. |
3241 | 422 | for (const CXXMethodDecl *MD : RD->methods()) |
3242 | 2.10k | if (MicrosoftVTableContext::hasVtableSlot(MD) && |
3243 | 2.10k | !isa<CXXDestructorDecl>(MD)265 && !MD->isPure()184 ) |
3244 | 182 | Work.insert(MD); |
3245 | 733 | while (!Work.empty()) { |
3246 | 311 | const CXXMethodDecl *MD = *Work.begin(); |
3247 | 311 | auto MethodRange = MD->overridden_methods(); |
3248 | | // If a virtual method has no-overrides it lives in its parent's vtable. |
3249 | 311 | if (MethodRange.begin() == MethodRange.end()) |
3250 | 195 | BasesWithOverriddenMethods.insert(MD->getParent()); |
3251 | 116 | else |
3252 | 116 | Work.insert(MethodRange.begin(), MethodRange.end()); |
3253 | | // We've finished processing this element, remove it from the working set. |
3254 | 311 | Work.erase(MD); |
3255 | 311 | } |
3256 | | // For each of our virtual bases, check if it is in the set of overridden |
3257 | | // bases or if it transitively contains a non-virtual base that is. |
3258 | 660 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
3259 | 660 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
3260 | 660 | if (!HasVtordispSet.count(BaseDecl) && |
3261 | 660 | RequiresVtordisp(BasesWithOverriddenMethods, BaseDecl)628 ) |
3262 | 94 | HasVtordispSet.insert(BaseDecl); |
3263 | 660 | } |
3264 | 422 | } |
3265 | | |
3266 | | /// getASTRecordLayout - Get or compute information about the layout of the |
3267 | | /// specified record (struct/union/class), which indicates its size and field |
3268 | | /// position information. |
3269 | | const ASTRecordLayout & |
3270 | 5.72M | ASTContext::getASTRecordLayout(const RecordDecl *D) const { |
3271 | | // These asserts test different things. A record has a definition |
3272 | | // as soon as we begin to parse the definition. That definition is |
3273 | | // not a complete definition (which is what isDefinition() tests) |
3274 | | // until we *finish* parsing the definition. |
3275 | | |
3276 | 5.72M | if (D->hasExternalLexicalStorage() && !D->getDefinition()118k ) |
3277 | 0 | getExternalSource()->CompleteType(const_cast<RecordDecl*>(D)); |
3278 | | |
3279 | 5.72M | D = D->getDefinition(); |
3280 | 5.72M | assert(D && "Cannot get layout of forward declarations!"); |
3281 | 0 | assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!"); |
3282 | 0 | assert(D->isCompleteDefinition() && "Cannot layout type before complete!"); |
3283 | | |
3284 | | // Look up this layout, if already laid out, return what we have. |
3285 | | // Note that we can't save a reference to the entry because this function |
3286 | | // is recursive. |
3287 | 0 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
3288 | 5.72M | if (Entry) return *Entry5.31M ; |
3289 | | |
3290 | 416k | const ASTRecordLayout *NewEntry = nullptr; |
3291 | | |
3292 | 416k | if (isMsLayout(*this)) { |
3293 | 7.87k | MicrosoftRecordLayoutBuilder Builder(*this); |
3294 | 7.87k | if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
3295 | 7.52k | Builder.cxxLayout(RD); |
3296 | 7.52k | NewEntry = new (*this) ASTRecordLayout( |
3297 | 7.52k | *this, Builder.Size, Builder.Alignment, Builder.Alignment, |
3298 | 7.52k | Builder.Alignment, Builder.RequiredAlignment, Builder.HasOwnVFPtr, |
3299 | 7.52k | Builder.HasOwnVFPtr || Builder.PrimaryBase6.68k , Builder.VBPtrOffset, |
3300 | 7.52k | Builder.DataSize, Builder.FieldOffsets, Builder.NonVirtualSize, |
3301 | 7.52k | Builder.Alignment, Builder.Alignment, CharUnits::Zero(), |
3302 | 7.52k | Builder.PrimaryBase, false, Builder.SharedVBPtrBase, |
3303 | 7.52k | Builder.EndsWithZeroSizedObject, Builder.LeadsWithZeroSizedBase, |
3304 | 7.52k | Builder.Bases, Builder.VBases); |
3305 | 7.52k | } else { |
3306 | 348 | Builder.layout(D); |
3307 | 348 | NewEntry = new (*this) ASTRecordLayout( |
3308 | 348 | *this, Builder.Size, Builder.Alignment, Builder.Alignment, |
3309 | 348 | Builder.Alignment, Builder.RequiredAlignment, Builder.Size, |
3310 | 348 | Builder.FieldOffsets); |
3311 | 348 | } |
3312 | 408k | } else { |
3313 | 408k | if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
3314 | 294k | EmptySubobjectMap EmptySubobjects(*this, RD); |
3315 | 294k | ItaniumRecordLayoutBuilder Builder(*this, &EmptySubobjects); |
3316 | 294k | Builder.Layout(RD); |
3317 | | |
3318 | | // In certain situations, we are allowed to lay out objects in the |
3319 | | // tail-padding of base classes. This is ABI-dependent. |
3320 | | // FIXME: this should be stored in the record layout. |
3321 | 294k | bool skipTailPadding = |
3322 | 294k | mustSkipTailPadding(getTargetInfo().getCXXABI(), RD); |
3323 | | |
3324 | | // FIXME: This should be done in FinalizeLayout. |
3325 | 294k | CharUnits DataSize = |
3326 | 294k | skipTailPadding ? Builder.getSize()147k : Builder.getDataSize()147k ; |
3327 | 294k | CharUnits NonVirtualSize = |
3328 | 294k | skipTailPadding ? DataSize147k : Builder.NonVirtualSize147k ; |
3329 | 294k | NewEntry = new (*this) ASTRecordLayout( |
3330 | 294k | *this, Builder.getSize(), Builder.Alignment, |
3331 | 294k | Builder.PreferredAlignment, Builder.UnadjustedAlignment, |
3332 | | /*RequiredAlignment : used by MS-ABI)*/ |
3333 | 294k | Builder.Alignment, Builder.HasOwnVFPtr, RD->isDynamicClass(), |
3334 | 294k | CharUnits::fromQuantity(-1), DataSize, Builder.FieldOffsets, |
3335 | 294k | NonVirtualSize, Builder.NonVirtualAlignment, |
3336 | 294k | Builder.PreferredNVAlignment, |
3337 | 294k | EmptySubobjects.SizeOfLargestEmptySubobject, Builder.PrimaryBase, |
3338 | 294k | Builder.PrimaryBaseIsVirtual, nullptr, false, false, Builder.Bases, |
3339 | 294k | Builder.VBases); |
3340 | 294k | } else { |
3341 | 113k | ItaniumRecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr); |
3342 | 113k | Builder.Layout(D); |
3343 | | |
3344 | 113k | NewEntry = new (*this) ASTRecordLayout( |
3345 | 113k | *this, Builder.getSize(), Builder.Alignment, |
3346 | 113k | Builder.PreferredAlignment, Builder.UnadjustedAlignment, |
3347 | | /*RequiredAlignment : used by MS-ABI)*/ |
3348 | 113k | Builder.Alignment, Builder.getSize(), Builder.FieldOffsets); |
3349 | 113k | } |
3350 | 408k | } |
3351 | | |
3352 | 416k | ASTRecordLayouts[D] = NewEntry; |
3353 | | |
3354 | 416k | if (getLangOpts().DumpRecordLayouts) { |
3355 | 1.36k | llvm::outs() << "\n*** Dumping AST Record Layout\n"; |
3356 | 1.36k | DumpRecordLayout(D, llvm::outs(), getLangOpts().DumpRecordLayoutsSimple); |
3357 | 1.36k | } |
3358 | | |
3359 | 416k | return *NewEntry; |
3360 | 5.72M | } |
3361 | | |
3362 | 170k | const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) { |
3363 | 170k | if (!getTargetInfo().getCXXABI().hasKeyFunctions()) |
3364 | 3.38k | return nullptr; |
3365 | | |
3366 | 167k | assert(RD->getDefinition() && "Cannot get key function for forward decl!"); |
3367 | 0 | RD = RD->getDefinition(); |
3368 | | |
3369 | | // Beware: |
3370 | | // 1) computing the key function might trigger deserialization, which might |
3371 | | // invalidate iterators into KeyFunctions |
3372 | | // 2) 'get' on the LazyDeclPtr might also trigger deserialization and |
3373 | | // invalidate the LazyDeclPtr within the map itself |
3374 | 167k | LazyDeclPtr Entry = KeyFunctions[RD]; |
3375 | 167k | const Decl *Result = |
3376 | 167k | Entry ? Entry.get(getExternalSource())21.6k : computeKeyFunction(*this, RD)145k ; |
3377 | | |
3378 | | // Store it back if it changed. |
3379 | 167k | if (Entry.isOffset() || Entry.isValid() != bool(Result)) |
3380 | 14.6k | KeyFunctions[RD] = const_cast<Decl*>(Result); |
3381 | | |
3382 | 167k | return cast_or_null<CXXMethodDecl>(Result); |
3383 | 170k | } |
3384 | | |
3385 | 14 | void ASTContext::setNonKeyFunction(const CXXMethodDecl *Method) { |
3386 | 14 | assert(Method == Method->getFirstDecl() && |
3387 | 14 | "not working with method declaration from class definition"); |
3388 | | |
3389 | | // Look up the cache entry. Since we're working with the first |
3390 | | // declaration, its parent must be the class definition, which is |
3391 | | // the correct key for the KeyFunctions hash. |
3392 | 0 | const auto &Map = KeyFunctions; |
3393 | 14 | auto I = Map.find(Method->getParent()); |
3394 | | |
3395 | | // If it's not cached, there's nothing to do. |
3396 | 14 | if (I == Map.end()) return0 ; |
3397 | | |
3398 | | // If it is cached, check whether it's the target method, and if so, |
3399 | | // remove it from the cache. Note, the call to 'get' might invalidate |
3400 | | // the iterator and the LazyDeclPtr object within the map. |
3401 | 14 | LazyDeclPtr Ptr = I->second; |
3402 | 14 | if (Ptr.get(getExternalSource()) == Method) { |
3403 | | // FIXME: remember that we did this for module / chained PCH state? |
3404 | 14 | KeyFunctions.erase(Method->getParent()); |
3405 | 14 | } |
3406 | 14 | } |
3407 | | |
3408 | 604 | static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) { |
3409 | 604 | const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent()); |
3410 | 604 | return Layout.getFieldOffset(FD->getFieldIndex()); |
3411 | 604 | } |
3412 | | |
3413 | 564 | uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const { |
3414 | 564 | uint64_t OffsetInBits; |
3415 | 564 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
3416 | 538 | OffsetInBits = ::getFieldOffset(*this, FD); |
3417 | 538 | } else { |
3418 | 26 | const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD); |
3419 | | |
3420 | 26 | OffsetInBits = 0; |
3421 | 26 | for (const NamedDecl *ND : IFD->chain()) |
3422 | 66 | OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(ND)); |
3423 | 26 | } |
3424 | | |
3425 | 564 | return OffsetInBits; |
3426 | 564 | } |
3427 | | |
3428 | | uint64_t ASTContext::lookupFieldBitOffset(const ObjCInterfaceDecl *OID, |
3429 | | const ObjCImplementationDecl *ID, |
3430 | 3.35k | const ObjCIvarDecl *Ivar) const { |
3431 | 3.35k | Ivar = Ivar->getCanonicalDecl(); |
3432 | 3.35k | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
3433 | | |
3434 | | // FIXME: We should eliminate the need to have ObjCImplementationDecl passed |
3435 | | // in here; it should never be necessary because that should be the lexical |
3436 | | // decl context for the ivar. |
3437 | | |
3438 | | // If we know have an implementation (and the ivar is in it) then |
3439 | | // look up in the implementation layout. |
3440 | 3.35k | const ASTRecordLayout *RL; |
3441 | 3.35k | if (ID && declaresSameEntity(ID->getClassInterface(), Container)2.51k ) |
3442 | 2.45k | RL = &getASTObjCImplementationLayout(ID); |
3443 | 900 | else |
3444 | 900 | RL = &getASTObjCInterfaceLayout(Container); |
3445 | | |
3446 | | // Compute field index. |
3447 | | // |
3448 | | // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is |
3449 | | // implemented. This should be fixed to get the information from the layout |
3450 | | // directly. |
3451 | 3.35k | unsigned Index = 0; |
3452 | | |
3453 | 3.35k | for (const ObjCIvarDecl *IVD = Container->all_declared_ivar_begin(); |
3454 | 8.63k | IVD; IVD = IVD->getNextIvar()5.27k ) { |
3455 | 8.63k | if (Ivar == IVD) |
3456 | 3.35k | break; |
3457 | 5.27k | ++Index; |
3458 | 5.27k | } |
3459 | 3.35k | assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!"); |
3460 | | |
3461 | 0 | return RL->getFieldOffset(Index); |
3462 | 3.35k | } |
3463 | | |
3464 | | /// getObjCLayout - Get or compute information about the layout of the |
3465 | | /// given interface. |
3466 | | /// |
3467 | | /// \param Impl - If given, also include the layout of the interface's |
3468 | | /// implementation. This may differ by including synthesized ivars. |
3469 | | const ASTRecordLayout & |
3470 | | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
3471 | 18.8k | const ObjCImplementationDecl *Impl) const { |
3472 | | // Retrieve the definition |
3473 | 18.8k | if (D->hasExternalLexicalStorage() && !D->getDefinition()126 ) |
3474 | 0 | getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D)); |
3475 | 18.8k | D = D->getDefinition(); |
3476 | 18.8k | assert(D && !D->isInvalidDecl() && D->isThisDeclarationADefinition() && |
3477 | 18.8k | "Invalid interface decl!"); |
3478 | | |
3479 | | // Look up this layout, if already laid out, return what we have. |
3480 | 0 | const ObjCContainerDecl *Key = |
3481 | 18.8k | Impl ? (const ObjCContainerDecl*) Impl3.58k : (const ObjCContainerDecl*) D15.2k ; |
3482 | 18.8k | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
3483 | 11.4k | return *Entry; |
3484 | | |
3485 | | // Add in synthesized ivar count if laying out an implementation. |
3486 | 7.41k | if (Impl) { |
3487 | 2.56k | unsigned SynthCount = CountNonClassIvars(D); |
3488 | | // If there aren't any synthesized ivars then reuse the interface |
3489 | | // entry. Note we can't cache this because we simply free all |
3490 | | // entries later; however we shouldn't look up implementations |
3491 | | // frequently. |
3492 | 2.56k | if (SynthCount == 0) |
3493 | 2.32k | return getObjCLayout(D, nullptr); |
3494 | 2.56k | } |
3495 | | |
3496 | 5.09k | ItaniumRecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr); |
3497 | 5.09k | Builder.Layout(D); |
3498 | | |
3499 | 5.09k | const ASTRecordLayout *NewEntry = new (*this) ASTRecordLayout( |
3500 | 5.09k | *this, Builder.getSize(), Builder.Alignment, Builder.PreferredAlignment, |
3501 | 5.09k | Builder.UnadjustedAlignment, |
3502 | | /*RequiredAlignment : used by MS-ABI)*/ |
3503 | 5.09k | Builder.Alignment, Builder.getDataSize(), Builder.FieldOffsets); |
3504 | | |
3505 | 5.09k | ObjCLayouts[Key] = NewEntry; |
3506 | | |
3507 | 5.09k | return *NewEntry; |
3508 | 7.41k | } |
3509 | | |
3510 | | static void PrintOffset(raw_ostream &OS, |
3511 | 5.54k | CharUnits Offset, unsigned IndentLevel) { |
3512 | 5.54k | OS << llvm::format("%10" PRId64 " | ", (int64_t)Offset.getQuantity()); |
3513 | 5.54k | OS.indent(IndentLevel * 2); |
3514 | 5.54k | } |
3515 | | |
3516 | | static void PrintBitFieldOffset(raw_ostream &OS, CharUnits Offset, |
3517 | | unsigned Begin, unsigned Width, |
3518 | 552 | unsigned IndentLevel) { |
3519 | 552 | llvm::SmallString<10> Buffer; |
3520 | 552 | { |
3521 | 552 | llvm::raw_svector_ostream BufferOS(Buffer); |
3522 | 552 | BufferOS << Offset.getQuantity() << ':'; |
3523 | 552 | if (Width == 0) { |
3524 | 108 | BufferOS << '-'; |
3525 | 444 | } else { |
3526 | 444 | BufferOS << Begin << '-' << (Begin + Width - 1); |
3527 | 444 | } |
3528 | 552 | } |
3529 | | |
3530 | 552 | OS << llvm::right_justify(Buffer, 10) << " | "; |
3531 | 552 | OS.indent(IndentLevel * 2); |
3532 | 552 | } |
3533 | | |
3534 | 2.40k | static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) { |
3535 | 2.40k | OS << " | "; |
3536 | 2.40k | OS.indent(IndentLevel * 2); |
3537 | 2.40k | } |
3538 | | |
3539 | | static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD, |
3540 | | const ASTContext &C, |
3541 | | CharUnits Offset, |
3542 | | unsigned IndentLevel, |
3543 | | const char* Description, |
3544 | | bool PrintSizeInfo, |
3545 | 2.86k | bool IncludeVirtualBases) { |
3546 | 2.86k | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
3547 | 2.86k | auto CXXRD = dyn_cast<CXXRecordDecl>(RD); |
3548 | | |
3549 | 2.86k | PrintOffset(OS, Offset, IndentLevel); |
3550 | 2.86k | OS << C.getTypeDeclType(const_cast<RecordDecl *>(RD)); |
3551 | 2.86k | if (Description) |
3552 | 1.58k | OS << ' ' << Description; |
3553 | 2.86k | if (CXXRD && CXXRD->isEmpty()2.70k ) |
3554 | 817 | OS << " (empty)"; |
3555 | 2.86k | OS << '\n'; |
3556 | | |
3557 | 2.86k | IndentLevel++; |
3558 | | |
3559 | | // Dump bases. |
3560 | 2.86k | if (CXXRD) { |
3561 | 2.70k | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
3562 | 2.70k | bool HasOwnVFPtr = Layout.hasOwnVFPtr(); |
3563 | 2.70k | bool HasOwnVBPtr = Layout.hasOwnVBPtr(); |
3564 | | |
3565 | | // Vtable pointer. |
3566 | 2.70k | if (CXXRD->isDynamicClass() && !PrimaryBase793 && !isMsLayout(C)736 ) { |
3567 | 17 | PrintOffset(OS, Offset, IndentLevel); |
3568 | 17 | OS << '(' << *RD << " vtable pointer)\n"; |
3569 | 2.68k | } else if (HasOwnVFPtr) { |
3570 | 345 | PrintOffset(OS, Offset, IndentLevel); |
3571 | | // vfptr (for Microsoft C++ ABI) |
3572 | 345 | OS << '(' << *RD << " vftable pointer)\n"; |
3573 | 345 | } |
3574 | | |
3575 | | // Collect nvbases. |
3576 | 2.70k | SmallVector<const CXXRecordDecl *, 4> Bases; |
3577 | 2.70k | for (const CXXBaseSpecifier &Base : CXXRD->bases()) { |
3578 | 1.40k | assert(!Base.getType()->isDependentType() && |
3579 | 1.40k | "Cannot layout class with dependent bases."); |
3580 | 1.40k | if (!Base.isVirtual()) |
3581 | 773 | Bases.push_back(Base.getType()->getAsCXXRecordDecl()); |
3582 | 1.40k | } |
3583 | | |
3584 | | // Sort nvbases by offset. |
3585 | 2.70k | llvm::stable_sort( |
3586 | 2.70k | Bases, [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { |
3587 | 312 | return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R); |
3588 | 312 | }); |
3589 | | |
3590 | | // Dump (non-virtual) bases |
3591 | 2.70k | for (const CXXRecordDecl *Base : Bases) { |
3592 | 773 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
3593 | 773 | DumpRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
3594 | 773 | Base == PrimaryBase ? "(primary base)"57 : "(base)"716 , |
3595 | 773 | /*PrintSizeInfo=*/false, |
3596 | 773 | /*IncludeVirtualBases=*/false); |
3597 | 773 | } |
3598 | | |
3599 | | // vbptr (for Microsoft C++ ABI) |
3600 | 2.70k | if (HasOwnVBPtr) { |
3601 | 406 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
3602 | 406 | OS << '(' << *RD << " vbtable pointer)\n"; |
3603 | 406 | } |
3604 | 2.70k | } |
3605 | | |
3606 | | // Dump fields. |
3607 | 2.86k | uint64_t FieldNo = 0; |
3608 | 2.86k | for (RecordDecl::field_iterator I = RD->field_begin(), |
3609 | 5.43k | E = RD->field_end(); I != E; ++I, ++FieldNo2.57k ) { |
3610 | 2.57k | const FieldDecl &Field = **I; |
3611 | 2.57k | uint64_t LocalFieldOffsetInBits = Layout.getFieldOffset(FieldNo); |
3612 | 2.57k | CharUnits FieldOffset = |
3613 | 2.57k | Offset + C.toCharUnitsFromBits(LocalFieldOffsetInBits); |
3614 | | |
3615 | | // Recursively dump fields of record type. |
3616 | 2.57k | if (auto RT = Field.getType()->getAs<RecordType>()) { |
3617 | 189 | DumpRecordLayout(OS, RT->getDecl(), C, FieldOffset, IndentLevel, |
3618 | 189 | Field.getName().data(), |
3619 | 189 | /*PrintSizeInfo=*/false, |
3620 | 189 | /*IncludeVirtualBases=*/true); |
3621 | 189 | continue; |
3622 | 189 | } |
3623 | | |
3624 | 2.38k | if (Field.isBitField()) { |
3625 | 552 | uint64_t LocalFieldByteOffsetInBits = C.toBits(FieldOffset - Offset); |
3626 | 552 | unsigned Begin = LocalFieldOffsetInBits - LocalFieldByteOffsetInBits; |
3627 | 552 | unsigned Width = Field.getBitWidthValue(C); |
3628 | 552 | PrintBitFieldOffset(OS, FieldOffset, Begin, Width, IndentLevel); |
3629 | 1.83k | } else { |
3630 | 1.83k | PrintOffset(OS, FieldOffset, IndentLevel); |
3631 | 1.83k | } |
3632 | 2.38k | const QualType &FieldType = C.getLangOpts().DumpRecordLayoutsCanonical |
3633 | 2.38k | ? Field.getType().getCanonicalType()2 |
3634 | 2.38k | : Field.getType()2.38k ; |
3635 | 2.38k | OS << FieldType << ' ' << Field << '\n'; |
3636 | 2.38k | } |
3637 | | |
3638 | | // Dump virtual bases. |
3639 | 2.86k | if (CXXRD && IncludeVirtualBases2.70k ) { |
3640 | 1.31k | const ASTRecordLayout::VBaseOffsetsMapTy &VtorDisps = |
3641 | 1.31k | Layout.getVBaseOffsetsMap(); |
3642 | | |
3643 | 1.31k | for (const CXXBaseSpecifier &Base : CXXRD->vbases()) { |
3644 | 620 | assert(Base.isVirtual() && "Found non-virtual class!"); |
3645 | 0 | const CXXRecordDecl *VBase = Base.getType()->getAsCXXRecordDecl(); |
3646 | | |
3647 | 620 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
3648 | | |
3649 | 620 | if (VtorDisps.find(VBase)->second.hasVtorDisp()) { |
3650 | 74 | PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel); |
3651 | 74 | OS << "(vtordisp for vbase " << *VBase << ")\n"; |
3652 | 74 | } |
3653 | | |
3654 | 620 | DumpRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
3655 | 620 | VBase == Layout.getPrimaryBase() ? |
3656 | 620 | "(primary virtual base)"0 : "(virtual base)", |
3657 | 620 | /*PrintSizeInfo=*/false, |
3658 | 620 | /*IncludeVirtualBases=*/false); |
3659 | 620 | } |
3660 | 1.31k | } |
3661 | | |
3662 | 2.86k | if (!PrintSizeInfo) return1.58k ; |
3663 | | |
3664 | 1.28k | PrintIndentNoOffset(OS, IndentLevel - 1); |
3665 | 1.28k | OS << "[sizeof=" << Layout.getSize().getQuantity(); |
3666 | 1.28k | if (CXXRD && !isMsLayout(C)1.12k ) |
3667 | 256 | OS << ", dsize=" << Layout.getDataSize().getQuantity(); |
3668 | 1.28k | OS << ", align=" << Layout.getAlignment().getQuantity(); |
3669 | 1.28k | if (C.getTargetInfo().defaultsToAIXPowerAlignment()) |
3670 | 252 | OS << ", preferredalign=" << Layout.getPreferredAlignment().getQuantity(); |
3671 | | |
3672 | 1.28k | if (CXXRD) { |
3673 | 1.12k | OS << ",\n"; |
3674 | 1.12k | PrintIndentNoOffset(OS, IndentLevel - 1); |
3675 | 1.12k | OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity(); |
3676 | 1.12k | OS << ", nvalign=" << Layout.getNonVirtualAlignment().getQuantity(); |
3677 | 1.12k | if (C.getTargetInfo().defaultsToAIXPowerAlignment()) |
3678 | 204 | OS << ", preferrednvalign=" |
3679 | 204 | << Layout.getPreferredNVAlignment().getQuantity(); |
3680 | 1.12k | } |
3681 | 1.28k | OS << "]\n"; |
3682 | 1.28k | } |
3683 | | |
3684 | | void ASTContext::DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS, |
3685 | 1.36k | bool Simple) const { |
3686 | 1.36k | if (!Simple) { |
3687 | 1.28k | ::DumpRecordLayout(OS, RD, *this, CharUnits(), 0, nullptr, |
3688 | 1.28k | /*PrintSizeInfo*/ true, |
3689 | 1.28k | /*IncludeVirtualBases=*/true); |
3690 | 1.28k | return; |
3691 | 1.28k | } |
3692 | | |
3693 | | // The "simple" format is designed to be parsed by the |
3694 | | // layout-override testing code. There shouldn't be any external |
3695 | | // uses of this format --- when LLDB overrides a layout, it sets up |
3696 | | // the data structures directly --- so feel free to adjust this as |
3697 | | // you like as long as you also update the rudimentary parser for it |
3698 | | // in libFrontend. |
3699 | | |
3700 | 83 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
3701 | 83 | OS << "Type: " << getTypeDeclType(RD) << "\n"; |
3702 | 83 | OS << "\nLayout: "; |
3703 | 83 | OS << "<ASTRecordLayout\n"; |
3704 | 83 | OS << " Size:" << toBits(Info.getSize()) << "\n"; |
3705 | 83 | if (!isMsLayout(*this)) |
3706 | 72 | OS << " DataSize:" << toBits(Info.getDataSize()) << "\n"; |
3707 | 83 | OS << " Alignment:" << toBits(Info.getAlignment()) << "\n"; |
3708 | 83 | if (Target->defaultsToAIXPowerAlignment()) |
3709 | 0 | OS << " PreferredAlignment:" << toBits(Info.getPreferredAlignment()) |
3710 | 0 | << "\n"; |
3711 | 83 | OS << " FieldOffsets: ["; |
3712 | 270 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i187 ) { |
3713 | 187 | if (i) |
3714 | 111 | OS << ", "; |
3715 | 187 | OS << Info.getFieldOffset(i); |
3716 | 187 | } |
3717 | 83 | OS << "]>\n"; |
3718 | 83 | } |