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