Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/TemplateBase.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- TemplateBase.cpp - Common template AST class 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 common classes used throughout C++ template
10
// representations.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/TemplateBase.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/Decl.h"
17
#include "clang/AST/DeclBase.h"
18
#include "clang/AST/DeclTemplate.h"
19
#include "clang/AST/DependenceFlags.h"
20
#include "clang/AST/Expr.h"
21
#include "clang/AST/ExprCXX.h"
22
#include "clang/AST/PrettyPrinter.h"
23
#include "clang/AST/TemplateName.h"
24
#include "clang/AST/Type.h"
25
#include "clang/AST/TypeLoc.h"
26
#include "clang/Basic/Diagnostic.h"
27
#include "clang/Basic/LLVM.h"
28
#include "clang/Basic/LangOptions.h"
29
#include "clang/Basic/SourceLocation.h"
30
#include "llvm/ADT/APSInt.h"
31
#include "llvm/ADT/FoldingSet.h"
32
#include "llvm/ADT/None.h"
33
#include "llvm/ADT/SmallString.h"
34
#include "llvm/ADT/StringExtras.h"
35
#include "llvm/ADT/StringRef.h"
36
#include "llvm/Support/Casting.h"
37
#include "llvm/Support/Compiler.h"
38
#include "llvm/Support/ErrorHandling.h"
39
#include "llvm/Support/raw_ostream.h"
40
#include <cassert>
41
#include <cstddef>
42
#include <cstdint>
43
#include <cstring>
44
45
using namespace clang;
46
47
/// Print a template integral argument value.
48
///
49
/// \param TemplArg the TemplateArgument instance to print.
50
///
51
/// \param Out the raw_ostream instance to use for printing.
52
///
53
/// \param Policy the printing policy for EnumConstantDecl printing.
54
///
55
/// \param IncludeType If set, ensure that the type of the expression printed
56
/// matches the type of the template argument.
57
static void printIntegral(const TemplateArgument &TemplArg, raw_ostream &Out,
58
215k
                          const PrintingPolicy &Policy, bool IncludeType) {
59
215k
  const Type *T = TemplArg.getIntegralType().getTypePtr();
60
215k
  const llvm::APSInt &Val = TemplArg.getAsIntegral();
61
62
215k
  if (Policy.UseEnumerators) {
63
138k
    if (const EnumType *ET = T->getAs<EnumType>()) {
64
1.12k
      for (const EnumConstantDecl *ECD : ET->getDecl()->enumerators()) {
65
        // In Sema::CheckTemplateArugment, enum template arguments value are
66
        // extended to the size of the integer underlying the enum type.  This
67
        // may create a size difference between the enum value and template
68
        // argument value, requiring isSameValue here instead of operator==.
69
1.12k
        if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
70
1.10k
          ECD->printQualifiedName(Out, Policy);
71
1.10k
          return;
72
1.10k
        }
73
1.12k
      }
74
1.10k
    }
75
138k
  }
76
77
214k
  if (Policy.MSVCFormatting)
78
16
    IncludeType = false;
79
80
214k
  if (T->isBooleanType()) {
81
91.8k
    if (!Policy.MSVCFormatting)
82
91.8k
      Out << (Val.getBoolValue() ? 
"true"38.3k
:
"false"53.5k
);
83
0
    else
84
0
      Out << Val;
85
122k
  } else if (T->isCharType()) {
86
117
    if (IncludeType) {
87
43
      if (T->isSpecificBuiltinType(BuiltinType::SChar))
88
4
        Out << "(signed char)";
89
39
      else if (T->isSpecificBuiltinType(BuiltinType::UChar))
90
4
        Out << "(unsigned char)";
91
43
    }
92
117
    CharacterLiteral::print(Val.getZExtValue(), CharacterLiteral::Ascii, Out);
93
122k
  } else if (T->isAnyCharacterType() && 
!Policy.MSVCFormatting104
) {
94
90
    CharacterLiteral::CharacterKind Kind;
95
90
    if (T->isWideCharType())
96
21
      Kind = CharacterLiteral::Wide;
97
69
    else if (T->isChar8Type())
98
46
      Kind = CharacterLiteral::UTF8;
99
23
    else if (T->isChar16Type())
100
0
      Kind = CharacterLiteral::UTF16;
101
23
    else if (T->isChar32Type())
102
23
      Kind = CharacterLiteral::UTF32;
103
0
    else
104
0
      Kind = CharacterLiteral::Ascii;
105
90
    CharacterLiteral::print(Val.getExtValue(), Kind, Out);
106
122k
  } else if (IncludeType) {
107
52.5k
    if (const auto *BT = T->getAs<BuiltinType>()) {
108
52.3k
      switch (BT->getKind()) {
109
168
      case BuiltinType::ULongLong:
110
168
        Out << Val << "ULL";
111
168
        break;
112
0
      case BuiltinType::LongLong:
113
0
        Out << Val << "LL";
114
0
        break;
115
11.5k
      case BuiltinType::ULong:
116
11.5k
        Out << Val << "UL";
117
11.5k
        break;
118
23.2k
      case BuiltinType::Long:
119
23.2k
        Out << Val << "L";
120
23.2k
        break;
121
814
      case BuiltinType::UInt:
122
814
        Out << Val << "U";
123
814
        break;
124
16.5k
      case BuiltinType::Int:
125
16.5k
        Out << Val;
126
16.5k
        break;
127
9
      default:
128
9
        Out << "(" << T->getCanonicalTypeInternal().getAsString(Policy) << ")"
129
9
            << Val;
130
9
        break;
131
52.3k
      }
132
52.3k
    } else
133
130
      Out << "(" << T->getCanonicalTypeInternal().getAsString(Policy) << ")"
134
130
          << Val;
135
52.5k
  } else
136
69.6k
    Out << Val;
137
214k
}
138
139
168
static unsigned getArrayDepth(QualType type) {
140
168
  unsigned count = 0;
141
273
  while (const auto *arrayType = type->getAsArrayTypeUnsafe()) {
142
105
    count++;
143
105
    type = arrayType->getElementType();
144
105
  }
145
168
  return count;
146
168
}
147
148
1.79k
static bool needsAmpersandOnTemplateArg(QualType paramType, QualType argType) {
149
  // Generally, if the parameter type is a pointer, we must be taking the
150
  // address of something and need a &.  However, if the argument is an array,
151
  // this could be implicit via array-to-pointer decay.
152
1.79k
  if (!paramType->isPointerType())
153
365
    return paramType->isMemberPointerType();
154
1.42k
  if (argType->isArrayType())
155
84
    return getArrayDepth(argType) == getArrayDepth(paramType->getPointeeType());
156
1.34k
  return true;
157
1.42k
}
158
159
//===----------------------------------------------------------------------===//
160
// TemplateArgument Implementation
161
//===----------------------------------------------------------------------===//
162
163
TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value,
164
2.55M
                                   QualType Type) {
165
2.55M
  Integer.Kind = Integral;
166
  // Copy the APSInt value into our decomposed form.
167
2.55M
  Integer.BitWidth = Value.getBitWidth();
168
2.55M
  Integer.IsUnsigned = Value.isUnsigned();
169
  // If the value is large, we have to get additional memory from the ASTContext
170
2.55M
  unsigned NumWords = Value.getNumWords();
171
2.55M
  if (NumWords > 1) {
172
36
    void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t));
173
36
    std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t));
174
36
    Integer.pVal = static_cast<uint64_t *>(Mem);
175
2.55M
  } else {
176
2.55M
    Integer.VAL = Value.getZExtValue();
177
2.55M
  }
178
179
2.55M
  Integer.Type = Type.getAsOpaquePtr();
180
2.55M
}
181
182
TemplateArgument
183
TemplateArgument::CreatePackCopy(ASTContext &Context,
184
713k
                                 ArrayRef<TemplateArgument> Args) {
185
713k
  if (Args.empty())
186
29.0k
    return getEmptyPack();
187
188
684k
  return TemplateArgument(Args.copy(Context));
189
713k
}
190
191
41.0M
TemplateArgumentDependence TemplateArgument::getDependence() const {
192
41.0M
  auto Deps = TemplateArgumentDependence::None;
193
41.0M
  switch (getKind()) {
194
0
  case Null:
195
0
    llvm_unreachable("Should not have a NULL template argument");
196
197
32.6M
  case Type:
198
32.6M
    Deps = toTemplateArgumentDependence(getAsType()->getDependence());
199
32.6M
    if (isa<PackExpansionType>(getAsType()))
200
607k
      Deps |= TemplateArgumentDependence::Dependent;
201
32.6M
    return Deps;
202
203
138k
  case Template:
204
138k
    return toTemplateArgumentDependence(getAsTemplate().getDependence());
205
206
216
  case TemplateExpansion:
207
216
    return TemplateArgumentDependence::Dependent |
208
216
           TemplateArgumentDependence::Instantiation;
209
210
3.94k
  case Declaration: {
211
3.94k
    auto *DC = dyn_cast<DeclContext>(getAsDecl());
212
3.94k
    if (!DC)
213
2.51k
      DC = getAsDecl()->getDeclContext();
214
3.94k
    if (DC->isDependentContext())
215
10
      Deps = TemplateArgumentDependence::Dependent |
216
10
             TemplateArgumentDependence::Instantiation;
217
3.94k
    return Deps;
218
0
  }
219
220
4.95k
  case NullPtr:
221
1.81M
  case Integral:
222
1.81M
    return TemplateArgumentDependence::None;
223
224
5.84M
  case Expression:
225
5.84M
    Deps = toTemplateArgumentDependence(getAsExpr()->getDependence());
226
5.84M
    if (isa<PackExpansionExpr>(getAsExpr()))
227
112k
      Deps |= TemplateArgumentDependence::Dependent |
228
112k
              TemplateArgumentDependence::Instantiation;
229
5.84M
    return Deps;
230
231
615k
  case Pack:
232
615k
    for (const auto &P : pack_elements())
233
1.02M
      Deps |= P.getDependence();
234
615k
    return Deps;
235
41.0M
  }
236
0
  llvm_unreachable("unhandled ArgKind");
237
0
}
238
239
9.35M
bool TemplateArgument::isDependent() const {
240
9.35M
  return getDependence() & TemplateArgumentDependence::Dependent;
241
9.35M
}
242
243
3.20M
bool TemplateArgument::isInstantiationDependent() const {
244
3.20M
  return getDependence() & TemplateArgumentDependence::Instantiation;
245
3.20M
}
246
247
39.9M
bool TemplateArgument::isPackExpansion() const {
248
39.9M
  switch (getKind()) {
249
0
  case Null:
250
2.83k
  case Declaration:
251
2.21M
  case Integral:
252
2.27M
  case Pack:
253
2.36M
  case Template:
254
2.37M
  case NullPtr:
255
2.37M
    return false;
256
257
189
  case TemplateExpansion:
258
189
    return true;
259
260
31.4M
  case Type:
261
31.4M
    return isa<PackExpansionType>(getAsType());
262
263
6.12M
  case Expression:
264
6.12M
    return isa<PackExpansionExpr>(getAsExpr());
265
39.9M
  }
266
267
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
268
0
}
269
270
960k
bool TemplateArgument::containsUnexpandedParameterPack() const {
271
960k
  return getDependence() & TemplateArgumentDependence::UnexpandedPack;
272
960k
}
273
274
147
Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
275
147
  assert(getKind() == TemplateExpansion);
276
147
  if (TemplateArg.NumExpansions)
277
3
    return TemplateArg.NumExpansions - 1;
278
279
144
  return None;
280
147
}
281
282
87.8k
QualType TemplateArgument::getNonTypeTemplateArgumentType() const {
283
87.8k
  switch (getKind()) {
284
0
  case TemplateArgument::Null:
285
85.5k
  case TemplateArgument::Type:
286
85.6k
  case TemplateArgument::Template:
287
85.6k
  case TemplateArgument::TemplateExpansion:
288
85.7k
  case TemplateArgument::Pack:
289
85.7k
    return QualType();
290
291
234
  case TemplateArgument::Integral:
292
234
    return getIntegralType();
293
294
1.86k
  case TemplateArgument::Expression:
295
1.86k
    return getAsExpr()->getType();
296
297
26
  case TemplateArgument::Declaration:
298
26
    return getParamTypeForDecl();
299
300
0
  case TemplateArgument::NullPtr:
301
0
    return getNullPtrType();
302
87.8k
  }
303
304
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
305
0
}
306
307
void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
308
48.4M
                               const ASTContext &Context) const {
309
48.4M
  ID.AddInteger(getKind());
310
48.4M
  switch (getKind()) {
311
0
  case Null:
312
0
    break;
313
314
37.2M
  case Type:
315
37.2M
    getAsType().Profile(ID);
316
37.2M
    break;
317
318
54.1k
  case NullPtr:
319
54.1k
    getNullPtrType().Profile(ID);
320
54.1k
    break;
321
322
10.3k
  case Declaration:
323
10.3k
    getParamTypeForDecl().Profile(ID);
324
10.3k
    ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 
nullptr0
);
325
10.3k
    break;
326
327
111k
  case Template:
328
111k
  case TemplateExpansion: {
329
111k
    TemplateName Template = getAsTemplateOrTemplatePattern();
330
111k
    if (TemplateTemplateParmDecl *TTP
331
111k
          = dyn_cast_or_null<TemplateTemplateParmDecl>(
332
111k
                                                Template.getAsTemplateDecl())) {
333
27.1k
      ID.AddBoolean(true);
334
27.1k
      ID.AddInteger(TTP->getDepth());
335
27.1k
      ID.AddInteger(TTP->getPosition());
336
27.1k
      ID.AddBoolean(TTP->isParameterPack());
337
84.6k
    } else {
338
84.6k
      ID.AddBoolean(false);
339
84.6k
      ID.AddPointer(Context.getCanonicalTemplateName(Template)
340
84.6k
                                                          .getAsVoidPointer());
341
84.6k
    }
342
111k
    break;
343
111k
  }
344
345
4.41M
  case Integral:
346
4.41M
    getAsIntegral().Profile(ID);
347
4.41M
    getIntegralType().Profile(ID);
348
4.41M
    break;
349
350
5.08M
  case Expression:
351
5.08M
    getAsExpr()->Profile(ID, Context, true);
352
5.08M
    break;
353
354
1.59M
  case Pack:
355
1.59M
    ID.AddInteger(Args.NumArgs);
356
4.03M
    for (unsigned I = 0; I != Args.NumArgs; 
++I2.43M
)
357
2.43M
      Args.Args[I].Profile(ID, Context);
358
48.4M
  }
359
48.4M
}
360
361
10.7M
bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
362
10.7M
  if (getKind() != Other.getKind()) 
return false674
;
363
364
10.7M
  switch (getKind()) {
365
0
  case Null:
366
8.52M
  case Type:
367
10.0M
  case Expression:
368
10.0M
  case NullPtr:
369
10.0M
    return TypeOrValue.V == Other.TypeOrValue.V;
370
371
27.6k
  case Template:
372
27.7k
  case TemplateExpansion:
373
27.7k
    return TemplateArg.Name == Other.TemplateArg.Name &&
374
27.7k
           
TemplateArg.NumExpansions == Other.TemplateArg.NumExpansions19.0k
;
375
376
43
  case Declaration:
377
43
    return getAsDecl() == Other.getAsDecl();
378
379
231k
  case Integral:
380
231k
    return getIntegralType() == Other.getIntegralType() &&
381
231k
           getAsIntegral() == Other.getAsIntegral();
382
383
414k
  case Pack:
384
414k
    if (Args.NumArgs != Other.Args.NumArgs) 
return false0
;
385
906k
    
for (unsigned I = 0, E = Args.NumArgs; 414k
I != E;
++I491k
)
386
518k
      if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
387
26.9k
        return false;
388
387k
    return true;
389
10.7M
  }
390
391
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
392
0
}
393
394
70.6k
TemplateArgument TemplateArgument::getPackExpansionPattern() const {
395
70.6k
  assert(isPackExpansion());
396
397
0
  switch (getKind()) {
398
66.2k
  case Type:
399
66.2k
    return getAsType()->castAs<PackExpansionType>()->getPattern();
400
401
4.35k
  case Expression:
402
4.35k
    return cast<PackExpansionExpr>(getAsExpr())->getPattern();
403
404
54
  case TemplateExpansion:
405
54
    return TemplateArgument(getAsTemplateOrTemplatePattern());
406
407
0
  case Declaration:
408
0
  case Integral:
409
0
  case Pack:
410
0
  case Null:
411
0
  case Template:
412
0
  case NullPtr:
413
0
    return TemplateArgument();
414
70.6k
  }
415
416
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
417
0
}
418
419
void TemplateArgument::print(const PrintingPolicy &Policy, raw_ostream &Out,
420
2.11M
                             bool IncludeType) const {
421
422
2.11M
  switch (getKind()) {
423
101
  case Null:
424
101
    Out << "(no value)";
425
101
    break;
426
427
1.86M
  case Type: {
428
1.86M
    PrintingPolicy SubPolicy(Policy);
429
1.86M
    SubPolicy.SuppressStrongLifetime = true;
430
1.86M
    getAsType().print(Out, SubPolicy);
431
1.86M
    break;
432
0
  }
433
434
1.86k
  case Declaration: {
435
    // FIXME: Include the type if it's not obvious from the context.
436
1.86k
    NamedDecl *ND = getAsDecl();
437
1.86k
    if (getParamTypeForDecl()->isRecordType()) {
438
73
      if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
439
73
        TPO->printAsInit(Out, Policy);
440
73
        break;
441
73
      }
442
73
    }
443
1.79k
    if (auto *VD = dyn_cast<ValueDecl>(ND)) {
444
1.79k
      if (needsAmpersandOnTemplateArg(getParamTypeForDecl(), VD->getType()))
445
1.36k
        Out << "&";
446
1.79k
    }
447
1.79k
    ND->printQualifiedName(Out);
448
1.79k
    break;
449
1.86k
  }
450
451
1.43k
  case NullPtr:
452
    // FIXME: Include the type if it's not obvious from the context.
453
1.43k
    Out << "nullptr";
454
1.43k
    break;
455
456
1.69k
  case Template:
457
1.69k
    getAsTemplate().print(Out, Policy, TemplateName::Qualified::Fully);
458
1.69k
    break;
459
460
2
  case TemplateExpansion:
461
2
    getAsTemplateOrTemplatePattern().print(Out, Policy);
462
2
    Out << "...";
463
2
    break;
464
465
215k
  case Integral:
466
215k
    printIntegral(*this, Out, Policy, IncludeType);
467
215k
    break;
468
469
28.5k
  case Expression:
470
28.5k
    getAsExpr()->printPretty(Out, nullptr, Policy);
471
28.5k
    break;
472
473
1.32k
  case Pack:
474
1.32k
    Out << "<";
475
1.32k
    bool First = true;
476
3.37k
    for (const auto &P : pack_elements()) {
477
3.37k
      if (First)
478
1.26k
        First = false;
479
2.10k
      else
480
2.10k
        Out << ", ";
481
482
3.37k
      P.print(Policy, Out, IncludeType);
483
3.37k
    }
484
1.32k
    Out << ">";
485
1.32k
    break;
486
2.11M
  }
487
2.11M
}
488
489
0
void TemplateArgument::dump(raw_ostream &Out) const {
490
0
  LangOptions LO; // FIXME! see also TemplateName::dump().
491
0
  LO.CPlusPlus = true;
492
0
  LO.Bool = true;
493
0
  print(PrintingPolicy(LO), Out, /*IncludeType*/ true);
494
0
}
495
496
0
LLVM_DUMP_METHOD void TemplateArgument::dump() const { dump(llvm::errs()); }
497
498
//===----------------------------------------------------------------------===//
499
// TemplateArgumentLoc Implementation
500
//===----------------------------------------------------------------------===//
501
502
369k
SourceRange TemplateArgumentLoc::getSourceRange() const {
503
369k
  switch (Argument.getKind()) {
504
158k
  case TemplateArgument::Expression:
505
158k
    return getSourceExpression()->getSourceRange();
506
507
0
  case TemplateArgument::Declaration:
508
0
    return getSourceDeclExpression()->getSourceRange();
509
510
0
  case TemplateArgument::NullPtr:
511
0
    return getSourceNullPtrExpression()->getSourceRange();
512
513
201k
  case TemplateArgument::Type:
514
201k
    if (TypeSourceInfo *TSI = getTypeSourceInfo())
515
201k
      return TSI->getTypeLoc().getSourceRange();
516
0
    else
517
0
      return SourceRange();
518
519
9.42k
  case TemplateArgument::Template:
520
9.42k
    if (getTemplateQualifierLoc())
521
43
      return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
522
43
                         getTemplateNameLoc());
523
9.38k
    return SourceRange(getTemplateNameLoc());
524
525
2
  case TemplateArgument::TemplateExpansion:
526
2
    if (getTemplateQualifierLoc())
527
1
      return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
528
1
                         getTemplateEllipsisLoc());
529
1
    return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
530
531
0
  case TemplateArgument::Integral:
532
0
    return getSourceIntegralExpression()->getSourceRange();
533
534
0
  case TemplateArgument::Pack:
535
0
  case TemplateArgument::Null:
536
0
    return SourceRange();
537
369k
  }
538
539
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
540
0
}
541
542
template <typename T>
543
1.23k
static const T &DiagTemplateArg(const T &DB, const TemplateArgument &Arg) {
544
1.23k
  switch (Arg.getKind()) {
545
0
  case TemplateArgument::Null:
546
    // This is bad, but not as bad as crashing because of argument
547
    // count mismatches.
548
0
    return DB << "(null template argument)";
549
550
526
  case TemplateArgument::Type:
551
526
    return DB << Arg.getAsType();
552
553
8
  case TemplateArgument::Declaration:
554
8
    return DB << Arg.getAsDecl();
555
556
0
  case TemplateArgument::NullPtr:
557
0
    return DB << "nullptr";
558
559
44
  case TemplateArgument::Integral:
560
44
    return DB << toString(Arg.getAsIntegral(), 10);
561
562
62
  case TemplateArgument::Template:
563
62
    return DB << Arg.getAsTemplate();
564
565
0
  case TemplateArgument::TemplateExpansion:
566
0
    return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
567
568
461
  case TemplateArgument::Expression: {
569
    // This shouldn't actually ever happen, so it's okay that we're
570
    // regurgitating an expression here.
571
    // FIXME: We're guessing at LangOptions!
572
461
    SmallString<32> Str;
573
461
    llvm::raw_svector_ostream OS(Str);
574
461
    LangOptions LangOpts;
575
461
    LangOpts.CPlusPlus = true;
576
461
    PrintingPolicy Policy(LangOpts);
577
461
    Arg.getAsExpr()->printPretty(OS, nullptr, Policy);
578
461
    return DB << OS.str();
579
0
  }
580
581
132
  case TemplateArgument::Pack: {
582
    // FIXME: We're guessing at LangOptions!
583
132
    SmallString<32> Str;
584
132
    llvm::raw_svector_ostream OS(Str);
585
132
    LangOptions LangOpts;
586
132
    LangOpts.CPlusPlus = true;
587
132
    PrintingPolicy Policy(LangOpts);
588
132
    Arg.print(Policy, OS, /*IncludeType*/ true);
589
132
    return DB << OS.str();
590
0
  }
591
1.23k
  }
592
593
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
594
0
}
595
596
const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
597
1.23k
                                             const TemplateArgument &Arg) {
598
1.23k
  return DiagTemplateArg(DB, Arg);
599
1.23k
}
600
601
clang::TemplateArgumentLocInfo::TemplateArgumentLocInfo(
602
    ASTContext &Ctx, NestedNameSpecifierLoc QualifierLoc,
603
89.4k
    SourceLocation TemplateNameLoc, SourceLocation EllipsisLoc) {
604
89.4k
  TemplateTemplateArgLocInfo *Template = new (Ctx) TemplateTemplateArgLocInfo;
605
89.4k
  Template->Qualifier = QualifierLoc.getNestedNameSpecifier();
606
89.4k
  Template->QualifierLocData = QualifierLoc.getOpaqueData();
607
89.4k
  Template->TemplateNameLoc = TemplateNameLoc;
608
89.4k
  Template->EllipsisLoc = EllipsisLoc;
609
89.4k
  Pointer = Template;
610
89.4k
}
611
612
const ASTTemplateArgumentListInfo *
613
ASTTemplateArgumentListInfo::Create(const ASTContext &C,
614
266k
                                    const TemplateArgumentListInfo &List) {
615
266k
  std::size_t size = totalSizeToAlloc<TemplateArgumentLoc>(List.size());
616
266k
  void *Mem = C.Allocate(size, alignof(ASTTemplateArgumentListInfo));
617
266k
  return new (Mem) ASTTemplateArgumentListInfo(List);
618
266k
}
619
620
const ASTTemplateArgumentListInfo *
621
ASTTemplateArgumentListInfo::Create(const ASTContext &C,
622
0
                                    const ASTTemplateArgumentListInfo *List) {
623
0
  if (!List)
624
0
    return nullptr;
625
0
  std::size_t size =
626
0
      totalSizeToAlloc<TemplateArgumentLoc>(List->getNumTemplateArgs());
627
0
  void *Mem = C.Allocate(size, alignof(ASTTemplateArgumentListInfo));
628
0
  return new (Mem) ASTTemplateArgumentListInfo(List);
629
0
}
630
631
ASTTemplateArgumentListInfo::ASTTemplateArgumentListInfo(
632
266k
    const TemplateArgumentListInfo &Info) {
633
266k
  LAngleLoc = Info.getLAngleLoc();
634
266k
  RAngleLoc = Info.getRAngleLoc();
635
266k
  NumTemplateArgs = Info.size();
636
637
266k
  TemplateArgumentLoc *ArgBuffer = getTrailingObjects<TemplateArgumentLoc>();
638
804k
  for (unsigned i = 0; i != NumTemplateArgs; 
++i537k
)
639
537k
    new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
640
266k
}
641
642
ASTTemplateArgumentListInfo::ASTTemplateArgumentListInfo(
643
0
    const ASTTemplateArgumentListInfo *Info) {
644
0
  LAngleLoc = Info->getLAngleLoc();
645
0
  RAngleLoc = Info->getRAngleLoc();
646
0
  NumTemplateArgs = Info->getNumTemplateArgs();
647
648
0
  TemplateArgumentLoc *ArgBuffer = getTrailingObjects<TemplateArgumentLoc>();
649
0
  for (unsigned i = 0; i != NumTemplateArgs; ++i)
650
0
    new (&ArgBuffer[i]) TemplateArgumentLoc((*Info)[i]);
651
0
}
652
653
void ASTTemplateKWAndArgsInfo::initializeFrom(
654
    SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
655
94.0k
    TemplateArgumentLoc *OutArgArray) {
656
94.0k
  this->TemplateKWLoc = TemplateKWLoc;
657
94.0k
  LAngleLoc = Info.getLAngleLoc();
658
94.0k
  RAngleLoc = Info.getRAngleLoc();
659
94.0k
  NumTemplateArgs = Info.size();
660
661
216k
  for (unsigned i = 0; i != NumTemplateArgs; 
++i122k
)
662
122k
    new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
663
94.0k
}
664
665
307
void ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) {
666
307
  assert(TemplateKWLoc.isValid());
667
0
  LAngleLoc = SourceLocation();
668
307
  RAngleLoc = SourceLocation();
669
307
  this->TemplateKWLoc = TemplateKWLoc;
670
307
  NumTemplateArgs = 0;
671
307
}
672
673
void ASTTemplateKWAndArgsInfo::initializeFrom(
674
    SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
675
859k
    TemplateArgumentLoc *OutArgArray, TemplateArgumentDependence &Deps) {
676
859k
  this->TemplateKWLoc = TemplateKWLoc;
677
859k
  LAngleLoc = Info.getLAngleLoc();
678
859k
  RAngleLoc = Info.getRAngleLoc();
679
859k
  NumTemplateArgs = Info.size();
680
681
1.99M
  for (unsigned i = 0; i != NumTemplateArgs; 
++i1.13M
) {
682
1.13M
    Deps |= Info[i].getArgument().getDependence();
683
684
1.13M
    new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
685
1.13M
  }
686
859k
}
687
688
void ASTTemplateKWAndArgsInfo::copyInto(const TemplateArgumentLoc *ArgArray,
689
436k
                                        TemplateArgumentListInfo &Info) const {
690
436k
  Info.setLAngleLoc(LAngleLoc);
691
436k
  Info.setRAngleLoc(RAngleLoc);
692
995k
  for (unsigned I = 0; I != NumTemplateArgs; 
++I558k
)
693
558k
    Info.addArgument(ArgArray[I]);
694
436k
}