Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/AST/DeclarationName.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DeclarationName.cpp - Declaration names implementation -------------===//
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 file implements the DeclarationName and DeclarationNameTable
10
// classes.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/DeclarationName.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/Decl.h"
17
#include "clang/AST/DeclBase.h"
18
#include "clang/AST/DeclCXX.h"
19
#include "clang/AST/DeclTemplate.h"
20
#include "clang/AST/OpenMPClause.h"
21
#include "clang/AST/PrettyPrinter.h"
22
#include "clang/AST/Type.h"
23
#include "clang/AST/TypeLoc.h"
24
#include "clang/AST/TypeOrdering.h"
25
#include "clang/Basic/IdentifierTable.h"
26
#include "clang/Basic/LLVM.h"
27
#include "clang/Basic/LangOptions.h"
28
#include "clang/Basic/OperatorKinds.h"
29
#include "clang/Basic/SourceLocation.h"
30
#include "llvm/ADT/FoldingSet.h"
31
#include "llvm/Support/Casting.h"
32
#include "llvm/Support/Compiler.h"
33
#include "llvm/Support/ErrorHandling.h"
34
#include "llvm/Support/raw_ostream.h"
35
#include <algorithm>
36
#include <cassert>
37
#include <cstdint>
38
#include <string>
39
40
using namespace clang;
41
42
0
static int compareInt(unsigned A, unsigned B) {
43
0
  return (A < B ? -1 : (A > B ? 1 : 0));
44
0
}
45
46
0
int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) {
47
0
  if (LHS.getNameKind() != RHS.getNameKind())
48
0
    return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1);
49
50
0
  switch (LHS.getNameKind()) {
51
0
  case DeclarationName::Identifier: {
52
0
    IdentifierInfo *LII = LHS.castAsIdentifierInfo();
53
0
    IdentifierInfo *RII = RHS.castAsIdentifierInfo();
54
0
    if (!LII)
55
0
      return RII ? -1 : 0;
56
0
    if (!RII)
57
0
      return 1;
58
59
0
    return LII->getName().compare(RII->getName());
60
0
  }
61
62
0
  case DeclarationName::ObjCZeroArgSelector:
63
0
  case DeclarationName::ObjCOneArgSelector:
64
0
  case DeclarationName::ObjCMultiArgSelector: {
65
0
    Selector LHSSelector = LHS.getObjCSelector();
66
0
    Selector RHSSelector = RHS.getObjCSelector();
67
    // getNumArgs for ZeroArgSelector returns 0, but we still need to compare.
68
0
    if (LHS.getNameKind() == DeclarationName::ObjCZeroArgSelector &&
69
0
        RHS.getNameKind() == DeclarationName::ObjCZeroArgSelector) {
70
0
      return LHSSelector.getAsIdentifierInfo()->getName().compare(
71
0
          RHSSelector.getAsIdentifierInfo()->getName());
72
0
    }
73
0
    unsigned LN = LHSSelector.getNumArgs(), RN = RHSSelector.getNumArgs();
74
0
    for (unsigned I = 0, N = std::min(LN, RN); I != N; ++I) {
75
0
      if (int Compare = LHSSelector.getNameForSlot(I).compare(
76
0
              RHSSelector.getNameForSlot(I)))
77
0
        return Compare;
78
0
    }
79
80
0
    return compareInt(LN, RN);
81
0
  }
82
83
0
  case DeclarationName::CXXConstructorName:
84
0
  case DeclarationName::CXXDestructorName:
85
0
  case DeclarationName::CXXConversionFunctionName:
86
0
    if (QualTypeOrdering()(LHS.getCXXNameType(), RHS.getCXXNameType()))
87
0
      return -1;
88
0
    if (QualTypeOrdering()(RHS.getCXXNameType(), LHS.getCXXNameType()))
89
0
      return 1;
90
0
    return 0;
91
92
0
  case DeclarationName::CXXDeductionGuideName:
93
    // We never want to compare deduction guide names for templates from
94
    // different scopes, so just compare the template-name.
95
0
    return compare(LHS.getCXXDeductionGuideTemplate()->getDeclName(),
96
0
                   RHS.getCXXDeductionGuideTemplate()->getDeclName());
97
98
0
  case DeclarationName::CXXOperatorName:
99
0
    return compareInt(LHS.getCXXOverloadedOperator(),
100
0
                      RHS.getCXXOverloadedOperator());
101
102
0
  case DeclarationName::CXXLiteralOperatorName:
103
0
    return LHS.getCXXLiteralIdentifier()->getName().compare(
104
0
        RHS.getCXXLiteralIdentifier()->getName());
105
106
0
  case DeclarationName::CXXUsingDirective:
107
0
    return 0;
108
0
  }
109
110
0
  llvm_unreachable("Invalid DeclarationName Kind!");
111
0
}
112
113
static void printCXXConstructorDestructorName(QualType ClassType,
114
                                              raw_ostream &OS,
115
0
                                              PrintingPolicy Policy) {
116
  // We know we're printing C++ here. Ensure we print types properly.
117
0
  Policy.adjustForCPlusPlus();
118
119
0
  if (const RecordType *ClassRec = ClassType->getAs<RecordType>()) {
120
0
    ClassRec->getDecl()->printName(OS, Policy);
121
0
    return;
122
0
  }
123
0
  if (Policy.SuppressTemplateArgsInCXXConstructors) {
124
0
    if (auto *InjTy = ClassType->getAs<InjectedClassNameType>()) {
125
0
      InjTy->getDecl()->printName(OS, Policy);
126
0
      return;
127
0
    }
128
0
  }
129
0
  ClassType.print(OS, Policy);
130
0
}
131
132
void DeclarationName::print(raw_ostream &OS,
133
47
                            const PrintingPolicy &Policy) const {
134
47
  switch (getNameKind()) {
135
47
  case DeclarationName::Identifier:
136
47
    if (const IdentifierInfo *II = getAsIdentifierInfo()) {
137
47
      StringRef Name = II->getName();
138
      // If this is a mangled OpenMP variant name we strip off the mangling for
139
      // printing. It should not be visible to the user at all.
140
47
      if (II->isMangledOpenMPVariantName()) {
141
0
        std::pair<StringRef, StringRef> NameContextPair =
142
0
            Name.split(getOpenMPVariantManglingSeparatorStr());
143
0
        OS << NameContextPair.first << "["
144
0
           << OMPTraitInfo(NameContextPair.second) << "]";
145
47
      } else {
146
47
        OS << Name;
147
47
      }
148
47
    }
149
47
    return;
150
151
0
  case DeclarationName::ObjCZeroArgSelector:
152
0
  case DeclarationName::ObjCOneArgSelector:
153
0
  case DeclarationName::ObjCMultiArgSelector:
154
0
    getObjCSelector().print(OS);
155
0
    return;
156
157
0
  case DeclarationName::CXXConstructorName:
158
0
    return printCXXConstructorDestructorName(getCXXNameType(), OS, Policy);
159
160
0
  case DeclarationName::CXXDestructorName:
161
0
    OS << '~';
162
0
    return printCXXConstructorDestructorName(getCXXNameType(), OS, Policy);
163
164
0
  case DeclarationName::CXXDeductionGuideName:
165
0
    OS << "<deduction guide for ";
166
0
    getCXXDeductionGuideTemplate()->getDeclName().print(OS, Policy);
167
0
    OS << '>';
168
0
    return;
169
170
0
  case DeclarationName::CXXOperatorName: {
171
0
    const char *OpName = getOperatorSpelling(getCXXOverloadedOperator());
172
0
    assert(OpName && "not an overloaded operator");
173
174
0
    OS << "operator";
175
0
    if (OpName[0] >= 'a' && OpName[0] <= 'z')
176
0
      OS << ' ';
177
0
    OS << OpName;
178
0
    return;
179
0
  }
180
181
0
  case DeclarationName::CXXLiteralOperatorName:
182
0
    OS << "operator\"\"" << getCXXLiteralIdentifier()->getName();
183
0
    return;
184
185
0
  case DeclarationName::CXXConversionFunctionName: {
186
0
    OS << "operator ";
187
0
    QualType Type = getCXXNameType();
188
0
    if (const RecordType *Rec = Type->getAs<RecordType>()) {
189
0
      OS << *Rec->getDecl();
190
0
      return;
191
0
    }
192
    // We know we're printing C++ here, ensure we print 'bool' properly.
193
0
    PrintingPolicy CXXPolicy = Policy;
194
0
    CXXPolicy.adjustForCPlusPlus();
195
0
    Type.print(OS, CXXPolicy);
196
0
    return;
197
0
  }
198
0
  case DeclarationName::CXXUsingDirective:
199
0
    OS << "<using-directive>";
200
0
    return;
201
47
  }
202
203
0
  llvm_unreachable("Unexpected declaration name kind");
204
0
}
205
206
namespace clang {
207
208
1
raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) {
209
1
  LangOptions LO;
210
1
  N.print(OS, PrintingPolicy(LO));
211
1
  return OS;
212
1
}
213
214
} // namespace clang
215
216
0
bool DeclarationName::isDependentName() const {
217
0
  QualType T = getCXXNameType();
218
0
  if (!T.isNull() && T->isDependentType())
219
0
    return true;
220
221
  // A class-scope deduction guide in a dependent context has a dependent name.
222
0
  auto *TD = getCXXDeductionGuideTemplate();
223
0
  if (TD && TD->getDeclContext()->isDependentContext())
224
0
    return true;
225
226
0
  return false;
227
0
}
228
229
1
std::string DeclarationName::getAsString() const {
230
1
  std::string Result;
231
1
  llvm::raw_string_ostream OS(Result);
232
1
  OS << *this;
233
1
  return Result;
234
1
}
235
236
25
void *DeclarationName::getFETokenInfoSlow() const {
237
25
  switch (getNameKind()) {
238
0
  case Identifier:
239
0
    llvm_unreachable("case Identifier already handled by getFETokenInfo!");
240
0
  case CXXConstructorName:
241
0
  case CXXDestructorName:
242
0
  case CXXConversionFunctionName:
243
0
    return castAsCXXSpecialNameExtra()->FETokenInfo;
244
23
  case CXXOperatorName:
245
23
    return castAsCXXOperatorIdName()->FETokenInfo;
246
0
  case CXXDeductionGuideName:
247
0
    return castAsCXXDeductionGuideNameExtra()->FETokenInfo;
248
2
  case CXXLiteralOperatorName:
249
2
    return castAsCXXLiteralOperatorIdName()->FETokenInfo;
250
0
  default:
251
0
    llvm_unreachable("DeclarationName has no FETokenInfo!");
252
25
  }
253
25
}
254
255
0
void DeclarationName::setFETokenInfoSlow(void *T) {
256
0
  switch (getNameKind()) {
257
0
  case Identifier:
258
0
    llvm_unreachable("case Identifier already handled by setFETokenInfo!");
259
0
  case CXXConstructorName:
260
0
  case CXXDestructorName:
261
0
  case CXXConversionFunctionName:
262
0
    castAsCXXSpecialNameExtra()->FETokenInfo = T;
263
0
    break;
264
0
  case CXXOperatorName:
265
0
    castAsCXXOperatorIdName()->FETokenInfo = T;
266
0
    break;
267
0
  case CXXDeductionGuideName:
268
0
    castAsCXXDeductionGuideNameExtra()->FETokenInfo = T;
269
0
    break;
270
0
  case CXXLiteralOperatorName:
271
0
    castAsCXXLiteralOperatorIdName()->FETokenInfo = T;
272
0
    break;
273
0
  default:
274
0
    llvm_unreachable("DeclarationName has no FETokenInfo!");
275
0
  }
276
0
}
277
278
0
LLVM_DUMP_METHOD void DeclarationName::dump() const {
279
0
  llvm::errs() << *this << '\n';
280
0
}
281
282
46
DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
283
  // Initialize the overloaded operator names.
284
2.16k
  for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op)
285
2.11k
    CXXOperatorNames[Op].Kind = static_cast<OverloadedOperatorKind>(Op);
286
46
}
287
288
DeclarationName
289
0
DeclarationNameTable::getCXXDeductionGuideName(TemplateDecl *Template) {
290
0
  Template = cast<TemplateDecl>(Template->getCanonicalDecl());
291
292
0
  llvm::FoldingSetNodeID ID;
293
0
  ID.AddPointer(Template);
294
295
0
  void *InsertPos = nullptr;
296
0
  if (auto *Name = CXXDeductionGuideNames.FindNodeOrInsertPos(ID, InsertPos))
297
0
    return DeclarationName(Name);
298
299
0
  auto *Name = new (Ctx) detail::CXXDeductionGuideNameExtra(Template);
300
0
  CXXDeductionGuideNames.InsertNode(Name, InsertPos);
301
0
  return DeclarationName(Name);
302
0
}
303
304
0
DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
305
  // The type of constructors is unqualified.
306
0
  Ty = Ty.getUnqualifiedType();
307
  // Do we already have this C++ constructor name ?
308
0
  llvm::FoldingSetNodeID ID;
309
0
  ID.AddPointer(Ty.getAsOpaquePtr());
310
0
  void *InsertPos = nullptr;
311
0
  if (auto *Name = CXXConstructorNames.FindNodeOrInsertPos(ID, InsertPos))
312
0
    return {Name, DeclarationName::StoredCXXConstructorName};
313
314
  // We have to create it.
315
0
  auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty);
316
0
  CXXConstructorNames.InsertNode(SpecialName, InsertPos);
317
0
  return {SpecialName, DeclarationName::StoredCXXConstructorName};
318
0
}
319
320
0
DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) {
321
  // The type of destructors is unqualified.
322
0
  Ty = Ty.getUnqualifiedType();
323
  // Do we already have this C++ destructor name ?
324
0
  llvm::FoldingSetNodeID ID;
325
0
  ID.AddPointer(Ty.getAsOpaquePtr());
326
0
  void *InsertPos = nullptr;
327
0
  if (auto *Name = CXXDestructorNames.FindNodeOrInsertPos(ID, InsertPos))
328
0
    return {Name, DeclarationName::StoredCXXDestructorName};
329
330
  // We have to create it.
331
0
  auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty);
332
0
  CXXDestructorNames.InsertNode(SpecialName, InsertPos);
333
0
  return {SpecialName, DeclarationName::StoredCXXDestructorName};
334
0
}
335
336
DeclarationName
337
0
DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) {
338
  // Do we already have this C++ conversion function name ?
339
0
  llvm::FoldingSetNodeID ID;
340
0
  ID.AddPointer(Ty.getAsOpaquePtr());
341
0
  void *InsertPos = nullptr;
342
0
  if (auto *Name =
343
0
          CXXConversionFunctionNames.FindNodeOrInsertPos(ID, InsertPos))
344
0
    return {Name, DeclarationName::StoredCXXConversionFunctionName};
345
346
  // We have to create it.
347
0
  auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty);
348
0
  CXXConversionFunctionNames.InsertNode(SpecialName, InsertPos);
349
0
  return {SpecialName, DeclarationName::StoredCXXConversionFunctionName};
350
0
}
351
352
DeclarationName
353
DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
354
0
                                        CanQualType Ty) {
355
0
  switch (Kind) {
356
0
  case DeclarationName::CXXConstructorName:
357
0
    return getCXXConstructorName(Ty);
358
0
  case DeclarationName::CXXDestructorName:
359
0
    return getCXXDestructorName(Ty);
360
0
  case DeclarationName::CXXConversionFunctionName:
361
0
    return getCXXConversionFunctionName(Ty);
362
0
  default:
363
0
    llvm_unreachable("Invalid kind in getCXXSpecialName!");
364
0
  }
365
0
}
366
367
DeclarationName
368
2
DeclarationNameTable::getCXXLiteralOperatorName(const IdentifierInfo *II) {
369
2
  llvm::FoldingSetNodeID ID;
370
2
  ID.AddPointer(II);
371
372
2
  void *InsertPos = nullptr;
373
2
  if (auto *Name = CXXLiteralOperatorNames.FindNodeOrInsertPos(ID, InsertPos))
374
0
    return DeclarationName(Name);
375
376
2
  auto *LiteralName = new (Ctx) detail::CXXLiteralOperatorIdName(II);
377
2
  CXXLiteralOperatorNames.InsertNode(LiteralName, InsertPos);
378
2
  return DeclarationName(LiteralName);
379
2
}
380
381
40.4k
DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
382
40.4k
  switch (Name.getNameKind()) {
383
40.4k
  case DeclarationName::Identifier:
384
40.4k
  case DeclarationName::CXXDeductionGuideName:
385
40.4k
    break;
386
0
  case DeclarationName::CXXConstructorName:
387
0
  case DeclarationName::CXXDestructorName:
388
0
  case DeclarationName::CXXConversionFunctionName:
389
0
    setNamedTypeLoc(nullptr);
390
0
    break;
391
29
  case DeclarationName::CXXOperatorName:
392
29
    setCXXOperatorNameRange(SourceRange());
393
29
    break;
394
4
  case DeclarationName::CXXLiteralOperatorName:
395
4
    setCXXLiteralOperatorNameLoc(SourceLocation());
396
4
    break;
397
0
  case DeclarationName::ObjCZeroArgSelector:
398
0
  case DeclarationName::ObjCOneArgSelector:
399
0
  case DeclarationName::ObjCMultiArgSelector:
400
    // FIXME: ?
401
0
    break;
402
0
  case DeclarationName::CXXUsingDirective:
403
0
    break;
404
40.4k
  }
405
40.4k
}
406
407
3
bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
408
3
  switch (Name.getNameKind()) {
409
3
  case DeclarationName::Identifier:
410
3
  case DeclarationName::ObjCZeroArgSelector:
411
3
  case DeclarationName::ObjCOneArgSelector:
412
3
  case DeclarationName::ObjCMultiArgSelector:
413
3
  case DeclarationName::CXXOperatorName:
414
3
  case DeclarationName::CXXLiteralOperatorName:
415
3
  case DeclarationName::CXXUsingDirective:
416
3
  case DeclarationName::CXXDeductionGuideName:
417
3
    return false;
418
419
0
  case DeclarationName::CXXConstructorName:
420
0
  case DeclarationName::CXXDestructorName:
421
0
  case DeclarationName::CXXConversionFunctionName:
422
0
    if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
423
0
      return TInfo->getType()->containsUnexpandedParameterPack();
424
425
0
    return Name.getCXXNameType()->containsUnexpandedParameterPack();
426
3
  }
427
0
  llvm_unreachable("All name kinds handled.");
428
0
}
429
430
3
bool DeclarationNameInfo::isInstantiationDependent() const {
431
3
  switch (Name.getNameKind()) {
432
3
  case DeclarationName::Identifier:
433
3
  case DeclarationName::ObjCZeroArgSelector:
434
3
  case DeclarationName::ObjCOneArgSelector:
435
3
  case DeclarationName::ObjCMultiArgSelector:
436
3
  case DeclarationName::CXXOperatorName:
437
3
  case DeclarationName::CXXLiteralOperatorName:
438
3
  case DeclarationName::CXXUsingDirective:
439
3
  case DeclarationName::CXXDeductionGuideName:
440
3
    return false;
441
442
0
  case DeclarationName::CXXConstructorName:
443
0
  case DeclarationName::CXXDestructorName:
444
0
  case DeclarationName::CXXConversionFunctionName:
445
0
    if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
446
0
      return TInfo->getType()->isInstantiationDependentType();
447
448
0
    return Name.getCXXNameType()->isInstantiationDependentType();
449
3
  }
450
0
  llvm_unreachable("All name kinds handled.");
451
0
}
452
453
0
std::string DeclarationNameInfo::getAsString() const {
454
0
  std::string Result;
455
0
  llvm::raw_string_ostream OS(Result);
456
0
  OS << *this;
457
0
  return Result;
458
0
}
459
460
0
raw_ostream &clang::operator<<(raw_ostream &OS, DeclarationNameInfo DNInfo) {
461
0
  LangOptions LO;
462
0
  DNInfo.printName(OS, PrintingPolicy(LangOptions()));
463
0
  return OS;
464
0
}
465
466
0
void DeclarationNameInfo::printName(raw_ostream &OS, PrintingPolicy Policy) const {
467
0
  switch (Name.getNameKind()) {
468
0
  case DeclarationName::Identifier:
469
0
  case DeclarationName::ObjCZeroArgSelector:
470
0
  case DeclarationName::ObjCOneArgSelector:
471
0
  case DeclarationName::ObjCMultiArgSelector:
472
0
  case DeclarationName::CXXOperatorName:
473
0
  case DeclarationName::CXXLiteralOperatorName:
474
0
  case DeclarationName::CXXUsingDirective:
475
0
  case DeclarationName::CXXDeductionGuideName:
476
0
    Name.print(OS, Policy);
477
0
    return;
478
479
0
  case DeclarationName::CXXConstructorName:
480
0
  case DeclarationName::CXXDestructorName:
481
0
  case DeclarationName::CXXConversionFunctionName:
482
0
    if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo()) {
483
0
      if (Name.getNameKind() == DeclarationName::CXXDestructorName)
484
0
        OS << '~';
485
0
      else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
486
0
        OS << "operator ";
487
0
      LangOptions LO;
488
0
      Policy.adjustForCPlusPlus();
489
0
      Policy.SuppressScope = true;
490
0
      OS << TInfo->getType().getAsString(Policy);
491
0
    } else
492
0
      Name.print(OS, Policy);
493
0
    return;
494
0
  }
495
0
  llvm_unreachable("Unexpected declaration name kind");
496
0
}
497
498
61.6k
SourceLocation DeclarationNameInfo::getEndLocPrivate() const {
499
61.6k
  switch (Name.getNameKind()) {
500
61.6k
  case DeclarationName::Identifier:
501
61.6k
  case DeclarationName::CXXDeductionGuideName:
502
61.6k
    return NameLoc;
503
504
0
  case DeclarationName::CXXOperatorName:
505
0
    return LocInfo.getCXXOperatorNameEndLoc();
506
507
0
  case DeclarationName::CXXLiteralOperatorName:
508
0
    return LocInfo.getCXXLiteralOperatorNameLoc();
509
510
0
  case DeclarationName::CXXConstructorName:
511
0
  case DeclarationName::CXXDestructorName:
512
0
  case DeclarationName::CXXConversionFunctionName:
513
0
    if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
514
0
      return TInfo->getTypeLoc().getEndLoc();
515
0
    else
516
0
      return NameLoc;
517
518
    // DNInfo work in progress: FIXME.
519
0
  case DeclarationName::ObjCZeroArgSelector:
520
0
  case DeclarationName::ObjCOneArgSelector:
521
0
  case DeclarationName::ObjCMultiArgSelector:
522
0
  case DeclarationName::CXXUsingDirective:
523
0
    return NameLoc;
524
61.6k
  }
525
0
  llvm_unreachable("Unexpected declaration name kind");
526
0
}