clang  3.7.0
AST/MicrosoftCXXABI.cpp
Go to the documentation of this file.
1 //===------- 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 //
10 // This provides C++ AST support targeting the Microsoft Visual C++
11 // ABI.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "CXXABI.h"
16 #include "clang/AST/ASTContext.h"
17 #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 /// \brief 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 {
69  llvm::SmallDenseMap<CXXRecordDecl *, CXXConstructorDecl *> RecordToCopyCtor;
70  llvm::SmallDenseMap<std::pair<const CXXConstructorDecl *, unsigned>, Expr *>
71  CtorToDefaultArgExpr;
72 
73 public:
74  MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
75 
76  std::pair<uint64_t, unsigned>
77  getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override;
78 
79  CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
80  if (!isVariadic &&
81  Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
82  return CC_X86ThisCall;
83  return CC_C;
84  }
85 
86  bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
87  // FIXME: Audit the corners
88  if (!RD->isDynamicClass())
89  return false;
90 
91  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
92 
93  // In the Microsoft ABI, classes can have one or two vtable pointers.
94  CharUnits PointerSize =
96  return Layout.getNonVirtualSize() == PointerSize ||
97  Layout.getNonVirtualSize() == PointerSize * 2;
98  }
99 
100  void addDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
101  unsigned ParmIdx, Expr *DAE) override {
102  CtorToDefaultArgExpr[std::make_pair(CD, ParmIdx)] = DAE;
103  }
104 
105  Expr *getDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
106  unsigned ParmIdx) override {
107  return CtorToDefaultArgExpr[std::make_pair(CD, ParmIdx)];
108  }
109 
110  const CXXConstructorDecl *
111  getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
112  return RecordToCopyCtor[RD];
113  }
114 
115  void
116  addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
117  CXXConstructorDecl *CD) override {
118  assert(CD != nullptr);
119  assert(RecordToCopyCtor[RD] == nullptr || RecordToCopyCtor[RD] == CD);
120  RecordToCopyCtor[RD] = CD;
121  }
122 
123  MangleNumberingContext *createMangleNumberingContext() const override {
124  return new MicrosoftNumberingContext();
125  }
126 };
127 }
128 
129 // getNumBases() seems to only give us the number of direct bases, and not the
130 // total. This function tells us if we inherit from anybody that uses MI, or if
131 // we have a non-primary base class, which uses the multiple inheritance model.
133  while (RD->getNumBases() > 0) {
134  if (RD->getNumBases() > 1)
135  return true;
136  assert(RD->getNumBases() == 1);
137  const CXXRecordDecl *Base =
139  if (RD->isPolymorphic() && !Base->isPolymorphic())
140  return true;
141  RD = Base;
142  }
143  return false;
144 }
145 
146 MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const {
148  return MSInheritanceAttr::Keyword_unspecified_inheritance;
149  if (getNumVBases() > 0)
150  return MSInheritanceAttr::Keyword_virtual_inheritance;
152  return MSInheritanceAttr::Keyword_multiple_inheritance;
153  return MSInheritanceAttr::Keyword_single_inheritance;
154 }
155 
156 MSInheritanceAttr::Spelling
158  MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
159  assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
160  return IA->getSemanticSpelling();
161 }
162 
163 MSVtorDispAttr::Mode CXXRecordDecl::getMSVtorDispMode() const {
164  if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>())
165  return VDA->getVtorDispMode();
166  return MSVtorDispAttr::Mode(getASTContext().getLangOpts().VtorDispMode);
167 }
168 
169 // Returns the number of pointer and integer slots used to represent a member
170 // pointer in the MS C++ ABI.
171 //
172 // Member function pointers have the following general form; however, fields
173 // are dropped as permitted (under the MSVC interpretation) by the inheritance
174 // model of the actual class.
175 //
176 // struct {
177 // // A pointer to the member function to call. If the member function is
178 // // virtual, this will be a thunk that forwards to the appropriate vftable
179 // // slot.
180 // void *FunctionPointerOrVirtualThunk;
181 //
182 // // An offset to add to the address of the vbtable pointer after
183 // // (possibly) selecting the virtual base but before resolving and calling
184 // // the function.
185 // // Only needed if the class has any virtual bases or bases at a non-zero
186 // // offset.
187 // int NonVirtualBaseAdjustment;
188 //
189 // // The offset of the vb-table pointer within the object. Only needed for
190 // // incomplete types.
191 // int VBPtrOffset;
192 //
193 // // An offset within the vb-table that selects the virtual base containing
194 // // the member. Loading from this offset produces a new offset that is
195 // // added to the address of the vb-table pointer to produce the base.
196 // int VirtualBaseAdjustmentOffset;
197 // };
198 static std::pair<unsigned, unsigned>
200  const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
201  MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
202  unsigned Ptrs = 0;
203  unsigned Ints = 0;
204  if (MPT->isMemberFunctionPointer())
205  Ptrs = 1;
206  else
207  Ints = 1;
208  if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
209  Inheritance))
210  Ints++;
211  if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
212  Ints++;
213  if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
214  Ints++;
215  return std::make_pair(Ptrs, Ints);
216 }
217 
218 std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
219  const MemberPointerType *MPT) const {
220  // The nominal struct is laid out with pointers followed by ints and aligned
221  // to a pointer width if any are present and an int width otherwise.
222  const TargetInfo &Target = Context.getTargetInfo();
223  unsigned PtrSize = Target.getPointerWidth(0);
224  unsigned IntSize = Target.getIntWidth();
225 
226  unsigned Ptrs, Ints;
227  std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
228  uint64_t Width = Ptrs * PtrSize + Ints * IntSize;
229  unsigned Align;
230 
231  // When MSVC does x86_32 record layout, it aligns aggregate member pointers to
232  // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for
233  // function memptrs.
234  if (Ptrs + Ints > 1 && Target.getTriple().isArch32Bit())
235  Align = 64;
236  else if (Ptrs)
237  Align = Target.getPointerAlign(0);
238  else
239  Align = Target.getIntAlign();
240 
241  if (Target.getTriple().isArch64Bit())
242  Width = llvm::RoundUpToAlignment(Width, Align);
243  return std::make_pair(Width, Align);
244 }
245 
247  return new MicrosoftCXXABI(Ctx);
248 }
249 
Defines the clang::ASTContext interface.
CXXABI * CreateMicrosoftCXXABI(ASTContext &Ctx)
bool isParsingBaseSpecifiers() const
Definition: DeclCXX.h:699
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:252
MSInheritanceAttr::Spelling getMSInheritanceModel() const
Returns the inheritance model used for this record.
bool hasDefinition() const
Definition: DeclCXX.h:680
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2147
TLSKind getTLSKind() const
Definition: Decl.cpp:1803
MSVtorDispAttr::Mode getMSVtorDispMode() const
Controls when vtordisps will be emitted if this record is used as a virtual base. ...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:518
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
base_class_iterator bases_begin()
Definition: DeclCXX.h:720
bool isMemberFunctionPointer() const
Definition: Type.h:2368
ASTContext * Context
Exposes information about the current target.
MSInheritanceAttr::Spelling calculateInheritanceModel() const
Calculate what the inheritance model would be for this class.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Implements C++ ABI-specific semantic analysis functions.
Definition: CXXABI.h:29
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:707
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:204
CXXRecordDecl * getMostRecentCXXRecordDecl() const
Definition: Type.cpp:3625
static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD)
uint64_t getPointerAlign(unsigned AddrSpace) const
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2694
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:284
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1717
bool isDynamicClass() const
Definition: DeclCXX.h:693
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1505
CharUnits getNonVirtualSize() const
Definition: RecordLayout.h:194
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
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:285
Defines the clang::TargetInfo interface.
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:728
bool isPolymorphic() const
Definition: DeclCXX.h:1148
static std::pair< unsigned, unsigned > getMSMemberPointerSlots(const MemberPointerType *MPT)