blob: 51308ea0c0f550fe694ecb77b99ee132a97443c2 [file] [log] [blame]
Charles Davis071cc7d2010-08-16 03:33:14 +00001//===------- MicrosoftCXXABI.cpp - AST support for the Microsoft 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 Microsoft Visual C++
Charles Davis071cc7d2010-08-16 03:33:14 +000011// ABI.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CXXABI.h"
16#include "clang/AST/ASTContext.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000017#include "clang/AST/DeclCXX.h"
Anders Carlssondae0cb52010-11-25 01:51:53 +000018#include "clang/AST/RecordLayout.h"
19#include "clang/AST/Type.h"
20#include "clang/Basic/TargetInfo.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000021
22using namespace clang;
23
24namespace {
25class MicrosoftCXXABI : public CXXABI {
26 ASTContext &Context;
27public:
28 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
29
30 unsigned getMemberPointerSize(const MemberPointerType *MPT) const;
Charles Davis424ae982010-10-29 03:25:11 +000031
Timur Iskhodzhanov8f88a1d2012-07-12 09:50:54 +000032 CallingConv getDefaultMethodCallConv(bool isVariadic) const {
33 if (!isVariadic && Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
Charles Davis424ae982010-10-29 03:25:11 +000034 return CC_X86ThisCall;
35 else
36 return CC_C;
37 }
Anders Carlssondae0cb52010-11-25 01:51:53 +000038
39 bool isNearlyEmpty(const CXXRecordDecl *RD) const {
40 // FIXME: Audit the corners
41 if (!RD->isDynamicClass())
42 return false;
43
44 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
45
46 // In the Microsoft ABI, classes can have one or two vtable pointers.
Ken Dyck5c3633f2011-02-01 01:52:10 +000047 CharUnits PointerSize =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000048 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck5c3633f2011-02-01 01:52:10 +000049 return Layout.getNonVirtualSize() == PointerSize ||
50 Layout.getNonVirtualSize() == PointerSize * 2;
Anders Carlssondae0cb52010-11-25 01:51:53 +000051 }
Charles Davis071cc7d2010-08-16 03:33:14 +000052};
53}
54
55unsigned MicrosoftCXXABI::getMemberPointerSize(const MemberPointerType *MPT) const {
56 QualType Pointee = MPT->getPointeeType();
57 CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
Charles Davis071cc7d2010-08-16 03:33:14 +000058 if (RD->getNumVBases() > 0) {
59 if (Pointee->isFunctionType())
60 return 3;
61 else
62 return 2;
63 } else if (RD->getNumBases() > 1 && Pointee->isFunctionType())
64 return 2;
65 return 1;
66}
67
68CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
69 return new MicrosoftCXXABI(Ctx);
70}
71