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