clang 24.0.0git
RecordLayoutUtils.cpp
Go to the documentation of this file.
1//===--- RecordLayoutUtils.cpp - Shared record layout queries -------------===//
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
12
13namespace clang::CodeGenUtils {
14
16 const CXXRecordDecl *Query) {
17 const ASTRecordLayout &DeclLayout = Ctx.getASTRecordLayout(Decl);
18 if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query)
19 return false;
20 for (const auto &Base : Decl->bases())
21 if (!hasOwnStorage(Ctx, Base.getType()->getAsCXXRecordDecl(), Query))
22 return false;
23 return true;
24}
25
26bool isDiscreteBitFieldABI(const ASTContext &Ctx, const RecordDecl *RD) {
27 return Ctx.getTargetInfo().getCXXABI().isMicrosoft() || RD->isMsStruct(Ctx);
28}
29
30bool isEmptyFieldForLayout(const ASTContext &Ctx, const FieldDecl *FD) {
31 if (FD->isZeroLengthBitField())
32 return true;
33
34 if (FD->isUnnamedBitField())
35 return false;
36
37 return isEmptyRecordForLayout(Ctx, FD->getType());
38}
39
41 const auto *RD = T->getAsRecordDecl();
42 if (!RD)
43 return false;
44
45 // If this is a C++ record, check the bases first.
46 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
47 if (CXXRD->isDynamicClass())
48 return false;
49
50 for (const auto &I : CXXRD->bases())
51 if (!isEmptyRecordForLayout(Ctx, I.getType()))
52 return false;
53 }
54
55 for (const auto *I : RD->fields())
56 if (!isEmptyFieldForLayout(Ctx, I))
57 return false;
58
59 return true;
60}
61
63 return !Ctx.getTargetInfo().getCXXABI().isMicrosoft();
64}
65
66} // namespace clang::CodeGenUtils
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:971
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
bool isPrimaryBaseVirtual() const
isPrimaryBaseVirtual - Get whether the primary base for this record is virtual or not.
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a member of a struct/union/class.
Definition Decl.h:3311
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
Definition Decl.h:3417
bool isZeroLengthBitField() const
Is this a zero-length bit-field?
Definition Decl.cpp:4845
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4476
bool isMsStruct(const ASTContext &C) const
Get whether or not this is an ms_struct which can be turned on with an attribute, pragma,...
Definition Decl.cpp:5402
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
QualType getType() const
Definition Decl.h:725
Defines the clang::TargetInfo interface.
bool isDiscreteBitFieldABI(const ASTContext &Ctx, const RecordDecl *RD)
The Microsoft bitfield layout rule allocates discrete storage units of the field's formal type and on...
bool hasOwnStorage(const ASTContext &Ctx, const CXXRecordDecl *Decl, const CXXRecordDecl *Query)
Recursively searches all of the bases of Decl to find out whether Query is not the primary vbase of s...
bool isOverlappingVBaseABI(const ASTContext &Ctx)
The Itanium base layout rule allows virtual bases to overlap other bases, which complicates layout in...
bool isEmptyFieldForLayout(const ASTContext &Ctx, const FieldDecl *FD)
Return true iff the field is "empty", that is, either a zero-width bit-field or an isEmptyRecordForLa...
bool isEmptyRecordForLayout(const ASTContext &Ctx, QualType T)
Return true iff a structure contains only empty base classes (per isEmptyRecordForLayout) and fields ...
const FunctionProtoType * T