blob: b219c20459fcb016dd1cf4ca893315e0eede9258 [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?");
391 return ParsedTemplateArgument();
392}
393
394TypeResult Sema::ActOnPackExpansion(ParsedType Type,
395 SourceLocation EllipsisLoc) {
396 TypeSourceInfo *TSInfo;
397 GetTypeFromParser(Type, &TSInfo);
398 if (!TSInfo)
399 return true;
400
Douglas Gregorcded4f62011-01-14 17:04:44 +0000401 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc,
402 llvm::Optional<unsigned>());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000403 if (!TSResult)
404 return true;
405
406 return CreateParsedType(TSResult->getType(), TSResult);
407}
408
409TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000410 SourceLocation EllipsisLoc,
411 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor7536dd52010-12-20 02:24:11 +0000412 // Create the pack expansion type and source-location information.
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000413 QualType Result = CheckPackExpansion(Pattern->getType(),
414 Pattern->getTypeLoc().getSourceRange(),
Douglas Gregorcded4f62011-01-14 17:04:44 +0000415 EllipsisLoc, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000416 if (Result.isNull())
417 return 0;
418
Douglas Gregor7536dd52010-12-20 02:24:11 +0000419 TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result);
420 PackExpansionTypeLoc TL = cast<PackExpansionTypeLoc>(TSResult->getTypeLoc());
421 TL.setEllipsisLoc(EllipsisLoc);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000422
Douglas Gregor7536dd52010-12-20 02:24:11 +0000423 // Copy over the source-location information from the type.
424 memcpy(TL.getNextTypeLoc().getOpaqueData(),
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000425 Pattern->getTypeLoc().getOpaqueData(),
426 Pattern->getTypeLoc().getFullDataSize());
427 return TSResult;
Douglas Gregor7536dd52010-12-20 02:24:11 +0000428}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000429
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000430QualType Sema::CheckPackExpansion(QualType Pattern,
431 SourceRange PatternRange,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000432 SourceLocation EllipsisLoc,
433 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000434 // C++0x [temp.variadic]p5:
435 // The pattern of a pack expansion shall name one or more
436 // parameter packs that are not expanded by a nested pack
437 // expansion.
438 if (!Pattern->containsUnexpandedParameterPack()) {
439 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
440 << PatternRange;
441 return QualType();
442 }
443
Douglas Gregorcded4f62011-01-14 17:04:44 +0000444 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000445}
446
Douglas Gregorbe230c32011-01-03 17:17:50 +0000447ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
Douglas Gregor67fd1252011-01-14 21:20:45 +0000448 return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>());
449}
450
451ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
452 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregorbe230c32011-01-03 17:17:50 +0000453 if (!Pattern)
454 return ExprError();
455
456 // C++0x [temp.variadic]p5:
457 // The pattern of a pack expansion shall name one or more
458 // parameter packs that are not expanded by a nested pack
459 // expansion.
460 if (!Pattern->containsUnexpandedParameterPack()) {
461 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
462 << Pattern->getSourceRange();
463 return ExprError();
464 }
465
466 // Create the pack expansion expression and source-location information.
467 return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
Douglas Gregor67fd1252011-01-14 21:20:45 +0000468 EllipsisLoc, NumExpansions));
Douglas Gregorbe230c32011-01-03 17:17:50 +0000469}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000470
Douglas Gregord3731192011-01-10 07:32:04 +0000471/// \brief Retrieve the depth and index of a parameter pack.
472static std::pair<unsigned, unsigned>
473getDepthAndIndex(NamedDecl *ND) {
474 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
475 return std::make_pair(TTP->getDepth(), TTP->getIndex());
476
477 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
478 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
479
480 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
481 return std::make_pair(TTP->getDepth(), TTP->getIndex());
482}
483
Douglas Gregorb99268b2010-12-21 00:52:54 +0000484bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,
485 SourceRange PatternRange,
David Blaikiea71f9d02011-09-22 02:34:54 +0000486 ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000487 const MultiLevelTemplateArgumentList &TemplateArgs,
488 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000489 bool &RetainExpansion,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000490 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000491 ShouldExpand = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000492 RetainExpansion = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000493 std::pair<IdentifierInfo *, SourceLocation> FirstPack;
494 bool HaveFirstPack = false;
495
David Blaikiea71f9d02011-09-22 02:34:54 +0000496 for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
497 end = Unexpanded.end();
498 i != end; ++i) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000499 // Compute the depth and index for this parameter pack.
Ted Kremenek9577abc2011-01-23 17:04:59 +0000500 unsigned Depth = 0, Index = 0;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000501 IdentifierInfo *Name;
Douglas Gregor12c9c002011-01-07 16:43:16 +0000502 bool IsFunctionParameterPack = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000503
504 if (const TemplateTypeParmType *TTP
David Blaikiea71f9d02011-09-22 02:34:54 +0000505 = i->first.dyn_cast<const TemplateTypeParmType *>()) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000506 Depth = TTP->getDepth();
507 Index = TTP->getIndex();
Chandler Carruthb7efff42011-05-01 01:05:51 +0000508 Name = TTP->getIdentifier();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000509 } else {
David Blaikiea71f9d02011-09-22 02:34:54 +0000510 NamedDecl *ND = i->first.get<NamedDecl *>();
Douglas Gregord3731192011-01-10 07:32:04 +0000511 if (isa<ParmVarDecl>(ND))
Douglas Gregor12c9c002011-01-07 16:43:16 +0000512 IsFunctionParameterPack = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000513 else
514 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
515
Douglas Gregorb99268b2010-12-21 00:52:54 +0000516 Name = ND->getIdentifier();
517 }
518
Douglas Gregor12c9c002011-01-07 16:43:16 +0000519 // Determine the size of this argument pack.
520 unsigned NewPackSize;
521 if (IsFunctionParameterPack) {
522 // Figure out whether we're instantiating to an argument pack or not.
523 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
524
525 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
526 = CurrentInstantiationScope->findInstantiationOf(
David Blaikiea71f9d02011-09-22 02:34:54 +0000527 i->first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000528 if (Instantiation->is<DeclArgumentPack *>()) {
Douglas Gregor12c9c002011-01-07 16:43:16 +0000529 // We could expand this function parameter pack.
530 NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
531 } else {
532 // We can't expand this function parameter pack, so we can't expand
533 // the pack expansion.
534 ShouldExpand = false;
535 continue;
536 }
537 } else {
538 // If we don't have a template argument at this depth/index, then we
539 // cannot expand the pack expansion. Make a note of this, but we still
540 // want to check any parameter packs we *do* have arguments for.
541 if (Depth >= TemplateArgs.getNumLevels() ||
542 !TemplateArgs.hasTemplateArgument(Depth, Index)) {
543 ShouldExpand = false;
544 continue;
545 }
546
547 // Determine the size of the argument pack.
548 NewPackSize = TemplateArgs(Depth, Index).pack_size();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000549 }
550
Douglas Gregord3731192011-01-10 07:32:04 +0000551 // C++0x [temp.arg.explicit]p9:
552 // Template argument deduction can extend the sequence of template
553 // arguments corresponding to a template parameter pack, even when the
554 // sequence contains explicitly specified template arguments.
Douglas Gregor8619edd2011-01-20 23:15:49 +0000555 if (!IsFunctionParameterPack) {
556 if (NamedDecl *PartialPack
557 = CurrentInstantiationScope->getPartiallySubstitutedPack()){
558 unsigned PartialDepth, PartialIndex;
559 llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
560 if (PartialDepth == Depth && PartialIndex == Index)
561 RetainExpansion = true;
562 }
Douglas Gregord3731192011-01-10 07:32:04 +0000563 }
Douglas Gregor8619edd2011-01-20 23:15:49 +0000564
Douglas Gregorcded4f62011-01-14 17:04:44 +0000565 if (!NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000566 // The is the first pack we've seen for which we have an argument.
567 // Record it.
568 NumExpansions = NewPackSize;
569 FirstPack.first = Name;
David Blaikiea71f9d02011-09-22 02:34:54 +0000570 FirstPack.second = i->second;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000571 HaveFirstPack = true;
572 continue;
573 }
574
Douglas Gregorcded4f62011-01-14 17:04:44 +0000575 if (NewPackSize != *NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000576 // C++0x [temp.variadic]p5:
577 // All of the parameter packs expanded by a pack expansion shall have
578 // the same number of arguments specified.
Douglas Gregorcded4f62011-01-14 17:04:44 +0000579 if (HaveFirstPack)
580 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
581 << FirstPack.first << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000582 << SourceRange(FirstPack.second) << SourceRange(i->second);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000583 else
584 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
585 << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000586 << SourceRange(i->second);
Douglas Gregorb99268b2010-12-21 00:52:54 +0000587 return true;
588 }
589 }
590
591 return false;
592}
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000593
Douglas Gregor21371ea2011-01-11 03:14:20 +0000594unsigned Sema::getNumArgumentsInExpansion(QualType T,
595 const MultiLevelTemplateArgumentList &TemplateArgs) {
596 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000597 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000598 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
599
600 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
601 // Compute the depth and index for this parameter pack.
602 unsigned Depth;
603 unsigned Index;
604
605 if (const TemplateTypeParmType *TTP
606 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
607 Depth = TTP->getDepth();
608 Index = TTP->getIndex();
609 } else {
610 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
611 if (isa<ParmVarDecl>(ND)) {
612 // Function parameter pack.
613 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
614
615 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
616 = CurrentInstantiationScope->findInstantiationOf(
617 Unexpanded[I].first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000618 if (Instantiation->is<DeclArgumentPack *>())
Douglas Gregor21371ea2011-01-11 03:14:20 +0000619 return Instantiation->get<DeclArgumentPack *>()->size();
620
621 continue;
622 }
623
624 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
625 }
626 if (Depth >= TemplateArgs.getNumLevels() ||
627 !TemplateArgs.hasTemplateArgument(Depth, Index))
628 continue;
629
630 // Determine the size of the argument pack.
631 return TemplateArgs(Depth, Index).pack_size();
632 }
633
634 llvm_unreachable("No unexpanded parameter packs in type expansion.");
635 return 0;
636}
637
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000638bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
639 const DeclSpec &DS = D.getDeclSpec();
640 switch (DS.getTypeSpecType()) {
641 case TST_typename:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000642 case TST_typeofType:
Eli Friedmanb001de72011-10-06 23:00:33 +0000643 case TST_underlyingType:
644 case TST_atomic: {
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000645 QualType T = DS.getRepAsType().get();
646 if (!T.isNull() && T->containsUnexpandedParameterPack())
647 return true;
648 break;
649 }
650
651 case TST_typeofExpr:
652 case TST_decltype:
653 if (DS.getRepAsExpr() &&
654 DS.getRepAsExpr()->containsUnexpandedParameterPack())
655 return true;
656 break;
657
658 case TST_unspecified:
659 case TST_void:
660 case TST_char:
661 case TST_wchar:
662 case TST_char16:
663 case TST_char32:
664 case TST_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000665 case TST_half:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000666 case TST_float:
667 case TST_double:
668 case TST_bool:
669 case TST_decimal32:
670 case TST_decimal64:
671 case TST_decimal128:
672 case TST_enum:
673 case TST_union:
674 case TST_struct:
675 case TST_class:
676 case TST_auto:
John McCalla5fc4722011-04-09 22:50:59 +0000677 case TST_unknown_anytype:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000678 case TST_error:
679 break;
680 }
681
682 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
683 const DeclaratorChunk &Chunk = D.getTypeObject(I);
684 switch (Chunk.Kind) {
685 case DeclaratorChunk::Pointer:
686 case DeclaratorChunk::Reference:
687 case DeclaratorChunk::Paren:
688 // These declarator chunks cannot contain any parameter packs.
689 break;
690
691 case DeclaratorChunk::Array:
692 case DeclaratorChunk::Function:
693 case DeclaratorChunk::BlockPointer:
694 // Syntactically, these kinds of declarator chunks all come after the
695 // declarator-id (conceptually), so the parser should not invoke this
696 // routine at this time.
697 llvm_unreachable("Could not have seen this kind of declarator chunk");
698 break;
699
700 case DeclaratorChunk::MemberPointer:
701 if (Chunk.Mem.Scope().getScopeRep() &&
702 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
703 return true;
704 break;
705 }
706 }
707
708 return false;
709}
Douglas Gregoree8aff02011-01-04 17:33:58 +0000710
711/// \brief Called when an expression computing the size of a parameter pack
712/// is parsed.
713///
714/// \code
715/// template<typename ...Types> struct count {
716/// static const unsigned value = sizeof...(Types);
717/// };
718/// \endcode
719///
720//
721/// \param OpLoc The location of the "sizeof" keyword.
722/// \param Name The name of the parameter pack whose size will be determined.
723/// \param NameLoc The source location of the name of the parameter pack.
724/// \param RParenLoc The location of the closing parentheses.
725ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
726 SourceLocation OpLoc,
727 IdentifierInfo &Name,
728 SourceLocation NameLoc,
729 SourceLocation RParenLoc) {
730 // C++0x [expr.sizeof]p5:
731 // The identifier in a sizeof... expression shall name a parameter pack.
Douglas Gregoree8aff02011-01-04 17:33:58 +0000732 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
733 LookupName(R, S);
734
735 NamedDecl *ParameterPack = 0;
736 switch (R.getResultKind()) {
737 case LookupResult::Found:
738 ParameterPack = R.getFoundDecl();
739 break;
740
741 case LookupResult::NotFound:
742 case LookupResult::NotFoundInCurrentInstantiation:
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000743 if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
744 R.getLookupKind(), S, 0, 0,
745 false, CTC_NoKeywords)) {
746 if (NamedDecl *CorrectedResult = Corrected.getCorrectionDecl())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000747 if (CorrectedResult->isParameterPack()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000748 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregoree8aff02011-01-04 17:33:58 +0000749 ParameterPack = CorrectedResult;
750 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000751 << &Name << CorrectedQuotedStr
752 << FixItHint::CreateReplacement(
753 NameLoc, Corrected.getAsString(getLangOptions()));
Douglas Gregoree8aff02011-01-04 17:33:58 +0000754 Diag(ParameterPack->getLocation(), diag::note_parameter_pack_here)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000755 << CorrectedQuotedStr;
Douglas Gregoree8aff02011-01-04 17:33:58 +0000756 }
757 }
758
759 case LookupResult::FoundOverloaded:
760 case LookupResult::FoundUnresolvedValue:
761 break;
762
763 case LookupResult::Ambiguous:
764 DiagnoseAmbiguousLookup(R);
765 return ExprError();
766 }
767
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000768 if (!ParameterPack || !ParameterPack->isParameterPack()) {
Douglas Gregoree8aff02011-01-04 17:33:58 +0000769 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
770 << &Name;
771 return ExprError();
772 }
773
774 return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
775 ParameterPack, NameLoc, RParenLoc);
776}