1 //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides C++ AST support targeting the Itanium C++ ABI, which is
11 // documented at:
12 //  http://www.codesourcery.com/public/cxx-abi/abi.html
13 //  http://www.codesourcery.com/public/cxx-abi/abi-eh.html
14 //
15 // It also supports the closely-related ARM C++ ABI, documented at:
16 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "CXXABI.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclCXX.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 
28 using namespace clang;
29 
30 namespace {
31 
32 /// \brief Keeps track of the mangled names of lambda expressions and block
33 /// literals within a particular context.
34 class ItaniumNumberingContext : public MangleNumberingContext {
35   llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
36   llvm::DenseMap<IdentifierInfo *, unsigned> VarManglingNumbers;
37   llvm::DenseMap<IdentifierInfo *, unsigned> TagManglingNumbers;
38 
39 public:
40   unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
41     const FunctionProtoType *Proto =
42         CallOperator->getType()->getAs<FunctionProtoType>();
43     ASTContext &Context = CallOperator->getASTContext();
44 
45     QualType Key =
46         Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(),
47                                 FunctionProtoType::ExtProtoInfo());
48     Key = Context.getCanonicalType(Key);
49     return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
50   }
51 
52   unsigned getManglingNumber(const BlockDecl *BD) override {
53     const Type *Ty = nullptr;
54     return ++ManglingNumbers[Ty];
55   }
56 
57   unsigned getStaticLocalNumber(const VarDecl *VD) override {
58     return 0;
59   }
60 
61   /// Variable decls are numbered by identifier.
62   unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
63     return ++VarManglingNumbers[VD->getIdentifier()];
64   }
65 
66   unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
67     return ++TagManglingNumbers[TD->getIdentifier()];
68   }
69 };
70 
71 class ItaniumCXXABI : public CXXABI {
72 protected:
73   ASTContext &Context;
74 public:
75   ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
76 
77   std::pair<uint64_t, unsigned>
78   getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override {
79     const TargetInfo &Target = Context.getTargetInfo();
80     TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
81     uint64_t Width = Target.getTypeWidth(PtrDiff);
82     unsigned Align = Target.getTypeAlign(PtrDiff);
83     if (MPT->getPointeeType()->isFunctionType())
84       Width = 2 * Width;
85     return std::make_pair(Width, Align);
86   }
87 
88   CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
89     const llvm::Triple &T = Context.getTargetInfo().getTriple();
90     if (!isVariadic && T.isWindowsGNUEnvironment() &&
91         T.getArch() == llvm::Triple::x86)
92       return CC_X86ThisCall;
93     return CC_C;
94   }
95 
96   // We cheat and just check that the class has a vtable pointer, and that it's
97   // only big enough to have a vtable pointer and nothing more (or less).
98   bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
99 
100     // Check that the class has a vtable pointer.
101     if (!RD->isDynamicClass())
102       return false;
103 
104     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
105     CharUnits PointerSize =
106       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
107     return Layout.getNonVirtualSize() == PointerSize;
108   }
109 
110   MangleNumberingContext *createMangleNumberingContext() const override {
111     return new ItaniumNumberingContext();
112   }
113 };
114 }
115 
116 CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
117   return new ItaniumCXXABI(Ctx);
118 }
119