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 "CodeGenTypes.h"
22 #include "CGCXXABI.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Type.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetData.h"
28 using namespace clang;
29 using namespace CodeGen;
30 
31 namespace {
32 
33 class CGRecordLayoutBuilder {
34 public:
35   /// FieldTypes - Holds the LLVM types that the struct is created from.
36   std::vector<const llvm::Type *> FieldTypes;
37 
38   /// NonVirtualBaseFieldTypes - Holds the LLVM types for the non-virtual part
39   /// of the struct. For example, consider:
40   ///
41   /// struct A { int i; };
42   /// struct B { void *v; };
43   /// struct C : virtual A, B { };
44   ///
45   /// The LLVM type of C will be
46   /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
47   ///
48   /// And the LLVM type of the non-virtual base struct will be
49   /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
50   std::vector<const llvm::Type *> NonVirtualBaseFieldTypes;
51 
52   /// NonVirtualBaseTypeIsSameAsCompleteType - Whether the non-virtual part of
53   /// the struct is equivalent to the complete struct.
54   bool NonVirtualBaseTypeIsSameAsCompleteType;
55 
56   /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
57   typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
58   llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
59 
60   /// LLVMBitFieldInfo - Holds location and size information about a bit field.
61   typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
62   llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
63 
64   typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
65   llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
66 
67   /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
68   /// primary base classes for some other direct or indirect base class.
69   CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
70 
71   /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
72   /// avoid laying out virtual bases more than once.
73   llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
74 
75   /// IsZeroInitializable - Whether this struct can be C++
76   /// zero-initialized with an LLVM zeroinitializer.
77   bool IsZeroInitializable;
78 
79   /// Packed - Whether the resulting LLVM struct will be packed or not.
80   bool Packed;
81 
82 private:
83   CodeGenTypes &Types;
84 
85   /// Alignment - Contains the alignment of the RecordDecl.
86   //
87   // FIXME: This is not needed and should be removed.
88   unsigned Alignment;
89 
90   /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
91   /// this will have the number of bits still available in the field.
92   char BitsAvailableInLastField;
93 
94   /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
95   uint64_t NextFieldOffsetInBytes;
96 
97   /// LayoutUnionField - Will layout a field in an union and return the type
98   /// that the field will have.
99   const llvm::Type *LayoutUnionField(const FieldDecl *Field,
100                                      const ASTRecordLayout &Layout);
101 
102   /// LayoutUnion - Will layout a union RecordDecl.
103   void LayoutUnion(const RecordDecl *D);
104 
105   /// LayoutField - try to layout all fields in the record decl.
106   /// Returns false if the operation failed because the struct is not packed.
107   bool LayoutFields(const RecordDecl *D);
108 
109   /// Layout a single base, virtual or non-virtual
110   void LayoutBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
111 
112   /// LayoutVirtualBase - layout a single virtual base.
113   void LayoutVirtualBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
114 
115   /// LayoutVirtualBases - layout the virtual bases of a record decl.
116   void LayoutVirtualBases(const CXXRecordDecl *RD,
117                           const ASTRecordLayout &Layout);
118 
119   /// LayoutNonVirtualBase - layout a single non-virtual base.
120   void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
121                             uint64_t BaseOffset);
122 
123   /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
124   void LayoutNonVirtualBases(const CXXRecordDecl *RD,
125                              const ASTRecordLayout &Layout);
126 
127   /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
128   bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
129 
130   /// LayoutField - layout a single field. Returns false if the operation failed
131   /// because the current struct is not packed.
132   bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
133 
134   /// LayoutBitField - layout a single bit field.
135   void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
136 
137   /// AppendField - Appends a field with the given offset and type.
138   void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
139 
140   /// AppendPadding - Appends enough padding bytes so that the total
141   /// struct size is a multiple of the field alignment.
142   void AppendPadding(uint64_t FieldOffsetInBytes,
143                      unsigned FieldAlignmentInBytes);
144 
145   /// getByteArrayType - Returns a byte array type with the given number of
146   /// elements.
147   const llvm::Type *getByteArrayType(uint64_t NumBytes);
148 
149   /// AppendBytes - Append a given number of bytes to the record.
150   void AppendBytes(uint64_t NumBytes);
151 
152   /// AppendTailPadding - Append enough tail padding so that the type will have
153   /// the passed size.
154   void AppendTailPadding(uint64_t RecordSize);
155 
156   unsigned getTypeAlignment(const llvm::Type *Ty) const;
157 
158   /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
159   /// LLVM element types.
160   unsigned getAlignmentAsLLVMStruct() const;
161 
162   /// CheckZeroInitializable - Check if the given type contains a pointer
163   /// to data member.
164   void CheckZeroInitializable(QualType T);
165   void CheckZeroInitializable(const CXXRecordDecl *RD);
166 
167 public:
168   CGRecordLayoutBuilder(CodeGenTypes &Types)
169     : NonVirtualBaseTypeIsSameAsCompleteType(false), IsZeroInitializable(true),
170     Packed(false), Types(Types), Alignment(0), BitsAvailableInLastField(0),
171     NextFieldOffsetInBytes(0) { }
172 
173   /// Layout - Will layout a RecordDecl.
174   void Layout(const RecordDecl *D);
175 };
176 
177 }
178 
179 void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
180   Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
181   Packed = D->hasAttr<PackedAttr>();
182 
183   if (D->isUnion()) {
184     LayoutUnion(D);
185     return;
186   }
187 
188   if (LayoutFields(D))
189     return;
190 
191   // We weren't able to layout the struct. Try again with a packed struct
192   Packed = true;
193   NextFieldOffsetInBytes = 0;
194   FieldTypes.clear();
195   LLVMFields.clear();
196   LLVMBitFields.clear();
197   LLVMNonVirtualBases.clear();
198 
199   LayoutFields(D);
200 }
201 
202 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
203                                const FieldDecl *FD,
204                                uint64_t FieldOffset,
205                                uint64_t FieldSize,
206                                uint64_t ContainingTypeSizeInBits,
207                                unsigned ContainingTypeAlign) {
208   const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
209   uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
210   uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
211 
212   bool IsSigned = FD->getType()->isSignedIntegerType();
213 
214   if (FieldSize > TypeSizeInBits) {
215     // We have a wide bit-field. The extra bits are only used for padding, so
216     // if we have a bitfield of type T, with size N:
217     //
218     // T t : N;
219     //
220     // We can just assume that it's:
221     //
222     // T t : sizeof(T);
223     //
224     FieldSize = TypeSizeInBits;
225   }
226 
227   // Compute the access components. The policy we use is to start by attempting
228   // to access using the width of the bit-field type itself and to always access
229   // at aligned indices of that type. If such an access would fail because it
230   // extends past the bound of the type, then we reduce size to the next smaller
231   // power of two and retry. The current algorithm assumes pow2 sized types,
232   // although this is easy to fix.
233   //
234   // FIXME: This algorithm is wrong on big-endian systems, I think.
235   assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
236   CGBitFieldInfo::AccessInfo Components[3];
237   unsigned NumComponents = 0;
238   unsigned AccessedTargetBits = 0;       // The tumber of target bits accessed.
239   unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
240 
241   // Round down from the field offset to find the first access position that is
242   // at an aligned offset of the initial access type.
243   uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
244 
245   // Adjust initial access size to fit within record.
246   while (AccessWidth > 8 &&
247          AccessStart + AccessWidth > ContainingTypeSizeInBits) {
248     AccessWidth >>= 1;
249     AccessStart = FieldOffset - (FieldOffset % AccessWidth);
250   }
251 
252   while (AccessedTargetBits < FieldSize) {
253     // Check that we can access using a type of this size, without reading off
254     // the end of the structure. This can occur with packed structures and
255     // -fno-bitfield-type-align, for example.
256     if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
257       // If so, reduce access size to the next smaller power-of-two and retry.
258       AccessWidth >>= 1;
259       assert(AccessWidth >= 8 && "Cannot access under byte size!");
260       continue;
261     }
262 
263     // Otherwise, add an access component.
264 
265     // First, compute the bits inside this access which are part of the
266     // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
267     // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
268     // in the target that we are reading.
269     assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
270     assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
271     uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
272     uint64_t AccessBitsInFieldSize =
273       std::min(AccessWidth + AccessStart,
274                FieldOffset + FieldSize) - AccessBitsInFieldStart;
275 
276     assert(NumComponents < 3 && "Unexpected number of components!");
277     CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
278     AI.FieldIndex = 0;
279     // FIXME: We still follow the old access pattern of only using the field
280     // byte offset. We should switch this once we fix the struct layout to be
281     // pretty.
282     AI.FieldByteOffset = AccessStart / 8;
283     AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
284     AI.AccessWidth = AccessWidth;
285     AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
286     AI.TargetBitOffset = AccessedTargetBits;
287     AI.TargetBitWidth = AccessBitsInFieldSize;
288 
289     AccessStart += AccessWidth;
290     AccessedTargetBits += AI.TargetBitWidth;
291   }
292 
293   assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
294   return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
295 }
296 
297 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
298                                         const FieldDecl *FD,
299                                         uint64_t FieldOffset,
300                                         uint64_t FieldSize) {
301   const RecordDecl *RD = FD->getParent();
302   const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
303   uint64_t ContainingTypeSizeInBits = RL.getSize();
304   unsigned ContainingTypeAlign = RL.getAlignment();
305 
306   return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
307                   ContainingTypeAlign);
308 }
309 
310 void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
311                                            uint64_t FieldOffset) {
312   uint64_t FieldSize =
313     D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
314 
315   if (FieldSize == 0)
316     return;
317 
318   uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
319   unsigned NumBytesToAppend;
320 
321   if (FieldOffset < NextFieldOffset) {
322     assert(BitsAvailableInLastField && "Bitfield size mismatch!");
323     assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
324 
325     // The bitfield begins in the previous bit-field.
326     NumBytesToAppend =
327       llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
328   } else {
329     assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
330 
331     // Append padding if necessary.
332     AppendPadding(FieldOffset / 8, 1);
333 
334     NumBytesToAppend =
335       llvm::RoundUpToAlignment(FieldSize, 8) / 8;
336 
337     assert(NumBytesToAppend && "No bytes to append!");
338   }
339 
340   // Add the bit field info.
341   LLVMBitFields.push_back(
342     LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
343                                                  FieldSize)));
344 
345   AppendBytes(NumBytesToAppend);
346 
347   BitsAvailableInLastField =
348     NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
349 }
350 
351 bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
352                                         uint64_t FieldOffset) {
353   // If the field is packed, then we need a packed struct.
354   if (!Packed && D->hasAttr<PackedAttr>())
355     return false;
356 
357   if (D->isBitField()) {
358     // We must use packed structs for unnamed bit fields since they
359     // don't affect the struct alignment.
360     if (!Packed && !D->getDeclName())
361       return false;
362 
363     LayoutBitField(D, FieldOffset);
364     return true;
365   }
366 
367   CheckZeroInitializable(D->getType());
368 
369   assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
370   uint64_t FieldOffsetInBytes = FieldOffset / 8;
371 
372   const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
373   unsigned TypeAlignment = getTypeAlignment(Ty);
374 
375   // If the type alignment is larger then the struct alignment, we must use
376   // a packed struct.
377   if (TypeAlignment > Alignment) {
378     assert(!Packed && "Alignment is wrong even with packed struct!");
379     return false;
380   }
381 
382   if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
383     const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
384     if (const MaxFieldAlignmentAttr *MFAA =
385           RD->getAttr<MaxFieldAlignmentAttr>()) {
386       if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
387         return false;
388     }
389   }
390 
391   // Round up the field offset to the alignment of the field type.
392   uint64_t AlignedNextFieldOffsetInBytes =
393     llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
394 
395   if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
396     assert(!Packed && "Could not place field even with packed struct!");
397     return false;
398   }
399 
400   AppendPadding(FieldOffsetInBytes, TypeAlignment);
401 
402   // Now append the field.
403   LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
404   AppendField(FieldOffsetInBytes, Ty);
405 
406   return true;
407 }
408 
409 const llvm::Type *
410 CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
411                                         const ASTRecordLayout &Layout) {
412   if (Field->isBitField()) {
413     uint64_t FieldSize =
414       Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
415 
416     // Ignore zero sized bit fields.
417     if (FieldSize == 0)
418       return 0;
419 
420     const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
421     unsigned NumBytesToAppend =
422       llvm::RoundUpToAlignment(FieldSize, 8) / 8;
423 
424     if (NumBytesToAppend > 1)
425       FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
426 
427     // Add the bit field info.
428     LLVMBitFields.push_back(
429       LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
430                                                        0, FieldSize)));
431     return FieldTy;
432   }
433 
434   // This is a regular union field.
435   LLVMFields.push_back(LLVMFieldInfo(Field, 0));
436   return Types.ConvertTypeForMemRecursive(Field->getType());
437 }
438 
439 void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
440   assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
441 
442   const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
443 
444   const llvm::Type *Ty = 0;
445   uint64_t Size = 0;
446   unsigned Align = 0;
447 
448   bool HasOnlyZeroSizedBitFields = true;
449 
450   unsigned FieldNo = 0;
451   for (RecordDecl::field_iterator Field = D->field_begin(),
452        FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
453     assert(Layout.getFieldOffset(FieldNo) == 0 &&
454           "Union field offset did not start at the beginning of record!");
455     const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
456 
457     if (!FieldTy)
458       continue;
459 
460     HasOnlyZeroSizedBitFields = false;
461 
462     unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
463     uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
464 
465     if (FieldAlign < Align)
466       continue;
467 
468     if (FieldAlign > Align || FieldSize > Size) {
469       Ty = FieldTy;
470       Align = FieldAlign;
471       Size = FieldSize;
472     }
473   }
474 
475   // Now add our field.
476   if (Ty) {
477     AppendField(0, Ty);
478 
479     if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
480       // We need a packed struct.
481       Packed = true;
482       Align = 1;
483     }
484   }
485   if (!Align) {
486     assert(HasOnlyZeroSizedBitFields &&
487            "0-align record did not have all zero-sized bit-fields!");
488     Align = 1;
489   }
490 
491   // Append tail padding.
492   if (Layout.getSize() / 8 > Size)
493     AppendPadding(Layout.getSize() / 8, Align);
494 }
495 
496 void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *BaseDecl,
497                                        uint64_t BaseOffset) {
498   CheckZeroInitializable(BaseDecl);
499 
500   const ASTRecordLayout &Layout =
501     Types.getContext().getASTRecordLayout(BaseDecl);
502 
503   uint64_t NonVirtualSize = Layout.getNonVirtualSize();
504 
505   AppendPadding(BaseOffset / 8, 1);
506 
507   // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
508   AppendBytes(NonVirtualSize / 8);
509 }
510 
511 void
512 CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *BaseDecl,
513                                          uint64_t BaseOffset) {
514   // Ignore empty bases.
515   if (BaseDecl->isEmpty())
516     return;
517 
518   CheckZeroInitializable(BaseDecl);
519 
520   const ASTRecordLayout &Layout =
521     Types.getContext().getASTRecordLayout(BaseDecl);
522 
523   uint64_t NonVirtualSize = Layout.getNonVirtualSize();
524 
525   AppendPadding(BaseOffset / 8, 1);
526 
527   // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
528   AppendBytes(NonVirtualSize / 8);
529 
530   // FIXME: Add the vbase field info.
531 }
532 
533 /// LayoutVirtualBases - layout the non-virtual bases of a record decl.
534 void
535 CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
536                                           const ASTRecordLayout &Layout) {
537   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
538        E = RD->bases_end(); I != E; ++I) {
539     const CXXRecordDecl *BaseDecl =
540       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
541 
542     // We only want to lay out virtual bases that aren't indirect primary bases
543     // of some other base.
544     if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
545       // Only lay out the base once.
546       if (!LaidOutVirtualBases.insert(BaseDecl))
547         continue;
548 
549       uint64_t VBaseOffset = Layout.getVBaseClassOffsetInBits(BaseDecl);
550       LayoutVirtualBase(BaseDecl, VBaseOffset);
551     }
552 
553     if (!BaseDecl->getNumVBases()) {
554       // This base isn't interesting since it doesn't have any virtual bases.
555       continue;
556     }
557 
558     LayoutVirtualBases(BaseDecl, Layout);
559   }
560 }
561 
562 void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
563                                                  uint64_t BaseOffset) {
564   // Ignore empty bases.
565   if (BaseDecl->isEmpty())
566     return;
567 
568   LayoutBase(BaseDecl, BaseOffset);
569 
570   // Append the base field.
571   LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size() - 1));
572 }
573 
574 void
575 CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
576                                              const ASTRecordLayout &Layout) {
577   const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
578 
579   // Check if we need to add a vtable pointer.
580   if (RD->isDynamicClass()) {
581     if (!PrimaryBase) {
582       const llvm::Type *FunctionType =
583         llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
584                                 /*isVarArg=*/true);
585       const llvm::Type *VTableTy = FunctionType->getPointerTo();
586 
587       assert(NextFieldOffsetInBytes == 0 &&
588              "VTable pointer must come first!");
589       AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
590     } else {
591       if (!Layout.isPrimaryBaseVirtual())
592         LayoutNonVirtualBase(PrimaryBase, 0);
593       else
594         LayoutVirtualBase(PrimaryBase, 0);
595     }
596   }
597 
598   // Layout the non-virtual bases.
599   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
600        E = RD->bases_end(); I != E; ++I) {
601     if (I->isVirtual())
602       continue;
603 
604     const CXXRecordDecl *BaseDecl =
605       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
606 
607     // We've already laid out the primary base.
608     if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
609       continue;
610 
611     LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl));
612   }
613 }
614 
615 bool
616 CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
617   const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
618 
619   uint64_t AlignedNonVirtualTypeSize =
620     llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
621                              Layout.getNonVirtualAlign()) / 8;
622 
623 
624   // First check if we can use the same fields as for the complete class.
625   if (AlignedNonVirtualTypeSize == Layout.getSize() / 8) {
626     NonVirtualBaseTypeIsSameAsCompleteType = true;
627     return true;
628   }
629 
630   // Check if we need padding.
631   uint64_t AlignedNextFieldOffset =
632     llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
633                              getAlignmentAsLLVMStruct());
634 
635   if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize)
636     return false; // Needs packing.
637 
638   NonVirtualBaseFieldTypes = FieldTypes;
639 
640   if (AlignedNonVirtualTypeSize == AlignedNextFieldOffset) {
641     // We don't need any padding.
642     return true;
643   }
644 
645   uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
646   NonVirtualBaseFieldTypes.push_back(getByteArrayType(NumBytes));
647   return true;
648 }
649 
650 bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
651   assert(!D->isUnion() && "Can't call LayoutFields on a union!");
652   assert(Alignment && "Did not set alignment!");
653 
654   const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
655 
656   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
657   if (RD)
658     LayoutNonVirtualBases(RD, Layout);
659 
660   unsigned FieldNo = 0;
661 
662   for (RecordDecl::field_iterator Field = D->field_begin(),
663        FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
664     if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
665       assert(!Packed &&
666              "Could not layout fields even with a packed LLVM struct!");
667       return false;
668     }
669   }
670 
671   if (RD) {
672     // We've laid out the non-virtual bases and the fields, now compute the
673     // non-virtual base field types.
674     if (!ComputeNonVirtualBaseType(RD)) {
675       assert(!Packed && "Could not layout even with a packed LLVM struct!");
676       return false;
677     }
678 
679     // And lay out the virtual bases.
680     RD->getIndirectPrimaryBases(IndirectPrimaryBases);
681     if (Layout.isPrimaryBaseVirtual())
682       IndirectPrimaryBases.insert(Layout.getPrimaryBase());
683     LayoutVirtualBases(RD, Layout);
684   }
685 
686   // Append tail padding if necessary.
687   AppendTailPadding(Layout.getSize());
688 
689   return true;
690 }
691 
692 void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
693   assert(RecordSize % 8 == 0 && "Invalid record size!");
694 
695   uint64_t RecordSizeInBytes = RecordSize / 8;
696   assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
697 
698   uint64_t AlignedNextFieldOffset =
699     llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
700                              getAlignmentAsLLVMStruct());
701 
702   if (AlignedNextFieldOffset == RecordSizeInBytes) {
703     // We don't need any padding.
704     return;
705   }
706 
707   unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
708   AppendBytes(NumPadBytes);
709 }
710 
711 void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
712                                         const llvm::Type *FieldTy) {
713   uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
714 
715   FieldTypes.push_back(FieldTy);
716 
717   NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
718   BitsAvailableInLastField = 0;
719 }
720 
721 void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
722                                           unsigned FieldAlignmentInBytes) {
723   assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
724          "Incorrect field layout!");
725 
726   // Round up the field offset to the alignment of the field type.
727   uint64_t AlignedNextFieldOffsetInBytes =
728     llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignmentInBytes);
729 
730   if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
731     // Even with alignment, the field offset is not at the right place,
732     // insert padding.
733     uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
734 
735     AppendBytes(PaddingInBytes);
736   }
737 }
738 
739 const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) {
740   assert(NumBytes != 0 && "Empty byte array's aren't allowed.");
741 
742   const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
743   if (NumBytes > 1)
744     Ty = llvm::ArrayType::get(Ty, NumBytes);
745 
746   return Ty;
747 }
748 
749 void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
750   if (NumBytes == 0)
751     return;
752 
753   // Append the padding field
754   AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes));
755 }
756 
757 unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
758   if (Packed)
759     return 1;
760 
761   return Types.getTargetData().getABITypeAlignment(Ty);
762 }
763 
764 unsigned CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
765   if (Packed)
766     return 1;
767 
768   unsigned MaxAlignment = 1;
769   for (size_t i = 0; i != FieldTypes.size(); ++i)
770     MaxAlignment = std::max(MaxAlignment, getTypeAlignment(FieldTypes[i]));
771 
772   return MaxAlignment;
773 }
774 
775 void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
776   // This record already contains a member pointer.
777   if (!IsZeroInitializable)
778     return;
779 
780   // Can only have member pointers if we're compiling C++.
781   if (!Types.getContext().getLangOptions().CPlusPlus)
782     return;
783 
784   T = Types.getContext().getBaseElementType(T);
785 
786   if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
787     if (!Types.getCXXABI().isZeroInitializable(MPT))
788       IsZeroInitializable = false;
789   } else if (const RecordType *RT = T->getAs<RecordType>()) {
790     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
791     CheckZeroInitializable(RD);
792   }
793 }
794 
795 void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
796   // This record already contains a member pointer.
797   if (!IsZeroInitializable)
798     return;
799 
800   const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
801   if (!Layout.isZeroInitializable())
802     IsZeroInitializable = false;
803 }
804 
805 CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
806   CGRecordLayoutBuilder Builder(*this);
807 
808   Builder.Layout(D);
809 
810   const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
811                                                      Builder.FieldTypes,
812                                                      Builder.Packed);
813 
814   const llvm::StructType *BaseTy = 0;
815   if (isa<CXXRecordDecl>(D)) {
816     if (Builder.NonVirtualBaseTypeIsSameAsCompleteType)
817       BaseTy = Ty;
818     else if (!Builder.NonVirtualBaseFieldTypes.empty())
819       BaseTy = llvm::StructType::get(getLLVMContext(),
820                                      Builder.NonVirtualBaseFieldTypes,
821                                      Builder.Packed);
822   }
823 
824   CGRecordLayout *RL =
825     new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable);
826 
827   // Add all the non-virtual base field numbers.
828   RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
829                                   Builder.LLVMNonVirtualBases.end());
830 
831   // Add all the field numbers.
832   RL->FieldInfo.insert(Builder.LLVMFields.begin(),
833                        Builder.LLVMFields.end());
834 
835   // Add bitfield info.
836   RL->BitFields.insert(Builder.LLVMBitFields.begin(),
837                        Builder.LLVMBitFields.end());
838 
839   // Dump the layout, if requested.
840   if (getContext().getLangOptions().DumpRecordLayouts) {
841     llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
842     llvm::errs() << "Record: ";
843     D->dump();
844     llvm::errs() << "\nLayout: ";
845     RL->dump();
846   }
847 
848 #ifndef NDEBUG
849   // Verify that the computed LLVM struct size matches the AST layout size.
850   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
851 
852   uint64_t TypeSizeInBits = Layout.getSize();
853   assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
854          "Type size mismatch!");
855 
856   if (BaseTy) {
857     uint64_t AlignedNonVirtualTypeSizeInBits =
858       llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
859                                Layout.getNonVirtualAlign());
860 
861     assert(AlignedNonVirtualTypeSizeInBits ==
862            getTargetData().getTypeAllocSizeInBits(BaseTy) &&
863            "Type size mismatch!");
864   }
865 
866   // Verify that the LLVM and AST field offsets agree.
867   const llvm::StructType *ST =
868     dyn_cast<llvm::StructType>(RL->getLLVMType());
869   const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
870 
871   const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
872   RecordDecl::field_iterator it = D->field_begin();
873   for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
874     const FieldDecl *FD = *it;
875 
876     // For non-bit-fields, just check that the LLVM struct offset matches the
877     // AST offset.
878     if (!FD->isBitField()) {
879       unsigned FieldNo = RL->getLLVMFieldNo(FD);
880       assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
881              "Invalid field offset!");
882       continue;
883     }
884 
885     // Ignore unnamed bit-fields.
886     if (!FD->getDeclName())
887       continue;
888 
889     const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
890     for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
891       const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
892 
893       // Verify that every component access is within the structure.
894       uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
895       uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
896       assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
897              "Invalid bit-field access (out of range)!");
898     }
899   }
900 #endif
901 
902   return RL;
903 }
904 
905 void CGRecordLayout::print(llvm::raw_ostream &OS) const {
906   OS << "<CGRecordLayout\n";
907   OS << "  LLVMType:" << *LLVMType << "\n";
908   if (NonVirtualBaseLLVMType)
909     OS << "  NonVirtualBaseLLVMType:" << *NonVirtualBaseLLVMType << "\n";
910   OS << "  IsZeroInitializable:" << IsZeroInitializable << "\n";
911   OS << "  BitFields:[\n";
912 
913   // Print bit-field infos in declaration order.
914   std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
915   for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
916          it = BitFields.begin(), ie = BitFields.end();
917        it != ie; ++it) {
918     const RecordDecl *RD = it->first->getParent();
919     unsigned Index = 0;
920     for (RecordDecl::field_iterator
921            it2 = RD->field_begin(); *it2 != it->first; ++it2)
922       ++Index;
923     BFIs.push_back(std::make_pair(Index, &it->second));
924   }
925   llvm::array_pod_sort(BFIs.begin(), BFIs.end());
926   for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
927     OS.indent(4);
928     BFIs[i].second->print(OS);
929     OS << "\n";
930   }
931 
932   OS << "]>\n";
933 }
934 
935 void CGRecordLayout::dump() const {
936   print(llvm::errs());
937 }
938 
939 void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
940   OS << "<CGBitFieldInfo";
941   OS << " Size:" << Size;
942   OS << " IsSigned:" << IsSigned << "\n";
943 
944   OS.indent(4 + strlen("<CGBitFieldInfo"));
945   OS << " NumComponents:" << getNumComponents();
946   OS << " Components: [";
947   if (getNumComponents()) {
948     OS << "\n";
949     for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
950       const AccessInfo &AI = getComponent(i);
951       OS.indent(8);
952       OS << "<AccessInfo"
953          << " FieldIndex:" << AI.FieldIndex
954          << " FieldByteOffset:" << AI.FieldByteOffset
955          << " FieldBitStart:" << AI.FieldBitStart
956          << " AccessWidth:" << AI.AccessWidth << "\n";
957       OS.indent(8 + strlen("<AccessInfo"));
958       OS << " AccessAlignment:" << AI.AccessAlignment
959          << " TargetBitOffset:" << AI.TargetBitOffset
960          << " TargetBitWidth:" << AI.TargetBitWidth
961          << ">\n";
962     }
963     OS.indent(4);
964   }
965   OS << "]>";
966 }
967 
968 void CGBitFieldInfo::dump() const {
969   print(llvm::errs());
970 }
971