blob: 578466028ce8feec32c6fe99f6499862e1ad3411 [file] [log] [blame]
Charles Davis071cc7d2010-08-16 03:33:14 +00001//===------- 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//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides C++ AST support targeting the Itanium C++ ABI, which is
Charles Davis071cc7d2010-08-16 03:33:14 +000011// documented at:
12// http://www.codesourcery.com/public/cxx-abi/abi.html
13// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
John McCallee79a4c2010-08-21 22:46:04 +000014//
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//
Charles Davis071cc7d2010-08-16 03:33:14 +000018//===----------------------------------------------------------------------===//
19
20#include "CXXABI.h"
21#include "clang/AST/ASTContext.h"
Anders Carlssondae0cb52010-11-25 01:51:53 +000022#include "clang/AST/DeclCXX.h"
Reid Kleckner942f9fe2013-09-10 20:14:30 +000023#include "clang/AST/MangleNumberingContext.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "clang/AST/RecordLayout.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000025#include "clang/AST/Type.h"
Anders Carlssondae0cb52010-11-25 01:51:53 +000026#include "clang/Basic/TargetInfo.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000027
28using namespace clang;
29
30namespace {
Reid Kleckner942f9fe2013-09-10 20:14:30 +000031
32/// \brief Keeps track of the mangled names of lambda expressions and block
33/// literals within a particular context.
34class ItaniumNumberingContext : public MangleNumberingContext {
35 llvm::DenseMap<IdentifierInfo*, unsigned> VarManglingNumbers;
36
37public:
38 /// Variable decls are numbered by identifier.
39 virtual unsigned getManglingNumber(const VarDecl *VD) {
40 return ++VarManglingNumbers[VD->getIdentifier()];
41 }
42};
43
Charles Davis071cc7d2010-08-16 03:33:14 +000044class ItaniumCXXABI : public CXXABI {
John McCallee79a4c2010-08-21 22:46:04 +000045protected:
Charles Davis071cc7d2010-08-16 03:33:14 +000046 ASTContext &Context;
47public:
48 ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
49
Reid Kleckner84e9ab42013-03-28 20:02:56 +000050 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())
57 Width = 2 * Width;
58 return std::make_pair(Width, Align);
Charles Davis071cc7d2010-08-16 03:33:14 +000059 }
Charles Davis424ae982010-10-29 03:25:11 +000060
Timur Iskhodzhanov8f88a1d2012-07-12 09:50:54 +000061 CallingConv getDefaultMethodCallConv(bool isVariadic) const {
Charles Davis424ae982010-10-29 03:25:11 +000062 return CC_C;
63 }
Anders Carlssondae0cb52010-11-25 01:51:53 +000064
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 {
68
69 // Check that the class has a vtable pointer.
70 if (!RD->isDynamicClass())
71 return false;
72
73 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Ken Dyck5c3633f2011-02-01 01:52:10 +000074 CharUnits PointerSize =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000075 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck5c3633f2011-02-01 01:52:10 +000076 return Layout.getNonVirtualSize() == PointerSize;
Anders Carlssondae0cb52010-11-25 01:51:53 +000077 }
Reid Kleckner942f9fe2013-09-10 20:14:30 +000078
79 virtual MangleNumberingContext *createMangleNumberingContext() const {
80 return new ItaniumNumberingContext();
81 }
Charles Davis071cc7d2010-08-16 03:33:14 +000082};
John McCallee79a4c2010-08-21 22:46:04 +000083
84class ARMCXXABI : public ItaniumCXXABI {
85public:
86 ARMCXXABI(ASTContext &Ctx) : ItaniumCXXABI(Ctx) { }
87};
Charles Davis071cc7d2010-08-16 03:33:14 +000088}
89
90CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
91 return new ItaniumCXXABI(Ctx);
92}
93
John McCallee79a4c2010-08-21 22:46:04 +000094CXXABI *clang::CreateARMCXXABI(ASTContext &Ctx) {
95 return new ARMCXXABI(Ctx);
96}