1f4a2713aSLionel Sambuc //===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
8f4a2713aSLionel Sambuc //
9f4a2713aSLionel Sambuc // This file implements semantic analysis for C++0x variadic templates.
10f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
11f4a2713aSLionel Sambuc
12f4a2713aSLionel Sambuc #include "clang/Sema/Sema.h"
13*0a6a1f1dSLionel Sambuc #include "TypeLocBuilder.h"
14f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
15f4a2713aSLionel Sambuc #include "clang/AST/RecursiveASTVisitor.h"
16f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
17f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
18f4a2713aSLionel Sambuc #include "clang/Sema/ParsedTemplate.h"
19f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
20f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/Template.h"
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc using namespace clang;
24f4a2713aSLionel Sambuc
25f4a2713aSLionel Sambuc //----------------------------------------------------------------------------
26f4a2713aSLionel Sambuc // Visitor that collects unexpanded parameter packs
27f4a2713aSLionel Sambuc //----------------------------------------------------------------------------
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc namespace {
30f4a2713aSLionel Sambuc /// \brief A class that collects unexpanded parameter packs.
31f4a2713aSLionel Sambuc class CollectUnexpandedParameterPacksVisitor :
32f4a2713aSLionel Sambuc public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
33f4a2713aSLionel Sambuc {
34f4a2713aSLionel Sambuc typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
35f4a2713aSLionel Sambuc inherited;
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
38f4a2713aSLionel Sambuc
39f4a2713aSLionel Sambuc bool InLambda;
40f4a2713aSLionel Sambuc
41f4a2713aSLionel Sambuc public:
CollectUnexpandedParameterPacksVisitor(SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)42f4a2713aSLionel Sambuc explicit CollectUnexpandedParameterPacksVisitor(
43f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
44f4a2713aSLionel Sambuc : Unexpanded(Unexpanded), InLambda(false) { }
45f4a2713aSLionel Sambuc
shouldWalkTypesOfTypeLocs() const46f4a2713aSLionel Sambuc bool shouldWalkTypesOfTypeLocs() const { return false; }
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc //------------------------------------------------------------------------
49f4a2713aSLionel Sambuc // Recording occurrences of (unexpanded) parameter packs.
50f4a2713aSLionel Sambuc //------------------------------------------------------------------------
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc /// \brief Record occurrences of template type parameter packs.
VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL)53f4a2713aSLionel Sambuc bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
54f4a2713aSLionel Sambuc if (TL.getTypePtr()->isParameterPack())
55f4a2713aSLionel Sambuc Unexpanded.push_back(std::make_pair(TL.getTypePtr(), TL.getNameLoc()));
56f4a2713aSLionel Sambuc return true;
57f4a2713aSLionel Sambuc }
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc /// \brief Record occurrences of template type parameter packs
60f4a2713aSLionel Sambuc /// when we don't have proper source-location information for
61f4a2713aSLionel Sambuc /// them.
62f4a2713aSLionel Sambuc ///
63f4a2713aSLionel Sambuc /// Ideally, this routine would never be used.
VisitTemplateTypeParmType(TemplateTypeParmType * T)64f4a2713aSLionel Sambuc bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
65f4a2713aSLionel Sambuc if (T->isParameterPack())
66f4a2713aSLionel Sambuc Unexpanded.push_back(std::make_pair(T, SourceLocation()));
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc return true;
69f4a2713aSLionel Sambuc }
70f4a2713aSLionel Sambuc
71f4a2713aSLionel Sambuc /// \brief Record occurrences of function and non-type template
72f4a2713aSLionel Sambuc /// parameter packs in an expression.
VisitDeclRefExpr(DeclRefExpr * E)73f4a2713aSLionel Sambuc bool VisitDeclRefExpr(DeclRefExpr *E) {
74f4a2713aSLionel Sambuc if (E->getDecl()->isParameterPack())
75f4a2713aSLionel Sambuc Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation()));
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc return true;
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
80f4a2713aSLionel Sambuc /// \brief Record occurrences of template template parameter packs.
TraverseTemplateName(TemplateName Template)81f4a2713aSLionel Sambuc bool TraverseTemplateName(TemplateName Template) {
82f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP
83f4a2713aSLionel Sambuc = dyn_cast_or_null<TemplateTemplateParmDecl>(
84f4a2713aSLionel Sambuc Template.getAsTemplateDecl()))
85f4a2713aSLionel Sambuc if (TTP->isParameterPack())
86f4a2713aSLionel Sambuc Unexpanded.push_back(std::make_pair(TTP, SourceLocation()));
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc return inherited::TraverseTemplateName(Template);
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc /// \brief Suppress traversal into Objective-C container literal
92f4a2713aSLionel Sambuc /// elements that are pack expansions.
TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral * E)93f4a2713aSLionel Sambuc bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
94f4a2713aSLionel Sambuc if (!E->containsUnexpandedParameterPack())
95f4a2713aSLionel Sambuc return true;
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
98f4a2713aSLionel Sambuc ObjCDictionaryElement Element = E->getKeyValueElement(I);
99f4a2713aSLionel Sambuc if (Element.isPackExpansion())
100f4a2713aSLionel Sambuc continue;
101f4a2713aSLionel Sambuc
102f4a2713aSLionel Sambuc TraverseStmt(Element.Key);
103f4a2713aSLionel Sambuc TraverseStmt(Element.Value);
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc return true;
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc //------------------------------------------------------------------------
108f4a2713aSLionel Sambuc // Pruning the search for unexpanded parameter packs.
109f4a2713aSLionel Sambuc //------------------------------------------------------------------------
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc /// \brief Suppress traversal into statements and expressions that
112f4a2713aSLionel Sambuc /// do not contain unexpanded parameter packs.
TraverseStmt(Stmt * S)113f4a2713aSLionel Sambuc bool TraverseStmt(Stmt *S) {
114f4a2713aSLionel Sambuc Expr *E = dyn_cast_or_null<Expr>(S);
115f4a2713aSLionel Sambuc if ((E && E->containsUnexpandedParameterPack()) || InLambda)
116f4a2713aSLionel Sambuc return inherited::TraverseStmt(S);
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc return true;
119f4a2713aSLionel Sambuc }
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc /// \brief Suppress traversal into types that do not contain
122f4a2713aSLionel Sambuc /// unexpanded parameter packs.
TraverseType(QualType T)123f4a2713aSLionel Sambuc bool TraverseType(QualType T) {
124f4a2713aSLionel Sambuc if ((!T.isNull() && T->containsUnexpandedParameterPack()) || InLambda)
125f4a2713aSLionel Sambuc return inherited::TraverseType(T);
126f4a2713aSLionel Sambuc
127f4a2713aSLionel Sambuc return true;
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc
130f4a2713aSLionel Sambuc /// \brief Suppress traversel into types with location information
131f4a2713aSLionel Sambuc /// that do not contain unexpanded parameter packs.
TraverseTypeLoc(TypeLoc TL)132f4a2713aSLionel Sambuc bool TraverseTypeLoc(TypeLoc TL) {
133f4a2713aSLionel Sambuc if ((!TL.getType().isNull() &&
134f4a2713aSLionel Sambuc TL.getType()->containsUnexpandedParameterPack()) ||
135f4a2713aSLionel Sambuc InLambda)
136f4a2713aSLionel Sambuc return inherited::TraverseTypeLoc(TL);
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc return true;
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc
141f4a2713aSLionel Sambuc /// \brief Suppress traversal of non-parameter declarations, since
142f4a2713aSLionel Sambuc /// they cannot contain unexpanded parameter packs.
TraverseDecl(Decl * D)143f4a2713aSLionel Sambuc bool TraverseDecl(Decl *D) {
144f4a2713aSLionel Sambuc if ((D && isa<ParmVarDecl>(D)) || InLambda)
145f4a2713aSLionel Sambuc return inherited::TraverseDecl(D);
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc return true;
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc
150f4a2713aSLionel Sambuc /// \brief Suppress traversal of template argument pack expansions.
TraverseTemplateArgument(const TemplateArgument & Arg)151f4a2713aSLionel Sambuc bool TraverseTemplateArgument(const TemplateArgument &Arg) {
152f4a2713aSLionel Sambuc if (Arg.isPackExpansion())
153f4a2713aSLionel Sambuc return true;
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc return inherited::TraverseTemplateArgument(Arg);
156f4a2713aSLionel Sambuc }
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc /// \brief Suppress traversal of template argument pack expansions.
TraverseTemplateArgumentLoc(const TemplateArgumentLoc & ArgLoc)159f4a2713aSLionel Sambuc bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
160f4a2713aSLionel Sambuc if (ArgLoc.getArgument().isPackExpansion())
161f4a2713aSLionel Sambuc return true;
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc return inherited::TraverseTemplateArgumentLoc(ArgLoc);
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc /// \brief Note whether we're traversing a lambda containing an unexpanded
167f4a2713aSLionel Sambuc /// parameter pack. In this case, the unexpanded pack can occur anywhere,
168f4a2713aSLionel Sambuc /// including all the places where we normally wouldn't look. Within a
169f4a2713aSLionel Sambuc /// lambda, we don't propagate the 'contains unexpanded parameter pack' bit
170f4a2713aSLionel Sambuc /// outside an expression.
TraverseLambdaExpr(LambdaExpr * Lambda)171f4a2713aSLionel Sambuc bool TraverseLambdaExpr(LambdaExpr *Lambda) {
172f4a2713aSLionel Sambuc // The ContainsUnexpandedParameterPack bit on a lambda is always correct,
173f4a2713aSLionel Sambuc // even if it's contained within another lambda.
174f4a2713aSLionel Sambuc if (!Lambda->containsUnexpandedParameterPack())
175f4a2713aSLionel Sambuc return true;
176f4a2713aSLionel Sambuc
177f4a2713aSLionel Sambuc bool WasInLambda = InLambda;
178f4a2713aSLionel Sambuc InLambda = true;
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc // If any capture names a function parameter pack, that pack is expanded
181f4a2713aSLionel Sambuc // when the lambda is expanded.
182f4a2713aSLionel Sambuc for (LambdaExpr::capture_iterator I = Lambda->capture_begin(),
183f4a2713aSLionel Sambuc E = Lambda->capture_end();
184f4a2713aSLionel Sambuc I != E; ++I) {
185f4a2713aSLionel Sambuc if (I->capturesVariable()) {
186f4a2713aSLionel Sambuc VarDecl *VD = I->getCapturedVar();
187f4a2713aSLionel Sambuc if (VD->isParameterPack())
188f4a2713aSLionel Sambuc Unexpanded.push_back(std::make_pair(VD, I->getLocation()));
189f4a2713aSLionel Sambuc }
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambuc inherited::TraverseLambdaExpr(Lambda);
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc InLambda = WasInLambda;
195f4a2713aSLionel Sambuc return true;
196f4a2713aSLionel Sambuc }
197f4a2713aSLionel Sambuc };
198f4a2713aSLionel Sambuc }
199f4a2713aSLionel Sambuc
200*0a6a1f1dSLionel Sambuc /// \brief Determine whether it's possible for an unexpanded parameter pack to
201*0a6a1f1dSLionel Sambuc /// be valid in this location. This only happens when we're in a declaration
202*0a6a1f1dSLionel Sambuc /// that is nested within an expression that could be expanded, such as a
203*0a6a1f1dSLionel Sambuc /// lambda-expression within a function call.
204*0a6a1f1dSLionel Sambuc ///
205*0a6a1f1dSLionel Sambuc /// This is conservatively correct, but may claim that some unexpanded packs are
206*0a6a1f1dSLionel Sambuc /// permitted when they are not.
isUnexpandedParameterPackPermitted()207*0a6a1f1dSLionel Sambuc bool Sema::isUnexpandedParameterPackPermitted() {
208*0a6a1f1dSLionel Sambuc for (auto *SI : FunctionScopes)
209*0a6a1f1dSLionel Sambuc if (isa<sema::LambdaScopeInfo>(SI))
210*0a6a1f1dSLionel Sambuc return true;
211*0a6a1f1dSLionel Sambuc return false;
212*0a6a1f1dSLionel Sambuc }
213*0a6a1f1dSLionel Sambuc
214f4a2713aSLionel Sambuc /// \brief Diagnose all of the unexpanded parameter packs in the given
215f4a2713aSLionel Sambuc /// vector.
216f4a2713aSLionel Sambuc bool
DiagnoseUnexpandedParameterPacks(SourceLocation Loc,UnexpandedParameterPackContext UPPC,ArrayRef<UnexpandedParameterPack> Unexpanded)217f4a2713aSLionel Sambuc Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
218f4a2713aSLionel Sambuc UnexpandedParameterPackContext UPPC,
219f4a2713aSLionel Sambuc ArrayRef<UnexpandedParameterPack> Unexpanded) {
220f4a2713aSLionel Sambuc if (Unexpanded.empty())
221f4a2713aSLionel Sambuc return false;
222f4a2713aSLionel Sambuc
223f4a2713aSLionel Sambuc // If we are within a lambda expression, that lambda contains an unexpanded
224f4a2713aSLionel Sambuc // parameter pack, and we are done.
225f4a2713aSLionel Sambuc // FIXME: Store 'Unexpanded' on the lambda so we don't need to recompute it
226f4a2713aSLionel Sambuc // later.
227f4a2713aSLionel Sambuc for (unsigned N = FunctionScopes.size(); N; --N) {
228f4a2713aSLionel Sambuc if (sema::LambdaScopeInfo *LSI =
229f4a2713aSLionel Sambuc dyn_cast<sema::LambdaScopeInfo>(FunctionScopes[N-1])) {
230f4a2713aSLionel Sambuc LSI->ContainsUnexpandedParameterPack = true;
231f4a2713aSLionel Sambuc return false;
232f4a2713aSLionel Sambuc }
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc SmallVector<SourceLocation, 4> Locations;
236f4a2713aSLionel Sambuc SmallVector<IdentifierInfo *, 4> Names;
237f4a2713aSLionel Sambuc llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
238f4a2713aSLionel Sambuc
239f4a2713aSLionel Sambuc for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
240*0a6a1f1dSLionel Sambuc IdentifierInfo *Name = nullptr;
241f4a2713aSLionel Sambuc if (const TemplateTypeParmType *TTP
242f4a2713aSLionel Sambuc = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
243f4a2713aSLionel Sambuc Name = TTP->getIdentifier();
244f4a2713aSLionel Sambuc else
245f4a2713aSLionel Sambuc Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
246f4a2713aSLionel Sambuc
247*0a6a1f1dSLionel Sambuc if (Name && NamesKnown.insert(Name).second)
248f4a2713aSLionel Sambuc Names.push_back(Name);
249f4a2713aSLionel Sambuc
250f4a2713aSLionel Sambuc if (Unexpanded[I].second.isValid())
251f4a2713aSLionel Sambuc Locations.push_back(Unexpanded[I].second);
252f4a2713aSLionel Sambuc }
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc DiagnosticBuilder DB
255f4a2713aSLionel Sambuc = Names.size() == 0? Diag(Loc, diag::err_unexpanded_parameter_pack_0)
256f4a2713aSLionel Sambuc << (int)UPPC
257f4a2713aSLionel Sambuc : Names.size() == 1? Diag(Loc, diag::err_unexpanded_parameter_pack_1)
258f4a2713aSLionel Sambuc << (int)UPPC << Names[0]
259f4a2713aSLionel Sambuc : Names.size() == 2? Diag(Loc, diag::err_unexpanded_parameter_pack_2)
260f4a2713aSLionel Sambuc << (int)UPPC << Names[0] << Names[1]
261f4a2713aSLionel Sambuc : Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more)
262f4a2713aSLionel Sambuc << (int)UPPC << Names[0] << Names[1];
263f4a2713aSLionel Sambuc
264f4a2713aSLionel Sambuc for (unsigned I = 0, N = Locations.size(); I != N; ++I)
265f4a2713aSLionel Sambuc DB << SourceRange(Locations[I]);
266f4a2713aSLionel Sambuc return true;
267f4a2713aSLionel Sambuc }
268f4a2713aSLionel Sambuc
DiagnoseUnexpandedParameterPack(SourceLocation Loc,TypeSourceInfo * T,UnexpandedParameterPackContext UPPC)269f4a2713aSLionel Sambuc bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
270f4a2713aSLionel Sambuc TypeSourceInfo *T,
271f4a2713aSLionel Sambuc UnexpandedParameterPackContext UPPC) {
272f4a2713aSLionel Sambuc // C++0x [temp.variadic]p5:
273f4a2713aSLionel Sambuc // An appearance of a name of a parameter pack that is not expanded is
274f4a2713aSLionel Sambuc // ill-formed.
275f4a2713aSLionel Sambuc if (!T->getType()->containsUnexpandedParameterPack())
276f4a2713aSLionel Sambuc return false;
277f4a2713aSLionel Sambuc
278f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
279f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
280f4a2713aSLionel Sambuc T->getTypeLoc());
281f4a2713aSLionel Sambuc assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
282f4a2713aSLionel Sambuc return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc
DiagnoseUnexpandedParameterPack(Expr * E,UnexpandedParameterPackContext UPPC)285f4a2713aSLionel Sambuc bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
286f4a2713aSLionel Sambuc UnexpandedParameterPackContext UPPC) {
287f4a2713aSLionel Sambuc // C++0x [temp.variadic]p5:
288f4a2713aSLionel Sambuc // An appearance of a name of a parameter pack that is not expanded is
289f4a2713aSLionel Sambuc // ill-formed.
290f4a2713aSLionel Sambuc if (!E->containsUnexpandedParameterPack())
291f4a2713aSLionel Sambuc return false;
292f4a2713aSLionel Sambuc
293f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
294f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
295f4a2713aSLionel Sambuc assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
296f4a2713aSLionel Sambuc return DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded);
297f4a2713aSLionel Sambuc }
298f4a2713aSLionel Sambuc
DiagnoseUnexpandedParameterPack(const CXXScopeSpec & SS,UnexpandedParameterPackContext UPPC)299f4a2713aSLionel Sambuc bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
300f4a2713aSLionel Sambuc UnexpandedParameterPackContext UPPC) {
301f4a2713aSLionel Sambuc // C++0x [temp.variadic]p5:
302f4a2713aSLionel Sambuc // An appearance of a name of a parameter pack that is not expanded is
303f4a2713aSLionel Sambuc // ill-formed.
304f4a2713aSLionel Sambuc if (!SS.getScopeRep() ||
305f4a2713aSLionel Sambuc !SS.getScopeRep()->containsUnexpandedParameterPack())
306f4a2713aSLionel Sambuc return false;
307f4a2713aSLionel Sambuc
308f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
309f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
310f4a2713aSLionel Sambuc .TraverseNestedNameSpecifier(SS.getScopeRep());
311f4a2713aSLionel Sambuc assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
312f4a2713aSLionel Sambuc return DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
313f4a2713aSLionel Sambuc UPPC, Unexpanded);
314f4a2713aSLionel Sambuc }
315f4a2713aSLionel Sambuc
DiagnoseUnexpandedParameterPack(const DeclarationNameInfo & NameInfo,UnexpandedParameterPackContext UPPC)316f4a2713aSLionel Sambuc bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
317f4a2713aSLionel Sambuc UnexpandedParameterPackContext UPPC) {
318f4a2713aSLionel Sambuc // C++0x [temp.variadic]p5:
319f4a2713aSLionel Sambuc // An appearance of a name of a parameter pack that is not expanded is
320f4a2713aSLionel Sambuc // ill-formed.
321f4a2713aSLionel Sambuc switch (NameInfo.getName().getNameKind()) {
322f4a2713aSLionel Sambuc case DeclarationName::Identifier:
323f4a2713aSLionel Sambuc case DeclarationName::ObjCZeroArgSelector:
324f4a2713aSLionel Sambuc case DeclarationName::ObjCOneArgSelector:
325f4a2713aSLionel Sambuc case DeclarationName::ObjCMultiArgSelector:
326f4a2713aSLionel Sambuc case DeclarationName::CXXOperatorName:
327f4a2713aSLionel Sambuc case DeclarationName::CXXLiteralOperatorName:
328f4a2713aSLionel Sambuc case DeclarationName::CXXUsingDirective:
329f4a2713aSLionel Sambuc return false;
330f4a2713aSLionel Sambuc
331f4a2713aSLionel Sambuc case DeclarationName::CXXConstructorName:
332f4a2713aSLionel Sambuc case DeclarationName::CXXDestructorName:
333f4a2713aSLionel Sambuc case DeclarationName::CXXConversionFunctionName:
334f4a2713aSLionel Sambuc // FIXME: We shouldn't need this null check!
335f4a2713aSLionel Sambuc if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
336f4a2713aSLionel Sambuc return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
337f4a2713aSLionel Sambuc
338f4a2713aSLionel Sambuc if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
339f4a2713aSLionel Sambuc return false;
340f4a2713aSLionel Sambuc
341f4a2713aSLionel Sambuc break;
342f4a2713aSLionel Sambuc }
343f4a2713aSLionel Sambuc
344f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
345f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
346f4a2713aSLionel Sambuc .TraverseType(NameInfo.getName().getCXXNameType());
347f4a2713aSLionel Sambuc assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
348f4a2713aSLionel Sambuc return DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc
DiagnoseUnexpandedParameterPack(SourceLocation Loc,TemplateName Template,UnexpandedParameterPackContext UPPC)351f4a2713aSLionel Sambuc bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
352f4a2713aSLionel Sambuc TemplateName Template,
353f4a2713aSLionel Sambuc UnexpandedParameterPackContext UPPC) {
354f4a2713aSLionel Sambuc
355f4a2713aSLionel Sambuc if (Template.isNull() || !Template.containsUnexpandedParameterPack())
356f4a2713aSLionel Sambuc return false;
357f4a2713aSLionel Sambuc
358f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
359f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
360f4a2713aSLionel Sambuc .TraverseTemplateName(Template);
361f4a2713aSLionel Sambuc assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
362f4a2713aSLionel Sambuc return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
363f4a2713aSLionel Sambuc }
364f4a2713aSLionel Sambuc
DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,UnexpandedParameterPackContext UPPC)365f4a2713aSLionel Sambuc bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
366f4a2713aSLionel Sambuc UnexpandedParameterPackContext UPPC) {
367f4a2713aSLionel Sambuc if (Arg.getArgument().isNull() ||
368f4a2713aSLionel Sambuc !Arg.getArgument().containsUnexpandedParameterPack())
369f4a2713aSLionel Sambuc return false;
370f4a2713aSLionel Sambuc
371f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
372f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
373f4a2713aSLionel Sambuc .TraverseTemplateArgumentLoc(Arg);
374f4a2713aSLionel Sambuc assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
375f4a2713aSLionel Sambuc return DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
376f4a2713aSLionel Sambuc }
377f4a2713aSLionel Sambuc
collectUnexpandedParameterPacks(TemplateArgument Arg,SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)378f4a2713aSLionel Sambuc void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
379f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
380f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
381f4a2713aSLionel Sambuc .TraverseTemplateArgument(Arg);
382f4a2713aSLionel Sambuc }
383f4a2713aSLionel Sambuc
collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)384f4a2713aSLionel Sambuc void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
385f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
386f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
387f4a2713aSLionel Sambuc .TraverseTemplateArgumentLoc(Arg);
388f4a2713aSLionel Sambuc }
389f4a2713aSLionel Sambuc
collectUnexpandedParameterPacks(QualType T,SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)390f4a2713aSLionel Sambuc void Sema::collectUnexpandedParameterPacks(QualType T,
391f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
392f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
393f4a2713aSLionel Sambuc }
394f4a2713aSLionel Sambuc
collectUnexpandedParameterPacks(TypeLoc TL,SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)395f4a2713aSLionel Sambuc void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
396f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
397f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc
collectUnexpandedParameterPacks(CXXScopeSpec & SS,SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)400f4a2713aSLionel Sambuc void Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS,
401f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
402f4a2713aSLionel Sambuc NestedNameSpecifier *Qualifier = SS.getScopeRep();
403f4a2713aSLionel Sambuc if (!Qualifier)
404f4a2713aSLionel Sambuc return;
405f4a2713aSLionel Sambuc
406f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data());
407f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
408f4a2713aSLionel Sambuc .TraverseNestedNameSpecifierLoc(QualifierLoc);
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc
collectUnexpandedParameterPacks(const DeclarationNameInfo & NameInfo,SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)411f4a2713aSLionel Sambuc void Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo,
412f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
413f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded)
414f4a2713aSLionel Sambuc .TraverseDeclarationNameInfo(NameInfo);
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc
417f4a2713aSLionel Sambuc
418f4a2713aSLionel Sambuc ParsedTemplateArgument
ActOnPackExpansion(const ParsedTemplateArgument & Arg,SourceLocation EllipsisLoc)419f4a2713aSLionel Sambuc Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
420f4a2713aSLionel Sambuc SourceLocation EllipsisLoc) {
421f4a2713aSLionel Sambuc if (Arg.isInvalid())
422f4a2713aSLionel Sambuc return Arg;
423f4a2713aSLionel Sambuc
424f4a2713aSLionel Sambuc switch (Arg.getKind()) {
425f4a2713aSLionel Sambuc case ParsedTemplateArgument::Type: {
426f4a2713aSLionel Sambuc TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
427f4a2713aSLionel Sambuc if (Result.isInvalid())
428f4a2713aSLionel Sambuc return ParsedTemplateArgument();
429f4a2713aSLionel Sambuc
430f4a2713aSLionel Sambuc return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
431f4a2713aSLionel Sambuc Arg.getLocation());
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc
434f4a2713aSLionel Sambuc case ParsedTemplateArgument::NonType: {
435f4a2713aSLionel Sambuc ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
436f4a2713aSLionel Sambuc if (Result.isInvalid())
437f4a2713aSLionel Sambuc return ParsedTemplateArgument();
438f4a2713aSLionel Sambuc
439f4a2713aSLionel Sambuc return ParsedTemplateArgument(Arg.getKind(), Result.get(),
440f4a2713aSLionel Sambuc Arg.getLocation());
441f4a2713aSLionel Sambuc }
442f4a2713aSLionel Sambuc
443f4a2713aSLionel Sambuc case ParsedTemplateArgument::Template:
444f4a2713aSLionel Sambuc if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
445f4a2713aSLionel Sambuc SourceRange R(Arg.getLocation());
446f4a2713aSLionel Sambuc if (Arg.getScopeSpec().isValid())
447f4a2713aSLionel Sambuc R.setBegin(Arg.getScopeSpec().getBeginLoc());
448f4a2713aSLionel Sambuc Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
449f4a2713aSLionel Sambuc << R;
450f4a2713aSLionel Sambuc return ParsedTemplateArgument();
451f4a2713aSLionel Sambuc }
452f4a2713aSLionel Sambuc
453f4a2713aSLionel Sambuc return Arg.getTemplatePackExpansion(EllipsisLoc);
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc llvm_unreachable("Unhandled template argument kind?");
456f4a2713aSLionel Sambuc }
457f4a2713aSLionel Sambuc
ActOnPackExpansion(ParsedType Type,SourceLocation EllipsisLoc)458f4a2713aSLionel Sambuc TypeResult Sema::ActOnPackExpansion(ParsedType Type,
459f4a2713aSLionel Sambuc SourceLocation EllipsisLoc) {
460f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo;
461f4a2713aSLionel Sambuc GetTypeFromParser(Type, &TSInfo);
462f4a2713aSLionel Sambuc if (!TSInfo)
463f4a2713aSLionel Sambuc return true;
464f4a2713aSLionel Sambuc
465f4a2713aSLionel Sambuc TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, None);
466f4a2713aSLionel Sambuc if (!TSResult)
467f4a2713aSLionel Sambuc return true;
468f4a2713aSLionel Sambuc
469f4a2713aSLionel Sambuc return CreateParsedType(TSResult->getType(), TSResult);
470f4a2713aSLionel Sambuc }
471f4a2713aSLionel Sambuc
472f4a2713aSLionel Sambuc TypeSourceInfo *
CheckPackExpansion(TypeSourceInfo * Pattern,SourceLocation EllipsisLoc,Optional<unsigned> NumExpansions)473f4a2713aSLionel Sambuc Sema::CheckPackExpansion(TypeSourceInfo *Pattern, SourceLocation EllipsisLoc,
474f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions) {
475f4a2713aSLionel Sambuc // Create the pack expansion type and source-location information.
476f4a2713aSLionel Sambuc QualType Result = CheckPackExpansion(Pattern->getType(),
477f4a2713aSLionel Sambuc Pattern->getTypeLoc().getSourceRange(),
478f4a2713aSLionel Sambuc EllipsisLoc, NumExpansions);
479f4a2713aSLionel Sambuc if (Result.isNull())
480*0a6a1f1dSLionel Sambuc return nullptr;
481f4a2713aSLionel Sambuc
482f4a2713aSLionel Sambuc TypeLocBuilder TLB;
483f4a2713aSLionel Sambuc TLB.pushFullCopy(Pattern->getTypeLoc());
484f4a2713aSLionel Sambuc PackExpansionTypeLoc TL = TLB.push<PackExpansionTypeLoc>(Result);
485f4a2713aSLionel Sambuc TL.setEllipsisLoc(EllipsisLoc);
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc return TLB.getTypeSourceInfo(Context, Result);
488f4a2713aSLionel Sambuc }
489f4a2713aSLionel Sambuc
CheckPackExpansion(QualType Pattern,SourceRange PatternRange,SourceLocation EllipsisLoc,Optional<unsigned> NumExpansions)490f4a2713aSLionel Sambuc QualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange,
491f4a2713aSLionel Sambuc SourceLocation EllipsisLoc,
492f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions) {
493f4a2713aSLionel Sambuc // C++0x [temp.variadic]p5:
494f4a2713aSLionel Sambuc // The pattern of a pack expansion shall name one or more
495f4a2713aSLionel Sambuc // parameter packs that are not expanded by a nested pack
496f4a2713aSLionel Sambuc // expansion.
497f4a2713aSLionel Sambuc if (!Pattern->containsUnexpandedParameterPack()) {
498f4a2713aSLionel Sambuc Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
499f4a2713aSLionel Sambuc << PatternRange;
500f4a2713aSLionel Sambuc return QualType();
501f4a2713aSLionel Sambuc }
502f4a2713aSLionel Sambuc
503f4a2713aSLionel Sambuc return Context.getPackExpansionType(Pattern, NumExpansions);
504f4a2713aSLionel Sambuc }
505f4a2713aSLionel Sambuc
ActOnPackExpansion(Expr * Pattern,SourceLocation EllipsisLoc)506f4a2713aSLionel Sambuc ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
507f4a2713aSLionel Sambuc return CheckPackExpansion(Pattern, EllipsisLoc, None);
508f4a2713aSLionel Sambuc }
509f4a2713aSLionel Sambuc
CheckPackExpansion(Expr * Pattern,SourceLocation EllipsisLoc,Optional<unsigned> NumExpansions)510f4a2713aSLionel Sambuc ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
511f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions) {
512f4a2713aSLionel Sambuc if (!Pattern)
513f4a2713aSLionel Sambuc return ExprError();
514f4a2713aSLionel Sambuc
515f4a2713aSLionel Sambuc // C++0x [temp.variadic]p5:
516f4a2713aSLionel Sambuc // The pattern of a pack expansion shall name one or more
517f4a2713aSLionel Sambuc // parameter packs that are not expanded by a nested pack
518f4a2713aSLionel Sambuc // expansion.
519f4a2713aSLionel Sambuc if (!Pattern->containsUnexpandedParameterPack()) {
520f4a2713aSLionel Sambuc Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
521f4a2713aSLionel Sambuc << Pattern->getSourceRange();
522f4a2713aSLionel Sambuc return ExprError();
523f4a2713aSLionel Sambuc }
524f4a2713aSLionel Sambuc
525f4a2713aSLionel Sambuc // Create the pack expansion expression and source-location information.
526*0a6a1f1dSLionel Sambuc return new (Context)
527*0a6a1f1dSLionel Sambuc PackExpansionExpr(Context.DependentTy, Pattern, EllipsisLoc, NumExpansions);
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc
530f4a2713aSLionel Sambuc /// \brief Retrieve the depth and index of a parameter pack.
531f4a2713aSLionel Sambuc static std::pair<unsigned, unsigned>
getDepthAndIndex(NamedDecl * ND)532f4a2713aSLionel Sambuc getDepthAndIndex(NamedDecl *ND) {
533f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
534f4a2713aSLionel Sambuc return std::make_pair(TTP->getDepth(), TTP->getIndex());
535f4a2713aSLionel Sambuc
536f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
537f4a2713aSLionel Sambuc return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
538f4a2713aSLionel Sambuc
539f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
540f4a2713aSLionel Sambuc return std::make_pair(TTP->getDepth(), TTP->getIndex());
541f4a2713aSLionel Sambuc }
542f4a2713aSLionel Sambuc
CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,SourceRange PatternRange,ArrayRef<UnexpandedParameterPack> Unexpanded,const MultiLevelTemplateArgumentList & TemplateArgs,bool & ShouldExpand,bool & RetainExpansion,Optional<unsigned> & NumExpansions)543f4a2713aSLionel Sambuc bool Sema::CheckParameterPacksForExpansion(
544f4a2713aSLionel Sambuc SourceLocation EllipsisLoc, SourceRange PatternRange,
545f4a2713aSLionel Sambuc ArrayRef<UnexpandedParameterPack> Unexpanded,
546f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs, bool &ShouldExpand,
547f4a2713aSLionel Sambuc bool &RetainExpansion, Optional<unsigned> &NumExpansions) {
548f4a2713aSLionel Sambuc ShouldExpand = true;
549f4a2713aSLionel Sambuc RetainExpansion = false;
550f4a2713aSLionel Sambuc std::pair<IdentifierInfo *, SourceLocation> FirstPack;
551f4a2713aSLionel Sambuc bool HaveFirstPack = false;
552f4a2713aSLionel Sambuc
553f4a2713aSLionel Sambuc for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
554f4a2713aSLionel Sambuc end = Unexpanded.end();
555f4a2713aSLionel Sambuc i != end; ++i) {
556f4a2713aSLionel Sambuc // Compute the depth and index for this parameter pack.
557f4a2713aSLionel Sambuc unsigned Depth = 0, Index = 0;
558f4a2713aSLionel Sambuc IdentifierInfo *Name;
559f4a2713aSLionel Sambuc bool IsFunctionParameterPack = false;
560f4a2713aSLionel Sambuc
561f4a2713aSLionel Sambuc if (const TemplateTypeParmType *TTP
562f4a2713aSLionel Sambuc = i->first.dyn_cast<const TemplateTypeParmType *>()) {
563f4a2713aSLionel Sambuc Depth = TTP->getDepth();
564f4a2713aSLionel Sambuc Index = TTP->getIndex();
565f4a2713aSLionel Sambuc Name = TTP->getIdentifier();
566f4a2713aSLionel Sambuc } else {
567f4a2713aSLionel Sambuc NamedDecl *ND = i->first.get<NamedDecl *>();
568f4a2713aSLionel Sambuc if (isa<ParmVarDecl>(ND))
569f4a2713aSLionel Sambuc IsFunctionParameterPack = true;
570f4a2713aSLionel Sambuc else
571*0a6a1f1dSLionel Sambuc std::tie(Depth, Index) = getDepthAndIndex(ND);
572f4a2713aSLionel Sambuc
573f4a2713aSLionel Sambuc Name = ND->getIdentifier();
574f4a2713aSLionel Sambuc }
575f4a2713aSLionel Sambuc
576f4a2713aSLionel Sambuc // Determine the size of this argument pack.
577f4a2713aSLionel Sambuc unsigned NewPackSize;
578f4a2713aSLionel Sambuc if (IsFunctionParameterPack) {
579f4a2713aSLionel Sambuc // Figure out whether we're instantiating to an argument pack or not.
580f4a2713aSLionel Sambuc typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
581f4a2713aSLionel Sambuc
582f4a2713aSLionel Sambuc llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
583f4a2713aSLionel Sambuc = CurrentInstantiationScope->findInstantiationOf(
584f4a2713aSLionel Sambuc i->first.get<NamedDecl *>());
585f4a2713aSLionel Sambuc if (Instantiation->is<DeclArgumentPack *>()) {
586f4a2713aSLionel Sambuc // We could expand this function parameter pack.
587f4a2713aSLionel Sambuc NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
588f4a2713aSLionel Sambuc } else {
589f4a2713aSLionel Sambuc // We can't expand this function parameter pack, so we can't expand
590f4a2713aSLionel Sambuc // the pack expansion.
591f4a2713aSLionel Sambuc ShouldExpand = false;
592f4a2713aSLionel Sambuc continue;
593f4a2713aSLionel Sambuc }
594f4a2713aSLionel Sambuc } else {
595f4a2713aSLionel Sambuc // If we don't have a template argument at this depth/index, then we
596f4a2713aSLionel Sambuc // cannot expand the pack expansion. Make a note of this, but we still
597f4a2713aSLionel Sambuc // want to check any parameter packs we *do* have arguments for.
598f4a2713aSLionel Sambuc if (Depth >= TemplateArgs.getNumLevels() ||
599f4a2713aSLionel Sambuc !TemplateArgs.hasTemplateArgument(Depth, Index)) {
600f4a2713aSLionel Sambuc ShouldExpand = false;
601f4a2713aSLionel Sambuc continue;
602f4a2713aSLionel Sambuc }
603f4a2713aSLionel Sambuc
604f4a2713aSLionel Sambuc // Determine the size of the argument pack.
605f4a2713aSLionel Sambuc NewPackSize = TemplateArgs(Depth, Index).pack_size();
606f4a2713aSLionel Sambuc }
607f4a2713aSLionel Sambuc
608f4a2713aSLionel Sambuc // C++0x [temp.arg.explicit]p9:
609f4a2713aSLionel Sambuc // Template argument deduction can extend the sequence of template
610f4a2713aSLionel Sambuc // arguments corresponding to a template parameter pack, even when the
611f4a2713aSLionel Sambuc // sequence contains explicitly specified template arguments.
612f4a2713aSLionel Sambuc if (!IsFunctionParameterPack) {
613f4a2713aSLionel Sambuc if (NamedDecl *PartialPack
614f4a2713aSLionel Sambuc = CurrentInstantiationScope->getPartiallySubstitutedPack()){
615f4a2713aSLionel Sambuc unsigned PartialDepth, PartialIndex;
616*0a6a1f1dSLionel Sambuc std::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
617f4a2713aSLionel Sambuc if (PartialDepth == Depth && PartialIndex == Index)
618f4a2713aSLionel Sambuc RetainExpansion = true;
619f4a2713aSLionel Sambuc }
620f4a2713aSLionel Sambuc }
621f4a2713aSLionel Sambuc
622f4a2713aSLionel Sambuc if (!NumExpansions) {
623f4a2713aSLionel Sambuc // The is the first pack we've seen for which we have an argument.
624f4a2713aSLionel Sambuc // Record it.
625f4a2713aSLionel Sambuc NumExpansions = NewPackSize;
626f4a2713aSLionel Sambuc FirstPack.first = Name;
627f4a2713aSLionel Sambuc FirstPack.second = i->second;
628f4a2713aSLionel Sambuc HaveFirstPack = true;
629f4a2713aSLionel Sambuc continue;
630f4a2713aSLionel Sambuc }
631f4a2713aSLionel Sambuc
632f4a2713aSLionel Sambuc if (NewPackSize != *NumExpansions) {
633f4a2713aSLionel Sambuc // C++0x [temp.variadic]p5:
634f4a2713aSLionel Sambuc // All of the parameter packs expanded by a pack expansion shall have
635f4a2713aSLionel Sambuc // the same number of arguments specified.
636f4a2713aSLionel Sambuc if (HaveFirstPack)
637f4a2713aSLionel Sambuc Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
638f4a2713aSLionel Sambuc << FirstPack.first << Name << *NumExpansions << NewPackSize
639f4a2713aSLionel Sambuc << SourceRange(FirstPack.second) << SourceRange(i->second);
640f4a2713aSLionel Sambuc else
641f4a2713aSLionel Sambuc Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
642f4a2713aSLionel Sambuc << Name << *NumExpansions << NewPackSize
643f4a2713aSLionel Sambuc << SourceRange(i->second);
644f4a2713aSLionel Sambuc return true;
645f4a2713aSLionel Sambuc }
646f4a2713aSLionel Sambuc }
647f4a2713aSLionel Sambuc
648f4a2713aSLionel Sambuc return false;
649f4a2713aSLionel Sambuc }
650f4a2713aSLionel Sambuc
getNumArgumentsInExpansion(QualType T,const MultiLevelTemplateArgumentList & TemplateArgs)651f4a2713aSLionel Sambuc Optional<unsigned> Sema::getNumArgumentsInExpansion(QualType T,
652f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
653f4a2713aSLionel Sambuc QualType Pattern = cast<PackExpansionType>(T)->getPattern();
654f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
655f4a2713aSLionel Sambuc CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
656f4a2713aSLionel Sambuc
657f4a2713aSLionel Sambuc Optional<unsigned> Result;
658f4a2713aSLionel Sambuc for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
659f4a2713aSLionel Sambuc // Compute the depth and index for this parameter pack.
660f4a2713aSLionel Sambuc unsigned Depth;
661f4a2713aSLionel Sambuc unsigned Index;
662f4a2713aSLionel Sambuc
663f4a2713aSLionel Sambuc if (const TemplateTypeParmType *TTP
664f4a2713aSLionel Sambuc = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
665f4a2713aSLionel Sambuc Depth = TTP->getDepth();
666f4a2713aSLionel Sambuc Index = TTP->getIndex();
667f4a2713aSLionel Sambuc } else {
668f4a2713aSLionel Sambuc NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
669f4a2713aSLionel Sambuc if (isa<ParmVarDecl>(ND)) {
670f4a2713aSLionel Sambuc // Function parameter pack.
671f4a2713aSLionel Sambuc typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
672f4a2713aSLionel Sambuc
673f4a2713aSLionel Sambuc llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
674f4a2713aSLionel Sambuc = CurrentInstantiationScope->findInstantiationOf(
675f4a2713aSLionel Sambuc Unexpanded[I].first.get<NamedDecl *>());
676f4a2713aSLionel Sambuc if (Instantiation->is<Decl*>())
677f4a2713aSLionel Sambuc // The pattern refers to an unexpanded pack. We're not ready to expand
678f4a2713aSLionel Sambuc // this pack yet.
679f4a2713aSLionel Sambuc return None;
680f4a2713aSLionel Sambuc
681f4a2713aSLionel Sambuc unsigned Size = Instantiation->get<DeclArgumentPack *>()->size();
682f4a2713aSLionel Sambuc assert((!Result || *Result == Size) && "inconsistent pack sizes");
683f4a2713aSLionel Sambuc Result = Size;
684f4a2713aSLionel Sambuc continue;
685f4a2713aSLionel Sambuc }
686f4a2713aSLionel Sambuc
687*0a6a1f1dSLionel Sambuc std::tie(Depth, Index) = getDepthAndIndex(ND);
688f4a2713aSLionel Sambuc }
689f4a2713aSLionel Sambuc if (Depth >= TemplateArgs.getNumLevels() ||
690f4a2713aSLionel Sambuc !TemplateArgs.hasTemplateArgument(Depth, Index))
691f4a2713aSLionel Sambuc // The pattern refers to an unknown template argument. We're not ready to
692f4a2713aSLionel Sambuc // expand this pack yet.
693f4a2713aSLionel Sambuc return None;
694f4a2713aSLionel Sambuc
695f4a2713aSLionel Sambuc // Determine the size of the argument pack.
696f4a2713aSLionel Sambuc unsigned Size = TemplateArgs(Depth, Index).pack_size();
697f4a2713aSLionel Sambuc assert((!Result || *Result == Size) && "inconsistent pack sizes");
698f4a2713aSLionel Sambuc Result = Size;
699f4a2713aSLionel Sambuc }
700f4a2713aSLionel Sambuc
701f4a2713aSLionel Sambuc return Result;
702f4a2713aSLionel Sambuc }
703f4a2713aSLionel Sambuc
containsUnexpandedParameterPacks(Declarator & D)704f4a2713aSLionel Sambuc bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
705f4a2713aSLionel Sambuc const DeclSpec &DS = D.getDeclSpec();
706f4a2713aSLionel Sambuc switch (DS.getTypeSpecType()) {
707f4a2713aSLionel Sambuc case TST_typename:
708f4a2713aSLionel Sambuc case TST_typeofType:
709f4a2713aSLionel Sambuc case TST_underlyingType:
710f4a2713aSLionel Sambuc case TST_atomic: {
711f4a2713aSLionel Sambuc QualType T = DS.getRepAsType().get();
712f4a2713aSLionel Sambuc if (!T.isNull() && T->containsUnexpandedParameterPack())
713f4a2713aSLionel Sambuc return true;
714f4a2713aSLionel Sambuc break;
715f4a2713aSLionel Sambuc }
716f4a2713aSLionel Sambuc
717f4a2713aSLionel Sambuc case TST_typeofExpr:
718f4a2713aSLionel Sambuc case TST_decltype:
719f4a2713aSLionel Sambuc if (DS.getRepAsExpr() &&
720f4a2713aSLionel Sambuc DS.getRepAsExpr()->containsUnexpandedParameterPack())
721f4a2713aSLionel Sambuc return true;
722f4a2713aSLionel Sambuc break;
723f4a2713aSLionel Sambuc
724f4a2713aSLionel Sambuc case TST_unspecified:
725f4a2713aSLionel Sambuc case TST_void:
726f4a2713aSLionel Sambuc case TST_char:
727f4a2713aSLionel Sambuc case TST_wchar:
728f4a2713aSLionel Sambuc case TST_char16:
729f4a2713aSLionel Sambuc case TST_char32:
730f4a2713aSLionel Sambuc case TST_int:
731f4a2713aSLionel Sambuc case TST_int128:
732f4a2713aSLionel Sambuc case TST_half:
733f4a2713aSLionel Sambuc case TST_float:
734f4a2713aSLionel Sambuc case TST_double:
735f4a2713aSLionel Sambuc case TST_bool:
736f4a2713aSLionel Sambuc case TST_decimal32:
737f4a2713aSLionel Sambuc case TST_decimal64:
738f4a2713aSLionel Sambuc case TST_decimal128:
739f4a2713aSLionel Sambuc case TST_enum:
740f4a2713aSLionel Sambuc case TST_union:
741f4a2713aSLionel Sambuc case TST_struct:
742f4a2713aSLionel Sambuc case TST_interface:
743f4a2713aSLionel Sambuc case TST_class:
744f4a2713aSLionel Sambuc case TST_auto:
745f4a2713aSLionel Sambuc case TST_decltype_auto:
746f4a2713aSLionel Sambuc case TST_unknown_anytype:
747f4a2713aSLionel Sambuc case TST_error:
748f4a2713aSLionel Sambuc break;
749f4a2713aSLionel Sambuc }
750f4a2713aSLionel Sambuc
751f4a2713aSLionel Sambuc for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
752f4a2713aSLionel Sambuc const DeclaratorChunk &Chunk = D.getTypeObject(I);
753f4a2713aSLionel Sambuc switch (Chunk.Kind) {
754f4a2713aSLionel Sambuc case DeclaratorChunk::Pointer:
755f4a2713aSLionel Sambuc case DeclaratorChunk::Reference:
756f4a2713aSLionel Sambuc case DeclaratorChunk::Paren:
757*0a6a1f1dSLionel Sambuc case DeclaratorChunk::BlockPointer:
758f4a2713aSLionel Sambuc // These declarator chunks cannot contain any parameter packs.
759f4a2713aSLionel Sambuc break;
760f4a2713aSLionel Sambuc
761f4a2713aSLionel Sambuc case DeclaratorChunk::Array:
762*0a6a1f1dSLionel Sambuc if (Chunk.Arr.NumElts &&
763*0a6a1f1dSLionel Sambuc Chunk.Arr.NumElts->containsUnexpandedParameterPack())
764*0a6a1f1dSLionel Sambuc return true;
765*0a6a1f1dSLionel Sambuc break;
766f4a2713aSLionel Sambuc case DeclaratorChunk::Function:
767*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = Chunk.Fun.NumParams; i != e; ++i) {
768*0a6a1f1dSLionel Sambuc ParmVarDecl *Param = cast<ParmVarDecl>(Chunk.Fun.Params[i].Param);
769*0a6a1f1dSLionel Sambuc QualType ParamTy = Param->getType();
770*0a6a1f1dSLionel Sambuc assert(!ParamTy.isNull() && "Couldn't parse type?");
771*0a6a1f1dSLionel Sambuc if (ParamTy->containsUnexpandedParameterPack()) return true;
772*0a6a1f1dSLionel Sambuc }
773*0a6a1f1dSLionel Sambuc
774*0a6a1f1dSLionel Sambuc if (Chunk.Fun.getExceptionSpecType() == EST_Dynamic) {
775*0a6a1f1dSLionel Sambuc for (unsigned i = 0; i != Chunk.Fun.NumExceptions; ++i) {
776*0a6a1f1dSLionel Sambuc if (Chunk.Fun.Exceptions[i]
777*0a6a1f1dSLionel Sambuc .Ty.get()
778*0a6a1f1dSLionel Sambuc ->containsUnexpandedParameterPack())
779*0a6a1f1dSLionel Sambuc return true;
780*0a6a1f1dSLionel Sambuc }
781*0a6a1f1dSLionel Sambuc } else if (Chunk.Fun.getExceptionSpecType() == EST_ComputedNoexcept &&
782*0a6a1f1dSLionel Sambuc Chunk.Fun.NoexceptExpr->containsUnexpandedParameterPack())
783*0a6a1f1dSLionel Sambuc return true;
784*0a6a1f1dSLionel Sambuc
785*0a6a1f1dSLionel Sambuc if (Chunk.Fun.hasTrailingReturnType()) {
786*0a6a1f1dSLionel Sambuc QualType T = Chunk.Fun.getTrailingReturnType().get();
787*0a6a1f1dSLionel Sambuc if (!T.isNull() && T->containsUnexpandedParameterPack())
788*0a6a1f1dSLionel Sambuc return true;
789*0a6a1f1dSLionel Sambuc }
790*0a6a1f1dSLionel Sambuc break;
791f4a2713aSLionel Sambuc
792f4a2713aSLionel Sambuc case DeclaratorChunk::MemberPointer:
793f4a2713aSLionel Sambuc if (Chunk.Mem.Scope().getScopeRep() &&
794f4a2713aSLionel Sambuc Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
795f4a2713aSLionel Sambuc return true;
796f4a2713aSLionel Sambuc break;
797f4a2713aSLionel Sambuc }
798f4a2713aSLionel Sambuc }
799f4a2713aSLionel Sambuc
800f4a2713aSLionel Sambuc return false;
801f4a2713aSLionel Sambuc }
802f4a2713aSLionel Sambuc
803f4a2713aSLionel Sambuc namespace {
804f4a2713aSLionel Sambuc
805f4a2713aSLionel Sambuc // Callback to only accept typo corrections that refer to parameter packs.
806f4a2713aSLionel Sambuc class ParameterPackValidatorCCC : public CorrectionCandidateCallback {
807f4a2713aSLionel Sambuc public:
ValidateCandidate(const TypoCorrection & candidate)808*0a6a1f1dSLionel Sambuc bool ValidateCandidate(const TypoCorrection &candidate) override {
809f4a2713aSLionel Sambuc NamedDecl *ND = candidate.getCorrectionDecl();
810f4a2713aSLionel Sambuc return ND && ND->isParameterPack();
811f4a2713aSLionel Sambuc }
812f4a2713aSLionel Sambuc };
813f4a2713aSLionel Sambuc
814f4a2713aSLionel Sambuc }
815f4a2713aSLionel Sambuc
816f4a2713aSLionel Sambuc /// \brief Called when an expression computing the size of a parameter pack
817f4a2713aSLionel Sambuc /// is parsed.
818f4a2713aSLionel Sambuc ///
819f4a2713aSLionel Sambuc /// \code
820f4a2713aSLionel Sambuc /// template<typename ...Types> struct count {
821f4a2713aSLionel Sambuc /// static const unsigned value = sizeof...(Types);
822f4a2713aSLionel Sambuc /// };
823f4a2713aSLionel Sambuc /// \endcode
824f4a2713aSLionel Sambuc ///
825f4a2713aSLionel Sambuc //
826f4a2713aSLionel Sambuc /// \param OpLoc The location of the "sizeof" keyword.
827f4a2713aSLionel Sambuc /// \param Name The name of the parameter pack whose size will be determined.
828f4a2713aSLionel Sambuc /// \param NameLoc The source location of the name of the parameter pack.
829f4a2713aSLionel Sambuc /// \param RParenLoc The location of the closing parentheses.
ActOnSizeofParameterPackExpr(Scope * S,SourceLocation OpLoc,IdentifierInfo & Name,SourceLocation NameLoc,SourceLocation RParenLoc)830f4a2713aSLionel Sambuc ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
831f4a2713aSLionel Sambuc SourceLocation OpLoc,
832f4a2713aSLionel Sambuc IdentifierInfo &Name,
833f4a2713aSLionel Sambuc SourceLocation NameLoc,
834f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
835f4a2713aSLionel Sambuc // C++0x [expr.sizeof]p5:
836f4a2713aSLionel Sambuc // The identifier in a sizeof... expression shall name a parameter pack.
837f4a2713aSLionel Sambuc LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
838f4a2713aSLionel Sambuc LookupName(R, S);
839f4a2713aSLionel Sambuc
840*0a6a1f1dSLionel Sambuc NamedDecl *ParameterPack = nullptr;
841f4a2713aSLionel Sambuc switch (R.getResultKind()) {
842f4a2713aSLionel Sambuc case LookupResult::Found:
843f4a2713aSLionel Sambuc ParameterPack = R.getFoundDecl();
844f4a2713aSLionel Sambuc break;
845f4a2713aSLionel Sambuc
846f4a2713aSLionel Sambuc case LookupResult::NotFound:
847f4a2713aSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation:
848*0a6a1f1dSLionel Sambuc if (TypoCorrection Corrected =
849*0a6a1f1dSLionel Sambuc CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr,
850*0a6a1f1dSLionel Sambuc llvm::make_unique<ParameterPackValidatorCCC>(),
851*0a6a1f1dSLionel Sambuc CTK_ErrorRecovery)) {
852f4a2713aSLionel Sambuc diagnoseTypo(Corrected,
853f4a2713aSLionel Sambuc PDiag(diag::err_sizeof_pack_no_pack_name_suggest) << &Name,
854f4a2713aSLionel Sambuc PDiag(diag::note_parameter_pack_here));
855f4a2713aSLionel Sambuc ParameterPack = Corrected.getCorrectionDecl();
856f4a2713aSLionel Sambuc }
857f4a2713aSLionel Sambuc
858f4a2713aSLionel Sambuc case LookupResult::FoundOverloaded:
859f4a2713aSLionel Sambuc case LookupResult::FoundUnresolvedValue:
860f4a2713aSLionel Sambuc break;
861f4a2713aSLionel Sambuc
862f4a2713aSLionel Sambuc case LookupResult::Ambiguous:
863f4a2713aSLionel Sambuc DiagnoseAmbiguousLookup(R);
864f4a2713aSLionel Sambuc return ExprError();
865f4a2713aSLionel Sambuc }
866f4a2713aSLionel Sambuc
867f4a2713aSLionel Sambuc if (!ParameterPack || !ParameterPack->isParameterPack()) {
868f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
869f4a2713aSLionel Sambuc << &Name;
870f4a2713aSLionel Sambuc return ExprError();
871f4a2713aSLionel Sambuc }
872f4a2713aSLionel Sambuc
873f4a2713aSLionel Sambuc MarkAnyDeclReferenced(OpLoc, ParameterPack, true);
874f4a2713aSLionel Sambuc
875f4a2713aSLionel Sambuc return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
876f4a2713aSLionel Sambuc ParameterPack, NameLoc, RParenLoc);
877f4a2713aSLionel Sambuc }
878f4a2713aSLionel Sambuc
879f4a2713aSLionel Sambuc TemplateArgumentLoc
getTemplateArgumentPackExpansionPattern(TemplateArgumentLoc OrigLoc,SourceLocation & Ellipsis,Optional<unsigned> & NumExpansions) const880f4a2713aSLionel Sambuc Sema::getTemplateArgumentPackExpansionPattern(
881f4a2713aSLionel Sambuc TemplateArgumentLoc OrigLoc,
882f4a2713aSLionel Sambuc SourceLocation &Ellipsis, Optional<unsigned> &NumExpansions) const {
883f4a2713aSLionel Sambuc const TemplateArgument &Argument = OrigLoc.getArgument();
884f4a2713aSLionel Sambuc assert(Argument.isPackExpansion());
885f4a2713aSLionel Sambuc switch (Argument.getKind()) {
886f4a2713aSLionel Sambuc case TemplateArgument::Type: {
887f4a2713aSLionel Sambuc // FIXME: We shouldn't ever have to worry about missing
888f4a2713aSLionel Sambuc // type-source info!
889f4a2713aSLionel Sambuc TypeSourceInfo *ExpansionTSInfo = OrigLoc.getTypeSourceInfo();
890f4a2713aSLionel Sambuc if (!ExpansionTSInfo)
891f4a2713aSLionel Sambuc ExpansionTSInfo = Context.getTrivialTypeSourceInfo(Argument.getAsType(),
892f4a2713aSLionel Sambuc Ellipsis);
893f4a2713aSLionel Sambuc PackExpansionTypeLoc Expansion =
894f4a2713aSLionel Sambuc ExpansionTSInfo->getTypeLoc().castAs<PackExpansionTypeLoc>();
895f4a2713aSLionel Sambuc Ellipsis = Expansion.getEllipsisLoc();
896f4a2713aSLionel Sambuc
897f4a2713aSLionel Sambuc TypeLoc Pattern = Expansion.getPatternLoc();
898f4a2713aSLionel Sambuc NumExpansions = Expansion.getTypePtr()->getNumExpansions();
899f4a2713aSLionel Sambuc
900f4a2713aSLionel Sambuc // We need to copy the TypeLoc because TemplateArgumentLocs store a
901f4a2713aSLionel Sambuc // TypeSourceInfo.
902f4a2713aSLionel Sambuc // FIXME: Find some way to avoid the copy?
903f4a2713aSLionel Sambuc TypeLocBuilder TLB;
904f4a2713aSLionel Sambuc TLB.pushFullCopy(Pattern);
905f4a2713aSLionel Sambuc TypeSourceInfo *PatternTSInfo =
906f4a2713aSLionel Sambuc TLB.getTypeSourceInfo(Context, Pattern.getType());
907f4a2713aSLionel Sambuc return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
908f4a2713aSLionel Sambuc PatternTSInfo);
909f4a2713aSLionel Sambuc }
910f4a2713aSLionel Sambuc
911f4a2713aSLionel Sambuc case TemplateArgument::Expression: {
912f4a2713aSLionel Sambuc PackExpansionExpr *Expansion
913f4a2713aSLionel Sambuc = cast<PackExpansionExpr>(Argument.getAsExpr());
914f4a2713aSLionel Sambuc Expr *Pattern = Expansion->getPattern();
915f4a2713aSLionel Sambuc Ellipsis = Expansion->getEllipsisLoc();
916f4a2713aSLionel Sambuc NumExpansions = Expansion->getNumExpansions();
917f4a2713aSLionel Sambuc return TemplateArgumentLoc(Pattern, Pattern);
918f4a2713aSLionel Sambuc }
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc case TemplateArgument::TemplateExpansion:
921f4a2713aSLionel Sambuc Ellipsis = OrigLoc.getTemplateEllipsisLoc();
922f4a2713aSLionel Sambuc NumExpansions = Argument.getNumTemplateExpansions();
923f4a2713aSLionel Sambuc return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
924f4a2713aSLionel Sambuc OrigLoc.getTemplateQualifierLoc(),
925f4a2713aSLionel Sambuc OrigLoc.getTemplateNameLoc());
926f4a2713aSLionel Sambuc
927f4a2713aSLionel Sambuc case TemplateArgument::Declaration:
928f4a2713aSLionel Sambuc case TemplateArgument::NullPtr:
929f4a2713aSLionel Sambuc case TemplateArgument::Template:
930f4a2713aSLionel Sambuc case TemplateArgument::Integral:
931f4a2713aSLionel Sambuc case TemplateArgument::Pack:
932f4a2713aSLionel Sambuc case TemplateArgument::Null:
933f4a2713aSLionel Sambuc return TemplateArgumentLoc();
934f4a2713aSLionel Sambuc }
935f4a2713aSLionel Sambuc
936f4a2713aSLionel Sambuc llvm_unreachable("Invalid TemplateArgument Kind!");
937f4a2713aSLionel Sambuc }
938*0a6a1f1dSLionel Sambuc
CheckFoldOperand(Sema & S,Expr * E)939*0a6a1f1dSLionel Sambuc static void CheckFoldOperand(Sema &S, Expr *E) {
940*0a6a1f1dSLionel Sambuc if (!E)
941*0a6a1f1dSLionel Sambuc return;
942*0a6a1f1dSLionel Sambuc
943*0a6a1f1dSLionel Sambuc E = E->IgnoreImpCasts();
944*0a6a1f1dSLionel Sambuc if (isa<BinaryOperator>(E) || isa<AbstractConditionalOperator>(E)) {
945*0a6a1f1dSLionel Sambuc S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
946*0a6a1f1dSLionel Sambuc << E->getSourceRange()
947*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(E->getLocStart(), "(")
948*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(E->getLocEnd(), ")");
949*0a6a1f1dSLionel Sambuc }
950*0a6a1f1dSLionel Sambuc }
951*0a6a1f1dSLionel Sambuc
ActOnCXXFoldExpr(SourceLocation LParenLoc,Expr * LHS,tok::TokenKind Operator,SourceLocation EllipsisLoc,Expr * RHS,SourceLocation RParenLoc)952*0a6a1f1dSLionel Sambuc ExprResult Sema::ActOnCXXFoldExpr(SourceLocation LParenLoc, Expr *LHS,
953*0a6a1f1dSLionel Sambuc tok::TokenKind Operator,
954*0a6a1f1dSLionel Sambuc SourceLocation EllipsisLoc, Expr *RHS,
955*0a6a1f1dSLionel Sambuc SourceLocation RParenLoc) {
956*0a6a1f1dSLionel Sambuc // LHS and RHS must be cast-expressions. We allow an arbitrary expression
957*0a6a1f1dSLionel Sambuc // in the parser and reduce down to just cast-expressions here.
958*0a6a1f1dSLionel Sambuc CheckFoldOperand(*this, LHS);
959*0a6a1f1dSLionel Sambuc CheckFoldOperand(*this, RHS);
960*0a6a1f1dSLionel Sambuc
961*0a6a1f1dSLionel Sambuc // [expr.prim.fold]p3:
962*0a6a1f1dSLionel Sambuc // In a binary fold, op1 and op2 shall be the same fold-operator, and
963*0a6a1f1dSLionel Sambuc // either e1 shall contain an unexpanded parameter pack or e2 shall contain
964*0a6a1f1dSLionel Sambuc // an unexpanded parameter pack, but not both.
965*0a6a1f1dSLionel Sambuc if (LHS && RHS &&
966*0a6a1f1dSLionel Sambuc LHS->containsUnexpandedParameterPack() ==
967*0a6a1f1dSLionel Sambuc RHS->containsUnexpandedParameterPack()) {
968*0a6a1f1dSLionel Sambuc return Diag(EllipsisLoc,
969*0a6a1f1dSLionel Sambuc LHS->containsUnexpandedParameterPack()
970*0a6a1f1dSLionel Sambuc ? diag::err_fold_expression_packs_both_sides
971*0a6a1f1dSLionel Sambuc : diag::err_pack_expansion_without_parameter_packs)
972*0a6a1f1dSLionel Sambuc << LHS->getSourceRange() << RHS->getSourceRange();
973*0a6a1f1dSLionel Sambuc }
974*0a6a1f1dSLionel Sambuc
975*0a6a1f1dSLionel Sambuc // [expr.prim.fold]p2:
976*0a6a1f1dSLionel Sambuc // In a unary fold, the cast-expression shall contain an unexpanded
977*0a6a1f1dSLionel Sambuc // parameter pack.
978*0a6a1f1dSLionel Sambuc if (!LHS || !RHS) {
979*0a6a1f1dSLionel Sambuc Expr *Pack = LHS ? LHS : RHS;
980*0a6a1f1dSLionel Sambuc assert(Pack && "fold expression with neither LHS nor RHS");
981*0a6a1f1dSLionel Sambuc if (!Pack->containsUnexpandedParameterPack())
982*0a6a1f1dSLionel Sambuc return Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
983*0a6a1f1dSLionel Sambuc << Pack->getSourceRange();
984*0a6a1f1dSLionel Sambuc }
985*0a6a1f1dSLionel Sambuc
986*0a6a1f1dSLionel Sambuc BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Operator);
987*0a6a1f1dSLionel Sambuc return BuildCXXFoldExpr(LParenLoc, LHS, Opc, EllipsisLoc, RHS, RParenLoc);
988*0a6a1f1dSLionel Sambuc }
989*0a6a1f1dSLionel Sambuc
BuildCXXFoldExpr(SourceLocation LParenLoc,Expr * LHS,BinaryOperatorKind Operator,SourceLocation EllipsisLoc,Expr * RHS,SourceLocation RParenLoc)990*0a6a1f1dSLionel Sambuc ExprResult Sema::BuildCXXFoldExpr(SourceLocation LParenLoc, Expr *LHS,
991*0a6a1f1dSLionel Sambuc BinaryOperatorKind Operator,
992*0a6a1f1dSLionel Sambuc SourceLocation EllipsisLoc, Expr *RHS,
993*0a6a1f1dSLionel Sambuc SourceLocation RParenLoc) {
994*0a6a1f1dSLionel Sambuc return new (Context) CXXFoldExpr(Context.DependentTy, LParenLoc, LHS,
995*0a6a1f1dSLionel Sambuc Operator, EllipsisLoc, RHS, RParenLoc);
996*0a6a1f1dSLionel Sambuc }
997*0a6a1f1dSLionel Sambuc
BuildEmptyCXXFoldExpr(SourceLocation EllipsisLoc,BinaryOperatorKind Operator)998*0a6a1f1dSLionel Sambuc ExprResult Sema::BuildEmptyCXXFoldExpr(SourceLocation EllipsisLoc,
999*0a6a1f1dSLionel Sambuc BinaryOperatorKind Operator) {
1000*0a6a1f1dSLionel Sambuc // [temp.variadic]p9:
1001*0a6a1f1dSLionel Sambuc // If N is zero for a unary fold-expression, the value of the expression is
1002*0a6a1f1dSLionel Sambuc // * -> 1
1003*0a6a1f1dSLionel Sambuc // + -> int()
1004*0a6a1f1dSLionel Sambuc // & -> -1
1005*0a6a1f1dSLionel Sambuc // | -> int()
1006*0a6a1f1dSLionel Sambuc // && -> true
1007*0a6a1f1dSLionel Sambuc // || -> false
1008*0a6a1f1dSLionel Sambuc // , -> void()
1009*0a6a1f1dSLionel Sambuc // if the operator is not listed [above], the instantiation is ill-formed.
1010*0a6a1f1dSLionel Sambuc //
1011*0a6a1f1dSLionel Sambuc // Note that we need to use something like int() here, not merely 0, to
1012*0a6a1f1dSLionel Sambuc // prevent the result from being a null pointer constant.
1013*0a6a1f1dSLionel Sambuc QualType ScalarType;
1014*0a6a1f1dSLionel Sambuc switch (Operator) {
1015*0a6a1f1dSLionel Sambuc case BO_Add:
1016*0a6a1f1dSLionel Sambuc ScalarType = Context.IntTy;
1017*0a6a1f1dSLionel Sambuc break;
1018*0a6a1f1dSLionel Sambuc case BO_Mul:
1019*0a6a1f1dSLionel Sambuc return ActOnIntegerConstant(EllipsisLoc, 1);
1020*0a6a1f1dSLionel Sambuc case BO_Or:
1021*0a6a1f1dSLionel Sambuc ScalarType = Context.IntTy;
1022*0a6a1f1dSLionel Sambuc break;
1023*0a6a1f1dSLionel Sambuc case BO_And:
1024*0a6a1f1dSLionel Sambuc return CreateBuiltinUnaryOp(EllipsisLoc, UO_Minus,
1025*0a6a1f1dSLionel Sambuc ActOnIntegerConstant(EllipsisLoc, 1).get());
1026*0a6a1f1dSLionel Sambuc case BO_LOr:
1027*0a6a1f1dSLionel Sambuc return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_false);
1028*0a6a1f1dSLionel Sambuc case BO_LAnd:
1029*0a6a1f1dSLionel Sambuc return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_true);
1030*0a6a1f1dSLionel Sambuc case BO_Comma:
1031*0a6a1f1dSLionel Sambuc ScalarType = Context.VoidTy;
1032*0a6a1f1dSLionel Sambuc break;
1033*0a6a1f1dSLionel Sambuc
1034*0a6a1f1dSLionel Sambuc default:
1035*0a6a1f1dSLionel Sambuc return Diag(EllipsisLoc, diag::err_fold_expression_empty)
1036*0a6a1f1dSLionel Sambuc << BinaryOperator::getOpcodeStr(Operator);
1037*0a6a1f1dSLionel Sambuc }
1038*0a6a1f1dSLionel Sambuc
1039*0a6a1f1dSLionel Sambuc return new (Context) CXXScalarValueInitExpr(
1040*0a6a1f1dSLionel Sambuc ScalarType, Context.getTrivialTypeSourceInfo(ScalarType, EllipsisLoc),
1041*0a6a1f1dSLionel Sambuc EllipsisLoc);
1042*0a6a1f1dSLionel Sambuc }
1043