clang 24.0.0git
RecordLayout.cpp
Go to the documentation of this file.
1//===- RecordLayout.cpp - Layout information for a struct/union -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the RecordLayout interface.
10//
11//===----------------------------------------------------------------------===//
12
17#include <cassert>
18
19using namespace clang;
20
21void ASTRecordLayout::Destroy(ASTContext &Ctx) {
22 if (CXXInfo) {
23 CXXInfo->~CXXRecordLayoutInfo();
24 Ctx.Deallocate(CXXInfo);
25 }
26 this->~ASTRecordLayout();
27 Ctx.Deallocate(this);
28}
29
30ASTRecordLayout::ASTRecordLayout(
31 const ASTContext &Ctx, CharUnits size, CharUnits alignment,
32 CharUnits preferredAlignment, CharUnits unadjustedAlignment,
33 CharUnits requiredAlignment, CharUnits pragmaPackResistantAlignment,
34 CharUnits datasize, ArrayRef<uint64_t> fieldoffsets)
35 : Size(size), DataSize(datasize), Alignment(alignment),
36 PreferredAlignment(preferredAlignment),
37 UnadjustedAlignment(unadjustedAlignment),
38 RequiredAlignment(requiredAlignment),
39 PragmaPackResistantAlignment(pragmaPackResistantAlignment) {
40 FieldOffsets.append(Ctx, fieldoffsets.begin(), fieldoffsets.end());
41}
42
43// Constructor for C++ records.
44ASTRecordLayout::ASTRecordLayout(
45 const ASTContext &Ctx, CharUnits size, CharUnits alignment,
46 CharUnits preferredAlignment, CharUnits unadjustedAlignment,
47 CharUnits requiredAlignment, CharUnits pragmaPackResistantAlignment,
48 bool hasOwnVFPtr, bool hasExtendableVFPtr, CharUnits vbptroffset,
49 CharUnits datasize, ArrayRef<uint64_t> fieldoffsets,
50 CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
51 CharUnits preferrednvalignment, CharUnits nonrequirednvalignment,
52 CharUnits SizeOfLargestEmptySubobject, const CXXRecordDecl *PrimaryBase,
53 bool IsPrimaryBaseVirtual, const CXXRecordDecl *BaseSharingVBPtr,
54 bool EndsWithZeroSizedObject, bool LeadsWithZeroSizedBase,
55 const BaseOffsetsMapTy &BaseOffsets, const VBaseOffsetsMapTy &VBaseOffsets)
56 : Size(size), DataSize(datasize), Alignment(alignment),
57 PreferredAlignment(preferredAlignment),
58 UnadjustedAlignment(unadjustedAlignment),
59 RequiredAlignment(requiredAlignment),
60 PragmaPackResistantAlignment(pragmaPackResistantAlignment),
61 CXXInfo(new (Ctx) CXXRecordLayoutInfo) {
62 FieldOffsets.append(Ctx, fieldoffsets.begin(), fieldoffsets.end());
63
64 CXXInfo->PrimaryBase.setPointer(PrimaryBase);
65 CXXInfo->PrimaryBase.setInt(IsPrimaryBaseVirtual);
66 CXXInfo->NonVirtualSize = nonvirtualsize;
67 CXXInfo->NonVirtualAlignment = nonvirtualalignment;
68 CXXInfo->PreferredNVAlignment = preferrednvalignment;
69 CXXInfo->NonRequiredNVAlignment = nonrequirednvalignment;
70 CXXInfo->SizeOfLargestEmptySubobject = SizeOfLargestEmptySubobject;
71 CXXInfo->BaseOffsets = BaseOffsets;
72 CXXInfo->VBaseOffsets = VBaseOffsets;
73 CXXInfo->HasOwnVFPtr = hasOwnVFPtr;
74 CXXInfo->VBPtrOffset = vbptroffset;
75 CXXInfo->HasExtendableVFPtr = hasExtendableVFPtr;
76 CXXInfo->BaseSharingVBPtr = BaseSharingVBPtr;
77 CXXInfo->EndsWithZeroSizedObject = EndsWithZeroSizedObject;
78 CXXInfo->LeadsWithZeroSizedBase = LeadsWithZeroSizedBase;
79
80#ifndef NDEBUG
81 if (const CXXRecordDecl *PrimaryBase = getPrimaryBase()) {
82 if (isPrimaryBaseVirtual()) {
84 assert(getVBaseClassOffset(PrimaryBase).isZero() &&
85 "Primary virtual base must be at offset 0!");
86 }
87 } else {
88 assert(getBaseClassOffset(PrimaryBase).isZero() &&
89 "Primary base must be at offset 0!");
90 }
91 }
92#endif
93}
Defines the clang::ASTContext interface.
Defines the TargetCXXABI class, which abstracts details of the C++ ABI that we're targeting.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
void Deallocate(void *Ptr) const
Definition ASTContext.h:932
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:971
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool hasPrimaryVBases() const
Does this ABI allow virtual bases to be primary base classes?
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Defines the clang::TargetInfo interface.
Top level wrappers for InstallAPI frontend operations.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))