1 //===-- RecordLayout.cpp - Layout information for a struct/union -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RecordLayout interface.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/RecordLayout.h"
16 #include "clang/Basic/TargetInfo.h"
18 using namespace clang
;
20 void ASTRecordLayout::Destroy(ASTContext
&Ctx
) {
22 Ctx
.Deallocate(FieldOffsets
);
24 CXXInfo
->~CXXRecordLayoutInfo();
25 Ctx
.Deallocate(CXXInfo
);
27 this->~ASTRecordLayout();
31 ASTRecordLayout::ASTRecordLayout(const ASTContext
&Ctx
, CharUnits size
,
33 CharUnits requiredAlignment
,
35 const uint64_t *fieldoffsets
,
37 : Size(size
), DataSize(datasize
), Alignment(alignment
),
38 RequiredAlignment(requiredAlignment
), FieldOffsets(nullptr),
39 FieldCount(fieldcount
), CXXInfo(nullptr) {
41 FieldOffsets
= new (Ctx
) uint64_t[FieldCount
];
42 memcpy(FieldOffsets
, fieldoffsets
, FieldCount
* sizeof(*FieldOffsets
));
46 // Constructor for C++ records.
47 ASTRecordLayout::ASTRecordLayout(const ASTContext
&Ctx
,
48 CharUnits size
, CharUnits alignment
,
49 CharUnits requiredAlignment
,
50 bool hasOwnVFPtr
, bool hasExtendableVFPtr
,
51 CharUnits vbptroffset
,
53 const uint64_t *fieldoffsets
,
55 CharUnits nonvirtualsize
,
56 CharUnits nonvirtualalignment
,
57 CharUnits SizeOfLargestEmptySubobject
,
58 const CXXRecordDecl
*PrimaryBase
,
59 bool IsPrimaryBaseVirtual
,
60 const CXXRecordDecl
*BaseSharingVBPtr
,
61 bool HasZeroSizedSubObject
,
62 bool LeadsWithZeroSizedBase
,
63 const BaseOffsetsMapTy
& BaseOffsets
,
64 const VBaseOffsetsMapTy
& VBaseOffsets
)
65 : Size(size
), DataSize(datasize
), Alignment(alignment
),
66 RequiredAlignment(requiredAlignment
), FieldOffsets(nullptr),
67 FieldCount(fieldcount
), CXXInfo(new (Ctx
) CXXRecordLayoutInfo
)
70 FieldOffsets
= new (Ctx
) uint64_t[FieldCount
];
71 memcpy(FieldOffsets
, fieldoffsets
, FieldCount
* sizeof(*FieldOffsets
));
74 CXXInfo
->PrimaryBase
.setPointer(PrimaryBase
);
75 CXXInfo
->PrimaryBase
.setInt(IsPrimaryBaseVirtual
);
76 CXXInfo
->NonVirtualSize
= nonvirtualsize
;
77 CXXInfo
->NonVirtualAlignment
= nonvirtualalignment
;
78 CXXInfo
->SizeOfLargestEmptySubobject
= SizeOfLargestEmptySubobject
;
79 CXXInfo
->BaseOffsets
= BaseOffsets
;
80 CXXInfo
->VBaseOffsets
= VBaseOffsets
;
81 CXXInfo
->HasOwnVFPtr
= hasOwnVFPtr
;
82 CXXInfo
->VBPtrOffset
= vbptroffset
;
83 CXXInfo
->HasExtendableVFPtr
= hasExtendableVFPtr
;
84 CXXInfo
->BaseSharingVBPtr
= BaseSharingVBPtr
;
85 CXXInfo
->HasZeroSizedSubObject
= HasZeroSizedSubObject
;
86 CXXInfo
->LeadsWithZeroSizedBase
= LeadsWithZeroSizedBase
;
90 if (const CXXRecordDecl
*PrimaryBase
= getPrimaryBase()) {
91 if (isPrimaryBaseVirtual()) {
92 if (Ctx
.getTargetInfo().getCXXABI().hasPrimaryVBases()) {
93 assert(getVBaseClassOffset(PrimaryBase
).isZero() &&
94 "Primary virtual base must be at offset 0!");
97 assert(getBaseClassOffset(PrimaryBase
).isZero() &&
98 "Primary base must be at offset 0!");