blob: 26c0c089718af882e6d64f7c06cc36870337723b [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"
John McCall833ca992009-10-29 08:12:44 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregora9333192010-05-08 17:41:32 +000022#include "clang/Basic/Diagnostic.h"
Douglas Gregor87dd6972010-12-20 16:52:59 +000023#include "llvm/ADT/FoldingSet.h"
Douglas Gregor203e6a32011-01-11 23:09:57 +000024#include <algorithm>
John McCall275c10a2009-10-29 07:48:15 +000025
26using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// TemplateArgument Implementation
30//===----------------------------------------------------------------------===//
31
Douglas Gregor203e6a32011-01-11 23:09:57 +000032TemplateArgument TemplateArgument::CreatePackCopy(ASTContext &Context,
33 const TemplateArgument *Args,
34 unsigned NumArgs) {
35 if (NumArgs == 0)
36 return TemplateArgument(0, 0);
37
38 TemplateArgument *Storage = new (Context) TemplateArgument [NumArgs];
39 std::copy(Args, Args + NumArgs, Storage);
40 return TemplateArgument(Storage, NumArgs);
41}
42
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000043bool TemplateArgument::isDependent() const {
44 switch (getKind()) {
45 case Null:
46 assert(false && "Should not have a NULL template argument");
47 return false;
48
49 case Type:
50 return getAsType()->isDependentType();
51
52 case Template:
53 return getAsTemplate().isDependent();
Douglas Gregora7fc9012011-01-05 18:58:31 +000054
55 case TemplateExpansion:
56 return true;
57
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000058 case Declaration:
59 if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
60 return DC->isDependentContext();
61 return getAsDecl()->getDeclContext()->isDependentContext();
62
63 case Integral:
64 // Never dependent
65 return false;
66
67 case Expression:
68 return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent());
69
70 case Pack:
71 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) {
72 if (P->isDependent())
73 return true;
74 }
75
76 return false;
77 }
78
79 return false;
80}
81
Douglas Gregor8491ffe2010-12-20 22:05:00 +000082bool TemplateArgument::isPackExpansion() const {
83 switch (getKind()) {
84 case Null:
85 case Declaration:
86 case Integral:
87 case Pack:
Douglas Gregora7fc9012011-01-05 18:58:31 +000088 case Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +000089 return false;
90
Douglas Gregora7fc9012011-01-05 18:58:31 +000091 case TemplateExpansion:
92 return true;
93
Douglas Gregor8491ffe2010-12-20 22:05:00 +000094 case Type:
Douglas Gregorbe230c32011-01-03 17:17:50 +000095 return isa<PackExpansionType>(getAsType());
Douglas Gregora7fc9012011-01-05 18:58:31 +000096
Douglas Gregor8491ffe2010-12-20 22:05:00 +000097 case Expression:
Douglas Gregorbe230c32011-01-03 17:17:50 +000098 return isa<PackExpansionExpr>(getAsExpr());
Douglas Gregor8491ffe2010-12-20 22:05:00 +000099 }
100
101 return false;
102}
103
Douglas Gregord0937222010-12-13 22:49:22 +0000104bool TemplateArgument::containsUnexpandedParameterPack() const {
105 switch (getKind()) {
106 case Null:
107 case Declaration:
108 case Integral:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000109 case TemplateExpansion:
Douglas Gregord0937222010-12-13 22:49:22 +0000110 break;
111
112 case Type:
113 if (getAsType()->containsUnexpandedParameterPack())
114 return true;
115 break;
116
117 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000118 if (getAsTemplate().containsUnexpandedParameterPack())
Douglas Gregord0937222010-12-13 22:49:22 +0000119 return true;
120 break;
121
122 case Expression:
123 if (getAsExpr()->containsUnexpandedParameterPack())
124 return true;
125 break;
126
127 case Pack:
128 for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P)
129 if (P->containsUnexpandedParameterPack())
130 return true;
131
132 break;
133 }
134
135 return false;
136}
137
John McCall275c10a2009-10-29 07:48:15 +0000138void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +0000139 const ASTContext &Context) const {
John McCall275c10a2009-10-29 07:48:15 +0000140 ID.AddInteger(Kind);
141 switch (Kind) {
142 case Null:
143 break;
144
145 case Type:
146 getAsType().Profile(ID);
147 break;
148
149 case Declaration:
150 ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0);
151 break;
152
Douglas Gregor788cd062009-11-11 01:00:40 +0000153 case Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000154 case TemplateExpansion: {
155 TemplateName Template = getAsTemplateOrTemplatePattern();
Douglas Gregor74295b32009-11-23 12:52:47 +0000156 if (TemplateTemplateParmDecl *TTP
157 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Douglas Gregora7fc9012011-01-05 18:58:31 +0000158 Template.getAsTemplateDecl())) {
Douglas Gregor74295b32009-11-23 12:52:47 +0000159 ID.AddBoolean(true);
160 ID.AddInteger(TTP->getDepth());
161 ID.AddInteger(TTP->getPosition());
Douglas Gregorba68eca2011-01-05 17:40:24 +0000162 ID.AddBoolean(TTP->isParameterPack());
Douglas Gregor74295b32009-11-23 12:52:47 +0000163 } else {
164 ID.AddBoolean(false);
Douglas Gregora7fc9012011-01-05 18:58:31 +0000165 ID.AddPointer(Context.getCanonicalTemplateName(Template)
166 .getAsVoidPointer());
Douglas Gregor74295b32009-11-23 12:52:47 +0000167 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000168 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000169 }
Douglas Gregor788cd062009-11-11 01:00:40 +0000170
John McCall275c10a2009-10-29 07:48:15 +0000171 case Integral:
172 getAsIntegral()->Profile(ID);
173 getIntegralType().Profile(ID);
174 break;
175
176 case Expression:
177 getAsExpr()->Profile(ID, Context, true);
178 break;
179
180 case Pack:
181 ID.AddInteger(Args.NumArgs);
182 for (unsigned I = 0; I != Args.NumArgs; ++I)
183 Args.Args[I].Profile(ID, Context);
184 }
185}
John McCall833ca992009-10-29 08:12:44 +0000186
John McCall33500952010-06-11 00:33:02 +0000187bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
188 if (getKind() != Other.getKind()) return false;
189
190 switch (getKind()) {
191 case Null:
192 case Type:
193 case Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000194 case Expression:
195 case Template:
196 case TemplateExpansion:
John McCall33500952010-06-11 00:33:02 +0000197 return TypeOrValue == Other.TypeOrValue;
198
199 case Integral:
200 return getIntegralType() == Other.getIntegralType() &&
201 *getAsIntegral() == *Other.getAsIntegral();
202
203 case Pack:
204 if (Args.NumArgs != Other.Args.NumArgs) return false;
205 for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
206 if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
207 return false;
208 return true;
209 }
210
211 // Suppress warnings.
212 return false;
213}
214
Douglas Gregore02e2622010-12-22 21:19:48 +0000215TemplateArgument TemplateArgument::getPackExpansionPattern() const {
216 assert(isPackExpansion());
217
218 switch (getKind()) {
Douglas Gregorba68eca2011-01-05 17:40:24 +0000219 case Type:
220 return getAsType()->getAs<PackExpansionType>()->getPattern();
221
222 case Expression:
223 return cast<PackExpansionExpr>(getAsExpr())->getPattern();
224
Douglas Gregora7fc9012011-01-05 18:58:31 +0000225 case TemplateExpansion:
226 return TemplateArgument(getAsTemplateOrTemplatePattern(), false);
Douglas Gregorba68eca2011-01-05 17:40:24 +0000227
228 case Declaration:
229 case Integral:
230 case Pack:
231 case Null:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000232 case Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000233 return TemplateArgument();
Douglas Gregore02e2622010-12-22 21:19:48 +0000234 }
235
236 return TemplateArgument();
237}
238
Douglas Gregor87dd6972010-12-20 16:52:59 +0000239void TemplateArgument::print(const PrintingPolicy &Policy,
240 llvm::raw_ostream &Out) const {
241 switch (getKind()) {
242 case Null:
243 Out << "<no value>";
244 break;
245
246 case Type: {
247 std::string TypeStr;
248 getAsType().getAsStringInternal(TypeStr, Policy);
249 Out << TypeStr;
250 break;
251 }
252
253 case Declaration: {
254 bool Unnamed = true;
255 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getAsDecl())) {
256 if (ND->getDeclName()) {
257 Unnamed = false;
258 Out << ND->getNameAsString();
259 }
260 }
261
262 if (Unnamed) {
263 Out << "<anonymous>";
264 }
265 break;
266 }
267
Douglas Gregora7fc9012011-01-05 18:58:31 +0000268 case Template:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000269 getAsTemplate().print(Out, Policy);
270 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +0000271
272 case TemplateExpansion:
273 getAsTemplateOrTemplatePattern().print(Out, Policy);
274 Out << "...";
275 break;
276
Douglas Gregor87dd6972010-12-20 16:52:59 +0000277 case Integral: {
278 Out << getAsIntegral()->toString(10);
279 break;
280 }
281
Douglas Gregorba68eca2011-01-05 17:40:24 +0000282 case Expression:
Douglas Gregor87dd6972010-12-20 16:52:59 +0000283 getAsExpr()->printPretty(Out, 0, Policy);
284 break;
Douglas Gregor87dd6972010-12-20 16:52:59 +0000285
286 case Pack:
287 Out << "<";
288 bool First = true;
289 for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end();
290 P != PEnd; ++P) {
291 if (First)
292 First = false;
293 else
294 Out << ", ";
295
296 P->print(Policy, Out);
297 }
298 Out << ">";
299 break;
300 }
301}
302
John McCall833ca992009-10-29 08:12:44 +0000303//===----------------------------------------------------------------------===//
304// TemplateArgumentLoc Implementation
305//===----------------------------------------------------------------------===//
306
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000307TemplateArgumentLocInfo::TemplateArgumentLocInfo() {
308 memset(this, 0, sizeof(TemplateArgumentLocInfo));
309}
310
John McCall828bff22009-10-29 18:45:58 +0000311SourceRange TemplateArgumentLoc::getSourceRange() const {
John McCall833ca992009-10-29 08:12:44 +0000312 switch (Argument.getKind()) {
313 case TemplateArgument::Expression:
John McCall828bff22009-10-29 18:45:58 +0000314 return getSourceExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000315
John McCall833ca992009-10-29 08:12:44 +0000316 case TemplateArgument::Declaration:
John McCall828bff22009-10-29 18:45:58 +0000317 return getSourceDeclExpression()->getSourceRange();
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000318
John McCall828bff22009-10-29 18:45:58 +0000319 case TemplateArgument::Type:
Zhanyong Wanf38ef0c2010-09-03 23:50:56 +0000320 if (TypeSourceInfo *TSI = getTypeSourceInfo())
321 return TSI->getTypeLoc().getSourceRange();
322 else
323 return SourceRange();
324
Douglas Gregora7fc9012011-01-05 18:58:31 +0000325 case TemplateArgument::Template:
Douglas Gregor788cd062009-11-11 01:00:40 +0000326 if (getTemplateQualifierRange().isValid())
Douglas Gregora7fc9012011-01-05 18:58:31 +0000327 return SourceRange(getTemplateQualifierRange().getBegin(),
328 getTemplateNameLoc());
329 return SourceRange(getTemplateNameLoc());
330
331 case TemplateArgument::TemplateExpansion:
332 if (getTemplateQualifierRange().isValid())
333 return SourceRange(getTemplateQualifierRange().getBegin(),
334 getTemplateEllipsisLoc());
335 return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
336
John McCall833ca992009-10-29 08:12:44 +0000337 case TemplateArgument::Integral:
338 case TemplateArgument::Pack:
339 case TemplateArgument::Null:
John McCall828bff22009-10-29 18:45:58 +0000340 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000341 }
342
343 // Silence bonus gcc warning.
John McCall828bff22009-10-29 18:45:58 +0000344 return SourceRange();
John McCall833ca992009-10-29 08:12:44 +0000345}
Douglas Gregora9333192010-05-08 17:41:32 +0000346
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000347TemplateArgumentLoc
348TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000349 llvm::Optional<unsigned> &NumExpansions,
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000350 ASTContext &Context) const {
351 assert(Argument.isPackExpansion());
352
353 switch (Argument.getKind()) {
354 case TemplateArgument::Type: {
Douglas Gregor03491de2010-12-21 22:10:26 +0000355 // FIXME: We shouldn't ever have to worry about missing
356 // type-source info!
357 TypeSourceInfo *ExpansionTSInfo = getTypeSourceInfo();
358 if (!ExpansionTSInfo)
359 ExpansionTSInfo = Context.getTrivialTypeSourceInfo(
360 getArgument().getAsType(),
361 Ellipsis);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000362 PackExpansionTypeLoc Expansion
Douglas Gregor03491de2010-12-21 22:10:26 +0000363 = cast<PackExpansionTypeLoc>(ExpansionTSInfo->getTypeLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000364 Ellipsis = Expansion.getEllipsisLoc();
365
366 TypeLoc Pattern = Expansion.getPatternLoc();
Douglas Gregorcded4f62011-01-14 17:04:44 +0000367 NumExpansions = Expansion.getTypePtr()->getNumExpansions();
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000368
369 // FIXME: This is horrible. We know where the source location data is for
370 // the pattern, and we have the pattern's type, but we are forced to copy
371 // them into an ASTContext because TypeSourceInfo bundles them together
372 // and TemplateArgumentLoc traffics in TypeSourceInfo pointers.
373 TypeSourceInfo *PatternTSInfo
374 = Context.CreateTypeSourceInfo(Pattern.getType(),
375 Pattern.getFullDataSize());
376 memcpy(PatternTSInfo->getTypeLoc().getOpaqueData(),
377 Pattern.getOpaqueData(), Pattern.getFullDataSize());
378 return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
379 PatternTSInfo);
380 }
381
Douglas Gregorbe230c32011-01-03 17:17:50 +0000382 case TemplateArgument::Expression: {
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000383 PackExpansionExpr *Expansion
384 = cast<PackExpansionExpr>(Argument.getAsExpr());
385 Expr *Pattern = Expansion->getPattern();
386 Ellipsis = Expansion->getEllipsisLoc();
Douglas Gregor67fd1252011-01-14 21:20:45 +0000387 NumExpansions = Expansion->getNumExpansions();
Douglas Gregorbe230c32011-01-03 17:17:50 +0000388 return TemplateArgumentLoc(Pattern, Pattern);
389 }
Douglas Gregora7fc9012011-01-05 18:58:31 +0000390
391 case TemplateArgument::TemplateExpansion:
Douglas Gregorcded4f62011-01-14 17:04:44 +0000392 // FIXME: Variadic templates num expansions
Douglas Gregorb0ddf3a2011-01-06 00:33:28 +0000393 Ellipsis = getTemplateEllipsisLoc();
Douglas Gregorba68eca2011-01-05 17:40:24 +0000394 return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
395 getTemplateQualifierRange(),
396 getTemplateNameLoc());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000397
398 case TemplateArgument::Declaration:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000399 case TemplateArgument::Template:
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000400 case TemplateArgument::Integral:
401 case TemplateArgument::Pack:
402 case TemplateArgument::Null:
403 return TemplateArgumentLoc();
404 }
405
406 return TemplateArgumentLoc();
407}
408
Douglas Gregora9333192010-05-08 17:41:32 +0000409const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
410 const TemplateArgument &Arg) {
411 switch (Arg.getKind()) {
412 case TemplateArgument::Null:
John McCall67c4a0c2010-08-05 04:58:04 +0000413 // This is bad, but not as bad as crashing because of argument
414 // count mismatches.
415 return DB << "(null template argument)";
Douglas Gregora9333192010-05-08 17:41:32 +0000416
417 case TemplateArgument::Type:
418 return DB << Arg.getAsType();
419
420 case TemplateArgument::Declaration:
421 return DB << Arg.getAsDecl();
422
423 case TemplateArgument::Integral:
424 return DB << Arg.getAsIntegral()->toString(10);
425
426 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000427 return DB << Arg.getAsTemplate();
428
429 case TemplateArgument::TemplateExpansion:
430 return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
431
Douglas Gregora9333192010-05-08 17:41:32 +0000432 case TemplateArgument::Expression: {
433 // This shouldn't actually ever happen, so it's okay that we're
434 // regurgitating an expression here.
435 // FIXME: We're guessing at LangOptions!
436 llvm::SmallString<32> Str;
437 llvm::raw_svector_ostream OS(Str);
438 LangOptions LangOpts;
439 LangOpts.CPlusPlus = true;
440 PrintingPolicy Policy(LangOpts);
441 Arg.getAsExpr()->printPretty(OS, 0, Policy);
442 return DB << OS.str();
443 }
444
Douglas Gregor87dd6972010-12-20 16:52:59 +0000445 case TemplateArgument::Pack: {
446 // FIXME: We're guessing at LangOptions!
447 llvm::SmallString<32> Str;
448 llvm::raw_svector_ostream OS(Str);
449 LangOptions LangOpts;
450 LangOpts.CPlusPlus = true;
451 PrintingPolicy Policy(LangOpts);
452 Arg.print(Policy, OS);
453 return DB << OS.str();
454 }
Douglas Gregora9333192010-05-08 17:41:32 +0000455 }
456
457 return DB;
458}