blob: 972f2236bd4f9004eb5c3122bd901d5c8a431823 [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
Douglas Gregor9ef75892010-12-15 19:43:21 +000096 //------------------------------------------------------------------------
97 // Pruning the search for unexpanded parameter packs.
98 //------------------------------------------------------------------------
99
100 /// \brief Suppress traversal into statements and expressions that
101 /// do not contain unexpanded parameter packs.
102 bool TraverseStmt(Stmt *S) {
103 if (Expr *E = dyn_cast_or_null<Expr>(S))
104 if (E->containsUnexpandedParameterPack())
105 return inherited::TraverseStmt(E);
106
107 return true;
108 }
109
110 /// \brief Suppress traversal into types that do not contain
111 /// unexpanded parameter packs.
112 bool TraverseType(QualType T) {
113 if (!T.isNull() && T->containsUnexpandedParameterPack())
114 return inherited::TraverseType(T);
115
116 return true;
117 }
118
119 /// \brief Suppress traversel into types with location information
120 /// that do not contain unexpanded parameter packs.
121 bool TraverseTypeLoc(TypeLoc TL) {
Douglas Gregor10738d32010-12-23 23:51:58 +0000122 if (!TL.getType().isNull() &&
123 TL.getType()->containsUnexpandedParameterPack())
Douglas Gregor9ef75892010-12-15 19:43:21 +0000124 return inherited::TraverseTypeLoc(TL);
125
126 return true;
127 }
128
Douglas Gregorcff163e2010-12-15 21:57:59 +0000129 /// \brief Suppress traversal of non-parameter declarations, since
130 /// they cannot contain unexpanded parameter packs.
131 bool TraverseDecl(Decl *D) {
132 if (D && isa<ParmVarDecl>(D))
133 return inherited::TraverseDecl(D);
134
135 return true;
136 }
Douglas Gregorba68eca2011-01-05 17:40:24 +0000137
138 /// \brief Suppress traversal of template argument pack expansions.
139 bool TraverseTemplateArgument(const TemplateArgument &Arg) {
140 if (Arg.isPackExpansion())
141 return true;
142
143 return inherited::TraverseTemplateArgument(Arg);
144 }
145
146 /// \brief Suppress traversal of template argument pack expansions.
147 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
148 if (ArgLoc.getArgument().isPackExpansion())
149 return true;
150
151 return inherited::TraverseTemplateArgumentLoc(ArgLoc);
152 }
Douglas Gregor9ef75892010-12-15 19:43:21 +0000153 };
154}
155
156/// \brief Diagnose all of the unexpanded parameter packs in the given
157/// vector.
Douglas Gregor65019ac2011-10-25 03:44:56 +0000158void
159Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
160 UnexpandedParameterPackContext UPPC,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000161 const SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor65019ac2011-10-25 03:44:56 +0000162 if (Unexpanded.empty())
163 return;
164
Chris Lattner5f9e2722011-07-23 10:55:15 +0000165 SmallVector<SourceLocation, 4> Locations;
166 SmallVector<IdentifierInfo *, 4> Names;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000167 llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
168
169 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
170 IdentifierInfo *Name = 0;
171 if (const TemplateTypeParmType *TTP
172 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
Chandler Carruthb7efff42011-05-01 01:05:51 +0000173 Name = TTP->getIdentifier();
Douglas Gregor9ef75892010-12-15 19:43:21 +0000174 else
175 Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
176
177 if (Name && NamesKnown.insert(Name))
178 Names.push_back(Name);
179
180 if (Unexpanded[I].second.isValid())
181 Locations.push_back(Unexpanded[I].second);
182 }
183
184 DiagnosticBuilder DB
Douglas Gregor65019ac2011-10-25 03:44:56 +0000185 = Names.size() == 0? Diag(Loc, diag::err_unexpanded_parameter_pack_0)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000186 << (int)UPPC
Douglas Gregor65019ac2011-10-25 03:44:56 +0000187 : Names.size() == 1? Diag(Loc, diag::err_unexpanded_parameter_pack_1)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000188 << (int)UPPC << Names[0]
Douglas Gregor65019ac2011-10-25 03:44:56 +0000189 : Names.size() == 2? Diag(Loc, diag::err_unexpanded_parameter_pack_2)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000190 << (int)UPPC << Names[0] << Names[1]
Douglas Gregor65019ac2011-10-25 03:44:56 +0000191 : Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000192 << (int)UPPC << Names[0] << Names[1];
193
194 for (unsigned I = 0, N = Locations.size(); I != N; ++I)
195 DB << SourceRange(Locations[I]);
196}
197
Douglas Gregorc4633352010-12-15 17:38:57 +0000198bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
199 TypeSourceInfo *T,
200 UnexpandedParameterPackContext UPPC) {
201 // C++0x [temp.variadic]p5:
202 // An appearance of a name of a parameter pack that is not expanded is
203 // ill-formed.
204 if (!T->getType()->containsUnexpandedParameterPack())
205 return false;
206
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000208 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
209 T->getTypeLoc());
210 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000211 DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000212 return true;
213}
214
215bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
Douglas Gregor56c04582010-12-16 00:46:58 +0000216 UnexpandedParameterPackContext UPPC) {
Douglas Gregorc4633352010-12-15 17:38:57 +0000217 // 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 (!E->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).TraverseStmt(E);
225 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000226 DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000227 return true;
228}
Douglas Gregor56c04582010-12-16 00:46:58 +0000229
230bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
231 UnexpandedParameterPackContext UPPC) {
232 // C++0x [temp.variadic]p5:
233 // An appearance of a name of a parameter pack that is not expanded is
234 // ill-formed.
235 if (!SS.getScopeRep() ||
236 !SS.getScopeRep()->containsUnexpandedParameterPack())
237 return false;
238
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000240 CollectUnexpandedParameterPacksVisitor(Unexpanded)
241 .TraverseNestedNameSpecifier(SS.getScopeRep());
242 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000243 DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
Douglas Gregor56c04582010-12-16 00:46:58 +0000244 UPPC, Unexpanded);
245 return true;
246}
247
248bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
249 UnexpandedParameterPackContext UPPC) {
250 // C++0x [temp.variadic]p5:
251 // An appearance of a name of a parameter pack that is not expanded is
252 // ill-formed.
253 switch (NameInfo.getName().getNameKind()) {
254 case DeclarationName::Identifier:
255 case DeclarationName::ObjCZeroArgSelector:
256 case DeclarationName::ObjCOneArgSelector:
257 case DeclarationName::ObjCMultiArgSelector:
258 case DeclarationName::CXXOperatorName:
259 case DeclarationName::CXXLiteralOperatorName:
260 case DeclarationName::CXXUsingDirective:
261 return false;
262
263 case DeclarationName::CXXConstructorName:
264 case DeclarationName::CXXDestructorName:
265 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor099ffe82010-12-16 17:19:19 +0000266 // FIXME: We shouldn't need this null check!
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000267 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
268 return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
269
270 if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
Douglas Gregor56c04582010-12-16 00:46:58 +0000271 return false;
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000272
Douglas Gregor56c04582010-12-16 00:46:58 +0000273 break;
274 }
275
Chris Lattner5f9e2722011-07-23 10:55:15 +0000276 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000277 CollectUnexpandedParameterPacksVisitor(Unexpanded)
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000278 .TraverseType(NameInfo.getName().getCXXNameType());
Douglas Gregor56c04582010-12-16 00:46:58 +0000279 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000280 DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
Douglas Gregor56c04582010-12-16 00:46:58 +0000281 return true;
282}
Douglas Gregor6f526752010-12-16 08:48:57 +0000283
284bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
285 TemplateName Template,
286 UnexpandedParameterPackContext UPPC) {
287
288 if (Template.isNull() || !Template.containsUnexpandedParameterPack())
289 return false;
290
Chris Lattner5f9e2722011-07-23 10:55:15 +0000291 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6f526752010-12-16 08:48:57 +0000292 CollectUnexpandedParameterPacksVisitor(Unexpanded)
293 .TraverseTemplateName(Template);
294 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000295 DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregor6f526752010-12-16 08:48:57 +0000296 return true;
297}
298
Douglas Gregor925910d2011-01-03 20:35:03 +0000299bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
300 UnexpandedParameterPackContext UPPC) {
301 if (Arg.getArgument().isNull() ||
302 !Arg.getArgument().containsUnexpandedParameterPack())
303 return false;
304
Chris Lattner5f9e2722011-07-23 10:55:15 +0000305 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor925910d2011-01-03 20:35:03 +0000306 CollectUnexpandedParameterPacksVisitor(Unexpanded)
307 .TraverseTemplateArgumentLoc(Arg);
308 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Douglas Gregor65019ac2011-10-25 03:44:56 +0000309 DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
Douglas Gregor925910d2011-01-03 20:35:03 +0000310 return true;
311}
312
Douglas Gregore02e2622010-12-22 21:19:48 +0000313void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000314 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregore02e2622010-12-22 21:19:48 +0000315 CollectUnexpandedParameterPacksVisitor(Unexpanded)
316 .TraverseTemplateArgument(Arg);
317}
318
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000319void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000320 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000321 CollectUnexpandedParameterPacksVisitor(Unexpanded)
322 .TraverseTemplateArgumentLoc(Arg);
323}
324
Douglas Gregorb99268b2010-12-21 00:52:54 +0000325void Sema::collectUnexpandedParameterPacks(QualType T,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000326 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000327 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
328}
329
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000330void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000331 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000332 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
333}
334
Douglas Gregor65019ac2011-10-25 03:44:56 +0000335void Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS,
336 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
337 NestedNameSpecifier *Qualifier = SS.getScopeRep();
338 if (!Qualifier)
339 return;
340
341 NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data());
342 CollectUnexpandedParameterPacksVisitor(Unexpanded)
343 .TraverseNestedNameSpecifierLoc(QualifierLoc);
344}
345
346void Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo,
347 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
348 CollectUnexpandedParameterPacksVisitor(Unexpanded)
349 .TraverseDeclarationNameInfo(NameInfo);
350}
351
352
Douglas Gregor7536dd52010-12-20 02:24:11 +0000353ParsedTemplateArgument
354Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
355 SourceLocation EllipsisLoc) {
356 if (Arg.isInvalid())
357 return Arg;
358
359 switch (Arg.getKind()) {
360 case ParsedTemplateArgument::Type: {
361 TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
362 if (Result.isInvalid())
363 return ParsedTemplateArgument();
364
365 return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
366 Arg.getLocation());
367 }
368
Douglas Gregorbe230c32011-01-03 17:17:50 +0000369 case ParsedTemplateArgument::NonType: {
370 ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
371 if (Result.isInvalid())
372 return ParsedTemplateArgument();
373
374 return ParsedTemplateArgument(Arg.getKind(), Result.get(),
375 Arg.getLocation());
376 }
377
Douglas Gregor7536dd52010-12-20 02:24:11 +0000378 case ParsedTemplateArgument::Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000379 if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
380 SourceRange R(Arg.getLocation());
381 if (Arg.getScopeSpec().isValid())
382 R.setBegin(Arg.getScopeSpec().getBeginLoc());
383 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
384 << R;
385 return ParsedTemplateArgument();
386 }
387
388 return Arg.getTemplatePackExpansion(EllipsisLoc);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000389 }
390 llvm_unreachable("Unhandled template argument kind?");
Douglas Gregor7536dd52010-12-20 02:24:11 +0000391}
392
393TypeResult Sema::ActOnPackExpansion(ParsedType Type,
394 SourceLocation EllipsisLoc) {
395 TypeSourceInfo *TSInfo;
396 GetTypeFromParser(Type, &TSInfo);
397 if (!TSInfo)
398 return true;
399
Douglas Gregorcded4f62011-01-14 17:04:44 +0000400 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc,
401 llvm::Optional<unsigned>());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000402 if (!TSResult)
403 return true;
404
405 return CreateParsedType(TSResult->getType(), TSResult);
406}
407
408TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000409 SourceLocation EllipsisLoc,
410 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor7536dd52010-12-20 02:24:11 +0000411 // Create the pack expansion type and source-location information.
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000412 QualType Result = CheckPackExpansion(Pattern->getType(),
413 Pattern->getTypeLoc().getSourceRange(),
Douglas Gregorcded4f62011-01-14 17:04:44 +0000414 EllipsisLoc, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000415 if (Result.isNull())
416 return 0;
417
Douglas Gregor7536dd52010-12-20 02:24:11 +0000418 TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result);
419 PackExpansionTypeLoc TL = cast<PackExpansionTypeLoc>(TSResult->getTypeLoc());
420 TL.setEllipsisLoc(EllipsisLoc);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000421
Douglas Gregor7536dd52010-12-20 02:24:11 +0000422 // Copy over the source-location information from the type.
423 memcpy(TL.getNextTypeLoc().getOpaqueData(),
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000424 Pattern->getTypeLoc().getOpaqueData(),
425 Pattern->getTypeLoc().getFullDataSize());
426 return TSResult;
Douglas Gregor7536dd52010-12-20 02:24:11 +0000427}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000428
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000429QualType Sema::CheckPackExpansion(QualType Pattern,
430 SourceRange PatternRange,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000431 SourceLocation EllipsisLoc,
432 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000433 // C++0x [temp.variadic]p5:
434 // The pattern of a pack expansion shall name one or more
435 // parameter packs that are not expanded by a nested pack
436 // expansion.
437 if (!Pattern->containsUnexpandedParameterPack()) {
438 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
439 << PatternRange;
440 return QualType();
441 }
442
Douglas Gregorcded4f62011-01-14 17:04:44 +0000443 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000444}
445
Douglas Gregorbe230c32011-01-03 17:17:50 +0000446ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
Douglas Gregor67fd1252011-01-14 21:20:45 +0000447 return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>());
448}
449
450ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
451 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregorbe230c32011-01-03 17:17:50 +0000452 if (!Pattern)
453 return ExprError();
454
455 // C++0x [temp.variadic]p5:
456 // The pattern of a pack expansion shall name one or more
457 // parameter packs that are not expanded by a nested pack
458 // expansion.
459 if (!Pattern->containsUnexpandedParameterPack()) {
460 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
461 << Pattern->getSourceRange();
462 return ExprError();
463 }
464
465 // Create the pack expansion expression and source-location information.
466 return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
Douglas Gregor67fd1252011-01-14 21:20:45 +0000467 EllipsisLoc, NumExpansions));
Douglas Gregorbe230c32011-01-03 17:17:50 +0000468}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000469
Douglas Gregord3731192011-01-10 07:32:04 +0000470/// \brief Retrieve the depth and index of a parameter pack.
471static std::pair<unsigned, unsigned>
472getDepthAndIndex(NamedDecl *ND) {
473 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
474 return std::make_pair(TTP->getDepth(), TTP->getIndex());
475
476 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
477 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
478
479 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
480 return std::make_pair(TTP->getDepth(), TTP->getIndex());
481}
482
Douglas Gregorb99268b2010-12-21 00:52:54 +0000483bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,
484 SourceRange PatternRange,
David Blaikiea71f9d02011-09-22 02:34:54 +0000485 ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000486 const MultiLevelTemplateArgumentList &TemplateArgs,
487 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000488 bool &RetainExpansion,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000489 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000490 ShouldExpand = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000491 RetainExpansion = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000492 std::pair<IdentifierInfo *, SourceLocation> FirstPack;
493 bool HaveFirstPack = false;
494
David Blaikiea71f9d02011-09-22 02:34:54 +0000495 for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
496 end = Unexpanded.end();
497 i != end; ++i) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000498 // Compute the depth and index for this parameter pack.
Ted Kremenek9577abc2011-01-23 17:04:59 +0000499 unsigned Depth = 0, Index = 0;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000500 IdentifierInfo *Name;
Douglas Gregor12c9c002011-01-07 16:43:16 +0000501 bool IsFunctionParameterPack = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000502
503 if (const TemplateTypeParmType *TTP
David Blaikiea71f9d02011-09-22 02:34:54 +0000504 = i->first.dyn_cast<const TemplateTypeParmType *>()) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000505 Depth = TTP->getDepth();
506 Index = TTP->getIndex();
Chandler Carruthb7efff42011-05-01 01:05:51 +0000507 Name = TTP->getIdentifier();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000508 } else {
David Blaikiea71f9d02011-09-22 02:34:54 +0000509 NamedDecl *ND = i->first.get<NamedDecl *>();
Douglas Gregord3731192011-01-10 07:32:04 +0000510 if (isa<ParmVarDecl>(ND))
Douglas Gregor12c9c002011-01-07 16:43:16 +0000511 IsFunctionParameterPack = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000512 else
513 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
514
Douglas Gregorb99268b2010-12-21 00:52:54 +0000515 Name = ND->getIdentifier();
516 }
517
Douglas Gregor12c9c002011-01-07 16:43:16 +0000518 // Determine the size of this argument pack.
519 unsigned NewPackSize;
520 if (IsFunctionParameterPack) {
521 // Figure out whether we're instantiating to an argument pack or not.
522 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
523
524 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
525 = CurrentInstantiationScope->findInstantiationOf(
David Blaikiea71f9d02011-09-22 02:34:54 +0000526 i->first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000527 if (Instantiation->is<DeclArgumentPack *>()) {
Douglas Gregor12c9c002011-01-07 16:43:16 +0000528 // We could expand this function parameter pack.
529 NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
530 } else {
531 // We can't expand this function parameter pack, so we can't expand
532 // the pack expansion.
533 ShouldExpand = false;
534 continue;
535 }
536 } else {
537 // If we don't have a template argument at this depth/index, then we
538 // cannot expand the pack expansion. Make a note of this, but we still
539 // want to check any parameter packs we *do* have arguments for.
540 if (Depth >= TemplateArgs.getNumLevels() ||
541 !TemplateArgs.hasTemplateArgument(Depth, Index)) {
542 ShouldExpand = false;
543 continue;
544 }
545
546 // Determine the size of the argument pack.
547 NewPackSize = TemplateArgs(Depth, Index).pack_size();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000548 }
549
Douglas Gregord3731192011-01-10 07:32:04 +0000550 // C++0x [temp.arg.explicit]p9:
551 // Template argument deduction can extend the sequence of template
552 // arguments corresponding to a template parameter pack, even when the
553 // sequence contains explicitly specified template arguments.
Douglas Gregor8619edd2011-01-20 23:15:49 +0000554 if (!IsFunctionParameterPack) {
555 if (NamedDecl *PartialPack
556 = CurrentInstantiationScope->getPartiallySubstitutedPack()){
557 unsigned PartialDepth, PartialIndex;
558 llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
559 if (PartialDepth == Depth && PartialIndex == Index)
560 RetainExpansion = true;
561 }
Douglas Gregord3731192011-01-10 07:32:04 +0000562 }
Douglas Gregor8619edd2011-01-20 23:15:49 +0000563
Douglas Gregorcded4f62011-01-14 17:04:44 +0000564 if (!NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000565 // The is the first pack we've seen for which we have an argument.
566 // Record it.
567 NumExpansions = NewPackSize;
568 FirstPack.first = Name;
David Blaikiea71f9d02011-09-22 02:34:54 +0000569 FirstPack.second = i->second;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000570 HaveFirstPack = true;
571 continue;
572 }
573
Douglas Gregorcded4f62011-01-14 17:04:44 +0000574 if (NewPackSize != *NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000575 // C++0x [temp.variadic]p5:
576 // All of the parameter packs expanded by a pack expansion shall have
577 // the same number of arguments specified.
Douglas Gregorcded4f62011-01-14 17:04:44 +0000578 if (HaveFirstPack)
579 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
580 << FirstPack.first << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000581 << SourceRange(FirstPack.second) << SourceRange(i->second);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000582 else
583 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
584 << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000585 << SourceRange(i->second);
Douglas Gregorb99268b2010-12-21 00:52:54 +0000586 return true;
587 }
588 }
589
590 return false;
591}
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000592
Douglas Gregor21371ea2011-01-11 03:14:20 +0000593unsigned Sema::getNumArgumentsInExpansion(QualType T,
594 const MultiLevelTemplateArgumentList &TemplateArgs) {
595 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000596 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000597 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
598
599 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
600 // Compute the depth and index for this parameter pack.
601 unsigned Depth;
602 unsigned Index;
603
604 if (const TemplateTypeParmType *TTP
605 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
606 Depth = TTP->getDepth();
607 Index = TTP->getIndex();
608 } else {
609 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
610 if (isa<ParmVarDecl>(ND)) {
611 // Function parameter pack.
612 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
613
614 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
615 = CurrentInstantiationScope->findInstantiationOf(
616 Unexpanded[I].first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000617 if (Instantiation->is<DeclArgumentPack *>())
Douglas Gregor21371ea2011-01-11 03:14:20 +0000618 return Instantiation->get<DeclArgumentPack *>()->size();
619
620 continue;
621 }
622
623 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
624 }
625 if (Depth >= TemplateArgs.getNumLevels() ||
626 !TemplateArgs.hasTemplateArgument(Depth, Index))
627 continue;
628
629 // Determine the size of the argument pack.
630 return TemplateArgs(Depth, Index).pack_size();
631 }
632
633 llvm_unreachable("No unexpanded parameter packs in type expansion.");
Douglas Gregor21371ea2011-01-11 03:14:20 +0000634}
635
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000636bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
637 const DeclSpec &DS = D.getDeclSpec();
638 switch (DS.getTypeSpecType()) {
639 case TST_typename:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000640 case TST_typeofType:
Eli Friedmanb001de72011-10-06 23:00:33 +0000641 case TST_underlyingType:
642 case TST_atomic: {
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000643 QualType T = DS.getRepAsType().get();
644 if (!T.isNull() && T->containsUnexpandedParameterPack())
645 return true;
646 break;
647 }
648
649 case TST_typeofExpr:
650 case TST_decltype:
651 if (DS.getRepAsExpr() &&
652 DS.getRepAsExpr()->containsUnexpandedParameterPack())
653 return true;
654 break;
655
656 case TST_unspecified:
657 case TST_void:
658 case TST_char:
659 case TST_wchar:
660 case TST_char16:
661 case TST_char32:
662 case TST_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000663 case TST_half:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000664 case TST_float:
665 case TST_double:
666 case TST_bool:
667 case TST_decimal32:
668 case TST_decimal64:
669 case TST_decimal128:
670 case TST_enum:
671 case TST_union:
672 case TST_struct:
673 case TST_class:
674 case TST_auto:
John McCalla5fc4722011-04-09 22:50:59 +0000675 case TST_unknown_anytype:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000676 case TST_error:
677 break;
678 }
679
680 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
681 const DeclaratorChunk &Chunk = D.getTypeObject(I);
682 switch (Chunk.Kind) {
683 case DeclaratorChunk::Pointer:
684 case DeclaratorChunk::Reference:
685 case DeclaratorChunk::Paren:
686 // These declarator chunks cannot contain any parameter packs.
687 break;
688
689 case DeclaratorChunk::Array:
690 case DeclaratorChunk::Function:
691 case DeclaratorChunk::BlockPointer:
692 // Syntactically, these kinds of declarator chunks all come after the
693 // declarator-id (conceptually), so the parser should not invoke this
694 // routine at this time.
695 llvm_unreachable("Could not have seen this kind of declarator chunk");
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000696
697 case DeclaratorChunk::MemberPointer:
698 if (Chunk.Mem.Scope().getScopeRep() &&
699 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
700 return true;
701 break;
702 }
703 }
704
705 return false;
706}
Douglas Gregoree8aff02011-01-04 17:33:58 +0000707
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000708namespace {
709
710// Callback to only accept typo corrections that refer to parameter packs.
711class ParameterPackValidatorCCC : public CorrectionCandidateCallback {
712 public:
713 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
714 NamedDecl *ND = candidate.getCorrectionDecl();
715 return ND && ND->isParameterPack();
716 }
717};
718
719}
720
Douglas Gregoree8aff02011-01-04 17:33:58 +0000721/// \brief Called when an expression computing the size of a parameter pack
722/// is parsed.
723///
724/// \code
725/// template<typename ...Types> struct count {
726/// static const unsigned value = sizeof...(Types);
727/// };
728/// \endcode
729///
730//
731/// \param OpLoc The location of the "sizeof" keyword.
732/// \param Name The name of the parameter pack whose size will be determined.
733/// \param NameLoc The source location of the name of the parameter pack.
734/// \param RParenLoc The location of the closing parentheses.
735ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
736 SourceLocation OpLoc,
737 IdentifierInfo &Name,
738 SourceLocation NameLoc,
739 SourceLocation RParenLoc) {
740 // C++0x [expr.sizeof]p5:
741 // The identifier in a sizeof... expression shall name a parameter pack.
Douglas Gregoree8aff02011-01-04 17:33:58 +0000742 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
743 LookupName(R, S);
744
745 NamedDecl *ParameterPack = 0;
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000746 ParameterPackValidatorCCC Validator;
Douglas Gregoree8aff02011-01-04 17:33:58 +0000747 switch (R.getResultKind()) {
748 case LookupResult::Found:
749 ParameterPack = R.getFoundDecl();
750 break;
751
752 case LookupResult::NotFound:
753 case LookupResult::NotFoundInCurrentInstantiation:
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000754 if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000755 R.getLookupKind(), S, 0,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000756 Validator)) {
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000757 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
758 ParameterPack = Corrected.getCorrectionDecl();
759 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name_suggest)
760 << &Name << CorrectedQuotedStr
761 << FixItHint::CreateReplacement(
762 NameLoc, Corrected.getAsString(getLangOptions()));
763 Diag(ParameterPack->getLocation(), diag::note_parameter_pack_here)
764 << CorrectedQuotedStr;
Douglas Gregoree8aff02011-01-04 17:33:58 +0000765 }
766
767 case LookupResult::FoundOverloaded:
768 case LookupResult::FoundUnresolvedValue:
769 break;
770
771 case LookupResult::Ambiguous:
772 DiagnoseAmbiguousLookup(R);
773 return ExprError();
774 }
775
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000776 if (!ParameterPack || !ParameterPack->isParameterPack()) {
Douglas Gregoree8aff02011-01-04 17:33:58 +0000777 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
778 << &Name;
779 return ExprError();
780 }
781
782 return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
783 ParameterPack, NameLoc, RParenLoc);
784}