blob: e3925e812ac0074a30b897a759ff7a81ba60059f [file] [log] [blame]
Douglas Gregorb55fdf82010-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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000013#include "TypeLocBuilder.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/AST/Expr.h"
15#include "clang/AST/RecursiveASTVisitor.h"
16#include "clang/AST/TypeLoc.h"
Douglas Gregor820ba7b2011-01-04 17:33:58 +000017#include "clang/Sema/Lookup.h"
Douglas Gregord2fa7662010-12-20 02:24:11 +000018#include "clang/Sema/ParsedTemplate.h"
Richard Smith2589b9802012-07-25 03:56:55 +000019#include "clang/Sema/ScopeInfo.h"
Douglas Gregorb55fdf82010-12-15 17:38:57 +000020#include "clang/Sema/SemaInternal.h"
Douglas Gregor840bd6c2010-12-20 22:05:00 +000021#include "clang/Sema/Template.h"
Douglas Gregorb55fdf82010-12-15 17:38:57 +000022
23using namespace clang;
24
Douglas Gregor1da294a2010-12-15 19:43:21 +000025//----------------------------------------------------------------------------
26// Visitor that collects unexpanded parameter packs
27//----------------------------------------------------------------------------
28
Richard Smith78a07ba2017-08-15 19:11:21 +000029/// \brief Retrieve the depth and index of a parameter pack.
30static std::pair<unsigned, unsigned>
31getDepthAndIndex(NamedDecl *ND) {
32 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
33 return std::make_pair(TTP->getDepth(), TTP->getIndex());
34
35 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
36 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
37
38 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
39 return std::make_pair(TTP->getDepth(), TTP->getIndex());
40}
41
Douglas Gregor1da294a2010-12-15 19:43:21 +000042namespace {
43 /// \brief A class that collects unexpanded parameter packs.
44 class CollectUnexpandedParameterPacksVisitor :
45 public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
46 {
47 typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
48 inherited;
49
Chris Lattner0e62c1c2011-07-23 10:55:15 +000050 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
Douglas Gregor1da294a2010-12-15 19:43:21 +000051
Richard Smith78a07ba2017-08-15 19:11:21 +000052 bool InLambda = false;
53 unsigned DepthLimit = (unsigned)-1;
Richard Smith2589b9802012-07-25 03:56:55 +000054
Richard Smith78a07ba2017-08-15 19:11:21 +000055 void addUnexpanded(NamedDecl *ND, SourceLocation Loc = SourceLocation()) {
56 if (auto *PVD = dyn_cast<ParmVarDecl>(ND)) {
57 // For now, the only problematic case is a generic lambda's templated
58 // call operator, so we don't need to look for all the other ways we
59 // could have reached a dependent parameter pack.
60 auto *FD = dyn_cast<FunctionDecl>(PVD->getDeclContext());
61 auto *FTD = FD ? FD->getDescribedFunctionTemplate() : nullptr;
62 if (FTD && FTD->getTemplateParameters()->getDepth() >= DepthLimit)
63 return;
64 } else if (getDepthAndIndex(ND).first >= DepthLimit)
65 return;
66
67 Unexpanded.push_back({ND, Loc});
68 }
69 void addUnexpanded(const TemplateTypeParmType *T,
70 SourceLocation Loc = SourceLocation()) {
71 if (T->getDepth() < DepthLimit)
72 Unexpanded.push_back({T, Loc});
73 }
74
Douglas Gregor1da294a2010-12-15 19:43:21 +000075 public:
76 explicit CollectUnexpandedParameterPacksVisitor(
Richard Smith78a07ba2017-08-15 19:11:21 +000077 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
78 : Unexpanded(Unexpanded) {}
Douglas Gregor1da294a2010-12-15 19:43:21 +000079
Douglas Gregor15b4ec22010-12-20 23:07:20 +000080 bool shouldWalkTypesOfTypeLocs() const { return false; }
Richard Smith78a07ba2017-08-15 19:11:21 +000081
Douglas Gregor1da294a2010-12-15 19:43:21 +000082 //------------------------------------------------------------------------
83 // Recording occurrences of (unexpanded) parameter packs.
84 //------------------------------------------------------------------------
85
86 /// \brief Record occurrences of template type parameter packs.
87 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
88 if (TL.getTypePtr()->isParameterPack())
Richard Smith78a07ba2017-08-15 19:11:21 +000089 addUnexpanded(TL.getTypePtr(), TL.getNameLoc());
Douglas Gregor1da294a2010-12-15 19:43:21 +000090 return true;
91 }
92
93 /// \brief Record occurrences of template type parameter packs
94 /// when we don't have proper source-location information for
95 /// them.
96 ///
97 /// Ideally, this routine would never be used.
98 bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
99 if (T->isParameterPack())
Richard Smith78a07ba2017-08-15 19:11:21 +0000100 addUnexpanded(T);
Douglas Gregor1da294a2010-12-15 19:43:21 +0000101
102 return true;
103 }
104
Douglas Gregor476e3022011-01-19 21:32:01 +0000105 /// \brief Record occurrences of function and non-type template
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000106 /// parameter packs in an expression.
107 bool VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorf3010112011-01-07 16:43:16 +0000108 if (E->getDecl()->isParameterPack())
Richard Smith78a07ba2017-08-15 19:11:21 +0000109 addUnexpanded(E->getDecl(), E->getLocation());
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000110
111 return true;
112 }
113
Douglas Gregorf5500772011-01-05 15:48:55 +0000114 /// \brief Record occurrences of template template parameter packs.
115 bool TraverseTemplateName(TemplateName Template) {
Richard Smith78a07ba2017-08-15 19:11:21 +0000116 if (auto *TTP = dyn_cast_or_null<TemplateTemplateParmDecl>(
117 Template.getAsTemplateDecl())) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000118 if (TTP->isParameterPack())
Richard Smith78a07ba2017-08-15 19:11:21 +0000119 addUnexpanded(TTP);
120 }
Douglas Gregorf5500772011-01-05 15:48:55 +0000121
122 return inherited::TraverseTemplateName(Template);
123 }
Douglas Gregor1da294a2010-12-15 19:43:21 +0000124
Ted Kremeneke65b0862012-03-06 20:05:56 +0000125 /// \brief Suppress traversal into Objective-C container literal
126 /// elements that are pack expansions.
127 bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
128 if (!E->containsUnexpandedParameterPack())
129 return true;
130
131 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
132 ObjCDictionaryElement Element = E->getKeyValueElement(I);
133 if (Element.isPackExpansion())
134 continue;
135
136 TraverseStmt(Element.Key);
137 TraverseStmt(Element.Value);
138 }
139 return true;
140 }
Douglas Gregor1da294a2010-12-15 19:43:21 +0000141 //------------------------------------------------------------------------
142 // Pruning the search for unexpanded parameter packs.
143 //------------------------------------------------------------------------
144
145 /// \brief Suppress traversal into statements and expressions that
146 /// do not contain unexpanded parameter packs.
147 bool TraverseStmt(Stmt *S) {
Richard Smith2589b9802012-07-25 03:56:55 +0000148 Expr *E = dyn_cast_or_null<Expr>(S);
149 if ((E && E->containsUnexpandedParameterPack()) || InLambda)
150 return inherited::TraverseStmt(S);
Douglas Gregor1da294a2010-12-15 19:43:21 +0000151
Richard Smith2589b9802012-07-25 03:56:55 +0000152 return true;
Douglas Gregor1da294a2010-12-15 19:43:21 +0000153 }
154
155 /// \brief Suppress traversal into types that do not contain
156 /// unexpanded parameter packs.
157 bool TraverseType(QualType T) {
Richard Smith2589b9802012-07-25 03:56:55 +0000158 if ((!T.isNull() && T->containsUnexpandedParameterPack()) || InLambda)
Douglas Gregor1da294a2010-12-15 19:43:21 +0000159 return inherited::TraverseType(T);
160
161 return true;
162 }
163
164 /// \brief Suppress traversel into types with location information
165 /// that do not contain unexpanded parameter packs.
166 bool TraverseTypeLoc(TypeLoc TL) {
Richard Smith2589b9802012-07-25 03:56:55 +0000167 if ((!TL.getType().isNull() &&
168 TL.getType()->containsUnexpandedParameterPack()) ||
169 InLambda)
Douglas Gregor1da294a2010-12-15 19:43:21 +0000170 return inherited::TraverseTypeLoc(TL);
171
172 return true;
173 }
174
Douglas Gregora8461bb2010-12-15 21:57:59 +0000175 /// \brief Suppress traversal of non-parameter declarations, since
176 /// they cannot contain unexpanded parameter packs.
177 bool TraverseDecl(Decl *D) {
Richard Smith78a07ba2017-08-15 19:11:21 +0000178 auto *PVD = dyn_cast_or_null<ParmVarDecl>(D);
179 // A function parameter pack is a pack expansion, so cannot contain
180 // an unexpanded parameter pack.
181 if (PVD && PVD->isParameterPack())
182 return true;
183
184 if (PVD || InLambda)
Douglas Gregora8461bb2010-12-15 21:57:59 +0000185 return inherited::TraverseDecl(D);
186
Richard Smith2589b9802012-07-25 03:56:55 +0000187 return true;
Douglas Gregora8461bb2010-12-15 21:57:59 +0000188 }
Douglas Gregoreb29d182011-01-05 17:40:24 +0000189
190 /// \brief Suppress traversal of template argument pack expansions.
191 bool TraverseTemplateArgument(const TemplateArgument &Arg) {
192 if (Arg.isPackExpansion())
193 return true;
194
195 return inherited::TraverseTemplateArgument(Arg);
196 }
197
198 /// \brief Suppress traversal of template argument pack expansions.
199 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
200 if (ArgLoc.getArgument().isPackExpansion())
201 return true;
202
203 return inherited::TraverseTemplateArgumentLoc(ArgLoc);
204 }
Richard Smith2589b9802012-07-25 03:56:55 +0000205
206 /// \brief Note whether we're traversing a lambda containing an unexpanded
207 /// parameter pack. In this case, the unexpanded pack can occur anywhere,
208 /// including all the places where we normally wouldn't look. Within a
209 /// lambda, we don't propagate the 'contains unexpanded parameter pack' bit
210 /// outside an expression.
211 bool TraverseLambdaExpr(LambdaExpr *Lambda) {
212 // The ContainsUnexpandedParameterPack bit on a lambda is always correct,
213 // even if it's contained within another lambda.
214 if (!Lambda->containsUnexpandedParameterPack())
215 return true;
216
217 bool WasInLambda = InLambda;
Richard Smith78a07ba2017-08-15 19:11:21 +0000218 unsigned OldDepthLimit = DepthLimit;
Richard Smith2589b9802012-07-25 03:56:55 +0000219
Richard Smith78a07ba2017-08-15 19:11:21 +0000220 InLambda = true;
221 if (auto *TPL = Lambda->getTemplateParameterList())
222 DepthLimit = TPL->getDepth();
Richard Smith2589b9802012-07-25 03:56:55 +0000223
224 inherited::TraverseLambdaExpr(Lambda);
225
226 InLambda = WasInLambda;
Richard Smith78a07ba2017-08-15 19:11:21 +0000227 DepthLimit = OldDepthLimit;
Richard Smith2589b9802012-07-25 03:56:55 +0000228 return true;
229 }
Richard Smith78a07ba2017-08-15 19:11:21 +0000230
231 /// Suppress traversal within pack expansions in lambda captures.
232 bool TraverseLambdaCapture(LambdaExpr *Lambda, const LambdaCapture *C,
233 Expr *Init) {
234 if (C->isPackExpansion())
235 return true;
236 return inherited::TraverseLambdaCapture(Lambda, C, Init);
237 }
Douglas Gregor1da294a2010-12-15 19:43:21 +0000238 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000239}
Douglas Gregor1da294a2010-12-15 19:43:21 +0000240
Richard Smith36ee9fb2014-08-11 23:30:23 +0000241/// \brief Determine whether it's possible for an unexpanded parameter pack to
242/// be valid in this location. This only happens when we're in a declaration
243/// that is nested within an expression that could be expanded, such as a
244/// lambda-expression within a function call.
245///
246/// This is conservatively correct, but may claim that some unexpanded packs are
247/// permitted when they are not.
248bool Sema::isUnexpandedParameterPackPermitted() {
249 for (auto *SI : FunctionScopes)
250 if (isa<sema::LambdaScopeInfo>(SI))
251 return true;
252 return false;
253}
254
Douglas Gregor1da294a2010-12-15 19:43:21 +0000255/// \brief Diagnose all of the unexpanded parameter packs in the given
256/// vector.
Richard Smith2589b9802012-07-25 03:56:55 +0000257bool
Douglas Gregor4a2a8f72011-10-25 03:44:56 +0000258Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
259 UnexpandedParameterPackContext UPPC,
Bill Wendling8ac06af2012-02-22 09:38:11 +0000260 ArrayRef<UnexpandedParameterPack> Unexpanded) {
Douglas Gregor4a2a8f72011-10-25 03:44:56 +0000261 if (Unexpanded.empty())
Richard Smith2589b9802012-07-25 03:56:55 +0000262 return false;
263
Richard Smith78a07ba2017-08-15 19:11:21 +0000264 // If we are within a lambda expression and referencing a pack that is not
265 // a parameter of the lambda itself, that lambda contains an unexpanded
Richard Smith2589b9802012-07-25 03:56:55 +0000266 // parameter pack, and we are done.
267 // FIXME: Store 'Unexpanded' on the lambda so we don't need to recompute it
268 // later.
Richard Smith78a07ba2017-08-15 19:11:21 +0000269 SmallVector<UnexpandedParameterPack, 4> GenericLambdaParamReferences;
Richard Smith2589b9802012-07-25 03:56:55 +0000270 for (unsigned N = FunctionScopes.size(); N; --N) {
271 if (sema::LambdaScopeInfo *LSI =
272 dyn_cast<sema::LambdaScopeInfo>(FunctionScopes[N-1])) {
Richard Smith78a07ba2017-08-15 19:11:21 +0000273 if (LSI->isGenericLambda()) {
274 for (auto &Param : Unexpanded) {
275 auto *PD = dyn_cast_or_null<ParmVarDecl>(
276 Param.first.dyn_cast<NamedDecl *>());
277 if (PD && PD->getDeclContext() == LSI->CallOperator)
278 GenericLambdaParamReferences.push_back(Param);
279 }
280 }
281
282 // If we have references to a parameter of a generic lambda, only
283 // diagnose those ones. We don't know whether any other unexpanded
284 // parameters referenced herein are actually unexpanded; they might
285 // be expanded at an outer level.
286 if (!GenericLambdaParamReferences.empty()) {
287 Unexpanded = GenericLambdaParamReferences;
288 break;
289 }
290
Richard Smith2589b9802012-07-25 03:56:55 +0000291 LSI->ContainsUnexpandedParameterPack = true;
292 return false;
293 }
294 }
Douglas Gregor4a2a8f72011-10-25 03:44:56 +0000295
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000296 SmallVector<SourceLocation, 4> Locations;
297 SmallVector<IdentifierInfo *, 4> Names;
Douglas Gregor1da294a2010-12-15 19:43:21 +0000298 llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
299
300 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000301 IdentifierInfo *Name = nullptr;
Douglas Gregor1da294a2010-12-15 19:43:21 +0000302 if (const TemplateTypeParmType *TTP
303 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
Chandler Carruthdde65ea2011-05-01 01:05:51 +0000304 Name = TTP->getIdentifier();
Douglas Gregor1da294a2010-12-15 19:43:21 +0000305 else
306 Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
307
David Blaikie82e95a32014-11-19 07:49:47 +0000308 if (Name && NamesKnown.insert(Name).second)
Douglas Gregor1da294a2010-12-15 19:43:21 +0000309 Names.push_back(Name);
310
311 if (Unexpanded[I].second.isValid())
312 Locations.push_back(Unexpanded[I].second);
313 }
314
Benjamin Kramer3a8650a2015-03-27 17:23:14 +0000315 DiagnosticBuilder DB = Diag(Loc, diag::err_unexpanded_parameter_pack)
316 << (int)UPPC << (int)Names.size();
317 for (size_t I = 0, E = std::min(Names.size(), (size_t)2); I != E; ++I)
318 DB << Names[I];
Douglas Gregor1da294a2010-12-15 19:43:21 +0000319
320 for (unsigned I = 0, N = Locations.size(); I != N; ++I)
321 DB << SourceRange(Locations[I]);
Richard Smith2589b9802012-07-25 03:56:55 +0000322 return true;
Douglas Gregor1da294a2010-12-15 19:43:21 +0000323}
324
Douglas Gregorb55fdf82010-12-15 17:38:57 +0000325bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
326 TypeSourceInfo *T,
327 UnexpandedParameterPackContext UPPC) {
328 // C++0x [temp.variadic]p5:
329 // An appearance of a name of a parameter pack that is not expanded is
330 // ill-formed.
331 if (!T->getType()->containsUnexpandedParameterPack())
332 return false;
333
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000334 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor1da294a2010-12-15 19:43:21 +0000335 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
336 T->getTypeLoc());
337 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith2589b9802012-07-25 03:56:55 +0000338 return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregorb55fdf82010-12-15 17:38:57 +0000339}
340
341bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
Douglas Gregorc4356532010-12-16 00:46:58 +0000342 UnexpandedParameterPackContext UPPC) {
Douglas Gregorb55fdf82010-12-15 17:38:57 +0000343 // C++0x [temp.variadic]p5:
344 // An appearance of a name of a parameter pack that is not expanded is
345 // ill-formed.
346 if (!E->containsUnexpandedParameterPack())
347 return false;
348
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000349 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor1da294a2010-12-15 19:43:21 +0000350 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
351 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith2589b9802012-07-25 03:56:55 +0000352 return DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded);
Douglas Gregorb55fdf82010-12-15 17:38:57 +0000353}
Douglas Gregorc4356532010-12-16 00:46:58 +0000354
355bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
356 UnexpandedParameterPackContext UPPC) {
357 // C++0x [temp.variadic]p5:
358 // An appearance of a name of a parameter pack that is not expanded is
359 // ill-formed.
360 if (!SS.getScopeRep() ||
361 !SS.getScopeRep()->containsUnexpandedParameterPack())
362 return false;
363
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000364 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregorc4356532010-12-16 00:46:58 +0000365 CollectUnexpandedParameterPacksVisitor(Unexpanded)
366 .TraverseNestedNameSpecifier(SS.getScopeRep());
367 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith2589b9802012-07-25 03:56:55 +0000368 return DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
369 UPPC, Unexpanded);
Douglas Gregorc4356532010-12-16 00:46:58 +0000370}
371
372bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
373 UnexpandedParameterPackContext UPPC) {
374 // C++0x [temp.variadic]p5:
375 // An appearance of a name of a parameter pack that is not expanded is
376 // ill-formed.
377 switch (NameInfo.getName().getNameKind()) {
378 case DeclarationName::Identifier:
379 case DeclarationName::ObjCZeroArgSelector:
380 case DeclarationName::ObjCOneArgSelector:
381 case DeclarationName::ObjCMultiArgSelector:
382 case DeclarationName::CXXOperatorName:
383 case DeclarationName::CXXLiteralOperatorName:
384 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000385 case DeclarationName::CXXDeductionGuideName:
Douglas Gregorc4356532010-12-16 00:46:58 +0000386 return false;
387
388 case DeclarationName::CXXConstructorName:
389 case DeclarationName::CXXDestructorName:
390 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor062ecac2010-12-16 17:19:19 +0000391 // FIXME: We shouldn't need this null check!
Douglas Gregor6ab34af2010-12-16 01:40:04 +0000392 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
393 return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
394
395 if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
Douglas Gregorc4356532010-12-16 00:46:58 +0000396 return false;
Douglas Gregor6ab34af2010-12-16 01:40:04 +0000397
Douglas Gregorc4356532010-12-16 00:46:58 +0000398 break;
399 }
400
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000401 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregorc4356532010-12-16 00:46:58 +0000402 CollectUnexpandedParameterPacksVisitor(Unexpanded)
Douglas Gregor6ab34af2010-12-16 01:40:04 +0000403 .TraverseType(NameInfo.getName().getCXXNameType());
Douglas Gregorc4356532010-12-16 00:46:58 +0000404 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith2589b9802012-07-25 03:56:55 +0000405 return DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
Douglas Gregorc4356532010-12-16 00:46:58 +0000406}
Douglas Gregor6ff1fbf2010-12-16 08:48:57 +0000407
408bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
409 TemplateName Template,
410 UnexpandedParameterPackContext UPPC) {
411
412 if (Template.isNull() || !Template.containsUnexpandedParameterPack())
413 return false;
414
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000415 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6ff1fbf2010-12-16 08:48:57 +0000416 CollectUnexpandedParameterPacksVisitor(Unexpanded)
417 .TraverseTemplateName(Template);
418 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith2589b9802012-07-25 03:56:55 +0000419 return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregor6ff1fbf2010-12-16 08:48:57 +0000420}
421
Douglas Gregor14406932011-01-03 20:35:03 +0000422bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
423 UnexpandedParameterPackContext UPPC) {
424 if (Arg.getArgument().isNull() ||
425 !Arg.getArgument().containsUnexpandedParameterPack())
426 return false;
427
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000428 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor14406932011-01-03 20:35:03 +0000429 CollectUnexpandedParameterPacksVisitor(Unexpanded)
430 .TraverseTemplateArgumentLoc(Arg);
431 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith2589b9802012-07-25 03:56:55 +0000432 return DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
Douglas Gregor14406932011-01-03 20:35:03 +0000433}
434
Douglas Gregor0f3feb42010-12-22 21:19:48 +0000435void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000436 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor0f3feb42010-12-22 21:19:48 +0000437 CollectUnexpandedParameterPacksVisitor(Unexpanded)
438 .TraverseTemplateArgument(Arg);
439}
440
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000441void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000442 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000443 CollectUnexpandedParameterPacksVisitor(Unexpanded)
444 .TraverseTemplateArgumentLoc(Arg);
445}
446
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000447void Sema::collectUnexpandedParameterPacks(QualType T,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000448 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000449 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
450}
451
Douglas Gregor752a5952011-01-03 22:36:02 +0000452void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000453 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor752a5952011-01-03 22:36:02 +0000454 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
Richard Smith22a250c2016-12-19 04:08:53 +0000455}
456
Richard Smith151c4562016-12-20 21:35:28 +0000457void Sema::collectUnexpandedParameterPacks(
458 NestedNameSpecifierLoc NNS,
459 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
460 CollectUnexpandedParameterPacksVisitor(Unexpanded)
461 .TraverseNestedNameSpecifierLoc(NNS);
462}
463
464void Sema::collectUnexpandedParameterPacks(
465 const DeclarationNameInfo &NameInfo,
466 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor4a2a8f72011-10-25 03:44:56 +0000467 CollectUnexpandedParameterPacksVisitor(Unexpanded)
468 .TraverseDeclarationNameInfo(NameInfo);
469}
470
471
Douglas Gregord2fa7662010-12-20 02:24:11 +0000472ParsedTemplateArgument
473Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
474 SourceLocation EllipsisLoc) {
475 if (Arg.isInvalid())
476 return Arg;
477
478 switch (Arg.getKind()) {
479 case ParsedTemplateArgument::Type: {
480 TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
481 if (Result.isInvalid())
482 return ParsedTemplateArgument();
483
484 return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
485 Arg.getLocation());
486 }
487
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000488 case ParsedTemplateArgument::NonType: {
489 ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
490 if (Result.isInvalid())
491 return ParsedTemplateArgument();
492
493 return ParsedTemplateArgument(Arg.getKind(), Result.get(),
494 Arg.getLocation());
495 }
496
Douglas Gregord2fa7662010-12-20 02:24:11 +0000497 case ParsedTemplateArgument::Template:
Douglas Gregoreb29d182011-01-05 17:40:24 +0000498 if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
499 SourceRange R(Arg.getLocation());
500 if (Arg.getScopeSpec().isValid())
501 R.setBegin(Arg.getScopeSpec().getBeginLoc());
502 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
503 << R;
504 return ParsedTemplateArgument();
505 }
506
507 return Arg.getTemplatePackExpansion(EllipsisLoc);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000508 }
509 llvm_unreachable("Unhandled template argument kind?");
Douglas Gregord2fa7662010-12-20 02:24:11 +0000510}
511
512TypeResult Sema::ActOnPackExpansion(ParsedType Type,
513 SourceLocation EllipsisLoc) {
514 TypeSourceInfo *TSInfo;
515 GetTypeFromParser(Type, &TSInfo);
516 if (!TSInfo)
517 return true;
518
David Blaikie7a30dc52013-02-21 01:47:18 +0000519 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, None);
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000520 if (!TSResult)
521 return true;
522
523 return CreateParsedType(TSResult->getType(), TSResult);
524}
525
David Blaikie05785d12013-02-20 22:23:23 +0000526TypeSourceInfo *
527Sema::CheckPackExpansion(TypeSourceInfo *Pattern, SourceLocation EllipsisLoc,
528 Optional<unsigned> NumExpansions) {
Douglas Gregord2fa7662010-12-20 02:24:11 +0000529 // Create the pack expansion type and source-location information.
Douglas Gregor822d0302011-01-12 17:07:58 +0000530 QualType Result = CheckPackExpansion(Pattern->getType(),
531 Pattern->getTypeLoc().getSourceRange(),
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000532 EllipsisLoc, NumExpansions);
Douglas Gregor822d0302011-01-12 17:07:58 +0000533 if (Result.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +0000534 return nullptr;
Eli Friedman7152fbe2013-06-07 20:31:48 +0000535
536 TypeLocBuilder TLB;
537 TLB.pushFullCopy(Pattern->getTypeLoc());
538 PackExpansionTypeLoc TL = TLB.push<PackExpansionTypeLoc>(Result);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000539 TL.setEllipsisLoc(EllipsisLoc);
Eli Friedman7152fbe2013-06-07 20:31:48 +0000540
541 return TLB.getTypeSourceInfo(Context, Result);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000542}
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000543
David Blaikie05785d12013-02-20 22:23:23 +0000544QualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000545 SourceLocation EllipsisLoc,
David Blaikie05785d12013-02-20 22:23:23 +0000546 Optional<unsigned> NumExpansions) {
Douglas Gregor822d0302011-01-12 17:07:58 +0000547 // C++0x [temp.variadic]p5:
548 // The pattern of a pack expansion shall name one or more
549 // parameter packs that are not expanded by a nested pack
550 // expansion.
551 if (!Pattern->containsUnexpandedParameterPack()) {
552 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
553 << PatternRange;
554 return QualType();
555 }
556
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000557 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor822d0302011-01-12 17:07:58 +0000558}
559
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000560ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
David Blaikie7a30dc52013-02-21 01:47:18 +0000561 return CheckPackExpansion(Pattern, EllipsisLoc, None);
Douglas Gregorb8840002011-01-14 21:20:45 +0000562}
563
564ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
David Blaikie05785d12013-02-20 22:23:23 +0000565 Optional<unsigned> NumExpansions) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000566 if (!Pattern)
567 return ExprError();
568
569 // C++0x [temp.variadic]p5:
570 // The pattern of a pack expansion shall name one or more
571 // parameter packs that are not expanded by a nested pack
572 // expansion.
573 if (!Pattern->containsUnexpandedParameterPack()) {
574 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
575 << Pattern->getSourceRange();
576 return ExprError();
577 }
578
579 // Create the pack expansion expression and source-location information.
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000580 return new (Context)
581 PackExpansionExpr(Context.DependentTy, Pattern, EllipsisLoc, NumExpansions);
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000582}
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000583
David Blaikie05785d12013-02-20 22:23:23 +0000584bool Sema::CheckParameterPacksForExpansion(
585 SourceLocation EllipsisLoc, SourceRange PatternRange,
586 ArrayRef<UnexpandedParameterPack> Unexpanded,
587 const MultiLevelTemplateArgumentList &TemplateArgs, bool &ShouldExpand,
588 bool &RetainExpansion, Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000589 ShouldExpand = true;
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000590 RetainExpansion = false;
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000591 std::pair<IdentifierInfo *, SourceLocation> FirstPack;
592 bool HaveFirstPack = false;
593
David Blaikieb9c168a2011-09-22 02:34:54 +0000594 for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
595 end = Unexpanded.end();
596 i != end; ++i) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000597 // Compute the depth and index for this parameter pack.
Ted Kremenek582a0992011-01-23 17:04:59 +0000598 unsigned Depth = 0, Index = 0;
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000599 IdentifierInfo *Name;
Douglas Gregorf3010112011-01-07 16:43:16 +0000600 bool IsFunctionParameterPack = false;
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000601
602 if (const TemplateTypeParmType *TTP
David Blaikieb9c168a2011-09-22 02:34:54 +0000603 = i->first.dyn_cast<const TemplateTypeParmType *>()) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000604 Depth = TTP->getDepth();
605 Index = TTP->getIndex();
Chandler Carruthdde65ea2011-05-01 01:05:51 +0000606 Name = TTP->getIdentifier();
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000607 } else {
David Blaikieb9c168a2011-09-22 02:34:54 +0000608 NamedDecl *ND = i->first.get<NamedDecl *>();
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000609 if (isa<ParmVarDecl>(ND))
Douglas Gregorf3010112011-01-07 16:43:16 +0000610 IsFunctionParameterPack = true;
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000611 else
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000612 std::tie(Depth, Index) = getDepthAndIndex(ND);
613
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000614 Name = ND->getIdentifier();
615 }
616
Douglas Gregorf3010112011-01-07 16:43:16 +0000617 // Determine the size of this argument pack.
618 unsigned NewPackSize;
619 if (IsFunctionParameterPack) {
620 // Figure out whether we're instantiating to an argument pack or not.
621 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
622
623 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
624 = CurrentInstantiationScope->findInstantiationOf(
David Blaikieb9c168a2011-09-22 02:34:54 +0000625 i->first.get<NamedDecl *>());
Chris Lattner15a776f2011-02-17 19:38:27 +0000626 if (Instantiation->is<DeclArgumentPack *>()) {
Douglas Gregorf3010112011-01-07 16:43:16 +0000627 // We could expand this function parameter pack.
628 NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
629 } else {
630 // We can't expand this function parameter pack, so we can't expand
631 // the pack expansion.
632 ShouldExpand = false;
633 continue;
634 }
635 } else {
636 // If we don't have a template argument at this depth/index, then we
637 // cannot expand the pack expansion. Make a note of this, but we still
638 // want to check any parameter packs we *do* have arguments for.
639 if (Depth >= TemplateArgs.getNumLevels() ||
640 !TemplateArgs.hasTemplateArgument(Depth, Index)) {
641 ShouldExpand = false;
642 continue;
643 }
644
645 // Determine the size of the argument pack.
646 NewPackSize = TemplateArgs(Depth, Index).pack_size();
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000647 }
648
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000649 // C++0x [temp.arg.explicit]p9:
650 // Template argument deduction can extend the sequence of template
651 // arguments corresponding to a template parameter pack, even when the
652 // sequence contains explicitly specified template arguments.
Olivier Goffarteeba9e42016-05-26 12:55:34 +0000653 if (!IsFunctionParameterPack && CurrentInstantiationScope) {
Douglas Gregor63dad4d2011-01-20 23:15:49 +0000654 if (NamedDecl *PartialPack
655 = CurrentInstantiationScope->getPartiallySubstitutedPack()){
656 unsigned PartialDepth, PartialIndex;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000657 std::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
Douglas Gregor63dad4d2011-01-20 23:15:49 +0000658 if (PartialDepth == Depth && PartialIndex == Index)
659 RetainExpansion = true;
660 }
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000661 }
Douglas Gregor63dad4d2011-01-20 23:15:49 +0000662
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000663 if (!NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000664 // The is the first pack we've seen for which we have an argument.
665 // Record it.
666 NumExpansions = NewPackSize;
667 FirstPack.first = Name;
David Blaikieb9c168a2011-09-22 02:34:54 +0000668 FirstPack.second = i->second;
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000669 HaveFirstPack = true;
670 continue;
671 }
672
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000673 if (NewPackSize != *NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000674 // C++0x [temp.variadic]p5:
675 // All of the parameter packs expanded by a pack expansion shall have
676 // the same number of arguments specified.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000677 if (HaveFirstPack)
678 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
679 << FirstPack.first << Name << *NumExpansions << NewPackSize
David Blaikieb9c168a2011-09-22 02:34:54 +0000680 << SourceRange(FirstPack.second) << SourceRange(i->second);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000681 else
682 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
683 << Name << *NumExpansions << NewPackSize
David Blaikieb9c168a2011-09-22 02:34:54 +0000684 << SourceRange(i->second);
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000685 return true;
686 }
687 }
Richard Smithc5452ed2016-10-19 22:18:42 +0000688
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000689 return false;
690}
Douglas Gregor27b4c162010-12-23 22:44:42 +0000691
David Blaikie05785d12013-02-20 22:23:23 +0000692Optional<unsigned> Sema::getNumArgumentsInExpansion(QualType T,
Douglas Gregor5cde3862011-01-11 03:14:20 +0000693 const MultiLevelTemplateArgumentList &TemplateArgs) {
694 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000695 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor5cde3862011-01-11 03:14:20 +0000696 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
697
David Blaikie05785d12013-02-20 22:23:23 +0000698 Optional<unsigned> Result;
Douglas Gregor5cde3862011-01-11 03:14:20 +0000699 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
700 // Compute the depth and index for this parameter pack.
701 unsigned Depth;
702 unsigned Index;
703
704 if (const TemplateTypeParmType *TTP
705 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
706 Depth = TTP->getDepth();
707 Index = TTP->getIndex();
708 } else {
709 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
710 if (isa<ParmVarDecl>(ND)) {
711 // Function parameter pack.
712 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
713
714 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
715 = CurrentInstantiationScope->findInstantiationOf(
716 Unexpanded[I].first.get<NamedDecl *>());
Richard Smith198223b2012-07-18 01:29:05 +0000717 if (Instantiation->is<Decl*>())
718 // The pattern refers to an unexpanded pack. We're not ready to expand
719 // this pack yet.
David Blaikie7a30dc52013-02-21 01:47:18 +0000720 return None;
Richard Smith198223b2012-07-18 01:29:05 +0000721
722 unsigned Size = Instantiation->get<DeclArgumentPack *>()->size();
723 assert((!Result || *Result == Size) && "inconsistent pack sizes");
724 Result = Size;
Douglas Gregor5cde3862011-01-11 03:14:20 +0000725 continue;
726 }
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000727
728 std::tie(Depth, Index) = getDepthAndIndex(ND);
Douglas Gregor5cde3862011-01-11 03:14:20 +0000729 }
730 if (Depth >= TemplateArgs.getNumLevels() ||
731 !TemplateArgs.hasTemplateArgument(Depth, Index))
Richard Smith198223b2012-07-18 01:29:05 +0000732 // The pattern refers to an unknown template argument. We're not ready to
733 // expand this pack yet.
David Blaikie7a30dc52013-02-21 01:47:18 +0000734 return None;
Douglas Gregor5cde3862011-01-11 03:14:20 +0000735
736 // Determine the size of the argument pack.
Richard Smith198223b2012-07-18 01:29:05 +0000737 unsigned Size = TemplateArgs(Depth, Index).pack_size();
738 assert((!Result || *Result == Size) && "inconsistent pack sizes");
739 Result = Size;
Douglas Gregor5cde3862011-01-11 03:14:20 +0000740 }
741
Richard Smith198223b2012-07-18 01:29:05 +0000742 return Result;
Douglas Gregor5cde3862011-01-11 03:14:20 +0000743}
744
Douglas Gregor27b4c162010-12-23 22:44:42 +0000745bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
746 const DeclSpec &DS = D.getDeclSpec();
747 switch (DS.getTypeSpecType()) {
748 case TST_typename:
Alexis Hunt4a257072011-05-19 05:37:45 +0000749 case TST_typeofType:
Eli Friedman0dfb8892011-10-06 23:00:33 +0000750 case TST_underlyingType:
751 case TST_atomic: {
Douglas Gregor27b4c162010-12-23 22:44:42 +0000752 QualType T = DS.getRepAsType().get();
753 if (!T.isNull() && T->containsUnexpandedParameterPack())
754 return true;
755 break;
756 }
757
758 case TST_typeofExpr:
759 case TST_decltype:
760 if (DS.getRepAsExpr() &&
761 DS.getRepAsExpr()->containsUnexpandedParameterPack())
762 return true;
763 break;
764
765 case TST_unspecified:
766 case TST_void:
767 case TST_char:
768 case TST_wchar:
769 case TST_char16:
770 case TST_char32:
771 case TST_int:
Richard Smithf016bbc2012-04-04 06:24:32 +0000772 case TST_int128:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000773 case TST_half:
Douglas Gregor27b4c162010-12-23 22:44:42 +0000774 case TST_float:
775 case TST_double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000776 case TST_float128:
Douglas Gregor27b4c162010-12-23 22:44:42 +0000777 case TST_bool:
778 case TST_decimal32:
779 case TST_decimal64:
780 case TST_decimal128:
781 case TST_enum:
782 case TST_union:
783 case TST_struct:
Joao Matosdc86f942012-08-31 18:45:21 +0000784 case TST_interface:
Douglas Gregor27b4c162010-12-23 22:44:42 +0000785 case TST_class:
786 case TST_auto:
Richard Smithe301ba22015-11-11 02:02:15 +0000787 case TST_auto_type:
Richard Smith74aeef52013-04-26 16:15:35 +0000788 case TST_decltype_auto:
Alexey Bader954ba212016-04-08 13:40:33 +0000789#define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +0000790#include "clang/Basic/OpenCLImageTypes.def"
John McCall39439732011-04-09 22:50:59 +0000791 case TST_unknown_anytype:
Douglas Gregor27b4c162010-12-23 22:44:42 +0000792 case TST_error:
793 break;
794 }
Larisse Voufo2e846502014-08-29 21:08:16 +0000795
Douglas Gregor27b4c162010-12-23 22:44:42 +0000796 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
797 const DeclaratorChunk &Chunk = D.getTypeObject(I);
798 switch (Chunk.Kind) {
799 case DeclaratorChunk::Pointer:
800 case DeclaratorChunk::Reference:
801 case DeclaratorChunk::Paren:
Xiuli Pan9c14e282016-01-09 12:53:17 +0000802 case DeclaratorChunk::Pipe:
Larisse Voufo2e846502014-08-29 21:08:16 +0000803 case DeclaratorChunk::BlockPointer:
Douglas Gregor27b4c162010-12-23 22:44:42 +0000804 // These declarator chunks cannot contain any parameter packs.
805 break;
806
807 case DeclaratorChunk::Array:
Larisse Voufo2e846502014-08-29 21:08:16 +0000808 if (Chunk.Arr.NumElts &&
809 Chunk.Arr.NumElts->containsUnexpandedParameterPack())
810 return true;
811 break;
Douglas Gregor27b4c162010-12-23 22:44:42 +0000812 case DeclaratorChunk::Function:
Larisse Voufo2e846502014-08-29 21:08:16 +0000813 for (unsigned i = 0, e = Chunk.Fun.NumParams; i != e; ++i) {
814 ParmVarDecl *Param = cast<ParmVarDecl>(Chunk.Fun.Params[i].Param);
815 QualType ParamTy = Param->getType();
816 assert(!ParamTy.isNull() && "Couldn't parse type?");
817 if (ParamTy->containsUnexpandedParameterPack()) return true;
818 }
819
820 if (Chunk.Fun.getExceptionSpecType() == EST_Dynamic) {
Reid Kleckner078aea92016-12-09 17:14:05 +0000821 for (unsigned i = 0; i != Chunk.Fun.getNumExceptions(); ++i) {
Larisse Voufo2e846502014-08-29 21:08:16 +0000822 if (Chunk.Fun.Exceptions[i]
823 .Ty.get()
824 ->containsUnexpandedParameterPack())
825 return true;
826 }
827 } else if (Chunk.Fun.getExceptionSpecType() == EST_ComputedNoexcept &&
828 Chunk.Fun.NoexceptExpr->containsUnexpandedParameterPack())
829 return true;
830
Nico Weber8d26b722014-12-30 02:06:40 +0000831 if (Chunk.Fun.hasTrailingReturnType()) {
832 QualType T = Chunk.Fun.getTrailingReturnType().get();
833 if (!T.isNull() && T->containsUnexpandedParameterPack())
834 return true;
835 }
Larisse Voufo2e846502014-08-29 21:08:16 +0000836 break;
837
Douglas Gregor27b4c162010-12-23 22:44:42 +0000838 case DeclaratorChunk::MemberPointer:
839 if (Chunk.Mem.Scope().getScopeRep() &&
840 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
841 return true;
842 break;
843 }
844 }
845
846 return false;
847}
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000848
Kaelyn Uhrain637b5b32012-01-13 23:10:36 +0000849namespace {
850
851// Callback to only accept typo corrections that refer to parameter packs.
852class ParameterPackValidatorCCC : public CorrectionCandidateCallback {
853 public:
Craig Toppere14c0f82014-03-12 04:55:44 +0000854 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain637b5b32012-01-13 23:10:36 +0000855 NamedDecl *ND = candidate.getCorrectionDecl();
856 return ND && ND->isParameterPack();
857 }
858};
859
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000860}
Kaelyn Uhrain637b5b32012-01-13 23:10:36 +0000861
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000862/// \brief Called when an expression computing the size of a parameter pack
863/// is parsed.
864///
865/// \code
866/// template<typename ...Types> struct count {
867/// static const unsigned value = sizeof...(Types);
868/// };
869/// \endcode
870///
871//
872/// \param OpLoc The location of the "sizeof" keyword.
873/// \param Name The name of the parameter pack whose size will be determined.
874/// \param NameLoc The source location of the name of the parameter pack.
875/// \param RParenLoc The location of the closing parentheses.
876ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
877 SourceLocation OpLoc,
878 IdentifierInfo &Name,
879 SourceLocation NameLoc,
880 SourceLocation RParenLoc) {
881 // C++0x [expr.sizeof]p5:
882 // The identifier in a sizeof... expression shall name a parameter pack.
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000883 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
884 LookupName(R, S);
Craig Topperc3ec1492014-05-26 06:22:03 +0000885
886 NamedDecl *ParameterPack = nullptr;
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000887 switch (R.getResultKind()) {
888 case LookupResult::Found:
889 ParameterPack = R.getFoundDecl();
890 break;
891
892 case LookupResult::NotFound:
893 case LookupResult::NotFoundInCurrentInstantiation:
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000894 if (TypoCorrection Corrected =
895 CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr,
896 llvm::make_unique<ParameterPackValidatorCCC>(),
897 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000898 diagnoseTypo(Corrected,
899 PDiag(diag::err_sizeof_pack_no_pack_name_suggest) << &Name,
900 PDiag(diag::note_parameter_pack_here));
Kaelyn Uhrain637b5b32012-01-13 23:10:36 +0000901 ParameterPack = Corrected.getCorrectionDecl();
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000902 }
Richard Smithf9b15102013-08-17 00:46:16 +0000903
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000904 case LookupResult::FoundOverloaded:
905 case LookupResult::FoundUnresolvedValue:
906 break;
907
908 case LookupResult::Ambiguous:
909 DiagnoseAmbiguousLookup(R);
910 return ExprError();
911 }
912
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000913 if (!ParameterPack || !ParameterPack->isParameterPack()) {
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000914 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
915 << &Name;
916 return ExprError();
917 }
918
Nick Lewycky45b50522013-02-02 00:25:55 +0000919 MarkAnyDeclReferenced(OpLoc, ParameterPack, true);
Eli Friedman23b1be92012-03-01 21:32:56 +0000920
Richard Smithd784e682015-09-23 21:41:42 +0000921 return SizeOfPackExpr::Create(Context, OpLoc, ParameterPack, NameLoc,
922 RParenLoc);
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000923}
Eli Friedman94e9eaa2013-06-20 04:11:21 +0000924
925TemplateArgumentLoc
926Sema::getTemplateArgumentPackExpansionPattern(
927 TemplateArgumentLoc OrigLoc,
928 SourceLocation &Ellipsis, Optional<unsigned> &NumExpansions) const {
929 const TemplateArgument &Argument = OrigLoc.getArgument();
930 assert(Argument.isPackExpansion());
931 switch (Argument.getKind()) {
932 case TemplateArgument::Type: {
933 // FIXME: We shouldn't ever have to worry about missing
934 // type-source info!
935 TypeSourceInfo *ExpansionTSInfo = OrigLoc.getTypeSourceInfo();
936 if (!ExpansionTSInfo)
937 ExpansionTSInfo = Context.getTrivialTypeSourceInfo(Argument.getAsType(),
938 Ellipsis);
939 PackExpansionTypeLoc Expansion =
940 ExpansionTSInfo->getTypeLoc().castAs<PackExpansionTypeLoc>();
941 Ellipsis = Expansion.getEllipsisLoc();
942
943 TypeLoc Pattern = Expansion.getPatternLoc();
944 NumExpansions = Expansion.getTypePtr()->getNumExpansions();
945
946 // We need to copy the TypeLoc because TemplateArgumentLocs store a
947 // TypeSourceInfo.
948 // FIXME: Find some way to avoid the copy?
949 TypeLocBuilder TLB;
950 TLB.pushFullCopy(Pattern);
951 TypeSourceInfo *PatternTSInfo =
952 TLB.getTypeSourceInfo(Context, Pattern.getType());
953 return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
954 PatternTSInfo);
955 }
956
957 case TemplateArgument::Expression: {
958 PackExpansionExpr *Expansion
959 = cast<PackExpansionExpr>(Argument.getAsExpr());
960 Expr *Pattern = Expansion->getPattern();
961 Ellipsis = Expansion->getEllipsisLoc();
962 NumExpansions = Expansion->getNumExpansions();
963 return TemplateArgumentLoc(Pattern, Pattern);
964 }
965
966 case TemplateArgument::TemplateExpansion:
967 Ellipsis = OrigLoc.getTemplateEllipsisLoc();
968 NumExpansions = Argument.getNumTemplateExpansions();
969 return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
970 OrigLoc.getTemplateQualifierLoc(),
971 OrigLoc.getTemplateNameLoc());
972
973 case TemplateArgument::Declaration:
974 case TemplateArgument::NullPtr:
975 case TemplateArgument::Template:
976 case TemplateArgument::Integral:
977 case TemplateArgument::Pack:
978 case TemplateArgument::Null:
979 return TemplateArgumentLoc();
980 }
981
982 llvm_unreachable("Invalid TemplateArgument Kind!");
983}
Richard Smith0f0af192014-11-08 05:07:16 +0000984
Richard Smithc5452ed2016-10-19 22:18:42 +0000985Optional<unsigned> Sema::getFullyPackExpandedSize(TemplateArgument Arg) {
986 assert(Arg.containsUnexpandedParameterPack());
987
988 // If this is a substituted pack, grab that pack. If not, we don't know
989 // the size yet.
990 // FIXME: We could find a size in more cases by looking for a substituted
991 // pack anywhere within this argument, but that's not necessary in the common
992 // case for 'sizeof...(A)' handling.
993 TemplateArgument Pack;
994 switch (Arg.getKind()) {
995 case TemplateArgument::Type:
996 if (auto *Subst = Arg.getAsType()->getAs<SubstTemplateTypeParmPackType>())
997 Pack = Subst->getArgumentPack();
998 else
999 return None;
1000 break;
1001
1002 case TemplateArgument::Expression:
1003 if (auto *Subst =
1004 dyn_cast<SubstNonTypeTemplateParmPackExpr>(Arg.getAsExpr()))
1005 Pack = Subst->getArgumentPack();
1006 else if (auto *Subst = dyn_cast<FunctionParmPackExpr>(Arg.getAsExpr())) {
1007 for (ParmVarDecl *PD : *Subst)
1008 if (PD->isParameterPack())
1009 return None;
1010 return Subst->getNumExpansions();
1011 } else
1012 return None;
1013 break;
1014
1015 case TemplateArgument::Template:
1016 if (SubstTemplateTemplateParmPackStorage *Subst =
1017 Arg.getAsTemplate().getAsSubstTemplateTemplateParmPack())
1018 Pack = Subst->getArgumentPack();
1019 else
1020 return None;
1021 break;
1022
1023 case TemplateArgument::Declaration:
1024 case TemplateArgument::NullPtr:
1025 case TemplateArgument::TemplateExpansion:
1026 case TemplateArgument::Integral:
1027 case TemplateArgument::Pack:
1028 case TemplateArgument::Null:
1029 return None;
1030 }
1031
1032 // Check that no argument in the pack is itself a pack expansion.
1033 for (TemplateArgument Elem : Pack.pack_elements()) {
1034 // There's no point recursing in this case; we would have already
1035 // expanded this pack expansion into the enclosing pack if we could.
1036 if (Elem.isPackExpansion())
1037 return None;
1038 }
1039 return Pack.pack_size();
1040}
1041
Richard Smith0f0af192014-11-08 05:07:16 +00001042static void CheckFoldOperand(Sema &S, Expr *E) {
1043 if (!E)
1044 return;
1045
1046 E = E->IgnoreImpCasts();
Richard Smith66094432016-10-20 00:55:15 +00001047 auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
1048 if ((OCE && OCE->isInfixBinaryOp()) || isa<BinaryOperator>(E) ||
1049 isa<AbstractConditionalOperator>(E)) {
Richard Smith0f0af192014-11-08 05:07:16 +00001050 S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
1051 << E->getSourceRange()
1052 << FixItHint::CreateInsertion(E->getLocStart(), "(")
1053 << FixItHint::CreateInsertion(E->getLocEnd(), ")");
1054 }
1055}
1056
1057ExprResult Sema::ActOnCXXFoldExpr(SourceLocation LParenLoc, Expr *LHS,
1058 tok::TokenKind Operator,
1059 SourceLocation EllipsisLoc, Expr *RHS,
1060 SourceLocation RParenLoc) {
1061 // LHS and RHS must be cast-expressions. We allow an arbitrary expression
1062 // in the parser and reduce down to just cast-expressions here.
1063 CheckFoldOperand(*this, LHS);
1064 CheckFoldOperand(*this, RHS);
1065
Richard Smith90e043d2017-02-15 19:57:10 +00001066 auto DiscardOperands = [&] {
1067 CorrectDelayedTyposInExpr(LHS);
1068 CorrectDelayedTyposInExpr(RHS);
1069 };
1070
Richard Smith0f0af192014-11-08 05:07:16 +00001071 // [expr.prim.fold]p3:
1072 // In a binary fold, op1 and op2 shall be the same fold-operator, and
1073 // either e1 shall contain an unexpanded parameter pack or e2 shall contain
1074 // an unexpanded parameter pack, but not both.
1075 if (LHS && RHS &&
1076 LHS->containsUnexpandedParameterPack() ==
1077 RHS->containsUnexpandedParameterPack()) {
Richard Smith90e043d2017-02-15 19:57:10 +00001078 DiscardOperands();
Richard Smith0f0af192014-11-08 05:07:16 +00001079 return Diag(EllipsisLoc,
1080 LHS->containsUnexpandedParameterPack()
1081 ? diag::err_fold_expression_packs_both_sides
1082 : diag::err_pack_expansion_without_parameter_packs)
1083 << LHS->getSourceRange() << RHS->getSourceRange();
1084 }
1085
1086 // [expr.prim.fold]p2:
1087 // In a unary fold, the cast-expression shall contain an unexpanded
1088 // parameter pack.
1089 if (!LHS || !RHS) {
1090 Expr *Pack = LHS ? LHS : RHS;
1091 assert(Pack && "fold expression with neither LHS nor RHS");
Richard Smith90e043d2017-02-15 19:57:10 +00001092 DiscardOperands();
Richard Smith0f0af192014-11-08 05:07:16 +00001093 if (!Pack->containsUnexpandedParameterPack())
1094 return Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
1095 << Pack->getSourceRange();
1096 }
1097
1098 BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Operator);
1099 return BuildCXXFoldExpr(LParenLoc, LHS, Opc, EllipsisLoc, RHS, RParenLoc);
1100}
1101
1102ExprResult Sema::BuildCXXFoldExpr(SourceLocation LParenLoc, Expr *LHS,
1103 BinaryOperatorKind Operator,
1104 SourceLocation EllipsisLoc, Expr *RHS,
1105 SourceLocation RParenLoc) {
1106 return new (Context) CXXFoldExpr(Context.DependentTy, LParenLoc, LHS,
1107 Operator, EllipsisLoc, RHS, RParenLoc);
1108}
1109
1110ExprResult Sema::BuildEmptyCXXFoldExpr(SourceLocation EllipsisLoc,
1111 BinaryOperatorKind Operator) {
1112 // [temp.variadic]p9:
1113 // If N is zero for a unary fold-expression, the value of the expression is
Richard Smith0f0af192014-11-08 05:07:16 +00001114 // && -> true
1115 // || -> false
1116 // , -> void()
1117 // if the operator is not listed [above], the instantiation is ill-formed.
1118 //
1119 // Note that we need to use something like int() here, not merely 0, to
1120 // prevent the result from being a null pointer constant.
1121 QualType ScalarType;
1122 switch (Operator) {
Richard Smith0f0af192014-11-08 05:07:16 +00001123 case BO_LOr:
1124 return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_false);
1125 case BO_LAnd:
1126 return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_true);
1127 case BO_Comma:
1128 ScalarType = Context.VoidTy;
1129 break;
1130
1131 default:
1132 return Diag(EllipsisLoc, diag::err_fold_expression_empty)
1133 << BinaryOperator::getOpcodeStr(Operator);
1134 }
1135
1136 return new (Context) CXXScalarValueInitExpr(
1137 ScalarType, Context.getTrivialTypeSourceInfo(ScalarType, EllipsisLoc),
1138 EllipsisLoc);
1139}