blob: 0c011a8ef090befb97719a5e90962b152e3e6ed5 [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:
David Blaikieb219cfc2011-09-23 05:06:16 +000071 llvm_unreachable("Should not have a NULL template argument");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000072
73 case Type:
74 return getAsType()->isDependentType();
75
76 case Template:
77 return getAsTemplate().isDependent();
Douglas Gregora7fc9012011-01-05 18:58:31 +000078
79 case TemplateExpansion:
80 return true;
81
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000082 case Declaration:
83 if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
84 return DC->isDependentContext();
85 return getAsDecl()->getDeclContext()->isDependentContext();
86
87 case Integral:
88 // Never dependent
89 return false;
90
91 case Expression:
92 return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent());
93
94 case Pack:
95 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) {
96 if (P->isDependent())
97 return true;
98 }
99
100 return false;
101 }
102
103 return false;
104}
105
Douglas Gregor561f8122011-07-01 01:22:09 +0000106bool TemplateArgument::isInstantiationDependent() const {
107 switch (getKind()) {
108 case Null:
David Blaikieb219cfc2011-09-23 05:06:16 +0000109 llvm_unreachable("Should not have a NULL template argument");
Douglas Gregor561f8122011-07-01 01:22:09 +0000110
111 case Type:
112 return getAsType()->isInstantiationDependentType();
113
114 case Template:
115 return getAsTemplate().isInstantiationDependent();
116
117 case TemplateExpansion:
118 return true;
119
120 case Declaration:
121 if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
122 return DC->isDependentContext();
123 return getAsDecl()->getDeclContext()->isDependentContext();
124
125 case Integral:
126 // Never dependent
127 return false;
128
129 case Expression:
130 return getAsExpr()->isInstantiationDependent();
131
132 case Pack:
133 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) {
134 if (P->isInstantiationDependent())
135 return true;
136 }
137
138 return false;
139 }
140
141 return false;
142}
143
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000144bool TemplateArgument::isPackExpansion() const {
145 switch (getKind()) {
146 case Null:
147 case Declaration:
148 case Integral:
149 case Pack:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000150 case Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000151 return false;
152
Douglas Gregora7fc9012011-01-05 18:58:31 +0000153 case TemplateExpansion:
154 return true;
155
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000156 case Type:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000157 return isa<PackExpansionType>(getAsType());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000158
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000159 case Expression:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000160 return isa<PackExpansionExpr>(getAsExpr());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000161 }
162
163 return false;
164}
165
Douglas Gregord0937222010-12-13 22:49:22 +0000166bool TemplateArgument::containsUnexpandedParameterPack() const {
167 switch (getKind()) {
168 case Null:
169 case Declaration:
170 case Integral:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000171 case TemplateExpansion:
Douglas Gregord0937222010-12-13 22:49:22 +0000172 break;
173
174 case Type:
175 if (getAsType()->containsUnexpandedParameterPack())
176 return true;
177 break;
178
179 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000180 if (getAsTemplate().containsUnexpandedParameterPack())
Douglas Gregord0937222010-12-13 22:49:22 +0000181 return true;
182 break;
183
184 case Expression:
185 if (getAsExpr()->containsUnexpandedParameterPack())
186 return true;
187 break;
188
189 case Pack:
190 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P)
191 if (P->containsUnexpandedParameterPack())
192 return true;
193
194 break;
195 }
196
197 return false;
198}
199
Douglas Gregor2be29f42011-01-14 23:41:42 +0000200llvm::Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
201 assert(Kind == TemplateExpansion);
202 if (TemplateArg.NumExpansions)
203 return TemplateArg.NumExpansions - 1;
204
205 return llvm::Optional<unsigned>();
206}
207
John McCall275c10a2009-10-29 07:48:15 +0000208void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +0000209 const ASTContext &Context) const {
John McCall275c10a2009-10-29 07:48:15 +0000210 ID.AddInteger(Kind);
211 switch (Kind) {
212 case Null:
213 break;
214
215 case Type:
216 getAsType().Profile(ID);
217 break;
218
219 case Declaration:
220 ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0);
221 break;
222
Douglas Gregor788cd062009-11-11 01:00:40 +0000223 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000224 case TemplateExpansion: {
225 TemplateName Template = getAsTemplateOrTemplatePattern();
Douglas Gregor74295b32009-11-23 12:52:47 +0000226 if (TemplateTemplateParmDecl *TTP
227 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Douglas Gregora7fc9012011-01-05 18:58:31 +0000228 Template.getAsTemplateDecl())) {
Douglas Gregor74295b32009-11-23 12:52:47 +0000229 ID.AddBoolean(true);
230 ID.AddInteger(TTP->getDepth());
231 ID.AddInteger(TTP->getPosition());
Douglas Gregorba68eca2011-01-05 17:40:24 +0000232 ID.AddBoolean(TTP->isParameterPack());
Douglas Gregor74295b32009-11-23 12:52:47 +0000233 } else {
234 ID.AddBoolean(false);
Douglas Gregora7fc9012011-01-05 18:58:31 +0000235 ID.AddPointer(Context.getCanonicalTemplateName(Template)
236 .getAsVoidPointer());
Douglas Gregor74295b32009-11-23 12:52:47 +0000237 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000238 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000239 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000240
John McCall275c10a2009-10-29 07:48:15 +0000241 case Integral:
242 getAsIntegral()->Profile(ID);
243 getIntegralType().Profile(ID);
244 break;
245
246 case Expression:
247 getAsExpr()->Profile(ID, Context, true);
248 break;
249
250 case Pack:
251 ID.AddInteger(Args.NumArgs);
252 for (unsigned I = 0; I != Args.NumArgs; ++I)
253 Args.Args[I].Profile(ID, Context);
254 }
255}
John McCall833ca992009-10-29 08:12:44 +0000256
John McCall33500952010-06-11 00:33:02 +0000257bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
258 if (getKind() != Other.getKind()) return false;
259
260 switch (getKind()) {
261 case Null:
262 case Type:
263 case Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000264 case Expression:
265 case Template:
266 case TemplateExpansion:
John McCall33500952010-06-11 00:33:02 +0000267 return TypeOrValue == Other.TypeOrValue;
268
269 case Integral:
270 return getIntegralType() == Other.getIntegralType() &&
271 *getAsIntegral() == *Other.getAsIntegral();
272
273 case Pack:
274 if (Args.NumArgs != Other.Args.NumArgs) return false;
275 for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
276 if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
277 return false;
278 return true;
279 }
280
281 // Suppress warnings.
282 return false;
283}
284
Douglas Gregore02e2622010-12-22 21:19:48 +0000285TemplateArgument TemplateArgument::getPackExpansionPattern() const {
286 assert(isPackExpansion());
287
288 switch (getKind()) {
Douglas Gregorba68eca2011-01-05 17:40:24 +0000289 case Type:
290 return getAsType()->getAs<PackExpansionType>()->getPattern();
291
292 case Expression:
293 return cast<PackExpansionExpr>(getAsExpr())->getPattern();
294
Douglas Gregora7fc9012011-01-05 18:58:31 +0000295 case TemplateExpansion:
Douglas Gregor2be29f42011-01-14 23:41:42 +0000296 return TemplateArgument(getAsTemplateOrTemplatePattern());
Douglas Gregorba68eca2011-01-05 17:40:24 +0000297
298 case Declaration:
299 case Integral:
300 case Pack:
301 case Null:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000302 case Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000303 return TemplateArgument();
Douglas Gregore02e2622010-12-22 21:19:48 +0000304 }
305
306 return TemplateArgument();
307}
308
Douglas Gregor87dd6972010-12-20 16:52:59 +0000309void TemplateArgument::print(const PrintingPolicy &Policy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000310 raw_ostream &Out) const {
Douglas Gregor87dd6972010-12-20 16:52:59 +0000311 switch (getKind()) {
312 case Null:
313 Out << "<no value>";
314 break;
315
316 case Type: {
Douglas Gregore559ca12011-06-17 22:11:49 +0000317 PrintingPolicy SubPolicy(Policy);
318 SubPolicy.SuppressStrongLifetime = true;
Douglas Gregor87dd6972010-12-20 16:52:59 +0000319 std::string TypeStr;
Douglas Gregore559ca12011-06-17 22:11:49 +0000320 getAsType().getAsStringInternal(TypeStr, SubPolicy);
Douglas Gregor87dd6972010-12-20 16:52:59 +0000321 Out << TypeStr;
322 break;
323 }
324
325 case Declaration: {
326 bool Unnamed = true;
327 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getAsDecl())) {
328 if (ND->getDeclName()) {
329 Unnamed = false;
330 Out << ND->getNameAsString();
331 }
332 }
333
334 if (Unnamed) {
335 Out << "<anonymous>";
336 }
337 break;
338 }
339
Douglas Gregora7fc9012011-01-05 18:58:31 +0000340 case Template:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000341 getAsTemplate().print(Out, Policy);
342 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000343
344 case TemplateExpansion:
345 getAsTemplateOrTemplatePattern().print(Out, Policy);
346 Out << "...";
347 break;
348
Douglas Gregor87dd6972010-12-20 16:52:59 +0000349 case Integral: {
Chandler Carruth781701c2011-02-19 00:21:00 +0000350 printIntegral(*this, Out);
Douglas Gregor87dd6972010-12-20 16:52:59 +0000351 break;
352 }
353
Douglas Gregorba68eca2011-01-05 17:40:24 +0000354 case Expression:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000355 getAsExpr()->printPretty(Out, 0, Policy);
356 break;
Douglas Gregor87dd6972010-12-20 16:52:59 +0000357
358 case Pack:
359 Out << "<";
360 bool First = true;
361 for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end();
362 P != PEnd; ++P) {
363 if (First)
364 First = false;
365 else
366 Out << ", ";
367
368 P->print(Policy, Out);
369 }
370 Out << ">";
371 break;
372 }
373}
374
John McCall833ca992009-10-29 08:12:44 +0000375//===----------------------------------------------------------------------===//
376// TemplateArgumentLoc Implementation
377//===----------------------------------------------------------------------===//
378
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000379TemplateArgumentLocInfo::TemplateArgumentLocInfo() {
Chandler Carruth75c40642011-04-28 08:19:45 +0000380 memset((void*)this, 0, sizeof(TemplateArgumentLocInfo));
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000381}
382
John McCall828bff22009-10-29 18:45:58 +0000383SourceRange TemplateArgumentLoc::getSourceRange() const {
John McCall833ca992009-10-29 08:12:44 +0000384 switch (Argument.getKind()) {
385 case TemplateArgument::Expression:
John McCall828bff22009-10-29 18:45:58 +0000386 return getSourceExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000387
John McCall833ca992009-10-29 08:12:44 +0000388 case TemplateArgument::Declaration:
John McCall828bff22009-10-29 18:45:58 +0000389 return getSourceDeclExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000390
John McCall828bff22009-10-29 18:45:58 +0000391 case TemplateArgument::Type:
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000392 if (TypeSourceInfo *TSI = getTypeSourceInfo())
393 return TSI->getTypeLoc().getSourceRange();
394 else
395 return SourceRange();
396
Douglas Gregora7fc9012011-01-05 18:58:31 +0000397 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000398 if (getTemplateQualifierLoc())
399 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000400 getTemplateNameLoc());
401 return SourceRange(getTemplateNameLoc());
402
403 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000404 if (getTemplateQualifierLoc())
405 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000406 getTemplateEllipsisLoc());
407 return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
408
John McCall833ca992009-10-29 08:12:44 +0000409 case TemplateArgument::Integral:
410 case TemplateArgument::Pack:
411 case TemplateArgument::Null:
John McCall828bff22009-10-29 18:45:58 +0000412 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000413 }
414
415 // Silence bonus gcc warning.
John McCall828bff22009-10-29 18:45:58 +0000416 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000417}
Douglas Gregora9333192010-05-08 17:41:32 +0000418
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000419TemplateArgumentLoc
420TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000421 llvm::Optional<unsigned> &NumExpansions,
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000422 ASTContext &Context) const {
423 assert(Argument.isPackExpansion());
424
425 switch (Argument.getKind()) {
426 case TemplateArgument::Type: {
Douglas Gregor03491de2010-12-21 22:10:26 +0000427 // FIXME: We shouldn't ever have to worry about missing
428 // type-source info!
429 TypeSourceInfo *ExpansionTSInfo = getTypeSourceInfo();
430 if (!ExpansionTSInfo)
431 ExpansionTSInfo = Context.getTrivialTypeSourceInfo(
432 getArgument().getAsType(),
433 Ellipsis);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000434 PackExpansionTypeLoc Expansion
Douglas Gregor03491de2010-12-21 22:10:26 +0000435 = cast<PackExpansionTypeLoc>(ExpansionTSInfo->getTypeLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000436 Ellipsis = Expansion.getEllipsisLoc();
437
438 TypeLoc Pattern = Expansion.getPatternLoc();
Douglas Gregorcded4f62011-01-14 17:04:44 +0000439 NumExpansions = Expansion.getTypePtr()->getNumExpansions();
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000440
441 // FIXME: This is horrible. We know where the source location data is for
442 // the pattern, and we have the pattern's type, but we are forced to copy
443 // them into an ASTContext because TypeSourceInfo bundles them together
444 // and TemplateArgumentLoc traffics in TypeSourceInfo pointers.
445 TypeSourceInfo *PatternTSInfo
446 = Context.CreateTypeSourceInfo(Pattern.getType(),
447 Pattern.getFullDataSize());
448 memcpy(PatternTSInfo->getTypeLoc().getOpaqueData(),
449 Pattern.getOpaqueData(), Pattern.getFullDataSize());
450 return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
451 PatternTSInfo);
452 }
453
Douglas Gregorbe230c32011-01-03 17:17:50 +0000454 case TemplateArgument::Expression: {
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000455 PackExpansionExpr *Expansion
456 = cast<PackExpansionExpr>(Argument.getAsExpr());
457 Expr *Pattern = Expansion->getPattern();
458 Ellipsis = Expansion->getEllipsisLoc();
Douglas Gregor67fd1252011-01-14 21:20:45 +0000459 NumExpansions = Expansion->getNumExpansions();
Douglas Gregorbe230c32011-01-03 17:17:50 +0000460 return TemplateArgumentLoc(Pattern, Pattern);
461 }
Douglas Gregora7fc9012011-01-05 18:58:31 +0000462
463 case TemplateArgument::TemplateExpansion:
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000464 Ellipsis = getTemplateEllipsisLoc();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000465 NumExpansions = Argument.getNumTemplateExpansions();
Douglas Gregorba68eca2011-01-05 17:40:24 +0000466 return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000467 getTemplateQualifierLoc(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000468 getTemplateNameLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000469
470 case TemplateArgument::Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000471 case TemplateArgument::Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000472 case TemplateArgument::Integral:
473 case TemplateArgument::Pack:
474 case TemplateArgument::Null:
475 return TemplateArgumentLoc();
476 }
477
478 return TemplateArgumentLoc();
479}
480
Douglas Gregora9333192010-05-08 17:41:32 +0000481const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
482 const TemplateArgument &Arg) {
483 switch (Arg.getKind()) {
484 case TemplateArgument::Null:
John McCall67c4a0c2010-08-05 04:58:04 +0000485 // This is bad, but not as bad as crashing because of argument
486 // count mismatches.
487 return DB << "(null template argument)";
Douglas Gregora9333192010-05-08 17:41:32 +0000488
489 case TemplateArgument::Type:
490 return DB << Arg.getAsType();
491
492 case TemplateArgument::Declaration:
493 return DB << Arg.getAsDecl();
494
495 case TemplateArgument::Integral:
496 return DB << Arg.getAsIntegral()->toString(10);
497
498 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000499 return DB << Arg.getAsTemplate();
500
501 case TemplateArgument::TemplateExpansion:
502 return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
503
Douglas Gregora9333192010-05-08 17:41:32 +0000504 case TemplateArgument::Expression: {
505 // This shouldn't actually ever happen, so it's okay that we're
506 // regurgitating an expression here.
507 // FIXME: We're guessing at LangOptions!
508 llvm::SmallString<32> Str;
509 llvm::raw_svector_ostream OS(Str);
510 LangOptions LangOpts;
511 LangOpts.CPlusPlus = true;
512 PrintingPolicy Policy(LangOpts);
513 Arg.getAsExpr()->printPretty(OS, 0, Policy);
514 return DB << OS.str();
515 }
516
Douglas Gregor87dd6972010-12-20 16:52:59 +0000517 case TemplateArgument::Pack: {
518 // FIXME: We're guessing at LangOptions!
519 llvm::SmallString<32> Str;
520 llvm::raw_svector_ostream OS(Str);
521 LangOptions LangOpts;
522 LangOpts.CPlusPlus = true;
523 PrintingPolicy Policy(LangOpts);
524 Arg.print(Policy, OS);
525 return DB << OS.str();
526 }
Douglas Gregora9333192010-05-08 17:41:32 +0000527 }
528
529 return DB;
530}
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +0000531
532const ASTTemplateArgumentListInfo *
533ASTTemplateArgumentListInfo::Create(ASTContext &C,
534 const TemplateArgumentListInfo &List) {
535 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
536 ASTTemplateArgumentListInfo::sizeFor(List);
537 void *Mem = C.Allocate(size, llvm::alignOf<ASTTemplateArgumentListInfo>());
538 ASTTemplateArgumentListInfo *TAI = new (Mem) ASTTemplateArgumentListInfo();
539 TAI->initializeFrom(List);
540 return TAI;
541}
542
543void ASTTemplateArgumentListInfo::initializeFrom(
544 const TemplateArgumentListInfo &Info) {
545 LAngleLoc = Info.getLAngleLoc();
546 RAngleLoc = Info.getRAngleLoc();
547 NumTemplateArgs = Info.size();
548
549 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
550 for (unsigned i = 0; i != NumTemplateArgs; ++i)
551 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
552}
553
554void ASTTemplateArgumentListInfo::initializeFrom(
555 const TemplateArgumentListInfo &Info,
556 bool &Dependent,
557 bool &InstantiationDependent,
558 bool &ContainsUnexpandedParameterPack) {
559 LAngleLoc = Info.getLAngleLoc();
560 RAngleLoc = Info.getRAngleLoc();
561 NumTemplateArgs = Info.size();
562
563 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
564 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
565 Dependent = Dependent || Info[i].getArgument().isDependent();
566 InstantiationDependent = InstantiationDependent ||
567 Info[i].getArgument().isInstantiationDependent();
568 ContainsUnexpandedParameterPack
569 = ContainsUnexpandedParameterPack ||
570 Info[i].getArgument().containsUnexpandedParameterPack();
571
572 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
573 }
574}
575
576void ASTTemplateArgumentListInfo::copyInto(
577 TemplateArgumentListInfo &Info) const {
578 Info.setLAngleLoc(LAngleLoc);
579 Info.setRAngleLoc(RAngleLoc);
580 for (unsigned I = 0; I != NumTemplateArgs; ++I)
581 Info.addArgument(getTemplateArgs()[I]);
582}
583
584std::size_t ASTTemplateArgumentListInfo::sizeFor(unsigned NumTemplateArgs) {
585 return sizeof(ASTTemplateArgumentListInfo) +
586 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
587}
588
589std::size_t ASTTemplateArgumentListInfo::sizeFor(
590 const TemplateArgumentListInfo &Info) {
591 return sizeFor(Info.size());
592}