clang  10.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"
20 #include "clang/AST/RecordLayout.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/TargetInfo.h"
23 
24 using namespace clang;
25 
26 namespace {
27 
28 /// Numbers things which need to correspond across multiple TUs.
29 /// Typically these are things like static locals, lambdas, or blocks.
30 class MicrosoftNumberingContext : public MangleNumberingContext {
31  llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
32  unsigned LambdaManglingNumber;
33  unsigned StaticLocalNumber;
34  unsigned StaticThreadlocalNumber;
35 
36 public:
37  MicrosoftNumberingContext()
38  : MangleNumberingContext(), LambdaManglingNumber(0),
39  StaticLocalNumber(0), StaticThreadlocalNumber(0) {}
40 
41  unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
42  return ++LambdaManglingNumber;
43  }
44 
45  unsigned getManglingNumber(const BlockDecl *BD) override {
46  const Type *Ty = nullptr;
47  return ++ManglingNumbers[Ty];
48  }
49 
50  unsigned getStaticLocalNumber(const VarDecl *VD) override {
51  if (VD->getTLSKind())
52  return ++StaticThreadlocalNumber;
53  return ++StaticLocalNumber;
54  }
55 
56  unsigned getManglingNumber(const VarDecl *VD,
57  unsigned MSLocalManglingNumber) override {
58  return MSLocalManglingNumber;
59  }
60 
61  unsigned getManglingNumber(const TagDecl *TD,
62  unsigned MSLocalManglingNumber) override {
63  return MSLocalManglingNumber;
64  }
65 };
66 
67 class MicrosoftCXXABI : public CXXABI {
68  ASTContext &Context;
69  llvm::SmallDenseMap<CXXRecordDecl *, CXXConstructorDecl *> RecordToCopyCtor;
70 
71  llvm::SmallDenseMap<TagDecl *, DeclaratorDecl *>
72  UnnamedTagDeclToDeclaratorDecl;
73  llvm::SmallDenseMap<TagDecl *, TypedefNameDecl *>
74  UnnamedTagDeclToTypedefNameDecl;
75 
76 public:
77  MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
78 
79  MemberPointerInfo
80  getMemberPointerInfo(const MemberPointerType *MPT) const override;
81 
82  CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
83  if (!isVariadic &&
84  Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
85  return CC_X86ThisCall;
86  return Context.getTargetInfo().getDefaultCallingConv();
87  }
88 
89  bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
90  llvm_unreachable("unapplicable to the MS ABI");
91  }
92 
93  const CXXConstructorDecl *
94  getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
95  return RecordToCopyCtor[RD];
96  }
97 
98  void
99  addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
100  CXXConstructorDecl *CD) override {
101  assert(CD != nullptr);
102  assert(RecordToCopyCtor[RD] == nullptr || RecordToCopyCtor[RD] == CD);
103  RecordToCopyCtor[RD] = CD;
104  }
105 
106  void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
107  TypedefNameDecl *DD) override {
108  TD = TD->getCanonicalDecl();
109  DD = DD->getCanonicalDecl();
110  TypedefNameDecl *&I = UnnamedTagDeclToTypedefNameDecl[TD];
111  if (!I)
112  I = DD;
113  }
114 
115  TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
116  return UnnamedTagDeclToTypedefNameDecl.lookup(
117  const_cast<TagDecl *>(TD->getCanonicalDecl()));
118  }
119 
120  void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
121  DeclaratorDecl *DD) override {
122  TD = TD->getCanonicalDecl();
123  DD = cast<DeclaratorDecl>(DD->getCanonicalDecl());
124  DeclaratorDecl *&I = UnnamedTagDeclToDeclaratorDecl[TD];
125  if (!I)
126  I = DD;
127  }
128 
129  DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
130  return UnnamedTagDeclToDeclaratorDecl.lookup(
131  const_cast<TagDecl *>(TD->getCanonicalDecl()));
132  }
133 
134  std::unique_ptr<MangleNumberingContext>
135  createMangleNumberingContext() const override {
136  return std::make_unique<MicrosoftNumberingContext>();
137  }
138 };
139 }
140 
141 // getNumBases() seems to only give us the number of direct bases, and not the
142 // total. This function tells us if we inherit from anybody that uses MI, or if
143 // we have a non-primary base class, which uses the multiple inheritance model.
145  while (RD->getNumBases() > 0) {
146  if (RD->getNumBases() > 1)
147  return true;
148  assert(RD->getNumBases() == 1);
149  const CXXRecordDecl *Base =
151  if (RD->isPolymorphic() && !Base->isPolymorphic())
152  return true;
153  RD = Base;
154  }
155  return false;
156 }
157 
159  if (!hasDefinition() || isParsingBaseSpecifiers())
161  if (getNumVBases() > 0)
166 }
167 
169  MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
170  assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
171  return IA->getInheritanceModel();
172 }
173 
175  return !inheritanceModelHasOnlyOneField(/*IsMemberFunction=*/false,
176  getMSInheritanceModel()) ||
177  (hasDefinition() && isPolymorphic());
178 }
179 
181  if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>())
182  return VDA->getVtorDispMode();
183  return getASTContext().getLangOpts().getVtorDispMode();
184 }
185 
186 // Returns the number of pointer and integer slots used to represent a member
187 // pointer in the MS C++ ABI.
188 //
189 // Member function pointers have the following general form; however, fields
190 // are dropped as permitted (under the MSVC interpretation) by the inheritance
191 // model of the actual class.
192 //
193 // struct {
194 // // A pointer to the member function to call. If the member function is
195 // // virtual, this will be a thunk that forwards to the appropriate vftable
196 // // slot.
197 // void *FunctionPointerOrVirtualThunk;
198 //
199 // // An offset to add to the address of the vbtable pointer after
200 // // (possibly) selecting the virtual base but before resolving and calling
201 // // the function.
202 // // Only needed if the class has any virtual bases or bases at a non-zero
203 // // offset.
204 // int NonVirtualBaseAdjustment;
205 //
206 // // The offset of the vb-table pointer within the object. Only needed for
207 // // incomplete types.
208 // int VBPtrOffset;
209 //
210 // // An offset within the vb-table that selects the virtual base containing
211 // // the member. Loading from this offset produces a new offset that is
212 // // added to the address of the vb-table pointer to produce the base.
213 // int VirtualBaseAdjustmentOffset;
214 // };
215 static std::pair<unsigned, unsigned>
217  const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
218  MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
219  unsigned Ptrs = 0;
220  unsigned Ints = 0;
221  if (MPT->isMemberFunctionPointer())
222  Ptrs = 1;
223  else
224  Ints = 1;
226  Inheritance))
227  Ints++;
228  if (inheritanceModelHasVBPtrOffsetField(Inheritance))
229  Ints++;
230  if (inheritanceModelHasVBTableOffsetField(Inheritance))
231  Ints++;
232  return std::make_pair(Ptrs, Ints);
233 }
234 
235 CXXABI::MemberPointerInfo MicrosoftCXXABI::getMemberPointerInfo(
236  const MemberPointerType *MPT) const {
237  // The nominal struct is laid out with pointers followed by ints and aligned
238  // to a pointer width if any are present and an int width otherwise.
239  const TargetInfo &Target = Context.getTargetInfo();
240  unsigned PtrSize = Target.getPointerWidth(0);
241  unsigned IntSize = Target.getIntWidth();
242 
243  unsigned Ptrs, Ints;
244  std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
245  MemberPointerInfo MPI;
246  MPI.HasPadding = false;
247  MPI.Width = Ptrs * PtrSize + Ints * IntSize;
248 
249  // When MSVC does x86_32 record layout, it aligns aggregate member pointers to
250  // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for
251  // function memptrs.
252  if (Ptrs + Ints > 1 && Target.getTriple().isArch32Bit())
253  MPI.Align = 64;
254  else if (Ptrs)
255  MPI.Align = Target.getPointerAlign(0);
256  else
257  MPI.Align = Target.getIntAlign();
258 
259  if (Target.getTriple().isArch64Bit()) {
260  MPI.Width = llvm::alignTo(MPI.Width, MPI.Align);
261  MPI.HasPadding = MPI.Width != (Ptrs * PtrSize + Ints * IntSize);
262  }
263  return MPI;
264 }
265 
267  return new MicrosoftCXXABI(Ctx);
268 }
269 
Defines the clang::ASTContext interface.
MSVtorDispMode
In the Microsoft ABI, this controls the placement of virtual displacement members used to implement v...
Definition: LangOptions.h:49
CXXABI * CreateMicrosoftCXXABI(ASTContext &Ctx)
MSVtorDispMode getMSVtorDispMode() const
Controls when vtordisps will be emitted if this record is used as a virtual base. ...
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:581
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:994
C Language Family Type Representation.
The base class of the type hierarchy.
Definition: Type.h:1450
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:707
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2383
Represents a variable declaration or definition.
Definition: Decl.h:820
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:361
unsigned getIntAlign() const
Definition: TargetInfo.h:397
MSInheritanceModel calculateInheritanceModel() const
Calculate what the inheritance model would be for this class.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
bool inheritanceModelHasOnlyOneField(bool IsMemberFunction, MSInheritanceModel Inheritance)
TypedefNameDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this typedef-name.
Definition: Decl.h:3143
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
MSInheritanceModel
Assigned inheritance model for a class in the MS C++ ABI.
Definition: Specifiers.h:359
base_class_iterator bases_begin()
Definition: DeclCXX.h:594
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclBase.h:877
bool inheritanceModelHasVBTableOffsetField(MSInheritanceModel Inheritance)
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1690
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:671
Exposes information about the current target.
Definition: TargetInfo.h:164
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4037
unsigned getIntWidth() const
getIntWidth/Align - Return the size of &#39;signed int&#39; and &#39;unsigned int&#39; for this target, in bits.
Definition: TargetInfo.h:396
virtual CallingConv getDefaultCallingConv() const
Gets the default calling convention for the given target and declaration context. ...
Definition: TargetInfo.h:1276
TLSKind getTLSKind() const
Definition: Decl.cpp:1998
bool isPolymorphic() const
Whether this class is polymorphic (C++ [class.virtual]), which means that the class contains or inher...
Definition: DeclCXX.h:1117
static bool hasDefinition(const ObjCObjectPointerType *ObjPtr)
Implements C++ ABI-specific semantic analysis functions.
Definition: CXXABI.h:29
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:265
static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD)
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3219
TagDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: Decl.cpp:4143
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1931
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3071
CXXRecordDecl * getMostRecentCXXRecordDecl() const
Definition: Type.cpp:4152
Dataflow Directional Tag Classes.
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2833
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Represents a C++ struct/union/class.
Definition: DeclCXX.h:253
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition: Type.h:2857
bool inheritanceModelHasNVOffsetField(bool IsMemberFunction, MSInheritanceModel Inheritance)
uint64_t getPointerAlign(unsigned AddrSpace) const
Definition: TargetInfo.h:364
Defines the clang::TargetInfo interface.
bool nullFieldOffsetIsZero() const
In the Microsoft C++ ABI, use zero for the field offset of a null data member pointer if we can guara...
MSInheritanceModel getMSInheritanceModel() const
Returns the inheritance model used for this record.
bool inheritanceModelHasVBPtrOffsetField(MSInheritanceModel Inheritance)
static std::pair< unsigned, unsigned > getMSMemberPointerSlots(const MemberPointerType *MPT)
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:244