clang  9.0.0svn
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 
13 #include "clang/AST/RecordLayout.h"
14 #include "clang/AST/ASTContext.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include <cassert>
18 
19 using namespace clang;
20 
21 void ASTRecordLayout::Destroy(ASTContext &Ctx) {
22  if (CXXInfo) {
23  CXXInfo->~CXXRecordLayoutInfo();
24  Ctx.Deallocate(CXXInfo);
25  }
26  this->~ASTRecordLayout();
27  Ctx.Deallocate(this);
28 }
29 
30 ASTRecordLayout::ASTRecordLayout(const ASTContext &Ctx, CharUnits size,
31  CharUnits alignment,
32  CharUnits unadjustedAlignment,
33  CharUnits requiredAlignment,
34  CharUnits datasize,
35  ArrayRef<uint64_t> fieldoffsets)
36  : Size(size), DataSize(datasize), Alignment(alignment),
37  UnadjustedAlignment(unadjustedAlignment),
38  RequiredAlignment(requiredAlignment) {
39  FieldOffsets.append(Ctx, fieldoffsets.begin(), fieldoffsets.end());
40 }
41 
42 // Constructor for C++ records.
43 ASTRecordLayout::ASTRecordLayout(const ASTContext &Ctx,
44  CharUnits size, CharUnits alignment,
45  CharUnits unadjustedAlignment,
46  CharUnits requiredAlignment,
47  bool hasOwnVFPtr, bool hasExtendableVFPtr,
48  CharUnits vbptroffset,
49  CharUnits datasize,
50  ArrayRef<uint64_t> fieldoffsets,
51  CharUnits nonvirtualsize,
52  CharUnits nonvirtualalignment,
53  CharUnits SizeOfLargestEmptySubobject,
54  const CXXRecordDecl *PrimaryBase,
55  bool IsPrimaryBaseVirtual,
56  const CXXRecordDecl *BaseSharingVBPtr,
57  bool EndsWithZeroSizedObject,
58  bool LeadsWithZeroSizedBase,
59  const BaseOffsetsMapTy& BaseOffsets,
60  const VBaseOffsetsMapTy& VBaseOffsets)
61  : Size(size), DataSize(datasize), Alignment(alignment),
62  UnadjustedAlignment(unadjustedAlignment),
63  RequiredAlignment(requiredAlignment), CXXInfo(new (Ctx) CXXRecordLayoutInfo)
64 {
65  FieldOffsets.append(Ctx, fieldoffsets.begin(), fieldoffsets.end());
66 
67  CXXInfo->PrimaryBase.setPointer(PrimaryBase);
68  CXXInfo->PrimaryBase.setInt(IsPrimaryBaseVirtual);
69  CXXInfo->NonVirtualSize = nonvirtualsize;
70  CXXInfo->NonVirtualAlignment = nonvirtualalignment;
71  CXXInfo->SizeOfLargestEmptySubobject = SizeOfLargestEmptySubobject;
72  CXXInfo->BaseOffsets = BaseOffsets;
73  CXXInfo->VBaseOffsets = VBaseOffsets;
74  CXXInfo->HasOwnVFPtr = hasOwnVFPtr;
75  CXXInfo->VBPtrOffset = vbptroffset;
76  CXXInfo->HasExtendableVFPtr = hasExtendableVFPtr;
77  CXXInfo->BaseSharingVBPtr = BaseSharingVBPtr;
78  CXXInfo->EndsWithZeroSizedObject = EndsWithZeroSizedObject;
79  CXXInfo->LeadsWithZeroSizedBase = LeadsWithZeroSizedBase;
80 
81 #ifndef NDEBUG
82  if (const CXXRecordDecl *PrimaryBase = getPrimaryBase()) {
83  if (isPrimaryBaseVirtual()) {
85  assert(getVBaseClassOffset(PrimaryBase).isZero() &&
86  "Primary virtual base must be at offset 0!");
87  }
88  } else {
89  assert(getBaseClassOffset(PrimaryBase).isZero() &&
90  "Primary base must be at offset 0!");
91  }
92  }
93 #endif
94 }
Defines the clang::ASTContext interface.
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:689
bool isZero(ProgramStateRef State, const NonLoc &Val)
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1035
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
Defines the TargetCXXABI class, which abstracts details of the C++ ABI that we&#39;re targeting...
bool hasPrimaryVBases() const
Does this ABI allow virtual bases to be primary base classes?
Definition: TargetCXXABI.h:219
Dataflow Directional Tag Classes.
void Deallocate(void *Ptr) const
Definition: ASTContext.h:674
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
Defines the clang::TargetInfo interface.