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
11
12namespace clang::CodeGenUtils {
13
14bool isDiscreteBitFieldABI(const ASTContext &Ctx, const RecordDecl *RD) {
15 return Ctx.getTargetInfo().getCXXABI().isMicrosoft() || RD->isMsStruct(Ctx);
16}
17
18bool isEmptyFieldForLayout(const ASTContext &Ctx, const FieldDecl *FD) {
19 if (FD->isZeroLengthBitField())
20 return true;
21
22 if (FD->isUnnamedBitField())
23 return false;
24
25 return isEmptyRecordForLayout(Ctx, FD->getType());
26}
27
29 const auto *RD = T->getAsRecordDecl();
30 if (!RD)
31 return false;
32
33 // If this is a C++ record, check the bases first.
34 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
35 if (CXXRD->isDynamicClass())
36 return false;
37
38 for (const auto &I : CXXRD->bases())
39 if (!isEmptyRecordForLayout(Ctx, I.getType()))
40 return false;
41 }
42
43 for (const auto *I : RD->fields())
44 if (!isEmptyFieldForLayout(Ctx, I))
45 return false;
46
47 return true;
48}
49
51 return !Ctx.getTargetInfo().getCXXABI().isMicrosoft();
52}
53
54} // namespace clang::CodeGenUtils
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:239
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:969
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Represents a member of a struct/union/class.
Definition Decl.h:3295
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
Definition Decl.h:3401
bool isZeroLengthBitField() const
Is this a zero-length bit-field?
Definition Decl.cpp:4831
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4460
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:5388
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:724
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 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