blob: a2aa1b302644f38bbddeaf5d31023e9ed24597bb [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.
158static void
159DiagnoseUnexpandedParameterPacks(Sema &S, SourceLocation Loc,
160 Sema::UnexpandedParameterPackContext UPPC,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000161 const SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
162 SmallVector<SourceLocation, 4> Locations;
163 SmallVector<IdentifierInfo *, 4> Names;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000164 llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
165
166 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
167 IdentifierInfo *Name = 0;
168 if (const TemplateTypeParmType *TTP
169 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
Chandler Carruthb7efff42011-05-01 01:05:51 +0000170 Name = TTP->getIdentifier();
Douglas Gregor9ef75892010-12-15 19:43:21 +0000171 else
172 Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
173
174 if (Name && NamesKnown.insert(Name))
175 Names.push_back(Name);
176
177 if (Unexpanded[I].second.isValid())
178 Locations.push_back(Unexpanded[I].second);
179 }
180
181 DiagnosticBuilder DB
182 = Names.size() == 0? S.Diag(Loc, diag::err_unexpanded_parameter_pack_0)
183 << (int)UPPC
184 : Names.size() == 1? S.Diag(Loc, diag::err_unexpanded_parameter_pack_1)
185 << (int)UPPC << Names[0]
186 : Names.size() == 2? S.Diag(Loc, diag::err_unexpanded_parameter_pack_2)
187 << (int)UPPC << Names[0] << Names[1]
188 : S.Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more)
189 << (int)UPPC << Names[0] << Names[1];
190
191 for (unsigned I = 0, N = Locations.size(); I != N; ++I)
192 DB << SourceRange(Locations[I]);
193}
194
Douglas Gregorc4633352010-12-15 17:38:57 +0000195bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
196 TypeSourceInfo *T,
197 UnexpandedParameterPackContext UPPC) {
198 // C++0x [temp.variadic]p5:
199 // An appearance of a name of a parameter pack that is not expanded is
200 // ill-formed.
201 if (!T->getType()->containsUnexpandedParameterPack())
202 return false;
203
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000205 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
206 T->getTypeLoc());
207 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
208 DiagnoseUnexpandedParameterPacks(*this, Loc, UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000209 return true;
210}
211
212bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
Douglas Gregor56c04582010-12-16 00:46:58 +0000213 UnexpandedParameterPackContext UPPC) {
Douglas Gregorc4633352010-12-15 17:38:57 +0000214 // C++0x [temp.variadic]p5:
215 // An appearance of a name of a parameter pack that is not expanded is
216 // ill-formed.
217 if (!E->containsUnexpandedParameterPack())
218 return false;
219
Chris Lattner5f9e2722011-07-23 10:55:15 +0000220 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000221 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
222 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
223 DiagnoseUnexpandedParameterPacks(*this, E->getLocStart(), UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000224 return true;
225}
Douglas Gregor56c04582010-12-16 00:46:58 +0000226
227bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
228 UnexpandedParameterPackContext UPPC) {
229 // C++0x [temp.variadic]p5:
230 // An appearance of a name of a parameter pack that is not expanded is
231 // ill-formed.
232 if (!SS.getScopeRep() ||
233 !SS.getScopeRep()->containsUnexpandedParameterPack())
234 return false;
235
Chris Lattner5f9e2722011-07-23 10:55:15 +0000236 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000237 CollectUnexpandedParameterPacksVisitor(Unexpanded)
238 .TraverseNestedNameSpecifier(SS.getScopeRep());
239 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
240 DiagnoseUnexpandedParameterPacks(*this, SS.getRange().getBegin(),
241 UPPC, Unexpanded);
242 return true;
243}
244
245bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
246 UnexpandedParameterPackContext UPPC) {
247 // C++0x [temp.variadic]p5:
248 // An appearance of a name of a parameter pack that is not expanded is
249 // ill-formed.
250 switch (NameInfo.getName().getNameKind()) {
251 case DeclarationName::Identifier:
252 case DeclarationName::ObjCZeroArgSelector:
253 case DeclarationName::ObjCOneArgSelector:
254 case DeclarationName::ObjCMultiArgSelector:
255 case DeclarationName::CXXOperatorName:
256 case DeclarationName::CXXLiteralOperatorName:
257 case DeclarationName::CXXUsingDirective:
258 return false;
259
260 case DeclarationName::CXXConstructorName:
261 case DeclarationName::CXXDestructorName:
262 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor099ffe82010-12-16 17:19:19 +0000263 // FIXME: We shouldn't need this null check!
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000264 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
265 return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
266
267 if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
Douglas Gregor56c04582010-12-16 00:46:58 +0000268 return false;
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000269
Douglas Gregor56c04582010-12-16 00:46:58 +0000270 break;
271 }
272
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000274 CollectUnexpandedParameterPacksVisitor(Unexpanded)
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000275 .TraverseType(NameInfo.getName().getCXXNameType());
Douglas Gregor56c04582010-12-16 00:46:58 +0000276 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
277 DiagnoseUnexpandedParameterPacks(*this, NameInfo.getLoc(), UPPC, Unexpanded);
278 return true;
279}
Douglas Gregor6f526752010-12-16 08:48:57 +0000280
281bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
282 TemplateName Template,
283 UnexpandedParameterPackContext UPPC) {
284
285 if (Template.isNull() || !Template.containsUnexpandedParameterPack())
286 return false;
287
Chris Lattner5f9e2722011-07-23 10:55:15 +0000288 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6f526752010-12-16 08:48:57 +0000289 CollectUnexpandedParameterPacksVisitor(Unexpanded)
290 .TraverseTemplateName(Template);
291 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
292 DiagnoseUnexpandedParameterPacks(*this, Loc, UPPC, Unexpanded);
293 return true;
294}
295
Douglas Gregor925910d2011-01-03 20:35:03 +0000296bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
297 UnexpandedParameterPackContext UPPC) {
298 if (Arg.getArgument().isNull() ||
299 !Arg.getArgument().containsUnexpandedParameterPack())
300 return false;
301
Chris Lattner5f9e2722011-07-23 10:55:15 +0000302 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor925910d2011-01-03 20:35:03 +0000303 CollectUnexpandedParameterPacksVisitor(Unexpanded)
304 .TraverseTemplateArgumentLoc(Arg);
305 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
306 DiagnoseUnexpandedParameterPacks(*this, Arg.getLocation(), UPPC, Unexpanded);
307 return true;
308}
309
Douglas Gregore02e2622010-12-22 21:19:48 +0000310void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregore02e2622010-12-22 21:19:48 +0000312 CollectUnexpandedParameterPacksVisitor(Unexpanded)
313 .TraverseTemplateArgument(Arg);
314}
315
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000316void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000317 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000318 CollectUnexpandedParameterPacksVisitor(Unexpanded)
319 .TraverseTemplateArgumentLoc(Arg);
320}
321
Douglas Gregorb99268b2010-12-21 00:52:54 +0000322void Sema::collectUnexpandedParameterPacks(QualType T,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000323 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000324 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
325}
326
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000327void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000328 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000329 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
330}
331
Douglas Gregor7536dd52010-12-20 02:24:11 +0000332ParsedTemplateArgument
333Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
334 SourceLocation EllipsisLoc) {
335 if (Arg.isInvalid())
336 return Arg;
337
338 switch (Arg.getKind()) {
339 case ParsedTemplateArgument::Type: {
340 TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
341 if (Result.isInvalid())
342 return ParsedTemplateArgument();
343
344 return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
345 Arg.getLocation());
346 }
347
Douglas Gregorbe230c32011-01-03 17:17:50 +0000348 case ParsedTemplateArgument::NonType: {
349 ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
350 if (Result.isInvalid())
351 return ParsedTemplateArgument();
352
353 return ParsedTemplateArgument(Arg.getKind(), Result.get(),
354 Arg.getLocation());
355 }
356
Douglas Gregor7536dd52010-12-20 02:24:11 +0000357 case ParsedTemplateArgument::Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000358 if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
359 SourceRange R(Arg.getLocation());
360 if (Arg.getScopeSpec().isValid())
361 R.setBegin(Arg.getScopeSpec().getBeginLoc());
362 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
363 << R;
364 return ParsedTemplateArgument();
365 }
366
367 return Arg.getTemplatePackExpansion(EllipsisLoc);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000368 }
369 llvm_unreachable("Unhandled template argument kind?");
370 return ParsedTemplateArgument();
371}
372
373TypeResult Sema::ActOnPackExpansion(ParsedType Type,
374 SourceLocation EllipsisLoc) {
375 TypeSourceInfo *TSInfo;
376 GetTypeFromParser(Type, &TSInfo);
377 if (!TSInfo)
378 return true;
379
Douglas Gregorcded4f62011-01-14 17:04:44 +0000380 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc,
381 llvm::Optional<unsigned>());
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000382 if (!TSResult)
383 return true;
384
385 return CreateParsedType(TSResult->getType(), TSResult);
386}
387
388TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000389 SourceLocation EllipsisLoc,
390 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor7536dd52010-12-20 02:24:11 +0000391 // Create the pack expansion type and source-location information.
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000392 QualType Result = CheckPackExpansion(Pattern->getType(),
393 Pattern->getTypeLoc().getSourceRange(),
Douglas Gregorcded4f62011-01-14 17:04:44 +0000394 EllipsisLoc, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000395 if (Result.isNull())
396 return 0;
397
Douglas Gregor7536dd52010-12-20 02:24:11 +0000398 TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result);
399 PackExpansionTypeLoc TL = cast<PackExpansionTypeLoc>(TSResult->getTypeLoc());
400 TL.setEllipsisLoc(EllipsisLoc);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000401
Douglas Gregor7536dd52010-12-20 02:24:11 +0000402 // Copy over the source-location information from the type.
403 memcpy(TL.getNextTypeLoc().getOpaqueData(),
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000404 Pattern->getTypeLoc().getOpaqueData(),
405 Pattern->getTypeLoc().getFullDataSize());
406 return TSResult;
Douglas Gregor7536dd52010-12-20 02:24:11 +0000407}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000408
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000409QualType Sema::CheckPackExpansion(QualType Pattern,
410 SourceRange PatternRange,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000411 SourceLocation EllipsisLoc,
412 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000413 // C++0x [temp.variadic]p5:
414 // The pattern of a pack expansion shall name one or more
415 // parameter packs that are not expanded by a nested pack
416 // expansion.
417 if (!Pattern->containsUnexpandedParameterPack()) {
418 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
419 << PatternRange;
420 return QualType();
421 }
422
Douglas Gregorcded4f62011-01-14 17:04:44 +0000423 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000424}
425
Douglas Gregorbe230c32011-01-03 17:17:50 +0000426ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
Douglas Gregor67fd1252011-01-14 21:20:45 +0000427 return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>());
428}
429
430ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
431 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregorbe230c32011-01-03 17:17:50 +0000432 if (!Pattern)
433 return ExprError();
434
435 // C++0x [temp.variadic]p5:
436 // The pattern of a pack expansion shall name one or more
437 // parameter packs that are not expanded by a nested pack
438 // expansion.
439 if (!Pattern->containsUnexpandedParameterPack()) {
440 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
441 << Pattern->getSourceRange();
442 return ExprError();
443 }
444
445 // Create the pack expansion expression and source-location information.
446 return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
Douglas Gregor67fd1252011-01-14 21:20:45 +0000447 EllipsisLoc, NumExpansions));
Douglas Gregorbe230c32011-01-03 17:17:50 +0000448}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000449
Douglas Gregord3731192011-01-10 07:32:04 +0000450/// \brief Retrieve the depth and index of a parameter pack.
451static std::pair<unsigned, unsigned>
452getDepthAndIndex(NamedDecl *ND) {
453 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
454 return std::make_pair(TTP->getDepth(), TTP->getIndex());
455
456 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
457 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
458
459 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
460 return std::make_pair(TTP->getDepth(), TTP->getIndex());
461}
462
Douglas Gregorb99268b2010-12-21 00:52:54 +0000463bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,
464 SourceRange PatternRange,
David Blaikiea71f9d02011-09-22 02:34:54 +0000465 ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000466 const MultiLevelTemplateArgumentList &TemplateArgs,
467 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000468 bool &RetainExpansion,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000469 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000470 ShouldExpand = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000471 RetainExpansion = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000472 std::pair<IdentifierInfo *, SourceLocation> FirstPack;
473 bool HaveFirstPack = false;
474
David Blaikiea71f9d02011-09-22 02:34:54 +0000475 for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
476 end = Unexpanded.end();
477 i != end; ++i) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000478 // Compute the depth and index for this parameter pack.
Ted Kremenek9577abc2011-01-23 17:04:59 +0000479 unsigned Depth = 0, Index = 0;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000480 IdentifierInfo *Name;
Douglas Gregor12c9c002011-01-07 16:43:16 +0000481 bool IsFunctionParameterPack = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000482
483 if (const TemplateTypeParmType *TTP
David Blaikiea71f9d02011-09-22 02:34:54 +0000484 = i->first.dyn_cast<const TemplateTypeParmType *>()) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000485 Depth = TTP->getDepth();
486 Index = TTP->getIndex();
Chandler Carruthb7efff42011-05-01 01:05:51 +0000487 Name = TTP->getIdentifier();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000488 } else {
David Blaikiea71f9d02011-09-22 02:34:54 +0000489 NamedDecl *ND = i->first.get<NamedDecl *>();
Douglas Gregord3731192011-01-10 07:32:04 +0000490 if (isa<ParmVarDecl>(ND))
Douglas Gregor12c9c002011-01-07 16:43:16 +0000491 IsFunctionParameterPack = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000492 else
493 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
494
Douglas Gregorb99268b2010-12-21 00:52:54 +0000495 Name = ND->getIdentifier();
496 }
497
Douglas Gregor12c9c002011-01-07 16:43:16 +0000498 // Determine the size of this argument pack.
499 unsigned NewPackSize;
500 if (IsFunctionParameterPack) {
501 // Figure out whether we're instantiating to an argument pack or not.
502 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
503
504 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
505 = CurrentInstantiationScope->findInstantiationOf(
David Blaikiea71f9d02011-09-22 02:34:54 +0000506 i->first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000507 if (Instantiation->is<DeclArgumentPack *>()) {
Douglas Gregor12c9c002011-01-07 16:43:16 +0000508 // We could expand this function parameter pack.
509 NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
510 } else {
511 // We can't expand this function parameter pack, so we can't expand
512 // the pack expansion.
513 ShouldExpand = false;
514 continue;
515 }
516 } else {
517 // If we don't have a template argument at this depth/index, then we
518 // cannot expand the pack expansion. Make a note of this, but we still
519 // want to check any parameter packs we *do* have arguments for.
520 if (Depth >= TemplateArgs.getNumLevels() ||
521 !TemplateArgs.hasTemplateArgument(Depth, Index)) {
522 ShouldExpand = false;
523 continue;
524 }
525
526 // Determine the size of the argument pack.
527 NewPackSize = TemplateArgs(Depth, Index).pack_size();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000528 }
529
Douglas Gregord3731192011-01-10 07:32:04 +0000530 // C++0x [temp.arg.explicit]p9:
531 // Template argument deduction can extend the sequence of template
532 // arguments corresponding to a template parameter pack, even when the
533 // sequence contains explicitly specified template arguments.
Douglas Gregor8619edd2011-01-20 23:15:49 +0000534 if (!IsFunctionParameterPack) {
535 if (NamedDecl *PartialPack
536 = CurrentInstantiationScope->getPartiallySubstitutedPack()){
537 unsigned PartialDepth, PartialIndex;
538 llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
539 if (PartialDepth == Depth && PartialIndex == Index)
540 RetainExpansion = true;
541 }
Douglas Gregord3731192011-01-10 07:32:04 +0000542 }
Douglas Gregor8619edd2011-01-20 23:15:49 +0000543
Douglas Gregorcded4f62011-01-14 17:04:44 +0000544 if (!NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000545 // The is the first pack we've seen for which we have an argument.
546 // Record it.
547 NumExpansions = NewPackSize;
548 FirstPack.first = Name;
David Blaikiea71f9d02011-09-22 02:34:54 +0000549 FirstPack.second = i->second;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000550 HaveFirstPack = true;
551 continue;
552 }
553
Douglas Gregorcded4f62011-01-14 17:04:44 +0000554 if (NewPackSize != *NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000555 // C++0x [temp.variadic]p5:
556 // All of the parameter packs expanded by a pack expansion shall have
557 // the same number of arguments specified.
Douglas Gregorcded4f62011-01-14 17:04:44 +0000558 if (HaveFirstPack)
559 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
560 << FirstPack.first << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000561 << SourceRange(FirstPack.second) << SourceRange(i->second);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000562 else
563 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
564 << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000565 << SourceRange(i->second);
Douglas Gregorb99268b2010-12-21 00:52:54 +0000566 return true;
567 }
568 }
569
570 return false;
571}
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000572
Douglas Gregor21371ea2011-01-11 03:14:20 +0000573unsigned Sema::getNumArgumentsInExpansion(QualType T,
574 const MultiLevelTemplateArgumentList &TemplateArgs) {
575 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000576 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000577 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
578
579 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
580 // Compute the depth and index for this parameter pack.
581 unsigned Depth;
582 unsigned Index;
583
584 if (const TemplateTypeParmType *TTP
585 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
586 Depth = TTP->getDepth();
587 Index = TTP->getIndex();
588 } else {
589 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
590 if (isa<ParmVarDecl>(ND)) {
591 // Function parameter pack.
592 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
593
594 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
595 = CurrentInstantiationScope->findInstantiationOf(
596 Unexpanded[I].first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000597 if (Instantiation->is<DeclArgumentPack *>())
Douglas Gregor21371ea2011-01-11 03:14:20 +0000598 return Instantiation->get<DeclArgumentPack *>()->size();
599
600 continue;
601 }
602
603 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
604 }
605 if (Depth >= TemplateArgs.getNumLevels() ||
606 !TemplateArgs.hasTemplateArgument(Depth, Index))
607 continue;
608
609 // Determine the size of the argument pack.
610 return TemplateArgs(Depth, Index).pack_size();
611 }
612
613 llvm_unreachable("No unexpanded parameter packs in type expansion.");
614 return 0;
615}
616
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000617bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
618 const DeclSpec &DS = D.getDeclSpec();
619 switch (DS.getTypeSpecType()) {
620 case TST_typename:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000621 case TST_typeofType:
Eli Friedmanb001de72011-10-06 23:00:33 +0000622 case TST_underlyingType:
623 case TST_atomic: {
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000624 QualType T = DS.getRepAsType().get();
625 if (!T.isNull() && T->containsUnexpandedParameterPack())
626 return true;
627 break;
628 }
629
630 case TST_typeofExpr:
631 case TST_decltype:
632 if (DS.getRepAsExpr() &&
633 DS.getRepAsExpr()->containsUnexpandedParameterPack())
634 return true;
635 break;
636
637 case TST_unspecified:
638 case TST_void:
639 case TST_char:
640 case TST_wchar:
641 case TST_char16:
642 case TST_char32:
643 case TST_int:
644 case TST_float:
645 case TST_double:
646 case TST_bool:
647 case TST_decimal32:
648 case TST_decimal64:
649 case TST_decimal128:
650 case TST_enum:
651 case TST_union:
652 case TST_struct:
653 case TST_class:
654 case TST_auto:
John McCalla5fc4722011-04-09 22:50:59 +0000655 case TST_unknown_anytype:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000656 case TST_error:
657 break;
658 }
659
660 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
661 const DeclaratorChunk &Chunk = D.getTypeObject(I);
662 switch (Chunk.Kind) {
663 case DeclaratorChunk::Pointer:
664 case DeclaratorChunk::Reference:
665 case DeclaratorChunk::Paren:
666 // These declarator chunks cannot contain any parameter packs.
667 break;
668
669 case DeclaratorChunk::Array:
670 case DeclaratorChunk::Function:
671 case DeclaratorChunk::BlockPointer:
672 // Syntactically, these kinds of declarator chunks all come after the
673 // declarator-id (conceptually), so the parser should not invoke this
674 // routine at this time.
675 llvm_unreachable("Could not have seen this kind of declarator chunk");
676 break;
677
678 case DeclaratorChunk::MemberPointer:
679 if (Chunk.Mem.Scope().getScopeRep() &&
680 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
681 return true;
682 break;
683 }
684 }
685
686 return false;
687}
Douglas Gregoree8aff02011-01-04 17:33:58 +0000688
689/// \brief Called when an expression computing the size of a parameter pack
690/// is parsed.
691///
692/// \code
693/// template<typename ...Types> struct count {
694/// static const unsigned value = sizeof...(Types);
695/// };
696/// \endcode
697///
698//
699/// \param OpLoc The location of the "sizeof" keyword.
700/// \param Name The name of the parameter pack whose size will be determined.
701/// \param NameLoc The source location of the name of the parameter pack.
702/// \param RParenLoc The location of the closing parentheses.
703ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
704 SourceLocation OpLoc,
705 IdentifierInfo &Name,
706 SourceLocation NameLoc,
707 SourceLocation RParenLoc) {
708 // C++0x [expr.sizeof]p5:
709 // The identifier in a sizeof... expression shall name a parameter pack.
Douglas Gregoree8aff02011-01-04 17:33:58 +0000710 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
711 LookupName(R, S);
712
713 NamedDecl *ParameterPack = 0;
714 switch (R.getResultKind()) {
715 case LookupResult::Found:
716 ParameterPack = R.getFoundDecl();
717 break;
718
719 case LookupResult::NotFound:
720 case LookupResult::NotFoundInCurrentInstantiation:
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000721 if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
722 R.getLookupKind(), S, 0, 0,
723 false, CTC_NoKeywords)) {
724 if (NamedDecl *CorrectedResult = Corrected.getCorrectionDecl())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000725 if (CorrectedResult->isParameterPack()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000726 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregoree8aff02011-01-04 17:33:58 +0000727 ParameterPack = CorrectedResult;
728 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000729 << &Name << CorrectedQuotedStr
730 << FixItHint::CreateReplacement(
731 NameLoc, Corrected.getAsString(getLangOptions()));
Douglas Gregoree8aff02011-01-04 17:33:58 +0000732 Diag(ParameterPack->getLocation(), diag::note_parameter_pack_here)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000733 << CorrectedQuotedStr;
Douglas Gregoree8aff02011-01-04 17:33:58 +0000734 }
735 }
736
737 case LookupResult::FoundOverloaded:
738 case LookupResult::FoundUnresolvedValue:
739 break;
740
741 case LookupResult::Ambiguous:
742 DiagnoseAmbiguousLookup(R);
743 return ExprError();
744 }
745
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000746 if (!ParameterPack || !ParameterPack->isParameterPack()) {
Douglas Gregoree8aff02011-01-04 17:33:58 +0000747 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
748 << &Name;
749 return ExprError();
750 }
751
752 return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
753 ParameterPack, NameLoc, RParenLoc);
754}