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