Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/MicrosoftCXXABI.cpp
Line
Count
Source (jump to first uncovered line)
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"
17
#include "clang/AST/CXXInheritance.h"
18
#include "clang/AST/DeclCXX.h"
19
#include "clang/AST/Mangle.h"
20
#include "clang/AST/MangleNumberingContext.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
      : LambdaManglingNumber(0), StaticLocalNumber(0),
40
2.17k
        StaticThreadlocalNumber(0) {}
41
42
1.66k
  unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
43
1.66k
    return ++LambdaManglingNumber;
44
1.66k
  }
45
46
25
  unsigned getManglingNumber(const BlockDecl *BD) override {
47
25
    const Type *Ty = nullptr;
48
25
    return ++ManglingNumbers[Ty];
49
25
  }
50
51
111
  unsigned getStaticLocalNumber(const VarDecl *VD) override {
52
111
    if (VD->getTLSKind())
53
11
      return ++StaticThreadlocalNumber;
54
100
    return ++StaticLocalNumber;
55
111
  }
56
57
  unsigned getManglingNumber(const VarDecl *VD,
58
111
                             unsigned MSLocalManglingNumber) override {
59
111
    return MSLocalManglingNumber;
60
111
  }
61
62
  unsigned getManglingNumber(const TagDecl *TD,
63
501
                             unsigned MSLocalManglingNumber) override {
64
501
    return MSLocalManglingNumber;
65
501
  }
66
};
67
68
class MSHIPNumberingContext : public MicrosoftNumberingContext {
69
  std::unique_ptr<MangleNumberingContext> DeviceCtx;
70
71
public:
72
5
  MSHIPNumberingContext(MangleContext *DeviceMangler) {
73
5
    DeviceCtx = createItaniumNumberingContext(DeviceMangler);
74
5
  }
75
76
8
  unsigned getDeviceManglingNumber(const CXXMethodDecl *CallOperator) override {
77
8
    return DeviceCtx->getManglingNumber(CallOperator);
78
8
  }
79
};
80
81
class MSSYCLNumberingContext : public MicrosoftNumberingContext {
82
  std::unique_ptr<MangleNumberingContext> DeviceCtx;
83
84
public:
85
9
  MSSYCLNumberingContext(MangleContext *DeviceMangler) {
86
9
    DeviceCtx = createItaniumNumberingContext(DeviceMangler);
87
9
  }
88
89
22
  unsigned getDeviceManglingNumber(const CXXMethodDecl *CallOperator) override {
90
22
    return DeviceCtx->getManglingNumber(CallOperator);
91
22
  }
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
7.38k
  MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) {
109
7.38k
    if (Context.getLangOpts().CUDA && 
Context.getAuxTargetInfo()9
) {
110
7
      assert(Context.getTargetInfo().getCXXABI().isMicrosoft() &&
111
7
             Context.getAuxTargetInfo()->getCXXABI().isItaniumFamily() &&
112
7
             "Unexpected combination of C++ ABIs.");
113
0
      DeviceMangler.reset(
114
7
          Context.createMangleContext(Context.getAuxTargetInfo()));
115
7
    }
116
7.38k
    else if (Context.getLangOpts().isSYCL()) {
117
2
      DeviceMangler.reset(
118
2
          ItaniumMangleContext::create(Context, Context.getDiagnostics()));
119
2
    }
120
7.38k
  }
121
122
  MemberPointerInfo
123
  getMemberPointerInfo(const MemberPointerType *MPT) const override;
124
125
47.7k
  CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
126
47.7k
    if (!isVariadic &&
127
47.7k
        
Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x8647.6k
)
128
26.1k
      return CC_X86ThisCall;
129
21.5k
    return Context.getTargetInfo().getDefaultCallingConv();
130
47.7k
  }
131
132
0
  bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
133
0
    llvm_unreachable("unapplicable to the MS ABI");
134
0
  }
135
136
  const CXXConstructorDecl *
137
50
  getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
138
50
    return RecordToCopyCtor[RD];
139
50
  }
140
141
  void
142
  addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
143
20
                                       CXXConstructorDecl *CD) override {
144
20
    assert(CD != nullptr);
145
0
    assert(RecordToCopyCtor[RD] == nullptr || RecordToCopyCtor[RD] == CD);
146
0
    RecordToCopyCtor[RD] = CD;
147
20
  }
148
149
  void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
150
10
                                       TypedefNameDecl *DD) override {
151
10
    TD = TD->getCanonicalDecl();
152
10
    DD = DD->getCanonicalDecl();
153
10
    TypedefNameDecl *&I = UnnamedTagDeclToTypedefNameDecl[TD];
154
10
    if (!I)
155
10
      I = DD;
156
10
  }
157
158
2.18k
  TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
159
2.18k
    return UnnamedTagDeclToTypedefNameDecl.lookup(
160
2.18k
        const_cast<TagDecl *>(TD->getCanonicalDecl()));
161
2.18k
  }
162
163
  void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
164
93
                                      DeclaratorDecl *DD) override {
165
93
    TD = TD->getCanonicalDecl();
166
93
    DD = cast<DeclaratorDecl>(DD->getCanonicalDecl());
167
93
    DeclaratorDecl *&I = UnnamedTagDeclToDeclaratorDecl[TD];
168
93
    if (!I)
169
93
      I = DD;
170
93
  }
171
172
2.26k
  DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
173
2.26k
    return UnnamedTagDeclToDeclaratorDecl.lookup(
174
2.26k
        const_cast<TagDecl *>(TD->getCanonicalDecl()));
175
2.26k
  }
176
177
  std::unique_ptr<MangleNumberingContext>
178
2.17k
  createMangleNumberingContext() const override {
179
2.17k
    if (Context.getLangOpts().CUDA && 
Context.getAuxTargetInfo()5
) {
180
5
      assert(DeviceMangler && "Missing device mangler");
181
0
      return std::make_unique<MSHIPNumberingContext>(DeviceMangler.get());
182
2.16k
    } else if (Context.getLangOpts().isSYCL()) {
183
9
      assert(DeviceMangler && "Missing device mangler");
184
0
      return std::make_unique<MSSYCLNumberingContext>(DeviceMangler.get());
185
9
    }
186
187
2.15k
    return std::make_unique<MicrosoftNumberingContext>();
188
2.17k
  }
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.
195
540
static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
196
634
  while (RD->getNumBases() > 0) {
197
122
    if (RD->getNumBases() > 1)
198
20
      return true;
199
102
    assert(RD->getNumBases() == 1);
200
0
    const CXXRecordDecl *Base =
201
102
        RD->bases_begin()->getType()->getAsCXXRecordDecl();
202
102
    if (RD->isPolymorphic() && 
!Base->isPolymorphic()15
)
203
8
      return true;
204
94
    RD = Base;
205
94
  }
206
512
  return false;
207
540
}
208
209
641
MSInheritanceModel CXXRecordDecl::calculateInheritanceModel() const {
210
641
  if (!hasDefinition() || 
isParsingBaseSpecifiers()573
)
211
70
    return MSInheritanceModel::Unspecified;
212
571
  if (getNumVBases() > 0)
213
31
    return MSInheritanceModel::Virtual;
214
540
  if (usesMultipleInheritanceModel(this))
215
28
    return MSInheritanceModel::Multiple;
216
512
  return MSInheritanceModel::Single;
217
540
}
218
219
2.70k
MSInheritanceModel CXXRecordDecl::getMSInheritanceModel() const {
220
2.70k
  MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
221
2.70k
  assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
222
0
  return IA->getInheritanceModel();
223
2.70k
}
224
225
146
bool CXXRecordDecl::nullFieldOffsetIsZero() const {
226
146
  return !inheritanceModelHasOnlyOneField(/*IsMemberFunction=*/false,
227
146
                                          getMSInheritanceModel()) ||
228
146
         
(102
hasDefinition()102
&&
isPolymorphic()100
);
229
146
}
230
231
1.71k
MSVtorDispMode CXXRecordDecl::getMSVtorDispMode() const {
232
1.71k
  if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>())
233
26
    return VDA->getVtorDispMode();
234
1.68k
  return getASTContext().getLangOpts().getVtorDispMode();
235
1.71k
}
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>
267
859
getMSMemberPointerSlots(const MemberPointerType *MPT) {
268
859
  const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
269
859
  MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
270
859
  unsigned Ptrs = 0;
271
859
  unsigned Ints = 0;
272
859
  if (MPT->isMemberFunctionPointer())
273
682
    Ptrs = 1;
274
177
  else
275
177
    Ints = 1;
276
859
  if (inheritanceModelHasNVOffsetField(MPT->isMemberFunctionPointer(),
277
859
                                          Inheritance))
278
140
    Ints++;
279
859
  if (inheritanceModelHasVBPtrOffsetField(Inheritance))
280
159
    Ints++;
281
859
  if (inheritanceModelHasVBTableOffsetField(Inheritance))
282
208
    Ints++;
283
859
  return std::make_pair(Ptrs, Ints);
284
859
}
285
286
CXXABI::MemberPointerInfo MicrosoftCXXABI::getMemberPointerInfo(
287
859
    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
859
  const TargetInfo &Target = Context.getTargetInfo();
291
859
  unsigned PtrSize = Target.getPointerWidth(0);
292
859
  unsigned IntSize = Target.getIntWidth();
293
294
859
  unsigned Ptrs, Ints;
295
859
  std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
296
859
  MemberPointerInfo MPI;
297
859
  MPI.HasPadding = false;
298
859
  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
859
  if (Ptrs + Ints > 1 && 
Target.getTriple().isArch32Bit()230
)
304
102
    MPI.Align = 64;
305
757
  else if (Ptrs)
306
617
    MPI.Align = Target.getPointerAlign(0);
307
140
  else
308
140
    MPI.Align = Target.getIntAlign();
309
310
859
  if (Target.getTriple().isArch64Bit()) {
311
361
    MPI.Width = llvm::alignTo(MPI.Width, MPI.Align);
312
361
    MPI.HasPadding = MPI.Width != (Ptrs * PtrSize + Ints * IntSize);
313
361
  }
314
859
  return MPI;
315
859
}
316
317
7.38k
CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
318
7.38k
  return new MicrosoftCXXABI(Ctx);
319
7.38k
}