blob: d661d866c44e7d3b26c567c7ca4dacc185347dae [file] [log] [blame]
Douglas Gregorc4633352010-12-15 17:38:57 +00001//===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/
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// This file implements semantic analysis for C++0x variadic templates.
10//===----------------------------------------------------------------------===/
11
12#include "clang/Sema/Sema.h"
Douglas Gregoree8aff02011-01-04 17:33:58 +000013#include "clang/Sema/Lookup.h"
Douglas Gregor7536dd52010-12-20 02:24:11 +000014#include "clang/Sema/ParsedTemplate.h"
Douglas Gregorc4633352010-12-15 17:38:57 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregor8491ffe2010-12-20 22:05:00 +000016#include "clang/Sema/Template.h"
Douglas Gregorc4633352010-12-15 17:38:57 +000017#include "clang/AST/Expr.h"
Douglas Gregor9ef75892010-12-15 19:43:21 +000018#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregorc4633352010-12-15 17:38:57 +000019#include "clang/AST/TypeLoc.h"
20
21using namespace clang;
22
Douglas Gregor9ef75892010-12-15 19:43:21 +000023//----------------------------------------------------------------------------
24// Visitor that collects unexpanded parameter packs
25//----------------------------------------------------------------------------
26
Douglas Gregor9ef75892010-12-15 19:43:21 +000027namespace {
28 /// \brief A class that collects unexpanded parameter packs.
29 class CollectUnexpandedParameterPacksVisitor :
30 public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
31 {
32 typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
33 inherited;
34
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +000036
37 public:
38 explicit CollectUnexpandedParameterPacksVisitor(
Chris Lattner5f9e2722011-07-23 10:55:15 +000039 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
Douglas Gregor9ef75892010-12-15 19:43:21 +000040 : Unexpanded(Unexpanded) { }
41
Douglas Gregora40bc722010-12-20 23:07:20 +000042 bool shouldWalkTypesOfTypeLocs() const { return false; }
43
Douglas Gregor9ef75892010-12-15 19:43:21 +000044 //------------------------------------------------------------------------
45 // Recording occurrences of (unexpanded) parameter packs.
46 //------------------------------------------------------------------------
47
48 /// \brief Record occurrences of template type parameter packs.
49 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
50 if (TL.getTypePtr()->isParameterPack())
51 Unexpanded.push_back(std::make_pair(TL.getTypePtr(), TL.getNameLoc()));
52 return true;
53 }
54
55 /// \brief Record occurrences of template type parameter packs
56 /// when we don't have proper source-location information for
57 /// them.
58 ///
59 /// Ideally, this routine would never be used.
60 bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
61 if (T->isParameterPack())
62 Unexpanded.push_back(std::make_pair(T, SourceLocation()));
63
64 return true;
65 }
66
Douglas Gregora779d9c2011-01-19 21:32:01 +000067 /// \brief Record occurrences of function and non-type template
Douglas Gregor10738d32010-12-23 23:51:58 +000068 /// parameter packs in an expression.
69 bool VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor12c9c002011-01-07 16:43:16 +000070 if (E->getDecl()->isParameterPack())
71 Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation()));
Douglas Gregor10738d32010-12-23 23:51:58 +000072
73 return true;
74 }
75
Douglas Gregora779d9c2011-01-19 21:32:01 +000076 // \brief Record occurrences of function and non-type template parameter
77 // packs in a block-captured expression.
78 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
79 if (E->getDecl()->isParameterPack())
80 Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation()));
81
82 return true;
83 }
84
Douglas Gregor61c4d282011-01-05 15:48:55 +000085 /// \brief Record occurrences of template template parameter packs.
86 bool TraverseTemplateName(TemplateName Template) {
87 if (TemplateTemplateParmDecl *TTP
88 = dyn_cast_or_null<TemplateTemplateParmDecl>(
89 Template.getAsTemplateDecl()))
90 if (TTP->isParameterPack())
91 Unexpanded.push_back(std::make_pair(TTP, SourceLocation()));
92
93 return inherited::TraverseTemplateName(Template);
94 }
Douglas Gregor9ef75892010-12-15 19:43:21 +000095
Ted Kremenekebcb57a2012-03-06 20:05:56 +000096 /// \brief Suppress traversal into Objective-C container literal
97 /// elements that are pack expansions.
98 bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
99 if (!E->containsUnexpandedParameterPack())
100 return true;
101
102 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
103 ObjCDictionaryElement Element = E->getKeyValueElement(I);
104 if (Element.isPackExpansion())
105 continue;
106
107 TraverseStmt(Element.Key);
108 TraverseStmt(Element.Value);
109 }
110 return true;
111 }
Douglas Gregor9ef75892010-12-15 19:43:21 +0000112 //------------------------------------------------------------------------
113 // Pruning the search for unexpanded parameter packs.
114 //------------------------------------------------------------------------
115
116 /// \brief Suppress traversal into statements and expressions that
117 /// do not contain unexpanded parameter packs.
118 bool TraverseStmt(Stmt *S) {
119 if (Expr *E = dyn_cast_or_null<Expr>(S))
120 if (E->containsUnexpandedParameterPack())
121 return inherited::TraverseStmt(E);
122
123 return true;
124 }
125
126 /// \brief Suppress traversal into types that do not contain
127 /// unexpanded parameter packs.
128 bool TraverseType(QualType T) {
129 if (!T.isNull() && T->containsUnexpandedParameterPack())
130 return inherited::TraverseType(T);
131
132 return true;
133 }
134
135 /// \brief Suppress traversel into types with location information
136 /// that do not contain unexpanded parameter packs.
137 bool TraverseTypeLoc(TypeLoc TL) {
Douglas Gregor10738d32010-12-23 23:51:58 +0000138 if (!TL.getType().isNull() &&
139 TL.getType()->containsUnexpandedParameterPack())
Douglas Gregor9ef75892010-12-15 19:43:21 +0000140 return inherited::TraverseTypeLoc(TL);
141
142 return true;
143 }
144
Douglas Gregorcff163e2010-12-15 21:57:59 +0000145 /// \brief Suppress traversal of non-parameter declarations, since
146 /// they cannot contain unexpanded parameter packs.
147 bool TraverseDecl(Decl *D) {
148 if (D && isa<ParmVarDecl>(D))
149 return inherited::TraverseDecl(D);
150
151 return true;
152 }
Douglas Gregorba68eca2011-01-05 17:40:24 +0000153
154 /// \brief Suppress traversal of template argument pack expansions.
155 bool TraverseTemplateArgument(const TemplateArgument &Arg) {
156 if (Arg.isPackExpansion())
157 return true;
158
159 return inherited::TraverseTemplateArgument(Arg);
160 }
161
162 /// \brief Suppress traversal of template argument pack expansions.
163 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
164 if (ArgLoc.getArgument().isPackExpansion())
165 return true;
166
167 return inherited::TraverseTemplateArgumentLoc(ArgLoc);
168 }
Douglas Gregor9ef75892010-12-15 19:43:21 +0000169 };
170}
171
172/// \brief Diagnose all of the unexpanded parameter packs in the given
173/// vector.
Douglas Gregor65019ac2011-10-25 03:44:56 +0000174void
175Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
176 UnexpandedParameterPackContext UPPC,
Bill Wendling4fe5be02012-02-22 09:38:11 +0000177 ArrayRef<UnexpandedParameterPack> Unexpanded) {
Douglas Gregor65019ac2011-10-25 03:44:56 +0000178 if (Unexpanded.empty())
179 return;
180
Chris Lattner5f9e2722011-07-23 10:55:15 +0000181 SmallVector<SourceLocation, 4> Locations;
182 SmallVector<IdentifierInfo *, 4> Names;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000183 llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
184
185 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
186 IdentifierInfo *Name = 0;
187 if (const TemplateTypeParmType *TTP
188 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
Chandler Carruthb7efff42011-05-01 01:05:51 +0000189 Name = TTP->getIdentifier();
Douglas Gregor9ef75892010-12-15 19:43:21 +0000190 else
191 Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
192
193 if (Name && NamesKnown.insert(Name))
194 Names.push_back(Name);
195
196 if (Unexpanded[I].second.isValid())
197 Locations.push_back(Unexpanded[I].second);
198 }
199
200 DiagnosticBuilder DB
Douglas Gregor65019ac2011-10-25 03:44:56 +0000201 = Names.size() == 0? Diag(Loc, diag::err_unexpanded_parameter_pack_0)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000202 << (int)UPPC
Douglas Gregor65019ac2011-10-25 03:44:56 +0000203 : Names.size() == 1? Diag(Loc, diag::err_unexpanded_parameter_pack_1)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000204 << (int)UPPC << Names[0]
Douglas Gregor65019ac2011-10-25 03:44:56 +0000205 : Names.size() == 2? Diag(Loc, diag::err_unexpanded_parameter_pack_2)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000206 << (int)UPPC << Names[0] << Names[1]
Douglas Gregor65019ac2011-10-25 03:44:56 +0000207 : Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000208 << (int)UPPC << Names[0] << Names[1];
209
210 for (unsigned I = 0, N = Locations.size(); I != N; ++I)
211 DB << SourceRange(Locations[I]);
212}
213
Douglas Gregorc4633352010-12-15 17:38:57 +0000214bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
215 TypeSourceInfo *T,
216 UnexpandedParameterPackContext UPPC) {
217 // C++0x [temp.variadic]p5:
218 // An appearance of a name of a parameter pack that is not expanded is
219 // ill-formed.
220 if (!T->getType()->containsUnexpandedParameterPack())
221 return false;
222
Chris Lattner5f9e2722011-07-23 10:55:15 +0000223 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000224 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
225 T->getTypeLoc());
226 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000227 DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000228 return true;
229}
230
231bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
Douglas Gregor56c04582010-12-16 00:46:58 +0000232 UnexpandedParameterPackContext UPPC) {
Douglas Gregorc4633352010-12-15 17:38:57 +0000233 // C++0x [temp.variadic]p5:
234 // An appearance of a name of a parameter pack that is not expanded is
235 // ill-formed.
236 if (!E->containsUnexpandedParameterPack())
237 return false;
238
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000240 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
241 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000242 DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000243 return true;
244}
Douglas Gregor56c04582010-12-16 00:46:58 +0000245
246bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
247 UnexpandedParameterPackContext UPPC) {
248 // C++0x [temp.variadic]p5:
249 // An appearance of a name of a parameter pack that is not expanded is
250 // ill-formed.
251 if (!SS.getScopeRep() ||
252 !SS.getScopeRep()->containsUnexpandedParameterPack())
253 return false;
254
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000256 CollectUnexpandedParameterPacksVisitor(Unexpanded)
257 .TraverseNestedNameSpecifier(SS.getScopeRep());
258 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000259 DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
Douglas Gregor56c04582010-12-16 00:46:58 +0000260 UPPC, Unexpanded);
261 return true;
262}
263
264bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
265 UnexpandedParameterPackContext UPPC) {
266 // C++0x [temp.variadic]p5:
267 // An appearance of a name of a parameter pack that is not expanded is
268 // ill-formed.
269 switch (NameInfo.getName().getNameKind()) {
270 case DeclarationName::Identifier:
271 case DeclarationName::ObjCZeroArgSelector:
272 case DeclarationName::ObjCOneArgSelector:
273 case DeclarationName::ObjCMultiArgSelector:
274 case DeclarationName::CXXOperatorName:
275 case DeclarationName::CXXLiteralOperatorName:
276 case DeclarationName::CXXUsingDirective:
277 return false;
278
279 case DeclarationName::CXXConstructorName:
280 case DeclarationName::CXXDestructorName:
281 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor099ffe82010-12-16 17:19:19 +0000282 // FIXME: We shouldn't need this null check!
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000283 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
284 return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
285
286 if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
Douglas Gregor56c04582010-12-16 00:46:58 +0000287 return false;
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000288
Douglas Gregor56c04582010-12-16 00:46:58 +0000289 break;
290 }
291
Chris Lattner5f9e2722011-07-23 10:55:15 +0000292 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000293 CollectUnexpandedParameterPacksVisitor(Unexpanded)
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000294 .TraverseType(NameInfo.getName().getCXXNameType());
Douglas Gregor56c04582010-12-16 00:46:58 +0000295 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000296 DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
Douglas Gregor56c04582010-12-16 00:46:58 +0000297 return true;
298}
Douglas Gregor6f526752010-12-16 08:48:57 +0000299
300bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
301 TemplateName Template,
302 UnexpandedParameterPackContext UPPC) {
303
304 if (Template.isNull() || !Template.containsUnexpandedParameterPack())
305 return false;
306
Chris Lattner5f9e2722011-07-23 10:55:15 +0000307 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6f526752010-12-16 08:48:57 +0000308 CollectUnexpandedParameterPacksVisitor(Unexpanded)
309 .TraverseTemplateName(Template);
310 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000311 DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregor6f526752010-12-16 08:48:57 +0000312 return true;
313}
314
Douglas Gregor925910d2011-01-03 20:35:03 +0000315bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
316 UnexpandedParameterPackContext UPPC) {
317 if (Arg.getArgument().isNull() ||
318 !Arg.getArgument().containsUnexpandedParameterPack())
319 return false;
320
Chris Lattner5f9e2722011-07-23 10:55:15 +0000321 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor925910d2011-01-03 20:35:03 +0000322 CollectUnexpandedParameterPacksVisitor(Unexpanded)
323 .TraverseTemplateArgumentLoc(Arg);
324 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000325 DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
Douglas Gregor925910d2011-01-03 20:35:03 +0000326 return true;
327}
328
Douglas Gregore02e2622010-12-22 21:19:48 +0000329void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregore02e2622010-12-22 21:19:48 +0000331 CollectUnexpandedParameterPacksVisitor(Unexpanded)
332 .TraverseTemplateArgument(Arg);
333}
334
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000335void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000336 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000337 CollectUnexpandedParameterPacksVisitor(Unexpanded)
338 .TraverseTemplateArgumentLoc(Arg);
339}
340
Douglas Gregorb99268b2010-12-21 00:52:54 +0000341void Sema::collectUnexpandedParameterPacks(QualType T,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000342 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000343 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
344}
345
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000346void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000348 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
349}
350
Douglas Gregor65019ac2011-10-25 03:44:56 +0000351void Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS,
352 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
353 NestedNameSpecifier *Qualifier = SS.getScopeRep();
354 if (!Qualifier)
355 return;
356
357 NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data());
358 CollectUnexpandedParameterPacksVisitor(Unexpanded)
359 .TraverseNestedNameSpecifierLoc(QualifierLoc);
360}
361
362void Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo,
363 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
364 CollectUnexpandedParameterPacksVisitor(Unexpanded)
365 .TraverseDeclarationNameInfo(NameInfo);
366}
367
368
Douglas Gregor7536dd52010-12-20 02:24:11 +0000369ParsedTemplateArgument
370Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
371 SourceLocation EllipsisLoc) {
372 if (Arg.isInvalid())
373 return Arg;
374
375 switch (Arg.getKind()) {
376 case ParsedTemplateArgument::Type: {
377 TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
378 if (Result.isInvalid())
379 return ParsedTemplateArgument();
380
381 return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
382 Arg.getLocation());
383 }
384
Douglas Gregorbe230c32011-01-03 17:17:50 +0000385 case ParsedTemplateArgument::NonType: {
386 ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
387 if (Result.isInvalid())
388 return ParsedTemplateArgument();
389
390 return ParsedTemplateArgument(Arg.getKind(), Result.get(),
391 Arg.getLocation());
392 }
393
Douglas Gregor7536dd52010-12-20 02:24:11 +0000394 case ParsedTemplateArgument::Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000395 if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
396 SourceRange R(Arg.getLocation());
397 if (Arg.getScopeSpec().isValid())
398 R.setBegin(Arg.getScopeSpec().getBeginLoc());
399 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
400 << R;
401 return ParsedTemplateArgument();
402 }
403
404 return Arg.getTemplatePackExpansion(EllipsisLoc);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000405 }
406 llvm_unreachable("Unhandled template argument kind?");
Douglas Gregor7536dd52010-12-20 02:24:11 +0000407}
408
409TypeResult Sema::ActOnPackExpansion(ParsedType Type,
410 SourceLocation EllipsisLoc) {
411 TypeSourceInfo *TSInfo;
412 GetTypeFromParser(Type, &TSInfo);
413 if (!TSInfo)
414 return true;
415
Douglas Gregorcded4f62011-01-14 17:04:44 +0000416 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc,
417 llvm::Optional<unsigned>());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000418 if (!TSResult)
419 return true;
420
421 return CreateParsedType(TSResult->getType(), TSResult);
422}
423
424TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000425 SourceLocation EllipsisLoc,
426 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor7536dd52010-12-20 02:24:11 +0000427 // Create the pack expansion type and source-location information.
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000428 QualType Result = CheckPackExpansion(Pattern->getType(),
429 Pattern->getTypeLoc().getSourceRange(),
Douglas Gregorcded4f62011-01-14 17:04:44 +0000430 EllipsisLoc, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000431 if (Result.isNull())
432 return 0;
433
Douglas Gregor7536dd52010-12-20 02:24:11 +0000434 TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result);
435 PackExpansionTypeLoc TL = cast<PackExpansionTypeLoc>(TSResult->getTypeLoc());
436 TL.setEllipsisLoc(EllipsisLoc);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000437
Douglas Gregor7536dd52010-12-20 02:24:11 +0000438 // Copy over the source-location information from the type.
439 memcpy(TL.getNextTypeLoc().getOpaqueData(),
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000440 Pattern->getTypeLoc().getOpaqueData(),
441 Pattern->getTypeLoc().getFullDataSize());
442 return TSResult;
Douglas Gregor7536dd52010-12-20 02:24:11 +0000443}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000444
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000445QualType Sema::CheckPackExpansion(QualType Pattern,
446 SourceRange PatternRange,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000447 SourceLocation EllipsisLoc,
448 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000449 // C++0x [temp.variadic]p5:
450 // The pattern of a pack expansion shall name one or more
451 // parameter packs that are not expanded by a nested pack
452 // expansion.
453 if (!Pattern->containsUnexpandedParameterPack()) {
454 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
455 << PatternRange;
456 return QualType();
457 }
458
Douglas Gregorcded4f62011-01-14 17:04:44 +0000459 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000460}
461
Douglas Gregorbe230c32011-01-03 17:17:50 +0000462ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
Douglas Gregor67fd1252011-01-14 21:20:45 +0000463 return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>());
464}
465
466ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
467 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregorbe230c32011-01-03 17:17:50 +0000468 if (!Pattern)
469 return ExprError();
470
471 // C++0x [temp.variadic]p5:
472 // The pattern of a pack expansion shall name one or more
473 // parameter packs that are not expanded by a nested pack
474 // expansion.
475 if (!Pattern->containsUnexpandedParameterPack()) {
476 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
477 << Pattern->getSourceRange();
478 return ExprError();
479 }
480
481 // Create the pack expansion expression and source-location information.
482 return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
Douglas Gregor67fd1252011-01-14 21:20:45 +0000483 EllipsisLoc, NumExpansions));
Douglas Gregorbe230c32011-01-03 17:17:50 +0000484}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000485
Douglas Gregord3731192011-01-10 07:32:04 +0000486/// \brief Retrieve the depth and index of a parameter pack.
487static std::pair<unsigned, unsigned>
488getDepthAndIndex(NamedDecl *ND) {
489 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
490 return std::make_pair(TTP->getDepth(), TTP->getIndex());
491
492 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
493 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
494
495 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
496 return std::make_pair(TTP->getDepth(), TTP->getIndex());
497}
498
Douglas Gregorb99268b2010-12-21 00:52:54 +0000499bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,
500 SourceRange PatternRange,
David Blaikiea71f9d02011-09-22 02:34:54 +0000501 ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000502 const MultiLevelTemplateArgumentList &TemplateArgs,
503 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000504 bool &RetainExpansion,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000505 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000506 ShouldExpand = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000507 RetainExpansion = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000508 std::pair<IdentifierInfo *, SourceLocation> FirstPack;
509 bool HaveFirstPack = false;
510
David Blaikiea71f9d02011-09-22 02:34:54 +0000511 for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
512 end = Unexpanded.end();
513 i != end; ++i) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000514 // Compute the depth and index for this parameter pack.
Ted Kremenek9577abc2011-01-23 17:04:59 +0000515 unsigned Depth = 0, Index = 0;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000516 IdentifierInfo *Name;
Douglas Gregor12c9c002011-01-07 16:43:16 +0000517 bool IsFunctionParameterPack = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000518
519 if (const TemplateTypeParmType *TTP
David Blaikiea71f9d02011-09-22 02:34:54 +0000520 = i->first.dyn_cast<const TemplateTypeParmType *>()) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000521 Depth = TTP->getDepth();
522 Index = TTP->getIndex();
Chandler Carruthb7efff42011-05-01 01:05:51 +0000523 Name = TTP->getIdentifier();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000524 } else {
David Blaikiea71f9d02011-09-22 02:34:54 +0000525 NamedDecl *ND = i->first.get<NamedDecl *>();
Douglas Gregord3731192011-01-10 07:32:04 +0000526 if (isa<ParmVarDecl>(ND))
Douglas Gregor12c9c002011-01-07 16:43:16 +0000527 IsFunctionParameterPack = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000528 else
529 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
530
Douglas Gregorb99268b2010-12-21 00:52:54 +0000531 Name = ND->getIdentifier();
532 }
533
Douglas Gregor12c9c002011-01-07 16:43:16 +0000534 // Determine the size of this argument pack.
535 unsigned NewPackSize;
536 if (IsFunctionParameterPack) {
537 // Figure out whether we're instantiating to an argument pack or not.
538 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
539
540 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
541 = CurrentInstantiationScope->findInstantiationOf(
David Blaikiea71f9d02011-09-22 02:34:54 +0000542 i->first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000543 if (Instantiation->is<DeclArgumentPack *>()) {
Douglas Gregor12c9c002011-01-07 16:43:16 +0000544 // We could expand this function parameter pack.
545 NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
546 } else {
547 // We can't expand this function parameter pack, so we can't expand
548 // the pack expansion.
549 ShouldExpand = false;
550 continue;
551 }
552 } else {
553 // If we don't have a template argument at this depth/index, then we
554 // cannot expand the pack expansion. Make a note of this, but we still
555 // want to check any parameter packs we *do* have arguments for.
556 if (Depth >= TemplateArgs.getNumLevels() ||
557 !TemplateArgs.hasTemplateArgument(Depth, Index)) {
558 ShouldExpand = false;
559 continue;
560 }
561
562 // Determine the size of the argument pack.
563 NewPackSize = TemplateArgs(Depth, Index).pack_size();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000564 }
565
Douglas Gregord3731192011-01-10 07:32:04 +0000566 // C++0x [temp.arg.explicit]p9:
567 // Template argument deduction can extend the sequence of template
568 // arguments corresponding to a template parameter pack, even when the
569 // sequence contains explicitly specified template arguments.
Douglas Gregor8619edd2011-01-20 23:15:49 +0000570 if (!IsFunctionParameterPack) {
571 if (NamedDecl *PartialPack
572 = CurrentInstantiationScope->getPartiallySubstitutedPack()){
573 unsigned PartialDepth, PartialIndex;
574 llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
575 if (PartialDepth == Depth && PartialIndex == Index)
576 RetainExpansion = true;
577 }
Douglas Gregord3731192011-01-10 07:32:04 +0000578 }
Douglas Gregor8619edd2011-01-20 23:15:49 +0000579
Douglas Gregorcded4f62011-01-14 17:04:44 +0000580 if (!NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000581 // The is the first pack we've seen for which we have an argument.
582 // Record it.
583 NumExpansions = NewPackSize;
584 FirstPack.first = Name;
David Blaikiea71f9d02011-09-22 02:34:54 +0000585 FirstPack.second = i->second;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000586 HaveFirstPack = true;
587 continue;
588 }
589
Douglas Gregorcded4f62011-01-14 17:04:44 +0000590 if (NewPackSize != *NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000591 // C++0x [temp.variadic]p5:
592 // All of the parameter packs expanded by a pack expansion shall have
593 // the same number of arguments specified.
Douglas Gregorcded4f62011-01-14 17:04:44 +0000594 if (HaveFirstPack)
595 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
596 << FirstPack.first << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000597 << SourceRange(FirstPack.second) << SourceRange(i->second);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000598 else
599 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
600 << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000601 << SourceRange(i->second);
Douglas Gregorb99268b2010-12-21 00:52:54 +0000602 return true;
603 }
604 }
605
606 return false;
607}
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000608
Douglas Gregor21371ea2011-01-11 03:14:20 +0000609unsigned Sema::getNumArgumentsInExpansion(QualType T,
610 const MultiLevelTemplateArgumentList &TemplateArgs) {
611 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000612 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000613 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
614
615 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
616 // Compute the depth and index for this parameter pack.
617 unsigned Depth;
618 unsigned Index;
619
620 if (const TemplateTypeParmType *TTP
621 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
622 Depth = TTP->getDepth();
623 Index = TTP->getIndex();
624 } else {
625 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
626 if (isa<ParmVarDecl>(ND)) {
627 // Function parameter pack.
628 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
629
630 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
631 = CurrentInstantiationScope->findInstantiationOf(
632 Unexpanded[I].first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000633 if (Instantiation->is<DeclArgumentPack *>())
Douglas Gregor21371ea2011-01-11 03:14:20 +0000634 return Instantiation->get<DeclArgumentPack *>()->size();
635
636 continue;
637 }
638
639 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
640 }
641 if (Depth >= TemplateArgs.getNumLevels() ||
642 !TemplateArgs.hasTemplateArgument(Depth, Index))
643 continue;
644
645 // Determine the size of the argument pack.
646 return TemplateArgs(Depth, Index).pack_size();
647 }
648
649 llvm_unreachable("No unexpanded parameter packs in type expansion.");
Douglas Gregor21371ea2011-01-11 03:14:20 +0000650}
651
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000652bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
653 const DeclSpec &DS = D.getDeclSpec();
654 switch (DS.getTypeSpecType()) {
655 case TST_typename:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000656 case TST_typeofType:
Eli Friedmanb001de72011-10-06 23:00:33 +0000657 case TST_underlyingType:
658 case TST_atomic: {
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000659 QualType T = DS.getRepAsType().get();
660 if (!T.isNull() && T->containsUnexpandedParameterPack())
661 return true;
662 break;
663 }
664
665 case TST_typeofExpr:
666 case TST_decltype:
667 if (DS.getRepAsExpr() &&
668 DS.getRepAsExpr()->containsUnexpandedParameterPack())
669 return true;
670 break;
671
672 case TST_unspecified:
673 case TST_void:
674 case TST_char:
675 case TST_wchar:
676 case TST_char16:
677 case TST_char32:
678 case TST_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000679 case TST_half:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000680 case TST_float:
681 case TST_double:
682 case TST_bool:
683 case TST_decimal32:
684 case TST_decimal64:
685 case TST_decimal128:
686 case TST_enum:
687 case TST_union:
688 case TST_struct:
689 case TST_class:
690 case TST_auto:
John McCalla5fc4722011-04-09 22:50:59 +0000691 case TST_unknown_anytype:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000692 case TST_error:
693 break;
694 }
695
696 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
697 const DeclaratorChunk &Chunk = D.getTypeObject(I);
698 switch (Chunk.Kind) {
699 case DeclaratorChunk::Pointer:
700 case DeclaratorChunk::Reference:
701 case DeclaratorChunk::Paren:
702 // These declarator chunks cannot contain any parameter packs.
703 break;
704
705 case DeclaratorChunk::Array:
706 case DeclaratorChunk::Function:
707 case DeclaratorChunk::BlockPointer:
708 // Syntactically, these kinds of declarator chunks all come after the
709 // declarator-id (conceptually), so the parser should not invoke this
710 // routine at this time.
711 llvm_unreachable("Could not have seen this kind of declarator chunk");
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000712
713 case DeclaratorChunk::MemberPointer:
714 if (Chunk.Mem.Scope().getScopeRep() &&
715 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
716 return true;
717 break;
718 }
719 }
720
721 return false;
722}
Douglas Gregoree8aff02011-01-04 17:33:58 +0000723
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000724namespace {
725
726// Callback to only accept typo corrections that refer to parameter packs.
727class ParameterPackValidatorCCC : public CorrectionCandidateCallback {
728 public:
729 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
730 NamedDecl *ND = candidate.getCorrectionDecl();
731 return ND && ND->isParameterPack();
732 }
733};
734
735}
736
Douglas Gregoree8aff02011-01-04 17:33:58 +0000737/// \brief Called when an expression computing the size of a parameter pack
738/// is parsed.
739///
740/// \code
741/// template<typename ...Types> struct count {
742/// static const unsigned value = sizeof...(Types);
743/// };
744/// \endcode
745///
746//
747/// \param OpLoc The location of the "sizeof" keyword.
748/// \param Name The name of the parameter pack whose size will be determined.
749/// \param NameLoc The source location of the name of the parameter pack.
750/// \param RParenLoc The location of the closing parentheses.
751ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
752 SourceLocation OpLoc,
753 IdentifierInfo &Name,
754 SourceLocation NameLoc,
755 SourceLocation RParenLoc) {
756 // C++0x [expr.sizeof]p5:
757 // The identifier in a sizeof... expression shall name a parameter pack.
Douglas Gregoree8aff02011-01-04 17:33:58 +0000758 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
759 LookupName(R, S);
760
761 NamedDecl *ParameterPack = 0;
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000762 ParameterPackValidatorCCC Validator;
Douglas Gregoree8aff02011-01-04 17:33:58 +0000763 switch (R.getResultKind()) {
764 case LookupResult::Found:
765 ParameterPack = R.getFoundDecl();
766 break;
767
768 case LookupResult::NotFound:
769 case LookupResult::NotFoundInCurrentInstantiation:
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000770 if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000771 R.getLookupKind(), S, 0,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000772 Validator)) {
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000773 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
774 ParameterPack = Corrected.getCorrectionDecl();
775 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name_suggest)
776 << &Name << CorrectedQuotedStr
777 << FixItHint::CreateReplacement(
778 NameLoc, Corrected.getAsString(getLangOptions()));
779 Diag(ParameterPack->getLocation(), diag::note_parameter_pack_here)
780 << CorrectedQuotedStr;
Douglas Gregoree8aff02011-01-04 17:33:58 +0000781 }
782
783 case LookupResult::FoundOverloaded:
784 case LookupResult::FoundUnresolvedValue:
785 break;
786
787 case LookupResult::Ambiguous:
788 DiagnoseAmbiguousLookup(R);
789 return ExprError();
790 }
791
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000792 if (!ParameterPack || !ParameterPack->isParameterPack()) {
Douglas Gregoree8aff02011-01-04 17:33:58 +0000793 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
794 << &Name;
795 return ExprError();
796 }
797
Eli Friedman88530d52012-03-01 21:32:56 +0000798 MarkAnyDeclReferenced(OpLoc, ParameterPack);
799
Douglas Gregoree8aff02011-01-04 17:33:58 +0000800 return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
801 ParameterPack, NameLoc, RParenLoc);
802}