1 //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides C++ AST support targeting the Itanium C++ ABI, which is
12 // http://www.codesourcery.com/public/cxx-abi/abi.html
13 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
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
18 //===----------------------------------------------------------------------===//
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"
28 using namespace clang;
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<IdentifierInfo*, unsigned> VarManglingNumbers;
38 /// Variable decls are numbered by identifier.
39 virtual unsigned getManglingNumber(const VarDecl *VD) {
40 return ++VarManglingNumbers[VD->getIdentifier()];
44 class ItaniumCXXABI : public CXXABI {
48 ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
50 std::pair<uint64_t, unsigned>
51 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const {
52 const TargetInfo &Target = Context.getTargetInfo();
53 TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
54 uint64_t Width = Target.getTypeWidth(PtrDiff);
55 unsigned Align = Target.getTypeAlign(PtrDiff);
56 if (MPT->getPointeeType()->isFunctionType())
58 return std::make_pair(Width, Align);
61 CallingConv getDefaultMethodCallConv(bool isVariadic) const {
65 // We cheat and just check that the class has a vtable pointer, and that it's
66 // only big enough to have a vtable pointer and nothing more (or less).
67 bool isNearlyEmpty(const CXXRecordDecl *RD) const {
69 // Check that the class has a vtable pointer.
70 if (!RD->isDynamicClass())
73 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
74 CharUnits PointerSize =
75 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
76 return Layout.getNonVirtualSize() == PointerSize;
79 virtual MangleNumberingContext *createMangleNumberingContext() const {
80 return new ItaniumNumberingContext();
84 class ARMCXXABI : public ItaniumCXXABI {
86 ARMCXXABI(ASTContext &Ctx) : ItaniumCXXABI(Ctx) { }
90 CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
91 return new ItaniumCXXABI(Ctx);
94 CXXABI *clang::CreateARMCXXABI(ASTContext &Ctx) {
95 return new ARMCXXABI(Ctx);