blob: cb4cbd2f76a120d2f667e84579be56844e8f3654 [file] [log] [blame]
Eugene Zelenko21fadad2017-11-21 23:26:08 +00001//===- TemplateBase.cpp - Common template AST class implementation --------===//
John McCall588d2d52009-10-29 07:48:15 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
John McCall588d2d52009-10-29 07:48:15 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements common classes used throughout C++ template
10// representations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall588d2d52009-10-29 07:48:15 +000014#include "clang/AST/TemplateBase.h"
Douglas Gregor0192c232010-12-20 16:52:59 +000015#include "clang/AST/ASTContext.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000016#include "clang/AST/Decl.h"
John McCall588d2d52009-10-29 07:48:15 +000017#include "clang/AST/DeclBase.h"
Douglas Gregorbd866c22009-11-23 12:52:47 +000018#include "clang/AST/DeclTemplate.h"
John McCall588d2d52009-10-29 07:48:15 +000019#include "clang/AST/Expr.h"
Douglas Gregore8e9dd62011-01-03 17:17:50 +000020#include "clang/AST/ExprCXX.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000021#include "clang/AST/PrettyPrinter.h"
22#include "clang/AST/TemplateName.h"
Chandler Carruth4c4f8de2011-02-19 00:21:00 +000023#include "clang/AST/Type.h"
John McCall0ad16662009-10-29 08:12:44 +000024#include "clang/AST/TypeLoc.h"
Douglas Gregor3626a5c2010-05-08 17:41:32 +000025#include "clang/Basic/Diagnostic.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000026#include "clang/Basic/LLVM.h"
27#include "clang/Basic/LangOptions.h"
28#include "clang/Basic/SourceLocation.h"
29#include "llvm/ADT/APSInt.h"
Douglas Gregor0192c232010-12-20 16:52:59 +000030#include "llvm/ADT/FoldingSet.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000031#include "llvm/ADT/None.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000032#include "llvm/ADT/SmallString.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000033#include "llvm/ADT/StringRef.h"
34#include "llvm/Support/Casting.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000037#include "llvm/Support/raw_ostream.h"
Eugene Zelenko21fadad2017-11-21 23:26:08 +000038#include <cassert>
39#include <cstddef>
40#include <cstdint>
41#include <cstring>
John McCall588d2d52009-10-29 07:48:15 +000042
43using namespace clang;
44
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000045/// Print a template integral argument value.
Chandler Carruth4c4f8de2011-02-19 00:21:00 +000046///
47/// \param TemplArg the TemplateArgument instance to print.
48///
49/// \param Out the raw_ostream instance to use for printing.
Will Wilson67c41ba2014-12-13 04:31:07 +000050///
51/// \param Policy the printing policy for EnumConstantDecl printing.
Chandler Carruth4c4f8de2011-02-19 00:21:00 +000052static void printIntegral(const TemplateArgument &TemplArg,
Will Wilson67c41ba2014-12-13 04:31:07 +000053 raw_ostream &Out, const PrintingPolicy& Policy) {
Eugene Zelenko21fadad2017-11-21 23:26:08 +000054 const Type *T = TemplArg.getIntegralType().getTypePtr();
Benjamin Kramer6003ad52012-06-07 15:09:51 +000055 const llvm::APSInt &Val = TemplArg.getAsIntegral();
Chandler Carruth4c4f8de2011-02-19 00:21:00 +000056
Will Wilson70a15682014-12-13 04:38:19 +000057 if (const EnumType *ET = T->getAs<EnumType>()) {
Will Wilson67c41ba2014-12-13 04:31:07 +000058 for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) {
Richard Trieu94a9ae72015-01-09 00:58:16 +000059 // In Sema::CheckTemplateArugment, enum template arguments value are
60 // extended to the size of the integer underlying the enum type. This
61 // may create a size difference between the enum value and template
62 // argument value, requiring isSameValue here instead of operator==.
63 if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
Will Wilson67c41ba2014-12-13 04:31:07 +000064 ECD->printQualifiedName(Out, Policy);
65 return;
66 }
67 }
68 }
69
Reid Kleckner60103382015-12-16 02:04:40 +000070 if (T->isBooleanType() && !Policy.MSVCFormatting) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +000071 Out << (Val.getBoolValue() ? "true" : "false");
Chandler Carruth4c4f8de2011-02-19 00:21:00 +000072 } else if (T->isCharType()) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +000073 const char Ch = Val.getZExtValue();
Chandler Carruth20d0fee2011-02-25 20:09:13 +000074 Out << ((Ch == '\'') ? "'\\" : "'");
Benjamin Kramer49038022012-02-04 13:45:25 +000075 Out.write_escaped(StringRef(&Ch, 1), /*UseHexEscapes=*/ true);
Chandler Carruth20d0fee2011-02-25 20:09:13 +000076 Out << "'";
Chandler Carruth4c4f8de2011-02-19 00:21:00 +000077 } else {
Benjamin Kramer6003ad52012-06-07 15:09:51 +000078 Out << Val;
Chandler Carruth4c4f8de2011-02-19 00:21:00 +000079 }
80}
81
John McCall588d2d52009-10-29 07:48:15 +000082//===----------------------------------------------------------------------===//
83// TemplateArgument Implementation
84//===----------------------------------------------------------------------===//
85
Benjamin Kramer6003ad52012-06-07 15:09:51 +000086TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value,
Eli Friedman2d1611b2013-08-21 23:05:56 +000087 QualType Type) {
88 Integer.Kind = Integral;
Benjamin Kramer6003ad52012-06-07 15:09:51 +000089 // Copy the APSInt value into our decomposed form.
90 Integer.BitWidth = Value.getBitWidth();
91 Integer.IsUnsigned = Value.isUnsigned();
92 // If the value is large, we have to get additional memory from the ASTContext
Benjamin Kramer5d4cff72012-06-07 15:54:03 +000093 unsigned NumWords = Value.getNumWords();
94 if (NumWords > 1) {
95 void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t));
96 std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t));
Benjamin Kramer6003ad52012-06-07 15:09:51 +000097 Integer.pVal = static_cast<uint64_t *>(Mem);
98 } else {
99 Integer.VAL = Value.getZExtValue();
100 }
101
102 Integer.Type = Type.getAsOpaquePtr();
103}
104
Benjamin Kramercce63472015-08-05 09:40:22 +0000105TemplateArgument
106TemplateArgument::CreatePackCopy(ASTContext &Context,
107 ArrayRef<TemplateArgument> Args) {
108 if (Args.empty())
Eli Friedmanb826a002012-09-26 02:36:12 +0000109 return getEmptyPack();
Benjamin Kramercce63472015-08-05 09:40:22 +0000110
111 return TemplateArgument(Args.copy(Context));
Douglas Gregor74c6d192011-01-11 23:09:57 +0000112}
113
Douglas Gregora6e053e2010-12-15 01:34:56 +0000114bool TemplateArgument::isDependent() const {
115 switch (getKind()) {
116 case Null:
David Blaikie83d382b2011-09-23 05:06:16 +0000117 llvm_unreachable("Should not have a NULL template argument");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000118
119 case Type:
Richard Smithbc2e9712014-10-17 20:56:14 +0000120 return getAsType()->isDependentType() ||
121 isa<PackExpansionType>(getAsType());
Douglas Gregora6e053e2010-12-15 01:34:56 +0000122
123 case Template:
124 return getAsTemplate().isDependent();
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000125
126 case TemplateExpansion:
127 return true;
128
Douglas Gregora6e053e2010-12-15 01:34:56 +0000129 case Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +0000130 if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
131 return DC->isDependentContext();
132 return getAsDecl()->getDeclContext()->isDependentContext();
133
134 case NullPtr:
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000135 return false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000136
137 case Integral:
138 // Never dependent
139 return false;
140
141 case Expression:
Richard Smithbc2e9712014-10-17 20:56:14 +0000142 return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent() ||
143 isa<PackExpansionExpr>(getAsExpr()));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000144
145 case Pack:
Aaron Ballman2a89e852014-07-15 21:32:31 +0000146 for (const auto &P : pack_elements())
147 if (P.isDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000148 return true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000149 return false;
150 }
151
David Blaikiee4d798f2012-01-20 21:50:17 +0000152 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000153}
154
Douglas Gregor678d76c2011-07-01 01:22:09 +0000155bool TemplateArgument::isInstantiationDependent() const {
156 switch (getKind()) {
157 case Null:
David Blaikie83d382b2011-09-23 05:06:16 +0000158 llvm_unreachable("Should not have a NULL template argument");
Fangrui Song6907ce22018-07-30 19:24:48 +0000159
Douglas Gregor678d76c2011-07-01 01:22:09 +0000160 case Type:
161 return getAsType()->isInstantiationDependentType();
Fangrui Song6907ce22018-07-30 19:24:48 +0000162
Douglas Gregor678d76c2011-07-01 01:22:09 +0000163 case Template:
164 return getAsTemplate().isInstantiationDependent();
Fangrui Song6907ce22018-07-30 19:24:48 +0000165
Douglas Gregor678d76c2011-07-01 01:22:09 +0000166 case TemplateExpansion:
167 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000168
Douglas Gregor678d76c2011-07-01 01:22:09 +0000169 case Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +0000170 if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
171 return DC->isDependentContext();
172 return getAsDecl()->getDeclContext()->isDependentContext();
173
174 case NullPtr:
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000175 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000176
Douglas Gregor678d76c2011-07-01 01:22:09 +0000177 case Integral:
178 // Never dependent
179 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000180
Douglas Gregor678d76c2011-07-01 01:22:09 +0000181 case Expression:
182 return getAsExpr()->isInstantiationDependent();
Fangrui Song6907ce22018-07-30 19:24:48 +0000183
Douglas Gregor678d76c2011-07-01 01:22:09 +0000184 case Pack:
Aaron Ballman2a89e852014-07-15 21:32:31 +0000185 for (const auto &P : pack_elements())
186 if (P.isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000187 return true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000188 return false;
189 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000190
191 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregor678d76c2011-07-01 01:22:09 +0000192}
193
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000194bool TemplateArgument::isPackExpansion() const {
195 switch (getKind()) {
196 case Null:
197 case Declaration:
198 case Integral:
Fangrui Song6907ce22018-07-30 19:24:48 +0000199 case Pack:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000200 case Template:
Eli Friedmanb826a002012-09-26 02:36:12 +0000201 case NullPtr:
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000202 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000203
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000204 case TemplateExpansion:
205 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000206
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000207 case Type:
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000208 return isa<PackExpansionType>(getAsType());
Fangrui Song6907ce22018-07-30 19:24:48 +0000209
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000210 case Expression:
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000211 return isa<PackExpansionExpr>(getAsExpr());
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000212 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000213
214 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000215}
216
Douglas Gregor506bd562010-12-13 22:49:22 +0000217bool TemplateArgument::containsUnexpandedParameterPack() const {
218 switch (getKind()) {
219 case Null:
220 case Declaration:
221 case Integral:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000222 case TemplateExpansion:
Eli Friedmanb826a002012-09-26 02:36:12 +0000223 case NullPtr:
Douglas Gregor506bd562010-12-13 22:49:22 +0000224 break;
225
226 case Type:
227 if (getAsType()->containsUnexpandedParameterPack())
228 return true;
229 break;
230
231 case Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000232 if (getAsTemplate().containsUnexpandedParameterPack())
Douglas Gregor506bd562010-12-13 22:49:22 +0000233 return true;
234 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000235
Douglas Gregor506bd562010-12-13 22:49:22 +0000236 case Expression:
237 if (getAsExpr()->containsUnexpandedParameterPack())
238 return true;
239 break;
240
241 case Pack:
Aaron Ballman2a89e852014-07-15 21:32:31 +0000242 for (const auto &P : pack_elements())
243 if (P.containsUnexpandedParameterPack())
Douglas Gregor506bd562010-12-13 22:49:22 +0000244 return true;
245
246 break;
247 }
248
249 return false;
250}
251
David Blaikie05785d12013-02-20 22:23:23 +0000252Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
Eli Friedman2d1611b2013-08-21 23:05:56 +0000253 assert(getKind() == TemplateExpansion);
Douglas Gregore1d60df2011-01-14 23:41:42 +0000254 if (TemplateArg.NumExpansions)
255 return TemplateArg.NumExpansions - 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000256
257 return None;
Douglas Gregore1d60df2011-01-14 23:41:42 +0000258}
259
Richard Smith593d6a12016-12-23 01:30:39 +0000260QualType TemplateArgument::getNonTypeTemplateArgumentType() const {
261 switch (getKind()) {
262 case TemplateArgument::Null:
263 case TemplateArgument::Type:
264 case TemplateArgument::Template:
265 case TemplateArgument::TemplateExpansion:
266 case TemplateArgument::Pack:
267 return QualType();
268
269 case TemplateArgument::Integral:
270 return getIntegralType();
271
272 case TemplateArgument::Expression:
273 return getAsExpr()->getType();
274
275 case TemplateArgument::Declaration:
276 return getParamTypeForDecl();
277
278 case TemplateArgument::NullPtr:
279 return getNullPtrType();
280 }
281
282 llvm_unreachable("Invalid TemplateArgument Kind!");
283}
284
John McCall588d2d52009-10-29 07:48:15 +0000285void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad39c79802011-01-12 09:06:06 +0000286 const ASTContext &Context) const {
Eli Friedman2d1611b2013-08-21 23:05:56 +0000287 ID.AddInteger(getKind());
288 switch (getKind()) {
John McCall588d2d52009-10-29 07:48:15 +0000289 case Null:
290 break;
291
292 case Type:
293 getAsType().Profile(ID);
294 break;
295
Eli Friedman2d1611b2013-08-21 23:05:56 +0000296 case NullPtr:
297 getNullPtrType().Profile(ID);
298 break;
299
John McCall588d2d52009-10-29 07:48:15 +0000300 case Declaration:
Craig Topper36250ad2014-05-12 05:36:57 +0000301 ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : nullptr);
John McCall588d2d52009-10-29 07:48:15 +0000302 break;
303
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000304 case Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000305 case TemplateExpansion: {
306 TemplateName Template = getAsTemplateOrTemplatePattern();
Douglas Gregorbd866c22009-11-23 12:52:47 +0000307 if (TemplateTemplateParmDecl *TTP
308 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000309 Template.getAsTemplateDecl())) {
Douglas Gregorbd866c22009-11-23 12:52:47 +0000310 ID.AddBoolean(true);
311 ID.AddInteger(TTP->getDepth());
312 ID.AddInteger(TTP->getPosition());
Douglas Gregoreb29d182011-01-05 17:40:24 +0000313 ID.AddBoolean(TTP->isParameterPack());
Douglas Gregorbd866c22009-11-23 12:52:47 +0000314 } else {
315 ID.AddBoolean(false);
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000316 ID.AddPointer(Context.getCanonicalTemplateName(Template)
317 .getAsVoidPointer());
Douglas Gregorbd866c22009-11-23 12:52:47 +0000318 }
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000319 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000320 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000321
John McCall588d2d52009-10-29 07:48:15 +0000322 case Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +0000323 getAsIntegral().Profile(ID);
John McCall588d2d52009-10-29 07:48:15 +0000324 getIntegralType().Profile(ID);
325 break;
326
327 case Expression:
328 getAsExpr()->Profile(ID, Context, true);
329 break;
330
331 case Pack:
332 ID.AddInteger(Args.NumArgs);
333 for (unsigned I = 0; I != Args.NumArgs; ++I)
334 Args.Args[I].Profile(ID, Context);
335 }
336}
John McCall0ad16662009-10-29 08:12:44 +0000337
John McCallc392f372010-06-11 00:33:02 +0000338bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
339 if (getKind() != Other.getKind()) return false;
340
341 switch (getKind()) {
342 case Null:
343 case Type:
Fangrui Song6907ce22018-07-30 19:24:48 +0000344 case Expression:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000345 case Template:
346 case TemplateExpansion:
Eli Friedmanb826a002012-09-26 02:36:12 +0000347 case NullPtr:
Eli Friedman2d1611b2013-08-21 23:05:56 +0000348 return TypeOrValue.V == Other.TypeOrValue.V;
John McCallc392f372010-06-11 00:33:02 +0000349
Eli Friedmanb826a002012-09-26 02:36:12 +0000350 case Declaration:
David Blaikie0f62c8d2014-10-16 04:21:25 +0000351 return getAsDecl() == Other.getAsDecl();
Eli Friedmanb826a002012-09-26 02:36:12 +0000352
John McCallc392f372010-06-11 00:33:02 +0000353 case Integral:
354 return getIntegralType() == Other.getIntegralType() &&
Benjamin Kramer6003ad52012-06-07 15:09:51 +0000355 getAsIntegral() == Other.getAsIntegral();
John McCallc392f372010-06-11 00:33:02 +0000356
357 case Pack:
358 if (Args.NumArgs != Other.Args.NumArgs) return false;
359 for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
360 if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
361 return false;
362 return true;
363 }
364
David Blaikiee4d798f2012-01-20 21:50:17 +0000365 llvm_unreachable("Invalid TemplateArgument Kind!");
John McCallc392f372010-06-11 00:33:02 +0000366}
367
Douglas Gregor0f3feb42010-12-22 21:19:48 +0000368TemplateArgument TemplateArgument::getPackExpansionPattern() const {
369 assert(isPackExpansion());
Fangrui Song6907ce22018-07-30 19:24:48 +0000370
Douglas Gregor0f3feb42010-12-22 21:19:48 +0000371 switch (getKind()) {
Douglas Gregoreb29d182011-01-05 17:40:24 +0000372 case Type:
373 return getAsType()->getAs<PackExpansionType>()->getPattern();
Fangrui Song6907ce22018-07-30 19:24:48 +0000374
Douglas Gregoreb29d182011-01-05 17:40:24 +0000375 case Expression:
376 return cast<PackExpansionExpr>(getAsExpr())->getPattern();
Fangrui Song6907ce22018-07-30 19:24:48 +0000377
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000378 case TemplateExpansion:
Douglas Gregore1d60df2011-01-14 23:41:42 +0000379 return TemplateArgument(getAsTemplateOrTemplatePattern());
Eli Friedmanb826a002012-09-26 02:36:12 +0000380
Douglas Gregoreb29d182011-01-05 17:40:24 +0000381 case Declaration:
382 case Integral:
383 case Pack:
384 case Null:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000385 case Template:
Eli Friedmanb826a002012-09-26 02:36:12 +0000386 case NullPtr:
Douglas Gregoreb29d182011-01-05 17:40:24 +0000387 return TemplateArgument();
Douglas Gregor0f3feb42010-12-22 21:19:48 +0000388 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000389
390 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregor0f3feb42010-12-22 21:19:48 +0000391}
392
Fangrui Song6907ce22018-07-30 19:24:48 +0000393void TemplateArgument::print(const PrintingPolicy &Policy,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000394 raw_ostream &Out) const {
Douglas Gregor0192c232010-12-20 16:52:59 +0000395 switch (getKind()) {
396 case Null:
David Blaikieabe1a392014-04-02 05:58:29 +0000397 Out << "(no value)";
Douglas Gregor0192c232010-12-20 16:52:59 +0000398 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000399
Douglas Gregor0192c232010-12-20 16:52:59 +0000400 case Type: {
Douglas Gregore46db902011-06-17 22:11:49 +0000401 PrintingPolicy SubPolicy(Policy);
402 SubPolicy.SuppressStrongLifetime = true;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000403 getAsType().print(Out, SubPolicy);
Douglas Gregor0192c232010-12-20 16:52:59 +0000404 break;
405 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000406
Douglas Gregor0192c232010-12-20 16:52:59 +0000407 case Declaration: {
George Burgess IV00f70bd2018-03-01 05:43:23 +0000408 NamedDecl *ND = getAsDecl();
David Blaikieb8f6a8a2013-05-09 22:43:45 +0000409 Out << '&';
Eli Friedmanb826a002012-09-26 02:36:12 +0000410 if (ND->getDeclName()) {
411 // FIXME: distinguish between pointer and reference args?
David Blaikieb8f6a8a2013-05-09 22:43:45 +0000412 ND->printQualifiedName(Out);
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000413 } else {
David Blaikieabe1a392014-04-02 05:58:29 +0000414 Out << "(anonymous)";
Douglas Gregor0192c232010-12-20 16:52:59 +0000415 }
416 break;
417 }
Eli Friedmanb826a002012-09-26 02:36:12 +0000418
Abramo Bagnara49107012012-10-05 04:43:29 +0000419 case NullPtr:
Eli Friedmanb826a002012-09-26 02:36:12 +0000420 Out << "nullptr";
Abramo Bagnara49107012012-10-05 04:43:29 +0000421 break;
Eli Friedmanb826a002012-09-26 02:36:12 +0000422
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000423 case Template:
Douglas Gregor0192c232010-12-20 16:52:59 +0000424 getAsTemplate().print(Out, Policy);
425 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000426
427 case TemplateExpansion:
428 getAsTemplateOrTemplatePattern().print(Out, Policy);
429 Out << "...";
430 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000431
Eugene Zelenko21fadad2017-11-21 23:26:08 +0000432 case Integral:
Will Wilson67c41ba2014-12-13 04:31:07 +0000433 printIntegral(*this, Out, Policy);
Douglas Gregor0192c232010-12-20 16:52:59 +0000434 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000435
Douglas Gregoreb29d182011-01-05 17:40:24 +0000436 case Expression:
Craig Topper36250ad2014-05-12 05:36:57 +0000437 getAsExpr()->printPretty(Out, nullptr, Policy);
Douglas Gregor0192c232010-12-20 16:52:59 +0000438 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000439
Douglas Gregor0192c232010-12-20 16:52:59 +0000440 case Pack:
441 Out << "<";
442 bool First = true;
Aaron Ballman2a89e852014-07-15 21:32:31 +0000443 for (const auto &P : pack_elements()) {
Douglas Gregor0192c232010-12-20 16:52:59 +0000444 if (First)
445 First = false;
446 else
447 Out << ", ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000448
Aaron Ballman2a89e852014-07-15 21:32:31 +0000449 P.print(Policy, Out);
Douglas Gregor0192c232010-12-20 16:52:59 +0000450 }
451 Out << ">";
Fangrui Song6907ce22018-07-30 19:24:48 +0000452 break;
Douglas Gregor0192c232010-12-20 16:52:59 +0000453 }
454}
455
Yaron Kerenc6d8a572016-01-29 13:46:15 +0000456void TemplateArgument::dump(raw_ostream &Out) const {
457 LangOptions LO; // FIXME! see also TemplateName::dump().
458 LO.CPlusPlus = true;
459 LO.Bool = true;
460 print(PrintingPolicy(LO), Out);
461}
462
Yaron Kerencdae9412016-01-29 19:38:18 +0000463LLVM_DUMP_METHOD void TemplateArgument::dump() const { dump(llvm::errs()); }
Yaron Kerenc6d8a572016-01-29 13:46:15 +0000464
John McCall0ad16662009-10-29 08:12:44 +0000465//===----------------------------------------------------------------------===//
466// TemplateArgumentLoc Implementation
467//===----------------------------------------------------------------------===//
468
John McCall0d07eb32009-10-29 18:45:58 +0000469SourceRange TemplateArgumentLoc::getSourceRange() const {
John McCall0ad16662009-10-29 08:12:44 +0000470 switch (Argument.getKind()) {
471 case TemplateArgument::Expression:
John McCall0d07eb32009-10-29 18:45:58 +0000472 return getSourceExpression()->getSourceRange();
Zhanyong Wan18ca8bf2010-09-03 23:50:56 +0000473
John McCall0ad16662009-10-29 08:12:44 +0000474 case TemplateArgument::Declaration:
John McCall0d07eb32009-10-29 18:45:58 +0000475 return getSourceDeclExpression()->getSourceRange();
Zhanyong Wan18ca8bf2010-09-03 23:50:56 +0000476
Eli Friedmanb826a002012-09-26 02:36:12 +0000477 case TemplateArgument::NullPtr:
478 return getSourceNullPtrExpression()->getSourceRange();
479
John McCall0d07eb32009-10-29 18:45:58 +0000480 case TemplateArgument::Type:
Zhanyong Wan18ca8bf2010-09-03 23:50:56 +0000481 if (TypeSourceInfo *TSI = getTypeSourceInfo())
482 return TSI->getTypeLoc().getSourceRange();
483 else
484 return SourceRange();
485
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000486 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +0000487 if (getTemplateQualifierLoc())
Fangrui Song6907ce22018-07-30 19:24:48 +0000488 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000489 getTemplateNameLoc());
490 return SourceRange(getTemplateNameLoc());
491
492 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +0000493 if (getTemplateQualifierLoc())
Fangrui Song6907ce22018-07-30 19:24:48 +0000494 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000495 getTemplateEllipsisLoc());
496 return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
497
John McCall0ad16662009-10-29 08:12:44 +0000498 case TemplateArgument::Integral:
Eli Friedmanb826a002012-09-26 02:36:12 +0000499 return getSourceIntegralExpression()->getSourceRange();
500
John McCall0ad16662009-10-29 08:12:44 +0000501 case TemplateArgument::Pack:
502 case TemplateArgument::Null:
John McCall0d07eb32009-10-29 18:45:58 +0000503 return SourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000504 }
505
David Blaikiee4d798f2012-01-20 21:50:17 +0000506 llvm_unreachable("Invalid TemplateArgument Kind!");
John McCall0ad16662009-10-29 08:12:44 +0000507}
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000508
509const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
510 const TemplateArgument &Arg) {
511 switch (Arg.getKind()) {
512 case TemplateArgument::Null:
John McCall3b3a5ed2010-08-05 04:58:04 +0000513 // This is bad, but not as bad as crashing because of argument
514 // count mismatches.
515 return DB << "(null template argument)";
Fangrui Song6907ce22018-07-30 19:24:48 +0000516
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000517 case TemplateArgument::Type:
518 return DB << Arg.getAsType();
Fangrui Song6907ce22018-07-30 19:24:48 +0000519
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000520 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +0000521 return DB << Arg.getAsDecl();
522
523 case TemplateArgument::NullPtr:
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000524 return DB << "nullptr";
Fangrui Song6907ce22018-07-30 19:24:48 +0000525
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000526 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +0000527 return DB << Arg.getAsIntegral().toString(10);
Fangrui Song6907ce22018-07-30 19:24:48 +0000528
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000529 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000530 return DB << Arg.getAsTemplate();
531
532 case TemplateArgument::TemplateExpansion:
533 return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
534
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000535 case TemplateArgument::Expression: {
536 // This shouldn't actually ever happen, so it's okay that we're
537 // regurgitating an expression here.
538 // FIXME: We're guessing at LangOptions!
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000539 SmallString<32> Str;
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000540 llvm::raw_svector_ostream OS(Str);
541 LangOptions LangOpts;
542 LangOpts.CPlusPlus = true;
543 PrintingPolicy Policy(LangOpts);
Craig Topper36250ad2014-05-12 05:36:57 +0000544 Arg.getAsExpr()->printPretty(OS, nullptr, Policy);
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000545 return DB << OS.str();
546 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000547
Douglas Gregor0192c232010-12-20 16:52:59 +0000548 case TemplateArgument::Pack: {
549 // FIXME: We're guessing at LangOptions!
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000550 SmallString<32> Str;
Douglas Gregor0192c232010-12-20 16:52:59 +0000551 llvm::raw_svector_ostream OS(Str);
552 LangOptions LangOpts;
553 LangOpts.CPlusPlus = true;
554 PrintingPolicy Policy(LangOpts);
555 Arg.print(Policy, OS);
556 return DB << OS.str();
557 }
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000558 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000559
560 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregor3626a5c2010-05-08 17:41:32 +0000561}
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000562
563const ASTTemplateArgumentListInfo *
564ASTTemplateArgumentListInfo::Create(ASTContext &C,
565 const TemplateArgumentListInfo &List) {
James Y Knight04ec5bf2015-12-24 02:59:37 +0000566 std::size_t size = totalSizeToAlloc<TemplateArgumentLoc>(List.size());
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000567 void *Mem = C.Allocate(size, alignof(ASTTemplateArgumentListInfo));
James Y Knight04ec5bf2015-12-24 02:59:37 +0000568 return new (Mem) ASTTemplateArgumentListInfo(List);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000569}
570
James Y Knight04ec5bf2015-12-24 02:59:37 +0000571ASTTemplateArgumentListInfo::ASTTemplateArgumentListInfo(
572 const TemplateArgumentListInfo &Info) {
573 LAngleLoc = Info.getLAngleLoc();
574 RAngleLoc = Info.getRAngleLoc();
575 NumTemplateArgs = Info.size();
576
577 TemplateArgumentLoc *ArgBuffer = getTrailingObjects<TemplateArgumentLoc>();
578 for (unsigned i = 0; i != NumTemplateArgs; ++i)
579 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
580}
581
582void ASTTemplateKWAndArgsInfo::initializeFrom(
James Y Knighte7d82282015-12-29 18:15:14 +0000583 SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
584 TemplateArgumentLoc *OutArgArray) {
James Y Knight04ec5bf2015-12-24 02:59:37 +0000585 this->TemplateKWLoc = TemplateKWLoc;
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000586 LAngleLoc = Info.getLAngleLoc();
587 RAngleLoc = Info.getRAngleLoc();
588 NumTemplateArgs = Info.size();
589
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000590 for (unsigned i = 0; i != NumTemplateArgs; ++i)
James Y Knighte7d82282015-12-29 18:15:14 +0000591 new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000592}
593
James Y Knight04ec5bf2015-12-24 02:59:37 +0000594void ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) {
595 assert(TemplateKWLoc.isValid());
596 LAngleLoc = SourceLocation();
597 RAngleLoc = SourceLocation();
598 this->TemplateKWLoc = TemplateKWLoc;
599 NumTemplateArgs = 0;
600}
601
602void ASTTemplateKWAndArgsInfo::initializeFrom(
603 SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
James Y Knighte7d82282015-12-29 18:15:14 +0000604 TemplateArgumentLoc *OutArgArray, bool &Dependent,
605 bool &InstantiationDependent, bool &ContainsUnexpandedParameterPack) {
James Y Knight04ec5bf2015-12-24 02:59:37 +0000606 this->TemplateKWLoc = TemplateKWLoc;
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000607 LAngleLoc = Info.getLAngleLoc();
608 RAngleLoc = Info.getRAngleLoc();
609 NumTemplateArgs = Info.size();
610
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000611 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
612 Dependent = Dependent || Info[i].getArgument().isDependent();
James Y Knight04ec5bf2015-12-24 02:59:37 +0000613 InstantiationDependent = InstantiationDependent ||
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000614 Info[i].getArgument().isInstantiationDependent();
James Y Knight04ec5bf2015-12-24 02:59:37 +0000615 ContainsUnexpandedParameterPack =
616 ContainsUnexpandedParameterPack ||
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000617 Info[i].getArgument().containsUnexpandedParameterPack();
618
James Y Knighte7d82282015-12-29 18:15:14 +0000619 new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000620 }
621}
622
James Y Knighte7d82282015-12-29 18:15:14 +0000623void ASTTemplateKWAndArgsInfo::copyInto(const TemplateArgumentLoc *ArgArray,
624 TemplateArgumentListInfo &Info) const {
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000625 Info.setLAngleLoc(LAngleLoc);
626 Info.setRAngleLoc(RAngleLoc);
627 for (unsigned I = 0; I != NumTemplateArgs; ++I)
James Y Knighte7d82282015-12-29 18:15:14 +0000628 Info.addArgument(ArgArray[I]);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000629}