blob: b57f10b0c64392078d808ca7abbec17fc8d1b09f [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,
36 llvm::raw_ostream &Out) {
37 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 Gregor8491ffe2010-12-20 22:05:00 +0000107bool TemplateArgument::isPackExpansion() const {
108 switch (getKind()) {
109 case Null:
110 case Declaration:
111 case Integral:
112 case Pack:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000113 case Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000114 return false;
115
Douglas Gregora7fc9012011-01-05 18:58:31 +0000116 case TemplateExpansion:
117 return true;
118
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000119 case Type:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000120 return isa<PackExpansionType>(getAsType());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000121
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000122 case Expression:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000123 return isa<PackExpansionExpr>(getAsExpr());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000124 }
125
126 return false;
127}
128
Douglas Gregord0937222010-12-13 22:49:22 +0000129bool TemplateArgument::containsUnexpandedParameterPack() const {
130 switch (getKind()) {
131 case Null:
132 case Declaration:
133 case Integral:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000134 case TemplateExpansion:
Douglas Gregord0937222010-12-13 22:49:22 +0000135 break;
136
137 case Type:
138 if (getAsType()->containsUnexpandedParameterPack())
139 return true;
140 break;
141
142 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000143 if (getAsTemplate().containsUnexpandedParameterPack())
Douglas Gregord0937222010-12-13 22:49:22 +0000144 return true;
145 break;
146
147 case Expression:
148 if (getAsExpr()->containsUnexpandedParameterPack())
149 return true;
150 break;
151
152 case Pack:
153 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P)
154 if (P->containsUnexpandedParameterPack())
155 return true;
156
157 break;
158 }
159
160 return false;
161}
162
Douglas Gregor2be29f42011-01-14 23:41:42 +0000163llvm::Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
164 assert(Kind == TemplateExpansion);
165 if (TemplateArg.NumExpansions)
166 return TemplateArg.NumExpansions - 1;
167
168 return llvm::Optional<unsigned>();
169}
170
John McCall275c10a2009-10-29 07:48:15 +0000171void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +0000172 const ASTContext &Context) const {
John McCall275c10a2009-10-29 07:48:15 +0000173 ID.AddInteger(Kind);
174 switch (Kind) {
175 case Null:
176 break;
177
178 case Type:
179 getAsType().Profile(ID);
180 break;
181
182 case Declaration:
183 ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0);
184 break;
185
Douglas Gregor788cd062009-11-11 01:00:40 +0000186 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000187 case TemplateExpansion: {
188 TemplateName Template = getAsTemplateOrTemplatePattern();
Douglas Gregor74295b32009-11-23 12:52:47 +0000189 if (TemplateTemplateParmDecl *TTP
190 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Douglas Gregora7fc9012011-01-05 18:58:31 +0000191 Template.getAsTemplateDecl())) {
Douglas Gregor74295b32009-11-23 12:52:47 +0000192 ID.AddBoolean(true);
193 ID.AddInteger(TTP->getDepth());
194 ID.AddInteger(TTP->getPosition());
Douglas Gregorba68eca2011-01-05 17:40:24 +0000195 ID.AddBoolean(TTP->isParameterPack());
Douglas Gregor74295b32009-11-23 12:52:47 +0000196 } else {
197 ID.AddBoolean(false);
Douglas Gregora7fc9012011-01-05 18:58:31 +0000198 ID.AddPointer(Context.getCanonicalTemplateName(Template)
199 .getAsVoidPointer());
Douglas Gregor74295b32009-11-23 12:52:47 +0000200 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000201 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000202 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000203
John McCall275c10a2009-10-29 07:48:15 +0000204 case Integral:
205 getAsIntegral()->Profile(ID);
206 getIntegralType().Profile(ID);
207 break;
208
209 case Expression:
210 getAsExpr()->Profile(ID, Context, true);
211 break;
212
213 case Pack:
214 ID.AddInteger(Args.NumArgs);
215 for (unsigned I = 0; I != Args.NumArgs; ++I)
216 Args.Args[I].Profile(ID, Context);
217 }
218}
John McCall833ca992009-10-29 08:12:44 +0000219
John McCall33500952010-06-11 00:33:02 +0000220bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
221 if (getKind() != Other.getKind()) return false;
222
223 switch (getKind()) {
224 case Null:
225 case Type:
226 case Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000227 case Expression:
228 case Template:
229 case TemplateExpansion:
John McCall33500952010-06-11 00:33:02 +0000230 return TypeOrValue == Other.TypeOrValue;
231
232 case Integral:
233 return getIntegralType() == Other.getIntegralType() &&
234 *getAsIntegral() == *Other.getAsIntegral();
235
236 case Pack:
237 if (Args.NumArgs != Other.Args.NumArgs) return false;
238 for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
239 if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
240 return false;
241 return true;
242 }
243
244 // Suppress warnings.
245 return false;
246}
247
Douglas Gregore02e2622010-12-22 21:19:48 +0000248TemplateArgument TemplateArgument::getPackExpansionPattern() const {
249 assert(isPackExpansion());
250
251 switch (getKind()) {
Douglas Gregorba68eca2011-01-05 17:40:24 +0000252 case Type:
253 return getAsType()->getAs<PackExpansionType>()->getPattern();
254
255 case Expression:
256 return cast<PackExpansionExpr>(getAsExpr())->getPattern();
257
Douglas Gregora7fc9012011-01-05 18:58:31 +0000258 case TemplateExpansion:
Douglas Gregor2be29f42011-01-14 23:41:42 +0000259 return TemplateArgument(getAsTemplateOrTemplatePattern());
Douglas Gregorba68eca2011-01-05 17:40:24 +0000260
261 case Declaration:
262 case Integral:
263 case Pack:
264 case Null:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000265 case Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000266 return TemplateArgument();
Douglas Gregore02e2622010-12-22 21:19:48 +0000267 }
268
269 return TemplateArgument();
270}
271
Douglas Gregor87dd6972010-12-20 16:52:59 +0000272void TemplateArgument::print(const PrintingPolicy &Policy,
273 llvm::raw_ostream &Out) const {
274 switch (getKind()) {
275 case Null:
276 Out << "<no value>";
277 break;
278
279 case Type: {
280 std::string TypeStr;
281 getAsType().getAsStringInternal(TypeStr, Policy);
282 Out << TypeStr;
283 break;
284 }
285
286 case Declaration: {
287 bool Unnamed = true;
288 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getAsDecl())) {
289 if (ND->getDeclName()) {
290 Unnamed = false;
291 Out << ND->getNameAsString();
292 }
293 }
294
295 if (Unnamed) {
296 Out << "<anonymous>";
297 }
298 break;
299 }
300
Douglas Gregora7fc9012011-01-05 18:58:31 +0000301 case Template:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000302 getAsTemplate().print(Out, Policy);
303 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000304
305 case TemplateExpansion:
306 getAsTemplateOrTemplatePattern().print(Out, Policy);
307 Out << "...";
308 break;
309
Douglas Gregor87dd6972010-12-20 16:52:59 +0000310 case Integral: {
Chandler Carruth781701c2011-02-19 00:21:00 +0000311 printIntegral(*this, Out);
Douglas Gregor87dd6972010-12-20 16:52:59 +0000312 break;
313 }
314
Douglas Gregorba68eca2011-01-05 17:40:24 +0000315 case Expression:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000316 getAsExpr()->printPretty(Out, 0, Policy);
317 break;
Douglas Gregor87dd6972010-12-20 16:52:59 +0000318
319 case Pack:
320 Out << "<";
321 bool First = true;
322 for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end();
323 P != PEnd; ++P) {
324 if (First)
325 First = false;
326 else
327 Out << ", ";
328
329 P->print(Policy, Out);
330 }
331 Out << ">";
332 break;
333 }
334}
335
John McCall833ca992009-10-29 08:12:44 +0000336//===----------------------------------------------------------------------===//
337// TemplateArgumentLoc Implementation
338//===----------------------------------------------------------------------===//
339
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000340TemplateArgumentLocInfo::TemplateArgumentLocInfo() {
341 memset(this, 0, sizeof(TemplateArgumentLocInfo));
342}
343
John McCall828bff22009-10-29 18:45:58 +0000344SourceRange TemplateArgumentLoc::getSourceRange() const {
John McCall833ca992009-10-29 08:12:44 +0000345 switch (Argument.getKind()) {
346 case TemplateArgument::Expression:
John McCall828bff22009-10-29 18:45:58 +0000347 return getSourceExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000348
John McCall833ca992009-10-29 08:12:44 +0000349 case TemplateArgument::Declaration:
John McCall828bff22009-10-29 18:45:58 +0000350 return getSourceDeclExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000351
John McCall828bff22009-10-29 18:45:58 +0000352 case TemplateArgument::Type:
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000353 if (TypeSourceInfo *TSI = getTypeSourceInfo())
354 return TSI->getTypeLoc().getSourceRange();
355 else
356 return SourceRange();
357
Douglas Gregora7fc9012011-01-05 18:58:31 +0000358 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000359 if (getTemplateQualifierLoc())
360 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000361 getTemplateNameLoc());
362 return SourceRange(getTemplateNameLoc());
363
364 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000365 if (getTemplateQualifierLoc())
366 return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000367 getTemplateEllipsisLoc());
368 return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
369
John McCall833ca992009-10-29 08:12:44 +0000370 case TemplateArgument::Integral:
371 case TemplateArgument::Pack:
372 case TemplateArgument::Null:
John McCall828bff22009-10-29 18:45:58 +0000373 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000374 }
375
376 // Silence bonus gcc warning.
John McCall828bff22009-10-29 18:45:58 +0000377 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000378}
Douglas Gregora9333192010-05-08 17:41:32 +0000379
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000380TemplateArgumentLoc
381TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000382 llvm::Optional<unsigned> &NumExpansions,
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000383 ASTContext &Context) const {
384 assert(Argument.isPackExpansion());
385
386 switch (Argument.getKind()) {
387 case TemplateArgument::Type: {
Douglas Gregor03491de2010-12-21 22:10:26 +0000388 // FIXME: We shouldn't ever have to worry about missing
389 // type-source info!
390 TypeSourceInfo *ExpansionTSInfo = getTypeSourceInfo();
391 if (!ExpansionTSInfo)
392 ExpansionTSInfo = Context.getTrivialTypeSourceInfo(
393 getArgument().getAsType(),
394 Ellipsis);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000395 PackExpansionTypeLoc Expansion
Douglas Gregor03491de2010-12-21 22:10:26 +0000396 = cast<PackExpansionTypeLoc>(ExpansionTSInfo->getTypeLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000397 Ellipsis = Expansion.getEllipsisLoc();
398
399 TypeLoc Pattern = Expansion.getPatternLoc();
Douglas Gregorcded4f62011-01-14 17:04:44 +0000400 NumExpansions = Expansion.getTypePtr()->getNumExpansions();
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000401
402 // FIXME: This is horrible. We know where the source location data is for
403 // the pattern, and we have the pattern's type, but we are forced to copy
404 // them into an ASTContext because TypeSourceInfo bundles them together
405 // and TemplateArgumentLoc traffics in TypeSourceInfo pointers.
406 TypeSourceInfo *PatternTSInfo
407 = Context.CreateTypeSourceInfo(Pattern.getType(),
408 Pattern.getFullDataSize());
409 memcpy(PatternTSInfo->getTypeLoc().getOpaqueData(),
410 Pattern.getOpaqueData(), Pattern.getFullDataSize());
411 return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
412 PatternTSInfo);
413 }
414
Douglas Gregorbe230c32011-01-03 17:17:50 +0000415 case TemplateArgument::Expression: {
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000416 PackExpansionExpr *Expansion
417 = cast<PackExpansionExpr>(Argument.getAsExpr());
418 Expr *Pattern = Expansion->getPattern();
419 Ellipsis = Expansion->getEllipsisLoc();
Douglas Gregor67fd1252011-01-14 21:20:45 +0000420 NumExpansions = Expansion->getNumExpansions();
Douglas Gregorbe230c32011-01-03 17:17:50 +0000421 return TemplateArgumentLoc(Pattern, Pattern);
422 }
Douglas Gregora7fc9012011-01-05 18:58:31 +0000423
424 case TemplateArgument::TemplateExpansion:
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000425 Ellipsis = getTemplateEllipsisLoc();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000426 NumExpansions = Argument.getNumTemplateExpansions();
Douglas Gregorba68eca2011-01-05 17:40:24 +0000427 return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000428 getTemplateQualifierLoc(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000429 getTemplateNameLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000430
431 case TemplateArgument::Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000432 case TemplateArgument::Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000433 case TemplateArgument::Integral:
434 case TemplateArgument::Pack:
435 case TemplateArgument::Null:
436 return TemplateArgumentLoc();
437 }
438
439 return TemplateArgumentLoc();
440}
441
Douglas Gregora9333192010-05-08 17:41:32 +0000442const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
443 const TemplateArgument &Arg) {
444 switch (Arg.getKind()) {
445 case TemplateArgument::Null:
John McCall67c4a0c2010-08-05 04:58:04 +0000446 // This is bad, but not as bad as crashing because of argument
447 // count mismatches.
448 return DB << "(null template argument)";
Douglas Gregora9333192010-05-08 17:41:32 +0000449
450 case TemplateArgument::Type:
451 return DB << Arg.getAsType();
452
453 case TemplateArgument::Declaration:
454 return DB << Arg.getAsDecl();
455
456 case TemplateArgument::Integral:
457 return DB << Arg.getAsIntegral()->toString(10);
458
459 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000460 return DB << Arg.getAsTemplate();
461
462 case TemplateArgument::TemplateExpansion:
463 return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
464
Douglas Gregora9333192010-05-08 17:41:32 +0000465 case TemplateArgument::Expression: {
466 // This shouldn't actually ever happen, so it's okay that we're
467 // regurgitating an expression here.
468 // FIXME: We're guessing at LangOptions!
469 llvm::SmallString<32> Str;
470 llvm::raw_svector_ostream OS(Str);
471 LangOptions LangOpts;
472 LangOpts.CPlusPlus = true;
473 PrintingPolicy Policy(LangOpts);
474 Arg.getAsExpr()->printPretty(OS, 0, Policy);
475 return DB << OS.str();
476 }
477
Douglas Gregor87dd6972010-12-20 16:52:59 +0000478 case TemplateArgument::Pack: {
479 // FIXME: We're guessing at LangOptions!
480 llvm::SmallString<32> Str;
481 llvm::raw_svector_ostream OS(Str);
482 LangOptions LangOpts;
483 LangOpts.CPlusPlus = true;
484 PrintingPolicy Policy(LangOpts);
485 Arg.print(Policy, OS);
486 return DB << OS.str();
487 }
Douglas Gregora9333192010-05-08 17:41:32 +0000488 }
489
490 return DB;
491}