clang 24.0.0git
ItaniumCXXABI.cpp
Go to the documentation of this file.
1//===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
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 provides C++ AST support targeting the Itanium C++ ABI, which is
10// documented at:
11// http://www.codesourcery.com/public/cxx-abi/abi.html
12// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
13//
14// It also supports the closely-related ARM C++ ABI, documented at:
15// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
16//
17//===----------------------------------------------------------------------===//
18
19#include "CXXABI.h"
21#include "clang/AST/DeclCXX.h"
22#include "clang/AST/Mangle.h"
25#include "clang/AST/Type.h"
27#include "llvm/ADT/iterator.h"
28
29using namespace clang;
30
31namespace {
32
33/// According to Itanium C++ ABI 5.1.2:
34/// the name of an anonymous union is considered to be
35/// the name of the first named data member found by a pre-order,
36/// depth-first, declaration-order walk of the data members of
37/// the anonymous union.
38/// If there is no such data member (i.e., if all of the data members
39/// in the union are unnamed), then there is no way for a program to
40/// refer to the anonymous union, and there is therefore no need to mangle its name.
41///
42/// Returns the name of anonymous union VarDecl or nullptr if it is not found.
43static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
44 const auto *RD = VD.getType()->castAsRecordDecl();
45 assert(RD->isUnion() && "RecordType is expected to be a union.");
46 if (const FieldDecl *FD = RD->findFirstNamedDataMember()) {
47 return FD->getIdentifier();
48 }
49
50 return nullptr;
51}
52
53/// The name of a decomposition declaration.
54struct DecompositionDeclName {
55 using BindingArray = ArrayRef<const BindingDecl*>;
56
57 /// Representative example of a set of bindings with these names.
58 BindingArray Bindings;
59
60 /// Iterators over the sequence of identifiers in the name.
61 struct Iterator
62 : llvm::iterator_adaptor_base<Iterator, BindingArray::const_iterator,
63 std::random_access_iterator_tag,
64 const IdentifierInfo *> {
65 Iterator(BindingArray::const_iterator It) : iterator_adaptor_base(It) {}
66 const IdentifierInfo *operator*() const {
67 return (*this->I)->getIdentifier();
68 }
69 };
70 Iterator begin() const { return Iterator(Bindings.begin()); }
71 Iterator end() const { return Iterator(Bindings.end()); }
72};
73}
74
75namespace llvm {
76template<>
77struct DenseMapInfo<DecompositionDeclName> {
78 using ArrayInfo = llvm::DenseMapInfo<ArrayRef<const BindingDecl*>>;
79 static unsigned getHashValue(DecompositionDeclName Key) {
80 return llvm::hash_combine_range(Key);
81 }
82 static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS) {
83 return LHS.Bindings.size() == RHS.Bindings.size() &&
84 std::equal(LHS.begin(), LHS.end(), RHS.begin());
85 }
86};
87}
88
89namespace {
90
91/// Keeps track of the mangled names of lambda expressions and block
92/// literals within a particular context.
93class ItaniumNumberingContext : public MangleNumberingContext {
94 ItaniumMangleContext *Mangler;
95 llvm::StringMap<unsigned> LambdaManglingNumbers;
96 unsigned BlockManglingNumber = 0;
97 llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
98 llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
99 llvm::DenseMap<DecompositionDeclName, unsigned>
100 DecompsitionDeclManglingNumbers;
101
102public:
103 ItaniumNumberingContext(ItaniumMangleContext *Mangler) : Mangler(Mangler) {}
104
105 unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
106 const CXXRecordDecl *Lambda = CallOperator->getParent();
107 assert(Lambda->isLambda());
108
109 // Computation of the <lambda-sig> is non-trivial and subtle. Rather than
110 // duplicating it here, just mangle the <lambda-sig> directly.
111 llvm::SmallString<128> LambdaSig;
112 llvm::raw_svector_ostream Out(LambdaSig);
113 Mangler->mangleLambdaSig(Lambda, Out);
114
115 return ++LambdaManglingNumbers[LambdaSig];
116 }
117
118 unsigned getManglingNumber(const BlockDecl *BD) override {
119 return ++BlockManglingNumber;
120 }
121
122 unsigned getStaticLocalNumber(const VarDecl *VD) override {
123 return 0;
124 }
125
126 /// Variable decls are numbered by identifier.
127 unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
128 if (auto *DD = dyn_cast<DecompositionDecl>(VD)) {
129 DecompositionDeclName Name{DD->bindings()};
130 return ++DecompsitionDeclManglingNumbers[Name];
131 }
132
133 const IdentifierInfo *Identifier = VD->getIdentifier();
134 if (!Identifier) {
135 // VarDecl without an identifier represents an anonymous union
136 // declaration.
137 Identifier = findAnonymousUnionVarDeclName(*VD);
138 }
139 return ++VarManglingNumbers[Identifier];
140 }
141
142 unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
143 return ++TagManglingNumbers[TD->getIdentifier()];
144 }
145};
146
147// A version of this for SYCL that makes sure that 'device' mangling context
148// matches the lambda mangling number, so that __builtin_sycl_unique_stable_name
149// can be consistently generated between a MS and Itanium host by just referring
150// to the device mangling number.
151class ItaniumSYCLNumberingContext : public ItaniumNumberingContext {
152 llvm::DenseMap<const CXXMethodDecl *, unsigned> ManglingNumbers;
153 using ManglingItr = decltype(ManglingNumbers)::iterator;
154
155public:
156 ItaniumSYCLNumberingContext(ItaniumMangleContext *Mangler)
157 : ItaniumNumberingContext(Mangler) {}
158
159 unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
160 unsigned Number = ItaniumNumberingContext::getManglingNumber(CallOperator);
161 std::pair<ManglingItr, bool> emplace_result =
162 ManglingNumbers.try_emplace(CallOperator, Number);
163 (void)emplace_result;
164 assert(emplace_result.second && "Lambda number set multiple times?");
165 return Number;
166 }
167
168 using ItaniumNumberingContext::getManglingNumber;
169
170 unsigned getDeviceManglingNumber(const CXXMethodDecl *CallOperator) override {
171 ManglingItr Itr = ManglingNumbers.find(CallOperator);
172 assert(Itr != ManglingNumbers.end() && "Lambda not yet mangled?");
173
174 return Itr->second;
175 }
176};
177
178class ItaniumCXXABI : public CXXABI {
179private:
180 std::unique_ptr<MangleContext> Mangler;
181protected:
182 ASTContext &Context;
183public:
184 ItaniumCXXABI(ASTContext &Ctx)
185 : Mangler(Ctx.createMangleContext()), Context(Ctx) {}
186
187 MemberPointerInfo
188 getMemberPointerInfo(const MemberPointerType *MPT) const override {
189 const TargetInfo &Target = Context.getTargetInfo();
190 TargetInfo::IntType PtrDiff = Target.getPtrDiffType(LangAS::Default);
191 MemberPointerInfo MPI;
192 MPI.Width = Target.getTypeWidth(PtrDiff);
193 MPI.Align = Target.getTypeAlign(PtrDiff);
194 MPI.HasPadding = false;
195 if (MPT->isMemberFunctionPointer())
196 MPI.Width *= 2;
197 return MPI;
198 }
199
200 CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
201 const llvm::Triple &T = Context.getTargetInfo().getTriple();
202 if (!isVariadic && T.isWindowsGNUEnvironment() &&
203 T.getArch() == llvm::Triple::x86)
204 return CC_X86ThisCall;
205 return Context.getTargetInfo().getDefaultCallingConv();
206 }
207
208 // We cheat and just check that the class has a vtable pointer, and that it's
209 // only big enough to have a vtable pointer and nothing more (or less).
210 bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
211
212 // Check that the class has a vtable pointer.
213 if (!RD->isDynamicClass())
214 return false;
215
216 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
217 CharUnits PointerSize = Context.toCharUnitsFromBits(
218 Context.getTargetInfo().getPointerWidth(LangAS::Default));
219 return Layout.getNonVirtualSize() == PointerSize;
220 }
221
222 const CXXConstructorDecl *
223 getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
224 return nullptr;
225 }
226
227 void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
228 CXXConstructorDecl *CD) override {}
229
230 void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
231 TypedefNameDecl *DD) override {}
232
233 TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
234 return nullptr;
235 }
236
237 void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
238 DeclaratorDecl *DD) override {}
239
240 DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
241 return nullptr;
242 }
243
244 std::unique_ptr<MangleNumberingContext>
245 createMangleNumberingContext() const override {
246 if (Context.getLangOpts().isSYCL())
247 return std::make_unique<ItaniumSYCLNumberingContext>(
248 cast<ItaniumMangleContext>(Mangler.get()));
249 return std::make_unique<ItaniumNumberingContext>(
250 cast<ItaniumMangleContext>(Mangler.get()));
251 }
252};
253}
254
256 return new ItaniumCXXABI(Ctx);
257}
258
259std::unique_ptr<MangleNumberingContext>
261 return std::make_unique<ItaniumNumberingContext>(
263}
Defines the clang::ASTContext interface.
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, clang::CharUnits CU)
Definition CharUnits.h:194
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
#define CXXABI(Name, Str)
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Implements C++ ABI-specific semantic analysis functions.
Definition CXXABI.h:29
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2296
bool isLambda() const
Determine whether this class describes a lambda function object.
Definition DeclCXX.h:1028
bool isDynamicClass() const
Definition DeclCXX.h:575
Represents a member of a struct/union/class.
Definition Decl.h:3341
One of these records is kept for each identifier that is lexed.
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition Mangle.h:56
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition TypeBase.h:3774
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:297
RecordDecl * castAsRecordDecl() const
Definition Type.h:48
QualType getType() const
Definition Decl.h:725
Represents a variable declaration or definition.
Definition Decl.h:934
Defines the clang::TargetInfo interface.
@ Number
Just a number, nothing else.
Definition Primitives.h:26
Top level wrappers for InstallAPI frontend operations.
std::unique_ptr< MangleNumberingContext > createItaniumNumberingContext(MangleContext *)
CXXABI * CreateItaniumCXXABI(ASTContext &Ctx)
Creates an instance of a C++ ABI class.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:282
@ CC_X86ThisCall
Definition Specifiers.h:286
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
llvm::DenseMapInfo< ArrayRef< const BindingDecl * > > ArrayInfo
static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS)
static unsigned getHashValue(DecompositionDeclName Key)