blob: b963cec42c5bf3feb9a21b762d8579866b931941 [file] [log] [blame]
John McCall275c10a2009-10-29 07:48:15 +00001//===--- TemplateBase.cpp - Common template AST class implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements common classes used throughout C++ template
11// representations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall275c10a2009-10-29 07:48:15 +000015#include "clang/AST/TemplateBase.h"
Douglas Gregor87dd6972010-12-20 16:52:59 +000016#include "clang/AST/ASTContext.h"
John McCall275c10a2009-10-29 07:48:15 +000017#include "clang/AST/DeclBase.h"
Douglas Gregor74295b32009-11-23 12:52:47 +000018#include "clang/AST/DeclTemplate.h"
John McCall275c10a2009-10-29 07:48:15 +000019#include "clang/AST/Expr.h"
Douglas Gregorbe230c32011-01-03 17:17:50 +000020#include "clang/AST/ExprCXX.h"
Chandler Carruth781701c2011-02-19 00:21:00 +000021#include "clang/AST/Type.h"
John McCall833ca992009-10-29 08:12:44 +000022#include "clang/AST/TypeLoc.h"
Douglas Gregora9333192010-05-08 17:41:32 +000023#include "clang/Basic/Diagnostic.h"
Douglas Gregor87dd6972010-12-20 16:52:59 +000024#include "llvm/ADT/FoldingSet.h"
Douglas Gregor203e6a32011-01-11 23:09:57 +000025#include <algorithm>
Chandler Carruth781701c2011-02-19 00:21:00 +000026#include <cctype>
John McCall275c10a2009-10-29 07:48:15 +000027
28using namespace clang;
29
Chandler Carruth781701c2011-02-19 00:21:00 +000030/// \brief Print a template integral argument value.
31///
32/// \param TemplArg the TemplateArgument instance to print.
33///
34/// \param Out the raw_ostream instance to use for printing.
35static void printIntegral(const TemplateArgument &TemplArg,
Chris Lattner5f9e2722011-07-23 10:55:15 +000036 raw_ostream &Out) {
Chandler Carruth781701c2011-02-19 00:21:00 +000037 const ::clang::Type *T = TemplArg.getIntegralType().getTypePtr();
38 const llvm::APSInt *Val = TemplArg.getAsIntegral();
39
40 if (T->isBooleanType()) {
41 Out << (Val->getBoolValue() ? "true" : "false");
42 } else if (T->isCharType()) {
Chandler Carruth774e2b42011-02-25 20:09:13 +000043 const unsigned char Ch = Val->getZExtValue();
44 const std::string Str(1, Ch);
45 Out << ((Ch == '\'') ? "'\\" : "'");
46 Out.write_escaped(Str, /*UseHexEscapes=*/ true);
47 Out << "'";
Chandler Carruth781701c2011-02-19 00:21:00 +000048 } else {
49 Out << Val->toString(10);
50 }
51}
52
John McCall275c10a2009-10-29 07:48:15 +000053//===----------------------------------------------------------------------===//
54// TemplateArgument Implementation
55//===----------------------------------------------------------------------===//
56
Douglas Gregor203e6a32011-01-11 23:09:57 +000057TemplateArgument TemplateArgument::CreatePackCopy(ASTContext &Context,
58 const TemplateArgument *Args,
59 unsigned NumArgs) {
60 if (NumArgs == 0)
61 return TemplateArgument(0, 0);
62
63 TemplateArgument *Storage = new (Context) TemplateArgument [NumArgs];
64 std::copy(Args, Args + NumArgs, Storage);
65 return TemplateArgument(Storage, NumArgs);
66}
67
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000068bool TemplateArgument::isDependent() const {
69 switch (getKind()) {
70 case Null:
71 assert(false && "Should not have a NULL template argument");
72 return false;
73
74 case Type:
75 return getAsType()->isDependentType();
76
77 case Template:
78 return getAsTemplate().isDependent();
Douglas Gregora7fc9012011-01-05 18:58:31 +000079
80 case TemplateExpansion:
81 return true;
82
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000083 case Declaration:
84 if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
85 return DC->isDependentContext();
86 return getAsDecl()->getDeclContext()->isDependentContext();
87
88 case Integral:
89 // Never dependent
90 return false;
91
92 case Expression:
93 return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent());
94
95 case Pack:
96 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) {
97 if (P->isDependent())
98 return true;
99 }
100
101 return false;
102 }
103
104 return false;
105}
106
Douglas Gregor561f8122011-07-01 01:22:09 +0000107bool TemplateArgument::isInstantiationDependent() const {
108 switch (getKind()) {
109 case Null:
110 assert(false && "Should not have a NULL template argument");
111 return false;
112
113 case Type:
114 return getAsType()->isInstantiationDependentType();
115
116 case Template:
117 return getAsTemplate().isInstantiationDependent();
118
119 case TemplateExpansion:
120 return true;
121
122 case Declaration:
123 if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
124 return DC->isDependentContext();
125 return getAsDecl()->getDeclContext()->isDependentContext();
126
127 case Integral:
128 // Never dependent
129 return false;
130
131 case Expression:
132 return getAsExpr()->isInstantiationDependent();
133
134 case Pack:
135 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) {
136 if (P->isInstantiationDependent())
137 return true;
138 }
139
140 return false;
141 }
142
143 return false;
144}
145
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000146bool TemplateArgument::isPackExpansion() const {
147 switch (getKind()) {
148 case Null:
149 case Declaration:
150 case Integral:
151 case Pack:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000152 case Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000153 return false;
154
Douglas Gregora7fc9012011-01-05 18:58:31 +0000155 case TemplateExpansion:
156 return true;
157
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000158 case Type:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000159 return isa<PackExpansionType>(getAsType());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000160
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000161 case Expression:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000162 return isa<PackExpansionExpr>(getAsExpr());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000163 }
164
165 return false;
166}
167
Douglas Gregord0937222010-12-13 22:49:22 +0000168bool TemplateArgument::containsUnexpandedParameterPack() const {
169 switch (getKind()) {
170 case Null:
171 case Declaration:
172 case Integral:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000173 case TemplateExpansion:
Douglas Gregord0937222010-12-13 22:49:22 +0000174 break;
175
176 case Type:
177 if (getAsType()->containsUnexpandedParameterPack())
178 return true;
179 break;
180
181 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000182 if (getAsTemplate().containsUnexpandedParameterPack())
Douglas Gregord0937222010-12-13 22:49:22 +0000183 return true;
184 break;
185
186 case Expression:
187 if (getAsExpr()->containsUnexpandedParameterPack())
188 return true;
189 break;
190
191 case Pack:
192 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P)
193 if (P->containsUnexpandedParameterPack())
194 return true;
195
196 break;
197 }
198
199 return false;
200}
201
Douglas Gregor2be29f42011-01-14 23:41:42 +0000202llvm::Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
203 assert(Kind == TemplateExpansion);
204 if (TemplateArg.NumExpansions)
205 return TemplateArg.NumExpansions - 1;
206
207 return llvm::Optional<unsigned>();
208}
209
John McCall275c10a2009-10-29 07:48:15 +0000210void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +0000211 const ASTContext &Context) const {
John McCall275c10a2009-10-29 07:48:15 +0000212 ID.AddInteger(Kind);
213 switch (Kind) {
214 case Null:
215 break;
216
217 case Type:
218 getAsType().Profile(ID);
219 break;
220
221 case Declaration:
222 ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0);
223 break;
224
Douglas Gregor788cd062009-11-11 01:00:40 +0000225 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000226 case TemplateExpansion: {
227 TemplateName Template = getAsTemplateOrTemplatePattern();
Douglas Gregor74295b32009-11-23 12:52:47 +0000228 if (TemplateTemplateParmDecl *TTP
229 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Douglas Gregora7fc9012011-01-05 18:58:31 +0000230 Template.getAsTemplateDecl())) {
Douglas Gregor74295b32009-11-23 12:52:47 +0000231 ID.AddBoolean(true);
232 ID.AddInteger(TTP->getDepth());
233 ID.AddInteger(TTP->getPosition());
Douglas Gregorba68eca2011-01-05 17:40:24 +0000234 ID.AddBoolean(TTP->isParameterPack());
Douglas Gregor74295b32009-11-23 12:52:47 +0000235 } else {
236 ID.AddBoolean(false);
Douglas Gregora7fc9012011-01-05 18:58:31 +0000237 ID.AddPointer(Context.getCanonicalTemplateName(Template)
238 .getAsVoidPointer());
Douglas Gregor74295b32009-11-23 12:52:47 +0000239 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000240 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000241 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000242
John McCall275c10a2009-10-29 07:48:15 +0000243 case Integral:
244 getAsIntegral()->Profile(ID);
245 getIntegralType().Profile(ID);
246 break;
247
248 case Expression:
249 getAsExpr()->Profile(ID, Context, true);
250 break;
251
252 case Pack:
253 ID.AddInteger(Args.NumArgs);
254 for (unsigned I = 0; I != Args.NumArgs; ++I)
255 Args.Args[I].Profile(ID, Context);
256 }
257}
John McCall833ca992009-10-29 08:12:44 +0000258
John McCall33500952010-06-11 00:33:02 +0000259bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
260 if (getKind() != Other.getKind()) return false;
261
262 switch (getKind()) {
263 case Null:
264 case Type:
265 case Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000266 case Expression:
267 case Template:
268 case TemplateExpansion:
John McCall33500952010-06-11 00:33:02 +0000269 return TypeOrValue == Other.TypeOrValue;
270
271 case Integral:
272 return getIntegralType() == Other.getIntegralType() &&
273 *getAsIntegral() == *Other.getAsIntegral();
274
275 case Pack:
276 if (Args.NumArgs != Other.Args.NumArgs) return false;
277 for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
278 if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
279 return false;
280 return true;
281 }
282
283 // Suppress warnings.
284 return false;
285}
286
Douglas Gregore02e2622010-12-22 21:19:48 +0000287TemplateArgument TemplateArgument::getPackExpansionPattern() const {
288 assert(isPackExpansion());
289
290 switch (getKind()) {
Douglas Gregorba68eca2011-01-05 17:40:24 +0000291 case Type:
292 return getAsType()->getAs<PackExpansionType>()->getPattern();
293
294 case Expression:
295 return cast<PackExpansionExpr>(getAsExpr())->getPattern();
296
Douglas Gregora7fc9012011-01-05 18:58:31 +0000297 case TemplateExpansion:
Douglas Gregor2be29f42011-01-14 23:41:42 +0000298 return TemplateArgument(getAsTemplateOrTemplatePattern());
Douglas Gregorba68eca2011-01-05 17:40:24 +0000299
300 case Declaration:
301 case Integral:
302 case Pack:
303 case Null:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000304 case Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000305 return TemplateArgument();
Douglas Gregore02e2622010-12-22 21:19:48 +0000306 }
307
308 return TemplateArgument();
309}
310
Douglas Gregor87dd6972010-12-20 16:52:59 +0000311void TemplateArgument::print(const PrintingPolicy &Policy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000312 raw_ostream &Out) const {
Douglas Gregor87dd6972010-12-20 16:52:59 +0000313 switch (getKind()) {
314 case Null:
315 Out << "<no value>";
316 break;
317
318 case Type: {
Douglas Gregore559ca12011-06-17 22:11:49 +0000319 PrintingPolicy SubPolicy(Policy);
320 SubPolicy.SuppressStrongLifetime = true;
Douglas Gregor87dd6972010-12-20 16:52:59 +0000321 std::string TypeStr;
Douglas Gregore559ca12011-06-17 22:11:49 +0000322 getAsType().getAsStringInternal(TypeStr, SubPolicy);
Douglas Gregor87dd6972010-12-20 16:52:59 +0000323 Out << TypeStr;
324 break;
325 }
326
327 case Declaration: {
328 bool Unnamed = true;
329 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getAsDecl())) {
330 if (ND->getDeclName()) {
331 Unnamed = false;
332 Out << ND->getNameAsString();
333 }
334 }
335
336 if (Unnamed) {
337 Out << "<anonymous>";
338 }
339 break;
340 }
341
Douglas Gregora7fc9012011-01-05 18:58:31 +0000342 case Template:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000343 getAsTemplate().print(Out, Policy);
344 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000345
346 case TemplateExpansion:
347 getAsTemplateOrTemplatePattern().print(Out, Policy);
348 Out << "...";
349 break;
350
Douglas Gregor87dd6972010-12-20 16:52:59 +0000351 case Integral: {
Chandler Carruth781701c2011-02-19 00:21:00 +0000352 printIntegral(*this, Out);
Douglas Gregor87dd6972010-12-20 16:52:59 +0000353 break;
354 }
355
Douglas Gregorba68eca2011-01-05 17:40:24 +0000356 case Expression:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000357 getAsExpr()->printPretty(Out, 0, Policy);
358 break;
Douglas Gregor87dd6972010-12-20 16:52:59 +0000359
360 case Pack:
361 Out << "<";
362 bool First = true;
363 for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end();
364 P != PEnd; ++P) {
365 if (First)
366 First = false;
367 else
368 Out << ", ";
369
370 P->print(Policy, Out);
371 }
372 Out << ">";
373 break;
374 }
375}
376
John McCall833ca992009-10-29 08:12:44 +0000377//===----------------------------------------------------------------------===//
378// TemplateArgumentLoc Implementation
379//===----------------------------------------------------------------------===//
380
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000381TemplateArgumentLocInfo::TemplateArgumentLocInfo() {
Chandler Carruth75c40642011-04-28 08:19:45 +0000382 memset((void*)this, 0, sizeof(TemplateArgumentLocInfo));
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000383}
384
John McCall828bff22009-10-29 18:45:58 +0000385SourceRange TemplateArgumentLoc::getSourceRange() const {
John McCall833ca992009-10-29 08:12:44 +0000386 switch (Argument.getKind()) {
387 case TemplateArgument::Expression:
John McCall828bff22009-10-29 18:45:58 +0000388 return getSourceExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000389
John McCall833ca992009-10-29 08:12:44 +0000390 case TemplateArgument::Declaration:
John McCall828bff22009-10-29 18:45:58 +0000391 return getSourceDeclExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000392
John McCall828bff22009-10-29 18:45:58 +0000393 case TemplateArgument::Type:
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000394 if (TypeSourceInfo *TSI = getTypeSourceInfo())
395 return TSI->getTypeLoc().getSourceRange();
396 else
397 return SourceRange();
398
Douglas Gregora7fc9012011-01-05 18:58:31 +0000399 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000400 if (getTemplateQualifierLoc())
401 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000402 getTemplateNameLoc());
403 return SourceRange(getTemplateNameLoc());
404
405 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000406 if (getTemplateQualifierLoc())
407 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000408 getTemplateEllipsisLoc());
409 return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
410
John McCall833ca992009-10-29 08:12:44 +0000411 case TemplateArgument::Integral:
412 case TemplateArgument::Pack:
413 case TemplateArgument::Null:
John McCall828bff22009-10-29 18:45:58 +0000414 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000415 }
416
417 // Silence bonus gcc warning.
John McCall828bff22009-10-29 18:45:58 +0000418 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000419}
Douglas Gregora9333192010-05-08 17:41:32 +0000420
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000421TemplateArgumentLoc
422TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000423 llvm::Optional<unsigned> &NumExpansions,
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000424 ASTContext &Context) const {
425 assert(Argument.isPackExpansion());
426
427 switch (Argument.getKind()) {
428 case TemplateArgument::Type: {
Douglas Gregor03491de2010-12-21 22:10:26 +0000429 // FIXME: We shouldn't ever have to worry about missing
430 // type-source info!
431 TypeSourceInfo *ExpansionTSInfo = getTypeSourceInfo();
432 if (!ExpansionTSInfo)
433 ExpansionTSInfo = Context.getTrivialTypeSourceInfo(
434 getArgument().getAsType(),
435 Ellipsis);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000436 PackExpansionTypeLoc Expansion
Douglas Gregor03491de2010-12-21 22:10:26 +0000437 = cast<PackExpansionTypeLoc>(ExpansionTSInfo->getTypeLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000438 Ellipsis = Expansion.getEllipsisLoc();
439
440 TypeLoc Pattern = Expansion.getPatternLoc();
Douglas Gregorcded4f62011-01-14 17:04:44 +0000441 NumExpansions = Expansion.getTypePtr()->getNumExpansions();
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000442
443 // FIXME: This is horrible. We know where the source location data is for
444 // the pattern, and we have the pattern's type, but we are forced to copy
445 // them into an ASTContext because TypeSourceInfo bundles them together
446 // and TemplateArgumentLoc traffics in TypeSourceInfo pointers.
447 TypeSourceInfo *PatternTSInfo
448 = Context.CreateTypeSourceInfo(Pattern.getType(),
449 Pattern.getFullDataSize());
450 memcpy(PatternTSInfo->getTypeLoc().getOpaqueData(),
451 Pattern.getOpaqueData(), Pattern.getFullDataSize());
452 return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
453 PatternTSInfo);
454 }
455
Douglas Gregorbe230c32011-01-03 17:17:50 +0000456 case TemplateArgument::Expression: {
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000457 PackExpansionExpr *Expansion
458 = cast<PackExpansionExpr>(Argument.getAsExpr());
459 Expr *Pattern = Expansion->getPattern();
460 Ellipsis = Expansion->getEllipsisLoc();
Douglas Gregor67fd1252011-01-14 21:20:45 +0000461 NumExpansions = Expansion->getNumExpansions();
Douglas Gregorbe230c32011-01-03 17:17:50 +0000462 return TemplateArgumentLoc(Pattern, Pattern);
463 }
Douglas Gregora7fc9012011-01-05 18:58:31 +0000464
465 case TemplateArgument::TemplateExpansion:
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000466 Ellipsis = getTemplateEllipsisLoc();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000467 NumExpansions = Argument.getNumTemplateExpansions();
Douglas Gregorba68eca2011-01-05 17:40:24 +0000468 return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000469 getTemplateQualifierLoc(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000470 getTemplateNameLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000471
472 case TemplateArgument::Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000473 case TemplateArgument::Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000474 case TemplateArgument::Integral:
475 case TemplateArgument::Pack:
476 case TemplateArgument::Null:
477 return TemplateArgumentLoc();
478 }
479
480 return TemplateArgumentLoc();
481}
482
Douglas Gregora9333192010-05-08 17:41:32 +0000483const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
484 const TemplateArgument &Arg) {
485 switch (Arg.getKind()) {
486 case TemplateArgument::Null:
John McCall67c4a0c2010-08-05 04:58:04 +0000487 // This is bad, but not as bad as crashing because of argument
488 // count mismatches.
489 return DB << "(null template argument)";
Douglas Gregora9333192010-05-08 17:41:32 +0000490
491 case TemplateArgument::Type:
492 return DB << Arg.getAsType();
493
494 case TemplateArgument::Declaration:
495 return DB << Arg.getAsDecl();
496
497 case TemplateArgument::Integral:
498 return DB << Arg.getAsIntegral()->toString(10);
499
500 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000501 return DB << Arg.getAsTemplate();
502
503 case TemplateArgument::TemplateExpansion:
504 return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
505
Douglas Gregora9333192010-05-08 17:41:32 +0000506 case TemplateArgument::Expression: {
507 // This shouldn't actually ever happen, so it's okay that we're
508 // regurgitating an expression here.
509 // FIXME: We're guessing at LangOptions!
510 llvm::SmallString<32> Str;
511 llvm::raw_svector_ostream OS(Str);
512 LangOptions LangOpts;
513 LangOpts.CPlusPlus = true;
514 PrintingPolicy Policy(LangOpts);
515 Arg.getAsExpr()->printPretty(OS, 0, Policy);
516 return DB << OS.str();
517 }
518
Douglas Gregor87dd6972010-12-20 16:52:59 +0000519 case TemplateArgument::Pack: {
520 // FIXME: We're guessing at LangOptions!
521 llvm::SmallString<32> Str;
522 llvm::raw_svector_ostream OS(Str);
523 LangOptions LangOpts;
524 LangOpts.CPlusPlus = true;
525 PrintingPolicy Policy(LangOpts);
526 Arg.print(Policy, OS);
527 return DB << OS.str();
528 }
Douglas Gregora9333192010-05-08 17:41:32 +0000529 }
530
531 return DB;
532}
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +0000533
534const ASTTemplateArgumentListInfo *
535ASTTemplateArgumentListInfo::Create(ASTContext &C,
536 const TemplateArgumentListInfo &List) {
537 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
538 ASTTemplateArgumentListInfo::sizeFor(List);
539 void *Mem = C.Allocate(size, llvm::alignOf<ASTTemplateArgumentListInfo>());
540 ASTTemplateArgumentListInfo *TAI = new (Mem) ASTTemplateArgumentListInfo();
541 TAI->initializeFrom(List);
542 return TAI;
543}
544
545void ASTTemplateArgumentListInfo::initializeFrom(
546 const TemplateArgumentListInfo &Info) {
547 LAngleLoc = Info.getLAngleLoc();
548 RAngleLoc = Info.getRAngleLoc();
549 NumTemplateArgs = Info.size();
550
551 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
552 for (unsigned i = 0; i != NumTemplateArgs; ++i)
553 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
554}
555
556void ASTTemplateArgumentListInfo::initializeFrom(
557 const TemplateArgumentListInfo &Info,
558 bool &Dependent,
559 bool &InstantiationDependent,
560 bool &ContainsUnexpandedParameterPack) {
561 LAngleLoc = Info.getLAngleLoc();
562 RAngleLoc = Info.getRAngleLoc();
563 NumTemplateArgs = Info.size();
564
565 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
566 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
567 Dependent = Dependent || Info[i].getArgument().isDependent();
568 InstantiationDependent = InstantiationDependent ||
569 Info[i].getArgument().isInstantiationDependent();
570 ContainsUnexpandedParameterPack
571 = ContainsUnexpandedParameterPack ||
572 Info[i].getArgument().containsUnexpandedParameterPack();
573
574 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
575 }
576}
577
578void ASTTemplateArgumentListInfo::copyInto(
579 TemplateArgumentListInfo &Info) const {
580 Info.setLAngleLoc(LAngleLoc);
581 Info.setRAngleLoc(RAngleLoc);
582 for (unsigned I = 0; I != NumTemplateArgs; ++I)
583 Info.addArgument(getTemplateArgs()[I]);
584}
585
586std::size_t ASTTemplateArgumentListInfo::sizeFor(unsigned NumTemplateArgs) {
587 return sizeof(ASTTemplateArgumentListInfo) +
588 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
589}
590
591std::size_t ASTTemplateArgumentListInfo::sizeFor(
592 const TemplateArgumentListInfo &Info) {
593 return sizeFor(Info.size());
594}