]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/AST/ItaniumCXXABI.cpp
Merge llvm 3.6.0rc4 from ^/vendor/llvm/dist, merge clang 3.6.0rc4 from
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / AST / ItaniumCXXABI.cpp
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 /// According to Itanium C++ ABI 5.1.2:
33 /// the name of an anonymous union is considered to be
34 /// the name of the first named data member found by a pre-order,
35 /// depth-first, declaration-order walk of the data members of
36 /// the anonymous union.
37 /// If there is no such data member (i.e., if all of the data members
38 /// in the union are unnamed), then there is no way for a program to
39 /// refer to the anonymous union, and there is therefore no need to mangle its name.
40 ///
41 /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
42 static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
43   const RecordType *RT = VD.getType()->getAs<RecordType>();
44   assert(RT && "type of VarDecl is expected to be RecordType.");
45   assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
46   if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
47     return FD->getIdentifier();
48   }
49
50   return nullptr;
51 }
52
53 /// \brief Keeps track of the mangled names of lambda expressions and block
54 /// literals within a particular context.
55 class ItaniumNumberingContext : public MangleNumberingContext {
56   llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
57   llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
58   llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
59
60 public:
61   unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
62     const FunctionProtoType *Proto =
63         CallOperator->getType()->getAs<FunctionProtoType>();
64     ASTContext &Context = CallOperator->getASTContext();
65
66     QualType Key =
67         Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(),
68                                 FunctionProtoType::ExtProtoInfo());
69     Key = Context.getCanonicalType(Key);
70     return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
71   }
72
73   unsigned getManglingNumber(const BlockDecl *BD) override {
74     const Type *Ty = nullptr;
75     return ++ManglingNumbers[Ty];
76   }
77
78   unsigned getStaticLocalNumber(const VarDecl *VD) override {
79     return 0;
80   }
81
82   /// Variable decls are numbered by identifier.
83   unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
84     const IdentifierInfo *Identifier = VD->getIdentifier();
85     if (!Identifier) {
86       // VarDecl without an identifier represents an anonymous union declaration.
87       Identifier = findAnonymousUnionVarDeclName(*VD);
88     }
89     return ++VarManglingNumbers[Identifier];
90   }
91
92   unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
93     return ++TagManglingNumbers[TD->getIdentifier()];
94   }
95 };
96
97 class ItaniumCXXABI : public CXXABI {
98 protected:
99   ASTContext &Context;
100 public:
101   ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
102
103   std::pair<uint64_t, unsigned>
104   getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override {
105     const TargetInfo &Target = Context.getTargetInfo();
106     TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
107     uint64_t Width = Target.getTypeWidth(PtrDiff);
108     unsigned Align = Target.getTypeAlign(PtrDiff);
109     if (MPT->getPointeeType()->isFunctionType())
110       Width = 2 * Width;
111     return std::make_pair(Width, Align);
112   }
113
114   CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
115     const llvm::Triple &T = Context.getTargetInfo().getTriple();
116     if (!isVariadic && T.isWindowsGNUEnvironment() &&
117         T.getArch() == llvm::Triple::x86)
118       return CC_X86ThisCall;
119     return CC_C;
120   }
121
122   // We cheat and just check that the class has a vtable pointer, and that it's
123   // only big enough to have a vtable pointer and nothing more (or less).
124   bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
125
126     // Check that the class has a vtable pointer.
127     if (!RD->isDynamicClass())
128       return false;
129
130     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
131     CharUnits PointerSize = 
132       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
133     return Layout.getNonVirtualSize() == PointerSize;
134   }
135
136   MangleNumberingContext *createMangleNumberingContext() const override {
137     return new ItaniumNumberingContext();
138   }
139 };
140 }
141
142 CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
143   return new ItaniumCXXABI(Ctx);
144 }