1e5dd7070Spatrick //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This provides C++ AST support targeting the Itanium C++ ABI, which is
10e5dd7070Spatrick // documented at:
11e5dd7070Spatrick // http://www.codesourcery.com/public/cxx-abi/abi.html
12e5dd7070Spatrick // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
13e5dd7070Spatrick //
14e5dd7070Spatrick // It also supports the closely-related ARM C++ ABI, documented at:
15e5dd7070Spatrick // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
16e5dd7070Spatrick //
17e5dd7070Spatrick //===----------------------------------------------------------------------===//
18e5dd7070Spatrick
19e5dd7070Spatrick #include "CXXABI.h"
20e5dd7070Spatrick #include "clang/AST/ASTContext.h"
21e5dd7070Spatrick #include "clang/AST/DeclCXX.h"
22e5dd7070Spatrick #include "clang/AST/Mangle.h"
23e5dd7070Spatrick #include "clang/AST/MangleNumberingContext.h"
24e5dd7070Spatrick #include "clang/AST/RecordLayout.h"
25e5dd7070Spatrick #include "clang/AST/Type.h"
26e5dd7070Spatrick #include "clang/Basic/TargetInfo.h"
27e5dd7070Spatrick #include "llvm/ADT/FoldingSet.h"
28e5dd7070Spatrick #include "llvm/ADT/iterator.h"
29*12c85518Srobert #include <optional>
30e5dd7070Spatrick
31e5dd7070Spatrick using namespace clang;
32e5dd7070Spatrick
33e5dd7070Spatrick namespace {
34e5dd7070Spatrick
35e5dd7070Spatrick /// According to Itanium C++ ABI 5.1.2:
36e5dd7070Spatrick /// the name of an anonymous union is considered to be
37e5dd7070Spatrick /// the name of the first named data member found by a pre-order,
38e5dd7070Spatrick /// depth-first, declaration-order walk of the data members of
39e5dd7070Spatrick /// the anonymous union.
40e5dd7070Spatrick /// If there is no such data member (i.e., if all of the data members
41e5dd7070Spatrick /// in the union are unnamed), then there is no way for a program to
42e5dd7070Spatrick /// refer to the anonymous union, and there is therefore no need to mangle its name.
43e5dd7070Spatrick ///
44e5dd7070Spatrick /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
findAnonymousUnionVarDeclName(const VarDecl & VD)45e5dd7070Spatrick static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
46e5dd7070Spatrick const RecordType *RT = VD.getType()->getAs<RecordType>();
47e5dd7070Spatrick assert(RT && "type of VarDecl is expected to be RecordType.");
48e5dd7070Spatrick assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
49e5dd7070Spatrick if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
50e5dd7070Spatrick return FD->getIdentifier();
51e5dd7070Spatrick }
52e5dd7070Spatrick
53e5dd7070Spatrick return nullptr;
54e5dd7070Spatrick }
55e5dd7070Spatrick
56e5dd7070Spatrick /// The name of a decomposition declaration.
57e5dd7070Spatrick struct DecompositionDeclName {
58e5dd7070Spatrick using BindingArray = ArrayRef<const BindingDecl*>;
59e5dd7070Spatrick
60e5dd7070Spatrick /// Representative example of a set of bindings with these names.
61e5dd7070Spatrick BindingArray Bindings;
62e5dd7070Spatrick
63e5dd7070Spatrick /// Iterators over the sequence of identifiers in the name.
64e5dd7070Spatrick struct Iterator
65e5dd7070Spatrick : llvm::iterator_adaptor_base<Iterator, BindingArray::const_iterator,
66e5dd7070Spatrick std::random_access_iterator_tag,
67e5dd7070Spatrick const IdentifierInfo *> {
Iterator__anon150a7abe0111::DecompositionDeclName::Iterator68e5dd7070Spatrick Iterator(BindingArray::const_iterator It) : iterator_adaptor_base(It) {}
operator *__anon150a7abe0111::DecompositionDeclName::Iterator69e5dd7070Spatrick const IdentifierInfo *operator*() const {
70e5dd7070Spatrick return (*this->I)->getIdentifier();
71e5dd7070Spatrick }
72e5dd7070Spatrick };
begin__anon150a7abe0111::DecompositionDeclName73e5dd7070Spatrick Iterator begin() const { return Iterator(Bindings.begin()); }
end__anon150a7abe0111::DecompositionDeclName74e5dd7070Spatrick Iterator end() const { return Iterator(Bindings.end()); }
75e5dd7070Spatrick };
76e5dd7070Spatrick }
77e5dd7070Spatrick
78e5dd7070Spatrick namespace llvm {
isDenseMapKeyEmpty(T V)79e5dd7070Spatrick template<typename T> bool isDenseMapKeyEmpty(T V) {
80e5dd7070Spatrick return llvm::DenseMapInfo<T>::isEqual(
81e5dd7070Spatrick V, llvm::DenseMapInfo<T>::getEmptyKey());
82e5dd7070Spatrick }
isDenseMapKeyTombstone(T V)83e5dd7070Spatrick template<typename T> bool isDenseMapKeyTombstone(T V) {
84e5dd7070Spatrick return llvm::DenseMapInfo<T>::isEqual(
85e5dd7070Spatrick V, llvm::DenseMapInfo<T>::getTombstoneKey());
86e5dd7070Spatrick }
87e5dd7070Spatrick
88e5dd7070Spatrick template <typename T>
areDenseMapKeysEqualSpecialValues(T LHS,T RHS)89*12c85518Srobert std::optional<bool> areDenseMapKeysEqualSpecialValues(T LHS, T RHS) {
90e5dd7070Spatrick bool LHSEmpty = isDenseMapKeyEmpty(LHS);
91e5dd7070Spatrick bool RHSEmpty = isDenseMapKeyEmpty(RHS);
92e5dd7070Spatrick if (LHSEmpty || RHSEmpty)
93e5dd7070Spatrick return LHSEmpty && RHSEmpty;
94e5dd7070Spatrick
95e5dd7070Spatrick bool LHSTombstone = isDenseMapKeyTombstone(LHS);
96e5dd7070Spatrick bool RHSTombstone = isDenseMapKeyTombstone(RHS);
97e5dd7070Spatrick if (LHSTombstone || RHSTombstone)
98e5dd7070Spatrick return LHSTombstone && RHSTombstone;
99e5dd7070Spatrick
100*12c85518Srobert return std::nullopt;
101e5dd7070Spatrick }
102e5dd7070Spatrick
103e5dd7070Spatrick template<>
104e5dd7070Spatrick struct DenseMapInfo<DecompositionDeclName> {
105e5dd7070Spatrick using ArrayInfo = llvm::DenseMapInfo<ArrayRef<const BindingDecl*>>;
getEmptyKeyllvm::DenseMapInfo106e5dd7070Spatrick static DecompositionDeclName getEmptyKey() {
107e5dd7070Spatrick return {ArrayInfo::getEmptyKey()};
108e5dd7070Spatrick }
getTombstoneKeyllvm::DenseMapInfo109e5dd7070Spatrick static DecompositionDeclName getTombstoneKey() {
110e5dd7070Spatrick return {ArrayInfo::getTombstoneKey()};
111e5dd7070Spatrick }
getHashValuellvm::DenseMapInfo112e5dd7070Spatrick static unsigned getHashValue(DecompositionDeclName Key) {
113e5dd7070Spatrick assert(!isEqual(Key, getEmptyKey()) && !isEqual(Key, getTombstoneKey()));
114e5dd7070Spatrick return llvm::hash_combine_range(Key.begin(), Key.end());
115e5dd7070Spatrick }
isEqualllvm::DenseMapInfo116e5dd7070Spatrick static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS) {
117*12c85518Srobert if (std::optional<bool> Result =
118*12c85518Srobert areDenseMapKeysEqualSpecialValues(LHS.Bindings, RHS.Bindings))
119e5dd7070Spatrick return *Result;
120e5dd7070Spatrick
121e5dd7070Spatrick return LHS.Bindings.size() == RHS.Bindings.size() &&
122e5dd7070Spatrick std::equal(LHS.begin(), LHS.end(), RHS.begin());
123e5dd7070Spatrick }
124e5dd7070Spatrick };
125e5dd7070Spatrick }
126e5dd7070Spatrick
127e5dd7070Spatrick namespace {
128e5dd7070Spatrick
129e5dd7070Spatrick /// Keeps track of the mangled names of lambda expressions and block
130e5dd7070Spatrick /// literals within a particular context.
131e5dd7070Spatrick class ItaniumNumberingContext : public MangleNumberingContext {
132e5dd7070Spatrick ItaniumMangleContext *Mangler;
133e5dd7070Spatrick llvm::StringMap<unsigned> LambdaManglingNumbers;
134e5dd7070Spatrick unsigned BlockManglingNumber = 0;
135e5dd7070Spatrick llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
136e5dd7070Spatrick llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
137e5dd7070Spatrick llvm::DenseMap<DecompositionDeclName, unsigned>
138e5dd7070Spatrick DecompsitionDeclManglingNumbers;
139e5dd7070Spatrick
140e5dd7070Spatrick public:
ItaniumNumberingContext(ItaniumMangleContext * Mangler)141e5dd7070Spatrick ItaniumNumberingContext(ItaniumMangleContext *Mangler) : Mangler(Mangler) {}
142e5dd7070Spatrick
getManglingNumber(const CXXMethodDecl * CallOperator)143e5dd7070Spatrick unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
144e5dd7070Spatrick const CXXRecordDecl *Lambda = CallOperator->getParent();
145e5dd7070Spatrick assert(Lambda->isLambda());
146e5dd7070Spatrick
147e5dd7070Spatrick // Computation of the <lambda-sig> is non-trivial and subtle. Rather than
148e5dd7070Spatrick // duplicating it here, just mangle the <lambda-sig> directly.
149e5dd7070Spatrick llvm::SmallString<128> LambdaSig;
150e5dd7070Spatrick llvm::raw_svector_ostream Out(LambdaSig);
151e5dd7070Spatrick Mangler->mangleLambdaSig(Lambda, Out);
152e5dd7070Spatrick
153e5dd7070Spatrick return ++LambdaManglingNumbers[LambdaSig];
154e5dd7070Spatrick }
155e5dd7070Spatrick
getManglingNumber(const BlockDecl * BD)156e5dd7070Spatrick unsigned getManglingNumber(const BlockDecl *BD) override {
157e5dd7070Spatrick return ++BlockManglingNumber;
158e5dd7070Spatrick }
159e5dd7070Spatrick
getStaticLocalNumber(const VarDecl * VD)160e5dd7070Spatrick unsigned getStaticLocalNumber(const VarDecl *VD) override {
161e5dd7070Spatrick return 0;
162e5dd7070Spatrick }
163e5dd7070Spatrick
164e5dd7070Spatrick /// Variable decls are numbered by identifier.
getManglingNumber(const VarDecl * VD,unsigned)165e5dd7070Spatrick unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
166e5dd7070Spatrick if (auto *DD = dyn_cast<DecompositionDecl>(VD)) {
167e5dd7070Spatrick DecompositionDeclName Name{DD->bindings()};
168e5dd7070Spatrick return ++DecompsitionDeclManglingNumbers[Name];
169e5dd7070Spatrick }
170e5dd7070Spatrick
171e5dd7070Spatrick const IdentifierInfo *Identifier = VD->getIdentifier();
172e5dd7070Spatrick if (!Identifier) {
173e5dd7070Spatrick // VarDecl without an identifier represents an anonymous union
174e5dd7070Spatrick // declaration.
175e5dd7070Spatrick Identifier = findAnonymousUnionVarDeclName(*VD);
176e5dd7070Spatrick }
177e5dd7070Spatrick return ++VarManglingNumbers[Identifier];
178e5dd7070Spatrick }
179e5dd7070Spatrick
getManglingNumber(const TagDecl * TD,unsigned)180e5dd7070Spatrick unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
181e5dd7070Spatrick return ++TagManglingNumbers[TD->getIdentifier()];
182e5dd7070Spatrick }
183e5dd7070Spatrick };
184e5dd7070Spatrick
185*12c85518Srobert // A version of this for SYCL that makes sure that 'device' mangling context
186*12c85518Srobert // matches the lambda mangling number, so that __builtin_sycl_unique_stable_name
187*12c85518Srobert // can be consistently generated between a MS and Itanium host by just referring
188*12c85518Srobert // to the device mangling number.
189*12c85518Srobert class ItaniumSYCLNumberingContext : public ItaniumNumberingContext {
190*12c85518Srobert llvm::DenseMap<const CXXMethodDecl *, unsigned> ManglingNumbers;
191*12c85518Srobert using ManglingItr = decltype(ManglingNumbers)::iterator;
192*12c85518Srobert
193*12c85518Srobert public:
ItaniumSYCLNumberingContext(ItaniumMangleContext * Mangler)194*12c85518Srobert ItaniumSYCLNumberingContext(ItaniumMangleContext *Mangler)
195*12c85518Srobert : ItaniumNumberingContext(Mangler) {}
196*12c85518Srobert
getManglingNumber(const CXXMethodDecl * CallOperator)197*12c85518Srobert unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
198*12c85518Srobert unsigned Number = ItaniumNumberingContext::getManglingNumber(CallOperator);
199*12c85518Srobert std::pair<ManglingItr, bool> emplace_result =
200*12c85518Srobert ManglingNumbers.try_emplace(CallOperator, Number);
201*12c85518Srobert (void)emplace_result;
202*12c85518Srobert assert(emplace_result.second && "Lambda number set multiple times?");
203*12c85518Srobert return Number;
204*12c85518Srobert }
205*12c85518Srobert
206*12c85518Srobert using ItaniumNumberingContext::getManglingNumber;
207*12c85518Srobert
getDeviceManglingNumber(const CXXMethodDecl * CallOperator)208*12c85518Srobert unsigned getDeviceManglingNumber(const CXXMethodDecl *CallOperator) override {
209*12c85518Srobert ManglingItr Itr = ManglingNumbers.find(CallOperator);
210*12c85518Srobert assert(Itr != ManglingNumbers.end() && "Lambda not yet mangled?");
211*12c85518Srobert
212*12c85518Srobert return Itr->second;
213*12c85518Srobert }
214*12c85518Srobert };
215*12c85518Srobert
216e5dd7070Spatrick class ItaniumCXXABI : public CXXABI {
217e5dd7070Spatrick private:
218e5dd7070Spatrick std::unique_ptr<MangleContext> Mangler;
219e5dd7070Spatrick protected:
220e5dd7070Spatrick ASTContext &Context;
221e5dd7070Spatrick public:
ItaniumCXXABI(ASTContext & Ctx)222e5dd7070Spatrick ItaniumCXXABI(ASTContext &Ctx)
223e5dd7070Spatrick : Mangler(Ctx.createMangleContext()), Context(Ctx) {}
224e5dd7070Spatrick
225e5dd7070Spatrick MemberPointerInfo
getMemberPointerInfo(const MemberPointerType * MPT) const226e5dd7070Spatrick getMemberPointerInfo(const MemberPointerType *MPT) const override {
227e5dd7070Spatrick const TargetInfo &Target = Context.getTargetInfo();
228*12c85518Srobert TargetInfo::IntType PtrDiff = Target.getPtrDiffType(LangAS::Default);
229e5dd7070Spatrick MemberPointerInfo MPI;
230e5dd7070Spatrick MPI.Width = Target.getTypeWidth(PtrDiff);
231e5dd7070Spatrick MPI.Align = Target.getTypeAlign(PtrDiff);
232e5dd7070Spatrick MPI.HasPadding = false;
233e5dd7070Spatrick if (MPT->isMemberFunctionPointer())
234e5dd7070Spatrick MPI.Width *= 2;
235e5dd7070Spatrick return MPI;
236e5dd7070Spatrick }
237e5dd7070Spatrick
getDefaultMethodCallConv(bool isVariadic) const238e5dd7070Spatrick CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
239e5dd7070Spatrick const llvm::Triple &T = Context.getTargetInfo().getTriple();
240e5dd7070Spatrick if (!isVariadic && T.isWindowsGNUEnvironment() &&
241e5dd7070Spatrick T.getArch() == llvm::Triple::x86)
242e5dd7070Spatrick return CC_X86ThisCall;
243e5dd7070Spatrick return Context.getTargetInfo().getDefaultCallingConv();
244e5dd7070Spatrick }
245e5dd7070Spatrick
246e5dd7070Spatrick // We cheat and just check that the class has a vtable pointer, and that it's
247e5dd7070Spatrick // only big enough to have a vtable pointer and nothing more (or less).
isNearlyEmpty(const CXXRecordDecl * RD) const248e5dd7070Spatrick bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
249e5dd7070Spatrick
250e5dd7070Spatrick // Check that the class has a vtable pointer.
251e5dd7070Spatrick if (!RD->isDynamicClass())
252e5dd7070Spatrick return false;
253e5dd7070Spatrick
254e5dd7070Spatrick const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
255*12c85518Srobert CharUnits PointerSize = Context.toCharUnitsFromBits(
256*12c85518Srobert Context.getTargetInfo().getPointerWidth(LangAS::Default));
257e5dd7070Spatrick return Layout.getNonVirtualSize() == PointerSize;
258e5dd7070Spatrick }
259e5dd7070Spatrick
260e5dd7070Spatrick const CXXConstructorDecl *
getCopyConstructorForExceptionObject(CXXRecordDecl * RD)261e5dd7070Spatrick getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
262e5dd7070Spatrick return nullptr;
263e5dd7070Spatrick }
264e5dd7070Spatrick
addCopyConstructorForExceptionObject(CXXRecordDecl * RD,CXXConstructorDecl * CD)265e5dd7070Spatrick void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
266e5dd7070Spatrick CXXConstructorDecl *CD) override {}
267e5dd7070Spatrick
addTypedefNameForUnnamedTagDecl(TagDecl * TD,TypedefNameDecl * DD)268e5dd7070Spatrick void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
269e5dd7070Spatrick TypedefNameDecl *DD) override {}
270e5dd7070Spatrick
getTypedefNameForUnnamedTagDecl(const TagDecl * TD)271e5dd7070Spatrick TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
272e5dd7070Spatrick return nullptr;
273e5dd7070Spatrick }
274e5dd7070Spatrick
addDeclaratorForUnnamedTagDecl(TagDecl * TD,DeclaratorDecl * DD)275e5dd7070Spatrick void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
276e5dd7070Spatrick DeclaratorDecl *DD) override {}
277e5dd7070Spatrick
getDeclaratorForUnnamedTagDecl(const TagDecl * TD)278e5dd7070Spatrick DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
279e5dd7070Spatrick return nullptr;
280e5dd7070Spatrick }
281e5dd7070Spatrick
282e5dd7070Spatrick std::unique_ptr<MangleNumberingContext>
createMangleNumberingContext() const283e5dd7070Spatrick createMangleNumberingContext() const override {
284*12c85518Srobert if (Context.getLangOpts().isSYCL())
285*12c85518Srobert return std::make_unique<ItaniumSYCLNumberingContext>(
286*12c85518Srobert cast<ItaniumMangleContext>(Mangler.get()));
287e5dd7070Spatrick return std::make_unique<ItaniumNumberingContext>(
288e5dd7070Spatrick cast<ItaniumMangleContext>(Mangler.get()));
289e5dd7070Spatrick }
290e5dd7070Spatrick };
291e5dd7070Spatrick }
292e5dd7070Spatrick
CreateItaniumCXXABI(ASTContext & Ctx)293e5dd7070Spatrick CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
294e5dd7070Spatrick return new ItaniumCXXABI(Ctx);
295e5dd7070Spatrick }
296a9ac8606Spatrick
297a9ac8606Spatrick std::unique_ptr<MangleNumberingContext>
createItaniumNumberingContext(MangleContext * Mangler)298a9ac8606Spatrick clang::createItaniumNumberingContext(MangleContext *Mangler) {
299a9ac8606Spatrick return std::make_unique<ItaniumNumberingContext>(
300a9ac8606Spatrick cast<ItaniumMangleContext>(Mangler));
301a9ac8606Spatrick }
302