xref: /aosp_15_r20/external/clang/lib/AST/ItaniumCXXABI.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This provides C++ AST support targeting the Itanium C++ ABI, which is
11*67e74705SXin Li // documented at:
12*67e74705SXin Li //  http://www.codesourcery.com/public/cxx-abi/abi.html
13*67e74705SXin Li //  http://www.codesourcery.com/public/cxx-abi/abi-eh.html
14*67e74705SXin Li //
15*67e74705SXin Li // It also supports the closely-related ARM C++ ABI, documented at:
16*67e74705SXin Li // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
17*67e74705SXin Li //
18*67e74705SXin Li //===----------------------------------------------------------------------===//
19*67e74705SXin Li 
20*67e74705SXin Li #include "CXXABI.h"
21*67e74705SXin Li #include "clang/AST/ASTContext.h"
22*67e74705SXin Li #include "clang/AST/DeclCXX.h"
23*67e74705SXin Li #include "clang/AST/MangleNumberingContext.h"
24*67e74705SXin Li #include "clang/AST/RecordLayout.h"
25*67e74705SXin Li #include "clang/AST/Type.h"
26*67e74705SXin Li #include "clang/Basic/TargetInfo.h"
27*67e74705SXin Li 
28*67e74705SXin Li using namespace clang;
29*67e74705SXin Li 
30*67e74705SXin Li namespace {
31*67e74705SXin Li 
32*67e74705SXin Li /// According to Itanium C++ ABI 5.1.2:
33*67e74705SXin Li /// the name of an anonymous union is considered to be
34*67e74705SXin Li /// the name of the first named data member found by a pre-order,
35*67e74705SXin Li /// depth-first, declaration-order walk of the data members of
36*67e74705SXin Li /// the anonymous union.
37*67e74705SXin Li /// If there is no such data member (i.e., if all of the data members
38*67e74705SXin Li /// in the union are unnamed), then there is no way for a program to
39*67e74705SXin Li /// refer to the anonymous union, and there is therefore no need to mangle its name.
40*67e74705SXin Li ///
41*67e74705SXin Li /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
findAnonymousUnionVarDeclName(const VarDecl & VD)42*67e74705SXin Li static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
43*67e74705SXin Li   const RecordType *RT = VD.getType()->getAs<RecordType>();
44*67e74705SXin Li   assert(RT && "type of VarDecl is expected to be RecordType.");
45*67e74705SXin Li   assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
46*67e74705SXin Li   if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
47*67e74705SXin Li     return FD->getIdentifier();
48*67e74705SXin Li   }
49*67e74705SXin Li 
50*67e74705SXin Li   return nullptr;
51*67e74705SXin Li }
52*67e74705SXin Li 
53*67e74705SXin Li /// \brief Keeps track of the mangled names of lambda expressions and block
54*67e74705SXin Li /// literals within a particular context.
55*67e74705SXin Li class ItaniumNumberingContext : public MangleNumberingContext {
56*67e74705SXin Li   llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
57*67e74705SXin Li   llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
58*67e74705SXin Li   llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
59*67e74705SXin Li 
60*67e74705SXin Li public:
getManglingNumber(const CXXMethodDecl * CallOperator)61*67e74705SXin Li   unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
62*67e74705SXin Li     const FunctionProtoType *Proto =
63*67e74705SXin Li         CallOperator->getType()->getAs<FunctionProtoType>();
64*67e74705SXin Li     ASTContext &Context = CallOperator->getASTContext();
65*67e74705SXin Li 
66*67e74705SXin Li     QualType Key =
67*67e74705SXin Li         Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(),
68*67e74705SXin Li                                 FunctionProtoType::ExtProtoInfo());
69*67e74705SXin Li     Key = Context.getCanonicalType(Key);
70*67e74705SXin Li     return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
71*67e74705SXin Li   }
72*67e74705SXin Li 
getManglingNumber(const BlockDecl * BD)73*67e74705SXin Li   unsigned getManglingNumber(const BlockDecl *BD) override {
74*67e74705SXin Li     const Type *Ty = nullptr;
75*67e74705SXin Li     return ++ManglingNumbers[Ty];
76*67e74705SXin Li   }
77*67e74705SXin Li 
getStaticLocalNumber(const VarDecl * VD)78*67e74705SXin Li   unsigned getStaticLocalNumber(const VarDecl *VD) override {
79*67e74705SXin Li     return 0;
80*67e74705SXin Li   }
81*67e74705SXin Li 
82*67e74705SXin Li   /// Variable decls are numbered by identifier.
getManglingNumber(const VarDecl * VD,unsigned)83*67e74705SXin Li   unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
84*67e74705SXin Li     const IdentifierInfo *Identifier = VD->getIdentifier();
85*67e74705SXin Li     if (!Identifier) {
86*67e74705SXin Li       // VarDecl without an identifier represents an anonymous union declaration.
87*67e74705SXin Li       Identifier = findAnonymousUnionVarDeclName(*VD);
88*67e74705SXin Li     }
89*67e74705SXin Li     return ++VarManglingNumbers[Identifier];
90*67e74705SXin Li   }
91*67e74705SXin Li 
getManglingNumber(const TagDecl * TD,unsigned)92*67e74705SXin Li   unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
93*67e74705SXin Li     return ++TagManglingNumbers[TD->getIdentifier()];
94*67e74705SXin Li   }
95*67e74705SXin Li };
96*67e74705SXin Li 
97*67e74705SXin Li class ItaniumCXXABI : public CXXABI {
98*67e74705SXin Li protected:
99*67e74705SXin Li   ASTContext &Context;
100*67e74705SXin Li public:
ItaniumCXXABI(ASTContext & Ctx)101*67e74705SXin Li   ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
102*67e74705SXin Li 
103*67e74705SXin Li   std::pair<uint64_t, unsigned>
getMemberPointerWidthAndAlign(const MemberPointerType * MPT) const104*67e74705SXin Li   getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override {
105*67e74705SXin Li     const TargetInfo &Target = Context.getTargetInfo();
106*67e74705SXin Li     TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
107*67e74705SXin Li     uint64_t Width = Target.getTypeWidth(PtrDiff);
108*67e74705SXin Li     unsigned Align = Target.getTypeAlign(PtrDiff);
109*67e74705SXin Li     if (MPT->isMemberFunctionPointer())
110*67e74705SXin Li       Width = 2 * Width;
111*67e74705SXin Li     return std::make_pair(Width, Align);
112*67e74705SXin Li   }
113*67e74705SXin Li 
getDefaultMethodCallConv(bool isVariadic) const114*67e74705SXin Li   CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
115*67e74705SXin Li     const llvm::Triple &T = Context.getTargetInfo().getTriple();
116*67e74705SXin Li     if (!isVariadic && T.isWindowsGNUEnvironment() &&
117*67e74705SXin Li         T.getArch() == llvm::Triple::x86)
118*67e74705SXin Li       return CC_X86ThisCall;
119*67e74705SXin Li     return CC_C;
120*67e74705SXin Li   }
121*67e74705SXin Li 
122*67e74705SXin Li   // We cheat and just check that the class has a vtable pointer, and that it's
123*67e74705SXin Li   // only big enough to have a vtable pointer and nothing more (or less).
isNearlyEmpty(const CXXRecordDecl * RD) const124*67e74705SXin Li   bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
125*67e74705SXin Li 
126*67e74705SXin Li     // Check that the class has a vtable pointer.
127*67e74705SXin Li     if (!RD->isDynamicClass())
128*67e74705SXin Li       return false;
129*67e74705SXin Li 
130*67e74705SXin Li     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
131*67e74705SXin Li     CharUnits PointerSize =
132*67e74705SXin Li       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
133*67e74705SXin Li     return Layout.getNonVirtualSize() == PointerSize;
134*67e74705SXin Li   }
135*67e74705SXin Li 
136*67e74705SXin Li   const CXXConstructorDecl *
getCopyConstructorForExceptionObject(CXXRecordDecl * RD)137*67e74705SXin Li   getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
138*67e74705SXin Li     return nullptr;
139*67e74705SXin Li   }
140*67e74705SXin Li 
addCopyConstructorForExceptionObject(CXXRecordDecl * RD,CXXConstructorDecl * CD)141*67e74705SXin Li   void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
142*67e74705SXin Li                                             CXXConstructorDecl *CD) override {}
143*67e74705SXin Li 
addDefaultArgExprForConstructor(const CXXConstructorDecl * CD,unsigned ParmIdx,Expr * DAE)144*67e74705SXin Li   void addDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
145*67e74705SXin Li                                        unsigned ParmIdx, Expr *DAE) override {}
146*67e74705SXin Li 
getDefaultArgExprForConstructor(const CXXConstructorDecl * CD,unsigned ParmIdx)147*67e74705SXin Li   Expr *getDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
148*67e74705SXin Li                                         unsigned ParmIdx) override {
149*67e74705SXin Li     return nullptr;
150*67e74705SXin Li   }
151*67e74705SXin Li 
addTypedefNameForUnnamedTagDecl(TagDecl * TD,TypedefNameDecl * DD)152*67e74705SXin Li   void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
153*67e74705SXin Li                                        TypedefNameDecl *DD) override {}
154*67e74705SXin Li 
getTypedefNameForUnnamedTagDecl(const TagDecl * TD)155*67e74705SXin Li   TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
156*67e74705SXin Li     return nullptr;
157*67e74705SXin Li   }
158*67e74705SXin Li 
addDeclaratorForUnnamedTagDecl(TagDecl * TD,DeclaratorDecl * DD)159*67e74705SXin Li   void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
160*67e74705SXin Li                                       DeclaratorDecl *DD) override {}
161*67e74705SXin Li 
getDeclaratorForUnnamedTagDecl(const TagDecl * TD)162*67e74705SXin Li   DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
163*67e74705SXin Li     return nullptr;
164*67e74705SXin Li   }
165*67e74705SXin Li 
createMangleNumberingContext() const166*67e74705SXin Li   MangleNumberingContext *createMangleNumberingContext() const override {
167*67e74705SXin Li     return new ItaniumNumberingContext();
168*67e74705SXin Li   }
169*67e74705SXin Li };
170*67e74705SXin Li }
171*67e74705SXin Li 
CreateItaniumCXXABI(ASTContext & Ctx)172*67e74705SXin Li CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
173*67e74705SXin Li   return new ItaniumCXXABI(Ctx);
174*67e74705SXin Li }
175