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