blob: 78aa7f893a1a9e26225d9c7344641e18441eac4a [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000013#include "clang/AST/Expr.h"
14#include "clang/AST/RecursiveASTVisitor.h"
15#include "clang/AST/TypeLoc.h"
Douglas Gregoree8aff02011-01-04 17:33:58 +000016#include "clang/Sema/Lookup.h"
Douglas Gregor7536dd52010-12-20 02:24:11 +000017#include "clang/Sema/ParsedTemplate.h"
Richard Smith612409e2012-07-25 03:56:55 +000018#include "clang/Sema/ScopeInfo.h"
Douglas Gregorc4633352010-12-15 17:38:57 +000019#include "clang/Sema/SemaInternal.h"
Douglas Gregor8491ffe2010-12-20 22:05:00 +000020#include "clang/Sema/Template.h"
Eli Friedman44ee0a72013-06-07 20:31:48 +000021#include "TypeLocBuilder.h"
Douglas Gregorc4633352010-12-15 17:38:57 +000022
23using namespace clang;
24
Douglas Gregor9ef75892010-12-15 19:43:21 +000025//----------------------------------------------------------------------------
26// Visitor that collects unexpanded parameter packs
27//----------------------------------------------------------------------------
28
Douglas Gregor9ef75892010-12-15 19:43:21 +000029namespace {
30 /// \brief A class that collects unexpanded parameter packs.
31 class CollectUnexpandedParameterPacksVisitor :
32 public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
33 {
34 typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
35 inherited;
36
Chris Lattner5f9e2722011-07-23 10:55:15 +000037 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +000038
Richard Smith612409e2012-07-25 03:56:55 +000039 bool InLambda;
40
Douglas Gregor9ef75892010-12-15 19:43:21 +000041 public:
42 explicit CollectUnexpandedParameterPacksVisitor(
Chris Lattner5f9e2722011-07-23 10:55:15 +000043 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
Richard Smith612409e2012-07-25 03:56:55 +000044 : Unexpanded(Unexpanded), InLambda(false) { }
Douglas Gregor9ef75892010-12-15 19:43:21 +000045
Douglas Gregora40bc722010-12-20 23:07:20 +000046 bool shouldWalkTypesOfTypeLocs() const { return false; }
47
Douglas Gregor9ef75892010-12-15 19:43:21 +000048 //------------------------------------------------------------------------
49 // Recording occurrences of (unexpanded) parameter packs.
50 //------------------------------------------------------------------------
51
52 /// \brief Record occurrences of template type parameter packs.
53 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
54 if (TL.getTypePtr()->isParameterPack())
55 Unexpanded.push_back(std::make_pair(TL.getTypePtr(), TL.getNameLoc()));
56 return true;
57 }
58
59 /// \brief Record occurrences of template type parameter packs
60 /// when we don't have proper source-location information for
61 /// them.
62 ///
63 /// Ideally, this routine would never be used.
64 bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
65 if (T->isParameterPack())
66 Unexpanded.push_back(std::make_pair(T, SourceLocation()));
67
68 return true;
69 }
70
Douglas Gregora779d9c2011-01-19 21:32:01 +000071 /// \brief Record occurrences of function and non-type template
Douglas Gregor10738d32010-12-23 23:51:58 +000072 /// parameter packs in an expression.
73 bool VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor12c9c002011-01-07 16:43:16 +000074 if (E->getDecl()->isParameterPack())
75 Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation()));
Douglas Gregor10738d32010-12-23 23:51:58 +000076
77 return true;
78 }
79
Douglas Gregor61c4d282011-01-05 15:48:55 +000080 /// \brief Record occurrences of template template parameter packs.
81 bool TraverseTemplateName(TemplateName Template) {
82 if (TemplateTemplateParmDecl *TTP
83 = dyn_cast_or_null<TemplateTemplateParmDecl>(
84 Template.getAsTemplateDecl()))
85 if (TTP->isParameterPack())
86 Unexpanded.push_back(std::make_pair(TTP, SourceLocation()));
87
88 return inherited::TraverseTemplateName(Template);
89 }
Douglas Gregor9ef75892010-12-15 19:43:21 +000090
Ted Kremenekebcb57a2012-03-06 20:05:56 +000091 /// \brief Suppress traversal into Objective-C container literal
92 /// elements that are pack expansions.
93 bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
94 if (!E->containsUnexpandedParameterPack())
95 return true;
96
97 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
98 ObjCDictionaryElement Element = E->getKeyValueElement(I);
99 if (Element.isPackExpansion())
100 continue;
101
102 TraverseStmt(Element.Key);
103 TraverseStmt(Element.Value);
104 }
105 return true;
106 }
Douglas Gregor9ef75892010-12-15 19:43:21 +0000107 //------------------------------------------------------------------------
108 // Pruning the search for unexpanded parameter packs.
109 //------------------------------------------------------------------------
110
111 /// \brief Suppress traversal into statements and expressions that
112 /// do not contain unexpanded parameter packs.
113 bool TraverseStmt(Stmt *S) {
Richard Smith612409e2012-07-25 03:56:55 +0000114 Expr *E = dyn_cast_or_null<Expr>(S);
115 if ((E && E->containsUnexpandedParameterPack()) || InLambda)
116 return inherited::TraverseStmt(S);
Douglas Gregor9ef75892010-12-15 19:43:21 +0000117
Richard Smith612409e2012-07-25 03:56:55 +0000118 return true;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000119 }
120
121 /// \brief Suppress traversal into types that do not contain
122 /// unexpanded parameter packs.
123 bool TraverseType(QualType T) {
Richard Smith612409e2012-07-25 03:56:55 +0000124 if ((!T.isNull() && T->containsUnexpandedParameterPack()) || InLambda)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000125 return inherited::TraverseType(T);
126
127 return true;
128 }
129
130 /// \brief Suppress traversel into types with location information
131 /// that do not contain unexpanded parameter packs.
132 bool TraverseTypeLoc(TypeLoc TL) {
Richard Smith612409e2012-07-25 03:56:55 +0000133 if ((!TL.getType().isNull() &&
134 TL.getType()->containsUnexpandedParameterPack()) ||
135 InLambda)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000136 return inherited::TraverseTypeLoc(TL);
137
138 return true;
139 }
140
Douglas Gregorcff163e2010-12-15 21:57:59 +0000141 /// \brief Suppress traversal of non-parameter declarations, since
142 /// they cannot contain unexpanded parameter packs.
143 bool TraverseDecl(Decl *D) {
Richard Smith612409e2012-07-25 03:56:55 +0000144 if ((D && isa<ParmVarDecl>(D)) || InLambda)
Douglas Gregorcff163e2010-12-15 21:57:59 +0000145 return inherited::TraverseDecl(D);
146
Richard Smith612409e2012-07-25 03:56:55 +0000147 return true;
Douglas Gregorcff163e2010-12-15 21:57:59 +0000148 }
Douglas Gregorba68eca2011-01-05 17:40:24 +0000149
150 /// \brief Suppress traversal of template argument pack expansions.
151 bool TraverseTemplateArgument(const TemplateArgument &Arg) {
152 if (Arg.isPackExpansion())
153 return true;
154
155 return inherited::TraverseTemplateArgument(Arg);
156 }
157
158 /// \brief Suppress traversal of template argument pack expansions.
159 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
160 if (ArgLoc.getArgument().isPackExpansion())
161 return true;
162
163 return inherited::TraverseTemplateArgumentLoc(ArgLoc);
164 }
Richard Smith612409e2012-07-25 03:56:55 +0000165
166 /// \brief Note whether we're traversing a lambda containing an unexpanded
167 /// parameter pack. In this case, the unexpanded pack can occur anywhere,
168 /// including all the places where we normally wouldn't look. Within a
169 /// lambda, we don't propagate the 'contains unexpanded parameter pack' bit
170 /// outside an expression.
171 bool TraverseLambdaExpr(LambdaExpr *Lambda) {
172 // The ContainsUnexpandedParameterPack bit on a lambda is always correct,
173 // even if it's contained within another lambda.
174 if (!Lambda->containsUnexpandedParameterPack())
175 return true;
176
177 bool WasInLambda = InLambda;
178 InLambda = true;
179
180 // If any capture names a function parameter pack, that pack is expanded
181 // when the lambda is expanded.
182 for (LambdaExpr::capture_iterator I = Lambda->capture_begin(),
Richard Smith0d8e9642013-05-16 06:20:58 +0000183 E = Lambda->capture_end();
184 I != E; ++I) {
185 if (I->capturesVariable()) {
186 VarDecl *VD = I->getCapturedVar();
Richard Smith612409e2012-07-25 03:56:55 +0000187 if (VD->isParameterPack())
188 Unexpanded.push_back(std::make_pair(VD, I->getLocation()));
Richard Smith0d8e9642013-05-16 06:20:58 +0000189 }
190 }
Richard Smith612409e2012-07-25 03:56:55 +0000191
192 inherited::TraverseLambdaExpr(Lambda);
193
194 InLambda = WasInLambda;
195 return true;
196 }
Douglas Gregor9ef75892010-12-15 19:43:21 +0000197 };
198}
199
200/// \brief Diagnose all of the unexpanded parameter packs in the given
201/// vector.
Richard Smith612409e2012-07-25 03:56:55 +0000202bool
Douglas Gregor65019ac2011-10-25 03:44:56 +0000203Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
204 UnexpandedParameterPackContext UPPC,
Bill Wendling4fe5be02012-02-22 09:38:11 +0000205 ArrayRef<UnexpandedParameterPack> Unexpanded) {
Douglas Gregor65019ac2011-10-25 03:44:56 +0000206 if (Unexpanded.empty())
Richard Smith612409e2012-07-25 03:56:55 +0000207 return false;
208
209 // If we are within a lambda expression, that lambda contains an unexpanded
210 // parameter pack, and we are done.
211 // FIXME: Store 'Unexpanded' on the lambda so we don't need to recompute it
212 // later.
213 for (unsigned N = FunctionScopes.size(); N; --N) {
214 if (sema::LambdaScopeInfo *LSI =
215 dyn_cast<sema::LambdaScopeInfo>(FunctionScopes[N-1])) {
216 LSI->ContainsUnexpandedParameterPack = true;
217 return false;
218 }
219 }
Douglas Gregor65019ac2011-10-25 03:44:56 +0000220
Chris Lattner5f9e2722011-07-23 10:55:15 +0000221 SmallVector<SourceLocation, 4> Locations;
222 SmallVector<IdentifierInfo *, 4> Names;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000223 llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
224
225 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
226 IdentifierInfo *Name = 0;
227 if (const TemplateTypeParmType *TTP
228 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
Chandler Carruthb7efff42011-05-01 01:05:51 +0000229 Name = TTP->getIdentifier();
Douglas Gregor9ef75892010-12-15 19:43:21 +0000230 else
231 Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
232
233 if (Name && NamesKnown.insert(Name))
234 Names.push_back(Name);
235
236 if (Unexpanded[I].second.isValid())
237 Locations.push_back(Unexpanded[I].second);
238 }
239
240 DiagnosticBuilder DB
Douglas Gregor65019ac2011-10-25 03:44:56 +0000241 = Names.size() == 0? Diag(Loc, diag::err_unexpanded_parameter_pack_0)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000242 << (int)UPPC
Douglas Gregor65019ac2011-10-25 03:44:56 +0000243 : Names.size() == 1? Diag(Loc, diag::err_unexpanded_parameter_pack_1)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000244 << (int)UPPC << Names[0]
Douglas Gregor65019ac2011-10-25 03:44:56 +0000245 : Names.size() == 2? Diag(Loc, diag::err_unexpanded_parameter_pack_2)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000246 << (int)UPPC << Names[0] << Names[1]
Douglas Gregor65019ac2011-10-25 03:44:56 +0000247 : Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more)
Douglas Gregor9ef75892010-12-15 19:43:21 +0000248 << (int)UPPC << Names[0] << Names[1];
249
250 for (unsigned I = 0, N = Locations.size(); I != N; ++I)
251 DB << SourceRange(Locations[I]);
Richard Smith612409e2012-07-25 03:56:55 +0000252 return true;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000253}
254
Douglas Gregorc4633352010-12-15 17:38:57 +0000255bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
256 TypeSourceInfo *T,
257 UnexpandedParameterPackContext UPPC) {
258 // C++0x [temp.variadic]p5:
259 // An appearance of a name of a parameter pack that is not expanded is
260 // ill-formed.
261 if (!T->getType()->containsUnexpandedParameterPack())
262 return false;
263
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000265 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
266 T->getTypeLoc());
267 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith612409e2012-07-25 03:56:55 +0000268 return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000269}
270
271bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
Douglas Gregor56c04582010-12-16 00:46:58 +0000272 UnexpandedParameterPackContext UPPC) {
Douglas Gregorc4633352010-12-15 17:38:57 +0000273 // C++0x [temp.variadic]p5:
274 // An appearance of a name of a parameter pack that is not expanded is
275 // ill-formed.
276 if (!E->containsUnexpandedParameterPack())
277 return false;
278
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor9ef75892010-12-15 19:43:21 +0000280 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
281 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith612409e2012-07-25 03:56:55 +0000282 return DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded);
Douglas Gregorc4633352010-12-15 17:38:57 +0000283}
Douglas Gregor56c04582010-12-16 00:46:58 +0000284
285bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
286 UnexpandedParameterPackContext UPPC) {
287 // C++0x [temp.variadic]p5:
288 // An appearance of a name of a parameter pack that is not expanded is
289 // ill-formed.
290 if (!SS.getScopeRep() ||
291 !SS.getScopeRep()->containsUnexpandedParameterPack())
292 return false;
293
Chris Lattner5f9e2722011-07-23 10:55:15 +0000294 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000295 CollectUnexpandedParameterPacksVisitor(Unexpanded)
296 .TraverseNestedNameSpecifier(SS.getScopeRep());
297 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith612409e2012-07-25 03:56:55 +0000298 return DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
299 UPPC, Unexpanded);
Douglas Gregor56c04582010-12-16 00:46:58 +0000300}
301
302bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
303 UnexpandedParameterPackContext UPPC) {
304 // C++0x [temp.variadic]p5:
305 // An appearance of a name of a parameter pack that is not expanded is
306 // ill-formed.
307 switch (NameInfo.getName().getNameKind()) {
308 case DeclarationName::Identifier:
309 case DeclarationName::ObjCZeroArgSelector:
310 case DeclarationName::ObjCOneArgSelector:
311 case DeclarationName::ObjCMultiArgSelector:
312 case DeclarationName::CXXOperatorName:
313 case DeclarationName::CXXLiteralOperatorName:
314 case DeclarationName::CXXUsingDirective:
315 return false;
316
317 case DeclarationName::CXXConstructorName:
318 case DeclarationName::CXXDestructorName:
319 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor099ffe82010-12-16 17:19:19 +0000320 // FIXME: We shouldn't need this null check!
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000321 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
322 return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
323
324 if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
Douglas Gregor56c04582010-12-16 00:46:58 +0000325 return false;
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000326
Douglas Gregor56c04582010-12-16 00:46:58 +0000327 break;
328 }
329
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor56c04582010-12-16 00:46:58 +0000331 CollectUnexpandedParameterPacksVisitor(Unexpanded)
Douglas Gregor0762bfd2010-12-16 01:40:04 +0000332 .TraverseType(NameInfo.getName().getCXXNameType());
Douglas Gregor56c04582010-12-16 00:46:58 +0000333 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith612409e2012-07-25 03:56:55 +0000334 return DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
Douglas Gregor56c04582010-12-16 00:46:58 +0000335}
Douglas Gregor6f526752010-12-16 08:48:57 +0000336
337bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
338 TemplateName Template,
339 UnexpandedParameterPackContext UPPC) {
340
341 if (Template.isNull() || !Template.containsUnexpandedParameterPack())
342 return false;
343
Chris Lattner5f9e2722011-07-23 10:55:15 +0000344 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6f526752010-12-16 08:48:57 +0000345 CollectUnexpandedParameterPacksVisitor(Unexpanded)
346 .TraverseTemplateName(Template);
347 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith612409e2012-07-25 03:56:55 +0000348 return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
Douglas Gregor6f526752010-12-16 08:48:57 +0000349}
350
Douglas Gregor925910d2011-01-03 20:35:03 +0000351bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
352 UnexpandedParameterPackContext UPPC) {
353 if (Arg.getArgument().isNull() ||
354 !Arg.getArgument().containsUnexpandedParameterPack())
355 return false;
356
Chris Lattner5f9e2722011-07-23 10:55:15 +0000357 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor925910d2011-01-03 20:35:03 +0000358 CollectUnexpandedParameterPacksVisitor(Unexpanded)
359 .TraverseTemplateArgumentLoc(Arg);
360 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Richard Smith612409e2012-07-25 03:56:55 +0000361 return DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
Douglas Gregor925910d2011-01-03 20:35:03 +0000362}
363
Douglas Gregore02e2622010-12-22 21:19:48 +0000364void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000365 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregore02e2622010-12-22 21:19:48 +0000366 CollectUnexpandedParameterPacksVisitor(Unexpanded)
367 .TraverseTemplateArgument(Arg);
368}
369
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000370void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000371 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000372 CollectUnexpandedParameterPacksVisitor(Unexpanded)
373 .TraverseTemplateArgumentLoc(Arg);
374}
375
Douglas Gregorb99268b2010-12-21 00:52:54 +0000376void Sema::collectUnexpandedParameterPacks(QualType T,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000378 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
379}
380
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000381void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000382 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000383 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
384}
385
Douglas Gregor65019ac2011-10-25 03:44:56 +0000386void Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS,
387 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
388 NestedNameSpecifier *Qualifier = SS.getScopeRep();
389 if (!Qualifier)
390 return;
391
392 NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data());
393 CollectUnexpandedParameterPacksVisitor(Unexpanded)
394 .TraverseNestedNameSpecifierLoc(QualifierLoc);
395}
396
397void Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo,
398 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
399 CollectUnexpandedParameterPacksVisitor(Unexpanded)
400 .TraverseDeclarationNameInfo(NameInfo);
401}
402
403
Douglas Gregor7536dd52010-12-20 02:24:11 +0000404ParsedTemplateArgument
405Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
406 SourceLocation EllipsisLoc) {
407 if (Arg.isInvalid())
408 return Arg;
409
410 switch (Arg.getKind()) {
411 case ParsedTemplateArgument::Type: {
412 TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
413 if (Result.isInvalid())
414 return ParsedTemplateArgument();
415
416 return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
417 Arg.getLocation());
418 }
419
Douglas Gregorbe230c32011-01-03 17:17:50 +0000420 case ParsedTemplateArgument::NonType: {
421 ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
422 if (Result.isInvalid())
423 return ParsedTemplateArgument();
424
425 return ParsedTemplateArgument(Arg.getKind(), Result.get(),
426 Arg.getLocation());
427 }
428
Douglas Gregor7536dd52010-12-20 02:24:11 +0000429 case ParsedTemplateArgument::Template:
Douglas Gregorba68eca2011-01-05 17:40:24 +0000430 if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
431 SourceRange R(Arg.getLocation());
432 if (Arg.getScopeSpec().isValid())
433 R.setBegin(Arg.getScopeSpec().getBeginLoc());
434 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
435 << R;
436 return ParsedTemplateArgument();
437 }
438
439 return Arg.getTemplatePackExpansion(EllipsisLoc);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000440 }
441 llvm_unreachable("Unhandled template argument kind?");
Douglas Gregor7536dd52010-12-20 02:24:11 +0000442}
443
444TypeResult Sema::ActOnPackExpansion(ParsedType Type,
445 SourceLocation EllipsisLoc) {
446 TypeSourceInfo *TSInfo;
447 GetTypeFromParser(Type, &TSInfo);
448 if (!TSInfo)
449 return true;
450
David Blaikie66874fb2013-02-21 01:47:18 +0000451 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, None);
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000452 if (!TSResult)
453 return true;
454
455 return CreateParsedType(TSResult->getType(), TSResult);
456}
457
David Blaikiedc84cd52013-02-20 22:23:23 +0000458TypeSourceInfo *
459Sema::CheckPackExpansion(TypeSourceInfo *Pattern, SourceLocation EllipsisLoc,
460 Optional<unsigned> NumExpansions) {
Douglas Gregor7536dd52010-12-20 02:24:11 +0000461 // Create the pack expansion type and source-location information.
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000462 QualType Result = CheckPackExpansion(Pattern->getType(),
463 Pattern->getTypeLoc().getSourceRange(),
Douglas Gregorcded4f62011-01-14 17:04:44 +0000464 EllipsisLoc, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000465 if (Result.isNull())
466 return 0;
Eli Friedman44ee0a72013-06-07 20:31:48 +0000467
468 TypeLocBuilder TLB;
469 TLB.pushFullCopy(Pattern->getTypeLoc());
470 PackExpansionTypeLoc TL = TLB.push<PackExpansionTypeLoc>(Result);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000471 TL.setEllipsisLoc(EllipsisLoc);
Eli Friedman44ee0a72013-06-07 20:31:48 +0000472
473 return TLB.getTypeSourceInfo(Context, Result);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000474}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000475
David Blaikiedc84cd52013-02-20 22:23:23 +0000476QualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000477 SourceLocation EllipsisLoc,
David Blaikiedc84cd52013-02-20 22:23:23 +0000478 Optional<unsigned> NumExpansions) {
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000479 // C++0x [temp.variadic]p5:
480 // The pattern of a pack expansion shall name one or more
481 // parameter packs that are not expanded by a nested pack
482 // expansion.
483 if (!Pattern->containsUnexpandedParameterPack()) {
484 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
485 << PatternRange;
486 return QualType();
487 }
488
Douglas Gregorcded4f62011-01-14 17:04:44 +0000489 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor2fc1bb72011-01-12 17:07:58 +0000490}
491
Douglas Gregorbe230c32011-01-03 17:17:50 +0000492ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
David Blaikie66874fb2013-02-21 01:47:18 +0000493 return CheckPackExpansion(Pattern, EllipsisLoc, None);
Douglas Gregor67fd1252011-01-14 21:20:45 +0000494}
495
496ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
David Blaikiedc84cd52013-02-20 22:23:23 +0000497 Optional<unsigned> NumExpansions) {
Douglas Gregorbe230c32011-01-03 17:17:50 +0000498 if (!Pattern)
499 return ExprError();
500
501 // C++0x [temp.variadic]p5:
502 // The pattern of a pack expansion shall name one or more
503 // parameter packs that are not expanded by a nested pack
504 // expansion.
505 if (!Pattern->containsUnexpandedParameterPack()) {
506 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
507 << Pattern->getSourceRange();
508 return ExprError();
509 }
510
511 // Create the pack expansion expression and source-location information.
512 return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
Douglas Gregor67fd1252011-01-14 21:20:45 +0000513 EllipsisLoc, NumExpansions));
Douglas Gregorbe230c32011-01-03 17:17:50 +0000514}
Douglas Gregorb99268b2010-12-21 00:52:54 +0000515
Douglas Gregord3731192011-01-10 07:32:04 +0000516/// \brief Retrieve the depth and index of a parameter pack.
517static std::pair<unsigned, unsigned>
518getDepthAndIndex(NamedDecl *ND) {
519 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
520 return std::make_pair(TTP->getDepth(), TTP->getIndex());
521
522 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
523 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
524
525 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
526 return std::make_pair(TTP->getDepth(), TTP->getIndex());
527}
528
David Blaikiedc84cd52013-02-20 22:23:23 +0000529bool Sema::CheckParameterPacksForExpansion(
530 SourceLocation EllipsisLoc, SourceRange PatternRange,
531 ArrayRef<UnexpandedParameterPack> Unexpanded,
532 const MultiLevelTemplateArgumentList &TemplateArgs, bool &ShouldExpand,
533 bool &RetainExpansion, Optional<unsigned> &NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000534 ShouldExpand = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000535 RetainExpansion = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000536 std::pair<IdentifierInfo *, SourceLocation> FirstPack;
537 bool HaveFirstPack = false;
538
David Blaikiea71f9d02011-09-22 02:34:54 +0000539 for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
540 end = Unexpanded.end();
541 i != end; ++i) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000542 // Compute the depth and index for this parameter pack.
Ted Kremenek9577abc2011-01-23 17:04:59 +0000543 unsigned Depth = 0, Index = 0;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000544 IdentifierInfo *Name;
Douglas Gregor12c9c002011-01-07 16:43:16 +0000545 bool IsFunctionParameterPack = false;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000546
547 if (const TemplateTypeParmType *TTP
David Blaikiea71f9d02011-09-22 02:34:54 +0000548 = i->first.dyn_cast<const TemplateTypeParmType *>()) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000549 Depth = TTP->getDepth();
550 Index = TTP->getIndex();
Chandler Carruthb7efff42011-05-01 01:05:51 +0000551 Name = TTP->getIdentifier();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000552 } else {
David Blaikiea71f9d02011-09-22 02:34:54 +0000553 NamedDecl *ND = i->first.get<NamedDecl *>();
Douglas Gregord3731192011-01-10 07:32:04 +0000554 if (isa<ParmVarDecl>(ND))
Douglas Gregor12c9c002011-01-07 16:43:16 +0000555 IsFunctionParameterPack = true;
Douglas Gregord3731192011-01-10 07:32:04 +0000556 else
557 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
558
Douglas Gregorb99268b2010-12-21 00:52:54 +0000559 Name = ND->getIdentifier();
560 }
561
Douglas Gregor12c9c002011-01-07 16:43:16 +0000562 // Determine the size of this argument pack.
563 unsigned NewPackSize;
564 if (IsFunctionParameterPack) {
565 // Figure out whether we're instantiating to an argument pack or not.
566 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
567
568 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
569 = CurrentInstantiationScope->findInstantiationOf(
David Blaikiea71f9d02011-09-22 02:34:54 +0000570 i->first.get<NamedDecl *>());
Chris Lattnera70062f2011-02-17 19:38:27 +0000571 if (Instantiation->is<DeclArgumentPack *>()) {
Douglas Gregor12c9c002011-01-07 16:43:16 +0000572 // We could expand this function parameter pack.
573 NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
574 } else {
575 // We can't expand this function parameter pack, so we can't expand
576 // the pack expansion.
577 ShouldExpand = false;
578 continue;
579 }
580 } else {
581 // If we don't have a template argument at this depth/index, then we
582 // cannot expand the pack expansion. Make a note of this, but we still
583 // want to check any parameter packs we *do* have arguments for.
584 if (Depth >= TemplateArgs.getNumLevels() ||
585 !TemplateArgs.hasTemplateArgument(Depth, Index)) {
586 ShouldExpand = false;
587 continue;
588 }
589
590 // Determine the size of the argument pack.
591 NewPackSize = TemplateArgs(Depth, Index).pack_size();
Douglas Gregorb99268b2010-12-21 00:52:54 +0000592 }
593
Douglas Gregord3731192011-01-10 07:32:04 +0000594 // C++0x [temp.arg.explicit]p9:
595 // Template argument deduction can extend the sequence of template
596 // arguments corresponding to a template parameter pack, even when the
597 // sequence contains explicitly specified template arguments.
Douglas Gregor8619edd2011-01-20 23:15:49 +0000598 if (!IsFunctionParameterPack) {
599 if (NamedDecl *PartialPack
600 = CurrentInstantiationScope->getPartiallySubstitutedPack()){
601 unsigned PartialDepth, PartialIndex;
602 llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
603 if (PartialDepth == Depth && PartialIndex == Index)
604 RetainExpansion = true;
605 }
Douglas Gregord3731192011-01-10 07:32:04 +0000606 }
Douglas Gregor8619edd2011-01-20 23:15:49 +0000607
Douglas Gregorcded4f62011-01-14 17:04:44 +0000608 if (!NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000609 // The is the first pack we've seen for which we have an argument.
610 // Record it.
611 NumExpansions = NewPackSize;
612 FirstPack.first = Name;
David Blaikiea71f9d02011-09-22 02:34:54 +0000613 FirstPack.second = i->second;
Douglas Gregorb99268b2010-12-21 00:52:54 +0000614 HaveFirstPack = true;
615 continue;
616 }
617
Douglas Gregorcded4f62011-01-14 17:04:44 +0000618 if (NewPackSize != *NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000619 // C++0x [temp.variadic]p5:
620 // All of the parameter packs expanded by a pack expansion shall have
621 // the same number of arguments specified.
Douglas Gregorcded4f62011-01-14 17:04:44 +0000622 if (HaveFirstPack)
623 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
624 << FirstPack.first << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000625 << SourceRange(FirstPack.second) << SourceRange(i->second);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000626 else
627 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
628 << Name << *NumExpansions << NewPackSize
David Blaikiea71f9d02011-09-22 02:34:54 +0000629 << SourceRange(i->second);
Douglas Gregorb99268b2010-12-21 00:52:54 +0000630 return true;
631 }
632 }
633
634 return false;
635}
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000636
David Blaikiedc84cd52013-02-20 22:23:23 +0000637Optional<unsigned> Sema::getNumArgumentsInExpansion(QualType T,
Douglas Gregor21371ea2011-01-11 03:14:20 +0000638 const MultiLevelTemplateArgumentList &TemplateArgs) {
639 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000640 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000641 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
642
David Blaikiedc84cd52013-02-20 22:23:23 +0000643 Optional<unsigned> Result;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000644 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
645 // Compute the depth and index for this parameter pack.
646 unsigned Depth;
647 unsigned Index;
648
649 if (const TemplateTypeParmType *TTP
650 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
651 Depth = TTP->getDepth();
652 Index = TTP->getIndex();
653 } else {
654 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
655 if (isa<ParmVarDecl>(ND)) {
656 // Function parameter pack.
657 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
658
659 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
660 = CurrentInstantiationScope->findInstantiationOf(
661 Unexpanded[I].first.get<NamedDecl *>());
Richard Smith500d7292012-07-18 01:29:05 +0000662 if (Instantiation->is<Decl*>())
663 // The pattern refers to an unexpanded pack. We're not ready to expand
664 // this pack yet.
David Blaikie66874fb2013-02-21 01:47:18 +0000665 return None;
Richard Smith500d7292012-07-18 01:29:05 +0000666
667 unsigned Size = Instantiation->get<DeclArgumentPack *>()->size();
668 assert((!Result || *Result == Size) && "inconsistent pack sizes");
669 Result = Size;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000670 continue;
671 }
672
673 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
674 }
675 if (Depth >= TemplateArgs.getNumLevels() ||
676 !TemplateArgs.hasTemplateArgument(Depth, Index))
Richard Smith500d7292012-07-18 01:29:05 +0000677 // The pattern refers to an unknown template argument. We're not ready to
678 // expand this pack yet.
David Blaikie66874fb2013-02-21 01:47:18 +0000679 return None;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000680
681 // Determine the size of the argument pack.
Richard Smith500d7292012-07-18 01:29:05 +0000682 unsigned Size = TemplateArgs(Depth, Index).pack_size();
683 assert((!Result || *Result == Size) && "inconsistent pack sizes");
684 Result = Size;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000685 }
686
Richard Smith500d7292012-07-18 01:29:05 +0000687 return Result;
Douglas Gregor21371ea2011-01-11 03:14:20 +0000688}
689
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000690bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
691 const DeclSpec &DS = D.getDeclSpec();
692 switch (DS.getTypeSpecType()) {
693 case TST_typename:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000694 case TST_typeofType:
Eli Friedmanb001de72011-10-06 23:00:33 +0000695 case TST_underlyingType:
696 case TST_atomic: {
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000697 QualType T = DS.getRepAsType().get();
698 if (!T.isNull() && T->containsUnexpandedParameterPack())
699 return true;
700 break;
701 }
702
703 case TST_typeofExpr:
704 case TST_decltype:
705 if (DS.getRepAsExpr() &&
706 DS.getRepAsExpr()->containsUnexpandedParameterPack())
707 return true;
708 break;
709
710 case TST_unspecified:
711 case TST_void:
712 case TST_char:
713 case TST_wchar:
714 case TST_char16:
715 case TST_char32:
716 case TST_int:
Richard Smith5a5a9712012-04-04 06:24:32 +0000717 case TST_int128:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000718 case TST_half:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000719 case TST_float:
720 case TST_double:
721 case TST_bool:
722 case TST_decimal32:
723 case TST_decimal64:
724 case TST_decimal128:
725 case TST_enum:
726 case TST_union:
727 case TST_struct:
Joao Matos6666ed42012-08-31 18:45:21 +0000728 case TST_interface:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000729 case TST_class:
730 case TST_auto:
Richard Smitha2c36462013-04-26 16:15:35 +0000731 case TST_decltype_auto:
John McCalla5fc4722011-04-09 22:50:59 +0000732 case TST_unknown_anytype:
Guy Benyeib13621d2012-12-18 14:38:23 +0000733 case TST_image1d_t:
734 case TST_image1d_array_t:
735 case TST_image1d_buffer_t:
736 case TST_image2d_t:
737 case TST_image2d_array_t:
738 case TST_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +0000739 case TST_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000740 case TST_event_t:
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000741 case TST_error:
742 break;
743 }
744
745 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
746 const DeclaratorChunk &Chunk = D.getTypeObject(I);
747 switch (Chunk.Kind) {
748 case DeclaratorChunk::Pointer:
749 case DeclaratorChunk::Reference:
750 case DeclaratorChunk::Paren:
751 // These declarator chunks cannot contain any parameter packs.
752 break;
753
754 case DeclaratorChunk::Array:
755 case DeclaratorChunk::Function:
756 case DeclaratorChunk::BlockPointer:
757 // Syntactically, these kinds of declarator chunks all come after the
758 // declarator-id (conceptually), so the parser should not invoke this
759 // routine at this time.
760 llvm_unreachable("Could not have seen this kind of declarator chunk");
Douglas Gregora8bc8c92010-12-23 22:44:42 +0000761
762 case DeclaratorChunk::MemberPointer:
763 if (Chunk.Mem.Scope().getScopeRep() &&
764 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
765 return true;
766 break;
767 }
768 }
769
770 return false;
771}
Douglas Gregoree8aff02011-01-04 17:33:58 +0000772
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000773namespace {
774
775// Callback to only accept typo corrections that refer to parameter packs.
776class ParameterPackValidatorCCC : public CorrectionCandidateCallback {
777 public:
778 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
779 NamedDecl *ND = candidate.getCorrectionDecl();
780 return ND && ND->isParameterPack();
781 }
782};
783
784}
785
Douglas Gregoree8aff02011-01-04 17:33:58 +0000786/// \brief Called when an expression computing the size of a parameter pack
787/// is parsed.
788///
789/// \code
790/// template<typename ...Types> struct count {
791/// static const unsigned value = sizeof...(Types);
792/// };
793/// \endcode
794///
795//
796/// \param OpLoc The location of the "sizeof" keyword.
797/// \param Name The name of the parameter pack whose size will be determined.
798/// \param NameLoc The source location of the name of the parameter pack.
799/// \param RParenLoc The location of the closing parentheses.
800ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
801 SourceLocation OpLoc,
802 IdentifierInfo &Name,
803 SourceLocation NameLoc,
804 SourceLocation RParenLoc) {
805 // C++0x [expr.sizeof]p5:
806 // The identifier in a sizeof... expression shall name a parameter pack.
Douglas Gregoree8aff02011-01-04 17:33:58 +0000807 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
808 LookupName(R, S);
809
810 NamedDecl *ParameterPack = 0;
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000811 ParameterPackValidatorCCC Validator;
Douglas Gregoree8aff02011-01-04 17:33:58 +0000812 switch (R.getResultKind()) {
813 case LookupResult::Found:
814 ParameterPack = R.getFoundDecl();
815 break;
816
817 case LookupResult::NotFound:
818 case LookupResult::NotFoundInCurrentInstantiation:
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000819 if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000820 R.getLookupKind(), S, 0,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000821 Validator)) {
Richard Smith2d670972013-08-17 00:46:16 +0000822 diagnoseTypo(Corrected,
823 PDiag(diag::err_sizeof_pack_no_pack_name_suggest) << &Name,
824 PDiag(diag::note_parameter_pack_here));
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000825 ParameterPack = Corrected.getCorrectionDecl();
Douglas Gregoree8aff02011-01-04 17:33:58 +0000826 }
Richard Smith2d670972013-08-17 00:46:16 +0000827
Douglas Gregoree8aff02011-01-04 17:33:58 +0000828 case LookupResult::FoundOverloaded:
829 case LookupResult::FoundUnresolvedValue:
830 break;
831
832 case LookupResult::Ambiguous:
833 DiagnoseAmbiguousLookup(R);
834 return ExprError();
835 }
836
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000837 if (!ParameterPack || !ParameterPack->isParameterPack()) {
Douglas Gregoree8aff02011-01-04 17:33:58 +0000838 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
839 << &Name;
840 return ExprError();
841 }
842
Nick Lewyckyb7e5eec2013-02-02 00:25:55 +0000843 MarkAnyDeclReferenced(OpLoc, ParameterPack, true);
Eli Friedman88530d52012-03-01 21:32:56 +0000844
Douglas Gregoree8aff02011-01-04 17:33:58 +0000845 return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
846 ParameterPack, NameLoc, RParenLoc);
847}
Eli Friedman850cf512013-06-20 04:11:21 +0000848
849TemplateArgumentLoc
850Sema::getTemplateArgumentPackExpansionPattern(
851 TemplateArgumentLoc OrigLoc,
852 SourceLocation &Ellipsis, Optional<unsigned> &NumExpansions) const {
853 const TemplateArgument &Argument = OrigLoc.getArgument();
854 assert(Argument.isPackExpansion());
855 switch (Argument.getKind()) {
856 case TemplateArgument::Type: {
857 // FIXME: We shouldn't ever have to worry about missing
858 // type-source info!
859 TypeSourceInfo *ExpansionTSInfo = OrigLoc.getTypeSourceInfo();
860 if (!ExpansionTSInfo)
861 ExpansionTSInfo = Context.getTrivialTypeSourceInfo(Argument.getAsType(),
862 Ellipsis);
863 PackExpansionTypeLoc Expansion =
864 ExpansionTSInfo->getTypeLoc().castAs<PackExpansionTypeLoc>();
865 Ellipsis = Expansion.getEllipsisLoc();
866
867 TypeLoc Pattern = Expansion.getPatternLoc();
868 NumExpansions = Expansion.getTypePtr()->getNumExpansions();
869
870 // We need to copy the TypeLoc because TemplateArgumentLocs store a
871 // TypeSourceInfo.
872 // FIXME: Find some way to avoid the copy?
873 TypeLocBuilder TLB;
874 TLB.pushFullCopy(Pattern);
875 TypeSourceInfo *PatternTSInfo =
876 TLB.getTypeSourceInfo(Context, Pattern.getType());
877 return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
878 PatternTSInfo);
879 }
880
881 case TemplateArgument::Expression: {
882 PackExpansionExpr *Expansion
883 = cast<PackExpansionExpr>(Argument.getAsExpr());
884 Expr *Pattern = Expansion->getPattern();
885 Ellipsis = Expansion->getEllipsisLoc();
886 NumExpansions = Expansion->getNumExpansions();
887 return TemplateArgumentLoc(Pattern, Pattern);
888 }
889
890 case TemplateArgument::TemplateExpansion:
891 Ellipsis = OrigLoc.getTemplateEllipsisLoc();
892 NumExpansions = Argument.getNumTemplateExpansions();
893 return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
894 OrigLoc.getTemplateQualifierLoc(),
895 OrigLoc.getTemplateNameLoc());
896
897 case TemplateArgument::Declaration:
898 case TemplateArgument::NullPtr:
899 case TemplateArgument::Template:
900 case TemplateArgument::Integral:
901 case TemplateArgument::Pack:
902 case TemplateArgument::Null:
903 return TemplateArgumentLoc();
904 }
905
906 llvm_unreachable("Invalid TemplateArgument Kind!");
907}