clang  14.0.0git
MicrosoftCXXABI.cpp
Go to the documentation of this file.
1 //===------- MicrosoftCXXABI.cpp - AST support for the Microsoft C++ ABI --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This provides C++ AST support targeting the Microsoft Visual C++
10 // ABI.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CXXABI.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Mangle.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Basic/TargetInfo.h"
24 
25 using namespace clang;
26 
27 namespace {
28 
29 /// Numbers things which need to correspond across multiple TUs.
30 /// Typically these are things like static locals, lambdas, or blocks.
31 class MicrosoftNumberingContext : public MangleNumberingContext {
32  llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
33  unsigned LambdaManglingNumber;
34  unsigned StaticLocalNumber;
35  unsigned StaticThreadlocalNumber;
36 
37 public:
38  MicrosoftNumberingContext()
39  : MangleNumberingContext(), LambdaManglingNumber(0),
40  StaticLocalNumber(0), StaticThreadlocalNumber(0) {}
41 
42  unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
43  return ++LambdaManglingNumber;
44  }
45 
46  unsigned getManglingNumber(const BlockDecl *BD) override {
47  const Type *Ty = nullptr;
48  return ++ManglingNumbers[Ty];
49  }
50 
51  unsigned getStaticLocalNumber(const VarDecl *VD) override {
52  if (VD->getTLSKind())
53  return ++StaticThreadlocalNumber;
54  return ++StaticLocalNumber;
55  }
56 
57  unsigned getManglingNumber(const VarDecl *VD,
58  unsigned MSLocalManglingNumber) override {
59  return MSLocalManglingNumber;
60  }
61 
62  unsigned getManglingNumber(const TagDecl *TD,
63  unsigned MSLocalManglingNumber) override {
64  return MSLocalManglingNumber;
65  }
66 };
67 
68 class MSHIPNumberingContext : public MicrosoftNumberingContext {
69  std::unique_ptr<MangleNumberingContext> DeviceCtx;
70 
71 public:
72  MSHIPNumberingContext(MangleContext *DeviceMangler) {
73  DeviceCtx = createItaniumNumberingContext(DeviceMangler);
74  }
75 
76  unsigned getDeviceManglingNumber(const CXXMethodDecl *CallOperator) override {
77  return DeviceCtx->getManglingNumber(CallOperator);
78  }
79 };
80 
81 class MSSYCLNumberingContext : public MicrosoftNumberingContext {
82  std::unique_ptr<MangleNumberingContext> DeviceCtx;
83 
84 public:
85  MSSYCLNumberingContext(MangleContext *DeviceMangler) {
86  DeviceCtx = createItaniumNumberingContext(DeviceMangler);
87  }
88 
89  unsigned getDeviceManglingNumber(const CXXMethodDecl *CallOperator) override {
90  return DeviceCtx->getManglingNumber(CallOperator);
91  }
92 };
93 
94 class MicrosoftCXXABI : public CXXABI {
95  ASTContext &Context;
96  llvm::SmallDenseMap<CXXRecordDecl *, CXXConstructorDecl *> RecordToCopyCtor;
97 
98  llvm::SmallDenseMap<TagDecl *, DeclaratorDecl *>
99  UnnamedTagDeclToDeclaratorDecl;
100  llvm::SmallDenseMap<TagDecl *, TypedefNameDecl *>
101  UnnamedTagDeclToTypedefNameDecl;
102 
103  // MangleContext for device numbering context, which is based on Itanium C++
104  // ABI.
105  std::unique_ptr<MangleContext> DeviceMangler;
106 
107 public:
108  MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) {
109  if (Context.getLangOpts().CUDA && Context.getAuxTargetInfo()) {
110  assert(Context.getTargetInfo().getCXXABI().isMicrosoft() &&
111  Context.getAuxTargetInfo()->getCXXABI().isItaniumFamily() &&
112  "Unexpected combination of C++ ABIs.");
113  DeviceMangler.reset(
114  Context.createMangleContext(Context.getAuxTargetInfo()));
115  }
116  else if (Context.getLangOpts().isSYCL()) {
117  DeviceMangler.reset(
118  ItaniumMangleContext::create(Context, Context.getDiagnostics()));
119  }
120  }
121 
122  MemberPointerInfo
123  getMemberPointerInfo(const MemberPointerType *MPT) const override;
124 
125  CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
126  if (!isVariadic &&
127  Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
128  return CC_X86ThisCall;
129  return Context.getTargetInfo().getDefaultCallingConv();
130  }
131 
132  bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
133  llvm_unreachable("unapplicable to the MS ABI");
134  }
135 
136  const CXXConstructorDecl *
137  getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
138  return RecordToCopyCtor[RD];
139  }
140 
141  void
142  addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
143  CXXConstructorDecl *CD) override {
144  assert(CD != nullptr);
145  assert(RecordToCopyCtor[RD] == nullptr || RecordToCopyCtor[RD] == CD);
146  RecordToCopyCtor[RD] = CD;
147  }
148 
149  void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
150  TypedefNameDecl *DD) override {
151  TD = TD->getCanonicalDecl();
152  DD = DD->getCanonicalDecl();
153  TypedefNameDecl *&I = UnnamedTagDeclToTypedefNameDecl[TD];
154  if (!I)
155  I = DD;
156  }
157 
158  TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
159  return UnnamedTagDeclToTypedefNameDecl.lookup(
160  const_cast<TagDecl *>(TD->getCanonicalDecl()));
161  }
162 
163  void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
164  DeclaratorDecl *DD) override {
165  TD = TD->getCanonicalDecl();
166  DD = cast<DeclaratorDecl>(DD->getCanonicalDecl());
167  DeclaratorDecl *&I = UnnamedTagDeclToDeclaratorDecl[TD];
168  if (!I)
169  I = DD;
170  }
171 
172  DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
173  return UnnamedTagDeclToDeclaratorDecl.lookup(
174  const_cast<TagDecl *>(TD->getCanonicalDecl()));
175  }
176 
177  std::unique_ptr<MangleNumberingContext>
178  createMangleNumberingContext() const override {
179  if (Context.getLangOpts().CUDA && Context.getAuxTargetInfo()) {
180  assert(DeviceMangler && "Missing device mangler");
181  return std::make_unique<MSHIPNumberingContext>(DeviceMangler.get());
182  } else if (Context.getLangOpts().isSYCL()) {
183  assert(DeviceMangler && "Missing device mangler");
184  return std::make_unique<MSSYCLNumberingContext>(DeviceMangler.get());
185  }
186 
187  return std::make_unique<MicrosoftNumberingContext>();
188  }
189 };
190 }
191 
192 // getNumBases() seems to only give us the number of direct bases, and not the
193 // total. This function tells us if we inherit from anybody that uses MI, or if
194 // we have a non-primary base class, which uses the multiple inheritance model.
196  while (RD->getNumBases() > 0) {
197  if (RD->getNumBases() > 1)
198  return true;
199  assert(RD->getNumBases() == 1);
200  const CXXRecordDecl *Base =
202  if (RD->isPolymorphic() && !Base->isPolymorphic())
203  return true;
204  RD = Base;
205  }
206  return false;
207 }
208 
212  if (getNumVBases() > 0)
217 }
218 
220  MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
221  assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
222  return IA->getInheritanceModel();
223 }
224 
226  return !inheritanceModelHasOnlyOneField(/*IsMemberFunction=*/false,
228  (hasDefinition() && isPolymorphic());
229 }
230 
232  if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>())
233  return VDA->getVtorDispMode();
234  return getASTContext().getLangOpts().getVtorDispMode();
235 }
236 
237 // Returns the number of pointer and integer slots used to represent a member
238 // pointer in the MS C++ ABI.
239 //
240 // Member function pointers have the following general form; however, fields
241 // are dropped as permitted (under the MSVC interpretation) by the inheritance
242 // model of the actual class.
243 //
244 // struct {
245 // // A pointer to the member function to call. If the member function is
246 // // virtual, this will be a thunk that forwards to the appropriate vftable
247 // // slot.
248 // void *FunctionPointerOrVirtualThunk;
249 //
250 // // An offset to add to the address of the vbtable pointer after
251 // // (possibly) selecting the virtual base but before resolving and calling
252 // // the function.
253 // // Only needed if the class has any virtual bases or bases at a non-zero
254 // // offset.
255 // int NonVirtualBaseAdjustment;
256 //
257 // // The offset of the vb-table pointer within the object. Only needed for
258 // // incomplete types.
259 // int VBPtrOffset;
260 //
261 // // An offset within the vb-table that selects the virtual base containing
262 // // the member. Loading from this offset produces a new offset that is
263 // // added to the address of the vb-table pointer to produce the base.
264 // int VirtualBaseAdjustmentOffset;
265 // };
266 static std::pair<unsigned, unsigned>
268  const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
269  MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
270  unsigned Ptrs = 0;
271  unsigned Ints = 0;
272  if (MPT->isMemberFunctionPointer())
273  Ptrs = 1;
274  else
275  Ints = 1;
277  Inheritance))
278  Ints++;
279  if (inheritanceModelHasVBPtrOffsetField(Inheritance))
280  Ints++;
281  if (inheritanceModelHasVBTableOffsetField(Inheritance))
282  Ints++;
283  return std::make_pair(Ptrs, Ints);
284 }
285 
286 CXXABI::MemberPointerInfo MicrosoftCXXABI::getMemberPointerInfo(
287  const MemberPointerType *MPT) const {
288  // The nominal struct is laid out with pointers followed by ints and aligned
289  // to a pointer width if any are present and an int width otherwise.
290  const TargetInfo &Target = Context.getTargetInfo();
291  unsigned PtrSize = Target.getPointerWidth(0);
292  unsigned IntSize = Target.getIntWidth();
293 
294  unsigned Ptrs, Ints;
295  std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
296  MemberPointerInfo MPI;
297  MPI.HasPadding = false;
298  MPI.Width = Ptrs * PtrSize + Ints * IntSize;
299 
300  // When MSVC does x86_32 record layout, it aligns aggregate member pointers to
301  // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for
302  // function memptrs.
303  if (Ptrs + Ints > 1 && Target.getTriple().isArch32Bit())
304  MPI.Align = 64;
305  else if (Ptrs)
306  MPI.Align = Target.getPointerAlign(0);
307  else
308  MPI.Align = Target.getIntAlign();
309 
310  if (Target.getTriple().isArch64Bit()) {
311  MPI.Width = llvm::alignTo(MPI.Width, MPI.Align);
312  MPI.HasPadding = MPI.Width != (Ptrs * PtrSize + Ints * IntSize);
313  }
314  return MPI;
315 }
316 
318  return new MicrosoftCXXABI(Ctx);
319 }
clang::Decl::getASTContext
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:414
clang::CXXConstructorDecl
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2401
clang::CXXBaseSpecifier::getType
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:246
clang::VarDecl::getTLSKind
TLSKind getTLSKind() const
Definition: Decl.cpp:2051
clang::CXXABI::MemberPointerInfo
Definition: CXXABI.h:34
clang::MSInheritanceModel::Multiple
@ Multiple
clang::TargetCXXABI::isMicrosoft
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:138
TargetInfo.h
clang::MultiVersionKind::Target
@ Target
CXXInheritance.h
Attr.h
clang::TargetInfo::getCXXABI
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1203
clang::TargetCXXABI::isItaniumFamily
bool isItaniumFamily() const
Does this ABI generally fall into the Itanium family of ABIs?
Definition: TargetCXXABI.h:124
clang::TargetInfo
Exposes information about the current target.
Definition: TargetInfo.h:189
DeclCXX.h
clang::TagDecl::getCanonicalDecl
TagDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: Decl.cpp:4308
clang::ASTContext::getAuxTargetInfo
const TargetInfo * getAuxTargetInfo() const
Definition: ASTContext.h:748
clang::ASTContext::createMangleContext
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
Definition: ASTContext.cpp:11192
clang::MemberPointerType::isMemberFunctionPointer
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition: Type.h:2851
clang::CC_X86ThisCall
@ CC_X86ThisCall
Definition: Specifiers.h:267
clang::MSVtorDispMode
MSVtorDispMode
In the Microsoft ABI, this controls the placement of virtual displacement members used to implement v...
Definition: LangOptions.h:54
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
clang::MSInheritanceModel::Unspecified
@ Unspecified
clang::ItaniumMangleContext::create
static ItaniumMangleContext * create(ASTContext &Context, DiagnosticsEngine &Diags)
Definition: ItaniumMangle.cpp:6423
clang::TargetInfo::getDefaultCallingConv
virtual CallingConv getDefaultCallingConv() const
Gets the default calling convention for the given target and declaration context.
Definition: TargetInfo.h:1452
clang::MangleContext
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:44
clang::BlockDecl
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4153
clang::CXXRecordDecl::isParsingBaseSpecifiers
bool isParsingBaseSpecifiers() const
Definition: DeclCXX.h:573
clang::inheritanceModelHasVBTableOffsetField
bool inheritanceModelHasVBTableOffsetField(MSInheritanceModel Inheritance)
Definition: CXXInheritance.h:377
clang::CXXRecordDecl::getMSVtorDispMode
MSVtorDispMode getMSVtorDispMode() const
Controls when vtordisps will be emitted if this record is used as a virtual base.
Definition: MicrosoftCXXABI.cpp:231
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::CXXRecordDecl::getNumBases
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:583
clang::TypedefNameDecl::getCanonicalDecl
TypedefNameDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this typedef-name.
Definition: Decl.h:3252
clang::Decl::getCanonicalDecl
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclBase.h:901
clang::MSInheritanceModel::Single
@ Single
Type.h
ASTContext.h
clang::CallingConv
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:263
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::TagDecl
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3328
clang::CXXABI
Implements C++ ABI-specific semantic analysis functions.
Definition: CXXABI.h:30
clang::Type::getAsCXXRecordDecl
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1760
Base
clang::CreateMicrosoftCXXABI
CXXABI * CreateMicrosoftCXXABI(ASTContext &Ctx)
Definition: MicrosoftCXXABI.cpp:317
clang::CXXRecordDecl::getMSInheritanceModel
MSInheritanceModel getMSInheritanceModel() const
Returns the inheritance model used for this record.
Definition: MicrosoftCXXABI.cpp:219
CXXABI.h
clang::TargetInfo::getTriple
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1130
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
clang::inheritanceModelHasNVOffsetField
bool inheritanceModelHasNVOffsetField(bool IsMemberFunction, MSInheritanceModel Inheritance)
Definition: CXXInheritance.h:371
usesMultipleInheritanceModel
static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD)
Definition: MicrosoftCXXABI.cpp:195
clang::CXXRecordDecl::nullFieldOffsetIsZero
bool nullFieldOffsetIsZero() const
In the Microsoft C++ ABI, use zero for the field offset of a null data member pointer if we can guara...
Definition: MicrosoftCXXABI.cpp:225
clang::CXXRecordDecl::getNumVBases
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:604
clang::DeclaratorDecl
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:728
clang::inheritanceModelHasVBPtrOffsetField
bool inheritanceModelHasVBPtrOffsetField(MSInheritanceModel Inheritance)
Definition: CXXInheritance.h:365
clang::ASTContext::getTargetInfo
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:747
clang::LangOptions::isSYCL
bool isSYCL() const
Definition: LangOptions.h:472
clang::MSInheritanceModel::Virtual
@ Virtual
clang::MemberPointerType
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2831
getMSMemberPointerSlots
static std::pair< unsigned, unsigned > getMSMemberPointerSlots(const MemberPointerType *MPT)
Definition: MicrosoftCXXABI.cpp:267
clang::MangleNumberingContext
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
Definition: MangleNumberingContext.h:31
clang::inheritanceModelHasOnlyOneField
bool inheritanceModelHasOnlyOneField(bool IsMemberFunction, MSInheritanceModel Inheritance)
Definition: CXXInheritance.h:381
clang::CXXRecordDecl::calculateInheritanceModel
MSInheritanceModel calculateInheritanceModel() const
Calculate what the inheritance model would be for this class.
Definition: MicrosoftCXXABI.cpp:209
clang
Definition: CalledOnceCheck.h:17
clang::CXXRecordDecl::bases_begin
base_class_iterator bases_begin()
Definition: DeclCXX.h:596
clang::CXXRecordDecl::hasDefinition
bool hasDefinition() const
Definition: DeclCXX.h:542
clang::MSInheritanceModel
MSInheritanceModel
Assigned inheritance model for a class in the MS C++ ABI.
Definition: Specifiers.h:369
clang::TypedefNameDecl
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3180
clang::CXXRecordDecl::isPolymorphic
bool isPolymorphic() const
Whether this class is polymorphic (C++ [class.virtual]), which means that the class contains or inher...
Definition: DeclCXX.h:1159
Mangle.h
clang::MemberPointerType::getMostRecentCXXRecordDecl
CXXRecordDecl * getMostRecentCXXRecordDecl() const
Definition: Type.cpp:4390
MangleNumberingContext.h
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:765
clang::createItaniumNumberingContext
std::unique_ptr< MangleNumberingContext > createItaniumNumberingContext(MangleContext *)
Definition: ItaniumCXXABI.cpp:297
clang::ASTContext::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Definition: ASTContext.cpp:1499
RecordLayout.h
clang::CXXMethodDecl
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1948