clang 24.0.0git
ItaniumCXXABIUtils.cpp
Go to the documentation of this file.
1//===--- ItaniumCXXABIUtils.cpp - Shared Itanium C++ ABI 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 *Dst) {
17 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
18 /*DetectVirtual=*/false);
19
20 // If Dst is not derived from Src we can skip the whole computation below and
21 // return that Src is not a public base of Dst. Record all inheritance paths.
22 if (!Dst->isDerivedFrom(Src, Paths))
23 return CharUnits::fromQuantity(-2ULL);
24
25 unsigned NumPublicPaths = 0;
26 CharUnits Offset;
27
28 // Now walk all possible inheritance paths.
29 for (const CXXBasePath &Path : Paths) {
30 if (Path.Access != AS_public) // Ignore non-public inheritance.
31 continue;
32
33 ++NumPublicPaths;
34
35 for (const CXXBasePathElement &PathElement : Path) {
36 // If the path contains a virtual base class we can't give any hint.
37 // -1: no hint.
38 if (PathElement.Base->isVirtual())
39 return CharUnits::fromQuantity(-1ULL);
40
41 if (NumPublicPaths > 1) // Won't use offsets, skip computation.
42 continue;
43
44 // Accumulate the base class offsets.
45 const ASTRecordLayout &L = Ctx.getASTRecordLayout(PathElement.Class);
46 Offset += L.getBaseClassOffset(
47 PathElement.Base->getType()->getAsCXXRecordDecl());
48 }
49 }
50
51 // -2: Src is not a public base of Dst.
52 if (NumPublicPaths == 0)
53 return CharUnits::fromQuantity(-2ULL);
54
55 // -3: Src is a multiple public base type but never a virtual base type.
56 if (NumPublicPaths > 1)
57 return CharUnits::fromQuantity(-3ULL);
58
59 // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
60 // Return the offset of Src from the origin of Dst.
61 return Offset;
62}
63
64} // 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 ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Represents a path from a specific derived class (which is not represented as part of the path) to a p...
BasePaths - Represents the set of paths from a derived class to one of its (direct or indirect) bases...
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool isDerivedFrom(const CXXRecordDecl *Base) const
Determine whether this class is derived from the class Base.
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
CharUnits computeOffsetHint(ASTContext &Ctx, const CXXRecordDecl *Src, const CXXRecordDecl *Dst)
Compute the src2dst_offset hint as described in the Itanium C++ ABI [2.9.7].
@ AS_public
Definition Specifiers.h:125
Represents an element in a path from a derived class to a base class.