clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name RecordLayoutBuilder.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include/clang/AST -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../include -I /usr/src/gnu/usr.bin/clang/libclangAST/obj -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/lib/AST/RecordLayoutBuilder.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
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 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | struct BaseSubobjectInfo { |
40 | |
41 | const CXXRecordDecl *Class; |
42 | |
43 | |
44 | bool IsVirtual; |
45 | |
46 | |
47 | SmallVector<BaseSubobjectInfo*, 4> Bases; |
48 | |
49 | |
50 | |
51 | BaseSubobjectInfo *PrimaryVirtualBaseInfo; |
52 | |
53 | |
54 | const BaseSubobjectInfo *Derived; |
55 | }; |
56 | |
57 | |
58 | |
59 | |
60 | struct ExternalLayout { |
61 | ExternalLayout() : Size(0), Align(0) {} |
62 | |
63 | |
64 | uint64_t Size; |
65 | |
66 | |
67 | uint64_t Align; |
68 | |
69 | |
70 | llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsets; |
71 | |
72 | |
73 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsets; |
74 | |
75 | |
76 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> VirtualBaseOffsets; |
77 | |
78 | |
79 | |
80 | uint64_t getExternalFieldOffset(const FieldDecl *FD) { |
81 | assert(FieldOffsets.count(FD) && |
82 | "Field does not have an external offset"); |
83 | return FieldOffsets[FD]; |
84 | } |
85 | |
86 | bool getExternalNVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) { |
87 | auto Known = BaseOffsets.find(RD); |
88 | if (Known == BaseOffsets.end()) |
89 | return false; |
90 | BaseOffset = Known->second; |
91 | return true; |
92 | } |
93 | |
94 | bool getExternalVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) { |
95 | auto Known = VirtualBaseOffsets.find(RD); |
96 | if (Known == VirtualBaseOffsets.end()) |
97 | return false; |
98 | BaseOffset = Known->second; |
99 | return true; |
100 | } |
101 | }; |
102 | |
103 | |
104 | |
105 | class EmptySubobjectMap { |
106 | const ASTContext &Context; |
107 | uint64_t CharWidth; |
108 | |
109 | |
110 | const CXXRecordDecl *Class; |
111 | |
112 | |
113 | typedef llvm::TinyPtrVector<const CXXRecordDecl *> ClassVectorTy; |
114 | typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy; |
115 | EmptyClassOffsetsMapTy EmptyClassOffsets; |
116 | |
117 | |
118 | |
119 | CharUnits MaxEmptyClassOffset; |
120 | |
121 | |
122 | |
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 | |
137 | |
138 | bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const { |
139 | return Offset <= MaxEmptyClassOffset; |
140 | } |
141 | |
142 | CharUnits |
143 | getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const { |
144 | uint64_t FieldOffset = Layout.getFieldOffset(FieldNo); |
145 | assert(FieldOffset % CharWidth == 0 && |
146 | "Field offset not at char boundary!"); |
147 | |
148 | return Context.toCharUnitsFromBits(FieldOffset); |
149 | } |
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 | |
166 | |
167 | |
168 | CharUnits SizeOfLargestEmptySubobject; |
169 | |
170 | EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class) |
171 | : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) { |
172 | ComputeEmptySubobjectSizes(); |
173 | } |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
180 | CharUnits Offset); |
181 | |
182 | |
183 | |
184 | bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset); |
185 | }; |
186 | |
187 | void EmptySubobjectMap::ComputeEmptySubobjectSizes() { |
188 | |
189 | for (const CXXBaseSpecifier &Base : Class->bases()) { |
190 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
191 | |
192 | CharUnits EmptySize; |
193 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
194 | if (BaseDecl->isEmpty()) { |
195 | |
196 | EmptySize = Layout.getSize(); |
197 | } else { |
198 | |
199 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
200 | } |
201 | |
202 | if (EmptySize > SizeOfLargestEmptySubobject) |
203 | SizeOfLargestEmptySubobject = EmptySize; |
204 | } |
205 | |
206 | |
207 | for (const FieldDecl *FD : Class->fields()) { |
208 | const RecordType *RT = |
209 | Context.getBaseElementType(FD->getType())->getAs<RecordType>(); |
210 | |
211 | |
212 | if (!RT) |
213 | continue; |
214 | |
215 | CharUnits EmptySize; |
216 | const CXXRecordDecl *MemberDecl = RT->getAsCXXRecordDecl(); |
217 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl); |
218 | if (MemberDecl->isEmpty()) { |
219 | |
220 | EmptySize = Layout.getSize(); |
221 | } else { |
222 | |
223 | EmptySize = Layout.getSizeOfLargestEmptySubobject(); |
224 | } |
225 | |
226 | if (EmptySize > SizeOfLargestEmptySubobject) |
227 | SizeOfLargestEmptySubobject = EmptySize; |
228 | } |
229 | } |
230 | |
231 | bool |
232 | EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD, |
233 | CharUnits Offset) const { |
234 | |
235 | if (!RD->isEmpty()) |
236 | return true; |
237 | |
238 | EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset); |
239 | if (I == EmptyClassOffsets.end()) |
240 | return true; |
241 | |
242 | const ClassVectorTy &Classes = I->second; |
243 | if (llvm::find(Classes, RD) == Classes.end()) |
244 | return true; |
245 | |
246 | |
247 | return false; |
248 | } |
249 | |
250 | void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD, |
251 | CharUnits Offset) { |
252 | |
253 | if (!RD->isEmpty()) |
254 | return; |
255 | |
256 | |
257 | |
258 | ClassVectorTy &Classes = EmptyClassOffsets[Offset]; |
259 | if (llvm::is_contained(Classes, RD)) |
260 | return; |
261 | |
262 | Classes.push_back(RD); |
263 | |
264 | |
265 | if (Offset > MaxEmptyClassOffset) |
266 | MaxEmptyClassOffset = Offset; |
267 | } |
268 | |
269 | bool |
270 | EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info, |
271 | CharUnits Offset) { |
272 | |
273 | |
274 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
275 | return true; |
276 | |
277 | if (!CanPlaceSubobjectAtOffset(Info->Class, Offset)) |
278 | return false; |
279 | |
280 | |
281 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
282 | for (const BaseSubobjectInfo *Base : Info->Bases) { |
283 | if (Base->IsVirtual) |
284 | continue; |
285 | |
286 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
287 | |
288 | if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset)) |
289 | return false; |
290 | } |
291 | |
292 | if (Info->PrimaryVirtualBaseInfo) { |
293 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
294 | |
295 | if (Info == PrimaryVirtualBaseInfo->Derived) { |
296 | if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset)) |
297 | return false; |
298 | } |
299 | } |
300 | |
301 | |
302 | unsigned FieldNo = 0; |
303 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
304 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
305 | if (I->isBitField()) |
306 | continue; |
307 | |
308 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
309 | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
310 | return false; |
311 | } |
312 | |
313 | return true; |
314 | } |
315 | |
316 | void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info, |
317 | CharUnits Offset, |
318 | bool PlacingEmptyBase) { |
319 | if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) { |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | return; |
326 | } |
327 | |
328 | AddSubobjectAtOffset(Info->Class, Offset); |
329 | |
330 | |
331 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
332 | for (const BaseSubobjectInfo *Base : Info->Bases) { |
333 | if (Base->IsVirtual) |
334 | continue; |
335 | |
336 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
337 | UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase); |
338 | } |
339 | |
340 | if (Info->PrimaryVirtualBaseInfo) { |
341 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo; |
342 | |
343 | if (Info == PrimaryVirtualBaseInfo->Derived) |
344 | UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset, |
345 | PlacingEmptyBase); |
346 | } |
347 | |
348 | |
349 | unsigned FieldNo = 0; |
350 | for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(), |
351 | E = Info->Class->field_end(); I != E; ++I, ++FieldNo) { |
352 | if (I->isBitField()) |
353 | continue; |
354 | |
355 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
356 | UpdateEmptyFieldSubobjects(*I, FieldOffset, PlacingEmptyBase); |
357 | } |
358 | } |
359 | |
360 | bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info, |
361 | CharUnits Offset) { |
362 | |
363 | |
364 | if (SizeOfLargestEmptySubobject.isZero()) |
365 | return true; |
366 | |
367 | if (!CanPlaceBaseSubobjectAtOffset(Info, Offset)) |
368 | return false; |
369 | |
370 | |
371 | |
372 | UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty()); |
373 | return true; |
374 | } |
375 | |
376 | bool |
377 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD, |
378 | const CXXRecordDecl *Class, |
379 | CharUnits Offset) const { |
380 | |
381 | |
382 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
383 | return true; |
384 | |
385 | if (!CanPlaceSubobjectAtOffset(RD, Offset)) |
386 | return false; |
387 | |
388 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
389 | |
390 | |
391 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
392 | if (Base.isVirtual()) |
393 | continue; |
394 | |
395 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
396 | |
397 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
398 | if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset)) |
399 | return false; |
400 | } |
401 | |
402 | if (RD == Class) { |
403 | |
404 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
405 | const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl(); |
406 | |
407 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
408 | if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset)) |
409 | return false; |
410 | } |
411 | } |
412 | |
413 | |
414 | unsigned FieldNo = 0; |
415 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
416 | I != E; ++I, ++FieldNo) { |
417 | if (I->isBitField()) |
418 | continue; |
419 | |
420 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
421 | |
422 | if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset)) |
423 | return false; |
424 | } |
425 | |
426 | return true; |
427 | } |
428 | |
429 | bool |
430 | EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD, |
431 | CharUnits Offset) const { |
432 | |
433 | |
434 | if (!AnyEmptySubobjectsBeyondOffset(Offset)) |
435 | return true; |
436 | |
437 | QualType T = FD->getType(); |
438 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
439 | return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset); |
440 | |
441 | |
442 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
443 | QualType ElemTy = Context.getBaseElementType(AT); |
444 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
445 | if (!RT) |
446 | return true; |
447 | |
448 | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
449 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
450 | |
451 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
452 | CharUnits ElementOffset = Offset; |
453 | for (uint64_t I = 0; I != NumElements; ++I) { |
454 | |
455 | |
456 | if (!AnyEmptySubobjectsBeyondOffset(ElementOffset)) |
457 | return true; |
458 | |
459 | if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset)) |
460 | return false; |
461 | |
462 | ElementOffset += Layout.getSize(); |
463 | } |
464 | } |
465 | |
466 | return true; |
467 | } |
468 | |
469 | bool |
470 | EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD, |
471 | CharUnits Offset) { |
472 | if (!CanPlaceFieldSubobjectAtOffset(FD, Offset)) |
473 | return false; |
474 | |
475 | |
476 | |
477 | UpdateEmptyFieldSubobjects(FD, Offset, FD->hasAttr<NoUniqueAddressAttr>()); |
478 | return true; |
479 | } |
480 | |
481 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects( |
482 | const CXXRecordDecl *RD, const CXXRecordDecl *Class, CharUnits Offset, |
483 | bool PlacingOverlappingField) { |
484 | |
485 | |
486 | |
487 | |
488 | |
489 | |
490 | |
491 | |
492 | |
493 | |
494 | if (!PlacingOverlappingField && Offset >= SizeOfLargestEmptySubobject) |
495 | return; |
496 | |
497 | AddSubobjectAtOffset(RD, Offset); |
498 | |
499 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
500 | |
501 | |
502 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
503 | if (Base.isVirtual()) |
504 | continue; |
505 | |
506 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
507 | |
508 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl); |
509 | UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset, |
510 | PlacingOverlappingField); |
511 | } |
512 | |
513 | if (RD == Class) { |
514 | |
515 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
516 | const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl(); |
517 | |
518 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl); |
519 | UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset, |
520 | PlacingOverlappingField); |
521 | } |
522 | } |
523 | |
524 | |
525 | unsigned FieldNo = 0; |
526 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
527 | I != E; ++I, ++FieldNo) { |
528 | if (I->isBitField()) |
529 | continue; |
530 | |
531 | CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo); |
532 | |
533 | UpdateEmptyFieldSubobjects(*I, FieldOffset, PlacingOverlappingField); |
534 | } |
535 | } |
536 | |
537 | void EmptySubobjectMap::UpdateEmptyFieldSubobjects( |
538 | const FieldDecl *FD, CharUnits Offset, bool PlacingOverlappingField) { |
539 | QualType T = FD->getType(); |
540 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
541 | UpdateEmptyFieldSubobjects(RD, RD, Offset, PlacingOverlappingField); |
542 | return; |
543 | } |
544 | |
545 | |
546 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { |
547 | QualType ElemTy = Context.getBaseElementType(AT); |
548 | const RecordType *RT = ElemTy->getAs<RecordType>(); |
549 | if (!RT) |
550 | return; |
551 | |
552 | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
553 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
554 | |
555 | uint64_t NumElements = Context.getConstantArrayElementCount(AT); |
556 | CharUnits ElementOffset = Offset; |
557 | |
558 | for (uint64_t I = 0; I != NumElements; ++I) { |
559 | |
560 | |
561 | |
562 | |
563 | |
564 | if (!PlacingOverlappingField && |
565 | ElementOffset >= SizeOfLargestEmptySubobject) |
566 | return; |
567 | |
568 | UpdateEmptyFieldSubobjects(RD, RD, ElementOffset, |
569 | PlacingOverlappingField); |
570 | ElementOffset += Layout.getSize(); |
571 | } |
572 | } |
573 | } |
574 | |
575 | typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy; |
576 | |
577 | class ItaniumRecordLayoutBuilder { |
578 | protected: |
579 | |
580 | friend class clang::ASTContext; |
581 | |
582 | const ASTContext &Context; |
583 | |
584 | EmptySubobjectMap *EmptySubobjects; |
585 | |
586 | |
587 | uint64_t Size; |
588 | |
589 | |
590 | CharUnits Alignment; |
591 | |
592 | |
593 | CharUnits PreferredAlignment; |
594 | |
595 | |
596 | CharUnits UnpackedAlignment; |
597 | |
598 | |
599 | CharUnits UnadjustedAlignment; |
600 | |
601 | SmallVector<uint64_t, 16> FieldOffsets; |
602 | |
603 | |
604 | |
605 | unsigned UseExternalLayout : 1; |
606 | |
607 | |
608 | |
609 | unsigned InferAlignment : 1; |
610 | |
611 | |
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 | |
623 | |
624 | |
625 | |
626 | unsigned char UnfilledBitsInLastUnit; |
627 | |
628 | |
629 | |
630 | unsigned char LastBitfieldStorageUnitSize; |
631 | |
632 | |
633 | |
634 | CharUnits MaxFieldAlignment; |
635 | |
636 | |
637 | uint64_t DataSize; |
638 | |
639 | CharUnits NonVirtualSize; |
640 | CharUnits NonVirtualAlignment; |
641 | CharUnits PreferredNVAlignment; |
642 | |
643 | |
644 | |
645 | CharUnits PaddedFieldSize; |
646 | |
647 | |
648 | |
649 | const CXXRecordDecl *PrimaryBase; |
650 | |
651 | |
652 | |
653 | bool PrimaryBaseIsVirtual; |
654 | |
655 | |
656 | |
657 | bool HasOwnVFPtr; |
658 | |
659 | |
660 | bool HasPackedField; |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | bool HandledFirstNonOverlappingEmptyField; |
667 | |
668 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
669 | |
670 | |
671 | BaseOffsetsMapTy Bases; |
672 | |
673 | |
674 | ASTRecordLayout::VBaseOffsetsMapTy VBases; |
675 | |
676 | |
677 | |
678 | CXXIndirectPrimaryBaseSet IndirectPrimaryBases; |
679 | |
680 | |
681 | |
682 | const CXXRecordDecl *FirstNearlyEmptyVBase; |
683 | |
684 | |
685 | |
686 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
687 | |
688 | |
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 | 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 | TargetCXXABI getCXXABI() const { |
721 | return Context.getTargetInfo().getCXXABI(); |
722 | } |
723 | |
724 | |
725 | llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator; |
726 | |
727 | typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *> |
728 | BaseSubobjectInfoMapTy; |
729 | |
730 | |
731 | |
732 | BaseSubobjectInfoMapTy VirtualBaseInfo; |
733 | |
734 | |
735 | |
736 | BaseSubobjectInfoMapTy NonVirtualBaseInfo; |
737 | |
738 | |
739 | |
740 | void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD); |
741 | |
742 | |
743 | |
744 | BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD, |
745 | bool IsVirtual, |
746 | BaseSubobjectInfo *Derived); |
747 | |
748 | |
749 | void DeterminePrimaryBase(const CXXRecordDecl *RD); |
750 | |
751 | void SelectPrimaryVBase(const CXXRecordDecl *RD); |
752 | |
753 | void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign); |
754 | |
755 | |
756 | |
757 | void LayoutNonVirtualBases(const CXXRecordDecl *RD); |
758 | |
759 | |
760 | void LayoutNonVirtualBase(const BaseSubobjectInfo *Base); |
761 | |
762 | void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info, |
763 | CharUnits Offset); |
764 | |
765 | |
766 | void LayoutVirtualBases(const CXXRecordDecl *RD, |
767 | const CXXRecordDecl *MostDerivedClass); |
768 | |
769 | |
770 | void LayoutVirtualBase(const BaseSubobjectInfo *Base); |
771 | |
772 | |
773 | |
774 | CharUnits LayoutBase(const BaseSubobjectInfo *Base); |
775 | |
776 | |
777 | void InitializeLayout(const Decl *D); |
778 | |
779 | |
780 | |
781 | void FinishLayout(const NamedDecl *D); |
782 | |
783 | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment, |
784 | CharUnits PreferredAlignment); |
785 | void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment) { |
786 | UpdateAlignment(NewAlignment, UnpackedNewAlignment, NewAlignment); |
787 | } |
788 | void UpdateAlignment(CharUnits NewAlignment) { |
789 | UpdateAlignment(NewAlignment, NewAlignment, NewAlignment); |
790 | } |
791 | |
792 | |
793 | |
794 | |
795 | |
796 | |
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 | CharUnits getSize() const { |
807 | assert(Size % Context.getCharWidth() == 0); |
808 | return Context.toCharUnitsFromBits(Size); |
809 | } |
810 | uint64_t getSizeInBits() const { return Size; } |
811 | |
812 | void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); } |
813 | void setSize(uint64_t NewSize) { Size = NewSize; } |
814 | |
815 | CharUnits getAligment() const { return Alignment; } |
816 | |
817 | CharUnits getDataSize() const { |
818 | assert(DataSize % Context.getCharWidth() == 0); |
819 | return Context.toCharUnitsFromBits(DataSize); |
820 | } |
821 | uint64_t getDataSizeInBits() const { return DataSize; } |
822 | |
823 | void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); } |
824 | void setDataSize(uint64_t NewSize) { DataSize = NewSize; } |
825 | |
826 | ItaniumRecordLayoutBuilder(const ItaniumRecordLayoutBuilder &) = delete; |
827 | void operator=(const ItaniumRecordLayoutBuilder &) = delete; |
828 | }; |
829 | } |
830 | |
831 | void ItaniumRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) { |
832 | for (const auto &I : RD->bases()) { |
833 | assert(!I.getType()->isDependentType() && |
834 | "Cannot layout class with dependent bases."); |
835 | |
836 | const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
837 | |
838 | |
839 | if (I.isVirtual() && Context.isNearlyEmpty(Base)) { |
840 | |
841 | |
842 | if (!IndirectPrimaryBases.count(Base)) { |
843 | PrimaryBase = Base; |
844 | PrimaryBaseIsVirtual = true; |
845 | return; |
846 | } |
847 | |
848 | |
849 | if (!FirstNearlyEmptyVBase) |
850 | FirstNearlyEmptyVBase = Base; |
851 | } |
852 | |
853 | SelectPrimaryVBase(Base); |
854 | if (PrimaryBase) |
855 | return; |
856 | } |
857 | } |
858 | |
859 | |
860 | void ItaniumRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) { |
861 | |
862 | if (!RD->isDynamicClass()) |
863 | return; |
864 | |
865 | |
866 | |
867 | RD->getIndirectPrimaryBases(IndirectPrimaryBases); |
868 | |
869 | |
870 | |
871 | |
872 | for (const auto &I : RD->bases()) { |
873 | |
874 | if (I.isVirtual()) |
875 | continue; |
876 | |
877 | const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
878 | |
879 | if (Base->isDynamicClass()) { |
880 | |
881 | PrimaryBase = Base; |
882 | PrimaryBaseIsVirtual = false; |
883 | return; |
884 | } |
885 | } |
886 | |
887 | |
888 | |
889 | |
890 | |
891 | if (RD->getNumVBases() != 0) { |
892 | SelectPrimaryVBase(RD); |
893 | if (PrimaryBase) |
894 | return; |
895 | } |
896 | |
897 | |
898 | if (FirstNearlyEmptyVBase) { |
899 | PrimaryBase = FirstNearlyEmptyVBase; |
900 | PrimaryBaseIsVirtual = true; |
901 | return; |
902 | } |
903 | |
904 | assert(!PrimaryBase && "Should not get here with a primary base!"); |
905 | } |
906 | |
907 | BaseSubobjectInfo *ItaniumRecordLayoutBuilder::ComputeBaseSubobjectInfo( |
908 | const CXXRecordDecl *RD, bool IsVirtual, BaseSubobjectInfo *Derived) { |
909 | BaseSubobjectInfo *Info; |
910 | |
911 | if (IsVirtual) { |
912 | |
913 | BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD]; |
914 | if (InfoSlot) { |
915 | assert(InfoSlot->Class == RD && "Wrong class for virtual base info!"); |
916 | return InfoSlot; |
917 | } |
918 | |
919 | |
920 | InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
921 | Info = InfoSlot; |
922 | } else { |
923 | Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo; |
924 | } |
925 | |
926 | Info->Class = RD; |
927 | Info->IsVirtual = IsVirtual; |
928 | Info->Derived = nullptr; |
929 | Info->PrimaryVirtualBaseInfo = nullptr; |
930 | |
931 | const CXXRecordDecl *PrimaryVirtualBase = nullptr; |
932 | BaseSubobjectInfo *PrimaryVirtualBaseInfo = nullptr; |
933 | |
934 | |
935 | if (RD->getNumVBases()) { |
936 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
937 | if (Layout.isPrimaryBaseVirtual()) { |
938 | |
939 | PrimaryVirtualBase = Layout.getPrimaryBase(); |
940 | assert(PrimaryVirtualBase && "Didn't have a primary virtual base!"); |
941 | |
942 | |
943 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
944 | |
945 | if (PrimaryVirtualBaseInfo) { |
946 | if (PrimaryVirtualBaseInfo->Derived) { |
947 | |
948 | |
949 | |
950 | PrimaryVirtualBase = nullptr; |
951 | } else { |
952 | |
953 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
954 | PrimaryVirtualBaseInfo->Derived = Info; |
955 | } |
956 | } |
957 | } |
958 | } |
959 | |
960 | |
961 | for (const auto &I : RD->bases()) { |
962 | bool IsVirtual = I.isVirtual(); |
963 | |
964 | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
965 | |
966 | Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info)); |
967 | } |
968 | |
969 | if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) { |
970 | |
971 | |
972 | PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase); |
973 | assert(PrimaryVirtualBaseInfo && |
974 | "Did not create a primary virtual base!"); |
975 | |
976 | |
977 | Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo; |
978 | PrimaryVirtualBaseInfo->Derived = Info; |
979 | } |
980 | |
981 | return Info; |
982 | } |
983 | |
984 | void ItaniumRecordLayoutBuilder::ComputeBaseSubobjectInfo( |
985 | const CXXRecordDecl *RD) { |
986 | for (const auto &I : RD->bases()) { |
987 | bool IsVirtual = I.isVirtual(); |
988 | |
989 | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
990 | |
991 | |
992 | BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, |
993 | nullptr); |
994 | |
995 | if (IsVirtual) { |
996 | |
997 | assert(VirtualBaseInfo.count(BaseDecl) && |
998 | "Did not add virtual base!"); |
999 | } else { |
1000 | |
1001 | assert(!NonVirtualBaseInfo.count(BaseDecl) && |
1002 | "Non-virtual base already exists!"); |
1003 | NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info)); |
1004 | } |
1005 | } |
1006 | } |
1007 | |
1008 | void ItaniumRecordLayoutBuilder::EnsureVTablePointerAlignment( |
1009 | CharUnits UnpackedBaseAlign) { |
1010 | CharUnits BaseAlign = Packed ? CharUnits::One() : UnpackedBaseAlign; |
1011 | |
1012 | |
1013 | if (!MaxFieldAlignment.isZero()) { |
1014 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
1015 | UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment); |
1016 | } |
1017 | |
1018 | |
1019 | setSize(getSize().alignTo(BaseAlign)); |
1020 | |
1021 | |
1022 | UpdateAlignment(BaseAlign, UnpackedBaseAlign, BaseAlign); |
1023 | } |
1024 | |
1025 | void ItaniumRecordLayoutBuilder::LayoutNonVirtualBases( |
1026 | const CXXRecordDecl *RD) { |
1027 | |
1028 | DeterminePrimaryBase(RD); |
1029 | |
1030 | |
1031 | ComputeBaseSubobjectInfo(RD); |
1032 | |
1033 | |
1034 | if (PrimaryBase) { |
1035 | if (PrimaryBaseIsVirtual) { |
1036 | |
1037 | |
1038 | BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase); |
1039 | PrimaryBaseInfo->Derived = nullptr; |
1040 | |
1041 | |
1042 | IndirectPrimaryBases.insert(PrimaryBase); |
1043 | |
1044 | assert(!VisitedVirtualBases.count(PrimaryBase) && |
1045 | "vbase already visited!"); |
1046 | VisitedVirtualBases.insert(PrimaryBase); |
1047 | |
1048 | LayoutVirtualBase(PrimaryBaseInfo); |
1049 | } else { |
1050 | BaseSubobjectInfo *PrimaryBaseInfo = |
1051 | NonVirtualBaseInfo.lookup(PrimaryBase); |
1052 | assert(PrimaryBaseInfo && |
1053 | "Did not find base info for non-virtual primary base!"); |
1054 | |
1055 | LayoutNonVirtualBase(PrimaryBaseInfo); |
1056 | } |
1057 | |
1058 | |
1059 | |
1060 | } else if (RD->isDynamicClass()) { |
1061 | assert(DataSize == 0 && "Vtable pointer must be at offset zero!"); |
1062 | CharUnits PtrWidth = |
1063 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
1064 | CharUnits PtrAlign = |
1065 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
1066 | EnsureVTablePointerAlignment(PtrAlign); |
1067 | HasOwnVFPtr = true; |
1068 | |
1069 | assert(!IsUnion && "Unions cannot be dynamic classes."); |
1070 | HandledFirstNonOverlappingEmptyField = true; |
1071 | |
1072 | setSize(getSize() + PtrWidth); |
1073 | setDataSize(getSize()); |
1074 | } |
1075 | |
1076 | |
1077 | for (const auto &I : RD->bases()) { |
1078 | |
1079 | |
1080 | if (I.isVirtual()) |
1081 | continue; |
1082 | |
1083 | const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl(); |
1084 | |
1085 | |
1086 | |
1087 | |
1088 | if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual) |
1089 | continue; |
1090 | |
1091 | |
1092 | BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl); |
1093 | assert(BaseInfo && "Did not find base info for non-virtual base!"); |
1094 | |
1095 | LayoutNonVirtualBase(BaseInfo); |
1096 | } |
1097 | } |
1098 | |
1099 | void ItaniumRecordLayoutBuilder::LayoutNonVirtualBase( |
1100 | const BaseSubobjectInfo *Base) { |
1101 | |
1102 | CharUnits Offset = LayoutBase(Base); |
1103 | |
1104 | |
1105 | assert(!Bases.count(Base->Class) && "base offset already exists!"); |
1106 | Bases.insert(std::make_pair(Base->Class, Offset)); |
1107 | |
1108 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
1109 | } |
1110 | |
1111 | void ItaniumRecordLayoutBuilder::AddPrimaryVirtualBaseOffsets( |
1112 | const BaseSubobjectInfo *Info, CharUnits Offset) { |
1113 | |
1114 | if (!Info->Class->getNumVBases()) |
1115 | return; |
1116 | |
1117 | |
1118 | if (Info->PrimaryVirtualBaseInfo) { |
1119 | assert(Info->PrimaryVirtualBaseInfo->IsVirtual && |
1120 | "Primary virtual base is not virtual!"); |
1121 | if (Info->PrimaryVirtualBaseInfo->Derived == Info) { |
1122 | |
1123 | assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) && |
1124 | "primary vbase offset already exists!"); |
1125 | VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class, |
1126 | ASTRecordLayout::VBaseInfo(Offset, false))); |
1127 | |
1128 | |
1129 | AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset); |
1130 | } |
1131 | } |
1132 | |
1133 | |
1134 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class); |
1135 | for (const BaseSubobjectInfo *Base : Info->Bases) { |
1136 | if (Base->IsVirtual) |
1137 | continue; |
1138 | |
1139 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class); |
1140 | AddPrimaryVirtualBaseOffsets(Base, BaseOffset); |
1141 | } |
1142 | } |
1143 | |
1144 | void ItaniumRecordLayoutBuilder::LayoutVirtualBases( |
1145 | const CXXRecordDecl *RD, const CXXRecordDecl *MostDerivedClass) { |
1146 | const CXXRecordDecl *PrimaryBase; |
1147 | bool PrimaryBaseIsVirtual; |
1148 | |
1149 | if (MostDerivedClass == RD) { |
| |
1150 | PrimaryBase = this->PrimaryBase; |
1151 | PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual; |
1152 | } else { |
1153 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
1154 | PrimaryBase = Layout.getPrimaryBase(); |
1155 | PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
1156 | } |
1157 | |
1158 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 13 | | Assuming '__begin1' is not equal to '__end1' | |
|
1159 | assert(!Base.getType()->isDependentType() && |
1160 | "Cannot layout class with dependent bases."); |
1161 | |
1162 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 14 | | 'BaseDecl' initialized here | |
|
1163 | |
1164 | if (Base.isVirtual()) { |
| 15 | | Assuming the condition is true | |
|
| |
1165 | if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) { |
| 17 | | Assuming 'PrimaryBase' is equal to 'BaseDecl' | |
|
| 18 | | Assuming pointer value is null | |
|
| 19 | | Assuming 'PrimaryBaseIsVirtual' is true | |
|
| |
1166 | bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl); |
1167 | |
1168 | |
1169 | if (!IndirectPrimaryBase) { |
1170 | |
1171 | if (!VisitedVirtualBases.insert(BaseDecl).second) |
1172 | continue; |
1173 | |
1174 | const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl); |
1175 | assert(BaseInfo && "Did not find virtual base info!"); |
1176 | LayoutVirtualBase(BaseInfo); |
1177 | } |
1178 | } |
1179 | } |
1180 | |
1181 | if (!BaseDecl->getNumVBases()) { |
| 21 | | Called C++ object pointer is null |
|
1182 | |
1183 | continue; |
1184 | } |
1185 | |
1186 | LayoutVirtualBases(BaseDecl, MostDerivedClass); |
1187 | } |
1188 | } |
1189 | |
1190 | void ItaniumRecordLayoutBuilder::LayoutVirtualBase( |
1191 | const BaseSubobjectInfo *Base) { |
1192 | assert(!Base->Derived && "Trying to lay out a primary virtual base!"); |
1193 | |
1194 | |
1195 | CharUnits Offset = LayoutBase(Base); |
1196 | |
1197 | |
1198 | assert(!VBases.count(Base->Class) && "vbase offset already exists!"); |
1199 | VBases.insert(std::make_pair(Base->Class, |
1200 | ASTRecordLayout::VBaseInfo(Offset, false))); |
1201 | |
1202 | AddPrimaryVirtualBaseOffsets(Base, Offset); |
1203 | } |
1204 | |
1205 | CharUnits |
1206 | ItaniumRecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { |
1207 | assert(!IsUnion && "Unions cannot have base classes."); |
1208 | |
1209 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class); |
1210 | CharUnits Offset; |
1211 | |
1212 | |
1213 | bool HasExternalLayout = false; |
1214 | if (UseExternalLayout) { |
1215 | if (Base->IsVirtual) |
1216 | HasExternalLayout = External.getExternalVBaseOffset(Base->Class, Offset); |
1217 | else |
1218 | HasExternalLayout = External.getExternalNVBaseOffset(Base->Class, Offset); |
1219 | } |
1220 | |
1221 | auto getBaseOrPreferredBaseAlignFromUnpacked = [&](CharUnits UnpackedAlign) { |
1222 | |
1223 | |
1224 | return (Packed && ((Context.getLangOpts().getClangABICompat() <= |
1225 | LangOptions::ClangABI::Ver6) || |
1226 | Context.getTargetInfo().getTriple().isPS4() || |
1227 | Context.getTargetInfo().getTriple().isOSAIX())) |
1228 | ? CharUnits::One() |
1229 | : UnpackedAlign; |
1230 | }; |
1231 | |
1232 | CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlignment(); |
1233 | CharUnits UnpackedPreferredBaseAlign = Layout.getPreferredNVAlignment(); |
1234 | CharUnits BaseAlign = |
1235 | getBaseOrPreferredBaseAlignFromUnpacked(UnpackedBaseAlign); |
1236 | CharUnits PreferredBaseAlign = |
1237 | getBaseOrPreferredBaseAlignFromUnpacked(UnpackedPreferredBaseAlign); |
1238 | |
1239 | const bool DefaultsToAIXPowerAlignment = |
1240 | Context.getTargetInfo().defaultsToAIXPowerAlignment(); |
1241 | if (DefaultsToAIXPowerAlignment) { |
1242 | |
1243 | |
1244 | |
1245 | |
1246 | if (!Base->Class->isEmpty() && !HandledFirstNonOverlappingEmptyField) { |
1247 | |
1248 | |
1249 | HandledFirstNonOverlappingEmptyField = true; |
1250 | } else if (!IsNaturalAlign) { |
1251 | UnpackedPreferredBaseAlign = UnpackedBaseAlign; |
1252 | PreferredBaseAlign = BaseAlign; |
1253 | } |
1254 | } |
1255 | |
1256 | CharUnits UnpackedAlignTo = !DefaultsToAIXPowerAlignment |
1257 | ? UnpackedBaseAlign |
1258 | : UnpackedPreferredBaseAlign; |
1259 | |
1260 | if (Base->Class->isEmpty() && |
1261 | (!HasExternalLayout || Offset == CharUnits::Zero()) && |
1262 | EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { |
1263 | setSize(std::max(getSize(), Layout.getSize())); |
1264 | UpdateAlignment(BaseAlign, UnpackedAlignTo, PreferredBaseAlign); |
1265 | |
1266 | return CharUnits::Zero(); |
1267 | } |
1268 | |
1269 | |
1270 | |
1271 | if (!MaxFieldAlignment.isZero()) { |
1272 | BaseAlign = std::min(BaseAlign, MaxFieldAlignment); |
1273 | PreferredBaseAlign = std::min(PreferredBaseAlign, MaxFieldAlignment); |
1274 | UnpackedAlignTo = std::min(UnpackedAlignTo, MaxFieldAlignment); |
1275 | } |
1276 | |
1277 | CharUnits AlignTo = |
1278 | !DefaultsToAIXPowerAlignment ? BaseAlign : PreferredBaseAlign; |
1279 | if (!HasExternalLayout) { |
1280 | |
1281 | Offset = getDataSize().alignTo(AlignTo); |
1282 | |
1283 | |
1284 | while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset)) |
1285 | Offset += AlignTo; |
1286 | } else { |
1287 | bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset); |
1288 | (void)Allowed; |
1289 | assert(Allowed && "Base subobject externally placed at overlapping offset"); |
1290 | |
1291 | if (InferAlignment && Offset < getDataSize().alignTo(AlignTo)) { |
1292 | |
1293 | |
1294 | Alignment = CharUnits::One(); |
1295 | InferAlignment = false; |
1296 | } |
1297 | } |
1298 | |
1299 | if (!Base->Class->isEmpty()) { |
1300 | |
1301 | setDataSize(Offset + Layout.getNonVirtualSize()); |
1302 | |
1303 | setSize(std::max(getSize(), getDataSize())); |
1304 | } else |
1305 | setSize(std::max(getSize(), Offset + Layout.getSize())); |
1306 | |
1307 | |
1308 | UpdateAlignment(BaseAlign, UnpackedAlignTo, PreferredBaseAlign); |
1309 | |
1310 | return Offset; |
1311 | } |
1312 | |
1313 | void ItaniumRecordLayoutBuilder::InitializeLayout(const Decl *D) { |
1314 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
1315 | IsUnion = RD->isUnion(); |
1316 | IsMsStruct = RD->isMsStruct(Context); |
1317 | } |
1318 | |
1319 | Packed = D->hasAttr<PackedAttr>(); |
1320 | |
1321 | |
1322 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) { |
1323 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
1324 | } |
1325 | |
1326 | |
1327 | |
1328 | |
1329 | |
1330 | if (D->hasAttr<AlignMac68kAttr>()) { |
1331 | assert( |
1332 | !D->hasAttr<AlignNaturalAttr>() && |
1333 | "Having both mac68k and natural alignment on a decl is not allowed."); |
1334 | IsMac68kAlign = true; |
1335 | MaxFieldAlignment = CharUnits::fromQuantity(2); |
1336 | Alignment = CharUnits::fromQuantity(2); |
1337 | PreferredAlignment = CharUnits::fromQuantity(2); |
1338 | } else { |
1339 | if (D->hasAttr<AlignNaturalAttr>()) |
1340 | IsNaturalAlign = true; |
1341 | |
1342 | if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>()) |
1343 | MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment()); |
1344 | |
1345 | if (unsigned MaxAlign = D->getMaxAlignment()) |
1346 | UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign)); |
1347 | } |
1348 | |
1349 | HandledFirstNonOverlappingEmptyField = |
1350 | !Context.getTargetInfo().defaultsToAIXPowerAlignment() || IsNaturalAlign; |
1351 | |
1352 | |
1353 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
1354 | if (ExternalASTSource *Source = Context.getExternalSource()) { |
1355 | UseExternalLayout = Source->layoutRecordType( |
1356 | RD, External.Size, External.Align, External.FieldOffsets, |
1357 | External.BaseOffsets, External.VirtualBaseOffsets); |
1358 | |
1359 | |
1360 | if (UseExternalLayout) { |
1361 | if (External.Align > 0) { |
1362 | Alignment = Context.toCharUnitsFromBits(External.Align); |
1363 | PreferredAlignment = Context.toCharUnitsFromBits(External.Align); |
1364 | } else { |
1365 | |
1366 | InferAlignment = true; |
1367 | } |
1368 | } |
1369 | } |
1370 | } |
1371 | |
1372 | void ItaniumRecordLayoutBuilder::Layout(const RecordDecl *D) { |
1373 | InitializeLayout(D); |
1374 | LayoutFields(D); |
1375 | |
1376 | |
1377 | |
1378 | FinishLayout(D); |
1379 | } |
1380 | |
1381 | void ItaniumRecordLayoutBuilder::Layout(const CXXRecordDecl *RD) { |
1382 | InitializeLayout(RD); |
1383 | |
1384 | |
1385 | LayoutNonVirtualBases(RD); |
1386 | |
1387 | LayoutFields(RD); |
1388 | |
1389 | NonVirtualSize = Context.toCharUnitsFromBits( |
1390 | llvm::alignTo(getSizeInBits(), Context.getTargetInfo().getCharAlign())); |
1391 | NonVirtualAlignment = Alignment; |
1392 | PreferredNVAlignment = PreferredAlignment; |
1393 | |
1394 | |
1395 | LayoutVirtualBases(RD, RD); |
| 11 | | Calling 'ItaniumRecordLayoutBuilder::LayoutVirtualBases' | |
|
1396 | |
1397 | |
1398 | |
1399 | FinishLayout(RD); |
1400 | |
1401 | #ifndef NDEBUG |
1402 | |
1403 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
1404 | if (Base.isVirtual()) |
1405 | continue; |
1406 | |
1407 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
1408 | |
1409 | assert(Bases.count(BaseDecl) && "Did not find base offset!"); |
1410 | } |
1411 | |
1412 | |
1413 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
1414 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
1415 | |
1416 | assert(VBases.count(BaseDecl) && "Did not find base offset!"); |
1417 | } |
1418 | #endif |
1419 | } |
1420 | |
1421 | void ItaniumRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) { |
1422 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
1423 | const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD); |
1424 | |
1425 | UpdateAlignment(SL.getAlignment()); |
1426 | |
1427 | |
1428 | |
1429 | setDataSize(SL.getDataSize()); |
1430 | setSize(getDataSize()); |
1431 | } |
1432 | |
1433 | InitializeLayout(D); |
1434 | |
1435 | for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD; |
1436 | IVD = IVD->getNextIvar()) |
1437 | LayoutField(IVD, false); |
1438 | |
1439 | |
1440 | |
1441 | FinishLayout(D); |
1442 | } |
1443 | |
1444 | void ItaniumRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
1445 | |
1446 | |
1447 | bool InsertExtraPadding = D->mayInsertExtraPadding(true); |
1448 | bool HasFlexibleArrayMember = D->hasFlexibleArrayMember(); |
1449 | for (auto I = D->field_begin(), End = D->field_end(); I != End; ++I) { |
1450 | auto Next(I); |
1451 | ++Next; |
1452 | LayoutField(*I, |
1453 | InsertExtraPadding && (Next != End || !HasFlexibleArrayMember)); |
1454 | } |
1455 | } |
1456 | |
1457 | |
1458 | static uint64_t |
1459 | roundUpSizeToCharAlignment(uint64_t Size, |
1460 | const ASTContext &Context) { |
1461 | uint64_t CharAlignment = Context.getTargetInfo().getCharAlign(); |
1462 | return llvm::alignTo(Size, CharAlignment); |
1463 | } |
1464 | |
1465 | void ItaniumRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize, |
1466 | uint64_t StorageUnitSize, |
1467 | bool FieldPacked, |
1468 | const FieldDecl *D) { |
1469 | assert(Context.getLangOpts().CPlusPlus && |
1470 | "Can only have wide bit-fields in C++!"); |
1471 | |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | QualType IntegralPODTypes[] = { |
1477 | Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy, |
1478 | Context.UnsignedLongTy, Context.UnsignedLongLongTy |
1479 | }; |
1480 | |
1481 | QualType Type; |
1482 | for (const QualType &QT : IntegralPODTypes) { |
1483 | uint64_t Size = Context.getTypeSize(QT); |
1484 | |
1485 | if (Size > FieldSize) |
1486 | break; |
1487 | |
1488 | Type = QT; |
1489 | } |
1490 | assert(!Type.isNull() && "Did not find a type!"); |
1491 | |
1492 | CharUnits TypeAlign = Context.getTypeAlignInChars(Type); |
1493 | |
1494 | |
1495 | UnfilledBitsInLastUnit = 0; |
1496 | LastBitfieldStorageUnitSize = 0; |
1497 | |
1498 | uint64_t FieldOffset; |
1499 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
1500 | |
1501 | if (IsUnion) { |
1502 | uint64_t RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize, |
1503 | Context); |
1504 | setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize)); |
1505 | FieldOffset = 0; |
1506 | } else { |
1507 | |
1508 | |
1509 | FieldOffset = llvm::alignTo(getDataSizeInBits(), Context.toBits(TypeAlign)); |
1510 | |
1511 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
1512 | |
1513 | setDataSize( |
1514 | llvm::alignTo(NewSizeInBits, Context.getTargetInfo().getCharAlign())); |
1515 | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
1516 | } |
1517 | |
1518 | |
1519 | FieldOffsets.push_back(FieldOffset); |
1520 | |
1521 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset, |
1522 | Context.toBits(TypeAlign), FieldPacked, D); |
1523 | |
1524 | |
1525 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
1526 | |
1527 | |
1528 | UpdateAlignment(TypeAlign); |
1529 | } |
1530 | |
1531 | static bool isAIXLayout(const ASTContext &Context) { |
1532 | return Context.getTargetInfo().getTriple().getOS() == llvm::Triple::AIX; |
1533 | } |
1534 | |
1535 | void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) { |
1536 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
1537 | uint64_t FieldSize = D->getBitWidthValue(Context); |
1538 | TypeInfo FieldInfo = Context.getTypeInfo(D->getType()); |
1539 | uint64_t StorageUnitSize = FieldInfo.Width; |
1540 | unsigned FieldAlign = FieldInfo.Align; |
1541 | bool AlignIsRequired = FieldInfo.AlignIsRequired; |
1542 | |
1543 | |
1544 | |
1545 | |
1546 | |
1547 | |
1548 | |
1549 | |
1550 | |
1551 | |
1552 | |
1553 | |
1554 | |
1555 | |
1556 | |
1557 | |
1558 | |
1559 | |
1560 | |
1561 | |
1562 | |
1563 | |
1564 | |
1565 | |
1566 | |
1567 | |
1568 | |
1569 | |
1570 | |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | |
1594 | |
1595 | |
1596 | |
1597 | |
1598 | |
1599 | |
1600 | |
1601 | if (IsMsStruct) { |
1602 | |
1603 | FieldAlign = StorageUnitSize; |
1604 | |
1605 | |
1606 | |
1607 | |
1608 | if (LastBitfieldStorageUnitSize != StorageUnitSize || |
1609 | UnfilledBitsInLastUnit < FieldSize) { |
1610 | |
1611 | if (!LastBitfieldStorageUnitSize && !FieldSize) |
1612 | FieldAlign = 1; |
1613 | |
1614 | UnfilledBitsInLastUnit = 0; |
1615 | LastBitfieldStorageUnitSize = 0; |
1616 | } |
1617 | } |
1618 | |
1619 | if (isAIXLayout(Context)) { |
1620 | if (StorageUnitSize < Context.getTypeSize(Context.UnsignedIntTy)) { |
1621 | |
1622 | |
1623 | StorageUnitSize = Context.getTypeSize(Context.UnsignedIntTy); |
1624 | } else if (StorageUnitSize > Context.getTypeSize(Context.UnsignedIntTy) && |
1625 | Context.getTargetInfo().getTriple().isArch32Bit() && |
1626 | FieldSize <= 32) { |
1627 | |
1628 | |
1629 | StorageUnitSize = 32; |
1630 | |
1631 | if (!AlignIsRequired) |
1632 | FieldAlign = 32; |
1633 | } |
1634 | |
1635 | if (FieldAlign < StorageUnitSize) { |
1636 | |
1637 | |
1638 | FieldAlign = StorageUnitSize; |
1639 | } |
1640 | } |
1641 | |
1642 | |
1643 | |
1644 | |
1645 | if (FieldSize > StorageUnitSize && !isAIXLayout(Context)) { |
1646 | LayoutWideBitField(FieldSize, StorageUnitSize, FieldPacked, D); |
1647 | return; |
1648 | } |
1649 | |
1650 | |
1651 | uint64_t FieldOffset = |
1652 | IsUnion ? 0 : (getDataSizeInBits() - UnfilledBitsInLastUnit); |
1653 | |
1654 | |
1655 | if (!IsMsStruct && !Context.getTargetInfo().useBitFieldTypeAlignment()) { |
1656 | |
1657 | if (FieldSize == 0 && |
1658 | Context.getTargetInfo().useZeroLengthBitfieldAlignment()) { |
1659 | |
1660 | if (!IsUnion && FieldOffset == 0 && |
1661 | !Context.getTargetInfo().useLeadingZeroLengthBitfield()) |
1662 | FieldAlign = 1; |
1663 | else { |
1664 | |
1665 | |
1666 | unsigned ZeroLengthBitfieldBoundary = |
1667 | Context.getTargetInfo().getZeroLengthBitfieldBoundary(); |
1668 | FieldAlign = std::max(FieldAlign, ZeroLengthBitfieldBoundary); |
1669 | } |
1670 | |
1671 | } else { |
1672 | FieldAlign = 1; |
1673 | } |
1674 | } |
1675 | |
1676 | |
1677 | unsigned UnpackedFieldAlign = FieldAlign; |
1678 | |
1679 | |
1680 | if (!IsMsStruct && FieldPacked && FieldSize != 0) |
1681 | FieldAlign = 1; |
1682 | |
1683 | |
1684 | unsigned ExplicitFieldAlign = D->getMaxAlignment(); |
1685 | if (ExplicitFieldAlign) { |
1686 | FieldAlign = std::max(FieldAlign, ExplicitFieldAlign); |
1687 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, ExplicitFieldAlign); |
1688 | } |
1689 | |
1690 | |
1691 | |
1692 | unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); |
1693 | if (!MaxFieldAlignment.isZero() && FieldSize) { |
1694 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); |
1695 | if (FieldPacked) |
1696 | FieldAlign = UnpackedFieldAlign; |
1697 | else |
1698 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
1699 | } |
1700 | |
1701 | |
1702 | |
1703 | if (IsMsStruct && IsUnion) { |
1704 | FieldAlign = UnpackedFieldAlign = 1; |
1705 | } |
1706 | |
1707 | |
1708 | |
1709 | |
1710 | uint64_t UnpaddedFieldOffset = FieldOffset; |
1711 | uint64_t UnpackedFieldOffset = FieldOffset; |
1712 | |
1713 | |
1714 | |
1715 | |
1716 | if (IsMsStruct) { |
1717 | |
1718 | |
1719 | |
1720 | |
1721 | if (FieldSize == 0 || FieldSize > UnfilledBitsInLastUnit) { |
1722 | FieldOffset = llvm::alignTo(FieldOffset, FieldAlign); |
1723 | UnpackedFieldOffset = |
1724 | llvm::alignTo(UnpackedFieldOffset, UnpackedFieldAlign); |
1725 | UnfilledBitsInLastUnit = 0; |
1726 | } |
1727 | |
1728 | } else { |
1729 | |
1730 | bool AllowPadding = MaxFieldAlignment.isZero(); |
1731 | |
1732 | |
1733 | if (FieldSize == 0 || |
1734 | (AllowPadding && |
1735 | (FieldOffset & (FieldAlign - 1)) + FieldSize > StorageUnitSize)) { |
1736 | FieldOffset = llvm::alignTo(FieldOffset, FieldAlign); |
1737 | } else if (ExplicitFieldAlign && |
1738 | (MaxFieldAlignmentInBits == 0 || |
1739 | ExplicitFieldAlign <= MaxFieldAlignmentInBits) && |
1740 | Context.getTargetInfo().useExplicitBitFieldAlignment()) { |
1741 | |
1742 | |
1743 | FieldOffset = llvm::alignTo(FieldOffset, ExplicitFieldAlign); |
1744 | } |
1745 | |
1746 | |
1747 | if (FieldSize == 0 || |
1748 | (AllowPadding && |
1749 | (UnpackedFieldOffset & (UnpackedFieldAlign - 1)) + FieldSize > |
1750 | StorageUnitSize)) |
1751 | UnpackedFieldOffset = |
1752 | llvm::alignTo(UnpackedFieldOffset, UnpackedFieldAlign); |
1753 | else if (ExplicitFieldAlign && |
1754 | (MaxFieldAlignmentInBits == 0 || |
1755 | ExplicitFieldAlign <= MaxFieldAlignmentInBits) && |
1756 | Context.getTargetInfo().useExplicitBitFieldAlignment()) |
1757 | UnpackedFieldOffset = |
1758 | llvm::alignTo(UnpackedFieldOffset, ExplicitFieldAlign); |
1759 | } |
1760 | |
1761 | |
1762 | |
1763 | if (UseExternalLayout) |
1764 | FieldOffset = updateExternalFieldOffset(D, FieldOffset); |
1765 | |
1766 | |
1767 | FieldOffsets.push_back(FieldOffset); |
1768 | |
1769 | |
1770 | |
1771 | |
1772 | |
1773 | if (!IsMsStruct && |
1774 | !Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
1775 | !D->getIdentifier()) |
1776 | FieldAlign = UnpackedFieldAlign = 1; |
1777 | |
1778 | |
1779 | |
1780 | |
1781 | if (isAIXLayout(Context) && !MaxFieldAlignment.isZero() && !FieldSize) |
1782 | FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); |
1783 | |
1784 | |
1785 | if (!UseExternalLayout) |
1786 | CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, |
1787 | UnpackedFieldAlign, FieldPacked, D); |
1788 | |
1789 | |
1790 | |
1791 | |
1792 | if (IsUnion) { |
1793 | |
1794 | |
1795 | uint64_t RoundedFieldSize; |
1796 | if (IsMsStruct) { |
1797 | RoundedFieldSize = (FieldSize ? StorageUnitSize |
1798 | : Context.getTargetInfo().getCharWidth()); |
1799 | |
1800 | |
1801 | |
1802 | } else { |
1803 | RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize, Context); |
1804 | } |
1805 | setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize)); |
1806 | |
1807 | |
1808 | |
1809 | } else if (IsMsStruct && FieldSize) { |
1810 | |
1811 | |
1812 | if (!UnfilledBitsInLastUnit) { |
1813 | setDataSize(FieldOffset + StorageUnitSize); |
1814 | UnfilledBitsInLastUnit = StorageUnitSize; |
1815 | } |
1816 | UnfilledBitsInLastUnit -= FieldSize; |
1817 | LastBitfieldStorageUnitSize = StorageUnitSize; |
1818 | |
1819 | |
1820 | |
1821 | |
1822 | } else { |
1823 | uint64_t NewSizeInBits = FieldOffset + FieldSize; |
1824 | uint64_t CharAlignment = Context.getTargetInfo().getCharAlign(); |
1825 | setDataSize(llvm::alignTo(NewSizeInBits, CharAlignment)); |
1826 | UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits; |
1827 | |
1828 | |
1829 | |
1830 | |
1831 | LastBitfieldStorageUnitSize = 0; |
1832 | } |
1833 | |
1834 | |
1835 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
1836 | |
1837 | |
1838 | UnadjustedAlignment = |
1839 | std::max(UnadjustedAlignment, Context.toCharUnitsFromBits(FieldAlign)); |
1840 | UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), |
1841 | Context.toCharUnitsFromBits(UnpackedFieldAlign)); |
1842 | } |
1843 | |
1844 | void ItaniumRecordLayoutBuilder::LayoutField(const FieldDecl *D, |
1845 | bool InsertExtraPadding) { |
1846 | auto *FieldClass = D->getType()->getAsCXXRecordDecl(); |
1847 | bool PotentiallyOverlapping = D->hasAttr<NoUniqueAddressAttr>() && FieldClass; |
1848 | bool IsOverlappingEmptyField = |
1849 | PotentiallyOverlapping && FieldClass->isEmpty(); |
1850 | |
1851 | CharUnits FieldOffset = |
1852 | (IsUnion || IsOverlappingEmptyField) ? CharUnits::Zero() : getDataSize(); |
1853 | |
1854 | const bool DefaultsToAIXPowerAlignment = |
1855 | Context.getTargetInfo().defaultsToAIXPowerAlignment(); |
1856 | bool FoundFirstNonOverlappingEmptyFieldForAIX = false; |
1857 | if (DefaultsToAIXPowerAlignment && !HandledFirstNonOverlappingEmptyField) { |
1858 | assert(FieldOffset == CharUnits::Zero() && |
1859 | "The first non-overlapping empty field should have been handled."); |
1860 | |
1861 | if (!IsOverlappingEmptyField) { |
1862 | FoundFirstNonOverlappingEmptyFieldForAIX = true; |
1863 | |
1864 | |
1865 | |
1866 | |
1867 | |
1868 | |
1869 | HandledFirstNonOverlappingEmptyField = !IsUnion; |
1870 | } |
1871 | } |
1872 | |
1873 | if (D->isBitField()) { |
1874 | LayoutBitField(D); |
1875 | return; |
1876 | } |
1877 | |
1878 | uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit; |
1879 | |
1880 | UnfilledBitsInLastUnit = 0; |
1881 | LastBitfieldStorageUnitSize = 0; |
1882 | |
1883 | bool FieldPacked = Packed || D->hasAttr<PackedAttr>(); |
1884 | |
1885 | bool AlignIsRequired = false; |
1886 | CharUnits FieldSize; |
1887 | CharUnits FieldAlign; |
1888 | |
1889 | |
1890 | |
1891 | CharUnits EffectiveFieldSize; |
1892 | |
1893 | auto setDeclInfo = [&](bool IsIncompleteArrayType) { |
1894 | auto TI = Context.getTypeInfoInChars(D->getType()); |
1895 | FieldAlign = TI.Align; |
1896 | |
1897 | |
1898 | EffectiveFieldSize = FieldSize = |
1899 | IsIncompleteArrayType ? CharUnits::Zero() : TI.Width; |
1900 | AlignIsRequired = TI.AlignIsRequired; |
1901 | }; |
1902 | |
1903 | if (D->getType()->isIncompleteArrayType()) { |
1904 | setDeclInfo(true ); |
1905 | } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) { |
1906 | unsigned AS = Context.getTargetAddressSpace(RT->getPointeeType()); |
1907 | EffectiveFieldSize = FieldSize = Context.toCharUnitsFromBits( |
1908 | Context.getTargetInfo().getPointerWidth(AS)); |
1909 | FieldAlign = Context.toCharUnitsFromBits( |
1910 | Context.getTargetInfo().getPointerAlign(AS)); |
1911 | } else { |
1912 | setDeclInfo(false ); |
1913 | |
1914 | |
1915 | |
1916 | if (PotentiallyOverlapping) { |
1917 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(FieldClass); |
1918 | EffectiveFieldSize = |
1919 | std::max(Layout.getNonVirtualSize(), Layout.getDataSize()); |
1920 | } |
1921 | |
1922 | if (IsMsStruct) { |
1923 | |
1924 | |
1925 | |
1926 | |
1927 | |
1928 | QualType T = Context.getBaseElementType(D->getType()); |
1929 | if (const BuiltinType *BTy = T->getAs<BuiltinType>()) { |
1930 | CharUnits TypeSize = Context.getTypeSizeInChars(BTy); |
1931 | |
1932 | if (!llvm::isPowerOf2_64(TypeSize.getQuantity())) { |
1933 | assert( |
1934 | !Context.getTargetInfo().getTriple().isWindowsMSVCEnvironment() && |
1935 | "Non PowerOf2 size in MSVC mode"); |
1936 | |
1937 | |
1938 | |
1939 | |
1940 | |
1941 | |
1942 | |
1943 | |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | |
1950 | |
1951 | |
1952 | |
1953 | |
1954 | if (!Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) |
1955 | Diag(D->getLocation(), diag::warn_npot_ms_struct); |
1956 | } |
1957 | if (TypeSize > FieldAlign && |
1958 | llvm::isPowerOf2_64(TypeSize.getQuantity())) |
1959 | FieldAlign = TypeSize; |
1960 | } |
1961 | } |
1962 | } |
1963 | |
1964 | |
1965 | |
1966 | |
1967 | |
1968 | |
1969 | |
1970 | |
1971 | |
1972 | |
1973 | CharUnits PreferredAlign = FieldAlign; |
1974 | if (DefaultsToAIXPowerAlignment && !AlignIsRequired && |
1975 | (FoundFirstNonOverlappingEmptyFieldForAIX || IsNaturalAlign)) { |
1976 | auto performBuiltinTypeAlignmentUpgrade = [&](const BuiltinType *BTy) { |
1977 | if (BTy->getKind() == BuiltinType::Double || |
1978 | BTy->getKind() == BuiltinType::LongDouble) { |
1979 | assert(PreferredAlign == CharUnits::fromQuantity(4) && |
1980 | "No need to upgrade the alignment value."); |
1981 | PreferredAlign = CharUnits::fromQuantity(8); |
1982 | } |
1983 | }; |
1984 | |
1985 | const Type *Ty = D->getType()->getBaseElementTypeUnsafe(); |
1986 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
1987 | performBuiltinTypeAlignmentUpgrade(CTy->getElementType()->castAs<BuiltinType>()); |
1988 | } else if (const BuiltinType *BTy = Ty->getAs<BuiltinType>()) { |
1989 | performBuiltinTypeAlignmentUpgrade(BTy); |
1990 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
1991 | const RecordDecl *RD = RT->getDecl(); |
1992 | assert(RD && "Expected non-null RecordDecl."); |
1993 | const ASTRecordLayout &FieldRecord = Context.getASTRecordLayout(RD); |
1994 | PreferredAlign = FieldRecord.getPreferredAlignment(); |
1995 | } |
1996 | } |
1997 | |
1998 | |
1999 | |
2000 | CharUnits UnpackedFieldAlign = |
2001 | !DefaultsToAIXPowerAlignment ? FieldAlign : PreferredAlign; |
2002 | CharUnits UnpackedFieldOffset = FieldOffset; |
2003 | |
2004 | if (FieldPacked) { |
2005 | FieldAlign = CharUnits::One(); |
2006 | PreferredAlign = CharUnits::One(); |
2007 | } |
2008 | CharUnits MaxAlignmentInChars = |
2009 | Context.toCharUnitsFromBits(D->getMaxAlignment()); |
2010 | FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); |
2011 | PreferredAlign = std::max(PreferredAlign, MaxAlignmentInChars); |
2012 | UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); |
2013 | |
2014 | |
2015 | if (!MaxFieldAlignment.isZero()) { |
2016 | FieldAlign = std::min(FieldAlign, MaxFieldAlignment); |
2017 | PreferredAlign = std::min(PreferredAlign, MaxFieldAlignment); |
2018 | UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); |
2019 | } |
2020 | |
2021 | CharUnits AlignTo = |
2022 | !DefaultsToAIXPowerAlignment ? FieldAlign : PreferredAlign; |
2023 | |
2024 | FieldOffset = FieldOffset.alignTo(AlignTo); |
2025 | UnpackedFieldOffset = UnpackedFieldOffset.alignTo(UnpackedFieldAlign); |
2026 | |
2027 | if (UseExternalLayout) { |
2028 | FieldOffset = Context.toCharUnitsFromBits( |
2029 | updateExternalFieldOffset(D, Context.toBits(FieldOffset))); |
2030 | |
2031 | if (!IsUnion && EmptySubobjects) { |
2032 | |
2033 | bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset); |
2034 | (void)Allowed; |
2035 | assert(Allowed && "Externally-placed field cannot be placed here"); |
2036 | } |
2037 | } else { |
2038 | if (!IsUnion && EmptySubobjects) { |
2039 | |
2040 | while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { |
2041 | |
2042 | |
2043 | if (FieldOffset == CharUnits::Zero() && |
2044 | getDataSize() != CharUnits::Zero()) |
2045 | FieldOffset = getDataSize().alignTo(AlignTo); |
2046 | else |
2047 | FieldOffset += AlignTo; |
2048 | } |
2049 | } |
2050 | } |
2051 | |
2052 | |
2053 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
2054 | |
2055 | if (!UseExternalLayout) |
2056 | CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, |
2057 | Context.toBits(UnpackedFieldOffset), |
2058 | Context.toBits(UnpackedFieldAlign), FieldPacked, D); |
2059 | |
2060 | if (InsertExtraPadding) { |
2061 | CharUnits ASanAlignment = CharUnits::fromQuantity(8); |
2062 | CharUnits ExtraSizeForAsan = ASanAlignment; |
2063 | if (FieldSize % ASanAlignment) |
2064 | ExtraSizeForAsan += |
2065 | ASanAlignment - CharUnits::fromQuantity(FieldSize % ASanAlignment); |
2066 | EffectiveFieldSize = FieldSize = FieldSize + ExtraSizeForAsan; |
2067 | } |
2068 | |
2069 | |
2070 | if (!IsOverlappingEmptyField) { |
2071 | uint64_t EffectiveFieldSizeInBits = Context.toBits(EffectiveFieldSize); |
2072 | if (IsUnion) |
2073 | setDataSize(std::max(getDataSizeInBits(), EffectiveFieldSizeInBits)); |
2074 | else |
2075 | setDataSize(FieldOffset + EffectiveFieldSize); |
2076 | |
2077 | PaddedFieldSize = std::max(PaddedFieldSize, FieldOffset + FieldSize); |
2078 | setSize(std::max(getSizeInBits(), getDataSizeInBits())); |
2079 | } else { |
2080 | setSize(std::max(getSizeInBits(), |
2081 | (uint64_t)Context.toBits(FieldOffset + FieldSize))); |
2082 | } |
2083 | |
2084 | |
2085 | UnadjustedAlignment = std::max(UnadjustedAlignment, FieldAlign); |
2086 | UpdateAlignment(FieldAlign, UnpackedFieldAlign, PreferredAlign); |
2087 | } |
2088 | |
2089 | void ItaniumRecordLayoutBuilder::FinishLayout(const NamedDecl *D) { |
2090 | |
2091 | if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0) { |
2092 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
2093 | |
2094 | |
2095 | |
2096 | if (RD->isEmpty()) |
2097 | setSize(CharUnits::One()); |
2098 | } |
2099 | else |
2100 | setSize(CharUnits::One()); |
2101 | } |
2102 | |
2103 | |
2104 | |
2105 | setSize(std::max(getSizeInBits(), (uint64_t)Context.toBits(PaddedFieldSize))); |
2106 | |
2107 | |
2108 | |
2109 | uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastUnit; |
2110 | uint64_t UnpackedSizeInBits = |
2111 | llvm::alignTo(getSizeInBits(), Context.toBits(UnpackedAlignment)); |
2112 | |
2113 | uint64_t RoundedSize = llvm::alignTo( |
2114 | getSizeInBits(), |
2115 | Context.toBits(!Context.getTargetInfo().defaultsToAIXPowerAlignment() |
2116 | ? Alignment |
2117 | : PreferredAlignment)); |
2118 | |
2119 | if (UseExternalLayout) { |
2120 | |
2121 | |
2122 | |
2123 | if (InferAlignment && External.Size < RoundedSize) { |
2124 | Alignment = CharUnits::One(); |
2125 | PreferredAlignment = CharUnits::One(); |
2126 | InferAlignment = false; |
2127 | } |
2128 | setSize(External.Size); |
2129 | return; |
2130 | } |
2131 | |
2132 | |
2133 | setSize(RoundedSize); |
2134 | |
2135 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
2136 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
2137 | |
2138 | if (getSizeInBits() > UnpaddedSize) { |
2139 | unsigned PadSize = getSizeInBits() - UnpaddedSize; |
2140 | bool InBits = true; |
2141 | if (PadSize % CharBitNum == 0) { |
2142 | PadSize = PadSize / CharBitNum; |
2143 | InBits = false; |
2144 | } |
2145 | Diag(RD->getLocation(), diag::warn_padded_struct_size) |
2146 | << Context.getTypeDeclType(RD) |
2147 | << PadSize |
2148 | << (InBits ? 1 : 0); |
2149 | } |
2150 | |
2151 | |
2152 | |
2153 | |
2154 | if (Packed && UnpackedAlignment <= Alignment && |
2155 | UnpackedSizeInBits == getSizeInBits() && !HasPackedField) |
2156 | Diag(D->getLocation(), diag::warn_unnecessary_packed) |
2157 | << Context.getTypeDeclType(RD); |
2158 | } |
2159 | } |
2160 | |
2161 | void ItaniumRecordLayoutBuilder::UpdateAlignment( |
2162 | CharUnits NewAlignment, CharUnits UnpackedNewAlignment, |
2163 | CharUnits PreferredNewAlignment) { |
2164 | |
2165 | |
2166 | if (IsMac68kAlign || (UseExternalLayout && !InferAlignment)) |
2167 | return; |
2168 | |
2169 | if (NewAlignment > Alignment) { |
2170 | assert(llvm::isPowerOf2_64(NewAlignment.getQuantity()) && |
2171 | "Alignment not a power of 2"); |
2172 | Alignment = NewAlignment; |
2173 | } |
2174 | |
2175 | if (UnpackedNewAlignment > UnpackedAlignment) { |
2176 | assert(llvm::isPowerOf2_64(UnpackedNewAlignment.getQuantity()) && |
2177 | "Alignment not a power of 2"); |
2178 | UnpackedAlignment = UnpackedNewAlignment; |
2179 | } |
2180 | |
2181 | if (PreferredNewAlignment > PreferredAlignment) { |
2182 | assert(llvm::isPowerOf2_64(PreferredNewAlignment.getQuantity()) && |
2183 | "Alignment not a power of 2"); |
2184 | PreferredAlignment = PreferredNewAlignment; |
2185 | } |
2186 | } |
2187 | |
2188 | uint64_t |
2189 | ItaniumRecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field, |
2190 | uint64_t ComputedOffset) { |
2191 | uint64_t ExternalFieldOffset = External.getExternalFieldOffset(Field); |
2192 | |
2193 | if (InferAlignment && ExternalFieldOffset < ComputedOffset) { |
2194 | |
2195 | |
2196 | Alignment = CharUnits::One(); |
2197 | PreferredAlignment = CharUnits::One(); |
2198 | InferAlignment = false; |
2199 | } |
2200 | |
2201 | |
2202 | return ExternalFieldOffset; |
2203 | } |
2204 | |
2205 | |
2206 | |
2207 | |
2208 | |
2209 | |
2210 | static unsigned getPaddingDiagFromTagKind(TagTypeKind Tag) { |
2211 | switch (Tag) { |
2212 | case TTK_Struct: return 0; |
2213 | case TTK_Interface: return 1; |
2214 | case TTK_Class: return 2; |
2215 | default: llvm_unreachable("Invalid tag kind for field padding diagnostic!"); |
2216 | } |
2217 | } |
2218 | |
2219 | void ItaniumRecordLayoutBuilder::CheckFieldPadding( |
2220 | uint64_t Offset, uint64_t UnpaddedOffset, uint64_t UnpackedOffset, |
2221 | unsigned UnpackedAlign, bool isPacked, const FieldDecl *D) { |
2222 | |
2223 | |
2224 | if (isa<ObjCIvarDecl>(D)) |
2225 | return; |
2226 | |
2227 | |
2228 | |
2229 | if (D->getLocation().isInvalid()) |
2230 | return; |
2231 | |
2232 | unsigned CharBitNum = Context.getTargetInfo().getCharWidth(); |
2233 | |
2234 | |
2235 | if (!IsUnion && Offset > UnpaddedOffset) { |
2236 | unsigned PadSize = Offset - UnpaddedOffset; |
2237 | bool InBits = true; |
2238 | if (PadSize % CharBitNum == 0) { |
2239 | PadSize = PadSize / CharBitNum; |
2240 | InBits = false; |
2241 | } |
2242 | if (D->getIdentifier()) |
2243 | Diag(D->getLocation(), diag::warn_padded_struct_field) |
2244 | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
2245 | << Context.getTypeDeclType(D->getParent()) |
2246 | << PadSize |
2247 | << (InBits ? 1 : 0) |
2248 | << D->getIdentifier(); |
2249 | else |
2250 | Diag(D->getLocation(), diag::warn_padded_struct_anon_field) |
2251 | << getPaddingDiagFromTagKind(D->getParent()->getTagKind()) |
2252 | << Context.getTypeDeclType(D->getParent()) |
2253 | << PadSize |
2254 | << (InBits ? 1 : 0); |
2255 | } |
2256 | if (isPacked && Offset != UnpackedOffset) { |
2257 | HasPackedField = true; |
2258 | } |
2259 | } |
2260 | |
2261 | static const CXXMethodDecl *computeKeyFunction(ASTContext &Context, |
2262 | const CXXRecordDecl *RD) { |
2263 | |
2264 | if (!RD->isPolymorphic()) |
2265 | return nullptr; |
2266 | |
2267 | |
2268 | |
2269 | |
2270 | if (!RD->isExternallyVisible()) |
2271 | return nullptr; |
2272 | |
2273 | |
2274 | |
2275 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
2276 | if (TSK == TSK_ImplicitInstantiation || |
2277 | TSK == TSK_ExplicitInstantiationDeclaration || |
2278 | TSK == TSK_ExplicitInstantiationDefinition) |
2279 | return nullptr; |
2280 | |
2281 | bool allowInlineFunctions = |
2282 | Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline(); |
2283 | |
2284 | for (const CXXMethodDecl *MD : RD->methods()) { |
2285 | if (!MD->isVirtual()) |
2286 | continue; |
2287 | |
2288 | if (MD->isPure()) |
2289 | continue; |
2290 | |
2291 | |
2292 | |
2293 | if (MD->isImplicit()) |
2294 | continue; |
2295 | |
2296 | if (MD->isInlineSpecified() || MD->isConstexpr()) |
2297 | continue; |
2298 | |
2299 | if (MD->hasInlineBody()) |
2300 | continue; |
2301 | |
2302 | |
2303 | if (!MD->isUserProvided()) |
2304 | continue; |
2305 | |
2306 | |
2307 | if (!allowInlineFunctions) { |
2308 | const FunctionDecl *Def; |
2309 | if (MD->hasBody(Def) && Def->isInlineSpecified()) |
2310 | continue; |
2311 | } |
2312 | |
2313 | if (Context.getLangOpts().CUDA) { |
2314 | |
2315 | |
2316 | |
2317 | if (Context.getLangOpts().CUDAIsDevice) { |
2318 | |
2319 | if (!MD->hasAttr<CUDADeviceAttr>()) |
2320 | continue; |
2321 | } else { |
2322 | |
2323 | if (!MD->hasAttr<CUDAHostAttr>() && MD->hasAttr<CUDADeviceAttr>()) |
2324 | continue; |
2325 | } |
2326 | } |
2327 | |
2328 | |
2329 | |
2330 | |
2331 | if (MD->hasAttr<DLLImportAttr>() && !RD->hasAttr<DLLImportAttr>() && |
2332 | !Context.getTargetInfo().hasPS4DLLImportExport()) |
2333 | return nullptr; |
2334 | |
2335 | |
2336 | return MD; |
2337 | } |
2338 | |
2339 | return nullptr; |
2340 | } |
2341 | |
2342 | DiagnosticBuilder ItaniumRecordLayoutBuilder::Diag(SourceLocation Loc, |
2343 | unsigned DiagID) { |
2344 | return Context.getDiagnostics().Report(Loc, DiagID); |
2345 | } |
2346 | |
2347 | |
2348 | |
2349 | |
2350 | static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) { |
2351 | switch (ABI.getTailPaddingUseRules()) { |
2352 | case TargetCXXABI::AlwaysUseTailPadding: |
2353 | return false; |
2354 | |
2355 | case TargetCXXABI::UseTailPaddingUnlessPOD03: |
2356 | |
2357 | |
2358 | |
2359 | |
2360 | |
2361 | |
2362 | |
2363 | |
2364 | |
2365 | |
2366 | |
2367 | |
2368 | |
2369 | |
2370 | |
2371 | |
2372 | |
2373 | |
2374 | return RD->isPOD(); |
2375 | |
2376 | case TargetCXXABI::UseTailPaddingUnlessPOD11: |
2377 | |
2378 | |
2379 | |
2380 | |
2381 | |
2382 | |
2383 | return RD->isTrivial() && RD->isCXX11StandardLayout(); |
2384 | } |
2385 | |
2386 | llvm_unreachable("bad tail-padding use kind"); |
2387 | } |
2388 | |
2389 | static bool isMsLayout(const ASTContext &Context) { |
2390 | return Context.getTargetInfo().getCXXABI().isMicrosoft(); |
2391 | } |
2392 | |
2393 | |
2394 | |
2395 | |
2396 | |
2397 | |
2398 | |
2399 | |
2400 | |
2401 | |
2402 | |
2403 | |
2404 | |
2405 | |
2406 | |
2407 | |
2408 | |
2409 | |
2410 | |
2411 | |
2412 | |
2413 | |
2414 | |
2415 | |
2416 | |
2417 | |
2418 | |
2419 | |
2420 | |
2421 | |
2422 | |
2423 | |
2424 | |
2425 | |
2426 | |
2427 | |
2428 | |
2429 | |
2430 | |
2431 | |
2432 | |
2433 | |
2434 | |
2435 | |
2436 | |
2437 | |
2438 | |
2439 | |
2440 | |
2441 | |
2442 | |
2443 | |
2444 | |
2445 | |
2446 | |
2447 | |
2448 | |
2449 | |
2450 | |
2451 | |
2452 | |
2453 | |
2454 | |
2455 | |
2456 | |
2457 | |
2458 | |
2459 | |
2460 | |
2461 | |
2462 | |
2463 | |
2464 | |
2465 | |
2466 | |
2467 | |
2468 | |
2469 | |
2470 | |
2471 | |
2472 | |
2473 | |
2474 | |
2475 | |
2476 | |
2477 | |
2478 | |
2479 | |
2480 | |
2481 | |
2482 | |
2483 | namespace { |
2484 | struct MicrosoftRecordLayoutBuilder { |
2485 | struct ElementInfo { |
2486 | CharUnits Size; |
2487 | CharUnits Alignment; |
2488 | }; |
2489 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy; |
2490 | MicrosoftRecordLayoutBuilder(const ASTContext &Context) : Context(Context) {} |
2491 | private: |
2492 | MicrosoftRecordLayoutBuilder(const MicrosoftRecordLayoutBuilder &) = delete; |
2493 | void operator=(const MicrosoftRecordLayoutBuilder &) = delete; |
2494 | public: |
2495 | void layout(const RecordDecl *RD); |
2496 | void cxxLayout(const CXXRecordDecl *RD); |
2497 | |
2498 | void initializeLayout(const RecordDecl *RD); |
2499 | |
2500 | |
2501 | |
2502 | void initializeCXXLayout(const CXXRecordDecl *RD); |
2503 | void layoutNonVirtualBases(const CXXRecordDecl *RD); |
2504 | void layoutNonVirtualBase(const CXXRecordDecl *RD, |
2505 | const CXXRecordDecl *BaseDecl, |
2506 | const ASTRecordLayout &BaseLayout, |
2507 | const ASTRecordLayout *&PreviousBaseLayout); |
2508 | void injectVFPtr(const CXXRecordDecl *RD); |
2509 | void injectVBPtr(const CXXRecordDecl *RD); |
2510 | |
2511 | |
2512 | void layoutFields(const RecordDecl *RD); |
2513 | void layoutField(const FieldDecl *FD); |
2514 | void layoutBitField(const FieldDecl *FD); |
2515 | |
2516 | |
2517 | void layoutZeroWidthBitField(const FieldDecl *FD); |
2518 | void layoutVirtualBases(const CXXRecordDecl *RD); |
2519 | void finalizeLayout(const RecordDecl *RD); |
2520 | |
2521 | |
2522 | ElementInfo getAdjustedElementInfo(const ASTRecordLayout &Layout); |
2523 | |
2524 | |
2525 | |
2526 | ElementInfo getAdjustedElementInfo(const FieldDecl *FD); |
2527 | |
2528 | void placeFieldAtOffset(CharUnits FieldOffset) { |
2529 | FieldOffsets.push_back(Context.toBits(FieldOffset)); |
2530 | } |
2531 | |
2532 | void placeFieldAtBitOffset(uint64_t FieldOffset) { |
2533 | FieldOffsets.push_back(FieldOffset); |
2534 | } |
2535 | |
2536 | void computeVtorDispSet( |
2537 | llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtorDispSet, |
2538 | const CXXRecordDecl *RD) const; |
2539 | const ASTContext &Context; |
2540 | |
2541 | CharUnits Size; |
2542 | |
2543 | CharUnits NonVirtualSize; |
2544 | |
2545 | CharUnits DataSize; |
2546 | |
2547 | CharUnits Alignment; |
2548 | |
2549 | CharUnits MaxFieldAlignment; |
2550 | |
2551 | |
2552 | CharUnits RequiredAlignment; |
2553 | |
2554 | |
2555 | |
2556 | CharUnits CurrentBitfieldSize; |
2557 | |
2558 | CharUnits VBPtrOffset; |
2559 | |
2560 | CharUnits MinEmptyStructSize; |
2561 | |
2562 | ElementInfo PointerInfo; |
2563 | |
2564 | const CXXRecordDecl *PrimaryBase; |
2565 | |
2566 | const CXXRecordDecl *SharedVBPtrBase; |
2567 | |
2568 | SmallVector<uint64_t, 16> FieldOffsets; |
2569 | |
2570 | BaseOffsetsMapTy Bases; |
2571 | |
2572 | ASTRecordLayout::VBaseOffsetsMapTy VBases; |
2573 | |
2574 | |
2575 | |
2576 | unsigned RemainingBitsInField; |
2577 | bool IsUnion : 1; |
2578 | |
2579 | |
2580 | bool LastFieldIsNonZeroWidthBitfield : 1; |
2581 | |
2582 | bool HasOwnVFPtr : 1; |
2583 | |
2584 | bool HasVBPtr : 1; |
2585 | |
2586 | |
2587 | |
2588 | bool EndsWithZeroSizedObject : 1; |
2589 | |
2590 | |
2591 | bool LeadsWithZeroSizedBase : 1; |
2592 | |
2593 | |
2594 | bool UseExternalLayout : 1; |
2595 | |
2596 | |
2597 | |
2598 | ExternalLayout External; |
2599 | }; |
2600 | } |
2601 | |
2602 | MicrosoftRecordLayoutBuilder::ElementInfo |
2603 | MicrosoftRecordLayoutBuilder::getAdjustedElementInfo( |
2604 | const ASTRecordLayout &Layout) { |
2605 | ElementInfo Info; |
2606 | Info.Alignment = Layout.getAlignment(); |
2607 | |
2608 | if (!MaxFieldAlignment.isZero()) |
2609 | Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment); |
2610 | |
2611 | EndsWithZeroSizedObject = Layout.endsWithZeroSizedObject(); |
2612 | |
2613 | |
2614 | |
2615 | Alignment = std::max(Alignment, Info.Alignment); |
2616 | RequiredAlignment = std::max(RequiredAlignment, Layout.getRequiredAlignment()); |
2617 | Info.Alignment = std::max(Info.Alignment, Layout.getRequiredAlignment()); |
2618 | Info.Size = Layout.getNonVirtualSize(); |
2619 | return Info; |
2620 | } |
2621 | |
2622 | MicrosoftRecordLayoutBuilder::ElementInfo |
2623 | MicrosoftRecordLayoutBuilder::getAdjustedElementInfo( |
2624 | const FieldDecl *FD) { |
2625 | |
2626 | |
2627 | auto TInfo = |
2628 | Context.getTypeInfoInChars(FD->getType()->getUnqualifiedDesugaredType()); |
2629 | ElementInfo Info{TInfo.Width, TInfo.Align}; |
2630 | |
2631 | CharUnits FieldRequiredAlignment = |
2632 | Context.toCharUnitsFromBits(FD->getMaxAlignment()); |
2633 | |
2634 | if (Context.isAlignmentRequired(FD->getType())) |
2635 | FieldRequiredAlignment = std::max( |
2636 | Context.getTypeAlignInChars(FD->getType()), FieldRequiredAlignment); |
2637 | |
2638 | if (FD->isBitField()) |
2639 | |
2640 | |
2641 | Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment); |
2642 | else { |
2643 | if (auto RT = |
2644 | FD->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) { |
2645 | auto const &Layout = Context.getASTRecordLayout(RT->getDecl()); |
2646 | EndsWithZeroSizedObject = Layout.endsWithZeroSizedObject(); |
2647 | FieldRequiredAlignment = std::max(FieldRequiredAlignment, |
2648 | Layout.getRequiredAlignment()); |
2649 | } |
2650 | |
2651 | RequiredAlignment = std::max(RequiredAlignment, FieldRequiredAlignment); |
2652 | } |
2653 | |
2654 | if (!MaxFieldAlignment.isZero()) |
2655 | Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment); |
2656 | if (FD->hasAttr<PackedAttr>()) |
2657 | Info.Alignment = CharUnits::One(); |
2658 | Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment); |
2659 | return Info; |
2660 | } |
2661 | |
2662 | void MicrosoftRecordLayoutBuilder::layout(const RecordDecl *RD) { |
2663 | |
2664 | MinEmptyStructSize = CharUnits::fromQuantity(4); |
2665 | initializeLayout(RD); |
2666 | layoutFields(RD); |
2667 | DataSize = Size = Size.alignTo(Alignment); |
2668 | RequiredAlignment = std::max( |
2669 | RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment())); |
2670 | finalizeLayout(RD); |
2671 | } |
2672 | |
2673 | void MicrosoftRecordLayoutBuilder::cxxLayout(const CXXRecordDecl *RD) { |
2674 | |
2675 | MinEmptyStructSize = CharUnits::One(); |
2676 | initializeLayout(RD); |
2677 | initializeCXXLayout(RD); |
2678 | layoutNonVirtualBases(RD); |
2679 | layoutFields(RD); |
2680 | injectVBPtr(RD); |
2681 | injectVFPtr(RD); |
2682 | if (HasOwnVFPtr || (HasVBPtr && !SharedVBPtrBase)) |
2683 | Alignment = std::max(Alignment, PointerInfo.Alignment); |
2684 | auto RoundingAlignment = Alignment; |
2685 | if (!MaxFieldAlignment.isZero()) |
2686 | RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment); |
2687 | if (!UseExternalLayout) |
2688 | Size = Size.alignTo(RoundingAlignment); |
2689 | NonVirtualSize = Size; |
2690 | RequiredAlignment = std::max( |
2691 | RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment())); |
2692 | layoutVirtualBases(RD); |
2693 | finalizeLayout(RD); |
2694 | } |
2695 | |
2696 | void MicrosoftRecordLayoutBuilder::initializeLayout(const RecordDecl *RD) { |
2697 | IsUnion = RD->isUnion(); |
2698 | Size = CharUnits::Zero(); |
2699 | Alignment = CharUnits::One(); |
2700 | |
2701 | |
2702 | |
2703 | RequiredAlignment = Context.getTargetInfo().getTriple().isArch64Bit() |
2704 | ? CharUnits::One() |
2705 | : CharUnits::Zero(); |
2706 | |
2707 | MaxFieldAlignment = CharUnits::Zero(); |
2708 | |
2709 | if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) |
2710 | MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment); |
2711 | |
2712 | |
2713 | if (const MaxFieldAlignmentAttr *MFAA = RD->getAttr<MaxFieldAlignmentAttr>()){ |
2714 | unsigned PackedAlignment = MFAA->getAlignment(); |
2715 | if (PackedAlignment <= Context.getTargetInfo().getPointerWidth(0)) |
2716 | MaxFieldAlignment = Context.toCharUnitsFromBits(PackedAlignment); |
2717 | } |
2718 | |
2719 | if (RD->hasAttr<PackedAttr>()) |
2720 | MaxFieldAlignment = CharUnits::One(); |
2721 | |
2722 | |
2723 | UseExternalLayout = false; |
2724 | if (ExternalASTSource *Source = Context.getExternalSource()) |
2725 | UseExternalLayout = Source->layoutRecordType( |
2726 | RD, External.Size, External.Align, External.FieldOffsets, |
2727 | External.BaseOffsets, External.VirtualBaseOffsets); |
2728 | } |
2729 | |
2730 | void |
2731 | MicrosoftRecordLayoutBuilder::initializeCXXLayout(const CXXRecordDecl *RD) { |
2732 | EndsWithZeroSizedObject = false; |
2733 | LeadsWithZeroSizedBase = false; |
2734 | HasOwnVFPtr = false; |
2735 | HasVBPtr = false; |
2736 | PrimaryBase = nullptr; |
2737 | SharedVBPtrBase = nullptr; |
2738 | |
2739 | |
2740 | PointerInfo.Size = |
2741 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
2742 | PointerInfo.Alignment = |
2743 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0)); |
2744 | |
2745 | if (!MaxFieldAlignment.isZero()) |
2746 | PointerInfo.Alignment = std::min(PointerInfo.Alignment, MaxFieldAlignment); |
2747 | } |
2748 | |
2749 | void |
2750 | MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) { |
2751 | |
2752 | |
2753 | |
2754 | |
2755 | |
2756 | |
2757 | const ASTRecordLayout *PreviousBaseLayout = nullptr; |
2758 | bool HasPolymorphicBaseClass = false; |
2759 | |
2760 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
2761 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
2762 | HasPolymorphicBaseClass |= BaseDecl->isPolymorphic(); |
2763 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
2764 | |
2765 | if (Base.isVirtual()) { |
2766 | HasVBPtr = true; |
2767 | continue; |
2768 | } |
2769 | |
2770 | if (!SharedVBPtrBase && BaseLayout.hasVBPtr()) { |
2771 | SharedVBPtrBase = BaseDecl; |
2772 | HasVBPtr = true; |
2773 | } |
2774 | |
2775 | if (!BaseLayout.hasExtendableVFPtr()) |
2776 | continue; |
2777 | |
2778 | if (!PrimaryBase) { |
2779 | PrimaryBase = BaseDecl; |
2780 | LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase(); |
2781 | } |
2782 | |
2783 | layoutNonVirtualBase(RD, BaseDecl, BaseLayout, PreviousBaseLayout); |
2784 | } |
2785 | |
2786 | if (RD->isPolymorphic()) { |
2787 | if (!HasPolymorphicBaseClass) |
2788 | |
2789 | |
2790 | HasOwnVFPtr = true; |
2791 | else if (!PrimaryBase) { |
2792 | |
2793 | |
2794 | for (CXXMethodDecl *M : RD->methods()) { |
2795 | if (MicrosoftVTableContext::hasVtableSlot(M) && |
2796 | M->size_overridden_methods() == 0) { |
2797 | HasOwnVFPtr = true; |
2798 | break; |
2799 | } |
2800 | } |
2801 | } |
2802 | } |
2803 | |
2804 | |
2805 | bool CheckLeadingLayout = !PrimaryBase; |
2806 | |
2807 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
2808 | if (Base.isVirtual()) |
2809 | continue; |
2810 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
2811 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
2812 | |
2813 | if (BaseLayout.hasExtendableVFPtr()) { |
2814 | VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize(); |
2815 | continue; |
2816 | } |
2817 | |
2818 | |
2819 | if (CheckLeadingLayout) { |
2820 | CheckLeadingLayout = false; |
2821 | LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase(); |
2822 | } |
2823 | |
2824 | layoutNonVirtualBase(RD, BaseDecl, BaseLayout, PreviousBaseLayout); |
2825 | VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize(); |
2826 | } |
2827 | |
2828 | if (!HasVBPtr) |
2829 | VBPtrOffset = CharUnits::fromQuantity(-1); |
2830 | else if (SharedVBPtrBase) { |
2831 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(SharedVBPtrBase); |
2832 | VBPtrOffset = Bases[SharedVBPtrBase] + Layout.getVBPtrOffset(); |
2833 | } |
2834 | } |
2835 | |
2836 | static bool recordUsesEBO(const RecordDecl *RD) { |
2837 | if (!isa<CXXRecordDecl>(RD)) |
2838 | return false; |
2839 | if (RD->hasAttr<EmptyBasesAttr>()) |
2840 | return true; |
2841 | if (auto *LVA = RD->getAttr<LayoutVersionAttr>()) |
2842 | |
2843 | if (LVA->getVersion() <= LangOptions::MSVC2015) |
2844 | return false; |
2845 | |
2846 | |
2847 | |
2848 | return false; |
2849 | } |
2850 | |
2851 | void MicrosoftRecordLayoutBuilder::layoutNonVirtualBase( |
2852 | const CXXRecordDecl *RD, |
2853 | const CXXRecordDecl *BaseDecl, |
2854 | const ASTRecordLayout &BaseLayout, |
2855 | const ASTRecordLayout *&PreviousBaseLayout) { |
2856 | |
2857 | |
2858 | |
2859 | bool MDCUsesEBO = recordUsesEBO(RD); |
2860 | if (PreviousBaseLayout && PreviousBaseLayout->endsWithZeroSizedObject() && |
2861 | BaseLayout.leadsWithZeroSizedBase() && !MDCUsesEBO) |
2862 | Size++; |
2863 | ElementInfo Info = getAdjustedElementInfo(BaseLayout); |
2864 | CharUnits BaseOffset; |
2865 | |
2866 | |
2867 | bool FoundBase = false; |
2868 | if (UseExternalLayout) { |
2869 | FoundBase = External.getExternalNVBaseOffset(BaseDecl, BaseOffset); |
2870 | if (FoundBase) { |
2871 | assert(BaseOffset >= Size && "base offset already allocated"); |
2872 | Size = BaseOffset; |
2873 | } |
2874 | } |
2875 | |
2876 | if (!FoundBase) { |
2877 | if (MDCUsesEBO && BaseDecl->isEmpty()) { |
2878 | assert(BaseLayout.getNonVirtualSize() == CharUnits::Zero()); |
2879 | BaseOffset = CharUnits::Zero(); |
2880 | } else { |
2881 | |
2882 | BaseOffset = Size = Size.alignTo(Info.Alignment); |
2883 | } |
2884 | } |
2885 | Bases.insert(std::make_pair(BaseDecl, BaseOffset)); |
2886 | Size += BaseLayout.getNonVirtualSize(); |
2887 | PreviousBaseLayout = &BaseLayout; |
2888 | } |
2889 | |
2890 | void MicrosoftRecordLayoutBuilder::layoutFields(const RecordDecl *RD) { |
2891 | LastFieldIsNonZeroWidthBitfield = false; |
2892 | for (const FieldDecl *Field : RD->fields()) |
2893 | layoutField(Field); |
2894 | } |
2895 | |
2896 | void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) { |
2897 | if (FD->isBitField()) { |
2898 | layoutBitField(FD); |
2899 | return; |
2900 | } |
2901 | LastFieldIsNonZeroWidthBitfield = false; |
2902 | ElementInfo Info = getAdjustedElementInfo(FD); |
2903 | Alignment = std::max(Alignment, Info.Alignment); |
2904 | CharUnits FieldOffset; |
2905 | if (UseExternalLayout) |
2906 | FieldOffset = |
2907 | Context.toCharUnitsFromBits(External.getExternalFieldOffset(FD)); |
2908 | else if (IsUnion) |
2909 | FieldOffset = CharUnits::Zero(); |
2910 | else |
2911 | FieldOffset = Size.alignTo(Info.Alignment); |
2912 | placeFieldAtOffset(FieldOffset); |
2913 | Size = std::max(Size, FieldOffset + Info.Size); |
2914 | } |
2915 | |
2916 | void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) { |
2917 | unsigned Width = FD->getBitWidthValue(Context); |
2918 | if (Width == 0) { |
2919 | layoutZeroWidthBitField(FD); |
2920 | return; |
2921 | } |
2922 | ElementInfo Info = getAdjustedElementInfo(FD); |
2923 | |
2924 | |
2925 | if (Width > Context.toBits(Info.Size)) |
2926 | Width = Context.toBits(Info.Size); |
2927 | |
2928 | |
2929 | |
2930 | if (!UseExternalLayout && !IsUnion && LastFieldIsNonZeroWidthBitfield && |
2931 | CurrentBitfieldSize == Info.Size && Width <= RemainingBitsInField) { |
2932 | placeFieldAtBitOffset(Context.toBits(Size) - RemainingBitsInField); |
2933 | RemainingBitsInField -= Width; |
2934 | return; |
2935 | } |
2936 | LastFieldIsNonZeroWidthBitfield = true; |
2937 | CurrentBitfieldSize = Info.Size; |
2938 | if (UseExternalLayout) { |
2939 | auto FieldBitOffset = External.getExternalFieldOffset(FD); |
2940 | placeFieldAtBitOffset(FieldBitOffset); |
2941 | auto NewSize = Context.toCharUnitsFromBits( |
2942 | llvm::alignDown(FieldBitOffset, Context.toBits(Info.Alignment)) + |
2943 | Context.toBits(Info.Size)); |
2944 | Size = std::max(Size, NewSize); |
2945 | Alignment = std::max(Alignment, Info.Alignment); |
2946 | } else if (IsUnion) { |
2947 | placeFieldAtOffset(CharUnits::Zero()); |
2948 | Size = std::max(Size, Info.Size); |
2949 | |
2950 | } else { |
2951 | |
2952 | CharUnits FieldOffset = Size.alignTo(Info.Alignment); |
2953 | placeFieldAtOffset(FieldOffset); |
2954 | Size = FieldOffset + Info.Size; |
2955 | Alignment = std::max(Alignment, Info.Alignment); |
2956 | RemainingBitsInField = Context.toBits(Info.Size) - Width; |
2957 | } |
2958 | } |
2959 | |
2960 | void |
2961 | MicrosoftRecordLayoutBuilder::layoutZeroWidthBitField(const FieldDecl *FD) { |
2962 | |
2963 | |
2964 | if (!LastFieldIsNonZeroWidthBitfield) { |
2965 | placeFieldAtOffset(IsUnion ? CharUnits::Zero() : Size); |
2966 | |
2967 | |
2968 | return; |
2969 | } |
2970 | LastFieldIsNonZeroWidthBitfield = false; |
2971 | ElementInfo Info = getAdjustedElementInfo(FD); |
2972 | if (IsUnion) { |
2973 | placeFieldAtOffset(CharUnits::Zero()); |
2974 | Size = std::max(Size, Info.Size); |
2975 | |
2976 | } else { |
2977 | |
2978 | CharUnits FieldOffset = Size.alignTo(Info.Alignment); |
2979 | placeFieldAtOffset(FieldOffset); |
2980 | Size = FieldOffset; |
2981 | Alignment = std::max(Alignment, Info.Alignment); |
2982 | } |
2983 | } |
2984 | |
2985 | void MicrosoftRecordLayoutBuilder::injectVBPtr(const CXXRecordDecl *RD) { |
2986 | if (!HasVBPtr || SharedVBPtrBase) |
2987 | return; |
2988 | |
2989 | CharUnits InjectionSite = VBPtrOffset; |
2990 | |
2991 | VBPtrOffset = VBPtrOffset.alignTo(PointerInfo.Alignment); |
2992 | |
2993 | CharUnits FieldStart = VBPtrOffset + PointerInfo.Size; |
2994 | |
2995 | |
2996 | if (UseExternalLayout) { |
2997 | |
2998 | |
2999 | if (Size < FieldStart) |
3000 | Size = FieldStart; |
3001 | return; |
3002 | } |
3003 | |
3004 | |
3005 | CharUnits Offset = (FieldStart - InjectionSite) |
3006 | .alignTo(std::max(RequiredAlignment, Alignment)); |
3007 | Size += Offset; |
3008 | for (uint64_t &FieldOffset : FieldOffsets) |
3009 | FieldOffset += Context.toBits(Offset); |
3010 | for (BaseOffsetsMapTy::value_type &Base : Bases) |
3011 | if (Base.second >= InjectionSite) |
3012 | Base.second += Offset; |
3013 | } |
3014 | |
3015 | void MicrosoftRecordLayoutBuilder::injectVFPtr(const CXXRecordDecl *RD) { |
3016 | if (!HasOwnVFPtr) |
3017 | return; |
3018 | |
3019 | |
3020 | CharUnits Offset = |
3021 | PointerInfo.Size.alignTo(std::max(RequiredAlignment, Alignment)); |
3022 | |
3023 | |
3024 | if (HasVBPtr) |
3025 | VBPtrOffset += Offset; |
3026 | |
3027 | if (UseExternalLayout) { |
3028 | |
3029 | |
3030 | |
3031 | if (FieldOffsets.empty() && Bases.empty()) |
3032 | Size += Offset; |
3033 | return; |
3034 | } |
3035 | |
3036 | Size += Offset; |
3037 | |
3038 | |
3039 | |
3040 | for (uint64_t &FieldOffset : FieldOffsets) |
3041 | FieldOffset += Context.toBits(Offset); |
3042 | for (BaseOffsetsMapTy::value_type &Base : Bases) |
3043 | Base.second += Offset; |
3044 | } |
3045 | |
3046 | void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) { |
3047 | if (!HasVBPtr) |
3048 | return; |
3049 | |
3050 | CharUnits VtorDispSize = CharUnits::fromQuantity(4); |
3051 | CharUnits VtorDispAlignment = VtorDispSize; |
3052 | |
3053 | if (!MaxFieldAlignment.isZero()) |
3054 | VtorDispAlignment = std::min(VtorDispAlignment, MaxFieldAlignment); |
3055 | |
3056 | |
3057 | |
3058 | for (const CXXBaseSpecifier &VBase : RD->vbases()) { |
3059 | const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl(); |
3060 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
3061 | RequiredAlignment = |
3062 | std::max(RequiredAlignment, BaseLayout.getRequiredAlignment()); |
3063 | } |
3064 | VtorDispAlignment = std::max(VtorDispAlignment, RequiredAlignment); |
3065 | |
3066 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtorDispSet; |
3067 | computeVtorDispSet(HasVtorDispSet, RD); |
3068 | |
3069 | const ASTRecordLayout *PreviousBaseLayout = nullptr; |
3070 | for (const CXXBaseSpecifier &VBase : RD->vbases()) { |
3071 | const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl(); |
3072 | const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl); |
3073 | bool HasVtordisp = HasVtorDispSet.count(BaseDecl) > 0; |
3074 | |
3075 | |
3076 | |
3077 | |
3078 | |
3079 | if ((PreviousBaseLayout && PreviousBaseLayout->endsWithZeroSizedObject() && |
3080 | BaseLayout.leadsWithZeroSizedBase() && !recordUsesEBO(RD)) || |
3081 | HasVtordisp) { |
3082 | Size = Size.alignTo(VtorDispAlignment) + VtorDispSize; |
3083 | Alignment = std::max(VtorDispAlignment, Alignment); |
3084 | } |
3085 | |
3086 | ElementInfo Info = getAdjustedElementInfo(BaseLayout); |
3087 | CharUnits BaseOffset; |
3088 | |
3089 | |
3090 | if (UseExternalLayout) { |
3091 | if (!External.getExternalVBaseOffset(BaseDecl, BaseOffset)) |
3092 | BaseOffset = Size; |
3093 | } else |
3094 | BaseOffset = Size.alignTo(Info.Alignment); |
3095 | |
3096 | assert(BaseOffset >= Size && "base offset already allocated"); |
3097 | |
3098 | VBases.insert(std::make_pair(BaseDecl, |
3099 | ASTRecordLayout::VBaseInfo(BaseOffset, HasVtordisp))); |
3100 | Size = BaseOffset + BaseLayout.getNonVirtualSize(); |
3101 | PreviousBaseLayout = &BaseLayout; |
3102 | } |
3103 | } |
3104 | |
3105 | void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) { |
3106 | |
3107 | |
3108 | DataSize = Size; |
3109 | if (!RequiredAlignment.isZero()) { |
3110 | Alignment = std::max(Alignment, RequiredAlignment); |
3111 | auto RoundingAlignment = Alignment; |
3112 | if (!MaxFieldAlignment.isZero()) |
3113 | RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment); |
3114 | RoundingAlignment = std::max(RoundingAlignment, RequiredAlignment); |
3115 | Size = Size.alignTo(RoundingAlignment); |
3116 | } |
3117 | if (Size.isZero()) { |
3118 | if (!recordUsesEBO(RD) || !cast<CXXRecordDecl>(RD)->isEmpty()) { |
3119 | EndsWithZeroSizedObject = true; |
3120 | LeadsWithZeroSizedBase = true; |
3121 | } |
3122 | |
3123 | |
3124 | if (RequiredAlignment >= MinEmptyStructSize) |
3125 | Size = Alignment; |
3126 | else |
3127 | Size = MinEmptyStructSize; |
3128 | } |
3129 | |
3130 | if (UseExternalLayout) { |
3131 | Size = Context.toCharUnitsFromBits(External.Size); |
3132 | if (External.Align) |
3133 | Alignment = Context.toCharUnitsFromBits(External.Align); |
3134 | } |
3135 | } |
3136 | |
3137 | |
3138 | |
3139 | static bool |
3140 | RequiresVtordisp(const llvm::SmallPtrSetImpl<const CXXRecordDecl *> & |
3141 | BasesWithOverriddenMethods, |
3142 | const CXXRecordDecl *RD) { |
3143 | if (BasesWithOverriddenMethods.count(RD)) |
3144 | return true; |
3145 | |
3146 | |
3147 | for (const CXXBaseSpecifier &Base : RD->bases()) |
3148 | if (!Base.isVirtual() && |
3149 | RequiresVtordisp(BasesWithOverriddenMethods, |
3150 | Base.getType()->getAsCXXRecordDecl())) |
3151 | return true; |
3152 | return false; |
3153 | } |
3154 | |
3155 | void MicrosoftRecordLayoutBuilder::computeVtorDispSet( |
3156 | llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtordispSet, |
3157 | const CXXRecordDecl *RD) const { |
3158 | |
3159 | |
3160 | if (RD->getMSVtorDispMode() == MSVtorDispMode::ForVFTable) { |
3161 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
3162 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
3163 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
3164 | if (Layout.hasExtendableVFPtr()) |
3165 | HasVtordispSet.insert(BaseDecl); |
3166 | } |
3167 | return; |
3168 | } |
3169 | |
3170 | |
3171 | |
3172 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
3173 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
3174 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl); |
3175 | for (const auto &bi : Layout.getVBaseOffsetsMap()) |
3176 | if (bi.second.hasVtorDisp()) |
3177 | HasVtordispSet.insert(bi.first); |
3178 | } |
3179 | |
3180 | |
3181 | |
3182 | if ((!RD->hasUserDeclaredConstructor() && !RD->hasUserDeclaredDestructor()) || |
3183 | RD->getMSVtorDispMode() == MSVtorDispMode::Never) |
3184 | return; |
3185 | |
3186 | |
3187 | |
3188 | assert(RD->getMSVtorDispMode() == MSVtorDispMode::ForVBaseOverride); |
3189 | |
3190 | |
3191 | |
3192 | |
3193 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> Work; |
3194 | llvm::SmallPtrSet<const CXXRecordDecl *, 2> BasesWithOverriddenMethods; |
3195 | |
3196 | for (const CXXMethodDecl *MD : RD->methods()) |
3197 | if (MicrosoftVTableContext::hasVtableSlot(MD) && |
3198 | !isa<CXXDestructorDecl>(MD) && !MD->isPure()) |
3199 | Work.insert(MD); |
3200 | while (!Work.empty()) { |
3201 | const CXXMethodDecl *MD = *Work.begin(); |
3202 | auto MethodRange = MD->overridden_methods(); |
3203 | |
3204 | if (MethodRange.begin() == MethodRange.end()) |
3205 | BasesWithOverriddenMethods.insert(MD->getParent()); |
3206 | else |
3207 | Work.insert(MethodRange.begin(), MethodRange.end()); |
3208 | |
3209 | Work.erase(MD); |
3210 | } |
3211 | |
3212 | |
3213 | for (const CXXBaseSpecifier &Base : RD->vbases()) { |
3214 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
3215 | if (!HasVtordispSet.count(BaseDecl) && |
3216 | RequiresVtordisp(BasesWithOverriddenMethods, BaseDecl)) |
3217 | HasVtordispSet.insert(BaseDecl); |
3218 | } |
3219 | } |
3220 | |
3221 | |
3222 | |
3223 | |
3224 | const ASTRecordLayout & |
3225 | ASTContext::getASTRecordLayout(const RecordDecl *D) const { |
3226 | |
3227 | |
3228 | |
3229 | |
3230 | |
3231 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
| 4 | | Assuming the condition is false | |
|
3232 | getExternalSource()->CompleteType(const_cast<RecordDecl*>(D)); |
3233 | |
3234 | D = D->getDefinition(); |
3235 | assert(D && "Cannot get layout of forward declarations!"); |
3236 | assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!"); |
3237 | assert(D->isCompleteDefinition() && "Cannot layout type before complete!"); |
3238 | |
3239 | |
3240 | |
3241 | |
3242 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
3243 | if (Entry) return *Entry; |
| 5 | | Assuming 'Entry' is null | |
|
| |
3244 | |
3245 | const ASTRecordLayout *NewEntry = nullptr; |
3246 | |
3247 | if (isMsLayout(*this)) { |
| |
3248 | MicrosoftRecordLayoutBuilder Builder(*this); |
3249 | if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
3250 | Builder.cxxLayout(RD); |
3251 | NewEntry = new (*this) ASTRecordLayout( |
3252 | *this, Builder.Size, Builder.Alignment, Builder.Alignment, |
3253 | Builder.Alignment, Builder.RequiredAlignment, Builder.HasOwnVFPtr, |
3254 | Builder.HasOwnVFPtr || Builder.PrimaryBase, Builder.VBPtrOffset, |
3255 | Builder.DataSize, Builder.FieldOffsets, Builder.NonVirtualSize, |
3256 | Builder.Alignment, Builder.Alignment, CharUnits::Zero(), |
3257 | Builder.PrimaryBase, false, Builder.SharedVBPtrBase, |
3258 | Builder.EndsWithZeroSizedObject, Builder.LeadsWithZeroSizedBase, |
3259 | Builder.Bases, Builder.VBases); |
3260 | } else { |
3261 | Builder.layout(D); |
3262 | NewEntry = new (*this) ASTRecordLayout( |
3263 | *this, Builder.Size, Builder.Alignment, Builder.Alignment, |
3264 | Builder.Alignment, Builder.RequiredAlignment, Builder.Size, |
3265 | Builder.FieldOffsets); |
3266 | } |
3267 | } else { |
3268 | if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 8 | | Assuming 'D' is a 'CXXRecordDecl' | |
|
| |
3269 | EmptySubobjectMap EmptySubobjects(*this, RD); |
3270 | ItaniumRecordLayoutBuilder Builder(*this, &EmptySubobjects); |
3271 | Builder.Layout(RD); |
| 10 | | Calling 'ItaniumRecordLayoutBuilder::Layout' | |
|
3272 | |
3273 | |
3274 | |
3275 | |
3276 | bool skipTailPadding = |
3277 | mustSkipTailPadding(getTargetInfo().getCXXABI(), RD); |
3278 | |
3279 | |
3280 | CharUnits DataSize = |
3281 | skipTailPadding ? Builder.getSize() : Builder.getDataSize(); |
3282 | CharUnits NonVirtualSize = |
3283 | skipTailPadding ? DataSize : Builder.NonVirtualSize; |
3284 | NewEntry = new (*this) ASTRecordLayout( |
3285 | *this, Builder.getSize(), Builder.Alignment, |
3286 | Builder.PreferredAlignment, Builder.UnadjustedAlignment, |
3287 | |
3288 | Builder.Alignment, Builder.HasOwnVFPtr, RD->isDynamicClass(), |
3289 | CharUnits::fromQuantity(-1), DataSize, Builder.FieldOffsets, |
3290 | NonVirtualSize, Builder.NonVirtualAlignment, |
3291 | Builder.PreferredNVAlignment, |
3292 | EmptySubobjects.SizeOfLargestEmptySubobject, Builder.PrimaryBase, |
3293 | Builder.PrimaryBaseIsVirtual, nullptr, false, false, Builder.Bases, |
3294 | Builder.VBases); |
3295 | } else { |
3296 | ItaniumRecordLayoutBuilder Builder(*this, nullptr); |
3297 | Builder.Layout(D); |
3298 | |
3299 | NewEntry = new (*this) ASTRecordLayout( |
3300 | *this, Builder.getSize(), Builder.Alignment, |
3301 | Builder.PreferredAlignment, Builder.UnadjustedAlignment, |
3302 | |
3303 | Builder.Alignment, Builder.getSize(), Builder.FieldOffsets); |
3304 | } |
3305 | } |
3306 | |
3307 | ASTRecordLayouts[D] = NewEntry; |
3308 | |
3309 | if (getLangOpts().DumpRecordLayouts) { |
3310 | llvm::outs() << "\n*** Dumping AST Record Layout\n"; |
3311 | DumpRecordLayout(D, llvm::outs(), getLangOpts().DumpRecordLayoutsSimple); |
3312 | } |
3313 | |
3314 | return *NewEntry; |
3315 | } |
3316 | |
3317 | const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) { |
3318 | if (!getTargetInfo().getCXXABI().hasKeyFunctions()) |
3319 | return nullptr; |
3320 | |
3321 | assert(RD->getDefinition() && "Cannot get key function for forward decl!"); |
3322 | RD = RD->getDefinition(); |
3323 | |
3324 | |
3325 | |
3326 | |
3327 | |
3328 | |
3329 | LazyDeclPtr Entry = KeyFunctions[RD]; |
3330 | const Decl *Result = |
3331 | Entry ? Entry.get(getExternalSource()) : computeKeyFunction(*this, RD); |
3332 | |
3333 | |
3334 | if (Entry.isOffset() || Entry.isValid() != bool(Result)) |
3335 | KeyFunctions[RD] = const_cast<Decl*>(Result); |
3336 | |
3337 | return cast_or_null<CXXMethodDecl>(Result); |
3338 | } |
3339 | |
3340 | void ASTContext::setNonKeyFunction(const CXXMethodDecl *Method) { |
3341 | assert(Method == Method->getFirstDecl() && |
3342 | "not working with method declaration from class definition"); |
3343 | |
3344 | |
3345 | |
3346 | |
3347 | const auto &Map = KeyFunctions; |
3348 | auto I = Map.find(Method->getParent()); |
3349 | |
3350 | |
3351 | if (I == Map.end()) return; |
3352 | |
3353 | |
3354 | |
3355 | |
3356 | LazyDeclPtr Ptr = I->second; |
3357 | if (Ptr.get(getExternalSource()) == Method) { |
3358 | |
3359 | KeyFunctions.erase(Method->getParent()); |
3360 | } |
3361 | } |
3362 | |
3363 | static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) { |
3364 | const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent()); |
3365 | return Layout.getFieldOffset(FD->getFieldIndex()); |
3366 | } |
3367 | |
3368 | uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const { |
3369 | uint64_t OffsetInBits; |
3370 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) { |
3371 | OffsetInBits = ::getFieldOffset(*this, FD); |
3372 | } else { |
3373 | const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD); |
3374 | |
3375 | OffsetInBits = 0; |
3376 | for (const NamedDecl *ND : IFD->chain()) |
3377 | OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(ND)); |
3378 | } |
3379 | |
3380 | return OffsetInBits; |
3381 | } |
3382 | |
3383 | uint64_t ASTContext::lookupFieldBitOffset(const ObjCInterfaceDecl *OID, |
3384 | const ObjCImplementationDecl *ID, |
3385 | const ObjCIvarDecl *Ivar) const { |
3386 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
3387 | |
3388 | |
3389 | |
3390 | |
3391 | |
3392 | |
3393 | |
3394 | const ASTRecordLayout *RL; |
3395 | if (ID && declaresSameEntity(ID->getClassInterface(), Container)) |
3396 | RL = &getASTObjCImplementationLayout(ID); |
3397 | else |
3398 | RL = &getASTObjCInterfaceLayout(Container); |
3399 | |
3400 | |
3401 | |
3402 | |
3403 | |
3404 | |
3405 | unsigned Index = 0; |
3406 | |
3407 | for (const ObjCIvarDecl *IVD = Container->all_declared_ivar_begin(); |
3408 | IVD; IVD = IVD->getNextIvar()) { |
3409 | if (Ivar == IVD) |
3410 | break; |
3411 | ++Index; |
3412 | } |
3413 | assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!"); |
3414 | |
3415 | return RL->getFieldOffset(Index); |
3416 | } |
3417 | |
3418 | |
3419 | |
3420 | |
3421 | |
3422 | |
3423 | const ASTRecordLayout & |
3424 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
3425 | const ObjCImplementationDecl *Impl) const { |
3426 | |
3427 | if (D->hasExternalLexicalStorage() && !D->getDefinition()) |
3428 | getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D)); |
3429 | D = D->getDefinition(); |
3430 | assert(D && !D->isInvalidDecl() && D->isThisDeclarationADefinition() && |
3431 | "Invalid interface decl!"); |
3432 | |
3433 | |
3434 | const ObjCContainerDecl *Key = |
3435 | Impl ? (const ObjCContainerDecl*) Impl : (const ObjCContainerDecl*) D; |
3436 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
3437 | return *Entry; |
3438 | |
3439 | |
3440 | if (Impl) { |
3441 | unsigned SynthCount = CountNonClassIvars(D); |
3442 | |
3443 | |
3444 | |
3445 | |
3446 | if (SynthCount == 0) |
3447 | return getObjCLayout(D, nullptr); |
3448 | } |
3449 | |
3450 | ItaniumRecordLayoutBuilder Builder(*this, nullptr); |
3451 | Builder.Layout(D); |
3452 | |
3453 | const ASTRecordLayout *NewEntry = new (*this) ASTRecordLayout( |
3454 | *this, Builder.getSize(), Builder.Alignment, Builder.PreferredAlignment, |
3455 | Builder.UnadjustedAlignment, |
3456 | |
3457 | Builder.Alignment, Builder.getDataSize(), Builder.FieldOffsets); |
3458 | |
3459 | ObjCLayouts[Key] = NewEntry; |
3460 | |
3461 | return *NewEntry; |
3462 | } |
3463 | |
3464 | static void PrintOffset(raw_ostream &OS, |
3465 | CharUnits Offset, unsigned IndentLevel) { |
3466 | OS << llvm::format("%10" PRId64 " | ", (int64_t)Offset.getQuantity()); |
3467 | OS.indent(IndentLevel * 2); |
3468 | } |
3469 | |
3470 | static void PrintBitFieldOffset(raw_ostream &OS, CharUnits Offset, |
3471 | unsigned Begin, unsigned Width, |
3472 | unsigned IndentLevel) { |
3473 | llvm::SmallString<10> Buffer; |
3474 | { |
3475 | llvm::raw_svector_ostream BufferOS(Buffer); |
3476 | BufferOS << Offset.getQuantity() << ':'; |
3477 | if (Width == 0) { |
3478 | BufferOS << '-'; |
3479 | } else { |
3480 | BufferOS << Begin << '-' << (Begin + Width - 1); |
3481 | } |
3482 | } |
3483 | |
3484 | OS << llvm::right_justify(Buffer, 10) << " | "; |
3485 | OS.indent(IndentLevel * 2); |
3486 | } |
3487 | |
3488 | static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) { |
3489 | OS << " | "; |
3490 | OS.indent(IndentLevel * 2); |
3491 | } |
3492 | |
3493 | static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD, |
3494 | const ASTContext &C, |
3495 | CharUnits Offset, |
3496 | unsigned IndentLevel, |
3497 | const char* Description, |
3498 | bool PrintSizeInfo, |
3499 | bool IncludeVirtualBases) { |
3500 | const ASTRecordLayout &Layout = C.getASTRecordLayout(RD); |
3501 | auto CXXRD = dyn_cast<CXXRecordDecl>(RD); |
3502 | |
3503 | PrintOffset(OS, Offset, IndentLevel); |
3504 | OS << C.getTypeDeclType(const_cast<RecordDecl*>(RD)).getAsString(); |
3505 | if (Description) |
3506 | OS << ' ' << Description; |
3507 | if (CXXRD && CXXRD->isEmpty()) |
3508 | OS << " (empty)"; |
3509 | OS << '\n'; |
3510 | |
3511 | IndentLevel++; |
3512 | |
3513 | |
3514 | if (CXXRD) { |
3515 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
3516 | bool HasOwnVFPtr = Layout.hasOwnVFPtr(); |
3517 | bool HasOwnVBPtr = Layout.hasOwnVBPtr(); |
3518 | |
3519 | |
3520 | if (CXXRD->isDynamicClass() && !PrimaryBase && !isMsLayout(C)) { |
3521 | PrintOffset(OS, Offset, IndentLevel); |
3522 | OS << '(' << *RD << " vtable pointer)\n"; |
3523 | } else if (HasOwnVFPtr) { |
3524 | PrintOffset(OS, Offset, IndentLevel); |
3525 | |
3526 | OS << '(' << *RD << " vftable pointer)\n"; |
3527 | } |
3528 | |
3529 | |
3530 | SmallVector<const CXXRecordDecl *, 4> Bases; |
3531 | for (const CXXBaseSpecifier &Base : CXXRD->bases()) { |
3532 | assert(!Base.getType()->isDependentType() && |
3533 | "Cannot layout class with dependent bases."); |
3534 | if (!Base.isVirtual()) |
3535 | Bases.push_back(Base.getType()->getAsCXXRecordDecl()); |
3536 | } |
3537 | |
3538 | |
3539 | llvm::stable_sort( |
3540 | Bases, [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { |
3541 | return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R); |
3542 | }); |
3543 | |
3544 | |
3545 | for (const CXXRecordDecl *Base : Bases) { |
3546 | CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
3547 | DumpRecordLayout(OS, Base, C, BaseOffset, IndentLevel, |
3548 | Base == PrimaryBase ? "(primary base)" : "(base)", |
3549 | false, |
3550 | false); |
3551 | } |
3552 | |
3553 | |
3554 | if (HasOwnVBPtr) { |
3555 | PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel); |
3556 | OS << '(' << *RD << " vbtable pointer)\n"; |
3557 | } |
3558 | } |
3559 | |
3560 | |
3561 | uint64_t FieldNo = 0; |
3562 | for (RecordDecl::field_iterator I = RD->field_begin(), |
3563 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
3564 | const FieldDecl &Field = **I; |
3565 | uint64_t LocalFieldOffsetInBits = Layout.getFieldOffset(FieldNo); |
3566 | CharUnits FieldOffset = |
3567 | Offset + C.toCharUnitsFromBits(LocalFieldOffsetInBits); |
3568 | |
3569 | |
3570 | if (auto RT = Field.getType()->getAs<RecordType>()) { |
3571 | DumpRecordLayout(OS, RT->getDecl(), C, FieldOffset, IndentLevel, |
3572 | Field.getName().data(), |
3573 | false, |
3574 | true); |
3575 | continue; |
3576 | } |
3577 | |
3578 | if (Field.isBitField()) { |
3579 | uint64_t LocalFieldByteOffsetInBits = C.toBits(FieldOffset - Offset); |
3580 | unsigned Begin = LocalFieldOffsetInBits - LocalFieldByteOffsetInBits; |
3581 | unsigned Width = Field.getBitWidthValue(C); |
3582 | PrintBitFieldOffset(OS, FieldOffset, Begin, Width, IndentLevel); |
3583 | } else { |
3584 | PrintOffset(OS, FieldOffset, IndentLevel); |
3585 | } |
3586 | const QualType &FieldType = C.getLangOpts().DumpRecordLayoutsCanonical |
3587 | ? Field.getType().getCanonicalType() |
3588 | : Field.getType(); |
3589 | OS << FieldType.getAsString() << ' ' << Field << '\n'; |
3590 | } |
3591 | |
3592 | |
3593 | if (CXXRD && IncludeVirtualBases) { |
3594 | const ASTRecordLayout::VBaseOffsetsMapTy &VtorDisps = |
3595 | Layout.getVBaseOffsetsMap(); |
3596 | |
3597 | for (const CXXBaseSpecifier &Base : CXXRD->vbases()) { |
3598 | assert(Base.isVirtual() && "Found non-virtual class!"); |
3599 | const CXXRecordDecl *VBase = Base.getType()->getAsCXXRecordDecl(); |
3600 | |
3601 | CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase); |
3602 | |
3603 | if (VtorDisps.find(VBase)->second.hasVtorDisp()) { |
3604 | PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel); |
3605 | OS << "(vtordisp for vbase " << *VBase << ")\n"; |
3606 | } |
3607 | |
3608 | DumpRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel, |
3609 | VBase == Layout.getPrimaryBase() ? |
3610 | "(primary virtual base)" : "(virtual base)", |
3611 | false, |
3612 | false); |
3613 | } |
3614 | } |
3615 | |
3616 | if (!PrintSizeInfo) return; |
3617 | |
3618 | PrintIndentNoOffset(OS, IndentLevel - 1); |
3619 | OS << "[sizeof=" << Layout.getSize().getQuantity(); |
3620 | if (CXXRD && !isMsLayout(C)) |
3621 | OS << ", dsize=" << Layout.getDataSize().getQuantity(); |
3622 | OS << ", align=" << Layout.getAlignment().getQuantity(); |
3623 | if (C.getTargetInfo().defaultsToAIXPowerAlignment()) |
3624 | OS << ", preferredalign=" << Layout.getPreferredAlignment().getQuantity(); |
3625 | |
3626 | if (CXXRD) { |
3627 | OS << ",\n"; |
3628 | PrintIndentNoOffset(OS, IndentLevel - 1); |
3629 | OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity(); |
3630 | OS << ", nvalign=" << Layout.getNonVirtualAlignment().getQuantity(); |
3631 | if (C.getTargetInfo().defaultsToAIXPowerAlignment()) |
3632 | OS << ", preferrednvalign=" |
3633 | << Layout.getPreferredNVAlignment().getQuantity(); |
3634 | } |
3635 | OS << "]\n"; |
3636 | } |
3637 | |
3638 | void ASTContext::DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS, |
3639 | bool Simple) const { |
3640 | if (!Simple) { |
| 1 | Assuming 'Simple' is true | |
|
| |
3641 | ::DumpRecordLayout(OS, RD, *this, CharUnits(), 0, nullptr, |
3642 | true, |
3643 | true); |
3644 | return; |
3645 | } |
3646 | |
3647 | |
3648 | |
3649 | |
3650 | |
3651 | |
3652 | |
3653 | |
3654 | const ASTRecordLayout &Info = getASTRecordLayout(RD); |
| 3 | | Calling 'ASTContext::getASTRecordLayout' | |
|
3655 | OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n"; |
3656 | OS << "\nLayout: "; |
3657 | OS << "<ASTRecordLayout\n"; |
3658 | OS << " Size:" << toBits(Info.getSize()) << "\n"; |
3659 | if (!isMsLayout(*this)) |
3660 | OS << " DataSize:" << toBits(Info.getDataSize()) << "\n"; |
3661 | OS << " Alignment:" << toBits(Info.getAlignment()) << "\n"; |
3662 | if (Target->defaultsToAIXPowerAlignment()) |
3663 | OS << " PreferredAlignment:" << toBits(Info.getPreferredAlignment()) |
3664 | << "\n"; |
3665 | OS << " FieldOffsets: ["; |
3666 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) { |
3667 | if (i) |
3668 | OS << ", "; |
3669 | OS << Info.getFieldOffset(i); |
3670 | } |
3671 | OS << "]>\n"; |
3672 | } |