Coverage Report

Created: 2022-01-18 06:27

/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
197k
                          const PrintingPolicy &Policy, bool IncludeType) {
59
197k
  const Type *T = TemplArg.getIntegralType().getTypePtr();
60
197k
  const llvm::APSInt &Val = TemplArg.getAsIntegral();
61
62
197k
  if (const EnumType *ET = T->getAs<EnumType>()) {
63
1.26k
    for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) {
64
      // In Sema::CheckTemplateArugment, enum template arguments value are
65
      // extended to the size of the integer underlying the enum type.  This
66
      // may create a size difference between the enum value and template
67
      // argument value, requiring isSameValue here instead of operator==.
68
1.26k
      if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
69
1.23k
        ECD->printQualifiedName(Out, Policy);
70
1.23k
        return;
71
1.23k
      }
72
1.26k
    }
73
1.23k
  }
74
75
196k
  if (Policy.MSVCFormatting)
76
16
    IncludeType = false;
77
78
196k
  if (T->isBooleanType()) {
79
85.5k
    if (!Policy.MSVCFormatting)
80
85.5k
      Out << (Val.getBoolValue() ? 
"true"34.9k
:
"false"50.6k
);
81
0
    else
82
0
      Out << Val;
83
110k
  } else if (T->isCharType()) {
84
115
    if (IncludeType) {
85
43
      if (T->isSpecificBuiltinType(BuiltinType::SChar))
86
4
        Out << "(signed char)";
87
39
      else if (T->isSpecificBuiltinType(BuiltinType::UChar))
88
4
        Out << "(unsigned char)";
89
43
    }
90
115
    CharacterLiteral::print(Val.getZExtValue(), CharacterLiteral::Ascii, Out);
91
110k
  } else if (T->isAnyCharacterType() && 
!Policy.MSVCFormatting104
) {
92
90
    CharacterLiteral::CharacterKind Kind;
93
90
    if (T->isWideCharType())
94
21
      Kind = CharacterLiteral::Wide;
95
69
    else if (T->isChar8Type())
96
46
      Kind = CharacterLiteral::UTF8;
97
23
    else if (T->isChar16Type())
98
0
      Kind = CharacterLiteral::UTF16;
99
23
    else if (T->isChar32Type())
100
23
      Kind = CharacterLiteral::UTF32;
101
0
    else
102
0
      Kind = CharacterLiteral::Ascii;
103
90
    CharacterLiteral::print(Val.getExtValue(), Kind, Out);
104
110k
  } else if (IncludeType) {
105
47.5k
    if (const auto *BT = T->getAs<BuiltinType>()) {
106
47.5k
      switch (BT->getKind()) {
107
154
      case BuiltinType::ULongLong:
108
154
        Out << Val << "ULL";
109
154
        break;
110
0
      case BuiltinType::LongLong:
111
0
        Out << Val << "LL";
112
0
        break;
113
9.46k
      case BuiltinType::ULong:
114
9.46k
        Out << Val << "UL";
115
9.46k
        break;
116
21.9k
      case BuiltinType::Long:
117
21.9k
        Out << Val << "L";
118
21.9k
        break;
119
718
      case BuiltinType::UInt:
120
718
        Out << Val << "U";
121
718
        break;
122
15.2k
      case BuiltinType::Int:
123
15.2k
        Out << Val;
124
15.2k
        break;
125
8
      default:
126
8
        Out << "(" << T->getCanonicalTypeInternal().getAsString(Policy) << ")"
127
8
            << Val;
128
8
        break;
129
47.5k
      }
130
47.5k
    } else
131
1
      Out << "(" << T->getCanonicalTypeInternal().getAsString(Policy) << ")"
132
1
          << Val;
133
47.5k
  } else
134
62.8k
    Out << Val;
135
196k
}
136
137
168
static unsigned getArrayDepth(QualType type) {
138
168
  unsigned count = 0;
139
273
  while (const auto *arrayType = type->getAsArrayTypeUnsafe()) {
140
105
    count++;
141
105
    type = arrayType->getElementType();
142
105
  }
143
168
  return count;
144
168
}
145
146
1.79k
static bool needsAmpersandOnTemplateArg(QualType paramType, QualType argType) {
147
  // Generally, if the parameter type is a pointer, we must be taking the
148
  // address of something and need a &.  However, if the argument is an array,
149
  // this could be implicit via array-to-pointer decay.
150
1.79k
  if (!paramType->isPointerType())
151
365
    return paramType->isMemberPointerType();
152
1.42k
  if (argType->isArrayType())
153
84
    return getArrayDepth(argType) == getArrayDepth(paramType->getPointeeType());
154
1.34k
  return true;
155
1.42k
}
156
157
//===----------------------------------------------------------------------===//
158
// TemplateArgument Implementation
159
//===----------------------------------------------------------------------===//
160
161
TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value,
162
2.21M
                                   QualType Type) {
163
2.21M
  Integer.Kind = Integral;
164
  // Copy the APSInt value into our decomposed form.
165
2.21M
  Integer.BitWidth = Value.getBitWidth();
166
2.21M
  Integer.IsUnsigned = Value.isUnsigned();
167
  // If the value is large, we have to get additional memory from the ASTContext
168
2.21M
  unsigned NumWords = Value.getNumWords();
169
2.21M
  if (NumWords > 1) {
170
35
    void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t));
171
35
    std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t));
172
35
    Integer.pVal = static_cast<uint64_t *>(Mem);
173
2.21M
  } else {
174
2.21M
    Integer.VAL = Value.getZExtValue();
175
2.21M
  }
176
177
2.21M
  Integer.Type = Type.getAsOpaquePtr();
178
2.21M
}
179
180
TemplateArgument
181
TemplateArgument::CreatePackCopy(ASTContext &Context,
182
625k
                                 ArrayRef<TemplateArgument> Args) {
183
625k
  if (Args.empty())
184
27.2k
    return getEmptyPack();
185
186
598k
  return TemplateArgument(Args.copy(Context));
187
625k
}
188
189
31.8M
TemplateArgumentDependence TemplateArgument::getDependence() const {
190
31.8M
  auto Deps = TemplateArgumentDependence::None;
191
31.8M
  switch (getKind()) {
192
0
  case Null:
193
0
    llvm_unreachable("Should not have a NULL template argument");
194
195
24.8M
  case Type:
196
24.8M
    Deps = toTemplateArgumentDependence(getAsType()->getDependence());
197
24.8M
    if (isa<PackExpansionType>(getAsType()))
198
613k
      Deps |= TemplateArgumentDependence::Dependent;
199
24.8M
    return Deps;
200
201
118k
  case Template:
202
118k
    return toTemplateArgumentDependence(getAsTemplate().getDependence());
203
204
216
  case TemplateExpansion:
205
216
    return TemplateArgumentDependence::Dependent |
206
216
           TemplateArgumentDependence::Instantiation;
207
208
3.83k
  case Declaration: {
209
3.83k
    auto *DC = dyn_cast<DeclContext>(getAsDecl());
210
3.83k
    if (!DC)
211
2.40k
      DC = getAsDecl()->getDeclContext();
212
3.83k
    if (DC->isDependentContext())
213
10
      Deps = TemplateArgumentDependence::Dependent |
214
10
             TemplateArgumentDependence::Instantiation;
215
3.83k
    return Deps;
216
0
  }
217
218
1.71k
  case NullPtr:
219
1.56M
  case Integral:
220
1.56M
    return TemplateArgumentDependence::None;
221
222
4.79M
  case Expression:
223
4.79M
    Deps = toTemplateArgumentDependence(getAsExpr()->getDependence());
224
4.79M
    if (isa<PackExpansionExpr>(getAsExpr()))
225
97.9k
      Deps |= TemplateArgumentDependence::Dependent |
226
97.9k
              TemplateArgumentDependence::Instantiation;
227
4.79M
    return Deps;
228
229
553k
  case Pack:
230
553k
    for (const auto &P : pack_elements())
231
908k
      Deps |= P.getDependence();
232
553k
    return Deps;
233
31.8M
  }
234
0
  llvm_unreachable("unhandled ArgKind");
235
0
}
236
237
7.42M
bool TemplateArgument::isDependent() const {
238
7.42M
  return getDependence() & TemplateArgumentDependence::Dependent;
239
7.42M
}
240
241
2.82M
bool TemplateArgument::isInstantiationDependent() const {
242
2.82M
  return getDependence() & TemplateArgumentDependence::Instantiation;
243
2.82M
}
244
245
31.9M
bool TemplateArgument::isPackExpansion() const {
246
31.9M
  switch (getKind()) {
247
0
  case Null:
248
2.80k
  case Declaration:
249
1.95M
  case Integral:
250
2.01M
  case Pack:
251
2.09M
  case Template:
252
2.09M
  case NullPtr:
253
2.09M
    return false;
254
255
189
  case TemplateExpansion:
256
189
    return true;
257
258
24.5M
  case Type:
259
24.5M
    return isa<PackExpansionType>(getAsType());
260
261
5.24M
  case Expression:
262
5.24M
    return isa<PackExpansionExpr>(getAsExpr());
263
31.9M
  }
264
265
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
266
0
}
267
268
934k
bool TemplateArgument::containsUnexpandedParameterPack() const {
269
934k
  return getDependence() & TemplateArgumentDependence::UnexpandedPack;
270
934k
}
271
272
147
Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
273
147
  assert(getKind() == TemplateExpansion);
274
147
  if (TemplateArg.NumExpansions)
275
3
    return TemplateArg.NumExpansions - 1;
276
277
144
  return None;
278
147
}
279
280
88.1k
QualType TemplateArgument::getNonTypeTemplateArgumentType() const {
281
88.1k
  switch (getKind()) {
282
0
  case TemplateArgument::Null:
283
85.9k
  case TemplateArgument::Type:
284
86.0k
  case TemplateArgument::Template:
285
86.0k
  case TemplateArgument::TemplateExpansion:
286
86.1k
  case TemplateArgument::Pack:
287
86.1k
    return QualType();
288
289
222
  case TemplateArgument::Integral:
290
222
    return getIntegralType();
291
292
1.76k
  case TemplateArgument::Expression:
293
1.76k
    return getAsExpr()->getType();
294
295
26
  case TemplateArgument::Declaration:
296
26
    return getParamTypeForDecl();
297
298
0
  case TemplateArgument::NullPtr:
299
0
    return getNullPtrType();
300
88.1k
  }
301
302
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
303
0
}
304
305
void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
306
37.7M
                               const ASTContext &Context) const {
307
37.7M
  ID.AddInteger(getKind());
308
37.7M
  switch (getKind()) {
309
0
  case Null:
310
0
    break;
311
312
28.4M
  case Type:
313
28.4M
    getAsType().Profile(ID);
314
28.4M
    break;
315
316
11.3k
  case NullPtr:
317
11.3k
    getNullPtrType().Profile(ID);
318
11.3k
    break;
319
320
9.93k
  case Declaration:
321
9.93k
    getParamTypeForDecl().Profile(ID);
322
9.93k
    ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 
nullptr0
);
323
9.93k
    break;
324
325
92.5k
  case Template:
326
92.6k
  case TemplateExpansion: {
327
92.6k
    TemplateName Template = getAsTemplateOrTemplatePattern();
328
92.6k
    if (TemplateTemplateParmDecl *TTP
329
92.6k
          = dyn_cast_or_null<TemplateTemplateParmDecl>(
330
92.6k
                                                Template.getAsTemplateDecl())) {
331
14.8k
      ID.AddBoolean(true);
332
14.8k
      ID.AddInteger(TTP->getDepth());
333
14.8k
      ID.AddInteger(TTP->getPosition());
334
14.8k
      ID.AddBoolean(TTP->isParameterPack());
335
77.8k
    } else {
336
77.8k
      ID.AddBoolean(false);
337
77.8k
      ID.AddPointer(Context.getCanonicalTemplateName(Template)
338
77.8k
                                                          .getAsVoidPointer());
339
77.8k
    }
340
92.6k
    break;
341
92.5k
  }
342
343
3.55M
  case Integral:
344
3.55M
    getAsIntegral().Profile(ID);
345
3.55M
    getIntegralType().Profile(ID);
346
3.55M
    break;
347
348
4.25M
  case Expression:
349
4.25M
    getAsExpr()->Profile(ID, Context, true);
350
4.25M
    break;
351
352
1.38M
  case Pack:
353
1.38M
    ID.AddInteger(Args.NumArgs);
354
3.54M
    for (unsigned I = 0; I != Args.NumArgs; 
++I2.15M
)
355
2.15M
      Args.Args[I].Profile(ID, Context);
356
37.7M
  }
357
37.7M
}
358
359
8.18M
bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
360
8.18M
  if (getKind() != Other.getKind()) 
return false681
;
361
362
8.18M
  switch (getKind()) {
363
0
  case Null:
364
6.30M
  case Type:
365
7.59M
  case Expression:
366
7.59M
  case NullPtr:
367
7.59M
    return TypeOrValue.V == Other.TypeOrValue.V;
368
369
21.5k
  case Template:
370
21.6k
  case TemplateExpansion:
371
21.6k
    return TemplateArg.Name == Other.TemplateArg.Name &&
372
21.6k
           
TemplateArg.NumExpansions == Other.TemplateArg.NumExpansions17.1k
;
373
374
43
  case Declaration:
375
43
    return getAsDecl() == Other.getAsDecl();
376
377
194k
  case Integral:
378
194k
    return getIntegralType() == Other.getIntegralType() &&
379
194k
           getAsIntegral() == Other.getAsIntegral();
380
381
375k
  case Pack:
382
375k
    if (Args.NumArgs != Other.Args.NumArgs) 
return false0
;
383
801k
    
for (unsigned I = 0, E = Args.NumArgs; 375k
I != E;
++I426k
)
384
455k
      if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
385
28.7k
        return false;
386
346k
    return true;
387
8.18M
  }
388
389
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
390
0
}
391
392
95.3k
TemplateArgument TemplateArgument::getPackExpansionPattern() const {
393
95.3k
  assert(isPackExpansion());
394
395
0
  switch (getKind()) {
396
91.0k
  case Type:
397
91.0k
    return getAsType()->castAs<PackExpansionType>()->getPattern();
398
399
4.24k
  case Expression:
400
4.24k
    return cast<PackExpansionExpr>(getAsExpr())->getPattern();
401
402
54
  case TemplateExpansion:
403
54
    return TemplateArgument(getAsTemplateOrTemplatePattern());
404
405
0
  case Declaration:
406
0
  case Integral:
407
0
  case Pack:
408
0
  case Null:
409
0
  case Template:
410
0
  case NullPtr:
411
0
    return TemplateArgument();
412
95.3k
  }
413
414
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
415
0
}
416
417
void TemplateArgument::print(const PrintingPolicy &Policy, raw_ostream &Out,
418
1.79M
                             bool IncludeType) const {
419
420
1.79M
  switch (getKind()) {
421
95
  case Null:
422
95
    Out << "(no value)";
423
95
    break;
424
425
1.57M
  case Type: {
426
1.57M
    PrintingPolicy SubPolicy(Policy);
427
1.57M
    SubPolicy.SuppressStrongLifetime = true;
428
1.57M
    getAsType().print(Out, SubPolicy);
429
1.57M
    break;
430
0
  }
431
432
1.81k
  case Declaration: {
433
    // FIXME: Include the type if it's not obvious from the context.
434
1.81k
    NamedDecl *ND = getAsDecl();
435
1.81k
    if (getParamTypeForDecl()->isRecordType()) {
436
19
      if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
437
19
        TPO->printAsInit(Out);
438
19
        break;
439
19
      }
440
19
    }
441
1.79k
    if (auto *VD = dyn_cast<ValueDecl>(ND)) {
442
1.79k
      if (needsAmpersandOnTemplateArg(getParamTypeForDecl(), VD->getType()))
443
1.36k
        Out << "&";
444
1.79k
    }
445
1.79k
    ND->printQualifiedName(Out);
446
1.79k
    break;
447
1.81k
  }
448
449
1.27k
  case NullPtr:
450
    // FIXME: Include the type if it's not obvious from the context.
451
1.27k
    Out << "nullptr";
452
1.27k
    break;
453
454
1.78k
  case Template:
455
1.78k
    getAsTemplate().print(Out, Policy, TemplateName::Qualified::Fully);
456
1.78k
    break;
457
458
2
  case TemplateExpansion:
459
2
    getAsTemplateOrTemplatePattern().print(Out, Policy);
460
2
    Out << "...";
461
2
    break;
462
463
197k
  case Integral:
464
197k
    printIntegral(*this, Out, Policy, IncludeType);
465
197k
    break;
466
467
19.7k
  case Expression:
468
19.7k
    getAsExpr()->printPretty(Out, nullptr, Policy);
469
19.7k
    break;
470
471
1.26k
  case Pack:
472
1.26k
    Out << "<";
473
1.26k
    bool First = true;
474
2.43k
    for (const auto &P : pack_elements()) {
475
2.43k
      if (First)
476
1.20k
        First = false;
477
1.22k
      else
478
1.22k
        Out << ", ";
479
480
2.43k
      P.print(Policy, Out, IncludeType);
481
2.43k
    }
482
1.26k
    Out << ">";
483
1.26k
    break;
484
1.79M
  }
485
1.79M
}
486
487
0
void TemplateArgument::dump(raw_ostream &Out) const {
488
0
  LangOptions LO; // FIXME! see also TemplateName::dump().
489
0
  LO.CPlusPlus = true;
490
0
  LO.Bool = true;
491
0
  print(PrintingPolicy(LO), Out, /*IncludeType*/ true);
492
0
}
493
494
0
LLVM_DUMP_METHOD void TemplateArgument::dump() const { dump(llvm::errs()); }
495
496
//===----------------------------------------------------------------------===//
497
// TemplateArgumentLoc Implementation
498
//===----------------------------------------------------------------------===//
499
500
342k
SourceRange TemplateArgumentLoc::getSourceRange() const {
501
342k
  switch (Argument.getKind()) {
502
126k
  case TemplateArgument::Expression:
503
126k
    return getSourceExpression()->getSourceRange();
504
505
0
  case TemplateArgument::Declaration:
506
0
    return getSourceDeclExpression()->getSourceRange();
507
508
0
  case TemplateArgument::NullPtr:
509
0
    return getSourceNullPtrExpression()->getSourceRange();
510
511
207k
  case TemplateArgument::Type:
512
207k
    if (TypeSourceInfo *TSI = getTypeSourceInfo())
513
207k
      return TSI->getTypeLoc().getSourceRange();
514
0
    else
515
0
      return SourceRange();
516
517
8.86k
  case TemplateArgument::Template:
518
8.86k
    if (getTemplateQualifierLoc())
519
43
      return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
520
43
                         getTemplateNameLoc());
521
8.82k
    return SourceRange(getTemplateNameLoc());
522
523
2
  case TemplateArgument::TemplateExpansion:
524
2
    if (getTemplateQualifierLoc())
525
1
      return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
526
1
                         getTemplateEllipsisLoc());
527
1
    return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
528
529
0
  case TemplateArgument::Integral:
530
0
    return getSourceIntegralExpression()->getSourceRange();
531
532
0
  case TemplateArgument::Pack:
533
0
  case TemplateArgument::Null:
534
0
    return SourceRange();
535
342k
  }
536
537
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
538
0
}
539
540
template <typename T>
541
1.14k
static const T &DiagTemplateArg(const T &DB, const TemplateArgument &Arg) {
542
1.14k
  switch (Arg.getKind()) {
543
0
  case TemplateArgument::Null:
544
    // This is bad, but not as bad as crashing because of argument
545
    // count mismatches.
546
0
    return DB << "(null template argument)";
547
548
514
  case TemplateArgument::Type:
549
514
    return DB << Arg.getAsType();
550
551
8
  case TemplateArgument::Declaration:
552
8
    return DB << Arg.getAsDecl();
553
554
0
  case TemplateArgument::NullPtr:
555
0
    return DB << "nullptr";
556
557
44
  case TemplateArgument::Integral:
558
44
    return DB << toString(Arg.getAsIntegral(), 10);
559
560
24
  case TemplateArgument::Template:
561
24
    return DB << Arg.getAsTemplate();
562
563
0
  case TemplateArgument::TemplateExpansion:
564
0
    return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
565
566
429
  case TemplateArgument::Expression: {
567
    // This shouldn't actually ever happen, so it's okay that we're
568
    // regurgitating an expression here.
569
    // FIXME: We're guessing at LangOptions!
570
429
    SmallString<32> Str;
571
429
    llvm::raw_svector_ostream OS(Str);
572
429
    LangOptions LangOpts;
573
429
    LangOpts.CPlusPlus = true;
574
429
    PrintingPolicy Policy(LangOpts);
575
429
    Arg.getAsExpr()->printPretty(OS, nullptr, Policy);
576
429
    return DB << OS.str();
577
0
  }
578
579
126
  case TemplateArgument::Pack: {
580
    // FIXME: We're guessing at LangOptions!
581
126
    SmallString<32> Str;
582
126
    llvm::raw_svector_ostream OS(Str);
583
126
    LangOptions LangOpts;
584
126
    LangOpts.CPlusPlus = true;
585
126
    PrintingPolicy Policy(LangOpts);
586
126
    Arg.print(Policy, OS, /*IncludeType*/ true);
587
126
    return DB << OS.str();
588
0
  }
589
1.14k
  }
590
591
0
  llvm_unreachable("Invalid TemplateArgument Kind!");
592
0
}
593
594
const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
595
1.14k
                                             const TemplateArgument &Arg) {
596
1.14k
  return DiagTemplateArg(DB, Arg);
597
1.14k
}
598
599
clang::TemplateArgumentLocInfo::TemplateArgumentLocInfo(
600
    ASTContext &Ctx, NestedNameSpecifierLoc QualifierLoc,
601
79.6k
    SourceLocation TemplateNameLoc, SourceLocation EllipsisLoc) {
602
79.6k
  TemplateTemplateArgLocInfo *Template = new (Ctx) TemplateTemplateArgLocInfo;
603
79.6k
  Template->Qualifier = QualifierLoc.getNestedNameSpecifier();
604
79.6k
  Template->QualifierLocData = QualifierLoc.getOpaqueData();
605
79.6k
  Template->TemplateNameLoc = TemplateNameLoc;
606
79.6k
  Template->EllipsisLoc = EllipsisLoc;
607
79.6k
  Pointer = Template;
608
79.6k
}
609
610
const ASTTemplateArgumentListInfo *
611
ASTTemplateArgumentListInfo::Create(const ASTContext &C,
612
201k
                                    const TemplateArgumentListInfo &List) {
613
201k
  std::size_t size = totalSizeToAlloc<TemplateArgumentLoc>(List.size());
614
201k
  void *Mem = C.Allocate(size, alignof(ASTTemplateArgumentListInfo));
615
201k
  return new (Mem) ASTTemplateArgumentListInfo(List);
616
201k
}
617
618
ASTTemplateArgumentListInfo::ASTTemplateArgumentListInfo(
619
201k
    const TemplateArgumentListInfo &Info) {
620
201k
  LAngleLoc = Info.getLAngleLoc();
621
201k
  RAngleLoc = Info.getRAngleLoc();
622
201k
  NumTemplateArgs = Info.size();
623
624
201k
  TemplateArgumentLoc *ArgBuffer = getTrailingObjects<TemplateArgumentLoc>();
625
629k
  for (unsigned i = 0; i != NumTemplateArgs; 
++i427k
)
626
427k
    new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
627
201k
}
628
629
void ASTTemplateKWAndArgsInfo::initializeFrom(
630
    SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
631
55.8k
    TemplateArgumentLoc *OutArgArray) {
632
55.8k
  this->TemplateKWLoc = TemplateKWLoc;
633
55.8k
  LAngleLoc = Info.getLAngleLoc();
634
55.8k
  RAngleLoc = Info.getRAngleLoc();
635
55.8k
  NumTemplateArgs = Info.size();
636
637
133k
  for (unsigned i = 0; i != NumTemplateArgs; 
++i77.5k
)
638
77.5k
    new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
639
55.8k
}
640
641
307
void ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) {
642
307
  assert(TemplateKWLoc.isValid());
643
0
  LAngleLoc = SourceLocation();
644
307
  RAngleLoc = SourceLocation();
645
307
  this->TemplateKWLoc = TemplateKWLoc;
646
307
  NumTemplateArgs = 0;
647
307
}
648
649
void ASTTemplateKWAndArgsInfo::initializeFrom(
650
    SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
651
585k
    TemplateArgumentLoc *OutArgArray, TemplateArgumentDependence &Deps) {
652
585k
  this->TemplateKWLoc = TemplateKWLoc;
653
585k
  LAngleLoc = Info.getLAngleLoc();
654
585k
  RAngleLoc = Info.getRAngleLoc();
655
585k
  NumTemplateArgs = Info.size();
656
657
1.37M
  for (unsigned i = 0; i != NumTemplateArgs; 
++i788k
) {
658
788k
    Deps |= Info[i].getArgument().getDependence();
659
660
788k
    new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
661
788k
  }
662
585k
}
663
664
void ASTTemplateKWAndArgsInfo::copyInto(const TemplateArgumentLoc *ArgArray,
665
285k
                                        TemplateArgumentListInfo &Info) const {
666
285k
  Info.setLAngleLoc(LAngleLoc);
667
285k
  Info.setRAngleLoc(RAngleLoc);
668
634k
  for (unsigned I = 0; I != NumTemplateArgs; 
++I348k
)
669
348k
    Info.addArgument(ArgArray[I]);
670
285k
}